wiki-graph 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +916 -579
- package/dist/cli.js.map +1 -1
- package/dist/data/help/commands/library-predicate.jinja +18 -18
- package/dist/data/help/commands/library.jinja +8 -5
- package/dist/data/help/commands/predicate.jinja +108 -98
- package/dist/data/help/commands/root.jinja +1 -1
- package/dist/data/help/commands/uri.jinja +46 -19
- package/dist/data/help/topics/config.jinja +27 -0
- package/dist/data/help/topics/library.jinja +11 -7
- package/dist/data/help/topics/readiness.jinja +71 -34
- package/dist/data/help/topics/recipe.jinja +11 -8
- package/dist/data/help/topics/uri.jinja +10 -3
- package/dist/gc-worker.js +109 -84
- package/dist/gc-worker.js.map +1 -1
- package/dist/index.cjs +3657 -2517
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +3658 -2504
- package/dist/index.js.map +1 -1
- package/dist/queue-worker.js +571 -355
- package/dist/queue-worker.js.map +1 -1
- package/package.json +3 -3
- package/dist/cli.d.ts +0 -1
- package/dist/gc-worker.d.ts +0 -1
- package/dist/index.d.cts +0 -36
- package/dist/queue-worker.d.ts +0 -1
package/dist/queue-worker.js
CHANGED
|
@@ -3,119 +3,119 @@ import { fileURLToPath as __WIKIGRAPH_FILE_URL_TO_PATH__ } from "url";
|
|
|
3
3
|
import { resolve as __WIKIGRAPH_RESOLVE__ } from "path";
|
|
4
4
|
globalThis.__WIKIGRAPH_CLI_DIST_DIR__ ??= __WIKIGRAPH_RESOLVE__(__WIKIGRAPH_FILE_URL_TO_PATH__(new URL(".", import.meta.url)));
|
|
5
5
|
globalThis.__WIKIGRAPH_DATA_DIR__ ??= __WIKIGRAPH_RESOLVE__(__WIKIGRAPH_FILE_URL_TO_PATH__(new URL("./data", import.meta.url)));
|
|
6
|
-
var
|
|
6
|
+
var vC=Object.defineProperty;var c=(t,e)=>()=>(t&&(e=t(t=0)),e);var qf=(t,e)=>{for(var n in e)vC(t,n,{get:e[n],enumerable:!0})};function cu(t){return TC[t]}function Vn(t){let e=t?.trim().toLowerCase().replaceAll("_","-");if(!(e===void 0||e===""))return _C.get(e)}var TC,_C,nt=c(()=>{"use strict";TC={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"},_C=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 CC}from"crypto";function bi(t){return new uu().run(t)}function PC(t){return"toJSON"in t&&typeof t.toJSON=="function"}var Ko,uu,Jo=c(()=>{"use strict";Ko=Symbol("UNSERIALIZABLE");uu=class{#e=new Set;#t=CC("sha512");#n=new Set;run(e){if(!this.#r(e,""))throw new TypeError("Hash input must be JSON-serializable");return this.#t.digest("hex")}#r(e,n){let r=this.#s(e,n);return r===Ko?!1:(this.#a(r),!0)}#a(e){if(e===null){this.#t.update("null","utf8");return}if(typeof e=="string"){this.#t.update(JSON.stringify(e),"utf8");return}if(typeof e=="number"){this.#t.update(Number.isFinite(e)?String(e):"null","utf8");return}if(typeof e=="boolean"){this.#t.update(e?"true":"false","utf8");return}if(this.#e.has(e))throw new TypeError("Hash input must be JSON-serializable");this.#e.add(e);try{if(Array.isArray(e)){this.#i(e);return}this.#o(e)}finally{this.#e.delete(e)}}#i(e){this.#t.update("[","utf8");for(let[n,r]of e.entries()){n>0&&this.#t.update(",","utf8");let i=this.#s(r,String(n));if(i===Ko){this.#t.update("null","utf8");continue}this.#a(i)}this.#t.update("]","utf8")}#o(e){this.#t.update("{","utf8");let n=!1;for(let r of Object.keys(e)){let i=e[r],a=this.#s(i,r);a!==Ko&&(n&&this.#t.update(",","utf8"),this.#t.update(JSON.stringify(r),"utf8"),this.#t.update(":","utf8"),this.#a(a),n=!0)}this.#t.update("}","utf8")}#s(e,n){if(e===null)return null;if(typeof e=="string"||typeof e=="boolean")return e;if(typeof e=="number")return Number.isFinite(e)?e:null;if(e===void 0||typeof e=="function"||typeof e=="symbol")return Ko;if(typeof e=="bigint")throw new TypeError("Hash input must be JSON-serializable");if(e instanceof String||e instanceof Number||e instanceof Boolean)return this.#s(e.valueOf(),n);if(!PC(e))return e;if(this.#n.has(e))throw new TypeError("Hash input must be JSON-serializable");this.#n.add(e);try{return this.#s(e.toJSON(n),n)}finally{this.#n.delete(e)}}}});function E(t){return t instanceof Error}function ue(t){let e=[],n=new Set,r=t;for(;r!==void 0&&!n.has(r);){if(n.add(r),r instanceof Error){$f(e,AC(r)),r=r.cause;continue}$f(e,String(r));break}return e.join(": ")}function AC(t){let e=t,n=t.message.trim(),r=typeof e.code=="string"&&e.code!==""?e.code:void 0,i=typeof e.path=="string"&&e.path!==""?e.path:void 0;return r==="ENOENT"?i===void 0?"File not found (ENOENT)":`File not found: ${i} (ENOENT)`:r==="EACCES"||r==="EPERM"?i===void 0?`Permission denied (${r})`:`Permission denied: ${i} (${r})`:n===""?r===void 0?t.name:`${t.name} (${r})`:r===void 0?n:`${n} (${r})`}function $f(t,e){let n=e.trim();if(n==="")return;let r=t.at(-1);if(r===void 0){t.push(n);return}if(!(r===n||r.includes(n))){if(n.includes(r)){t[t.length-1]=n;return}t.push(n)}}var T=c(()=>{"use strict"});import{mkdir as hK,readFile as fK,rm as gK,writeFile as yK}from"fs/promises";import{join as SK,resolve as wK}from"path";import{z as Si}from"zod";var LC,kK,Kf=c(()=>{"use strict";Jo();T();LC=1,kK=Si.object({completedAt:Si.string().optional(),startedAt:Si.string(),status:Si.enum(["running","succeeded"]),version:Si.literal(LC)})});var Jf=c(()=>{"use strict";Kf()});import{readFile as WC,writeFile as FC}from"fs/promises";import{join as jC}from"path";var zo,zf=c(()=>{"use strict";T();zo=class{#e;constructor(e){this.#e=e}createEntry(e,n){return{cacheKey:e,response:n}}async read(e){try{return await WC(this.#t(e),"utf8")}catch(n){if(E(n)&&n.code==="ENOENT")return;throw n}}async write(e){await FC(this.#t(e.cacheKey),e.response,"utf8")}#t(e){return jC(this.#e,`${e}.txt`)}}});import{existsSync as GC,mkdirSync as BC,statSync as UC}from"fs";import{resolve as HC}from"path";function lu(t){if(t===void 0)return;let e=HC(t);if(!GC(e))return BC(e,{recursive:!0}),e;if(UC(e).isDirectory())return e}function Qf(t){let e=lu(t);if(e!==void 0)return new zo(e)}var Xf=c(()=>{"use strict";zf()});function Vf(t){let e=[],n=0;for(;n<t.messages.length;){let r=t.messages[n];if(r===void 0||r.role!=="system")break;e.push(r),n+=1}if(e.length===0)return t;if(e.length===1){let[r]=e;return r===void 0?t:{...t,messages:t.messages.slice(n),system:r}}return{...t,messages:t.messages.slice(n),system:e}}var Yf=c(()=>{"use strict"});function Zf(t){return t.some(e=>e.role!=="system"&&(typeof e.content!="string"||e.content.trim()!==""))}function eg(t){let e=["[[Parameters]]:",` model=${t.modelIdentity}`,` temperature=${String(t.resolvedTemperature)}`,` top_p=${String(t.resolvedTopP)}`];return t.modelProvider!==void 0&&e.push(` provider=${t.modelProvider}`),e.push(` model_id=${t.modelId}`),t.scope!==void 0&&e.push(` scope=${t.scope}`),Array.isArray(t.temperature)&&e.push(` temperature_schedule=${JSON.stringify(t.temperature)}`),Array.isArray(t.topP)&&e.push(` top_p_schedule=${JSON.stringify(t.topP)}`),t.retryIndex!==void 0&&t.retryMax!==void 0&&e.push(` retry_progress=${t.retryIndex}/${t.retryMax}`),t.cacheKey!==void 0&&e.push(` cache_key=${t.cacheKey}`),t.sessionId!==void 0&&e.push(` session_id=${t.sessionId}`),`${e.join(`
|
|
7
7
|
`)}
|
|
8
8
|
|
|
9
|
-
`}function
|
|
10
|
-
${t.map(
|
|
11
|
-
${
|
|
9
|
+
`}function tg(t){return`[[Request]]:
|
|
10
|
+
${t.map(n=>`${zC(n.role)}:
|
|
11
|
+
${JC(n.content)}`).join(`
|
|
12
12
|
|
|
13
13
|
`)}
|
|
14
14
|
|
|
15
|
-
`}function
|
|
15
|
+
`}function Yn(t,e){return[qC(t),...e===void 0?[]:[$C(e)]].join("")}function qC(t){return t==="cache-hit"?`[[Usage]]:
|
|
16
16
|
cache-hit
|
|
17
17
|
|
|
18
|
-
`:["[[Usage]]:",`input: ${
|
|
19
|
-
`)}function
|
|
20
|
-
${
|
|
18
|
+
`:["[[Usage]]:",`input: ${mu(t?.inputTokens)}`,`cache: ${mu(t?.inputTokenDetails.cacheReadTokens)}`,`output: ${mu(t?.outputTokens)}`,"",""].join(`
|
|
19
|
+
`)}function mu(t){return t===void 0?"unavailable":String(t)}function $C(t){return`[[Error Stack]]:
|
|
20
|
+
${KC(t)}
|
|
21
21
|
|
|
22
|
-
`}function
|
|
23
|
-
[LLMContext] Failed with ${this.#
|
|
22
|
+
`}function KC(t){return t instanceof Error?t.stack??ue(t):ue(t)}function JC(t){if(typeof t=="string")return t;let e=JSON.stringify(t,null,2);return typeof e=="string"?e:""}function zC(t){return t.length===0?t:`${t[0]?.toUpperCase()??""}${t.slice(1)}`}var ng=c(()=>{"use strict";T()});function rg(t){return typeof t=="string"?{identity:t,modelId:t}:QC(t)?{identity:t.provider===void 0?t.modelId:`${t.provider}:${t.modelId}`,modelId:t.modelId,...t.provider===void 0?{}:{provider:t.provider}}:{identity:"unknown-model",modelId:"unknown-model"}}function QC(t){return typeof t=="object"&&t!==null&&"modelId"in t&&typeof t.modelId=="string"&&(!("provider"in t)||typeof t.provider=="string")}var ig=c(()=>{"use strict"});import{APICallError as ag}from"ai";function og(t){return ag.isInstance(t)?pu(t)?!1:t.isRetryable?!0:eP(t.statusCode):!tP(t)&&nP(t)}function pu(t){return ag.isInstance(t)&&t.statusCode===402}function eP(t){return typeof t=="number"&&VC.has(t)}function tP(t){return sg(t,e=>XC.has(e.name))}function nP(t){return sg(t,e=>{let n=e,r=typeof n.code=="string"?n.code.toUpperCase():void 0;if(r!==void 0&&YC.has(r))return!0;let i=e.message.toLowerCase();return ZC.some(a=>i.includes(a))})}function sg(t,e){let n=new Set,r=t;for(;r instanceof Error&&!n.has(r);){if(e(r))return!0;n.add(r),r=r.cause}return!1}var XC,VC,YC,ZC,dg=c(()=>{"use strict";XC=new Set(["AbortError","ResponseAborted","TimeoutError"]),VC=new Set([408,409,425,429,500,502,503,504,520,522,524,529]),YC=new Set(["ECONNRESET","ECONNREFUSED","EAI_AGAIN","ETIMEDOUT","EPIPE","UND_ERR_CONNECT_TIMEOUT","UND_ERR_HEADERS_TIMEOUT","UND_ERR_SOCKET"]),ZC=["connection","terminated","timeout","network","rate limit"]});import{AsyncLocalStorage as rP}from"async_hooks";import{randomUUID as iP}from"crypto";import{existsSync as aP,mkdirSync as Qo}from"fs";import{dirname as oP,join as Zn,resolve as hu}from"path";import lg,{multistream as sP}from"pino";import cg from"pino-pretty";async function Vo(t,e){let n=t.logDirPath===void 0?void 0:hu(t.logDirPath),r=uP(),i=n===void 0?void 0:Zn(n,r),a=i===void 0?void 0:Zn(i,"artifacts");i!==void 0&&Qo(i,{recursive:!0});let o=cP({operation:t.operation,runId:r,verbose:t.verbose??!1,...i===void 0?{}:{eventLogPath:Zn(i,"run.log")}});return await Xo.run({artifactCounters:new Map,logger:o,runId:r,...a===void 0?{}:{artifactRootDirPath:a},...n===void 0?{}:{rootLogDirPath:n}},e)}function Ce(t){let e=Xo.getStore()?.logger??mg;return t===void 0?e:e.child(t)}function fu(t){if(t.logDirPath===void 0)return;let e=hu(t.logDirPath),n=Xo.getStore();if(n?.rootLogDirPath===e&&n.artifactRootDirPath!==void 0){let r=Zn(n.artifactRootDirPath,t.category);return Qo(r,{recursive:!0}),Zn(r,t.fileName)}return Qo(e,{recursive:!0}),Zn(e,t.fileName)}function Yo(t){if(t.logDirPath===void 0)return;let e=hu(t.logDirPath),n=Xo.getStore(),r=t.extension??".log",i=n?.artifactCounters??dP,a=`${e}:${t.category}:${t.prefix}:${r}`,o=i.get(a),s=o===void 0?1:o+1;for(let d=s;;d+=1){let u=t.alwaysNumbered===!0?`${t.prefix}-${d}${r}`:d===1?`${t.prefix}${r}`:`${t.prefix}-${d}${r}`,l=fu({category:t.category,fileName:u,logDirPath:t.logDirPath});if(l===void 0)return;if(!aP(l))return i.set(a,d),l}}function cP(t){let e=[];return t.eventLogPath!==void 0&&(Qo(oP(t.eventLogPath),{recursive:!0}),e.push({level:"info",stream:cg(ug(t.eventLogPath))})),t.verbose&&e.push({level:"info",stream:cg(ug(process.stderr))}),e.length===0?mg:lg({base:null,level:"info"},sP(e)).child({operation:t.operation,runId:t.runId})}function ug(t){return{colorize:!1,destination:t,ignore:"pid,hostname,operation,runId,component,scope,sessionId",mkdir:typeof t=="string",singleLine:!0,sync:!0,translateTime:"SYS:yyyy-mm-dd HH:MM:ss"}}function uP(){let t=new Date,e=String(t.getUTCFullYear()),n=wi(t.getUTCMonth()+1),r=wi(t.getUTCDate()),i=wi(t.getUTCHours()),a=wi(t.getUTCMinutes()),o=wi(t.getUTCSeconds());return`${e}${n}${r}-${i}${a}${o}-${iP().slice(0,8)}`}function wi(t){return String(t).padStart(2,"0")}var Xo,dP,mg,Pe=c(()=>{"use strict";Xo=new rP,dP=new Map,mg=lg({enabled:!1})});import{readFileSync as lP,statSync as pg}from"fs";import{resolve as fg,sep as hg}from"path";import{Environment as mP,Loader as pP}from"nunjucks";function Ei(t,e={}){return new mP(new gu(fg(t)),{autoescape:e.autoescape??!0,trimBlocks:e.trimBlocks??!0})}function yP(t){if(fP.test(t))throw new Error(`invalid path ${t}`);return`${t.replace(gP,"").replace(hP,"")}.jinja`}function bP(t,e,n){let r=t.endsWith(hg)?t:`${t}${hg}`;if(!(e===t||e.startsWith(r)))throw new Error(`cannot find ${n}`)}var hP,fP,gP,gu,Zo=c(()=>{"use strict";hP=/\.jinja$/i,fP=/^\.+\//,gP=/^\/+/;gu=class extends pP{#e;constructor(e){super(),this.#e=e}getSource(e){let n=yP(e),r=fg(this.#e,n);bP(this.#e,r,n);let i=lP(r,"utf8"),a=pg(r).mtimeMs;return{noCache:!1,path:r,src:i,upToDate:()=>pg(r).mtimeMs===a}}}});var Ae,Ii=c(()=>{"use strict";Ae=class{#e=0;#t;#n=[];constructor(e){if(!Number.isInteger(e)||e<1)throw new RangeError("concurrency must be a positive integer");this.#t=e}async use(e){await this.#r();try{return await e()}finally{this.#a()}}async#r(){if(this.#e<this.#t){this.#e+=1;return}await new Promise(e=>{this.#n.push(()=>{this.#e+=1,e()})})}#a(){this.#e-=1,this.#n.shift()?.()}}});var xi,yu=c(()=>{"use strict";Pe();xi=class{#e;#t=new Map;#n;#r=[];#a=!1;sessionId;constructor(e,n,r){this.sessionId=e,this.#e=r,this.#n=n}async request(e,n={}){return this.#i(),await this.#n({logFiles:this.#r,messages:e,options:n,pendingCacheEntries:this.#t})}async commit(){if(!this.#a){for(let e of this.#t.values())this.#e!==void 0&&await this.#e.write(e);this.#t.clear(),this.#a=!0}}rollback(){return this.#a||(this.#t.clear(),this.#a=!0),Promise.resolve()}async run(e){try{let n=await e(this);return await this.commit(),n}catch(n){throw await this.rollback(),this.#o(),n}}#i(){if(this.#a)throw new Error("LLMContext is already finalized")}#o(){if(this.#r.length===0)return;let e=Ce({component:"llm-context",sessionId:this.sessionId});e.warn(`
|
|
23
|
+
[LLMContext] Failed with ${this.#r.length} log file(s):`);for(let[n,r]of this.#r.entries())e.warn(` ${n+1}. ${r}`)}}});var er,bu=c(()=>{"use strict";er=class extends Error{isRetryable=!1;statusCode=402;constructor(e="LLM payment required.",n){super(e,n),this.name="LLMPaymentRequiredError"}}});import{appendFile as SP}from"fs/promises";function gg(t){return t===void 0?new es:new es(Yo({alwaysNumbered:!0,category:"llm",logDirPath:t,prefix:"request"}))}var es,yg=c(()=>{"use strict";Pe();es=class{#e;constructor(e){this.#e=e}get filePath(){return this.#e}async append(e){this.#e!==void 0&&await SP(this.#e,e,"utf8")}}});function ts(t,e,n,r){if(t===void 0)return;if(typeof t=="number")return t;if(t.length===1)return t[0];if(t.length!==2)throw new RangeError(`${e} must be a number or a 2-item range like [0.6, 0.98]`);let[i,a]=t;if(i===void 0||a===void 0)throw new RangeError(`${e} must be a number or a 2-item range like [0.6, 0.98]`);if(n===void 0||r===void 0||r<=0)return i;let s=Math.min(Math.max(n,0),r)/r;return i+(a-i)*s}function Su(t,e,n,r){if(t===void 0)return{temperature:n,topP:r};if(e===void 0)return{temperature:n,topP:r};let i=e[t];return i===void 0?{temperature:n,topP:r}:{temperature:i.temperature??n,topP:i.topP??r}}var wu=c(()=>{"use strict"});import{setTimeout as wP}from"timers/promises";import{generateText as EP,streamText as IP}from"ai";var xP,kP,bg,vt,Sg=c(()=>{"use strict";Xf();Yf();ng();ig();dg();Pe();Zo();Ii();Jo();T();yu();bu();yg();wu();xP=36e4,kP=6,bg=0,vt=class{#e;#t;#n;#r;#a;#i;#o;#s;#d;#c;#l;#u;#p;#m;#h;#f;#b;#g;config;constructor(e){let n=e.concurrent??kP,r=e.timeout??xP,i=e.temperature??.6,a=e.topP??.6,o=e.sampling,s=e.stream??!1,d=rg(e.model);this.config=Object.freeze({concurrent:n,modelId:d.modelId,temperature:i,stream:s,timeout:r,topP:a,...d.provider===void 0?{}:{provider:d.provider},...o===void 0?{}:{sampling:o}}),this.#e=Qf(e.cacheDirPath),this.#t=new Ae(n),this.#n=lu(e.logDirPath),this.#r=e.model,this.#a=d.provider,this.#i=d.modelId,this.#o=d.identity,this.#s=e.onStreamProgress,this.#d=e.onTokenUsage,this.#c=new Ae(n),this.#l=e.retryIntervalSeconds??6,this.#u=e.retryTimes??5,this.#p=o,this.#m=s,this.#h=Ei(e.dataDirPath),this.#f=i,this.#b=r,this.#g=a}context(){bg+=1;let e=bg;return new xi(e,async n=>await this.#S({...n.options,messages:n.messages,sessionId:e,...n.logFiles===void 0?{}:{logFiles:n.logFiles},...n.pendingCacheEntries===void 0?{}:{pendingCacheEntries:n.pendingCacheEntries}}),this.#e)}async withContext(e){return await this.context().run(e)}async request(e,n={}){return typeof e=="function"?await this.#t.use(async()=>await e(this.#y)):await this.#y(e,n)}#y=async(e,n={})=>await this.#S({messages:e,...n});loadSystemPrompt(e,n={}){return this.#h.render(e,n)}async#S(e){let n=Su(e.scope,this.#p,this.#f,this.#g),r=e.temperature??n.temperature,i=e.topP??n.topP,a=ts(r,"temperature",e.retryIndex,e.retryMax),o=ts(i,"top_p",e.retryIndex,e.retryMax),s=(e.useCache??!0)&&Zf(e.messages),d=this.#e!==void 0&&s?bi({messages:e.messages.map(h=>({content:h.content,role:h.role})),modelId:this.#i,provider:this.#a??null,temperature:a??null,topP:o??null}):void 0,u=gg(this.#n);if(u.filePath!==void 0&&e.logFiles!==void 0&&e.logFiles.push(u.filePath),await u.append(eg({cacheKey:d,modelId:this.#i,modelIdentity:this.#o,modelProvider:this.#a,resolvedTemperature:a,resolvedTopP:o,retryIndex:e.retryIndex,retryMax:e.retryMax,scope:e.scope,sessionId:e.sessionId,temperature:r,topP:i})),await u.append(tg(e.messages)),d!==void 0&&this.#e!==void 0&&s){let h=await this.#e.read(d);if(h!==void 0)return Ce({component:"llm",scope:e.scope,sessionId:e.sessionId}).info(`[Cache Hit] Using cached response (key: ${d.slice(0,12)}...)`),await u.append(`[[Response]] (from cache):
|
|
24
24
|
${h}
|
|
25
25
|
|
|
26
|
-
`),await u.append(
|
|
26
|
+
`),await u.append(Yn("cache-hit")),h}let l,m,p;for(let h=0;h<=this.#u;h+=1)try{l=await this.#c.use(async()=>{let y=Vf({maxRetries:0,messages:[...e.messages],model:this.#r,timeout:this.#b});if(e.signal!==void 0&&(y.abortSignal=e.signal),a!==void 0&&(y.temperature=a),o!==void 0&&(y.topP=o),this.#m){let f=[],I=IP(y);for await(let v of I.textStream)f.push(v),await this.#w(v.length);return m=await I.totalUsage,await this.#E(m),f.join("")}else{let f=await EP(y);return m=f.usage,await this.#w(f.text.length),await this.#E(m),f.text}}),await u.append(`[[Response]]:
|
|
27
27
|
${l}
|
|
28
28
|
|
|
29
|
-
`);break}catch(
|
|
30
|
-
${
|
|
29
|
+
`);break}catch(y){if(p=y,pu(y)){let f=new er(void 0,{cause:y});throw await u.append(`[[Error]]:
|
|
30
|
+
${ue(f)}
|
|
31
31
|
|
|
32
|
-
`),await u.append(
|
|
33
|
-
${
|
|
32
|
+
`),await u.append(Yn(m,y)),f}if(!og(y))throw await u.append(`[[Error]]:
|
|
33
|
+
${ue(y)}
|
|
34
34
|
|
|
35
|
-
`),await u.append(
|
|
35
|
+
`),await u.append(Yn(m,y)),y;await u.append(`[[Warning]]:
|
|
36
36
|
Request failed with connection error, retrying... (${h+1} times)
|
|
37
37
|
|
|
38
|
-
`),h<this.#
|
|
38
|
+
`),h<this.#u&&this.#l>0&&await wP(this.#l*1e3,void 0,{signal:e.signal})}if(l===void 0){let h=p===void 0?`LLM request failed after ${this.#u+1} attempts`:`LLM request failed after ${this.#u+1} attempts: ${ue(p)}`;throw await u.append(`[[Error]]:
|
|
39
39
|
${h}
|
|
40
40
|
|
|
41
|
-
`),await u.append(
|
|
41
|
+
`),await u.append(Yn(m,p)),new Error(h,{...p===void 0?{}:{cause:p}})}if(d!==void 0&&this.#e!==void 0&&s){let h=this.#e.createEntry(d,l);e.sessionId!==void 0&&e.pendingCacheEntries!==void 0?e.pendingCacheEntries.set(h.cacheKey,h):await this.#e.write(h)}return await u.append(Yn(m)),l}async#w(e){if(this.#s!==void 0)try{await this.#s({outputCharacters:e})}catch{return}}async#E(e){if(!(this.#d===void 0||e===void 0))try{await this.#d({...e.inputTokenDetails.cacheReadTokens===void 0?{}:{cacheReadTokens:e.inputTokenDetails.cacheReadTokens},...e.inputTokens===void 0?{}:{inputTokens:e.inputTokens},...e.outputTokens===void 0?{}:{outputTokens:e.outputTokens}})}catch{return}}}});var wg=c(()=>{"use strict";Sg()});var Eu=c(()=>{"use strict";wg();yu();bu();wu()});var RP,ki,Iu,Ri=c(()=>{"use strict";RP=Object.freeze(["serial-generation/editor-compress","serial-generation/editor-review","serial-generation/editor-review-guide","serial-generation/reader-choice","serial-generation/reader-extraction"]),ki=Object.freeze({compress:"serial-generation/editor-compress",review:"serial-generation/editor-review",reviewGuide:"serial-generation/editor-review-guide"}),Iu=Object.freeze({choice:"serial-generation/reader-choice",extraction:"serial-generation/reader-extraction"})});var Eg=c(()=>{"use strict"});import{existsSync as xg,statSync as kg}from"fs";import{fileURLToPath as vP}from"url";import{dirname as Rg,join as Ig,parse as TP,resolve as xu}from"path";function en(){let t=globalThis.__WIKIGRAPH_DATA_DIR__;if(typeof t=="string"&&t!=="")return t;let e=_P();return e!==void 0?e:CP()}function _P(){let t=Rg(vP(import.meta.url));for(let e of[xu(t,"../../../data"),xu(t,"../../data"),xu(t,"../data")])if(xg(e)&&kg(e).isDirectory())return e}function CP(){let t=process.cwd(),e=TP(t).root;for(;;){for(let n of[Ig(t,"data"),Ig(t,"packages","core","data")])if(xg(n)&&kg(n).isDirectory())return n;if(t===e)throw new Error("Could not locate data directory");t=Rg(t)}}var ns=c(()=>{"use strict"});import{AsyncLocalStorage as PP}from"async_hooks";import{stat as AP}from"fs/promises";import{resolve as LP}from"path";async function NP(t){let e=await AP(t).catch(n=>{if(!(typeof n=="object"&&n!==null&&"code"in n&&n.code==="ENOENT"))throw n});return e===void 0||e.size===0}function g(t,e){let n=t[e];if(typeof n!="number")throw new TypeError(`Expected ${e} to be a number`);return n}function w(t,e){let n=t[e];if(typeof n!="string")throw new TypeError(`Expected ${e} to be a string`);return n}function ye(t,e){let n=t[e];if(n!=null){if(typeof n!="string")throw new TypeError(`Expected ${e} to be a string`);return n}}async function DP(t,e={}){let n=await MP(),r=(e.readonly===!0?n.OPEN_READONLY:n.OPEN_READWRITE|n.OPEN_CREATE)|n.OPEN_FULLMUTEX;return await new Promise((i,a)=>{let o=new n.Database(t,r,s=>{if(s!==null){a(s);return}i(o)})})}async function MP(){let t=await import("sqlite3");return WP(t)}function WP(t){if(typeof t=="object"&&t!==null&&"default"in t&&typeof t.default=="object"&&t.default!==null&&"Database"in t.default)return t.default;if(typeof t=="object"&&t!==null&&"Database"in t)return t;throw new TypeError("Could not load sqlite3")}function ku(t){return t??[]}var OP,O,k=c(()=>{"use strict";OP=900*1e3;O=class t{#e;#t;#n=new PP;#r;#a=!1;#i=Promise.resolve();#o=0;constructor(e,n={}){this.#e=e,this.#t=n.onWrite}static async open(e,n="",r={}){let i=LP(e),a=r.readonly!==!0&&n.trim()!==""&&await NP(i),o=await DP(i,r),s=new t(o,r);return await s.#u(`PRAGMA busy_timeout = ${OP}`),r.readonly!==!0&&n.trim()!==""&&(await s.#u(n),a&&s.#c()),s}static async initialize(e,n){let r=await t.open(e);try{n.trim()!==""&&await r.#u(n)}finally{await r.close()}}async queryAll(e,n,r){return await this.#d(async()=>(this.#s(),(await this.#p(e,n)).map(r)))}async queryOne(e,n,r){return await this.#d(async()=>{this.#s();let i=await this.#m(e,n);return i===void 0?void 0:r(i)})}async run(e,n){await this.#d(async()=>{this.#s(),await this.#h(e,n),this.#c()})}async transaction(e){return await this.#d(async()=>{this.#s();let n=this.#o===0,r=this.#r??Symbol("database transaction scope");n&&(await this.#u("BEGIN IMMEDIATE"),this.#r=r),this.#o+=1;try{let i=await this.#n.run(r,e);return n&&await this.#u("COMMIT"),i}catch(i){throw n&&await this.#u("ROLLBACK"),i}finally{this.#o-=1,n&&(this.#r=void 0)}})}async flush(){await this.#d(()=>{this.#s()})}async close(){await this.#d(async()=>{this.#a||(await this.#l(),this.#a=!0)})}async getLastInsertRowId(){let e=await this.queryOne("SELECT last_insert_rowid() AS row_id",void 0,n=>g(n,"row_id"));if(e===void 0)throw new Error("Could not read last_insert_rowid()");return e}#s(){if(this.#a)throw new Error("Database is already closed")}async#d(e){let n=this.#n.getStore();if(n!==void 0&&n===this.#r)return await e();let r=this.#i.then(e);return this.#i=r.then(()=>{},()=>{}),await r}#c(){this.#t?.()}async#l(){await new Promise((e,n)=>{this.#e.close(r=>{if(r!==null){n(r);return}e()})})}async#u(e){await new Promise((n,r)=>{this.#e.exec(e,i=>{if(i!==null){r(i);return}n()})})}async#p(e,n){return await new Promise((r,i)=>{this.#e.all(e,ku(n),(a,o)=>{if(a!==null){i(a);return}r(o)})})}async#m(e,n){return await new Promise((r,i)=>{this.#e.get(e,ku(n),(a,o)=>{if(a!==null){i(a);return}r(o)})})}async#h(e,n){await new Promise((r,i)=>{this.#e.run(e,ku(n),a=>{if(a!==null){i(a);return}r()})})}}});import{AsyncLocalStorage as Tg}from"async_hooks";import{homedir as FP}from"os";import{join as tn,resolve as vg}from"path";function rt(){let t=jP.getStore()?.path;if(t!==void 0&&t.trim()!=="")return vg(t);let e=_g.getStore()?.path;return e!==void 0&&e.trim()!==""?vg(e):tn(FP(),".wikigraph")}async function Ru(t,e){return await _g.run({path:t},e)}function le(){return tn(rt(),"core.sqlite")}function nn(){return tn(rt(),"cache")}function tr(){return tn(nn(),"cache.sqlite")}function nr(){return tn(rt(),"jobs")}function Le(){return tn(rt(),"staging")}function rr(){return tn(rt(),"tmp")}var jP,_g,me=c(()=>{"use strict";jP=new Tg,_g=new Tg});import{mkdir as GP,rm as BP,stat as Ag}from"fs/promises";import{dirname as UP,join as V,resolve as Tu}from"path";async function Re(){let t=le(),e=Tu(t),n=await is(t);if(vu(e,n))return;if(rn!==void 0){await rn;let i=await is(t);if(vu(e,i))return}let r=await is(t);if(rn!==void 0)return await rn,Re();if(!vu(e,r)){rn=(async()=>{if(!await it(t)){await Pg(t,rs),await Cg(e);return}let i=await ss(t);if(i>rs)throw new Error(`Unsupported Wiki Graph home schema version: ${i}.`);i<rs&&(await zP(),await JP(),await YP(),await Pg(t,rs)),await Cg(e)})();try{await rn;return}finally{rn=void 0}}}async function Cg(t){let e=await is(t);if(e===void 0){vi=void 0;return}vi={fingerprint:e,path:t}}async function is(t){try{let e=await Ag(t);return{dev:e.dev,ino:e.ino,mtimeMs:e.mtimeMs,size:e.size}}catch(e){if(E(e)&&e.code==="ENOENT")return;throw e}}function vu(t,e){return e===void 0||vi===void 0?!1:vi.path===t&&$P(vi.fingerprint,e)}function $P(t,e){return t.dev===e.dev&&t.ino===e.ino&&t.mtimeMs===e.mtimeMs&&t.size===e.size}async function ss(t=le()){if(!await it(t))return 0;let e=await O.open(t,"",{readonly:!0});try{return await qe(e,"schema_versions")?await e.queryOne("SELECT version FROM schema_versions WHERE scope = ?",["home"],r=>Number(r.version))??1:1}finally{await e.close()}}function Lg(t){let e=Tu(t);return KP().some(n=>e===Tu(n))}function KP(){let t=nn();return[le(),V(t,"search-sessions.sqlite"),V(t,"continuation-cursors.sqlite"),tr(),V(nr(),"job.sqlite"),V(rr(),"gc.sqlite"),V(Le(),"staging.sqlite")]}async function Pg(t,e){await GP(UP(t),{recursive:!0});let n=await O.open(t);try{await n.run(qP),await n.run(`
|
|
42
42
|
INSERT INTO schema_versions (scope, version, updated_at)
|
|
43
43
|
VALUES (?, ?, ?)
|
|
44
44
|
ON CONFLICT(scope) DO UPDATE SET
|
|
45
45
|
version = excluded.version,
|
|
46
46
|
updated_at = excluded.updated_at
|
|
47
|
-
`,["home",e,new Date().toISOString()])}finally{await
|
|
47
|
+
`,["home",e,new Date().toISOString()])}finally{await n.close()}}async function JP(){let t=nn(),e=Le(),n=nr(),r=rr();await He(V(t,"search-sessions.sqlite")),await He(V(t,"continuation-cursors.sqlite")),await He(tr()),await He(V(e,"library")),await He(V(r,"gc.sqlite")),await He(V(r,"gc.last-run")),await He(V(n,"cache")),await He(V(n,"job.sqlite"));let i=V(e,"staging.sqlite");await it(i)&&(await eA(i),await tA(i))}async function zP(){await QP(),await XP(),await VP(),await ZP()}async function QP(){let t=le();if(!await it(t))return;let e=await O.open(t,"",{readonly:!0});try{if(await qe(e,"library_locks")&&(await e.queryAll(`
|
|
48
48
|
SELECT owner_pid, heartbeat_at
|
|
49
49
|
FROM library_locks
|
|
50
|
-
`,void 0,i=>({heartbeatAt:Number(i.heartbeat_at),ownerPid:Number(i.owner_pid)}))).some(i=>
|
|
50
|
+
`,void 0,i=>({heartbeatAt:Number(i.heartbeat_at),ownerPid:Number(i.owner_pid)}))).some(i=>Ti(i.ownerPid,i.heartbeatAt)))throw new Error("Cannot upgrade home with active library locks.");if(!await qe(e,"state_locks"))return;if((await e.queryAll(`
|
|
51
51
|
SELECT owner_pid, heartbeat_at
|
|
52
52
|
FROM state_locks
|
|
53
53
|
WHERE scope = 'library'
|
|
54
|
-
`,void 0,
|
|
54
|
+
`,void 0,r=>({heartbeatAt:Number(r.heartbeat_at),ownerPid:Number(r.owner_pid)}))).some(r=>Ti(r.ownerPid,r.heartbeatAt)))throw new Error("Cannot upgrade home with active library locks.")}finally{await e.close()}}async function XP(){let t=V(rr(),"gc.sqlite");if(!await it(t))return;let e=await O.open(t,"",{readonly:!0});try{if(!await qe(e,"gc_locks"))return;if((await e.queryAll("SELECT owner_pid, heartbeat_at FROM gc_locks",void 0,r=>({heartbeatAt:Number(r.heartbeat_at),ownerPid:Number(r.owner_pid)}))).some(r=>Ti(r.ownerPid,r.heartbeatAt)))throw new Error("Cannot upgrade home with an active GC lock.")}finally{await e.close()}}async function VP(){let t=V(Le(),"staging.sqlite");if(!await it(t))return;let e=await O.open(t,"",{readonly:!0});try{for(let n of["archive_owners","entry_locks","entry_sqlite_leases","archive_commit_locks"]){if(!await qe(e,n))continue;if((await e.queryAll(`
|
|
55
55
|
SELECT owner_pid, heartbeat_at
|
|
56
|
-
FROM ${
|
|
57
|
-
`,void 0,i=>({heartbeatAt:Number(i.heartbeat_at),ownerPid:Number(i.owner_pid)}))).some(i=>
|
|
56
|
+
FROM ${n}
|
|
57
|
+
`,void 0,i=>({heartbeatAt:Number(i.heartbeat_at),ownerPid:Number(i.owner_pid)}))).some(i=>Ti(i.ownerPid,i.heartbeatAt)))throw new Error(`Cannot upgrade home with active coordinator state: ${n}.`)}}finally{await e.close()}}async function YP(){let t=V(Le(),"staging.sqlite");if(!await it(t))return;let e=await O.open(t,"",{readonly:!0});try{if(!await qe(e,"entry_overlays"))return;if((await e.queryAll(`
|
|
58
58
|
SELECT entry_path
|
|
59
59
|
FROM entry_overlays
|
|
60
|
-
`,void 0,i=>String(i.entry_path))).find(i
|
|
60
|
+
`,void 0,i=>String(i.entry_path))).find(i=>!nA(i))!==void 0)throw new Error("Cannot upgrade home with non-derived coordinator overlays.")}finally{await e.close()}}async function ZP(){let t=V(nr(),"job.sqlite");if(!await it(t))return;let e=await O.open(t,"",{readonly:!0});try{if(await qe(e,"build_jobs")&&(await e.queryAll(`
|
|
61
61
|
SELECT job_id
|
|
62
62
|
FROM build_jobs
|
|
63
63
|
WHERE state IN ('queued', 'running', 'canceling', 'paused')
|
|
64
|
-
`,void 0,
|
|
64
|
+
`,void 0,r=>String(r.job_id))).length>0)throw new Error("Cannot upgrade home with active build jobs.");if(await qe(e,"build_worker_lease")){let n=await e.queryOne(`
|
|
65
65
|
SELECT owner_pid, heartbeat_at
|
|
66
66
|
FROM build_worker_lease
|
|
67
67
|
WHERE id = 1
|
|
68
|
-
`,void 0,
|
|
68
|
+
`,void 0,r=>({heartbeatAt:r.heartbeat_at===null?void 0:Number(r.heartbeat_at),ownerPid:r.owner_pid===null?void 0:Number(r.owner_pid)}));if(n?.ownerPid!==void 0&&n.heartbeatAt!==void 0&&Ti(n.ownerPid,n.heartbeatAt))throw new Error("Cannot upgrade home with an active build worker lease.")}}finally{await e.close()}}async function eA(t,e){let n=await O.open(t);try{if(!await qe(n,"entry_overlays"))return;let r=e===void 0?"WHERE entry_path IN (?, ?)":"WHERE archive_key = ? AND entry_path IN (?, ?)",i=e===void 0?[as,os]:[e,as,os],a=await n.queryAll(`
|
|
69
69
|
SELECT archive_key, workspace_path
|
|
70
70
|
FROM entry_overlays
|
|
71
|
-
${
|
|
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
|
|
71
|
+
${r}
|
|
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 He(o.workspacePath);await n.run(`
|
|
73
73
|
DELETE FROM entry_overlays
|
|
74
|
-
${
|
|
75
|
-
`,i)}finally{await
|
|
74
|
+
${r}
|
|
75
|
+
`,i)}finally{await n.close()}}async function tA(t){let e=await O.open(t);try{if(!await qe(e,"entry_overlays"))return;let n=await e.queryAll(`
|
|
76
76
|
SELECT archive_key, archive_path, entry_path, workspace_path
|
|
77
77
|
FROM entry_overlays
|
|
78
|
-
WHERE entry_path IN (?, ?)
|
|
79
|
-
`,["database.db",
|
|
78
|
+
WHERE entry_path IN (?, ?, ?)
|
|
79
|
+
`,["database.db",as,os],r=>({archiveKey:String(r.archive_key),archivePath:String(r.archive_path),entryPath:String(r.entry_path),workspacePath:r.workspace_path===null?void 0:String(r.workspace_path)}));for(let r of n)await it(r.archivePath)||(r.workspacePath!==void 0&&await He(r.workspacePath),await e.run(`
|
|
80
80
|
DELETE FROM entry_overlays
|
|
81
81
|
WHERE archive_key = ? AND entry_path = ?
|
|
82
|
-
`,[
|
|
82
|
+
`,[r.archiveKey,r.entryPath]))}finally{await e.close()}}function nA(t){return t===as||t===os}async function qe(t,e){return await t.queryOne(`
|
|
83
83
|
SELECT 1 AS present
|
|
84
84
|
FROM sqlite_master
|
|
85
85
|
WHERE type = 'table' AND name = ?
|
|
86
|
-
`,[e],()=>!0)===!0}function
|
|
86
|
+
`,[e],()=>!0)===!0}function Ti(t,e){return Date.now()-e<=HP&&rA(t)}function rA(t){try{return process.kill(t,0),!0}catch{return!1}}async function He(t){await BP(t,{force:!0,recursive:!0})}async function it(t){try{return await Ag(t),!0}catch{return!1}}var rs,HP,as,os,rn,vi,qP,an=c(()=>{"use strict";me();T();k();rs=3,HP=300*1e3,as="index.db",os="fts.db",qP=`
|
|
87
87
|
CREATE TABLE IF NOT EXISTS schema_versions (
|
|
88
88
|
scope TEXT PRIMARY KEY,
|
|
89
89
|
version INTEGER NOT NULL,
|
|
90
90
|
updated_at TEXT NOT NULL
|
|
91
91
|
);
|
|
92
|
-
`});import{createHash as
|
|
93
|
-
`,{encoding:"utf8",mode:384}),await
|
|
94
|
-
`,{encoding:"utf8",mode:384})}async function
|
|
92
|
+
`});import{createHash as iA}from"crypto";import{chmod as aA,mkdir as Dg,readFile as Mg,rename as oA,rm as Wg,stat as Fg,writeFile as jg}from"fs/promises";import{dirname as sA,resolve as dA}from"path";import{setTimeout as cA}from"timers/promises";async function D(t,e,n={}){return await mA(t),await Gg(t,e),await O.open(t,"",n)}function mA(t){return Lg(t)?Re():Promise.resolve()}async function Gg(t,e){let n=dA(t),r=wA(n),i=IA(e);if(await Ng(n,r,i)){await Tt(n),await Tt(r);return}await Dg(sA(n),{recursive:!0}),await hA(n,async()=>{if(await Ng(n,r,i)){await Tt(n),await Tt(r);return}await O.initialize(n,e),await Tt(n),await pA(r,i)}),await Tt(n),await Tt(r)}async function pA(t,e){let n=`${t}.${process.pid}.tmp`;await jg(n,`${e}
|
|
93
|
+
`,{encoding:"utf8",mode:384}),await oA(n,t),await Tt(t)}async function hA(t,e){let n=`${t}.init.lock`;for(;;)try{await Dg(n,{mode:448});break}catch(r){if(!E(r)||r.code!=="EEXIST")throw r;await fA(n),await cA(uA)}try{await Og(n);let r=setInterval(()=>{Og(n).catch(()=>{})},lA);try{return await e()}finally{clearInterval(r)}}finally{await Wg(n,{force:!0,recursive:!0})}}async function Og(t){await jg(`${t}/owner.json`,`${JSON.stringify({at:Date.now(),pid:process.pid},null,2)}
|
|
94
|
+
`,{encoding:"utf8",mode:384})}async function fA(t){try{let e=await gA(t);if(e!==void 0){if(SA(e.pid)||Date.now()-e.at<_u)return}else if(!await bA(t,_u))return;await Wg(t,{force:!0,recursive:!0})}catch(e){if(E(e)&&e.code==="ENOENT")return;throw e}}async function gA(t){try{let e=JSON.parse(await Mg(`${t}/owner.json`,"utf8"));return yA(e)?e:void 0}catch(e){if(E(e)&&e.code==="ENOENT"||e instanceof SyntaxError)return;throw e}}function yA(t){if(typeof t!="object"||t===null)return!1;let e=t;return typeof e.pid=="number"&&Number.isInteger(e.pid)&&e.pid>0&&typeof e.at=="number"}async function bA(t,e){let n=await Fg(t);return Date.now()-n.mtimeMs>=e}function SA(t){try{return process.kill(t,0),!0}catch(e){return!(E(e)&&e.code==="ESRCH")}}async function Ng(t,e,n){if(!await EA(t))return!1;try{return(await Mg(e,"utf8")).trim()===n}catch(r){if(E(r)&&r.code==="ENOENT")return!1;throw r}}function wA(t){return`${t}.initialized`}async function EA(t){try{return(await Fg(t)).isFile()}catch(e){if(E(e)&&e.code==="ENOENT")return!1;throw e}}async function Tt(t){try{await aA(t,384)}catch(e){if(E(e)&&e.code==="ENOENT")return;throw e}}function IA(t){return iA("sha256").update(t).digest("hex")}var uA,_u,lA,Bg=c(()=>{"use strict";T();an();k();uA=50,_u=300*1e3,lA=Math.floor(_u/2)});import{mkdir as xA,readFile as kA,readdir as RA,rm as vA,writeFile as TA}from"fs/promises";var Ug,Hg=c(()=>{"use strict";T();Ug={deleteTree:async t=>{await vA(t,{force:!0,recursive:!0})},ensureDirectory:async t=>{await xA(t,{recursive:!0})},listFiles:async t=>(await RA(t,{withFileTypes:!0})).filter(e=>e.isFile()).map(e=>e.name),readFile:async t=>{try{return await kA(t)}catch(e){if(E(e)&&e.code==="ENOENT")return;throw e}},writeFile:async(t,e,n)=>{await TA(t,e,{...typeof e=="string"?{encoding:"utf8"}:{},flag:n.overwrite===!0?"w":"wx"})}}});function ir(t){let e=new Set(Object.values(t));return n=>e.has(n)}function _i(t,e){let n=ir(t);return r=>{if(n(r))return r;throw new Error(`Unknown ${e}: ${r}`)}}var Cu=c(()=>{"use strict"});function _A(t){return t.replace(/\s+/gu," ").trim()}var Oe,Pu,Au,$e,Lu,ar,at,Ou,Ke=c(()=>{"use strict";Cu();Oe=(i=>(i.Verbatim="verbatim",i.Detailed="detailed",i.Focused="focused",i.Relevant="relevant",i))(Oe||{}),Pu=ir(Oe),Au=_i(Oe,"chunk retention"),$e=(r=>(r.Critical="critical",r.Important="important",r.Helpful="helpful",r))($e||{}),Lu=ir($e),ar=_i($e,"chunk importance"),at=class{#e;rawText;wordsCount;constructor(e,n){this.rawText=e,this.wordsCount=n}get text(){return this.#e??=_A(this.rawText),this.#e}toJSON(){return{text:this.text,wordsCount:this.wordsCount}}},Ou=["fts","embedding-source","embedding-summary"]});var ds,Nu=c(()=>{"use strict";Ke();ds=class{#e=!1;#t;#n;#r=[];#a;#i;#o="";constructor(e,n,r){this.#t=r.discard,this.#n=r.finalize,this.#a=e,this.#i=n}addSentence(e,n,r){this.#s();let i=this.#i+this.#r.length,a=new at(e,n);return r!==void 0&&Object.assign(a,{byteLength:r.byteLength,byteOffset:r.byteOffset}),this.#r.push(a),[this.#a,i]}async commit(){return this.#s(),this.#e=!0,await this.#n(this.#i,this.#o,this.#r)}async commitWithText(e){return this.#s(),this.#e=!0,await this.#n(this.#i,e,this.#r)}discard(){this.#s(),this.#e=!0,this.#t()}setSummary(e){this.#s(),this.#o=e}get fragmentId(){return this.#i}#s(){if(this.#e)throw new Error("Text stream draft is already finalized")}}});function Ne(t){let e=0,n=!1;for(let r of t.trim()){if(CA.test(r)){e+=1,n=!1;continue}if(PA.test(r)){n||(e+=1),n=!0;continue}n&&AA.test(r)||(n=!1)}return e}var CA,PA,AA,on=c(()=>{"use strict";CA=new RegExp("\\p{Script=Han}","u"),PA=/[\p{Letter}\p{Number}]/u,AA=new RegExp("\\p{Mark}","u")});async function $g(t,e){if(e!==void 0)return await LA(t,e);let n=[];for(let r of OA().segment(t)){let i=r.segment;if(i.trim()==="")continue;let a=new at(i,Ne(i));Object.assign(a,{byteLength:Buffer.byteLength(i,"utf8"),byteOffset:Buffer.byteLength(t.slice(0,r.index),"utf8")}),n.push(a)}return n}async function LA(t,e){let n=[];for await(let r of e.pipe([t])){let i=t.slice(r.offset,r.offset+r.text.length);if(i.trim()==="")continue;let a=new at(i,r.wordsCount);Object.assign(a,{byteLength:Buffer.byteLength(i,"utf8"),byteOffset:Buffer.byteLength(t.slice(0,r.offset),"utf8")}),n.push(a)}return n}function OA(){return qg??=new Intl.Segmenter(void 0,{granularity:"sentence"}),qg}function Kg(t){let e=t.byteOffset;return typeof e=="number"?e:0}function Jg(t){return typeof t.byteOffset=="number"}function zg(t){let e=t.byteLength;return typeof e=="number"?e:Buffer.byteLength(Du(t),"utf8")}function Du(t){let e=t.rawText;return typeof e=="string"?e:t.text}var qg,Qg=c(()=>{"use strict";on();Ke()});var ot,cs=c(()=>{"use strict";ot={source:1,summary:2}});import{join as Xg,resolve as DA}from"path";function us(t){return{byteLength:Number(t.byte_length),byteOffset:Number(t.byte_offset),sentenceIndex:Number(t.sentence_index),wordsCount:Number(t.words_count)}}var or,Mu=c(()=>{"use strict";Ke();Nu();Qg();cs();or=class t{static#e=new Map;#t;#n;#r;#a;#i;constructor(e,n,r,i,a){this.#t=n,this.#n=DA(e),this.#r=r,this.#a=i,this.#i=a}async createDraft(){let e=this.#f();if(e.draftOpen)throw new Error("Only one text stream draft can be open at a time");return await this.#r.ensureDirectory(this.#m()),e.draftOpen=!0,new ds(this.#i,await this.#u(),{discard:()=>{e.draftOpen=!1},finalize:async(n,r,i)=>await this.#c(n,r,i)})}async getFragment(e){let r=(await this.#l()).find(i=>i.fragmentId===e);if(r===void 0)throw new Error(`Fragment ${e} does not exist`);return r}async getSentence(e){let n=await this.#o(e);if(n===void 0)throw new RangeError(`Sentence ${e} does not exist`);return this.#d(n,await this.#p())}async listFragmentIds(){return(await this.#l()).map(e=>e.fragmentId)}async listSentences(){let e=await this.#s(),n=await this.#p();return e.map(r=>this.#d(r,n))}async listSentencesInRange(e,n){if(n<e)return[];let r=await this.#t.queryAll(`
|
|
95
95
|
SELECT sentence_index, byte_offset, byte_length, words_count
|
|
96
96
|
FROM text_sentence_records
|
|
97
97
|
WHERE kind = ? AND chapter_id = ?
|
|
98
98
|
AND sentence_index BETWEEN ? AND ?
|
|
99
99
|
ORDER BY sentence_index
|
|
100
|
-
`,[
|
|
100
|
+
`,[ot[this.#a],this.#i,e,n],us),i=await this.#p();return r.map(a=>this.#d(a,i))}async readTextInRange(e,n){if(n<e)return"";let r=await this.#t.queryAll(`
|
|
101
101
|
SELECT sentence_index, byte_offset, byte_length, words_count
|
|
102
102
|
FROM text_sentence_records
|
|
103
103
|
WHERE kind = ? AND chapter_id = ?
|
|
104
104
|
AND sentence_index BETWEEN ? AND ?
|
|
105
105
|
ORDER BY sentence_index
|
|
106
|
-
`,[
|
|
106
|
+
`,[ot[this.#a],this.#i,e,n],us),i=r[0],a=r[r.length-1];return i===void 0||a===void 0?void 0:(await this.#p()).subarray(i.byteOffset,a.byteOffset+a.byteLength).toString("utf8")}async#o(e){return await this.#t.queryOne(`
|
|
107
107
|
SELECT sentence_index, byte_offset, byte_length, words_count
|
|
108
108
|
FROM text_sentence_records
|
|
109
109
|
WHERE kind = ? AND chapter_id = ? AND sentence_index = ?
|
|
110
|
-
`,[
|
|
110
|
+
`,[ot[this.#a],this.#i,e],us)}async#s(){return await this.#t.queryAll(`
|
|
111
111
|
SELECT sentence_index, byte_offset, byte_length, words_count
|
|
112
112
|
FROM text_sentence_records
|
|
113
113
|
WHERE kind = ? AND chapter_id = ?
|
|
114
114
|
ORDER BY sentence_index
|
|
115
|
-
`,[
|
|
115
|
+
`,[ot[this.#a],this.#i],us)}#d(e,n){return new at(n.subarray(e.byteOffset,e.byteOffset+e.byteLength).toString("utf8"),e.wordsCount)}async readText(){let e=await this.#r.readFile(this.#h());return e===void 0?void 0:Buffer.from(e).toString("utf8")}async writeTextStream(e,n={}){await this.delete();let r=await $g(e,n.segmenter),i=await this.createDraft();for(let a of r)i.addSentence(a.text,a.wordsCount,{byteOffset:a.byteOffset,byteLength:a.byteLength});await i.commitWithText(e)}async delete(){await this.#r.deleteTree(this.#h()),await this.#t.run(`
|
|
116
116
|
DELETE FROM text_sentence_records
|
|
117
117
|
WHERE kind = ? AND chapter_id = ?
|
|
118
|
-
`,[
|
|
118
|
+
`,[ot[this.#a],this.#i]),delete this.#f().nextSentenceIndex}get path(){return this.#h()}get serialId(){return this.#i}async#c(e,n,r){let i=this.#f();i.draftOpen=!1;let a=await this.#r.readFile(this.#h()),o=a===void 0?Buffer.alloc(0):Buffer.from(a),s=n===""?r.map(Du).join(""):n,d=Buffer.from(s,"utf8"),u=o.length;if(await this.#r.ensureDirectory(this.#m()),await this.#r.writeFile(this.#h(),Buffer.concat([o,d]),{overwrite:!0}),r.length!==0){for(let l=0;l<r.length;l+=1){let m=r[l];if(m===void 0)continue;let p=zg(m),h=Kg(m),y=u+h;await this.#t.run(`
|
|
119
119
|
INSERT OR REPLACE INTO text_sentence_records (
|
|
120
120
|
kind,
|
|
121
121
|
chapter_id,
|
|
@@ -125,54 +125,54 @@ ${h}
|
|
|
125
125
|
byte_length
|
|
126
126
|
)
|
|
127
127
|
VALUES (?, ?, ?, ?, ?, ?)
|
|
128
|
-
`,[
|
|
128
|
+
`,[ot[this.#a],this.#i,e+l,m.wordsCount,y,p]),Jg(m)||(u+=p)}return i.nextSentenceIndex=e+r.length,{fragmentId:e,sentences:r,serialId:this.#i,summary:""}}}async#l(){let e=await this.listSentences(),n=[],r=[],i=0,a=0;for(let o=0;o<e.length;o+=1){let s=e[o];s!==void 0&&(r.length>0&&i+s.wordsCount>600&&(n.push({fragmentId:a,sentences:r,serialId:this.#i,summary:""}),a=o,r=[],i=0),r.push(s),i+=s.wordsCount)}return r.length>0&&n.push({fragmentId:a,sentences:r,serialId:this.#i,summary:""}),n}async#u(){let e=this.#f();return e.nextSentenceIndex!==void 0||(e.nextSentenceIndex=await this.#t.queryOne(`
|
|
129
129
|
SELECT COALESCE(MAX(sentence_index), -1) + 1 AS next_index
|
|
130
130
|
FROM text_sentence_records
|
|
131
131
|
WHERE kind = ? AND chapter_id = ?
|
|
132
|
-
`,[Et[this.#i],this.#a],r=>Number(r.next_index))??0),e.nextSentenceIndex}async#p(){let e=await this.#n.readFile(this.#h());return Buffer.from(e??new Uint8Array)}#m(){return ay(this.#r,"texts",this.#i)}#h(){return ay(this.#m(),`${this.#a}.txt`)}#f(){let e=`${this.#r}\0${this.#i}\0${this.#a}`,r=t.#e.get(e);return r===void 0&&(r={draftOpen:!1},t.#e.set(e,r)),r}}});import{join as wP,resolve as EP}from"path";var di,oy=d(()=>{"use strict";Yf();au();di=class{#e;#t;#r;constructor(e,r,n=Vf){this.#e=r,this.#t=EP(e),this.#r=n}async ensureCreated(){await this.#r.ensureDirectory(this.#n("source")),await this.#r.ensureDirectory(this.#n("summary"))}async getSentence(e){return(await this.getSerial(e[0]).getSentence(e[1])).text}getSerial(e){return new Gr(this.#t,this.#e,this.#r,"source",e)}getSummarySerial(e){return new Gr(this.#t,this.#e,this.#r,"summary",e)}#n(e){return wP(this.#t,"texts",e)}}});var ou=d(()=>{"use strict";oy();ru();au();iu()});import{mkdir as kP,readFile as IP,readdir as xP,writeFile as RP}from"fs/promises";var Wo,Br,Fo=d(()=>{"use strict";T();Wo={write:async(t,e)=>{await RP(t,e,"utf8")}},Br={ensureDirectory:async t=>{await kP(t,{recursive:!0})},listFiles:async t=>(await xP(t,{withFileTypes:!0})).filter(e=>e.isFile()).map(e=>e.name),readFile:async t=>{try{return await IP(t)}catch(e){if(w(e)&&e.code==="ENOENT")return;throw e}}}});var ui,su=d(()=>{"use strict";ui=class{#e=!1;#t;#r;#n;#i;#a=[];#o="";#s;constructor(e,r,n,i){this.#t=e,this.#r=i.discard,this.#n=i.finalize,this.#i=r,this.#s=n}addSentence(e,r){this.#c();let n=this.#s+this.#a.length;return this.#a.push({text:e,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(e){this.#c(),this.#o=e}get fragmentId(){return this.#i}#c(){if(this.#e)throw new Error("Fragment draft is already finalized")}}});import{join as vP,resolve as TP}from"path";async function sy(t,e=Br){return cu(t,await _P(t,e))}function cu(t,e){if(e===void 0)throw new Error(`Fragment file does not exist: ${t}`);let r=JSON.parse(Buffer.from(e).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(CP),summary:r.summary}}async function _P(t,e){if(e.listFileContents===void 0)return await e.readFile(t);let r=TP(vP(t,"..")),n=t.slice(r.length+1);return(await e.listFileContents(r)).get(n)}function CP(t){if(typeof t!="object"||t===null)throw new TypeError("Sentence entry must be an object");let{text:e,wordsCount:r}=t;if(typeof e!="string"||typeof r!="number")throw new TypeError("Sentence entry is invalid");return{text:e,wordsCount:r}}var cy=d(()=>{"use strict";Fo()});function dy(t){return t.split(/\n+/u).map(e=>e.trim()).filter(e=>e!=="").map(e=>({text:e,wordsCount:PP(e)}))}function PP(t){let e=t.trim();return e===""?0:e.split(/\s+/u).length}var uy=d(()=>{"use strict"});var ly,my,du=d(()=>{"use strict";ly="serial-",my=/^fragment_(\d+)\.json$/});import{join as py,resolve as AP}from"path";var Ur,uu=d(()=>{"use strict";T();su();Fo();cy();uy();du();Ur=class{#e;#t=!1;#r;#n;#i;#a;#o;#s;#c;constructor(e,r,n="fragments",i,a){this.#r=AP(e),this.#e=r,this.#n=a??Br,this.#a=n,this.#c=i??Wo}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 ui(this.#e,await this.#d(),await this.#p(),{discard:()=>{this.#u()},finalize:async(e,r,n,i)=>await this.#l(e,r,n,i)})}async getFragment(e){let r=this.#n.listFileContents===void 0?await sy(this.#m(e),this.#n):cu(this.#m(e),(await this.#h()).get(`fragment_${e}.json`));return{serialId:this.#e,fragmentId:e,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=>my.exec(r)).filter(r=>r!==null).map(r=>Number(r[1])).sort((r,n)=>r-n)}catch(e){if(w(e)&&e.code==="ENOENT")return[];throw e}}async writeTextStream(e,r={}){await this.#n.ensureDirectory(this.path);let n=dy(e);if(n.length===0){let s=await this.#d();await this.#c.write(this.#m(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 py(this.#r,this.#a,`${ly}${this.#e}`)}async#l(e,r,n,i){if(this.#t=!1,i.length!==0)return await this.#n.ensureDirectory(this.path),await this.#c.write(this.#m(e),JSON.stringify({sentences:i,summary:n},void 0,2)),this.#o=e+1,this.#s=r+i.length,{serialId:this.#e,fragmentId:e,summary:n,sentences:i}}#u(){this.#t=!1}async#d(){if(this.#o!==void 0)return this.#o;let e=await this.listFragmentIds(),r=e[e.length-1];return this.#o=r===void 0?0:r+1,this.#o}async#p(){if(this.#s!==void 0)return this.#s;let e=0;for(let r of await this.listFragmentIds())e+=(await this.getFragment(r)).sentences.length;return this.#s=e,this.#s}#m(e){return py(this.path,`fragment_${e}.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 OP,resolve as NP}from"path";var Vt,hy=d(()=>{"use strict";Fo();uu();Vt=class{#e;#t;#r;constructor(e,r,n){this.#e=NP(e),this.#t=n??Br,this.#r=r??Wo}async ensureCreated(){await this.#t.ensureDirectory(this.path)}getSerial(e){return new Ur(this.#e,e,"fragments",this.#r,this.#t)}getSummarySerial(e){return new Ur(this.#e,e,"summaries",this.#r,this.#t)}async getSentence(e){let[r,n]=e,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(e,r){return(await this.getSerial(e).getFragment(r)).summary}async getWordsCount(e,r){return(await this.getSerial(e).getFragment(r)).sentences.reduce((i,a)=>i+a.wordsCount,0)}get path(){return OP(this.#e,"fragments")}}});var lu=d(()=>{"use strict";hy();su();uu();du()});import{z as Ae}from"zod";var fy,kt,yy,mu,li=d(()=>{"use strict";fy=["epub","pdf","txt","markdown"],kt=1,yy=Ae.enum(fy),mu=Ae.object({version:Ae.literal(kt),sourceFormat:yy,title:Ae.string().min(1).nullable(),authors:Ae.array(Ae.string().min(1)),language:Ae.string().min(1).nullable(),identifier:Ae.string().min(1).nullable(),publisher:Ae.string().min(1).nullable(),publishedAt:Ae.string().min(1).nullable(),description:Ae.string().min(1).nullable()})});import{randomBytes as DP}from"crypto";function mi(t){return`wikg://chapter/${t}`}function by(t,e="chapter path"){let r=t.trim();if(r==="")throw new Error(`Invalid ${e}: expected an absolute chapter path.`);if(/^\d+$/u.test(r))throw new Error(`Invalid ${e}: numeric chapter ids are internal and are not accepted.`);if(r.startsWith("/")||r.endsWith("/"))throw new Error(`Invalid ${e}: use <key>[/<key>]* without leading or trailing slash.`);let n=r.split("/");for(let i of n)jo(i,e);return n.join("/")}function jo(t,e="chapter key"){if(t===""||t==="."||t===".."||t.includes("/"))throw new Error(`Invalid ${e}: chapter paths do not support empty, '.', '..', or relative segments.`);if(!/^[a-z0-9][a-z0-9-]*$/u.test(t))throw new Error(`Invalid ${e}: ${t}. Use lowercase letters, numbers, and hyphens.`);if(/^\d+$/u.test(t))throw new Error(`Invalid ${e}: numeric chapter keys are not accepted.`);if(MP.has(t))throw new Error(`Invalid ${e}: ${t} is reserved.`);return t}function pi(t){for(let e=0;e<gy;e+=1){let r=DP(WP).toString("hex");try{jo(r)}catch{continue}if(!t.has(r))return r}throw new Error(`Unable to create a unique chapter key after ${gy} attempts.`)}function Sy(t,e){let r=by(e).split("/"),n=t,i;for(let a of r){if(i=n.find(o=>o.key===a),i===void 0)return;n=i.children}return i?.serialId}var MP,WP,gy,rt=d(()=>{"use strict";MP=new Set(["tree","source","summary","title","state","chunk","entity","triple","cover","meta","job","help"]),WP=6,gy=100});import{z as nt}from"zod";var it,pu,Go,Bo=d(()=>{"use strict";rt();it=1,pu=nt.object({key:nt.string().min(1).transform(t=>jo(t)).optional(),title:nt.string().min(1).nullable().optional(),serialId:nt.number().int().nonnegative().optional(),children:nt.lazy(()=>nt.array(pu))}),Go=nt.object({version:nt.literal(it),items:nt.array(pu)})});import{createHash as FP}from"crypto";import{posix as hu}from"path";import{PassThrough as jP}from"stream";import{open as GP}from"yauzl";function at(t){let e=t.replaceAll("\\","/").trim(),r=e.startsWith("/")?e.slice(1):e;return hu.normalize(r).replace(/^(\.\/)+/,"").replace(/^\/+/,"")}function BP(t){let[e]=t.split("#",1);return at(e??"")}function fu(t){if(t===void 0)return;let e=t.startsWith("#")?t.slice(1):t;return e===""?void 0:e}function hi(t){let[e,r]=t.split("#",2);return{path:at(e??""),fragment:fu(r)}}async function UP(t){return await new Promise((e,r)=>{GP(t,{autoClose:!1,lazyEntries:!0},(n,i)=>{if(n!==null||i===void 0){r(n??new Error(`Cannot open EPUB archive: ${t}`));return}e(i)})})}async function HP(t){return await new Promise((e,r)=>{let n=new Map;t.on("entry",i=>{if(i.fileName.endsWith("/")){t.readEntry();return}n.set(at(i.fileName),i),t.readEntry()}),t.once("end",()=>{e(n)}),t.once("error",i=>{r(i)}),t.readEntry()})}function qP(t){if(Buffer.isBuffer(t))return t;if(typeof t=="string")return Buffer.from(t,"utf8");throw new Error("Unexpected ZIP stream chunk type")}async function wy(t){return await new Promise((e,r)=>{let n=[];t.on("data",i=>{n.push(qP(i))}),t.once("end",()=>{e(Buffer.concat(n))}),t.once("error",i=>{r(i)}),t.resume()})}async function $P(t,e){return await new Promise((r,n)=>{t.openReadStream(e,(i,a)=>{if(i!==null||a===void 0){n(i??new Error(`Cannot open EPUB entry: ${e.fileName}`));return}r(KP(a))})})}function KP(t){let e=new jP;return t.once("error",r=>{e.destroy(r)}),e.once("close",()=>{t.unpipe(e),t.destroyed||t.destroy()}),t.pipe(e),e}var Uo,fi=d(()=>{"use strict";Uo=class t{#e;#t;#r;#n=!1;constructor(e,r,n){this.#e=e,this.#t=r,this.#r=n}static async open(e){let r=await UP(e),n=await HP(r);return new t(e,r,n)}close(){return this.#n||(this.#n=!0,this.#t.close()),Promise.resolve()}hasEntry(e){return this.#r.has(at(e))}listEntries(){return[...this.#r.keys()]}async openReadStream(e){let r=this.#i(e);return await $P(this.#t,r)}async readText(e){let r=await this.openReadStream(e);return(await wy(r)).toString("utf8")}async readBuffer(e){let r=await this.openReadStream(e);return await wy(r)}resolveRelativePath(e,r){let n=BP(r);if(n==="")throw new Error(`Invalid EPUB href: ${r}`);return at(hu.join(hu.dirname(at(e)),n))}createSectionId(e,r){let n=at(e);return r===void 0||r===""?n:`${n}#${r}`}createSyntheticSectionId(e,r){let n=at(e);return`toc:${FP("sha1").update(`${n}:${r??""}`).digest("hex").slice(0,10)}`}get path(){return this.#e}#i(e){let r=at(e),n=this.#r.get(r);if(n===void 0)throw new Error(`EPUB entry does not exist: ${r}`);return n}}});import{Parser as JP}from"htmlparser2";async function ky(t,e){let r=new Map;for(let[n,i]of e.entries()){let a=await t.openReadStream(n);a.setEncoding("utf8");let o=await Iy(a,i);for(let s of i){let c=o.get(s.id)??"";r.set(s.id,{hasContent:c.trim()!=="",wordsCount:ZP(c)})}}return r}function QP(t){let e=new Map;for(let[r,n]of t.entries())for(let i of n)e.set(i.id,{path:r,targets:n});return e}async function XP(t,e){return await Iy(t,e)}async function Iy(t,e){let r=new Map,n=new Map,i=[...e],a=i.map(()=>""),s=i[0]?.fragment===void 0&&i[0]!==void 0?0:-1,c=0;i.forEach((l,m)=>{n.set(l.id,""),l.fragment!==void 0&&!r.has(l.fragment)&&r.set(l.fragment,m)});let u=new JP({onopentag(l,m){let p=l.toLowerCase();if(Ey.has(p)){c+=1;return}let h=m.id??m["xml:id"];if(h!==void 0){let g=r.get(h);g!==void 0&&g>s&&(s=g)}p==="br"&&yu(a,s,`
|
|
133
|
-
`)},ontext(l){
|
|
132
|
+
`,[ot[this.#a],this.#i],n=>Number(n.next_index))??0),e.nextSentenceIndex}async#p(){let e=await this.#r.readFile(this.#h());return Buffer.from(e??new Uint8Array)}#m(){return Xg(this.#n,"texts",this.#a)}#h(){return Xg(this.#m(),`${this.#i}.txt`)}#f(){let e=`${this.#n}\0${this.#a}\0${this.#i}`,n=t.#e.get(e);return n===void 0&&(n={draftOpen:!1},t.#e.set(e,n)),n}}});import{join as MA,resolve as WA}from"path";var Ci,Vg=c(()=>{"use strict";Hg();Mu();Ci=class{#e;#t;#n;constructor(e,n,r=Ug){this.#e=n,this.#t=WA(e),this.#n=r}async ensureCreated(){await this.#n.ensureDirectory(this.#r("source")),await this.#n.ensureDirectory(this.#r("summary"))}async getSentence(e){return(await this.getSerial(e[0]).getSentence(e[1])).text}getSerial(e){return new or(this.#t,this.#e,this.#n,"source",e)}getSummarySerial(e){return new or(this.#t,this.#e,this.#n,"summary",e)}#r(e){return MA(this.#t,"texts",e)}}});var Wu=c(()=>{"use strict";Vg();Nu();Mu();cs()});import{mkdir as FA,readFile as jA,readdir as GA,writeFile as BA}from"fs/promises";var ls,sr,ms=c(()=>{"use strict";T();ls={write:async(t,e)=>{await BA(t,e,"utf8")}},sr={ensureDirectory:async t=>{await FA(t,{recursive:!0})},listFiles:async t=>(await GA(t,{withFileTypes:!0})).filter(e=>e.isFile()).map(e=>e.name),readFile:async t=>{try{return await jA(t)}catch(e){if(E(e)&&e.code==="ENOENT")return;throw e}}}});var Pi,Fu=c(()=>{"use strict";Pi=class{#e=!1;#t;#n;#r;#a;#i=[];#o="";#s;constructor(e,n,r,i){this.#t=e,this.#n=i.discard,this.#r=i.finalize,this.#a=n,this.#s=r}addSentence(e,n){this.#d();let r=this.#s+this.#i.length;return this.#i.push({text:e,wordsCount:n}),[this.#t,r]}async commit(){return this.#d(),this.#e=!0,await this.#r(this.#a,this.#s,this.#o,this.#i)}discard(){this.#d(),this.#e=!0,this.#n()}setSummary(e){this.#d(),this.#o=e}get fragmentId(){return this.#a}#d(){if(this.#e)throw new Error("Fragment draft is already finalized")}}});import{join as UA,resolve as HA}from"path";async function Yg(t,e=sr){return ju(t,await qA(t,e))}function ju(t,e){if(e===void 0)throw new Error(`Fragment file does not exist: ${t}`);let n=JSON.parse(Buffer.from(e).toString("utf8"));if(typeof n!="object"||n===null)throw new TypeError("Fragment file must be an object");if(!("summary"in n)||typeof n.summary!="string")throw new TypeError("Fragment file summary must be a string");if(!("sentences"in n)||!Array.isArray(n.sentences))throw new TypeError("Fragment file sentences must be an array");return{sentences:n.sentences.map($A),summary:n.summary}}async function qA(t,e){if(e.listFileContents===void 0)return await e.readFile(t);let n=HA(UA(t,"..")),r=t.slice(n.length+1);return(await e.listFileContents(n)).get(r)}function $A(t){if(typeof t!="object"||t===null)throw new TypeError("Sentence entry must be an object");let{text:e,wordsCount:n}=t;if(typeof e!="string"||typeof n!="number")throw new TypeError("Sentence entry is invalid");return{text:e,wordsCount:n}}var Zg=c(()=>{"use strict";ms()});function ey(t){return t.split(/\n+/u).map(e=>e.trim()).filter(e=>e!=="").map(e=>({text:e,wordsCount:Ne(e)}))}var ty=c(()=>{"use strict";on()});var ny,ry,Gu=c(()=>{"use strict";ny="serial-",ry=/^fragment_(\d+)\.json$/});import{join as iy,resolve as JA}from"path";var dr,Bu=c(()=>{"use strict";T();Fu();ms();Zg();ty();Gu();dr=class{#e;#t=!1;#n;#r;#a;#i;#o;#s;#d;constructor(e,n,r="fragments",i,a){this.#n=JA(e),this.#e=n,this.#r=a??sr,this.#i=r,this.#d=i??ls}async createDraft(){if(this.#t)throw new Error("Only one fragment draft can be open at a time");return await this.#r.ensureDirectory(this.path),this.#t=!0,new Pi(this.#e,await this.#u(),await this.#p(),{discard:()=>{this.#l()},finalize:async(e,n,r,i)=>await this.#c(e,n,r,i)})}async getFragment(e){let n=this.#r.listFileContents===void 0?await Yg(this.#m(e),this.#r):ju(this.#m(e),(await this.#h()).get(`fragment_${e}.json`));return{serialId:this.#e,fragmentId:e,summary:n.summary,sentences:n.sentences}}async listFragmentIds(){try{return(this.#r.listFileContents===void 0?await this.#r.listFiles(this.path):[...(await this.#h()).keys()]).map(n=>ry.exec(n)).filter(n=>n!==null).map(n=>Number(n[1])).sort((n,r)=>n-r)}catch(e){if(E(e)&&e.code==="ENOENT")return[];throw e}}async writeTextStream(e,n={}){await this.#r.ensureDirectory(this.path);let r=ey(e);if(r.length===0){let s=await this.#u();await this.#d.write(this.#m(s),JSON.stringify({sentences:[],summary:""},void 0,2)),this.#o=s+1;return}let i=n.maxWordsCount??600,a=await this.createDraft(),o=0;for(let s of r)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 iy(this.#n,this.#i,`${ny}${this.#e}`)}async#c(e,n,r,i){if(this.#t=!1,i.length!==0)return await this.#r.ensureDirectory(this.path),await this.#d.write(this.#m(e),JSON.stringify({sentences:i,summary:r},void 0,2)),this.#o=e+1,this.#s=n+i.length,{serialId:this.#e,fragmentId:e,summary:r,sentences:i}}#l(){this.#t=!1}async#u(){if(this.#o!==void 0)return this.#o;let e=await this.listFragmentIds(),n=e[e.length-1];return this.#o=n===void 0?0:n+1,this.#o}async#p(){if(this.#s!==void 0)return this.#s;let e=0;for(let n of await this.listFragmentIds())e+=(await this.getFragment(n)).sentences.length;return this.#s=e,this.#s}#m(e){return iy(this.path,`fragment_${e}.json`)}async#h(){if(this.#r.listFileContents===void 0)throw new Error("Fragment file access does not support batch reads.");return this.#a??=this.#r.listFileContents(this.path),await this.#a}}});import{join as zA,resolve as QA}from"path";var sn,ay=c(()=>{"use strict";ms();Bu();sn=class{#e;#t;#n;constructor(e,n,r){this.#e=QA(e),this.#t=r??sr,this.#n=n??ls}async ensureCreated(){await this.#t.ensureDirectory(this.path)}getSerial(e){return new dr(this.#e,e,"fragments",this.#n,this.#t)}getSummarySerial(e){return new dr(this.#e,e,"summaries",this.#n,this.#t)}async getSentence(e){let[n,r]=e,i=await this.getSerial(n).listFragmentIds(),a=r;for(let o of i){let s=await this.getSerial(n).getFragment(o),d=s.sentences[a];if(d!==void 0)return d.text;a-=s.sentences.length}throw new RangeError(`Sentence ${r} does not exist`)}async getSummary(e,n){return(await this.getSerial(e).getFragment(n)).summary}async getWordsCount(e,n){return(await this.getSerial(e).getFragment(n)).sentences.reduce((i,a)=>i+a.wordsCount,0)}get path(){return zA(this.#e,"fragments")}}});var Uu=c(()=>{"use strict";ay();Fu();Bu();Gu()});import{z as De}from"zod";var oy,_t,sy,Hu,Ai=c(()=>{"use strict";oy=["epub","pdf","txt","markdown"],_t=1,sy=De.enum(oy),Hu=De.object({version:De.literal(_t),sourceFormat:sy,title:De.string().min(1).nullable(),authors:De.array(De.string().min(1)),language:De.string().min(1).nullable(),identifier:De.string().min(1).nullable(),publisher:De.string().min(1).nullable(),publishedAt:De.string().min(1).nullable(),description:De.string().min(1).nullable()})});import{randomBytes as XA}from"crypto";function Li(t){return`wikg://chapter/${t}`}function cy(t,e="chapter path"){let n=t.trim();if(n==="")throw new Error(`Invalid ${e}: expected an absolute chapter path.`);if(/^\d+$/u.test(n))throw new Error(`Invalid ${e}: numeric chapter ids are internal and are not accepted.`);if(n.startsWith("/")||n.endsWith("/"))throw new Error(`Invalid ${e}: use <key>[/<key>]* without leading or trailing slash.`);let r=n.split("/");for(let i of r)ps(i,e);return r.join("/")}function ps(t,e="chapter key"){if(t===""||t==="."||t===".."||t.includes("/"))throw new Error(`Invalid ${e}: chapter paths do not support empty, '.', '..', or relative segments.`);if(!/^[a-z0-9][a-z0-9-]*$/u.test(t))throw new Error(`Invalid ${e}: ${t}. Use lowercase letters, numbers, and hyphens.`);if(/^\d+$/u.test(t))throw new Error(`Invalid ${e}: numeric chapter keys are not accepted.`);if(VA.has(t))throw new Error(`Invalid ${e}: ${t} is reserved.`);return t}function Oi(t){for(let e=0;e<dy;e+=1){let n=XA(YA).toString("hex");try{ps(n)}catch{continue}if(!t.has(n))return n}throw new Error(`Unable to create a unique chapter key after ${dy} attempts.`)}function uy(t,e){let n=cy(e).split("/"),r=t,i;for(let a of n){if(i=r.find(o=>o.key===a),i===void 0)return;r=i.children}return i?.serialId}var VA,YA,dy,st=c(()=>{"use strict";VA=new Set(["tree","source","summary","title","state","chunk","entity","triple","cover","meta","job","help"]),YA=6,dy=100});import{z as dt}from"zod";var ct,qu,hs,fs=c(()=>{"use strict";st();ct=1,qu=dt.object({key:dt.string().min(1).transform(t=>ps(t)).optional(),title:dt.string().min(1).nullable().optional(),serialId:dt.number().int().nonnegative().optional(),children:dt.lazy(()=>dt.array(qu))}),hs=dt.object({version:dt.literal(ct),items:dt.array(qu)})});import{createHash as ZA}from"crypto";import{posix as $u}from"path";import{PassThrough as eL}from"stream";import{open as tL}from"yauzl";function ut(t){let e=t.replaceAll("\\","/").trim(),n=e.startsWith("/")?e.slice(1):e;return $u.normalize(n).replace(/^(\.\/)+/,"").replace(/^\/+/,"")}function nL(t){let[e]=t.split("#",1);return ut(e??"")}function Ku(t){if(t===void 0)return;let e=t.startsWith("#")?t.slice(1):t;return e===""?void 0:e}function Ni(t){let[e,n]=t.split("#",2);return{path:ut(e??""),fragment:Ku(n)}}async function rL(t){return await new Promise((e,n)=>{tL(t,{autoClose:!1,lazyEntries:!0},(r,i)=>{if(r!==null||i===void 0){n(r??new Error(`Cannot open EPUB archive: ${t}`));return}e(i)})})}async function iL(t){return await new Promise((e,n)=>{let r=new Map;t.on("entry",i=>{if(i.fileName.endsWith("/")){t.readEntry();return}r.set(ut(i.fileName),i),t.readEntry()}),t.once("end",()=>{e(r)}),t.once("error",i=>{n(i)}),t.readEntry()})}function aL(t){if(Buffer.isBuffer(t))return t;if(typeof t=="string")return Buffer.from(t,"utf8");throw new Error("Unexpected ZIP stream chunk type")}async function ly(t){return await new Promise((e,n)=>{let r=[];t.on("data",i=>{r.push(aL(i))}),t.once("end",()=>{e(Buffer.concat(r))}),t.once("error",i=>{n(i)}),t.resume()})}async function oL(t,e){return await new Promise((n,r)=>{t.openReadStream(e,(i,a)=>{if(i!==null||a===void 0){r(i??new Error(`Cannot open EPUB entry: ${e.fileName}`));return}n(sL(a))})})}function sL(t){let e=new eL;return t.once("error",n=>{e.destroy(n)}),e.once("close",()=>{t.unpipe(e),t.destroyed||t.destroy()}),t.pipe(e),e}var gs,Di=c(()=>{"use strict";gs=class t{#e;#t;#n;#r=!1;constructor(e,n,r){this.#e=e,this.#t=n,this.#n=r}static async open(e){let n=await rL(e),r=await iL(n);return new t(e,n,r)}close(){return this.#r||(this.#r=!0,this.#t.close()),Promise.resolve()}hasEntry(e){return this.#n.has(ut(e))}listEntries(){return[...this.#n.keys()]}async openReadStream(e){let n=this.#a(e);return await oL(this.#t,n)}async readText(e){let n=await this.openReadStream(e);return(await ly(n)).toString("utf8")}async readBuffer(e){let n=await this.openReadStream(e);return await ly(n)}resolveRelativePath(e,n){let r=nL(n);if(r==="")throw new Error(`Invalid EPUB href: ${n}`);return ut($u.join($u.dirname(ut(e)),r))}createSectionId(e,n){let r=ut(e);return n===void 0||n===""?r:`${r}#${n}`}createSyntheticSectionId(e,n){let r=ut(e);return`toc:${ZA("sha1").update(`${r}:${n??""}`).digest("hex").slice(0,10)}`}get path(){return this.#e}#a(e){let n=ut(e),r=this.#n.get(n);if(r===void 0)throw new Error(`EPUB entry does not exist: ${n}`);return r}}});import{Parser as dL}from"htmlparser2";async function py(t,e){let n=new Map;for(let[r,i]of e.entries()){let a=await t.openReadStream(r);a.setEncoding("utf8");let o=await hy(a,i);for(let s of i){let d=o.get(s.id)??"";n.set(s.id,{hasContent:d.trim()!=="",wordsCount:Ne(d)})}}return n}function uL(t){let e=new Map;for(let[n,r]of t.entries())for(let i of r)e.set(i.id,{path:n,targets:r});return e}async function lL(t,e){return await hy(t,e)}async function hy(t,e){let n=new Map,r=new Map,i=[...e],a=i.map(()=>""),s=i[0]?.fragment===void 0&&i[0]!==void 0?0:-1,d=0;i.forEach((l,m)=>{r.set(l.id,""),l.fragment!==void 0&&!n.has(l.fragment)&&n.set(l.fragment,m)});let u=new dL({onopentag(l,m){let p=l.toLowerCase();if(my.has(p)){d+=1;return}let h=m.id??m["xml:id"];if(h!==void 0){let y=n.get(h);y!==void 0&&y>s&&(s=y)}p==="br"&&Ju(a,s,`
|
|
133
|
+
`)},ontext(l){d>0||Ju(a,s,l)},onclosetag(l){let m=l.toLowerCase();if(my.has(m)){d=Math.max(0,d-1);return}cL.has(m)&&Ju(a,s,`
|
|
134
134
|
|
|
135
|
-
`)}},{decodeEntities:!0});for await(let l of t)u.write(
|
|
135
|
+
`)}},{decodeEntities:!0});for await(let l of t)u.write(pL(l));return u.end(),i.forEach((l,m)=>{r.set(l.id,mL(a[m]??""))}),r}function Ju(t,e,n){e<0||e>=t.length||(t[e]+=n)}function mL(t){return t.replace(/\r\n/gu,`
|
|
136
136
|
`).replace(/\n{3,}/gu,`
|
|
137
137
|
|
|
138
|
-
`).trim()}function YP(t){if(typeof t=="string")return t;if(Buffer.isBuffer(t))return t.toString("utf8");throw new Error("Unexpected HTML stream chunk type")}function ZP(t){return[...eL().segment(t)].filter(e=>e.isWordLike).length}function eL(){return gu===void 0&&(gu=new Intl.Segmenter(void 0,{granularity:"word"})),gu}var zP,Ey,Ho,gu,xy=d(()=>{"use strict";zP=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"]),Ey=new Set(["script","style"]),Ho=class{#e;#t;constructor(e,r){this.#e=e,this.#t=QP(r)}async openSection(e){let r=this.#t.get(e);return r===void 0?[]:[(await this.#r(r.path,r.targets)).get(e)??""]}async#r(e,r){let n=await this.#e.openReadStream(e);return n.setEncoding("utf8"),await XP(n,r)}}});import{SaxesParser as tL}from"saxes";function yi(t){let e=new tL({xmlns:!1}),r=[],n;if(e.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)}),e.on("text",i=>{let a=r[r.length-1];a!==void 0&&(a.text+=i)}),e.on("cdata",i=>{let a=r[r.length-1];a!==void 0&&(a.text+=i)}),e.on("closetag",()=>{r.pop()}),e.write(t).close(),n===void 0)throw new Error("XML document is empty");return Ry(n)}function Oe(t,e){return t.children.find(r=>bi(r.name)===e)}function It(t,e){return t.children.filter(r=>bi(r.name)===e)}function ce(t,e){return t.attributes[e]}function gi(t){let e=t.text;for(let r of t.children)e+=gi(r);return e}function bi(t){let e=t.indexOf(":");return e===-1?t:t.slice(e+1)}function Ry(t){return{name:t.name,attributes:Object.freeze({...t.attributes}),children:Object.freeze(t.children.map(Ry)),text:t.text}}var bu=d(()=>{"use strict"});import{posix as rL}from"path";async function Ty(t){let e=await aL(t),r=yi(await t.readText(e)),n=oL(ce(r,"version")),i=Oe(r,"metadata"),a=Oe(r,"manifest"),o=Oe(r,"spine"),s=Oe(r,"guide"),c=cL(t,e,a),u=new Map([...c.values()].map(E=>[E.path,E])),l=dL(o,c),m=uL(t,e,s),p=sL(i),h=lL(c),g=mL(c,o),f=pL(c,i);return{version:n,opfPath:e,metadata:{...p,version:kt,sourceFormat:"epub"},manifest:c,manifestByPath:u,spine:l,navPath:h,ncxPath:g,coverPath:f?.path,coverMediaType:f?.mediaType,guideNonContentPaths:m}}async function aL(t){let e=yi(await t.readText("META-INF/container.xml")),n=Oe(e,"rootfiles")?.children.find(a=>bi(a.name)==="rootfile"),i=n===void 0?void 0:ce(n,"full-path");if(i===void 0||i.trim()==="")throw new Error("EPUB container.xml does not contain an OPF rootfile");return i}function oL(t){return t?.trim().startsWith("3")===!0?3:2}function sL(t){if(t===void 0)return vy();let e=Yt(t,"title"),r=Yt(t,"creator"),n=Yt(t,"language"),i=Yt(t,"identifier"),a=Yt(t,"publisher"),o=Yt(t,"date"),s=Yt(t,"description");return{...vy(),title:e[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 vy(){return{version:kt,sourceFormat:"epub",title:null,authors:[],language:null,identifier:null,publisher:null,publishedAt:null,description:null}}function Yt(t,e){return t.children.filter(r=>bi(r.name)===e).map(r=>fL(gi(r))).filter(r=>r!==void 0)}function cL(t,e,r){let n=new Map;if(r===void 0)return n;for(let i of It(r,"item")){let a=ce(i,"id"),o=ce(i,"href"),s=ce(i,"media-type")??"";a===void 0||o===void 0||n.set(a,{id:a,path:t.resolveRelativePath(e,o),mediaType:s,properties:new Set((ce(i,"properties")??"").split(/\s+/u).map(c=>c.trim().toLowerCase()).filter(c=>c!==""))})}return n}function dL(t,e){return t===void 0?[]:It(t,"itemref").map(r=>ce(r,"idref")).filter(r=>r!==void 0).map(r=>{let n=e.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 uL(t,e,r){let n=new Set;if(r===void 0)return n;for(let i of It(r,"reference")){let a=(ce(i,"type")??"").trim().toLowerCase();if(!iL.has(a))continue;let o=ce(i,"href");o===void 0||o.trim()===""||n.add(t.resolveRelativePath(e,hi(o).path))}return n}function lL(t){for(let e of t.values())if(e.properties.has("nav"))return e.path}function mL(t,e){let r=e===void 0?void 0:ce(e,"toc");if(r!==void 0)return t.get(r)?.path;for(let n of t.values())if(n.mediaType==="application/x-dtbncx+xml")return n.path}function pL(t,e){for(let n of t.values())if(n.properties.has("cover-image")&&n.mediaType.startsWith("image/"))return n;let r=hL(e);if(r!==void 0){let n=t.get(r);if(n?.mediaType.startsWith("image/")===!0)return n}for(let n of nL){let i=t.get(n);if(i?.mediaType.startsWith("image/")===!0)return i}for(let n of t.values())if(n.mediaType.startsWith("image/")&&rL.basename(n.path).toLowerCase().includes("cover"))return n}function hL(t){if(t!==void 0)for(let e of It(t,"meta")){if((ce(e,"name")??"").trim().toLowerCase()!=="cover")continue;let n=ce(e,"content");if(n!==void 0&&n.trim()!=="")return n.trim()}}function fL(t){let e=t.replace(/\s+/gu," ").trim();return e===""?void 0:e}var nL,iL,Su=d(()=>{"use strict";li();fi();bu();nL=["cover-image","coverimg","cover-img","cover"],iL=new Set(["cover","titlepage","other.titlepage"])});import{basename as yL,extname as gL,posix as bL}from"path";import{parseDocument as SL}from"htmlparser2";async function Cy(t,e){let r=await EL(t,e)??await kL(t,e)??[];if(r.length===0)return _y(e,new Set);let n=Ay(r),i=_y(e,n);return i.length===0?r:[...r,...i]}async function EL(t,e){if(e.navPath===void 0)return;let r=SL(await t.readText(e.navPath),{decodeEntities:!0}),n=wu(r,o=>xt(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=wu(n,o=>xt(o)==="ol");if(i===void 0)return;let a=Si(i).filter(o=>xt(o)==="li").map(o=>Py(t,e.navPath,o)).filter(o=>o!==void 0);return a.length===0?void 0:a}async function kL(t,e){if(e.ncxPath===void 0)return;let r=yi(await t.readText(e.ncxPath)),n=Oe(r,"navMap");if(n===void 0)return;let i=It(n,"navPoint").map(a=>Ly(t,e.ncxPath,a)).filter(a=>a!==void 0);return i.length===0?void 0:i}function Py(t,e,r){let n=Oy(r),i=n===void 0?void 0:My(Ny(n)),a=n!==void 0&&xt(n)==="a"?n.attribs?.href:void 0,o=xL(t,e,a),s=Si(r).filter(c=>xt(c)==="ol").flatMap(c=>Si(c)).filter(c=>xt(c)==="li").map(c=>Py(t,e,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 Ly(t,e,r){let n=Oe(r,"navLabel"),i=n===void 0?void 0:Oe(n,"text"),a=i===void 0?void 0:My(gi(i)),o=Oe(r,"content"),s=RL(t,e,ce(o??r,"src")),c=It(r,"navPoint").map(u=>Ly(t,e,u)).filter(u=>u!==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 _y(t,e){return t.spine.filter(r=>!IL(t,r.path,e)).filter(r=>!e.has(r.path)).map(r=>({title:vL(r.path),path:r.path,fragment:void 0,children:[]}))}function IL(t,e,r){if(t.guideNonContentPaths.has(e))return!0;let n=bL.basename(e).toLowerCase();return t.navPath===e?!0:wL.has(n)&&!r.has(e)}function Ay(t){let e=new Set;for(let r of t){r.path!==void 0&&e.add(r.path);for(let n of Ay(r.children))e.add(n)}return e}function xL(t,e,r){if(r===void 0||r.trim()==="")return{path:void 0,fragment:void 0};let{path:n,fragment:i}=hi(r);return{path:n===""?void 0:t.resolveRelativePath(e,n),fragment:i}}function RL(t,e,r){if(r===void 0||r.trim()==="")return{path:void 0,fragment:void 0};let{path:n,fragment:i}=hi(r);return{path:n===""?void 0:t.resolveRelativePath(e,n),fragment:i}}function wu(t,e){if(Dy(t)&&e(t))return t;for(let r of t.children??[]){let n=wu(r,e);if(n!==void 0)return n}}function Oy(t){for(let e of Si(t)){let r=xt(e);if(r==="a"||r==="span")return e}for(let e of Si(t)){if(xt(e)==="ol")continue;let r=Oy(e);if(r!==void 0)return r}}function Ny(t){return t.type==="text"?t.data??"":(t.children??[]).map(Ny).join("")}function Si(t){return(t.children??[]).filter(Dy)}function Dy(t){return t.type==="tag"||t.type==="script"||t.type==="style"}function xt(t){return t.name?.toLowerCase()}function vL(t){let e=yL(t,gL(t)).trim();return e===""?void 0:e}function My(t){let e=t.replace(/\s+/gu," ").trim();return e===""?void 0:e}var wL,Wy=d(()=>{"use strict";fi();Su();bu();wL=new Set(["toc.xhtml","nav.xhtml","contents.xhtml","tableofcontents.xhtml","cover.xhtml","titlepage.xhtml"])});function TL(t){if(t.hasEntry("META-INF/encryption.xml"))throw new Error("Encrypted EPUB is not supported: found META-INF/encryption.xml.")}async function _L(t,e){if(!(e.coverPath===void 0||e.coverMediaType===void 0))return{path:e.coverPath,mediaType:e.coverMediaType,data:await t.readBuffer(e.coverPath)}}function CL(t,e){let r=new Map;return e.map((n,i)=>Fy(t,n,[i],r))}function Fy(t,e,r,n){let i=fu(e.fragment),a=e.path===void 0?`toc:${r.join(".")}`:t.createSectionId(e.path,i),o=PL(a,n);return{hasContent:e.path!==void 0,id:o,title:e.title,path:e.path,fragment:i,wordsCount:0,children:e.children.map((s,c)=>Fy(t,s,[...r,c],n))}}function jy(t,e){return t.map(r=>{let n=e.get(r.id);return{...r,hasContent:n?.hasContent??!1,wordsCount:n?.wordsCount??0,children:jy(r.children,e)}})}function PL(t,e){let r=e.get(t)??0;return e.set(t,r+1),r===0?t:`${t}~${r+1}`}function LL(t){let e=new Map;for(let r of Gy(t)){if(r.path===void 0)continue;let n=e.get(r.path)??[];n.push({id:r.id,path:r.path,fragment:r.fragment}),e.set(r.path,n)}return e}function Gy(t){let e=[];for(let r of t)e.push(r),e.push(...Gy(r.children));return e}var Eu,ku,wi,Iu,By=d(()=>{"use strict";fi();fi();xy();Wy();Su();Eu=class t{#e;#t;constructor(e,r){this.#e=e,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(e=>new t(this.#e,e))}async open(){return await this.#e.openSection(this.#t.id)}},ku=class t{#e;#t;#r;#n;constructor(e,r,n,i){this.#e=e,this.#t=r,this.#r=n,this.#n=i}static async open(e){TL(e);let r=await Ty(e),n=await Cy(e,r),i=CL(e,n),a=LL(i),o=await ky(e,a),s=jy(i,o),c=await _L(e,r);return new t(r,c,s,new Ho(e,a))}readMeta(){return Promise.resolve(this.#e.metadata)}readCover(){return Promise.resolve(this.#t)}readSections(){return Promise.resolve(this.#r.map(e=>new Eu(this,e)))}async openSection(e){return await this.#n.openSection(e)}},wi=class{get format(){return"epub"}async openSession(e,r){let n=await Uo.open(e);try{let i=await ku.open(n);return await r(i)}finally{await n.close()}}},Iu=new wi});var Uy=d(()=>{"use strict";By()});import{createReadStream as AL}from"fs";import{stat as OL}from"fs/promises";import{basename as NL,extname as DL,resolve as vu}from"path";function WL(t){let e=NL(t,DL(t)).trim();return e===""?null:e}var ML,xu,Ru,Ei,Hy,qy,$y=d(()=>{"use strict";li();ML="root",xu=class{#e;#t;constructor(e,r=ML){this.#e=vu(e),this.#t=r}get id(){return this.#t}get hasContent(){return!0}get title(){}get children(){return[]}open(){return Promise.resolve(AL(this.#e,{encoding:"utf8"}))}},Ru=class{#e;#t;#r;constructor(e,r){this.#e=vu(e),this.#t=r,this.#r=new xu(this.#e)}readMeta(){return Promise.resolve({version:kt,sourceFormat:this.#t,title:WL(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])}},Ei=class{#e;constructor(e){this.#e=e}get format(){return this.#e}async openSession(e,r){let n=vu(e);if(!(await OL(n)).isFile())throw new Error(`Source file is not a regular file: ${n}`);return await r(new Ru(n,this.#e))}},Hy=new Ei("txt"),qy=new Ei("markdown")});var Zt=d(()=>{"use strict";li();Uy();$y();Bo()});function Tu(t){return{children:t.children.map(Tu),...t.key===void 0?{}:{key:t.key},...t.serialId===void 0?{}:{serialId:t.serialId},title:t.title}}function _u(t){let e=t?.trim();return e===void 0||e===""?void 0:e}var Cu=d(()=>{"use strict"});async function Pu(t){let e=await t.readToc(),r=e?.items.map(Tu)??[];return FL(r),e===void 0?{items:[],version:it}:{items:r,version:e.version}}function FL(t){let e=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(e.has(i.key))throw new Error(`Duplicate chapter key: ${i.key}.`);e.add(i.key),r(i.children)}};r(t)}function $o(t){let e=new Set,r=!1,n=a=>{for(let o of a){if(o.key!==void 0){if(e.has(o.key))throw new Error(`Duplicate chapter key: ${o.key}.`);e.add(o.key)}n(o.children)}},i=a=>{for(let o of a)o.key===void 0&&(o.key=pi(e),e.add(o.key),r=!0),i(o.children)};return n(t),i(t),r}async function Lu(t,e,r,n=[],i=[],a=0){for(let o of e){let s=_u(o.title)??null,c=o.key??`chapter-${o.serialId??"group"}`,u=o.serialId===void 0?[...n,...s===null?[]:[s]]:[...n,s??`Chapter ${o.serialId}`],l=[...i,c];if(o.serialId===r)return await Ky(t,o,o.serialId,{chapterPath:l,depth:a,title:s,tocPath:u});let m=await Lu(t,o.children,r,u,l,a+1);if(m!==void 0)return m}}async function qo(t,e,r=[],n=[],i=0){let a=[];for(let o of e){let s=_u(o.title)??null,c=o.key??`chapter-${o.serialId??"group"}`,u=o.serialId===void 0?[...r,...s===null?[]:[s]]:[...r,s??`Chapter ${o.serialId}`],l=[...n,c];if(o.serialId===void 0){a.push(...await qo(t,o.children,u,l,i+1));continue}a.push(await Ky(t,o,o.serialId,{chapterPath:l,depth:i,title:s,tocPath:u})),a.push(...await qo(t,o.children,u,l,i+1))}return a}async function Ky(t,e,r,n){let[i,a]=await Promise.all([t.serials.getById(r),jL(t,r)]);return{chapterId:r,childCount:e.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 GL(t,r,a.fragmentCount),title:n.title,tocPath:n.tocPath,uri:mi(n.chapterPath.join("/")),words:a.words}}async function jL(t,e){let r=await t.readDatabase(async o=>await o.queryAll(`
|
|
138
|
+
`).trim()}function pL(t){if(typeof t=="string")return t;if(Buffer.isBuffer(t))return t.toString("utf8");throw new Error("Unexpected HTML stream chunk type")}var cL,my,ys,fy=c(()=>{"use strict";on();cL=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"]),my=new Set(["script","style"]),ys=class{#e;#t;constructor(e,n){this.#e=e,this.#t=uL(n)}async openSection(e){let n=this.#t.get(e);return n===void 0?[]:[(await this.#n(n.path,n.targets)).get(e)??""]}async#n(e,n){let r=await this.#e.openReadStream(e);return r.setEncoding("utf8"),await lL(r,n)}}});import{SaxesParser as hL}from"saxes";function Mi(t){let e=new hL({xmlns:!1}),n=[],r;if(e.on("opentag",i=>{let a={name:i.name,attributes:Object.fromEntries(Object.entries(i.attributes).map(([s,d])=>[s,String(d)])),children:[],text:""},o=n[n.length-1];o===void 0?r=a:o.children.push(a),n.push(a)}),e.on("text",i=>{let a=n[n.length-1];a!==void 0&&(a.text+=i)}),e.on("cdata",i=>{let a=n[n.length-1];a!==void 0&&(a.text+=i)}),e.on("closetag",()=>{n.pop()}),e.write(t).close(),r===void 0)throw new Error("XML document is empty");return gy(r)}function Me(t,e){return t.children.find(n=>Fi(n.name)===e)}function Ct(t,e){return t.children.filter(n=>Fi(n.name)===e)}function pe(t,e){return t.attributes[e]}function Wi(t){let e=t.text;for(let n of t.children)e+=Wi(n);return e}function Fi(t){let e=t.indexOf(":");return e===-1?t:t.slice(e+1)}function gy(t){return{name:t.name,attributes:Object.freeze({...t.attributes}),children:Object.freeze(t.children.map(gy)),text:t.text}}var zu=c(()=>{"use strict"});import{posix as fL}from"path";async function by(t){let e=await bL(t),n=Mi(await t.readText(e)),r=SL(pe(n,"version")),i=Me(n,"metadata"),a=Me(n,"manifest"),o=Me(n,"spine"),s=Me(n,"guide"),d=EL(t,e,a),u=new Map([...d.values()].map(I=>[I.path,I])),l=IL(o,d),m=xL(t,e,s),p=wL(i),h=kL(d),y=RL(d,o),f=vL(d,i);return{version:r,opfPath:e,metadata:{...p,version:_t,sourceFormat:"epub"},manifest:d,manifestByPath:u,spine:l,navPath:h,ncxPath:y,coverPath:f?.path,coverMediaType:f?.mediaType,guideNonContentPaths:m}}async function bL(t){let e=Mi(await t.readText("META-INF/container.xml")),r=Me(e,"rootfiles")?.children.find(a=>Fi(a.name)==="rootfile"),i=r===void 0?void 0:pe(r,"full-path");if(i===void 0||i.trim()==="")throw new Error("EPUB container.xml does not contain an OPF rootfile");return i}function SL(t){return t?.trim().startsWith("3")===!0?3:2}function wL(t){if(t===void 0)return yy();let e=dn(t,"title"),n=dn(t,"creator"),r=dn(t,"language"),i=dn(t,"identifier"),a=dn(t,"publisher"),o=dn(t,"date"),s=dn(t,"description");return{...yy(),title:e[0]??null,authors:[...n],language:r[0]??null,identifier:i[0]??null,publisher:a[0]??null,publishedAt:o[0]??null,description:s[0]??null}}function yy(){return{version:_t,sourceFormat:"epub",title:null,authors:[],language:null,identifier:null,publisher:null,publishedAt:null,description:null}}function dn(t,e){return t.children.filter(n=>Fi(n.name)===e).map(n=>_L(Wi(n))).filter(n=>n!==void 0)}function EL(t,e,n){let r=new Map;if(n===void 0)return r;for(let i of Ct(n,"item")){let a=pe(i,"id"),o=pe(i,"href"),s=pe(i,"media-type")??"";a===void 0||o===void 0||r.set(a,{id:a,path:t.resolveRelativePath(e,o),mediaType:s,properties:new Set((pe(i,"properties")??"").split(/\s+/u).map(d=>d.trim().toLowerCase()).filter(d=>d!==""))})}return r}function IL(t,e){return t===void 0?[]:Ct(t,"itemref").map(n=>pe(n,"idref")).filter(n=>n!==void 0).map(n=>{let r=e.get(n);if(!(r===void 0||r.mediaType!=="application/xhtml+xml"&&r.mediaType!=="text/html"))return{idref:n,path:r.path,mediaType:r.mediaType}}).filter(n=>n!==void 0)}function xL(t,e,n){let r=new Set;if(n===void 0)return r;for(let i of Ct(n,"reference")){let a=(pe(i,"type")??"").trim().toLowerCase();if(!yL.has(a))continue;let o=pe(i,"href");o===void 0||o.trim()===""||r.add(t.resolveRelativePath(e,Ni(o).path))}return r}function kL(t){for(let e of t.values())if(e.properties.has("nav"))return e.path}function RL(t,e){let n=e===void 0?void 0:pe(e,"toc");if(n!==void 0)return t.get(n)?.path;for(let r of t.values())if(r.mediaType==="application/x-dtbncx+xml")return r.path}function vL(t,e){for(let r of t.values())if(r.properties.has("cover-image")&&r.mediaType.startsWith("image/"))return r;let n=TL(e);if(n!==void 0){let r=t.get(n);if(r?.mediaType.startsWith("image/")===!0)return r}for(let r of gL){let i=t.get(r);if(i?.mediaType.startsWith("image/")===!0)return i}for(let r of t.values())if(r.mediaType.startsWith("image/")&&fL.basename(r.path).toLowerCase().includes("cover"))return r}function TL(t){if(t!==void 0)for(let e of Ct(t,"meta")){if((pe(e,"name")??"").trim().toLowerCase()!=="cover")continue;let r=pe(e,"content");if(r!==void 0&&r.trim()!=="")return r.trim()}}function _L(t){let e=t.replace(/\s+/gu," ").trim();return e===""?void 0:e}var gL,yL,Qu=c(()=>{"use strict";Ai();Di();zu();gL=["cover-image","coverimg","cover-img","cover"],yL=new Set(["cover","titlepage","other.titlepage"])});import{basename as CL,extname as PL,posix as AL}from"path";import{parseDocument as LL}from"htmlparser2";async function wy(t,e){let n=await NL(t,e)??await DL(t,e)??[];if(n.length===0)return Sy(e,new Set);let r=xy(n),i=Sy(e,r);return i.length===0?n:[...n,...i]}async function NL(t,e){if(e.navPath===void 0)return;let n=LL(await t.readText(e.navPath),{decodeEntities:!0}),r=Xu(n,o=>Pt(o)!=="nav"?!1:(o.attribs?.["epub:type"]??o.attribs?.type??"").split(/\s+/u).map(d=>d.trim().toLowerCase()).includes("toc"));if(r===void 0)return;let i=Xu(r,o=>Pt(o)==="ol");if(i===void 0)return;let a=ji(i).filter(o=>Pt(o)==="li").map(o=>Ey(t,e.navPath,o)).filter(o=>o!==void 0);return a.length===0?void 0:a}async function DL(t,e){if(e.ncxPath===void 0)return;let n=Mi(await t.readText(e.ncxPath)),r=Me(n,"navMap");if(r===void 0)return;let i=Ct(r,"navPoint").map(a=>Iy(t,e.ncxPath,a)).filter(a=>a!==void 0);return i.length===0?void 0:i}function Ey(t,e,n){let r=ky(n),i=r===void 0?void 0:Ty(Ry(r)),a=r!==void 0&&Pt(r)==="a"?r.attribs?.href:void 0,o=WL(t,e,a),s=ji(n).filter(d=>Pt(d)==="ol").flatMap(d=>ji(d)).filter(d=>Pt(d)==="li").map(d=>Ey(t,e,d)).filter(d=>d!==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 Iy(t,e,n){let r=Me(n,"navLabel"),i=r===void 0?void 0:Me(r,"text"),a=i===void 0?void 0:Ty(Wi(i)),o=Me(n,"content"),s=FL(t,e,pe(o??n,"src")),d=Ct(n,"navPoint").map(u=>Iy(t,e,u)).filter(u=>u!==void 0);if(!(a===void 0&&s.path===void 0&&d.length===0))return{title:a,path:s.path,fragment:s.fragment,children:d}}function Sy(t,e){return t.spine.filter(n=>!ML(t,n.path,e)).filter(n=>!e.has(n.path)).map(n=>({title:jL(n.path),path:n.path,fragment:void 0,children:[]}))}function ML(t,e,n){if(t.guideNonContentPaths.has(e))return!0;let r=AL.basename(e).toLowerCase();return t.navPath===e?!0:OL.has(r)&&!n.has(e)}function xy(t){let e=new Set;for(let n of t){n.path!==void 0&&e.add(n.path);for(let r of xy(n.children))e.add(r)}return e}function WL(t,e,n){if(n===void 0||n.trim()==="")return{path:void 0,fragment:void 0};let{path:r,fragment:i}=Ni(n);return{path:r===""?void 0:t.resolveRelativePath(e,r),fragment:i}}function FL(t,e,n){if(n===void 0||n.trim()==="")return{path:void 0,fragment:void 0};let{path:r,fragment:i}=Ni(n);return{path:r===""?void 0:t.resolveRelativePath(e,r),fragment:i}}function Xu(t,e){if(vy(t)&&e(t))return t;for(let n of t.children??[]){let r=Xu(n,e);if(r!==void 0)return r}}function ky(t){for(let e of ji(t)){let n=Pt(e);if(n==="a"||n==="span")return e}for(let e of ji(t)){if(Pt(e)==="ol")continue;let n=ky(e);if(n!==void 0)return n}}function Ry(t){return t.type==="text"?t.data??"":(t.children??[]).map(Ry).join("")}function ji(t){return(t.children??[]).filter(vy)}function vy(t){return t.type==="tag"||t.type==="script"||t.type==="style"}function Pt(t){return t.name?.toLowerCase()}function jL(t){let e=CL(t,PL(t)).trim();return e===""?void 0:e}function Ty(t){let e=t.replace(/\s+/gu," ").trim();return e===""?void 0:e}var OL,_y=c(()=>{"use strict";Di();Qu();zu();OL=new Set(["toc.xhtml","nav.xhtml","contents.xhtml","tableofcontents.xhtml","cover.xhtml","titlepage.xhtml"])});function GL(t){if(t.hasEntry("META-INF/encryption.xml"))throw new Error("Encrypted EPUB is not supported: found META-INF/encryption.xml.")}async function BL(t,e){if(!(e.coverPath===void 0||e.coverMediaType===void 0))return{path:e.coverPath,mediaType:e.coverMediaType,data:await t.readBuffer(e.coverPath)}}function UL(t,e){let n=new Map;return e.map((r,i)=>Cy(t,r,[i],n))}function Cy(t,e,n,r){let i=Ku(e.fragment),a=e.path===void 0?`toc:${n.join(".")}`:t.createSectionId(e.path,i),o=HL(a,r);return{hasContent:e.path!==void 0,id:o,title:e.title,path:e.path,fragment:i,wordsCount:0,children:e.children.map((s,d)=>Cy(t,s,[...n,d],r))}}function Py(t,e){return t.map(n=>{let r=e.get(n.id);return{...n,hasContent:r?.hasContent??!1,wordsCount:r?.wordsCount??0,children:Py(n.children,e)}})}function HL(t,e){let n=e.get(t)??0;return e.set(t,n+1),n===0?t:`${t}~${n+1}`}function qL(t){let e=new Map;for(let n of Ay(t)){if(n.path===void 0)continue;let r=e.get(n.path)??[];r.push({id:n.id,path:n.path,fragment:n.fragment}),e.set(n.path,r)}return e}function Ay(t){let e=[];for(let n of t)e.push(n),e.push(...Ay(n.children));return e}var Vu,Yu,Gi,Zu,Ly=c(()=>{"use strict";Di();Di();fy();_y();Qu();Vu=class t{#e;#t;constructor(e,n){this.#e=e,this.#t=n}get id(){return this.#t.id}get hasContent(){return this.#t.hasContent}get title(){return this.#t.title}get wordsCount(){return this.#t.wordsCount}get children(){return this.#t.children.map(e=>new t(this.#e,e))}async open(){return await this.#e.openSection(this.#t.id)}},Yu=class t{#e;#t;#n;#r;constructor(e,n,r,i){this.#e=e,this.#t=n,this.#n=r,this.#r=i}static async open(e){GL(e);let n=await by(e),r=await wy(e,n),i=UL(e,r),a=qL(i),o=await py(e,a),s=Py(i,o),d=await BL(e,n);return new t(n,d,s,new ys(e,a))}readMeta(){return Promise.resolve(this.#e.metadata)}readCover(){return Promise.resolve(this.#t)}readSections(){return Promise.resolve(this.#n.map(e=>new Vu(this,e)))}async openSection(e){return await this.#r.openSection(e)}},Gi=class{get format(){return"epub"}async openSession(e,n){let r=await gs.open(e);try{let i=await Yu.open(r);return await n(i)}finally{await r.close()}}},Zu=new Gi});var Oy=c(()=>{"use strict";Ly()});import{createReadStream as $L}from"fs";import{stat as KL}from"fs/promises";import{basename as JL,extname as zL,resolve as nl}from"path";function XL(t){let e=JL(t,zL(t)).trim();return e===""?null:e}var QL,el,tl,Bi,Ny,Dy,My=c(()=>{"use strict";Ai();QL="root",el=class{#e;#t;constructor(e,n=QL){this.#e=nl(e),this.#t=n}get id(){return this.#t}get hasContent(){return!0}get title(){}get children(){return[]}open(){return Promise.resolve($L(this.#e,{encoding:"utf8"}))}},tl=class{#e;#t;#n;constructor(e,n){this.#e=nl(e),this.#t=n,this.#n=new el(this.#e)}readMeta(){return Promise.resolve({version:_t,sourceFormat:this.#t,title:XL(this.#e),authors:[],language:null,identifier:null,publisher:null,publishedAt:null,description:null})}readCover(){return Promise.resolve(void 0)}readSections(){return Promise.resolve([this.#n])}},Bi=class{#e;constructor(e){this.#e=e}get format(){return this.#e}async openSession(e,n){let r=nl(e);if(!(await KL(r)).isFile())throw new Error(`Source file is not a regular file: ${r}`);return await n(new tl(r,this.#e))}},Ny=new Bi("txt"),Dy=new Bi("markdown")});var cn=c(()=>{"use strict";Ai();Oy();My();fs()});function rl(t){return{children:t.children.map(rl),...t.key===void 0?{}:{key:t.key},...t.serialId===void 0?{}:{serialId:t.serialId},title:t.title}}function il(t){let e=t?.trim();return e===void 0||e===""?void 0:e}var al=c(()=>{"use strict"});async function ol(t){let e=await t.readToc(),n=e?.items.map(rl)??[];return VL(n),e===void 0?{items:[],version:ct}:{items:n,version:e.version}}function VL(t){let e=new Set,n=r=>{for(let i of r){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(e.has(i.key))throw new Error(`Duplicate chapter key: ${i.key}.`);e.add(i.key),n(i.children)}};n(t)}function Ss(t){let e=new Set,n=!1,r=a=>{for(let o of a){if(o.key!==void 0){if(e.has(o.key))throw new Error(`Duplicate chapter key: ${o.key}.`);e.add(o.key)}r(o.children)}},i=a=>{for(let o of a)o.key===void 0&&(o.key=Oi(e),e.add(o.key),n=!0),i(o.children)};return r(t),i(t),n}async function sl(t,e,n,r=[],i=[],a=0){for(let o of e){let s=il(o.title)??null,d=o.key??`chapter-${o.serialId??"group"}`,u=o.serialId===void 0?[...r,...s===null?[]:[s]]:[...r,s??`Chapter ${o.serialId}`],l=[...i,d];if(o.serialId===n)return await Wy(t,o,o.serialId,{chapterPath:l,depth:a,title:s,tocPath:u});let m=await sl(t,o.children,n,u,l,a+1);if(m!==void 0)return m}}async function bs(t,e,n=[],r=[],i=0){let a=[];for(let o of e){let s=il(o.title)??null,d=o.key??`chapter-${o.serialId??"group"}`,u=o.serialId===void 0?[...n,...s===null?[]:[s]]:[...n,s??`Chapter ${o.serialId}`],l=[...r,d];if(o.serialId===void 0){a.push(...await bs(t,o.children,u,l,i+1));continue}a.push(await Wy(t,o,o.serialId,{chapterPath:l,depth:i,title:s,tocPath:u})),a.push(...await bs(t,o.children,u,l,i+1))}return a}async function Wy(t,e,n,r){let[i,a]=await Promise.all([t.serials.getById(n),YL(t,n)]);return{chapterId:n,childCount:e.children.length,depth:r.depth,documentOrder:i?.documentOrder??n,fragmentCount:a.fragmentCount,key:r.chapterPath.at(-1)??`chapter-${n}`,path:r.chapterPath.join("/"),stage:await ZL(t,n,a.fragmentCount),title:r.title,tocPath:r.tocPath,uri:Li(r.chapterPath.join("/")),words:a.words}}async function YL(t,e){let n=await t.readDatabase(async o=>await o.queryAll(`
|
|
139
139
|
SELECT words_count
|
|
140
140
|
FROM text_sentence_records
|
|
141
141
|
WHERE kind = 1 AND chapter_id = ?
|
|
142
142
|
ORDER BY sentence_index
|
|
143
|
-
`,[e],s=>Number(s.words_count))),
|
|
143
|
+
`,[e],s=>Number(s.words_count))),r=0,i=0,a=0;for(let o of n)i>0&&i+o>600&&(r+=1,i=0),i+=o,a+=o;return i>0&&(r+=1),{fragmentCount:r,words:a}}async function ZL(t,e,n){return await t.readDatabase(async a=>await a.queryOne(`
|
|
144
144
|
SELECT COUNT(*) AS count
|
|
145
145
|
FROM text_sentence_records
|
|
146
146
|
WHERE kind = 2 AND chapter_id = ?
|
|
147
|
-
`,[e],o=>Number(o.count))??0)>0?"summarized":(await t.serials.getById(e))?.topologyReady===!0?"graphed":
|
|
147
|
+
`,[e],o=>Number(o.count))??0)>0?"summarized":(await t.serials.getById(e))?.topologyReady===!0?"graphed":n>0?"sourced":"planned"}var Ui=c(()=>{"use strict";cn();st();al()});async function Fy(t){await eO(t),await rO(t),await nO(t),await aO(t),await oO(t),await tO(t),await iO(t)}async function eO(t){(await At(t,"serials")).has("document_order")||await t.transaction(async()=>{(await At(t,"serials")).has("document_order")||await t.run(`
|
|
148
148
|
ALTER TABLE serials
|
|
149
149
|
ADD COLUMN document_order INTEGER NOT NULL DEFAULT 0
|
|
150
|
-
`)})}async function
|
|
150
|
+
`)})}async function tO(t){await t.run(`
|
|
151
151
|
CREATE INDEX IF NOT EXISTS idx_serials_document_order
|
|
152
152
|
ON serials(document_order, id)
|
|
153
|
-
`)}async function
|
|
153
|
+
`)}async function nO(t){(await At(t,"serial_states")).has("revision")||await t.transaction(async()=>{(await At(t,"serial_states")).has("revision")||await t.run(`
|
|
154
154
|
ALTER TABLE serial_states
|
|
155
155
|
ADD COLUMN revision INTEGER NOT NULL DEFAULT 0
|
|
156
|
-
`)})}async function
|
|
156
|
+
`)})}async function rO(t){await t.run(`
|
|
157
157
|
CREATE TABLE IF NOT EXISTS graph_build_parameters (
|
|
158
158
|
hash TEXT PRIMARY KEY,
|
|
159
159
|
prompt TEXT NOT NULL,
|
|
160
160
|
language TEXT,
|
|
161
161
|
created_at TEXT NOT NULL
|
|
162
162
|
)
|
|
163
|
-
`)}async function
|
|
163
|
+
`)}async function iO(t){await t.run(`
|
|
164
164
|
CREATE INDEX IF NOT EXISTS idx_serial_states_topology_parameter_hash
|
|
165
165
|
ON serial_states(topology_parameter_hash)
|
|
166
166
|
`),await t.run(`
|
|
167
167
|
CREATE INDEX IF NOT EXISTS idx_serial_states_knowledge_graph_parameter_hash
|
|
168
168
|
ON serial_states(knowledge_graph_parameter_hash)
|
|
169
|
-
`)}async function
|
|
169
|
+
`)}async function aO(t){(await At(t,"serial_states")).has("knowledge_graph_ready")||await t.transaction(async()=>{(await At(t,"serial_states")).has("knowledge_graph_ready")||await t.run(`
|
|
170
170
|
ALTER TABLE serial_states
|
|
171
171
|
ADD COLUMN knowledge_graph_ready INTEGER NOT NULL DEFAULT 0
|
|
172
|
-
`)})}async function
|
|
172
|
+
`)})}async function oO(t){let e=await At(t,"serial_states"),n=[["topology_parameter_hash","TEXT"],["knowledge_graph_parameter_hash","TEXT"]],r=[];for(let i of n)e.has(i[0])||r.push(i);r.length!==0&&await t.transaction(async()=>{let i=await At(t,"serial_states");for(let[a,o]of r)i.has(a)||await t.run(`
|
|
173
173
|
ALTER TABLE serial_states
|
|
174
174
|
ADD COLUMN ${a} ${o}
|
|
175
|
-
`)})}async function
|
|
175
|
+
`)})}async function At(t,e){let n=await t.queryAll(`PRAGMA table_info(${e})`,void 0,r=>String(r.name));return new Set(n)}var dl,cl,ul,Hi=c(()=>{"use strict";dl=`
|
|
176
176
|
CREATE TABLE IF NOT EXISTS serials (
|
|
177
177
|
id INTEGER PRIMARY KEY,
|
|
178
178
|
document_order INTEGER NOT NULL DEFAULT 0
|
|
@@ -193,11 +193,6 @@ ${h}
|
|
|
193
193
|
value INTEGER NOT NULL
|
|
194
194
|
);
|
|
195
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
196
|
CREATE TABLE IF NOT EXISTS graph_build_parameters (
|
|
202
197
|
hash TEXT PRIMARY KEY,
|
|
203
198
|
prompt TEXT NOT NULL,
|
|
@@ -205,6 +200,49 @@ ${h}
|
|
|
205
200
|
created_at TEXT NOT NULL
|
|
206
201
|
);
|
|
207
202
|
|
|
203
|
+
CREATE TABLE IF NOT EXISTS index_artifacts (
|
|
204
|
+
serial_id INTEGER NOT NULL,
|
|
205
|
+
kind TEXT NOT NULL,
|
|
206
|
+
source_revision INTEGER NOT NULL,
|
|
207
|
+
created_at TEXT NOT NULL,
|
|
208
|
+
metadata_json TEXT NOT NULL DEFAULT '{}',
|
|
209
|
+
PRIMARY KEY (serial_id, kind),
|
|
210
|
+
FOREIGN KEY (serial_id) REFERENCES serials(id)
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
CREATE INDEX IF NOT EXISTS idx_index_artifacts_kind
|
|
214
|
+
ON index_artifacts(kind, serial_id);
|
|
215
|
+
|
|
216
|
+
CREATE TABLE IF NOT EXISTS index_artifact_lexical_rows (
|
|
217
|
+
serial_id INTEGER NOT NULL,
|
|
218
|
+
kind TEXT NOT NULL,
|
|
219
|
+
row_id TEXT NOT NULL,
|
|
220
|
+
object_kind TEXT NOT NULL,
|
|
221
|
+
object_id TEXT NOT NULL,
|
|
222
|
+
sentence_index INTEGER,
|
|
223
|
+
text TEXT NOT NULL,
|
|
224
|
+
tokens_json TEXT NOT NULL,
|
|
225
|
+
metadata_json TEXT NOT NULL DEFAULT '{}',
|
|
226
|
+
PRIMARY KEY (serial_id, kind, row_id),
|
|
227
|
+
FOREIGN KEY (serial_id, kind) REFERENCES index_artifacts(serial_id, kind)
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
CREATE INDEX IF NOT EXISTS idx_index_artifact_lexical_rows_object
|
|
231
|
+
ON index_artifact_lexical_rows(serial_id, kind, object_kind, object_id);
|
|
232
|
+
|
|
233
|
+
CREATE TABLE IF NOT EXISTS index_artifact_embedding_segments (
|
|
234
|
+
serial_id INTEGER NOT NULL,
|
|
235
|
+
kind TEXT NOT NULL,
|
|
236
|
+
segment_index INTEGER NOT NULL,
|
|
237
|
+
start_sentence_index INTEGER NOT NULL,
|
|
238
|
+
end_sentence_index INTEGER NOT NULL,
|
|
239
|
+
words_count INTEGER NOT NULL,
|
|
240
|
+
text TEXT NOT NULL,
|
|
241
|
+
vector_json TEXT NOT NULL,
|
|
242
|
+
PRIMARY KEY (serial_id, kind, segment_index),
|
|
243
|
+
FOREIGN KEY (serial_id, kind) REFERENCES index_artifacts(serial_id, kind)
|
|
244
|
+
);
|
|
245
|
+
|
|
208
246
|
CREATE TABLE IF NOT EXISTS chunks (
|
|
209
247
|
id INTEGER PRIMARY KEY,
|
|
210
248
|
generation INTEGER NOT NULL,
|
|
@@ -461,7 +499,7 @@ ${h}
|
|
|
461
499
|
triple_predicate,
|
|
462
500
|
triple_object_qid
|
|
463
501
|
);
|
|
464
|
-
`,
|
|
502
|
+
`,cl=`
|
|
465
503
|
id INTEGER PRIMARY KEY,
|
|
466
504
|
archive_id INTEGER NOT NULL,
|
|
467
505
|
kind INTEGER NOT NULL,
|
|
@@ -471,14 +509,14 @@ ${h}
|
|
|
471
509
|
byte_offset INTEGER NOT NULL DEFAULT 0,
|
|
472
510
|
byte_length INTEGER NOT NULL DEFAULT 0,
|
|
473
511
|
UNIQUE(archive_id, kind, chapter_id, sentence_index)
|
|
474
|
-
`,
|
|
512
|
+
`,ul=`
|
|
475
513
|
CREATE TABLE IF NOT EXISTS search_index_state (
|
|
476
514
|
key TEXT PRIMARY KEY,
|
|
477
515
|
value TEXT NOT NULL
|
|
478
516
|
);
|
|
479
517
|
|
|
480
518
|
CREATE TABLE IF NOT EXISTS text_sentence_records (
|
|
481
|
-
${
|
|
519
|
+
${cl}
|
|
482
520
|
);
|
|
483
521
|
|
|
484
522
|
CREATE INDEX IF NOT EXISTS idx_text_sentence_records_chapter
|
|
@@ -491,6 +529,22 @@ ${Ou}
|
|
|
491
529
|
tokenize='ascii'
|
|
492
530
|
);
|
|
493
531
|
|
|
532
|
+
CREATE TABLE IF NOT EXISTS text_embedding_segments (
|
|
533
|
+
id INTEGER PRIMARY KEY,
|
|
534
|
+
archive_id INTEGER NOT NULL,
|
|
535
|
+
kind INTEGER NOT NULL,
|
|
536
|
+
chapter_id INTEGER NOT NULL,
|
|
537
|
+
start_sentence_index INTEGER NOT NULL,
|
|
538
|
+
end_sentence_index INTEGER NOT NULL,
|
|
539
|
+
words_count INTEGER NOT NULL,
|
|
540
|
+
model TEXT NOT NULL,
|
|
541
|
+
dimensions INTEGER NOT NULL,
|
|
542
|
+
vector BLOB NOT NULL
|
|
543
|
+
);
|
|
544
|
+
|
|
545
|
+
CREATE INDEX IF NOT EXISTS idx_text_embedding_segments_scope
|
|
546
|
+
ON text_embedding_segments(archive_id, kind, chapter_id, start_sentence_index, end_sentence_index);
|
|
547
|
+
|
|
494
548
|
CREATE TABLE IF NOT EXISTS search_object_properties_records (
|
|
495
549
|
id INTEGER PRIMARY KEY,
|
|
496
550
|
archive_id INTEGER NOT NULL,
|
|
@@ -519,10 +573,10 @@ ${Ou}
|
|
|
519
573
|
updated_at INTEGER NOT NULL,
|
|
520
574
|
PRIMARY KEY (archive_id, chapter_id)
|
|
521
575
|
);
|
|
522
|
-
`});function
|
|
576
|
+
`});function Gy(t,e){return t-e}function By(t){return typeof t=="object"&&t!==null&&"code"in t&&typeof t.code=="string"&&t.code.startsWith("SQLITE_CONSTRAINT")}function Uy(t,e){let n=[];for(let r=0;r<t.length;r+=e)n.push(t.slice(r,r+e));return n}function ws(t){let e=ye(t,"strength");return{fromId:g(t,"from_id"),toId:g(t,"to_id"),weight:g(t,"weight"),...e===void 0?{}:{strength:e}}}function un(t){let e=ll(t,"sentence_index"),n=ll(t,"confidence"),r=ye(t,"note");return{chapterId:g(t,"chapter_id"),...n===void 0?{}:{confidence:n},id:w(t,"id"),...r===void 0?{}:{note:r},qid:w(t,"qid"),rangeEnd:g(t,"range_end"),rangeStart:g(t,"range_start"),...e===void 0?{}:{sentenceIndex:e},surface:w(t,"surface")}}function Hy(t){let e=new Set,n=[];for(let r of t)e.has(r.id)||(e.add(r.id),n.push(r));return n}function qi(t){let e=ll(t,"confidence"),n=ye(t,"note");return{...e===void 0?{}:{confidence:e},evidenceSentenceIds:[],id:w(t,"id"),...n===void 0?{}:{note:n},predicate:w(t,"predicate"),sourceMentionId:w(t,"source_mention_id"),targetMentionId:w(t,"target_mention_id")}}function qy(t){try{return JSON.parse(t)}catch(e){throw new Error(`Invalid object metadata JSON: ${sO(e)}`)}}function ll(t,e){let n=t[e];if(n!=null){if(typeof n!="number")throw new TypeError(`Expected ${e} to be a number`);return n}}function sO(t){return t instanceof Error?t.message:String(t)}function $y(t){return t.replace(/[\\%_]/gu,e=>`\\${e}`)}function Ky(t){return t!==void 0&&Lu(t)?t:void 0}function Jy(t){return t!==void 0&&Pu(t)?t:void 0}var jy,ln=c(()=>{"use strict";k();Ke();jy=900});var cr,zy=c(()=>{"use strict";k();ln();cr=class{#e;constructor(e){this.#e=e}async countAll(){return await this.#e.queryOne(`
|
|
523
577
|
SELECT COUNT(*) AS count
|
|
524
578
|
FROM chunks
|
|
525
|
-
`,void 0,e=>
|
|
579
|
+
`,void 0,e=>g(e,"count"))??0}async create(e){return await this.#e.transaction(async()=>{await this.#e.run(`
|
|
526
580
|
INSERT INTO chunks (
|
|
527
581
|
generation,
|
|
528
582
|
serial_id,
|
|
@@ -535,7 +589,7 @@ ${Ou}
|
|
|
535
589
|
weight
|
|
536
590
|
)
|
|
537
591
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
538
|
-
`,[e.generation,e.sentenceId[0],e.sentenceId[1],e.label,e.content,e.retention??null,e.importance??null,e.wordsCount,e.weight]);let
|
|
592
|
+
`,[e.generation,e.sentenceId[0],e.sentenceId[1],e.label,e.content,e.retention??null,e.importance??null,e.wordsCount,e.weight]);let n=await this.#e.getLastInsertRowId(),r={...e,id:n};return await this.#n(r),r})}async save(e){await this.#e.transaction(async()=>{await this.#e.run(`
|
|
539
593
|
INSERT OR REPLACE INTO chunks (
|
|
540
594
|
id,
|
|
541
595
|
generation,
|
|
@@ -549,7 +603,7 @@ ${Ou}
|
|
|
549
603
|
weight
|
|
550
604
|
)
|
|
551
605
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
552
|
-
`,[e.id,e.generation,e.sentenceId[0],e.sentenceId[1],e.label,e.content,e.retention??null,e.importance??null,e.wordsCount,e.weight]),await this.#
|
|
606
|
+
`,[e.id,e.generation,e.sentenceId[0],e.sentenceId[1],e.label,e.content,e.retention??null,e.importance??null,e.wordsCount,e.weight]),await this.#n(e)})}async getById(e){let n=await this.#e.queryOne(`
|
|
553
607
|
SELECT
|
|
554
608
|
id,
|
|
555
609
|
generation,
|
|
@@ -563,7 +617,7 @@ ${Ou}
|
|
|
563
617
|
weight
|
|
564
618
|
FROM chunks
|
|
565
619
|
WHERE id = ?
|
|
566
|
-
`,[e],
|
|
620
|
+
`,[e],r=>r);if(n!==void 0)return await this.#r(n)}async listAll(){let e=await this.#e.queryAll(`
|
|
567
621
|
SELECT
|
|
568
622
|
id,
|
|
569
623
|
generation,
|
|
@@ -577,7 +631,7 @@ ${Ou}
|
|
|
577
631
|
weight
|
|
578
632
|
FROM chunks
|
|
579
633
|
ORDER BY id
|
|
580
|
-
`,void 0,
|
|
634
|
+
`,void 0,n=>n);return await Promise.all(e.map(async n=>await this.#r(n)))}async listBySentenceStartIndexes(e,n){if(n.length===0)return[];let r=await Promise.all(n.map(async i=>await this.listBySentenceRange(e,i,i)));return Hy(r.flat())}async listBySentenceRange(e,n,r){let i=await this.#e.queryAll(`
|
|
581
635
|
SELECT
|
|
582
636
|
id,
|
|
583
637
|
generation,
|
|
@@ -593,7 +647,7 @@ ${Ou}
|
|
|
593
647
|
WHERE serial_id = ?
|
|
594
648
|
AND sentence_index BETWEEN ? AND ?
|
|
595
649
|
ORDER BY id
|
|
596
|
-
`,[e,r
|
|
650
|
+
`,[e,n,r],a=>a);return await Promise.all(i.map(async a=>await this.#r(a)))}async listBySerial(e){let n=await this.#e.queryAll(`
|
|
597
651
|
SELECT
|
|
598
652
|
id,
|
|
599
653
|
generation,
|
|
@@ -608,29 +662,29 @@ ${Ou}
|
|
|
608
662
|
FROM chunks
|
|
609
663
|
WHERE serial_id = ?
|
|
610
664
|
ORDER BY id
|
|
611
|
-
`,[e],
|
|
665
|
+
`,[e],r=>r);return await Promise.all(n.map(async r=>await this.#r(r)))}async getMaxId(){return await this.#e.queryOne(`
|
|
612
666
|
SELECT MAX(id) AS id
|
|
613
667
|
FROM chunks
|
|
614
|
-
`,void 0,e=>{let
|
|
668
|
+
`,void 0,e=>{let n=e.id;return typeof n=="number"?n:0})??0}async listFragmentPairs(){return await this.#e.queryAll(`
|
|
615
669
|
SELECT DISTINCT serial_id, sentence_index
|
|
616
670
|
FROM chunks
|
|
617
671
|
ORDER BY serial_id, sentence_index
|
|
618
|
-
`,void 0,e=>[
|
|
672
|
+
`,void 0,e=>[g(e,"serial_id"),g(e,"sentence_index")])}async#t(e){return await this.#e.queryAll(`
|
|
619
673
|
SELECT serial_id, sentence_index
|
|
620
674
|
FROM chunk_sentences
|
|
621
675
|
WHERE chunk_id = ?
|
|
622
676
|
ORDER BY serial_id, sentence_index
|
|
623
|
-
`,[e],
|
|
677
|
+
`,[e],n=>[g(n,"serial_id"),g(n,"sentence_index")])}async#n(e){await this.#e.run(`
|
|
624
678
|
DELETE FROM chunk_sentences
|
|
625
679
|
WHERE chunk_id = ?
|
|
626
|
-
`,[e.id]);for(let
|
|
680
|
+
`,[e.id]);for(let n of e.sentenceIds)await this.#e.run(`
|
|
627
681
|
INSERT INTO chunk_sentences (
|
|
628
682
|
chunk_id,
|
|
629
683
|
serial_id,
|
|
630
684
|
sentence_index
|
|
631
685
|
)
|
|
632
686
|
VALUES (?, ?, ?)
|
|
633
|
-
`,[e.id,
|
|
687
|
+
`,[e.id,n[0],n[1]])}async#r(e){let n=g(e,"id"),r=Ky(ye(e,"importance")),i=Jy(ye(e,"retention"));return{content:w(e,"content"),generation:g(e,"generation"),id:n,label:w(e,"label"),sentenceId:[g(e,"serial_id"),g(e,"sentence_index")],sentenceIds:await this.#t(n),wordsCount:g(e,"wordsCount"),weight:g(e,"weight"),...r===void 0?{}:{importance:r},...i===void 0?{}:{retention:i}}}}});var ur,Qy=c(()=>{"use strict";k();ur=class{#e;constructor(e){this.#e=e}async save(e){await this.#e.run(`
|
|
634
688
|
INSERT OR REPLACE INTO sentence_groups (
|
|
635
689
|
serial_id,
|
|
636
690
|
group_id,
|
|
@@ -638,30 +692,30 @@ ${Ou}
|
|
|
638
692
|
end_sentence_index
|
|
639
693
|
)
|
|
640
694
|
VALUES (?, ?, ?, ?)
|
|
641
|
-
`,[e.serialId,e.groupId,e.startSentenceIndex,e.endSentenceIndex])}async saveMany(e){await this.#e.transaction(async()=>{for(let
|
|
695
|
+
`,[e.serialId,e.groupId,e.startSentenceIndex,e.endSentenceIndex])}async saveMany(e){await this.#e.transaction(async()=>{for(let n of e)await this.save(n)})}async listBySerial(e){return await this.#e.queryAll(`
|
|
642
696
|
SELECT serial_id, group_id, start_sentence_index, end_sentence_index
|
|
643
697
|
FROM sentence_groups
|
|
644
698
|
WHERE serial_id = ?
|
|
645
699
|
ORDER BY group_id, start_sentence_index
|
|
646
|
-
`,[e],
|
|
700
|
+
`,[e],n=>({serialId:g(n,"serial_id"),groupId:g(n,"group_id"),startSentenceIndex:g(n,"start_sentence_index"),endSentenceIndex:g(n,"end_sentence_index")}))}async listSerialIds(){return await this.#e.queryAll(`
|
|
647
701
|
SELECT DISTINCT serial_id
|
|
648
702
|
FROM sentence_groups
|
|
649
703
|
ORDER BY serial_id
|
|
650
|
-
`,void 0,e=>
|
|
704
|
+
`,void 0,e=>g(e,"serial_id"))}async listGroupIdsForSerial(e){return await this.#e.queryAll(`
|
|
651
705
|
SELECT DISTINCT group_id
|
|
652
706
|
FROM sentence_groups
|
|
653
707
|
WHERE serial_id = ?
|
|
654
708
|
ORDER BY group_id
|
|
655
|
-
`,[e],
|
|
709
|
+
`,[e],n=>g(n,"group_id"))}}});var lr,Xy=c(()=>{"use strict";k();Jo();lr=class{#e;constructor(e){this.#e=e}async save(e){let n=bi({language:e.language??null,prompt:e.prompt}),r=new Date().toISOString();return await this.#e.run(`
|
|
656
710
|
INSERT OR IGNORE INTO graph_build_parameters (
|
|
657
711
|
hash, prompt, language, created_at
|
|
658
712
|
)
|
|
659
713
|
VALUES (?, ?, ?, ?)
|
|
660
|
-
`,[
|
|
714
|
+
`,[n,e.prompt,e.language??null,r]),await this.getByHash(n)}async getByHash(e){return await this.#e.queryOne(`
|
|
661
715
|
SELECT hash, prompt, language, created_at
|
|
662
716
|
FROM graph_build_parameters
|
|
663
717
|
WHERE hash = ?
|
|
664
|
-
`,[e],
|
|
718
|
+
`,[e],n=>{let r=ye(n,"language");return{createdAt:w(n,"created_at"),hash:w(n,"hash"),...r===void 0?{}:{language:r},prompt:w(n,"prompt")}})}async deleteUnreferenced(){await this.#e.run(`
|
|
665
719
|
DELETE FROM graph_build_parameters
|
|
666
720
|
WHERE hash NOT IN (
|
|
667
721
|
SELECT topology_parameter_hash
|
|
@@ -672,7 +726,125 @@ ${Ou}
|
|
|
672
726
|
FROM serial_states
|
|
673
727
|
WHERE knowledge_graph_parameter_hash IS NOT NULL
|
|
674
728
|
)
|
|
675
|
-
`)}}});
|
|
729
|
+
`)}}});function ml(t){return{createdAt:w(t,"created_at"),kind:uO(w(t,"kind")),metadata:Yy(w(t,"metadata_json")),serialId:g(t,"serial_id"),sourceRevision:g(t,"source_revision")}}function dO(t){let e=Vy(t,"sentence_index");return{metadata:Yy(w(t,"metadata_json")),objectId:w(t,"object_id"),objectKind:w(t,"object_kind"),rowId:w(t,"row_id"),...e===void 0?{}:{sentenceIndex:e},text:w(t,"text"),tokens:lO(w(t,"tokens_json"))}}function cO(t){return{endSentenceIndex:g(t,"end_sentence_index"),segmentIndex:g(t,"segment_index"),startSentenceIndex:g(t,"start_sentence_index"),text:w(t,"text"),vector:mO(w(t,"vector_json")),wordsCount:g(t,"words_count")}}function uO(t){if(Ou.includes(t))return t;throw new Error(`Unknown index artifact kind: ${t}`)}function Vy(t,e){let n=t[e];if(n!=null){if(typeof n!="number")throw new TypeError(`Expected ${e} to be a number`);return n}}function Yy(t){let e=JSON.parse(t);if(typeof e!="object"||e===null||Array.isArray(e))throw new TypeError("Expected artifact metadata to be a JSON object");return e}function lO(t){let e=JSON.parse(t);if(!Array.isArray(e)||e.some(n=>typeof n!="string"))throw new TypeError("Expected artifact tokens to be a string array");return e}function mO(t){let e=JSON.parse(t);if(!Array.isArray(e)||e.some(n=>typeof n!="number"))throw new TypeError("Expected artifact vector to be a number array");return e}var mr,Zy=c(()=>{"use strict";k();Ke();mr=class{#e;constructor(e){this.#e=e}async get(e,n){return await this.#e.queryOne(`
|
|
730
|
+
SELECT serial_id, kind, source_revision, created_at, metadata_json
|
|
731
|
+
FROM index_artifacts
|
|
732
|
+
WHERE serial_id = ?
|
|
733
|
+
AND kind = ?
|
|
734
|
+
`,[e,n],ml)}async list(e){return e===void 0?await this.#e.queryAll(`
|
|
735
|
+
SELECT serial_id, kind, source_revision, created_at, metadata_json
|
|
736
|
+
FROM index_artifacts
|
|
737
|
+
ORDER BY serial_id, kind
|
|
738
|
+
`,void 0,ml):await this.#e.queryAll(`
|
|
739
|
+
SELECT serial_id, kind, source_revision, created_at, metadata_json
|
|
740
|
+
FROM index_artifacts
|
|
741
|
+
WHERE kind = ?
|
|
742
|
+
ORDER BY serial_id
|
|
743
|
+
`,[e],ml)}async listCoverage(e){return await this.#e.queryAll(`
|
|
744
|
+
SELECT
|
|
745
|
+
serials.id AS serial_id,
|
|
746
|
+
COALESCE(serial_states.revision, 0) AS serial_revision,
|
|
747
|
+
index_artifacts.source_revision AS source_revision
|
|
748
|
+
FROM serials
|
|
749
|
+
LEFT JOIN serial_states
|
|
750
|
+
ON serial_states.serial_id = serials.id
|
|
751
|
+
LEFT JOIN index_artifacts
|
|
752
|
+
ON index_artifacts.serial_id = serials.id
|
|
753
|
+
AND index_artifacts.kind = ?
|
|
754
|
+
ORDER BY serials.document_order, serials.id
|
|
755
|
+
`,[e],n=>{let r=g(n,"serial_revision"),i=Vy(n,"source_revision");return{current:i===r,kind:e,serialId:g(n,"serial_id"),serialRevision:r,...i===void 0?{}:{sourceRevision:i}}})}async listLexicalRows(e,n="fts"){return await this.#e.queryAll(`
|
|
756
|
+
SELECT
|
|
757
|
+
row_id,
|
|
758
|
+
object_kind,
|
|
759
|
+
object_id,
|
|
760
|
+
sentence_index,
|
|
761
|
+
text,
|
|
762
|
+
tokens_json,
|
|
763
|
+
metadata_json
|
|
764
|
+
FROM index_artifact_lexical_rows
|
|
765
|
+
WHERE serial_id = ?
|
|
766
|
+
AND kind = ?
|
|
767
|
+
ORDER BY row_id
|
|
768
|
+
`,[e,n],dO)}async listEmbeddingSegments(e,n){return await this.#e.queryAll(`
|
|
769
|
+
SELECT
|
|
770
|
+
segment_index,
|
|
771
|
+
start_sentence_index,
|
|
772
|
+
end_sentence_index,
|
|
773
|
+
words_count,
|
|
774
|
+
text,
|
|
775
|
+
vector_json
|
|
776
|
+
FROM index_artifact_embedding_segments
|
|
777
|
+
WHERE serial_id = ?
|
|
778
|
+
AND kind = ?
|
|
779
|
+
ORDER BY segment_index
|
|
780
|
+
`,[e,n],cO)}async replaceFts(e){await this.#e.transaction(async()=>{await this.#t({kind:"fts",metadata:e.metadata??{},serialId:e.serialId,sourceRevision:e.sourceRevision}),await this.#e.run(`
|
|
781
|
+
DELETE FROM index_artifact_lexical_rows
|
|
782
|
+
WHERE serial_id = ?
|
|
783
|
+
AND kind = 'fts'
|
|
784
|
+
`,[e.serialId]);for(let n of e.lexicalRows)await this.#e.run(`
|
|
785
|
+
INSERT INTO index_artifact_lexical_rows (
|
|
786
|
+
serial_id,
|
|
787
|
+
kind,
|
|
788
|
+
row_id,
|
|
789
|
+
object_kind,
|
|
790
|
+
object_id,
|
|
791
|
+
sentence_index,
|
|
792
|
+
text,
|
|
793
|
+
tokens_json,
|
|
794
|
+
metadata_json
|
|
795
|
+
)
|
|
796
|
+
VALUES (?, 'fts', ?, ?, ?, ?, ?, ?, ?)
|
|
797
|
+
`,[e.serialId,n.rowId,n.objectKind,n.objectId,n.sentenceIndex??null,n.text,JSON.stringify(n.tokens),JSON.stringify(n.metadata)])})}async replaceEmbedding(e){await this.#e.transaction(async()=>{await this.#t({kind:e.kind,metadata:e.metadata??{},serialId:e.serialId,sourceRevision:e.sourceRevision}),await this.#e.run(`
|
|
798
|
+
DELETE FROM index_artifact_embedding_segments
|
|
799
|
+
WHERE serial_id = ?
|
|
800
|
+
AND kind = ?
|
|
801
|
+
`,[e.serialId,e.kind]);for(let n of e.segments)await this.#e.run(`
|
|
802
|
+
INSERT INTO index_artifact_embedding_segments (
|
|
803
|
+
serial_id,
|
|
804
|
+
kind,
|
|
805
|
+
segment_index,
|
|
806
|
+
start_sentence_index,
|
|
807
|
+
end_sentence_index,
|
|
808
|
+
words_count,
|
|
809
|
+
text,
|
|
810
|
+
vector_json
|
|
811
|
+
)
|
|
812
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
813
|
+
`,[e.serialId,e.kind,n.segmentIndex,n.startSentenceIndex,n.endSentenceIndex,n.wordsCount,n.text,JSON.stringify(n.vector)])})}async delete(e,n){await this.#e.transaction(async()=>{await this.#e.run(`
|
|
814
|
+
DELETE FROM index_artifact_lexical_rows
|
|
815
|
+
WHERE serial_id = ?
|
|
816
|
+
AND kind = ?
|
|
817
|
+
`,[e,n]),await this.#e.run(`
|
|
818
|
+
DELETE FROM index_artifact_embedding_segments
|
|
819
|
+
WHERE serial_id = ?
|
|
820
|
+
AND kind = ?
|
|
821
|
+
`,[e,n]),await this.#e.run(`
|
|
822
|
+
DELETE FROM index_artifacts
|
|
823
|
+
WHERE serial_id = ?
|
|
824
|
+
AND kind = ?
|
|
825
|
+
`,[e,n])})}async deleteBySerial(e){await this.#e.transaction(async()=>{await this.#e.run(`
|
|
826
|
+
DELETE FROM index_artifact_lexical_rows
|
|
827
|
+
WHERE serial_id = ?
|
|
828
|
+
`,[e]),await this.#e.run(`
|
|
829
|
+
DELETE FROM index_artifact_embedding_segments
|
|
830
|
+
WHERE serial_id = ?
|
|
831
|
+
`,[e]),await this.#e.run(`
|
|
832
|
+
DELETE FROM index_artifacts
|
|
833
|
+
WHERE serial_id = ?
|
|
834
|
+
`,[e])})}async#t(e){await this.#e.run(`
|
|
835
|
+
INSERT INTO index_artifacts (
|
|
836
|
+
serial_id,
|
|
837
|
+
kind,
|
|
838
|
+
source_revision,
|
|
839
|
+
created_at,
|
|
840
|
+
metadata_json
|
|
841
|
+
)
|
|
842
|
+
VALUES (?, ?, ?, ?, ?)
|
|
843
|
+
ON CONFLICT(serial_id, kind) DO UPDATE SET
|
|
844
|
+
source_revision = excluded.source_revision,
|
|
845
|
+
created_at = excluded.created_at,
|
|
846
|
+
metadata_json = excluded.metadata_json
|
|
847
|
+
`,[e.serialId,e.kind,e.sourceRevision,new Date().toISOString(),JSON.stringify(e.metadata)])}}});var pr,eb=c(()=>{"use strict";k();ln();pr=class{#e;constructor(e){this.#e=e}async save(e){await this.#e.transaction(async()=>{await this.#t(e)})}async#t(e){await this.#e.run(`
|
|
676
848
|
INSERT OR REPLACE INTO mention_links (
|
|
677
849
|
id,
|
|
678
850
|
source_mention_id,
|
|
@@ -685,14 +857,14 @@ ${Ou}
|
|
|
685
857
|
`,[e.id,e.sourceMentionId,e.targetMentionId,e.predicate,e.confidence??null,e.note??null]),await this.#e.run(`
|
|
686
858
|
DELETE FROM mention_link_evidence_sentences
|
|
687
859
|
WHERE link_id = ?
|
|
688
|
-
`,[e.id]);for(let[r
|
|
860
|
+
`,[e.id]);for(let[n,r]of e.evidenceSentenceIds)await this.#e.run(`
|
|
689
861
|
INSERT INTO mention_link_evidence_sentences (
|
|
690
862
|
link_id,
|
|
691
863
|
chapter_id,
|
|
692
864
|
sentence_index
|
|
693
865
|
)
|
|
694
866
|
VALUES (?, ?, ?)
|
|
695
|
-
`,[e.id,r
|
|
867
|
+
`,[e.id,n,r])}async saveMany(e){await this.#e.transaction(async()=>{for(let n of e)await this.#t(n)})}async getById(e){let n=await this.#e.queryOne(`
|
|
696
868
|
SELECT
|
|
697
869
|
id,
|
|
698
870
|
source_mention_id,
|
|
@@ -702,7 +874,7 @@ ${Ou}
|
|
|
702
874
|
note
|
|
703
875
|
FROM mention_links
|
|
704
876
|
WHERE id = ?
|
|
705
|
-
`,[e],
|
|
877
|
+
`,[e],qi);return n===void 0?void 0:await this.#r(n)}async listAll(){let e=await this.#e.queryAll(`
|
|
706
878
|
SELECT
|
|
707
879
|
id,
|
|
708
880
|
source_mention_id,
|
|
@@ -712,7 +884,7 @@ ${Ou}
|
|
|
712
884
|
note
|
|
713
885
|
FROM mention_links
|
|
714
886
|
ORDER BY id
|
|
715
|
-
`,[],
|
|
887
|
+
`,[],qi);return await this.#n(e)}async countByTriple(e){return await this.#e.queryOne(`
|
|
716
888
|
SELECT count(*) AS count
|
|
717
889
|
FROM mention_links
|
|
718
890
|
INNER JOIN mentions AS source_mentions
|
|
@@ -723,7 +895,7 @@ ${Ou}
|
|
|
723
895
|
AND mention_links.predicate = ?
|
|
724
896
|
AND target_mentions.qid = ?
|
|
725
897
|
${e.chapterId===void 0?"":"AND source_mentions.chapter_id = ?"}
|
|
726
|
-
`,[e.subjectQid,e.predicate,e.objectQid,...e.chapterId===void 0?[]:[e.chapterId]],
|
|
898
|
+
`,[e.subjectQid,e.predicate,e.objectQid,...e.chapterId===void 0?[]:[e.chapterId]],n=>g(n,"count"))??0}async listByTriple(e){let n=e.order==="desc"?"DESC":"ASC",r=e.limit===void 0?e.offset===void 0?"":"LIMIT -1":"LIMIT ?",i=await this.#e.queryAll(`
|
|
727
899
|
SELECT
|
|
728
900
|
mention_links.id AS id,
|
|
729
901
|
mention_links.source_mention_id AS source_mention_id,
|
|
@@ -743,13 +915,13 @@ ${Ou}
|
|
|
743
915
|
AND target_mentions.qid = ?
|
|
744
916
|
${e.chapterId===void 0?"":"AND source_mentions.chapter_id = ?"}
|
|
745
917
|
ORDER BY
|
|
746
|
-
source_serials.document_order ${
|
|
747
|
-
source_mentions.chapter_id ${
|
|
748
|
-
source_mentions.sentence_index ${
|
|
749
|
-
mention_links.id ${
|
|
750
|
-
${
|
|
918
|
+
source_serials.document_order ${n},
|
|
919
|
+
source_mentions.chapter_id ${n},
|
|
920
|
+
source_mentions.sentence_index ${n},
|
|
921
|
+
mention_links.id ${n}
|
|
922
|
+
${r}
|
|
751
923
|
${e.offset===void 0?"":"OFFSET ?"}
|
|
752
|
-
`,[e.subjectQid,e.predicate,e.objectQid,...e.chapterId===void 0?[]:[e.chapterId],...e.limit===void 0?[]:[e.limit],...e.offset===void 0?[]:[e.offset]],
|
|
924
|
+
`,[e.subjectQid,e.predicate,e.objectQid,...e.chapterId===void 0?[]:[e.chapterId],...e.limit===void 0?[]:[e.limit],...e.offset===void 0?[]:[e.offset]],qi);return await this.#n(i)}async listByChapter(e){let n=await this.#e.queryAll(`
|
|
753
925
|
SELECT
|
|
754
926
|
mention_links.id AS id,
|
|
755
927
|
mention_links.source_mention_id AS source_mention_id,
|
|
@@ -765,7 +937,7 @@ ${Ou}
|
|
|
765
937
|
WHERE source_mentions.chapter_id = ?
|
|
766
938
|
OR target_mentions.chapter_id = ?
|
|
767
939
|
ORDER BY mention_links.id
|
|
768
|
-
`,[e,e],
|
|
940
|
+
`,[e,e],qi);return await this.#n(n)}async deleteByChapter(e){await this.#e.transaction(async()=>{await this.#e.run(`
|
|
769
941
|
DELETE FROM mention_link_evidence_sentences
|
|
770
942
|
WHERE link_id IN (
|
|
771
943
|
SELECT mention_links.id
|
|
@@ -789,17 +961,17 @@ ${Ou}
|
|
|
789
961
|
FROM mentions
|
|
790
962
|
WHERE chapter_id = ?
|
|
791
963
|
)
|
|
792
|
-
`,[e,e])})}async#
|
|
964
|
+
`,[e,e])})}async#n(e){if(e.length===0)return[];let n=new Map,r=[...new Set(e.map(i=>i.id))];for(let i of Uy(r,jy)){let a=i.map(()=>"?").join(", "),o=await this.#e.queryAll(`
|
|
793
965
|
SELECT link_id, chapter_id, sentence_index
|
|
794
966
|
FROM mention_link_evidence_sentences
|
|
795
967
|
WHERE link_id IN (${a})
|
|
796
968
|
ORDER BY link_id, chapter_id, sentence_index
|
|
797
|
-
`,i,s=>({linkId:
|
|
969
|
+
`,i,s=>({linkId:w(s,"link_id"),sentenceId:[g(s,"chapter_id"),g(s,"sentence_index")]}));for(let s of o){let d=n.get(s.linkId)??[];d.push(s.sentenceId),n.set(s.linkId,d)}}return e.map(i=>({...i,evidenceSentenceIds:n.get(i.id)??[]}))}async#r(e){let n=await this.#e.queryAll(`
|
|
798
970
|
SELECT chapter_id, sentence_index
|
|
799
971
|
FROM mention_link_evidence_sentences
|
|
800
972
|
WHERE link_id = ?
|
|
801
973
|
ORDER BY chapter_id, sentence_index
|
|
802
|
-
`,[e.id],
|
|
974
|
+
`,[e.id],r=>[g(r,"chapter_id"),g(r,"sentence_index")]);return{...e,evidenceSentenceIds:n}}}});var hr,tb=c(()=>{"use strict";k();ln();hr=class{#e;constructor(e){this.#e=e}async save(e){await this.#e.run(`
|
|
803
975
|
INSERT OR REPLACE INTO mentions (
|
|
804
976
|
id,
|
|
805
977
|
chapter_id,
|
|
@@ -812,7 +984,7 @@ ${Ou}
|
|
|
812
984
|
note
|
|
813
985
|
)
|
|
814
986
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
815
|
-
`,[e.id,e.chapterId,e.sentenceIndex??null,e.rangeStart,e.rangeEnd,e.surface,e.qid,e.confidence??null,e.note??null])}async saveMany(e){await this.#e.transaction(async()=>{for(let
|
|
987
|
+
`,[e.id,e.chapterId,e.sentenceIndex??null,e.rangeStart,e.rangeEnd,e.surface,e.qid,e.confidence??null,e.note??null])}async saveMany(e){await this.#e.transaction(async()=>{for(let n of e)await this.save(n)})}async getById(e){return await this.#e.queryOne(`
|
|
816
988
|
SELECT
|
|
817
989
|
id,
|
|
818
990
|
chapter_id,
|
|
@@ -825,7 +997,7 @@ ${Ou}
|
|
|
825
997
|
note
|
|
826
998
|
FROM mentions
|
|
827
999
|
WHERE id = ?
|
|
828
|
-
`,[e],
|
|
1000
|
+
`,[e],un)}async listAll(){return await this.#e.queryAll(`
|
|
829
1001
|
SELECT
|
|
830
1002
|
id,
|
|
831
1003
|
chapter_id,
|
|
@@ -838,12 +1010,12 @@ ${Ou}
|
|
|
838
1010
|
note
|
|
839
1011
|
FROM mentions
|
|
840
1012
|
ORDER BY chapter_id, sentence_index, range_start, range_end, id
|
|
841
|
-
`,void 0,
|
|
1013
|
+
`,void 0,un)}async countByQid(e,n={}){return await this.#e.queryOne(`
|
|
842
1014
|
SELECT count(*) AS count
|
|
843
1015
|
FROM mentions
|
|
844
1016
|
WHERE qid = ?
|
|
845
|
-
${
|
|
846
|
-
`,
|
|
1017
|
+
${n.chapterId===void 0?"":"AND chapter_id = ?"}
|
|
1018
|
+
`,n.chapterId===void 0?[e]:[e,n.chapterId],r=>g(r,"count"))??0}async listByQid(e,n={}){let r=n.order==="desc"?"DESC":"ASC",i=n.limit===void 0?n.offset===void 0?"":"LIMIT -1":"LIMIT ?";return await this.#e.queryAll(`
|
|
847
1019
|
SELECT
|
|
848
1020
|
mentions.id AS id,
|
|
849
1021
|
mentions.chapter_id AS chapter_id,
|
|
@@ -858,24 +1030,24 @@ ${Ou}
|
|
|
858
1030
|
INNER JOIN serials
|
|
859
1031
|
ON serials.id = mentions.chapter_id
|
|
860
1032
|
WHERE mentions.qid = ?
|
|
861
|
-
${
|
|
1033
|
+
${n.chapterId===void 0?"":"AND mentions.chapter_id = ?"}
|
|
862
1034
|
ORDER BY
|
|
863
|
-
serials.document_order ${
|
|
864
|
-
mentions.chapter_id ${
|
|
865
|
-
mentions.sentence_index ${
|
|
866
|
-
mentions.range_start ${
|
|
867
|
-
mentions.range_end ${
|
|
868
|
-
mentions.id ${
|
|
1035
|
+
serials.document_order ${r},
|
|
1036
|
+
mentions.chapter_id ${r},
|
|
1037
|
+
mentions.sentence_index ${r},
|
|
1038
|
+
mentions.range_start ${r},
|
|
1039
|
+
mentions.range_end ${r},
|
|
1040
|
+
mentions.id ${r}
|
|
869
1041
|
${i}
|
|
870
|
-
${
|
|
871
|
-
`,[e,...
|
|
1042
|
+
${n.offset===void 0?"":"OFFSET ?"}
|
|
1043
|
+
`,[e,...n.chapterId===void 0?[]:[n.chapterId],...n.limit===void 0?[]:[n.limit],...n.offset===void 0?[]:[n.offset]],un)}async listLabelsByQid(e,n={}){return await this.#e.queryAll(`
|
|
872
1044
|
SELECT surface
|
|
873
1045
|
FROM mentions
|
|
874
1046
|
WHERE qid = ?
|
|
875
|
-
${
|
|
1047
|
+
${n.chapterId===void 0?"":"AND chapter_id = ?"}
|
|
876
1048
|
GROUP BY surface
|
|
877
1049
|
ORDER BY count(*) DESC, surface
|
|
878
|
-
`,
|
|
1050
|
+
`,n.chapterId===void 0?[e]:[e,n.chapterId],r=>w(r,"surface"))}async listBySurfaces(e){let n=[...new Set(e.map(r=>r.trim()))].filter(r=>r!=="");return n.length===0?[]:await this.#e.queryAll(`
|
|
879
1051
|
SELECT
|
|
880
1052
|
id,
|
|
881
1053
|
chapter_id,
|
|
@@ -887,9 +1059,9 @@ ${Ou}
|
|
|
887
1059
|
confidence,
|
|
888
1060
|
note
|
|
889
1061
|
FROM mentions
|
|
890
|
-
WHERE surface IN (${
|
|
1062
|
+
WHERE surface IN (${n.map(()=>"?").join(", ")})
|
|
891
1063
|
ORDER BY chapter_id, sentence_index, range_start, range_end, id
|
|
892
|
-
`,
|
|
1064
|
+
`,n,un)}async listBySurfaceTerms(e){let n=[...new Set(e.map(i=>i.trim().toLowerCase()))].filter(i=>i!=="");if(n.length===0)return[];let r=n.map(()=>"lower(surface) LIKE ? ESCAPE '\\'").join(" OR ");return await this.#e.queryAll(`
|
|
893
1065
|
SELECT
|
|
894
1066
|
id,
|
|
895
1067
|
chapter_id,
|
|
@@ -901,9 +1073,9 @@ ${Ou}
|
|
|
901
1073
|
confidence,
|
|
902
1074
|
note
|
|
903
1075
|
FROM mentions
|
|
904
|
-
WHERE ${
|
|
1076
|
+
WHERE ${r}
|
|
905
1077
|
ORDER BY chapter_id, sentence_index, range_start, range_end, id
|
|
906
|
-
`,
|
|
1078
|
+
`,n.map(i=>`%${$y(i)}%`),un)}async listByChapter(e){return await this.#e.queryAll(`
|
|
907
1079
|
SELECT
|
|
908
1080
|
id,
|
|
909
1081
|
chapter_id,
|
|
@@ -917,15 +1089,15 @@ ${Ou}
|
|
|
917
1089
|
FROM mentions
|
|
918
1090
|
WHERE chapter_id = ?
|
|
919
1091
|
ORDER BY sentence_index, range_start, range_end, id
|
|
920
|
-
`,[e],
|
|
1092
|
+
`,[e],un)}async deleteByChapter(e){await this.#e.run(`
|
|
921
1093
|
DELETE FROM mentions
|
|
922
1094
|
WHERE chapter_id = ?
|
|
923
|
-
`,[e])}}});var
|
|
1095
|
+
`,[e])}}});var fr,nb=c(()=>{"use strict";k();ln();fr=class{#e;constructor(e){this.#e=e}async getMap(e){let n=await this.#e.queryAll(`
|
|
924
1096
|
SELECT key, value_json
|
|
925
1097
|
FROM object_metadata
|
|
926
1098
|
WHERE object_path = ?
|
|
927
1099
|
ORDER BY key
|
|
928
|
-
`,[e],i=>({key:
|
|
1100
|
+
`,[e],i=>({key:w(i,"key"),value:qy(w(i,"value_json"))})),r={};for(let i of n)r[i.key]=i.value;return r}async replaceMap(e,n){await this.#e.transaction(async()=>{await this.clear(e.objectPath);for(let[r,i]of Object.entries(n))await this.put(e,r,i)})}async put(e,n,r){await this.#e.run(`
|
|
929
1101
|
INSERT INTO object_metadata (
|
|
930
1102
|
object_kind,
|
|
931
1103
|
object_path,
|
|
@@ -950,11 +1122,11 @@ ${Ou}
|
|
|
950
1122
|
triple_subject_qid = excluded.triple_subject_qid,
|
|
951
1123
|
triple_predicate = excluded.triple_predicate,
|
|
952
1124
|
triple_object_qid = excluded.triple_object_qid
|
|
953
|
-
`,[e.kind,e.objectPath,
|
|
1125
|
+
`,[e.kind,e.objectPath,n,JSON.stringify(r),new Date().toISOString(),e.chapterId??null,e.chunkId??null,e.entityQid??null,e.tripleSubjectQid??null,e.triplePredicate??null,e.tripleObjectQid??null])}async deleteKey(e,n){await this.#e.run(`
|
|
954
1126
|
DELETE FROM object_metadata
|
|
955
1127
|
WHERE object_path = ?
|
|
956
1128
|
AND key = ?
|
|
957
|
-
`,[e,
|
|
1129
|
+
`,[e,n])}async clear(e){await this.#e.run(`
|
|
958
1130
|
DELETE FROM object_metadata
|
|
959
1131
|
WHERE object_path = ?
|
|
960
1132
|
`,[e])}async deleteChapterSubtree(e){await this.#e.run(`
|
|
@@ -990,17 +1162,17 @@ ${Ou}
|
|
|
990
1162
|
AND mention_links.predicate = object_metadata.triple_predicate
|
|
991
1163
|
AND target_mentions.qid = object_metadata.triple_object_qid
|
|
992
1164
|
)
|
|
993
|
-
`)}}});var
|
|
1165
|
+
`)}}});var gr,rb=c(()=>{"use strict";k();ln();gr=class{#e;constructor(e){this.#e=e}async countAll(){return await this.#e.queryOne(`
|
|
994
1166
|
SELECT COUNT(*) AS count
|
|
995
1167
|
FROM reading_edges
|
|
996
|
-
`,void 0,e=>
|
|
1168
|
+
`,void 0,e=>g(e,"count"))??0}async save(e){await this.#e.run(`
|
|
997
1169
|
INSERT OR REPLACE INTO reading_edges (from_id, to_id, strength, weight)
|
|
998
1170
|
VALUES (?, ?, ?, ?)
|
|
999
1171
|
`,[e.fromId,e.toId,e.strength??null,e.weight])}async listAll(){return await this.#e.queryAll(`
|
|
1000
1172
|
SELECT from_id, to_id, strength, weight
|
|
1001
1173
|
FROM reading_edges
|
|
1002
1174
|
ORDER BY from_id, to_id
|
|
1003
|
-
`,void 0,e=>
|
|
1175
|
+
`,void 0,e=>ws(e))}async listBySerial(e){return await this.#e.queryAll(`
|
|
1004
1176
|
SELECT
|
|
1005
1177
|
reading_edges.from_id AS from_id,
|
|
1006
1178
|
reading_edges.to_id AS to_id,
|
|
@@ -1013,12 +1185,12 @@ ${Ou}
|
|
|
1013
1185
|
ON to_chunks.id = reading_edges.to_id
|
|
1014
1186
|
WHERE from_chunks.serial_id = ? AND to_chunks.serial_id = ?
|
|
1015
1187
|
ORDER BY reading_edges.from_id, reading_edges.to_id
|
|
1016
|
-
`,[e,e],
|
|
1188
|
+
`,[e,e],n=>ws(n))}async listIncoming(e){return await this.#t("to_id",e)}async listOutgoing(e){return await this.#t("from_id",e)}async#t(e,n){return await this.#e.queryAll(`
|
|
1017
1189
|
SELECT from_id, to_id, strength, weight
|
|
1018
1190
|
FROM reading_edges
|
|
1019
1191
|
WHERE ${e} = ?
|
|
1020
1192
|
ORDER BY from_id, to_id
|
|
1021
|
-
`,[
|
|
1193
|
+
`,[n],r=>ws(r))}}});function pO(t){let e=ye(t,"topology_parameter_hash"),n=ye(t,"knowledge_graph_parameter_hash");return{documentOrder:g(t,"document_order"),id:g(t,"id"),knowledgeGraphReady:g(t,"knowledge_graph_ready")!==0,...n===void 0?{}:{knowledgeGraphParameterHash:n},revision:g(t,"revision"),topologyReady:g(t,"topology_ready")!==0,...e===void 0?{}:{topologyParameterHash:e}}}var yr,ib=c(()=>{"use strict";k();ln();yr=class{#e;constructor(e){this.#e=e}async create(){return await this.#e.transaction(async()=>{await this.#e.run(`
|
|
1022
1194
|
INSERT INTO serials DEFAULT VALUES
|
|
1023
1195
|
`);let e=await this.#e.getLastInsertRowId();return await this.#e.run(`
|
|
1024
1196
|
INSERT INTO serial_states (
|
|
@@ -1033,7 +1205,7 @@ ${Ou}
|
|
|
1033
1205
|
serial_id, revision, topology_ready, knowledge_graph_ready
|
|
1034
1206
|
)
|
|
1035
1207
|
VALUES (?, ?, ?, ?)
|
|
1036
|
-
`,[e,0,0,0])})}catch(
|
|
1208
|
+
`,[e,0,0,0])})}catch(n){throw By(n)?new Error(`Serial ${e} already exists`):n}}async ensure(e){await this.#e.transaction(async()=>{await this.#e.run(`
|
|
1037
1209
|
INSERT OR IGNORE INTO serials (id)
|
|
1038
1210
|
VALUES (?)
|
|
1039
1211
|
`,[e]),await this.#e.run(`
|
|
@@ -1054,16 +1226,16 @@ ${Ou}
|
|
|
1054
1226
|
LEFT JOIN serial_states
|
|
1055
1227
|
ON serial_states.serial_id = serials.id
|
|
1056
1228
|
WHERE serials.id = ?
|
|
1057
|
-
`,[e],
|
|
1229
|
+
`,[e],pO)}async getRevision(e){return await this.#e.queryOne(`
|
|
1058
1230
|
SELECT COALESCE(revision, 0) AS revision
|
|
1059
1231
|
FROM serial_states
|
|
1060
1232
|
WHERE serial_id = ?
|
|
1061
|
-
`,[e],
|
|
1233
|
+
`,[e],n=>g(n,"revision"))??0}async getRevisions(e){let n=[...new Set(e)].sort(Gy);if(n.length===0)return new Map;let r=await this.#e.queryAll(`
|
|
1062
1234
|
SELECT serial_id, COALESCE(revision, 0) AS revision
|
|
1063
1235
|
FROM serial_states
|
|
1064
|
-
WHERE serial_id IN (${
|
|
1236
|
+
WHERE serial_id IN (${n.map(()=>"?").join(", ")})
|
|
1065
1237
|
ORDER BY serial_id
|
|
1066
|
-
`,
|
|
1238
|
+
`,n,i=>[g(i,"serial_id"),g(i,"revision")]);return new Map(r)}async bumpRevision(e){await this.ensure(e),await this.#e.transaction(async()=>{await this.#e.run(`
|
|
1067
1239
|
UPDATE serial_states
|
|
1068
1240
|
SET revision = revision + 1
|
|
1069
1241
|
WHERE serial_id = ?
|
|
@@ -1075,28 +1247,28 @@ ${Ou}
|
|
|
1075
1247
|
SELECT value
|
|
1076
1248
|
FROM archive_revisions
|
|
1077
1249
|
WHERE key = 'chapters'
|
|
1078
|
-
`,void 0,e=>
|
|
1250
|
+
`,void 0,e=>g(e,"value"))??0}async getMaxId(){return await this.#e.queryOne(`
|
|
1079
1251
|
SELECT COALESCE(MAX(id), 0) AS max_id
|
|
1080
1252
|
FROM serials
|
|
1081
|
-
`,void 0,
|
|
1253
|
+
`,void 0,n=>g(n,"max_id"))??0}async setTopologyReady(e,n=!0,r){if(await this.ensure(e),n){await this.#e.run(`
|
|
1082
1254
|
UPDATE serial_states
|
|
1083
1255
|
SET
|
|
1084
1256
|
topology_ready = ?,
|
|
1085
1257
|
topology_parameter_hash = COALESCE(?, topology_parameter_hash)
|
|
1086
1258
|
WHERE serial_id = ?
|
|
1087
|
-
`,[1,
|
|
1259
|
+
`,[1,r??null,e]);return}await this.#e.run(`
|
|
1088
1260
|
UPDATE serial_states
|
|
1089
1261
|
SET
|
|
1090
1262
|
topology_ready = ?,
|
|
1091
1263
|
topology_parameter_hash = NULL
|
|
1092
1264
|
WHERE serial_id = ?
|
|
1093
|
-
`,[0,e])}async setKnowledgeGraphReady(e,
|
|
1265
|
+
`,[0,e])}async setKnowledgeGraphReady(e,n=!0,r){if(await this.ensure(e),n){await this.#e.run(`
|
|
1094
1266
|
UPDATE serial_states
|
|
1095
1267
|
SET
|
|
1096
1268
|
knowledge_graph_ready = ?,
|
|
1097
1269
|
knowledge_graph_parameter_hash = COALESCE(?, knowledge_graph_parameter_hash)
|
|
1098
1270
|
WHERE serial_id = ?
|
|
1099
|
-
`,[1,
|
|
1271
|
+
`,[1,r??null,e]);return}await this.#e.run(`
|
|
1100
1272
|
UPDATE serial_states
|
|
1101
1273
|
SET
|
|
1102
1274
|
knowledge_graph_ready = ?,
|
|
@@ -1106,23 +1278,23 @@ ${Ou}
|
|
|
1106
1278
|
SELECT id
|
|
1107
1279
|
FROM serials
|
|
1108
1280
|
ORDER BY id
|
|
1109
|
-
`,void 0,e=>
|
|
1281
|
+
`,void 0,e=>g(e,"id"))}async listDocumentOrders(){let e=await this.#e.queryAll(`
|
|
1110
1282
|
SELECT id, document_order
|
|
1111
1283
|
FROM serials
|
|
1112
1284
|
ORDER BY id
|
|
1113
|
-
`,void 0,
|
|
1285
|
+
`,void 0,n=>[g(n,"id"),g(n,"document_order")]);return new Map(e)}async setDocumentOrders(e){await this.#e.transaction(async()=>{for(let n of e)await this.#e.run(`
|
|
1114
1286
|
INSERT OR IGNORE INTO serials (id)
|
|
1115
1287
|
VALUES (?)
|
|
1116
|
-
`,[
|
|
1288
|
+
`,[n.serialId]),await this.#e.run(`
|
|
1117
1289
|
INSERT OR IGNORE INTO serial_states (
|
|
1118
1290
|
serial_id, revision, topology_ready, knowledge_graph_ready
|
|
1119
1291
|
)
|
|
1120
1292
|
VALUES (?, ?, ?, ?)
|
|
1121
|
-
`,[
|
|
1293
|
+
`,[n.serialId,0,0,0]),await this.#e.run(`
|
|
1122
1294
|
UPDATE serials
|
|
1123
1295
|
SET document_order = ?
|
|
1124
1296
|
WHERE id = ?
|
|
1125
|
-
`,[
|
|
1297
|
+
`,[n.documentOrder,n.serialId])})}}});var br,ab=c(()=>{"use strict";k();br=class{#e;constructor(e){this.#e=e}async save(e){await this.#e.run(`
|
|
1126
1298
|
INSERT OR REPLACE INTO snake_chunks (snake_id, chunk_id, position)
|
|
1127
1299
|
VALUES (?, ?, ?)
|
|
1128
1300
|
`,[e.snakeId,e.chunkId,e.position])}async listChunkIds(e){return await this.#e.queryAll(`
|
|
@@ -1130,12 +1302,12 @@ ${Ou}
|
|
|
1130
1302
|
FROM snake_chunks
|
|
1131
1303
|
WHERE snake_id = ?
|
|
1132
1304
|
ORDER BY position
|
|
1133
|
-
`,[e],
|
|
1305
|
+
`,[e],n=>g(n,"chunk_id"))}async listBySnake(e){return await this.#e.queryAll(`
|
|
1134
1306
|
SELECT snake_id, chunk_id, position
|
|
1135
1307
|
FROM snake_chunks
|
|
1136
1308
|
WHERE snake_id = ?
|
|
1137
1309
|
ORDER BY position
|
|
1138
|
-
`,[e],
|
|
1310
|
+
`,[e],n=>({chunkId:g(n,"chunk_id"),position:g(n,"position"),snakeId:g(n,"snake_id")}))}}});var Sr,ob=c(()=>{"use strict";k();Sr=class{#e;constructor(e){this.#e=e}async create(e){return await this.#e.run(`
|
|
1139
1311
|
INSERT INTO snakes (
|
|
1140
1312
|
serial_id,
|
|
1141
1313
|
group_id,
|
|
@@ -1160,12 +1332,12 @@ ${Ou}
|
|
|
1160
1332
|
weight
|
|
1161
1333
|
FROM snakes
|
|
1162
1334
|
WHERE id = ?
|
|
1163
|
-
`,[e],
|
|
1335
|
+
`,[e],n=>({serialId:g(n,"serial_id"),firstLabel:w(n,"first_label"),groupId:g(n,"group_id"),id:g(n,"id"),lastLabel:w(n,"last_label"),localSnakeId:g(n,"local_snake_id"),size:g(n,"size"),wordsCount:g(n,"wordsCount"),weight:g(n,"weight")}))}async listIdsByGroup(e,n){return await this.#e.queryAll(`
|
|
1164
1336
|
SELECT id
|
|
1165
1337
|
FROM snakes
|
|
1166
1338
|
WHERE serial_id = ? AND group_id = ?
|
|
1167
1339
|
ORDER BY id
|
|
1168
|
-
`,[e,
|
|
1340
|
+
`,[e,n],r=>g(r,"id"))}async listBySerial(e){return await this.#e.queryAll(`
|
|
1169
1341
|
SELECT
|
|
1170
1342
|
id,
|
|
1171
1343
|
serial_id,
|
|
@@ -1179,16 +1351,16 @@ ${Ou}
|
|
|
1179
1351
|
FROM snakes
|
|
1180
1352
|
WHERE serial_id = ?
|
|
1181
1353
|
ORDER BY group_id, id
|
|
1182
|
-
`,[e],
|
|
1354
|
+
`,[e],n=>({serialId:g(n,"serial_id"),firstLabel:w(n,"first_label"),groupId:g(n,"group_id"),id:g(n,"id"),lastLabel:w(n,"last_label"),localSnakeId:g(n,"local_snake_id"),size:g(n,"size"),wordsCount:g(n,"wordsCount"),weight:g(n,"weight")}))}}});var wr,sb=c(()=>{"use strict";k();wr=class{#e;constructor(e){this.#e=e}async save(e){await this.#e.run(`
|
|
1183
1355
|
INSERT OR REPLACE INTO snake_edges (from_snake_id, to_snake_id, weight)
|
|
1184
1356
|
VALUES (?, ?, ?)
|
|
1185
|
-
`,[e.fromSnakeId,e.toSnakeId,e.weight])}async listIncoming(e){return await this.#t("to_snake_id",e)}async listOutgoing(e){return await this.#t("from_snake_id",e)}async listWithin(e){if(e.length===0)return[];let
|
|
1357
|
+
`,[e.fromSnakeId,e.toSnakeId,e.weight])}async listIncoming(e){return await this.#t("to_snake_id",e)}async listOutgoing(e){return await this.#t("from_snake_id",e)}async listWithin(e){if(e.length===0)return[];let n=e.map(()=>"?").join(", ");return await this.#e.queryAll(`
|
|
1186
1358
|
SELECT from_snake_id, to_snake_id, weight
|
|
1187
1359
|
FROM snake_edges
|
|
1188
|
-
WHERE from_snake_id IN (${
|
|
1189
|
-
AND to_snake_id IN (${
|
|
1360
|
+
WHERE from_snake_id IN (${n})
|
|
1361
|
+
AND to_snake_id IN (${n})
|
|
1190
1362
|
ORDER BY from_snake_id, to_snake_id
|
|
1191
|
-
`,[...e,...e],
|
|
1363
|
+
`,[...e,...e],r=>({fromSnakeId:g(r,"from_snake_id"),toSnakeId:g(r,"to_snake_id"),weight:g(r,"weight")}))}async listBySerial(e){return await this.#e.queryAll(`
|
|
1192
1364
|
SELECT
|
|
1193
1365
|
snake_edges.from_snake_id AS from_snake_id,
|
|
1194
1366
|
snake_edges.to_snake_id AS to_snake_id,
|
|
@@ -1200,22 +1372,42 @@ ${Ou}
|
|
|
1200
1372
|
ON to_snakes.id = snake_edges.to_snake_id
|
|
1201
1373
|
WHERE from_snakes.serial_id = ? AND to_snakes.serial_id = ?
|
|
1202
1374
|
ORDER BY snake_edges.from_snake_id, snake_edges.to_snake_id
|
|
1203
|
-
`,[e,e],
|
|
1375
|
+
`,[e,e],n=>({fromSnakeId:g(n,"from_snake_id"),toSnakeId:g(n,"to_snake_id"),weight:g(n,"weight")}))}async#t(e,n){return await this.#e.queryAll(`
|
|
1204
1376
|
SELECT from_snake_id, to_snake_id, weight
|
|
1205
1377
|
FROM snake_edges
|
|
1206
1378
|
WHERE ${e} = ?
|
|
1207
1379
|
ORDER BY from_snake_id, to_snake_id
|
|
1208
|
-
`,[
|
|
1209
|
-
`,fileStore:t.fileStore,options:t.options??{},path:t.path})}async function
|
|
1380
|
+
`,[n],r=>({fromSnakeId:g(r,"from_snake_id"),toSnakeId:g(r,"to_snake_id"),weight:g(r,"weight")}))}}});var db=c(()=>{"use strict";ab();ob();sb()});var pl=c(()=>{"use strict";zy();Qy();Xy();Zy();eb();tb();nb();rb();ib();db()});function cb(t,e){return e-t}var $i,hl=c(()=>{"use strict";$i=class{#e=[];#t;#n=new Set;#r=!1;#a=!1;constructor(e){this.#t=e}complete(){this.#i(),this.#r=!0}async dispose(){this.#a||(this.#a=!0,!this.#r&&await this.#t.rollbackContext(this))}ownSerial(e){this.#i(),this.#n.add(e)}async run(e){return this.#i(),await this.#t.runWithContext(this,e)}listCreatedFilePaths(){return[...this.#e]}listOwnedSerialIds(){return[...this.#n]}registerCreatedFile(e){this.#e.push(e)}#i(){if(this.#a)throw new Error("DocumentContext is already disposed")}}});import{mkdir as hO,readFile as fO,readdir as gO,rm as yO,unlink as bO,writeFile as ub}from"fs/promises";import{join as lb}from"path";var fl,gl=c(()=>{"use strict";T();fl={close:()=>Promise.resolve(),deleteFile:async t=>{await bO(t)},deleteTree:async t=>{await yO(t,{force:!0,recursive:!0})},ensureDirectory:async t=>{await hO(t,{recursive:!0})},initializeDatabaseSchema:()=>!0,markDatabaseDirty:()=>{},markSearchIndexDatabaseDirty:()=>{},openDatabaseReadonly:()=>!1,listFiles:async t=>(await gO(t,{withFileTypes:!0})).filter(e=>e.isFile()).map(e=>e.name),readFile:async t=>{try{return await fO(t)}catch(e){if(E(e)&&e.code==="ENOENT")return;throw e}},resolveDatabasePath:t=>Promise.resolve(lb(t,"database.db")),resolveSearchIndexDatabasePath:t=>Promise.resolve(lb(t,"index.db")),writeFile:async(t,e,n)=>{typeof e=="string"?await ub(t,e,{encoding:"utf8",flag:n.overwrite===!0?"w":"wx"}):await ub(t,e,{flag:n.overwrite===!0?"w":"wx"})}}});import{join as Es}from"path";async function yl(t){let e=await SO(t.fileStore,t.path);if(e!==void 0)return t.parse(JSON.parse(e))}async function mb(t,e){return await t.readFile(e)}async function Is(t){await hb({context:t.context,content:`${JSON.stringify(t.value,null,2)}
|
|
1381
|
+
`,fileStore:t.fileStore,options:t.options??{},path:t.path})}async function pb(t){await hb({context:t.context,content:t.content,fileStore:t.fileStore,options:{overwrite:!1},path:t.path})}function bl(t){return Es(xs(t),"data.bin")}function xs(t){return Es(t,"cover")}function Sl(t){return Es(xs(t),"info.json")}function ks(t){return Es(t,"toc.json")}async function SO(t,e){let n=await t.readFile(e);return n===void 0?void 0:Buffer.from(n).toString("utf8")}async function hb(t){try{await t.fileStore.writeFile(t.path,t.content,t.options)}catch(e){throw E(e)&&e.code==="EEXIST"?new Error(`File already exists: ${t.path}`):e}t.options.overwrite!==!0&&t.context?.registerCreatedFile(t.path)}var fb=c(()=>{"use strict";T()});var wl,Er,Ki,Ji,mn,pn=c(()=>{"use strict";wl="6",Er=300,Ki=420,Ji=80,mn=80});import{stat as wO}from"fs/promises";import{join as El,resolve as EO}from"path";async function Il(t){return await xO(EO(t.documentPath),()=>IO(t))}async function IO(t){let e=t.fileStore.resolveSearchIndexDatabasePath===void 0?El(t.documentPath,"index.db"):await t.fileStore.resolveSearchIndexDatabasePath(t.documentPath),n=!t.readonly&&await kO(e);if(!n&&!await RO(e)){if(await vO(t.fileStore,t.documentPath),t.readonly)throw new Error("Search index cache is missing: index.db");e=t.fileStore.resolveSearchIndexDatabasePath===void 0?El(t.documentPath,"index.db"):await t.fileStore.resolveSearchIndexDatabasePath(t.documentPath),n=!0}let r=await O.open(e,n?ul:"",{onWrite:()=>{t.fileStore.markSearchIndexDatabaseDirty?.()},readonly:t.readonly});t.readonly||await TO(r);try{return await t.operation(r)}finally{await r.close()}}async function xO(t,e){let n=Rs.get(t)??Promise.resolve(),r,i=new Promise(o=>{r=o}),a=n.then(()=>i);Rs.set(t,a),await n;try{return await e()}finally{r(),Rs.get(t)===a&&Rs.delete(t)}}async function kO(t){let e=await wO(t).catch(n=>{if(!(E(n)&&n.code==="ENOENT"))throw n});return e===void 0||e.size===0}async function RO(t){let e=await O.open(t,"",{readonly:!0}).catch(()=>{});if(e===void 0)return!1;try{return await e.queryOne(`
|
|
1382
|
+
SELECT value
|
|
1383
|
+
FROM search_index_state
|
|
1384
|
+
WHERE key = 'version'
|
|
1385
|
+
`,void 0,r=>String(r.value)).catch(()=>{})===wl}finally{await e.close()}}async function vO(t,e){try{await t.deleteFile(El(e,"index.db"))}catch(n){if(E(n)&&n.code==="ENOENT")return;throw n}}async function TO(t){await gb(t,"text_sentence_records","archive_id","INTEGER NOT NULL DEFAULT 0"),await CO(t,"text_sentence_records",["archive_id","kind","chapter_id","sentence_index"])||await _O(t),await gb(t,"search_object_properties_records","archive_id","INTEGER NOT NULL DEFAULT 0"),await t.run(`
|
|
1210
1386
|
CREATE TABLE IF NOT EXISTS index_dirty_chapters (
|
|
1211
1387
|
archive_id INTEGER NOT NULL,
|
|
1212
1388
|
chapter_id INTEGER NOT NULL,
|
|
1213
1389
|
updated_at INTEGER NOT NULL,
|
|
1214
1390
|
PRIMARY KEY (archive_id, chapter_id)
|
|
1215
1391
|
)
|
|
1216
|
-
`)
|
|
1392
|
+
`),await t.run(`
|
|
1393
|
+
CREATE TABLE IF NOT EXISTS text_embedding_segments (
|
|
1394
|
+
id INTEGER PRIMARY KEY,
|
|
1395
|
+
archive_id INTEGER NOT NULL,
|
|
1396
|
+
kind INTEGER NOT NULL,
|
|
1397
|
+
chapter_id INTEGER NOT NULL,
|
|
1398
|
+
start_sentence_index INTEGER NOT NULL,
|
|
1399
|
+
end_sentence_index INTEGER NOT NULL,
|
|
1400
|
+
words_count INTEGER NOT NULL,
|
|
1401
|
+
model TEXT NOT NULL,
|
|
1402
|
+
dimensions INTEGER NOT NULL,
|
|
1403
|
+
vector BLOB NOT NULL
|
|
1404
|
+
)
|
|
1405
|
+
`),await t.run(`
|
|
1406
|
+
CREATE INDEX IF NOT EXISTS idx_text_embedding_segments_scope
|
|
1407
|
+
ON text_embedding_segments(archive_id, kind, chapter_id, start_sentence_index, end_sentence_index)
|
|
1408
|
+
`)}async function _O(t){await t.transaction(async()=>{await t.run(`
|
|
1217
1409
|
CREATE TABLE text_sentence_records_next (
|
|
1218
|
-
${
|
|
1410
|
+
${cl}
|
|
1219
1411
|
)
|
|
1220
1412
|
`),await t.run(`
|
|
1221
1413
|
INSERT INTO text_sentence_records_next (
|
|
@@ -1244,13 +1436,13 @@ ${Ou}
|
|
|
1244
1436
|
`),await t.run(`
|
|
1245
1437
|
CREATE INDEX IF NOT EXISTS idx_text_sentence_records_chapter
|
|
1246
1438
|
ON text_sentence_records(archive_id, kind, chapter_id, sentence_index)
|
|
1247
|
-
`)})}async function
|
|
1439
|
+
`)})}async function CO(t,e,n){let r=await t.queryAll(`PRAGMA index_list(${e})`,void 0,i=>({name:String(i.name),unique:Number(i.unique)===1}));for(let i of r){if(!i.unique)continue;let o=(await t.queryAll(`PRAGMA index_info(${i.name})`,void 0,s=>({name:String(s.name),seqno:Number(s.seqno)}))).sort((s,d)=>s.seqno-d.seqno).map(s=>s.name);if(o.length===n.length&&o.every((s,d)=>s===n[d]))return!0}return!1}async function gb(t,e,n,r){(await t.queryAll(`PRAGMA table_info(${e})`,void 0,a=>String(a.name))).includes(n)||await t.run(`ALTER TABLE ${e} ADD COLUMN ${n} ${r}`)}var Rs,yb=c(()=>{"use strict";T();k();Hi();pn();Rs=new Map});async function bb(t){await AO(t.context.listOwnedSerialIds(),t.deleteSerialResources),await PO(t.fileStore,t.context.listCreatedFilePaths())}async function Sb(t){await t.metadata.deleteChapterSubtree(t.serialId),await xl({database:t.database,metadata:t.metadata,serialId:t.serialId}),await t.database.transaction(async()=>{await t.database.run(`
|
|
1248
1440
|
DELETE FROM serial_states
|
|
1249
1441
|
WHERE serial_id = ?
|
|
1250
1442
|
`,[t.serialId]),await t.database.run(`
|
|
1251
1443
|
DELETE FROM serials
|
|
1252
1444
|
WHERE id = ?
|
|
1253
|
-
`,[t.serialId])}),await t.textStreams.getSerial(t.serialId).delete(),await t.deleteSummary(t.serialId),await t.serials.bumpChaptersRevision(),await t.graphBuildParameters.deleteUnreferenced()}async function
|
|
1445
|
+
`,[t.serialId])}),await t.textStreams.getSerial(t.serialId).delete(),await t.deleteSummary(t.serialId),await t.serials.bumpChaptersRevision(),await t.graphBuildParameters.deleteUnreferenced()}async function xl(t){await t.database.transaction(async()=>{await kl(t),await Rl(t)})}async function kl(t){await t.database.transaction(async()=>{await t.database.run(`
|
|
1254
1446
|
DELETE FROM mention_link_evidence_sentences
|
|
1255
1447
|
WHERE link_id IN (
|
|
1256
1448
|
SELECT mention_links.id
|
|
@@ -1276,7 +1468,7 @@ ${Ou}
|
|
|
1276
1468
|
`,[t.serialId,t.serialId]),await t.database.run(`
|
|
1277
1469
|
DELETE FROM mentions
|
|
1278
1470
|
WHERE chapter_id = ?
|
|
1279
|
-
`,[t.serialId]),await t.database.run(`
|
|
1471
|
+
`,[t.serialId]),await t.metadata.deleteDeletedEntitiesAndTriples()})}async function Rl(t){await t.database.transaction(async()=>{await t.database.run(`
|
|
1280
1472
|
DELETE FROM snake_edges
|
|
1281
1473
|
WHERE from_snake_id IN (
|
|
1282
1474
|
SELECT id
|
|
@@ -1317,23 +1509,23 @@ ${Ou}
|
|
|
1317
1509
|
`,[t.serialId]),await t.database.run(`
|
|
1318
1510
|
DELETE FROM chunks
|
|
1319
1511
|
WHERE serial_id = ?
|
|
1320
|
-
`,[t.serialId]),await t.metadata.deleteDeletedChunks()
|
|
1321
|
-
`)}function
|
|
1512
|
+
`,[t.serialId]),await t.metadata.deleteDeletedChunks()})}async function PO(t,e){for(let n of[...e].reverse())try{await t.deleteFile(n)}catch(r){if(E(r)&&r.code==="ENOENT")continue;throw r}}async function AO(t,e){for(let n of[...t].sort(cb))await e(n)}var wb=c(()=>{"use strict";T();hl()});function vl(t){let e=[];for(let n of t)n.serialId!==void 0&&e.push(n.serialId),e.push(...vl(n.children));return e}var Eb=c(()=>{"use strict"});import{AsyncLocalStorage as LO}from"async_hooks";import{join as Ib,resolve as OO}from"path";import{z as Tl}from"zod";function xb(t){let e={items:t.items.map(Rb),version:t.version};return Ss(e.items),e}function Rb(t){return{children:t.children.map(Rb),...t.key===void 0?{}:{key:t.key},...t.serialId===void 0?{}:{serialId:t.serialId},...t.title===void 0?{}:{title:t.title}}}var NO,he,vb=c(()=>{"use strict";Ai();fs();Ui();k();Wu();Hi();pl();Ke();hl();gl();fb();yb();wb();Eb();NO=Tl.object({mediaType:Tl.string().min(1),path:Tl.string()}),he=class t{chunks;fragmentGroups;graphBuildParameters;indexArtifacts;readingEdges;mentionLinks;mentions;metadata;path;serials;snakeChunks;snakeEdges;snakes;#e;#t;#n;#r=new LO;constructor(e,n,r,i=fl){this.#e=e,this.#t=i,this.#n=n,this.chunks=new cr(e),this.fragmentGroups=new ur(e),this.graphBuildParameters=new lr(e),this.indexArtifacts=new mr(e),this.readingEdges=new gr(e),this.mentionLinks=new pr(e),this.mentions=new hr(e),this.metadata=new fr(e),this.path=r,this.serials=new yr(e),this.snakeChunks=new br(e),this.snakeEdges=new wr(e),this.snakes=new Sr(e)}static async open(e,n={}){let r=OO(e),i=n.fileStore??fl;try{let a=await i.resolveDatabasePath(r);await i.ensureDirectory(r);let o=i.initializeDatabaseSchema(),s=await O.open(a,o?dl:"",{onWrite:()=>{i.markDatabaseDirty?.()},readonly:i.openDatabaseReadonly()});o&&await Fy(s);let d=new Ci(r,s,{deleteTree:async l=>{await i.deleteTree(l)},ensureDirectory:async l=>{await i.ensureDirectory(l)},listFiles:async l=>await i.listFiles(l),readFile:async l=>await i.readFile(l),writeFile:async(l,m,p)=>{await i.writeFile(l,m,p)}});return await d.ensureCreated(),new t(s,d,r,i)}catch(a){throw await i.close(),a}}static async openSession(e,n){let r=await t.open(e);try{return await r.openSession(async()=>await n(r))}finally{await r.release()}}getSerialFragments(e){return this.#n.getSerial(e)}getSummaryFragments(e){return this.#n.getSummarySerial(e)}createContext(){return new $i(this)}async createSerial(){let e=await this.serials.create();return this.#r.getStore()?.ownSerial(e),e}async clearSerialGraph(e){await this.clearSerialDerivedArtifacts(e)}async clearSerialDerivedArtifacts(e){await this.indexArtifacts.deleteBySerial(e),await this.deleteSummary(e),await xl({database:this.#e,metadata:this.metadata,serialId:e}),await this.serials.setTopologyReady(e,!1),await this.serials.setKnowledgeGraphReady(e,!1),await this.graphBuildParameters.deleteUnreferenced()}async clearSerialKnowledgeGraph(e){await this.indexArtifacts.delete(e,"fts"),await kl({database:this.#e,metadata:this.metadata,serialId:e}),await this.serials.setKnowledgeGraphReady(e,!1),await this.graphBuildParameters.deleteUnreferenced()}async clearSerialReadingGraph(e){await this.deleteSummary(e),await Rl({database:this.#e,metadata:this.metadata,serialId:e}),await this.serials.setTopologyReady(e,!1),await this.graphBuildParameters.deleteUnreferenced()}async clearSerialSource(e){await this.clearSerialDerivedArtifacts(e),await this.#n.getSerial(e).delete(),await this.serials.bumpRevision(e)}async deleteSerial(e){await this.#i(e)}async deleteSummary(e){await this.indexArtifacts.delete(e,"fts"),await this.indexArtifacts.delete(e,"embedding-summary"),await this.#n.getSummarySerial(e).delete()}async getSentence(e){return await this.#n.getSentence(e)}async openSession(e){if(this.#r.getStore()!==void 0)return await e(this);let r=new $i(this);try{let i=await this.#e.transaction(async()=>await r.run(async()=>await e(this)));return r.complete(),i}finally{await r.dispose()}}async readDatabase(e){return await e(this.#e)}async readSearchIndexDatabase(e){return await Il({documentPath:this.path,fileStore:this.#t,operation:e,readonly:!0})}async writeSearchIndexDatabase(e){return await Il({documentPath:this.path,fileStore:this.#t,operation:e,readonly:!1})}async deleteSearchIndexDatabase(){await this.#t.deleteFile(Ib(this.path,"index.db")),await this.#t.deleteFile(Ib(this.path,"fts.db")).catch(()=>{})}async peekNextSerialId(){return await this.serials.getMaxId()+1}async readBookMeta(){let e=await this.metadata.getMap("");return Object.keys(e).length===0?void 0:Hu.parse(e)}async readCover(){let e=await yl({fileStore:this.#t,parse:r=>NO.parse(r),path:Sl(this.path)});if(e===void 0)return;let n=await mb(this.#t,bl(this.path));if(n===void 0)throw new Error("Cover data is missing");return{data:n,mediaType:e.mediaType,path:e.path}}async readSummary(e){return await this.#n.getSummarySerial(e).readText()}async readToc(){return await yl({fileStore:this.#t,parse:e=>hs.parse(e),path:ks(this.path)})}async writeBookMeta(e){if(Object.keys(await this.metadata.getMap("")).length>0)throw new Error("Archive metadata already exists.");await this.replaceBookMeta(e)}async replaceBookMeta(e){await this.metadata.replaceMap({kind:1,objectPath:""},{...e})}async writeCover(e){await this.#t.ensureDirectory(xs(this.path)),await Is({context:this.#r.getStore(),fileStore:this.#t,path:Sl(this.path),value:{mediaType:e.mediaType,path:e.path}}),await pb({content:e.data,context:this.#r.getStore(),fileStore:this.#t,path:bl(this.path)})}async writeSummary(e,n){await this.deleteSummary(e),await this.#n.getSummarySerial(e).writeTextStream(n)}async writeToc(e){let n=xb(e);await Is({context:this.#r.getStore(),fileStore:this.#t,path:ks(this.path),value:n}),await this.#a(n),await this.serials.bumpChaptersRevision()}async replaceToc(e){let n=xb(e);await Is({context:this.#r.getStore(),fileStore:this.#t,options:{overwrite:!0},path:ks(this.path),value:n}),await this.#a(n),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(e,n){return await this.#r.run(e,n)}async rollbackContext(e){await bb({context:e,deleteSerialResources:async n=>{await this.#i(n)},fileStore:this.#t})}async#a(e){await this.serials.setDocumentOrders(vl(e.items).map((n,r)=>({documentOrder:r,serialId:n})))}async#i(e){await this.indexArtifacts.deleteBySerial(e),await Sb({database:this.#e,deleteSummary:async n=>{await this.deleteSummary(n)},graphBuildParameters:this.graphBuildParameters,metadata:this.metadata,serialId:e,serials:this.serials,textStreams:this.#n})}}});var Tb=c(()=>{"use strict"});var _l=c(()=>{"use strict";vb();gl();Tb()});function zi(t,e){let n=new sn(e);return new Proxy(t,{get(r,i,a){if(i==="getSerialFragments")return s=>n.getSerial(s);if(i==="getSummaryFragments")return s=>n.getSummarySerial(s);if(i==="getSentence")return async s=>await n.getSentence(s);let o=Reflect.get(r,i,a);return typeof o!="function"?o:o.bind(r)}})}var _b=c(()=>{"use strict";Uu()});var N=c(()=>{"use strict";k();Bg();Wu();Uu();_l();_b();Hi();pl();Ke()});var Cl,Pl=c(()=>{"use strict";Cl=new Set(["registry","arc","path","tree","meta","index","chapter","chunk","entity","triple"])});import{isAbsolute as rY,relative as iY,resolve as Cb}from"path";import{homedir as DO}from"os";function Qi(t){let e=fn(t);if(e.protocol!=="wikg")throw new Error(vs(t));let n=FO(e.path,e.fragment);if(n!==void 0)return n;let r=e.path,i=r.findIndex(s=>s.endsWith(hn));if(i<0)return{objectUri:Ir(r.join("/"),Al(e.fragment))};let a=jO(r.slice(0,i+1)),o=r.slice(i+1).join("/");if(a==="")throw new Error(`Invalid Wiki Graph archive URI: ${t}`);return{archivePath:BO(a),...o===""?{}:{objectUri:Ir(o,Al(e.fragment))}}}function fn(t){let e=t.indexOf("://");if(e<=0)throw new Error(vs(t));let n=t.slice(0,e),r=t.slice(e+3),i=r.indexOf("#"),a=i<0?r:r.slice(0,i),o=i<0?void 0:r.slice(i+1);if(o?.includes("#")===!0)throw new Error(`Invalid Wiki Graph URI fragment: ${t}`);return{protocol:n,path:MO(a,t),...WO(o,t)}}function MO(t,e){let n=t.replace(/\/+$/u,"");if(n==="")return[];if(n.startsWith("/")){let i=n.slice(1);if(i==="")return["/"];let a=i.split("/");return Pb(a,e),["/",...a]}let r=n.split("/");return Pb(r,e),r}function Pb(t,e){if(t.includes(""))throw new Error(`Invalid Wiki Graph URI path: empty path segment in ${e}`)}function WO(t,e){if(t===void 0)return{};if(t==="")throw new Error(`Invalid Wiki Graph URI fragment: ${e}`);let n=/^(?<begin>[0-9]+)\.\.(?<end>[0-9]+)$/u.exec(t);if(n?.groups!==void 0){let r=Number(n.groups.begin),i=Number(n.groups.end);return{fragment:{begin:r,end:i}}}if(/^[0-9]+$/u.test(t))return{fragment:Number(t)};throw new Error(`Invalid Wiki Graph URI fragment: ${e}`)}function FO(t,e){if(t[0]!=="lib")return;let n=t[1]==="arc"?1:t[2]==="arc"?2:-1;if(n<0)return;let r=n===2?t[1]:void 0,i=t[n+1];if(i===void 0||i.endsWith(hn)||GO(i))return;let a=`${be}lib/${r===void 0?"":`${r}/`}arc/${i}`,o=t.slice(n+2).join("/");return{archivePath:a,...o===""?{}:{objectUri:Ir(o,Al(e))}}}function jO(t){return t[0]==="/"?`/${t.slice(1).join("/")}`:t.join("/")}function Al(t){if(t!==void 0)return typeof t=="number"?String(t):`${t.begin}..${t.end}`}function GO(t){return Cl.has(t)}function BO(t){return t.startsWith("~/")?Cb(DO(),t.slice(2)):Cb(t)}function xr(t,e){let n=t.replace(/\\/gu,"/");return e===void 0||e===be?`${be}${n}`:`${be}${n}/${UO(e)}`}function Xi(t,e){return xr(t,Ir(`chapter/${e}`))}function Ir(t,e){let n=t.replace(/^\/+/u,"").replace(/\/+$/u,"");return`${be}${n}${e===void 0?"":`#${e}`}`}function vs(t){let e=t.endsWith(hn)&&t.startsWith("/")?`${be}${t}`:"wikg:///absolute/path/book.wikg";return[`Expected a Wiki Graph URI with a .wikg archive locator: ${t}`,`Example: ${e}`,"See: wg help uri"].join(`
|
|
1513
|
+
`)}function UO(t){let e=HO(t);if(e===void 0)throw new Error(`Expected a Wiki Graph object URI: ${t}`);return t.slice(e.length).replace(/^\/+/u,"")}function HO(t){if(t.startsWith(be))return be}var be,hn,Lt=c(()=>{"use strict";Pl();be="wikg://",hn=".wikg"});import{randomUUID as cY}from"crypto";async function Ab(t){let e=await JO(t.databasePath);try{return await KO(e,t),await e.queryOne(`
|
|
1322
1514
|
SELECT 1 AS active
|
|
1323
1515
|
FROM state_locks
|
|
1324
1516
|
WHERE scope = ? AND resource_key = ?
|
|
1325
1517
|
LIMIT 1
|
|
1326
|
-
`,[t.scope,t.resourceKey],()=>!0)===!0}finally{await e.close()}}async function
|
|
1518
|
+
`,[t.scope,t.resourceKey],()=>!0)===!0}finally{await e.close()}}async function KO(t,e){let n=[],r=[];e.scope!==void 0&&(n.push("scope = ?"),r.push(e.scope)),e.resourceKey!==void 0&&(n.push("resource_key = ?"),r.push(e.resourceKey));let i=await t.queryAll(`
|
|
1327
1519
|
SELECT scope, resource_key, mode, owner_id, owner_pid, heartbeat_at
|
|
1328
1520
|
FROM state_locks
|
|
1329
|
-
${
|
|
1330
|
-
`,
|
|
1521
|
+
${n.length===0?"":`WHERE ${n.join(" AND ")}`}
|
|
1522
|
+
`,r,a=>({lock:zO(a),resourceKey:w(a,"resource_key"),scope:w(a,"scope")}));for(let{lock:a,resourceKey:o,scope:s}of i)XO(a,e.staleMs)&&await t.run(`
|
|
1331
1523
|
DELETE FROM state_locks
|
|
1332
1524
|
WHERE scope = ?
|
|
1333
1525
|
AND resource_key = ?
|
|
1334
1526
|
AND owner_id = ?
|
|
1335
1527
|
AND heartbeat_at = ?
|
|
1336
|
-
`,[s,o,a.ownerId,a.heartbeatAt])}async function
|
|
1528
|
+
`,[s,o,a.ownerId,a.heartbeatAt])}async function JO(t){return await D(t,qO)}function zO(t){return{heartbeatAt:g(t,"heartbeat_at"),mode:QO(w(t,"mode")),ownerId:w(t,"owner_id"),ownerPid:g(t,"owner_pid")}}function QO(t){return t==="read"||t==="write"?t:"write"}function XO(t,e=$O){return Date.now()-t.heartbeatAt>e&&!VO(t.ownerPid)}function VO(t){try{return process.kill(t,0),!0}catch(e){return!(E(e)&&e.code==="ESRCH")}}var qO,$O,Lb=c(()=>{"use strict";k();N();T();qO=`
|
|
1337
1529
|
CREATE TABLE IF NOT EXISTS state_locks (
|
|
1338
1530
|
scope TEXT NOT NULL,
|
|
1339
1531
|
resource_key TEXT NOT NULL,
|
|
@@ -1347,7 +1539,7 @@ ${Ou}
|
|
|
1347
1539
|
|
|
1348
1540
|
CREATE INDEX IF NOT EXISTS idx_state_locks_resource
|
|
1349
1541
|
ON state_locks(scope, resource_key);
|
|
1350
|
-
|
|
1542
|
+
`,$O=300*1e3});async function Nb(t){return await Ab({databasePath:le(),resourceKey:eN(t),scope:ZO,staleMs:Ob})||await tN(t)}function eN(t){return String(t)}async function tN(t){let e=await nN();try{let n=await e.queryOne("SELECT owner_pid, heartbeat_at FROM library_locks WHERE library_id = ?",[t],r=>({heartbeatAt:g(r,"heartbeat_at"),ownerPid:g(r,"owner_pid")}));return n!==void 0&&iN(n)}finally{await e.close()}}async function nN(){let t=await D(le(),YO);return await rN(t),t}async function rN(t){new Set(await t.queryAll("PRAGMA table_info(library_locks)",void 0,n=>w(n,"name"))).has("heartbeat_at")||await t.run("ALTER TABLE library_locks ADD COLUMN heartbeat_at INTEGER NOT NULL DEFAULT 0")}function iN(t){return Date.now()-t.heartbeatAt<=Ob&&aN(t.ownerPid)}function aN(t){try{return process.kill(t,0),!0}catch(e){return!(E(e)&&e.code==="ESRCH")}}var YO,ZO,Ob,Vi=c(()=>{"use strict";k();N();me();Lb();T();YO=`
|
|
1351
1543
|
CREATE TABLE IF NOT EXISTS library_locks (
|
|
1352
1544
|
library_id INTEGER PRIMARY KEY,
|
|
1353
1545
|
mode TEXT NOT NULL,
|
|
@@ -1356,41 +1548,43 @@ ${Ou}
|
|
|
1356
1548
|
heartbeat_at INTEGER NOT NULL,
|
|
1357
1549
|
created_at INTEGER NOT NULL
|
|
1358
1550
|
);
|
|
1359
|
-
`,
|
|
1551
|
+
`,ZO="library",Ob=300*1e3});import{randomBytes as xY}from"crypto";import{constants as RY}from"fs";import{access as TY,mkdir as _Y,stat as CY}from"fs/promises";import{join as AY,resolve as LY}from"path";var kr=c(()=>{"use strict";k();N();me();Lt();T();Vi();Pl()});var ee,fe,Ol,Nl,Db,Ot=c(()=>{"use strict";ee=".wikg-mutation-token",fe="manifest.json",Ol="index.db",Nl="fts.db",Db=[/^\.wikg-mutation-token$/u,/^manifest\.json$/u,/^database\.db$/u,/^toc\.json$/u,/^cover\/(?:data\.bin|info\.json)$/u,/^texts\/(?:source|summary)\/\d+\.txt$/u]});import{posix as oN,resolve as sN,sep as Mb}from"path";function te(t){let e=t.replaceAll("\\","/").trim(),n=e.startsWith("/")?e.slice(1):e;return oN.normalize(n).replace(/^(\.\/)+/u,"").replace(/^\/+/u,"")}function lt(t){return Db.some(e=>e.test(t))}function Wb(t,e,n){let r=sN(t),i=r.endsWith(Mb)?r:`${r}${Mb}`;if(!(e===r||e.startsWith(i)))throw new Error(`Invalid archive entry path: ${n}`)}function Fb(t){return[...t].sort((e,n)=>Gb(e.archivePath,n.archivePath))}function jb(t){return[...t].sort(Gb)}function Gb(t,e){return t===ee?e===ee?0:-1:e===ee?1:t.localeCompare(e)}var gn=c(()=>{"use strict";Ot()});import{randomBytes as dN}from"crypto";import{z as Dl}from"zod";function Rr(t){let e;try{e=JSON.parse(t)}catch{throw new Error(`Invalid WIKG manifest: ${fe}.`)}let n=uN.safeParse(e);if(!n.success)throw new Error(`Unsupported WIKG format version in ${fe}.`);return{formatVersion:n.data.formatVersion,schemaVersion:n.data.schemaVersion??1}}function Fl(){let t=dN(32).toString("base64url");return Buffer.from(`${Bb}
|
|
1360
1552
|
${t}
|
|
1361
|
-
`,"utf8")}function
|
|
1362
|
-
`,
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1553
|
+
`,"utf8")}function _s(t){let e=t.split(/\r?\n/u),n=e[0],r=e[1];if(n!==Bb||r===void 0||!cN.test(r))throw new Error(`Invalid WIKG mutation token: ${ee}.`);return r}var Ts,Ml,Wl,Bb,cN,uN,vr=c(()=>{"use strict";Ot();Ts=1,Ml=3,Wl=`${JSON.stringify({formatVersion:Ts,schemaVersion:Ml})}
|
|
1554
|
+
`,Bb="wikg-mutation-token:v1",cN=/^[A-Za-z0-9_-]{43}$/u,uN=Dl.object({formatVersion:Dl.literal(Ts),schemaVersion:Dl.number().int().positive().optional()})});import{open as lN}from"fs/promises";import{inflateRaw as mN}from"zlib";import{open as pN}from"yauzl";async function Nt(t){let e=await gN(t);try{let n=await hN(e);return await yN(t,n),{entries:n,zipFile:e}}catch(n){throw e.close(),n}}async function hN(t){return await new Promise((e,n)=>{let r=[];t.on("entry",i=>{if(i.fileName.endsWith("/")){t.readEntry();return}r.push(i),t.readEntry()}),t.once("end",()=>{e(r)}),t.once("error",i=>{n(i)}),t.readEntry()})}async function Cs(t,e){return(await fN(t,e)).toString("utf8")}async function fN(t,e){let n=await lN(t,"r");try{return await Dt(n,e)}finally{await n.close()}}async function Dt(t,e){let n=await SN(t,e);if(e.compressionMethod===0)return n;if(e.compressionMethod===8)return await wN(n);throw new Error(`Unsupported ZIP compression method: ${e.fileName}`)}async function gN(t){return await new Promise((e,n)=>{pN(t,{autoClose:!1,lazyEntries:!0},(r,i)=>{if(r!==null||i===void 0){n(r??new Error(`Cannot open archive: ${t}`));return}e(i)})})}async function yN(t,e){await bN(t,e);let n=e.find(r=>te(r.fileName)===fe);if(n===void 0)throw new Error(`Missing WIKG manifest: ${fe}.`);Rr(await Cs(t,n))}async function bN(t,e){if(te(e[0]?.fileName??"")!==ee)throw new Error(`Missing WIKG mutation token: ${ee}.`);_s(await Cs(t,e[0]))}async function SN(t,e){let n=Buffer.alloc(30);if(await t.read(n,0,n.length,e.relativeOffsetOfLocalHeader),n.readUInt32LE(0)!==67324752)throw new Error(`Invalid ZIP local file header: ${e.fileName}`);let r=n.readUInt16LE(26),i=n.readUInt16LE(28),a=e.relativeOffsetOfLocalHeader+30+r+i,o=Buffer.alloc(e.compressedSize);return await t.read(o,0,o.length,a),o}async function wN(t){return await new Promise((e,n)=>{mN(t,(r,i)=>{if(r!==null){n(r);return}e(i)})})}var Yi=c(()=>{"use strict";Ot();vr();gn()});import{mkdir as EN,open as IN,writeFile as xN}from"fs/promises";import{dirname as kN,resolve as RN}from"path";async function Tr(t,e){let{entries:n,zipFile:r}=await Nt(t),i=await IN(t,"r");try{for(let a of n){let o=te(a.fileName);if(o==="")throw new Error(`Invalid archive entry path: ${a.fileName}`);if(!lt(o))continue;let s=RN(e,o);Wb(e,s,o),await EN(kN(s),{recursive:!0}),await xN(s,await Dt(i,a))}}finally{await i.close(),r.close()}}var jl=c(()=>{"use strict";gn();Yi()});import{createHash as vN}from"crypto";function As(t){return t.normalize("NFKC").replace(CN,"").replace(PN," ").replace(AN," ").trim()}function Mt(t){let e=As(t),n=[],r=[],i=[];for(let a of Ps(e,"phrase"))n.push(yn(a,"hp",1));for(let a of Ps(e,"bigram"))n.push(yn(a,"h2",1));for(let a of Ps(e,"trigram"))n.push(yn(a,"h3",1));for(let a of WN(e))n.push(yn(a,"hw",1));for(let a of MN(e)){n.push(yn(a,"le",1));let o=FN(a);o!==a&&!Ub.has(o)&&r.push(yn(o,"ls",2))}for(let a of Ps(e,"char"))i.push(yn(a,"hc",3));return{tier1:Gl(n),tier2:Gl(r),tier3:Gl(i)}}function Ps(t,e){let n=[];for(let r of t.matchAll(TN)){let i=[...r[0]];if(e==="phrase"){i.length>=2&&n.push(i.join(""));continue}if(e==="char"){n.push(...i.filter(o=>!NN.has(o)));continue}let a=e==="bigram"?2:3;if(!(i.length<a))for(let o=0;o<=i.length-a;o+=1)n.push(i.slice(o,o+a).join(""))}return n}function MN(t){return[...t.toLowerCase().matchAll(_N)].map(e=>e[0]).filter(e=>!Ub.has(e))}function WN(t){if(typeof Intl.Segmenter!="function")return[];let e=new Intl.Segmenter("zh-CN",{granularity:"word"}),n=[];for(let r of e.segment(t)){let i=As(r.segment).toLowerCase();r.isWordLike===!0&&[...i].length>=2&&LN.test(i)&&n.push(i)}return n}function yn(t,e,n){let r=As(t).toLowerCase(),i=vN("sha256").update(r).digest("hex").slice(0,20);return{encoded:`${e}${i}`,raw:r,tier:n}}function Gl(t){let e=new Set,n=[];for(let r of t)e.has(r.encoded)||(e.add(r.encoded),n.push(r));return n}function FN(t){let e=DN.get(t);return e!==void 0?e:!ON.test(t)||/\d/u.test(t)||t.length<4?t:jN(t)}function jN(t){let e=t;return e.endsWith("ies")&&e.length>4?e=`${e.slice(0,-3)}y`:e.endsWith("ing")&&e.length>5?e=e.slice(0,-3):e.endsWith("ed")&&e.length>4||e.endsWith("es")&&e.length>4?e=e.slice(0,-2):e.endsWith("s")&&e.length>4&&(e=e.slice(0,-1)),e.length>3&&e.at(-1)===e.at(-2)&&/[bcdfghjklmnpqrstvwxyz]/u.test(e.at(-1))&&(e=e.slice(0,-1)),e}var TN,_N,CN,PN,AN,LN,ON,NN,Ub,DN,Wt=c(()=>{"use strict";TN=new RegExp("\\p{Script=Han}+","gu"),_N=/[\p{Script=Latin}\p{Number}]+/gu,CN=/[\u200B-\u200D\u2060\uFEFF]/gu,PN=/[\p{Cc}\p{Cf}]/gu,AN=/\s+/gu,LN=new RegExp("^\\p{Script=Han}+$","u"),ON=/^[a-z0-9]+$/u,NN=new Set(["\u7684","\u5730","\u5F97","\u4E86","\u7740","\u8FC7","\u548C","\u4E0E","\u53CA","\u6216","\u800C","\u5728","\u662F","\u4E3A","\u4EE5","\u4E8E","\u5BF9","\u4E2D","\u4E0A","\u4E0B","\u7B49"]),Ub=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"]),DN=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 u2}from"crypto";async function Zi(t){return await t.queryOne(`
|
|
1555
|
+
SELECT value
|
|
1556
|
+
FROM search_index_state
|
|
1557
|
+
WHERE key = 'fingerprint'
|
|
1558
|
+
`,void 0,e=>String(e.value))}var Ls=c(()=>{"use strict"});var Bl=c(()=>{"use strict";pn()});var Ns=c(()=>{"use strict";k();Bl()});var Hb=c(()=>{"use strict";Wt();Ls();Ns();pn()});var $b=c(()=>{"use strict";pn()});var Kb=c(()=>{"use strict";Wt()});var Jb=c(()=>{"use strict"});var Kl=c(()=>{"use strict";k();Jb();Ls()});var zb=c(()=>{"use strict";k();Wt();Kb();Bl();pn();Kl();Ns()});var Qb=c(()=>{"use strict";Hb();$b();Ls();zb();Kl();pn();Ns()});var jt=c(()=>{"use strict";Qb();Wt()});var _r=c(()=>{"use strict";jt()});async function mt(t,e){let n=await zl(t,e);await t.indexArtifacts.replaceFts(n)}async function zl(t,e){let n=await t.serials.getRevision(e),r=await rS(t.getSerialFragments(e)),i=await rS(t.getSummaryFragments(e));return ea({chapterTitles:await UN(t,e),chunks:await t.chunks.listBySerial(e),mentions:await t.mentions.listByChapter(e),sentences:r,serialId:e,sourceRevision:n,summarySentences:i})}function ea(t){return{lexicalRows:[...(t.chapterTitles??[]).map(e=>Ds({objectId:String(e.id),objectKind:"chapter-title",rowId:`chapter-title:${e.id}`,text:e.title})),...t.sentences.map((e,n)=>nS({objectKind:"source-sentence",rowPrefix:"source-sentence",sentence:e,sentenceIndex:n,serialId:t.serialId})),...(t.summarySentences??[]).map((e,n)=>nS({objectKind:"summary-sentence",rowPrefix:"summary-sentence",sentence:e,sentenceIndex:n,serialId:t.serialId})),...(t.chunks??[]).flatMap(e=>[Ds({metadata:{wordsCount:e.wordsCount},objectId:String(e.id),objectKind:"chunk-label",rowId:`chunk-label:${e.id}`,text:e.label}),Ds({metadata:{wordsCount:e.wordsCount},objectId:String(e.id),objectKind:"chunk-content",rowId:`chunk-content:${e.id}`,text:e.content})]),...(t.mentions??[]).map(e=>Ds({objectId:e.qid,objectKind:"mention-surface",rowId:`mention-surface:${e.id}`,text:e.surface}))],metadata:{source:"chapter-lexical",version:1},serialId:t.serialId,sourceRevision:t.sourceRevision}}async function Ms(t){let e=qN(t.sentences),n=e.length===0?[]:(await t.embeddingProvider.embedTexts(e.map(i=>i.text),t.signal===void 0?void 0:{signal:t.signal})).embeddings;if(n.length!==e.length)throw new Error(`Embedding provider returned ${n.length} vectors for ${e.length} segments.`);let r=t.embeddingProvider.dimensions??n[0]?.length??0;if(e.length>0&&r<=0)throw new Error("Embedding provider returned no usable vector dimensions.");return{kind:t.kind,metadata:{dimensions:r,...t.embeddingProvider.identity===void 0?{}:{identity:t.embeddingProvider.identity},model:t.embeddingProvider.model,version:1},segments:e.map((i,a)=>{let o=n[a]??[];if(o.length!==r)throw new Error(`Embedding provider returned ${o.length} dimensions; expected ${r}.`);return{...i,vector:o}}),serialId:t.serialId,sourceRevision:t.sourceRevision}}function nS(t){let e=t.sentence,n=Mt(e.text),r={tier1:n.tier1.map(i=>i.encoded),tier2:n.tier2.map(i=>i.encoded),tier3:n.tier3.map(i=>i.encoded)};return{metadata:{tiers:r,wordsCount:e.wordsCount},objectId:`${t.serialId}:${t.sentenceIndex}`,objectKind:t.objectKind,rowId:`${t.rowPrefix}:${t.sentenceIndex}`,sentenceIndex:t.sentenceIndex,text:e.text,tokens:[...r.tier1,...r.tier2,...r.tier3]}}function Ds(t){let e=Mt(t.text),n={tier1:e.tier1.map(r=>r.encoded),tier2:e.tier2.map(r=>r.encoded),tier3:e.tier3.map(r=>r.encoded)};return{metadata:{...t.metadata??{},tiers:n},objectId:t.objectId,objectKind:t.objectKind,rowId:t.rowId,text:t.text,tokens:[...n.tier1,...n.tier2,...n.tier3]}}async function rS(t){if(t.listSentences===void 0)throw new Error("Text stream does not expose sentence listing.");return await t.listSentences()}async function UN(t,e){let n=await HN(t);if(n===void 0)return[];let i=aS(n.items).find(a=>a.serialId===e);return i===void 0||typeof i.title!="string"?[]:[{id:e,title:i.title}]}async function HN(t){return await t.readToc?.()}function aS(t){return t.flatMap(e=>[e,...aS(e.children)])}function qN(t){$N();let e=t.map((i,a)=>({sentenceIndex:a,text:i.text,wordsCount:KN(i.wordsCount)})).filter(i=>i.text.trim()!==""),n=[],r=0;for(;r<e.length;){let i=r,a=0;for(;i<e.length;){let u=Math.max(0,e[i].wordsCount);if(i>r&&a>=mn&&a+u>Ki||(a+=u,i+=1,a>=Er))break}let o=e.slice(r,i),s=iS(o,n.length);if(s.wordsCount<mn&&n.length>0){let u=n.pop(),l=e.filter(m=>m.sentenceIndex>=u.startSentenceIndex&&m.sentenceIndex<=s.endSentenceIndex);n.push(iS(l,n.length));break}if(n.push(s),i>=e.length)break;let d=JN(e,r,i);r=d<=r?i:d}return n.map((i,a)=>({...i,segmentIndex:a}))}function $N(){if(mn<0||Ji<0||Er<mn||Ki<Er)throw new Error("Invalid Dense segment word-count configuration.")}function KN(t){if(!Number.isFinite(t)||t<0)throw new Error("Sentence word count must be non-negative.");return t}function iS(t,e){let n=t[0],r=t.at(-1);if(n===void 0||r===void 0)throw new Error("Cannot create an empty embedding segment.");return{endSentenceIndex:r.sentenceIndex,segmentIndex:e,startSentenceIndex:n.sentenceIndex,text:t.map(i=>i.text).join(`
|
|
1559
|
+
`),wordsCount:t.reduce((i,a)=>i+a.wordsCount,0)}}function JN(t,e,n){let r=0;for(let i=n-1;i>e;i-=1)if(r+=Math.max(0,t[i].wordsCount),r>=Ji)return i;return n}var oS=c(()=>{"use strict";_r();Wt()});import{createReadStream as zN,createWriteStream as QN}from"fs";import{mkdir as XN}from"fs/promises";import{dirname as VN}from"path";import{createInterface as YN}from"readline";import{z as C}from"zod";async function ta(t,e){await XN(VN(t),{recursive:!0});let n=QN(t,{encoding:"utf8",flags:"w"});await rD(n,e)}async function rD(t,e){let n=pD(t);try{let r=iD(e);if(await Ql(t,n,r),"lexicalRows"in e){for(let i of e.lexicalRows)await Ql(t,n,{type:"lexical-row",...mD(i)});return}for(let i of e.segments)await Ql(t,n,{type:"segment",...i})}finally{try{await hD(t,n)}finally{await gD(),n.dispose()}}}async function na(t){let e=YN({crlfDelay:1/0,input:zN(t,{encoding:"utf8"})}),n,r=[],i=[],a=0;for await(let o of e){if(a+=1,o.trim()==="")continue;let s;try{s=JSON.parse(o)}catch(d){throw new Error(`Invalid index artifact JSONL at ${t}:${a}`,{cause:d})}if(n===void 0){n=aD(s,t,a);continue}if(sS(n)){r.push(oD(s,t,a));continue}i.push(sD(s,t,a))}if(n===void 0)throw new Error(`Index artifact output ${t} has no manifest.`);return sS(n)?{lexicalRows:r,serialId:n.chapterId,sourceRevision:n.inputRevision}:(dD(n),cD(n,i),{kind:n.artifactKind,metadata:{dimensions:n.embedding.dimensions,...n.embedding.identity===void 0?{}:{identity:n.embedding.identity},model:n.embedding.model,version:1},segments:i,serialId:n.chapterId,sourceRevision:n.inputRevision})}function iD(t){if("lexicalRows"in t)return{artifactKind:"fts",chapterId:t.serialId,inputRevision:t.sourceRevision,protocol:Xl,type:"manifest"};let e=uD(t.metadata,"dimensions"),n=lD(t.metadata,"model"),r=dS(t.metadata,"identity");return{artifactKind:t.kind,chapterId:t.serialId,embedding:{dimensions:e,...r===void 0?{}:{identity:r},model:n},inputRevision:t.sourceRevision,protocol:Xl,type:"manifest"}}function aD(t,e,n){try{let r=eD.parse(t);if(r.artifactKind==="fts"&&r.embedding!==void 0)throw new Error("FTS output manifest must not include embedding data.");if(r.artifactKind!=="fts"&&r.embedding===void 0)throw new Error("Embedding output manifest must include embedding data.");return r}catch(r){throw new Error(`Invalid index artifact output manifest at ${e}:${n}`,{cause:r})}}function oD(t,e,n){try{let r=tD.parse(t);return{metadata:r.metadata??{},objectId:r.objectId,objectKind:r.objectKind,rowId:r.rowId,...r.sentenceIndex===void 0?{}:{sentenceIndex:r.sentenceIndex},text:r.text,tokens:r.tokens}}catch(r){throw new Error(`Invalid index artifact lexical row at ${e}:${n}`,{cause:r})}}function sD(t,e,n){try{let r=nD.parse(t);if(r.endSentenceIndex<r.startSentenceIndex)throw new Error(`Segment ${r.segmentIndex} ends before it starts.`);return{endSentenceIndex:r.endSentenceIndex,segmentIndex:r.segmentIndex,startSentenceIndex:r.startSentenceIndex,text:r.text,vector:r.vector,wordsCount:r.wordsCount}}catch(r){throw new Error(`Invalid index artifact embedding segment at ${e}:${n}`,{cause:r})}}function dD(t){if(t.artifactKind==="fts"||t.embedding===void 0)throw new Error("Expected an embedding index artifact output manifest.")}function cD(t,e){let n=new Set;for(let r of e){if(n.has(r.segmentIndex))throw new Error(`Duplicate embedding segment ${r.segmentIndex}.`);if(r.vector.length!==t.embedding.dimensions)throw new Error(`Embedding segment ${r.segmentIndex} has ${r.vector.length} dimensions; expected ${t.embedding.dimensions}.`);n.add(r.segmentIndex)}}function sS(t){return t.artifactKind==="fts"}function uD(t,e){let n=t?.[e];if(typeof n=="number"&&Number.isInteger(n)&&n>=0)return n;throw new Error(`Embedding index artifact metadata ${e} must be a nonnegative integer.`)}function dS(t,e){let n=t?.[e];if(n!==void 0){if(typeof n=="string")return n;throw new Error(`Embedding index artifact metadata ${e} must be a string.`)}}function lD(t,e){let n=dS(t,e);if(n!==void 0)return n;throw new Error(`Embedding index artifact metadata is missing ${e}.`)}function mD(t){if(t.metadata!==void 0&&typeof t.metadata=="object"&&t.metadata!==null&&Object.keys(t.metadata).length===0){let{metadata:e,...n}=t;return n}return t}function pD(t){let e,n=new Set,r=i=>{e??=i;for(let a of n)a(e)};return t.on("error",r),{dispose(){t.off("error",r),n.clear()},race(i){return e!==void 0?Promise.reject(e):new Promise((a,o)=>{let s=u=>o(u),d=()=>{n.delete(s)};n.add(s),i.then(u=>{d(),a(u)},u=>{d(),o(cS(u))})})}}}async function hD(t,e){if(t.closed)return;let n=fD(t);if(t.destroyed){await e.race(n);return}await e.race(new Promise((r,i)=>{try{t.end(a=>{if(a!=null){i(a);return}r()})}catch(a){i(cS(a))}})),await e.race(n)}function fD(t){return t.closed?Promise.resolve():new Promise(e=>{t.once("close",e)})}function gD(){return new Promise(t=>{setImmediate(t)})}function cS(t){return t instanceof Error?t:new Error(String(t))}async function Ql(t,e,n){await e.race(new Promise((r,i)=>{let a=!1,o=!1,s=!1,d=!1,u=()=>{t.off("drain",p)},l=y=>{if(!s){if(s=!0,u(),y!=null){i(y);return}r()}},m=()=>{d&&(!o||a)&&l()},p=()=>{a=!0,m()};t.write(`${JSON.stringify(n)}
|
|
1560
|
+
`,y=>{if(y!=null){l(y);return}d=!0,m()})||(o=!0,t.once("drain",p))}))}var Xl,ZN,eD,tD,nD,uS=c(()=>{"use strict";Xl="wikg-index-output/v1",ZN=C.enum(["fts","embedding-source","embedding-summary"]),eD=C.object({artifactKind:ZN,chapterId:C.number().int().nonnegative(),embedding:C.object({dimensions:C.number().int().nonnegative(),identity:C.string().optional(),model:C.string()}).optional(),inputRevision:C.number().int().nonnegative(),protocol:C.literal(Xl),type:C.literal("manifest")}),tD=C.object({metadata:C.record(C.string(),C.unknown()).optional(),objectId:C.string().min(1),objectKind:C.string().min(1),rowId:C.string().min(1),sentenceIndex:C.number().int().nonnegative().optional(),text:C.string(),tokens:C.array(C.string()),type:C.literal("lexical-row")}),nD=C.object({endSentenceIndex:C.number().int().nonnegative(),segmentIndex:C.number().int().nonnegative(),startSentenceIndex:C.number().int().nonnegative(),text:C.string(),vector:C.array(C.number()),wordsCount:C.number().int().nonnegative(),type:C.literal("segment")})});var Cr=c(()=>{"use strict";oS();uS()});var Ee,A,pt,Vl,We=c(()=>{"use strict";Ee="database.db",A="index.db",pt="fts.db",Vl=Symbol("WikgArchiveSession constructor token")});import{readdir as ED}from"fs/promises";import{join as ID,posix as xD,relative as kD,sep as RD}from"path";async function Yl(t,e=t){let n=await ED(e,{withFileTypes:!0}),r=[];for(let i of[...n].sort(vD)){let a=ID(e,i.name);if(i.isDirectory()){r.push(...await Yl(t,a));continue}i.isFile()&&r.push({absolutePath:a,archivePath:kD(t,a).split(RD).join(xD.sep)})}return r.filter(i=>lt(i.archivePath))}function lS(t){return!(t.archivePath==="manifest.json"||t.archivePath===".wikg-mutation-token"||t.archivePath===Nl||t.archivePath===Ol)}function vD(t,e){return t.name.localeCompare(e.name)}var mS=c(()=>{"use strict";Ot();gn()});import{createWriteStream as TD}from"fs";import{mkdir as hS,open as _D}from"fs/promises";import{dirname as fS}from"path";import{finished as pS}from"stream/promises";import{ZipFile as gS}from"yazl";async function Pr(t,e){await hS(fS(e),{recursive:!0});let n=new gS,r=await Yl(t),i=Fb([{archivePath:ee,content:Fl()},{archivePath:fe,content:Buffer.from(Wl,"utf8")},...r.filter(a=>lS({archivePath:a.archivePath}))]);for(let a of i)"content"in a?n.addBuffer(a.content,a.archivePath,{compress:!1}):n.addFile(a.absolutePath,a.archivePath,{compress:!1});n.end(),await yS(n,e)}async function ra(t,e,n,r={}){await hS(fS(e),{recursive:!0});let{entries:i,zipFile:a}=await Nt(t),o=new Map(n.map(l=>[te(l.entryPath),l])),s=new Set;for(let l of i){let m=te(l.fileName);m!==""&<(m)&&s.add(m)}for(let l of o.values()){let m=te(l.entryPath);m!==""&<(m)&&s.add(m)}s.add(ee),s.add(fe);let d=new gS,u=await _D(t,"r");try{for(let l of jb(s)){let m=o.get(l);if(l===ee){if(r.preserveMutationToken===!0){let h=i.find(y=>te(y.fileName)===l);if(h!==void 0){d.addBuffer(await Dt(u,h),l,{compress:!1});continue}}d.addBuffer(Fl(),l,{compress:!1});continue}if(l===fe){d.addBuffer(Buffer.from(Wl,"utf8"),l,{compress:!1});continue}if(m?.kind==="deleted")continue;if(m?.kind==="file"){d.addFile(m.workspacePath,l,{compress:!1});continue}let p=i.find(h=>te(h.fileName)===l);p!==void 0&&d.addBuffer(await Dt(u,p),l,{compress:!1})}}finally{await u.close(),a.close()}d.end(),await yS(d,e)}async function yS(t,e){let n=TD(e),r=pS(n),i=pS(t.outputStream);t.outputStream.pipe(n),await Promise.all([r,i])}var Zl=c(()=>{"use strict";Ot();mS();vr();gn();Yi()});import{createHash as CD,randomUUID as PD}from"crypto";import{stat as bS}from"fs/promises";import{resolve as AD}from"path";function Ar(t){return CD("sha256").update(AD(t)).digest("hex")}async function Sn(t){let e=await bS(t);return`${e.size}:${e.mtimeMs}`}function wn(){return`${process.pid}-${PD()}`}async function ia(t){await new Promise(e=>{setTimeout(e,t)})}async function Gt(t){try{return await bS(t),!0}catch(e){if(E(e)&&e.code==="ENOENT")return!1;throw e}}var ht=c(()=>{"use strict";T()});import{randomUUID as o3}from"crypto";import{mkdtemp as d3,rename as c3,rm as u3,stat as l3,writeFile as m3}from"fs/promises";import{tmpdir as h3}from"os";import{dirname as g3,join as y3,resolve as SS}from"path";async function tm(t){let e=await nm(t);if(e>em)throw new Error(`Unsupported Wiki Graph archive schema version: ${e}.`);if(e<em)throw new Error(`This Wiki Graph archive uses schema v${e} and must be upgraded before use.
|
|
1561
|
+
Run: wg maintenance upgrade ${t}`)}async function nm(t){let{entries:e,zipFile:n}=await Nt(SS(t));try{let r=e.find(i=>te(i.fileName)===fe);if(r===void 0)throw new Error(`Missing WIKG manifest: ${fe}.`);return Rr(await Cs(SS(t),r)).schemaVersion}finally{n.close()}}var em,N3,Ws=c(()=>{"use strict";N();Ui();cs();an();Cr();me();on();Ot();We();jl();vr();gn();Yi();Zl();ht();fs();an();em=3,N3=300*1e3});import{open as LD,readFile as OD}from"fs/promises";import{join as ND}from"path";async function Fs(t){let e=await ft.open(t);try{return e.listEntries()}finally{e.close()}}async function Lr(t,e){let n=await ft.open(t);try{return await n.readEntry(e)}finally{n.close()}}async function En(t){let e=await ft.open(t);try{let n=await e.readEntry(ee);if(n===void 0)throw new Error(`Missing WIKG mutation token: ${ee}.`);return _s(n.toString("utf8"))}finally{e.close()}}async function aa(t){return Rr(await OD(ND(t,fe),"utf8")).formatVersion}var ft,wS=c(()=>{"use strict";Ot();vr();gn();Yi();Ws();ft=class t{#e;#t;#n;#r;#a;constructor(e,n,r){this.#r=e,this.#a=n,this.#e=new Map(r.map(i=>[te(i.fileName),i]).filter(([i])=>i!=="").filter(([i])=>lt(i))),this.#t=[...this.#e.keys()].sort((i,a)=>i.localeCompare(a))}static async open(e){await tm(e);let{entries:n,zipFile:r}=await Nt(e);return new t(e,r,n)}close(){this.#a.close(),this.#n!==void 0&&(this.#n.then(async e=>{await e.close()}),this.#n=void 0)}listEntries(){return this.#t}async readEntry(e){let n=this.#e.get(te(e));if(n!==void 0)return await Dt(await this.#i(),n)}async#i(){return this.#n??=LD(this.#r,"r"),await this.#n}}});var Bt=c(()=>{"use strict";jl();wS();vr();Zl()});var CS=c(()=>{"use strict";Lt()});import{mkdir as MD,mkdtemp as WD}from"fs/promises";import{join as PS}from"path";function Or(){return rr()}function im(t){let e=Or();return t===void 0?e:PS(e,t)}async function gt(t){let e=im(t);return await MD(e,{recursive:!0}),await WD(PS(e,`${t}-`))}var yt=c(()=>{"use strict";me()});import{join as FD}from"path";async function ne(t){let e=Date.now(),n=new Set;for(let r of["archive_owners","entry_locks","entry_sqlite_leases","archive_commit_locks"]){let i=await t.queryAll(`
|
|
1368
1562
|
SELECT DISTINCT owner_pid
|
|
1369
|
-
FROM ${
|
|
1563
|
+
FROM ${r}
|
|
1370
1564
|
WHERE owner_pid IS NOT NULL
|
|
1371
1565
|
AND heartbeat_at <= ?
|
|
1372
|
-
`,[e-6e4],a=>
|
|
1566
|
+
`,[e-6e4],a=>Je(a,"owner_pid"));for(let a of i)BD(a)||n.add(a)}n.size!==0&&(await t.run(`
|
|
1373
1567
|
DELETE FROM archive_owners
|
|
1374
|
-
WHERE owner_pid IN (${
|
|
1375
|
-
`,[...
|
|
1568
|
+
WHERE owner_pid IN (${In(n.size)})
|
|
1569
|
+
`,[...n]),await t.run(`
|
|
1376
1570
|
DELETE FROM entry_locks
|
|
1377
|
-
WHERE owner_pid IN (${
|
|
1378
|
-
`,[...
|
|
1571
|
+
WHERE owner_pid IN (${In(n.size)})
|
|
1572
|
+
`,[...n]),await t.run(`
|
|
1379
1573
|
DELETE FROM entry_sqlite_leases
|
|
1380
|
-
WHERE owner_pid IN (${
|
|
1381
|
-
`,[...
|
|
1574
|
+
WHERE owner_pid IN (${In(n.size)})
|
|
1575
|
+
`,[...n]),await t.run(`
|
|
1382
1576
|
DELETE FROM archive_commit_locks
|
|
1383
|
-
WHERE owner_pid IN (${
|
|
1384
|
-
`,[...
|
|
1577
|
+
WHERE owner_pid IN (${In(n.size)})
|
|
1578
|
+
`,[...n]))}function In(t){return Array.from({length:t},()=>"?").join(", ")}function BD(t){try{return process.kill(t,0),!0}catch(e){return!(e instanceof Error&&"code"in e&&e.code==="ESRCH")}}async function P(t){return await GD.use(async()=>{let e=await UD();try{return await t(e)}finally{await e.close()}})}async function UD(){let t=await D(FD(om(),"staging.sqlite"),jD);return await HD(t),t}async function HD(t){let e=await t.queryAll("PRAGMA table_info(entry_overlays)",void 0,r=>Q(r,"name"));e.includes("archive_signature")||await t.run(`
|
|
1385
1579
|
ALTER TABLE entry_overlays
|
|
1386
1580
|
ADD COLUMN archive_signature TEXT
|
|
1387
1581
|
`),e.includes("mutation_token")||await t.run(`
|
|
1388
1582
|
ALTER TABLE entry_overlays
|
|
1389
1583
|
ADD COLUMN mutation_token TEXT
|
|
1390
|
-
`),(await t.queryAll("PRAGMA table_info(entry_sqlite_leases)",void 0,
|
|
1584
|
+
`),(await t.queryAll("PRAGMA table_info(entry_sqlite_leases)",void 0,r=>Q(r,"name"))).includes("mode")||await t.run(`
|
|
1391
1585
|
ALTER TABLE entry_sqlite_leases
|
|
1392
1586
|
ADD COLUMN mode TEXT NOT NULL DEFAULT 'read'
|
|
1393
|
-
`)}function
|
|
1587
|
+
`)}function om(){return Le()}function Ut(t){let e=am(t,"workspace_path"),n=am(t,"archive_signature"),r=am(t,"mutation_token");return{archiveKey:Q(t,"archive_key"),archivePath:Q(t,"archive_path"),...n===void 0?{}:{archiveSignature:n},entryPath:Q(t,"entry_path"),kind:$D(t),...r===void 0?{}:{mutationToken:r},updatedAt:Je(t,"updated_at"),...e===void 0?{}:{workspacePath:e}}}function sm(t){return{entryPath:Q(t,"entry_path"),mode:qD(t),ownerId:Q(t,"owner_id")}}function AS(t){return{ownerId:Q(t,"owner_id")}}function qD(t){let e=Q(t,"mode");if(e==="read"||e==="state"||e==="write")return e;throw new Error(`Unsupported entry lock mode: ${e}.`)}function $D(t){let e=Q(t,"kind");if(e==="deleted"||e==="file")return e;throw new Error(`Unsupported entry overlay kind: ${e}.`)}function Q(t,e){let n=t[e];if(typeof n!="string")throw new TypeError(`Expected ${e} to be a string`);return n}function Je(t,e){let n=t[e];if(typeof n!="number")throw new TypeError(`Expected ${e} to be a number`);return n}function am(t,e){let n=t[e];if(n!=null){if(typeof n!="string")throw new TypeError(`Expected ${e} to be a string`);return n}}var jD,GD,bt=c(()=>{"use strict";me();N();Ii();We();jD=`
|
|
1394
1588
|
CREATE TABLE IF NOT EXISTS entry_overlays (
|
|
1395
1589
|
archive_key TEXT NOT NULL,
|
|
1396
1590
|
archive_path TEXT NOT NULL,
|
|
@@ -1441,29 +1635,39 @@ CREATE TABLE IF NOT EXISTS archive_commit_locks (
|
|
|
1441
1635
|
heartbeat_at INTEGER NOT NULL,
|
|
1442
1636
|
created_at INTEGER NOT NULL
|
|
1443
1637
|
);
|
|
1444
|
-
`,
|
|
1638
|
+
`,GD=new Ae(1)});async function OS(t){let e=wn();for(;;){if(await P(async r=>(await ne(r),await r.transaction(async()=>await r.queryOne("SELECT * FROM archive_commit_locks WHERE archive_key = ?",[t],AS)!==void 0?!1:(await r.run(`
|
|
1445
1639
|
INSERT INTO archive_commit_locks (
|
|
1446
1640
|
archive_key, owner_id, owner_pid, heartbeat_at, created_at
|
|
1447
1641
|
) VALUES (?, ?, ?, ?, ?)
|
|
1448
|
-
`,[t,e,process.pid,Date.now(),Date.now()]),!0)))))return async()=>{await
|
|
1642
|
+
`,[t,e,process.pid,Date.now(),Date.now()]),!0)))))return async()=>{await P(async r=>{await r.run("DELETE FROM archive_commit_locks WHERE archive_key = ? AND owner_id = ?",[t,e])})};await ia(100)}}async function Fe(t,e,n){let r=wn();for(;;){if(await P(async a=>(await ne(a),await a.transaction(async()=>(await a.queryAll(`
|
|
1449
1643
|
SELECT *
|
|
1450
1644
|
FROM entry_locks
|
|
1451
1645
|
WHERE archive_key = ?
|
|
1452
|
-
`,[t],
|
|
1646
|
+
`,[t],sm)).some(s=>s.ownerId!==r&&KD(e,s.entryPath)&&NS(n,s.mode))?!1:(await a.run(`
|
|
1453
1647
|
INSERT INTO entry_locks (
|
|
1454
1648
|
archive_key, entry_path, mode, owner_id, owner_pid, heartbeat_at, created_at
|
|
1455
1649
|
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
1456
|
-
`,[t,e,r,
|
|
1650
|
+
`,[t,e,n,r,process.pid,Date.now(),Date.now()]),!0)))))return async()=>{await P(async a=>{await a.run(`
|
|
1457
1651
|
DELETE FROM entry_locks
|
|
1458
1652
|
WHERE archive_key = ? AND entry_path = ? AND owner_id = ?
|
|
1459
|
-
`,[t,e,
|
|
1653
|
+
`,[t,e,r])})};await ia(100)}}async function ze(t,e,n,r){let i=await Fe(t,e,n);try{return await r()}finally{await i()}}function NS(t,e){return t==="state"||e==="state"?t==="state"&&e==="state":!(t==="read"&&e==="read")}function KD(t,e){return t===e||LS(t,e)||LS(e,t)}function LS(t,e){return t.endsWith("/")&&e.startsWith(t)}async function DS(t){let e=Date.now()+6e4;for(;;){if(await P(async r=>(await ne(r),await r.transaction(async()=>{let i=await r.queryAll(`
|
|
1654
|
+
SELECT *
|
|
1655
|
+
FROM entry_locks
|
|
1656
|
+
WHERE archive_key = ?
|
|
1657
|
+
AND entry_path = ?
|
|
1658
|
+
`,[t.archiveKey,t.entryPath],sm),a=JD(t.mode);return i.some(s=>NS(a,s.mode))||(await r.queryAll(`
|
|
1659
|
+
SELECT owner_id, mode
|
|
1660
|
+
FROM entry_sqlite_leases
|
|
1661
|
+
WHERE archive_key = ?
|
|
1662
|
+
AND entry_path = ?
|
|
1663
|
+
`,[t.archiveKey,t.entryPath],s=>({mode:QD(s),ownerId:Q(s,"owner_id")}))).some(s=>s.ownerId!==t.ownerId&&zD(t.mode,s.mode))?!1:(await r.run(`
|
|
1460
1664
|
INSERT OR REPLACE INTO entry_sqlite_leases (
|
|
1461
1665
|
archive_key, entry_path, mode, owner_id, owner_pid, heartbeat_at, created_at
|
|
1462
1666
|
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
1463
|
-
`,[t.archiveKey,t.entryPath,t.mode,t.ownerId,process.pid,Date.now(),Date.now()])})}async function
|
|
1667
|
+
`,[t.archiveKey,t.entryPath,t.mode,t.ownerId,process.pid,Date.now(),Date.now()]),!0)}))))return;if(Date.now()>=e)throw new Error(`Timed out waiting for SQLite ${t.mode} lease: ${t.entryPath}.`);await ia(100)}}async function oa(t){await P(async e=>{await e.run(`
|
|
1464
1668
|
DELETE FROM entry_sqlite_leases
|
|
1465
1669
|
WHERE archive_key = ? AND entry_path = ? AND owner_id = ?
|
|
1466
|
-
`,[t.archiveKey,t.entryPath,t.ownerId])})}async function
|
|
1670
|
+
`,[t.archiveKey,t.entryPath,t.ownerId])})}async function sa(t,e,n={}){let r=Date.now()+6e4;for(;;){if(await P(async a=>(await ne(a),await a.queryOne(`
|
|
1467
1671
|
SELECT COUNT(*) AS count
|
|
1468
1672
|
FROM entry_sqlite_leases AS lease
|
|
1469
1673
|
LEFT JOIN archive_owners AS owner
|
|
@@ -1471,20 +1675,21 @@ LEFT JOIN archive_owners AS owner
|
|
|
1471
1675
|
AND owner.owner_id = lease.owner_id
|
|
1472
1676
|
WHERE lease.archive_key = ?
|
|
1473
1677
|
AND lease.entry_path = ?
|
|
1678
|
+
AND lease.owner_id <> ?
|
|
1474
1679
|
AND (
|
|
1475
1680
|
owner.owner_id IS NULL
|
|
1476
1681
|
OR owner.heartbeat_at > ?
|
|
1477
1682
|
)
|
|
1478
|
-
`,[t,e,Date.now()-6e4],
|
|
1683
|
+
`,[t,e,n.exceptOwnerId??"",Date.now()-6e4],o=>Je(o,"count"))))===0)return;if(Date.now()>=r)throw new Error(`Timed out waiting for SQLite leases: ${e}.`);await ia(100)}}function JD(t){return t==="read"?"read":"write"}function zD(t,e){return!(t==="read"&&e==="read")}function QD(t){let e=Q(t,"mode");if(e==="read"||e==="write")return e;throw new Error(`Unsupported SQLite lease mode: ${e}.`)}var da=c(()=>{"use strict";ht();We();bt()});async function Gs(t,e){let n=t.filter(a=>a.startsWith(e.prefix)),r=await P(async a=>await a.queryAll(`
|
|
1479
1684
|
SELECT *
|
|
1480
1685
|
FROM entry_overlays
|
|
1481
1686
|
WHERE archive_key = ?
|
|
1482
|
-
`,[e.archiveKey],
|
|
1687
|
+
`,[e.archiveKey],Ut)),i=new Set(n);for(let a of r)a.entryPath.startsWith(e.prefix)&&(a.kind==="deleted"?i.delete(a.entryPath):i.add(a.entryPath));return[...i].sort((a,o)=>a.localeCompare(o))}async function dm(t){let e=await St(t.archiveKey,t.entryPath);return e?.kind==="deleted"?{kind:"deleted"}:e?.workspacePath!==void 0?{kind:"workspace",path:e.workspacePath}:{kind:"archive"}}async function St(t,e){return await P(async n=>await n.queryOne("SELECT * FROM entry_overlays WHERE archive_key = ? AND entry_path = ?",[t,e],Ut))}async function kn(t){return await P(async e=>await e.queryAll(`
|
|
1483
1688
|
SELECT *
|
|
1484
1689
|
FROM entry_overlays
|
|
1485
1690
|
WHERE archive_key = ?
|
|
1486
1691
|
ORDER BY entry_path ASC
|
|
1487
|
-
`,[t],
|
|
1692
|
+
`,[t],Ut))}async function ca(t){let e=t.entryPath===A?await En(t.archivePath):void 0;await P(async n=>{await n.run(`
|
|
1488
1693
|
INSERT INTO entry_overlays (
|
|
1489
1694
|
archive_key, archive_path, entry_path, kind, workspace_path,
|
|
1490
1695
|
archive_signature, mutation_token, updated_at
|
|
@@ -1496,16 +1701,12 @@ DO UPDATE SET kind = excluded.kind,
|
|
|
1496
1701
|
archive_signature = excluded.archive_signature,
|
|
1497
1702
|
mutation_token = excluded.mutation_token,
|
|
1498
1703
|
updated_at = excluded.updated_at
|
|
1499
|
-
`,[t.archiveKey,t.archivePath,t.entryPath,t.kind,t.workspacePath??null,await
|
|
1500
|
-
SELECT value
|
|
1501
|
-
FROM search_index_state
|
|
1502
|
-
WHERE key = 'fingerprint'
|
|
1503
|
-
`,void 0,e=>String(e.value))}var os=d(()=>{"use strict"});var mn=d(()=>{"use strict"});var gl=d(()=>{"use strict";mn()});var Ib=d(()=>{"use strict";x();gl()});var Rb=d(()=>{"use strict";Wi();os();Ib();mn()});var Tb=d(()=>{"use strict";mn()});var _b=d(()=>{"use strict";Wi()});var Cb=d(()=>{"use strict"});var Sl=d(()=>{"use strict";x();Cb();os()});var Pb=d(()=>{"use strict";x();Wi();_b();gl();mn();Sl()});var Lb=d(()=>{"use strict";x()});var Ab=d(()=>{"use strict";Rb();Tb();os();Pb();Lb();Sl();mn()});var Dt=d(()=>{"use strict";Ab();Wi()});var hn=d(()=>{"use strict";Dt()});async function cs(t,e){return await C(async r=>{await V(r);let n=await r.queryOne("SELECT COUNT(*) AS count FROM archive_owners WHERE archive_key = ?",[t],a=>Ue(a,"count")),i=await r.queryOne(`
|
|
1704
|
+
`,[t.archiveKey,t.archivePath,t.entryPath,t.kind,t.workspacePath??null,await Sn(t.archivePath),e??null,Date.now()])})}async function Nr(t,e){await P(async n=>{await n.run("DELETE FROM entry_overlays WHERE archive_key = ? AND entry_path = ?",[t,e])})}async function MS(t,e){let n=await St(t,e);n?.workspacePath!==void 0&&await ca({archiveKey:t,archivePath:n.archivePath,entryPath:e,kind:n.kind,workspacePath:n.workspacePath})}async function WS(t){return await P(async e=>await e.queryOne("SELECT archive_path FROM entry_overlays WHERE archive_key = ? LIMIT 1",[t],n=>Q(n,"archive_path")))}function FS(t){if(t.kind==="deleted")return{entryPath:t.entryPath,kind:"deleted"};if(t.workspacePath===void 0)throw new Error(`Missing workspace path for ${t.entryPath}.`);return{entryPath:t.entryPath,kind:"file",workspacePath:t.workspacePath}}var Dr=c(()=>{"use strict";Bt();ht();We();bt()});async function Bs(t,e){return await P(async n=>{await ne(n);let r=await n.queryOne("SELECT COUNT(*) AS count FROM archive_owners WHERE archive_key = ?",[t],a=>Je(a,"count")),i=await n.queryOne(`
|
|
1504
1705
|
SELECT COUNT(*) AS count
|
|
1505
1706
|
FROM entry_sqlite_leases
|
|
1506
1707
|
WHERE archive_key = ?
|
|
1507
1708
|
AND entry_path = ?
|
|
1508
|
-
`,[t,e],a=>
|
|
1709
|
+
`,[t,e],a=>Je(a,"count"));return(r??0)>0||(i??0)>0})}async function Us(t){return await P(async e=>{await ne(e);let n=await e.queryOne("SELECT COUNT(*) AS count FROM archive_owners WHERE archive_key = ?",[t],a=>Je(a,"count")),r=await e.queryOne("SELECT COUNT(*) AS count FROM entry_sqlite_leases WHERE archive_key = ?",[t],a=>Je(a,"count")),i=await e.queryOne("SELECT COUNT(*) AS count FROM entry_locks WHERE archive_key = ?",[t],a=>Je(a,"count"));return(n??0)>0||(r??0)>0||(i??0)>0})}var cm=c(()=>{"use strict";bt()});import{mkdir as jS,readdir as GS}from"fs/promises";import{basename as XD,dirname as VD,join as Hs}from"path";async function ua(t,e){let n=Hs(qs(),t,VD(e));return await jS(n,{recursive:!0}),Hs(n,XD(e))}function qs(){return Hs(om(),"work")}async function BS(t){if(await jS(t,{recursive:!0}),(await GS(t)).length>0)throw new Error(`Read workspace directory is not empty: ${t}`)}async function um(t){let e;try{e=await GS(t,{withFileTypes:!0})}catch(r){if(E(r)&&r.code==="ENOENT")return[];throw r}let n=[];for(let r of e){let i=Hs(t,r.name);r.isDirectory()?n.push(...await um(i)):r.isFile()&&n.push(i)}return n}function $s(t){return t.replaceAll("\\","/").replace(/^\/+/u,"")}function US(t){let e=$s(t);return e===""||e.endsWith("/")?e:`${e}/`}var la=c(()=>{"use strict";T();bt()});import{mkdir as YD,rename as ZD,rm as eM}from"fs/promises";import{dirname as tM}from"path";async function HS(t){if(t.workspacePath===void 0||!await Gt(t.workspacePath))return"missing";if(!await Gt(t.archivePath))return"dirty";try{let e=await nM(t.workspacePath),n=await Sn(t.archivePath);return e===void 0?"dirty":t.archiveSignature===n?"current":"dirty"}catch{return"dirty"}}async function nM(t){await Re();let e=await O.open(t,"",{readonly:!0});try{return await Zi(e)}finally{await e.close()}}async function qS(t){let e=await En(t.targetArchivePath),n=await rM({mutationToken:e,targetArchiveKey:t.targetArchiveKey}),r=[];for(let o of n)o.workspacePath===void 0||await Gt(o.archivePath)||await Us(o.archiveKey)||r.push(o);if(r.length!==1)return;let i=r[0],a=await Fe(i.archiveKey,A,"write");try{let o=await Fe(i.archiveKey,A,"state");try{let s=await St(i.archiveKey,A),d=await St(t.targetArchiveKey,A);if(s?.workspacePath===void 0||s.workspacePath!==i.workspacePath||s.mutationToken!==e||d!==void 0||await Gt(i.archivePath)||await Bs(i.archiveKey,A))return;let u=await ua(t.targetArchiveKey,A);await eM(u,{force:!0}).catch(()=>{}),await YD(tM(u),{recursive:!0}),await ZD(s.workspacePath,u),await P(async l=>{await l.run(`
|
|
1509
1710
|
UPDATE entry_overlays
|
|
1510
1711
|
SET archive_key = ?,
|
|
1511
1712
|
archive_path = ?,
|
|
@@ -1514,7 +1715,7 @@ SET archive_key = ?,
|
|
|
1514
1715
|
updated_at = ?
|
|
1515
1716
|
WHERE archive_key = ?
|
|
1516
1717
|
AND entry_path = ?
|
|
1517
|
-
`,[t.targetArchiveKey,t.targetArchivePath,u,await
|
|
1718
|
+
`,[t.targetArchiveKey,t.targetArchivePath,u,await Sn(t.targetArchivePath),Date.now(),i.archiveKey,A])})}finally{await o()}}finally{await a()}}async function rM(t){return await P(async e=>(await ne(e),await e.queryAll(`
|
|
1518
1719
|
SELECT *
|
|
1519
1720
|
FROM entry_overlays
|
|
1520
1721
|
WHERE entry_path = ?
|
|
@@ -1523,28 +1724,28 @@ WHERE entry_path = ?
|
|
|
1523
1724
|
AND mutation_token = ?
|
|
1524
1725
|
AND archive_key <> ?
|
|
1525
1726
|
ORDER BY updated_at ASC
|
|
1526
|
-
`,[
|
|
1727
|
+
`,[A,t.mutationToken,t.targetArchiveKey],Ut)))}var lm=c(()=>{"use strict";N();an();_r();Bt();ht();cm();We();da();Dr();bt();la()});import{mkdir as $S,readFile as KS,rename as iM,rm as Ks,writeFile as JS}from"fs/promises";import{dirname as zS,posix as aM,resolve as QS}from"path";var Mr,mm=c(()=>{"use strict";an();Bt();ht();We();da();Dr();lm();la();Mr=class{#e;#t;#n;#r;#a;#i=wn();constructor(e,n){this.#t=QS(e),this.#e=Ar(this.#t),this.#a=n.readonlyDatabase===!0,this.#s=n.session}async#o(e){let n=await St(this.#e,e);if(n===void 0)return;let r=await Sn(this.#t);if(n.archiveSignature===r)return n;await Nr(this.#e,e),n.workspacePath!==void 0&&await Ks(n.workspacePath,{force:!0}).catch(()=>{}),this.#r?.delete(e)}#s;async close(){this.#n!==void 0&&((await this.#n).close(),this.#n=void 0),await oa({archiveKey:this.#e,entryPath:Ee,ownerId:this.#i}),await oa({archiveKey:this.#e,entryPath:A,ownerId:this.#i}),await oa({archiveKey:this.#e,entryPath:pt,ownerId:this.#i})}async deleteFile(e){let n=this.#c(e);await ze(this.#e,n,"write",async()=>{(n===Ee||n===A||n===pt)&&await sa(this.#e,n,{exceptOwnerId:this.#i}),await ze(this.#e,n,"state",async()=>{let r=await this.#o(n);await ca({archiveKey:this.#e,archivePath:this.#t,entryPath:n,kind:"deleted"}),r?.workspacePath!==void 0&&await Ks(r.workspacePath,{force:!0}).catch(()=>{}),this.#r?.set(n,{kind:"deleted"}),this.#s?.modifyEntry(n)})})}async deleteTree(e){let n=this.#c(e),r=await Gs(await this.#l(),{archiveKey:this.#e,prefix:`${n}/`});for(let i of r)await this.deleteFile(i)}ensureDirectory(){return Promise.resolve()}initializeDatabaseSchema(){return!1}markDatabaseDirty(){this.#a||this.#s?.observeDirtyEntry(Ee)}markSearchIndexDatabaseDirty(){this.#a||this.#s?.observeDirtyEntry(A)}openDatabaseReadonly(){return this.#a}async listFiles(e){let n=this.#c(e),r=n===""?"":`${n}/`;return(await this.#h(r)).map(([a])=>a).map(a=>a.slice(r.length)).filter(a=>!a.includes("/")).map(a=>aM.basename(a)).sort((a,o)=>a.localeCompare(o))}async listFileContents(e){let n=this.#c(e),r=n===""?"":`${n}/`;return await ze(this.#e,US(r),"read",async()=>{let i=new Map;for(let[a,o]of await this.#h(r)){let s=a.slice(r.length);if(s.includes("/"))continue;if(o.kind==="workspace"){i.set(s,await KS(o.path));continue}let d=await this.#u(a);d!==void 0&&i.set(s,d)}return i})}async readFile(e){let n=this.#c(e);return await ze(this.#e,n,"read",async()=>{let r=(await this.#m()).get(n)??await ze(this.#e,n,"state",async()=>await dm({archiveKey:this.#e,entryPath:n}));if(r.kind==="deleted"){this.#s?.observeDirtyEntry(n);return}return r.kind==="workspace"?(this.#s?.observeDirtyEntry(n),await KS(r.path)):await this.#u(n)})}async resolveDatabasePath(){return await this.#d(Ee,{createIfMissing:!0,readonly:this.#a})}async resolveSearchIndexDatabasePath(){return await this.#d(A,{createIfMissing:!this.#a,readonly:this.#a})}async#d(e,n){e===A&&await Re(),await DS({archiveKey:this.#e,entryPath:e,mode:n.readonly?"read":"write",ownerId:this.#i});try{return await ze(this.#e,e,"state",async()=>{let r=await this.#o(e);if(e===A&&r?.kind!=="file"&&(await qS({targetArchiveKey:this.#e,targetArchivePath:this.#t}),r=await this.#o(e)),r?.kind!=="file"){let i=await Lr(this.#t,e)??(e===A?await Lr(this.#t,pt):void 0);if(i===void 0&&!n.createIfMissing)throw new Error(`Archive SQLite entry is missing: ${e}`);let a=await ua(this.#e,e);await $S(zS(a),{recursive:!0}),await JS(a,i??new Uint8Array),await ca({archiveKey:this.#e,archivePath:this.#t,entryPath:e,kind:"file",workspacePath:a}),r=await this.#o(e)}if(r?.workspacePath===void 0)throw new Error("Could not materialize SQLite database.");return r.workspacePath})}catch(r){throw await oa({archiveKey:this.#e,entryPath:e,ownerId:this.#i}),r}}async writeFile(e,n,r){let i=this.#c(e);await ze(this.#e,i,"write",async()=>{let a=await ze(this.#e,i,"state",async()=>await dm({archiveKey:this.#e,entryPath:i})),o=a.kind==="archive"&&await this.#u(i)!==void 0;if(r.overwrite!==!0&&(a.kind==="workspace"||o))throw new Error(`File already exists: ${e}`);await ze(this.#e,i,"state",async()=>{let s=await ua(this.#e,i);await $S(zS(s),{recursive:!0});let d=`${s}.${process.pid}.${Date.now()}.tmp`;try{await JS(d,n),await iM(d,s)}finally{await Ks(d,{force:!0}).catch(()=>{})}await ca({archiveKey:this.#e,archivePath:this.#t,entryPath:i,kind:"file",workspacePath:s}),this.#r?.set(i,{kind:"workspace",path:s}),a.kind==="workspace"&&a.path!==s&&await Ks(a.path,{force:!0}).catch(()=>{}),this.#s?.modifyEntry(i)})})}#c(e){let n=QS(e),r=this.#t.endsWith("/")?this.#t:`${this.#t}/`;return n.startsWith(r)?$s(n.slice(r.length)):$s(e)}async#l(){return(await this.#p()).listEntries()}async#u(e){return await(await this.#p()).readEntry(e)}async#p(){return this.#n??=ft.open(this.#t),await this.#n}async#m(){if(this.#r!==void 0)return this.#r;let e=new Map;for(let n of await this.#l())e.set(n,{kind:"archive"});for(let n of await kn(this.#e))n.kind==="deleted"?e.set(n.entryPath,{kind:"deleted"}):n.workspacePath!==void 0&&e.set(n.entryPath,{kind:"workspace",path:n.workspacePath});return this.#r=e,e}async#h(e){let n=[];for(let[r,i]of await this.#m())i.kind==="deleted"||!r.startsWith(e)||n.push([r,i]);return n.sort(([r],[i])=>r.localeCompare(i))}}});import{mkdir as oM,mkdtemp as sM,rename as dM,rm as XS}from"fs/promises";import{tmpdir as cM}from"os";import{basename as uM,dirname as lM,join as VS,resolve as Y5}from"path";async function Js(t,e){let n=(await kn(t)).filter(s=>s.entryPath!==A&&(e===void 0||e.has(s.entryPath))),r=await WS(t);if(n.length===0||r===void 0)return;let i=n.map(s=>s.entryPath).sort((s,d)=>s.localeCompare(d)),a=new Set(i),o=[];try{for(let u of i)o.push(await Fe(t,u,"write"));for(let u of i)o.push(await Fe(t,u,"state"));i.includes(Ee)&&await sa(t,Ee),i.includes(A)&&await sa(t,A);let s=(await kn(t)).filter(u=>a.has(u.entryPath));if(s.length===0)return;let d=await OS(t);try{let u=await sM(VS(cM(),"wikigraph-flush-")),l=VS(u,uM(r));try{if(await ra(r,l,[...s.map(FS),...s.some(m=>m.entryPath===Ee||m.entryPath===A)?[{entryPath:pt,kind:"deleted"}]:[]]),await oM(lM(r),{recursive:!0}),await dM(l,r),e?.has(A)===!0&&s.some(m=>m.entryPath===Ee)){let m=await Fe(t,A,"state");try{await MS(t,A)}finally{await m()}}}finally{await XS(u,{force:!0,recursive:!0})}}finally{await d()}for(let u of s)await Nr(t,u.entryPath),u.workspacePath!==void 0&&await XS(u.workspacePath,{force:!0})}finally{for(let s of o.reverse())await s()}}var zs=c(()=>{"use strict";Bt();ht();We();da();Dr();bt()});async function YS(t){await P(async e=>{await ne(e),await e.run(`
|
|
1527
1728
|
INSERT OR REPLACE INTO archive_owners (
|
|
1528
1729
|
archive_key, owner_id, owner_pid, heartbeat_at, created_at
|
|
1529
1730
|
) VALUES (?, ?, ?, ?, ?)
|
|
1530
|
-
`,[t.archiveKey,t.ownerId,process.pid,Date.now(),Date.now()])})}async function
|
|
1731
|
+
`,[t.archiveKey,t.ownerId,process.pid,Date.now(),Date.now()])})}async function ZS(t){await P(async e=>{await e.run(`
|
|
1531
1732
|
UPDATE archive_owners
|
|
1532
1733
|
SET heartbeat_at = ?, owner_pid = ?
|
|
1533
1734
|
WHERE archive_key = ? AND owner_id = ?
|
|
1534
|
-
`,[Date.now(),process.pid,t.archiveKey,t.ownerId])})}async function
|
|
1735
|
+
`,[Date.now(),process.pid,t.archiveKey,t.ownerId])})}async function ew(t){await P(async e=>{await e.transaction(async()=>{await e.run("DELETE FROM entry_sqlite_leases WHERE archive_key = ? AND owner_id = ?",[t.archiveKey,t.ownerId]),await e.run("DELETE FROM archive_owners WHERE archive_key = ? AND owner_id = ?",[t.archiveKey,t.ownerId])})})}async function tw(t){await P(async n=>{await ne(n)});let e=await P(async n=>await n.queryAll(`
|
|
1535
1736
|
SELECT owner_id
|
|
1536
1737
|
FROM archive_owners
|
|
1537
1738
|
WHERE archive_key = ?
|
|
1538
1739
|
AND heartbeat_at <= ?
|
|
1539
|
-
`,[t,Date.now()-6e4],
|
|
1740
|
+
`,[t,Date.now()-6e4],r=>Q(r,"owner_id")));e.length!==0&&(await P(async n=>{await n.transaction(async()=>{await n.run(`
|
|
1540
1741
|
DELETE FROM entry_sqlite_leases
|
|
1541
1742
|
WHERE archive_key = ?
|
|
1542
|
-
AND owner_id IN (${
|
|
1543
|
-
`,[t,...e]),await
|
|
1743
|
+
AND owner_id IN (${In(e.length)})
|
|
1744
|
+
`,[t,...e]),await n.run(`
|
|
1544
1745
|
DELETE FROM archive_owners
|
|
1545
1746
|
WHERE archive_key = ?
|
|
1546
|
-
AND owner_id IN (${
|
|
1547
|
-
`,[t,...e])})}),await
|
|
1747
|
+
AND owner_id IN (${In(e.length)})
|
|
1748
|
+
`,[t,...e])})}),await Js(t))}var nw=c(()=>{"use strict";We();zs();bt()});var u6,ma=c(()=>{"use strict";u6=new RegExp("\\p{Script=Han}","u")});var pm=c(()=>{"use strict"});var hm,Rn=c(()=>{"use strict";hm=`
|
|
1548
1749
|
CREATE TABLE IF NOT EXISTS search_sessions (
|
|
1549
1750
|
session_id TEXT PRIMARY KEY,
|
|
1550
1751
|
archive_key TEXT NOT NULL,
|
|
@@ -1648,43 +1849,43 @@ ON search_sessions(expires_at);
|
|
|
1648
1849
|
|
|
1649
1850
|
CREATE INDEX IF NOT EXISTS idx_search_sessions_prune
|
|
1650
1851
|
ON search_sessions(accessed_at DESC, created_at DESC, session_id);
|
|
1651
|
-
`});import{join as
|
|
1852
|
+
`});import{join as mM}from"path";import{rm as rw}from"fs/promises";async function vn(){let t=gm(),e=await D(t,hm);return fm===t?e:await hM(e)?(fm=t,e):(await e.close(),await rw(t,{force:!0}),await rw(`${t}.initialized`,{force:!0}),fm=t,await D(t,hm))}function gm(){return mM(pM(),"search-sessions.sqlite")}function pM(){return nn()}async function hM(t){return await Qs(t,"search_entity_hits","archive_id")&&await Qs(t,"search_chunk_hits","archive_id")&&await Qs(t,"search_triple_hits","archive_id")&&await Qs(t,"search_evidence_hit_events","archive_id")}async function Qs(t,e,n){return(await t.queryAll(`PRAGMA table_info(${e})`,void 0,i=>String(i.name))).includes(n)}var fm,Wr=c(()=>{"use strict";me();N();Rn()});var Xs=c(()=>{"use strict"});async function ym(t,e){await iw(t,e),await t.run("DELETE FROM search_sessions WHERE session_id = ?",[e])}async function iw(t,e){await t.run("DELETE FROM search_results WHERE session_id = ?",[e]),await t.run("DELETE FROM search_evidence_hit_events WHERE session_id = ?",[e]),await t.run("DELETE FROM search_entity_hits WHERE session_id = ?",[e]),await t.run("DELETE FROM search_triple_hits WHERE session_id = ?",[e]),await t.run("DELETE FROM search_chunk_hits WHERE session_id = ?",[e])}async function bm(t){await t.run(`
|
|
1652
1853
|
DELETE FROM predicate_dictionary
|
|
1653
1854
|
WHERE NOT EXISTS (
|
|
1654
1855
|
SELECT 1
|
|
1655
1856
|
FROM search_triple_hits
|
|
1656
1857
|
WHERE search_triple_hits.predicate_id = predicate_dictionary.id
|
|
1657
1858
|
)
|
|
1658
|
-
`)}var
|
|
1859
|
+
`)}var Sm=c(()=>{"use strict";k();Wr();Xs();Rn()});import{stat as yM}from"fs/promises";async function Fr(t){let e=await vn();try{let n=await e.queryAll(`
|
|
1659
1860
|
SELECT session_id
|
|
1660
1861
|
FROM search_sessions
|
|
1661
1862
|
WHERE archive_key = ?
|
|
1662
|
-
`,[t],
|
|
1863
|
+
`,[t],r=>w(r,"session_id"));await e.transaction(async()=>{for(let r of n)await ym(e,r);await bm(e)})}finally{await e.close()}}async function Vs(t){let e=gm(),n=await aw(e),r=await vn();try{let i=await r.queryOne("SELECT COUNT(*) AS count FROM search_sessions",void 0,u=>g(u,"count")),a=t.force?await SM(r):await bM(r,t.now),o=t.force?[]:await wM(r),s=[...new Set([...a,...o])].sort();!t.dryRun&&s.length>0&&(await r.transaction(async()=>{for(let u of s)await ym(r,u);await bm(r)}),await r.run("VACUUM"));let d=await aw(e);return{freedBytes:Math.max(0,n-d),removed:s.length,scanned:i??0}}finally{await r.close()}}async function bM(t,e){return await t.queryAll(`
|
|
1663
1864
|
SELECT session_id
|
|
1664
1865
|
FROM search_sessions
|
|
1665
1866
|
WHERE expires_at < ?
|
|
1666
|
-
`,[e],
|
|
1867
|
+
`,[e],n=>w(n,"session_id"))}async function SM(t){return await t.queryAll("SELECT session_id FROM search_sessions",void 0,e=>w(e,"session_id"))}async function wM(t){return await t.queryAll(`
|
|
1667
1868
|
SELECT session_id
|
|
1668
1869
|
FROM search_sessions
|
|
1669
1870
|
ORDER BY accessed_at DESC, created_at DESC, session_id
|
|
1670
1871
|
LIMIT -1 OFFSET ?
|
|
1671
|
-
`,[500],e=>
|
|
1872
|
+
`,[500],e=>w(e,"session_id"))}async function aw(t){try{return(await yM(t)).size}catch(e){if(E(e)&&e.code==="ENOENT")return 0;throw e}}var ow=c(()=>{"use strict";k();T();Wr();Rn();Sm()});var sw=c(()=>{"use strict";Rn()});var wm=c(()=>{"use strict"});var Ys=c(()=>{"use strict";k();Wr();Xs();sw();wm()});import{createHash as X6}from"crypto";var cw=c(()=>{"use strict";Rn()});var uw=c(()=>{"use strict";k();pm();Wr();Ys();cw();Xs();Rn();Sm()});var lw=c(()=>{"use strict";k();Wr();Ys()});var Tn=c(()=>{"use strict";pm();ow();Ys();uw();lw();wm()});var Zs=c(()=>{"use strict"});var Sw=c(()=>{"use strict"});var Im=c(()=>{"use strict"});var ed=c(()=>{"use strict"});var xm=c(()=>{"use strict";ed()});var km=c(()=>{"use strict";ed()});var nd=c(()=>{"use strict";Zs();Im();xm();km()});var Rw=c(()=>{"use strict";Lt()});var vw=c(()=>{"use strict";Zs();Sw();Im();ed();nd();xm();km();Rw()});var B=c(()=>{"use strict";vw()});var vm=c(()=>{"use strict"});async function re(t,e){let n=await ol(t),r=await sl(t,n.items,e);if(r===void 0)throw new Error(`Chapter ${e} does not exist. Use \`wg <archive-uri>/chapter list\` to discover chapter ids.`);let i=await t.serials.getById(e),a=await t.readSummary(e);return{...r,graphReady:i?.topologyReady===!0,hasSummary:a!==void 0}}async function Y(t){let e=await ol(t);return await bs(t,e.items)}var Tm=c(()=>{"use strict";Ui();al();vm()});async function jr(t,e,n){let r=await re(t,e);if(r.stage!==n)throw new Error(`Chapter ${e} is ${r.stage}. Expected ${n} before committing build output.`)}var Tw=c(()=>{"use strict";Tm()});var Se=c(()=>{"use strict";Tm();Tw();vm()});var _n=c(()=>{"use strict";Se();jt();Wt()});var ie=c(()=>{"use strict";Lt();B()});var je=c(()=>{"use strict";Lt();B()});var Qe=c(()=>{"use strict";on();B();ie()});var Ge=c(()=>{"use strict";Se();jt();B();ie();je();Qe();B();_n()});var ba=c(()=>{"use strict";Tn();jt();Ge();je()});var Am=c(()=>{"use strict";Se();ma();B();ie();Qe();je()});var Sa=c(()=>{"use strict"});var Ve=c(()=>{"use strict";Se();Sa();B();ie();Qe()});var wa=c(()=>{"use strict";Qe()});var Ea=c(()=>{"use strict";ie();Qe();Ve()});var Lm=c(()=>{"use strict";B();Ve();wa();Ea()});var Fw=c(()=>{"use strict";jt();_n();B();Ge();Ve();wa();Lm();Ea();Ea();wa()});var jw=c(()=>{"use strict";Fw();Lm();wa();Ea()});var ka=c(()=>{"use strict";jw()});var Bw=c(()=>{"use strict";Tn();B();ie();Qe();Ve();je();ka()});var va=c(()=>{"use strict";ie();Ve();B();je();ka();Bw()});var Dm=c(()=>{"use strict";Se();B();ie();Qe();Ve();va();Ta();je()});var Ta=c(()=>{"use strict";B();ie();Qe();je();Dm()});var cd=c(()=>{"use strict";B();Ge();je()});var ud=c(()=>{"use strict";B();Ge()});var ld=c(()=>{"use strict";Se();ma();Tn();jt();B();ba();Am();va();Ta();Ge();cd();ud()});var Kw=c(()=>{"use strict";ma();Tn();B();_n();ba();Am();va();Ta();ld();Ge()});var Jw=c(()=>{"use strict";Kw();cd();ud();ld();ba();Ge()});var md=c(()=>{"use strict";Ve();ka();ie()});var _a=c(()=>{"use strict";B()});var pd=c(()=>{"use strict";B();_n();ie();Ge();ba();jt();_a()});var Wm=c(()=>{"use strict";B();je();md();pd();_a()});import{stat as pW}from"fs/promises";async function Fm(t){let e=tr(),n=await zw(e);if(n===void 0)return{freedBytes:0,removed:0,scanned:0};let r=await D(e,Vw);try{await Yw(r);let i=await bW(r),a=new Date(t.now-hW).toISOString(),o=await SW(r,a);!t.dryRun&&o>0&&(await r.transaction(async()=>{await r.run("DELETE FROM qid_cache WHERE checked_at < ?",[a]),await r.run("DELETE FROM disambiguation_cache WHERE checked_at < ?",[a])}),await r.run("VACUUM"));let s=await zw(e);return{freedBytes:t.dryRun?fW(n,i,o):Math.max(0,n-(s??0)),removed:o,scanned:i}}finally{await r.close()}}function fW(t,e,n){return e===0||n===0?0:Math.round(t*(n/e))}async function Yw(t){await gW(t),await yW(t)}async function gW(t){(await Ca(t,"qid_cache")).has("language")||await t.transaction(async()=>{(await Ca(t,"qid_cache")).has("language")||(await t.run("ALTER TABLE qid_cache RENAME TO qid_cache_legacy"),await t.run(Qw),await t.run(`
|
|
1672
1873
|
INSERT OR IGNORE INTO qid_cache (
|
|
1673
1874
|
qid, language, label, description, pages_json, checked_at, updated_at
|
|
1674
1875
|
)
|
|
1675
1876
|
SELECT qid, 'en', label, description, pages_json, checked_at, updated_at
|
|
1676
1877
|
FROM qid_cache_legacy
|
|
1677
|
-
`),await t.run("DROP TABLE qid_cache_legacy"))})}async function
|
|
1878
|
+
`),await t.run("DROP TABLE qid_cache_legacy"))})}async function yW(t){(await Ca(t,"disambiguation_cache")).has("wiki")||await t.transaction(async()=>{(await Ca(t,"disambiguation_cache")).has("wiki")||(await t.run("ALTER TABLE disambiguation_cache RENAME TO disambiguation_cache_legacy"),await t.run(Xw),await t.run(`
|
|
1678
1879
|
INSERT OR IGNORE INTO disambiguation_cache (
|
|
1679
1880
|
qid, wiki, pages_json, profile_json, checked_at
|
|
1680
1881
|
)
|
|
1681
1882
|
SELECT qid, 'enwiki', pages_json, profile_json, checked_at
|
|
1682
1883
|
FROM disambiguation_cache_legacy
|
|
1683
|
-
`),await t.run("DROP TABLE disambiguation_cache_legacy"))}),(await
|
|
1884
|
+
`),await t.run("DROP TABLE disambiguation_cache_legacy"))}),(await Ca(t,"disambiguation_cache")).has("profile_error_json")||await t.run("ALTER TABLE disambiguation_cache ADD COLUMN profile_error_json TEXT")}async function Ca(t,e){let n=await t.queryAll(`PRAGMA table_info(${e})`,void 0,r=>Ht(r.name,"name"));return new Set(n)}async function bW(t){let e=await hd(t,"qid_cache"),n=await hd(t,"disambiguation_cache");return e+n}async function SW(t,e){let n=await hd(t,"qid_cache",e),r=await hd(t,"disambiguation_cache",e);return n+r}async function hd(t,e,n){return await t.queryOne(`
|
|
1684
1885
|
SELECT COUNT(*) AS count
|
|
1685
1886
|
FROM ${e}
|
|
1686
|
-
${
|
|
1687
|
-
`,
|
|
1887
|
+
${n===void 0?"":"WHERE checked_at < ?"}
|
|
1888
|
+
`,n===void 0?void 0:[n],r=>g(r,"count"))??0}async function zw(t){try{return(await pW(t)).size}catch(e){if(E(e)&&e.code==="ENOENT")return;throw e}}function wW(t){let e=Pa(t.description),n=Pa(t.label);return{checkedAt:Ht(t.checked_at,"checked_at"),...e===void 0?{}:{description:e},...n===void 0?{}:{label:n},qid:Ht(t.qid,"qid"),sitelinks:xW(Ht(t.pages_json,"pages_json")),updatedAt:Ht(t.updated_at,"updated_at")}}function EW(t){let e=Pa(t.profile_error_json),n=e===void 0?void 0:_W(e);return{checkedAt:Ht(t.checked_at,"checked_at"),disambiguationQid:Ht(t.qid,"qid"),pages:kW(Ht(t.pages_json,"pages_json")),...n===void 0?{}:{profileError:n},...Pa(t.profile_json)===void 0?{}:{profile:TW(Pa(t.profile_json))}}}function IW(t){if(t===void 0)return!1;let e=Date.parse(t.retryAfter);return Number.isFinite(e)&&e<=Date.now()}function xW(t){let e=JSON.parse(t);return Array.isArray(e)?e.filter(RW):[]}function kW(t){let e=JSON.parse(t);return Array.isArray(e)?e.filter(vW):[]}function RW(t){if(typeof t!="object"||t===null)return!1;let e=t;return typeof e.isDisambiguation=="boolean"&&typeof e.title=="string"&&e.title!==""&&Zw(e.wiki)}function vW(t){if(typeof t!="object"||t===null)return!1;let e=t;return Array.isArray(e.linkedQids)&&e.linkedQids.every(LW)&&typeof e.text=="string"&&e.text!==""&&typeof e.title=="string"&&e.title!==""&&Zw(e.wiki)}function TW(t){let e=JSON.parse(t);if(!CW(e))throw new Error("Expected profile_json to contain a disambiguation profile.");return e}function _W(t){let e=JSON.parse(t);if(AW(e))return e}function CW(t){if(typeof t!="object"||t===null)return!1;let e=t;return Array.isArray(e.meanings)&&e.meanings.every(PW)&&typeof e.sourceQid=="string"&&/^Q[1-9]\d*$/u.test(e.sourceQid)&&(!("surface"in e)||typeof e.surface=="string")}function PW(t){if(typeof t!="object"||t===null)return!1;let e=t;return(!("category"in e)||typeof e.category=="string")&&typeof e.information=="string"&&typeof e.name=="string"&&e.name!==""&&(e.priority==="primary"||e.priority==="secondary"||e.priority==="other")&&typeof e.qid=="string"&&/^Q[1-9]\d*$/u.test(e.qid)}function AW(t){if(typeof t!="object"||t===null)return!1;let e=t;return typeof e.failedAt=="string"&&typeof e.message=="string"&&typeof e.retryAfter=="string"}function LW(t){if(typeof t!="object"||t===null)return!1;let e=t;return typeof e.qid=="string"&&/^Q[1-9]\d*$/u.test(e.qid)&&typeof e.title=="string"&&e.title!==""}function Zw(t){return t==="enwiki"||t==="zhwiki"}function Ht(t,e){if(typeof t!="string")throw new Error(`Expected ${e} to be a string.`);return t}function Pa(t){return typeof t=="string"&&t!==""?t:void 0}var Qw,Xw,Vw,hW,Br,jm=c(()=>{"use strict";me();k();N();T();Qw=`
|
|
1688
1889
|
CREATE TABLE IF NOT EXISTS qid_cache (
|
|
1689
1890
|
qid TEXT NOT NULL,
|
|
1690
1891
|
language TEXT NOT NULL,
|
|
@@ -1695,7 +1896,7 @@ CREATE TABLE IF NOT EXISTS qid_cache (
|
|
|
1695
1896
|
updated_at TEXT NOT NULL,
|
|
1696
1897
|
PRIMARY KEY (qid, language)
|
|
1697
1898
|
);
|
|
1698
|
-
|
|
1899
|
+
`,Xw=`
|
|
1699
1900
|
CREATE TABLE IF NOT EXISTS disambiguation_cache (
|
|
1700
1901
|
qid TEXT NOT NULL,
|
|
1701
1902
|
wiki TEXT NOT NULL,
|
|
@@ -1705,21 +1906,21 @@ CREATE TABLE IF NOT EXISTS disambiguation_cache (
|
|
|
1705
1906
|
checked_at TEXT NOT NULL,
|
|
1706
1907
|
PRIMARY KEY (qid, wiki)
|
|
1707
1908
|
);
|
|
1708
|
-
`,
|
|
1709
|
-
${
|
|
1909
|
+
`,Vw=`
|
|
1910
|
+
${Qw}
|
|
1710
1911
|
|
|
1711
1912
|
CREATE INDEX IF NOT EXISTS idx_qid_cache_checked_at
|
|
1712
1913
|
ON qid_cache(checked_at);
|
|
1713
1914
|
|
|
1714
|
-
${
|
|
1915
|
+
${Xw}
|
|
1715
1916
|
|
|
1716
1917
|
CREATE INDEX IF NOT EXISTS idx_disambiguation_cache_checked_at
|
|
1717
1918
|
ON disambiguation_cache(checked_at);
|
|
1718
|
-
`,
|
|
1919
|
+
`,hW=720*60*60*1e3,Br=class t{#e;constructor(e){this.#e=e}static async open(e){let n=e??tr(),r=await D(n,Vw);try{await Yw(r)}catch(i){throw await r.close().catch(()=>{}),i}return new t(r)}async close(){await this.#e.close()}async getQids(e,n){if(e.length===0)return new Map;let r=new Map;for(let i of e){let a=await this.#e.queryOne(`
|
|
1719
1920
|
SELECT *
|
|
1720
1921
|
FROM qid_cache
|
|
1721
1922
|
WHERE qid = ? AND language = ?
|
|
1722
|
-
`,[i,
|
|
1923
|
+
`,[i,n],wW);a!==void 0&&r.set(i,a)}return r}async putQids(e,n){e.length!==0&&await this.#e.transaction(async()=>{for(let r of e)await this.#e.run(`
|
|
1723
1924
|
INSERT INTO qid_cache (
|
|
1724
1925
|
qid, language, label, description, pages_json, checked_at, updated_at
|
|
1725
1926
|
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
@@ -1729,11 +1930,11 @@ ON CONFLICT(qid, language) DO UPDATE SET
|
|
|
1729
1930
|
pages_json = excluded.pages_json,
|
|
1730
1931
|
checked_at = excluded.checked_at,
|
|
1731
1932
|
updated_at = excluded.updated_at
|
|
1732
|
-
`,[
|
|
1933
|
+
`,[r.qid,n,r.label??null,r.description??null,JSON.stringify(r.sitelinks),r.checkedAt,r.updatedAt])})}async getDisambiguations(e,n){if(e.length===0)return new Map;let r=new Map;for(let i of e){let a=await this.#e.queryOne(`
|
|
1733
1934
|
SELECT *
|
|
1734
1935
|
FROM disambiguation_cache
|
|
1735
1936
|
WHERE qid = ? AND wiki = ?
|
|
1736
|
-
`,[i,
|
|
1937
|
+
`,[i,n],EW);if(a!==void 0){if(IW(a.profileError))continue;r.set(i,a)}}return r}async putDisambiguations(e,n){e.length!==0&&await this.#e.transaction(async()=>{for(let r of e)await this.#e.run(`
|
|
1737
1938
|
INSERT INTO disambiguation_cache (
|
|
1738
1939
|
qid, wiki, pages_json, profile_json, profile_error_json, checked_at
|
|
1739
1940
|
) VALUES (?, ?, ?, ?, ?, ?)
|
|
@@ -1742,41 +1943,41 @@ ON CONFLICT(qid, wiki) DO UPDATE SET
|
|
|
1742
1943
|
profile_json = excluded.profile_json,
|
|
1743
1944
|
profile_error_json = excluded.profile_error_json,
|
|
1744
1945
|
checked_at = excluded.checked_at
|
|
1745
|
-
`,[
|
|
1746
|
-
`)}function
|
|
1747
|
-
`)}function
|
|
1748
|
-
`)}function
|
|
1749
|
-
`)}function
|
|
1750
|
-
`)}function
|
|
1751
|
-
`)},{role:"user",content:[`Source QID: ${t.sourceQid}`,t.surface===void 0?"":`Surface: ${t.surface}`,`Maximum meanings: ${e}`,"","Page QID links:",JSON.stringify(t.pageQidLinks.map(
|
|
1752
|
-
${
|
|
1753
|
-
|
|
1754
|
-
`),"","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:t.sourceQid,...t.surface===void 0?{}:{surface:t.surface}},null,2)].filter(
|
|
1755
|
-
`)}]}function
|
|
1756
|
-
`).map(e=>e.trim()).filter(e=>e.startsWith("* ")).map(e=>({links:
|
|
1757
|
-
|
|
1758
|
-
[truncated]`}var
|
|
1759
|
-
[truncated ${t.length
|
|
1760
|
-
`,"utf8")}warnFailed(){this.#t||(this.#t=!0,
|
|
1761
|
-
[Wikipage] Failed with fetch log: ${this.#e}`))}}});function
|
|
1946
|
+
`,[r.disambiguationQid,n,JSON.stringify(r.pages),r.profile===void 0?null:JSON.stringify(r.profile),r.profileError===void 0?null:JSON.stringify(r.profileError),r.checkedAt])})}}});var M,Ie,Aa,Ur,La,wt,Gm=c(()=>{"use strict";M=class extends Error{issues;constructor(e){super(`Parse failed with ${e.length} issue(s)`),this.name="ParsedJsonError",this.issues=[...e]}},Ie=class extends Error{attempts;maxRetries;constructor(e,n,r){super(e),this.name="GuaranteedRequestFailureError",this.attempts=n,this.maxRetries=r}},Aa=class extends Ie{constructor(e,n){super("LLM returned empty response after all retries",e,n),this.name="GuaranteedEmptyResponseError"}},Ur=class extends Ie{response;reason;constructor(e,n,r){super(`Suspected model refusal after ${e} JSON syntax error attempt(s): ${r.reason}. Last response: ${JSON.stringify(r.response)}`,e,n),this.name="SuspectedModelRefusalError",this.response=r.response,this.reason=r.reason}},La=class extends Ie{issues;response;constructor(e,n,r,i){super("Schema validation failed after all retries",e,n),this.name="GuaranteedSchemaValidationError",this.issues=[...r.issues],this.response=r.response,this.cause=i}},wt=class extends Ie{issues;response;constructor(e,n,r,i){super("Parse validation failed after all retries",e,n),this.name="GuaranteedParseValidationError",this.issues=[...r.issues],this.response=r.response,this.cause=i}}});function eE(t,e){return[{role:"system",content:t},{role:"user",content:e}]}function tE(t){let e=t.trim();return e===""?"ambiguous":NW(e)?"malformed_json":WW(e)?"natural_language":Bm(e)?"ambiguous":rE(e)?"natural_language":"ambiguous"}function nE(t){let e=t.trim().toLowerCase();return e.includes("malformed_json")?"malformed_json":e.includes("natural_language")?"natural_language":(e.includes("ambiguous"),"ambiguous")}function NW(t){return/^(?:```json\b|```|{|\[)/i.test(t)||/"[^"\r\n]+"\s*:/.test(t)||Bm(t)&&MW(t)}function Bm(t){let e=(t.match(/[{}[\]:,]/g)??[]).length+(t.match(/"/g)??[]).length;return/:\s/.test(t)||e>=3||DW(t)||/[:,]\s*$/.test(t)}function DW(t){return fd(t,"{")!==fd(t,"}")||fd(t,"[")!==fd(t,"]")}function MW(t){return/[:, "{[]\s*$/.test(t)}function WW(t){return OW.some(e=>e.test(t))?!0:!Bm(t)&&rE(t)}function rE(t){return/[\p{Script=Han}A-Za-z]/u.test(t)&&!/^[{[]/.test(t)&&!/"[^"\r\n]+"\s*:/.test(t)}function fd(t,e){return t.split(e).length-1}var ae,OW,Um=c(()=>{"use strict";ae="guaranteed/response_intent_classifier",OW=[/\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 FW}from"jsonrepair";function iE(t){let e=t.replace(/```(?:json)?\s*\n(.*?)\n```/gs,"$1"),n=e.match(/\[[\s\S]*\]/),r=e.match(/\{[\s\S]*\}/);return n?.index!==void 0&&n[0]!==void 0&&(r?.index===void 0||n.index<r.index)?n[0]:r?.[0]!==void 0?r[0]:e.trim()}function aE(t){try{return FW(t)}catch{return t}}function oE(t){return["Your JSON has a syntax error.",`Error: ${t.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(`
|
|
1947
|
+
`)}function sE(t){return["Your previous reply looked like malformed JSON.",`Error: ${t.message}`,"","Return complete and valid JSON directly.","Do not answer conversationally.","Do not add explanations or markdown fences."].join(`
|
|
1948
|
+
`)}function Hm(){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(`
|
|
1949
|
+
`)}function dE(t){return["Your JSON has structural issues.","",...t.issues.map(lE),"","Regenerate the complete and valid JSON directly. Do not add explanations."].join(`
|
|
1950
|
+
`)}function cE(t){return t.issues.map(lE)}function uE(t){return["Your JSON has the following issues.","",...t.map(n=>`- ${n}`),"","Regenerate the complete and valid JSON directly. Do not add explanations."].join(`
|
|
1951
|
+
`)}function lE(t){return`- **${t.path.length===0?"<root>":t.path.join(".")}**: ${t.message}`}var mE=c(()=>{"use strict"});async function Z(t){let e=[...t.messages],n=[...t.messages],r=0,i=0,a=t.maxRetries??fE;for(let o=0;o<=a;o+=1){let s=await t.request(n,o,a);if(s===void 0||s.trim()===""){if(o>=a)throw new Aa(o+1,a);continue}let d;try{let l=iE(s),m=aE(l);d=JSON.parse(m)}catch(l){let m=await hE(t,s,o);if(m==="natural_language"){if(r+=1,i=0,r>=2||o>=a){let p=o>=a?"last retry still returned natural-language content":"two consecutive retries returned natural-language content";throw new Ur(o+1,a,{response:s,reason:p})}n=Oa(e,s,Hm(),!1);continue}r=0,i+=1,n=Oa(e,s,m==="malformed_json"?sE(pE(l)):oE(pE(l)),i<jW);continue}r=0,i=0;let u=await t.schema.safeParseAsync(d);if(!u.success){if(GW(u.error,d)&&await hE(t,s,o)==="natural_language"){if(r+=1,i=0,r>=2||o>=a){let p=o>=a?"last retry still returned natural-language content":"two consecutive retries returned natural-language content";throw new Ur(o+1,a,{response:s,reason:p})}n=Oa(e,s,Hm(),!1);continue}r=0;let l=dE(u.error);if(o>=a)throw new La(o+1,a,{issues:cE(u.error),response:s},u.error);n=Oa(e,s,l,!0);continue}try{return await t.parse(u.data,o,a)}catch(l){if(!(l instanceof M))throw l;let m=uE(l.issues);if(o>=a)throw new wt(o+1,a,{issues:l.issues,response:s},l);n=Oa(e,s,m,!0)}}throw new Error("requestGuaranteedJson failed unexpectedly")}function Oa(t,e,n,r){return[...t,...r?[{role:"assistant",content:e}]:[],{role:"user",content:n}]}function pE(t){return t instanceof SyntaxError?t:new SyntaxError(String(t))}async function hE(t,e,n){let r=tE(e);if(r!=="ambiguous")return r;try{let i=await t.request(eE(t.responseIntentClassifierPrompt,e),n,t.maxRetries??fE);if(i===void 0||i.trim()==="")return"natural_language";let a=nE(i);return a==="ambiguous"?"natural_language":a}catch{return"natural_language"}}function GW(t,e){return BW(e)?t.issues.some(n=>n.code==="invalid_type"&&n.path.length===0&&(n.expected==="object"||n.expected==="array")):!1}function BW(t){return t===null||typeof t!="object"&&typeof t!="function"}var fE,jW,gE=c(()=>{"use strict";Um();Gm();mE();fE=12,jW=3});var ve=c(()=>{"use strict";Gm();Um();gE()});import{z as Te}from"zod";function gd(t){return async e=>await Z({messages:KW(e,t.maxMeanings??24),parse:n=>$W(e,qW(n)),request:t.request,responseIntentClassifierPrompt:ae,schema:HW,...t.maxRetries===void 0?{}:{maxRetries:t.maxRetries}})}function qW(t){return{meanings:t.meanings.map(e=>({...e.category===void 0?{}:{category:e.category},information:e.information,name:e.name,priority:e.priority,qid:e.qid})),sourceQid:t.sourceQid,...t.surface===void 0?{}:{surface:t.surface}}}function $W(t,e){let n=new Set(t.pageQidLinks.map(o=>o.qid)),r=[],i=new Set,a=[];e.sourceQid!==t.sourceQid&&r.push(`sourceQid must be ${t.sourceQid}, but got ${e.sourceQid}.`);for(let o of e.meanings)if(!(o.qid===null||o.qid==="")){if(!n.has(o.qid)){r.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)){r.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&&r.push("Return at least one meaning from the pageQidLinks list."),r.length>0)throw new M(r);return{meanings:a,sourceQid:t.sourceQid,...e.surface===void 0?{}:{surface:e.surface}}}function KW(t,e){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(`
|
|
1952
|
+
`)},{role:"user",content:[`Source QID: ${t.sourceQid}`,t.surface===void 0?"":`Surface: ${t.surface}`,`Maximum meanings: ${e}`,"","Page QID links:",JSON.stringify(t.pageQidLinks.map(JW),null,2),"","Structured disambiguation pages:",JSON.stringify(t.pages.map(zW),null,2),"","Disambiguation page text:",t.pages.map(n=>`--- ${n.wiki}:${n.title} ---
|
|
1953
|
+
${YW(n.text)}`).join(`
|
|
1954
|
+
|
|
1955
|
+
`),"","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:t.sourceQid,...t.surface===void 0?{}:{surface:t.surface}},null,2)].filter(n=>n!=="").join(`
|
|
1956
|
+
`)}]}function JW(t){return{qid:t.qid,title:t.title}}function zW(t){return{...t.pageId===void 0?{}:{pageid:t.pageId},items:QW(t.text),title:t.title,wiki:t.wiki}}function QW(t){return t.split(`
|
|
1957
|
+
`).map(e=>e.trim()).filter(e=>e.startsWith("* ")).map(e=>({links:XW(e),text:VW(e.slice(2).trim())}))}function XW(t){return[...t.matchAll(yE)].map(e=>({label:e[1],qid:e[2]}))}function VW(t){return t.replace(yE,"$1")}function YW(t){return t.length<=16e3?t:`${t.slice(0,16e3)}
|
|
1958
|
+
|
|
1959
|
+
[truncated]`}var UW,HW,yE,bE=c(()=>{"use strict";ve();UW=Te.object({category:Te.string().min(1).optional(),information:Te.string(),name:Te.string().min(1),priority:Te.enum(["primary","secondary","other"]),qid:Te.union([Te.string(),Te.null()])}).strict(),HW=Te.object({meanings:Te.array(UW),sourceQid:Te.string().regex(/^Q[1-9]\d*$/u),surface:Te.string().min(1).optional()}).strict();yE=/\[\[([^\]|]+)\|wikg:\/\/qid=(Q[1-9]\d*)\]\]/gu});async function ZW(t){await new Promise(e=>{setTimeout(e,t)})}function yd(t){if(t===null||t.trim()==="")return;let e=Number(t);if(Number.isFinite(e))return Math.max(0,e*1e3);let n=Date.parse(t);if(!Number.isNaN(n))return Math.max(0,n-Date.now())}var Hr,qm=c(()=>{"use strict";Hr=class{#e;#t;#n=0;#r=0;#a=0;#i=[];constructor(e){this.#e=Math.max(1,Math.floor(e.concurrency)),this.#t=Math.max(0,Math.floor(e.minRequestIntervalMs))}async use(e){await this.#o();try{return await e()}finally{this.#n-=1,this.#d()}}blockFor(e){!Number.isFinite(e)||e<=0||(this.#r=Math.max(this.#r,Date.now()+e))}async#o(){await new Promise(e=>{this.#i.push(e),this.#d()}),await this.#s(),this.#n+=1,this.#a=Date.now()}async#s(){let e=Math.max(0,this.#r-Date.now()),n=Math.max(0,this.#a+this.#t-Date.now()),r=Math.max(e,n);r>0&&await ZW(r)}#d(){if(this.#n>=this.#e)return;let e=this.#i.shift();e!==void 0&&e()}}});import{createHash as eF}from"crypto";import{appendFile as tF}from"fs/promises";function SE(t){if(t===void 0)return new bd;let e=fu({category:"wikipage",fileName:"wikipage-fetch.jsonl",logDirPath:t});return e===void 0?new bd:new Jm(e)}function nF(t){return{action:t.url.searchParams.get("action")??null,attempt:t.attempt,batch:rF(t.url),durationMs:t.durationMs,endpoint:`${t.url.origin}${t.url.pathname}`,host:t.url.host,ok:t.response?.ok??!1,startedAt:new Date(t.startedAt).toISOString(),...t.response===void 0?{}:{retryAfter:t.response.headers.get("retry-after"),status:t.response.status},...t.error===void 0?{}:{error:iF(t.error)},...t.responseText===void 0?{}:{responseText:dF(t.responseText)}}}function rF(t){let e=t.searchParams.get("ids");if(e!==null)return Km("qid",e.split("|"));let n=t.searchParams.get("titles");if(n!==null)return Km("title",n.split("|"));let r=t.searchParams.get("page");if(r!==null)return Km("page",[r])}function Km(t,e){let n=e.join("|");return{count:e.length,hash:eF("sha256").update(n).digest("hex").slice(0,16),kind:t,sample:e.slice(0,5)}}function iF(t){return{message:ue(t),name:t instanceof Error?t.name:typeof t,...aF(t)}}function aF(t){if(!(t instanceof Error)||t.cause===void 0)return{};let e=t.cause;return{cause:e instanceof Error?{message:ue(e),name:e.name,...sF(e)}:oF(e)}}function oF(t){if(typeof t=="string")return t;if(typeof t=="number"||typeof t=="boolean"||typeof t=="bigint"||typeof t=="symbol")return t.toString();try{return JSON.stringify(t)}catch{return"unserializable error cause"}}function sF(t){let e=t.code;return typeof e=="string"?{code:e}:{}}function dF(t){return t.length<=$m?t:`${t.slice(0,$m)}
|
|
1960
|
+
[truncated ${t.length-$m} chars]`}var $m,bd,Jm,wE=c(()=>{"use strict";Pe();T();$m=16384,bd=class{filePath=void 0;append(){return Promise.resolve()}warnFailed(){}},Jm=class{#e;#t=!1;constructor(e){this.#e=e}get filePath(){return this.#e}async append(e){await tF(this.#e,`${JSON.stringify(nF(e))}
|
|
1961
|
+
`,"utf8")}warnFailed(){this.#t||(this.#t=!0,Ce({component:"wikipage"}).warn(`
|
|
1962
|
+
[Wikipage] Failed with fetch log: ${this.#e}`))}}});function oe(t){return typeof t=="object"&&t!==null?t:{}}function EE(t){return Array.isArray(t)?t:[]}function Et(t){return typeof t=="string"&&t!==""?t:void 0}function Na(t){return typeof t=="number"&&Number.isFinite(t)?t:void 0}function Da(t,e){let n=t;for(let r of e)n=oe(n)[r];return Et(n)}var Sd=c(()=>{"use strict"});import{DomUtils as cF,parseDocument as uF}from"htmlparser2";function IE(t,e){return t.replace(/\[\[([^\]|]+)\|wikigraph-title:\/\/([^\]\s]+)\]\]/gu,(n,r,i)=>{let a=decodeURIComponent(i),o=e.get(a);return o===void 0?r:`[[${r}|wikg://qid=${o}]]`})}function xE(t){if(t===void 0||t.trim()==="")return{linkedTitles:[],text:""};let e=new Set,n=uF(t),r=Pn(cF.getChildren(n),{linkedTitles:e,listDepth:0}).replace(/[ \t]+\n/gu,`
|
|
1762
1963
|
`).replace(/\n{3,}/gu,`
|
|
1763
1964
|
|
|
1764
|
-
`).trim();return{linkedTitles:[...e],text:
|
|
1765
|
-
${s} ${
|
|
1766
|
-
`}if(a==="li"){let o=" ".repeat(Math.max(0,e.listDepth-1)),s=
|
|
1767
|
-
${o}* ${s}`}if(a==="ul"||a==="ol")return`${
|
|
1768
|
-
`;if(
|
|
1965
|
+
`).trim();return{linkedTitles:[...e],text:r}}function Pn(t,e){return t.map(n=>mF(n,e)).join("")}function mF(t,e){let n=oe(t),r=Et(n.type);if(r==="text")return yF(Et(n.data)??"");if(r!=="tag"&&r!=="script"&&r!=="style")return"";let i=t;if(fF(i))return"";let a=i.name.toLowerCase();if(a==="a")return pF(i,e);if(/^h[1-6]$/u.test(a)){let o=Math.min(6,Math.max(1,Number(a.slice(1)))),s="=".repeat(o),d=Pn(i.children??[],e).trim();return d===""?"":`
|
|
1966
|
+
${s} ${d} ${s}
|
|
1967
|
+
`}if(a==="li"){let o=" ".repeat(Math.max(0,e.listDepth-1)),s=Pn(i.children??[],{...e,listDepth:e.listDepth+1}).trim();return s===""?"":`
|
|
1968
|
+
${o}* ${s}`}if(a==="ul"||a==="ol")return`${Pn(i.children??[],e)}
|
|
1969
|
+
`;if(gF(a)){let o=Pn(i.children??[],e).trim();return o===""?"":`
|
|
1769
1970
|
${o}
|
|
1770
|
-
`}return hr(i.children??[],e)}function eM(t,e){let r=hr(t.children??[],e).trim(),n=tM(t);return n===void 0?r:(e.linkedTitles.add(n),`[[${r===""?n:r}|${YD}${encodeURIComponent(n)}]]`)}function tM(t){let e=t.attribs.title,r=t.attribs.href;if(e!==void 0&&e!==""&&!e.includes(":"))return e;if(r===void 0||!r.startsWith("/wiki/"))return;let n=decodeURIComponent(r.slice(6)).replaceAll("_"," ");return n===""||n.includes(":")?void 0:n}function rM(t){let e=t.name.toLowerCase();if(["script","style","table","sup"].includes(e))return!0;let r=t.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 nM(t){return["blockquote","br","dd","div","dl","dt","p","section"].includes(t)}function iM(t){return t.replace(/\s+/gu," ")}var YD,am=d(()=>{"use strict";Bs();YD="wikigraph-title://"});function Sw(t){let e=t.headers.get("content-type");return e===null?!1:/(?:json|text|xml|javascript|html)/iu.test(e)}function ww(t){return t instanceof In?t.status===429||t.status===500||t.status===502||t.status===503||t.status===504:t instanceof TypeError}function Ew(t,e,r){if(t instanceof In&&t.retryAfterMs!==void 0)return t.retryAfterMs;if(r<=0)return 0;let n=r*2**e,i=Math.floor(Math.random()*Math.min(r,250));return n+i}async function kw(t){t<=0||await new Promise(e=>{setTimeout(e,t)})}var In,Iw=d(()=>{"use strict";In=class extends Error{retryAfterMs;status;url;constructor(e,r,n){super(`Wikimedia request failed with ${r}: ${e.toString()}`),this.retryAfterMs=n,this.status=r,this.url=e}}});function xw(t){return[...new Set([t,"zh","en"])].join("|")}function ua(t,e){for(let r of[e,"zh","en"]){let n=da(t,[r,"value"]);if(n!==void 0)return n}}function sm(t){return t==="zhwiki"?"https://zh.wikipedia.org/":"https://en.wikipedia.org/"}function Rw(t){return t==="zhwiki"||t==="enwiki"?t:void 0}var om,cm=d(()=>{"use strict";Bs();om=["zhwiki","enwiki"]});var xn,vw=d(()=>{"use strict";_e();T();tm();am();Bs();Iw();cm();xn=class{#e;#t;#r;#n;#i;#a;#o;#s;constructor(e){this.#e=e.fetch??fetch,this.#t=e.language,this.#n=e.requestLog,this.#s=e.wiki,this.#o=e.userAgent,this.#i=Math.max(0,Math.floor(e.retryBaseDelayMs)),this.#a=Math.max(0,Math.floor(e.retryTimes)),this.#r=new kn({concurrency:e.concurrency,minRequestIntervalMs:e.minRequestIntervalMs})}async getEntities(e){if(e.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",e.join("|")),r.searchParams.set("props","labels|descriptions|sitelinks"),r.searchParams.set("languages",xw(this.#t)),r.searchParams.set("sitefilter",om.join("|")),r.searchParams.set("format","json"),r.searchParams.set("formatversion","2");let n=await this.#c(r),i=te(n.entities),a=new Map;for(let o of e){let s=te(i[o]),c=te(s.labels),u=te(s.descriptions);a.set(o,{...ua(c,this.#t)===void 0?{}:{label:ua(c,this.#t)},...ua(u,this.#t)===void 0?{}:{description:ua(u,this.#t)},qid:o,sitelinks:om.flatMap(l=>{let m=da(s,["sitelinks",l,"title"]);return m===void 0?[]:[{title:m,wiki:l}]})})}return a}async getPagesByTitles(e,r=Rw(this.#s)??"enwiki"){if(e.length===0)return new Map;let n=new URL(`${sm(r)}w/api.php`);n.searchParams.set("action","query"),n.searchParams.set("titles",e.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=yw(te(te(i.query).pages)),o=new Map;for(let s of a){let c=te(s),u=ht(c.title);u!==void 0&&o.set(u,{isDisambiguation:te(c.pageprops).disambiguation!==void 0,...ca(c.pageid)===void 0?{}:{pageId:ca(c.pageid)},title:u,wiki:r,...ht(te(c.pageprops).wikibase_item)===void 0?{}:{wikibaseItem:ht(te(c.pageprops).wikibase_item)}})}return o}async parseDisambiguationPage(e,r){let n=new URL(`${sm(r)}w/api.php`);n.searchParams.set("action","parse"),n.searchParams.set("page",e),n.searchParams.set("prop","text|properties"),n.searchParams.set("format","json"),n.searchParams.set("formatversion","2");let i=await this.#c(n),a=te(i.parse),o=bw(da(a,["text"]));return{linkedTitles:o.linkedTitles,...ca(a.pageid)===void 0?{}:{pageId:ca(a.pageid)},text:o.text,title:ht(a.title)??e,wiki:r}}async#c(e){let r;for(let n=0;n<=this.#a;n+=1)try{return await this.#r.use(async()=>await this.#l(e,n+1))}catch(i){if(r=i,!ww(i)||n>=this.#a)throw this.#n.warnFailed(),i;await kw(Ew(i,n,this.#i))}throw r instanceof Error?r:new Error(String(r))}async#l(e,r){let n=Date.now(),i;try{i=await this.#e(e,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:e}),o}let a=js(i.headers.get("retry-after"));if(a!==void 0&&this.#r.blockFor(a),!i.ok){let o=Sw(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:e}),new In(e,i.status,a)}return await this.#u({attempt:r,durationMs:Date.now()-n,response:i,startedAt:n,url:e}),te(await i.json())}async#u(e){try{await this.#n.append(e)}catch(r){Te({component:"wikipage"}).warn(`Failed to write wikipage fetch log entry: ${ae(r)}`)}}}});var dm=d(()=>{"use strict";vw();am();cm()});function pM(t,e,r,n){return{checkedAt:n,...e?.description===void 0?{}:{description:e.description},...e?.label===void 0?{}:{label:e.label},qid:t,sitelinks:e?.sitelinks.flatMap(i=>{let a=r.get(Cw(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 hM(t){return{checkedAt:t.checkedAt,disambiguationQid:t.disambiguationQid,linkedQids:Tw(t.pages),pages:t.pages,...t.profile===void 0?{}:{profile:t.profile}}}function Tw(t){let e=new Map;for(let r of t)for(let n of r.linkedQids)e.set(n.qid,n);return[...e.values()]}function fM(t){let e=0;for(let r of t.values())e+=r.size;return e}function yM(t){return t.sitelinks.some(e=>e.isDisambiguation)}function gM(t,e){let r=t.find(n=>n.wiki===e)??t[0];return r===void 0?void 0:_w(r)}function _w(t){return{title:t.title,wiki:t.wiki}}function bM(t){return"close"in t&&typeof t.close=="function"}function Cw(t,e){return`${t}:${e}`}function SM(t){return[...new Set(t.map(e=>e.trim().toUpperCase()).filter(e=>/^Q[1-9]\d*$/u.test(e)))]}function wM(t){let e=t?.trim().toLowerCase();return e==="en"||e==="zh"?e:oM}function EM(t){return t===void 0||!Number.isFinite(t)?sM:Math.max(1,Math.floor(t))}function um(t,e){let r=[];for(let n=0;n<t.length;n+=e)r.push(t.slice(n,n+e));return r}var aM,oM,sM,cM,dM,uM,lM,mM,fr,Pw=d(()=>{"use strict";Xl();fw();Ie();dm();aM=3,oM="zh",sM=50,cM=100,dM=1e3,uM=3,lM="WikiGraph/0.3 (https://github.com/oomol-lab/wiki-graph)",mM=360*60*1e3,fr=class t{#e;#t;#r;#n;#i;#a;#o;#s;constructor(e){this.#e=e.client,this.#r=e.language,this.#t=e.maxBatchSize,this.#n=e.normalizer,this.#i=e.ownsStore??!1,this.#a=e.progress,this.#o=e.store,this.#s=e.wiki}static async open(e={}){let r=wM(e.language),n=e.wiki??`${r}wiki`,i=await wn.open(e.cacheDatabasePath);return new t({client:new xn({concurrency:e.concurrency??aM,language:r,requestLog:hw(e.logDirPath),minRequestIntervalMs:e.minRequestIntervalMs??cM,retryBaseDelayMs:e.retryBaseDelayMs??dM,retryTimes:e.retryTimes??uM,userAgent:e.userAgent??lM,wiki:n,...e.fetch===void 0?{}:{fetch:e.fetch}}),language:r,maxBatchSize:EM(e.maxBatchSize),normalizer:e.normalizer,ownsStore:!0,progress:e.progress,store:i,wiki:n})}async close(){this.#i&&bM(this.#o)&&await this.#o.close()}async resolveQids(e){let r=SM(e),n=await this.#c(r),i=await this.#u([...n.values()].filter(yM));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??[],u=gM(o.sitelinks,this.#s);return{...o.description===void 0?{}:{description:o.description},...s===void 0?{}:{disambiguation:hM(s)},...c.length===0?{}:{disambiguationPages:c},isDisambiguation:c.length>0,...o.label===void 0?{}:{label:o.label},qid:a,...u===void 0?{}:{sitelink:u},...o.sitelinks.length===0?{}:{sitelinks:o.sitelinks.map(_w)}}})}async#c(e){let r=new Map(await this.#o.getQids(e,this.#r)),n=e.filter(o=>!r.has(o)),i=r.size,a=0;await this.#m("qid",i,e.length);for(let o of um(n,this.#t)){let s=await this.#e.getEntities(o);a+=o.length,await this.#m("entity",a,n.length);let c=await this.#l(s),u=new Date().toISOString(),l=o.map(m=>pM(m,s.get(m),c,u));await this.#o.putQids(l,this.#r);for(let m of l)r.set(m.qid,m);i+=o.length,await this.#m("qid",i,e.length)}return r}async#l(e){let r=new Map,n=new Map;for(let i of e.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 um([...a],this.#t)){for(let[s,c]of await this.#e.getPagesByTitles(o,i))r.set(Cw(i,s),c);await this.#m("page",r.size,fM(n))}return r}async#u(e){let r=e.map(o=>o.qid),n=new Map(await this.#o.getDisambiguations(r,this.#s)),i=e.filter(o=>!n.has(o.qid)),a=n.size;await this.#m("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.#m("disambiguation-page",a,r.length)}return n}async#d(e){let r=[];for(let a of e.sitelinks.filter(o=>o.isDisambiguation)){let o=await this.#e.parseDisambiguationPage(a.title,a.wiki),s=new Map;for(let l of um(o.linkedTitles,this.#t)){for(let[m,p]of await this.#e.getPagesByTitles(l,a.wiki))s.set(m,p);await this.#m("linked-page",s.size,o.linkedTitles.length)}let c=new Map,u=[];for(let l of o.linkedTitles){let m=s.get(l)?.wikibaseItem;m!==void 0&&(c.set(l,m),u.push({qid:m,title:l}))}r.push({linkedQids:u,...o.pageId===void 0?{}:{pageId:o.pageId},text:gw(o.text,c),title:o.title,wiki:o.wiki})}let n=Tw(r),i=await this.#p(e,r,n);return{checkedAt:new Date().toISOString(),disambiguationQid:e.qid,pages:r,...i}}async#p(e,r,n){if(this.#n===void 0||n.length===0)return{};try{return{profile:await this.#n({pageQidLinks:n,pages:r,sourceQid:e.qid,...e.label===void 0?{}:{surface:e.label}})}}catch(i){if(!(i instanceof we))throw i;let a=new Date,o=new Date(a.getTime()+mM);return{profileError:{failedAt:a.toISOString(),message:i.message,retryAfter:o.toISOString()}}}}async#m(e,r,n){await this.#a?.({detail:e,done:Math.min(Math.max(0,r),Math.max(0,n)),total:Math.max(0,n)})}}});var la=d(()=>{"use strict";Xl();pw();tm();Pw();dm()});var lm=d(()=>{"use strict";la()});var Aw=d(()=>{"use strict";zi();j();Je();Ns();zl();Ds();ra();lm();Z()});var Us=d(()=>{"use strict";Aw();zl();Ns();Ds();ra();lm()});var mm=d(()=>{"use strict";vt();be();zi();j();Je();Yi();ta();Ne();Us();Z();$e()});var Ow=d(()=>{"use strict";mm();Us();Z()});var km=d(()=>{"use strict";US();mm();Us();Hi();Z();Kl();ea();Ow()});var Nw=d(()=>{"use strict";km()});import{randomBytes as pee}from"crypto";import{join as fee}from"path";var vM,Dw=d(()=>{"use strict";se();x();L();vM=10080*60*1e3});var Mw=d(()=>{"use strict"});var Ww=d(()=>{"use strict";x();Dw();Mw()});var Fw=d(()=>{"use strict"});var jw=d(()=>{"use strict";Ww();Fw()});var Rn=d(()=>{"use strict";Nw();jw();mr()});var Im=d(()=>{"use strict";_e()});var Uw=d(()=>{"use strict";Im()});var Hw=d(()=>{"use strict";Uw();Im()});function qw(t,e){return t-e}function $w(t){let e=ha();for(let r of t)e[String(r)]=!0;return e}function xm(t,e){return Object.hasOwn(t,String(e))}function CM(t){return Object.keys(t).length}function ha(){return Object.create(null)}var Ks,Kw=d(()=>{"use strict";Ks=class{#e;constructor(e){this.#e=e}selectTopChunks(e){if(e.allChunks.length===0)return[];if(e.latestChunkIds.length===0)return[...e.allChunks].slice(0,e.capacity);let r=$w(e.latestChunkIds),n=e.allChunks.filter(o=>!xm(r,o.id));if(n.length===0)return[];let i=this.#t({getSuccessorChunkIds:e.getSuccessorChunkIds,latestChunkIds:e.latestChunkIds});if(CM(i)===0)return[...n].sort((o,s)=>s.generation-o.generation).slice(0,e.capacity);let a=this.#r({allChunkIds:e.allChunks.map(o=>o.id),forwardScores:i,getSuccessorChunkIds:e.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,e.capacity).map(o=>o.chunk)}#t(e){let r=e.latestChunkIds.length;if(r===0)return ha();let n=1/r,i=ha(),a=ha(),o=[...e.latestChunkIds],s=0;for(let c of e.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 u=i[String(c)];if(u===void 0)continue;let l=[...e.getSuccessorChunkIds(c)].sort(qw);if(l.length===0)continue;let m=u/l.length;for(let p of l){let h=String(p);i[h]=(i[h]??0)+m,!xm(a,p)&&(a[h]=!0,o.push(p))}}return i}#r(e){let r=ha();for(let s of e.allChunkIds)for(let c of e.getSuccessorChunkIds(s)){let u=String(c),l=r[u]??[];r[u]=[...l,s]}let n={...e.forwardScores},i=Object.keys(e.forwardScores).map(Number),a=$w(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 u=[...r[String(s)]??[]].sort(qw);if(u.length===0)continue;let l=c/u.length;for(let m of u){let p=String(m);n[p]=(n[p]??0)+l,!xm(a,m)&&(a[p]=!0,i.push(m))}}return n}}});var Js,Jw=d(()=>{"use strict";Js=class{#e;#t=[];#r=[];#n=[];#i=0;constructor(e){this.#e=e}get capacity(){return this.#e}get generation(){return this.#i}addChunks(e){this.#t.push(...e)}setRetainedChunks(e){this.#r.splice(0,this.#r.length,...e.previousFragmentChunks),this.#n.splice(0,this.#n.length,...e.extraChunks)}finalizeFragment(){let e=[...this.#t];return this.#t.splice(0,this.#t.length),this.#i+=1,e}getChunks(){return[...this.#t,...this.#r,...this.#n]}getAllChunksForSaving(){return[...this.#t]}getChunksForPrompt(e=!0){return e?this.getChunks():[...this.#r,...this.#n]}formatForPrompt(e=!0){let r=this.getChunksForPrompt(e);return r.length===0?"(empty)":r.map(n=>`${n.id}. [${n.label}] - ${n.content}`).join(`
|
|
1771
|
-
`)}clear(){this.#t.splice(0,this.#t.length),this.#
|
|
1772
|
-
prev: ${
|
|
1773
|
-
text: ${
|
|
1774
|
-
next: ${
|
|
1775
|
-
`);return{candidates:i,code:
|
|
1971
|
+
`}return Pn(i.children??[],e)}function pF(t,e){let n=Pn(t.children??[],e).trim(),r=hF(t);return r===void 0?n:(e.linkedTitles.add(r),`[[${n===""?r:n}|${lF}${encodeURIComponent(r)}]]`)}function hF(t){let e=t.attribs.title,n=t.attribs.href;if(e!==void 0&&e!==""&&!e.includes(":"))return e;if(n===void 0||!n.startsWith("/wiki/"))return;let r=decodeURIComponent(n.slice(6)).replaceAll("_"," ");return r===""||r.includes(":")?void 0:r}function fF(t){let e=t.name.toLowerCase();if(["script","style","table","sup"].includes(e))return!0;let n=t.attribs.class??"";return["catlinks","metadata","mw-editsection","mw-empty-elt","navbox","noprint","reference","reflist","shortdescription","toc"].some(r=>n.split(/\s+/u).includes(r))}function gF(t){return["blockquote","br","dd","div","dl","dt","p","section"].includes(t)}function yF(t){return t.replace(/\s+/gu," ")}var lF,zm=c(()=>{"use strict";Sd();lF="wikigraph-title://"});function kE(t){let e=t.headers.get("content-type");return e===null?!1:/(?:json|text|xml|javascript|html)/iu.test(e)}function RE(t){return t instanceof qr?t.status===429||t.status===500||t.status===502||t.status===503||t.status===504:t instanceof TypeError}function vE(t,e,n){if(t instanceof qr&&t.retryAfterMs!==void 0)return t.retryAfterMs;if(n<=0)return 0;let r=n*2**e,i=Math.floor(Math.random()*Math.min(n,250));return r+i}async function TE(t){t<=0||await new Promise(e=>{setTimeout(e,t)})}var qr,_E=c(()=>{"use strict";qr=class extends Error{retryAfterMs;status;url;constructor(e,n,r){super(`Wikimedia request failed with ${n}: ${e.toString()}`),this.retryAfterMs=r,this.status=n,this.url=e}}});function CE(t){return[...new Set([t,"zh","en"])].join("|")}function Ma(t,e){for(let n of[e,"zh","en"]){let r=Da(t,[n,"value"]);if(r!==void 0)return r}}function Xm(t){return t==="zhwiki"?"https://zh.wikipedia.org/":"https://en.wikipedia.org/"}function PE(t){return t==="zhwiki"||t==="enwiki"?t:void 0}var Qm,Vm=c(()=>{"use strict";Sd();Qm=["zhwiki","enwiki"]});var $r,AE=c(()=>{"use strict";Pe();T();qm();zm();Sd();_E();Vm();$r=class{#e;#t;#n;#r;#a;#i;#o;#s;constructor(e){this.#e=e.fetch??fetch,this.#t=e.language,this.#r=e.requestLog,this.#s=e.wiki,this.#o=e.userAgent,this.#a=Math.max(0,Math.floor(e.retryBaseDelayMs)),this.#i=Math.max(0,Math.floor(e.retryTimes)),this.#n=new Hr({concurrency:e.concurrency,minRequestIntervalMs:e.minRequestIntervalMs})}async getEntities(e){if(e.length===0)return new Map;let n=new URL("https://www.wikidata.org/w/api.php");n.searchParams.set("action","wbgetentities"),n.searchParams.set("ids",e.join("|")),n.searchParams.set("props","labels|descriptions|sitelinks"),n.searchParams.set("languages",CE(this.#t)),n.searchParams.set("sitefilter",Qm.join("|")),n.searchParams.set("format","json"),n.searchParams.set("formatversion","2");let r=await this.#d(n),i=oe(r.entities),a=new Map;for(let o of e){let s=oe(i[o]),d=oe(s.labels),u=oe(s.descriptions);a.set(o,{...Ma(d,this.#t)===void 0?{}:{label:Ma(d,this.#t)},...Ma(u,this.#t)===void 0?{}:{description:Ma(u,this.#t)},qid:o,sitelinks:Qm.flatMap(l=>{let m=Da(s,["sitelinks",l,"title"]);return m===void 0?[]:[{title:m,wiki:l}]})})}return a}async getPagesByTitles(e,n=PE(this.#s)??"enwiki"){if(e.length===0)return new Map;let r=new URL(`${Xm(n)}w/api.php`);r.searchParams.set("action","query"),r.searchParams.set("titles",e.join("|")),r.searchParams.set("prop","pageprops"),r.searchParams.set("ppprop","disambiguation|wikibase_item"),r.searchParams.set("format","json"),r.searchParams.set("formatversion","2");let i=await this.#d(r),a=EE(oe(oe(i.query).pages)),o=new Map;for(let s of a){let d=oe(s),u=Et(d.title);u!==void 0&&o.set(u,{isDisambiguation:oe(d.pageprops).disambiguation!==void 0,...Na(d.pageid)===void 0?{}:{pageId:Na(d.pageid)},title:u,wiki:n,...Et(oe(d.pageprops).wikibase_item)===void 0?{}:{wikibaseItem:Et(oe(d.pageprops).wikibase_item)}})}return o}async parseDisambiguationPage(e,n){let r=new URL(`${Xm(n)}w/api.php`);r.searchParams.set("action","parse"),r.searchParams.set("page",e),r.searchParams.set("prop","text|properties"),r.searchParams.set("format","json"),r.searchParams.set("formatversion","2");let i=await this.#d(r),a=oe(i.parse),o=xE(Da(a,["text"]));return{linkedTitles:o.linkedTitles,...Na(a.pageid)===void 0?{}:{pageId:Na(a.pageid)},text:o.text,title:Et(a.title)??e,wiki:n}}async#d(e){let n;for(let r=0;r<=this.#i;r+=1)try{return await this.#n.use(async()=>await this.#c(e,r+1))}catch(i){if(n=i,!RE(i)||r>=this.#i)throw this.#r.warnFailed(),i;await TE(vE(i,r,this.#a))}throw n instanceof Error?n:new Error(String(n))}async#c(e,n){let r=Date.now(),i;try{i=await this.#e(e,this.#o===void 0?void 0:{headers:{"User-Agent":this.#o}})}catch(o){throw await this.#l({attempt:n,durationMs:Date.now()-r,error:o,startedAt:r,url:e}),o}let a=yd(i.headers.get("retry-after"));if(a!==void 0&&this.#n.blockFor(a),!i.ok){let o=kE(i)?await i.text():void 0;throw await this.#l({attempt:n,durationMs:Date.now()-r,response:i,...o===void 0?{}:{responseText:o},startedAt:r,url:e}),new qr(e,i.status,a)}return await this.#l({attempt:n,durationMs:Date.now()-r,response:i,startedAt:r,url:e}),oe(await i.json())}async#l(e){try{await this.#r.append(e)}catch(n){Ce({component:"wikipage"}).warn(`Failed to write wikipage fetch log entry: ${ue(n)}`)}}}});var Ym=c(()=>{"use strict";AE();zm();Vm()});function vF(t,e,n,r){return{checkedAt:r,...e?.description===void 0?{}:{description:e.description},...e?.label===void 0?{}:{label:e.label},qid:t,sitelinks:e?.sitelinks.flatMap(i=>{let a=n.get(NE(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:r}}function TF(t){return{checkedAt:t.checkedAt,disambiguationQid:t.disambiguationQid,linkedQids:LE(t.pages),pages:t.pages,...t.profile===void 0?{}:{profile:t.profile}}}function LE(t){let e=new Map;for(let n of t)for(let r of n.linkedQids)e.set(r.qid,r);return[...e.values()]}function _F(t){let e=0;for(let n of t.values())e+=n.size;return e}function CF(t){return t.sitelinks.some(e=>e.isDisambiguation)}function PF(t,e){let n=t.find(r=>r.wiki===e)??t[0];return n===void 0?void 0:OE(n)}function OE(t){return{title:t.title,wiki:t.wiki}}function AF(t){return"close"in t&&typeof t.close=="function"}function NE(t,e){return`${t}:${e}`}function LF(t){return[...new Set(t.map(e=>e.trim().toUpperCase()).filter(e=>/^Q[1-9]\d*$/u.test(e)))]}function OF(t){let e=t?.trim().toLowerCase();return e==="en"||e==="zh"?e:SF}function NF(t){return t===void 0||!Number.isFinite(t)?wF:Math.max(1,Math.floor(t))}function Zm(t,e){let n=[];for(let r=0;r<t.length;r+=e)n.push(t.slice(r,r+e));return n}var bF,SF,wF,EF,IF,xF,kF,RF,An,DE=c(()=>{"use strict";jm();wE();ve();Ym();bF=3,SF="zh",wF=50,EF=100,IF=1e3,xF=3,kF="WikiGraph/0.3 (https://github.com/oomol-lab/wiki-graph)",RF=360*60*1e3,An=class t{#e;#t;#n;#r;#a;#i;#o;#s;constructor(e){this.#e=e.client,this.#n=e.language,this.#t=e.maxBatchSize,this.#r=e.normalizer,this.#a=e.ownsStore??!1,this.#i=e.progress,this.#o=e.store,this.#s=e.wiki}static async open(e={}){let n=OF(e.language),r=e.wiki??`${n}wiki`,i=await Br.open(e.cacheDatabasePath);return new t({client:new $r({concurrency:e.concurrency??bF,language:n,requestLog:SE(e.logDirPath),minRequestIntervalMs:e.minRequestIntervalMs??EF,retryBaseDelayMs:e.retryBaseDelayMs??IF,retryTimes:e.retryTimes??xF,userAgent:e.userAgent??kF,wiki:r,...e.fetch===void 0?{}:{fetch:e.fetch}}),language:n,maxBatchSize:NF(e.maxBatchSize),normalizer:e.normalizer,ownsStore:!0,progress:e.progress,store:i,wiki:r})}async close(){this.#a&&AF(this.#o)&&await this.#o.close()}async resolveQids(e){let n=LF(e),r=await this.#d(n),i=await this.#l([...r.values()].filter(CF));return n.map(a=>{let o=r.get(a);if(o===void 0)return{isDisambiguation:!1,qid:a};let s=i.get(a),d=s?.pages??[],u=PF(o.sitelinks,this.#s);return{...o.description===void 0?{}:{description:o.description},...s===void 0?{}:{disambiguation:TF(s)},...d.length===0?{}:{disambiguationPages:d},isDisambiguation:d.length>0,...o.label===void 0?{}:{label:o.label},qid:a,...u===void 0?{}:{sitelink:u},...o.sitelinks.length===0?{}:{sitelinks:o.sitelinks.map(OE)}}})}async#d(e){let n=new Map(await this.#o.getQids(e,this.#n)),r=e.filter(o=>!n.has(o)),i=n.size,a=0;await this.#m("qid",i,e.length);for(let o of Zm(r,this.#t)){let s=await this.#e.getEntities(o);a+=o.length,await this.#m("entity",a,r.length);let d=await this.#c(s),u=new Date().toISOString(),l=o.map(m=>vF(m,s.get(m),d,u));await this.#o.putQids(l,this.#n);for(let m of l)n.set(m.qid,m);i+=o.length,await this.#m("qid",i,e.length)}return n}async#c(e){let n=new Map,r=new Map;for(let i of e.values())for(let a of i.sitelinks){let o=r.get(a.wiki)??new Set;o.add(a.title),r.set(a.wiki,o)}for(let[i,a]of r)for(let o of Zm([...a],this.#t)){for(let[s,d]of await this.#e.getPagesByTitles(o,i))n.set(NE(i,s),d);await this.#m("page",n.size,_F(r))}return n}async#l(e){let n=e.map(o=>o.qid),r=new Map(await this.#o.getDisambiguations(n,this.#s)),i=e.filter(o=>!r.has(o.qid)),a=r.size;await this.#m("disambiguation-page",a,n.length);for(let o of i){let s=await this.#u(o);await this.#o.putDisambiguations([s],this.#s),r.set(o.qid,s),a+=1,await this.#m("disambiguation-page",a,n.length)}return r}async#u(e){let n=[];for(let a of e.sitelinks.filter(o=>o.isDisambiguation)){let o=await this.#e.parseDisambiguationPage(a.title,a.wiki),s=new Map;for(let l of Zm(o.linkedTitles,this.#t)){for(let[m,p]of await this.#e.getPagesByTitles(l,a.wiki))s.set(m,p);await this.#m("linked-page",s.size,o.linkedTitles.length)}let d=new Map,u=[];for(let l of o.linkedTitles){let m=s.get(l)?.wikibaseItem;m!==void 0&&(d.set(l,m),u.push({qid:m,title:l}))}n.push({linkedQids:u,...o.pageId===void 0?{}:{pageId:o.pageId},text:IE(o.text,d),title:o.title,wiki:o.wiki})}let r=LE(n),i=await this.#p(e,n,r);return{checkedAt:new Date().toISOString(),disambiguationQid:e.qid,pages:n,...i}}async#p(e,n,r){if(this.#r===void 0||r.length===0)return{};try{return{profile:await this.#r({pageQidLinks:r,pages:n,sourceQid:e.qid,...e.label===void 0?{}:{surface:e.label}})}}catch(i){if(!(i instanceof Ie))throw i;let a=new Date,o=new Date(a.getTime()+RF);return{profileError:{failedAt:a.toISOString(),message:i.message,retryAfter:o.toISOString()}}}}async#m(e,n,r){await this.#i?.({detail:e,done:Math.min(Math.max(0,n),Math.max(0,r)),total:Math.max(0,r)})}}});var Wa=c(()=>{"use strict";jm();bE();qm();DE();Ym()});var ep=c(()=>{"use strict";Wa()});var WE=c(()=>{"use strict";Sa();B();Ve();md();Wm();pd();_a();ep();ie()});var wd=c(()=>{"use strict";WE();Wm();md();pd();_a();ep()});var tp=c(()=>{"use strict";Lt();Se();Sa();B();Ve();ka();Ta();je();wd();ie();Qe()});var FE=c(()=>{"use strict";tp();wd();ie()});var mp=c(()=>{"use strict";Jw();tp();wd();_n();ie();Dm();va();FE()});var BE=c(()=>{"use strict";mp()});import{randomBytes as kre}from"crypto";import{join as vre}from"path";var jF,UE=c(()=>{"use strict";me();k();N();jF=10080*60*1e3});var HE=c(()=>{"use strict"});var qE=c(()=>{"use strict";k();UE();HE()});var $E=c(()=>{"use strict"});var KE=c(()=>{"use strict";qE();$E()});var Kr=c(()=>{"use strict";BE();KE();Tn()});var pp=c(()=>{"use strict";Pe()});var QE=c(()=>{"use strict";pp()});var XE=c(()=>{"use strict";QE();pp()});function VE(t,e){return t-e}function YE(t){let e=Ga();for(let n of t)e[String(n)]=!0;return e}function hp(t,e){return Object.hasOwn(t,String(e))}function UF(t){return Object.keys(t).length}function Ga(){return Object.create(null)}var kd,ZE=c(()=>{"use strict";kd=class{#e;constructor(e){this.#e=e}selectTopChunks(e){if(e.allChunks.length===0)return[];if(e.latestChunkIds.length===0)return[...e.allChunks].slice(0,e.capacity);let n=YE(e.latestChunkIds),r=e.allChunks.filter(o=>!hp(n,o.id));if(r.length===0)return[];let i=this.#t({getSuccessorChunkIds:e.getSuccessorChunkIds,latestChunkIds:e.latestChunkIds});if(UF(i)===0)return[...r].sort((o,s)=>s.generation-o.generation).slice(0,e.capacity);let a=this.#n({allChunkIds:e.allChunks.map(o=>o.id),forwardScores:i,getSuccessorChunkIds:e.getSuccessorChunkIds});return r.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,e.capacity).map(o=>o.chunk)}#t(e){let n=e.latestChunkIds.length;if(n===0)return Ga();let r=1/n,i=Ga(),a=Ga(),o=[...e.latestChunkIds],s=0;for(let d of e.latestChunkIds)i[String(d)]=r,a[String(d)]=!0;for(;s<o.length;){let d=o[s];if(s+=1,d===void 0)continue;let u=i[String(d)];if(u===void 0)continue;let l=[...e.getSuccessorChunkIds(d)].sort(VE);if(l.length===0)continue;let m=u/l.length;for(let p of l){let h=String(p);i[h]=(i[h]??0)+m,!hp(a,p)&&(a[h]=!0,o.push(p))}}return i}#n(e){let n=Ga();for(let s of e.allChunkIds)for(let d of e.getSuccessorChunkIds(s)){let u=String(d),l=n[u]??[];n[u]=[...l,s]}let r={...e.forwardScores},i=Object.keys(e.forwardScores).map(Number),a=YE(i),o=0;for(;o<i.length;){let s=i[o];if(o+=1,s===void 0)continue;let d=r[String(s)];if(d===void 0)continue;let u=[...n[String(s)]??[]].sort(VE);if(u.length===0)continue;let l=d/u.length;for(let m of u){let p=String(m);r[p]=(r[p]??0)+l,!hp(a,m)&&(a[p]=!0,i.push(m))}}return r}}});var Rd,eI=c(()=>{"use strict";Rd=class{#e;#t=[];#n=[];#r=[];#a=0;constructor(e){this.#e=e}get capacity(){return this.#e}get generation(){return this.#a}addChunks(e){this.#t.push(...e)}setRetainedChunks(e){this.#n.splice(0,this.#n.length,...e.previousFragmentChunks),this.#r.splice(0,this.#r.length,...e.extraChunks)}finalizeFragment(){let e=[...this.#t];return this.#t.splice(0,this.#t.length),this.#a+=1,e}getChunks(){return[...this.#t,...this.#n,...this.#r]}getAllChunksForSaving(){return[...this.#t]}getChunksForPrompt(e=!0){return e?this.getChunks():[...this.#n,...this.#r]}formatForPrompt(e=!0){let n=this.getChunksForPrompt(e);return n.length===0?"(empty)":n.map(r=>`${r.id}. [${r.label}] - ${r.content}`).join(`
|
|
1972
|
+
`)}clear(){this.#t.splice(0,this.#t.length),this.#n.splice(0,this.#n.length),this.#r.splice(0,this.#r.length)}}});async function HF(t){let e=Object.create(null);for(let[i,a]of t.chunkBatch.chunks.entries()){a.id=await t.idGenerator(),a.generation=t.generation;let o=t.chunkBatch.tempIds[i];o===void 0||o===""||(e[o]=a)}let n=[...t.visibleChunks,...t.chunkBatch.chunks],r=[];for(let i of t.chunkBatch.links){let a=tI(i.from,e,n),o=tI(i.to,e,n);if(a===void 0||o===void 0)continue;let[s,d]=a.id>o.id?[a.id,o.id]:[o.id,a.id];if(qF(n,d,s),i.strength===void 0){r.push({fromId:s,toId:d});continue}r.push({fromId:s,strength:i.strength,toId:d})}return{chunks:t.chunkBatch.chunks,edges:r,...t.chunkBatch.importanceAnnotations===void 0?{}:{importanceAnnotations:t.chunkBatch.importanceAnnotations}}}function tI(t,e,n){return typeof t=="string"?e[t]:n.find(r=>r.id===t)}function qF(t,e,n){for(let r of t)if(r.id===e){r.links.includes(n)||r.links.push(n);return}}var vd,nI=c(()=>{"use strict";ZE();eI();vd=class{#e;#t;#n;constructor(e,n,r){this.#e=r,this.#t=new kd(n),this.#n=new Rd(e)}get capacity(){return this.#n.capacity}createChunkBatchContext(e){return{visibleChunkIds:this.#n.getChunksForPrompt(e?.includeCurrentFragment??!0).map(r=>r.id),workingMemoryPrompt:this.#n.formatForPrompt(e?.includeCurrentFragment??!0)}}async acceptChunkBatch(e){let n=await HF({chunkBatch:e,generation:this.#n.generation,idGenerator:this.#e,visibleChunks:this.#n.getChunks()});return this.#n.addChunks(n.chunks),n}completeFragment(e){let n=this.#n.getAllChunksForSaving(),r=n.map(a=>a.id),i=this.#t.selectTopChunks({allChunks:e.allChunks,capacity:this.#n.capacity,getSuccessorChunkIds:e.getSuccessorChunkIds,latestChunkIds:r});this.#n.setRetainedChunks({extraChunks:i,previousFragmentChunks:n}),this.#n.finalizeFragment()}clear(){this.#n.clear()}}});function Ua(t){let e=t.trim();return e===""?[]:Array.from(KF.segment(e)).map(n=>n.segment.trim()).filter(n=>n!=="")}function Be(t){let e=t.replace(/\s+/gu," ").trim(),n=JF(e),r="";for(let i of n)if(!zF(i)){if(!Ba(i)){r+=i;continue}r+=i.toLowerCase().normalize("NFD").replace(new RegExp("\\p{M}","gu"),"")}return r}function JF(t){let e=[],n=0;for(;n<t.length;){let r=t[n];if(r===void 0)break;if(r!==" "){e.push(r),n+=1;continue}if(e.length>=2&&$F.has(e[e.length-1]??"")&&Ba(e[e.length-2]??"")&&n<t.length-1&&Ba(t[n+1]??"")){e.pop(),n+=1;continue}let i=e[e.length-1]??"",a=t[n+1]??"";i!==""&&a!==""&&Ba(i)&&Ba(a)&&e.push(r),n+=1}return e}function Ba(t){return t!==""&&new RegExp("\\p{Script=Latin}","u").test(t)&&new RegExp("\\p{L}","u").test(t)}function zF(t){return/[\p{P}\p{S}]/u.test(t)}var $F,KF,Ha=c(()=>{"use strict";$F=new Set(["\u2010","\u2011","\u2012","\u2013","\u2014","\u2015"]),KF=new Intl.Segmenter(void 0,{granularity:"sentence"})});function gp(){return{exactNormalized:!1,exactRaw:!1,exactSubstring:!1,matchEnd:-1,matchStart:-1,score:0}}function iI(t,e){let n=Td(t,2),r=Td(e,2),i=Td(t,3),a=Td(e,3);return(rI(n,r)+rI(i,a))/2}function Td(t,e){if(t.length<=e)return t===""?new Set:new Set([t]);let n=new Set;for(let r=0;r<=t.length-e;r+=1)n.add(t.slice(r,r+e));return n}function rI(t,e){if(t.size===0||e.size===0)return 0;let n=0;for(let r of t)e.has(r)&&(n+=1);return 2*n/(t.size+e.size)}function aI(t,e){if(t===e)return 1;if(t===""||e==="")return 0;let n=Array.from({length:e.length+1},(a,o)=>o),r=new Array(e.length+1).fill(0);for(let[a,o]of[...t].entries()){r[0]=a+1;for(let[s,d]of[...e].entries()){let u=(r[s]??0)+1,l=(n[s+1]??0)+1,m=(n[s]??0)+(o===d?0:1);r[s+1]=Math.min(u,l,m)}[n,r]=[r,n]}return 1-(n[e.length]??Math.max(t.length,e.length))/Math.max(t.length,e.length)}function oI(t,e){return 1-Math.abs(t.length-e.length)/Math.max(t.length,e.length)}function sI(t,e){if(t===e)return 1;let n=t.length,r=e.length;if(n===0||r===0)return 0;let i=Array.from({length:n+1},()=>new Array(r+1).fill(0)),a=0;for(let o=1;o<=n;o+=1)for(let s=1;s<=r;s+=1){if(t[o-1]!==e[s-1])continue;let d=i[o];d!==void 0&&(d[s]=(i[o-1]?.[s-1]??0)+1,a=Math.max(a,d[s]??0))}return 2*a/(n+r)}function dI(t){return` - ${t.occurrenceId} score=${t.score.toFixed(3)}
|
|
1973
|
+
prev: ${fp(t.prevText)}
|
|
1974
|
+
text: ${fp(t.text,120)}
|
|
1975
|
+
next: ${fp(t.nextText)}`}function fp(t,e=80){let n=t.replace(/\s+/gu," ").trim();return n===""?"(none)":n.length<=e?n:`${n.slice(0,e-3)}...`}var yp=c(()=>{"use strict"});var bp=c(()=>{"use strict"});function uI(t,e,n){return t.mode==="head_tail"?VF(t,e):Sp(t.text??"",e,n)}function VF(t,e){let n=t.head??"",r=t.tail??"",i=Be(e),a=Be(n),o=Be(r);if(a===""||o==="")return gp();let s=i.indexOf(a),d=i.lastIndexOf(o);if(s!==-1&&d!==-1&&s<=d){let p=a.length+o.length,h=Math.min(1,p/Math.max(1,i.length));return{exactNormalized:!1,exactRaw:!1,exactSubstring:!0,matchEnd:d+o.length,matchStart:s,score:.82+.18*h}}let u=Sp(n,e,"start_anchor"),l=Sp(r,e,"end_anchor"),m=u.matchStart<=l.matchStart?1:.85;return{exactNormalized:!1,exactRaw:!1,exactSubstring:!1,matchEnd:Math.max(u.matchEnd,l.matchEnd),matchStart:Math.min(u.matchStart,l.matchStart),score:(u.score+l.score)/2*m}}function Sp(t,e,n){let r=t.trim(),i=e.trim(),a=Be(r),o=Be(i);if(a===""||o==="")return gp();if(r===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 h=o.indexOf(a),y=a.length/Math.max(1,o.length),f=cI({candidateNormalized:o,label:n,matchEnd:h+a.length,matchStart:h,queryNormalized:a});return{exactNormalized:!1,exactRaw:!1,exactSubstring:!0,matchEnd:h+a.length,matchStart:h,score:Math.min(1,.75+.25*y+f)}}if(a.includes(o)){let h=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*h)}}let s=0,d=-1,u=-1,l=Math.min(o.length,Math.max(a.length,Math.floor(a.length*1.2))),m=Math.min(l,Math.max(1,Math.floor(a.length*.8))),p=new Set(a);for(let h=m;h<=l;h+=1)for(let y=0;y<=o.length-h;y+=1){let f=o.slice(y,y+h),I=new Set(f),v=new Set([...p,...I]).size,x=[...p].filter(_=>I.has(_)).length;if((v===0?0:x/v)<.35)continue;let G=.35*iI(a,f)+.35*sI(a,f)+.2*aI(a,f)+.1*oI(a,f)+cI({candidateNormalized:o,label:n,matchEnd:y+h,matchStart:y,queryNormalized:a});G>s&&(s=Math.min(1,G),d=y,u=y+h)}return{exactNormalized:!1,exactRaw:!1,exactSubstring:!1,matchEnd:u,matchStart:d,score:s}}function cI(t){if(t.matchStart<0||t.matchEnd<0||t.queryNormalized.length<12||t.label==="start_anchor"&&t.matchStart!==0||t.label==="end_anchor"&&t.matchEnd!==t.candidateNormalized.length)return 0;let e=t.queryNormalized.length/Math.max(1,t.candidateNormalized.length);return .08*(.5+.5*Math.min(1,e))}var lI=c(()=>{"use strict";Ha();yp();bp()});function mI(t){if(t==null)return[void 0,"anchor is required"];if(typeof t=="string"){let a=t.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],d=o[o.length-1];return s===void 0||d===void 0?[void 0,"anchor string is empty"]:[{head:s,mode:"head_tail",tail:d},void 0]}}return[{mode:"full",text:a},void 0]}if(typeof t!="object")return[void 0,`expected string or object, got ${typeof t}`];let e=t;if((e.mode==="head_tail"||e.head!==void 0?"head_tail":"full")==="full"){let a=typeof e.text=="string"?e.text.trim():"";return a===""?[void 0,"full anchor requires non-empty 'text'"]:[{mode:"full",text:a},void 0]}let r=typeof e.head=="string"?e.head.trim():"",i=typeof e.tail=="string"?e.tail.trim():"";return r!==""&&i===""?[{mode:"full",text:r},void 0]:r===""&&i!==""?[{mode:"full",text:i},void 0]:r===""||i===""?[void 0,"head_tail anchor requires non-empty 'head' and 'tail'"]:[{head:r,mode:"head_tail",tail:i},void 0]}function pI(t,e){if(t===void 0)return;if(t.mode==="head_tail"){let r=e==="end_anchor"?wp(t.tail??"","end_anchor"):wp(t.head??"","start_anchor");return r===""?void 0:{mode:"full",text:r}}return{mode:"full",text:wp(t.text??"",e)}}function Ep(t){return t.mode==="head_tail"?Be(t.head??"").length+Be(t.tail??"").length:Be(t.text??"").length}function wp(t,e){let n=Ua(t);return n.length<=1?t.trim():e==="end_anchor"?n[n.length-1]??t.trim():n[0]??t.trim()}var hI=c(()=>{"use strict";Ha()});var qt,bI=c(()=>{"use strict";lI();hI();yp();bp();qt=class{resolve(e,n,r){let i=e.start_anchor??e.start,a=e.end_anchor??e.end,[o,s]=this.parseAnchor(i,"start_anchor");if(s!==void 0)return[void 0,s];let d;if(a!==void 0){let[f,I]=this.parseAnchor(a,"end_anchor");if(I!==void 0)return[void 0,I];d=f}let[u,l,m]=this.resolveAnchor({anchor:o,candidateSentenceIds:n,candidateTexts:r,label:"start_anchor"});if(m!==void 0)return[void 0,m];if(u===void 0)return[void 0,{candidates:[],code:"none",fieldName:"start_anchor",message:"start_anchor could not be matched: no candidate selected"}];if(d===void 0)return[{confidence:u.score,sentenceIds:[u.sentenceId],strategy:l},void 0];let[p,h,y]=this.resolveAnchor({anchor:d,candidateSentenceIds:n,candidateTexts:r,label:"end_anchor",minIndex:u.index});return y!==void 0?[void 0,y]:p===void 0?[void 0,{candidates:[],code:"none",fieldName:"end_anchor",message:"end_anchor could not be matched: no candidate selected"}]:p.index<u.index?[void 0,{candidates:[],code:"invalid_range",fieldName:"end_anchor",message:`Invalid evidence range: end_anchor resolved before start_anchor (${u.occurrenceId} -> ${p.occurrenceId}).`}]:[{confidence:Math.min(u.score,p.score),sentenceIds:n.slice(u.index,p.index+1),strategy:`${l}+${h}`},void 0]}resolveWithOverrides(e){let n=e.evidence.start_anchor??e.evidence.start,r=e.evidence.end_anchor??e.evidence.end,[i,a]=this.parseAnchor(n,"start_anchor");if(a!==void 0)return[void 0,a];let o=e.overrides.start_anchor,[s,d,u]=o===void 0?this.resolveAnchor({anchor:i,candidateSentenceIds:e.candidateSentenceIds,candidateTexts:e.candidateTexts,label:"start_anchor"}):[o,"choice_final",void 0];if(u!==void 0)return[void 0,u];if(s===void 0)return[void 0,{candidates:[],code:"none",fieldName:"start_anchor",message:"start_anchor could not be matched: no candidate selected"}];if(r===void 0)return[{confidence:s.score,sentenceIds:[s.sentenceId],strategy:d},void 0];let[l,m]=this.parseAnchor(r,"end_anchor");if(m!==void 0)return[void 0,m];let p=e.overrides.end_anchor,[h,y,f]=p===void 0?this.resolveAnchor({anchor:l,candidateSentenceIds:e.candidateSentenceIds,candidateTexts:e.candidateTexts,label:"end_anchor",minIndex:s.index}):[p,"choice_final",void 0];return f!==void 0?[void 0,f]:h===void 0?[void 0,{candidates:[],code:"none",fieldName:"end_anchor",message:"end_anchor could not be matched: no candidate selected"}]:h.index<s.index?[void 0,{candidates:[],code:"invalid_range",fieldName:"end_anchor",message:`Invalid evidence range: end_anchor resolved before start_anchor (${s.occurrenceId} -> ${h.occurrenceId}).`}]:[{confidence:Math.min(s.score,h.score),sentenceIds:e.candidateSentenceIds.slice(s.index,h.index+1),strategy:`${d}+${y}`},void 0]}parseAnchor(e,n){let[r,i]=mI(e);return i!==void 0?[void 0,{candidates:[],code:"invalid_anchor",fieldName:n,message:`Invalid evidence.${n}: ${i}`}]:[pI(r,n),void 0]}resolveAnchor(e){if(e.anchor===void 0)return[void 0,"invalid",{candidates:[],code:"invalid",fieldName:e.label,message:`${e.label} is missing`}];let n=this.rankAnchor(e.anchor,e.candidateSentenceIds,e.candidateTexts,e.label),r=e.minIndex;if(r!==void 0&&(n=n.filter(l=>l.index>=r)),n.length===0)return[void 0,"none",{candidates:[],code:"none",fieldName:e.label,message:`${e.label} could not be matched: no candidates available`}];let i=n.filter(l=>l.exactRaw);if(i.length===1)return[i[0],"exact_raw",void 0];if(i.length>1)return[void 0,"ambiguous_exact_raw",this.#t(e.label,"ambiguous_exact_raw",i)];let a=n.filter(l=>l.exactSubstring);if(a.length===1&&Ep(e.anchor)>=8)return[a[0],"exact_substring",void 0];if(a.length>1&&Ep(e.anchor)>=8){let l=this.#e(a);return l!==void 0?[l,"exact_substring_scored",void 0]:[void 0,"ambiguous_exact_substring",this.#t(e.label,"ambiguous_exact_substring",a)]}let o=n.filter(l=>l.exactNormalized);if(o.length===1)return[o[0],"exact_normalized",void 0];if(o.length>1){let l=this.#e(o);return l!==void 0?[l,"exact_normalized_scored",void 0]:[void 0,"ambiguous_exact_normalized",this.#t(e.label,"ambiguous_exact_normalized",o)]}let s=n[0];if(s===void 0)return[void 0,"none",{candidates:[],code:"none",fieldName:e.label,message:`${e.label} could not be matched: no candidates available`}];let d=n[1]?.score??0,u=s.score-d;return s.score<.55?[void 0,"low_confidence",{candidates:n.slice(0,3),code:"low_confidence",fieldName:e.label,message:`${e.label} could not be matched confidently. Best candidate score=${s.score.toFixed(3)}.`}]:n.length===1?[s,"auto_top1_single_candidate",void 0]:s.score>=.97?[s,"auto_top1_very_high",void 0]:s.score>=.9&&u>=.07?[s,"auto_top1",void 0]:[void 0,"ambiguous_ranked",this.#t(e.label,"ambiguous_ranked",n)]}rankAnchor(e,n,r,i){let a=[];for(let[o,s]of n.entries()){let d=r[o]??"",u=o>0?r[o-1]??"":"",l=o<r.length-1?r[o+1]??"":"",m=uI(e,d,i);a.push({exactNormalized:m.exactNormalized,exactRaw:m.exactRaw,exactSubstring:m.exactSubstring,index:o,nextText:l,occurrenceId:`S${o+1}`,prevText:u,score:m.score,sentenceId:s,text:d})}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(e){if(e.length===0)return;if(e.length===1)return e[0];let n=e[0];if(n===void 0)return;let r=e[1]?.score??0;if(n.score>=.9&&n.score-r>=.07)return n}#t(e,n,r){let i=r.slice(0,3),a=i.map(o=>dI(o)).join(`
|
|
1976
|
+
`);return{candidates:i,code:n,fieldName:e,message:`${e} is ambiguous. Top candidates:
|
|
1776
1977
|
${a}
|
|
1777
|
-
Please provide a more specific anchor or surrounding context.`}}}});var
|
|
1778
|
-
`),
|
|
1779
|
-
`)}function
|
|
1978
|
+
Please provide a more specific anchor or surrounding context.`}}}});var SI=c(()=>{"use strict"});var wI=c(()=>{"use strict";bI();SI()});var Ln,_d,EI=c(()=>{"use strict";Ln=["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(`
|
|
1979
|
+
`),_d=[{quote:"exact short source quote copied from the selected sentence",sentence_id:"sentence id from the source context, such as S1"}]});function $t(t){return t.replace(new RegExp("\\p{Default_Ignorable_Code_Point}","gu"),"").normalize("NFKC").replace(/\s+/gu," ").trim()}function Pd(t){let r=II(t).replace(new RegExp("\\p{Default_Ignorable_Code_Point}","gu"),"").normalize("NFKC"),a=ij(r).normalize("NFD").replace(new RegExp("\\p{M}","gu"),"");return II(a).replace(/ +/gu," ").trim()}function RI(t){let e=vI(t).trim();return{normalizedQuote:Pd(e),quoteRaw:e}}function Ip(t){let{normalizedQuote:e,quoteRaw:n}=t.preparedQuote,r=vI(t.sentence).trim(),i=Pd(r);if(e===""||i==="")return TI(e,i);if(n===r)return{exactNormalized:!0,exactRaw:!0,exactSubstring:!0,matchEnd:i.length,matchStart:0,normalizedQuote:e,normalizedSentence:i,score:1,strategy:"exact"};if(e===i)return{exactNormalized:!0,exactRaw:!1,exactSubstring:!0,matchEnd:i.length,matchStart:0,normalizedQuote:e,normalizedSentence:i,score:.995,strategy:"normalized_exact"};let a=i.indexOf(e);if(a!==-1){let o=e.length/Math.max(1,i.length);return{exactNormalized:!1,exactRaw:!1,exactSubstring:!0,matchEnd:a+e.length,matchStart:a,normalizedQuote:e,normalizedSentence:i,score:Math.min(.99,.95+.04*o),strategy:"normalized_substring"}}return tj(e,i)}function tj(t,e){let n=Math.max(1,Math.floor(t.length*.72)),r=Math.min(e.length,Math.max(n,Math.ceil(t.length*2.25))),i=TI(t,e);for(let a=n;a<=r;a+=1)for(let o=0;o<=e.length-a;o+=1){let s=e.slice(o,o+a),d=nj(t,s);d<=i.score||(i={exactNormalized:!1,exactRaw:!1,exactSubstring:!1,matchEnd:o+a,matchStart:o,normalizedQuote:t,normalizedSentence:e,score:d,strategy:"fuzzy_window"})}return i}function nj(t,e){let n=cj(t,e),r=uj(t,e),i=lj(t,e),a=mj(t,e),o=sj(t,e),s=dj(t,e),d=pj(t,e),u=Math.max(n,r,i,a),l=.2*n+.18*r+.24*i+.2*a+.08*o+.05*s+.05*d;return d>=.75&&u<.45&&(l*=.65),Math.max(0,Math.min(1,l))}function rj(t){return ej.get(t)??t.toLocaleLowerCase("und")}function ij(t){let e="";for(let n of t)e+=rj(n);return e}function II(t){let e="";for(let n of t)e+=aj(n)||!oj(n)?n:" ";return e}function aj(t){return t==="+"||t==="#"||t==="&"}function oj(t){return/\s/u.test(t)||new RegExp("\\p{Separator}","u").test(t)||/[\p{Dash_Punctuation}\p{Connector_Punctuation}\p{Initial_Punctuation}\p{Final_Punctuation}\p{Open_Punctuation}\p{Close_Punctuation}\p{Other_Punctuation}]/u.test(t)||/[\\/.。.。、,,::;;!!??·・•|]/u.test(t)}function vI(t){return t.replace(/<\/?(?:mention|quote)\b[^>]*>/giu,"")}function TI(t,e){return{exactNormalized:!1,exactRaw:!1,exactSubstring:!1,matchEnd:-1,matchStart:-1,normalizedQuote:t,normalizedSentence:e,score:0,strategy:"empty"}}function sj(t,e){let n=xI(Cd(t,2),Cd(e,2)),r=xI(Cd(t,3),Cd(e,3));return(n+r)/2}function Cd(t,e){if(t.length<=e)return t===""?new Set:new Set([t]);let n=new Set;for(let r=0;r<=t.length-e;r+=1)n.add(t.slice(r,r+e));return n}function xI(t,e){if(t.size===0||e.size===0)return 0;let n=0;for(let r of t)e.has(r)&&(n+=1);return 2*n/(t.size+e.size)}function dj(t,e){return 1-Math.abs(t.length-e.length)/Math.max(t.length,e.length)}function cj(t,e){if(t===e)return 1;if(t===""||e==="")return 0;let n=Array.from({length:e.length+1},(s,d)=>d),r=new Array(e.length+1).fill(0),i=[...t],a=[...e];for(let[s,d]of i.entries()){r[0]=s+1;for(let[u,l]of a.entries()){let m=(r[u]??0)+1,p=(n[u+1]??0)+1,h=(n[u]??0)+(d===l?0:1);r[u+1]=Math.min(m,p,h)}[n,r]=[r,n]}return 1-(n[a.length]??Math.max(t.length,e.length))/Math.max(t.length,e.length)}function uj(t,e){if(t===""||e==="")return 0;let n=[...t],r=[...e],i=new Array(r.length+1).fill(0),a=new Array(r.length+1).fill(0),o=0;for(let s=1;s<=n.length;s+=1){for(let d=1;d<=r.length;d+=1)a[d]=n[s-1]===r[d-1]?(i[d-1]??0)+1:0,o=Math.max(o,a[d]??0);[i,a]=[a,i],a.fill(0)}return 2*o/(n.length+r.length)}function lj(t,e){if(t===""||e==="")return 0;let n=[...t],r=[...e],i=new Array(r.length+1).fill(0),a=new Array(r.length+1).fill(0);for(let s=1;s<=n.length;s+=1){for(let d=1;d<=r.length;d+=1)a[d]=n[s-1]===r[d-1]?(i[d-1]??0)+1:Math.max(i[d]??0,a[d-1]??0);[i,a]=[a,i],a.fill(0)}return 2*(i[r.length]??0)/(n.length+r.length)}function mj(t,e){if(t===""||e==="")return 0;let n=[...t],r=[...e],i=new Array(r.length+1).fill(0),a=new Array(r.length+1).fill(0);for(let o=1;o<=n.length;o+=1){for(let s=1;s<=r.length;s+=1)a[s]=n[o-1]===r[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[r.length]??0)/n.length}function pj(t,e){let n=kI(t),r=kI(e),i=0,a=0;for(let[o,s]of n){let d=r.get(o)??0;i+=Math.min(s,d),a+=s}return a===0?0:i/a}function kI(t){let e=new Map;for(let n of t)e.set(n,(e.get(n)??0)+1);return e}var ej,xp=c(()=>{"use strict";ej=new Map([["\xDF","ss"],["\u1E9E","ss"],["\u03C2","\u03C3"]])});function Rp(t){let e=Rj(t.evidence.quote),n=vj(t.evidence.sentence_id),r;if(e==="")return[void 0,{candidates:[],code:"invalid",message:"Evidence quote is missing or empty."}];let i=RI(e);if(n!==void 0){let l=t.sentences.findIndex(p=>p.id===n),m=t.sentences[l];if(m!==void 0){let p=PI({index:l,scored:Ip({preparedQuote:i,sentence:m.text}),sentence:m,sentences:t.sentences});if(r=p,kj(p))return[{candidate:p,confidence:p.score,sentenceIds:[p.sentence.sentenceId],strategy:`sentence_id+${p.strategy}`},void 0]}}let a=wj(i,t.sentences,kp);if(n!==void 0){let l=a.find(p=>p.occurrenceId===n)??r,m=a[0];if(l!==void 0&&m!==void 0&&xj(l,m))return[{candidate:l,confidence:l.score,sentenceIds:[l.sentence.sentenceId],strategy:`sentence_id+${l.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,d=o.score-s,u=yj(e);return o.score<gj?[void 0,{candidates:a.slice(0,kp),code:"low_confidence",message:`Evidence quote could not be matched confidently. Best candidate score=${o.score.toFixed(3)}.`+u}]:o.score>=fj&&d>=CI?[{candidate:o,confidence:o.score,sentenceIds:[o.sentence.sentenceId],strategy:`quote_auto_top1:${o.strategy}`},void 0]:[void 0,{candidates:a.slice(0,kp),code:"ambiguous",message:"Evidence quote is ambiguous. Choose one candidate occurrence ID."+u}]}function yj(t){return Ua(t).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 Jr(t){let e=bj(t.evidence);if(e.length===0)return[void 0,{candidates:[],code:"invalid",message:"Evidence selection list is empty."}];let n=[],r=[],i=1,a;for(let o of e){let[s,d]=Rp({evidence:o,sentences:t.sentences});if(d!==void 0)return[void 0,d];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),n.push(...s.sentenceIds),r.push(s.strategy)}return a===void 0?[void 0,{candidates:[],code:"none",message:"Evidence selection could not be resolved."}]:[{candidate:a,confidence:i,sentenceIds:Tj(n),strategy:`list:${r.join(",")}`},void 0]}function bj(t){return Sj(t)?t:[t]}function Sj(t){return Array.isArray(t)}function wj(t,e,n){let r=[];for(let[i,a]of e.entries()){let o=Ip({preparedQuote:t,sentence:a.text}),s=PI({index:i,scored:o,sentence:a,sentences:e});Ej(r,s,n)}return r}function Ej(t,e,n){let r=t.findIndex(i=>Ij(e,i)<0);if(r===-1){t.length<n&&t.push(e);return}t.splice(r,0,e),t.length>n&&t.pop()}function PI(t){return{exactNormalized:t.scored.exactNormalized,exactRaw:t.scored.exactRaw,exactSubstring:t.scored.exactSubstring,index:t.index,nextText:t.sentences[t.index+1]?.text??"",occurrenceId:t.sentence.id,prevText:t.sentences[t.index-1]?.text??"",score:t.scored.score,sentence:t.sentence,strategy:t.scored.strategy}}function Ij(t,e){return t.score!==e.score?e.score-t.score:t.exactRaw!==e.exactRaw?t.exactRaw?-1:1:t.exactSubstring!==e.exactSubstring?t.exactSubstring?-1:1:t.exactNormalized!==e.exactNormalized?t.exactNormalized?-1:1:t.index-e.index}function xj(t,e){return(t.score>=hj||t.exactSubstring&&t.score>=_I)&&(t.occurrenceId===e.occurrenceId||e.score-t.score<CI)}function kj(t){return t.score>=_I&&t.exactSubstring}function Rj(t){return $t(t??"")}function vj(t){let e=t?.trim();return e===""?void 0:e}function Tj(t){let e=new Set,n=[];for(let r of t){let i=r.join(":");e.has(i)||(e.add(i),n.push(r))}return n}var hj,_I,fj,CI,gj,kp,AI=c(()=>{"use strict";xp();Ha();hj=.72,_I=.95,fj=.72,CI=.15,gj=.45,kp=5});var zr=c(()=>{"use strict";wI();EI();xp();AI();Ha()});function LI(t){let e=Tp();for(let n of t){let r=qa(n.sentenceId);e[r]=n.wordsCount}return e}function OI(t){let e=Tp();for(let n of t.sentences)e[qa(n.sentenceId)]=n.rawText;return e}function NI(t){return t.map(e=>e.strength===void 0?{from:e.from,to:e.to}:{from:e.from,strength:e.strength,to:e.to})}function DI(t){return[t.occurrenceId,`prev: ${vp(t.prevText)}`,`text: ${vp(t.text)}`,`next: ${vp(t.nextText)}`].join(`
|
|
1980
|
+
`)}function vp(t){let e=t.replace(/\s+/gu," ").trim();return e===""?"(none)":e}function MI(t){return t==="evidence"||t==="start_anchor"||t==="end_anchor"?t:void 0}function WI(t){return typeof t=="object"&&t!==null}function FI(t){return t instanceof Error?t.message:String(t)}function jI(t){return t instanceof wt&&t.cause instanceof M}function qa(t){return t.join(":")}function $a(t){let e=Tp();for(let n of t)e[String(n)]=!0;return e}function Ka(t,e){return Aj(t,String(e))}function Aj(t,e){return Object.hasOwn(t,e)}function Tp(){return Object.create(null)}function _p(t){return t.length===1?t[0]:void 0}var Ja=c(()=>{"use strict";ve()});function GI(t){let{annotations:e,issues:n,validImportanceChunkIds:r}=t;if(e.length===0)return[];if(r===void 0)return e.map(a=>({chunkId:a.chunk_id,importance:ar(a.importance)}));let i=[];for(let a of e){if(!Ka(r,a.chunk_id)){n.push(`importance_annotations references unknown chunk_id ${a.chunk_id}`);continue}i.push({chunkId:a.chunk_id,importance:ar(a.importance)})}return i}var BI=c(()=>{"use strict";N();Ja()});function HI(t){let e=$a(t.tempIds),n=$a(t.visibleChunkIds),r=[];for(let[i,a]of t.links.entries()){let o=UI({fieldName:"from",index:i+1,issues:t.issues,reference:a.from,validChunkIds:n,validTempIds:e}),s=UI({fieldName:"to",index:i+1,issues:t.issues,reference:a.to,validChunkIds:n,validTempIds:e});o&&s&&r.push(a)}return r}function UI(t){return typeof t.reference=="string"?Ka(t.validTempIds,t.reference):Ka(t.validChunkIds,t.reference)?!0:(t.issues.push(`Link #${t.index}: "${t.fieldName}" chunk_id ${t.reference} does not exist in visible chunks`),!1)}var qI=c(()=>{"use strict";Ja()});function KI(t){return Array.isArray(t)?t.map($I):typeof t.quote=="string"||typeof t.sentence_id=="string"?$I(t):void 0}function $I(t){return{...typeof t.quote=="string"?{quote:t.quote}:{},...typeof t.sentence_id=="string"?{sentence_id:t.sentence_id}:{}}}function JI(t,e){return{candidates:t.candidates.map(Lj),code:t.code,fieldName:e,message:t.message}}function Lj(t){return{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}}var zI=c(()=>{"use strict"});import{z as R}from"zod";var QI,Oj,XI,Nj,VI,Dj,Mj,YI,ZI,Ad=c(()=>{"use strict";N();QI=R.object({from:R.union([R.number().int(),R.string()]),strength:R.string().optional(),to:R.union([R.number().int(),R.string()])}),Oj=R.object({quote:R.string().optional(),sentence_id:R.string().optional()}).passthrough(),XI=R.union([R.record(R.string(),R.unknown()),R.array(Oj)]),Nj=R.object({content:R.string(),evidence:XI.nullish(),label:R.string(),retention:R.enum(["verbatim","detailed","focused","relevant"]),temp_id:R.string()}).passthrough(),VI=R.object({chunks:R.array(Nj),fragment_summary:R.string(),links:R.array(QI)}),Dj=R.object({content:R.string(),evidence:XI.nullish(),importance:R.enum(["critical","important","helpful"]),label:R.string(),temp_id:R.string()}).passthrough(),Mj=R.object({chunk_id:R.number().int(),importance:R.enum(["critical","important","helpful"])}),YI=R.object({chunks:R.array(Dj),importance_annotations:R.array(Mj),links:R.array(QI)}),ZI=R.object({choice:R.string()})});var Wj,Ld,ex=c(()=>{"use strict";ve();zr();zI();Ja();Ad();Wj=3,Ld=class{#e;#t=new qt;#n;#r;#a;constructor(e){this.#e=e.choiceSystemPrompt,this.#n=e.projection,this.#r=e.responseIntentClassifierPrompt,this.#a=e.requestChoice}async resolve(e){let n=e.data.evidence;if(n==null)return[[],void 0];if(!WI(n)&&!Array.isArray(n))return[[],{candidates:[],code:"invalid_evidence",fieldName:"evidence",message:`Chunk #${e.chunkIndex} ("${e.chunkLabel}"): evidence must be an object or array`}];let r=this.#n.sentences,i=r.map(_=>_.sentenceId),a=r.map((_,Rt)=>({id:`S${Rt+1}`,sentenceId:_.sentenceId,text:_.projectedText})),o=KI(n),[s,d]=o===void 0?[void 0,void 0]:Jr({evidence:o,sentences:a});if(s!==void 0)return[s.sentenceIds,void 0];let u=Array.isArray(n)?[]:this.#i(n);if(u.length>0)return[u,void 0];let l=r.map(_=>_.projectedText),[m,p]=Array.isArray(n)?[void 0,void 0]:this.#t.resolve(n,i,l);if(m!==void 0)return[m.sentenceIds,void 0];let h=d===void 0?p:JI(d,"evidence");if(h===void 0)return[[],void 0];if(!(h.code.startsWith("ambiguous")||h.code==="low_confidence"&&e.isLastGenerationAttempt&&h.candidates.length>0))return[[],h];let f=MI(h.fieldName);if(f===void 0)return[[],h];let[I,v]=await this.#o({candidates:h.candidates,chunkData:e.data,chunkIndex:e.chunkIndex,chunkLabel:e.chunkLabel,fieldName:f});if(v!==void 0)return v.code==="choice_parse_failed_full_fragment"?[i,void 0]:[[],v];if(I===void 0)return[[],{candidates:h.candidates,code:"choice_failed",fieldName:h.fieldName,message:`Second-stage choice failed for ${h.fieldName}: no candidate returned.`}];if(f==="evidence")return[[I.sentenceId],void 0];if(Array.isArray(n))return[[],h];if(e.isLastGenerationAttempt){let[_,Rt]=this.#t.resolveWithOverrides({candidateSentenceIds:i,candidateTexts:l,evidence:n,overrides:{[f]:I}});return _!==void 0?[_.sentenceIds,void 0]:[[],Rt]}let x={...n,[f]:{mode:"full",text:I.text}},[J,G]=this.#t.resolve(x,i,l);return J!==void 0?[J.sentenceIds,void 0]:[[],G]}#i(e){let n=e.start_anchor??e.start,[r,i]=this.#t.parseAnchor(n,"start_anchor");if(i!==void 0||r?.text===void 0)return[];let a=_p(this.#n.findExactMatches(r.text));if(a===void 0)return[];let o=e.end_anchor??e.end;if(o===void 0)return this.#n.resolveSentenceIds(a);let[s,d]=this.#t.parseAnchor(o,"end_anchor");if(d!==void 0||s?.text===void 0)return[];let u=_p(this.#n.findExactMatches(s.text));return u===void 0||u.offset<a.offset?[]:this.#n.resolveSentenceIds({length:u.offset+u.length-a.offset,offset:a.offset})}async#o(e){let n=this.#s(e),r=e.candidates.map(i=>i.occurrenceId);try{return[await Z({maxRetries:Wj,messages:n,parse:a=>{let o=e.candidates.find(s=>s.occurrenceId===a.choice);if(o===void 0)throw new M([`Invalid choice "${a.choice}". Expected one of: ${r.join(", ")}`]);return o},responseIntentClassifierPrompt:this.#r,request:this.#a,schema:ZI}),void 0]}catch(i){return jI(i)?[void 0,{candidates:e.candidates,code:"choice_parse_failed_full_fragment",fieldName:e.fieldName,message:`Second-stage choice parse validation failed for ${e.fieldName}; falling back to the full fragment span.`}]:[void 0,{candidates:e.candidates,code:"choice_failed",fieldName:e.fieldName,message:`Second-stage choice failed for ${e.fieldName}: ${FI(i)}`}]}}#s(e){return[{content:this.#e,role:"system"},{content:`Previously generated chunk (do NOT rewrite it):
|
|
1780
1981
|
\`\`\`json
|
|
1781
1982
|
${JSON.stringify(e.chunkData,null,2)}
|
|
1782
1983
|
\`\`\`
|
|
@@ -1784,46 +1985,46 @@ ${JSON.stringify(e.chunkData,null,2)}
|
|
|
1784
1985
|
Resolve only this field: "${e.fieldName}" for chunk #${e.chunkIndex} [${e.chunkLabel}].
|
|
1785
1986
|
Choose exactly one candidate occurrence ID from the list below.
|
|
1786
1987
|
|
|
1787
|
-
`+e.candidates.map(
|
|
1788
|
-
`),role:"user"}]}}});var
|
|
1789
|
-
`)}function OW(t){return t instanceof pt&&t.cause instanceof O}function NW(t){return OW(t)||t instanceof we}function DW(t){let e=Object.create(null);for(let r of t)e[String(r.id)]=r;return e}function MW(t,e){let r=[];e.length!==t.length&&r.push(`Expected ${t.length} translated chunk(s), got ${e.length}`);for(let[n,i]of t.entries()){let a=e[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 O(r)}var CW,PW,LW,AW,xa,ok=d(()=>{"use strict";Ie();Tn();QE();rc();rc();ZE();ik();CW=7,PW=Ia.object({content:Ia.string(),id:Ia.number(),label:Ia.string()}),LW=Ia.array(PW),AW=3,xa=class{#e;#t;#r;#n;#i;constructor(e){this.#e=e.extractionGuidance,this.#t=e.llm,this.#r=e.scopes,this.#n=e.sentenceTextSource,this.#i=e.userLanguage}async extractUserFocused(e){let r=new jt(e.sentences),n=this.#a({promptTemplateName:ek,templateContext:{extraction_guidance:this.#e,evidence_selection_prompt:yr,user_language:this.#i,working_memory:e.workingMemoryPrompt},text:ak(r)}),i=await this.#o({emptyChunkBatch:{chunks:[],links:[],orderCorrect:!0,tempIds:[]},messages:n,metadataField:"retention",projection:r,schema:qE,sentences:e.sentences,visibleChunkIds:e.visibleChunkIds});return{chunkBatch:await this.#s(i.result.chunkBatch,i.parser),fragmentSummary:i.result.fragmentSummary??""}}async extractBookCoherence(e){let r=new jt(e.sentences),n=this.#a({promptTemplateName:tk,templateContext:{user_focused_chunks:e.userFocusedChunks.map(a=>({content:a.content,id:a.id,label:a.label})),evidence_selection_prompt:yr,user_language:this.#i,working_memory:e.workingMemoryPrompt},text:ak(r)}),i=await this.#o({emptyChunkBatch:{chunks:[],importanceAnnotations:[],links:[],orderCorrect:!0,tempIds:[]},messages:n,metadataField:"importance",projection:r,schema:$E,sentences:e.sentences,validImportanceChunkIds:e.userFocusedChunks.map(a=>a.id),visibleChunkIds:e.visibleChunkIds});return await this.#s(i.result.chunkBatch,i.parser)}#a(e){return[{content:this.#t.loadSystemPrompt(e.promptTemplateName,e.templateContext),role:"system"},{content:e.text,role:"user"}]}async#o(e){return await this.#t.withContext(async r=>{let n=new tc({metadataField:e.metadataField,projection:e.projection,responseIntentClassifierPrompt:this.#t.loadSystemPrompt(ee),sentenceTextSource:this.#n,sentences:e.sentences,visibleChunkIds:e.visibleChunkIds,choiceSystemPrompt:this.#t.loadSystemPrompt(rk,{extraction_guidance:this.#e,metadata_field:e.metadataField,user_language:this.#i}),requestChoice:async(i,a,o)=>await r.request(i,{retryIndex:a,retryMax:o,scope:this.#r.choice,useCache:!1}),...e.validImportanceChunkIds===void 0?{}:{validImportanceChunkIds:e.validImportanceChunkIds}});try{let i=await z({messages:e.messages,schema:e.schema,maxRetries:CW,parse:async(a,o,s)=>await n.parse(a,{isLastGenerationAttempt:o>=s}),responseIntentClassifierPrompt:this.#t.loadSystemPrompt(ee),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(NW(i))return{parser:n,result:{chunkBatch:e.emptyChunkBatch}};throw i}})}async#s(e,r){if(this.#i===void 0||e.chunks.length===0)return e;let n=[];for(let[i,a]of e.chunks.entries())YE({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 e;try{let i=await this.#c(n,this.#i),a=DW(i);return{...e,chunks:e.chunks.map((o,s)=>{let c=a[String(s)];return c===void 0?o:{...o,content:c.content,label:c.label}})}}catch{return e}}async#c(e,r){return await z({messages:[{content:this.#t.loadSystemPrompt(nk,{user_language:r}),role:"system"},{content:JSON.stringify(e,void 0,2),role:"user"}],schema:LW,maxRetries:AW,parse:n=>(MW(e,n),n),responseIntentClassifierPrompt:this.#t.loadSystemPrompt(ee),request:async(n,i,a)=>await this.#t.request(n,{retryIndex:i,retryMax:a,scope:this.#r.extraction})})}}});async function Gm(t,e){return await new xa(t).extractUserFocused(e)}async function Bm(t,e){return await new xa(t).extractBookCoherence(e)}var Um=d(()=>{"use strict";ok()});function sk(){return new Hm}var WW,Hm,ck=d(()=>{"use strict";WW=/(?:[.!?。!?…]+|(?:\r?\n)\s*(?:\r?\n)?|[.!?。!?…]+["'”’」』】)》〉〕〗]*)$/u,Hm=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(e){let r="",n=0;for await(let a of e){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(e,r){if(e.trim()==="")return{remainder:"",sentences:[]};let n=Array.from(this.#t.segment(e));if(n.length===0)return{remainder:e,sentences:[]};if(r||this.#o(e))return{remainder:"",sentences:n.map(a=>a.segment.trim()).filter(a=>a!=="")};let i=n[n.length-1];return i===void 0?{remainder:e,sentences:[]}:{remainder:e.slice(i.index),sentences:n.slice(0,-1).map(a=>a.segment.trim()).filter(a=>a!=="")}}#i(e){let r=0;for(let n of this.#r.segment(e))"isWordLike"in n&&n.isWordLike&&(r+=1);return r>0?r:this.#a(e)}#a(e){let r=0;for(let n of this.#e.segment(e))r+=1;return r}#o(e){return WW.test(e.trimEnd())}}});function gr(t,e){return(e?.adapter??sk()).pipe(t)}var qm=d(()=>{"use strict";ck()});var Ra,dk=d(()=>{"use strict";Qw();Um();qm();Ra=class{#e;#t;#r;constructor(e){this.#e=new zs(e.attention.capacity,e.attention.generationDecayFactor,e.attention.idGenerator),this.#t={extractionGuidance:e.extractionGuidance,llm:e.llm,scopes:e.scopes,sentenceTextSource:e.sentenceTextSource,...e.userLanguage===void 0?{}:{userLanguage:e.userLanguage}},this.#r=e.segmenter}get capacity(){return this.#e.capacity}segment(e){return this.#r===void 0?gr(e):gr(e,{adapter:this.#r})}async extractUserFocused(e){let r=this.#e.createChunkBatchContext(),n=await Gm(this.#t,{sentences:e.sentences,text:e.text,visibleChunkIds:r.visibleChunkIds,workingMemoryPrompt:r.workingMemoryPrompt});return{delta:await this.#e.acceptChunkBatch(n.chunkBatch),fragmentSummary:n.fragmentSummary}}async extractBookCoherence(e){let r=this.#e.createChunkBatchContext({includeCurrentFragment:!1}),n=[...r.visibleChunkIds,...e.userFocusedChunks.map(a=>a.id)],i=await Bm(this.#t,{sentences:e.sentences,text:e.text,userFocusedChunks:e.userFocusedChunks,visibleChunkIds:n,workingMemoryPrompt:r.workingMemoryPrompt});return await this.#e.acceptChunkBatch(i)}completeFragment(e){this.#e.completeFragment(e)}clear(){this.#e.clear()}}});var ac=d(()=>{"use strict";dk();qm()});var oc=d(()=>{"use strict"});async function lk(t,e){let r=t.listSentences===void 0?await FW(t):await t.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>e&&(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 mk(t){let e=[];for await(let r of t)e.push(r);return e.join("")}function pk(t){return t.reduce((e,r)=>e+r.wordsCount,0)}async function hk(t,e){return e.length===0?"":(await Promise.all(e.map(async n=>await t.getFragment(n)))).flatMap(n=>n.sentences.map(i=>i.text)).join(" ").trim()}async function FW(t){return(await Promise.all((await t.listFragmentIds()).map(async r=>await t.getFragment(r)))).sort((r,n)=>r.fragmentId-n.fragmentId).flatMap(r=>r.sentences)}var sc=d(()=>{"use strict";ac();oc()});var fk=d(()=>{"use strict";sc()});async function bk(t){let e=await t.document.snakes.listIdsByGroup(t.serialId,t.groupId),r=await Promise.all(e.map(async o=>await t.document.snakes.getById(o))),n=await Promise.all(r.filter(o=>o!==void 0).map(async o=>await HW(o,t.document)));if(n.length<=t.maxClues)return gk(n);let i=[...n],a=-1;for(;i.length>t.maxClues;){i.sort(Sk);let o=Math.floor(t.maxClues*.75),s=i.slice(o);if(s.length<2)break;let c=qW(s),[u,l]=c??[...i].sort(BW).slice(0,2);if(u===void 0||l===void 0)break;i=i.filter(m=>m!==u&&m!==l),i.push($W(u,l,a)),a-=1}return gk(i)}function GW(t,e){let r=yk(t.chunks),n=yk(e.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 yk(t){let e=Object.create(null),r=[];for(let n of t)for(let i of n.sentenceIds){let a=i[1],o=String(a);e[o]!==!0&&(e[o]=!0,r.push(a))}return r.sort(wk),r}function BW(t,e){return t.weight-e.weight}function Sk(t,e){return e.weight-t.weight}function UW(t,e){let[r,n]=t.sentenceId,[i,a]=e.sentenceId;return r!==i?r-i:n-a}function wk(t,e){return t-e}async function HW(t,e){let r=await e.snakeChunks.listChunkIds(t.id),n=(await Promise.all(r.map(async i=>await e.chunks.getById(i)))).filter(i=>i!==void 0);return{clueId:t.id,chunks:n,isMerged:!1,label:`${t.firstLabel} -> ${t.lastLabel}`,sourceSnakeIds:[t.id],weight:t.weight}}function qW(t){let e,r=-1;for(let n=0;n<t.length;n+=1){let i=t[n];if(i!==void 0)for(let a=n+1;a<t.length;a+=1){let o=t[a];if(o===void 0)continue;let s=GW(i,o);s<=r||(r=s,e=[i,o])}}return e}function $W(t,e,r){let n=[...t.sourceSnakeIds,...e.sourceSnakeIds].sort(wk),i=[...t.chunks,...e.chunks].sort(UW);return{clueId:r,chunks:i,isMerged:!0,label:`Merged minor clues (${n.length})`,sourceSnakeIds:n,weight:t.weight+e.weight}}function gk(t){let e=t.reduce((n,i)=>n+i.weight,0),r=t.map(n=>({...n,weight:e===0?0:n.weight/e}));return r.sort(Sk),r}var Ek=d(()=>{"use strict"});var kk,Ik,xk,Rk,cc=d(()=>{"use strict";kk="editor/clue_reviewer",Ik="editor/clue_reviewer_generator",xk="editor/revision_feedback",Rk="editor/text_compressor"});function KW(t,e,r){let n=[{content:t.systemPrompt,role:"system"},{content:t.markedText,role:"user"}];return e!==void 0&&r!==void 0&&n.push({content:`<final>${e}</final>`,role:"assistant"},{content:r,role:"user"}),n}function JW(t){let e=t.trim(),r=/^<final>([\s\S]*)<\/final>$/.exec(e);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 zW(t){return["Your previous response was invalid.",t instanceof Error?t.message:String(t),"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 $m,dc,vk=d(()=>{"use strict";cc();$m=2,dc=class{#e;#t;#r;#n;constructor(e,r,n,i){this.#e=n,this.#t=e,this.#r=r,this.#n=i}async request(e){let r=Math.floor(e.targetLength*.85),n=Math.floor(e.targetLength*1.15),i=this.#t.loadSystemPrompt(Rk,{acceptable_max:n,acceptable_min:r,compression_ratio:Math.floor(this.#e*100),original_length:e.markedText.length,target_length:e.targetLength,user_language:this.#n}),a=KW({markedText:e.markedText,systemPrompt:i},e.previousCompressedText,e.revisionFeedback);return await this.#t.request(async o=>{let s=a;for(let c=0;c<=$m;c+=1){let u=await o(s,{retryIndex:c,retryMax:$m,scope:this.#r,...c===0?{}:{useCache:!1}});try{return JW(u)}catch(l){if(c>=$m)throw l;s=[...s,{content:u,role:"assistant"},{content:zW(l),role:"user"}]}}throw new Error("Compression request failed unexpectedly")})}}});var _n,Bre,Tk,uc=d(()=>{"use strict";Yd();_n=(n=>(n.Critical="critical",n.Major="major",n.Minor="minor",n))(_n||{}),Bre=Fr(_n),Tk=ci(_n,"review severity")});function _k(t){let e=0;for(let r of t)for(let n of r.issues)e+=Km[n.severity]*r.weight;return e}function Ck(t){let e=Ak(t.reviews),r=e.slice(0,9),n=e.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)`),t.llm.loadSystemPrompt(xk,{issues_description:i.join(`
|
|
1790
|
-
`)})}function
|
|
1791
|
-
`;let
|
|
1792
|
-
`)}function
|
|
1793
|
-
`)}function
|
|
1988
|
+
`+e.candidates.map(DI).join(`
|
|
1989
|
+
`),role:"user"}]}}});var Od,tx=c(()=>{"use strict";ve();N();BI();Ja();qI();ex();Ad();Od=class{#e;#t;#n;#r;#a;#i;#o;constructor(e){this.#e=new Ld({choiceSystemPrompt:e.choiceSystemPrompt,projection:e.projection,requestChoice:e.requestChoice,responseIntentClassifierPrompt:e.responseIntentClassifierPrompt}),this.#t=e.metadataField,this.#r=e.sentenceTextSource,this.#o=e.visibleChunkIds,this.#a=LI(e.projection.sentences.map(n=>({sentenceId:n.sentenceId,wordsCount:n.wordsCount}))),this.#n=OI(e.projection),this.#i=e.validImportanceChunkIds===void 0?void 0:$a(e.validImportanceChunkIds)}async parse(e,n){let r=[],i=[],a=[],o="importance_annotations"in e?GI({annotations:e.importance_annotations,issues:r,validImportanceChunkIds:this.#i}):void 0,s="fragment_summary"in e?e.fragment_summary:void 0,d=NI(e.links);for(let[l,m]of e.chunks.entries()){let p=l+1,h=[],y=m.label.trim(),f=m.content.trim();y===""&&h.push(`Chunk #${p}: Missing or empty "label" field`),f===""&&h.push(`Chunk #${p}: Missing or empty "content" field`);let[I,v]=await this.#e.resolve({chunkIndex:p,chunkLabel:y,data:m,isLastGenerationAttempt:n.isLastGenerationAttempt});if(I.length===0&&(v!==void 0?h.push(`Chunk #${p} ("${y}"): ${v.message}`):h.push(`Chunk #${p} ("${y}"): Missing evidence`)),h.length>0){r.push(...h);continue}let x=I[0];if(x===void 0){r.push(`Chunk #${p} ("${y}"): Unable to resolve any sentence IDs`);continue}let J=I.reduce((G,_)=>G+(this.#a[qa(_)]??0),0);if(this.#t==="retention"){let G=m;i.push({content:f,generation:0,id:0,label:y,links:[],retention:Au(G.retention),sentenceId:x,sentenceIds:[...I],wordsCount:J})}else{let G=m;i.push({content:f,generation:0,id:0,importance:ar(G.importance),label:y,links:[],sentenceId:x,sentenceIds:[...I],wordsCount:J})}a.push(m.temp_id)}let u=HI({issues:r,links:d,tempIds:a,visibleChunkIds:this.#o});if(r.length>0)throw new M(r);return{chunkBatch:{chunks:i,links:u,orderCorrect:!0,tempIds:a,...o===void 0?{}:{importanceAnnotations:o}},...s===void 0?{}:{fragmentSummary:s}}}async getChunkSourceSentences(e){let n=[];for(let r of e.sentenceIds){let i=qa(r),a=this.#n[i];if(a!==void 0){n.push(a);continue}n.push(await this.#r.getSentence(r))}return n}}});var nx=c(()=>{"use strict";tx();Ad()});function za(t){let e="";for(let n of t)e+=jj[n]??n;return e}var jj,Kt,Nd=c(()=>{"use strict";jj={'"':"\uFF02","\\":"\uFF3C"},Kt=class{#e;#t;#n;constructor(e){let n=[],r="",i="",a=0;for(let[o,s]of e.entries()){o>0&&(r+=" ",i+=" ",a+=1);let d=za(s.text),u=a,l=u+d.length;r+=s.text,i+=d,n.push({endOffset:l,projectedText:d,rawText:s.text,sentenceId:s.sentenceId,startOffset:u,wordsCount:s.wordsCount}),a=l}this.#t=r,this.#e=i,this.#n=n}get projectedText(){return this.#e}get rawText(){return this.#t}get sentences(){return this.#n}findExactMatches(e){let n=za(e).trim();if(n==="")return[];let r=[],i=0;for(;i<=this.#e.length-n.length;){let a=this.#e.indexOf(n,i);if(a===-1)break;r.push({length:n.length,offset:a}),i=a+1}return r}projectText(e){return za(e)}resolveSentenceIds(e){if(e.length<=0)return[];let n=e.offset,r=e.offset+e.length;return this.#n.filter(i=>i.endOffset>n&&i.startOffset<r).map(i=>i.sentenceId)}}});import{detect as Gj,validateISO2 as rx}from"tinyld";function Dd(t){let e=t.trim();if(e!=="")try{return Bj(Gj(e))}catch{return}}function Md(t){return cu(t)}function Bj(t){let e=t.trim().toLowerCase(),n=rx(e);if(n!=="")return n;let r=e.split("-")[0];if(r===void 0)return;let i=rx(r);return i===""?void 0:i}var Cp=c(()=>{"use strict";nt()});function ax(t){let e=Md(t.targetLanguage);return ix(t.label,e)||ix(t.content,e)}function ix(t,e){if(t.trim()==="")return!1;let n=Dd(t);return n===void 0?!0:n!==e}var ox=c(()=>{"use strict";Cp();nt()});var sx,dx,cx,ux,lx=c(()=>{"use strict";sx="topologization/user_focused_extraction",dx="topologization/book_coherence_extraction",cx="topologization/evidence_choice",ux="topologization/translate_chunks"});import{z as Qa}from"zod";function mx(t){return t.sentences.map((e,n)=>`S${n+1}: ${za($t(e.rawText))}`).join(`
|
|
1990
|
+
`)}function Kj(t){return t instanceof wt&&t.cause instanceof M}function Jj(t){return Kj(t)||t instanceof Ie}function zj(t){let e=Object.create(null);for(let n of t)e[String(n.id)]=n;return e}function Qj(t,e){let n=[];e.length!==t.length&&n.push(`Expected ${t.length} translated chunk(s), got ${e.length}`);for(let[r,i]of t.entries()){let a=e[r];if(a===void 0){n.push(`Missing translated chunk at position ${r}`);continue}a.id!==i.id&&n.push(`Translated chunk at position ${r} must keep id ${i.id}, got ${a.id}`)}if(n.length>0)throw new M(n)}var Uj,Hj,qj,$j,Xa,px=c(()=>{"use strict";ve();zr();nx();Nd();Nd();ox();lx();Uj=7,Hj=Qa.object({content:Qa.string(),id:Qa.number(),label:Qa.string()}),qj=Qa.array(Hj),$j=3,Xa=class{#e;#t;#n;#r;#a;constructor(e){this.#e=e.extractionGuidance,this.#t=e.llm,this.#n=e.scopes,this.#r=e.sentenceTextSource,this.#a=e.userLanguage}async extractUserFocused(e){let n=new Kt(e.sentences),r=this.#i({promptTemplateName:sx,templateContext:{extraction_guidance:this.#e,evidence_selection_prompt:Ln,user_language:this.#a,working_memory:e.workingMemoryPrompt},text:mx(n)}),i=await this.#o({emptyChunkBatch:{chunks:[],links:[],orderCorrect:!0,tempIds:[]},messages:r,metadataField:"retention",projection:n,schema:VI,sentences:e.sentences,visibleChunkIds:e.visibleChunkIds});return{chunkBatch:await this.#s(i.result.chunkBatch,i.parser),fragmentSummary:i.result.fragmentSummary??""}}async extractBookCoherence(e){let n=new Kt(e.sentences),r=this.#i({promptTemplateName:dx,templateContext:{user_focused_chunks:e.userFocusedChunks.map(a=>({content:a.content,id:a.id,label:a.label})),evidence_selection_prompt:Ln,user_language:this.#a,working_memory:e.workingMemoryPrompt},text:mx(n)}),i=await this.#o({emptyChunkBatch:{chunks:[],importanceAnnotations:[],links:[],orderCorrect:!0,tempIds:[]},messages:r,metadataField:"importance",projection:n,schema:YI,sentences:e.sentences,validImportanceChunkIds:e.userFocusedChunks.map(a=>a.id),visibleChunkIds:e.visibleChunkIds});return await this.#s(i.result.chunkBatch,i.parser)}#i(e){return[{content:this.#t.loadSystemPrompt(e.promptTemplateName,e.templateContext),role:"system"},{content:e.text,role:"user"}]}async#o(e){return await this.#t.withContext(async n=>{let r=new Od({metadataField:e.metadataField,projection:e.projection,responseIntentClassifierPrompt:this.#t.loadSystemPrompt(ae),sentenceTextSource:this.#r,sentences:e.sentences,visibleChunkIds:e.visibleChunkIds,choiceSystemPrompt:this.#t.loadSystemPrompt(cx,{extraction_guidance:this.#e,metadata_field:e.metadataField,user_language:this.#a}),requestChoice:async(i,a,o)=>await n.request(i,{retryIndex:a,retryMax:o,scope:this.#n.choice,useCache:!1}),...e.validImportanceChunkIds===void 0?{}:{validImportanceChunkIds:e.validImportanceChunkIds}});try{let i=await Z({messages:e.messages,schema:e.schema,maxRetries:Uj,parse:async(a,o,s)=>await r.parse(a,{isLastGenerationAttempt:o>=s}),responseIntentClassifierPrompt:this.#t.loadSystemPrompt(ae),request:async(a,o,s)=>await n.request(a,{retryIndex:o,retryMax:s,scope:this.#n.extraction})});return{parser:r,result:i}}catch(i){if(Jj(i))return{parser:r,result:{chunkBatch:e.emptyChunkBatch}};throw i}})}async#s(e,n){if(this.#a===void 0||e.chunks.length===0)return e;let r=[];for(let[i,a]of e.chunks.entries())ax({content:a.content,label:a.label,targetLanguage:this.#a})&&r.push({content:a.content,id:i,label:a.label,sourceSentences:await n.getChunkSourceSentences(a)});if(r.length===0)return e;try{let i=await this.#d(r,this.#a),a=zj(i);return{...e,chunks:e.chunks.map((o,s)=>{let d=a[String(s)];return d===void 0?o:{...o,content:d.content,label:d.label}})}}catch{return e}}async#d(e,n){return await Z({messages:[{content:this.#t.loadSystemPrompt(ux,{user_language:n}),role:"system"},{content:JSON.stringify(e,void 0,2),role:"user"}],schema:qj,maxRetries:$j,parse:r=>(Qj(e,r),r),responseIntentClassifierPrompt:this.#t.loadSystemPrompt(ae),request:async(r,i,a)=>await this.#t.request(r,{retryIndex:i,retryMax:a,scope:this.#n.extraction})})}}});async function Pp(t,e){return await new Xa(t).extractUserFocused(e)}async function Ap(t,e){return await new Xa(t).extractBookCoherence(e)}var Lp=c(()=>{"use strict";px()});function hx(){return new Op}var Xj,Op,fx=c(()=>{"use strict";on();Xj=/(?:[.!?。!?…]+|(?:\r?\n)\s*(?:\r?\n)?|[.!?。!?…]+["'”’」』】)》〉〕〗]*)$/u,Op=class{#e=new Intl.Segmenter(void 0,{granularity:"sentence"});async*pipe(e){let n="",r=0;for await(let a of e){if(a==="")continue;n+=a;let{remainder:o,sentences:s}=this.#t(n,!1);n=o;for(let d of s)yield{offset:r,text:d,wordsCount:Ne(d)},r+=d.length}let{sentences:i}=this.#t(n,!0);for(let a of i)yield{offset:r,text:a,wordsCount:Ne(a)},r+=a.length}#t(e,n){if(e.trim()==="")return{remainder:"",sentences:[]};let r=Array.from(this.#e.segment(e));if(r.length===0)return{remainder:e,sentences:[]};if(n||this.#n(e))return{remainder:"",sentences:r.map(a=>a.segment.trim()).filter(a=>a!=="")};let i=r[r.length-1];return i===void 0?{remainder:e,sentences:[]}:{remainder:e.slice(i.index),sentences:r.slice(0,-1).map(a=>a.segment.trim()).filter(a=>a!=="")}}#n(e){return Xj.test(e.trimEnd())}}});function On(t,e){return(e?.adapter??hx()).pipe(t)}var Np=c(()=>{"use strict";fx()});var Va,gx=c(()=>{"use strict";nI();Lp();Np();Va=class{#e;#t;#n;constructor(e){this.#e=new vd(e.attention.capacity,e.attention.generationDecayFactor,e.attention.idGenerator),this.#t={extractionGuidance:e.extractionGuidance,llm:e.llm,scopes:e.scopes,sentenceTextSource:e.sentenceTextSource,...e.userLanguage===void 0?{}:{userLanguage:e.userLanguage}},this.#n=e.segmenter}get capacity(){return this.#e.capacity}segment(e){return this.#n===void 0?On(e):On(e,{adapter:this.#n})}async extractUserFocused(e){let n=this.#e.createChunkBatchContext(),r=await Pp(this.#t,{sentences:e.sentences,text:e.text,visibleChunkIds:n.visibleChunkIds,workingMemoryPrompt:n.workingMemoryPrompt});return{delta:await this.#e.acceptChunkBatch(r.chunkBatch),fragmentSummary:r.fragmentSummary}}async extractBookCoherence(e){let n=this.#e.createChunkBatchContext({includeCurrentFragment:!1}),r=[...n.visibleChunkIds,...e.userFocusedChunks.map(a=>a.id)],i=await Ap(this.#t,{sentences:e.sentences,text:e.text,userFocusedChunks:e.userFocusedChunks,visibleChunkIds:r,workingMemoryPrompt:n.workingMemoryPrompt});return await this.#e.acceptChunkBatch(i)}completeFragment(e){this.#e.completeFragment(e)}clear(){this.#e.clear()}}});var Wd=c(()=>{"use strict";gx();Np()});var Fd=c(()=>{"use strict"});async function bx(t,e){let n=t.listSentences===void 0?await Vj(t):await t.listSentences(),r=[],i=[],a=0,o=0;for(let s=0;s<n.length;s+=1){let d=n[s];d===void 0||d.text.trim()===""||(i.length>0&&o+d.wordsCount>e&&(r.push({startSentenceIndex:a,sentences:i}),i=[],o=0),i.length===0&&(a=s),i.push(d),o+=d.wordsCount)}return i.length>0&&r.push({startSentenceIndex:a,sentences:i}),r}async function Sx(t){let e=[];for await(let n of t)e.push(n);return e.join("")}function wx(t){return t.reduce((e,n)=>e+n.wordsCount,0)}async function Ex(t,e){return e.length===0?"":(await Promise.all(e.map(async r=>await t.getFragment(r)))).flatMap(r=>r.sentences.map(i=>i.text)).join(" ").trim()}async function Vj(t){return(await Promise.all((await t.listFragmentIds()).map(async n=>await t.getFragment(n)))).sort((n,r)=>n.fragmentId-r.fragmentId).flatMap(n=>n.sentences)}var jd=c(()=>{"use strict";Wd();Fd()});var Ix=c(()=>{"use strict";jd()});async function Rx(t){let e=await t.document.snakes.listIdsByGroup(t.serialId,t.groupId),n=await Promise.all(e.map(async o=>await t.document.snakes.getById(o))),r=await Promise.all(n.filter(o=>o!==void 0).map(async o=>await nG(o,t.document)));if(r.length<=t.maxClues)return kx(r);let i=[...r],a=-1;for(;i.length>t.maxClues;){i.sort(vx);let o=Math.floor(t.maxClues*.75),s=i.slice(o);if(s.length<2)break;let d=rG(s),[u,l]=d??[...i].sort(eG).slice(0,2);if(u===void 0||l===void 0)break;i=i.filter(m=>m!==u&&m!==l),i.push(iG(u,l,a)),a-=1}return kx(i)}function Zj(t,e){let n=xx(t.chunks),r=xx(e.chunks),i=Object.create(null),a=0;for(let o of n)i[String(o)]=!0,a+=1;for(let o of r){let s=String(o);i[s]!==!0&&(i[s]=!0,a+=1)}return n.length+r.length-a}function xx(t){let e=Object.create(null),n=[];for(let r of t)for(let i of r.sentenceIds){let a=i[1],o=String(a);e[o]!==!0&&(e[o]=!0,n.push(a))}return n.sort(Tx),n}function eG(t,e){return t.weight-e.weight}function vx(t,e){return e.weight-t.weight}function tG(t,e){let[n,r]=t.sentenceId,[i,a]=e.sentenceId;return n!==i?n-i:r-a}function Tx(t,e){return t-e}async function nG(t,e){let n=await e.snakeChunks.listChunkIds(t.id),r=(await Promise.all(n.map(async i=>await e.chunks.getById(i)))).filter(i=>i!==void 0);return{clueId:t.id,chunks:r,isMerged:!1,label:`${t.firstLabel} -> ${t.lastLabel}`,sourceSnakeIds:[t.id],weight:t.weight}}function rG(t){let e,n=-1;for(let r=0;r<t.length;r+=1){let i=t[r];if(i!==void 0)for(let a=r+1;a<t.length;a+=1){let o=t[a];if(o===void 0)continue;let s=Zj(i,o);s<=n||(n=s,e=[i,o])}}return e}function iG(t,e,n){let r=[...t.sourceSnakeIds,...e.sourceSnakeIds].sort(Tx),i=[...t.chunks,...e.chunks].sort(tG);return{clueId:n,chunks:i,isMerged:!0,label:`Merged minor clues (${r.length})`,sourceSnakeIds:r,weight:t.weight+e.weight}}function kx(t){let e=t.reduce((r,i)=>r+i.weight,0),n=t.map(r=>({...r,weight:e===0?0:r.weight/e}));return n.sort(vx),n}var _x=c(()=>{"use strict"});var Cx,Px,Ax,Lx,Gd=c(()=>{"use strict";Cx="editor/clue_reviewer",Px="editor/clue_reviewer_generator",Ax="editor/revision_feedback",Lx="editor/text_compressor"});function aG(t,e,n){let r=[{content:t.systemPrompt,role:"system"},{content:t.markedText,role:"user"}];return e!==void 0&&n!==void 0&&r.push({content:`<final>${e}</final>`,role:"assistant"},{content:n,role:"user"}),r}function oG(t){let e=t.trim(),n=/^<final>([\s\S]*)<\/final>$/.exec(e);if(n===null)throw new Error("Compression response must be exactly one <final>...</final> block.");let r=n[1]?.trim();if(r===void 0||r==="")throw new Error("Compression response contained an empty <final> block.");if(/<\/?[A-Za-z][^>]*>/.test(r))throw new Error("Compressed text must not contain XML or HTML tags.");return r}function sG(t){return["Your previous response was invalid.",t instanceof Error?t.message:String(t),"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 Dp,Bd,Ox=c(()=>{"use strict";Gd();Dp=2,Bd=class{#e;#t;#n;#r;constructor(e,n,r,i){this.#e=r,this.#t=e,this.#n=n,this.#r=i}async request(e){let n=Math.floor(e.targetLength*.85),r=Math.floor(e.targetLength*1.15),i=this.#t.loadSystemPrompt(Lx,{acceptable_max:r,acceptable_min:n,compression_ratio:Math.floor(this.#e*100),original_length:e.markedText.length,target_length:e.targetLength,user_language:this.#r}),a=aG({markedText:e.markedText,systemPrompt:i},e.previousCompressedText,e.revisionFeedback);return await this.#t.request(async o=>{let s=a;for(let d=0;d<=Dp;d+=1){let u=await o(s,{retryIndex:d,retryMax:Dp,scope:this.#n,...d===0?{}:{useCache:!1}});try{return oG(u)}catch(l){if(d>=Dp)throw l;s=[...s,{content:u,role:"assistant"},{content:sG(l),role:"user"}]}}throw new Error("Compression request failed unexpectedly")})}}});var Qr,Zae,Nx,Ud=c(()=>{"use strict";Cu();Qr=(r=>(r.Critical="critical",r.Major="major",r.Minor="minor",r))(Qr||{}),Zae=ir(Qr),Nx=_i(Qr,"review severity")});function Dx(t){let e=0;for(let n of t)for(let r of n.issues)e+=Mp[r.severity]*n.weight;return e}function Mx(t){let e=jx(t.reviews),n=e.slice(0,9),r=e.length-n.length,i=[];for(let a=0;a<n.length;a+=1){let o=n[a];o!==void 0&&(i.push(`${a+1}. [${o.severity.toUpperCase()}]`,` Problem: ${o.problem}`),o.suggestion!==""&&i.push(` Suggestion: ${o.suggestion}`),i.push(""))}return r>0&&i.push(`... and ${r} more issues hidden (lower priority)`),t.llm.loadSystemPrompt(Ax,{issues_description:i.join(`
|
|
1991
|
+
`)})}function Wx(t){let e=jx(t);if(e.length===0)return`No issues found - all reviewers are satisfied.
|
|
1992
|
+
`;let n=[];for(let r=0;r<e.length;r+=1){let i=e[r];i!==void 0&&(n.push(`${r+1}. [${i.severity.toUpperCase()}]`,` Problem: ${i.problem}`),i.suggestion!==""&&n.push(` Suggestion: ${i.suggestion}`),n.push(""))}return n.join(`
|
|
1993
|
+
`)}function Fx(t){let e=t.reduce((n,r)=>n===void 0||r.score<n.score?r:n,void 0);if(e===void 0)throw new Error("Compression failed: no versions generated");return e}function jx(t){let e=t.flatMap(n=>n.issues.map(r=>({...r,weight:n.weight})));return e.sort((n,r)=>{let i=Mp[r.severity]-Mp[n.severity];return i!==0?i:r.weight-n.weight}),e}var Mp,Wp=c(()=>{"use strict";Gd();Ud();Mp=Object.freeze({critical:9,major:3,minor:1})});function Gx(t){if(t.userLanguage===void 0)return;let e=Md(t.userLanguage),n=Dd(t.compressedText);if(!(n===void 0||n===e))return{detectedLanguageCode:n,review:{clueId:-1,issues:[{problem:`Output language error: detected ${n}, but ${e} (${t.userLanguage}) is required.`,severity:"critical",suggestion:`Please translate the entire compressed text to ${t.userLanguage}. Maintain all information integrity and ensure the translation sounds natural and native, not machine-translated.`}],weight:1},targetLanguageCode:e}}var Bx=c(()=>{"use strict";Cp();nt();Ud()});import{appendFile as Hd,writeFile as dG}from"fs/promises";function cG(t,e,n){let r=["=".repeat(80),`CHUNK HIERARCHY - Serial ${n}, Group ${e}`,"=".repeat(80),""];for(let i=0;i<t.length;i+=1){let a=t[i];if(a!==void 0){r.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 d=s.content.length>60?`${s.content.slice(0,60)}...`:s.content;r.push(` - Chunk ${o+1}/${a.chunks.length} (ID: ${s.id})`,` Label: ${s.label}`,` Retention: ${s.retention??"N/A"}`,` Importance: ${s.importance??"N/A"}`,` Content: ${d}`,"")}}}return r.push("=".repeat(80),""),r.join(`
|
|
1994
|
+
`)}function uG(t){let e=t.getFullYear(),n=String(t.getMonth()+1).padStart(2,"0"),r=String(t.getDate()).padStart(2,"0"),i=String(t.getHours()).padStart(2,"0"),a=String(t.getMinutes()).padStart(2,"0"),o=String(t.getSeconds()).padStart(2,"0");return`${e}-${n}-${r} ${i}-${a}-${o}`}var qd,Ux=c(()=>{"use strict";Pe();Wp();qd=class{#e;#t;#n;#r;#a;#i;constructor(e,n,r){this.#e=r.compressionRatio,this.#t=n,this.#n=r.logDirPath,this.#r=r.maxIterations,this.#a=e}async appendCompressionResult(e){if(this.#i===void 0)return;let n=[];e.thinkingText!==""&&n.push("Thinking:","-".repeat(80),e.thinkingText,"-".repeat(80),""),n.push(`Compressed Text (${e.compressedText.length} characters):`,"-".repeat(80),e.compressedText,"-".repeat(80),"",""),await Hd(this.#i,`${n.join(`
|
|
1794
1995
|
`)}
|
|
1795
|
-
`,"utf8")}async appendFinalSelection(e,
|
|
1996
|
+
`,"utf8")}async appendFinalSelection(e,n){if(this.#i===void 0)return;let r=["","=".repeat(80),"FINAL SELECTION","=".repeat(80),"",`Selected: Iteration ${e.iteration}/${this.#r}`,`Score: ${e.score.toFixed(2)}`,`Length: ${e.text.length} characters`,`Compression ratio: ${(e.text.length/n).toFixed(1)}%`,"","=".repeat(80),""];e.score>0&&r.push("REMAINING UNRESOLVED ISSUES","=".repeat(80),"",Wx(e.reviews),"=".repeat(80),""),await Hd(this.#i,`${r.join(`
|
|
1796
1997
|
`)}
|
|
1797
|
-
`,"utf8")}async appendIterationHeader(e,
|
|
1998
|
+
`,"utf8")}async appendIterationHeader(e,n){if(this.#i===void 0)return;let r=["","=".repeat(80),`ITERATION ${e}/${this.#r}`,"=".repeat(80),""];n!==void 0&&n.trim()!==""&&r.push("Revision Feedback:","-".repeat(80),n,"-".repeat(80),""),await Hd(this.#i,`${r.join(`
|
|
1798
1999
|
`)}
|
|
1799
|
-
`,"utf8")}async appendLanguageMismatch(e){if(this.#
|
|
2000
|
+
`,"utf8")}async appendLanguageMismatch(e){if(this.#i===void 0)return;let n=e.review.issues[0];if(n===void 0)return;let r=["","!".repeat(80),"LANGUAGE MISMATCH DETECTED","!".repeat(80),`Expected: ${e.targetLanguageCode} (${e.userLanguage??"unknown"})`,`Detected: ${e.detectedLanguageCode}`,`Issue: ${n.problem}`,`Suggestion: ${n.suggestion}`,"!".repeat(80),""];await Hd(this.#i,`${r.join(`
|
|
1800
2001
|
`)}
|
|
1801
|
-
`,"utf8")}async initialize(e){if(this.#
|
|
1802
|
-
`),"utf8")}}});async function
|
|
2002
|
+
`,"utf8")}async initialize(e){if(this.#n===void 0)return;let n=uG(new Date);this.#i=Yo({category:"editor",logDirPath:this.#n,prefix:`compression-serial-${this.#a}-group-${this.#t}`}),this.#i!==void 0&&await dG(this.#i,["=== Text Compression Log ===",`Serial: ${this.#a}, Group: ${this.#t}`,`Started at: ${n}`,`Compression ratio target: ${Math.round(this.#e*100)}%`,`Max iterations: ${this.#r}`,"","",cG(e,this.#t,this.#a),""].join(`
|
|
2003
|
+
`),"utf8")}}});async function $x(t){return await jp({chunks:t.chunks,segmentStartIndexes:await fG(t.chunks,t.serialFragments),serialFragments:t.serialFragments,...t.fullMarkup===void 0?{}:{fullMarkup:t.fullMarkup},...t.wrapHighRetention===void 0?{}:{wrapHighRetention:t.wrapHighRetention}})}async function jp(t){if(t.segmentStartIndexes.length===0)return"";let e=pG(t.chunks),n=[...t.segmentStartIndexes].sort(Ya),r=[...await t.serialFragments.listFragmentIds()].sort(Ya),i=await yG(hG(n,r),t.serialFragments),a=[];for(let o=0;o<n.length;o+=1){let s=n[o];if(s===void 0)continue;let d=i[String(s)];if(d!==void 0){if(o>0){let u=n[o-1];if(u!==void 0&&s>u){let l=mG({fragments:i,skippedStartIndexes:Kx(r,u,s)});l!==""&&a.push(l)}}a.push(lG({chunkCoverage:e,fragment:d,fullMarkup:t.fullMarkup??!1,wrapHighRetention:t.wrapHighRetention??!1}))}}return a.join(`
|
|
1803
2004
|
|
|
1804
|
-
`)}function YW(t){let e=[],{fragment:r}=t,n=0;for(;n<r.sentences.length;){if(r.sentences[n]===void 0){n+=1;continue}let o=zm(r.serialId,r.fragmentId+n),s=t.chunkCoverage[o];if(s===void 0){e.push({endIndex:n+1,startIndex:n}),n+=1;continue}let c=Mk(s),u=n+1;for(;u<r.sentences.length;){let l=zm(r.serialId,r.fragmentId+u),m=t.chunkCoverage[l];if(m===void 0||Mk(m).label!==c.label)break;u+=1}e.push({chunkAttributes:c,endIndex:u,startIndex:n}),n=u}return e.map(a=>aF({fragment:r,fullMarkup:t.fullMarkup,range:a,wrapHighRetention:t.wrapHighRetention})).join(" ")}function ZW(t){let e=[];for(let r of t.skippedStartIndexes){let n=t.fragments[String(r)]?.summary?.trim()??"";n!==""&&e.push(n)}return e.join(" ")}function eF(t){let e=Object.create(null);for(let r of t)for(let n of r.sentenceIds){let i=zm(...n),a=e[i];e[i]=a===void 0?[r]:[...a,r]}return e}function zm(t,e){return`${t}:${e}`}function tF(t,e){let r=new Set(t);for(let n=1;n<t.length;n+=1){let i=t[n-1],a=t[n];if(!(i===void 0||a===void 0))for(let o of jk(e,i,a))r.add(o)}return[...r].sort(va)}function jk(t,e,r){return t.filter(n=>n>e&&n<r)}async function rF(t,e){let r=[...await e.listFragmentIds()].sort(va),n=Object.create(null),i=[];for(let a of t)for(let o of a.sentenceIds){let s=nF(r,o[1]);if(s===void 0)continue;let c=String(s);n[c]!==!0&&(n[c]=!0,i.push(s))}return i.sort(va),i}function nF(t,e){let r;for(let n of t){if(n>e)break;r=n}return r}async function iF(t,e){let r=Object.create(null);return await Promise.all(t.map(async n=>{r[String(n)]=await e.getFragment(n)})),r}function va(t,e){return t-e}function Mk(t){let e=Wk({detailed:3,focused:2,relevant:1,verbatim:4}),r=Wk({critical:3,helpful:1,important:2}),n=t[0],i=t[0];for(let o of t)Cn(o.retention,e)>Cn(n?.retention,e)&&(n=o),Cn(o.importance,r)>Cn(i?.importance,r)&&(i=o);return{label:(Cn(n?.retention,e)>=Cn(i?.importance,r)?n:i)?.label??"",...i?.importance===void 0?{}:{importance:i.importance},...n?.retention===void 0?{}:{retention:n.retention}}}function Wk(t){return Object.freeze(t)}function Cn(t,e){return t===void 0?0:e[t]??0}function aF(t){let e=t.fragment.sentences.slice(t.range.startIndex,t.range.endIndex).map(n=>n.text).join(" "),r=t.range.chunkAttributes;if(r===void 0)return e;if(t.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(" ")}>${e}</chunk>`}return t.wrapHighRetention&&(r.retention==="verbatim"||r.retention==="detailed")?`<chunk retention="${r.retention}">${e}</chunk>`:e}var Xm=d(()=>{"use strict";L()});function Bk(t){return t.replace(/<chunk(?:\s+[^>]*)?>/g,"").replace(/<\/chunk>/g,"")}function Uk(t){let e=t.match(/##\s*(?:Compressed\s+Text|压缩文本)\s*\n+(.*?)(?:\n+---|\*\*CRITICAL\*\*|$)/is);return e?.[1]!==void 0?Gk(e[1].trim()):Gk(t.trim())}function Hk(t){let e=t.match(/##\s*(?:Compressed\s+Text|压缩文本)\s*/i);return e?.index===void 0?"":t.slice(0,e.index).trim()}function Gk(t){return t.match(/^```(?:\w+)?\s*\n([\s\S]*?)\n```$/)?.[1]?.trim()??t}var qk=d(()=>{"use strict"});import{z as Pn}from"zod";function dF(t,e){return e===void 0?[{content:t.systemPrompt,role:"system"},{content:t.compressedText,role:"user"}]:[{content:t.systemPrompt,role:"system"},{content:e[0],role:"user"},{content:e[1],role:"assistant"},{content:t.compressedText,role:"user"}]}var oF,sF,cF,pc,$k=d(()=>{"use strict";_e();Ie();cc();Xm();uc();oF=["critical","major","minor"],sF=Pn.object({problem:Pn.string(),severity:Pn.enum(oF),suggestion:Pn.string().default("")}),cF=Pn.object({issues:Pn.array(sF)}),pc=class{#e;#t;#r;#n;#i;constructor(e,r,n,i){this.#e=e,this.#t=n.reviewGuide,this.#r=n.review,this.#n=r,this.#i=i}async generateClueReviewers(e){return await Promise.all(e.map(async r=>await this.#e.request(async n=>{let i=await Fk({chunks:r.chunks,fullMarkup:!0,serialFragments:this.#n}),a=[{content:this.#e.loadSystemPrompt(Ik),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(e,r,n){let i=await Promise.all(r.map(async o=>await this.#e.request(async s=>{let c=this.#e.loadSystemPrompt(kk,{thread_info:o.reviewerInfo,user_language:this.#i}),u=n[String(o.clueId)],l=dF({compressedText:e,systemPrompt:c},u);try{return await z({messages:l,parse:m=>({rawResponse:JSON.stringify(m),review:{clueId:o.clueId,issues:m.issues.map(p=>({...p,severity:Tk(p.severity)})),weight:o.weight}}),responseIntentClassifierPrompt:this.#e.loadSystemPrompt(ee),request:async(m,p,h)=>await s(m,{retryIndex:p,retryMax:h,scope:this.#r,useCache:!1}),schema:cF})}catch(m){if(!(m instanceof we))throw m;return Te({clueId:o.clueId,component:"editor-review"}).warn({error:m},"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 Ta(t){return await new Ym(t).run()}function Vm(t,e){return t-e}function uF(t){return t.flatMap(e=>e.chunks)}function lF(t){let e=t.document??t.workspace;if(e===void 0)throw new Error("Editor requires a document");return e}var Ym,Kk=d(()=>{"use strict";Ek();vk();Jm();Nk();Dk();Xm();qk();$k();Ym=class{#e;#t;#r;#n;#i;#a;#o;#s;#c;#l;#u;#d;#p;constructor(e){let r=lF(e);this.#e=e.compressionRatio??.2,this.#t=e.groupId,this.#r=e.llm,this.#i=e.maxClues??10,this.#a=e.maxIterations??5,this.#c=e.scopes,this.#l=r.getSerialFragments(e.serialId),this.#u=e.serialId,this.#d=e.userLanguage,this.#p=r,this.#o=new dc(this.#r,e.scopes.compress,this.#e,this.#d),this.#n=new mc(this.#u,this.#t,{compressionRatio:this.#e,maxIterations:this.#a,...e.logDirPath===void 0?{}:{logDirPath:e.logDirPath}}),this.#s=new pc(this.#r,this.#l,{review:this.#c.review,reviewGuide:this.#c.reviewGuide},this.#d)}async run(){let e=await this.#m(),r=await this.#h(e);if(r.length===0)return"";let n=await bk({document:this.#p,groupId:this.#t,maxClues:this.#i,serialId:this.#u}),i=await this.#f(e);if(i.trim()==="")return"";await this.#n.initialize(n);let a=await Qm({chunks:uF(n),segmentStartIndexes:r,serialFragments:this.#l,wrapHighRetention:!0}),o=await this.#s.generateClueReviewers(n),s=Math.floor(i.length*this.#e),c=[],u=Object.create(null),l,m;for(let h=1;h<=this.#a;h+=1){await this.#n.appendIterationHeader(h,m);let g=await this.#o.request({markedText:a,targetLength:s,...l===void 0?{}:{previousCompressedText:l},...m===void 0?{}:{revisionFeedback:m}}),f=Bk(Uk(g)),E=Hk(g);await this.#n.appendCompressionResult({compressedText:f,thinkingText:E});let v=await this.#s.reviewCompression(f,o,u),I=[...v.reviews],q=Ok({compressedText:f,...this.#d===void 0?{}:{userLanguage:this.#d}});q!==void 0&&(I.push(q.review),await this.#n.appendLanguageMismatch({detectedLanguageCode:q.detectedLanguageCode,review:q.review,targetLanguageCode:q.targetLanguageCode,userLanguage:this.#d}));let F=_k(I);if(c.push({iteration:h,reviews:I,score:F,text:f}),F===0)break;if(!(h>=this.#a)){m=Ck({llm:this.#r,reviews:I}),l=f;for(let _ of Object.keys(v.rawResponses)){let bt=v.rawResponses[_];bt!==void 0&&(u[_]=[f,bt])}}}let p=Lk(c);return await this.#n.appendFinalSelection(p,i.length),p.text}async#m(){return(await this.#p.fragmentGroups.listBySerial(this.#u)).filter(e=>e.groupId===this.#t).map(e=>({endSentenceIndex:e.endSentenceIndex,startSentenceIndex:e.startSentenceIndex}))}async#h(e){let r=[...await this.#l.listFragmentIds()].sort(Vm),n=new Set;for(let i of e)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(Vm)}async#f(e){return(await Promise.all(e.map(async n=>({sentences:await this.#b(n.startSentenceIndex,n.endSentenceIndex)})))).flatMap(n=>n.sentences.map(i=>i.text)).join(" ")}async#b(e,r){if(this.#l.listSentencesInRange!==void 0)return await this.#l.listSentencesInRange(e,r);let n=[...await this.#l.listFragmentIds()].sort(Vm);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>=e}).map(async a=>await this.#l.getFragment(a)))).flatMap(a=>a.sentences.filter((o,s)=>{let c=a.fragmentId+s;return c>=e&&c<=r}))}}});var Zm=d(()=>{"use strict";Kk()});async function Qk(t){let e=await mF(t.fragments);return hF(pF({chunks:t.chunks,edges:t.edges,segmentWordsCounts:e}))}async function mF(t){let e=Vk(),r=await t.listFragmentIds();for(let n of r){let i=await t.getFragment(n);e[String(n)]=i.sentences.reduce((a,o)=>a+o.wordsCount,0)}return e}function pF(t){let e=Object.keys(t.segmentWordsCounts).map(Number).sort(Xk),r=gF(),n=Vk(),i=yF();for(let o of t.chunks){let s=Jk(e,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 t.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=fF();for(let o of t.chunks)a[String(o.id)]=o;return e.filter(o=>r[String(o)]!==void 0).map(o=>{let s=r[String(o)]??[],c=0,u=0;for(let l of s){let m=n[String(l)]??0,h=(i[String(l)]??[]).map(f=>({otherChunkId:f.fromId===l?f.toId:f.fromId,weight:f.weight})).map(f=>{let E=a[String(f.otherChunkId)];if(E!==void 0)return{otherSegmentStartIndex:Jk(e,E.sentenceId[1]),weight:f.weight}}).filter(f=>f!==void 0&&f.otherSegmentStartIndex!==void 0&&f.otherSegmentStartIndex!==o);if(h.length===0)continue;let g=h.reduce((f,E)=>f+E.weight,0);if(g!==0)for(let f of h){let E=m/g*f.weight;if(f.otherSegmentStartIndex<o){u+=E;continue}c+=E}}return{endIncision:c,startSentenceIndex:o,startIncision:u,wordsCount:t.segmentWordsCounts[String(o)]??0}})}function Jk(t,e){let r;for(let n of t){if(n>e)break;r=n}return r}function hF(t){let e=t.flatMap(l=>[l.startIncision,l.endIncision]).filter(l=>l>0).sort(Xk);if(e.length===0)return[...t];let r=Math.min(Math.floor(e.length*.98),e.length-1),n=e[r]??0,i=e.filter(l=>l<n);if(i.length===0)return t.map(l=>({...l,endIncision:l.endIncision>0?10:0,startIncision:l.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,u=c-s===0?1:c-s;return t.map(l=>({...l,endIncision:zk(l.endIncision,{logRange:u,minLog:s,threshold:n}),startIncision:zk(l.startIncision,{logRange:u,minLog:s,threshold:n})}))}function zk(t,e){if(t===0)return 0;if(t>=e.threshold)return 10;let r=t>0?Math.log(t):e.minLog;return Math.max(1,Math.min(10,Math.round(1+(r-e.minLog)/e.logRange*9)))}function Xk(t,e){return t-e}function fF(){return Object.create(null)}function yF(){return Object.create(null)}function gF(){return Object.create(null)}function Vk(){return Object.create(null)}var Yk=d(()=>{"use strict"});function eI(t){if(t.segmentInfos.length===0)return[];let e=t.segmentInfos.map(n=>({count:n.wordsCount,endIncision:n.endIncision,startSentenceIndex:n.startSentenceIndex,startIncision:n.startIncision}));return bF(e,t.groupWordsCount).map((n,i)=>({endSentenceIndex:Math.max(...n),groupId:i,serialId:t.serialId,startSentenceIndex:Math.min(...n)}))}function bF(t,e){return ep(t,e).map(r=>r.map(n=>n.startSentenceIndex))}function ep(t,e){if(t.length===0)return[];if(e<=0)return t.map(o=>[o]);let r=t.reduce((o,s)=>o+s.count,0);if(t.length===1||r<=e)return[[...t]];let n=SF(t,e);if(n===void 0)return Zk(t,e);let i=t.slice(0,n+1),a=t.slice(n+1);return i.length===0||a.length===0||i.length===t.length||a.length===t.length?Zk(t,e):[...ep(i,e),...ep(a,e)]}function SF(t,e){let r=t.reduce((c,u)=>c+u.count,0),n=0,i,a=0,o=Number.POSITIVE_INFINITY,s=Number.POSITIVE_INFINITY;for(let c=0;c<t.length-1;c+=1){let u=t[c],l=t[c+1];if(u===void 0||l===void 0)continue;n+=u.count;let m=r-n,p=u.endIncision+l.startIncision;if(p<=0)continue;let h=+(n>e)+ +(m>e),g=Math.max(0,n-e)+Math.max(0,m-e);(h<o||h===o&&p>a||h===o&&p===a&&g<s)&&(a=p,i=c,o=h,s=g)}return i}function Zk(t,e){let r=[],n=[],i=0;for(let a of t)n.length>0&&i+a.count>e&&(r.push(n),n=[],i=0),n.push(a),i+=a.count,i>=e&&(r.push(n),n=[],i=0);return n.length>0&&r.push(n),r}var tI=d(()=>{"use strict"});async function rI(t){let e=await Qk({chunks:t.chunks,edges:t.edges,fragments:t.fragments});return eI({segmentInfos:e,groupWordsCount:t.groupWordsCount,serialId:t.serialId})}var nI=d(()=>{"use strict";Yk();tI()});function tp(t,e){let[r,n]=t.sentenceId,[i,a]=e.sentenceId;return r!==i?r-i:n!==a?n-a:t.id-e.id}function iI(t,e){return t.value!==e.value?e.value-t.value:t.leftClusterId!==e.leftClusterId?t.leftClusterId-e.leftClusterId:t.rightClusterId-e.rightClusterId}function _a(t,e){return t-e}var Ca=d(()=>{"use strict"});function oI(t){let e=rp();for(let r of t)e[String(r.id)]=xF(r);return e}function sI(t){let e=rp();for(let n of t.edges){let i=aI(n.strength);e[String(n.fromId)]=(e[String(n.fromId)]??0)+i,e[String(n.toId)]=(e[String(n.toId)]??0)+i}let r=rp();for(let n of t.edges){let i=aI(n.strength),a=t.chunkWeights[String(n.fromId)]??0,o=t.chunkWeights[String(n.toId)]??0,s=e[String(n.fromId)]??0,c=e[String(n.toId)]??0,u=s===0?0:a*(i/s),l=c===0?0:o*(i/c);r[D(n.fromId,n.toId)]=Math.max(u+l,wF)}return r}function D(t,e){return`${t}:${e}`}function xF(t){return vF(t.retention)+RF(t.importance)}function RF(t){return t===void 0?0:EF[t]??0}function aI(t){return t===void 0?1:kF[t]??1}function vF(t){return t===void 0?0:IF[t]??0}function rp(){return Object.create(null)}var wF,EF,kF,IF,Ln=d(()=>{"use strict";L();wF=.1,EF={critical:9,helpful:1,important:3},kF={critical:9,helpful:1,important:3},IF={detailed:9,focused:3,relevant:1,verbatim:27}});var An=d(()=>{"use strict"});function Gt(){return Object.create(null)}function cI(){return Object.create(null)}function dI(){return Object.create(null)}function hc(){return Object.create(null)}function uI(){return Object.create(null)}function On(){return Object.create(null)}function lI(){return Object.create(null)}function np(){return Object.create(null)}var Pa=d(()=>{"use strict"});function La(t){switch(t){case"critical":return 9;case"important":return 3;case"helpful":return 1;default:return 1}}var ip=d(()=>{"use strict";An()});function Aa(t,e,r){let n=t[String(e)]??[];n.includes(r)||(t[String(e)]=[...n,r])}function mI(t,e){let r=cI(),n=dI(),i=np(),a=np(),o=[...t].map(c=>c.id).sort(_a),s=hc();for(let c of t)r[String(c.id)]=c;for(let c of e){if(r[String(c.fromId)]===void 0||r[String(c.toId)]===void 0)continue;let u=D(c.fromId,c.toId);n[u]=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),Aa(s,c.fromId,c.toId),Aa(s,c.toId,c.fromId)}return{chunksById:r,edgeByKey:n,fingerprintEdgeWeightsByKey:_F(r,e),incomingEdgesByChunkId:i,outgoingEdgesByChunkId:a,sortedChunkIds:o,undirectedAdjacentChunkIdsByChunkId:s}}function _F(t,e){let r=On();for(let i of e){if(t[String(i.fromId)]===void 0||t[String(i.toId)]===void 0)continue;let a=La(i.strength);r[String(i.fromId)]=(r[String(i.fromId)]??0)+a,r[String(i.toId)]=(r[String(i.toId)]??0)+a}let n=On();for(let i of e){let a=t[String(i.fromId)],o=t[String(i.toId)];if(a===void 0||o===void 0)continue;let s=La(i.strength),c=r[String(i.fromId)]??0,u=r[String(i.toId)]??0,l=c===0?0:a.weight*(s/c),m=u===0?0:o.weight*(s/u),p=Math.max(l+m,.1);n[D(i.fromId,i.toId)]=p,n[D(i.toId,i.fromId)]=p}return n}var ap=d(()=>{"use strict";Ln();An();Ca();Pa();ip()});function op(t){let e=[...t.chunkIds].sort(_a),r=Gt(),n=hc(),i=Gt(),a=[];for(let o of e)r[String(o)]=!0;for(let o of t.edges)r[String(o.fromId)]!==!0||r[String(o.toId)]!==!0||(Aa(n,o.fromId,o.toId),Aa(n,o.toId,o.fromId));for(let o of e){if(i[String(o)]===!0)continue;let s=[o],c=[];for(i[String(o)]=!0;s.length>0;){let u=s.pop();if(u!==void 0){c.push(u);for(let l of n[String(u)]??[])i[String(l)]!==!0&&(i[String(l)]=!0,s.push(l))}}c.sort(_a),a.push(c)}return a.sort((o,s)=>{let c=o[0]??Number.POSITIVE_INFINITY,u=s[0]??Number.POSITIVE_INFINITY;return c-u}),a}function pI(t){let e=t.sortedChunkIds[0];if(e===void 0)return!0;let r=Gt(),n=[e];for(r[String(e)]=!0;n.length>0;){let i=n.pop();if(i!==void 0)for(let a of t.undirectedAdjacentChunkIdsByChunkId[String(i)]??[])r[String(a)]!==!0&&(r[String(a)]=!0,n.push(a))}return t.sortedChunkIds.every(i=>r[String(i)]===!0)}var sp=d(()=>{"use strict";Ca();ap();Pa()});function hI(t){let e=uI();for(let r of t.sortedChunkIds){let n=On(),i=[r],a=Gt();for(n[String(r)]=1,a[String(r)]=!0;i.length>0;){let s=On();for(let c of i){let u=n[String(c)]??0,l=0,m=[];for(let p of t.incomingEdgesByChunkId[String(c)]??[]){if(a[String(p.fromId)]===!0)continue;let h=t.fingerprintEdgeWeightsByKey[D(p.fromId,c)]??t.fingerprintEdgeWeightsByKey[D(c,p.fromId)]??1;m.push([p.fromId,h]),l+=h}for(let p of t.outgoingEdgesByChunkId[String(c)]??[]){if(a[String(p.toId)]===!0)continue;let h=t.fingerprintEdgeWeightsByKey[D(c,p.toId)]??t.fingerprintEdgeWeightsByKey[D(p.toId,c)]??1;m.push([p.toId,h]),l+=h}if(!(l<=0))for(let[p,h]of m){let g=u*(h/l)*.75;s[String(p)]=(s[String(p)]??0)+g}}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;e[String(r)]=n}return e}function fI(t){let e=Object.keys(t)[0];if(e===void 0)return;let r=Object.keys(t[e]??{}).sort();for(let[n,i]of Object.entries(t)){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 yI=d(()=>{"use strict";Ln();An();Pa()});function cp(t,e,r,n){let i=n===void 0?new Map(t.sortedChunkIds.map(c=>[c,[c]])):new Map([...n.entries()].map(([c,u])=>[c,[...u]])),a=lI(),o=[],s=Gt();for(let[c,u]of i.entries())for(let l of u)a[String(l)]=c;for(let c of i.keys())for(let u of i.keys()){if(c>=u)continue;let l=i.get(c),m=i.get(u);l===void 0||m===void 0||bI(t,l,m)&&(o.push(EI(c,u,wI(t,l,m,e))),s[kI(c,u)]=!0)}for(;o.length>0;){o.sort(iI);let c=o.shift();if(c===void 0)continue;let u=a[String(c.leftClusterId)],l=a[String(c.rightClusterId)];if(u===void 0||l===void 0||u===l)continue;let m=i.get(u),p=i.get(l);if(m===void 0||p===void 0||!bI(t,m,p)||r.enableBonus&&m.length>=2&&p.length>=2||SI(t,m)+SI(t,p)>r.snakeWordsCount)continue;m.push(...p),i.delete(l);for(let g of p)a[String(g)]=u;let h=new Set;for(let g of m)for(let f of t.undirectedAdjacentChunkIdsByChunkId[String(g)]??[])h.add(f);for(let g of h){let f=a[String(g)];if(f===void 0||f===u)continue;let E=i.get(f);if(E===void 0)continue;let v=kI(u,f);s[v]!==!0&&(o.push(EI(Math.min(u,f),Math.max(u,f),wI(t,m,E,e))),s[v]=!0)}}return i}function bI(t,e,r){for(let n of e)for(let i of r)if(t.edgeByKey[D(n,i)]!==void 0||t.edgeByKey[D(i,n)]!==void 0)return!0;return!1}function SI(t,e){return e.reduce((r,n)=>r+(t.chunksById[String(n)]?.wordsCount??0),0)}function wI(t,e,r,n){let i=0,a=Number.POSITIVE_INFINITY;for(let o of e)for(let s of r){let c=t.edgeByKey[D(o,s)]??t.edgeByKey[D(s,o)];if(c===void 0)continue;i+=1;let u=n[String(o)]??{},l=n[String(s)]??{},m=0;for(let v of Object.keys(u)){let I=(u[v]??0)-(l[v]??0);m+=I*I}let p=La(c.strength),h=t.chunksById[String(o)],g=t.chunksById[String(s)];h?.retention!==void 0&&g?.retention!==void 0&&(p+=3),h?.importance!==void 0&&g?.importance!==void 0&&(p+=3);let E=(1-Math.sqrt(m)/2)*p;a=Math.min(a,E)}return i===0?Number.NEGATIVE_INFINITY:a}function EI(t,e,r){return{leftClusterId:t,rightClusterId:e,value:r}}function kI(t,e){return t<e?D(t,e):D(e,t)}var II=d(()=>{"use strict";Ln();An();Ca();Pa();ip()});function dp(t){if(t.chunks.length===0)return[];let e=mI(t.chunks,t.edges);if(!pI(e))throw new Error("Snake detector requires a weakly connected component input");let r=t.snakeWordsCount??280;if(t.chunks.reduce((s,c)=>s+c.wordsCount,0)<=r)return[[...t.chunks].sort(tp).map(s=>s.id)];let i=hI(e);fI(i);let a=cp(e,i,{enableBonus:!0,snakeWordsCount:r},void 0);return[...cp(e,i,{enableBonus:!1,snakeWordsCount:r},a).values()].map(s=>[...s].sort((c,u)=>{let l=e.chunksById[String(c)],m=e.chunksById[String(u)];return l===void 0||m===void 0?c-u:tp(l,m)}))}var xI=d(()=>{"use strict";sp();An();Ca();yI();ap();II()});var RI=d(()=>{"use strict";xI();sp()});function TI(t){let e=OF(),r=vI();for(let[i,a]of t.snakes.entries())for(let o of a)e[String(o)]=i;for(let i of t.edges){let a=e[String(i.fromId)],o=e[String(i.toId)];if(a===void 0||o===void 0||a===o)continue;let s=D(a,o);r[s]=(r[s]??0)+i.weight}let n=vI();for(let[i,a]of Object.entries(r)){let[o="",s=""]=i.split(":"),c=Number(o),u=Number(s),l=t.snakes[c]?.[0],m=t.snakes[u]?.[0];if(l===void 0||m===void 0)continue;let p=t.chunksById[String(l)],h=t.chunksById[String(m)];if(p===void 0||h===void 0)continue;let g=LF(p,h)<0?D(c,u):D(u,c);n[g]=(n[g]??0)+a}return Object.keys(n).sort(AF).map(i=>{let[a="",o=""]=i.split(":");return{fromSnakeIndex:Number(a),toSnakeIndex:Number(o),weight:n[i]??0}})}function LF(t,e){let[r,n]=t.sentenceId,[i,a]=e.sentenceId;return r!==i?r-i:n!==a?n-a:t.id-e.id}function AF(t,e){let[r="",n=""]=t.split(":"),[i="",a=""]=e.split(":"),o=Number(r),s=Number(i);return o!==s?o-s:Number(n)-Number(a)}function vI(){return Object.create(null)}function OF(){return Object.create(null)}var _I=d(()=>{"use strict";Ln()});var CI=d(()=>{"use strict";RI();_I()});function DF(t,e){let[r="",n=""]=t.split(":"),[i="",a=""]=e.split(":"),o=Number(r),s=Number(i);return o!==s?o-s:Number(n)-Number(a)}function lp(t,e){return t-e}function PI(){return Object.create(null)}function MF(){return Object.create(null)}function WF(t){let e=PI(),r=jF(),n=GF(),i=[];for(let c of t.sentenceGroups)c.serialId===t.serialId&&i.push(c);for(let c of t.chunks){let u=up(i,c.sentenceId[1]);e[String(c.id)]=c,u!==void 0&&(r[String(u)]===void 0&&(r[String(u)]=[]),r[String(u)]?.push(c.id))}for(let c of t.edges){let u=e[String(c.fromId)],l=e[String(c.toId)];if(u===void 0||l===void 0)continue;let m=up(i,u.sentenceId[1]),p=up(i,l.sentenceId[1]);m===void 0||p===void 0||m===p&&(n[String(m)]===void 0&&(n[String(m)]=[]),n[String(m)]?.push(c))}let a=[],o=[],s=[];for(let c of BF(r)){let u=[...r[String(c)]??[]].sort(lp),l=n[String(c)]??[],m=[];for(let h of op({chunkIds:u,edges:l})){let g=FF();for(let f of h)g[String(f)]=!0;m.push(...dp({chunks:h.map(f=>e[String(f)]).filter(f=>f!==void 0),edges:l.filter(f=>g[String(f.fromId)]===!0&&g[String(f.toId)]===!0),snakeWordsCount:NF}))}let p=a.length;for(let[h,g]of m.entries()){let f=g[0],E=g[g.length-1],v=f===void 0?void 0:e[String(f)],I=E===void 0?void 0:e[String(E)];if(v===void 0||I===void 0)continue;let q=p+h;a.push({firstLabel:v.label,groupId:c,lastLabel:I.label,localSnakeId:h,size:g.length,weight:g.reduce((F,_)=>F+(e[String(_)]?.weight??0),0),wordsCount:g.reduce((F,_)=>F+(e[String(_)]?.wordsCount??0),0)});for(let[F,_]of g.entries())o.push({chunkId:_,position:F,snakeIndex:q})}s.push(...TI({chunksById:e,edges:l,snakes:m}).map(h=>({fromSnakeIndex:p+h.fromSnakeIndex,toSnakeIndex:p+h.toSnakeIndex,weight:h.weight})))}return{snakeChunks:o,snakeEdges:s,snakes:a}}function up(t,e){return t.find(r=>e>=r.startSentenceIndex&&e<=r.endSentenceIndex)?.groupId}function FF(){return Object.create(null)}function jF(){return Object.create(null)}function GF(){return Object.create(null)}function BF(t){return Object.keys(t).map(Number).sort(lp)}var NF,Oa,LI=d(()=>{"use strict";nI();CI();Ln();NF=280,Oa=class{#e=[];#t=PI();#r;#n=[];#i=MF();#a;#o;constructor(e,r,n){this.#r=e,this.#a=n,this.#o=r}accept(e){for(let r of e.chunks)this.#l(r);if(e.importanceAnnotations!==void 0)for(let r of e.importanceAnnotations){let n=this.#t[String(r.chunkId)];n!==void 0&&(this.#t[String(r.chunkId)]={...n,importance:r.importance})}for(let r of e.edges)this.#u(r)}async finalize(){let e=this.#s(),r=this.#c(),n=oI(e),i=sI({chunkWeights:n,edges:r}),a=e.map(m=>({...m,weight:n[String(m.id)]??0})),o=r.map(m=>({...m,weight:i[D(m.fromId,m.toId)]??0})),s=await rI({chunks:a,edges:o,fragments:this.#r.getSerialFragments(this.#o),groupWordsCount:this.#a,serialId:this.#o}),c=WF({chunks:a,edges:o,sentenceGroups:s,serialId:this.#o});await this.#r.serials.ensure(this.#o);let u=new Map;for(let m of a){let p=await this.#r.chunks.create({content:m.content,generation:m.generation,label:m.label,sentenceId:m.sentenceId,sentenceIds:m.sentenceIds,weight:m.weight,wordsCount:m.wordsCount,...m.importance===void 0?{}:{importance:m.importance},...m.retention===void 0?{}:{retention:m.retention}});u.set(m.id,p.id)}for(let m of o){let p=u.get(m.fromId),h=u.get(m.toId);p===void 0||h===void 0||await this.#r.readingEdges.save({...m,fromId:p,toId:h})}await this.#r.fragmentGroups.saveMany(s);let l=[];for(let m of c.snakes)l.push(await this.#r.snakes.create({firstLabel:m.firstLabel,groupId:m.groupId,lastLabel:m.lastLabel,localSnakeId:m.localSnakeId,serialId:this.#o,size:m.size,weight:m.weight,wordsCount:m.wordsCount}));for(let m of c.snakeChunks){let p=l[m.snakeIndex];p!==void 0&&await this.#r.snakeChunks.save({chunkId:u.get(m.chunkId)??m.chunkId,position:m.position,snakeId:p})}for(let m of c.snakeEdges){let p=l[m.fromSnakeIndex],h=l[m.toSnakeIndex];p===void 0||h===void 0||await this.#r.snakeEdges.save({fromSnakeId:p,toSnakeId:h,weight:m.weight})}}#s(){return[...this.#e].sort(lp).map(e=>this.#t[String(e)]).filter(e=>e!==void 0)}#c(){return[...this.#n].sort(DF).map(e=>this.#i[e]).filter(e=>e!==void 0)}#l(e){let r=String(e.id);this.#t[r]===void 0&&this.#e.push(e.id);let n=e.importance,i=e.retention;this.#t[r]={content:e.content,generation:e.generation,id:e.id,label:e.label,sentenceId:e.sentenceId,sentenceIds:[...e.sentenceIds],wordsCount:e.wordsCount,weight:0,...n===void 0?{}:{importance:n},...i===void 0?{}:{retention:i}}}#u(e){let r=D(e.fromId,e.toId);this.#i[r]===void 0&&this.#n.push(r);let n=e.strength;this.#i[r]={fromId:e.fromId,toId:e.toId,weight:0,...n===void 0?{}:{strength:n}}}}});var AI=d(()=>{"use strict";LI()});function OI(t){let e=t.document??t.workspace;if(e===void 0)throw new Error("SerialGeneration requires a document");return e}async function mp(t,e){let r=await t.serials.getById(e);if(r===void 0)throw new Error(`Chapter ${e} does not exist. Use \`wg <archive-uri>/chapter list\` to discover chapter ids.`);return r}var pp=d(()=>{"use strict"});function NI(){return Object.create(null)}function hp(t,e,r,n){r.accept(n),t.push(...n.chunks);for(let i of n.edges)UF(e,i.fromId,i.toId)}function UF(t,e,r){let n=t[String(e)]??[];n.includes(r)||(t[String(e)]=[...n,r].sort(HF))}function HF(t,e){return t-e}var DI=d(()=>{"use strict"});async function Nn(t,e,r,n={}){await t.getSerialFragments(e).writeTextStream(await mk(r),{...n.segmenter===void 0?{}:{segmenter:n.segmenter}}),await t.serials.bumpRevision(e)}var fp=d(()=>{"use strict";sc()});var Dn,yp,yc=d(()=>{"use strict";Dn=class{#e;#t;#r;constructor(e,r,n){this.#e=r,this.#t=n,this.#r=new yp(e,r)}get id(){return this.#e}getSummary(){return this.#t}getTopology(){return this.#r}},yp=class{#e;#t;#r;#n;#i;#a;#o;constructor(e,r){this.#e=e.chunks,this.#t=e.fragmentGroups,this.#r=e.readingEdges,this.#n=r,this.#i=e.snakeChunks,this.#a=e.snakeEdges,this.#o=e.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(e){let r=await this.#o.getById(e);if(r===void 0||r.serialId!==this.#n)throw new Error(`Snake ${e} does not belong to this serial`);return await this.#i.listBySnake(e)}async listSnakeEdges(){return await this.#a.listBySerial(this.#n)}async listSnakes(){return await this.#o.listBySerial(this.#n)}}});var br,MI=d(()=>{"use strict";ri();ai();Zm();ac();AI();sc();oc();pp();DI();fp();yc();br=class{#e=320;#t;#r=new Ce(1);#n;#i;#a;#o=new Ce(1);#s;#c;#l=1;constructor(e){let r=OI(e);this.#t=r.fragmentGroups,this.#s=e.llm,this.#n=e.logDirPath,this.#i=r.serials,this.#c=e.segmenter,this.#a=r}async generate(e,r,n){let i=await this.#f();return await Nn(this.#a,i,e,{...this.#c===void 0?{}:{segmenter:this.#c}}),await this.#u(i,r,n)}async create(e,r){return await this.generate(e,r)}async generateInto(e,r,n,i){return await this.#b(e),await Nn(this.#a,e,r,{...this.#c===void 0?{}:{segmenter:this.#c}}),await this.#u(e,n,i)}async buildTopologyInto(e,r,n){await this.#m(e,r.extractionPrompt,r.userLanguage,n)}async buildSummary(e,r={}){let n=await this.#p(e,r.userLanguage);return new Dn(this.#a,e,n)}async#u(e,r,n){await this.#m(e,r.extractionPrompt,r.userLanguage,n);let i=await this.#p(e,r.userLanguage);return await n?.complete(),new Dn(this.#a,e,i)}async#d(){return await this.#r.use(()=>{let e=this.#l;return this.#l+=1,e})}async#p(e,r){if(!(await mp(this.#a,e)).topologyReady)throw new Error(`Serial ${e} is not ready for summary`);let i=await this.#a.readSummary(e);if(i!==void 0)return i;let a=this.#y(e),o=await a.listFragmentIds();if(o.length<=1){let l=await hk(a,o);return await this.#o.use(async()=>await this.#a.writeSummary(e,l)),l}let s=await this.#t.listGroupIdsForSerial(e),c=[];for(let l of s){let m=await Ta({...this.#g({groupId:l,serialId:e,userLanguage:r})});m.trim()!==""&&c.push(m)}let u=c.join(`
|
|
2005
|
+
`)}function lG(t){let e=[],{fragment:n}=t,r=0;for(;r<n.sentences.length;){if(n.sentences[r]===void 0){r+=1;continue}let o=Fp(n.serialId,n.fragmentId+r),s=t.chunkCoverage[o];if(s===void 0){e.push({endIndex:r+1,startIndex:r}),r+=1;continue}let d=Hx(s),u=r+1;for(;u<n.sentences.length;){let l=Fp(n.serialId,n.fragmentId+u),m=t.chunkCoverage[l];if(m===void 0||Hx(m).label!==d.label)break;u+=1}e.push({chunkAttributes:d,endIndex:u,startIndex:r}),r=u}return e.map(a=>bG({fragment:n,fullMarkup:t.fullMarkup,range:a,wrapHighRetention:t.wrapHighRetention})).join(" ")}function mG(t){let e=[];for(let n of t.skippedStartIndexes){let r=t.fragments[String(n)]?.summary?.trim()??"";r!==""&&e.push(r)}return e.join(" ")}function pG(t){let e=Object.create(null);for(let n of t)for(let r of n.sentenceIds){let i=Fp(...r),a=e[i];e[i]=a===void 0?[n]:[...a,n]}return e}function Fp(t,e){return`${t}:${e}`}function hG(t,e){let n=new Set(t);for(let r=1;r<t.length;r+=1){let i=t[r-1],a=t[r];if(!(i===void 0||a===void 0))for(let o of Kx(e,i,a))n.add(o)}return[...n].sort(Ya)}function Kx(t,e,n){return t.filter(r=>r>e&&r<n)}async function fG(t,e){let n=[...await e.listFragmentIds()].sort(Ya),r=Object.create(null),i=[];for(let a of t)for(let o of a.sentenceIds){let s=gG(n,o[1]);if(s===void 0)continue;let d=String(s);r[d]!==!0&&(r[d]=!0,i.push(s))}return i.sort(Ya),i}function gG(t,e){let n;for(let r of t){if(r>e)break;n=r}return n}async function yG(t,e){let n=Object.create(null);return await Promise.all(t.map(async r=>{n[String(r)]=await e.getFragment(r)})),n}function Ya(t,e){return t-e}function Hx(t){let e=qx({detailed:3,focused:2,relevant:1,verbatim:4}),n=qx({critical:3,helpful:1,important:2}),r=t[0],i=t[0];for(let o of t)Xr(o.retention,e)>Xr(r?.retention,e)&&(r=o),Xr(o.importance,n)>Xr(i?.importance,n)&&(i=o);return{label:(Xr(r?.retention,e)>=Xr(i?.importance,n)?r:i)?.label??"",...i?.importance===void 0?{}:{importance:i.importance},...r?.retention===void 0?{}:{retention:r.retention}}}function qx(t){return Object.freeze(t)}function Xr(t,e){return t===void 0?0:e[t]??0}function bG(t){let e=t.fragment.sentences.slice(t.range.startIndex,t.range.endIndex).map(r=>r.text).join(" "),n=t.range.chunkAttributes;if(n===void 0)return e;if(t.fullMarkup){let r=[`label="${n.label}"`];return n.retention!==void 0&&r.push(`retention="${n.retention}"`),n.importance!==void 0&&r.push(`importance="${n.importance}"`),`<chunk ${r.join(" ")}>${e}</chunk>`}return t.wrapHighRetention&&(n.retention==="verbatim"||n.retention==="detailed")?`<chunk retention="${n.retention}">${e}</chunk>`:e}var Gp=c(()=>{"use strict";N()});function zx(t){return t.replace(/<chunk(?:\s+[^>]*)?>/g,"").replace(/<\/chunk>/g,"")}function Qx(t){let e=t.match(/##\s*(?:Compressed\s+Text|压缩文本)\s*\n+(.*?)(?:\n+---|\*\*CRITICAL\*\*|$)/is);return e?.[1]!==void 0?Jx(e[1].trim()):Jx(t.trim())}function Xx(t){let e=t.match(/##\s*(?:Compressed\s+Text|压缩文本)\s*/i);return e?.index===void 0?"":t.slice(0,e.index).trim()}function Jx(t){return t.match(/^```(?:\w+)?\s*\n([\s\S]*?)\n```$/)?.[1]?.trim()??t}var Vx=c(()=>{"use strict"});import{z as Vr}from"zod";function IG(t,e){return e===void 0?[{content:t.systemPrompt,role:"system"},{content:t.compressedText,role:"user"}]:[{content:t.systemPrompt,role:"system"},{content:e[0],role:"user"},{content:e[1],role:"assistant"},{content:t.compressedText,role:"user"}]}var SG,wG,EG,$d,Yx=c(()=>{"use strict";Pe();ve();Gd();Gp();Ud();SG=["critical","major","minor"],wG=Vr.object({problem:Vr.string(),severity:Vr.enum(SG),suggestion:Vr.string().default("")}),EG=Vr.object({issues:Vr.array(wG)}),$d=class{#e;#t;#n;#r;#a;constructor(e,n,r,i){this.#e=e,this.#t=r.reviewGuide,this.#n=r.review,this.#r=n,this.#a=i}async generateClueReviewers(e){return await Promise.all(e.map(async n=>await this.#e.request(async r=>{let i=await $x({chunks:n.chunks,fullMarkup:!0,serialFragments:this.#r}),a=[{content:this.#e.loadSystemPrompt(Px),role:"system"},{content:i,role:"user"}],o=await r(a,{scope:this.#t});return{clueId:n.clueId,label:n.label,reviewerInfo:o.trim(),weight:n.weight}})))}async reviewCompression(e,n,r){let i=await Promise.all(n.map(async o=>await this.#e.request(async s=>{let d=this.#e.loadSystemPrompt(Cx,{thread_info:o.reviewerInfo,user_language:this.#a}),u=r[String(o.clueId)],l=IG({compressedText:e,systemPrompt:d},u);try{return await Z({messages:l,parse:m=>({rawResponse:JSON.stringify(m),review:{clueId:o.clueId,issues:m.issues.map(p=>({...p,severity:Nx(p.severity)})),weight:o.weight}}),responseIntentClassifierPrompt:this.#e.loadSystemPrompt(ae),request:async(m,p,h)=>await s(m,{retryIndex:p,retryMax:h,scope:this.#n,useCache:!1}),schema:EG})}catch(m){if(!(m instanceof Ie))throw m;return Ce({clueId:o.clueId,component:"editor-review"}).warn({error:m},"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 Za(t){return await new Up(t).run()}function Bp(t,e){return t-e}function xG(t){return t.flatMap(e=>e.chunks)}function kG(t){let e=t.document??t.workspace;if(e===void 0)throw new Error("Editor requires a document");return e}var Up,Zx=c(()=>{"use strict";_x();Ox();Wp();Bx();Ux();Gp();Vx();Yx();Up=class{#e;#t;#n;#r;#a;#i;#o;#s;#d;#c;#l;#u;#p;constructor(e){let n=kG(e);this.#e=e.compressionRatio??.2,this.#t=e.groupId,this.#n=e.llm,this.#a=e.maxClues??10,this.#i=e.maxIterations??5,this.#d=e.scopes,this.#c=n.getSerialFragments(e.serialId),this.#l=e.serialId,this.#u=e.userLanguage,this.#p=n,this.#o=new Bd(this.#n,e.scopes.compress,this.#e,this.#u),this.#r=new qd(this.#l,this.#t,{compressionRatio:this.#e,maxIterations:this.#i,...e.logDirPath===void 0?{}:{logDirPath:e.logDirPath}}),this.#s=new $d(this.#n,this.#c,{review:this.#d.review,reviewGuide:this.#d.reviewGuide},this.#u)}async run(){let e=await this.#m(),n=await this.#h(e);if(n.length===0)return"";let r=await Rx({document:this.#p,groupId:this.#t,maxClues:this.#a,serialId:this.#l}),i=await this.#f(e);if(i.trim()==="")return"";await this.#r.initialize(r);let a=await jp({chunks:xG(r),segmentStartIndexes:n,serialFragments:this.#c,wrapHighRetention:!0}),o=await this.#s.generateClueReviewers(r),s=Math.floor(i.length*this.#e),d=[],u=Object.create(null),l,m;for(let h=1;h<=this.#i;h+=1){await this.#r.appendIterationHeader(h,m);let y=await this.#o.request({markedText:a,targetLength:s,...l===void 0?{}:{previousCompressedText:l},...m===void 0?{}:{revisionFeedback:m}}),f=zx(Qx(y)),I=Xx(y);await this.#r.appendCompressionResult({compressedText:f,thinkingText:I});let v=await this.#s.reviewCompression(f,o,u),x=[...v.reviews],J=Gx({compressedText:f,...this.#u===void 0?{}:{userLanguage:this.#u}});J!==void 0&&(x.push(J.review),await this.#r.appendLanguageMismatch({detectedLanguageCode:J.detectedLanguageCode,review:J.review,targetLanguageCode:J.targetLanguageCode,userLanguage:this.#u}));let G=Dx(x);if(d.push({iteration:h,reviews:x,score:G,text:f}),G===0)break;if(!(h>=this.#i)){m=Mx({llm:this.#n,reviews:x}),l=f;for(let _ of Object.keys(v.rawResponses)){let Rt=v.rawResponses[_];Rt!==void 0&&(u[_]=[f,Rt])}}}let p=Fx(d);return await this.#r.appendFinalSelection(p,i.length),p.text}async#m(){return(await this.#p.fragmentGroups.listBySerial(this.#l)).filter(e=>e.groupId===this.#t).map(e=>({endSentenceIndex:e.endSentenceIndex,startSentenceIndex:e.startSentenceIndex}))}async#h(e){let n=[...await this.#c.listFragmentIds()].sort(Bp),r=new Set;for(let i of e)for(let a=0;a<n.length;a+=1){let o=n[a];if(o===void 0)continue;let s=n[a+1],d=s===void 0?1/0:s-1;o<=i.endSentenceIndex&&d>=i.startSentenceIndex&&r.add(o)}return[...r].sort(Bp)}async#f(e){return(await Promise.all(e.map(async r=>({sentences:await this.#b(r.startSentenceIndex,r.endSentenceIndex)})))).flatMap(r=>r.sentences.map(i=>i.text)).join(" ")}async#b(e,n){if(this.#c.listSentencesInRange!==void 0)return await this.#c.listSentencesInRange(e,n);let r=[...await this.#c.listFragmentIds()].sort(Bp);return(await Promise.all(r.filter((a,o)=>{let s=r[o+1],d=s===void 0?1/0:s-1;return a<=n&&d>=e}).map(async a=>await this.#c.getFragment(a)))).flatMap(a=>a.sentences.filter((o,s)=>{let d=a.fragmentId+s;return d>=e&&d<=n}))}}});var Hp=c(()=>{"use strict";Zx()});async function nk(t){let e=await RG(t.fragments);return TG(vG({chunks:t.chunks,edges:t.edges,segmentWordsCounts:e}))}async function RG(t){let e=ik(),n=await t.listFragmentIds();for(let r of n){let i=await t.getFragment(r);e[String(r)]=i.sentences.reduce((a,o)=>a+o.wordsCount,0)}return e}function vG(t){let e=Object.keys(t.segmentWordsCounts).map(Number).sort(rk),n=PG(),r=ik(),i=CG();for(let o of t.chunks){let s=ek(e,o.sentenceId[1]);if(s===void 0)continue;let d=String(s);r[String(o.id)]=o.weight,n[d]===void 0&&(n[d]=[]),n[d]?.push(o.id)}for(let o of t.edges){let s=String(o.fromId),d=String(o.toId);i[s]===void 0&&(i[s]=[]),i[d]===void 0&&(i[d]=[]),i[s]?.push(o),i[d]?.push(o)}let a=_G();for(let o of t.chunks)a[String(o.id)]=o;return e.filter(o=>n[String(o)]!==void 0).map(o=>{let s=n[String(o)]??[],d=0,u=0;for(let l of s){let m=r[String(l)]??0,h=(i[String(l)]??[]).map(f=>({otherChunkId:f.fromId===l?f.toId:f.fromId,weight:f.weight})).map(f=>{let I=a[String(f.otherChunkId)];if(I!==void 0)return{otherSegmentStartIndex:ek(e,I.sentenceId[1]),weight:f.weight}}).filter(f=>f!==void 0&&f.otherSegmentStartIndex!==void 0&&f.otherSegmentStartIndex!==o);if(h.length===0)continue;let y=h.reduce((f,I)=>f+I.weight,0);if(y!==0)for(let f of h){let I=m/y*f.weight;if(f.otherSegmentStartIndex<o){u+=I;continue}d+=I}}return{endIncision:d,startSentenceIndex:o,startIncision:u,wordsCount:t.segmentWordsCounts[String(o)]??0}})}function ek(t,e){let n;for(let r of t){if(r>e)break;n=r}return n}function TG(t){let e=t.flatMap(l=>[l.startIncision,l.endIncision]).filter(l=>l>0).sort(rk);if(e.length===0)return[...t];let n=Math.min(Math.floor(e.length*.98),e.length-1),r=e[n]??0,i=e.filter(l=>l<r);if(i.length===0)return t.map(l=>({...l,endIncision:l.endIncision>0?10:0,startIncision:l.startIncision>0?10:0}));let a=Math.min(...i),o=Math.max(...i),s=a>0?Math.log(a):0,d=o>0?Math.log(o):0,u=d-s===0?1:d-s;return t.map(l=>({...l,endIncision:tk(l.endIncision,{logRange:u,minLog:s,threshold:r}),startIncision:tk(l.startIncision,{logRange:u,minLog:s,threshold:r})}))}function tk(t,e){if(t===0)return 0;if(t>=e.threshold)return 10;let n=t>0?Math.log(t):e.minLog;return Math.max(1,Math.min(10,Math.round(1+(n-e.minLog)/e.logRange*9)))}function rk(t,e){return t-e}function _G(){return Object.create(null)}function CG(){return Object.create(null)}function PG(){return Object.create(null)}function ik(){return Object.create(null)}var ak=c(()=>{"use strict"});function sk(t){if(t.segmentInfos.length===0)return[];let e=t.segmentInfos.map(r=>({count:r.wordsCount,endIncision:r.endIncision,startSentenceIndex:r.startSentenceIndex,startIncision:r.startIncision}));return AG(e,t.groupWordsCount).map((r,i)=>({endSentenceIndex:Math.max(...r),groupId:i,serialId:t.serialId,startSentenceIndex:Math.min(...r)}))}function AG(t,e){return qp(t,e).map(n=>n.map(r=>r.startSentenceIndex))}function qp(t,e){if(t.length===0)return[];if(e<=0)return t.map(o=>[o]);let n=t.reduce((o,s)=>o+s.count,0);if(t.length===1||n<=e)return[[...t]];let r=LG(t,e);if(r===void 0)return ok(t,e);let i=t.slice(0,r+1),a=t.slice(r+1);return i.length===0||a.length===0||i.length===t.length||a.length===t.length?ok(t,e):[...qp(i,e),...qp(a,e)]}function LG(t,e){let n=t.reduce((d,u)=>d+u.count,0),r=0,i,a=0,o=Number.POSITIVE_INFINITY,s=Number.POSITIVE_INFINITY;for(let d=0;d<t.length-1;d+=1){let u=t[d],l=t[d+1];if(u===void 0||l===void 0)continue;r+=u.count;let m=n-r,p=u.endIncision+l.startIncision;if(p<=0)continue;let h=+(r>e)+ +(m>e),y=Math.max(0,r-e)+Math.max(0,m-e);(h<o||h===o&&p>a||h===o&&p===a&&y<s)&&(a=p,i=d,o=h,s=y)}return i}function ok(t,e){let n=[],r=[],i=0;for(let a of t)r.length>0&&i+a.count>e&&(n.push(r),r=[],i=0),r.push(a),i+=a.count,i>=e&&(n.push(r),r=[],i=0);return r.length>0&&n.push(r),n}var dk=c(()=>{"use strict"});async function ck(t){let e=await nk({chunks:t.chunks,edges:t.edges,fragments:t.fragments});return sk({segmentInfos:e,groupWordsCount:t.groupWordsCount,serialId:t.serialId})}var uk=c(()=>{"use strict";ak();dk()});function $p(t,e){let[n,r]=t.sentenceId,[i,a]=e.sentenceId;return n!==i?n-i:r!==a?r-a:t.id-e.id}function lk(t,e){return t.value!==e.value?e.value-t.value:t.leftClusterId!==e.leftClusterId?t.leftClusterId-e.leftClusterId:t.rightClusterId-e.rightClusterId}function eo(t,e){return t-e}var to=c(()=>{"use strict"});function pk(t){let e=Kp();for(let n of t)e[String(n.id)]=WG(n);return e}function hk(t){let e=Kp();for(let r of t.edges){let i=mk(r.strength);e[String(r.fromId)]=(e[String(r.fromId)]??0)+i,e[String(r.toId)]=(e[String(r.toId)]??0)+i}let n=Kp();for(let r of t.edges){let i=mk(r.strength),a=t.chunkWeights[String(r.fromId)]??0,o=t.chunkWeights[String(r.toId)]??0,s=e[String(r.fromId)]??0,d=e[String(r.toId)]??0,u=s===0?0:a*(i/s),l=d===0?0:o*(i/d);n[W(r.fromId,r.toId)]=Math.max(u+l,OG)}return n}function W(t,e){return`${t}:${e}`}function WG(t){return jG(t.retention)+FG(t.importance)}function FG(t){return t===void 0?0:NG[t]??0}function mk(t){return t===void 0?1:DG[t]??1}function jG(t){return t===void 0?0:MG[t]??0}function Kp(){return Object.create(null)}var OG,NG,DG,MG,Yr=c(()=>{"use strict";N();OG=.1,NG={critical:9,helpful:1,important:3},DG={critical:9,helpful:1,important:3},MG={detailed:9,focused:3,relevant:1,verbatim:27}});var Zr=c(()=>{"use strict"});function Jt(){return Object.create(null)}function fk(){return Object.create(null)}function gk(){return Object.create(null)}function Kd(){return Object.create(null)}function yk(){return Object.create(null)}function ei(){return Object.create(null)}function bk(){return Object.create(null)}function Jp(){return Object.create(null)}var no=c(()=>{"use strict"});function ro(t){switch(t){case"critical":return 9;case"important":return 3;case"helpful":return 1;default:return 1}}var zp=c(()=>{"use strict";Zr()});function io(t,e,n){let r=t[String(e)]??[];r.includes(n)||(t[String(e)]=[...r,n])}function Sk(t,e){let n=fk(),r=gk(),i=Jp(),a=Jp(),o=[...t].map(d=>d.id).sort(eo),s=Kd();for(let d of t)n[String(d.id)]=d;for(let d of e){if(n[String(d.fromId)]===void 0||n[String(d.toId)]===void 0)continue;let u=W(d.fromId,d.toId);r[u]=d,i[String(d.toId)]===void 0&&(i[String(d.toId)]=[]),a[String(d.fromId)]===void 0&&(a[String(d.fromId)]=[]),i[String(d.toId)]?.push(d),a[String(d.fromId)]?.push(d),io(s,d.fromId,d.toId),io(s,d.toId,d.fromId)}return{chunksById:n,edgeByKey:r,fingerprintEdgeWeightsByKey:BG(n,e),incomingEdgesByChunkId:i,outgoingEdgesByChunkId:a,sortedChunkIds:o,undirectedAdjacentChunkIdsByChunkId:s}}function BG(t,e){let n=ei();for(let i of e){if(t[String(i.fromId)]===void 0||t[String(i.toId)]===void 0)continue;let a=ro(i.strength);n[String(i.fromId)]=(n[String(i.fromId)]??0)+a,n[String(i.toId)]=(n[String(i.toId)]??0)+a}let r=ei();for(let i of e){let a=t[String(i.fromId)],o=t[String(i.toId)];if(a===void 0||o===void 0)continue;let s=ro(i.strength),d=n[String(i.fromId)]??0,u=n[String(i.toId)]??0,l=d===0?0:a.weight*(s/d),m=u===0?0:o.weight*(s/u),p=Math.max(l+m,.1);r[W(i.fromId,i.toId)]=p,r[W(i.toId,i.fromId)]=p}return r}var Qp=c(()=>{"use strict";Yr();Zr();to();no();zp()});function Xp(t){let e=[...t.chunkIds].sort(eo),n=Jt(),r=Kd(),i=Jt(),a=[];for(let o of e)n[String(o)]=!0;for(let o of t.edges)n[String(o.fromId)]!==!0||n[String(o.toId)]!==!0||(io(r,o.fromId,o.toId),io(r,o.toId,o.fromId));for(let o of e){if(i[String(o)]===!0)continue;let s=[o],d=[];for(i[String(o)]=!0;s.length>0;){let u=s.pop();if(u!==void 0){d.push(u);for(let l of r[String(u)]??[])i[String(l)]!==!0&&(i[String(l)]=!0,s.push(l))}}d.sort(eo),a.push(d)}return a.sort((o,s)=>{let d=o[0]??Number.POSITIVE_INFINITY,u=s[0]??Number.POSITIVE_INFINITY;return d-u}),a}function wk(t){let e=t.sortedChunkIds[0];if(e===void 0)return!0;let n=Jt(),r=[e];for(n[String(e)]=!0;r.length>0;){let i=r.pop();if(i!==void 0)for(let a of t.undirectedAdjacentChunkIdsByChunkId[String(i)]??[])n[String(a)]!==!0&&(n[String(a)]=!0,r.push(a))}return t.sortedChunkIds.every(i=>n[String(i)]===!0)}var Vp=c(()=>{"use strict";to();Qp();no()});function Ek(t){let e=yk();for(let n of t.sortedChunkIds){let r=ei(),i=[n],a=Jt();for(r[String(n)]=1,a[String(n)]=!0;i.length>0;){let s=ei();for(let d of i){let u=r[String(d)]??0,l=0,m=[];for(let p of t.incomingEdgesByChunkId[String(d)]??[]){if(a[String(p.fromId)]===!0)continue;let h=t.fingerprintEdgeWeightsByKey[W(p.fromId,d)]??t.fingerprintEdgeWeightsByKey[W(d,p.fromId)]??1;m.push([p.fromId,h]),l+=h}for(let p of t.outgoingEdgesByChunkId[String(d)]??[]){if(a[String(p.toId)]===!0)continue;let h=t.fingerprintEdgeWeightsByKey[W(d,p.toId)]??t.fingerprintEdgeWeightsByKey[W(p.toId,d)]??1;m.push([p.toId,h]),l+=h}if(!(l<=0))for(let[p,h]of m){let y=u*(h/l)*.75;s[String(p)]=(s[String(p)]??0)+y}}i=Object.keys(s).map(Number);for(let d of i)r[String(d)]=s[String(d)]??0,a[String(d)]=!0}let o=Object.values(r).reduce((s,d)=>s+d,0);if(o>0)for(let s of Object.keys(r))r[s]=(r[s]??0)/o;e[String(n)]=r}return e}function Ik(t){let e=Object.keys(t)[0];if(e===void 0)return;let n=Object.keys(t[e]??{}).sort();for(let[r,i]of Object.entries(t)){let a=Object.keys(i).sort();if(a.length!==n.length||a.some((o,s)=>o!==n[s]))throw new Error(`Fingerprint structure mismatch for chunk ${r}`)}}var xk=c(()=>{"use strict";Yr();Zr();no()});function Yp(t,e,n,r){let i=r===void 0?new Map(t.sortedChunkIds.map(d=>[d,[d]])):new Map([...r.entries()].map(([d,u])=>[d,[...u]])),a=bk(),o=[],s=Jt();for(let[d,u]of i.entries())for(let l of u)a[String(l)]=d;for(let d of i.keys())for(let u of i.keys()){if(d>=u)continue;let l=i.get(d),m=i.get(u);l===void 0||m===void 0||Rk(t,l,m)&&(o.push(_k(d,u,Tk(t,l,m,e))),s[Ck(d,u)]=!0)}for(;o.length>0;){o.sort(lk);let d=o.shift();if(d===void 0)continue;let u=a[String(d.leftClusterId)],l=a[String(d.rightClusterId)];if(u===void 0||l===void 0||u===l)continue;let m=i.get(u),p=i.get(l);if(m===void 0||p===void 0||!Rk(t,m,p)||n.enableBonus&&m.length>=2&&p.length>=2||vk(t,m)+vk(t,p)>n.snakeWordsCount)continue;m.push(...p),i.delete(l);for(let y of p)a[String(y)]=u;let h=new Set;for(let y of m)for(let f of t.undirectedAdjacentChunkIdsByChunkId[String(y)]??[])h.add(f);for(let y of h){let f=a[String(y)];if(f===void 0||f===u)continue;let I=i.get(f);if(I===void 0)continue;let v=Ck(u,f);s[v]!==!0&&(o.push(_k(Math.min(u,f),Math.max(u,f),Tk(t,m,I,e))),s[v]=!0)}}return i}function Rk(t,e,n){for(let r of e)for(let i of n)if(t.edgeByKey[W(r,i)]!==void 0||t.edgeByKey[W(i,r)]!==void 0)return!0;return!1}function vk(t,e){return e.reduce((n,r)=>n+(t.chunksById[String(r)]?.wordsCount??0),0)}function Tk(t,e,n,r){let i=0,a=Number.POSITIVE_INFINITY;for(let o of e)for(let s of n){let d=t.edgeByKey[W(o,s)]??t.edgeByKey[W(s,o)];if(d===void 0)continue;i+=1;let u=r[String(o)]??{},l=r[String(s)]??{},m=0;for(let v of Object.keys(u)){let x=(u[v]??0)-(l[v]??0);m+=x*x}let p=ro(d.strength),h=t.chunksById[String(o)],y=t.chunksById[String(s)];h?.retention!==void 0&&y?.retention!==void 0&&(p+=3),h?.importance!==void 0&&y?.importance!==void 0&&(p+=3);let I=(1-Math.sqrt(m)/2)*p;a=Math.min(a,I)}return i===0?Number.NEGATIVE_INFINITY:a}function _k(t,e,n){return{leftClusterId:t,rightClusterId:e,value:n}}function Ck(t,e){return t<e?W(t,e):W(e,t)}var Pk=c(()=>{"use strict";Yr();Zr();to();no();zp()});function Zp(t){if(t.chunks.length===0)return[];let e=Sk(t.chunks,t.edges);if(!wk(e))throw new Error("Snake detector requires a weakly connected component input");let n=t.snakeWordsCount??280;if(t.chunks.reduce((s,d)=>s+d.wordsCount,0)<=n)return[[...t.chunks].sort($p).map(s=>s.id)];let i=Ek(e);Ik(i);let a=Yp(e,i,{enableBonus:!0,snakeWordsCount:n},void 0);return[...Yp(e,i,{enableBonus:!1,snakeWordsCount:n},a).values()].map(s=>[...s].sort((d,u)=>{let l=e.chunksById[String(d)],m=e.chunksById[String(u)];return l===void 0||m===void 0?d-u:$p(l,m)}))}var Ak=c(()=>{"use strict";Vp();Zr();to();xk();Qp();Pk()});var Lk=c(()=>{"use strict";Ak();Vp()});function Nk(t){let e=KG(),n=Ok();for(let[i,a]of t.snakes.entries())for(let o of a)e[String(o)]=i;for(let i of t.edges){let a=e[String(i.fromId)],o=e[String(i.toId)];if(a===void 0||o===void 0||a===o)continue;let s=W(a,o);n[s]=(n[s]??0)+i.weight}let r=Ok();for(let[i,a]of Object.entries(n)){let[o="",s=""]=i.split(":"),d=Number(o),u=Number(s),l=t.snakes[d]?.[0],m=t.snakes[u]?.[0];if(l===void 0||m===void 0)continue;let p=t.chunksById[String(l)],h=t.chunksById[String(m)];if(p===void 0||h===void 0)continue;let y=qG(p,h)<0?W(d,u):W(u,d);r[y]=(r[y]??0)+a}return Object.keys(r).sort($G).map(i=>{let[a="",o=""]=i.split(":");return{fromSnakeIndex:Number(a),toSnakeIndex:Number(o),weight:r[i]??0}})}function qG(t,e){let[n,r]=t.sentenceId,[i,a]=e.sentenceId;return n!==i?n-i:r!==a?r-a:t.id-e.id}function $G(t,e){let[n="",r=""]=t.split(":"),[i="",a=""]=e.split(":"),o=Number(n),s=Number(i);return o!==s?o-s:Number(r)-Number(a)}function Ok(){return Object.create(null)}function KG(){return Object.create(null)}var Dk=c(()=>{"use strict";Yr()});var Mk=c(()=>{"use strict";Lk();Dk()});function zG(t,e){let[n="",r=""]=t.split(":"),[i="",a=""]=e.split(":"),o=Number(n),s=Number(i);return o!==s?o-s:Number(r)-Number(a)}function th(t,e){return t-e}function Wk(){return Object.create(null)}function QG(){return Object.create(null)}function XG(t){let e=Wk(),n=YG(),r=ZG(),i=[];for(let d of t.sentenceGroups)d.serialId===t.serialId&&i.push(d);for(let d of t.chunks){let u=eh(i,d.sentenceId[1]);e[String(d.id)]=d,u!==void 0&&(n[String(u)]===void 0&&(n[String(u)]=[]),n[String(u)]?.push(d.id))}for(let d of t.edges){let u=e[String(d.fromId)],l=e[String(d.toId)];if(u===void 0||l===void 0)continue;let m=eh(i,u.sentenceId[1]),p=eh(i,l.sentenceId[1]);m===void 0||p===void 0||m===p&&(r[String(m)]===void 0&&(r[String(m)]=[]),r[String(m)]?.push(d))}let a=[],o=[],s=[];for(let d of e0(n)){let u=[...n[String(d)]??[]].sort(th),l=r[String(d)]??[],m=[];for(let h of Xp({chunkIds:u,edges:l})){let y=VG();for(let f of h)y[String(f)]=!0;m.push(...Zp({chunks:h.map(f=>e[String(f)]).filter(f=>f!==void 0),edges:l.filter(f=>y[String(f.fromId)]===!0&&y[String(f.toId)]===!0),snakeWordsCount:JG}))}let p=a.length;for(let[h,y]of m.entries()){let f=y[0],I=y[y.length-1],v=f===void 0?void 0:e[String(f)],x=I===void 0?void 0:e[String(I)];if(v===void 0||x===void 0)continue;let J=p+h;a.push({firstLabel:v.label,groupId:d,lastLabel:x.label,localSnakeId:h,size:y.length,weight:y.reduce((G,_)=>G+(e[String(_)]?.weight??0),0),wordsCount:y.reduce((G,_)=>G+(e[String(_)]?.wordsCount??0),0)});for(let[G,_]of y.entries())o.push({chunkId:_,position:G,snakeIndex:J})}s.push(...Nk({chunksById:e,edges:l,snakes:m}).map(h=>({fromSnakeIndex:p+h.fromSnakeIndex,toSnakeIndex:p+h.toSnakeIndex,weight:h.weight})))}return{snakeChunks:o,snakeEdges:s,snakes:a}}function eh(t,e){return t.find(n=>e>=n.startSentenceIndex&&e<=n.endSentenceIndex)?.groupId}function VG(){return Object.create(null)}function YG(){return Object.create(null)}function ZG(){return Object.create(null)}function e0(t){return Object.keys(t).map(Number).sort(th)}var JG,ao,Fk=c(()=>{"use strict";uk();Mk();Yr();JG=280,ao=class{#e=[];#t=Wk();#n;#r=[];#a=QG();#i;#o;constructor(e,n,r){this.#n=e,this.#i=r,this.#o=n}accept(e){for(let n of e.chunks)this.#c(n);if(e.importanceAnnotations!==void 0)for(let n of e.importanceAnnotations){let r=this.#t[String(n.chunkId)];r!==void 0&&(this.#t[String(n.chunkId)]={...r,importance:n.importance})}for(let n of e.edges)this.#l(n)}async finalize(){let e=this.#s(),n=this.#d(),r=pk(e),i=hk({chunkWeights:r,edges:n}),a=e.map(m=>({...m,weight:r[String(m.id)]??0})),o=n.map(m=>({...m,weight:i[W(m.fromId,m.toId)]??0})),s=await ck({chunks:a,edges:o,fragments:this.#n.getSerialFragments(this.#o),groupWordsCount:this.#i,serialId:this.#o}),d=XG({chunks:a,edges:o,sentenceGroups:s,serialId:this.#o});await this.#n.serials.ensure(this.#o);let u=new Map;for(let m of a){let p=await this.#n.chunks.create({content:m.content,generation:m.generation,label:m.label,sentenceId:m.sentenceId,sentenceIds:m.sentenceIds,weight:m.weight,wordsCount:m.wordsCount,...m.importance===void 0?{}:{importance:m.importance},...m.retention===void 0?{}:{retention:m.retention}});u.set(m.id,p.id)}for(let m of o){let p=u.get(m.fromId),h=u.get(m.toId);p===void 0||h===void 0||await this.#n.readingEdges.save({...m,fromId:p,toId:h})}await this.#n.fragmentGroups.saveMany(s);let l=[];for(let m of d.snakes)l.push(await this.#n.snakes.create({firstLabel:m.firstLabel,groupId:m.groupId,lastLabel:m.lastLabel,localSnakeId:m.localSnakeId,serialId:this.#o,size:m.size,weight:m.weight,wordsCount:m.wordsCount}));for(let m of d.snakeChunks){let p=l[m.snakeIndex];p!==void 0&&await this.#n.snakeChunks.save({chunkId:u.get(m.chunkId)??m.chunkId,position:m.position,snakeId:p})}for(let m of d.snakeEdges){let p=l[m.fromSnakeIndex],h=l[m.toSnakeIndex];p===void 0||h===void 0||await this.#n.snakeEdges.save({fromSnakeId:p,toSnakeId:h,weight:m.weight})}}#s(){return[...this.#e].sort(th).map(e=>this.#t[String(e)]).filter(e=>e!==void 0)}#d(){return[...this.#r].sort(zG).map(e=>this.#a[e]).filter(e=>e!==void 0)}#c(e){let n=String(e.id);this.#t[n]===void 0&&this.#e.push(e.id);let r=e.importance,i=e.retention;this.#t[n]={content:e.content,generation:e.generation,id:e.id,label:e.label,sentenceId:e.sentenceId,sentenceIds:[...e.sentenceIds],wordsCount:e.wordsCount,weight:0,...r===void 0?{}:{importance:r},...i===void 0?{}:{retention:i}}}#l(e){let n=W(e.fromId,e.toId);this.#a[n]===void 0&&this.#r.push(n);let r=e.strength;this.#a[n]={fromId:e.fromId,toId:e.toId,weight:0,...r===void 0?{}:{strength:r}}}}});var jk=c(()=>{"use strict";Fk()});function Gk(t){let e=t.document??t.workspace;if(e===void 0)throw new Error("SerialGeneration requires a document");return e}async function nh(t,e){let n=await t.serials.getById(e);if(n===void 0)throw new Error(`Chapter ${e} does not exist. Use \`wg <archive-uri>/chapter list\` to discover chapter ids.`);return n}var rh=c(()=>{"use strict"});function Bk(){return Object.create(null)}function ih(t,e,n,r){n.accept(r),t.push(...r.chunks);for(let i of r.edges)t0(e,i.fromId,i.toId)}function t0(t,e,n){let r=t[String(e)]??[];r.includes(n)||(t[String(e)]=[...r,n].sort(n0))}function n0(t,e){return t-e}var Uk=c(()=>{"use strict"});async function ti(t,e,n,r={}){let i=t.getSerialFragments(e);await t.clearSerialDerivedArtifacts(e),await i.writeTextStream(await Sx(n),{...r.segmenter===void 0?{}:{segmenter:r.segmenter}}),await t.serials.bumpRevision(e)}var ah=c(()=>{"use strict";jd()});var ni,oh,zd=c(()=>{"use strict";ni=class{#e;#t;#n;constructor(e,n,r){this.#e=n,this.#t=r,this.#n=new oh(e,n)}get id(){return this.#e}getSummary(){return this.#t}getTopology(){return this.#n}},oh=class{#e;#t;#n;#r;#a;#i;#o;constructor(e,n){this.#e=e.chunks,this.#t=e.fragmentGroups,this.#n=e.readingEdges,this.#r=n,this.#a=e.snakeChunks,this.#i=e.snakeEdges,this.#o=e.snakes}async listChunks(){return await this.#e.listBySerial(this.#r)}async listEdges(){return await this.#n.listBySerial(this.#r)}async listGroups(){return await this.#t.listBySerial(this.#r)}async listSnakeChunks(e){let n=await this.#o.getById(e);if(n===void 0||n.serialId!==this.#r)throw new Error(`Snake ${e} does not belong to this serial`);return await this.#a.listBySnake(e)}async listSnakeEdges(){return await this.#i.listBySerial(this.#r)}async listSnakes(){return await this.#o.listBySerial(this.#r)}}});var Nn,Hk=c(()=>{"use strict";Ii();Ri();Hp();Wd();jk();jd();Fd();rh();Uk();ah();zd();Nn=class{#e=320;#t;#n=new Ae(1);#r;#a;#i;#o=new Ae(1);#s;#d;#c=1;constructor(e){let n=Gk(e);this.#t=n.fragmentGroups,this.#s=e.llm,this.#r=e.logDirPath,this.#a=n.serials,this.#d=e.segmenter,this.#i=n}async generate(e,n,r){let i=await this.#f();return await ti(this.#i,i,e,{...this.#d===void 0?{}:{segmenter:this.#d}}),await this.#l(i,n,r)}async create(e,n){return await this.generate(e,n)}async generateInto(e,n,r,i){return await this.#b(e),await ti(this.#i,e,n,{...this.#d===void 0?{}:{segmenter:this.#d}}),await this.#l(e,r,i)}async buildTopologyInto(e,n,r){await this.#m(e,n.extractionPrompt,n.userLanguage,r)}async buildSummary(e,n={}){let r=await this.#p(e,n.userLanguage);return new ni(this.#i,e,r)}async#l(e,n,r){await this.#m(e,n.extractionPrompt,n.userLanguage,r);let i=await this.#p(e,n.userLanguage);return await r?.complete(),new ni(this.#i,e,i)}async#u(){return await this.#n.use(()=>{let e=this.#c;return this.#c+=1,e})}async#p(e,n){if(!(await nh(this.#i,e)).topologyReady)throw new Error(`Serial ${e} is not ready for summary`);let i=await this.#i.readSummary(e);if(i!==void 0)return i;let a=this.#g(e),o=await a.listFragmentIds();if(o.length<=1){let l=await Ex(a,o);return await this.#o.use(async()=>await this.#i.writeSummary(e,l)),l}let s=await this.#t.listGroupIdsForSerial(e),d=[];for(let l of s){let m=await Za({...this.#y({groupId:l,serialId:e,userLanguage:n})});m.trim()!==""&&d.push(m)}let u=d.join(`
|
|
1805
2006
|
|
|
1806
|
-
`);return await this.#o.use(async()=>await this.#
|
|
2007
|
+
`);return await this.#o.use(async()=>await this.#i.writeSummary(e,u)),u}async#m(e,n,r,i){let a=new Va({attention:{capacity:7,generationDecayFactor:.5,idGenerator:async()=>await this.#u()},extractionGuidance:n,llm:this.#s,scopes:Iu,sentenceTextSource:this.#i,...this.#d===void 0?{}:{segmenter:this.#d},...r===void 0?{}:{userLanguage:r}}),o=new ao(this.#i,e,3840),s=[],d=Bk(),u=this.#i.getSerialFragments(e);for(let l of await bx(u,this.#e)){let m=wx(l.sentences);await this.#h({allChunks:s,fragment:{startSentenceIndex:l.startSentenceIndex,sentences:l.sentences},reader:a,serialId:e,successorIdsByChunkId:d,topology:o}),await i?.advance(m)}await this.#o.use(async()=>{await o.finalize(),await this.#a.setTopologyReady(e)})}async#h(e){let n=e.fragment.sentences.map((o,s)=>({sentenceId:[e.serialId,e.fragment.startSentenceIndex+s],text:o.text,wordsCount:o.wordsCount})),r=n.map(o=>o.text).join(" "),i=await e.reader.extractUserFocused({sentences:n,text:r}),a=await e.reader.extractBookCoherence({sentences:n,text:r,userFocusedChunks:i.delta.chunks});ih(e.allChunks,e.successorIdsByChunkId,e.topology,i.delta),ih(e.allChunks,e.successorIdsByChunkId,e.topology,a),e.reader.completeFragment({allChunks:e.allChunks,getSuccessorChunkIds:o=>e.successorIdsByChunkId[String(o)]??[]})}async#f(){return await this.#n.use(async()=>await this.#a.create())}async#b(e){await this.#n.use(async()=>await this.#a.createWithId(e))}#g(e){return this.#i.getSerialFragments(e)}#y(e){return{compressionRatio:.2,groupId:e.groupId,llm:this.#s,maxClues:10,maxIterations:5,scopes:ki,serialId:e.serialId,document:this.#i,...this.#r===void 0?{}:{logDirPath:this.#r},...e.userLanguage===void 0?{}:{userLanguage:e.userLanguage}}}}});var qk=c(()=>{"use strict";rh();zd()});var $k=c(()=>{"use strict";Ix();Hk();Fd();qk();ah();zd()});var ri=c(()=>{"use strict";$k()});var Kk=c(()=>{"use strict";st();Ii();ri();cn()});import{mkdir as c0,writeFile as u0}from"fs/promises";import{dirname as l0}from"path";async function sh(t){let e=await t.document.openSession(async n=>await m0(n));await c0(l0(t.path),{recursive:!0}),await u0(t.path,e,"utf8")}async function m0(t){let e=await t.readToc();if(e===void 0)throw new Error("Document TOC is missing");let n=await Jk(t,e.items);return n.length===0?"":`${n.join(`
|
|
1807
2008
|
|
|
1808
2009
|
`)}
|
|
1809
|
-
`}async function
|
|
2010
|
+
`}async function Jk(t,e){let n=[];for(let r of e){let i=await p0(t,r);i!==void 0&&n.push(i)}return n}async function p0(t,e){let n=[e.title?.trim()].filter(i=>i!==void 0&&i!=="");if(e.serialId!==void 0){let i=await t.readSummary(e.serialId);if(i===void 0)throw new Error(`Chapter ${e.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()!==""&&n.push(i.trim())}let r=await Jk(t,e.children);if(!(n.length===0&&r.length===0))return[...n,...r].join(`
|
|
1810
2011
|
|
|
1811
|
-
`)}var
|
|
2012
|
+
`)}var zk=c(()=>{"use strict"});function Qd(t){return t.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">").replaceAll('"',""").replaceAll("'","'")}function Xd(t){let e=t?.trim();return e===void 0||e===""?"und":e}var Vd=c(()=>{"use strict"});function Yd(){return Qk??=Ei(en()),Qk}function Xk(t){return Yd().render("output/epub/cover.xhtml",t)}function Vk(t){return Yd().render("output/epub/nav.xhtml",t)}function Yk(t){return Yd().render("output/epub/package.opf.xml",t)}function dh(t){return Yd().render("output/epub/section.xhtml",t)}var Qk,oo=c(()=>{"use strict";ns();Zo()});import{createWriteStream as h0}from"fs";import{dirname as f0,extname as g0}from"path";import{finished as Zk}from"stream/promises";import{mkdir as y0}from"fs/promises";import{ZipFile as b0}from"yazl";async function eR(t,e){await y0(f0(t),{recursive:!0});let n=new b0;n.addBuffer(Buffer.from("application/epub+zip"),"mimetype",{compress:!1}),n.addBuffer(Buffer.from(S0,"utf8"),"META-INF/container.xml"),n.addBuffer(Buffer.from(e.packageOpf,"utf8"),"OEBPS/package.opf"),n.addBuffer(Buffer.from(e.navXhtml,"utf8"),"OEBPS/nav.xhtml");for(let o of e.sections)n.addBuffer(Buffer.from(o.xhtml,"utf8"),`OEBPS/${o.href}`);if(e.cover!==void 0){let o=ch(e.cover),s=Xd(e.meta.language);n.addBuffer(Buffer.from(e.cover.data),`OEBPS/${o}`),n.addBuffer(Buffer.from(w0(e.meta,o,s),"utf8"),"OEBPS/text/cover.xhtml")}n.end();let r=h0(t),i=Zk(r),a=Zk(n.outputStream);n.outputStream.pipe(r),await Promise.all([i,a])}function ch(t){return`images/cover${E0(t)}`}function w0(t,e,n){let r=t.title?.trim()||"Untitled";return Xk({coverImageHref:e,language:n,title:r})}function E0(t){let e=g0(t.path).toLowerCase();if(e!=="")return e;switch(t.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 S0,uh=c(()=>{"use strict";Vd();oo();S0=`<?xml version="1.0" encoding="UTF-8"?>
|
|
1812
2013
|
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
|
|
1813
2014
|
<rootfiles>
|
|
1814
2015
|
<rootfile full-path="OEBPS/package.opf" media-type="application/oebps-package+xml"/>
|
|
1815
2016
|
</rootfiles>
|
|
1816
2017
|
</container>
|
|
1817
|
-
`});function
|
|
1818
|
-
${t.map(e=>
|
|
2018
|
+
`});function tR(t,e){let n=t.title?.trim()||"Untitled";return{href:"text/section-1.xhtml",id:"section-1",title:n,xhtml:dh({language:e,paragraphs:[],title:n})}}function nR(t,e,n,r){let i=n?.trim()||`Section ${t}`;return{href:`text/serial-${t}.xhtml`,id:`serial-${t}`,title:i,xhtml:dh({language:e,paragraphs:I0(r.trim()),title:i})}}function I0(t){return t.split(/\n\s*\n/gu).map(e=>e.replace(/\s*\n\s*/gu," ").trim()).filter(e=>e!=="")}var rR=c(()=>{"use strict";oo()});function lh(t,e){return t.map(n=>({children:lh(n.children,e),href:n.serialId===void 0?void 0:e.get(n.serialId)?.href,title:n.title?.trim()||`Section ${n.serialId??1}`}))}function iR(t,e,n){let r=t.title?.trim()||"Untitled";return Vk({itemsMarkup:x0(n),language:e,title:r})}function x0(t){return t.length===0?"<ol></ol>":`<ol>
|
|
2019
|
+
${t.map(e=>aR(e,1)).join(`
|
|
1819
2020
|
`)}
|
|
1820
|
-
</ol>`}function
|
|
1821
|
-
${
|
|
1822
|
-
${t.children.map(a=>
|
|
2021
|
+
</ol>`}function aR(t,e){let n=" ".repeat(e),r=t.href===void 0?`<span>${Qd(t.title)}</span>`:`<a href="${Qd(t.href)}">${Qd(t.title)}</a>`,i=t.children.length===0?"":`
|
|
2022
|
+
${n} <ol>
|
|
2023
|
+
${t.children.map(a=>aR(a,e+2)).join(`
|
|
1823
2024
|
`)}
|
|
1824
|
-
${r} </ol>`;return`${r}<li>${n}${i}</li>`}var ZI=d(()=>{"use strict";Sc();Na()});function ex(t){let e=t.meta.identifier?.trim()||`urn:uuid:${crypto.randomUUID()}`,r=t.meta.title?.trim()||"Untitled";return $I({authors:t.meta.authors,coverImageHref:t.coverImageHref,coverMediaType:t.coverMediaType,coverPageHref:t.coverPageHref,description:t.meta.description,identifier:e,language:t.language,modifiedAt:t.modifiedAt,publishedAt:t.meta.publishedAt,publisher:t.meta.publisher,sections:t.sections.map(n=>({href:n.href,id:n.id})),title:r,version:kp})}var kp,Ip=d(()=>{"use strict";Na();kp="3.0"});async function tx(t){let[e,r,n]=await Promise.all([t.readBookMeta(),t.readToc(),t.readCover()]);if(e===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=bc(e.language),o=await rx(t,a,r.items,i),s=Ep(r.items,i);o.length===0&&o.push(zI(e,a));let c=n===void 0?void 0:Sp(n),u=n===void 0?void 0:"text/cover.xhtml";return{cover:n,meta:e,navXhtml:VI(e,a,s),packageOpf:ex({coverImageHref:c,coverMediaType:n?.mediaType,coverPageHref:u,language:a,meta:e,modifiedAt:new Date().toISOString().replace(/\.\d{3}Z$/u,"Z"),sections:o}),sections:o}}async function rx(t,e,r,n){let i=[];for(let a of r){if(a.serialId!==void 0){let o=await t.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=QI(a.serialId,e,a.title,o);n.set(a.serialId,s),i.push(s)}i.push(...await rx(t,e,a.children,n))}return i}var nx=d(()=>{"use strict";wp();XI();ZI();Ip();Sc()});async function xp(t){let e=await t.document.openSession(async r=>await tx(r));await JI(t.path,e)}var ix=d(()=>{"use strict";wp();nx();Ip()});var ax=d(()=>{"use strict";BI();ix()});function ox(t,e){return t.serialId===e||t.children.some(r=>ox(r,e))}async function lj(t){mj(t)&&await t.flush()}function mj(t){return"flush"in t&&typeof t.flush=="function"}async function sx(t,e,r=[]){let n=[];for(let i of e){let a=i.title?.trim()||`Chapter ${i.serialId??"group"}`,o=[...r,a];i.serialId!==void 0&&await t.readSummary(i.serialId)!==void 0&&n.push({fragmentCount:(await t.getSerialFragments(i.serialId).listFragmentIds()).length,serialId:i.serialId,title:a,tocPath:o}),n.push(...await sx(t,i.children,o))}return n}var Sr,Ec=d(()=>{"use strict";ax();ze();Sr=class{#e;#t;constructor(e,r){this.#e=e,this.#t=r}async exportEpub(e){await xp({document:this.#e,path:e})}async exportText(e){await gp({document:this.#e,path:e})}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 Ni(this.#t)}async readChapterStage(e){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=>ox(o,e)))throw new Error(`Chapter ${e} does not exist. Use \`wg <archive-uri>/chapter list\` to discover chapter ids.`);return await r.readSummary(e)!==void 0?"summarized":(await r.serials.getById(e))?.topologyReady===!0?"graphed":(await r.getSerialFragments(e).listFragmentIds()).length>0?"sourced":"planned"})}async listSerials(){return await this.#e.openSession(async e=>{let r=await e.readToc();if(r===void 0)throw new Error("Document TOC is missing");return await sx(e,r.items)})}async readSerialSummary(e){return await this.#e.openSession(async r=>{if(await r.serials.getById(e)===void 0)throw new Error(`No completed summary exists for id ${e}. Use \`wg wikg://<archive.wikg>/chapter list\` to discover chapter ids, then \`wg wikg://<archive.wikg>/chapter/${e}/summary get\` after summary is ready.`);let i=await r.readSummary(e);if(i===void 0)throw new Error(`Chapter ${e} 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(e){await lj(this.#e),await on(this.#t,e)}}});import{rm as Gae}from"fs/promises";import{resolve as Uae}from"path";var cx=d(()=>{"use strict";ut();Zt();Zt();L();rt();Hw();Mn();jI();Ec()});function kc(t={}){return Object.freeze({"serial-generation/editor-compress":Ma(Da["serial-generation/editor-compress"],t),"serial-generation/editor-review":Ma(Da["serial-generation/editor-review"],t),"serial-generation/editor-review-guide":Ma(Da["serial-generation/editor-review-guide"],t),"serial-generation/reader-choice":Ma(Da["serial-generation/reader-choice"],t),"serial-generation/reader-extraction":Ma(Da["serial-generation/reader-extraction"],t)})}function Ma(t,e){return Object.freeze({...t,...e.temperature===void 0?{}:{temperature:e.temperature},...e.topP===void 0?{}:{topP:e.topP}})}var Da,Rp=d(()=>{"use strict";ai();Da=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 Re(t){let e=t?.trim();return e===void 0||e===""?Ic:e}function wr(t){let e=t?.trim();return e===void 0||e===""?xc:e}var Ic,xc,Bt=d(()=>{"use strict";Ic="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.",xc=["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 vp=d(()=>{"use strict";Bt()});var foe,ux=d(()=>{"use strict";Po();_e();qd();cx();Rp();ze();vp();foe=Kt()});var Tp,_p=d(()=>{"use strict";Tp=["queued","running","canceling","paused","succeeded","failed","canceled"]});import{mkdir as Rc}from"fs/promises";import{join as ft}from"path";function lx(){return ft(Wa(),"job.sqlite")}async function mx(t){let e=ft(Cp(),t);return await Rc(e,{recursive:!0}),e}function Cp(){return ft(Wa(),"work")}async function px(t){let e=ft(pj(),t);return await Rc(e,{recursive:!0}),e}function pj(){return ft(Wa(),"cache")}async function hx(t){let e=ft(hj(),t);return await Rc(e,{recursive:!0}),e}function hj(){return ft(Wa(),"logs")}async function fx(t){let e=ft(Wa(),"events");return await Rc(e,{recursive:!0}),ft(e,`${t}.ndjson`)}function Wa(){return Mr()}var vc=d(()=>{"use strict";se()});import{createHash as fj}from"crypto";import{resolve as yj}from"path";function Tc(t){return fj("sha256").update(yj(t)).digest("hex")}function Fa(t){try{return process.kill(t,0),!0}catch{return!1}}function yx(t){return t instanceof Error?{message:t.message,name:t.name,stack:t.stack}:t}async function gx(t){await new Promise(e=>{setTimeout(e,t)})}var Wn=d(()=>{"use strict"});function Pp(t,e){return e===void 0?{}:{[t]:e}}function Lp(t,e){if(!(t===void 0&&e===void 0))return(t??0)+(e??0)}function gj(t){return t.phaseDetail??t.unit}function bj(t){if(t.phaseDetail!==void 0)return t.phaseDetail;switch(t.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 Ap(t,e){return e<=0?Math.max(0,t):Math.min(e,Math.max(0,t))}var _c,Ut,Op=d(()=>{"use strict";_c=class{#e;#t;#r;#n=4;#i=5e3;#a=0;#o=0;#s=0;#c;#l=new Map;#u;#d={};#p=0;#m=Promise.resolve();#h=0;#f=0;#b=Promise.resolve();constructor(e,r,n){this.#e=e,this.#t=r,this.#r=n}async addOutputCharacters(e){await this.#y(async()=>{await this.throwIfStopped(),this.#s+=e,await this.#g()})}async addTokenUsage(e){await this.#y(async()=>{await this.throwIfStopped(),this.#d={...Pp("cacheReadTokens",Lp(this.#d.cacheReadTokens,e.cacheReadTokens)),...Pp("inputTokens",Lp(this.#d.inputTokens,e.inputTokens)),...Pp("outputTokens",Lp(this.#d.outputTokens,e.outputTokens))},await this.#g(!0)})}async setTotals(e){await this.#y(async()=>{await this.throwIfStopped(),this.#h=e.totalGraphWords??this.#h,this.#f=e.totalReadingSummaryWords??this.#f,await this.#g(!0)})}async stepStarted(e){await this.#y(async()=>{await this.throwIfStopped(),this.#u=e,this.#c=void 0,this.#l.clear(),await this.#r.markBuildJobStep(this.#e.jobId,e),await this.#r.appendBuildJobEvent(this.#e,{at:Date.now(),jobId:this.#e.jobId,seq:0,step:e,type:"step_started"}),await this.#g(!0)})}async stepCompleted(e){await this.#y(async()=>{await this.throwIfStopped(),this.#u=e,this.#c=void 0,this.#l.clear(),await this.#r.appendBuildJobEvent(this.#e,{at:Date.now(),jobId:this.#e.jobId,seq:0,step:e,type:"step_completed"}),await this.#g(!0)})}async updateWords(e){await this.#y(async()=>{await this.throwIfStopped(),this.#a=e.graphWords===void 0?this.#a:Ap(e.graphWords,this.#h),this.#p=e.readingSummaryWords===void 0?this.#p:Ap(e.readingSummaryWords,this.#f),await this.#g()})}async updatePhase(e){await this.#y(async()=>{await this.throwIfStopped(),this.#c!==e.phase&&this.#l.clear(),this.#c=e.phase,this.#l.set(gj(e),{done:Ap(e.done,e.total),name:bj(e),total:Math.max(0,e.total),unit:e.unit}),await this.#g(e.force??!0)})}async throwIfStopped(){let e=this.#m.then(async()=>{let r=await this.#r.readBuildJobForStopCheck(this.#e.jobId);if(!(r.state==="running"&&r.ownerId===this.#t))throw new Ut(`Job ${this.#e.jobId} is ${r.state}. Stop current worker execution.`)});this.#m=e.catch(()=>{}),await e}async#y(e){let r=this.#b.then(e,e);this.#b=r.catch(()=>{}),await r}async#g(e=!1){let r=Date.now();if(!e&&r-this.#o<this.#i)return;this.#o=r;let n=this.#E();await this.#r.appendBuildJobEvent(this.#e,{at:r,counters:this.#S(),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"})}#S(){let e=this.#w();return[...e===void 0?[]:[e],...this.#l.values()]}#w(){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.#p,name:"words",total:this.#f,unit:"word"};case void 0:return}}#E(){let e=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},...e===void 0?{}:{outputTokens:e}};return Object.keys(r).length===0?void 0:r}};Ut=class extends Error{constructor(e){super(e),this.name="BuildJobStoppedError"}}});import{appendFile as Sj,readFile as wj}from"fs/promises";async function Er(t){let e;try{e=await wj(t.eventsPath,"utf8")}catch{return[]}return e.split(`
|
|
1825
|
-
`).filter(
|
|
1826
|
-
`,"utf8")}async function
|
|
2025
|
+
${n} </ol>`;return`${n}<li>${r}${i}</li>`}var oR=c(()=>{"use strict";Vd();oo()});function sR(t){let e=t.meta.identifier?.trim()||`urn:uuid:${crypto.randomUUID()}`,n=t.meta.title?.trim()||"Untitled";return Yk({authors:t.meta.authors,coverImageHref:t.coverImageHref,coverMediaType:t.coverMediaType,coverPageHref:t.coverPageHref,description:t.meta.description,identifier:e,language:t.language,modifiedAt:t.modifiedAt,publishedAt:t.meta.publishedAt,publisher:t.meta.publisher,sections:t.sections.map(r=>({href:r.href,id:r.id})),title:n,version:mh})}var mh,ph=c(()=>{"use strict";oo();mh="3.0"});async function dR(t){let[e,n,r]=await Promise.all([t.readBookMeta(),t.readToc(),t.readCover()]);if(e===void 0)throw new Error("Archive metadata is missing");if(n===void 0)throw new Error("Document TOC is missing");let i=new Map,a=Xd(e.language),o=await cR(t,a,n.items,i),s=lh(n.items,i);o.length===0&&o.push(tR(e,a));let d=r===void 0?void 0:ch(r),u=r===void 0?void 0:"text/cover.xhtml";return{cover:r,meta:e,navXhtml:iR(e,a,s),packageOpf:sR({coverImageHref:d,coverMediaType:r?.mediaType,coverPageHref:u,language:a,meta:e,modifiedAt:new Date().toISOString().replace(/\.\d{3}Z$/u,"Z"),sections:o}),sections:o}}async function cR(t,e,n,r){let i=[];for(let a of n){if(a.serialId!==void 0){let o=await t.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=nR(a.serialId,e,a.title,o);r.set(a.serialId,s),i.push(s)}i.push(...await cR(t,e,a.children,r))}return i}var uR=c(()=>{"use strict";uh();rR();oR();ph();Vd()});async function hh(t){let e=await t.document.openSession(async n=>await dR(n));await eR(t.path,e)}var lR=c(()=>{"use strict";uh();uR();ph()});var mR=c(()=>{"use strict";zk();lR()});function pR(t,e){return t.serialId===e||t.children.some(n=>pR(n,e))}async function k0(t){R0(t)&&await t.flush()}function R0(t){return"flush"in t&&typeof t.flush=="function"}async function hR(t,e,n=[]){let r=[];for(let i of e){let a=i.title?.trim()||`Chapter ${i.serialId??"group"}`,o=[...n,a];i.serialId!==void 0&&await t.readSummary(i.serialId)!==void 0&&r.push({fragmentCount:(await t.getSerialFragments(i.serialId).listFragmentIds()).length,serialId:i.serialId,title:a,tocPath:o}),r.push(...await hR(t,i.children,o))}return r}var Dn,Zd=c(()=>{"use strict";mR();Ye();Dn=class{#e;#t;constructor(e,n){this.#e=e,this.#t=n}async exportEpub(e){await hh({document:this.#e,path:e})}async exportText(e){await sh({document:this.#e,path:e})}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 aa(this.#t)}async readChapterStage(e){return await this.#e.openSession(async n=>{let r=await n.readToc();if(r===void 0)throw new Error("Document TOC is missing");if(!r.items.some(o=>pR(o,e)))throw new Error(`Chapter ${e} does not exist. Use \`wg <archive-uri>/chapter list\` to discover chapter ids.`);return await n.readSummary(e)!==void 0?"summarized":(await n.serials.getById(e))?.topologyReady===!0?"graphed":(await n.getSerialFragments(e).listFragmentIds()).length>0?"sourced":"planned"})}async listSerials(){return await this.#e.openSession(async e=>{let n=await e.readToc();if(n===void 0)throw new Error("Document TOC is missing");return await hR(e,n.items)})}async readSerialSummary(e){return await this.#e.openSession(async n=>{if(await n.serials.getById(e)===void 0)throw new Error(`No completed summary exists for id ${e}. Use \`wg wikg://<archive.wikg>/chapter list\` to discover chapter ids, then \`wg wikg://<archive.wikg>/chapter/${e}/summary get\` after summary is ready.`);let i=await n.readSummary(e);if(i===void 0)throw new Error(`Chapter ${e} 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(e){await k0(this.#e),await Pr(this.#t,e)}}});import{rm as Yde}from"fs/promises";import{resolve as ece}from"path";var fR=c(()=>{"use strict";yt();cn();cn();N();st();XE();ri();Kk();Zd()});function ec(t={}){return Object.freeze({"serial-generation/editor-compress":co(so["serial-generation/editor-compress"],t),"serial-generation/editor-review":co(so["serial-generation/editor-review"],t),"serial-generation/editor-review-guide":co(so["serial-generation/editor-review-guide"],t),"serial-generation/reader-choice":co(so["serial-generation/reader-choice"],t),"serial-generation/reader-extraction":co(so["serial-generation/reader-extraction"],t)})}function co(t,e){return Object.freeze({...t,...e.temperature===void 0?{}:{temperature:e.temperature},...e.topP===void 0?{}:{topP:e.topP}})}var so,fh=c(()=>{"use strict";Ri();so=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 _e(t){let e=t?.trim();return e===void 0||e===""?tc:e}function Mn(t){let e=t?.trim();return e===void 0||e===""?nc:e}var tc,nc,zt=c(()=>{"use strict";tc="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.",nc=["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 gh=c(()=>{"use strict";zt()});var Tce,yR=c(()=>{"use strict";ns();Pe();Eu();fR();fh();Ye();gh();Tce=en()});var yh,bh=c(()=>{"use strict";yh=["queued","running","canceling","paused","succeeded","failed","canceled"]});import{mkdir as rc}from"fs/promises";import{join as It}from"path";function bR(){return It(uo(),"job.sqlite")}async function SR(t){let e=It(Sh(),t);return await rc(e,{recursive:!0}),e}function Sh(){return It(uo(),"work")}async function wR(t){let e=It(v0(),t);return await rc(e,{recursive:!0}),e}function v0(){return It(uo(),"cache")}async function ER(t){let e=It(T0(),t);return await rc(e,{recursive:!0}),e}function T0(){return It(uo(),"logs")}async function IR(t){let e=It(uo(),"events");return await rc(e,{recursive:!0}),It(e,`${t}.ndjson`)}function uo(){return nr()}var ic=c(()=>{"use strict";me()});import{createHash as _0}from"crypto";import{resolve as C0}from"path";function ac(t){return _0("sha256").update(C0(t)).digest("hex")}function lo(t){try{return process.kill(t,0),!0}catch{return!1}}function xR(t){return t instanceof Error?{message:t.message,name:t.name,stack:t.stack}:t}async function kR(t){await new Promise(e=>{setTimeout(e,t)})}var ii=c(()=>{"use strict"});function wh(t,e){return e===void 0?{}:{[t]:e}}function Eh(t,e){if(!(t===void 0&&e===void 0))return(t??0)+(e??0)}function P0(t){return t.phaseDetail??t.unit}function A0(t){if(t.phaseDetail!==void 0)return t.phaseDetail;switch(t.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 Ih(t,e){return e<=0?Math.max(0,t):Math.min(e,Math.max(0,t))}var oc,Qt,xh=c(()=>{"use strict";oc=class{#e;#t;#n;#r=4;#a=5e3;#i=0;#o=0;#s=0;#d;#c=new Map;#l;#u={};#p=0;#m=Promise.resolve();#h=0;#f=0;#b=Promise.resolve();constructor(e,n,r){this.#e=e,this.#t=n,this.#n=r}async addOutputCharacters(e){await this.#g(async()=>{await this.throwIfStopped(),this.#s+=e,await this.#y()})}async addTokenUsage(e){await this.#g(async()=>{await this.throwIfStopped(),this.#u={...wh("cacheReadTokens",Eh(this.#u.cacheReadTokens,e.cacheReadTokens)),...wh("inputTokens",Eh(this.#u.inputTokens,e.inputTokens)),...wh("outputTokens",Eh(this.#u.outputTokens,e.outputTokens))},await this.#y(!0)})}async setTotals(e){await this.#g(async()=>{await this.throwIfStopped(),this.#h=e.totalGraphWords??this.#h,this.#f=e.totalReadingSummaryWords??this.#f,await this.#y(!0)})}async stepStarted(e){await this.#g(async()=>{await this.throwIfStopped(),this.#l=e,this.#d=void 0,this.#c.clear(),await this.#n.markBuildJobStep(this.#e.jobId,e),await this.#n.appendBuildJobEvent(this.#e,{at:Date.now(),jobId:this.#e.jobId,seq:0,step:e,type:"step_started"}),await this.#y(!0)})}async stepCompleted(e){await this.#g(async()=>{await this.throwIfStopped(),this.#l=e,this.#d=void 0,this.#c.clear(),await this.#n.appendBuildJobEvent(this.#e,{at:Date.now(),jobId:this.#e.jobId,seq:0,step:e,type:"step_completed"}),await this.#y(!0)})}async updateWords(e){await this.#g(async()=>{await this.throwIfStopped(),this.#i=e.graphWords===void 0?this.#i:Ih(e.graphWords,this.#h),this.#p=e.readingSummaryWords===void 0?this.#p:Ih(e.readingSummaryWords,this.#f),await this.#y()})}async updatePhase(e){await this.#g(async()=>{await this.throwIfStopped(),this.#d!==e.phase&&this.#c.clear(),this.#d=e.phase,this.#c.set(P0(e),{done:Ih(e.done,e.total),name:A0(e),total:Math.max(0,e.total),unit:e.unit}),await this.#y(e.force??!0)})}async throwIfStopped(){let e=this.#m.then(async()=>{let n=await this.#n.readBuildJobForStopCheck(this.#e.jobId);if(!(n.state==="running"&&n.ownerId===this.#t))throw new Qt(`Job ${this.#e.jobId} is ${n.state}. Stop current worker execution.`)});this.#m=e.catch(()=>{}),await e}async#g(e){let n=this.#b.then(e,e);this.#b=n.catch(()=>{}),await n}async#y(e=!1){let n=Date.now();if(!e&&n-this.#o<this.#a)return;this.#o=n;let r=this.#E();await this.#n.appendBuildJobEvent(this.#e,{at:n,counters:this.#S(),jobId:this.#e.jobId,...this.#d===void 0?{}:{phase:this.#d},seq:0,...this.#l===void 0?{}:{step:this.#l},...r===void 0?{}:{tokens:r},type:"status_snapshot"})}#S(){let e=this.#w();return[...e===void 0?[]:[e],...this.#c.values()]}#w(){switch(this.#l){case"reading-graph":return this.#h<=0?void 0:{done:this.#i,name:"words",total:this.#h,unit:"word"};case"reading-summary":return this.#f<=0?void 0:{done:this.#p,name:"words",total:this.#f,unit:"word"};case void 0:return}}#E(){let e=this.#u.outputTokens??(this.#s===0?void 0:Math.floor(this.#s/this.#r)),n={...this.#u.cacheReadTokens===void 0?{}:{cacheReadTokens:this.#u.cacheReadTokens},...this.#u.inputTokens===void 0?{}:{inputTokens:this.#u.inputTokens},...e===void 0?{}:{outputTokens:e}};return Object.keys(n).length===0?void 0:n}};Qt=class extends Error{constructor(e){super(e),this.name="BuildJobStoppedError"}}});import{appendFile as L0,readFile as O0}from"fs/promises";async function Wn(t){let e;try{e=await O0(t.eventsPath,"utf8")}catch{return[]}return e.split(`
|
|
2026
|
+
`).filter(n=>n.trim()!=="").map(n=>JSON.parse(n))}async function se(t,e){let n=await N0(t)+1,r={...e,jobId:t.jobId,seq:n};await L0(t.eventsPath,`${JSON.stringify(r)}
|
|
2027
|
+
`,"utf8")}async function N0(t){return(await Wn(t)).at(-1)?.seq??0}var mo=c(()=>{"use strict"});var RR,vR=c(()=>{"use strict";RR=`
|
|
1827
2028
|
CREATE TABLE IF NOT EXISTS build_jobs (
|
|
1828
2029
|
job_id TEXT PRIMARY KEY,
|
|
1829
2030
|
archive_key TEXT NOT NULL,
|
|
@@ -1861,6 +2062,15 @@ ON build_jobs(archive_key, chapter_id)
|
|
|
1861
2062
|
WHERE target IN ('reading-graph', 'reading-summary')
|
|
1862
2063
|
AND state IN ('queued', 'running', 'canceling', 'paused');
|
|
1863
2064
|
|
|
2065
|
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_build_jobs_active_index_chapter_target
|
|
2066
|
+
ON build_jobs(archive_key, chapter_id, target)
|
|
2067
|
+
WHERE target IN (
|
|
2068
|
+
'index-fts',
|
|
2069
|
+
'index-embedding-source',
|
|
2070
|
+
'index-embedding-summary'
|
|
2071
|
+
)
|
|
2072
|
+
AND state IN ('queued', 'running', 'canceling', 'paused');
|
|
2073
|
+
|
|
1864
2074
|
CREATE INDEX IF NOT EXISTS idx_build_jobs_queue
|
|
1865
2075
|
ON build_jobs(state, queue_rank, updated_at);
|
|
1866
2076
|
|
|
@@ -1873,31 +2083,31 @@ CREATE TABLE IF NOT EXISTS build_worker_lease (
|
|
|
1873
2083
|
|
|
1874
2084
|
INSERT OR IGNORE INTO build_worker_lease (id)
|
|
1875
2085
|
VALUES (1);
|
|
1876
|
-
`});function
|
|
2086
|
+
`});function xe(t){let e=M0(Fn(t,"current_step"),"current_step"),n=Fn(t,"owner_id"),r=t.owner_pid===null?void 0:de(t,"owner_pid"),i=t.reading_summary_started_at===null?void 0:de(t,"reading_summary_started_at"),a=t.finished_at===null?void 0:de(t,"finished_at"),o=Fn(t,"error_json"),s=t.input_revision===null?void 0:de(t,"input_revision"),d=Fn(t,"llm_json"),u=Fn(t,"prompt");return{archiveKey:Ue(t,"archive_key"),archivePath:Ue(t,"archive_path"),cachePath:Ue(t,"cache_path"),chapterId:de(t,"chapter_id"),createdAt:de(t,"created_at"),...e===void 0?{}:{currentStep:e},...o===void 0?{}:{errorJSON:o},eventsPath:Ue(t,"events_path"),...a===void 0?{}:{finishedAt:a},jobId:Ue(t,"job_id"),...s===void 0?{}:{inputRevision:s},logPath:Ue(t,"log_path"),...d===void 0?{}:{llmJSON:d},...n===void 0?{}:{ownerId:n},...r===void 0?{}:{ownerPid:r},...u===void 0?{}:{prompt:u},queueRank:de(t,"queue_rank"),state:D0(Ue(t,"state")),...i===void 0?{}:{readingSummaryStartedAt:i},target:TR(Ue(t,"target"),"target"),updatedAt:de(t,"updated_at"),workspacePath:Ue(t,"workspace_path")}}function D0(t){if(yh.includes(t))return t;throw new Error(`Invalid build job state: ${t}`)}function TR(t,e){if(t==="index-embedding-source"||t==="index-embedding-summary"||t==="index-fts"||t==="reading-graph"||t==="knowledge-graph"||t==="reading-summary")return t;throw new Error(`Invalid ${e}: ${t}`)}function M0(t,e){return t===void 0?void 0:TR(t,e)}function _R(t){return t==="knowledge-graph"?"knowledge-graph":t.startsWith("index-")?t:"reading"}function Ue(t,e){let n=t[e];if(typeof n!="string")throw new TypeError(`Expected ${e} to be a string`);return n}function de(t,e){let n=t[e];if(typeof n!="number")throw new TypeError(`Expected ${e} to be a number`);return n}function Fn(t,e){let n=t[e];if(n!=null){if(typeof n!="string")throw new TypeError(`Expected ${e} to be a string`);return n}}var jn=c(()=>{"use strict";bh()});async function U(t,e){let n=await t.queryOne("SELECT * FROM build_jobs WHERE job_id = ?",[e],xe);if(n===void 0)throw new Error(`Build job not found: ${e}`);return n}async function CR(t,e){let n=e.trim();if(n==="")throw new Error("Build job id is empty.");let r=await t.queryAll(`
|
|
1877
2087
|
SELECT job_id
|
|
1878
2088
|
FROM build_jobs
|
|
1879
2089
|
WHERE substr(job_id, 1, ?) = ?
|
|
1880
2090
|
ORDER BY created_at DESC
|
|
1881
|
-
`,[
|
|
2091
|
+
`,[n.length,n],i=>Ue(i,"job_id"));if(r.length===0)throw new Error(`Build job not found: ${e}`);if(r.length>1)throw new Error(`Build job id prefix is ambiguous: ${e}. Matches: ${r.join(", ")}`);return r[0]}async function PR(t){return await t.queryOne("SELECT COALESCE(MAX(queue_rank), 0) AS rank FROM build_jobs",void 0,e=>de(e,"rank"))??0}async function sc(t){return await t.queryOne("SELECT COALESCE(MIN(queue_rank), 0) AS rank FROM build_jobs",void 0,e=>de(e,"rank"))??0}async function F(){return await D(bR(),RR)}async function AR(){return await F()}var ai=c(()=>{"use strict";N();vR();ic();jn()});import{rm as W0}from"fs/promises";async function LR(t,e){let n=Date.now();await t.run(`
|
|
1882
2092
|
UPDATE build_jobs
|
|
1883
2093
|
SET state = 'canceling', updated_at = ?
|
|
1884
2094
|
WHERE job_id = ? AND state = 'running'
|
|
1885
|
-
`,[
|
|
2095
|
+
`,[n,e.jobId]);let r=await U(t,e.jobId);return await se(r,{at:n,jobId:e.jobId,seq:0,state:"canceling",type:"canceling"}),r}async function po(t,e){let n=Date.now();await t.run(`
|
|
1886
2096
|
UPDATE build_jobs
|
|
1887
2097
|
SET state = 'canceled', owner_id = NULL, owner_pid = NULL,
|
|
1888
2098
|
current_step = NULL, finished_at = ?, updated_at = ?
|
|
1889
2099
|
WHERE job_id = ?
|
|
1890
|
-
`,[
|
|
2100
|
+
`,[n,n,e.jobId]);let r=await U(t,e.jobId);return await se(r,{at:n,jobId:e.jobId,seq:0,state:"canceled",type:"canceled"}),r}async function kh(t,e,n){let r=Date.now(),i=xR(n),a=JSON.stringify(i);await t.run(`
|
|
1891
2101
|
UPDATE build_jobs
|
|
1892
2102
|
SET state = 'failed', owner_id = NULL, owner_pid = NULL,
|
|
1893
2103
|
current_step = NULL, finished_at = ?, updated_at = ?, error_json = ?
|
|
1894
2104
|
WHERE job_id = ?
|
|
1895
|
-
`,[
|
|
2105
|
+
`,[r,r,a,e.jobId]),await se(e,{at:r,error:i,jobId:e.jobId,seq:0,state:"failed",type:"failed"})}async function OR(t,e){let n=await F();try{await n.transaction(async()=>{let r=await U(n,t);if(r.ownerId!==e||r.state!=="running")return;let i=Date.now();await n.run(`
|
|
1896
2106
|
UPDATE build_jobs
|
|
1897
2107
|
SET state = 'succeeded', owner_id = NULL, owner_pid = NULL,
|
|
1898
2108
|
current_step = NULL, finished_at = ?, updated_at = ?
|
|
1899
2109
|
WHERE job_id = ?
|
|
1900
|
-
`,[i,i,t]),await
|
|
2110
|
+
`,[i,i,t]),await se(r,{at:i,jobId:t,seq:0,state:"succeeded",type:"succeeded"}),await W0(r.workspacePath,{force:!0,recursive:!0})})}finally{await n.close()}}async function NR(t,e,n){let r=await F();try{await r.transaction(async()=>{let i=await U(r,t);i.ownerId!==e||i.state!=="running"||await kh(r,i,n)})}finally{await r.close()}}async function DR(t,e){let n=await F();try{await n.transaction(async()=>{let r=await U(n,t);r.ownerId!==e||r.state!=="canceling"||await po(n,r)})}finally{await n.close()}}async function MR(t,e){let n=await F();try{await n.run(`
|
|
1901
2111
|
UPDATE build_jobs
|
|
1902
2112
|
SET current_step = ?,
|
|
1903
2113
|
reading_summary_started_at = CASE
|
|
@@ -1906,28 +2116,28 @@ SET current_step = ?,
|
|
|
1906
2116
|
END,
|
|
1907
2117
|
updated_at = ?
|
|
1908
2118
|
WHERE job_id = ?
|
|
1909
|
-
`,[e,e,Date.now(),Date.now(),t])}finally{await
|
|
2119
|
+
`,[e,e,Date.now(),Date.now(),t])}finally{await n.close()}}var dc=c(()=>{"use strict";ii();mo();ai()});import{rm as F0}from"fs/promises";async function ke(t){let e=[];await t.transaction(async()=>{let n=await t.queryAll(`
|
|
1910
2120
|
SELECT *
|
|
1911
2121
|
FROM build_jobs
|
|
1912
2122
|
WHERE state IN ('running', 'canceling')
|
|
1913
2123
|
AND owner_pid IS NOT NULL
|
|
1914
|
-
`,void 0,
|
|
2124
|
+
`,void 0,xe);for(let r of n)if(!(r.ownerPid!==void 0&&lo(r.ownerPid))){if(r.state==="canceling"){await po(t,r);continue}await kh(t,r,{message:"Build worker process disappeared before finishing the job.",name:"BuildJobWorkerLost"}),e.push(r.workspacePath)}});for(let n of e)await F0(n,{force:!0,recursive:!0})}var cc=c(()=>{"use strict";ii();jn();dc()});import{randomUUID as j0}from"crypto";import{resolve as WR}from"path";async function ho(t){let e=await F();try{return await ke(e),await e.transaction(async()=>{let n=WR(t.archivePath),r=ac(n),i=Date.now(),a=await FR(e,{archiveKey:r,chapterId:t.chapterId,target:t.target});if(a!==void 0)return await U0(e,a,t,i);let o=t.jobId??j0(),s=await SR(o),d=await wR(o),u=await ER(o),l=await IR(o),m=t.boost===!0?await sc(e)-1:await PR(e)+1;await e.run(`
|
|
1915
2125
|
INSERT INTO build_jobs (
|
|
1916
2126
|
job_id, archive_key, archive_path, chapter_id, target, state, queue_rank,
|
|
1917
2127
|
workspace_path, cache_path, log_path, events_path, llm_json, prompt,
|
|
1918
2128
|
created_at, updated_at
|
|
1919
2129
|
) VALUES (?, ?, ?, ?, ?, 'queued', ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
1920
|
-
`,[o,n,
|
|
2130
|
+
`,[o,r,n,t.chapterId,t.target,m,s,d,u,l,t.llmJSON??null,t.prompt??null,i,i]);let p=await U(e,o);return await se(p,{at:i,jobId:o,seq:0,state:"queued",type:"created"}),p})}finally{await e.close()}}async function Gn(t){let e=await F();try{return await e.transaction(async()=>{await e.run(`
|
|
1921
2131
|
UPDATE build_jobs
|
|
1922
2132
|
SET input_revision = ?,
|
|
1923
2133
|
updated_at = ?
|
|
1924
2134
|
WHERE job_id = ?
|
|
1925
2135
|
AND owner_id = ?
|
|
1926
2136
|
AND state = 'running'
|
|
1927
|
-
`,[t.currentRevision,Date.now(),t.jobId,t.ownerId])}),await
|
|
2137
|
+
`,[t.currentRevision,Date.now(),t.jobId,t.ownerId])}),await U(e,t.jobId)}finally{await e.close()}}async function fo(t){let e=await z(t.jobId);if(e.state!=="running"||e.ownerId!==t.ownerId)throw new Qt(`Job ${t.jobId} is ${e.state}. Stop current worker execution.`);if(e.inputRevision===void 0)throw new Error(`Job ${t.jobId} has no recorded chapter input revision. Requeue the job before committing build output.`);if(e.inputRevision!==t.currentRevision)throw new Error(`Chapter ${e.chapterId} changed while job ${e.jobId} was running. Requeue the job before committing build output.`)}async function go(t={}){let e=await F();try{await ke(e);let n=[],r=[];return t.archivePath!==void 0&&(n.push(ac(WR(t.archivePath))),r.push("archive_key = ?")),(t.activeOnly===!0||t.all!==!0)&&r.push("state IN ('queued', 'running', 'canceling', 'paused')"),await e.queryAll(`
|
|
1928
2138
|
SELECT *
|
|
1929
2139
|
FROM build_jobs
|
|
1930
|
-
${
|
|
2140
|
+
${r.length===0?"":`WHERE ${r.join(" AND ")}`}
|
|
1931
2141
|
ORDER BY
|
|
1932
2142
|
CASE state
|
|
1933
2143
|
WHEN 'running' THEN 0
|
|
@@ -1938,112 +2148,118 @@ ORDER BY
|
|
|
1938
2148
|
END,
|
|
1939
2149
|
queue_rank ASC,
|
|
1940
2150
|
updated_at DESC
|
|
1941
|
-
`,
|
|
2151
|
+
`,n,xe)}finally{await e.close()}}async function FR(t,e){let n=B0(e.target);return await t.queryOne(`
|
|
1942
2152
|
SELECT *
|
|
1943
2153
|
FROM build_jobs
|
|
1944
2154
|
WHERE archive_key = ?
|
|
1945
2155
|
AND chapter_id = ?
|
|
1946
2156
|
AND state IN ('queued', 'running', 'canceling', 'paused')
|
|
1947
|
-
AND ${
|
|
2157
|
+
AND ${n}
|
|
1948
2158
|
ORDER BY updated_at DESC
|
|
1949
2159
|
LIMIT 1
|
|
1950
|
-
`,[e.archiveKey,e.chapterId],
|
|
2160
|
+
`,[e.archiveKey,e.chapterId],xe)}function B0(t){switch(t){case"knowledge-graph":return"target = 'knowledge-graph'";case"reading-graph":case"reading-summary":return"target IN ('reading-graph', 'reading-summary')";case"index-fts":return"target = 'index-fts'";case"index-embedding-source":return"target = 'index-embedding-source'";case"index-embedding-summary":return"target = 'index-embedding-summary'"}}async function U0(t,e,n,r){let i=e.target==="reading-graph"&&n.target==="reading-summary"?"reading-summary":e.target,a=n.boost===!0&&e.state==="queued"?await sc(t)-1:e.queueRank;if(i===e.target&&a===e.queueRank)return e;await t.run(`
|
|
1951
2161
|
UPDATE build_jobs
|
|
1952
2162
|
SET target = ?, queue_rank = ?, updated_at = ?
|
|
1953
2163
|
WHERE job_id = ?
|
|
1954
|
-
`,[i,a,
|
|
2164
|
+
`,[i,a,r,e.jobId]);let o=await U(t,e.jobId);return i!==e.target&&await se(o,{at:r,from:e.target,jobId:e.jobId,seq:0,to:i,type:"target_changed"}),a!==e.queueRank&&await se(o,{at:r,jobId:e.jobId,seq:0,state:o.state,type:"boosted"}),o}async function z(t){let e=await F();try{return await ke(e),await U(e,t)}finally{await e.close()}}async function Rh(t){let e=await AR();try{return await U(e,t)}finally{await e.close()}}async function yo(t){let e=await F();try{return await ke(e),await CR(e,t)}finally{await e.close()}}async function bo(t){return await H0(t,"paused","paused",{allowedStates:["queued","running"]})}async function So(t){let e=await F();try{return await ke(e),await e.transaction(async()=>{let n=await U(e,t);if(n.state==="queued")return n;if(n.state!=="paused")throw new Error(`Cannot resume ${n.state} job ${t}.`);let r=Date.now();await e.run(`
|
|
2165
|
+
UPDATE build_jobs
|
|
2166
|
+
SET state = ?, updated_at = ?, finished_at = ?,
|
|
2167
|
+
owner_id = NULL,
|
|
2168
|
+
owner_pid = NULL
|
|
2169
|
+
WHERE job_id = ?
|
|
2170
|
+
`,["queued",r,null,t]);let i=await U(e,t);return await se(i,{at:r,jobId:t,seq:0,state:"queued",type:"resumed"}),i})}finally{await e.close()}}async function wo(t){let e=await F();try{return await ke(e),await e.transaction(async()=>{let n=await U(e,t);if(n.state==="running")return await LR(e,n);if(n.state==="queued"||n.state==="paused")return await po(e,n);throw new Error(`Cannot cancel ${n.state} job ${t}.`)})}finally{await e.close()}}async function Eo(t){let e=await F();try{return await ke(e),await e.transaction(async()=>{let n=await U(e,t);if(n.state!=="queued")throw new Error(`Only queued jobs can be boosted. Job ${t} is ${n.state}.`);let r=Date.now();await e.run(`
|
|
1955
2171
|
UPDATE build_jobs
|
|
1956
2172
|
SET queue_rank = ?, updated_at = ?
|
|
1957
2173
|
WHERE job_id = ?
|
|
1958
|
-
`,[await
|
|
2174
|
+
`,[await sc(e)-1,r,t]);let i=await U(e,t);return await se(i,{at:r,jobId:t,seq:0,state:"queued",type:"boosted"}),i})}finally{await e.close()}}async function Io(t,e){let n=await F();try{return await ke(n),await n.transaction(async()=>{let r=await U(n,t);if(!G0.has(r.state))throw new Error(`Cannot change target for ${r.state} job ${t}.`);if(r.target===e)return r;let i=await FR(n,{archiveKey:r.archiveKey,chapterId:r.chapterId,target:e});if(i!==void 0&&i.jobId!==r.jobId)throw new Error(`Chapter ${r.chapterId} already has active ${_R(e)} job ${i.jobId}.`);if(r.target==="reading-summary"&&e==="reading-graph"&&(r.readingSummaryStartedAt!==void 0||r.currentStep==="reading-summary"))throw new Error(`Cannot downgrade job ${t} after summary has started. Cancel it explicitly instead.`);let a=Date.now();await n.run(`
|
|
1959
2175
|
UPDATE build_jobs
|
|
1960
2176
|
SET target = ?, updated_at = ?
|
|
1961
2177
|
WHERE job_id = ?
|
|
1962
|
-
`,[e,a,t]);let o=await
|
|
2178
|
+
`,[e,a,t]);let o=await U(n,t);return await se(o,{at:a,from:r.target,jobId:t,seq:0,to:e,type:"target_changed"}),o})}finally{await n.close()}}async function H0(t,e,n,r){let i=await F();try{return await ke(i),await i.transaction(async()=>{let a=await U(i,t);if(!r.allowedStates.includes(a.state))throw new Error(`Cannot ${q0(n)} ${a.state} job ${t}.`);let o=Date.now();await i.run(`
|
|
1963
2179
|
UPDATE build_jobs
|
|
1964
2180
|
SET state = ?, updated_at = ?, finished_at = ?,
|
|
1965
2181
|
owner_id = CASE WHEN ? = 1 THEN NULL ELSE owner_id END,
|
|
1966
2182
|
owner_pid = CASE WHEN ? = 1 THEN NULL ELSE owner_pid END
|
|
1967
2183
|
WHERE job_id = ?
|
|
1968
|
-
`,[e,o,
|
|
2184
|
+
`,[e,o,r.finished===!0?o:null,r.clearOwner===!0||r.finished===!0?1:0,r.clearOwner===!0||r.finished===!0?1:0,t]);let s=await U(i,t);return await se(s,{at:o,jobId:t,seq:0,state:e,type:n}),s})}finally{await i.close()}}function q0(t){return t==="paused"?"pause":"resume"}var G0,vh=c(()=>{"use strict";ic();ii();xh();mo();ai();cc();dc();jn();G0=new Set(["queued","running","canceling","paused"])});import{resolve as gue}from"path";var jR=c(()=>{"use strict";ii();ai();cc();jn()});import{randomUUID as $0}from"crypto";async function xo(t){let e=await F(),n=`${process.pid}-${$0()}`,r=Math.max(1,t.concurrency),i=t.idleTimeoutMs??1e4,a=!1,o=0,s=Date.now(),d=l=>{a=!0};process.once("SIGINT",d),process.once("SIGTERM",d);let u=setInterval(()=>{GR(n).catch(()=>{})},K0);try{if(!await X0(e,n))return;let m=async()=>{for(;!a;){await GR(n,e),await ke(e),o+=1;let p;try{p=await Q0(e,n)}finally{p===void 0&&(o-=1)}if(p===void 0){if(o===0&&Date.now()-s>=i)break;await kR(500);continue}s=Date.now();try{await J0(p,n,t)}finally{o-=1,o===0&&(s=Date.now())}}};await Promise.all(Array.from({length:r},async()=>await m()))}finally{clearInterval(u),process.removeListener("SIGINT",d),process.removeListener("SIGTERM",d),await V0(e,n),await e.close()}}async function J0(t,e,n){let r=new oc(t,e,{appendBuildJobEvent:se,markBuildJobStep:MR,readBuildJobForStopCheck:Rh}),i=new AbortController,a=setInterval(()=>{z0(t,e,i).catch(()=>{})},500);try{await se(t,{at:Date.now(),jobId:t.jobId,seq:0,state:"running",type:"started"}),await n.executeJob(t,r,{signal:i.signal}),await r.throwIfStopped(),await OR(t.jobId,e)}catch(o){if(o instanceof Qt||i.signal.aborted){await DR(t.jobId,e);return}await NR(t.jobId,e,o)}finally{clearInterval(a)}}async function z0(t,e,n){if(n.signal.aborted)return;let r=await Rh(t.jobId);r.state==="running"&&r.ownerId===e||n.abort(new Qt(`Job ${t.jobId} is ${r.state}. Stop current worker execution.`))}async function Q0(t,e){return await t.transaction(async()=>{let n=await t.queryOne(`
|
|
1969
2185
|
SELECT *
|
|
1970
2186
|
FROM build_jobs
|
|
1971
2187
|
WHERE state = 'queued'
|
|
1972
2188
|
ORDER BY queue_rank ASC, created_at ASC
|
|
1973
2189
|
LIMIT 1
|
|
1974
|
-
`,void 0,
|
|
2190
|
+
`,void 0,xe);if(n===void 0)return;let r=Date.now();return await t.run(`
|
|
1975
2191
|
UPDATE build_jobs
|
|
1976
2192
|
SET state = 'running', owner_id = ?, owner_pid = ?, updated_at = ?
|
|
1977
2193
|
WHERE job_id = ? AND state = 'queued'
|
|
1978
|
-
`,[e,process.pid,n
|
|
2194
|
+
`,[e,process.pid,r,n.jobId]),await U(t,n.jobId)})}async function X0(t,e){return await Y0(t),await t.transaction(async()=>{let n=await t.queryOne(`
|
|
1979
2195
|
SELECT owner_pid
|
|
1980
2196
|
FROM build_worker_lease
|
|
1981
2197
|
WHERE id = 1
|
|
1982
|
-
`,void 0,
|
|
2198
|
+
`,void 0,r=>({ownerPid:r.owner_pid===null?void 0:de(r,"owner_pid")}));return n?.ownerPid!==void 0&&lo(n.ownerPid)?!1:(await t.run(`
|
|
1983
2199
|
UPDATE build_worker_lease
|
|
1984
2200
|
SET owner_id = ?, owner_pid = ?, heartbeat_at = ?
|
|
1985
2201
|
WHERE id = 1
|
|
1986
|
-
`,[e,process.pid,Date.now()]),!0)})}async function
|
|
2202
|
+
`,[e,process.pid,Date.now()]),!0)})}async function GR(t,e){let n=e??await F();try{await n.run(`
|
|
1987
2203
|
UPDATE build_worker_lease
|
|
1988
2204
|
SET heartbeat_at = ?
|
|
1989
2205
|
WHERE id = 1 AND owner_id = ?
|
|
1990
|
-
`,[Date.now(),t])}finally{e===void 0&&await
|
|
2206
|
+
`,[Date.now(),t])}finally{e===void 0&&await n.close()}}async function V0(t,e){await t.run(`
|
|
1991
2207
|
UPDATE build_worker_lease
|
|
1992
2208
|
SET owner_id = NULL, owner_pid = NULL, heartbeat_at = NULL
|
|
1993
2209
|
WHERE id = 1 AND owner_id = ?
|
|
1994
|
-
`,[e])}async function
|
|
2210
|
+
`,[e])}async function Y0(t){let e=await t.queryOne(`
|
|
1995
2211
|
SELECT owner_id, owner_pid
|
|
1996
2212
|
FROM build_worker_lease
|
|
1997
2213
|
WHERE id = 1
|
|
1998
|
-
`,void 0,
|
|
2214
|
+
`,void 0,n=>({ownerId:Fn(n,"owner_id"),ownerPid:n.owner_pid===null?void 0:de(n,"owner_pid")}));e?.ownerPid===void 0||lo(e.ownerPid)||await t.run(`
|
|
1999
2215
|
UPDATE build_worker_lease
|
|
2000
2216
|
SET owner_id = NULL, owner_pid = NULL, heartbeat_at = NULL
|
|
2001
2217
|
WHERE id = 1
|
|
2002
|
-
`)}var
|
|
2218
|
+
`)}var K0,BR=c(()=>{"use strict";ii();xh();mo();ai();cc();dc();vh();jn();K0=5e3});import{readdir as uc,rm as Z0,rmdir as eB,stat as Th}from"fs/promises";import{join as lc}from"path";function _h(t){return tB.has(t)}async function mc(t){let e=await uc(t).catch(r=>{if(!(E(r)&&r.code==="ENOENT"))throw r});if(e===void 0||e.some(r=>!_h(r)))return 0;let n=0;for(let r of e){let i=lc(t,r);n+=await we(i),await Z0(i,{force:!0,recursive:!0})}return await eB(t).catch(r=>{if(!(E(r)&&r.code==="ENOENT"))throw r}),n}async function pc(t){let e=await uc(t,{withFileTypes:!0}).catch(a=>{if(E(a)&&a.code==="ENOENT")return[];throw a}),n=0,r=0,i=0;for(let a of e){if(!a.isDirectory())continue;i+=1;let o=lc(t,a.name),s=await Th(o).catch(u=>{if(!(E(u)&&u.code==="ENOENT"))throw u});if(s===void 0||Date.now()-s.mtimeMs<nB)continue;let d=await mc(o);if(await UR(o)){n+=d;continue}n+=d,r+=1}return{freedBytes:n,removed:r,scanned:i}}async function Ch(t){let e=await uc(t,{withFileTypes:!0}).catch(a=>{if(E(a)&&a.code==="ENOENT")return[];throw a}),n=0,r=0,i=0;for(let a of e){if(!a.isDirectory())continue;let o=lc(t,a.name),s=await Ch(o);n+=s.freedBytes,r+=s.removed,i+=s.scanned+1;let d=await mc(o);if(await UR(o)){n+=d;continue}n+=d,r+=1}return{freedBytes:n,removed:r,scanned:i}}async function we(t){try{let e=await Th(t);if(e.isDirectory()){let n=await uc(t),r=0;for(let i of n)r+=await we(lc(t,i));return r}return e.size}catch(e){if(E(e)&&e.code==="ENOENT")return 0;throw e}}async function UR(t){try{return await Th(t),!0}catch(e){if(E(e)&&e.code==="ENOENT")return!1;throw e}}var tB,nB,ko=c(()=>{"use strict";T();tB=new Set([".DS_Store"]),nB=6e4});import{rm as Xt}from"fs/promises";async function Ro(t={}){let e=await F(),n=Date.now()-(t.olderThanMs??10080*60*1e3),r=t.states??["succeeded","failed","canceled"];try{let i=r.map(()=>"?").join(", "),a=await e.queryAll(`
|
|
2003
2219
|
SELECT *
|
|
2004
2220
|
FROM build_jobs
|
|
2005
2221
|
WHERE state IN (${i})
|
|
2006
2222
|
AND updated_at <= ?
|
|
2007
|
-
`,[...n
|
|
2223
|
+
`,[...r,n],xe);for(let o of a)await Xt(o.workspacePath,{force:!0,recursive:!0}),await Xt(o.cachePath,{force:!0,recursive:!0}),await Xt(o.logPath,{force:!0,recursive:!0}),await Xt(o.eventsPath,{force:!0}),await e.run("DELETE FROM build_jobs WHERE job_id = ?",[o.jobId]);return a.length}finally{await e.close()}}async function hc(t){let e=await F(),n=t.force?t.now:t.now-10080*60*1e3;try{let r=await e.queryOne("SELECT COUNT(*) AS count FROM build_jobs",void 0,s=>de(s,"count")),i=await e.queryAll(`
|
|
2008
2224
|
SELECT *
|
|
2009
2225
|
FROM build_jobs
|
|
2010
2226
|
WHERE state IN ('succeeded', 'failed', 'canceled')
|
|
2011
2227
|
AND updated_at <= ?
|
|
2012
|
-
`,[
|
|
2013
|
-
`)}finally{await SG(r)}}async function*Bn(t){let e=Hj({crlfDelay:1/0,input:Bj(t,{encoding:"utf8"})}),r=0;for await(let n of e)if(r+=1,n.trim()!=="")try{yield Gp(JSON.parse(n))}catch(i){throw new Error(`Invalid WikgObject JSONL record at ${t}:${r}`,{cause:i})}}async function*Bp(t){yield{chapterId:t.chapterId,schemaVersion:jc,stream:"knowledge-graph",type:"meta"},t.parameter!==void 0&&(yield{...t.parameter.language===void 0?{}:{language:t.parameter.language},prompt:t.parameter.prompt,scope:"knowledge-graph",type:"parameter"});for await(let e of t.mentions){if(e.chapterId!==t.chapterId)throw new Error(`Mention ${e.id} belongs to chapter ${e.chapterId}, expected chapter ${t.chapterId}.`);yield sG(e)}for await(let e of t.mentionLinks)yield dG(t.chapterId,e);yield{type:"end"}}async function Up(t,e){let r=[],n=[],i,a=!1,o=!1;for await(let s of e)switch(s.type){case"meta":if(s.chapterId!==t)throw new Error(`WikgObject stream belongs to chapter ${s.chapterId}, expected chapter ${t}.`);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(cG(t,s));break;case"mention-link":n.push(uG(t,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*Za(t){yield{chapterId:t.chapterId,schemaVersion:jc,stream:"reading-graph",type:"meta"},t.parameter!==void 0&&(yield{...t.parameter.language===void 0?{}:{language:t.parameter.language},prompt:t.parameter.prompt,scope:"reading-graph",type:"parameter"});for(let o of await Wc(t.document,t.chapterId))yield lG(o);let e=new Map,r=0;for(let o of await t.document.chunks.listBySerial(t.chapterId)){let s=`chunk-${r}`;r+=1,e.set(o.id,s),yield pG(t.chapterId,s,o)}for(let o of await t.document.readingEdges.listBySerial(t.chapterId)){let s=e.get(o.fromId),c=e.get(o.toId);s===void 0||c===void 0||(yield hG(s,c,o))}for(let o of await t.document.fragmentGroups.listBySerial(t.chapterId))yield fG(o);let n=new Map,i=0,a=await t.document.snakes.listBySerial(t.chapterId);for(let o of a){let s=`snake-${i}`;i+=1,n.set(o.id,s),yield yG(s,o);for(let c of await t.document.snakeChunks.listBySnake(o.id)){let u=e.get(c.chunkId);u!==void 0&&(yield{chunkId:u,position:c.position,snakeId:s,type:"snake-chunk"})}}for(let o of await t.document.snakeEdges.listBySerial(t.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 Hp(t,e){let r=[],n=[],i=[],a=[],o=[],s=[],c=[],u,l=!1,m=!1;for await(let p of e)switch(p.type){case"meta":if(p.chapterId!==t)throw new Error(`WikgObject stream belongs to chapter ${p.chapterId}, expected chapter ${t}.`);if(p.stream!=="reading-graph")throw new Error(`Expected reading-graph stream, received ${p.stream}.`);l=!0;break;case"parameter":p.scope==="reading-graph"&&(u=p);break;case"source-fragment":i.push(mG(t,p));break;case"reading-chunk":r.push(p);break;case"reading-edge":a.push(p);break;case"fragment-group":n.push(p);break;case"snake":c.push(p);break;case"snake-chunk":o.push(p);break;case"snake-edge":s.push(p);break;case"end":m=!0;break;default:throw new Error(`Unexpected ${p.type} record in reading graph stream.`)}if(!l)throw new Error("WikgObject stream is missing a meta record.");if(!m)throw new Error("WikgObject stream is missing an end record.");return gG({chunks:r,readingEdges:a,snakeChunks:o,snakeEdges:s,snakes:c}),{chunks:r,fragmentGroups:n,fragments:i,...u===void 0?{}:{parameter:u},readingEdges:a,snakeChunks:o,snakeEdges:s,snakes:c}}function sG(t){return{...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,type:"mention"}}function cG(t,e){return{chapterId:t,...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}}function dG(t,e){return{...e.confidence===void 0?{}:{confidence:e.confidence},evidenceSentenceIndexes:e.evidenceSentenceIds.map(r=>{if(r[0]!==t)throw new Error(`Mention link ${e.id} evidence sentence ${$x(r)} is outside chapter ${t}.`);return r[1]}),id:e.id,...e.note===void 0?{}:{note:e.note},predicate:e.predicate,sourceMentionId:e.sourceMentionId,targetMentionId:e.targetMentionId,type:"mention-link"}}function uG(t,e){return{...e.confidence===void 0?{}:{confidence:e.confidence},evidenceSentenceIds:e.evidenceSentenceIndexes.map(r=>[t,r]),id:e.id,...e.note===void 0?{}:{note:e.note},predicate:e.predicate,sourceMentionId:e.sourceMentionId,targetMentionId:e.targetMentionId}}function lG(t){return{fragmentId:t.fragmentId,sentences:t.sentences.map(e=>({text:e.text,wordsCount:e.wordsCount})),summary:t.summary,type:"source-fragment"}}function mG(t,e){return{fragmentId:e.fragmentId,sentences:e.sentences,serialId:t,summary:e.summary}}function pG(t,e,r){return{content:r.content,generation:r.generation,id:e,...r.importance===void 0?{}:{importance:r.importance},label:r.label,...r.retention===void 0?{}:{retention:r.retention},sentenceIndex:Hx(t,r.sentenceId),sentenceIndexes:r.sentenceIds.map(n=>Hx(t,n)),type:"reading-chunk",weight:r.weight,wordsCount:r.wordsCount}}function hG(t,e,r){return{fromChunkId:t,...r.strength===void 0?{}:{strength:r.strength},toChunkId:e,type:"reading-edge",weight:r.weight}}function fG(t){return{endSentenceIndex:t.endSentenceIndex,groupId:t.groupId,startSentenceIndex:t.startSentenceIndex,type:"fragment-group"}}function yG(t,e){return{firstLabel:e.firstLabel,groupId:e.groupId,id:t,lastLabel:e.lastLabel,localSnakeId:e.localSnakeId,size:e.size,type:"snake",weight:e.weight,wordsCount:e.wordsCount}}function gG(t){let e=new Set;for(let n of t.chunks){if(e.has(n.id))throw new Error(`Duplicate reading chunk id ${n.id}.`);e.add(n.id)}let r=new Set;for(let n of t.snakes){if(r.has(n.id))throw new Error(`Duplicate snake id ${n.id}.`);r.add(n.id)}for(let n of t.readingEdges)Gn(e,n.fromChunkId,"chunk"),Gn(e,n.toChunkId,"chunk");for(let n of t.snakeChunks)Gn(r,n.snakeId,"snake"),Gn(e,n.chunkId,"chunk");for(let n of t.snakeEdges)Gn(r,n.fromSnakeId,"snake"),Gn(r,n.toSnakeId,"snake")}function Gn(t,e,r){if(!t.has(e))throw new Error(`Unknown ${r} id ${e}.`)}function Hx(t,e){if(e[0]!==t)throw new Error(`Sentence ${$x(e)} is outside chapter ${t}.`);return e[1]}function $x(t){return t.join(":")}async function bG(t,e){await new Promise((r,n)=>{t.write(e,i=>{if(i!=null){n(i);return}r()})})}async function SG(t){await new Promise((e,r)=>{t.end(n=>{if(n!=null){r(n);return}e()})})}var jc,$,qj,$j,qx,Kj,Jj,zj,Qj,Xj,Vj,Yj,Zj,eG,tG,rG,nG,iG,aG,oG,Un=d(()=>{"use strict";tt();Fc();jc=1,$=b.number().int().nonnegative(),qj=b.literal(jc),$j=b.string().regex(/^Q[1-9][0-9]*$/u),qx=b.number().min(0).max(1).optional(),Kj=b.object({text:b.string(),wordsCount:$}).strict(),Jj=b.object({type:b.literal("meta"),schemaVersion:qj,chapterId:b.number().int(),stream:b.enum(["knowledge-graph","reading-graph","summary"])}).strict(),zj=b.object({type:b.literal("parameter"),scope:b.enum(["knowledge-graph","reading-graph","summary"]),language:b.string().optional(),prompt:b.string()}).strict(),Qj=b.object({type:b.literal("source-fragment"),fragmentId:$,summary:b.string(),sentences:b.array(Kj)}).strict(),Xj=b.object({type:b.literal("reading-chunk"),id:b.string().min(1),generation:b.number().int(),sentenceIndex:$,label:b.string(),content:b.string(),sentenceIndexes:b.array($).min(1),retention:b.enum(Le).optional(),importance:b.enum(Ge).optional(),wordsCount:$,weight:b.number()}).strict(),Vj=b.object({type:b.literal("reading-edge"),fromChunkId:b.string().min(1),toChunkId:b.string().min(1),strength:b.string().optional(),weight:b.number()}).strict(),Yj=b.object({type:b.literal("fragment-group"),groupId:$,startSentenceIndex:$,endSentenceIndex:$}).strict(),Zj=b.object({type:b.literal("snake"),id:b.string().min(1),groupId:$,localSnakeId:$,size:$,firstLabel:b.string(),lastLabel:b.string(),wordsCount:$,weight:b.number()}).strict(),eG=b.object({type:b.literal("snake-chunk"),snakeId:b.string().min(1),chunkId:b.string().min(1),position:$}).strict(),tG=b.object({type:b.literal("snake-edge"),fromSnakeId:b.string().min(1),toSnakeId:b.string().min(1),weight:b.number()}).strict(),rG=b.object({type:b.literal("summary"),text:b.string()}).strict(),nG=b.object({type:b.literal("mention"),id:b.string().min(1),fragmentId:$.optional(),sentenceIndex:$.optional(),rangeStart:$,rangeEnd:$,surface:b.string().min(1),qid:$j,confidence:qx,note:b.string().optional()}).strict(),iG=b.object({type:b.literal("mention-link"),id:b.string().min(1),sourceMentionId:b.string().min(1),targetMentionId:b.string().min(1),predicate:b.string().min(1),evidenceSentenceIndexes:b.array($).min(1),confidence:qx,note:b.string().optional()}).strict(),aG=b.object({type:b.literal("end")}).strict(),oG=b.discriminatedUnion("type",[Jj,zj,Qj,Xj,Vj,Yj,Zj,eG,tG,rG,nG,iG,aG])});function Kx(t){return{extractionPrompt:Re(t.extractionPrompt),...t.userLanguage===void 0?{}:{userLanguage:t.userLanguage}}}function Jx(t){let e=Lr(t.userLanguage);return{...e===void 0?{}:{language:e},prompt:Re(t.extractionPrompt)}}var zx=d(()=>{"use strict";Ve();Bt()});async function Qx(t,e,r){let n=new Vt(t),i=n.getSerial(e),a=await i.createDraft(),o=0,s=!1;await n.ensureCreated();for await(let c of gr(r)){let u=c.text.trim();u!==""&&(o>0&&o+c.wordsCount>wG&&(await a.commit(),a=await i.createDraft(),o=0),a.addSentence(u,c.wordsCount),o+=c.wordsCount,s=!0)}s?await a.commit():a.discard()}var wG,Xx=d(()=>{"use strict";L();ac();wG=320});import{mkdir as EG,rm as kG}from"fs/promises";import{join as Vx}from"path";async function Gc(t,e){let r=Vx(e.workspacePath,"graph-document"),n=Vx(e.workspacePath,"reading-graph.jsonl"),i=Jx(e);await kG(r,{force:!0,recursive:!0}),await EG(e.workspacePath,{recursive:!0});let a=await ye.open(r);try{await a.openSession(async o=>{await o.serials.createWithId(t),await Qx(r,t,e.sourceText);let s=vi(o,r);await new br({document:s,llm:e.llm,...e.logDirPath===void 0?{}:{logDirPath:e.logDirPath}}).buildTopologyInto(t,Kx(e),e.progressTracker)}),await xr(n,Za({chapterId:t,document:vi(a,r),parameter:i}))}finally{await a.release()}return{chapterId:t,documentPath:r,objectsPath:n,parameter:i}}async function Bc(t,e){let r=await Hp(e.chapterId,Bn(e.objectsPath));return await t.openSession(async n=>{await bn(n,e.chapterId,"sourced"),await n.clearSerialGraph(e.chapterId),await n.serials.ensure(e.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:[e.chapterId,s.sentenceIndex],sentenceIds:s.sentenceIndexes.map(u=>[e.chapterId,u]),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:Hn(i,s.fromChunkId,"chunk"),...s.strength===void 0?{}:{strength:s.strength},toId:Hn(i,s.toChunkId,"chunk"),weight:s.weight});await n.fragmentGroups.saveMany(r.fragmentGroups.map(s=>({endSentenceIndex:s.endSentenceIndex,groupId:s.groupId,serialId:e.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:e.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:Hn(i,s.chunkId,"chunk"),position:s.position,snakeId:Hn(a,s.snakeId,"snake")});for(let s of r.snakeEdges)await n.snakeEdges.save({fromSnakeId:Hn(a,s.fromSnakeId,"snake"),toSnakeId:Hn(a,s.toSnakeId,"snake"),weight:s.weight});let o=await n.graphBuildParameters.save(r.parameter??e.parameter);await n.serials.setTopologyReady(e.chapterId,!0,o.hash)}),await Y(t,e.chapterId)}function Hn(t,e,r){let n=t.get(e);if(n===void 0)throw new Error(`Unknown ${r} id ${e}.`);return n}var Yx=d(()=>{"use strict";L();be();Mn();Un();zx();Xx()});function Rr(t,e){return t-e}function Zx(t){let e=new Map;for(let r of t){let n=e.get(r.serialId)??[];n.push(r.fragmentId),e.set(r.serialId,n)}return new Map([...e.entries()].map(([r,n])=>[r,n.sort(Rr)]))}function eR(t,e){let r=new Set(e);return t.flatMap((n,i)=>{if(!r.has(n))return[];let a=t[i+1];return[{endSentenceIndex:a===void 0?1/0:a-1,startSentenceIndex:n}]})}function tR(t,e){return t.id-e.id}function rR(t,e){return t.serialId-e.serialId||t.groupId-e.groupId||t.startSentenceIndex-e.startSentenceIndex||t.endSentenceIndex-e.endSentenceIndex}function nR(t,e){return t.fromId-e.fromId||t.toId-e.toId}function iR(t,e){return t[0]-e[0]||t[1]-e[1]}function aR(t,e){return t.groupId-e.groupId||t.id-e.id}function oR(t,e){return t.snakeId-e.snakeId||t.position-e.position}function sR(t,e){return t.fromSnakeId-e.fromSnakeId||t.toSnakeId-e.toSnakeId}var Uc=d(()=>{"use strict"});var Hc,cR=d(()=>{"use strict";Uc();Hc=class{#e;#t;#r;constructor(e,r){this.#e=[...e].sort(tR),this.#t=new Map(e.map(n=>[n.id,n])),this.#r=Zx(r)}getById(e){return Promise.resolve(this.#t.get(e))}countAll(){return Promise.resolve(this.#e.length)}listAll(){return Promise.resolve([...this.#e])}listBySentenceStartIndexes(e,r){let n=eR(this.#r.get(e)??[],r);return Promise.resolve(this.#e.filter(i=>i.sentenceId[0]===e&&n.some(a=>i.sentenceId[1]>=a.startSentenceIndex&&i.sentenceId[1]<=a.endSentenceIndex)))}listBySentenceRange(e,r,n){return Promise.resolve(this.#e.filter(i=>i.sentenceId[0]===e&&i.sentenceId[1]>=r&&i.sentenceId[1]<=n))}listBySerial(e){return Promise.resolve(this.#e.filter(r=>r.sentenceId[0]===e))}getMaxId(){return Promise.resolve(this.#e.reduce((e,r)=>Math.max(e,r.id),0))}listFragmentPairs(){let e=new Set;for(let r of this.#e)e.add(`${r.sentenceId[0]}:${r.sentenceId[1]}`);return Promise.resolve([...e].map(r=>r.split(":").map(Number)).sort(iR).map(([r,n])=>[r,n]))}}});var qc,$c,Kc,Jc,dR=d(()=>{"use strict";qc=class{countByQid(e,r){return Promise.resolve(0)}getById(e){return Promise.resolve(void 0)}listAll(){return Promise.resolve([])}listByQid(e,r){return Promise.resolve([])}listLabelsByQid(e,r){return Promise.resolve([])}listBySurfaceTerms(e){return Promise.resolve([])}listBySurfaces(e){return Promise.resolve([])}listByChapter(e){return Promise.resolve([])}},$c=class{countByTriple(e){return Promise.resolve(0)}getById(e){return Promise.resolve(void 0)}listAll(){return Promise.resolve([])}listByTriple(e){return Promise.resolve([])}listByChapter(e){return Promise.resolve([])}},Kc=class{getMap(e){return Promise.resolve({})}},Jc=class{getByHash(e){return Promise.resolve(void 0)}}});var zc,Qc,Xc,Vc,Yc,uR=d(()=>{"use strict";Uc();zc=class{#e;#t;constructor(e,r){this.#e=[...e].sort(nR),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(e){return Promise.resolve(this.#e.filter(r=>this.#t.get(r.fromId)===e&&this.#t.get(r.toId)===e))}listIncoming(e){return Promise.resolve(this.#e.filter(r=>r.toId===e))}listOutgoing(e){return Promise.resolve(this.#e.filter(r=>r.fromId===e))}},Qc=class{#e;#t;constructor(e){this.#e=[...e].sort(aR),this.#t=new Map(e.map(r=>[r.id,r]))}getById(e){return Promise.resolve(this.#t.get(e))}listIdsByGroup(e,r){return Promise.resolve(this.#e.filter(n=>n.serialId===e&&n.groupId===r).map(n=>n.id).sort(Rr))}listBySerial(e){return Promise.resolve(this.#e.filter(r=>r.serialId===e))}},Xc=class{#e;constructor(e){this.#e=[...e].sort(oR)}listChunkIds(e){return Promise.resolve(this.#e.filter(r=>r.snakeId===e).map(r=>r.chunkId))}listBySnake(e){return Promise.resolve(this.#e.filter(r=>r.snakeId===e))}},Vc=class{#e;#t;constructor(e,r){this.#e=[...e].sort(sR),this.#t=new Map(r.map(n=>[n.id,n.serialId]))}listIncoming(e){return Promise.resolve(this.#e.filter(r=>r.toSnakeId===e))}listOutgoing(e){return Promise.resolve(this.#e.filter(r=>r.fromSnakeId===e))}listWithin(e){let r=new Set(e);return Promise.resolve(this.#e.filter(n=>r.has(n.fromSnakeId)&&r.has(n.toSnakeId)))}listBySerial(e){return Promise.resolve(this.#e.filter(r=>this.#t.get(r.fromSnakeId)===e&&this.#t.get(r.toSnakeId)===e))}},Yc=class{#e;constructor(e){this.#e=[...e].sort(rR)}listBySerial(e){return Promise.resolve(this.#e.filter(r=>r.serialId===e))}listSerialIds(){return Promise.resolve([...new Set(this.#e.map(e=>e.serialId))].sort(Rr))}listGroupIdsForSerial(e){return Promise.resolve([...new Set(this.#e.filter(r=>r.serialId===e).map(r=>r.groupId))].sort(Rr))}}});var Zc,qn,lR=d(()=>{"use strict";Uc();Zc=class{#e;constructor(e){this.#e=e}getById(e){return Promise.resolve(e===this.#e.id?this.#e:void 0)}getRevision(e){return Promise.resolve(e===this.#e.id?this.#e.revision:0)}getRevisions(e){return Promise.resolve(new Map(e.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]]))}},qn=class{path="";serialId;#e;constructor(e,r){this.serialId=e,this.#e=new Map(r.filter(n=>n.serialId===e).map(n=>[n.fragmentId,n]))}getFragment(e){let r=this.#e.get(e);if(r===void 0)throw new Error(`Fragment ${e} does not exist`);return Promise.resolve(r)}listFragmentIds(){return Promise.resolve([...this.#e.keys()].sort(Rr))}async getSentence(e){let r=(await this.listSentences())[e];if(r===void 0)throw new RangeError(`Sentence ${e} does not exist`);return r}async listSentencesInRange(e,r){return(await this.listSentences()).slice(e,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 e=await this.listSentences();if(e.length!==0)return e.map(r=>r.text).join("")}}});var ed,mR=d(()=>{"use strict";cR();dR();uR();lR();ed=class{chunks;fragmentGroups;graphBuildParameters;readingEdges;mentionLinks;mentions;metadata;serials;snakeChunks;snakeEdges;snakes;#e;constructor(e){let r=e.serial.id;this.chunks=new Hc(e.chunks,e.fragments),this.fragmentGroups=new Yc(e.fragmentGroups),this.graphBuildParameters=new Jc,this.readingEdges=new zc(e.readingEdges,e.chunks),this.mentionLinks=new $c,this.mentions=new qc,this.metadata=new Kc,this.serials=new Zc(e.serial),this.snakeChunks=new Xc(e.snakeChunks),this.snakeEdges=new Vc(e.snakeEdges,e.snakes),this.snakes=new Qc(e.snakes),this.#e=new qn(r,e.fragments)}async getSentence(e){let[r,n]=e,i=(await this.getSerialFragments(r).listSentences())[n];if(i===void 0)throw new RangeError(`Sentence ${n} does not exist`);return i.text}getSerialFragments(e){return e!==this.#e.serialId?new qn(e,[]):this.#e}getSummaryFragments(e){return new qn(e,[])}async openSession(e){return await e(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(e){return Promise.resolve(void 0)}readToc(){return Promise.resolve(void 0)}release(){return Promise.resolve()}}});import{z as S}from"zod";function fR(t){return{id:t.id,generation:t.generation,sentenceId:t.sentenceId,label:t.label,content:t.content,sentenceIds:t.sentenceIds,...t.retention===void 0?{}:{retention:t.retention},...t.importance===void 0?{}:{importance:t.importance},wordsCount:t.wordsCount,weight:t.weight}}function yR(t){return{fromId:t.fromId,toId:t.toId,...t.strength===void 0?{}:{strength:t.strength},weight:t.weight}}var pR,IG,xG,RG,vG,TG,_G,CG,PG,LG,hR,gR=d(()=>{"use strict";tt();pR=S.tuple([S.number(),S.number()]),IG=S.object({text:S.string(),wordsCount:S.number()}),xG=S.object({serialId:S.number(),fragmentId:S.number(),summary:S.string(),sentences:S.array(IG)}),RG=S.object({documentOrder:S.number().optional(),id:S.number(),knowledgeGraphReady:S.boolean(),knowledgeGraphParameterHash:S.string().optional(),revision:S.number().optional(),topologyParameterHash:S.string().optional(),topologyReady:S.boolean()}).transform(t=>({documentOrder:t.documentOrder??t.id,id:t.id,knowledgeGraphReady:t.knowledgeGraphReady,...t.knowledgeGraphParameterHash===void 0?{}:{knowledgeGraphParameterHash:t.knowledgeGraphParameterHash},revision:t.revision??0,topologyReady:t.topologyReady,...t.topologyParameterHash===void 0?{}:{topologyParameterHash:t.topologyParameterHash}})),vG=S.object({id:S.number(),generation:S.number(),sentenceId:pR,label:S.string(),content:S.string(),sentenceIds:S.array(pR),retention:S.enum(Le).optional(),importance:S.enum(Ge).optional(),wordsCount:S.number(),weight:S.number()}),TG=S.object({fromId:S.number(),toId:S.number(),strength:S.string().optional(),weight:S.number()}),_G=S.object({id:S.number(),serialId:S.number(),groupId:S.number(),localSnakeId:S.number(),size:S.number(),firstLabel:S.string(),lastLabel:S.string(),wordsCount:S.number(),weight:S.number()}),CG=S.object({snakeId:S.number(),chunkId:S.number(),position:S.number()}),PG=S.object({fromSnakeId:S.number(),toSnakeId:S.number(),weight:S.number()}),LG=S.object({serialId:S.number(),groupId:S.number(),startSentenceIndex:S.number(),endSentenceIndex:S.number()}),hR=S.object({chunks:S.array(vG),fragmentGroups:S.array(LG),fragments:S.array(xG),readingEdges:S.array(TG),serial:RG,snakeChunks:S.array(CG),snakeEdges:S.array(PG),snakes:S.array(_G)})});import{readFile as AG,writeFile as OG}from"fs/promises";async function bR(t){let e=hR.parse(JSON.parse(await AG(t,"utf8")));return{...e,chunks:e.chunks.map(fR),readingEdges:e.readingEdges.map(yR)}}async function SR(t,e){await OG(t,`${JSON.stringify(e)}
|
|
2014
|
-
`,"utf8")}var
|
|
2228
|
+
`,[n],xe),a=t.dryRun?{freedBytes:0,removed:0,scanned:0}:await pc(Sh()),o=0;for(let s of i)o+=await we(s.workspacePath),o+=await we(s.cachePath),o+=await we(s.logPath),o+=await we(s.eventsPath),t.dryRun||(await Xt(s.workspacePath,{force:!0,recursive:!0}),await Xt(s.cachePath,{force:!0,recursive:!0}),await Xt(s.logPath,{force:!0,recursive:!0}),await Xt(s.eventsPath,{force:!0}),await e.run("DELETE FROM build_jobs WHERE job_id = ?",[s.jobId]));return{freedBytes:o+a.freedBytes,removed:i.length+a.removed,scanned:(r??0)+a.scanned}}finally{await e.close()}}var HR=c(()=>{"use strict";ko();ic();ai();jn()});var KR=c(()=>{"use strict";bh();vh();jR();BR();mo();HR()});async function fc(t,e){let n=t.getSerialFragments(e);return await Promise.all((await n.listFragmentIds()).map(async r=>await n.getFragment(r)))}async function*JR(t,e){let n=t.getSerialFragments(e);for(let r of await n.listFragmentIds()){let i=await n.getFragment(r);for(let a of i.sentences)yield a.text}}async function zR(t){let e=[];for await(let n of t)e.push(n);return e}var gc=c(()=>{"use strict"});import{createReadStream as rB,createWriteStream as iB}from"fs";import{createInterface as aB}from"readline";import{z as b}from"zod";function Ph(t){return IB.parse(t)}async function Bn(t,e){let n=iB(t,{encoding:"utf8",flags:"wx"});try{for await(let r of e)await NB(n,`${JSON.stringify(Ph(r))}
|
|
2229
|
+
`)}finally{await DB(n)}}async function*si(t){let e=aB({crlfDelay:1/0,input:rB(t,{encoding:"utf8"})}),n=0;for await(let r of e)if(n+=1,r.trim()!=="")try{yield Ph(JSON.parse(r))}catch(i){throw new Error(`Invalid WikgObject JSONL record at ${t}:${n}`,{cause:i})}}async function*Ah(t){yield{chapterId:t.chapterId,schemaVersion:yc,stream:"knowledge-graph",type:"meta"},t.parameter!==void 0&&(yield{...t.parameter.language===void 0?{}:{language:t.parameter.language},prompt:t.parameter.prompt,scope:"knowledge-graph",type:"parameter"});for await(let e of t.mentions){if(e.chapterId!==t.chapterId)throw new Error(`Mention ${e.id} belongs to chapter ${e.chapterId}, expected chapter ${t.chapterId}.`);yield xB(e)}for await(let e of t.mentionLinks)yield RB(t.chapterId,e);yield{type:"end"}}async function Lh(t,e){let n=[],r=[],i,a=!1,o=!1;for await(let s of e)switch(s.type){case"meta":if(s.chapterId!==t)throw new Error(`WikgObject stream belongs to chapter ${s.chapterId}, expected chapter ${t}.`);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":n.push(kB(t,s));break;case"mention-link":r.push(vB(t,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:r,mentions:n,...i===void 0?{}:{parameter:i}}}async function*vo(t){yield{chapterId:t.chapterId,schemaVersion:yc,stream:"reading-graph",type:"meta"},t.parameter!==void 0&&(yield{...t.parameter.language===void 0?{}:{language:t.parameter.language},prompt:t.parameter.prompt,scope:"reading-graph",type:"parameter"});for(let o of await fc(t.document,t.chapterId))yield TB(o);let e=new Map,n=0;for(let o of await t.document.chunks.listBySerial(t.chapterId)){let s=`chunk-${n}`;n+=1,e.set(o.id,s),yield CB(t.chapterId,s,o)}for(let o of await t.document.readingEdges.listBySerial(t.chapterId)){let s=e.get(o.fromId),d=e.get(o.toId);s===void 0||d===void 0||(yield PB(s,d,o))}for(let o of await t.document.fragmentGroups.listBySerial(t.chapterId))yield AB(o);let r=new Map,i=0,a=await t.document.snakes.listBySerial(t.chapterId);for(let o of a){let s=`snake-${i}`;i+=1,r.set(o.id,s),yield LB(s,o);for(let d of await t.document.snakeChunks.listBySnake(o.id)){let u=e.get(d.chunkId);u!==void 0&&(yield{chunkId:u,position:d.position,snakeId:s,type:"snake-chunk"})}}for(let o of await t.document.snakeEdges.listBySerial(t.chapterId)){let s=r.get(o.fromSnakeId),d=r.get(o.toSnakeId);s===void 0||d===void 0||(yield{fromSnakeId:s,toSnakeId:d,type:"snake-edge",weight:o.weight})}yield{type:"end"}}async function Oh(t,e){let n=[],r=[],i=[],a=[],o=[],s=[],d=[],u,l=!1,m=!1;for await(let p of e)switch(p.type){case"meta":if(p.chapterId!==t)throw new Error(`WikgObject stream belongs to chapter ${p.chapterId}, expected chapter ${t}.`);if(p.stream!=="reading-graph")throw new Error(`Expected reading-graph stream, received ${p.stream}.`);l=!0;break;case"parameter":p.scope==="reading-graph"&&(u=p);break;case"source-fragment":i.push(_B(t,p));break;case"reading-chunk":n.push(p);break;case"reading-edge":a.push(p);break;case"fragment-group":r.push(p);break;case"snake":d.push(p);break;case"snake-chunk":o.push(p);break;case"snake-edge":s.push(p);break;case"end":m=!0;break;default:throw new Error(`Unexpected ${p.type} record in reading graph stream.`)}if(!l)throw new Error("WikgObject stream is missing a meta record.");if(!m)throw new Error("WikgObject stream is missing an end record.");return OB({chunks:n,readingEdges:a,snakeChunks:o,snakeEdges:s,snakes:d}),{chunks:n,fragmentGroups:r,fragments:i,...u===void 0?{}:{parameter:u},readingEdges:a,snakeChunks:o,snakeEdges:s,snakes:d}}function xB(t){return{...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,type:"mention"}}function kB(t,e){return{chapterId:t,...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}}function RB(t,e){return{...e.confidence===void 0?{}:{confidence:e.confidence},evidenceSentenceIndexes:e.evidenceSentenceIds.map(n=>{if(n[0]!==t)throw new Error(`Mention link ${e.id} evidence sentence ${VR(n)} is outside chapter ${t}.`);return n[1]}),id:e.id,...e.note===void 0?{}:{note:e.note},predicate:e.predicate,sourceMentionId:e.sourceMentionId,targetMentionId:e.targetMentionId,type:"mention-link"}}function vB(t,e){return{...e.confidence===void 0?{}:{confidence:e.confidence},evidenceSentenceIds:e.evidenceSentenceIndexes.map(n=>[t,n]),id:e.id,...e.note===void 0?{}:{note:e.note},predicate:e.predicate,sourceMentionId:e.sourceMentionId,targetMentionId:e.targetMentionId}}function TB(t){return{fragmentId:t.fragmentId,sentences:t.sentences.map(e=>({text:e.text,wordsCount:e.wordsCount})),summary:t.summary,type:"source-fragment"}}function _B(t,e){return{fragmentId:e.fragmentId,sentences:e.sentences,serialId:t,summary:e.summary}}function CB(t,e,n){return{content:n.content,generation:n.generation,id:e,...n.importance===void 0?{}:{importance:n.importance},label:n.label,...n.retention===void 0?{}:{retention:n.retention},sentenceIndex:QR(t,n.sentenceId),sentenceIndexes:n.sentenceIds.map(r=>QR(t,r)),type:"reading-chunk",weight:n.weight,wordsCount:n.wordsCount}}function PB(t,e,n){return{fromChunkId:t,...n.strength===void 0?{}:{strength:n.strength},toChunkId:e,type:"reading-edge",weight:n.weight}}function AB(t){return{endSentenceIndex:t.endSentenceIndex,groupId:t.groupId,startSentenceIndex:t.startSentenceIndex,type:"fragment-group"}}function LB(t,e){return{firstLabel:e.firstLabel,groupId:e.groupId,id:t,lastLabel:e.lastLabel,localSnakeId:e.localSnakeId,size:e.size,type:"snake",weight:e.weight,wordsCount:e.wordsCount}}function OB(t){let e=new Set;for(let r of t.chunks){if(e.has(r.id))throw new Error(`Duplicate reading chunk id ${r.id}.`);e.add(r.id)}let n=new Set;for(let r of t.snakes){if(n.has(r.id))throw new Error(`Duplicate snake id ${r.id}.`);n.add(r.id)}for(let r of t.readingEdges)oi(e,r.fromChunkId,"chunk"),oi(e,r.toChunkId,"chunk");for(let r of t.snakeChunks)oi(n,r.snakeId,"snake"),oi(e,r.chunkId,"chunk");for(let r of t.snakeEdges)oi(n,r.fromSnakeId,"snake"),oi(n,r.toSnakeId,"snake")}function oi(t,e,n){if(!t.has(e))throw new Error(`Unknown ${n} id ${e}.`)}function QR(t,e){if(e[0]!==t)throw new Error(`Sentence ${VR(e)} is outside chapter ${t}.`);return e[1]}function VR(t){return t.join(":")}async function NB(t,e){await new Promise((n,r)=>{t.write(e,i=>{if(i!=null){r(i);return}n()})})}async function DB(t){await new Promise((e,n)=>{t.end(r=>{if(r!=null){n(r);return}e()})})}var yc,X,oB,sB,XR,dB,cB,uB,lB,mB,pB,hB,fB,gB,yB,bB,SB,wB,EB,IB,di=c(()=>{"use strict";Ke();gc();yc=1,X=b.number().int().nonnegative(),oB=b.literal(yc),sB=b.string().regex(/^Q[1-9][0-9]*$/u),XR=b.number().min(0).max(1).optional(),dB=b.object({text:b.string(),wordsCount:X}).strict(),cB=b.object({type:b.literal("meta"),schemaVersion:oB,chapterId:b.number().int(),stream:b.enum(["knowledge-graph","reading-graph","summary"])}).strict(),uB=b.object({type:b.literal("parameter"),scope:b.enum(["knowledge-graph","reading-graph","summary"]),language:b.string().optional(),prompt:b.string()}).strict(),lB=b.object({type:b.literal("source-fragment"),fragmentId:X,summary:b.string(),sentences:b.array(dB)}).strict(),mB=b.object({type:b.literal("reading-chunk"),id:b.string().min(1),generation:b.number().int(),sentenceIndex:X,label:b.string(),content:b.string(),sentenceIndexes:b.array(X).min(1),retention:b.enum(Oe).optional(),importance:b.enum($e).optional(),wordsCount:X,weight:b.number()}).strict(),pB=b.object({type:b.literal("reading-edge"),fromChunkId:b.string().min(1),toChunkId:b.string().min(1),strength:b.string().optional(),weight:b.number()}).strict(),hB=b.object({type:b.literal("fragment-group"),groupId:X,startSentenceIndex:X,endSentenceIndex:X}).strict(),fB=b.object({type:b.literal("snake"),id:b.string().min(1),groupId:X,localSnakeId:X,size:X,firstLabel:b.string(),lastLabel:b.string(),wordsCount:X,weight:b.number()}).strict(),gB=b.object({type:b.literal("snake-chunk"),snakeId:b.string().min(1),chunkId:b.string().min(1),position:X}).strict(),yB=b.object({type:b.literal("snake-edge"),fromSnakeId:b.string().min(1),toSnakeId:b.string().min(1),weight:b.number()}).strict(),bB=b.object({type:b.literal("summary"),text:b.string()}).strict(),SB=b.object({type:b.literal("mention"),id:b.string().min(1),fragmentId:X.optional(),sentenceIndex:X.optional(),rangeStart:X,rangeEnd:X,surface:b.string().min(1),qid:sB,confidence:XR,note:b.string().optional()}).strict(),wB=b.object({type:b.literal("mention-link"),id:b.string().min(1),sourceMentionId:b.string().min(1),targetMentionId:b.string().min(1),predicate:b.string().min(1),evidenceSentenceIndexes:b.array(X).min(1),confidence:XR,note:b.string().optional()}).strict(),EB=b.object({type:b.literal("end")}).strict(),IB=b.discriminatedUnion("type",[cB,uB,lB,mB,pB,hB,fB,gB,yB,bB,SB,wB,EB])});function YR(t){return{extractionPrompt:_e(t.extractionPrompt),...t.userLanguage===void 0?{}:{userLanguage:t.userLanguage}}}function ZR(t){let e=Vn(t.userLanguage);return{...e===void 0?{}:{language:e},prompt:_e(t.extractionPrompt)}}var ev=c(()=>{"use strict";nt();zt()});async function tv(t,e,n){let r=new sn(t),i=r.getSerial(e),a=await i.createDraft(),o=0,s=!1;await r.ensureCreated();for await(let d of On(n)){let u=d.text.trim();u!==""&&(o>0&&o+d.wordsCount>MB&&(await a.commit(),a=await i.createDraft(),o=0),a.addSentence(u,d.wordsCount),o+=d.wordsCount,s=!0)}s?await a.commit():a.discard()}var MB,nv=c(()=>{"use strict";N();Wd();MB=320});import{mkdir as WB,rm as FB}from"fs/promises";import{join as rv}from"path";async function bc(t,e){let n=rv(e.workspacePath,"graph-document"),r=rv(e.workspacePath,"reading-graph.jsonl"),i=ZR(e);await FB(n,{force:!0,recursive:!0}),await WB(e.workspacePath,{recursive:!0});let a=await he.open(n);try{await a.openSession(async o=>{await o.serials.createWithId(t),await tv(n,t,e.sourceText);let s=zi(o,n);await new Nn({document:s,llm:e.llm,...e.logDirPath===void 0?{}:{logDirPath:e.logDirPath}}).buildTopologyInto(t,YR(e),e.progressTracker)}),await Bn(r,vo({chapterId:t,document:zi(a,n),parameter:i}))}finally{await a.release()}return{chapterId:t,documentPath:n,objectsPath:r,parameter:i}}async function Sc(t,e){let n=await Oh(e.chapterId,si(e.objectsPath));return await t.openSession(async r=>{await jr(r,e.chapterId,"sourced");let i=await r.indexArtifacts.get(e.chapterId,"fts")!==void 0;await r.clearSerialReadingGraph(e.chapterId),await r.serials.ensure(e.chapterId);let a=new Map;for(let d of n.chunks){let u=await r.chunks.create({content:d.content,generation:d.generation,label:d.label,sentenceId:[e.chapterId,d.sentenceIndex],sentenceIds:d.sentenceIndexes.map(l=>[e.chapterId,l]),weight:d.weight,wordsCount:d.wordsCount,...d.importance===void 0?{}:{importance:d.importance},...d.retention===void 0?{}:{retention:d.retention}});a.set(d.id,u.id)}for(let d of n.readingEdges)await r.readingEdges.save({fromId:ci(a,d.fromChunkId,"chunk"),...d.strength===void 0?{}:{strength:d.strength},toId:ci(a,d.toChunkId,"chunk"),weight:d.weight});await r.fragmentGroups.saveMany(n.fragmentGroups.map(d=>({endSentenceIndex:d.endSentenceIndex,groupId:d.groupId,serialId:e.chapterId,startSentenceIndex:d.startSentenceIndex})));let o=new Map;for(let d of n.snakes){let u=await r.snakes.create({firstLabel:d.firstLabel,groupId:d.groupId,lastLabel:d.lastLabel,localSnakeId:d.localSnakeId,serialId:e.chapterId,size:d.size,weight:d.weight,wordsCount:d.wordsCount});o.set(d.id,u)}for(let d of n.snakeChunks)await r.snakeChunks.save({chunkId:ci(a,d.chunkId,"chunk"),position:d.position,snakeId:ci(o,d.snakeId,"snake")});for(let d of n.snakeEdges)await r.snakeEdges.save({fromSnakeId:ci(o,d.fromSnakeId,"snake"),toSnakeId:ci(o,d.toSnakeId,"snake"),weight:d.weight});let s=await r.graphBuildParameters.save(n.parameter??e.parameter);await r.serials.setTopologyReady(e.chapterId,!0,s.hash),i&&await mt(r,e.chapterId)}),await re(t,e.chapterId)}function ci(t,e,n){let r=t.get(e);if(r===void 0)throw new Error(`Unknown ${n} id ${e}.`);return r}var iv=c(()=>{"use strict";N();Se();ri();di();Cr();ev();nv()});function Un(t,e){return t-e}function av(t){let e=new Map;for(let n of t){let r=e.get(n.serialId)??[];r.push(n.fragmentId),e.set(n.serialId,r)}return new Map([...e.entries()].map(([n,r])=>[n,r.sort(Un)]))}function ov(t,e){let n=new Set(e);return t.flatMap((r,i)=>{if(!n.has(r))return[];let a=t[i+1];return[{endSentenceIndex:a===void 0?1/0:a-1,startSentenceIndex:r}]})}function sv(t,e){return t.id-e.id}function dv(t,e){return t.serialId-e.serialId||t.groupId-e.groupId||t.startSentenceIndex-e.startSentenceIndex||t.endSentenceIndex-e.endSentenceIndex}function cv(t,e){return t.fromId-e.fromId||t.toId-e.toId}function uv(t,e){return t[0]-e[0]||t[1]-e[1]}function lv(t,e){return t.groupId-e.groupId||t.id-e.id}function mv(t,e){return t.snakeId-e.snakeId||t.position-e.position}function pv(t,e){return t.fromSnakeId-e.fromSnakeId||t.toSnakeId-e.toSnakeId}var wc=c(()=>{"use strict"});var Ec,hv=c(()=>{"use strict";wc();Ec=class{#e;#t;#n;constructor(e,n){this.#e=[...e].sort(sv),this.#t=new Map(e.map(r=>[r.id,r])),this.#n=av(n)}getById(e){return Promise.resolve(this.#t.get(e))}countAll(){return Promise.resolve(this.#e.length)}listAll(){return Promise.resolve([...this.#e])}listBySentenceStartIndexes(e,n){let r=ov(this.#n.get(e)??[],n);return Promise.resolve(this.#e.filter(i=>i.sentenceId[0]===e&&r.some(a=>i.sentenceId[1]>=a.startSentenceIndex&&i.sentenceId[1]<=a.endSentenceIndex)))}listBySentenceRange(e,n,r){return Promise.resolve(this.#e.filter(i=>i.sentenceId[0]===e&&i.sentenceId[1]>=n&&i.sentenceId[1]<=r))}listBySerial(e){return Promise.resolve(this.#e.filter(n=>n.sentenceId[0]===e))}getMaxId(){return Promise.resolve(this.#e.reduce((e,n)=>Math.max(e,n.id),0))}listFragmentPairs(){let e=new Set;for(let n of this.#e)e.add(`${n.sentenceId[0]}:${n.sentenceId[1]}`);return Promise.resolve([...e].map(n=>n.split(":").map(Number)).sort(uv).map(([n,r])=>[n,r]))}}});var Ic,xc,kc,Rc,vc,fv=c(()=>{"use strict";Ic=class{countByQid(e,n){return Promise.resolve(0)}getById(e){return Promise.resolve(void 0)}listAll(){return Promise.resolve([])}listByQid(e,n){return Promise.resolve([])}listLabelsByQid(e,n){return Promise.resolve([])}listBySurfaceTerms(e){return Promise.resolve([])}listBySurfaces(e){return Promise.resolve([])}listByChapter(e){return Promise.resolve([])}},xc=class{countByTriple(e){return Promise.resolve(0)}getById(e){return Promise.resolve(void 0)}listAll(){return Promise.resolve([])}listByTriple(e){return Promise.resolve([])}listByChapter(e){return Promise.resolve([])}},kc=class{getMap(e){return Promise.resolve({})}},Rc=class{getByHash(e){return Promise.resolve(void 0)}},vc=class{get(e,n){return Promise.resolve(void 0)}list(e){return Promise.resolve([])}listCoverage(e){return Promise.resolve([])}listLexicalRows(e,n){return Promise.resolve([])}listEmbeddingSegments(e,n){return Promise.resolve([])}}});var Tc,_c,Cc,Pc,Ac,gv=c(()=>{"use strict";wc();Tc=class{#e;#t;constructor(e,n){this.#e=[...e].sort(cv),this.#t=new Map(n.map(r=>[r.id,r.sentenceId[0]]))}listAll(){return Promise.resolve([...this.#e])}countAll(){return Promise.resolve(this.#e.length)}listBySerial(e){return Promise.resolve(this.#e.filter(n=>this.#t.get(n.fromId)===e&&this.#t.get(n.toId)===e))}listIncoming(e){return Promise.resolve(this.#e.filter(n=>n.toId===e))}listOutgoing(e){return Promise.resolve(this.#e.filter(n=>n.fromId===e))}},_c=class{#e;#t;constructor(e){this.#e=[...e].sort(lv),this.#t=new Map(e.map(n=>[n.id,n]))}getById(e){return Promise.resolve(this.#t.get(e))}listIdsByGroup(e,n){return Promise.resolve(this.#e.filter(r=>r.serialId===e&&r.groupId===n).map(r=>r.id).sort(Un))}listBySerial(e){return Promise.resolve(this.#e.filter(n=>n.serialId===e))}},Cc=class{#e;constructor(e){this.#e=[...e].sort(mv)}listChunkIds(e){return Promise.resolve(this.#e.filter(n=>n.snakeId===e).map(n=>n.chunkId))}listBySnake(e){return Promise.resolve(this.#e.filter(n=>n.snakeId===e))}},Pc=class{#e;#t;constructor(e,n){this.#e=[...e].sort(pv),this.#t=new Map(n.map(r=>[r.id,r.serialId]))}listIncoming(e){return Promise.resolve(this.#e.filter(n=>n.toSnakeId===e))}listOutgoing(e){return Promise.resolve(this.#e.filter(n=>n.fromSnakeId===e))}listWithin(e){let n=new Set(e);return Promise.resolve(this.#e.filter(r=>n.has(r.fromSnakeId)&&n.has(r.toSnakeId)))}listBySerial(e){return Promise.resolve(this.#e.filter(n=>this.#t.get(n.fromSnakeId)===e&&this.#t.get(n.toSnakeId)===e))}},Ac=class{#e;constructor(e){this.#e=[...e].sort(dv)}listBySerial(e){return Promise.resolve(this.#e.filter(n=>n.serialId===e))}listSerialIds(){return Promise.resolve([...new Set(this.#e.map(e=>e.serialId))].sort(Un))}listGroupIdsForSerial(e){return Promise.resolve([...new Set(this.#e.filter(n=>n.serialId===e).map(n=>n.groupId))].sort(Un))}}});var Lc,ui,yv=c(()=>{"use strict";wc();Lc=class{#e;constructor(e){this.#e=e}getById(e){return Promise.resolve(e===this.#e.id?this.#e:void 0)}getRevision(e){return Promise.resolve(e===this.#e.id?this.#e.revision:0)}getRevisions(e){return Promise.resolve(new Map(e.filter(n=>n===this.#e.id).map(n=>[n,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]]))}},ui=class{path="";serialId;#e;constructor(e,n){this.serialId=e,this.#e=new Map(n.filter(r=>r.serialId===e).map(r=>[r.fragmentId,r]))}getFragment(e){let n=this.#e.get(e);if(n===void 0)throw new Error(`Fragment ${e} does not exist`);return Promise.resolve(n)}listFragmentIds(){return Promise.resolve([...this.#e.keys()].sort(Un))}async getSentence(e){let n=(await this.listSentences())[e];if(n===void 0)throw new RangeError(`Sentence ${e} does not exist`);return n}async listSentencesInRange(e,n){return(await this.listSentences()).slice(e,n+1)}async listSentences(){return(await Promise.all((await this.listFragmentIds()).map(async n=>await this.getFragment(n)))).flatMap(n=>n.sentences)}async readText(){let e=await this.listSentences();if(e.length!==0)return e.map(n=>n.text).join("")}}});var Oc,bv=c(()=>{"use strict";hv();fv();gv();yv();Oc=class{chunks;fragmentGroups;graphBuildParameters;indexArtifacts;readingEdges;mentionLinks;mentions;metadata;serials;snakeChunks;snakeEdges;snakes;#e;constructor(e){let n=e.serial.id;this.chunks=new Ec(e.chunks,e.fragments),this.fragmentGroups=new Ac(e.fragmentGroups),this.graphBuildParameters=new Rc,this.indexArtifacts=new vc,this.readingEdges=new Tc(e.readingEdges,e.chunks),this.mentionLinks=new xc,this.mentions=new Ic,this.metadata=new kc,this.serials=new Lc(e.serial),this.snakeChunks=new Cc(e.snakeChunks),this.snakeEdges=new Pc(e.snakeEdges,e.snakes),this.snakes=new _c(e.snakes),this.#e=new ui(n,e.fragments)}async getSentence(e){let[n,r]=e,i=(await this.getSerialFragments(n).listSentences())[r];if(i===void 0)throw new RangeError(`Sentence ${r} does not exist`);return i.text}getSerialFragments(e){return e!==this.#e.serialId?new ui(e,[]):this.#e}getSummaryFragments(e){return new ui(e,[])}async openSession(e){return await e(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(e){return Promise.resolve(void 0)}readToc(){return Promise.resolve(void 0)}release(){return Promise.resolve()}}});import{z as S}from"zod";function Ev(t){return{id:t.id,generation:t.generation,sentenceId:t.sentenceId,label:t.label,content:t.content,sentenceIds:t.sentenceIds,...t.retention===void 0?{}:{retention:t.retention},...t.importance===void 0?{}:{importance:t.importance},wordsCount:t.wordsCount,weight:t.weight}}function Iv(t){return{fromId:t.fromId,toId:t.toId,...t.strength===void 0?{}:{strength:t.strength},weight:t.weight}}var Sv,jB,GB,BB,UB,HB,qB,$B,KB,JB,wv,xv=c(()=>{"use strict";Ke();Sv=S.tuple([S.number(),S.number()]),jB=S.object({text:S.string(),wordsCount:S.number()}),GB=S.object({serialId:S.number(),fragmentId:S.number(),summary:S.string(),sentences:S.array(jB)}),BB=S.object({documentOrder:S.number().optional(),id:S.number(),knowledgeGraphReady:S.boolean(),knowledgeGraphParameterHash:S.string().optional(),revision:S.number().optional(),topologyParameterHash:S.string().optional(),topologyReady:S.boolean()}).transform(t=>({documentOrder:t.documentOrder??t.id,id:t.id,knowledgeGraphReady:t.knowledgeGraphReady,...t.knowledgeGraphParameterHash===void 0?{}:{knowledgeGraphParameterHash:t.knowledgeGraphParameterHash},revision:t.revision??0,topologyReady:t.topologyReady,...t.topologyParameterHash===void 0?{}:{topologyParameterHash:t.topologyParameterHash}})),UB=S.object({id:S.number(),generation:S.number(),sentenceId:Sv,label:S.string(),content:S.string(),sentenceIds:S.array(Sv),retention:S.enum(Oe).optional(),importance:S.enum($e).optional(),wordsCount:S.number(),weight:S.number()}),HB=S.object({fromId:S.number(),toId:S.number(),strength:S.string().optional(),weight:S.number()}),qB=S.object({id:S.number(),serialId:S.number(),groupId:S.number(),localSnakeId:S.number(),size:S.number(),firstLabel:S.string(),lastLabel:S.string(),wordsCount:S.number(),weight:S.number()}),$B=S.object({snakeId:S.number(),chunkId:S.number(),position:S.number()}),KB=S.object({fromSnakeId:S.number(),toSnakeId:S.number(),weight:S.number()}),JB=S.object({serialId:S.number(),groupId:S.number(),startSentenceIndex:S.number(),endSentenceIndex:S.number()}),wv=S.object({chunks:S.array(UB),fragmentGroups:S.array(JB),fragments:S.array(GB),readingEdges:S.array(HB),serial:BB,snakeChunks:S.array($B),snakeEdges:S.array(KB),snakes:S.array(qB)})});import{readFile as zB,writeFile as QB}from"fs/promises";async function kv(t){let e=wv.parse(JSON.parse(await zB(t,"utf8")));return{...e,chunks:e.chunks.map(Ev),readingEdges:e.readingEdges.map(Iv)}}async function Rv(t,e){await QB(t,`${JSON.stringify(e)}
|
|
2230
|
+
`,"utf8")}var vv=c(()=>{"use strict";xv()});async function Tv(t,e,n){let r=t.getSerialFragments(e),i=await r.listFragmentIds();if(i.length<=1)return await XB(r,i);let a=[];for(let o of await t.fragmentGroups.listGroupIdsForSerial(e)){let s=await Za({compressionRatio:.2,document:t,groupId:o,llm:n.llm,maxClues:10,maxIterations:5,scopes:ki,serialId:e,...n.logDirPath===void 0?{}:{logDirPath:n.logDirPath},...n.userLanguage===void 0?{}:{userLanguage:n.userLanguage}});s.trim()!==""&&a.push(s)}return a.join(`
|
|
2015
2231
|
|
|
2016
|
-
`)}async function
|
|
2017
|
-
`)}finally{await
|
|
2232
|
+
`)}async function XB(t,e){return e.length===0?"":(await Promise.all(e.map(async r=>await t.getFragment(r)))).flatMap(r=>r.sentences.map(i=>i.text)).join(" ").trim()}var _v=c(()=>{"use strict";Ri();Hp()});import{mkdir as YB}from"fs/promises";import{join as Cv}from"path";async function Nc(t,e){let n=await kv(e.snapshotPath);return await ZB(n,t,e)}async function Dc(t,e,n){return await t.openSession(async r=>{await jr(r,e,"graphed");let i=await r.indexArtifacts.get(e,"fts")!==void 0;await r.writeSummary(e,n),i&&await mt(r,e)}),await re(t,e)}async function Mc(t,e,n){let r=Cv(n,"summary-input.json"),i=Cv(n,"reading-graph.jsonl");await YB(n,{recursive:!0}),await jr(t,e,"graphed");let a=await fc(t,e),o=await t.snakes.listBySerial(e),s=(await Promise.all(o.map(async d=>await t.snakeChunks.listBySnake(d.id)))).flat();return await Rv(r,{chunks:await t.chunks.listBySerial(e),fragmentGroups:await t.fragmentGroups.listBySerial(e),fragments:a,readingEdges:await t.readingEdges.listBySerial(e),serial:{documentOrder:e,id:e,knowledgeGraphReady:!1,revision:0,topologyReady:!0},snakeChunks:s,snakeEdges:await t.snakeEdges.listBySerial(e),snakes:o}),await Bn(i,vo({chapterId:e,document:t})),{filePath:r,objectsPath:i}}async function ZB(t,e,n){if(t.serial.id!==e)throw new Error(`Summary snapshot belongs to chapter ${t.serial.id}, not chapter ${e}.`);if(!t.serial.topologyReady)throw new Error(`Chapter ${e} is not ready for summary.`);return await Tv(new Oc(t),e,n)}var Pv=c(()=>{"use strict";N();Se();bv();vv();gc();_v();di();Cr()});var Av=c(()=>{"use strict"});var Nh=c(()=>{"use strict";Pv();gc();Av()});async function li(t,e){return{details:await re(t,e),revision:await t.serials.getRevision(e),sourceText:await zR(JR(t,e))}}var Lv=c(()=>{"use strict";Se();Nh()});var Ov=c(()=>{"use strict"});var Nv=c(()=>{"use strict";iv();Lv();Ov()});var Dv=c(()=>{"use strict";Nv();Nh()});import{createReadStream as pme,createWriteStream as eU}from"fs";import{createInterface as fme}from"readline";import{z as H}from"zod";function Mv(t,e){let n=new Set;for(let i of e.mentions){if(i.chapterId!==t)throw new Error(`Mention ${i.id} belongs to chapter ${i.chapterId}, expected chapter ${t}.`);if(i.rangeEnd<=i.rangeStart)throw new Error(`Mention ${i.id} has invalid range [${i.rangeStart}, ${i.rangeEnd}).`);if(n.has(i.id))throw new Error(`Duplicate mention id ${i.id}.`);n.add(i.id)}let r=new Set;for(let i of e.mentionLinks){if(r.has(i.id))throw new Error(`Duplicate mention link id ${i.id}.`);if(!n.has(i.sourceMentionId))throw new Error(`Mention link ${i.id} references unknown source mention ${i.sourceMentionId}.`);if(!n.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]!==t)throw new Error(`Mention link ${i.id} evidence sentence ${iU(a)} is outside chapter ${t}.`);r.add(i.id)}}function iU(t){return t.join(":")}async function Dh(t,e,n){let r=eU(t,{encoding:"utf8",flags:"wx"});try{for await(let i of e)r.write(`${JSON.stringify(n(i))}
|
|
2233
|
+
`)}finally{await aU(r)}}function Mh(t){let e=tU.parse(t);return{chapterId:e.chapterId,...e.confidence===void 0?{}:{confidence:e.confidence},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}}function Wh(t){let e=rU.parse(t);return{...e.confidence===void 0?{}:{confidence:e.confidence},evidenceSentenceIds:e.evidenceSentenceIds,id:e.id,...e.note===void 0?{}:{note:e.note},predicate:e.predicate,sourceMentionId:e.sourceMentionId,targetMentionId:e.targetMentionId}}async function aU(t){await new Promise((e,n)=>{t.end(r=>{if(r!=null){n(r);return}e()})})}var tU,nU,rU,Fh=c(()=>{"use strict";tU=H.object({id:H.string().min(1),chapterId:H.number().int(),sentenceIndex:H.number().int().nonnegative().optional(),rangeStart:H.number().int().nonnegative(),rangeEnd:H.number().int().nonnegative(),surface:H.string().min(1),qid:H.string().regex(/^Q[1-9][0-9]*$/),confidence:H.number().min(0).max(1).optional(),note:H.string().optional()}),nU=H.tuple([H.number().int().nonnegative(),H.number().int().nonnegative()]).readonly(),rU=H.object({id:H.string().min(1),sourceMentionId:H.string().min(1),targetMentionId:H.string().min(1),predicate:H.string().min(1),evidenceSentenceIds:H.array(nU).min(1),confidence:H.number().min(0).max(1).optional(),note:H.string().optional()})});import{mkdir as oU,rm as sU}from"fs/promises";import{join as Wc}from"path";async function Fc(t,e){let n=Wc(e.workspacePath,"knowledge-graph",`chapter-${t}`),r=Wc(n,"mentions.jsonl"),i=Wc(n,"mention-links.jsonl"),a=Wc(n,"wikg-objects.jsonl"),o=e.parameter??{language:"zh",prompt:""};await sU(n,{force:!0,recursive:!0}),await oU(n,{recursive:!0});let s=await Wv(e.mentions,Mh),d=await Wv(e.mentionLinks,Wh);return await Bn(a,Ah({chapterId:t,mentionLinks:d,mentions:s,parameter:o})),await Dh(r,s,Mh),await Dh(i,d,Wh),{chapterId:t,mentionLinksPath:i,mentionsPath:r,objectsPath:a,parameter:o,workspacePath:n}}async function Wv(t,e){let n=[];for await(let r of t)n.push(e(r));return n}var jh=c(()=>{"use strict";nt();di();Fh()});async function jc(t,e){let n=await Lh(e.chapterId,si(e.objectsPath)),r=n.mentions,i=n.mentionLinks,a=n.parameter??e.parameter;Mv(e.chapterId,{mentionLinks:i,mentions:r}),await t.openSession(async o=>{await o.serials.ensure(e.chapterId);let s=await o.mentionLinks.listByChapter(e.chapterId),d=await o.indexArtifacts.get(e.chapterId,"fts")!==void 0;if(s.length>0&&i.length===0)throw new Error(`Refusing to replace chapter ${e.chapterId} knowledge graph with an artifact that contains no mention links.`);await o.clearSerialKnowledgeGraph(e.chapterId),await o.mentions.saveMany(r),await o.mentionLinks.saveMany(i);let u=await o.graphBuildParameters.save(a);await o.serials.setKnowledgeGraphReady(e.chapterId,!0,u.hash),d&&await mt(o,e.chapterId)})}var jv=c(()=>{"use strict";di();Cr();Fh()});function Gh(t){return t.qidOptions.reduce((e,n)=>e+Gv(n),0)}function Gv(t){return t.disambiguation===void 0?1:Bv(t).length}function mi(t){return[...new Set(t.qidOptions.flatMap(e=>Bv(e)))]}function Gc(t,e){return{...t,qidOptions:t.qidOptions.flatMap(n=>{let r=dU(n,e);return r===void 0?[]:[r]})}}function dU(t,e){if(t.disambiguation===void 0)return e.has(t.qid)?t:void 0;let r=(t.disambiguation.profile?.meanings??[]).length>0,i=r?t.disambiguation.linkedQids:t.disambiguation.linkedQids.filter(s=>e.has(s.qid)),a=cU(t.disambiguation.profile,e),o=(a?.meanings.length??0)>0;if(!(r&&!o)&&!(i.length===0&&!o))return{...t,disambiguation:{...t.disambiguation,linkedQids:i,...a===void 0?{}:{profile:a}}}}function cU(t,e){if(t!==void 0)return{...t,meanings:t.meanings.filter(n=>e.has(n.qid))}}function Bv(t){if(t.disambiguation===void 0)return[t.qid];let e=t.disambiguation.profile?.meanings.map(n=>n.qid)??[];return e.length>0?[...new Set(e)]:[...new Set(t.disambiguation.linkedQids.map(n=>n.qid))]}var To=c(()=>{"use strict"});import{z as xt}from"zod";async function Bc(t){try{return await Z({messages:EU(t),parse:e=>Bh(t.candidates,pU(e),t.window.groups),request:t.request,responseIntentClassifierPrompt:ae,schema:mU,...t.maxRetries===void 0?{}:{maxRetries:t.maxRetries}})}catch(e){return{continuations:[],fallback:{issues:[CU(e)],reason:"guaranteed_json_failed"},mentions:[],policyUpdates:[]}}}function pU(t){return{groups:t.groups.map(e=>({decisions:e.decisions.map(n=>({candidateId:n.candidateId,decision:n.decision,...hU(n.qid)})),groupId:e.groupId}))}}function hU(t){let e=t?.trim();return e===void 0||e===""?{}:{qid:e}}function Bh(t,e,n=Hv(t)){let r=Uh(t,e,n);if(r.length>0)throw new M(r);let i=Hh(t),a=[],o=[],s=[];for(let d of e.groups){let u=[];for(let l of d.decisions){let m=i.get(l.candidateId);if(l.decision==="continue"){if(m.hasMoreOptions!==!0){s.push({candidateId:m.id,decision:"skip_this_time",surface:m.surface});continue}u.push(m.id);continue}if(l.decision==="recall"){o.push({candidateId:m.id,qid:l.qid,range:m.range,surface:m.surface});continue}s.push({candidateId:m.id,decision:l.decision,...l.qid===void 0?{}:{qid:l.qid},surface:m.surface})}u.length>0&&a.push({candidateIds:u,groupId:d.groupId})}return{continuations:a,mentions:o,policyUpdates:s}}function Uh(t,e,n=Hv(t)){let r=[],i=Hh(t),a=wU(n),o=new Set,s=new Set,d=[];for(let u of e.groups){let l=a.get(u.groupId);if(l===void 0){r.push(`Unknown groupId "${u.groupId}". Use exactly these group IDs: ${[...a.keys()].join(", ")}.`);continue}if(o.has(u.groupId)){r.push(`Duplicate group result for ${u.groupId}.`);continue}o.add(u.groupId);for(let m of u.decisions){let p=i.get(m.candidateId);if(p===void 0){r.push(`Unknown candidateId "${m.candidateId}". Use only one of: ${[...i.keys()].join(", ")}.`);continue}if(!l.has(m.candidateId)){r.push(`Candidate ${m.candidateId} does not belong to group ${u.groupId}. Use only candidates from that group: ${[...l].join(", ")}.`);continue}if(s.has(m.candidateId)){r.push(`Duplicate decision for candidate ${m.candidateId}. Return exactly one decision per candidate at most.`);continue}if(s.add(m.candidateId),m.decision==="recall"){fU(r,p,m),d.push({candidate:p,decision:m});continue}if(m.decision==="continue"){m.qid!==void 0&&r.push(`Candidate ${p.id} uses decision "continue" but includes qid. Continue means this candidate page has no final choice yet.`);continue}m.qid!==void 0&&r.push(`Candidate ${p.id} uses decision "${m.decision}" but includes qid. Include qid only when decision is "recall".`)}}for(let u of a.keys())o.has(u)||r.push(`Missing group result for ${u}. Return exactly one result for every input group; use decisions: [] when nothing should be recalled.`);for(let u=0;u<d.length;u+=1)for(let l=u+1;l<d.length;l+=1){let m=d[u],p=d[l];SU(m.candidate.range,p.candidate.range)&&r.push(`Decision conflict: candidates ${m.candidate.id} and ${p.candidate.id} both use decision "recall", but their ranges overlap.
|
|
2018
2234
|
- ${m.candidate.id}: "${m.candidate.surface}" [${m.candidate.range.start}, ${m.candidate.range.end})
|
|
2019
2235
|
- ${p.candidate.id}: "${p.candidate.surface}" [${p.candidate.range.start}, ${p.candidate.range.end})
|
|
2020
|
-
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
|
|
2021
|
-
`)}function
|
|
2236
|
+
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 r}function fU(t,e,n){if(n.qid===void 0){t.push(`Candidate ${e.id} uses decision "recall" but does not provide qid. A recalled mention must choose one concrete QID from the candidate options.`);return}if(yU(e,n.qid)){t.push(`Candidate ${e.id} selected source disambiguation QID ${n.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}gU(e,n.qid)||t.push(bU(e,n.qid))}function gU(t,e){return Uv(t).includes(e)}function yU(t,e){return t.qidOptions.some(n=>n.disambiguation!==void 0&&n.qid===e)}function Uv(t){return mi(t)}function bU(t,e){return`Candidate ${t.id} selected qid ${e}, but that QID is not available for this candidate. Allowed QIDs for "${t.surface}" are: ${Uv(t).join(", ")}.`}function SU(t,e){return t.start<e.end&&e.start<t.end}function Hh(t){return new Map(t.map(e=>[e.id,e]))}function wU(t){return new Map(t.map(e=>[e.id,new Set(e.candidateIds)]))}function Hv(t){return t.length===0?[]:[{candidateIds:t.map(e=>e.id),id:"g1",range:{end:Math.max(...t.map(e=>e.range.end)),start:Math.min(...t.map(e=>e.range.start))}}]}function EU(t){return[{role:"system",content:IU(t)},{role:"user",content:xU(t)}]}function IU(t){return["You judge which precomputed Wikidata mention candidates should be recalled.","","Recall policy:",t.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(`
|
|
2237
|
+
`)}function xU(t){return["Tagged context:",kU(t),"","Candidate groups:",RU(t).map(e=>JSON.stringify(e)).join(`
|
|
2022
2238
|
`),"","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(`
|
|
2023
|
-
`)}function
|
|
2024
|
-
`);if(t instanceof Error){let e=t.issues;return Array.isArray(e)&&e.every(
|
|
2025
|
-
`):t.message}return String(t)}var
|
|
2026
|
-
`)}function
|
|
2027
|
-
`)}function
|
|
2028
|
-
`);if(t instanceof Error){let e=t.issues;return Array.isArray(e)&&e.every(
|
|
2029
|
-
`):t.message}return String(t)}var
|
|
2030
|
-
WikiSpine setup guide: ${L0}`}var L0,dh,QR=d(()=>{"use strict";L0="https://raw.githubusercontent.com/oomol-lab/wiki-graph/refs/heads/main/docs/wikispine-runtime.md",dh="https://wikispi-service-cxbfjlteab.cn-hangzhou.fcapp.run"});function XR(t){let e=new Intl.Segmenter(void 0,{granularity:"word"}),r=new Intl.Segmenter(void 0,{granularity:"grapheme"}),n=[];for(let i of e.segment(t))"isWordLike"in i&&i.isWordLike&&n.push({end:i.index+i.segment.length,start:i.index});return n.length>0?n:[...r.segment(t)].filter(i=>i.segment.trim()!=="").map(i=>({end:i.index+i.segment.length,start:i.index}))}function lh(t){let e=XR(t.text);if(e.length===0)return{end:t.text.length,start:0};let r=U0(e,t.rangeStart),n=H0(e,t.rangeEnd),i=Math.max(0,Math.floor(t.words/2)),a=Math.max(0,r-i),o=Math.min(e.length-1,n+i);return{end:e[o]?.end??t.text.length,start:e[a]?.start??0}}function U0(t,e){let r=t.findIndex(n=>n.end>e);return r===-1?t.length-1:r}function H0(t,e){for(let r=t.length-1;r>=0;r-=1){let n=t[r];if(n!==void 0&&n.start<e)return r}return 0}var mh=d(()=>{"use strict"});function dd(t){let e=$0(K0(t.candidates??[])),r=[],n=[],i=0;for(let a of e){let o=q0(a);n.length>0&&i+o>t.optionBudget&&(r.push(VR(t.text,n,t)),n=[],i=0),n.push(a),i+=o}return n.length>0&&r.push(VR(t.text,n,t)),r}function q0(t){return t.candidates.reduce((e,r)=>e+Xp(r),0)}function VR(t,e,r){let n=Math.min(...e.map(o=>o.range.start)),i=Math.max(...e.map(o=>o.range.end)),a=lh({rangeEnd:i,rangeStart:n,text:t,words:r.contextWords});return{baseOffset:a.start,candidates:e.flatMap(o=>o.candidates),groups:e.map((o,s)=>({candidateIds:o.candidates.map(c=>c.id),id:`g${s+1}`,range:o.range})),text:t.slice(a.start,a.end)}}function $0(t){let e=[],r;for(let n of t){if(r===void 0||n.range.start>=r.range.end){r={candidates:[n],range:n.range},e.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)}},e[e.length-1]=r}return e}function K0(t){return t.filter(e=>e.range.start<e.range.end).sort(J0)}function J0(t,e){return t.range.start-e.range.start||e.range.end-t.range.end||t.id.localeCompare(e.id)}var YR=d(()=>{"use strict";mh();eo()});var vr=d(()=>{"use strict";th();jR();eo();BR();$R();KR();oh();QR();YR();mh()});var ud=d(()=>{"use strict"});async function ev(t){if(t.candidates.length===0)return[];let e=sh({candidates:t.candidates,percentile:.1,text:t.text});await t.progressTracker?.updatePhase({done:0,phase:"screening",phaseDetail:`${e.suspiciousSurfaces.length} high-frequency surfaces`,total:1,unit:"window"}),await t.progressTracker?.throwIfStopped();let r=await ih({policyPrompt:t.policyPrompt,request:t.request,suspiciousSurfaces:e.suspiciousSurfaces});await t.progressTracker?.throwIfStopped();let n=new Set(r.protectedSurfaces.map(a=>a.text)),i=new Set(e.candidates.map(ZR));return await t.progressTracker?.updatePhase({done:1,phase:"screening",phaseDetail:`${n.size} protected surfaces`,total:1,unit:"window"}),e.suppressedCandidates.filter(a=>i.has(ZR(a))||n.has(a.surface))}function tv(t){return new Set(t.flatMap(e=>e.qidOptions.map(r=>r.qid))).size}function ZR(t){return`${t.range.start}\0${t.range.end}\0${t.surface}`}var rv=d(()=>{"use strict";vr();ud()});async function nv(t,e){let r=t.getSerialFragments(e);return await Promise.all((await r.listFragmentIds()).map(async n=>await r.getFragment(n)))}function iv(t){return t.flatMap(e=>e.sentences.map(r=>r.text)).join(" ")}function av(t){let e=[],r=0;for(let n of t)for(let i=0;i<n.sentences.length;i+=1){let a=n.sentences[i];e.push({id:`${n.serialId}:${n.fragmentId+i}`,range:{end:r+a.text.length,start:r},text:a.text}),r+=a.text.length+1}return e}var ph=d(()=>{"use strict"});function cv(t){let e=new Map(t.map(c=>[c.id,c])),r=new Map(t.map(c=>[c.id,new Set])),n=new Map(t.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),io(o,c.surface).recallCount+=1,a.set(ao(c.surface,c.qid),(a.get(ao(c.surface,c.qid))??0)+1)},close(c,u){i.add(c);let l=e.get(c);l!==void 0&&(u==="skip_this_time"||u==="never_recall")&&(io(o,l.surface).rejectCount+=1)},continue(c){let u=e.get(c);u!==void 0&&(io(o,u.surface).continueCount+=1)},getStats(){return{...s}},nextPage(c){let u=[],l=c===void 0?t.map(m=>m.id):[...c];for(let m of l){if(i.has(m))continue;let p=e.get(m),h=r.get(m);if(p===void 0||h===void 0)continue;let g=eB(p,a),f=Kn(g),E=n.get(m)??0,v=Y0(io(o,p.surface),E),I=f.filter(_=>!h.has(_)).slice(0,v);if(I.length===0){i.add(m);continue}for(let _ of I)h.add(_);let q=f.some(_=>!h.has(_)),F=sd(g,new Set(I));q||i.add(m),io(o,p.surface).seenCount+=1,n.set(m,E+1),s.candidatePageCount+=1,s.qidAppearanceCount+=I.length,u.push({...F,...q?{hasMoreOptions:!0}:{}})}return u}}}function io(t,e){let r=t.get(e);if(r!==void 0)return r;let n={continueCount:0,recallCount:0,rejectCount:0,seenCount:0};return t.set(e,n),n}function Y0(t,e){let r=Z0(t)?V0:X0;return r[Math.min(e,r.length-1)]}function Z0(t){return t.seenCount<ov?!1:t.recallCount>=2&&t.continueCount===0||t.rejectCount>=2&&t.recallCount===0||t.continueCount>=ov}function dv(t,e){return`efficiency qid/mention=${e===0?"n/a":(t.qidAppearanceCount/e).toFixed(1)} qids=${t.qidAppearanceCount} mentions=${e} pages=${t.candidatePageCount}`}function eB(t,e){return{...t,qidOptions:[...t.qidOptions].sort((r,n)=>sv(t.surface,n,e)-sv(t.surface,r,e))}}function sv(t,e,r){let n=r.get(ao(t,e.qid))??0,i=e.disambiguation?.linkedQids.reduce((o,s)=>o+(r.get(ao(t,s.qid))??0),0)??0,a=e.disambiguation?.profile?.meanings.reduce((o,s)=>o+(r.get(ao(t,s.qid))??0),0)??0;return n+i+a}function ao(t,e){return`${t}\0${e}`}var X0,V0,ov,uv=d(()=>{"use strict";vr();X0=[5,10,20,35],V0=[3,5,10,20,35],ov=3});async function ld(t,e,r){let n=t.lazy;if(n!==void 0)return await Promise.all(e.map(async a=>await n(async o=>await r(a,o))));let i=[];for(let a of e)i.push(await r(a,t));return i}var hh=d(()=>{"use strict"});async function md(t){let e=[],r=cv(t.candidates),n=0,i=0,a=r.nextPage();for(;a.length>0;){let o=dd({candidates:a,contextWords:220,optionBudget:50,text:t.text});if(i+=o.length,o.length===0)break;await t.progressTracker?.updatePhase({done:n,phase:"grounding",total:i,unit:"window"});let s=await ld(t.request,o,async(u,l)=>{try{return await t.progressTracker?.throwIfStopped(),await cd({candidates:u.candidates,policyPrompt:t.policyPrompt,request:l,window:u})}finally{n+=1,await t.progressTracker?.updatePhase({done:n,phase:"grounding",total:i,unit:"window"})}}),c=new Set;for(let u of s){for(let l of u.mentions)e.push(l),r.accept(l);for(let l of u.policyUpdates)r.close(l.candidateId,l.decision);for(let l of u.continuations)for(let m of l.candidateIds)c.add(m),r.continue(m)}a=r.nextPage(c)}return await t.progressTracker?.updatePhase({done:n,phase:"grounding",phaseDetail:dv(r.getStats(),e.length),total:i,unit:"window"}),e}var fh=d(()=>{"use strict";vr();ud();uv();hh()});async function lv(t){let e=[],r=nB(t.fragments),n=await md({candidates:t.candidates,policyPrompt:t.policyPrompt,...t.progressTracker===void 0?{}:{progressTracker:t.progressTracker},request:t.request,text:t.text}),i=1;for(let a of n){let o=iB(r,a.range.start);e.push(rB(t.chapterId,a,o,i)),i+=1}return e}function rB(t,e,r,n){return{chapterId:t,...e.confidence===void 0?{}:{confidence:e.confidence},id:`m${t}-${n}`,...e.note===void 0?{}:{note:e.note},qid:e.qid,rangeEnd:r.rangeStart+e.surface.length,rangeStart:r.rangeStart,sentenceIndex:r.sentenceIndex,surface:e.surface}}function nB(t){let e=[],r=0;for(let n of t)for(let i=0;i<n.sentences.length;i+=1){let a=n.sentences[i].text.length;e.push({absoluteStart:r,length:a,sentenceIndex:n.fragmentId+i}),r+=a+1}return e}function iB(t,e){for(let r of t){let n=r.absoluteStart+r.length;if(e>=r.absoluteStart&&e<n)return{rangeStart:e-r.absoluteStart,sentenceIndex:r.sentenceIndex}}throw new Error(`Mention offset ${e} is outside chapter text.`)}var mv=d(()=>{"use strict";fh()});function pv(t){return{language:Lr(t.resolverOptions?.language)??"zh",prompt:t.policyPrompt}}var hv=d(()=>{"use strict";Ve()});function pd(t){return async e=>{await t.throwIfStopped();let r=aB(e);await t.updatePhase({done:e.done,...r.detail===void 0?{}:{phaseDetail:r.detail},phase:"enrichment",total:e.total,unit:r.unit})}}function aB(t){switch(t.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 yh=d(()=>{"use strict"});var fv=d(()=>{"use strict";Um();Tn();rc()});import{z as W}from"zod";async function gh(t){if(t.window.mentions.length<2)return[];try{return await z({messages:hB(t),parse:e=>mB(t,e),request:t.request,responseIntentClassifierPrompt:ee,schema:dB,...t.maxRetries===void 0?{}:{maxRetries:t.maxRetries}})}catch{return[]}}function mB(t,e){let r=new Map(t.window.mentions.map(m=>[m.id,m])),n=new jt(t.sentences.map((m,p)=>({sentenceId:[t.chapterId,(t.fragmentId??0)+p],text:m.text,wordsCount:m.wordsCount}))),i=Sv(t.sentences),a=n.sentences.flatMap((m,p)=>{let h=i[p];return h===void 0||!bv(h,t.window.range)?[]:[{id:`S${p+1}`,sentenceId:m.sentenceId,text:m.projectedText}]}),o=[],s=[],c=new Set,u=a.map(m=>m.sentenceId),l=a.map(m=>m.text);for(let[m,p]of e.relations.entries()){let h=`relations[${m}]`,g=r.get(p.sourceMentionId),f=r.get(p.targetMentionId);if(g===void 0){o.push(`${h}.sourceMentionId references an unknown mention.`);continue}if(f===void 0){o.push(`${h}.targetMentionId references an unknown mention.`);continue}if(g.id===f.id){o.push(`${h} 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=IB(p.predicate);if(E===""){o.push(`${h}.predicate normalizes to an empty label.`);continue}if(E==="mentions"){o.push(`${h}.predicate must be semantic; "mentions" is not a relation predicate.`);continue}let[v,I]=SB({evidence:wB(p.evidence),sentences:a}),[q,F]=!Array.isArray(p.evidence)&&v===void 0&&I===void 0?lB.resolve(p.evidence,u,l):[void 0,void 0],_=v??q,bt=I===void 0?F:EB(I);if(_===void 0){o.push(`${h}.evidence could not be resolved: ${bt?.message??"unknown failure"}`);continue}let So=pB(_.sentenceIds);if(So.length===0){o.push(`${h}.evidence resolved to no sentences.`);continue}if(So.some(([Ld,m_])=>Ld!==t.chapterId||i[m_-(t.fragmentId??0)]===void 0)){o.push(`${h}.evidence resolved outside this fragment.`);continue}let Vh=`${g.id}\0${f.id}\0${E}\0${So.map(Ld=>Ld.join(":")).join(",")}`;c.has(Vh)||(c.add(Vh),s.push({...p.confidence===void 0?{}:{confidence:p.confidence},evidenceSentenceIds:So,predicate:E,sourceMentionId:g.id,targetMentionId:f.id}))}if(o.length>0)throw new O(o);return s}function pB(t){let e=new Set,r=[];for(let n of t){let i=n.join(":");e.has(i)||(e.add(i),r.push(n))}return r.sort(([n,i],[a,o])=>n-a||i-o)}function hB(t){return[{content:fB(),role:"system"},{content:yB(t),role:"user"}]}function fB(){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.",yr,"- 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:",uB.join(", ")].join(`
|
|
2031
|
-
`)}function
|
|
2032
|
-
`)}function
|
|
2033
|
-
`)}function bB(t){let e=t.mentions.filter(i=>i.range.start>=t.sentenceOffset.start&&i.range.end<=t.sentenceOffset.end).sort((i,a)=>i.range.start-a.range.start),r=[],n=t.sentenceOffset.start;for(let i of e)r.push(Tr(t.sentence.text.slice(n-t.sentenceOffset.start,i.range.start-t.sentenceOffset.start))),r.push(`<mention id="${hd(i.id)}" qid="${hd(i.qid??"")}">${Tr(t.sentence.text.slice(i.range.start-t.sentenceOffset.start,i.range.end-t.sentenceOffset.start))}</mention>`),n=i.range.end;return r.push(Tr(t.sentence.text.slice(n-t.sentenceOffset.start))),r.join("")}function bv(t,e){return t.start<e.end&&e.start<t.end}function SB(t){return t.evidence===void 0?[void 0,void 0]:vn({evidence:t.evidence,sentences:t.sentences})}function wB(t){return Array.isArray(t)?t.map(gv):typeof t.quote=="string"||typeof t.sentence_id=="string"?gv(t):void 0}function gv(t){return{...typeof t.quote=="string"?{quote:t.quote}:{},...typeof t.sentence_id=="string"?{sentence_id:t.sentence_id}:{}}}function EB(t){return{candidates:t.candidates.map(e=>({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})),code:t.code,fieldName:"evidence",message:t.message}}function kB(t){let e=[...t.mentions].sort((i,a)=>i.range.start-a.range.start),r=[],n=t.range.start;for(let i of e)r.push(Tr(t.text.slice(n-t.range.start,i.range.start-t.range.start))),r.push(`<mention id="${hd(i.id)}" qid="${hd(i.qid??"")}">${Tr(t.text.slice(i.range.start-t.range.start,i.range.end-t.range.start))}</mention>`),n=i.range.end;return r.push(Tr(t.text.slice(n-t.range.start))),r.join("")}function Sv(t){let e=[],r=0;for(let n of t){let i=r,a=i+n.text.length;e.push({end:a,start:i}),r=a+1}return e}function IB(t){return t.trim().replace(/[\s-]+/gu,"_").replace(/_+/gu,"_").replace(/^_+|_+$/gu,"").toLowerCase()}function hd(t){return Tr(t).replaceAll('"',""")}function Tr(t){return t.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">")}var yv,oB,sB,cB,dB,uB,lB,wv=d(()=>{"use strict";Ie();Tn();fv();yv=W.object({head:W.string().optional(),mode:W.enum(["full","head_tail"]).optional(),tail:W.string().optional(),text:W.string().optional()}).strict(),oB=W.object({quote:W.string().optional(),sentence_id:W.string().optional()}).strict(),sB=W.union([W.object({end_anchor:W.union([W.string(),yv]).optional(),quote:W.string().optional(),sentence_id:W.string().optional(),start_anchor:W.union([W.string(),yv]).optional()}).strict(),W.array(oB)]),cB=W.object({confidence:W.number().min(0).max(1).optional(),evidence:sB,predicate:W.string().min(1).max(64),sourceMentionId:W.string().min(1),targetMentionId:W.string().min(1)}).strict(),dB=W.object({relations:W.array(cB)}).strict(),uB=["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"],lB=new Wt});function bh(t){RB(t);let e=vB(t.mentions);if(e.length===0||t.text.length===0)return[];let r=t.windowLength-t.maxEvidenceDistance,n=[];for(let i=0;i<t.text.length;i=xB(i,r,t.text.length)){let a=Math.min(i+t.windowLength,t.text.length),o=e.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:t.text.slice(i,a)}),a>=t.text.length)break}return n}function xB(t,e,r){return Math.min(t+e,r)}function RB(t){if(!Number.isInteger(t.windowLength)||t.windowLength<=0)throw new Error("windowLength must be a positive integer.");if(!Number.isInteger(t.maxEvidenceDistance)||t.maxEvidenceDistance<=0)throw new Error("maxEvidenceDistance must be a positive integer.");if(t.windowLength<=t.maxEvidenceDistance)throw new Error("windowLength must be greater than maxEvidenceDistance so eligible mention pairs are guaranteed to share at least one evidence window.")}function vB(t){return t.filter(e=>e.range.start<e.range.end).sort(TB)}function TB(t,e){return t.range.start-e.range.start||t.range.end-e.range.end||t.id.localeCompare(e.id)}var Ev=d(()=>{"use strict"});var kv=d(()=>{"use strict";wv();Ev()});async function Iv(t){let e=PB(t.fragments,t.mentions),r=0;return await t.progressTracker?.updatePhase({done:0,phase:"relation-discovery",total:e.length,unit:"window"}),(await ld(t.request,e,async(i,a)=>{try{return await t.progressTracker?.throwIfStopped(),await gh({chapterId:i.fragment.serialId,fragmentId:i.fragment.fragmentId,request:a,sentences:i.fragment.sentences,window:i.window})}finally{r+=1,await t.progressTracker?.updatePhase({done:r,phase:"relation-discovery",total:e.length,unit:"window"})}})).flat().map((i,a)=>({...i.confidence===void 0?{}:{confidence:i.confidence},evidenceSentenceIds:i.evidenceSentenceIds,id:`l${t.fragments[0]?.serialId??0}-${a+1}`,predicate:i.predicate,sourceMentionId:i.sourceMentionId,targetMentionId:i.targetMentionId}))}function PB(t,e){return t.flatMap(r=>{let n=r.fragmentId,i=n+r.sentences.length-1,a=LB(r.sentences,e.filter(s=>s.sentenceIndex!==void 0&&s.sentenceIndex>=n&&s.sentenceIndex<=i),n);return bh({maxEvidenceDistance:700,mentions:a,text:OB(r.sentences),windowLength:1800}).map(s=>({fragment:r,window:s}))})}function LB(t,e,r){let n=AB(t);return e.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 AB(t){let e=[],r=0;for(let n of t){let i=r,a=i+n.text.length;e.push({end:a,start:i}),r=a+1}return e}function OB(t){return t.map(e=>e.text).join(" ")}var xv=d(()=>{"use strict";kv();ud();hh()});async function oo(t,e){let r=await Y(t,e);if(r.stage==="planned")throw new Error(`Chapter ${e} is planned. Set source before generating Knowledge Graph.`);return{details:r,fragments:await nv(t,e)}}var Sh=d(()=>{"use strict";be();ph()});async function fd(t,e,r){if(e.details.chapterId!==t)throw new Error(`Knowledge Graph snapshot belongs to chapter ${e.details.chapterId}, not chapter ${t}.`);if(e.details.stage==="planned")throw new Error(`Chapter ${t} is planned. Set source before generating Knowledge Graph.`);let n=e.fragments,i=iv(n),a=av(n),o=wr(r.policyPrompt);await r.progressTracker?.updatePhase({done:0,phase:"matching",phaseDetail:"text",total:i.length,unit:"char"});let s=await uh({includeDisambiguation:!0,onProgress:async h=>{await r.progressTracker?.updatePhase({done:h.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 ev({candidates:s,policyPrompt:o,...r.progressTracker===void 0?{}:{progressTracker:r.progressTracker},request:r.request,text:i});await r.progressTracker?.throwIfStopped();let u=tv(c);await r.progressTracker?.updatePhase({done:0,phase:"enrichment",total:u,unit:"qid"});let l=await nh(c,{...r.progressTracker===void 0?{}:{progress:pd(r.progressTracker)},...r.resolverOptions===void 0?{}:{resolverOptions:r.resolverOptions}});await r.progressTracker?.throwIfStopped(),await r.progressTracker?.updatePhase({done:u,phase:"enrichment",total:u,unit:"qid"});let m=await lv({candidates:l,chapterId:t,fragments:n,policyPrompt:o,...r.progressTracker===void 0?{}:{progressTracker:r.progressTracker},request:r.request,text:i});await r.progressTracker?.throwIfStopped();let p=await Iv({fragments:n,mentions:m,...r.progressTracker===void 0?{}:{progressTracker:r.progressTracker},request:r.request});return await r.progressTracker?.throwIfStopped(),await ad(t,{mentionLinks:p,mentions:m,parameter:pv({policyPrompt:o,...r.resolverOptions===void 0?{}:{resolverOptions:r.resolverOptions}}),workspacePath:r.workspacePath})}var Rv=d(()=>{"use strict";vr();Bt();Qp();rv();ph();mv();hv();yh();xv();Sh()});var vv=d(()=>{"use strict"});var Tv=d(()=>{"use strict";Qp();OR();Rv();fh();yh();Sh();vv()});var _v=d(()=>{"use strict";Tv()});var so=d(()=>{"use strict";be()});var wh=d(()=>{"use strict";Bt()});var Eh=d(()=>{"use strict";Ve();Bt();Mn();so();wh()});var Lv=d(()=>{"use strict";Bt();so();Eh()});function yd(t){return{children:t.children.map(yd),...t.key===void 0?{}:{key:t.key},...t.serialId===void 0?{}:{serialId:t.serialId},title:t.title}}var kh=d(()=>{"use strict";rt()});async function Av(t){let e=await t.readToc(),r=e?.items.map(yd)??[];return MB(r),e===void 0?{items:[],version:it}:{items:r,version:e.version}}function MB(t){let e=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(e.has(i.key))throw new Error(`Duplicate chapter key: ${i.key}.`);e.add(i.key),r(i.children)}};r(t)}var Ov=d(()=>{"use strict";rt();ki();Zt();kh()});async function zn(t,e){let r=await Av(t),n=Sy(r.items,e);if(n===void 0)throw new Error(`Chapter path ${e} does not exist.`);return n}var Nv=d(()=>{"use strict";Mn();rt();so();Ov();kh()});import{z as _r}from"zod";var Dv,Kle,Mv=d(()=>{"use strict";Dv=_r.object({children:_r.lazy(()=>_r.array(Dv)),uri:_r.string().min(1),title:_r.string().nullable().optional()}).strict(),Kle=_r.object({chapters:_r.array(Dv)}).strict()});var Wv=d(()=>{"use strict";be()});var Fv=d(()=>{"use strict";Lv();so();Eh();Nv();wh();Mv();Wv()});var jv=d(()=>{"use strict";zi()});var qt=d(()=>{"use strict";ux();Rn();Gx();CR();_v();Rn();vp();Fv();jv();Ec()});import{randomUUID as ZB}from"crypto";import{join as eU}from"path";async function Gv(t="global"){let e=await Ih(),r=`${process.pid}-${ZB()}`,n=Date.now(),i=!1;try{await e.transaction(async()=>{await aU(e),await e.run(`
|
|
2239
|
+
`)}function kU(t){let e=[...t.window.groups].sort((i,a)=>i.range.start-a.range.start),n=[],r=t.window.baseOffset;for(let i of e)n.push(t.window.text.slice(r-t.window.baseOffset,i.range.start-t.window.baseOffset)),n.push(`<group id="${_U(i.id)}">${qv(t.window.text.slice(i.range.start-t.window.baseOffset,i.range.end-t.window.baseOffset))}</group>`),r=i.range.end;return n.push(t.window.text.slice(r-t.window.baseOffset)),n.join("")}function RU(t){let e=Hh(t.window.candidates);return t.window.groups.map(n=>({candidates:n.candidateIds.flatMap(r=>{let i=e.get(r);return i===void 0?[]:[qh(i)]}),groupId:n.id}))}function qh(t,e){let n=vU(t.qidOptions);return{candidateId:t.id,...t.hasMoreOptions===!0?{hasMoreOptions:!0}:{},...n.disambiguationOptions.length===0?{}:{disambiguationOptions:n.disambiguationOptions},...n.entityOptions.length===0?{}:{entityOptions:n.entityOptions}}}function vU(t){let e=[],n=[];for(let r of t){if(r.disambiguation!==void 0){let i=r.disambiguation.profile?.meanings??[];e.push({...r.label===void 0?{}:{label:r.label},meanings:i.length>0?i.map(TU):r.disambiguation.linkedQids.map(a=>({name:a.title,qid:a.qid}))});continue}n.push({...r.description===void 0?{}:{description:r.description},...r.label===void 0?{}:{label:r.label},qid:r.qid})}return{disambiguationOptions:e,entityOptions:n}}function TU(t){return{...t.information===""?{}:{information:t.information},name:t.name,qid:t.qid}}function _U(t){return qv(t).replaceAll('"',""")}function qv(t){return t.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">")}function CU(t){if(t instanceof M)return t.issues.join(`
|
|
2240
|
+
`);if(t instanceof Error){let e=t.issues;return Array.isArray(e)&&e.every(n=>typeof n=="string")?e.join(`
|
|
2241
|
+
`):t.message}return String(t)}var uU,lU,mU,$h=c(()=>{"use strict";ve();To();uU=xt.object({candidateId:xt.string().min(1),decision:xt.enum(["continue","recall","skip_this_time","never_recall"]),qid:xt.string().nullable().optional()}).strict(),lU=xt.object({decisions:xt.array(uU),groupId:xt.string().min(1)}).strict(),mU=xt.object({groups:xt.array(lU)}).strict()});import{z as _o}from"zod";var AU,Wme,$v=c(()=>{"use strict";ve();To();$h();AU=_o.object({decision:_o.enum(["keep","reject"]),qid:_o.string().regex(/^Q[1-9][0-9]*$/)}).strict(),Wme=_o.object({qids:_o.array(AU)}).strict()});async function Jh(t,e={}){if(t.length===0)return[];if(e.resolver!==void 0)return Kh(t,await e.resolver.resolveQids(Kv(t)));let n=await An.open({...e.resolverOptions??{},...e.progress===void 0?{}:{progress:e.progress}});try{return Kh(t,await n.resolveQids(Kv(t)))}finally{await n.close()}}function Kh(t,e){let n=new Map(e.map(r=>[r.qid,r]));return t.map(r=>({...r,qidOptions:r.qidOptions.map(i=>LU(i,n.get(i.qid)))}))}function LU(t,e){return e===void 0?t:{...t,...e.description===void 0?{}:{description:e.description},...e.disambiguation===void 0?{}:{disambiguation:e.disambiguation},isDisambiguation:e.isDisambiguation,...e.label===void 0?{}:{label:e.label}}}function Kv(t){return[...new Set(t.flatMap(e=>e.qidOptions.map(n=>n.qid)))]}var Jv=c(()=>{"use strict";Wa()});import{z as Co}from"zod";async function zh(t){if(t.suspiciousSurfaces.length===0)return{protectedSurfaces:[]};try{return await Z({messages:MU(t),parse:e=>zv(t.suspiciousSurfaces,DU(e)),request:t.request,responseIntentClassifierPrompt:ae,schema:NU,...t.maxRetries===void 0?{}:{maxRetries:t.maxRetries}})}catch(e){return{fallback:{issues:[jU(e)],reason:"guaranteed_json_failed"},protectedSurfaces:[]}}}function DU(t){return{protectedSurfaces:t.protectedSurfaces.map(e=>({...e.note===void 0?{}:{note:e.note},surfaceId:e.surfaceId}))}}function zv(t,e){let n=Qv(t,e);if(n.length>0)throw new M(n);let r=Xv(t),i=[];for(let a of e.protectedSurfaces){let o=r.get(a.surfaceId);i.push({...a.note===void 0?{}:{note:a.note},surfaceId:a.surfaceId,text:o.text})}return{protectedSurfaces:i}}function Qv(t,e){let n=[],r=Xv(t),i=new Set;for(let a of e.protectedSurfaces){if(!r.has(a.surfaceId)){n.push(`Unknown surfaceId "${a.surfaceId}". Use only these surface IDs: ${[...r.keys()].join(", ")}.`);continue}if(i.has(a.surfaceId)){n.push(`Duplicate protected surface ${a.surfaceId}.`);continue}i.add(a.surfaceId)}return n}function MU(t){return[{role:"system",content:WU(t)},{role:"user",content:FU(t)}]}function WU(t){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:",t.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(`
|
|
2242
|
+
`)}function FU(t){return["Suspicious high-frequency surfaces:",JSON.stringify(t.suspiciousSurfaces.map(e=>({count:e.count,surfaceId:e.id,text:e.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(`
|
|
2243
|
+
`)}function Xv(t){return new Map(t.map(e=>[e.id,e]))}function jU(t){if(t instanceof M)return t.issues.join(`
|
|
2244
|
+
`);if(t instanceof Error){let e=t.issues;return Array.isArray(e)&&e.every(n=>typeof n=="string")?e.join(`
|
|
2245
|
+
`):t.message}return String(t)}var OU,NU,Vv=c(()=>{"use strict";ve();OU=Co.object({note:Co.string().max(24).optional(),surfaceId:Co.string().min(1)}).strict(),NU=Co.object({protectedSurfaces:Co.array(OU)}).strict()});function Qh(t){let e=[...t].filter(r=>r.range.start<r.range.end).sort(GU),n=[];for(let r of e)n.some(i=>UU(i,r))||n.push(r);return n.sort(BU)}function GU(t,e){return t.range.start-e.range.start||e.range.end-t.range.end||pi(e)-pi(t)}function BU(t,e){return t.range.start-e.range.start||t.range.end-e.range.end||pi(t)-pi(e)}function UU(t,e){return t.range.start<=e.range.start&&e.range.end<=t.range.end&&pi(t)>pi(e)}function pi(t){return t.range.end-t.range.start}var Xh=c(()=>{"use strict"});function Vh(t){let e=Qh(t.candidates),n=HU(e,t.text),r=qU(n,t.percentile),i=new Set(r.map(a=>a.text));return{candidates:e.filter(a=>!i.has(t.text.slice(a.range.start,a.range.end))),suppressedCandidates:e,suspiciousSurfaces:r}}function HU(t,e){let n=new Map;for(let r of t){let i=e.slice(r.range.start,r.range.end);n.set(i,(n.get(i)??0)+1)}return[...n.entries()].map(([r,i],a)=>({count:i,id:`s${a+1}`,text:r})).sort($U)}function qU(t,e){if(t.length===0||e<=0)return[];let n=Math.ceil(t.length*Math.min(1,e));return t.slice(0,n)}function $U(t,e){return e.count-t.count||t.text.localeCompare(e.text)||t.id.localeCompare(e.id)}var Yv=c(()=>{"use strict";Xh()});import{spawn as KU}from"child_process";async function ef(t){let e=[],n=1;for(let r of t.sentences){for(let i of await zU(r,t)){let a=r.text.slice(i.start,i.end);e.push({id:`c${n}`,qidOptions:i.qids.map(tH),range:{end:r.range.start+i.end,start:r.range.start+i.start},surface:a}),n+=1}await t.onProgress?.({coveredRangeEnd:r.range.end})}return e}async function zU(t,e){return nH(e)==="fetch"?await VU(e,t):await QU(e.command??"wikispine",XU(e),t,e)}async function QU(t,e,n,r){return await new Promise((i,a)=>{let o=KU(t,e,{stdio:["pipe","pipe","pipe"]}),s=Zv(r.onProgress,{onFailure:l=>{a(l),o.kill()}}),d=eT({onMatch:l=>{s.report({coveredRangeEnd:n.range.start+l.end})}}),u=[];o.stdout.setEncoding("utf8"),o.stdout.on("data",l=>{try{d.push(l)}catch(m){a(Yh(m)),o.kill()}}),o.stderr.on("data",l=>{u.push(l)}),o.on("error",l=>{a(new Error(Po(`Failed to start wikispine command: ${l.message}`)))}),o.on("close",l=>{if(l!==0){a(new Error(Po(`wikispine match failed with exit code ${l}: ${Buffer.concat(u).toString("utf8")}`)));return}(async()=>{try{let m=d.finish();await s.wait(),i(m)}catch(m){a(Yh(m))}})()}),o.stdin.end(n.text)})}function XU(t){let e=["match"];return t.dataDir!==void 0&&e.push("--data-dir",t.dataDir),t.includeDisambiguation===!1&&e.push("--exclude-disambiguation"),t.maxCandidatesPerSurface!==void 0&&e.push("--max-candidates-per-surface",String(t.maxCandidatesPerSurface)),e}async function VU(t,e){let n=rH(t.endpoint),r=await(t.fetch??fetch)(`${n}/match`,{body:JSON.stringify({options:{...t.includeDisambiguation===void 0?{}:{include_disambiguation:t.includeDisambiguation},...t.maxCandidatesPerSurface===void 0?{}:{max_candidates_per_surface:t.maxCandidatesPerSurface}},text:e.text}),headers:{accept:"application/x-ndjson","content-type":"application/json"},method:"POST"});if(!r.ok)throw new Error(Po(`WikiSpine fetch provider failed with HTTP ${r.status}: ${await r.text()}`));let i=Zv(t.onProgress),a=eT({onMatch:u=>{i.report({coveredRangeEnd:e.range.start+u.end})}});if(r.body===null){a.push(await r.text());let u=a.finish();return await i.wait(),u}let o=r.body.getReader(),s=new TextDecoder;for(;;){let{done:u,value:l}=await o.read();if(u)break;a.push(s.decode(l,{stream:!0})),i.throwIfFailed()}a.push(s.decode());let d=a.finish();return await i.wait(),d}function Zv(t,e){let n=[],r;function i(a){r===void 0&&(r=Yh(a),e?.onFailure?.(r))}return{report:a=>{if(!(t===void 0||r!==void 0))try{n.push(Promise.resolve(t(a)).catch(i))}catch(o){i(o)}},throwIfFailed:()=>{if(r!==void 0)throw r},wait:async()=>{if(await Promise.all(n),r!==void 0)throw r}}}function eT(t){let e=[],n="";function r(a){if(a.trim()==="")return;let o=YU(JSON.parse(a));o.type==="match"&&(e.push(o.match),t?.onMatch?.(o.match))}function i(a){return new Error(Po(`Invalid WikiSpine match response: ${a instanceof Error?a.message:String(a)}`))}return{finish:()=>{try{return r(n),n="",e}catch(a){throw i(a)}},push:a=>{try{n+=a;let o=n.split(/\r?\n/u);n=o.pop()??"";for(let s of o)r(s)}catch(o){throw i(o)}}}}function YU(t){if(typeof t!="object"||t===null)throw new Error("Expected wikispine event to be an object.");let e=t;if(e.type==="done")return{type:"done"};if(e.type==="match"&&ZU(e.match))return{match:e.match,type:"match"};throw new Error(`Unexpected wikispine event: ${JSON.stringify(t)}`)}function Yh(t){return t instanceof Error?t:new Error(String(t))}function ZU(t){if(typeof t!="object"||t===null)return!1;let e=t;return typeof e.end=="number"&&Array.isArray(e.qids)&&e.qids.every(eH)&&typeof e.start=="number"&&typeof e.surface_id=="number"}function eH(t){if(typeof t!="object"||t===null)return!1;let e=t;return(!("disambiguation"in e)||typeof e.disambiguation=="boolean")&&typeof e.qid=="string"&&/^Q[1-9]\d*$/u.test(e.qid)}function tH(t){return{isDisambiguation:t.disambiguation===!0,qid:t.qid}}function nH(t){return t.provider??"cli"}function rH(t){let e=(t??Zh).trim().replace(/\/+$/u,"");if(e==="")throw new Error(Po("WikiSpine fetch provider has no default endpoint."));return e}function Po(t){return`${t}
|
|
2246
|
+
WikiSpine setup guide: ${JU}`}var JU,Zh,tT=c(()=>{"use strict";JU="https://raw.githubusercontent.com/oomol-lab/wiki-graph/refs/heads/main/docs/wikispine-runtime.md",Zh="https://wikispi-service-cxbfjlteab.cn-hangzhou.fcapp.run"});function nT(t){let e=new Intl.Segmenter(void 0,{granularity:"word"}),n=new Intl.Segmenter(void 0,{granularity:"grapheme"}),r=[];for(let i of e.segment(t))"isWordLike"in i&&i.isWordLike&&r.push({end:i.index+i.segment.length,start:i.index});return r.length>0?r:[...n.segment(t)].filter(i=>i.segment.trim()!=="").map(i=>({end:i.index+i.segment.length,start:i.index}))}function tf(t){let e=nT(t.text);if(e.length===0)return{end:t.text.length,start:0};let n=iH(e,t.rangeStart),r=aH(e,t.rangeEnd),i=Math.max(0,Math.floor(t.words/2)),a=Math.max(0,n-i),o=Math.min(e.length-1,r+i);return{end:e[o]?.end??t.text.length,start:e[a]?.start??0}}function iH(t,e){let n=t.findIndex(r=>r.end>e);return n===-1?t.length-1:n}function aH(t,e){for(let n=t.length-1;n>=0;n-=1){let r=t[n];if(r!==void 0&&r.start<e)return n}return 0}var nf=c(()=>{"use strict"});function Uc(t){let e=sH(dH(t.candidates??[])),n=[],r=[],i=0;for(let a of e){let o=oH(a);r.length>0&&i+o>t.optionBudget&&(n.push(rT(t.text,r,t)),r=[],i=0),r.push(a),i+=o}return r.length>0&&n.push(rT(t.text,r,t)),n}function oH(t){return t.candidates.reduce((e,n)=>e+Gh(n),0)}function rT(t,e,n){let r=Math.min(...e.map(o=>o.range.start)),i=Math.max(...e.map(o=>o.range.end)),a=tf({rangeEnd:i,rangeStart:r,text:t,words:n.contextWords});return{baseOffset:a.start,candidates:e.flatMap(o=>o.candidates),groups:e.map((o,s)=>({candidateIds:o.candidates.map(d=>d.id),id:`g${s+1}`,range:o.range})),text:t.slice(a.start,a.end)}}function sH(t){let e=[],n;for(let r of t){if(n===void 0||r.range.start>=n.range.end){n={candidates:[r],range:r.range},e.push(n);continue}n={candidates:[...n.candidates,r],range:{end:Math.max(n.range.end,r.range.end),start:Math.min(n.range.start,r.range.start)}},e[e.length-1]=n}return e}function dH(t){return t.filter(e=>e.range.start<e.range.end).sort(cH)}function cH(t,e){return t.range.start-e.range.start||e.range.end-t.range.end||t.id.localeCompare(e.id)}var iT=c(()=>{"use strict";nf();To()});var Hn=c(()=>{"use strict";$h();$v();To();Jv();Vv();Yv();Xh();tT();iT();nf()});var Hc=c(()=>{"use strict"});async function oT(t){if(t.candidates.length===0)return[];let e=Vh({candidates:t.candidates,percentile:.1,text:t.text});await t.progressTracker?.updatePhase({done:0,phase:"screening",phaseDetail:`${e.suspiciousSurfaces.length} high-frequency surfaces`,total:1,unit:"window"}),await t.progressTracker?.throwIfStopped();let n=await zh({policyPrompt:t.policyPrompt,request:t.request,suspiciousSurfaces:e.suspiciousSurfaces});await t.progressTracker?.throwIfStopped();let r=new Set(n.protectedSurfaces.map(a=>a.text)),i=new Set(e.candidates.map(aT));return await t.progressTracker?.updatePhase({done:1,phase:"screening",phaseDetail:`${r.size} protected surfaces`,total:1,unit:"window"}),e.suppressedCandidates.filter(a=>i.has(aT(a))||r.has(a.surface))}function sT(t){return new Set(t.flatMap(e=>e.qidOptions.map(n=>n.qid))).size}function aT(t){return`${t.range.start}\0${t.range.end}\0${t.surface}`}var dT=c(()=>{"use strict";Hn();Hc()});async function cT(t,e){let n=t.getSerialFragments(e);return await Promise.all((await n.listFragmentIds()).map(async r=>await n.getFragment(r)))}function uT(t){return t.flatMap(e=>e.sentences.map(n=>n.text)).join(" ")}function lT(t){let e=[],n=0;for(let r of t)for(let i=0;i<r.sentences.length;i+=1){let a=r.sentences[i];e.push({id:`${r.serialId}:${r.fragmentId+i}`,range:{end:n+a.text.length,start:n},text:a.text}),n+=a.text.length+1}return e}var rf=c(()=>{"use strict"});function hT(t){let e=new Map(t.map(d=>[d.id,d])),n=new Map(t.map(d=>[d.id,new Set])),r=new Map(t.map(d=>[d.id,0])),i=new Set,a=new Map,o=new Map,s={candidatePageCount:0,qidAppearanceCount:0};return{accept(d){i.add(d.candidateId),Ao(o,d.surface).recallCount+=1,a.set(Lo(d.surface,d.qid),(a.get(Lo(d.surface,d.qid))??0)+1)},close(d,u){i.add(d);let l=e.get(d);l!==void 0&&(u==="skip_this_time"||u==="never_recall")&&(Ao(o,l.surface).rejectCount+=1)},continue(d){let u=e.get(d);u!==void 0&&(Ao(o,u.surface).continueCount+=1)},getStats(){return{...s}},nextPage(d){let u=[],l=d===void 0?t.map(m=>m.id):[...d];for(let m of l){if(i.has(m))continue;let p=e.get(m),h=n.get(m);if(p===void 0||h===void 0)continue;let y=gH(p,a),f=mi(y),I=r.get(m)??0,v=hH(Ao(o,p.surface),I),x=f.filter(_=>!h.has(_)).slice(0,v);if(x.length===0){i.add(m);continue}for(let _ of x)h.add(_);let J=f.some(_=>!h.has(_)),G=Gc(y,new Set(x));J||i.add(m),Ao(o,p.surface).seenCount+=1,r.set(m,I+1),s.candidatePageCount+=1,s.qidAppearanceCount+=x.length,u.push({...G,...J?{hasMoreOptions:!0}:{}})}return u}}}function Ao(t,e){let n=t.get(e);if(n!==void 0)return n;let r={continueCount:0,recallCount:0,rejectCount:0,seenCount:0};return t.set(e,r),r}function hH(t,e){let n=fH(t)?pH:mH;return n[Math.min(e,n.length-1)]}function fH(t){return t.seenCount<mT?!1:t.recallCount>=2&&t.continueCount===0||t.rejectCount>=2&&t.recallCount===0||t.continueCount>=mT}function fT(t,e){return`efficiency qid/mention=${e===0?"n/a":(t.qidAppearanceCount/e).toFixed(1)} qids=${t.qidAppearanceCount} mentions=${e} pages=${t.candidatePageCount}`}function gH(t,e){return{...t,qidOptions:[...t.qidOptions].sort((n,r)=>pT(t.surface,r,e)-pT(t.surface,n,e))}}function pT(t,e,n){let r=n.get(Lo(t,e.qid))??0,i=e.disambiguation?.linkedQids.reduce((o,s)=>o+(n.get(Lo(t,s.qid))??0),0)??0,a=e.disambiguation?.profile?.meanings.reduce((o,s)=>o+(n.get(Lo(t,s.qid))??0),0)??0;return r+i+a}function Lo(t,e){return`${t}\0${e}`}var mH,pH,mT,gT=c(()=>{"use strict";Hn();mH=[5,10,20,35],pH=[3,5,10,20,35],mT=3});async function qc(t,e,n){let r=t.lazy;if(r!==void 0)return await Promise.all(e.map(async a=>await r(async o=>await n(a,o))));let i=[];for(let a of e)i.push(await n(a,t));return i}var af=c(()=>{"use strict"});async function $c(t){let e=[],n=hT(t.candidates),r=0,i=0,a=n.nextPage();for(;a.length>0;){let o=Uc({candidates:a,contextWords:220,optionBudget:50,text:t.text});if(i+=o.length,o.length===0)break;await t.progressTracker?.updatePhase({done:r,phase:"grounding",total:i,unit:"window"});let s=await qc(t.request,o,async(u,l)=>{try{return await t.progressTracker?.throwIfStopped(),await Bc({candidates:u.candidates,policyPrompt:t.policyPrompt,request:l,window:u})}finally{r+=1,await t.progressTracker?.updatePhase({done:r,phase:"grounding",total:i,unit:"window"})}}),d=new Set;for(let u of s){for(let l of u.mentions)e.push(l),n.accept(l);for(let l of u.policyUpdates)n.close(l.candidateId,l.decision);for(let l of u.continuations)for(let m of l.candidateIds)d.add(m),n.continue(m)}a=n.nextPage(d)}return await t.progressTracker?.updatePhase({done:r,phase:"grounding",phaseDetail:fT(n.getStats(),e.length),total:i,unit:"window"}),e}var of=c(()=>{"use strict";Hn();Hc();gT();af()});async function yT(t){let e=[],n=SH(t.fragments),r=await $c({candidates:t.candidates,policyPrompt:t.policyPrompt,...t.progressTracker===void 0?{}:{progressTracker:t.progressTracker},request:t.request,text:t.text}),i=1;for(let a of r){let o=wH(n,a.range.start);e.push(bH(t.chapterId,a,o,i)),i+=1}return e}function bH(t,e,n,r){return{chapterId:t,...e.confidence===void 0?{}:{confidence:e.confidence},id:`m${t}-${r}`,...e.note===void 0?{}:{note:e.note},qid:e.qid,rangeEnd:n.rangeStart+e.surface.length,rangeStart:n.rangeStart,sentenceIndex:n.sentenceIndex,surface:e.surface}}function SH(t){let e=[],n=0;for(let r of t)for(let i=0;i<r.sentences.length;i+=1){let a=r.sentences[i].text.length;e.push({absoluteStart:n,length:a,sentenceIndex:r.fragmentId+i}),n+=a+1}return e}function wH(t,e){for(let n of t){let r=n.absoluteStart+n.length;if(e>=n.absoluteStart&&e<r)return{rangeStart:e-n.absoluteStart,sentenceIndex:n.sentenceIndex}}throw new Error(`Mention offset ${e} is outside chapter text.`)}var bT=c(()=>{"use strict";of()});function ST(t){return{language:Vn(t.resolverOptions?.language)??"zh",prompt:t.policyPrompt}}var wT=c(()=>{"use strict";nt()});function Kc(t){return async e=>{await t.throwIfStopped();let n=EH(e);await t.updatePhase({done:e.done,...n.detail===void 0?{}:{phaseDetail:n.detail},phase:"enrichment",total:e.total,unit:n.unit})}}function EH(t){switch(t.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 sf=c(()=>{"use strict"});var ET=c(()=>{"use strict";Lp();zr();Nd()});import{z as j}from"zod";async function df(t){if(t.window.mentions.length<2)return[];try{return await Z({messages:PH(t),parse:e=>_H(t,e),request:t.request,responseIntentClassifierPrompt:ae,schema:RH,...t.maxRetries===void 0?{}:{maxRetries:t.maxRetries}})}catch{return[]}}function _H(t,e){let n=new Map(t.window.mentions.map(m=>[m.id,m])),r=new Kt(t.sentences.map((m,p)=>({sentenceId:[t.chapterId,(t.fragmentId??0)+p],text:m.text,wordsCount:m.wordsCount}))),i=RT(t.sentences),a=r.sentences.flatMap((m,p)=>{let h=i[p];return h===void 0||!kT(h,t.window.range)?[]:[{id:`S${p+1}`,sentenceId:m.sentenceId,text:m.projectedText}]}),o=[],s=[],d=new Set,u=a.map(m=>m.sentenceId),l=a.map(m=>m.text);for(let[m,p]of e.relations.entries()){let h=`relations[${m}]`,y=n.get(p.sourceMentionId),f=n.get(p.targetMentionId);if(y===void 0){o.push(`${h}.sourceMentionId references an unknown mention.`);continue}if(f===void 0){o.push(`${h}.targetMentionId references an unknown mention.`);continue}if(y.id===f.id){o.push(`${h} links a mention to itself. Use two different tagged mention IDs, or remove the relation if the other endpoint is not tagged.`);continue}let I=jH(p.predicate);if(I===""){o.push(`${h}.predicate normalizes to an empty label.`);continue}if(I==="mentions"){o.push(`${h}.predicate must be semantic; "mentions" is not a relation predicate.`);continue}let[v,x]=DH({evidence:MH(p.evidence),sentences:a}),[J,G]=!Array.isArray(p.evidence)&&v===void 0&&x===void 0?TH.resolve(p.evidence,u,l):[void 0,void 0],_=v??J,Rt=x===void 0?G:WH(x);if(_===void 0){o.push(`${h}.evidence could not be resolved: ${Rt?.message??"unknown failure"}`);continue}let $o=CH(_.sentenceIds);if($o.length===0){o.push(`${h}.evidence resolved to no sentences.`);continue}if($o.some(([du,RC])=>du!==t.chapterId||i[RC-(t.fragmentId??0)]===void 0)){o.push(`${h}.evidence resolved outside this fragment.`);continue}let Hf=`${y.id}\0${f.id}\0${I}\0${$o.map(du=>du.join(":")).join(",")}`;d.has(Hf)||(d.add(Hf),s.push({...p.confidence===void 0?{}:{confidence:p.confidence},evidenceSentenceIds:$o,predicate:I,sourceMentionId:y.id,targetMentionId:f.id}))}if(o.length>0)throw new M(o);return s}function CH(t){let e=new Set,n=[];for(let r of t){let i=r.join(":");e.has(i)||(e.add(i),n.push(r))}return n.sort(([r,i],[a,o])=>r-a||i-o)}function PH(t){return[{content:AH(),role:"system"},{content:LH(t),role:"user"}]}function AH(){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.",Ln,"- 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:",vH.join(", ")].join(`
|
|
2247
|
+
`)}function LH(t){return["Source sentences with mention tags:",OH(t),"","Return this JSON shape:",JSON.stringify({relations:[{confidence:.86,evidence:_d,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(`
|
|
2248
|
+
`)}function OH(t){let e=RT(t.sentences),n=t.sentences.flatMap((r,i)=>{let a=e[i];return a===void 0||!kT(a,t.window.range)?[]:[`S${i+1}: ${$t(NH({mentions:t.window.mentions,sentence:r,sentenceOffset:a}))}`]});return n.length===0?FH(t.window):n.join(`
|
|
2249
|
+
`)}function NH(t){let e=t.mentions.filter(i=>i.range.start>=t.sentenceOffset.start&&i.range.end<=t.sentenceOffset.end).sort((i,a)=>i.range.start-a.range.start),n=[],r=t.sentenceOffset.start;for(let i of e)n.push(qn(t.sentence.text.slice(r-t.sentenceOffset.start,i.range.start-t.sentenceOffset.start))),n.push(`<mention id="${Jc(i.id)}" qid="${Jc(i.qid??"")}">${qn(t.sentence.text.slice(i.range.start-t.sentenceOffset.start,i.range.end-t.sentenceOffset.start))}</mention>`),r=i.range.end;return n.push(qn(t.sentence.text.slice(r-t.sentenceOffset.start))),n.join("")}function kT(t,e){return t.start<e.end&&e.start<t.end}function DH(t){return t.evidence===void 0?[void 0,void 0]:Jr({evidence:t.evidence,sentences:t.sentences})}function MH(t){return Array.isArray(t)?t.map(xT):typeof t.quote=="string"||typeof t.sentence_id=="string"?xT(t):void 0}function xT(t){return{...typeof t.quote=="string"?{quote:t.quote}:{},...typeof t.sentence_id=="string"?{sentence_id:t.sentence_id}:{}}}function WH(t){return{candidates:t.candidates.map(e=>({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})),code:t.code,fieldName:"evidence",message:t.message}}function FH(t){let e=[...t.mentions].sort((i,a)=>i.range.start-a.range.start),n=[],r=t.range.start;for(let i of e)n.push(qn(t.text.slice(r-t.range.start,i.range.start-t.range.start))),n.push(`<mention id="${Jc(i.id)}" qid="${Jc(i.qid??"")}">${qn(t.text.slice(i.range.start-t.range.start,i.range.end-t.range.start))}</mention>`),r=i.range.end;return n.push(qn(t.text.slice(r-t.range.start))),n.join("")}function RT(t){let e=[],n=0;for(let r of t){let i=n,a=i+r.text.length;e.push({end:a,start:i}),n=a+1}return e}function jH(t){return t.trim().replace(/[\s-]+/gu,"_").replace(/_+/gu,"_").replace(/^_+|_+$/gu,"").toLowerCase()}function Jc(t){return qn(t).replaceAll('"',""")}function qn(t){return t.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">")}var IT,IH,xH,kH,RH,vH,TH,vT=c(()=>{"use strict";ve();zr();ET();IT=j.object({head:j.string().optional(),mode:j.enum(["full","head_tail"]).optional(),tail:j.string().optional(),text:j.string().optional()}).strict(),IH=j.object({quote:j.string().optional(),sentence_id:j.string().optional()}).strict(),xH=j.union([j.object({end_anchor:j.union([j.string(),IT]).optional(),quote:j.string().optional(),sentence_id:j.string().optional(),start_anchor:j.union([j.string(),IT]).optional()}).strict(),j.array(IH)]),kH=j.object({confidence:j.number().min(0).max(1).optional(),evidence:xH,predicate:j.string().min(1).max(64),sourceMentionId:j.string().min(1),targetMentionId:j.string().min(1)}).strict(),RH=j.object({relations:j.array(kH)}).strict(),vH=["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"],TH=new qt});function cf(t){BH(t);let e=UH(t.mentions);if(e.length===0||t.text.length===0)return[];let n=t.windowLength-t.maxEvidenceDistance,r=[];for(let i=0;i<t.text.length;i=GH(i,n,t.text.length)){let a=Math.min(i+t.windowLength,t.text.length),o=e.filter(s=>s.range.start>=i&&s.range.end<=a);if(o.length>=2&&r.push({baseOffset:i,mentions:o,range:{end:a,start:i},text:t.text.slice(i,a)}),a>=t.text.length)break}return r}function GH(t,e,n){return Math.min(t+e,n)}function BH(t){if(!Number.isInteger(t.windowLength)||t.windowLength<=0)throw new Error("windowLength must be a positive integer.");if(!Number.isInteger(t.maxEvidenceDistance)||t.maxEvidenceDistance<=0)throw new Error("maxEvidenceDistance must be a positive integer.");if(t.windowLength<=t.maxEvidenceDistance)throw new Error("windowLength must be greater than maxEvidenceDistance so eligible mention pairs are guaranteed to share at least one evidence window.")}function UH(t){return t.filter(e=>e.range.start<e.range.end).sort(HH)}function HH(t,e){return t.range.start-e.range.start||t.range.end-e.range.end||t.id.localeCompare(e.id)}var TT=c(()=>{"use strict"});var _T=c(()=>{"use strict";vT();TT()});async function CT(t){let e=KH(t.fragments,t.mentions),n=0;return await t.progressTracker?.updatePhase({done:0,phase:"relation-discovery",total:e.length,unit:"window"}),(await qc(t.request,e,async(i,a)=>{try{return await t.progressTracker?.throwIfStopped(),await df({chapterId:i.fragment.serialId,fragmentId:i.fragment.fragmentId,request:a,sentences:i.fragment.sentences,window:i.window})}finally{n+=1,await t.progressTracker?.updatePhase({done:n,phase:"relation-discovery",total:e.length,unit:"window"})}})).flat().map((i,a)=>({...i.confidence===void 0?{}:{confidence:i.confidence},evidenceSentenceIds:i.evidenceSentenceIds,id:`l${t.fragments[0]?.serialId??0}-${a+1}`,predicate:i.predicate,sourceMentionId:i.sourceMentionId,targetMentionId:i.targetMentionId}))}function KH(t,e){return t.flatMap(n=>{let r=n.fragmentId,i=r+n.sentences.length-1,a=JH(n.sentences,e.filter(s=>s.sentenceIndex!==void 0&&s.sentenceIndex>=r&&s.sentenceIndex<=i),r);return cf({maxEvidenceDistance:700,mentions:a,text:QH(n.sentences),windowLength:1800}).map(s=>({fragment:n,window:s}))})}function JH(t,e,n){let r=zH(t);return e.flatMap(i=>{if(i.sentenceIndex===void 0)return[];let a=r[i.sentenceIndex-n];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 zH(t){let e=[],n=0;for(let r of t){let i=n,a=i+r.text.length;e.push({end:a,start:i}),n=a+1}return e}function QH(t){return t.map(e=>e.text).join(" ")}var PT=c(()=>{"use strict";_T();Hc();af()});async function Oo(t,e){let n=await re(t,e);if(n.stage==="planned")throw new Error(`Chapter ${e} is planned. Set source before generating Knowledge Graph.`);return{details:n,fragments:await cT(t,e)}}var uf=c(()=>{"use strict";Se();rf()});async function zc(t,e,n){if(e.details.chapterId!==t)throw new Error(`Knowledge Graph snapshot belongs to chapter ${e.details.chapterId}, not chapter ${t}.`);if(e.details.stage==="planned")throw new Error(`Chapter ${t} is planned. Set source before generating Knowledge Graph.`);let r=e.fragments,i=uT(r),a=lT(r),o=Mn(n.policyPrompt);await n.progressTracker?.updatePhase({done:0,phase:"matching",phaseDetail:"text",total:i.length,unit:"char"});let s=await ef({includeDisambiguation:!0,onProgress:async h=>{await n.progressTracker?.updatePhase({done:h.coveredRangeEnd,force:!1,phase:"matching",phaseDetail:"text",total:i.length,unit:"char"})},...n.wikispine??{},sentences:a});await n.progressTracker?.throwIfStopped(),await n.progressTracker?.updatePhase({done:i.length,phase:"matching",phaseDetail:"text",total:i.length,unit:"char"});let d=await oT({candidates:s,policyPrompt:o,...n.progressTracker===void 0?{}:{progressTracker:n.progressTracker},request:n.request,text:i});await n.progressTracker?.throwIfStopped();let u=sT(d);await n.progressTracker?.updatePhase({done:0,phase:"enrichment",total:u,unit:"qid"});let l=await Jh(d,{...n.progressTracker===void 0?{}:{progress:Kc(n.progressTracker)},...n.resolverOptions===void 0?{}:{resolverOptions:n.resolverOptions}});await n.progressTracker?.throwIfStopped(),await n.progressTracker?.updatePhase({done:u,phase:"enrichment",total:u,unit:"qid"});let m=await yT({candidates:l,chapterId:t,fragments:r,policyPrompt:o,...n.progressTracker===void 0?{}:{progressTracker:n.progressTracker},request:n.request,text:i});await n.progressTracker?.throwIfStopped();let p=await CT({fragments:r,mentions:m,...n.progressTracker===void 0?{}:{progressTracker:n.progressTracker},request:n.request});return await n.progressTracker?.throwIfStopped(),await Fc(t,{mentionLinks:p,mentions:m,parameter:ST({policyPrompt:o,...n.resolverOptions===void 0?{}:{resolverOptions:n.resolverOptions}}),workspacePath:n.workspacePath})}var AT=c(()=>{"use strict";Hn();zt();jh();dT();rf();bT();wT();sf();PT();uf()});var LT=c(()=>{"use strict"});var OT=c(()=>{"use strict";jh();jv();AT();of();sf();uf();LT()});var NT=c(()=>{"use strict";OT()});var No=c(()=>{"use strict";Se()});var lf=c(()=>{"use strict";zt()});var mf=c(()=>{"use strict";nt();zt();ri();No();lf()});var WT=c(()=>{"use strict";zt();No();mf()});function Qc(t){return{children:t.children.map(Qc),...t.key===void 0?{}:{key:t.key},...t.serialId===void 0?{}:{serialId:t.serialId},title:t.title}}var pf=c(()=>{"use strict";st()});async function FT(t){let e=await t.readToc(),n=e?.items.map(Qc)??[];return YH(n),e===void 0?{items:[],version:ct}:{items:n,version:e.version}}function YH(t){let e=new Set,n=r=>{for(let i of r){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(e.has(i.key))throw new Error(`Duplicate chapter key: ${i.key}.`);e.add(i.key),n(i.children)}};n(t)}var jT=c(()=>{"use strict";st();Ui();cn();pf()});async function hi(t,e){let n=await FT(t),r=uy(n.items,e);if(r===void 0)throw new Error(`Chapter path ${e} does not exist.`);return r}var GT=c(()=>{"use strict";ri();st();No();jT();pf()});import{z as $n}from"zod";var BT,sfe,UT=c(()=>{"use strict";BT=$n.object({children:$n.lazy(()=>$n.array(BT)),uri:$n.string().min(1),title:$n.string().nullable().optional()}).strict(),sfe=$n.object({chapters:$n.array(BT)}).strict()});var HT=c(()=>{"use strict";Se()});var qT=c(()=>{"use strict";WT();No();mf();GT();lf();UT();HT()});var $T=c(()=>{"use strict";Sa()});var Vt=c(()=>{"use strict";yR();Kr();KR();Dv();NT();Kr();gh();qT();$T();Zd()});import{randomUUID as fq}from"crypto";import{join as gq}from"path";async function KT(t="global"){let e=await hf(),n=`${process.pid}-${fq()}`,r=Date.now(),i=!1;try{await e.transaction(async()=>{await Eq(e),await e.run(`
|
|
2034
2250
|
INSERT OR IGNORE INTO gc_locks (
|
|
2035
2251
|
scope, owner_id, owner_pid, heartbeat_at, created_at
|
|
2036
2252
|
) VALUES (?, ?, ?, ?, ?)
|
|
2037
|
-
`,[t,
|
|
2253
|
+
`,[t,n,process.pid,r,r]),i=await e.queryOne("SELECT owner_id FROM gc_locks WHERE scope = ?",[t],o=>w(o,"owner_id"))===n})}finally{await e.close()}if(!i)return;let a=setInterval(()=>{wq(t,n).catch(()=>{})},Sq);return async()=>{clearInterval(a);let o=await hf();try{await o.run("DELETE FROM gc_locks WHERE scope = ? AND owner_id = ?",[t,n])}finally{await o.close()}}}async function wq(t,e){let n=await hf();try{await n.run(`
|
|
2038
2254
|
UPDATE gc_locks
|
|
2039
2255
|
SET heartbeat_at = ?, owner_pid = ?
|
|
2040
2256
|
WHERE scope = ? AND owner_id = ?
|
|
2041
|
-
`,[Date.now(),process.pid,t,e])}finally{await
|
|
2257
|
+
`,[Date.now(),process.pid,t,e])}finally{await n.close()}}async function Eq(t){let e=await t.queryAll("SELECT * FROM gc_locks",void 0,kq);for(let{lock:n,scope:r}of e.filter(({lock:i})=>Iq(i)))await t.run(`
|
|
2042
2258
|
DELETE FROM gc_locks
|
|
2043
2259
|
WHERE scope = ?
|
|
2044
2260
|
AND owner_id = ?
|
|
2045
2261
|
AND heartbeat_at = ?
|
|
2046
|
-
`,[n
|
|
2262
|
+
`,[r,n.ownerId,n.heartbeatAt])}function Iq(t){return Date.now()-t.heartbeatAt<bq?!1:!Rq(t.ownerPid)}async function hf(){return await D(gq(Or(),"gc.sqlite"),yq)}function xq(t){return{createdAt:g(t,"created_at"),heartbeatAt:g(t,"heartbeat_at"),ownerId:w(t,"owner_id"),ownerPid:g(t,"owner_pid")}}function kq(t){return{lock:xq(t),scope:w(t,"scope")}}function Rq(t){try{return process.kill(t,0),!0}catch(e){return!(E(e)&&e.code==="ESRCH")}}var yq,bq,Sq,JT=c(()=>{"use strict";yt();k();N();T();yq=`
|
|
2047
2263
|
CREATE TABLE IF NOT EXISTS gc_locks (
|
|
2048
2264
|
scope TEXT PRIMARY KEY,
|
|
2049
2265
|
owner_id TEXT NOT NULL,
|
|
@@ -2051,15 +2267,15 @@ CREATE TABLE IF NOT EXISTS gc_locks (
|
|
|
2051
2267
|
heartbeat_at INTEGER NOT NULL,
|
|
2052
2268
|
created_at INTEGER NOT NULL
|
|
2053
2269
|
);
|
|
2054
|
-
`,
|
|
2055
|
-
`,"utf8")}function
|
|
2270
|
+
`,bq=6e4,Sq=2e4});import{readdir as zT,rm as vq,stat as Tq}from"fs/promises";import{join as QT}from"path";async function XT(t){let e=im(),n=0,r=0,i=0;for(let a of await Cq(e)){n+=1;let o=await Tq(a).catch(d=>{if(!(E(d)&&d.code==="ENOENT"))throw d});if(o===void 0||!t.force&&t.now-o.mtimeMs<_q)continue;let s=await we(a);t.dryRun||await vq(a,{force:!0,recursive:!0}),r+=1,i+=s}return{freedBytes:i,removed:r,scanned:n}}async function Cq(t){try{let e=await zT(t,{withFileTypes:!0}),n=[];for(let r of e){if(!r.isDirectory())continue;let i=QT(t,r.name),a=await zT(i,{withFileTypes:!0});for(let o of a)n.push(QT(i,o.name))}return n}catch(e){if(E(e)&&e.code==="ENOENT")return[];throw e}}var _q,VT=c(()=>{"use strict";yt();T();ko();_q=3600*1e3});import{mkdir as Pq,readFile as Aq,writeFile as Lq}from"fs/promises";import{join as Oq}from"path";async function YT(t={}){let e=Date.now(),n=Or(),r=await KT();if(r===void 0)return{finishedAt:Date.now(),freedBytes:0,jobs:[],removed:0,scanned:0,skipped:!0,startedAt:e};try{if(t.opportunistic===!0&&!await Wq(n,e))return{finishedAt:Date.now(),freedBytes:0,jobs:[],removed:0,scanned:0,skipped:!0,startedAt:e};let i={dryRun:t.dryRun===!0,force:t.force===!0,now:e,stateDirectoryPath:n},a=[];for(let s of Nq)a.push(await Mq(s,i));let o={finishedAt:Date.now(),freedBytes:ff(a,"freedBytes"),jobs:a,removed:ff(a,"removed"),scanned:ff(a,"scanned"),skipped:!1,startedAt:e};return await Fq(n,o.finishedAt).catch(()=>{}),o}finally{await r()}}async function Mq(t,e){try{return{name:t.name,...await t.run(e)}}catch(n){return{error:ue(n),freedBytes:0,name:t.name,removed:0,scanned:0}}}function ff(t,e){return t.reduce((n,r)=>{let i=r[e];return n+(typeof i=="number"?i:0)},0)}async function Wq(t,e){let n=Number((await Aq(ZT(t),"utf8").catch(()=>"")).trim());return!Number.isFinite(n)||e-n>=Dq}async function Fq(t,e){await Pq(t,{recursive:!0}),await Lq(ZT(t),`${e}
|
|
2271
|
+
`,"utf8")}function ZT(t){return Oq(t,"gc.last-run")}var Nq,Dq,e_=c(()=>{"use strict";yt();Kr();Vt();Ye();Wa();Vc();T();JT();VT();Nq=[{name:"wikg-coordinator",run:Xc},{name:"search-cache",run:Vs},{name:"library-index",run:t_},{name:"wikipage-cache",run:Fm},{name:"build-queue",run:hc},{name:"tmp",run:XT}],Dq=600*1e3});var n_={};qf(n_,{tryRunWikiGraphGc:()=>YT});var r_=c(()=>{"use strict";e_()});import{mkdir as Gq,rm as Bq,writeFile as Uq}from"fs/promises";import{dirname as Hq,isAbsolute as qq,relative as $q,resolve as gf}from"path";var Do,yf=c(()=>{"use strict";yt();Bt();ht();We();mm();zs();nw();Dr();la();Do=class t{#e;#t;#n=wn();#r;#a=new Set;#i=new Set;#o=!1;constructor(e,n){if(n!==Vl)throw new Error("Use WikgCoordinator.withArchiveSession().");this.#t=gf(e),this.#e=Ar(this.#t),this.#r=setInterval(()=>{ZS({archiveKey:this.#e,ownerId:this.#n}).catch(()=>{})},2e4)}static async open(e){let n=new t(e,Vl);try{return await YS({archiveKey:n.#e,ownerId:n.#n}),n}catch(r){throw clearInterval(n.#r),r}}get archiveKey(){return this.#e}get archivePath(){return this.#t}get ownerId(){return this.#n}observeDirtyEntry(e){this.#a.add(e)}modifyEntry(e){this.#i.add(e)}createFileStore(e={}){return new Mr(this.#t,{...e,session:this})}async materializeReadWorkspace(e,n){let r=e===void 0,i=r?await gt("archive-open"):gf(e);r||await BS(i);let a=this.createFileStore({readonlyDatabase:!0}),o=await Gs(await Fs(this.#t),{archiveKey:this.#e,prefix:""});try{for(let s of o){let d=await a.readFile(s);if(d===void 0)continue;let u=gf(i,s),l=$q(i,u);if(l.startsWith("..")||qq(l))throw new Error(`Archive entry escapes read workspace: ${s}`);await Gq(Hq(u),{recursive:!0}),await Uq(u,d)}return await n(i)}finally{await a.close(),r&&await Bq(i,{force:!0,recursive:!0})}}async close(){if(!this.#o){this.#o=!0,clearInterval(this.#r);try{await this.#s(),await tw(this.#e)}finally{await ew({archiveKey:this.#e,ownerId:this.#n})}await Promise.resolve().then(()=>(r_(),n_)).then(async({tryRunWikiGraphGc:e})=>{await e({opportunistic:!0})}).catch(()=>{})}}async#s(){let e=new Set([...this.#a,...this.#i]);e.size!==0&&await Js(this.#e,e)}}});import{rm as i_}from"fs/promises";import{resolve as Mo}from"path";var fi,a_=c(()=>{"use strict";yt();Bt();mm();yf();fi=class{createFileStore(e,n={}){return new Mr(Mo(e),n)}async withArchiveSession(e,n){let r=await Do.open(Mo(e));try{return await n(r)}finally{await r.close()}}async withReadWorkspace(e,n,r={}){let i=r.documentDirPath===void 0?await gt("archive-open"):Mo(r.documentDirPath);try{return await Tr(Mo(e),i),await n(i)}finally{r.documentDirPath===void 0&&await i_(i,{force:!0,recursive:!0})}}async withWriteWorkspace(e,n){let r=await gt("archive-write");try{return await Tr(Mo(e),r),await n(r)}finally{await i_(r,{force:!0,recursive:!0})}}}});import{readdir as Jq,rm as s_}from"fs/promises";import{basename as zq,dirname as Qq,join as o_}from"path";async function Xc(t){let e=await Xq(),n=0,r=0;for(let o of e){let s=await Vq(o,t);s.removed&&(n+=1,r+=s.freedBytes)}let i=await e$(t),a=t.dryRun?{freedBytes:0,removed:0,scanned:0}:await pc(qs());return{freedBytes:r+i.freedBytes+a.freedBytes,removed:n+i.removed+a.removed,scanned:e.length+i.scanned+a.scanned}}async function Xq(){return await P(async t=>(await ne(t),await t.queryAll(`
|
|
2056
2272
|
SELECT overlay.*
|
|
2057
2273
|
FROM entry_overlays AS overlay
|
|
2058
|
-
WHERE overlay.entry_path IN (?, ?)
|
|
2274
|
+
WHERE overlay.entry_path IN (?, ?, ?)
|
|
2059
2275
|
AND overlay.kind = 'file'
|
|
2060
2276
|
AND overlay.workspace_path IS NOT NULL
|
|
2061
2277
|
ORDER BY overlay.updated_at ASC
|
|
2062
|
-
`,[
|
|
2278
|
+
`,[Ee,A,pt],Ut)))}async function Vq(t,e){let n=await Fe(t.archiveKey,t.entryPath,"write");try{let r=await Fe(t.archiveKey,t.entryPath,"state");try{let i=await St(t.archiveKey,t.entryPath);if(i?.workspacePath===void 0||i.workspacePath!==t.workspacePath||!await Yq(i,e))return{freedBytes:0,removed:!1};let a=await we(i.workspacePath);return e.dryRun||(await Nr(i.archiveKey,i.entryPath),await s_(i.workspacePath,{force:!0}),a+=await mc(Qq(i.workspacePath))),{freedBytes:a,removed:!0}}finally{await r()}}finally{await n()}}async function Yq(t,e){return await Bs(t.archiveKey,t.entryPath)||t.workspacePath===void 0?!1:await Zq(t)?!0:!(!e.force&&e.now-t.updatedAt<36e5||d_(t.entryPath)&&!e.force)}async function Zq(t){return t.workspacePath===void 0||!await Gt(t.workspacePath)||!await Gt(t.archivePath)?!0:d_(t.entryPath)?await HS(t)!=="current":!1}function d_(t){return t===A||t===pt}async function e$(t){let e=qs(),n;try{n=await Jq(e,{withFileTypes:!0})}catch(o){if(E(o)&&o.code==="ENOENT")return{freedBytes:0,removed:0,scanned:0};throw o}let r=0,i=0,a=0;for(let o of n){if(!o.isDirectory())continue;let s=o.name;if(await Us(s))continue;let d=new Set((await kn(s)).map(u=>u.workspacePath).filter(u=>u!==void 0));for(let u of await um(o_(e,s))){if(a+=1,_h(zq(u))||d.has(u))continue;let l=await we(u);t.dryRun||await s_(u,{force:!0}),r+=l,i+=1}if(!t.dryRun){let u=await Ch(o_(e,s));r+=u.freedBytes,i+=u.removed,a+=u.scanned}}return{freedBytes:r,removed:i,scanned:a}}var c_=c(()=>{"use strict";ko();T();cm();ht();We();da();Dr();lm();bt();la()});var u_=c(()=>{"use strict";a_();yf();zs();c_()});var bf=c(()=>{"use strict";u_()});import{resolve as t$}from"path";var L,l_=c(()=>{"use strict";N();bf();Kr();Zd();L=class{#e;#t=new fi;constructor(e){this.#e=t$(e)}async read(e,n={}){return await this.readDocument(async(r,i)=>await e(new Dn(r,i)),n)}async readDocument(e,n={}){return await this.#t.withArchiveSession(this.#e,async r=>{if(n.documentDirPath!==void 0)return await r.materializeReadWorkspace(n.documentDirPath,async a=>{let o=await he.open(a);try{return await e(o,a)}finally{await o.release()}});let i=await he.open(this.#e,{fileStore:r.createFileStore({readonlyDatabase:!0,...n.searchIndexWritebackPolicy===void 0?{}:{searchIndexWritebackPolicy:n.searchIndexWritebackPolicy}})});try{return await e(i,this.#e)}finally{await i.release()}})}async write(e,n={}){return await this.#t.withArchiveSession(this.#e,async r=>{let i=await he.open(this.#e,{fileStore:r.createFileStore({...n.searchIndexWritebackPolicy===void 0?{}:{searchIndexWritebackPolicy:n.searchIndexWritebackPolicy}})});try{return await e(i)}finally{try{await i.release()}finally{await Fr(this.#e)}}})}}});var Ye=c(()=>{"use strict";Bt();CS();l_();bf()});import{createHash as lye}from"crypto";import{mkdir as pye,readdir as n$,rm as r$}from"fs/promises";import{join as m_}from"path";async function t_(t){let e=m_(Le(),"library"),n=await i$();if(n===void 0)return{freedBytes:0,removed:0,scanned:0};let r=new Set(n.map(d=>String(d))),i=await n$(e,{withFileTypes:!0}).catch(d=>{if(E(d)&&d.code==="ENOENT")return[];throw d}),a=0,o=0,s=0;for(let d of i){if(!d.isDirectory()||(a+=1,r.has(d.name)))continue;let u=Number(d.name);if(Number.isInteger(u)&&await Nb(u))continue;let l=m_(e,d.name),m=await we(l);t.dryRun||await r$(l,{force:!0,recursive:!0}),o+=1,s+=m}return{freedBytes:s,removed:o,scanned:a}}async function i$(){let t=await D(le(),"");try{return await t.queryAll("SELECT id FROM libraries",void 0,e=>g(e,"id"))}catch{return}finally{await t.close()}}var Fo=c(()=>{"use strict";k();an();Hi();N();Ye();_n();_r();Wt();ko();me();T();jo();kr();Vi()});import{randomBytes as Oye}from"crypto";import{constants as Dye,copyFile as Mye,mkdir as Wye,opendir as Fye,realpath as jye,rename as Gye,rm as Bye,stat as Uye}from"fs/promises";import{dirname as qye,isAbsolute as $ye,join as Kye,relative as Jye,resolve as zye}from"path";var o$,ibe,jo=c(()=>{"use strict";k();N();me();Lt();Ye();T();kr();Fo();Vi();o$=`
|
|
2063
2279
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
2064
2280
|
library_id INTEGER NOT NULL,
|
|
2065
2281
|
public_id TEXT NOT NULL,
|
|
@@ -2072,9 +2288,9 @@ ORDER BY overlay.updated_at ASC
|
|
|
2072
2288
|
created_at TEXT NOT NULL,
|
|
2073
2289
|
updated_at TEXT NOT NULL,
|
|
2074
2290
|
UNIQUE(library_id, public_id)
|
|
2075
|
-
`,
|
|
2291
|
+
`,ibe=`
|
|
2076
2292
|
CREATE TABLE IF NOT EXISTS library_archives (
|
|
2077
|
-
${
|
|
2293
|
+
${o$}
|
|
2078
2294
|
);
|
|
2079
2295
|
|
|
2080
2296
|
CREATE INDEX IF NOT EXISTS idx_library_archives_library
|
|
@@ -2082,29 +2298,29 @@ ${qU}
|
|
|
2082
2298
|
|
|
2083
2299
|
CREATE INDEX IF NOT EXISTS idx_library_archives_library_path
|
|
2084
2300
|
ON library_archives(library_id, relative_path);
|
|
2085
|
-
`});var
|
|
2086
|
-
See: ${e}`}var
|
|
2087
|
-
`}function
|
|
2088
|
-
`}function
|
|
2301
|
+
`});var Ef=c(()=>{"use strict";Ye();jo();kr()});var f_=c(()=>{"use strict";Zs();nd();ma();ud();cd();Ge();ld();Tn();_r();Fo();Ef()});var g_=c(()=>{"use strict";mp();nd();f_();Ge();jo();Ef();kr();Fo()});var Vc=c(()=>{"use strict";kr();jo();g_();Fo();Vi()});import{posix as Ube,resolve as Hbe,sep as qbe}from"path";import{open as Kbe}from"yauzl";var y_=c(()=>{"use strict"});import{createWriteStream as nSe}from"fs";import{mkdir as iSe}from"fs/promises";import{dirname as oSe,resolve as sSe}from"path";import{pipeline as cSe}from"stream/promises";var b_=c(()=>{"use strict";y_()});var If=c(()=>{"use strict"});var S_=c(()=>{"use strict";k();If()});import{readFile as bSe,readdir as SSe}from"fs/promises";import{join as ESe}from"path";var w_=c(()=>{"use strict";T()});import{join as RSe}from"path";var E_=c(()=>{"use strict";k();If()});import{readFile as PSe,readdir as ASe}from"fs/promises";import{join as OSe}from"path";var I_=c(()=>{"use strict";_l();T()});import{mkdir as FSe,writeFile as jSe}from"fs/promises";import{join as BSe}from"path";var x_=c(()=>{"use strict"});import{rm as XSe}from"fs/promises";import{join as YSe}from"path";var k_=c(()=>{"use strict";k();w_();E_();I_();x_()});var R_=c(()=>{"use strict";k_()});import{rm as lwe}from"fs/promises";import{join as pwe,resolve as hwe}from"path";var xf=c(()=>{"use strict";yt();N();Ye();b_();S_();R_()});import{rm as xwe}from"fs/promises";import{homedir as Rwe}from"os";import{resolve as Twe}from"path";var v_=c(()=>{"use strict";Vc();me();xf();Ye();Ws()});var K=c(()=>{"use strict";nt();Jf();Eu();Ri();Pe();Eg();ns();Zo();Vc();me();yt();zr();Wa();Hn();fh();Vt();st();Ye();N();cn();Kr();Ws();v_();_r();Cr();xf();T();Vt();Vt();Vt();Vt();Vt();zt();Hn();N();di()});function $(t,e){return`${t}
|
|
2302
|
+
See: ${e}`}var q,kf=c(()=>{"use strict";K();q={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 cIe}from"path";var T_=c(()=>{"use strict";kf()});import{AsyncLocalStorage as w$}from"async_hooks";function Yc(){return Yt.getStore()?.cwd??process.cwd()}function __(){return Yt.getStore()?.env??process.env}function C_(){return Yt.getStore()?.devProjectRoot}function P_(){return Yt.getStore()?.stateDir}function A_(){return E$??Yt.getStore()?.queueAutostart??!0}function Rf(){return Yt.getStore()?.stdout??process.stdout}function L_(){return Yt.getStore()?.stdoutIsTTY??Rf().isTTY}function O_(t){let e=Yt.getStore();if(e===void 0){process.exitCode=t;return}e.exitCode=t}var Yt,E$,gi=c(()=>{"use strict";Yt=new w$});import{createReadStream as yIe}from"fs";import{rm as SIe}from"fs/promises";import{join as EIe}from"path";async function ce(t){await I$(t)}async function I$(t){await new Promise((e,n)=>{Rf().write(t,r=>{if(r==null){e();return}if(x$(r)){O_(0),e();return}n(r)})})}function x$(t){return t.code==="EPIPE"}var N_=c(()=>{"use strict";K();gi()});function Go(t){return`${JSON.stringify(Zc(t),null,2)}
|
|
2303
|
+
`}function D_(t){return`${JSON.stringify(Zc(t))}
|
|
2304
|
+
`}function Zc(t){if(t===null||typeof t=="boolean"||typeof t=="number"||typeof t=="string")return t;if(Array.isArray(t))return t.map(e=>Zc(e)??null);if(typeof t=="object")return R$(t)}function R$(t){let e=Object.entries(t).map(([r,i],a)=>({index:a,key:r,priority:k$.get(r)??Number.MAX_SAFE_INTEGER,value:i})),n={};for(let r of e.sort((i,a)=>i.priority-a.priority||i.index-a.index)){let i=Zc(r.value);i!==void 0&&(n[r.key]=i)}return n}var k$,M_=c(()=>{"use strict";k$=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((t,e)=>[t,e]))});function T$(t){return t!==""&&v$.test(t)?t:`'${t.replaceAll("'","'\\''")}'`}function _$(t){return t.map(T$).join(" ")}function vf(t){return _$(["wg",...t])}var v$,W_=c(()=>{"use strict";K();v$=/^[A-Za-z0-9_@%+=:,./-]+$/u});var F_=c(()=>{"use strict"});import{existsSync as PIe,readFileSync as AIe}from"fs";import{dirname as OIe,join as NIe,parse as DIe,resolve as MIe}from"path";var j_=c(()=>{"use strict";gi()});var et=c(()=>{"use strict";kf();T_();N_();M_();W_();F_();j_()});async function yi(t){return await A$(async e=>await L$(e,t))}function P$(t,e){switch(t){case"embeddings":return N$(e);case"llm":return D$(e);case"concurrent":return M$(e);case"wikispine":return W$(e)}}async function A$(t){let e=await D(le(),C$);try{return await t(e)}finally{await e.close()}}async function L$(t,e){let n=await t.queryOne("SELECT value_json FROM config_sections WHERE section = ?",[e],r=>String(r.value_json));return n===void 0?{}:O$(e,n)}function O$(t,e){let n;try{n=JSON.parse(e)}catch(r){throw new Error(`Invalid local config JSON for ${t}: ${F$(r)}`)}if(n===null||typeof n!="object"||Array.isArray(n))throw new Error(`Invalid local config section ${t}: expected object.`);return P$(t,n)}function N$(t){let e=new Set(["apiKey","baseURL","dimensions","model","name","provider"]),n=new Set(["openai","openai-compatible"]),r={};for(let[i,a]of Object.entries(t)){if(!e.has(i))throw new Error(`Unknown embeddings config key: ${i}`);if(i==="dimensions"){let s=typeof a=="number"?a:typeof a=="string"?Number(a):NaN;if(!Number.isInteger(s)||s<=0)throw new Error("embeddings.dimensions must be a positive integer.");r[i]=s;continue}if(typeof a!="string"||a.trim()==="")throw new Error(`embeddings.${i} must be a non-empty string.`);let o=a.trim();if(i==="provider"&&!n.has(o))throw new Error(`Unknown embeddings.provider: ${o}`);r[i]=o}return r}function D$(t){let e=new Set(["apiKey","baseURL","model","name","provider"]),n=new Set(["anthropic","google","openai","openai-compatible"]),r={};for(let[i,a]of Object.entries(t)){if(!e.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"&&!n.has(o))throw new Error(`Unknown llm.provider: ${o}`);r[i]=o}return r}function M$(t){let e=new Set(["job","request"]),n={};for(let[r,i]of Object.entries(t)){if(!e.has(r))throw new Error(`Unknown concurrent config key: ${r}`);let a=typeof i=="number"?i:typeof i=="string"?Number(i):NaN;if(!Number.isInteger(a)||a<=0)throw new Error(`concurrent.${r} must be a positive integer.`);n[r]=a}return n}function W$(t){let e=new Set(["provider"]),n=new Set(["cli","fetch"]),r={};for(let[i,a]of Object.entries(t)){if(!e.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"&&!n.has(o))throw new Error(`Unknown wikispine.provider: ${o}`);r[i]=o}return r}function F$(t){return t instanceof Error?t.message:String(t)}var C$,eu=c(()=>{"use strict";K();K();C$=`
|
|
2089
2305
|
CREATE TABLE IF NOT EXISTS config_sections (
|
|
2090
2306
|
section TEXT PRIMARY KEY,
|
|
2091
2307
|
value_json TEXT NOT NULL,
|
|
2092
2308
|
updated_at INTEGER NOT NULL
|
|
2093
2309
|
);
|
|
2094
|
-
`});import{z as
|
|
2095
|
-
`),this.#
|
|
2096
|
-
`)}async#s(e){await
|
|
2097
|
-
`)}#
|
|
2098
|
-
`);return}await
|
|
2099
|
-
${t.map(
|
|
2310
|
+
`});import{z as ge}from"zod";async function Kn(t){let[e,n,r,i]=await Promise.all([yi("embeddings"),yi("llm"),yi("concurrent"),yi("wikispine")]),a=t?.llmJSON===void 0?void 0:U$(t.llmJSON),o=B_({apiKey:Bo(a?.apiKey,Zt(n.apiKey)),baseURL:Bo(a?.baseURL,Zt(n.baseURL)),model:Bo(a?.model,Zt(n.model)),name:Bo(a?.name,Zt(n.name)),provider:Bo(a?.provider,K$(n.provider))}),s=G_(r.request),d=G_(r.job),u=J$(i),l=B$(e);return{...d===void 0&&s===void 0?{}:{concurrent:{...d===void 0?{}:{job:d},...s===void 0?{}:{request:s}}},...l===void 0?{}:{embedding:l},...o===void 0?{}:{llm:o},...u===void 0?{}:{wikispine:u}}}function B$(t){let e=t??{},n=Q$(e.provider),r=Zt(e.apiKey),i=Zt(e.baseURL),a=typeof e.dimensions=="number"&&Number.isInteger(e.dimensions)&&e.dimensions>0?e.dimensions:void 0,o=Zt(e.model),s=Zt(e.name);if(!(n===void 0||o===void 0||n==="openai-compatible"&&i===void 0||n==="openai"&&i!==void 0))return{...r===void 0?{}:{apiKey:r},...i===void 0?{}:{baseURL:i},...a===void 0?{}:{dimensions:a},...o===void 0?{}:{model:o},...s===void 0?{}:{name:s},...n===void 0?{}:{provider:n}}}function U$(t){let e=X$(t);if(e===void 0)throw new Error($("--llm must be a non-empty JSON object.",q.config));let n;try{n=JSON.parse(e)}catch(d){throw new Error($(`Invalid --llm JSON: ${V$(d)}`,q.config))}let r=G$.safeParse(n);if(!r.success)throw new Error($(`Invalid --llm config: ${r.error.issues.map(d=>`${d.path.join(".")||"<root>"}: ${d.message}`).join("; ")}`,q.config));let i=r.data.llm??r.data,a=i.provider??H$(i),o=$$(i),s=B_({apiKey:i.apiKey,baseURL:o,model:i.model,name:i.name,provider:a});if(s===void 0)throw new Error($("--llm must contain at least one supported LLM field.",q.config));return s}function H$(t){return t.provider===void 0&&(t.baseURL!==void 0||t.baseUrl!==void 0||t.chatCompletionsUrl!==void 0)?"openai-compatible":t.provider}function q$(t){if(t.chatCompletionsUrl===void 0)return;let e="/chat/completions";if(!t.chatCompletionsUrl.endsWith(e))throw new Error($("--llm chatCompletionsUrl must end with /chat/completions when baseURL is not provided.",q.config));return t.chatCompletionsUrl.slice(0,-e.length)}function $$(t){return t.baseURL!==void 0?t.baseURL:t.baseUrl!==void 0?t.baseUrl:q$(t)}function B_(t){if(!(t.apiKey===void 0&&t.baseURL===void 0&&t.model===void 0&&t.name===void 0&&t.provider===void 0))return{...t.apiKey===void 0?{}:{apiKey:t.apiKey},...t.baseURL===void 0?{}:{baseURL:t.baseURL},...t.model===void 0?{}:{model:t.model},...t.name===void 0?{}:{name:t.name},...t.provider===void 0?{}:{provider:t.provider}}}function K$(t){if(typeof t!="string")return;let e=Tf.safeParse(t);return e.success?e.data:void 0}function J$(t){let e=z$(t.provider);if(e!==void 0)return{provider:e}}function z$(t){return t==="cli"||t==="fetch"?t:void 0}function Q$(t){return t==="openai"||t==="openai-compatible"?t:void 0}function Zt(t){return typeof t=="string"?t:void 0}function G_(t){return typeof t=="number"&&Number.isInteger(t)&&t>0?t:void 0}function Bo(t,e){return t??e}function X$(t){let e=t?.trim();return e===void 0||e===""?void 0:e}function V$(t){return t instanceof Error?t.message:String(t)}var j$,Tf,G$,Uo=c(()=>{"use strict";et();eu();j$=["anthropic","google","openai","openai-compatible"],Tf=ge.enum(j$),G$=ge.object({apiKey:ge.string().min(1).optional(),baseURL:ge.string().min(1).optional(),baseUrl:ge.string().min(1).optional(),chatCompletionsUrl:ge.string().min(1).optional(),llm:ge.object({apiKey:ge.string().min(1).optional(),baseURL:ge.string().min(1).optional(),baseUrl:ge.string().min(1).optional(),chatCompletionsUrl:ge.string().min(1).optional(),model:ge.string().min(1).optional(),name:ge.string().min(1).optional(),provider:Tf.optional()}).optional(),model:ge.string().min(1).optional(),name:ge.string().min(1).optional(),provider:Tf.optional()})});import{createAnthropic as Y$}from"@ai-sdk/anthropic";import{createGoogleGenerativeAI as Z$}from"@ai-sdk/google";import{createOpenAI as e1}from"@ai-sdk/openai";import{createOpenAICompatible as t1}from"@ai-sdk/openai-compatible";function U_(t){let e=t.llm;if(e?.provider===void 0||e.model===void 0)throw new Error($("Missing LLM configuration. Set --llm for one run, or configure `wikg://local/config/llm` with provider and model.",q.config));return{model:n1(e.provider,e.model,{apiKey:e.apiKey,baseURL:e.baseURL,name:e.name}),...t.concurrent?.request===void 0?{}:{concurrent:t.concurrent.request}}}function n1(t,e,n){switch(t){case"anthropic":return Y$({...n.apiKey===void 0?{}:{apiKey:n.apiKey},...n.name===void 0?{}:{name:n.name}})(e);case"google":return Z$({...n.apiKey===void 0?{}:{apiKey:n.apiKey},...n.name===void 0?{}:{name:n.name}})(e);case"openai":{if(n.baseURL!==void 0)throw new Error($("openai does not accept llm.baseURL or baseURL in --llm JSON. Use openai-compatible for third-party OpenAI-style APIs.",q.config));return e1({...n.apiKey===void 0?{}:{apiKey:n.apiKey},...n.name===void 0?{}:{name:n.name}})(e)}case"openai-compatible":{if(n.baseURL===void 0)throw new Error($("openai-compatible requires llm.baseURL or baseURL in --llm JSON.",q.config));return t1({baseURL:n.baseURL,name:n.name??r1(n.baseURL),...n.apiKey===void 0?{}:{apiKey:n.apiKey}})(e)}}}function r1(t){try{return new URL(t).hostname}catch{return"openai-compatible"}}var _f=c(()=>{"use strict";et()});function H_(t,e,n,r,i){let a=i1(t),o=Math.max(n,Math.ceil(e/a.wordsPerCall)),s=Math.max(1,t==="reading-graph"?1:r.request),d=Math.ceil(o/s),u=e*a.secondsPerWord;return{model:i,timeSeconds:{max:Math.ceil(d*a.maxSecondsPerCall+u),min:Math.ceil(d*a.minSecondsPerCall+u)},tokens:{cacheableInput:Math.ceil(e*a.cacheableInputTokenPerWord),input:Math.ceil(e*a.inputTokenPerWord),output:Math.ceil(e*a.outputTokenPerWord)}}}function q_(t){if(!t.hasGenerationWork)return[];let e=[],n=t.concurrent.request<4?6:t.concurrent.request<8?8:void 0;return n!==void 0&&e.push({command:`wg wikg://local/config/concurrent put request ${n}`,current:t.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:n}),t.concurrent.job<4&&t.chapters>1&&e.push({command:"wg wikg://local/config/concurrent put job 4",current:t.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}),e}function $_(t){return t?.model===void 0?"not configured":t.provider===void 0?t.model:`${t.provider}/${t.model}`}function Cf(t){if(t<60)return`${t}s`;let e=Math.round(t/60);return e<60?`${e}m`:`${Math.round(e/60)}h`}function i1(t){switch(t){case"index-fts":return{cacheableInputTokenPerWord:0,inputTokenPerWord:0,maxSecondsPerCall:1,minSecondsPerCall:0,outputTokenPerWord:0,secondsPerWord:5e-4,wordsPerCall:1e5};case"index-embedding-source":case"index-embedding-summary":return{cacheableInputTokenPerWord:0,inputTokenPerWord:0,maxSecondsPerCall:10,minSecondsPerCall:1,outputTokenPerWord:0,secondsPerWord:.002,wordsPerCall:1e4};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 K_=c(()=>{"use strict"});import{clearLine as dxe,cursorTo as cxe,moveCursor as uxe}from"readline";var J_=c(()=>{"use strict"});function a1(t){let e=t.counters??[],n=e.length===0?"":` ${e.map(o1).join(" | ")}`,r=s1(t.metricGroups);return`${t.phase}${n}${r===""?"":` ${r}`}`}function o1(t){return t.name===t.unit?`${t.name} ${t.done}/${t.total}`:`${t.name} ${t.done}/${t.total} ${t.unit}`}function s1(t){return t===void 0?"":t.flatMap(n=>{let r=n.metrics.map(i=>`${i.name}: ${i.value}`);return r.length===0?[]:[`[${n.name} ${r.join(" / ")}]`]}).join(" ")}function d1(t){let e=t.counters??[];return e.length>0&&e.every(n=>n.total>0&&n.done>=n.total)}var tu,z_=c(()=>{"use strict";et();et();gi();tu=class{#e;#t;#n;#r;#a;#i=!1;constructor(e){this.#e=e.jsonl,this.#t=e.throttleMs,this.#n=e.tty??L_()===!0}async write(e){if(!(e.kind==="status"&&!this.#c(e))){if(this.#e){await this.#s(D_(e.json));return}if(e.kind==="status"){await this.#d(a1(e));return}await this.#o(e.text)}}async#o(e){this.#n&&this.#i&&(await ce(`
|
|
2311
|
+
`),this.#i=!1),await this.#s(`${e}
|
|
2312
|
+
`)}async#s(e){await ce(e)}async#d(e){if(this.#n){await ce(`\r\x1B[2K${e}`),this.#i=!0;return}await this.#s(`${e}
|
|
2313
|
+
`)}#c(e){let n=e.phase,r=d1(e),i=Date.now(),a=this.#r===void 0?Number.POSITIVE_INFINITY:i-this.#r;return n!==this.#a||r||this.#r===void 0||a>=this.#t?(this.#r=i,this.#a=n,!0):!1}}});function Q_(t,e){let n=U_(t);return new vt({dataDirPath:en(),sampling:ec({...n.temperature===void 0?{}:{temperature:n.temperature},...n.topP===void 0?{}:{topP:n.topP}}),...n,...e?.cacheDirPath===void 0?{}:{cacheDirPath:e.cacheDirPath},...e?.logDirPath===void 0?{}:{logDirPath:e.logDirPath},...e?.onStreamProgress===void 0?{}:{onStreamProgress:e.onStreamProgress},...e?.onTokenUsage===void 0?{}:{onTokenUsage:e.onTokenUsage}})}async function nu(t){let e=await Kn({...t.llmJSON===void 0?{}:{llmJSON:t.llmJSON}});if(e.llm?.provider===void 0||e.llm.model===void 0)throw new Error($("Missing LLM configuration. Set --llm for one run, or configure `wikg://local/config/llm` with provider and model.",q.config));return e}var X_=c(()=>{"use strict";K();K();K();K();Uo();et();_f()});var Ho=c(()=>{"use strict";Uo();_f();eu();K_();J_();z_();X_()});function Y_(t){let e=c1(t.env);if(t.envPolicy==="production"){let a=Pf.filter(o=>e[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:e,envPolicy:t.envPolicy,stateDir:t.stateDir}}let n=e.WIKIGRAPH_DEV??e.WIKIGRAPH_STATE_DIR,r=V_(t.stateDir??n);if(r===void 0)throw new Error("Wiki Graph development entries require an explicit runtime state directory.");let i=V_(t.devProjectRoot);if(i===void 0)throw new Error("Wiki Graph development entries require an explicit project root.");return{devProjectRoot:i,env:e,envPolicy:t.envPolicy,stateDir:r}}function c1(t){return t===void 0?{...process.env}:{...process.env,...t}}function V_(t){if(!(t===void 0||t.trim()===""))return t}var Pf,Af=c(()=>{"use strict";Pf=["WIKIGRAPH_DEV","WIKIGRAPH_ENV_POLICY","WIKIGRAPH_QUEUE_DISABLE_AUTOSTART","WIKIGRAPH_STATE_DIR"]});import{spawn as u1}from"child_process";import{existsSync as l1}from"fs";import{join as Lf,resolve as m1}from"path";function Z_(t,e={}){let n=p1(t,e.args??[]);return u1(n.command,n.args,{cwd:Yc(),detached:e.detached===!0,env:h1(),stdio:e.detached===!0?"ignore":["ignore","pipe","pipe"]})}function p1(t,e){let n=C_(),r=P_();return n!==void 0&&r!==void 0?{args:[Lf(n,"node_modules","tsx","dist","cli.mjs"),Lf(n,"packages","cli","src","bin",`${t}.ts`),"--wikigraph-internal-child",t,"--wikigraph-state-dir",r,...e],command:process.execPath}:{args:[f1(t),"--wikigraph-internal-child",t,...r===void 0?[]:["--wikigraph-state-dir",r],...e],command:process.execPath}}function h1(){let t={...__()};for(let e of Pf)delete t[e];return t}function f1(t){let e=`${t}.js`,n=globalThis.__WIKIGRAPH_CLI_DIST_DIR__??m1(Yc(),"packages","cli","dist"),r=Lf(n,e);return l1(r),r}var eC=c(()=>{"use strict";gi();Af()});function ru(t){let e={job:t.config.concurrent?.job??3,request:t.config.concurrent?.request??6},n=t.chapters.reduce((o,s)=>o+s.words,0),r=$_(t.config.llm),i=y1({chapters:t.chapters,concurrent:e,model:r,target:t.target}),a=Math.max(0,...i.map(o=>o.chapters));return{chapters:t.chapters.length,concurrent:e,includesPrerequisites:i.some(o=>o.prerequisite),performanceHints:q_({chapters:a,concurrent:e,hasGenerationWork:i.length>0}),planning:b1(r,i),steps:i,target:t.target,words:n}}function y1(t){switch(t.target){case"index-fts":case"index-embedding-source":case"index-embedding-summary":return[Jn({chapters:t.chapters,concurrent:t.concurrent,model:t.model,prerequisite:!1,task:t.target})].filter(e=>e.chapters>0);case"knowledge-graph":return[Jn({chapters:t.chapters,concurrent:t.concurrent,model:t.model,prerequisite:!0,task:"index-fts"}),Jn({chapters:t.chapters,concurrent:t.concurrent,model:t.model,prerequisite:!1,task:"knowledge-graph"})].filter(e=>e.chapters>0);case"reading-graph":return[Jn({chapters:t.chapters.filter(e=>e.stage==="sourced"),concurrent:t.concurrent,model:t.model,prerequisite:!0,task:"index-fts"}),Jn({chapters:t.chapters.filter(e=>e.stage==="sourced"),concurrent:t.concurrent,model:t.model,prerequisite:!1,task:"reading-graph"})].filter(e=>e.chapters>0);case"reading-summary":{let e=t.chapters.filter(r=>r.stage==="sourced"),n=t.chapters.filter(r=>r.stage==="sourced"||r.stage==="graphed");return[Jn({chapters:e,concurrent:t.concurrent,model:t.model,prerequisite:!0,task:"reading-graph"}),Jn({chapters:n,concurrent:t.concurrent,model:t.model,prerequisite:!1,task:"reading-summary"})].filter(r=>r.chapters>0)}}}function Jn(t){let e=t.chapters.reduce((n,r)=>n+r.words,0);return{chapters:t.chapters.length,planning:H_(t.task,e,t.chapters.length,t.concurrent,t.model),prerequisite:t.prerequisite,task:t.task,words:e}}function b1(t,e){return{model:t,timeSeconds:{max:e.reduce((n,r)=>n+r.planning.timeSeconds.max,0),min:e.reduce((n,r)=>n+r.planning.timeSeconds.min,0)},tokens:{cacheableInput:e.reduce((n,r)=>n+r.planning.tokens.cacheableInput,0),input:e.reduce((n,r)=>n+r.planning.tokens.input,0),output:e.reduce((n,r)=>n+r.planning.tokens.output,0)}}}function Nf(t){return{chapters:t.chapters,concurrent:t.concurrent,includesPrerequisites:t.includesPrerequisites,performanceHints:t.performanceHints,steps:t.steps.map(e=>({chapters:e.chapters,prerequisite:e.prerequisite,task:e.task,tokens:e.planning.tokens,waitSeconds:e.planning.timeSeconds,words:e.words})),target:t.target,tokens:t.planning.tokens,waitSeconds:t.planning.timeSeconds,words:t.words,model:t.planning.model}}function Df(t){return["Estimate:",` Work: ${t.target} over ${t.chapters} chapter${t.chapters===1?"":"s"} / ${t.words} words`,...t.includesPrerequisites?[" Includes prerequisite Reading Graph work where missing."]:[],` Model: ${t.planning.model}`,` Tokens: ${t.planning.tokens.input} input / ${t.planning.tokens.cacheableInput} cacheable input / ${t.planning.tokens.output} output`,` Wait: ${Cf(t.planning.timeSeconds.min)}-${Cf(t.planning.timeSeconds.max)}`,` Current concurrency: job=${t.concurrent.job} request=${t.concurrent.request}`,...S1(t.performanceHints)]}function S1(t){return t.length===0?[]:["Performance hints:",...t.flatMap(e=>[` ${e.message}`,` Command: ${e.command}`])]}var iu=c(()=>{"use strict";Ho()});async function nC(t,e){if(e.json){await ce(Go({items:t.map(ou)}));return}if(t.length===0){await ce(`No jobs.
|
|
2314
|
+
`);return}await ce(`${w1()}
|
|
2315
|
+
${t.map(n=>`${n.jobId.slice(0,8).padEnd(8)} ${n.state.padEnd(9)} ${(n.currentStep??"-").padEnd(7)} ${n.target.padEnd(7)} ${n.chapterId.toString().padStart(7)} ${x1(n.archivePath)}`).join(`
|
|
2100
2316
|
`)}
|
|
2101
|
-
`)}function
|
|
2317
|
+
`)}function w1(){return`${"JOB".padEnd(8)} ${"STATE".padEnd(9)} ${"STEP".padEnd(7)} ${"TARGET".padEnd(7)} ${"CHAPTER".padStart(7)} ARCHIVE`}async function rC(t,e){if(e.json){await ce(Go(ou(t)));return}await ce([`Job: ${t.jobId}`,`State: ${t.state}`,`Archive: ${t.archivePath}`,`Chapter: ${t.chapterId}`,`Target: ${t.target}`,`Step: ${t.currentStep??"-"}`,`Workspace: ${t.workspacePath}`,`Cache: ${t.cachePath}`,`Logs: ${t.logPath}`,...t.errorJSON===void 0?[]:[`Error: ${t.errorJSON}`]].join(`
|
|
2102
2318
|
`)+`
|
|
2103
|
-
`)}function
|
|
2104
|
-
`))}async function
|
|
2319
|
+
`)}function ou(t){return{archiveKey:t.archiveKey,archivePath:t.archivePath,cachePath:t.cachePath,chapterId:t.chapterId,createdAt:t.createdAt,...t.currentStep===void 0?{}:{currentStep:t.currentStep},...t.errorJSON===void 0?{}:{errorJSON:t.errorJSON},eventsPath:t.eventsPath,...t.finishedAt===void 0?{}:{finishedAt:t.finishedAt},jobId:t.jobId,logPath:t.logPath,...t.llmJSON===void 0?{}:{llm:E1(t.llmJSON)},...t.ownerId===void 0?{}:{ownerId:t.ownerId},...t.ownerPid===void 0?{}:{ownerPid:t.ownerPid},...t.prompt===void 0?{}:{prompt:t.prompt},queueRank:t.queueRank,state:t.state,...t.readingSummaryStartedAt===void 0?{}:{readingSummaryStartedAt:t.readingSummaryStartedAt},target:t.target,updatedAt:t.updatedAt,workspacePath:t.workspacePath}}function au(t){if(t.state==="queued")return"Job is queued; the requested artifact or generated data is not ready until the job succeeds."}function E1(t){let e;try{e=JSON.parse(t)}catch{return{configured:!0,invalid:!0}}let n=I1(e);return{configured:!0,...tt(n,"provider")===void 0?{}:{provider:tt(n,"provider")},...tt(n,"model")===void 0?{}:{model:tt(n,"model")},...tt(n,"name")===void 0?{}:{name:tt(n,"name")},hasApiKey:tt(n,"apiKey")!==void 0,hasBaseURL:tt(n,"baseURL")!==void 0||tt(n,"baseUrl")!==void 0||tt(n,"chatCompletionsUrl")!==void 0}}function I1(t){if(!tC(t))return{};let e=t.llm;return tC(e)?e:t}function tt(t,e){let n=t[e];return typeof n=="string"&&n!==""?n:void 0}function tC(t){return t!==null&&typeof t=="object"&&!Array.isArray(t)}async function zn(t,e={json:!1}){if(e.json){await ce(Go({...ou(t),...au(t)===void 0?{}:{notice:au(t)},...e.estimate===void 0?{}:{estimate:Nf(e.estimate)},...e.watch===!0?{watchCommand:vf([`wikg://local/job/${t.jobId}`,"watch"])}:{}}));return}await ce([`Job ${t.jobId} ${t.state} ${t.target} chapter ${t.chapterId} ${t.archivePath}`,...au(t)===void 0?[]:[au(t)],...e.watch===!0?[`Watch: ${vf([`wikg://local/job/${t.jobId}`,"watch"])}`]:[],...e.estimate===void 0?[]:["",...Df(e.estimate)],""].join(`
|
|
2320
|
+
`))}async function iC(t){if(t.json){await ce(Go({created:t.created.map(n=>ou(n.job)),...t.estimate===void 0?{}:{estimate:Nf(t.estimate)},skipped:t.skipped}));return}let e=[`Created: ${t.created.length}`,`Skipped: ${t.skipped.length}`];for(let n of t.created)e.push(`Job ${n.job.jobId} ${n.job.state} ${n.job.target} chapter ${n.job.chapterId}`);for(let n of t.skipped)e.push(`Skipped chapter ${n.chapterId}: ${n.reason}`);t.estimate!==void 0&&e.push("",...Df(t.estimate)),await ce(`${e.join(`
|
|
2105
2321
|
`)}
|
|
2106
|
-
`)}function
|
|
2107
|
-
steps: ${aq(t.step)}`};case"step_completed":return{json:t,kind:"lifecycle",text:`${t.step} completed`};case"created":return{json:t,kind:"lifecycle",text:"created"};default:return{json:t,kind:"lifecycle",text:t.type}}}function iq(t){switch(t){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 aq(t){switch(t){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 t}}function oq(t){return{done:t.done,name:t.name,total:t.total,unit:sq(t.unit)}}function sq(t){switch(t){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 t}}function cq(t){if(t===void 0)return;let e=[...t.inputTokens===void 0?[]:[{name:"input",value:t.inputTokens}],...t.cacheReadTokens===void 0?[]:[{name:"cache",value:t.cacheReadTokens}],...t.outputTokens===void 0?[]:[{name:"output",value:t.outputTokens}]];return e.length===0?void 0:{metrics:e,name:"tokens"}}async function dq(t){await new Promise(e=>{setTimeout(e,t)})}var tq,rq,t_=d(()=>{"use strict";H();yo();tq=new Set(["succeeded","failed","canceled"]),rq=6e3});async function r_(){let t=await Id();await Xa({concurrency:t.concurrent?.job??3,executeJob:async(e,r,n)=>{await uq(e,r,n)}})}async function uq(t,e,r){await Ro({logDirPath:t.logPath,operation:"build-job"},async()=>{await lq(t,e,r)})}async function lq(t,e,r){let n=await vd({...t.llmJSON===void 0?{}:{llmJSON:t.llmJSON}}),i=UT(n,{cacheDirPath:t.cachePath,logDirPath:t.logPath,onStreamProgress:async f=>{await e.addOutputCharacters(f.outputCharacters)},onTokenUsage:async f=>{await e.addTokenUsage({...f.cacheReadTokens===void 0?{}:{cacheReadTokens:f.cacheReadTokens},...f.inputTokens===void 0?{}:{inputTokens:f.inputTokens},...f.outputTokens===void 0?{}:{outputTokens:f.outputTokens}})}}),a=t.prompt??n.prompt,o=Re(a),s=wr(a),c=async(f,E,v)=>await i.request(f,{retryIndex:E,retryMax:v,scope:"serial-generation/reader-extraction",signal:r.signal});c.lazy=async f=>await i.request(async()=>await f(c));let u=await new M(t.archivePath).readDocument(async f=>await $n(f,t.chapterId)),{details:l}=u,{sourceText:m}=u;if(await jn({currentRevision:u.revision,jobId:t.jobId,ownerId:Jh(t)}),await e.setTotals({totalGraphWords:l.stage==="sourced"?l.words:0,totalReadingSummaryWords:l.stage==="sourced"||l.stage==="graphed"?l.words:0}),l.stage==="planned")throw new Error(`Chapter ${t.chapterId} is planned. Set source before queueing a build job.`);if(t.target==="knowledge-graph"){let f=zh(n);await e.stepStarted("knowledge-graph");let E=await new M(t.archivePath).readDocument(async I=>(await bo(t,I),await oo(I,t.chapterId))),v=await fd(t.chapterId,E,{policyPrompt:s,progressTracker:e,request:c,resolverOptions:{logDirPath:t.logPath,normalizer:Fs({request:c})},wikispine:f,workspacePath:t.workspacePath});await e.updatePhase({done:0,phase:"committing",total:1,unit:"item"}),await new M(t.archivePath).write(async I=>{Vn(await ne(t.jobId)),await bo(t,I),await od(I,v)}),await e.updatePhase({done:1,phase:"committing",total:1,unit:"item"}),await e.stepCompleted("knowledge-graph"),Vn(await ne(t.jobId));return}if(l.stage==="sourced"){let f=0;await e.stepStarted("reading-graph");let E=await Gc(t.chapterId,{extractionPrompt:o,llm:i,sourceText:m,workspacePath:t.workspacePath,progressTracker:{async advance(I){f+=I,await e.updateWords({graphWords:f})},async complete(I){await e.updateWords({graphWords:I??l.words})}}});await e.updatePhase({done:0,phase:"committing",total:1,unit:"item"}),l=await new M(t.archivePath).write(async I=>(Vn(await ne(t.jobId)),await bo(t,I),await Bc(I,E))),await e.updatePhase({done:1,phase:"committing",total:1,unit:"item"});let v=await new M(t.archivePath).readDocument(async I=>await $n(I,t.chapterId));l=v.details,await jn({currentRevision:v.revision,jobId:t.jobId,ownerId:Jh(t)}),await e.updateWords({graphWords:l.words}),await e.stepCompleted("reading-graph")}let p=await ne(t.jobId);if(Vn(p),p.target==="reading-graph"||l.stage==="summarized")return;if(l.stage!=="graphed"&&({details:l}=await new M(t.archivePath).readDocument(async f=>await $n(f,t.chapterId))),l.stage!=="graphed")throw new Error(`Chapter ${t.chapterId} is ${l.stage}. Cannot generate summary.`);await e.stepStarted("reading-summary");let h=await new M(t.archivePath).readDocument(async f=>(await bo(t,f),await nd(f,t.chapterId,t.workspacePath))),g=await td(t.chapterId,{llm:i,snapshotPath:h.filePath,workspacePath:t.workspacePath});await e.updatePhase({done:0,phase:"committing",total:1,unit:"item"}),l=await new M(t.archivePath).write(async f=>(Vn(await ne(t.jobId)),await bo(t,f),await rd(f,t.chapterId,g))),await e.updatePhase({done:1,phase:"committing",total:1,unit:"item"}),await e.updateWords({readingSummaryWords:l.words}),await e.stepCompleted("reading-summary"),Vn(await ne(t.jobId))}function zh(t){if(t.wikispine?.provider!==void 0)return t.wikispine;throw new Error(pe(["Knowledge Graph requires WikiSpine.","Configure `wikg://local/config/wikispine` with provider `cli` or `fetch`, then run `wg wikg://local/config/wikispine test`."].join(" "),me.config))}function Vn(t){if(t.state!=="running")throw new Error(`Job ${t.jobId} is ${t.state}. Stop before flushing.`)}async function bo(t,e){await Ua({currentRevision:await e.serials.getRevision(t.chapterId),jobId:t.jobId,ownerId:Jh(t)})}function Jh(t){if(t.ownerId===void 0)throw new Error(`Job ${t.jobId} is not owned by this worker.`);return t.ownerId}var Qh=d(()=>{"use strict";H();H();H();H();xd();yo();gt()});async function Xh(t,e){let r=Ti(e.archivePath).objectUri;if(r===void 0)return;let n=mq(r);if(n===void 0||n.kind==="collection"&&e.depth===void 0)return;let i=await J(t),a=n.kind==="collection"?pq(i,e.depth):hq(i,await zn(t,n.chapterPath),e.depth);return{chapterIds:a.map(o=>o.chapterId),entries:a}}function mq(t){if(t==="wikg://chapter"||t==="wikg://chapter/")return{kind:"collection"};let e=/^wikg:\/\/chapter\/([^/].*?)(?:\/)?$/u.exec(t);if(e?.[1]!==void 0)return{chapterPath:decodeURIComponent(e[1]),kind:"chapter"}}function pq(t,e){return e===void 0?t:t.filter(r=>r.depth<=e)}function hq(t,e,r){let n=t.find(a=>a.chapterId===e);if(n===void 0)throw new Error(`Chapter ${e} does not exist.`);let i=`${n.path}/`;return t.filter(a=>a.chapterId===n.chapterId?!0:a.path.startsWith(i)?r===void 0||a.depth-n.depth<=r:!1)}var n_=d(()=>{"use strict";H()});var i_={};Yh(i_,{runQueueCommand:()=>fq,runQueueWorker:()=>r_});async function fq(t){switch(t.action){case"add":{let e=await yq(t),r=e?.length===1?e[0]:void 0;r!==void 0&&await XT(t,r),YT(t);let n=await vd({...t.llmJSON===void 0?{}:{llmJSON:t.llmJSON}});if(t.target==="knowledge-graph"&&zh(n),e===void 0)await Kh(t,n);else if(e.length===1){let i=e[0],a=await VT(t,i),o=_d({chapters:[a],config:n,target:t.target??"reading-summary"});await Cr(await $h(t,i),{estimate:o,json:t.json??!1,watch:!0})}else await Kh({...t,chapterIds:e},n);go();return}case"list":await JT(await Ha({...t.activeOnly===void 0?{}:{activeOnly:t.activeOnly},...t.all===void 0?{}:{all:t.all},...t.archivePath===void 0?{}:{archivePath:t.archivePath}}),{json:t.json??!1});return;case"status":await zT(await ne(await Pr(t)),{json:t.json??!1});return;case"watch":await e_(await Pr(t),{from:t.from??"beginning",jsonl:t.jsonl??!process.stdout.isTTY});return;case"pause":await Cr(await $a(await Pr(t)));return;case"resume":await Cr(await Ka(await Pr(t))),go();return;case"cancel":await Cr(await Ja(await Pr(t)));return;case"boost":await Cr(await za(await Pr(t))),go();return;case"target":await Cr(await Qa(await Pr(t),t.target??"reading-summary")),go();return;case"clean":await ie(`Cleaned ${await Ya()} jobs.
|
|
2108
|
-
`);return}}async function
|
|
2322
|
+
`)}function x1(t){return t.split(/[\\/]/u).at(-1)??t}var Mf=c(()=>{"use strict";et();iu()});async function Wf(t,e){return await ho({archivePath:t.archivePath,boost:t.boost??!1,chapterId:e,...t.llmJSON===void 0?{}:{llmJSON:t.llmJSON},...t.prompt===void 0?{}:{prompt:t.prompt},target:t.target??"reading-summary"})}async function Ff(t,e){let n=[],r=[];await new L(t.archivePath).readDocument(async i=>{let a=t.chapterIds===void 0?void 0:new Set(t.chapterIds);for(let o of await Y(i)){if(a!==void 0&&!a.has(o.chapterId))continue;if(o.stage==="planned"){r.push({chapterId:o.chapterId,reason:"planned"});continue}let s=t.target??"reading-summary";if(s==="index-embedding-summary"){let d=await i.readSummary(o.chapterId);if(d===void 0||d.trim()===""){r.push({chapterId:o.chapterId,reason:"missing summary"});continue}}if(s==="knowledge-graph"||s==="reading-graph"){let d=await i.indexArtifacts.get(o.chapterId,"fts"),u=await i.serials.getRevision(o.chapterId);if(d?.sourceRevision!==u){r.push({chapterId:o.chapterId,reason:"missing current FTS index artifact"});continue}}try{n.push({chapter:o,job:await Wf(t,o.chapterId)})}catch(d){r.push({chapterId:o.chapterId,reason:d instanceof Error?d.message:String(d)})}}}),await iC({created:n,...n.length===0?{}:{estimate:ru({chapters:n.map(i=>i.chapter),config:e,target:t.target??"reading-summary"})},json:t.json??!1,skipped:r})}async function aC(t,e){let n;if(await new L(t.archivePath).readDocument(async i=>{n=(await Y(i)).find(a=>a.chapterId===e)}),n===void 0)throw new Error(`Chapter ${e} does not exist.`);await new L(t.archivePath).read(async i=>{if(await i.readChapterStage(e)==="planned")throw new Error(`Chapter ${n.uri} is planned. Set source before queueing a build job.`)});let r=t.target??"reading-summary";r!=="index-embedding-summary"&&r!=="knowledge-graph"&&r!=="reading-graph"||await new L(t.archivePath).readDocument(async i=>{if(r==="index-embedding-summary"){let a=await i.readSummary(e);if(a===void 0||a.trim()==="")throw new Error(`Chapter ${n.uri} has no summary. Build a reading summary before queueing a summary embedding index artifact job.`)}if(r==="knowledge-graph"||r==="reading-graph"){let a=await i.indexArtifacts.get(e,"fts"),o=await i.serials.getRevision(e);if(a?.sourceRevision!==o)throw new Error(`Chapter ${n.uri} needs a current FTS index artifact before queueing ${r}.`)}})}async function oC(t,e){let n;if(await new L(t.archivePath).readDocument(async r=>{n=(await Y(r)).find(i=>i.chapterId===e)}),n===void 0)throw new Error(`Chapter ${e} does not exist.`);return n}function sC(t){if(t.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 qo(){if(!A_())return;Z_("queue-worker",{detached:!0}).unref()}var dC=c(()=>{"use strict";K();K();gi();eC();iu();Mf()});async function cC(t,e){let n=0,r=new tu({jsonl:e.jsonl,throttleMs:R1});if(e.from==="now"){let i=await z(t);n=(await Wn(i)).at(-1)?.seq??0}for(;;){let i=await z(t),a=(await Wn(i)).filter(o=>o.seq>n);for(let o of a)n=Math.max(n,o.seq),await r.write(v1(o));if(k1.has(i.state))return;await L1(1e3)}}function v1(t){switch(t.type){case"status_snapshot":{let e=A1(t.tokens);return{counters:t.counters.map(C1),json:t,kind:"status",...e===void 0?{}:{metricGroups:[e]},phase:t.phase??T1(t.step)}}case"target_changed":return{json:t,kind:"lifecycle",text:`target ${t.from} -> ${t.to}`};case"step_started":return{json:t,kind:"lifecycle",text:`${t.step} started
|
|
2323
|
+
steps: ${_1(t.step)}`};case"step_completed":return{json:t,kind:"lifecycle",text:`${t.step} completed`};case"created":return{json:t,kind:"lifecycle",text:"created"};default:return{json:t,kind:"lifecycle",text:t.type}}}function T1(t){switch(t){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 _1(t){switch(t){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 t}}function C1(t){return{done:t.done,name:t.name,total:t.total,unit:P1(t.unit)}}function P1(t){switch(t){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 t}}function A1(t){if(t===void 0)return;let e=[...t.inputTokens===void 0?[]:[{name:"input",value:t.inputTokens}],...t.cacheReadTokens===void 0?[]:[{name:"cache",value:t.cacheReadTokens}],...t.outputTokens===void 0?[]:[{name:"output",value:t.outputTokens}]];return e.length===0?void 0:{metrics:e,name:"tokens"}}async function L1(t){await new Promise(e=>{setTimeout(e,t)})}var k1,R1,uC=c(()=>{"use strict";K();Ho();k1=new Set(["succeeded","failed","canceled"]),R1=6e3});import{embed as tke,embedMany as O1}from"ai";import{createOpenAI as N1}from"@ai-sdk/openai";import{createOpenAICompatible as D1}from"@ai-sdk/openai-compatible";function lC(t){let e=U1(t.provider),n=H1(t.model),r=j1(e,n,t),i=G1(e,t);return{...t.dimensions===void 0?{}:{dimensions:t.dimensions},identity:W1(e,n,t),model:n,embedTexts:async(a,o)=>{let s=[],d=0;for(let u of F1(a,M1)){o?.signal?.throwIfAborted();let l=await O1({...o?.signal===void 0?{}:{abortSignal:o.signal},model:r,...i===void 0?{}:{providerOptions:i},values:[...u]});s.push(...l.embeddings),d+=l.usage.tokens??0}return{embeddings:s,...d===0?{}:{tokens:d}}}}}function W1(t,e,n){return JSON.stringify({...n.baseURL===void 0?{}:{baseURL:n.baseURL},...n.dimensions===void 0?{}:{dimensions:n.dimensions},model:e,provider:t})}function*F1(t,e){for(let n=0;n<t.length;n+=e)yield t.slice(n,n+e)}function j1(t,e,n){switch(t){case"openai":{if(n.baseURL!==void 0)throw new Error($("openai does not accept embeddings.baseURL. Use openai-compatible for third-party OpenAI-style embeddings APIs.",q.config));return N1({...n.apiKey===void 0?{}:{apiKey:n.apiKey},...n.name===void 0?{}:{name:n.name}}).embeddingModel(e)}case"openai-compatible":{if(n.baseURL===void 0)throw new Error($("openai-compatible requires embeddings.baseURL.",q.config));return D1({baseURL:n.baseURL,name:n.name??q1(n.baseURL),...n.apiKey===void 0?{}:{apiKey:n.apiKey}}).embeddingModel(e)}}}function G1(t,e){if(e.dimensions!==void 0)return{[B1(t,e)]:{dimensions:e.dimensions}}}function B1(t,e){return t==="openai"?"openai":"openaiCompatible"}function U1(t){if(t!==void 0)return t;throw new Error($("Missing embeddings configuration. Configure `wikg://local/config/embeddings` with provider and model.",q.config))}function H1(t){if(t!==void 0)return t;throw new Error($("Missing embeddings.model. Configure `wikg://local/config/embeddings` before using Dense search.",q.config))}function q1(t){try{return new URL(t).hostname}catch{return"openai-compatible"}}var M1,mC=c(()=>{"use strict";et();eu();M1=10});import{join as $1}from"path";async function gC(){let t=await Kn();await xo({concurrency:t.concurrent?.job??3,executeJob:async(e,n,r)=>{await K1(e,n,r)}})}async function K1(t,e,n){await Vo({logDirPath:t.logPath,operation:"build-job"},async()=>{await J1(t,e,n)})}async function J1(t,e,n){if(Z1(t.target)){await Q1(t,e,n);return}await z1(t,e,n)}async function z1(t,e,n){let r=await nu({...t.llmJSON===void 0?{}:{llmJSON:t.llmJSON}}),i=Q_(r,{cacheDirPath:t.cachePath,logDirPath:t.logPath,onStreamProgress:async f=>{await e.addOutputCharacters(f.outputCharacters)},onTokenUsage:async f=>{await e.addTokenUsage({...f.cacheReadTokens===void 0?{}:{cacheReadTokens:f.cacheReadTokens},...f.inputTokens===void 0?{}:{inputTokens:f.inputTokens},...f.outputTokens===void 0?{}:{outputTokens:f.outputTokens}})}}),a=t.prompt??r.prompt,o=_e(a),s=Mn(a),d=async(f,I,v)=>await i.request(f,{retryIndex:I,retryMax:v,scope:"serial-generation/reader-extraction",signal:n.signal});d.lazy=async f=>await i.request(async()=>await f(d));let u=await new L(t.archivePath).readDocument(async f=>await li(f,t.chapterId)),{details:l}=u,{sourceText:m}=u;if(await Gn({currentRevision:u.revision,jobId:t.jobId,ownerId:su(t)}),await e.setTotals({totalGraphWords:l.stage==="sourced"?l.words:0,totalReadingSummaryWords:l.stage==="sourced"||l.stage==="graphed"?l.words:0}),l.stage==="planned")throw new Error(`Chapter ${t.chapterId} is planned. Set source before queueing a build job.`);if((t.target==="knowledge-graph"||t.target==="reading-graph")&&await new L(t.archivePath).readDocument(async f=>{if((await f.indexArtifacts.get(t.chapterId,"fts"))?.sourceRevision!==u.revision)throw new Error(`Chapter ${t.chapterId} needs a current FTS index artifact before running ${t.target}.`)}),t.target==="knowledge-graph"){let f=Gf(r);await e.stepStarted("knowledge-graph");let I=await new L(t.archivePath).readDocument(async x=>(await Qn(t,x),await Oo(x,t.chapterId))),v=await zc(t.chapterId,I,{policyPrompt:s,progressTracker:e,request:d,resolverOptions:{logDirPath:t.logPath,normalizer:gd({request:d})},wikispine:f,workspacePath:t.workspacePath});await e.updatePhase({done:0,phase:"committing",total:1,unit:"item"}),await new L(t.archivePath).write(async x=>{kt(await z(t.jobId)),await Qn(t,x),await jc(x,v)}),await e.updatePhase({done:1,phase:"committing",total:1,unit:"item"}),await e.stepCompleted("knowledge-graph"),kt(await z(t.jobId));return}if(l.stage==="sourced"){let f=0;await e.stepStarted("reading-graph");let I=await bc(t.chapterId,{extractionPrompt:o,llm:i,sourceText:m,workspacePath:t.workspacePath,progressTracker:{async advance(x){f+=x,await e.updateWords({graphWords:f})},async complete(x){await e.updateWords({graphWords:x??l.words})}}});await e.updatePhase({done:0,phase:"committing",total:1,unit:"item"}),l=await new L(t.archivePath).write(async x=>(kt(await z(t.jobId)),await Qn(t,x),await Sc(x,I))),await e.updatePhase({done:1,phase:"committing",total:1,unit:"item"});let v=await new L(t.archivePath).readDocument(async x=>await li(x,t.chapterId));l=v.details,await Gn({currentRevision:v.revision,jobId:t.jobId,ownerId:su(t)}),await e.updateWords({graphWords:l.words}),await e.stepCompleted("reading-graph")}let p=await z(t.jobId);if(kt(p),p.target==="reading-graph"||l.stage==="summarized")return;if(l.stage!=="graphed"&&({details:l}=await new L(t.archivePath).readDocument(async f=>await li(f,t.chapterId))),l.stage!=="graphed")throw new Error(`Chapter ${t.chapterId} is ${l.stage}. Cannot generate summary.`);await e.stepStarted("reading-summary");let h=await new L(t.archivePath).readDocument(async f=>(await Qn(t,f),await Mc(f,t.chapterId,t.workspacePath))),y=await Nc(t.chapterId,{llm:i,snapshotPath:h.filePath,workspacePath:t.workspacePath});await e.updatePhase({done:0,phase:"committing",total:1,unit:"item"}),l=await new L(t.archivePath).write(async f=>(kt(await z(t.jobId)),await Qn(t,f),await Dc(f,t.chapterId,y))),await e.updatePhase({done:1,phase:"committing",total:1,unit:"item"}),await e.updateWords({readingSummaryWords:l.words}),await e.stepCompleted("reading-summary"),kt(await z(t.jobId))}async function Q1(t,e,n){await e.stepStarted(t.target);let r=await X1(t);if(await Gn({currentRevision:r.revision,jobId:t.jobId,ownerId:su(t)}),await e.updatePhase({done:0,phase:"indexing",total:1,unit:"item"}),t.target==="index-fts"){let d=hC(t);await ta(d,ea({chapterTitles:r.chapterTitles,chunks:r.chunks,mentions:r.mentions,sentences:r.sentences,serialId:t.chapterId,sourceRevision:r.revision,summarySentences:r.summarySentences}));let u=await na(d);if(fC(t,r.revision,u),!("lexicalRows"in u))throw new Error("Index artifact output is not an FTS artifact.");await new L(t.archivePath).write(async l=>{kt(await z(t.jobId)),await jf(l,t.chapterId),await Qn(t,l),await l.indexArtifacts.replaceFts(u)}),await pC(t,e);return}let i=await Kn();if(i.embedding===void 0)throw new Error($("Missing embeddings configuration. Configure `wikg://local/config/embeddings` before building embedding index artifacts.",q.config));let a=hC(t),o=await Ms({embeddingProvider:lC(i.embedding),kind:t.target==="index-embedding-source"?"embedding-source":"embedding-summary",sentences:r.sentences,serialId:t.chapterId,signal:n.signal,sourceRevision:r.revision});await ta(a,o);let s=await na(a);if(fC(t,r.revision,s),"lexicalRows"in s)throw new Error("Index artifact output is not an embedding artifact.");await new L(t.archivePath).write(async d=>{kt(await z(t.jobId)),await jf(d,t.chapterId),await Qn(t,d),await d.indexArtifacts.replaceEmbedding(s)}),await pC(t,e)}async function pC(t,e){await e.updatePhase({done:1,phase:"indexing",total:1,unit:"item"}),await e.stepCompleted(t.target),kt(await z(t.jobId))}async function X1(t){return await new L(t.archivePath).readDocument(async e=>{await jf(e,t.chapterId);let n=await e.serials.getRevision(t.chapterId),r=t.target==="index-embedding-summary"?e.getSummaryFragments(t.chapterId):e.getSerialFragments(t.chapterId);if(t.target==="index-embedding-summary"){let a=await e.readSummary(t.chapterId);if(a===void 0||a.trim()==="")throw new Error(`Chapter ${t.chapterId} has no summary. Build a reading summary before building a summary embedding index artifact.`)}if(r.listSentences===void 0)throw new Error("Text stream does not expose sentence listing.");let i=await r.listSentences();return t.target!=="index-fts"?{chapterTitles:[],chunks:[],mentions:[],revision:n,sentences:i,summarySentences:[]}:{chapterTitles:await Y1(e,t.chapterId),chunks:await e.chunks.listBySerial(t.chapterId),mentions:await e.mentions.listByChapter(t.chapterId),revision:n,sentences:i,summarySentences:await V1(e.getSummaryFragments(t.chapterId))}})}async function V1(t){if(t.listSentences===void 0)throw new Error("Text stream does not expose sentence listing.");return await t.listSentences()}async function Y1(t,e){let r=await t.readToc?.();if(r===void 0)return[];let i=yC(r.items).find(a=>a.serialId===e);return i===void 0||typeof i.title!="string"?[]:[{id:e,title:i.title}]}function yC(t){return t.flatMap(e=>[e,...yC(e.children)])}function hC(t){return $1(t.workspacePath,"index-artifact-output.jsonl")}function fC(t,e,n){if(n.serialId!==t.chapterId)throw new Error(`Index artifact output targets chapter ${n.serialId}, expected chapter ${t.chapterId}.`);if(n.sourceRevision!==e)throw new Error(`Index artifact output targets input revision ${n.sourceRevision}, expected revision ${e}.`);if(t.target==="index-fts"){if("lexicalRows"in n)return;throw new Error("Index artifact output is not an FTS artifact.")}if("lexicalRows"in n)throw new Error("Index artifact output is not an embedding artifact.");let r=t.target==="index-embedding-source"?"embedding-source":"embedding-summary";if(n.kind!==r)throw new Error(`Index artifact output kind ${n.kind} does not match ${t.target}.`)}async function jf(t,e){if(await t.serials.getById(e)===void 0)throw new Error(`Chapter ${e} no longer exists. Queue a new build job for an existing chapter.`)}function Gf(t){if(t.wikispine?.provider!==void 0)return t.wikispine;throw new Error($(["Knowledge Graph requires WikiSpine.","Configure `wikg://local/config/wikispine` with provider `cli` or `fetch`, then run `wg wikg://local/config/wikispine test`."].join(" "),q.config))}function Z1(t){return t==="index-fts"||t==="index-embedding-source"||t==="index-embedding-summary"}function kt(t){if(t.state!=="running")throw new Error(`Job ${t.jobId} is ${t.state}. Stop before flushing.`)}async function Qn(t,e){await fo({currentRevision:await e.serials.getRevision(t.chapterId),jobId:t.jobId,ownerId:su(t)})}function su(t){if(t.ownerId===void 0)throw new Error(`Job ${t.jobId} is not owned by this worker.`);return t.ownerId}var Bf=c(()=>{"use strict";K();K();K();K();Uo();Ho();mC();et()});async function Uf(t,e){let n=Qi(e.archivePath).objectUri;if(n===void 0)return;let r=eK(n);if(r===void 0||r.kind==="collection"&&e.depth===void 0)return;let i=await Y(t),a=r.kind==="collection"?tK(i,e.depth):nK(i,await hi(t,r.chapterPath),e.depth);return{chapterIds:a.map(o=>o.chapterId),entries:a}}function eK(t){if(t==="wikg://chapter"||t==="wikg://chapter/")return{kind:"collection"};let e=/^wikg:\/\/chapter\/([^/].*?)(?:\/)?$/u.exec(t);if(e?.[1]!==void 0)return{chapterPath:decodeURIComponent(e[1]),kind:"chapter"}}function tK(t,e){return e===void 0?t:t.filter(n=>n.depth<=e)}function nK(t,e,n){let r=t.find(a=>a.chapterId===e);if(r===void 0)throw new Error(`Chapter ${e} does not exist.`);let i=`${r.path}/`;return t.filter(a=>a.chapterId===r.chapterId?!0:a.path.startsWith(i)?n===void 0||a.depth-r.depth<=n:!1)}var bC=c(()=>{"use strict";K()});var SC={};qf(SC,{runQueueCommand:()=>rK,runQueueWorker:()=>gC});async function rK(t){switch(t.action){case"add":{let e=await aK(t),n=e?.length===1?e[0]:void 0;n!==void 0&&await aC(t,n);let r=t.target??"reading-summary";r!=="index-fts"&&sC(t);let i=iK(r)?await nu({...t.llmJSON===void 0?{}:{llmJSON:t.llmJSON}}):await Kn({...t.llmJSON===void 0?{}:{llmJSON:t.llmJSON}});if((r==="index-embedding-source"||r==="index-embedding-summary")&&i.embedding===void 0)throw new Error("Missing embeddings configuration. Configure `wikg://local/config/embeddings` before queueing embedding index artifact jobs.");if(t.target==="knowledge-graph"&&Gf(i),e===void 0)await Ff(t,i);else if(e.length===1){let a=e[0],o=await oC(t,a),s=ru({chapters:[o],config:i,target:t.target??"reading-summary"});await zn(await Wf(t,a),{estimate:s,json:t.json??!1,watch:!0})}else await Ff({...t,chapterIds:e},i);qo();return}case"list":await nC(await go({...t.activeOnly===void 0?{}:{activeOnly:t.activeOnly},...t.all===void 0?{}:{all:t.all},...t.archivePath===void 0?{}:{archivePath:t.archivePath}}),{json:t.json??!1});return;case"status":await rC(await z(await Xn(t)),{json:t.json??!1});return;case"watch":await cC(await Xn(t),{from:t.from??"beginning",jsonl:t.jsonl??!process.stdout.isTTY});return;case"pause":await zn(await bo(await Xn(t)));return;case"resume":await zn(await So(await Xn(t))),qo();return;case"cancel":await zn(await wo(await Xn(t)));return;case"boost":await zn(await Eo(await Xn(t))),qo();return;case"target":await zn(await Io(await Xn(t),t.target??"reading-summary")),qo();return;case"clean":await ce(`Cleaned ${await Ro()} jobs.
|
|
2324
|
+
`);return}}function iK(t){return t==="knowledge-graph"||t==="reading-graph"||t==="reading-summary"}async function Xn(t){return await yo(t.jobId)}async function aK(t){if(t.chapterId!==void 0)return[t.chapterId];if(t.chapterPath===void 0&&t.depth===void 0)return;let e;return await new L(t.archivePath).readDocument(async n=>{if(t.chapterPath===void 0){e=(await Uf(n,{archivePath:xr(t.archivePath,"wikg://chapter"),...t.depth===void 0?{}:{depth:t.depth}}))?.chapterIds;return}e=(await Uf(n,{archivePath:Xi(t.archivePath,t.chapterPath),...t.depth===void 0?{}:{depth:t.depth}}))?.chapterIds??[await hi(n,t.chapterPath)]}),e}var wC=c(()=>{"use strict";K();Uo();Ho();et();dC();iu();Mf();uC();Bf();bC();Bf()});async function EC(){return(await Promise.resolve().then(()=>(wC(),SC))).runQueueWorker()}K();Af();var IC="--wikigraph-internal-child",xC="--wikigraph-state-dir";async function kC(t,e){let n=oK(process.argv.slice(2));if(n.internalChild!==t)throw new Error("This Wiki Graph worker entry is internal.");let r=Y_({argv:n.argv,envPolicy:"production",stateDir:n.stateDir});return await Ru(r.stateDir,()=>e(n))}function oK(t){let e=[],n,r;for(let i=0;i<t.length;i+=1){let a=t[i];if(a===IC){if(i+1>=t.length)throw new Error(`${IC} requires a value.`);n=t[i+1],i+=1;continue}if(a!==xC){e.push(a);continue}if(i+1>=t.length)throw new Error(`${xC} requires a value.`);r=t[i+1],i+=1}return{argv:e,internalChild:n??"",stateDir:r}}async function sK(){await kC("queue-worker",async()=>await EC())}sK().catch(t=>{process.stderr.write(`${t instanceof Error?t.stack??t.message:String(t)}
|
|
2109
2325
|
`),process.exitCode=1});
|
|
2110
2326
|
//# sourceMappingURL=queue-worker.js.map
|