seojeom-mcp 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -105,7 +105,7 @@ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.
105
105
  `:`[${o[c]}\r
106
106
  ]`;continue}n+=o[c],o[c]==="\\"?i=!0:a&&o[c]==="]"?a=!1:!a&&o[c]==="["&&(a=!0)}try{new RegExp(n)}catch{return console.warn(`Could not convert regex pattern at ${e.currentPath.join("/")} to a flag-independent form! Falling back to the flag-ignorant source`),t.source}return n}function pm(t,e){if(e.target==="openAi"&&console.warn("Warning: OpenAI may not support records in schemas! Try an array of key-value pairs instead."),e.target==="openApi3"&&t.keyType?._def.typeName===T.ZodEnum)return{type:"object",required:t.keyType._def.values,properties:t.keyType._def.values.reduce((o,n)=>({...o,[n]:H(t.valueType._def,{...e,currentPath:[...e.currentPath,"properties",n]})??Ce(e)}),{}),additionalProperties:e.rejectedAdditionalProperties};let r={type:"object",additionalProperties:H(t.valueType._def,{...e,currentPath:[...e.currentPath,"additionalProperties"]})??e.allowedAdditionalProperties};if(e.target==="openApi3")return r;if(t.keyType?._def.typeName===T.ZodString&&t.keyType._def.checks?.length){let{type:o,...n}=um(t.keyType._def,e);return{...r,propertyNames:n}}else{if(t.keyType?._def.typeName===T.ZodEnum)return{...r,propertyNames:{enum:t.keyType._def.values}};if(t.keyType?._def.typeName===T.ZodBranded&&t.keyType._def.type._def.typeName===T.ZodString&&t.keyType._def.type._def.checks?.length){let{type:o,...n}=lm(t.keyType._def,e);return{...r,propertyNames:n}}}return r}function nT(t,e){if(e.mapStrategy==="record")return pm(t,e);let r=H(t.keyType._def,{...e,currentPath:[...e.currentPath,"items","items","0"]})||Ce(e),o=H(t.valueType._def,{...e,currentPath:[...e.currentPath,"items","items","1"]})||Ce(e);return{type:"array",maxItems:125,items:{type:"array",items:[r,o],minItems:2,maxItems:2}}}function oT(t){let e=t.values,o=Object.keys(t.values).filter(i=>typeof e[e[i]]!="number").map(i=>e[i]),n=Array.from(new Set(o.map(i=>typeof i)));return{type:n.length===1?n[0]==="string"?"string":"number":["string","number"],enum:o}}function iT(t){return t.target==="openAi"?void 0:{not:Ce({...t,currentPath:[...t.currentPath,"not"]})}}function aT(t){return t.target==="openApi3"?{enum:["null"],nullable:!0}:{type:"null"}}var Oc={ZodString:"string",ZodNumber:"number",ZodBigInt:"integer",ZodBoolean:"boolean",ZodNull:"null"};function cT(t,e){if(e.target==="openApi3")return sT(t,e);let r=t.options instanceof Map?Array.from(t.options.values()):t.options;if(r.every(o=>o._def.typeName in Oc&&(!o._def.checks||!o._def.checks.length))){let o=r.reduce((n,i)=>{let a=Oc[i._def.typeName];return a&&!n.includes(a)?[...n,a]:n},[]);return{type:o.length>1?o:o[0]}}else if(r.every(o=>o._def.typeName==="ZodLiteral"&&!o.description)){let o=r.reduce((n,i)=>{let a=typeof i._def.value;switch(a){case"string":case"number":case"boolean":return[...n,a];case"bigint":return[...n,"integer"];case"object":if(i._def.value===null)return[...n,"null"];default:return n}},[]);if(o.length===r.length){let n=o.filter((i,a,s)=>s.indexOf(i)===a);return{type:n.length>1?n:n[0],enum:r.reduce((i,a)=>i.includes(a._def.value)?i:[...i,a._def.value],[])}}}else if(r.every(o=>o._def.typeName==="ZodEnum"))return{type:"string",enum:r.reduce((o,n)=>[...o,...n._def.values.filter(i=>!o.includes(i))],[])};return sT(t,e)}var sT=(t,e)=>{let r=(t.options instanceof Map?Array.from(t.options.values()):t.options).map((o,n)=>H(o._def,{...e,currentPath:[...e.currentPath,"anyOf",`${n}`]})).filter(o=>!!o&&(!e.strictUnions||typeof o=="object"&&Object.keys(o).length>0));return r.length?{anyOf:r}:void 0};function lT(t,e){if(["ZodString","ZodNumber","ZodBigInt","ZodBoolean","ZodNull"].includes(t.innerType._def.typeName)&&(!t.innerType._def.checks||!t.innerType._def.checks.length))return e.target==="openApi3"?{type:Oc[t.innerType._def.typeName],nullable:!0}:{type:[Oc[t.innerType._def.typeName],"null"]};if(e.target==="openApi3"){let o=H(t.innerType._def,{...e,currentPath:[...e.currentPath]});return o&&"$ref"in o?{allOf:[o],nullable:!0}:o&&{...o,nullable:!0}}let r=H(t.innerType._def,{...e,currentPath:[...e.currentPath,"anyOf","0"]});return r&&{anyOf:[r,{type:"null"}]}}function uT(t,e){let r={type:"number"};if(!t.checks)return r;for(let o of t.checks)switch(o.kind){case"int":r.type="integer",n0(r,"type",o.message,e);break;case"min":e.target==="jsonSchema7"?o.inclusive?ue(r,"minimum",o.value,o.message,e):ue(r,"exclusiveMinimum",o.value,o.message,e):(o.inclusive||(r.exclusiveMinimum=!0),ue(r,"minimum",o.value,o.message,e));break;case"max":e.target==="jsonSchema7"?o.inclusive?ue(r,"maximum",o.value,o.message,e):ue(r,"exclusiveMaximum",o.value,o.message,e):(o.inclusive||(r.exclusiveMaximum=!0),ue(r,"maximum",o.value,o.message,e));break;case"multipleOf":ue(r,"multipleOf",o.value,o.message,e);break}return r}function pT(t,e){let r=e.target==="openAi",o={type:"object",properties:{}},n=[],i=t.shape();for(let s in i){let c=i[s];if(c===void 0||c._def===void 0)continue;let l=n7(c);l&&r&&(c._def.typeName==="ZodOptional"&&(c=c._def.innerType),c.isNullable()||(c=c.nullable()),l=!1);let u=H(c._def,{...e,currentPath:[...e.currentPath,"properties",s],propertyPath:[...e.currentPath,"properties",s]});u!==void 0&&(o.properties[s]=u,l||n.push(s))}n.length&&(o.required=n);let a=r7(t,e);return a!==void 0&&(o.additionalProperties=a),o}function r7(t,e){if(t.catchall._def.typeName!=="ZodNever")return H(t.catchall._def,{...e,currentPath:[...e.currentPath,"additionalProperties"]});switch(t.unknownKeys){case"passthrough":return e.allowedAdditionalProperties;case"strict":return e.rejectedAdditionalProperties;case"strip":return e.removeAdditionalStrategy==="strict"?e.allowedAdditionalProperties:e.rejectedAdditionalProperties}}function n7(t){try{return t.isOptional()}catch{return!0}}var dT=(t,e)=>{if(e.currentPath.toString()===e.propertyPath?.toString())return H(t.innerType._def,e);let r=H(t.innerType._def,{...e,currentPath:[...e.currentPath,"anyOf","1"]});return r?{anyOf:[{not:Ce(e)},r]}:Ce(e)};var fT=(t,e)=>{if(e.pipeStrategy==="input")return H(t.in._def,e);if(e.pipeStrategy==="output")return H(t.out._def,e);let r=H(t.in._def,{...e,currentPath:[...e.currentPath,"allOf","0"]}),o=H(t.out._def,{...e,currentPath:[...e.currentPath,"allOf",r?"1":"0"]});return{allOf:[r,o].filter(n=>n!==void 0)}};function mT(t,e){return H(t.type._def,e)}function hT(t,e){let o={type:"array",uniqueItems:!0,items:H(t.valueType._def,{...e,currentPath:[...e.currentPath,"items"]})};return t.minSize&&ue(o,"minItems",t.minSize.value,t.minSize.message,e),t.maxSize&&ue(o,"maxItems",t.maxSize.value,t.maxSize.message,e),o}function gT(t,e){return t.rest?{type:"array",minItems:t.items.length,items:t.items.map((r,o)=>H(r._def,{...e,currentPath:[...e.currentPath,"items",`${o}`]})).reduce((r,o)=>o===void 0?r:[...r,o],[]),additionalItems:H(t.rest._def,{...e,currentPath:[...e.currentPath,"additionalItems"]})}:{type:"array",minItems:t.items.length,maxItems:t.items.length,items:t.items.map((r,o)=>H(r._def,{...e,currentPath:[...e.currentPath,"items",`${o}`]})).reduce((r,o)=>o===void 0?r:[...r,o],[])}}function vT(t){return{not:Ce(t)}}function yT(t){return Ce(t)}var xT=(t,e)=>H(t.innerType._def,e);var bT=(t,e,r)=>{switch(e){case T.ZodString:return um(t,r);case T.ZodNumber:return uT(t,r);case T.ZodObject:return pT(t,r);case T.ZodBigInt:return J4(t,r);case T.ZodBoolean:return K4();case T.ZodDate:return o0(t,r);case T.ZodUndefined:return vT(r);case T.ZodNull:return aT(r);case T.ZodArray:return B4(t,r);case T.ZodUnion:case T.ZodDiscriminatedUnion:return cT(t,r);case T.ZodIntersection:return eT(t,r);case T.ZodTuple:return gT(t,r);case T.ZodRecord:return pm(t,r);case T.ZodLiteral:return tT(t,r);case T.ZodEnum:return Y4(t);case T.ZodNativeEnum:return oT(t);case T.ZodNullable:return lT(t,r);case T.ZodOptional:return dT(t,r);case T.ZodMap:return nT(t,r);case T.ZodSet:return hT(t,r);case T.ZodLazy:return()=>t.getter()._def;case T.ZodPromise:return mT(t,r);case T.ZodNaN:case T.ZodNever:return iT(r);case T.ZodEffects:return Q4(t,r);case T.ZodAny:return Ce(r);case T.ZodUnknown:return yT(r);case T.ZodDefault:return X4(t,r);case T.ZodBranded:return lm(t,r);case T.ZodReadonly:return xT(t,r);case T.ZodCatch:return W4(t,r);case T.ZodPipeline:return fT(t,r);case T.ZodFunction:case T.ZodVoid:case T.ZodSymbol:return;default:return(o=>{})(e)}};function H(t,e,r=!1){let o=e.seen.get(t);if(e.override){let s=e.override?.(t,e,o,r);if(s!==V4)return s}if(o&&!r){let s=o7(o,e);if(s!==void 0)return s}let n={def:t,path:e.currentPath,jsonSchema:void 0};e.seen.set(t,n);let i=bT(t,t.typeName,e),a=typeof i=="function"?H(i(),e):i;if(a&&i7(t,e,a),e.postProcess){let s=e.postProcess(a,t,e);return n.jsonSchema=a,s}return n.jsonSchema=a,a}var o7=(t,e)=>{switch(e.$refStrategy){case"root":return{$ref:t.path.join("/")};case"relative":return{$ref:cm(e.currentPath,t.path)};case"none":case"seen":return t.path.length<e.currentPath.length&&t.path.every((r,o)=>e.currentPath[o]===r)?(console.warn(`Recursive reference detected at ${e.currentPath.join("/")}! Defaulting to any`),Ce(e)):e.$refStrategy==="seen"?Ce(e):void 0}},i7=(t,e,r)=>(t.description&&(r.description=t.description,e.markdownDescription&&(r.markdownDescription=t.description)),r);var s0=(t,e)=>{let r=H4(e),o=typeof e=="object"&&e.definitions?Object.entries(e.definitions).reduce((c,[l,u])=>({...c,[l]:H(u._def,{...r,currentPath:[...r.basePath,r.definitionPath,l]},!0)??Ce(r)}),{}):void 0,n=typeof e=="string"?e:e?.nameStrategy==="title"?void 0:e?.name,i=H(t._def,n===void 0?r:{...r,currentPath:[...r.basePath,r.definitionPath,n]},!1)??Ce(r),a=typeof e=="object"&&e.name!==void 0&&e.nameStrategy==="title"?e.name:void 0;a!==void 0&&(i.title=a),r.flags.hasReferencedOpenAiAnyType&&(o||(o={}),o[r.openAiAnyTypeName]||(o[r.openAiAnyTypeName]={type:["string","number","integer","boolean","array","null"],items:{$ref:r.$refStrategy==="relative"?"1":[...r.basePath,r.definitionPath,r.openAiAnyTypeName].join("/")}}));let s=n===void 0?o?{...i,[r.definitionPath]:o}:i:{$ref:[...r.$refStrategy==="relative"?[]:r.basePath,r.definitionPath,n].join("/"),[r.definitionPath]:{...o,[n]:i}};return r.target==="jsonSchema7"?s.$schema="http://json-schema.org/draft-07/schema#":(r.target==="jsonSchema2019-09"||r.target==="openAi")&&(s.$schema="https://json-schema.org/draft/2019-09/schema#"),r.target==="openAi"&&("anyOf"in s||"oneOf"in s||"allOf"in s||"type"in s&&Array.isArray(s.type))&&console.warn("Warning: OpenAI may not support schemas with unions as roots! Try wrapping it in an object property."),s};function a7(t){return!t||t==="jsonSchema7"||t==="draft-7"?"draft-7":t==="jsonSchema2019-09"||t==="draft-2020-12"?"draft-2020-12":"draft-7"}function c0(t,e){return ir(t)?ho(t,{target:a7(e?.target),io:e?.pipeStrategy??"input"}):s0(t,{strictUnions:e?.strictUnions??!0,pipeStrategy:e?.pipeStrategy??"input"})}function l0(t){let r=Mn(t)?.method;if(!r)throw new Error("Schema is missing a method literal");let o=sm(r);if(typeof o!="string")throw new Error("Schema method literal must be a string");return o}function u0(t,e){let r=Dn(t,e);if(!r.success)throw r.error;return r.data}var s7=6e4,dm=class{constructor(e){this._options=e,this._requestMessageId=0,this._requestHandlers=new Map,this._requestHandlerAbortControllers=new Map,this._notificationHandlers=new Map,this._responseHandlers=new Map,this._progressHandlers=new Map,this._timeoutInfo=new Map,this._pendingDebouncedNotifications=new Set,this._taskProgressTokens=new Map,this._requestResolvers=new Map,this.setNotificationHandler(Td,r=>{this._oncancel(r)}),this.setNotificationHandler(Od,r=>{this._onprogress(r)}),this.setRequestHandler(zd,r=>({})),this._taskStore=e?.taskStore,this._taskMessageQueue=e?.taskMessageQueue,this._taskStore&&(this.setRequestHandler(Ad,async(r,o)=>{let n=await this._taskStore.getTask(r.params.taskId,o.sessionId);if(!n)throw new q(F.InvalidParams,"Failed to retrieve task: Task not found");return{...n}}),this.setRequestHandler(Nd,async(r,o)=>{let n=async()=>{let i=r.params.taskId;if(this._taskMessageQueue){let s;for(;s=await this._taskMessageQueue.dequeue(i,o.sessionId);){if(s.type==="response"||s.type==="error"){let c=s.message,l=c.id,u=this._requestResolvers.get(l);if(u)if(this._requestResolvers.delete(l),s.type==="response")u(c);else{let p=c,d=new q(p.error.code,p.error.message,p.error.data);u(d)}else{let p=s.type==="response"?"Response":"Error";this._onerror(new Error(`${p} handler missing for request ${l}`))}continue}await this._transport?.send(s.message,{relatedRequestId:o.requestId})}}let a=await this._taskStore.getTask(i,o.sessionId);if(!a)throw new q(F.InvalidParams,`Task not found: ${i}`);if(!Ln(a.status))return await this._waitForTaskUpdate(i,o.signal),await n();if(Ln(a.status)){let s=await this._taskStore.getTaskResult(i,o.sessionId);return this._clearTaskQueue(i),{...s,_meta:{...s._meta,[In]:{taskId:i}}}}return await n()};return await n()}),this.setRequestHandler(Dd,async(r,o)=>{try{let{tasks:n,nextCursor:i}=await this._taskStore.listTasks(r.params?.cursor,o.sessionId);return{tasks:n,nextCursor:i,_meta:{}}}catch(n){throw new q(F.InvalidParams,`Failed to list tasks: ${n instanceof Error?n.message:String(n)}`)}}),this.setRequestHandler(Ld,async(r,o)=>{try{let n=await this._taskStore.getTask(r.params.taskId,o.sessionId);if(!n)throw new q(F.InvalidParams,`Task not found: ${r.params.taskId}`);if(Ln(n.status))throw new q(F.InvalidParams,`Cannot cancel task in terminal status: ${n.status}`);await this._taskStore.updateTaskStatus(r.params.taskId,"cancelled","Client cancelled task execution.",o.sessionId),this._clearTaskQueue(r.params.taskId);let i=await this._taskStore.getTask(r.params.taskId,o.sessionId);if(!i)throw new q(F.InvalidParams,`Task not found after cancellation: ${r.params.taskId}`);return{_meta:{},...i}}catch(n){throw n instanceof q?n:new q(F.InvalidRequest,`Failed to cancel task: ${n instanceof Error?n.message:String(n)}`)}}))}async _oncancel(e){if(!e.params.requestId)return;this._requestHandlerAbortControllers.get(e.params.requestId)?.abort(e.params.reason)}_setupTimeout(e,r,o,n,i=!1){this._timeoutInfo.set(e,{timeoutId:setTimeout(n,r),startTime:Date.now(),timeout:r,maxTotalTimeout:o,resetTimeoutOnProgress:i,onTimeout:n})}_resetTimeout(e){let r=this._timeoutInfo.get(e);if(!r)return!1;let o=Date.now()-r.startTime;if(r.maxTotalTimeout&&o>=r.maxTotalTimeout)throw this._timeoutInfo.delete(e),q.fromError(F.RequestTimeout,"Maximum total timeout exceeded",{maxTotalTimeout:r.maxTotalTimeout,totalElapsed:o});return clearTimeout(r.timeoutId),r.timeoutId=setTimeout(r.onTimeout,r.timeout),!0}_cleanupTimeout(e){let r=this._timeoutInfo.get(e);r&&(clearTimeout(r.timeoutId),this._timeoutInfo.delete(e))}async connect(e){if(this._transport)throw new Error("Already connected to a transport. Call close() before connecting to a new transport, or use a separate Protocol instance per connection.");this._transport=e;let r=this.transport?.onclose;this._transport.onclose=()=>{r?.(),this._onclose()};let o=this.transport?.onerror;this._transport.onerror=i=>{o?.(i),this._onerror(i)};let n=this._transport?.onmessage;this._transport.onmessage=(i,a)=>{n?.(i,a),wi(i)||_y(i)?this._onresponse(i):Ed(i)?this._onrequest(i,a):Dk(i)?this._onnotification(i):this._onerror(new Error(`Unknown message type: ${JSON.stringify(i)}`))},await this._transport.start()}_onclose(){let e=this._responseHandlers;this._responseHandlers=new Map,this._progressHandlers.clear(),this._taskProgressTokens.clear(),this._pendingDebouncedNotifications.clear();for(let o of this._timeoutInfo.values())clearTimeout(o.timeoutId);this._timeoutInfo.clear();for(let o of this._requestHandlerAbortControllers.values())o.abort();this._requestHandlerAbortControllers.clear();let r=q.fromError(F.ConnectionClosed,"Connection closed");this._transport=void 0,this.onclose?.();for(let o of e.values())o(r)}_onerror(e){this.onerror?.(e)}_onnotification(e){let r=this._notificationHandlers.get(e.method)??this.fallbackNotificationHandler;r!==void 0&&Promise.resolve().then(()=>r(e)).catch(o=>this._onerror(new Error(`Uncaught error in notification handler: ${o}`)))}_onrequest(e,r){let o=this._requestHandlers.get(e.method)??this.fallbackRequestHandler,n=this._transport,i=e.params?._meta?.[In]?.taskId;if(o===void 0){let u={jsonrpc:"2.0",id:e.id,error:{code:F.MethodNotFound,message:"Method not found"}};i&&this._taskMessageQueue?this._enqueueTaskMessage(i,{type:"error",message:u,timestamp:Date.now()},n?.sessionId).catch(p=>this._onerror(new Error(`Failed to enqueue error response: ${p}`))):n?.send(u).catch(p=>this._onerror(new Error(`Failed to send an error response: ${p}`)));return}let a=new AbortController;this._requestHandlerAbortControllers.set(e.id,a);let s=Ak(e.params)?e.params.task:void 0,c=this._taskStore?this.requestTaskStore(e,n?.sessionId):void 0,l={signal:a.signal,sessionId:n?.sessionId,_meta:e.params?._meta,sendNotification:async u=>{if(a.signal.aborted)return;let p={relatedRequestId:e.id};i&&(p.relatedTask={taskId:i}),await this.notification(u,p)},sendRequest:async(u,p,d)=>{if(a.signal.aborted)throw new q(F.ConnectionClosed,"Request was cancelled");let f={...d,relatedRequestId:e.id};i&&!f.relatedTask&&(f.relatedTask={taskId:i});let h=f.relatedTask?.taskId??i;return h&&c&&await c.updateTaskStatus(h,"input_required"),await this.request(u,p,f)},authInfo:r?.authInfo,requestId:e.id,requestInfo:r?.requestInfo,taskId:i,taskStore:c,taskRequestedTtl:s?.ttl,closeSSEStream:r?.closeSSEStream,closeStandaloneSSEStream:r?.closeStandaloneSSEStream};Promise.resolve().then(()=>{s&&this.assertTaskHandlerCapability(e.method)}).then(()=>o(e,l)).then(async u=>{if(a.signal.aborted)return;let p={result:u,jsonrpc:"2.0",id:e.id};i&&this._taskMessageQueue?await this._enqueueTaskMessage(i,{type:"response",message:p,timestamp:Date.now()},n?.sessionId):await n?.send(p)},async u=>{if(a.signal.aborted)return;let p={jsonrpc:"2.0",id:e.id,error:{code:Number.isSafeInteger(u.code)?u.code:F.InternalError,message:u.message??"Internal error",...u.data!==void 0&&{data:u.data}}};i&&this._taskMessageQueue?await this._enqueueTaskMessage(i,{type:"error",message:p,timestamp:Date.now()},n?.sessionId):await n?.send(p)}).catch(u=>this._onerror(new Error(`Failed to send response: ${u}`))).finally(()=>{this._requestHandlerAbortControllers.get(e.id)===a&&this._requestHandlerAbortControllers.delete(e.id)})}_onprogress(e){let{progressToken:r,...o}=e.params,n=Number(r),i=this._progressHandlers.get(n);if(!i){this._onerror(new Error(`Received a progress notification for an unknown token: ${JSON.stringify(e)}`));return}let a=this._responseHandlers.get(n),s=this._timeoutInfo.get(n);if(s&&a&&s.resetTimeoutOnProgress)try{this._resetTimeout(n)}catch(c){this._responseHandlers.delete(n),this._progressHandlers.delete(n),this._cleanupTimeout(n),a(c);return}i(o)}_onresponse(e){let r=Number(e.id),o=this._requestResolvers.get(r);if(o){if(this._requestResolvers.delete(r),wi(e))o(e);else{let a=new q(e.error.code,e.error.message,e.error.data);o(a)}return}let n=this._responseHandlers.get(r);if(n===void 0){this._onerror(new Error(`Received a response for an unknown message ID: ${JSON.stringify(e)}`));return}this._responseHandlers.delete(r),this._cleanupTimeout(r);let i=!1;if(wi(e)&&e.result&&typeof e.result=="object"){let a=e.result;if(a.task&&typeof a.task=="object"){let s=a.task;typeof s.taskId=="string"&&(i=!0,this._taskProgressTokens.set(s.taskId,r))}}if(i||this._progressHandlers.delete(r),wi(e))n(e);else{let a=q.fromError(e.error.code,e.error.message,e.error.data);n(a)}}get transport(){return this._transport}async close(){await this._transport?.close()}async*requestStream(e,r,o){let{task:n}=o??{};if(!n){try{yield{type:"result",result:await this.request(e,r,o)}}catch(a){yield{type:"error",error:a instanceof q?a:new q(F.InternalError,String(a))}}return}let i;try{let a=await this.request(e,Si,o);if(a.task)i=a.task.taskId,yield{type:"taskCreated",task:a.task};else throw new q(F.InternalError,"Task creation did not return a task");for(;;){let s=await this.getTask({taskId:i},o);if(yield{type:"taskStatus",task:s},Ln(s.status)){s.status==="completed"?yield{type:"result",result:await this.getTaskResult({taskId:i},r,o)}:s.status==="failed"?yield{type:"error",error:new q(F.InternalError,`Task ${i} failed`)}:s.status==="cancelled"&&(yield{type:"error",error:new q(F.InternalError,`Task ${i} was cancelled`)});return}if(s.status==="input_required"){yield{type:"result",result:await this.getTaskResult({taskId:i},r,o)};return}let c=s.pollInterval??this._options?.defaultTaskPollInterval??1e3;await new Promise(l=>setTimeout(l,c)),o?.signal?.throwIfAborted()}}catch(a){yield{type:"error",error:a instanceof q?a:new q(F.InternalError,String(a))}}}request(e,r,o){let{relatedRequestId:n,resumptionToken:i,onresumptiontoken:a,task:s,relatedTask:c}=o??{};return new Promise((l,u)=>{let p=$=>{u($)};if(!this._transport){p(new Error("Not connected"));return}if(this._options?.enforceStrictCapabilities===!0)try{this.assertCapabilityForMethod(e.method),s&&this.assertTaskCapability(e.method)}catch($){p($);return}o?.signal?.throwIfAborted();let d=this._requestMessageId++,f={...e,jsonrpc:"2.0",id:d};o?.onprogress&&(this._progressHandlers.set(d,o.onprogress),f.params={...e.params,_meta:{...e.params?._meta||{},progressToken:d}}),s&&(f.params={...f.params,task:s}),c&&(f.params={...f.params,_meta:{...f.params?._meta||{},[In]:c}});let h=$=>{this._responseHandlers.delete(d),this._progressHandlers.delete(d),this._cleanupTimeout(d),this._transport?.send({jsonrpc:"2.0",method:"notifications/cancelled",params:{requestId:d,reason:String($)}},{relatedRequestId:n,resumptionToken:i,onresumptiontoken:a}).catch(z=>this._onerror(new Error(`Failed to send cancellation: ${z}`)));let S=$ instanceof q?$:new q(F.RequestTimeout,String($));u(S)};this._responseHandlers.set(d,$=>{if(!o?.signal?.aborted){if($ instanceof Error)return u($);try{let S=Dn(r,$.result);S.success?l(S.data):u(S.error)}catch(S){u(S)}}}),o?.signal?.addEventListener("abort",()=>{h(o?.signal?.reason)});let m=o?.timeout??s7,g=()=>h(q.fromError(F.RequestTimeout,"Request timed out",{timeout:m}));this._setupTimeout(d,m,o?.maxTotalTimeout,g,o?.resetTimeoutOnProgress??!1);let b=c?.taskId;if(b){let $=S=>{let z=this._responseHandlers.get(d);z?z(S):this._onerror(new Error(`Response handler missing for side-channeled request ${d}`))};this._requestResolvers.set(d,$),this._enqueueTaskMessage(b,{type:"request",message:f,timestamp:Date.now()}).catch(S=>{this._cleanupTimeout(d),u(S)})}else this._transport.send(f,{relatedRequestId:n,resumptionToken:i,onresumptiontoken:a}).catch($=>{this._cleanupTimeout(d),u($)})})}async getTask(e,r){return this.request({method:"tasks/get",params:e},Cd,r)}async getTaskResult(e,r,o){return this.request({method:"tasks/result",params:e},r,o)}async listTasks(e,r){return this.request({method:"tasks/list",params:e},Md,r)}async cancelTask(e,r){return this.request({method:"tasks/cancel",params:e},Lk,r)}async notification(e,r){if(!this._transport)throw new Error("Not connected");this.assertNotificationCapability(e.method);let o=r?.relatedTask?.taskId;if(o){let s={...e,jsonrpc:"2.0",params:{...e.params,_meta:{...e.params?._meta||{},[In]:r.relatedTask}}};await this._enqueueTaskMessage(o,{type:"notification",message:s,timestamp:Date.now()});return}if((this._options?.debouncedNotificationMethods??[]).includes(e.method)&&!e.params&&!r?.relatedRequestId&&!r?.relatedTask){if(this._pendingDebouncedNotifications.has(e.method))return;this._pendingDebouncedNotifications.add(e.method),Promise.resolve().then(()=>{if(this._pendingDebouncedNotifications.delete(e.method),!this._transport)return;let s={...e,jsonrpc:"2.0"};r?.relatedTask&&(s={...s,params:{...s.params,_meta:{...s.params?._meta||{},[In]:r.relatedTask}}}),this._transport?.send(s,r).catch(c=>this._onerror(c))});return}let a={...e,jsonrpc:"2.0"};r?.relatedTask&&(a={...a,params:{...a.params,_meta:{...a.params?._meta||{},[In]:r.relatedTask}}}),await this._transport.send(a,r)}setRequestHandler(e,r){let o=l0(e);this.assertRequestHandlerCapability(o),this._requestHandlers.set(o,(n,i)=>{let a=u0(e,n);return Promise.resolve(r(a,i))})}removeRequestHandler(e){this._requestHandlers.delete(e)}assertCanSetRequestHandler(e){if(this._requestHandlers.has(e))throw new Error(`A request handler for ${e} already exists, which would be overridden`)}setNotificationHandler(e,r){let o=l0(e);this._notificationHandlers.set(o,n=>{let i=u0(e,n);return Promise.resolve(r(i))})}removeNotificationHandler(e){this._notificationHandlers.delete(e)}_cleanupTaskProgressHandler(e){let r=this._taskProgressTokens.get(e);r!==void 0&&(this._progressHandlers.delete(r),this._taskProgressTokens.delete(e))}async _enqueueTaskMessage(e,r,o){if(!this._taskStore||!this._taskMessageQueue)throw new Error("Cannot enqueue task message: taskStore and taskMessageQueue are not configured");let n=this._options?.maxTaskQueueSize;await this._taskMessageQueue.enqueue(e,r,o,n)}async _clearTaskQueue(e,r){if(this._taskMessageQueue){let o=await this._taskMessageQueue.dequeueAll(e,r);for(let n of o)if(n.type==="request"&&Ed(n.message)){let i=n.message.id,a=this._requestResolvers.get(i);a?(a(new q(F.InternalError,"Task cancelled or completed")),this._requestResolvers.delete(i)):this._onerror(new Error(`Resolver missing for request ${i} during task ${e} cleanup`))}}}async _waitForTaskUpdate(e,r){let o=this._options?.defaultTaskPollInterval??1e3;try{let n=await this._taskStore?.getTask(e);n?.pollInterval&&(o=n.pollInterval)}catch{}return new Promise((n,i)=>{if(r.aborted){i(new q(F.InvalidRequest,"Request cancelled"));return}let a=setTimeout(n,o);r.addEventListener("abort",()=>{clearTimeout(a),i(new q(F.InvalidRequest,"Request cancelled"))},{once:!0})})}requestTaskStore(e,r){let o=this._taskStore;if(!o)throw new Error("No task store configured");return{createTask:async n=>{if(!e)throw new Error("No request provided");return await o.createTask(n,e.id,{method:e.method,params:e.params},r)},getTask:async n=>{let i=await o.getTask(n,r);if(!i)throw new q(F.InvalidParams,"Failed to retrieve task: Task not found");return i},storeTaskResult:async(n,i,a)=>{await o.storeTaskResult(n,i,a,r);let s=await o.getTask(n,r);if(s){let c=Hs.parse({method:"notifications/tasks/status",params:s});await this.notification(c),Ln(s.status)&&this._cleanupTaskProgressHandler(n)}},getTaskResult:n=>o.getTaskResult(n,r),updateTaskStatus:async(n,i,a)=>{let s=await o.getTask(n,r);if(!s)throw new q(F.InvalidParams,`Task "${n}" not found - it may have been cleaned up`);if(Ln(s.status))throw new q(F.InvalidParams,`Cannot update task "${n}" from terminal status "${s.status}" to "${i}". Terminal states (completed, failed, cancelled) cannot transition to other states.`);await o.updateTaskStatus(n,i,a,r);let c=await o.getTask(n,r);if(c){let l=Hs.parse({method:"notifications/tasks/status",params:c});await this.notification(l),Ln(c.status)&&this._cleanupTaskProgressHandler(n)}},listTasks:n=>o.listTasks(n,r)}}};function _T(t){return t!==null&&typeof t=="object"&&!Array.isArray(t)}function wT(t,e){let r={...t};for(let o in e){let n=o,i=e[n];if(i===void 0)continue;let a=r[n];_T(a)&&_T(i)?r[n]={...a,...i}:r[n]=i}return r}var c6=$l(K_(),1),l6=$l(s6(),1);function QJ(){let t=new c6.default({strict:!1,validateFormats:!0,validateSchema:!1,allErrors:!0});return(0,l6.default)(t),t}var Jm=class{constructor(e){this._ajv=e??QJ()}getValidator(e){let r="$id"in e&&typeof e.$id=="string"?this._ajv.getSchema(e.$id)??this._ajv.compile(e):this._ajv.compile(e);return o=>r(o)?{valid:!0,data:o,errorMessage:void 0}:{valid:!1,data:void 0,errorMessage:this._ajv.errorsText(r.errors)}}};var Km=class{constructor(e){this._server=e}requestStream(e,r,o){return this._server.requestStream(e,r,o)}createMessageStream(e,r){let o=this._server.getClientCapabilities();if((e.tools||e.toolChoice)&&!o?.sampling?.tools)throw new Error("Client does not support sampling tools capability.");if(e.messages.length>0){let n=e.messages[e.messages.length-1],i=Array.isArray(n.content)?n.content:[n.content],a=i.some(u=>u.type==="tool_result"),s=e.messages.length>1?e.messages[e.messages.length-2]:void 0,c=s?Array.isArray(s.content)?s.content:[s.content]:[],l=c.some(u=>u.type==="tool_use");if(a){if(i.some(u=>u.type!=="tool_result"))throw new Error("The last message must contain only tool_result content if any is present");if(!l)throw new Error("tool_result blocks are not matching any tool_use from the previous message")}if(l){let u=new Set(c.filter(d=>d.type==="tool_use").map(d=>d.id)),p=new Set(i.filter(d=>d.type==="tool_result").map(d=>d.toolUseId));if(u.size!==p.size||![...u].every(d=>p.has(d)))throw new Error("ids of tool_result blocks and tool_use blocks from previous message do not match")}}return this.requestStream({method:"sampling/createMessage",params:e},Ks,r)}elicitInputStream(e,r){let o=this._server.getClientCapabilities(),n=e.mode??"form";switch(n){case"url":{if(!o?.elicitation?.url)throw new Error("Client does not support url elicitation.");break}case"form":{if(!o?.elicitation?.form)throw new Error("Client does not support form elicitation.");break}}let i=n==="form"&&e.mode===void 0?{...e,mode:"form"}:e;return this.requestStream({method:"elicitation/create",params:i},Pi,r)}async getTask(e,r){return this._server.getTask({taskId:e},r)}async getTaskResult(e,r,o){return this._server.getTaskResult({taskId:e},r,o)}async listTasks(e,r){return this._server.listTasks(e?{cursor:e}:void 0,r)}async cancelTask(e,r){return this._server.cancelTask({taskId:e},r)}};function u6(t,e,r){if(!t)throw new Error(`${r} does not support task creation (required for ${e})`);switch(e){case"tools/call":if(!t.tools?.call)throw new Error(`${r} does not support task creation for tools/call (required for ${e})`);break;default:break}}function p6(t,e,r){if(!t)throw new Error(`${r} does not support task creation (required for ${e})`);switch(e){case"sampling/createMessage":if(!t.sampling?.createMessage)throw new Error(`${r} does not support task creation for sampling/createMessage (required for ${e})`);break;case"elicitation/create":if(!t.elicitation?.create)throw new Error(`${r} does not support task creation for elicitation/create (required for ${e})`);break;default:break}}var Wm=class extends dm{constructor(e,r){super(r),this._serverInfo=e,this._loggingLevels=new Map,this.LOG_LEVEL_SEVERITY=new Map(Js.options.map((o,n)=>[o,n])),this.isMessageIgnored=(o,n)=>{let i=this._loggingLevels.get(n);return i?this.LOG_LEVEL_SEVERITY.get(o)<this.LOG_LEVEL_SEVERITY.get(i):!1},this._capabilities=r?.capabilities??{},this._instructions=r?.instructions,this._jsonSchemaValidator=r?.jsonSchemaValidator??new Jm,this.setRequestHandler(Sy,o=>this._oninitialize(o)),this.setNotificationHandler($y,()=>this.oninitialized?.()),this._capabilities.logging&&this.setRequestHandler(Ty,async(o,n)=>{let i=n.sessionId||n.requestInfo?.headers["mcp-session-id"]||void 0,{level:a}=o.params,s=Js.safeParse(a);return s.success&&this._loggingLevels.set(i,s.data),{}})}get experimental(){return this._experimental||(this._experimental={tasks:new Km(this)}),this._experimental}registerCapabilities(e){if(this.transport)throw new Error("Cannot register capabilities after connecting to transport");this._capabilities=wT(this._capabilities,e)}setRequestHandler(e,r){let n=Mn(e)?.method;if(!n)throw new Error("Schema is missing a method literal");let i;if(ir(n)){let s=n;i=s._zod?.def?.value??s.value}else{let s=n;i=s._def?.value??s.value}if(typeof i!="string")throw new Error("Schema method literal must be a string");if(i==="tools/call"){let s=async(c,l)=>{let u=Dn(ki,c);if(!u.success){let h=u.error instanceof Error?u.error.message:String(u.error);throw new q(F.InvalidParams,`Invalid tools/call request: ${h}`)}let{params:p}=u.data,d=await Promise.resolve(r(c,l));if(p.task){let h=Dn(Si,d);if(!h.success){let m=h.error instanceof Error?h.error.message:String(h.error);throw new q(F.InvalidParams,`Invalid task creation result: ${m}`)}return h.data}let f=Dn(Hd,d);if(!f.success){let h=f.error instanceof Error?f.error.message:String(f.error);throw new q(F.InvalidParams,`Invalid tools/call result: ${h}`)}return f.data};return super.setRequestHandler(e,s)}return super.setRequestHandler(e,r)}assertCapabilityForMethod(e){switch(e){case"sampling/createMessage":if(!this._clientCapabilities?.sampling)throw new Error(`Client does not support sampling (required for ${e})`);break;case"elicitation/create":if(!this._clientCapabilities?.elicitation)throw new Error(`Client does not support elicitation (required for ${e})`);break;case"roots/list":if(!this._clientCapabilities?.roots)throw new Error(`Client does not support listing roots (required for ${e})`);break;case"ping":break}}assertNotificationCapability(e){switch(e){case"notifications/message":if(!this._capabilities.logging)throw new Error(`Server does not support logging (required for ${e})`);break;case"notifications/resources/updated":case"notifications/resources/list_changed":if(!this._capabilities.resources)throw new Error(`Server does not support notifying about resources (required for ${e})`);break;case"notifications/tools/list_changed":if(!this._capabilities.tools)throw new Error(`Server does not support notifying of tool list changes (required for ${e})`);break;case"notifications/prompts/list_changed":if(!this._capabilities.prompts)throw new Error(`Server does not support notifying of prompt list changes (required for ${e})`);break;case"notifications/elicitation/complete":if(!this._clientCapabilities?.elicitation?.url)throw new Error(`Client does not support URL elicitation (required for ${e})`);break;case"notifications/cancelled":break;case"notifications/progress":break}}assertRequestHandlerCapability(e){if(this._capabilities)switch(e){case"completion/complete":if(!this._capabilities.completions)throw new Error(`Server does not support completions (required for ${e})`);break;case"logging/setLevel":if(!this._capabilities.logging)throw new Error(`Server does not support logging (required for ${e})`);break;case"prompts/get":case"prompts/list":if(!this._capabilities.prompts)throw new Error(`Server does not support prompts (required for ${e})`);break;case"resources/list":case"resources/templates/list":case"resources/read":if(!this._capabilities.resources)throw new Error(`Server does not support resources (required for ${e})`);break;case"tools/call":case"tools/list":if(!this._capabilities.tools)throw new Error(`Server does not support tools (required for ${e})`);break;case"tasks/get":case"tasks/list":case"tasks/result":case"tasks/cancel":if(!this._capabilities.tasks)throw new Error(`Server does not support tasks capability (required for ${e})`);break;case"ping":case"initialize":break}}assertTaskCapability(e){p6(this._clientCapabilities?.tasks?.requests,e,"Client")}assertTaskHandlerCapability(e){this._capabilities&&u6(this._capabilities.tasks?.requests,e,"Server")}async _oninitialize(e){let r=e.params.protocolVersion;return this._clientCapabilities=e.params.capabilities,this._clientVersion=e.params.clientInfo,{protocolVersion:vy.includes(r)?r:gy,capabilities:this.getCapabilities(),serverInfo:this._serverInfo,...this._instructions&&{instructions:this._instructions}}}getClientCapabilities(){return this._clientCapabilities}getClientVersion(){return this._clientVersion}getCapabilities(){return this._capabilities}async ping(){return this.request({method:"ping"},jd)}async createMessage(e,r){if((e.tools||e.toolChoice)&&!this._clientCapabilities?.sampling?.tools)throw new Error("Client does not support sampling tools capability.");if(e.messages.length>0){let o=e.messages[e.messages.length-1],n=Array.isArray(o.content)?o.content:[o.content],i=n.some(l=>l.type==="tool_result"),a=e.messages.length>1?e.messages[e.messages.length-2]:void 0,s=a?Array.isArray(a.content)?a.content:[a.content]:[],c=s.some(l=>l.type==="tool_use");if(i){if(n.some(l=>l.type!=="tool_result"))throw new Error("The last message must contain only tool_result content if any is present");if(!c)throw new Error("tool_result blocks are not matching any tool_use from the previous message")}if(c){let l=new Set(s.filter(p=>p.type==="tool_use").map(p=>p.id)),u=new Set(n.filter(p=>p.type==="tool_result").map(p=>p.toolUseId));if(l.size!==u.size||![...l].every(p=>u.has(p)))throw new Error("ids of tool_result blocks and tool_use blocks from previous message do not match")}}return e.tools?this.request({method:"sampling/createMessage",params:e},zy,r):this.request({method:"sampling/createMessage",params:e},Ks,r)}async elicitInput(e,r){switch(e.mode??"form"){case"url":{if(!this._clientCapabilities?.elicitation?.url)throw new Error("Client does not support url elicitation.");let n=e;return this.request({method:"elicitation/create",params:n},Pi,r)}case"form":{if(!this._clientCapabilities?.elicitation?.form)throw new Error("Client does not support form elicitation.");let n=e.mode==="form"?e:{...e,mode:"form"},i=await this.request({method:"elicitation/create",params:n},Pi,r);if(i.action==="accept"&&i.content&&n.requestedSchema)try{let s=this._jsonSchemaValidator.getValidator(n.requestedSchema)(i.content);if(!s.valid)throw new q(F.InvalidParams,`Elicitation response content does not match requested schema: ${s.errorMessage}`)}catch(a){throw a instanceof q?a:new q(F.InternalError,`Error validating elicitation response: ${a instanceof Error?a.message:String(a)}`)}return i}}}createElicitationCompletionNotifier(e,r){if(!this._clientCapabilities?.elicitation?.url)throw new Error("Client does not support URL elicitation (required for notifications/elicitation/complete)");return()=>this.notification({method:"notifications/elicitation/complete",params:{elicitationId:e}},r)}async listRoots(e,r){return this.request({method:"roots/list",params:e},Oy,r)}async sendLoggingMessage(e,r){if(this._capabilities.logging&&!this.isMessageIgnored(e.level,r))return this.notification({method:"notifications/message",params:e})}async sendResourceUpdated(e){return this.notification({method:"notifications/resources/updated",params:e})}async sendResourceListChanged(){return this.notification({method:"notifications/resources/list_changed"})}async sendToolListChanged(){return this.notification({method:"notifications/tools/list_changed"})}async sendPromptListChanged(){return this.notification({method:"notifications/prompts/list_changed"})}};var f6=Symbol.for("mcp.completable");function nw(t){return!!t&&typeof t=="object"&&f6 in t}function m6(t){return t[f6]?.complete}var d6;(function(t){t.Completable="McpCompletable"})(d6||(d6={}));var YJ=/^[A-Za-z0-9._-]{1,128}$/;function eK(t){let e=[];if(t.length===0)return{isValid:!1,warnings:["Tool name cannot be empty"]};if(t.length>128)return{isValid:!1,warnings:[`Tool name exceeds maximum length of 128 characters (current: ${t.length})`]};if(t.includes(" ")&&e.push("Tool name contains spaces, which may cause parsing issues"),t.includes(",")&&e.push("Tool name contains commas, which may cause parsing issues"),(t.startsWith("-")||t.endsWith("-"))&&e.push("Tool name starts or ends with a dash, which may cause parsing issues in some contexts"),(t.startsWith(".")||t.endsWith("."))&&e.push("Tool name starts or ends with a dot, which may cause parsing issues in some contexts"),!YJ.test(t)){let r=t.split("").filter(o=>!/[A-Za-z0-9._-]/.test(o)).filter((o,n,i)=>i.indexOf(o)===n);return e.push(`Tool name contains invalid characters: ${r.map(o=>`"${o}"`).join(", ")}`,"Allowed characters are: A-Z, a-z, 0-9, underscore (_), dash (-), and dot (.)"),{isValid:!1,warnings:e}}return{isValid:!0,warnings:e}}function tK(t,e){if(e.length>0){console.warn(`Tool name validation warning for "${t}":`);for(let r of e)console.warn(` - ${r}`);console.warn("Tool registration will proceed, but this may cause compatibility issues."),console.warn("Consider updating the tool name to conform to the MCP tool naming standard."),console.warn("See SEP: Specify Format for Tool Names (https://github.com/modelcontextprotocol/modelcontextprotocol/issues/986) for more details.")}}function ow(t){let e=eK(t);return tK(t,e.warnings),e.isValid}var Xm=class{constructor(e){this._mcpServer=e}registerToolTask(e,r,o){let n={taskSupport:"required",...r.execution};if(n.taskSupport==="forbidden")throw new Error(`Cannot register task-based tool '${e}' with taskSupport 'forbidden'. Use registerTool() instead.`);return this._mcpServer._createRegisteredTool(e,r.title,r.description,r.inputSchema,r.outputSchema,r.annotations,n,r._meta,o)}};var Ta=class{constructor(e,r){this._registeredResources={},this._registeredResourceTemplates={},this._registeredTools={},this._registeredPrompts={},this._toolHandlersInitialized=!1,this._completionHandlerInitialized=!1,this._resourceHandlersInitialized=!1,this._promptHandlersInitialized=!1,this.server=new Wm(e,r)}get experimental(){return this._experimental||(this._experimental={tasks:new Xm(this)}),this._experimental}async connect(e){return await this.server.connect(e)}async close(){await this.server.close()}setToolRequestHandlers(){this._toolHandlersInitialized||(this.server.assertCanSetRequestHandler(Jn(Gd)),this.server.assertCanSetRequestHandler(Jn(ki)),this.server.registerCapabilities({tools:{listChanged:!0}}),this.server.setRequestHandler(Gd,()=>({tools:Object.entries(this._registeredTools).filter(([,e])=>e.enabled).map(([e,r])=>{let o={name:e,title:r.title,description:r.description,inputSchema:(()=>{let n=fa(r.inputSchema);return n?c0(n,{strictUnions:!0,pipeStrategy:"input"}):rK})(),annotations:r.annotations,execution:r.execution,_meta:r._meta};if(r.outputSchema){let n=fa(r.outputSchema);n&&(o.outputSchema=c0(n,{strictUnions:!0,pipeStrategy:"output"}))}return o})})),this.server.setRequestHandler(ki,async(e,r)=>{try{let o=this._registeredTools[e.params.name];if(!o)throw new q(F.InvalidParams,`Tool ${e.params.name} not found`);if(!o.enabled)throw new q(F.InvalidParams,`Tool ${e.params.name} disabled`);let n=!!e.params.task,i=o.execution?.taskSupport,a="createTask"in o.handler;if((i==="required"||i==="optional")&&!a)throw new q(F.InternalError,`Tool ${e.params.name} has taskSupport '${i}' but was not registered with registerToolTask`);if(i==="required"&&!n)throw new q(F.MethodNotFound,`Tool ${e.params.name} requires task augmentation (taskSupport: 'required')`);if(i==="optional"&&!n&&a)return await this.handleAutomaticTaskPolling(o,e,r);let s=await this.validateToolInput(o,e.params.arguments,e.params.name),c=await this.executeToolHandler(o,s,r);return n||await this.validateToolOutput(o,c,e.params.name),c}catch(o){if(o instanceof q&&o.code===F.UrlElicitationRequired)throw o;return this.createToolError(o instanceof Error?o.message:String(o))}}),this._toolHandlersInitialized=!0)}createToolError(e){return{content:[{type:"text",text:e}],isError:!0}}async validateToolInput(e,r,o){if(!e.inputSchema)return;let i=fa(e.inputSchema)??e.inputSchema,a=await im(i,r);if(!a.success){let s="error"in a?a.error:"Unknown error",c=am(s);throw new q(F.InvalidParams,`Input validation error: Invalid arguments for tool ${o}: ${c}`)}return a.data}async validateToolOutput(e,r,o){if(!e.outputSchema||!("content"in r)||r.isError)return;if(!r.structuredContent)throw new q(F.InvalidParams,`Output validation error: Tool ${o} has an output schema but no structured content was provided`);let n=fa(e.outputSchema),i=await im(n,r.structuredContent);if(!i.success){let a="error"in i?i.error:"Unknown error",s=am(a);throw new q(F.InvalidParams,`Output validation error: Invalid structured content for tool ${o}: ${s}`)}}async executeToolHandler(e,r,o){let n=e.handler;if("createTask"in n){if(!o.taskStore)throw new Error("No task store provided.");let a={...o,taskStore:o.taskStore};if(e.inputSchema){let s=n;return await Promise.resolve(s.createTask(r,a))}else{let s=n;return await Promise.resolve(s.createTask(a))}}if(e.inputSchema){let a=n;return await Promise.resolve(a(r,o))}else{let a=n;return await Promise.resolve(a(o))}}async handleAutomaticTaskPolling(e,r,o){if(!o.taskStore)throw new Error("No task store provided for task-capable tool.");let n=await this.validateToolInput(e,r.params.arguments,r.params.name),i=e.handler,a={...o,taskStore:o.taskStore},s=n?await Promise.resolve(i.createTask(n,a)):await Promise.resolve(i.createTask(a)),c=s.task.taskId,l=s.task,u=l.pollInterval??5e3;for(;l.status!=="completed"&&l.status!=="failed"&&l.status!=="cancelled";){await new Promise(d=>setTimeout(d,u));let p=await o.taskStore.getTask(c);if(!p)throw new q(F.InternalError,`Task ${c} not found during polling`);l=p}return await o.taskStore.getTaskResult(c)}setCompletionRequestHandler(){this._completionHandlerInitialized||(this.server.assertCanSetRequestHandler(Jn(Bd)),this.server.registerCapabilities({completions:{}}),this.server.setRequestHandler(Bd,async e=>{switch(e.params.ref.type){case"ref/prompt":return Gk(e),this.handlePromptCompletion(e,e.params.ref);case"ref/resource":return Hk(e),this.handleResourceCompletion(e,e.params.ref);default:throw new q(F.InvalidParams,`Invalid completion reference: ${e.params.ref}`)}}),this._completionHandlerInitialized=!0)}async handlePromptCompletion(e,r){let o=this._registeredPrompts[r.name];if(!o)throw new q(F.InvalidParams,`Prompt ${r.name} not found`);if(!o.enabled)throw new q(F.InvalidParams,`Prompt ${r.name} disabled`);if(!o.argsSchema)return ll;let i=Mn(o.argsSchema)?.[e.params.argument.name];if(!nw(i))return ll;let a=m6(i);if(!a)return ll;let s=await a(e.params.argument.value,e.params.context);return g6(s)}async handleResourceCompletion(e,r){let o=Object.values(this._registeredResourceTemplates).find(a=>a.resourceTemplate.uriTemplate.toString()===r.uri);if(!o){if(this._registeredResources[r.uri])return ll;throw new q(F.InvalidParams,`Resource template ${e.params.ref.uri} not found`)}let n=o.resourceTemplate.completeCallback(e.params.argument.name);if(!n)return ll;let i=await n(e.params.argument.value,e.params.context);return g6(i)}setResourceRequestHandlers(){this._resourceHandlersInitialized||(this.server.assertCanSetRequestHandler(Jn(qd)),this.server.assertCanSetRequestHandler(Jn(Ud)),this.server.assertCanSetRequestHandler(Jn(Zd)),this.server.registerCapabilities({resources:{listChanged:!0}}),this.server.setRequestHandler(qd,async(e,r)=>{let o=Object.entries(this._registeredResources).filter(([i,a])=>a.enabled).map(([i,a])=>({uri:i,name:a.name,...a.metadata})),n=[];for(let i of Object.values(this._registeredResourceTemplates)){if(!i.resourceTemplate.listCallback)continue;let a=await i.resourceTemplate.listCallback(r);for(let s of a.resources)n.push({...i.metadata,...s})}return{resources:[...o,...n]}}),this.server.setRequestHandler(Ud,async()=>({resourceTemplates:Object.entries(this._registeredResourceTemplates).map(([r,o])=>({name:r,uriTemplate:o.resourceTemplate.uriTemplate.toString(),...o.metadata}))})),this.server.setRequestHandler(Zd,async(e,r)=>{let o=new URL(e.params.uri),n=this._registeredResources[o.toString()];if(n){if(!n.enabled)throw new q(F.InvalidParams,`Resource ${o} disabled`);return n.readCallback(o,r)}for(let i of Object.values(this._registeredResourceTemplates)){let a=i.resourceTemplate.uriTemplate.match(o.toString());if(a)return i.readCallback(o,a,r)}throw new q(F.InvalidParams,`Resource ${o} not found`)}),this._resourceHandlersInitialized=!0)}setPromptRequestHandlers(){this._promptHandlersInitialized||(this.server.assertCanSetRequestHandler(Jn(Fd)),this.server.assertCanSetRequestHandler(Jn(Vd)),this.server.registerCapabilities({prompts:{listChanged:!0}}),this.server.setRequestHandler(Fd,()=>({prompts:Object.entries(this._registeredPrompts).filter(([,e])=>e.enabled).map(([e,r])=>({name:e,title:r.title,description:r.description,arguments:r.argsSchema?nK(r.argsSchema):void 0}))})),this.server.setRequestHandler(Vd,async(e,r)=>{let o=this._registeredPrompts[e.params.name];if(!o)throw new q(F.InvalidParams,`Prompt ${e.params.name} not found`);if(!o.enabled)throw new q(F.InvalidParams,`Prompt ${e.params.name} disabled`);if(o.argsSchema){let n=fa(o.argsSchema),i=await im(n,e.params.arguments);if(!i.success){let c="error"in i?i.error:"Unknown error",l=am(c);throw new q(F.InvalidParams,`Invalid arguments for prompt ${e.params.name}: ${l}`)}let a=i.data,s=o.callback;return await Promise.resolve(s(a,r))}else{let n=o.callback;return await Promise.resolve(n(r))}}),this._promptHandlersInitialized=!0)}resource(e,r,...o){let n;typeof o[0]=="object"&&(n=o.shift());let i=o[0];if(typeof r=="string"){if(this._registeredResources[r])throw new Error(`Resource ${r} is already registered`);let a=this._createRegisteredResource(e,void 0,r,n,i);return this.setResourceRequestHandlers(),this.sendResourceListChanged(),a}else{if(this._registeredResourceTemplates[e])throw new Error(`Resource template ${e} is already registered`);let a=this._createRegisteredResourceTemplate(e,void 0,r,n,i);return this.setResourceRequestHandlers(),this.sendResourceListChanged(),a}}registerResource(e,r,o,n){if(typeof r=="string"){if(this._registeredResources[r])throw new Error(`Resource ${r} is already registered`);let i=this._createRegisteredResource(e,o.title,r,o,n);return this.setResourceRequestHandlers(),this.sendResourceListChanged(),i}else{if(this._registeredResourceTemplates[e])throw new Error(`Resource template ${e} is already registered`);let i=this._createRegisteredResourceTemplate(e,o.title,r,o,n);return this.setResourceRequestHandlers(),this.sendResourceListChanged(),i}}_createRegisteredResource(e,r,o,n,i){let a={name:e,title:r,metadata:n,readCallback:i,enabled:!0,disable:()=>a.update({enabled:!1}),enable:()=>a.update({enabled:!0}),remove:()=>a.update({uri:null}),update:s=>{typeof s.uri<"u"&&s.uri!==o&&(delete this._registeredResources[o],s.uri&&(this._registeredResources[s.uri]=a)),typeof s.name<"u"&&(a.name=s.name),typeof s.title<"u"&&(a.title=s.title),typeof s.metadata<"u"&&(a.metadata=s.metadata),typeof s.callback<"u"&&(a.readCallback=s.callback),typeof s.enabled<"u"&&(a.enabled=s.enabled),this.sendResourceListChanged()}};return this._registeredResources[o]=a,a}_createRegisteredResourceTemplate(e,r,o,n,i){let a={resourceTemplate:o,title:r,metadata:n,readCallback:i,enabled:!0,disable:()=>a.update({enabled:!1}),enable:()=>a.update({enabled:!0}),remove:()=>a.update({name:null}),update:l=>{typeof l.name<"u"&&l.name!==e&&(delete this._registeredResourceTemplates[e],l.name&&(this._registeredResourceTemplates[l.name]=a)),typeof l.title<"u"&&(a.title=l.title),typeof l.template<"u"&&(a.resourceTemplate=l.template),typeof l.metadata<"u"&&(a.metadata=l.metadata),typeof l.callback<"u"&&(a.readCallback=l.callback),typeof l.enabled<"u"&&(a.enabled=l.enabled),this.sendResourceListChanged()}};this._registeredResourceTemplates[e]=a;let s=o.uriTemplate.variableNames;return Array.isArray(s)&&s.some(l=>!!o.completeCallback(l))&&this.setCompletionRequestHandler(),a}_createRegisteredPrompt(e,r,o,n,i){let a={title:r,description:o,argsSchema:n===void 0?void 0:Eo(n),callback:i,enabled:!0,disable:()=>a.update({enabled:!1}),enable:()=>a.update({enabled:!0}),remove:()=>a.update({name:null}),update:s=>{typeof s.name<"u"&&s.name!==e&&(delete this._registeredPrompts[e],s.name&&(this._registeredPrompts[s.name]=a)),typeof s.title<"u"&&(a.title=s.title),typeof s.description<"u"&&(a.description=s.description),typeof s.argsSchema<"u"&&(a.argsSchema=Eo(s.argsSchema)),typeof s.callback<"u"&&(a.callback=s.callback),typeof s.enabled<"u"&&(a.enabled=s.enabled),this.sendPromptListChanged()}};return this._registeredPrompts[e]=a,n&&Object.values(n).some(c=>{let l=c instanceof bi?c._def?.innerType:c;return nw(l)})&&this.setCompletionRequestHandler(),a}_createRegisteredTool(e,r,o,n,i,a,s,c,l){ow(e);let u={title:r,description:o,inputSchema:h6(n),outputSchema:h6(i),annotations:a,execution:s,_meta:c,handler:l,enabled:!0,disable:()=>u.update({enabled:!1}),enable:()=>u.update({enabled:!0}),remove:()=>u.update({name:null}),update:p=>{typeof p.name<"u"&&p.name!==e&&(typeof p.name=="string"&&ow(p.name),delete this._registeredTools[e],p.name&&(this._registeredTools[p.name]=u)),typeof p.title<"u"&&(u.title=p.title),typeof p.description<"u"&&(u.description=p.description),typeof p.paramsSchema<"u"&&(u.inputSchema=Eo(p.paramsSchema)),typeof p.outputSchema<"u"&&(u.outputSchema=Eo(p.outputSchema)),typeof p.callback<"u"&&(u.handler=p.callback),typeof p.annotations<"u"&&(u.annotations=p.annotations),typeof p._meta<"u"&&(u._meta=p._meta),typeof p.enabled<"u"&&(u.enabled=p.enabled),this.sendToolListChanged()}};return this._registeredTools[e]=u,this.setToolRequestHandlers(),this.sendToolListChanged(),u}tool(e,...r){if(this._registeredTools[e])throw new Error(`Tool ${e} is already registered`);let o,n,i,a;if(typeof r[0]=="string"&&(o=r.shift()),r.length>1){let c=r[0];if(iw(c))n=r.shift(),r.length>1&&typeof r[0]=="object"&&r[0]!==null&&!iw(r[0])&&(a=r.shift());else if(typeof c=="object"&&c!==null){if(Object.values(c).some(l=>typeof l=="object"&&l!==null))throw new Error(`Tool ${e} expected a Zod schema or ToolAnnotations, but received an unrecognized object`);a=r.shift()}}let s=r[0];return this._createRegisteredTool(e,void 0,o,n,i,a,{taskSupport:"forbidden"},void 0,s)}registerTool(e,r,o){if(this._registeredTools[e])throw new Error(`Tool ${e} is already registered`);let{title:n,description:i,inputSchema:a,outputSchema:s,annotations:c,_meta:l}=r;return this._createRegisteredTool(e,n,i,a,s,c,{taskSupport:"forbidden"},l,o)}prompt(e,...r){if(this._registeredPrompts[e])throw new Error(`Prompt ${e} is already registered`);let o;typeof r[0]=="string"&&(o=r.shift());let n;r.length>1&&(n=r.shift());let i=r[0],a=this._createRegisteredPrompt(e,void 0,o,n,i);return this.setPromptRequestHandlers(),this.sendPromptListChanged(),a}registerPrompt(e,r,o){if(this._registeredPrompts[e])throw new Error(`Prompt ${e} is already registered`);let{title:n,description:i,argsSchema:a}=r,s=this._createRegisteredPrompt(e,n,i,a,o);return this.setPromptRequestHandlers(),this.sendPromptListChanged(),s}isConnected(){return this.server.transport!==void 0}async sendLoggingMessage(e,r){return this.server.sendLoggingMessage(e,r)}sendResourceListChanged(){this.isConnected()&&this.server.sendResourceListChanged()}sendToolListChanged(){this.isConnected()&&this.server.sendToolListChanged()}sendPromptListChanged(){this.isConnected()&&this.server.sendPromptListChanged()}};var rK={type:"object",properties:{}};function v6(t){return t!==null&&typeof t=="object"&&"parse"in t&&typeof t.parse=="function"&&"safeParse"in t&&typeof t.safeParse=="function"}function y6(t){return"_def"in t||"_zod"in t||v6(t)}function iw(t){return typeof t!="object"||t===null||y6(t)?!1:Object.keys(t).length===0?!0:Object.values(t).some(v6)}function h6(t){if(t){if(iw(t))return Eo(t);if(!y6(t))throw new Error("inputSchema must be a Zod schema or raw shape, received an unrecognized object");return t}}function nK(t){let e=Mn(t);return e?Object.entries(e).map(([r,o])=>{let n=U4(o),i=Z4(o);return{name:r,description:n,required:!i}}):[]}function Jn(t){let r=Mn(t)?.method;if(!r)throw new Error("Schema is missing a method literal");let o=sm(r);if(typeof o=="string")return o;throw new Error("Schema method literal must be a string")}function g6(t){return{completion:{values:t.slice(0,100),total:t.length,hasMore:t.length>100}}}var ll={completion:{values:[],hasMore:!1}};import{Readable as x6}from"stream";import uK from"crypto";var oK=global.Request,cw=class extends oK{constructor(t,e){typeof t=="object"&&Oa in t&&(t=t[Oa]()),typeof e?.body?.getReader<"u"&&(e.duplex??="half"),super(t,e)}},iK=t=>{let e=[],r=t.rawHeaders;for(let o=0;o<r.length;o+=2){let{[o]:n,[o+1]:i}=r;n.charCodeAt(0)!==58&&e.push([n,i])}return new Headers(e)},aK=Symbol("wrapBodyStream"),sK=(t,e,r,o,n)=>{let i={method:t,headers:r,signal:n.signal};if(t==="TRACE"){i.method="GET";let a=new cw(e,i);return Object.defineProperty(a,"method",{get(){return"TRACE"}}),a}if(!(t==="GET"||t==="HEAD"))if("rawBody"in o&&o.rawBody instanceof Buffer)i.body=new ReadableStream({start(a){a.enqueue(o.rawBody),a.close()}});else if(o[aK]){let a;i.body=new ReadableStream({async pull(s){try{a||=x6.toWeb(o).getReader();let{done:c,value:l}=await a.read();c?s.close():s.enqueue(l)}catch(c){s.error(c)}}})}else i.body=x6.toWeb(o);return new cw(e,i)},Oa=Symbol("getRequestCache"),_6=Symbol("requestCache"),aw=Symbol("incomingKey"),b6=Symbol("urlKey"),cK=Symbol("headersKey"),sw=Symbol("abortControllerKey"),lK=Symbol("getAbortController"),Ym={get method(){return this[aw].method||"GET"},get url(){return this[b6]},get headers(){return this[cK]||=iK(this[aw])},[lK](){return this[Oa](),this[sw]},[Oa](){return this[sw]||=new AbortController,this[_6]||=sK(this.method,this[b6],this.headers,this[aw],this[sw])}};["body","bodyUsed","cache","credentials","destination","integrity","mode","redirect","referrer","referrerPolicy","signal","keepalive"].forEach(t=>{Object.defineProperty(Ym,t,{get(){return this[Oa]()[t]}})});["arrayBuffer","blob","clone","formData","json","text"].forEach(t=>{Object.defineProperty(Ym,t,{value:function(){return this[Oa]()[t]()}})});Object.defineProperty(Ym,Symbol.for("nodejs.util.inspect.custom"),{value:function(t,e,r){let o={method:this.method,url:this.url,headers:this.headers,nativeRequest:this[_6]};return`Request (lightweight) ${r(o,{...e,depth:t==null?null:t-1})}`}});Object.setPrototypeOf(Ym,cw.prototype);var lw=Symbol("responseCache"),za=Symbol("getResponseCache"),Qm=Symbol("cache"),uw=global.Response,ul=class w6{#t;#e;[za](){return delete this[Qm],this[lw]||=new uw(this.#t,this.#e)}constructor(e,r){let o;if(this.#t=e,r instanceof w6){let n=r[lw];if(n){this.#e=n,this[za]();return}else this.#e=r.#e,o=new Headers(r.#e.headers)}else this.#e=r;(typeof e=="string"||typeof e?.getReader<"u"||e instanceof Blob||e instanceof Uint8Array)&&(this[Qm]=[r?.status||200,e,o||r?.headers])}get headers(){let e=this[Qm];return e?(e[2]instanceof Headers||(e[2]=new Headers(e[2]||{"content-type":"text/plain; charset=UTF-8"})),e[2]):this[za]().headers}get status(){return this[Qm]?.[0]??this[za]().status}get ok(){let e=this.status;return e>=200&&e<300}};["body","bodyUsed","redirected","statusText","trailers","type","url"].forEach(t=>{Object.defineProperty(ul.prototype,t,{get(){return this[za]()[t]}})});["arrayBuffer","blob","clone","formData","json","text"].forEach(t=>{Object.defineProperty(ul.prototype,t,{value:function(){return this[za]()[t]()}})});Object.defineProperty(ul.prototype,Symbol.for("nodejs.util.inspect.custom"),{value:function(t,e,r){let o={status:this.status,headers:this.headers,ok:this.ok,nativeResponse:this[lw]};return`Response (lightweight) ${r(o,{...e,depth:t==null?null:t-1})}`}});Object.setPrototypeOf(ul,uw);Object.setPrototypeOf(ul.prototype,uw.prototype);typeof global.crypto>"u"&&(global.crypto=uK);var fle=64*1024*1024;var pl=class extends Error{status;path;bodyText;constructor(e){super(`upstream request failed: ${e.status} ${e.path}`),this.status=e.status,this.path=e.path,this.bodyText=e.bodyText}},eh=class{baseUrl;cookieHeader;authHeaderName;authHeaderValue;constructor(e){this.baseUrl=e.apiBaseUrl,this.cookieHeader=e.apiCookieHeader,this.authHeaderName=e.apiHeaderName,this.authHeaderValue=e.apiHeaderValue}describeAuthMode(){return this.authHeaderName&&this.authHeaderValue?`header:${this.authHeaderName}`:this.cookieHeader?"cookie":"none"}getBaseUrl(){return this.baseUrl}async getJson(e){return this.requestJson("GET",e)}async postJson(e,r){return this.requestJson("POST",e,r)}async putJson(e,r){return this.requestJson("PUT",e,r)}buildHeaders(e){let r=new Headers({Accept:"application/json"});return(e==="POST"||e==="PUT")&&r.set("Content-Type","application/json"),this.cookieHeader&&r.set("Cookie",this.cookieHeader),this.authHeaderName&&this.authHeaderValue&&r.set(this.authHeaderName,this.authHeaderValue),r}async requestJson(e,r,o){let n=await fetch(new URL(r,this.baseUrl),{method:e,headers:this.buildHeaders(e),body:e==="POST"||e==="PUT"?JSON.stringify(o??null):void 0});if(!n.ok){let i=await n.text().catch(()=>null);throw new pl({status:n.status,path:r,bodyText:i})}return await n.json()}};import{existsSync as P6}from"node:fs";import{readFile as IK}from"node:fs/promises";import mw from"node:path";import{randomUUID as dK}from"node:crypto";import{mkdir as fK,readFile as mK,stat as S6,writeFile as hK}from"node:fs/promises";import Kn from"node:path";var fl=class extends Error{targetRef;expectedVersion;actualVersion;constructor(e){super("graph mutation version conflict"),this.name="LocalGraphMutationVersionConflictError",this.targetRef=e.targetRef??null,this.expectedVersion=e.expectedVersion??null,this.actualVersion=e.actualVersion??null}},gK=new Date(0).toISOString(),$6=[".seojeom/graph/canonical-slice.json",".seojeom/canonical-graph-slice.json",".seojeom/graph/canonical-slice.json",".seojeom/canonical-graph-slice.json"],k6={create_node:10,create_edge:20},vK={character:["character_state_packet"],organization:["organization_state_packet"],story_root:["story_engine_control"],arc:["arc_control"],episode:["episode_control"],world_rule:["world_rule_packet"]};function je(t){return!t||Array.isArray(t)||typeof t!="object"?{}:t}function U(t,e=""){return typeof t=="string"?t:e}function O(t){return typeof t=="string"?t:null}function V(t){return Array.isArray(t)?t.filter(e=>typeof e=="string"):[]}function kt(t){return typeof t=="number"&&Number.isFinite(t)?t:null}function fw(t,e=!1){return typeof t=="boolean"?t:e}function qo(t){return!t||Array.isArray(t)||typeof t!="object"?null:t}function yK(t){return t.replace(/\\/g,"/")}function ur(t){return t.trim().toLowerCase().normalize("NFKC").replace(/[^\p{Letter}\p{Number}]+/gu,"-").replace(/^-+|-+$/g,"")||dK().slice(0,8)}function pw(t,e){if(!t.has(e))return t.add(e),e;let r=2;for(;t.has(`${e}-${r}`);)r+=1;let o=`${e}-${r}`;return t.add(o),o}function th(t){return[...t].sort((e,r)=>(r.updatedAt??"").localeCompare(e.updatedAt??""))}function dl(t){return typeof t=="number"&&Number.isFinite(t)?t+1:1}function dw(t){throw new fl(t)}function xK(){return{version:{versionToken:"local-snapshot:empty",snapshotAt:gK},nodes:[],edges:[],packets:[]}}function bK(t){let e=je(t),r=U(e.id).trim(),o=U(e.label).trim(),n=U(e.type).trim(),i=U(e.plane).trim();if(!r||!o||!n||!i)return null;let a=je(e.meta),s=e.attributes?je(e.attributes):je(a.attributes),c=e.facets?je(e.facets):je(a.facets);return{id:r,currentVersion:kt(e.currentVersion),type:n,plane:i,nodeClass:U(e.nodeClass,"aggregate_root"),primaryDimension:U(e.primaryDimension,"canon_entity"),label:o,aliases:V(e.aliases),summary:U(e.summary),authorityLevel:U(e.authorityLevel,"user"),typePath:V(e.typePath),referenceRefIds:V(e.referenceRefIds),evidenceRefIds:V(e.evidenceRefIds),description:O(e.description),scopeLevel:O(e.scopeLevel),scopeRef:O(e.scopeRef),status:O(e.status),sourceRef:O(e.sourceRef),sourceTurnRefs:V(e.sourceTurnRefs),sourceMessageRefs:V(e.sourceMessageRefs),sourceAtomRefs:V(e.sourceAtomRefs),ownerDocumentId:O(e.ownerDocumentId),updatedAt:O(e.updatedAt),timeline:e.timeline?je(e.timeline):null,attributes:s,facets:c,meta:a}}function _K(t){let e=je(t),r=U(e.id).trim(),o=U(e.sourceRef).trim(),n=U(e.targetRef).trim(),i=U(e.relationType).trim();return!r||!o||!n||!i?null:{id:r,currentVersion:kt(e.currentVersion),sourceRef:o,targetRef:n,relationType:i,relationFamily:U(e.relationFamily,"derived"),primaryDimension:U(e.primaryDimension,"canon_entity"),sourceRole:U(e.sourceRole,"source"),targetRole:U(e.targetRole,"target"),directional:fw(e.directional,!0),authorityLevel:U(e.authorityLevel,"user"),summary:O(e.summary),updatedAt:O(e.updatedAt),timeline:e.timeline?je(e.timeline):null,evidenceRefIds:V(e.evidenceRefIds),properties:qo(e.properties),scopeLevel:O(e.scopeLevel),scopeRef:O(e.scopeRef)}}function wK(t){let e=je(t),r=U(e.id).trim(),o=U(e.ref).trim(),n=U(e.ownerRef).trim(),i=U(e.scopeRef).trim();return!r||!o||!n||!i?null:{id:r,currentVersion:kt(e.currentVersion),ref:o,packetKind:U(e.packetKind,"state"),ownerRef:n,scopeLevel:U(e.scopeLevel,"episode"),scopeRef:i,authorityLevel:U(e.authorityLevel,"user"),state:U(e.state,"active"),summaryText:O(e.summaryText),payload:je(e.payload),evidenceRefIds:V(e.evidenceRefIds),updatedAt:O(e.updatedAt)}}function SK(t){let e=je(JSON.parse(t)),r=je(e.version);return{version:{versionToken:U(r.versionToken,"local-snapshot"),snapshotAt:U(r.snapshotAt,new Date().toISOString())},nodes:Array.isArray(e.nodes)?e.nodes.map(o=>bK(o)).filter(o=>o!==null):[],edges:Array.isArray(e.edges)?e.edges.map(o=>_K(o)).filter(o=>o!==null):[],packets:Array.isArray(e.packets)?e.packets.map(o=>wK(o)).filter(o=>o!==null):[]}}function $K(t,e){let r=e?.trim().toLowerCase();return r?t.filter(o=>[o.id,o.label,o.type,o.summary,o.description??"",o.sourceRef??"",...o.aliases].join(`
107
107
  `).toLowerCase().includes(r)):t}function kK(t,e){return e==null?t:{version:t.version,nodes:t.nodes.filter(r=>{let o=kt(r.timeline?.anchorEpisodeNumber);return o==null||o<=e}),edges:t.edges.filter(r=>{let o=kt(r.timeline?.validFromEpisode),n=kt(r.timeline?.validUntilEpisode);return!(o!=null&&o>e||n!=null&&n<e)}),packets:t.packets}}function PK(t,e,r,o){let n=new Set(r),i=new Set(r);for(let s=0;s<o&&i.size!==0;s+=1){let c=new Set;for(let l of e)i.has(l.sourceRef)&&!n.has(l.targetRef)&&c.add(l.targetRef),i.has(l.targetRef)&&!n.has(l.sourceRef)&&c.add(l.sourceRef);for(let l of c)n.add(l);i=c}let a=new Set(t.map(s=>s.id).filter(s=>n.has(s)));return{nodes:t.filter(s=>a.has(s.id)),edges:e.filter(s=>a.has(s.sourceRef)&&a.has(s.targetRef)),allowedRefs:a}}var Aa=class{staticProjectId;staticProjectRoot;rootsService;projectRuntime;get projectId(){if(this.projectRuntime){let e=this.projectRuntime.getProjectId();if(!e)throw new Error(Tt);return e}return this.staticProjectId}get projectRoot(){if(this.projectRuntime&&!this.projectRuntime.getSnapshot().projectRoot)throw new Error(Tt);return this.rootsService?this.rootsService.getSnapshot().fsPath:this.staticProjectRoot}explicitSlicePath;constructor(e){this.staticProjectId=e.projectId,this.staticProjectRoot=e.projectRoot,this.rootsService=e.rootsService??null,this.projectRuntime=e.projectRuntime??null,this.explicitSlicePath=e.graphSlicePath?.trim()??null}async getResolvedSnapshotPath(){if(this.explicitSlicePath){let e=Kn.isAbsolute(this.explicitSlicePath)?this.explicitSlicePath:Kn.resolve(this.projectRoot,this.explicitSlicePath);if(!(await S6(e).catch(()=>null))?.isFile())throw new Error(`local graph snapshot not found: ${e}`);return e}for(let e of $6){let r=Kn.resolve(this.projectRoot,e);if((await S6(r).catch(()=>null))?.isFile())return r}return null}async getWritableSnapshotPath(){let e=await this.getResolvedSnapshotPath();return e||(this.explicitSlicePath?Kn.isAbsolute(this.explicitSlicePath)?this.explicitSlicePath:Kn.resolve(this.projectRoot,this.explicitSlicePath):Kn.resolve(this.projectRoot,$6[0]))}async loadSlice(){let e=await this.getResolvedSnapshotPath();if(!e)return xK();let r=await mK(e,"utf8");return SK(r)}async writeSlice(e,r){await fK(Kn.dirname(e),{recursive:!0}),await hK(e,`${JSON.stringify(r,null,2)}
108
- `,"utf8")}resolveNodeRefFromPayload(e,r){if(r.nodeId?.trim()){let a=e.nodes.find(s=>s.id===r.nodeId);if(!a)throw new Error(`${r.role} node not found in local graph snapshot: ${r.nodeId}`);return a.id}let o=r.nodeLabel?.trim();if(!o)throw new Error(`${r.role} node reference is missing`);let n=e.nodes.filter(a=>a.label.trim()===o);if(n.length===1)return n[0].id;if(n.length>1)throw new Error(`${r.role} node label is ambiguous in local graph snapshot: ${o}`);let i=e.nodes.filter(a=>a.label.trim().toLowerCase()===o.toLowerCase());if(i.length===1)return i[0].id;throw i.length>1?new Error(`${r.role} node label is ambiguous in local graph snapshot: ${o}`):new Error(`${r.role} node label not found in local graph snapshot: ${o}`)}resolveNodeIndex(e,r){let o=e.nodes.findIndex(n=>n.id===r);if(o===-1)throw new Error(`local graph node not found: ${r}`);return o}resolveEdgeIndex(e,r){let o=e.edges.findIndex(n=>n.id===r);if(o===-1)throw new Error(`local graph edge not found: ${r}`);return o}ensureNodeBaseVersion(e,r){typeof r=="number"&&typeof e.currentVersion=="number"&&r!==e.currentVersion&&dw({targetRef:e.id,expectedVersion:r,actualVersion:e.currentVersion})}ensureEdgeBaseVersion(e,r){typeof r=="number"&&typeof e.currentVersion=="number"&&r!==e.currentVersion&&dw({targetRef:e.id,expectedVersion:r,actualVersion:e.currentVersion})}resolvePacketIndex(e,r){let o=e.packets.findIndex(n=>n.ref===r);if(o===-1)throw new Error(`local graph packet not found: ${r}`);return o}ensurePacketBaseVersion(e,r){typeof r=="number"&&typeof e.currentVersion=="number"&&r!==e.currentVersion&&dw({targetRef:e.ref,expectedVersion:r,actualVersion:e.currentVersion})}resolvePacketKindForNodeType(e,r){let o=vK[e]??[],n=O(r.packetKind);if(n){if(o.includes(n))return n;throw new Error(`local graph packet kind ${n} is not supported for node type: ${e}`)}return o[0]??null}resolvePacketScopeRef(e){let r=O(e.scopeRef);if(r)return r;let o=O(e.scopeEpisodeId);if(o)return`episode:${o}`;let n=U(e.scopeLevel).trim();if(!n)throw new Error("local graph packet mutation is missing packet.scopeLevel");return`scope:${n}`}buildPacketRef(e){let r=U(e.packet.scopeLevel).trim();if(!r)throw new Error("local graph packet mutation is missing packet.scopeLevel");let o=this.resolvePacketScopeRef(e.packet),n=e.nodeRef.includes(":")?e.nodeRef.slice(e.nodeRef.indexOf(":")+1):e.nodeRef;return`${e.packetKind}:${n}:${r}:${o}`}buildPacketSummary(e,r){if(e==="story_engine_control")return[O(r.summaryText),O(r.corePromise),O(r.soloClimbContract),O(r.thematicContradiction),O(je(r.endingAnchor).readerFinalQuestion)].filter(s=>!!s).join(" | ")||"story engine control";if(e==="arc_control")return[O(r.summaryText),O(r.arcQuestion),O(r.arcReversal),O(r.episodeRange),O(r.obligationSummary)].filter(s=>!!s).join(" | ")||"arc control";if(e==="episode_control")return[O(r.summaryText),O(r.obligationSummary),O(r.coreLoopFocus),O(r.emotionalTarget)].filter(s=>!!s).join(" | ")||"episode control";if(e==="world_rule_packet")return[O(r.summaryText),O(r.coreStatement),O(r.narrativePurpose)].filter(s=>!!s).join(" | ")||"world rule packet";let o=O(r.summaryText),n=O(r.emotionalBaseline);return[o,n&&!o?.includes(`\uAC10\uC815: ${n}`)?`\uAC10\uC815: ${n}`:null,O(r.currentLocationRef)?`\uC7A5\uC18C: ${O(r.currentLocationRef)}`:null,O(r.headquartersRef)?`\uAC70\uC810: ${O(r.headquartersRef)}`:null].filter(a=>!!a).join(" | ")||"state packet"}buildPacketPayload(e,r,o){return r==="story_engine_control"?{corePromise:O(o.corePromise),soloClimbContract:O(o.soloClimbContract),arcProgressionBeats:V(o.arcProgressionBeats),externalReactionCadence:V(o.externalReactionCadence),careLoopCadence:V(o.careLoopCadence),revealLadderPriority:V(o.revealLadderPriority),thematicContradiction:O(o.thematicContradiction),endingAnchor:je(o.endingAnchor)}:r==="arc_control"?{arcQuestion:O(o.arcQuestion),arcReversal:O(o.arcReversal),payoffTargets:V(o.payoffTargets),obligationSummary:O(o.obligationSummary),episodeRange:O(o.episodeRange),episodeBeatSheet:V(o.episodeBeatSheet),towerProgressionStep:O(o.towerProgressionStep),externalPressureLine:O(o.externalPressureLine),softBeatQuota:O(o.softBeatQuota)}:r==="episode_control"?{requiredCallbacks:V(o.requiredCallbacks),mustPlant:V(o.mustPlant),mustPayoff:V(o.mustPayoff),revealQuota:kt(o.revealQuota),emotionalTarget:O(o.emotionalTarget),obligationSummary:O(o.obligationSummary),coreLoopFocus:O(o.coreLoopFocus),externalReactionMode:O(o.externalReactionMode),careBeatType:O(o.careBeatType),cliffType:O(o.cliffType)}:r==="world_rule_packet"?{publicSummary:O(o.publicSummary),coreStatement:O(o.coreStatement),mechanism:O(o.mechanism),knownLimitations:V(o.knownLimitations),hardLimits:V(o.hardLimits),costConditions:V(o.costConditions),sideEffects:V(o.sideEffects),knownExceptions:V(o.knownExceptions),suspectedLoopholes:V(o.suspectedLoopholes),secretExceptions:V(o.secretExceptions),narrativePurpose:O(o.narrativePurpose),conflictPotential:O(o.conflictPotential),characterImpact:O(o.characterImpact)}:e==="organization"?{headquartersRef:O(o.headquartersRef),activeTerritoryRefs:V(o.activeTerritoryRefs),activeGoalRefs:V(o.activeGoalRefs),activeConflictRefs:V(o.activeConflictRefs),currentPressureSources:V(o.currentPressureSources),cohesionStatus:O(o.cohesionStatus),publicStanding:O(o.publicStanding),anchorEpisodeNumber:null}:{currentLocationRef:O(o.currentLocationRef),currentAllegianceRef:O(o.currentAllegianceRef),activeGoalRefs:V(o.activeGoalRefs),activeConflictRefs:V(o.activeConflictRefs),emotionalBaseline:O(o.emotionalBaseline),activeSecretRefs:V(o.activeSecretRefs),recentRelationshipShifts:V(o.recentRelationshipShifts),currentPressureSources:V(o.currentPressureSources),voiceDriftNotes:V(o.voiceDriftNotes),audienceKnowledgeSplit:V(o.audienceKnowledgeSplit),lastUpdatedFromEvidenceRef:O(o.lastUpdatedFromEvidenceRef),anchorEpisodeNumber:null}}applyCreateNodeItem(e,r,o){let n=je(r.payload),i=U(n.title).trim();if(!i)throw new Error(`local create_node payload is missing title for ${r.id}`);let a=U(n.nodeTypeHint,"note").trim()||"note",s=kt(n.atEpisode),c=a==="event"?"event":"entity",l=c==="event"||s!=null?"timeline":"canon_entity",u=new Set(e.nodes.map(d=>d.id)),p=pw(u,`${ur(a)}:${ur(i)}`);return e.nodes.push({id:p,type:a,plane:c,nodeClass:"aggregate_root",primaryDimension:l,label:i,aliases:[],summary:U(n.summary),authorityLevel:"approved_ai",typePath:[a],description:O(n.content),sourceRef:r.id,ownerDocumentId:null,updatedAt:o,timeline:s!=null?{anchorEpisodeNumber:s}:null,meta:{localProposalItemId:r.id,tags:V(n.tags),linkedEpisodeIds:V(n.linkedEpisodeIds),linkedCharacterIds:V(n.linkedCharacterIds),focusNodeIds:V(n.focusNodeIds),attributes:je(n.attributes)}}),p}applyCreateEdgeItem(e,r,o,n){let i=je(r.payload),a=this.resolveNodeRefFromPayload(e,{nodeId:O(i.sourceNodeId),nodeLabel:O(i.sourceLabel),role:"source"}),s=this.resolveNodeRefFromPayload(e,{nodeId:O(i.targetNodeId),nodeLabel:O(i.targetLabel),role:"target"}),c=U(i.edgeType,"related_to").trim()||"related_to",l=U(i.relationClass,"derived").trim()||"derived",u=kt(i.atEpisode)??n?.currentEpisodeNumber??null,p=new Set(e.edges.map(f=>f.id)),d=pw(p,`edge:${ur(a)}:${ur(c)}:${ur(s)}`);return e.edges.push({id:d,sourceRef:a,targetRef:s,relationType:c,relationFamily:l,primaryDimension:u!=null?"timeline":"canon_entity",sourceRole:l==="participation"?"participant":"source",targetRole:l==="participation"&&c==="participates_in"?"event":"target",directional:fw(i.isDirectional,!0),authorityLevel:"approved_ai",summary:O(i.summary)??O(i.content),updatedAt:o,timeline:u!=null||n?.triggerEpisodeId!=null||n?.factText!=null?{validFromEpisode:u,validUntilEpisode:null,triggerEpisodeId:n?.triggerEpisodeId??null,timeLabel:n?.factText??null}:null}),d}applyCreateNodeMutation(e,r,o){let n=je(r.node),i=U(n.label).trim();if(!i)throw new Error("local create_node mutation is missing node.label");let a=U(n.type,"note").trim()||"note",s=qo(n.timeline),c=U(n.layer).trim()||(a==="event"?"event":"entity"),l=c==="event"||s?"timeline":"canon_entity",u=new Set(e.nodes.map(d=>d.id)),p=pw(u,`${ur(a)}:${ur(i)}`);return e.nodes.push({id:p,currentVersion:1,type:a,plane:c,nodeClass:"aggregate_root",primaryDimension:l,label:i,aliases:V(n.tags),summary:U(n.summary),authorityLevel:"user",typePath:[a],referenceRefIds:[],evidenceRefIds:V(r.evidenceRefs),description:O(n.description),scopeLevel:O(s?.scopeLevel),scopeRef:null,status:null,sourceRef:null,sourceTurnRefs:V(r.sourceTurnRefs),sourceMessageRefs:V(r.sourceMessageRefs),sourceAtomRefs:V(r.sourceAtomRefs),ownerDocumentId:null,updatedAt:o,timeline:s,meta:{linkedEpisodeIds:V(n.linkedEpisodeIds),linkedCharacterIds:V(n.linkedCharacterIds),attributes:je(n.attributes),...n.facets?{facets:n.facets}:{},...O(n.currentStateRef)?{currentStateRef:O(n.currentStateRef)}:{}}}),{id:p,nodeRef:p,packetRef:null,sourceRef:null,targetRef:null,relationType:null,state:"created",timeline:null}}applyUpdateNodeMutation(e,r,o){let n=U(r.nodeId).trim();if(!n)throw new Error("local update_node mutation is missing nodeId");let i=je(r.patch),a=this.resolveNodeIndex(e,n),s=e.nodes[a];this.ensureNodeBaseVersion(s,kt(r.baseVersion)),U(i.label).trim()&&(s.label=U(i.label).trim()),"summary"in i&&(s.summary=U(i.summary)),"description"in i&&(s.description=O(i.description)),"tags"in i&&(s.aliases=V(i.tags)),s.meta=s.meta??{},"attributes"in i&&(s.attributes=je(i.attributes),s.meta.attributes=s.attributes),"facets"in i&&(s.facets=je(i.facets),s.meta.facets=s.facets);for(let c of["linkedEpisodeIds","linkedCharacterIds","category","role","statusTag","slug","lane","currentStateRef"])c in i&&(s.meta[c]=i[c]);return s.currentVersion=dl(s.currentVersion),s.updatedAt=o,{id:s.id,nodeRef:s.id,packetRef:null,sourceRef:null,targetRef:null,relationType:null,state:"updated",timeline:null}}applyPatchNodeTimelineMutation(e,r,o){let n=U(r.nodeId).trim();if(!n)throw new Error("local patch_node_timeline mutation is missing nodeId");let i=this.resolveNodeIndex(e,n),a=e.nodes[i];this.ensureNodeBaseVersion(a,kt(r.baseVersion));let s=r.timeline?qo(r.timeline):null;return a.scopeLevel=O(s?.scopeLevel),a.timeline=s,a.currentVersion=dl(a.currentVersion),a.updatedAt=o,{id:a.id,nodeRef:a.id,packetRef:null,sourceRef:null,targetRef:null,relationType:null,state:"timeline_patched",timeline:s}}applyDeleteNodeMutation(e,r){let o=U(r.nodeId).trim();if(!o)throw new Error("local delete_node mutation is missing nodeId");let n=this.resolveNodeIndex(e,o),i=e.nodes[n];this.ensureNodeBaseVersion(i,kt(r.baseVersion));let[a]=e.nodes.splice(n,1);return e.edges=e.edges.filter(s=>s.sourceRef!==a.id&&s.targetRef!==a.id),{id:a.id,nodeRef:a.id,packetRef:null,sourceRef:null,targetRef:null,relationType:null,state:"deleted",timeline:null}}applyCreateEdgeMutation(e,r,o){let n=je(r.edge),i=this.resolveNodeRefFromPayload(e,{nodeId:O(n.sourceNodeId),nodeLabel:null,role:"source"}),a=this.resolveNodeRefFromPayload(e,{nodeId:O(n.targetNodeId),nodeLabel:null,role:"target"}),s=U(n.edgeType,"related_to").trim()||"related_to",c=U(n.relationClass,"derived").trim()||"derived",l=U(n.id).trim()||`edge:${ur(i)}:${ur(s)}:${ur(a)}`;return e.edges.push({id:l,currentVersion:1,sourceRef:i,targetRef:a,relationType:s,relationFamily:c,primaryDimension:n.timeline?"timeline":"canon_entity",sourceRole:c==="participation"?"participant":"source",targetRole:c==="participation"&&s==="participates_in"?"event":"target",directional:fw(n.isDirectional,!0),authorityLevel:"user",summary:O(n.summary),updatedAt:o,timeline:qo(n.timeline),evidenceRefIds:V(n.evidenceRefs),properties:qo(n.properties),scopeLevel:null,scopeRef:null}),{id:l,nodeRef:null,packetRef:null,sourceRef:i,targetRef:a,relationType:s,state:"created",timeline:null}}applyUpdateEdgeMutation(e,r,o){let n=U(r.edgeId).trim();if(!n)throw new Error("local update_edge mutation is missing edgeId");let i=je(r.patch),a=typeof i.sourceNodeId=="string"?this.resolveNodeRefFromPayload(e,{nodeId:i.sourceNodeId,nodeLabel:null,role:"source"}):null,s=typeof i.targetNodeId=="string"?this.resolveNodeRefFromPayload(e,{nodeId:i.targetNodeId,nodeLabel:null,role:"target"}):null,c=this.resolveEdgeIndex(e,n),l=e.edges[c];return this.ensureEdgeBaseVersion(l,kt(r.baseVersion)),U(i.edgeType).trim()&&(l.relationType=U(i.edgeType).trim()),U(i.relationClass).trim()&&(l.relationFamily=U(i.relationClass).trim()),typeof i.isDirectional=="boolean"&&(l.directional=i.isDirectional),"summary"in i&&(l.summary=O(i.summary)),"properties"in i&&(l.properties=qo(i.properties)),"timeline"in i&&(l.timeline=i.timeline?qo(i.timeline):null),a&&(l.sourceRef=a),s&&(l.targetRef=s),l.currentVersion=dl(l.currentVersion),l.updatedAt=o,{id:l.id,nodeRef:null,packetRef:null,sourceRef:l.sourceRef,targetRef:l.targetRef,relationType:l.relationType,state:"updated",timeline:l.timeline}}applyDeleteEdgeMutation(e,r){let o=U(r.edgeId).trim();if(!o)throw new Error("local delete_edge mutation is missing edgeId");let n=this.resolveEdgeIndex(e,o),i=e.edges[n];this.ensureEdgeBaseVersion(i,kt(r.baseVersion));let[a]=e.edges.splice(n,1);return{id:a.id,nodeRef:null,packetRef:null,sourceRef:a.sourceRef,targetRef:a.targetRef,relationType:a.relationType,state:"deleted",timeline:a.timeline}}applyCreatePacketMutation(e,r,o){let n=U(r.nodeId).trim();if(!n)throw new Error("local create_packet mutation is missing nodeId");let i=this.resolveNodeIndex(e,n),a=e.nodes[i],s=je(r.packet),c=this.resolvePacketKindForNodeType(a.type,s);if(!c)throw new Error(`local graph packets are not supported for node type: ${a.type}`);let l=this.buildPacketRef({nodeRef:a.id,packetKind:c,packet:s}),u=this.buildPacketPayload(a.type,c,s),p=this.buildPacketSummary(c,s),d="evidenceRefs"in s?V(s.evidenceRefs):V(r.evidenceRefs),f=e.packets.findIndex(h=>h.ref===l);if(f>=0){let h=e.packets[f];return h.currentVersion=dl(h.currentVersion),h.packetKind=c,h.ownerRef=a.id,h.scopeLevel=U(s.scopeLevel,h.scopeLevel),h.scopeRef=this.resolvePacketScopeRef(s),h.authorityLevel="user",h.state="active",h.summaryText=p,h.payload=u,h.evidenceRefIds=d,h.updatedAt=o,a.meta=a.meta??{},a.meta.currentStateRef=h.ref,{id:h.ref,nodeRef:a.id,packetRef:h.ref,sourceRef:null,targetRef:null,relationType:null,state:"updated",timeline:null}}return e.packets.push({id:`packet:${ur(l)}`,currentVersion:1,ref:l,packetKind:c,ownerRef:a.id,scopeLevel:U(s.scopeLevel),scopeRef:this.resolvePacketScopeRef(s),authorityLevel:"user",state:"active",summaryText:p,payload:u,evidenceRefIds:d,updatedAt:o}),a.meta=a.meta??{},a.meta.currentStateRef=l,{id:l,nodeRef:a.id,packetRef:l,sourceRef:null,targetRef:null,relationType:null,state:"created",timeline:null}}applyUpdatePacketMutation(e,r,o){let n=U(r.nodeId).trim();if(!n)throw new Error("local update_packet mutation is missing nodeId");let i=this.resolveNodeIndex(e,n),a=e.nodes[i],s=je(r.packet),c=this.resolvePacketKindForNodeType(a.type,s);if(!c)throw new Error(`local graph packets are not supported for node type: ${a.type}`);let l=O(r.packetRef);if(l){let m=this.resolvePacketIndex(e,l),g=e.packets[m];this.ensurePacketBaseVersion(g,kt(r.baseVersion))}let u=this.buildPacketRef({nodeRef:a.id,packetKind:c,packet:s}),p=this.buildPacketPayload(a.type,c,s),d=this.buildPacketSummary(c,s),f="evidenceRefs"in s?V(s.evidenceRefs):V(r.evidenceRefs),h=e.packets.findIndex(m=>m.ref===u);if(h>=0){let m=e.packets[h];return m.currentVersion=dl(m.currentVersion),m.packetKind=c,m.ownerRef=a.id,m.scopeLevel=U(s.scopeLevel,m.scopeLevel),m.scopeRef=this.resolvePacketScopeRef(s),m.authorityLevel="user",m.state="active",m.summaryText=d,m.payload=p,m.evidenceRefIds=f,m.updatedAt=o,a.meta=a.meta??{},a.meta.currentStateRef=m.ref,{id:m.ref,nodeRef:a.id,packetRef:m.ref,sourceRef:null,targetRef:null,relationType:null,state:"updated",timeline:null}}return e.packets.push({id:`packet:${ur(u)}`,currentVersion:1,ref:u,packetKind:c,ownerRef:a.id,scopeLevel:U(s.scopeLevel),scopeRef:this.resolvePacketScopeRef(s),authorityLevel:"user",state:"active",summaryText:d,payload:p,evidenceRefIds:f,updatedAt:o}),a.meta=a.meta??{},a.meta.currentStateRef=u,{id:u,nodeRef:a.id,packetRef:u,sourceRef:null,targetRef:null,relationType:null,state:"created",timeline:null}}async applyMutation(e){let r=U(e.kind).trim();if(!r)throw new Error("local graph mutation is missing kind");let o=await this.getWritableSnapshotPath(),n=await this.loadSlice(),i=structuredClone(n),a=new Date().toISOString(),s;switch(r){case"create_node":s=this.applyCreateNodeMutation(i,e,a);break;case"update_node":s=this.applyUpdateNodeMutation(i,e,a);break;case"patch_node_timeline":s=this.applyPatchNodeTimelineMutation(i,e,a);break;case"delete_node":s=this.applyDeleteNodeMutation(i,e);break;case"create_edge":s=this.applyCreateEdgeMutation(i,e,a);break;case"create_packet":s=this.applyCreatePacketMutation(i,e,a);break;case"update_edge":s=this.applyUpdateEdgeMutation(i,e,a);break;case"update_packet":s=this.applyUpdatePacketMutation(i,e,a);break;case"delete_edge":s=this.applyDeleteEdgeMutation(i,e);break;default:throw new Error(`local graph mutation kind is not supported: ${r}`)}return i.version={versionToken:`local-snapshot:${a}`,snapshotAt:a},await this.writeSlice(o,i),{result:s,appliedAt:a,snapshotAt:i.version.snapshotAt,versionToken:i.version.versionToken,snapshotPath:o}}async applyProposalItems(e){let r=await this.getWritableSnapshotPath(),o=await this.loadSlice(),n=structuredClone(o),i=new Date().toISOString(),a={},s=[...e.items].sort((c,l)=>(k6[c.opKind]??Number.MAX_SAFE_INTEGER)-(k6[l.opKind]??Number.MAX_SAFE_INTEGER)||c.sequence-l.sequence);for(let c of s)switch(c.opKind){case"create_node":a[c.id]=this.applyCreateNodeItem(n,c,i);break;case"create_edge":a[c.id]=this.applyCreateEdgeItem(n,c,i,e.temporalContext);break;default:throw new Error(`local graph apply does not support proposal opKind: ${c.opKind}`)}return n.version={versionToken:`local-snapshot:${i}`,snapshotAt:i},await this.writeSlice(r,n),{appliedAt:i,snapshotAt:n.version.snapshotAt,versionToken:n.version.versionToken,snapshotPath:r,targetRefsByItemId:a}}async query(e){let r=await this.getResolvedSnapshotPath(),o=await this.loadSlice(),n=[],i=new Map(o.nodes.map(f=>[f.id,f]));for(let f of o.edges)(!i.has(f.sourceRef)||!i.has(f.targetRef))&&n.push(`Local graph edge ${f.id} references missing node refs (${f.sourceRef} -> ${f.targetRef}).`);let a=kK(o,e.filters?.atEpisode),s=a.nodes,c=a.edges,l=a.packets;if(e.filters?.nodeRefs?.length){let f=new Set(e.filters.nodeRefs);s=s.filter(h=>f.has(h.id)),c=c.filter(h=>f.has(h.sourceRef)||f.has(h.targetRef)),l=l.filter(h=>f.has(h.ownerRef)||f.has(h.scopeRef))}if(e.filters?.planes?.length){let f=new Set(e.filters.planes);s=s.filter(h=>f.has(h.plane))}if(e.filters?.nodeTypes?.length){let f=new Set(e.filters.nodeTypes);s=s.filter(h=>f.has(h.type))}s=$K(s,e.filters?.textSearch);let u=new Set(s.map(f=>f.id));c=c.filter(f=>u.has(f.sourceRef)||u.has(f.targetRef)),l=l.filter(f=>u.has(f.ownerRef)||u.has(f.scopeRef));let p=Array.from(new Set([...e.filters?.focusRef?[e.filters.focusRef]:[],...e.filters?.focusRefs??[]].filter(f=>typeof f=="string"&&f.trim().length>0)));if(p.length>0){let f=e.filters?.neighborDepth??(e.mode==="node_detail"?1:0),h=PK(s,c,p,f);s=h.nodes,c=h.edges,l=l.filter(m=>h.allowedRefs.has(m.ownerRef)||h.allowedRefs.has(m.scopeRef))}else c=c.filter(f=>u.has(f.sourceRef)&&u.has(f.targetRef));let d=e.pagination?.limit;if(typeof d=="number"&&d>0){let f=th(s),h=new Set(p),g=(h.size>0?[...f.filter($=>h.has($.id)),...f.filter($=>!h.has($.id))]:f).slice(0,d),b=new Set(g.map($=>$.id));s=g,c=c.filter($=>b.has($.sourceRef)&&b.has($.targetRef)),l=l.filter($=>b.has($.ownerRef)||b.has($.scopeRef))}else s=th(s),c=th(c),l=th(l);return{versionToken:o.version.versionToken,snapshotAt:o.version.snapshotAt,nodeCount:s.length,edgeCount:c.length,packetCount:l.length,nodes:e.include?.nodes===!1?[]:s,edges:e.include?.edges===!1?[]:c,packets:e.include?.packets===!1?[]:l,aggregateDetails:null,warnings:n,meta:{authority:"local-snapshot",readSource:"local-json",projectId:this.projectId,snapshotPath:r?yK(Kn.relative(this.projectRoot,r)):null,revisionHint:o.version.versionToken}}}};var RK=[0,50,100,200,400,800,1600];function EK(t){return!!(t&&typeof t=="object"&&!Array.isArray(t))}function jK(t){let e=mw.join(t,".seojeom"),r=mw.join(t,".ainovel");return P6(e)||!P6(r)?e:r}function rh(t){let e=t.nodes??[],r=t.edges??[],o=t.packets??[];return{versionToken:"versionToken"in t?t.versionToken:void 0,snapshotAt:"snapshotAt"in t?t.snapshotAt:void 0,nodeCount:t.nodeCount??e.length,edgeCount:t.edgeCount??r.length,packetCount:t.packetCount??o.length,nodes:e,edges:r,packets:o,aggregateDetails:t.aggregateDetails??void 0,meta:t.meta,warnings:t.warnings}}async function TK(t){await new Promise(e=>globalThis.setTimeout(e,t))}var nh=class{descriptorPath;fallbackStore;fetchFn;constructor(e){let r=jK(e.projectRoot);this.descriptorPath=mw.join(r,".graph-ipc.json"),this.fallbackStore=new Aa({projectId:e.projectId,projectRoot:e.projectRoot,graphSlicePath:e.graphSlicePath??null}),this.fetchFn=e.fetchFn??fetch}getDescriptorPath(){return this.descriptorPath}async getResolvedSnapshotPath(){return this.fallbackStore.getResolvedSnapshotPath()}async hasReadyDescriptor(){return await this.readDescriptorWithRetry()!=null}async getInfo(){let e=await this.readDescriptorWithRetry();if(!e)return null;try{return await this.requestJson(e,"/graph/info")}catch{return null}}async listNodes(e){return this.requestOrFallback("/graph/list",e,()=>this.fallbackStore.query({mode:"slice",filters:{nodeTypes:e.nodeTypes,planes:e.planes,textSearch:e.labelContains??null},include:{nodes:!0,edges:!1,packets:!1},pagination:{limit:e.limit??50}}))}async search(e){return this.requestOrFallback("/graph/search",e,()=>this.fallbackStore.query({mode:"slice",filters:{nodeTypes:e.nodeTypes,textSearch:e.query},include:{nodes:!0,edges:!0,packets:!0},pagination:{limit:e.limit??20}}))}async neighbors(e){return this.requestOrFallback("/graph/neighbors",e,()=>this.fallbackStore.query({mode:"slice",filters:{focusRef:e.startRef,focusRefs:[e.startRef],neighborDepth:1},include:{nodes:!0,edges:!0,packets:!0},pagination:{limit:100}}))}async traverse(e){return this.requestOrFallback("/graph/traverse",e,()=>this.fallbackStore.query({mode:"node_detail",filters:{focusRef:e.startRef,focusRefs:[e.startRef],neighborDepth:Math.min(Math.max(e.depth,0),2),textSearch:typeof e.filter?.textSearch=="string"?e.filter.textSearch:null,nodeTypes:Array.isArray(e.filter?.nodeTypes)?e.filter.nodeTypes.filter(r=>typeof r=="string"):void 0,atEpisode:typeof e.filter?.atEpisode=="number"?e.filter.atEpisode:null},include:{nodes:!0,edges:!0,packets:!0}}))}async query(e){let r=e.filters??{},o=Array.isArray(r.focusRefs)?r.focusRefs.filter(l=>typeof l=="string"&&l.length>0):typeof r.focusRef=="string"&&r.focusRef.length>0?[r.focusRef]:[],n=Array.isArray(r.nodeRefs)?r.nodeRefs.filter(l=>typeof l=="string"&&l.length>0):[],i=Array.isArray(r.planes)?r.planes.filter(l=>typeof l=="string"&&l.length>0):[],a=Array.isArray(r.nodeTypes)?r.nodeTypes.filter(l=>typeof l=="string"&&l.length>0):[],s=typeof r.textSearch=="string"&&r.textSearch.trim().length>0?r.textSearch:null;return o.length===1&&n.length===0&&i.length===0?this.requestOrFallback("/graph/traverse",{startRef:o[0],depth:Math.min(Math.max(r.neighborDepth??1,0),2),filter:{textSearch:s,nodeTypes:a,atEpisode:typeof r.atEpisode=="number"&&Number.isFinite(r.atEpisode)?r.atEpisode:null}},()=>this.fallbackStore.query(e)):rh(await this.fallbackStore.query(e))}async requestOrFallback(e,r,o){let n=await this.readDescriptorWithRetry();if(!n)return rh(await o());try{return rh(await this.requestJson(n,e,r))}catch{return rh(await o())}}async readDescriptorWithRetry(){for(let e of RK){e>0&&await TK(e);let r=await this.readDescriptorOnce();if(r)return r}return null}async readDescriptorOnce(){try{let e=await IK(this.descriptorPath,"utf8"),r=JSON.parse(e);return!EK(r)||r.protocol!=="http"||typeof r.host!="string"||typeof r.port!="number"||typeof r.bearerToken!="string"||typeof r.startedAt!="string"?null:{protocol:"http",host:r.host,port:r.port,bearerToken:r.bearerToken,startedAt:r.startedAt}}catch{return null}}async requestJson(e,r,o){let n=await this.fetchFn(new URL(r,`${e.protocol}://${e.host}:${e.port}`),{method:o===void 0?"GET":"POST",headers:{Accept:"application/json",Authorization:`Bearer ${e.bearerToken}`,...o===void 0?{}:{"Content-Type":"application/json"}},body:o===void 0?void 0:JSON.stringify(o)});if(!n.ok)throw new Error(`graph IPC request failed: ${n.status} ${r}`);return await n.json()}};import{mkdir as I6,readFile as zK,readdir as OK,rename as AK,stat as R6,writeFile as E6}from"node:fs/promises";import Ht from"node:path";var Uo="---";function CK(t){return t.replace(/_([a-z])/g,(e,r)=>r.toUpperCase())}function NK(t){return t.replace(/[A-Z]/g,e=>`_${e.toLowerCase()}`)}function N6(t){return t.replace(/\\/g,"/")}function D6(t){return t.trim().toLowerCase().replace(/[^\p{L}\p{N}]+/gu,"-").replace(/^-+|-+$/g,"")||"untitled"}function oh(t){return t.replace(/\.[^.]+$/u,"")}function j6(t){let e=t.trim();if(!e)return"";if(e==="null")return null;if(e==="true")return!0;if(e==="false")return!1;if(/^-?\d+(\.\d+)?$/.test(e))return Number(e);if(e.startsWith("{")&&e.endsWith("}")||e.startsWith("[")&&e.endsWith("]"))try{return JSON.parse(e)}catch{return e}if(e.startsWith('"')&&e.endsWith('"')||e.startsWith("'")&&e.endsWith("'"))try{return JSON.parse(e)}catch{return e.slice(1,-1)}return e}function T6(t){let e=t.replace(/\r\n/g,`
108
+ `,"utf8")}resolveNodeRefFromPayload(e,r){if(r.nodeId?.trim()){let a=e.nodes.find(s=>s.id===r.nodeId);if(!a)throw new Error(`${r.role} node not found in local graph snapshot: ${r.nodeId}`);return a.id}let o=r.nodeLabel?.trim();if(!o)throw new Error(`${r.role} node reference is missing`);let n=e.nodes.filter(a=>a.label.trim()===o);if(n.length===1)return n[0].id;if(n.length>1)throw new Error(`${r.role} node label is ambiguous in local graph snapshot: ${o}`);let i=e.nodes.filter(a=>a.label.trim().toLowerCase()===o.toLowerCase());if(i.length===1)return i[0].id;throw i.length>1?new Error(`${r.role} node label is ambiguous in local graph snapshot: ${o}`):new Error(`${r.role} node label not found in local graph snapshot: ${o}`)}resolveNodeIndex(e,r){let o=e.nodes.findIndex(n=>n.id===r);if(o===-1)throw new Error(`local graph node not found: ${r}`);return o}resolveEdgeIndex(e,r){let o=e.edges.findIndex(n=>n.id===r);if(o===-1)throw new Error(`local graph edge not found: ${r}`);return o}ensureNodeBaseVersion(e,r){typeof r=="number"&&typeof e.currentVersion=="number"&&r!==e.currentVersion&&dw({targetRef:e.id,expectedVersion:r,actualVersion:e.currentVersion})}ensureEdgeBaseVersion(e,r){typeof r=="number"&&typeof e.currentVersion=="number"&&r!==e.currentVersion&&dw({targetRef:e.id,expectedVersion:r,actualVersion:e.currentVersion})}resolvePacketIndex(e,r){let o=e.packets.findIndex(n=>n.ref===r);if(o===-1)throw new Error(`local graph packet not found: ${r}`);return o}ensurePacketBaseVersion(e,r){typeof r=="number"&&typeof e.currentVersion=="number"&&r!==e.currentVersion&&dw({targetRef:e.ref,expectedVersion:r,actualVersion:e.currentVersion})}resolvePacketKindForNodeType(e,r){let o=vK[e]??[],n=O(r.packetKind);if(n){if(o.includes(n))return n;throw new Error(`local graph packet kind ${n} is not supported for node type: ${e}`)}return o[0]??null}resolvePacketScopeRef(e){let r=O(e.scopeRef);if(r)return r;let o=O(e.scopeEpisodeId);if(o)return`episode:${o}`;let n=U(e.scopeLevel).trim();if(!n)throw new Error("local graph packet mutation is missing packet.scopeLevel");return`scope:${n}`}buildPacketRef(e){let r=U(e.packet.scopeLevel).trim();if(!r)throw new Error("local graph packet mutation is missing packet.scopeLevel");let o=this.resolvePacketScopeRef(e.packet),n=e.nodeRef.includes(":")?e.nodeRef.slice(e.nodeRef.indexOf(":")+1):e.nodeRef;return`${e.packetKind}:${n}:${r}:${o}`}buildPacketSummary(e,r){if(e==="story_engine_control")return[O(r.summaryText),O(r.corePromise),O(r.soloClimbContract),O(r.thematicContradiction),O(je(r.endingAnchor).readerFinalQuestion)].filter(s=>!!s).join(" | ")||"story engine control";if(e==="arc_control")return[O(r.summaryText),O(r.arcQuestion),O(r.arcReversal),O(r.episodeRange),O(r.obligationSummary)].filter(s=>!!s).join(" | ")||"arc control";if(e==="episode_control")return[O(r.summaryText),O(r.obligationSummary),O(r.coreLoopFocus),O(r.dopaminePayoffType),O(r.emotionalTarget)].filter(s=>!!s).join(" | ")||"episode control";if(e==="world_rule_packet")return[O(r.summaryText),O(r.coreStatement),O(r.narrativePurpose)].filter(s=>!!s).join(" | ")||"world rule packet";let o=O(r.summaryText),n=O(r.emotionalBaseline);return[o,n&&!o?.includes(`\uAC10\uC815: ${n}`)?`\uAC10\uC815: ${n}`:null,O(r.currentLocationRef)?`\uC7A5\uC18C: ${O(r.currentLocationRef)}`:null,O(r.headquartersRef)?`\uAC70\uC810: ${O(r.headquartersRef)}`:null].filter(a=>!!a).join(" | ")||"state packet"}buildPacketPayload(e,r,o){return r==="story_engine_control"?{corePromise:O(o.corePromise),soloClimbContract:O(o.soloClimbContract),arcProgressionBeats:V(o.arcProgressionBeats),externalReactionCadence:V(o.externalReactionCadence),careLoopCadence:V(o.careLoopCadence),revealLadderPriority:V(o.revealLadderPriority),thematicContradiction:O(o.thematicContradiction),endingAnchor:je(o.endingAnchor)}:r==="arc_control"?{arcQuestion:O(o.arcQuestion),arcReversal:O(o.arcReversal),payoffTargets:V(o.payoffTargets),obligationSummary:O(o.obligationSummary),episodeRange:O(o.episodeRange),episodeBeatSheet:V(o.episodeBeatSheet),towerProgressionStep:O(o.towerProgressionStep),externalPressureLine:O(o.externalPressureLine),softBeatQuota:O(o.softBeatQuota)}:r==="episode_control"?{requiredCallbacks:V(o.requiredCallbacks),mustPlant:V(o.mustPlant),mustPayoff:V(o.mustPayoff),revealQuota:kt(o.revealQuota),emotionalTarget:O(o.emotionalTarget),dopaminePayoffType:O(o.dopaminePayoffType),obligationSummary:O(o.obligationSummary),coreLoopFocus:O(o.coreLoopFocus),externalReactionMode:O(o.externalReactionMode),careBeatType:O(o.careBeatType),cliffType:O(o.cliffType),cliffFamilyOverride:O(o.cliffFamilyOverride),revealPayoffPhaseOverride:O(o.revealPayoffPhaseOverride),questionAnswerPhaseOverride:O(o.questionAnswerPhaseOverride)}:r==="world_rule_packet"?{publicSummary:O(o.publicSummary),coreStatement:O(o.coreStatement),mechanism:O(o.mechanism),knownLimitations:V(o.knownLimitations),hardLimits:V(o.hardLimits),costConditions:V(o.costConditions),sideEffects:V(o.sideEffects),knownExceptions:V(o.knownExceptions),suspectedLoopholes:V(o.suspectedLoopholes),secretExceptions:V(o.secretExceptions),narrativePurpose:O(o.narrativePurpose),conflictPotential:O(o.conflictPotential),characterImpact:O(o.characterImpact)}:e==="organization"?{headquartersRef:O(o.headquartersRef),activeTerritoryRefs:V(o.activeTerritoryRefs),activeGoalRefs:V(o.activeGoalRefs),activeConflictRefs:V(o.activeConflictRefs),currentPressureSources:V(o.currentPressureSources),cohesionStatus:O(o.cohesionStatus),publicStanding:O(o.publicStanding),anchorEpisodeNumber:null}:{currentLocationRef:O(o.currentLocationRef),currentAllegianceRef:O(o.currentAllegianceRef),activeGoalRefs:V(o.activeGoalRefs),activeConflictRefs:V(o.activeConflictRefs),emotionalBaseline:O(o.emotionalBaseline),activeSecretRefs:V(o.activeSecretRefs),recentRelationshipShifts:V(o.recentRelationshipShifts),currentPressureSources:V(o.currentPressureSources),voiceDriftNotes:V(o.voiceDriftNotes),audienceKnowledgeSplit:V(o.audienceKnowledgeSplit),lastUpdatedFromEvidenceRef:O(o.lastUpdatedFromEvidenceRef),anchorEpisodeNumber:null}}applyCreateNodeItem(e,r,o){let n=je(r.payload),i=U(n.title).trim();if(!i)throw new Error(`local create_node payload is missing title for ${r.id}`);let a=U(n.nodeTypeHint,"note").trim()||"note",s=kt(n.atEpisode),c=a==="event"?"event":"entity",l=c==="event"||s!=null?"timeline":"canon_entity",u=new Set(e.nodes.map(d=>d.id)),p=pw(u,`${ur(a)}:${ur(i)}`);return e.nodes.push({id:p,type:a,plane:c,nodeClass:"aggregate_root",primaryDimension:l,label:i,aliases:[],summary:U(n.summary),authorityLevel:"approved_ai",typePath:[a],description:O(n.content),sourceRef:r.id,ownerDocumentId:null,updatedAt:o,timeline:s!=null?{anchorEpisodeNumber:s}:null,meta:{localProposalItemId:r.id,tags:V(n.tags),linkedEpisodeIds:V(n.linkedEpisodeIds),linkedCharacterIds:V(n.linkedCharacterIds),focusNodeIds:V(n.focusNodeIds),attributes:je(n.attributes)}}),p}applyCreateEdgeItem(e,r,o,n){let i=je(r.payload),a=this.resolveNodeRefFromPayload(e,{nodeId:O(i.sourceNodeId),nodeLabel:O(i.sourceLabel),role:"source"}),s=this.resolveNodeRefFromPayload(e,{nodeId:O(i.targetNodeId),nodeLabel:O(i.targetLabel),role:"target"}),c=U(i.edgeType,"related_to").trim()||"related_to",l=U(i.relationClass,"derived").trim()||"derived",u=kt(i.atEpisode)??n?.currentEpisodeNumber??null,p=new Set(e.edges.map(f=>f.id)),d=pw(p,`edge:${ur(a)}:${ur(c)}:${ur(s)}`);return e.edges.push({id:d,sourceRef:a,targetRef:s,relationType:c,relationFamily:l,primaryDimension:u!=null?"timeline":"canon_entity",sourceRole:l==="participation"?"participant":"source",targetRole:l==="participation"&&c==="participates_in"?"event":"target",directional:fw(i.isDirectional,!0),authorityLevel:"approved_ai",summary:O(i.summary)??O(i.content),updatedAt:o,timeline:u!=null||n?.triggerEpisodeId!=null||n?.factText!=null?{validFromEpisode:u,validUntilEpisode:null,triggerEpisodeId:n?.triggerEpisodeId??null,timeLabel:n?.factText??null}:null}),d}applyCreateNodeMutation(e,r,o){let n=je(r.node),i=U(n.label).trim();if(!i)throw new Error("local create_node mutation is missing node.label");let a=U(n.type,"note").trim()||"note",s=qo(n.timeline),c=U(n.layer).trim()||(a==="event"?"event":"entity"),l=c==="event"||s?"timeline":"canon_entity",u=new Set(e.nodes.map(d=>d.id)),p=pw(u,`${ur(a)}:${ur(i)}`);return e.nodes.push({id:p,currentVersion:1,type:a,plane:c,nodeClass:"aggregate_root",primaryDimension:l,label:i,aliases:V(n.tags),summary:U(n.summary),authorityLevel:"user",typePath:[a],referenceRefIds:[],evidenceRefIds:V(r.evidenceRefs),description:O(n.description),scopeLevel:O(s?.scopeLevel),scopeRef:null,status:null,sourceRef:null,sourceTurnRefs:V(r.sourceTurnRefs),sourceMessageRefs:V(r.sourceMessageRefs),sourceAtomRefs:V(r.sourceAtomRefs),ownerDocumentId:null,updatedAt:o,timeline:s,meta:{linkedEpisodeIds:V(n.linkedEpisodeIds),linkedCharacterIds:V(n.linkedCharacterIds),attributes:je(n.attributes),...n.facets?{facets:n.facets}:{},...O(n.currentStateRef)?{currentStateRef:O(n.currentStateRef)}:{}}}),{id:p,nodeRef:p,packetRef:null,sourceRef:null,targetRef:null,relationType:null,state:"created",timeline:null}}applyUpdateNodeMutation(e,r,o){let n=U(r.nodeId).trim();if(!n)throw new Error("local update_node mutation is missing nodeId");let i=je(r.patch),a=this.resolveNodeIndex(e,n),s=e.nodes[a];this.ensureNodeBaseVersion(s,kt(r.baseVersion)),U(i.label).trim()&&(s.label=U(i.label).trim()),"summary"in i&&(s.summary=U(i.summary)),"description"in i&&(s.description=O(i.description)),"tags"in i&&(s.aliases=V(i.tags)),s.meta=s.meta??{},"attributes"in i&&(s.attributes=je(i.attributes),s.meta.attributes=s.attributes),"facets"in i&&(s.facets=je(i.facets),s.meta.facets=s.facets);for(let c of["linkedEpisodeIds","linkedCharacterIds","category","role","statusTag","slug","lane","currentStateRef"])c in i&&(s.meta[c]=i[c]);return s.currentVersion=dl(s.currentVersion),s.updatedAt=o,{id:s.id,nodeRef:s.id,packetRef:null,sourceRef:null,targetRef:null,relationType:null,state:"updated",timeline:null}}applyPatchNodeTimelineMutation(e,r,o){let n=U(r.nodeId).trim();if(!n)throw new Error("local patch_node_timeline mutation is missing nodeId");let i=this.resolveNodeIndex(e,n),a=e.nodes[i];this.ensureNodeBaseVersion(a,kt(r.baseVersion));let s=r.timeline?qo(r.timeline):null;return a.scopeLevel=O(s?.scopeLevel),a.timeline=s,a.currentVersion=dl(a.currentVersion),a.updatedAt=o,{id:a.id,nodeRef:a.id,packetRef:null,sourceRef:null,targetRef:null,relationType:null,state:"timeline_patched",timeline:s}}applyDeleteNodeMutation(e,r){let o=U(r.nodeId).trim();if(!o)throw new Error("local delete_node mutation is missing nodeId");let n=this.resolveNodeIndex(e,o),i=e.nodes[n];this.ensureNodeBaseVersion(i,kt(r.baseVersion));let[a]=e.nodes.splice(n,1);return e.edges=e.edges.filter(s=>s.sourceRef!==a.id&&s.targetRef!==a.id),{id:a.id,nodeRef:a.id,packetRef:null,sourceRef:null,targetRef:null,relationType:null,state:"deleted",timeline:null}}applyCreateEdgeMutation(e,r,o){let n=je(r.edge),i=this.resolveNodeRefFromPayload(e,{nodeId:O(n.sourceNodeId),nodeLabel:null,role:"source"}),a=this.resolveNodeRefFromPayload(e,{nodeId:O(n.targetNodeId),nodeLabel:null,role:"target"}),s=U(n.edgeType,"related_to").trim()||"related_to",c=U(n.relationClass,"derived").trim()||"derived",l=U(n.id).trim()||`edge:${ur(i)}:${ur(s)}:${ur(a)}`;return e.edges.push({id:l,currentVersion:1,sourceRef:i,targetRef:a,relationType:s,relationFamily:c,primaryDimension:n.timeline?"timeline":"canon_entity",sourceRole:c==="participation"?"participant":"source",targetRole:c==="participation"&&s==="participates_in"?"event":"target",directional:fw(n.isDirectional,!0),authorityLevel:"user",summary:O(n.summary),updatedAt:o,timeline:qo(n.timeline),evidenceRefIds:V(n.evidenceRefs),properties:qo(n.properties),scopeLevel:null,scopeRef:null}),{id:l,nodeRef:null,packetRef:null,sourceRef:i,targetRef:a,relationType:s,state:"created",timeline:null}}applyUpdateEdgeMutation(e,r,o){let n=U(r.edgeId).trim();if(!n)throw new Error("local update_edge mutation is missing edgeId");let i=je(r.patch),a=typeof i.sourceNodeId=="string"?this.resolveNodeRefFromPayload(e,{nodeId:i.sourceNodeId,nodeLabel:null,role:"source"}):null,s=typeof i.targetNodeId=="string"?this.resolveNodeRefFromPayload(e,{nodeId:i.targetNodeId,nodeLabel:null,role:"target"}):null,c=this.resolveEdgeIndex(e,n),l=e.edges[c];return this.ensureEdgeBaseVersion(l,kt(r.baseVersion)),U(i.edgeType).trim()&&(l.relationType=U(i.edgeType).trim()),U(i.relationClass).trim()&&(l.relationFamily=U(i.relationClass).trim()),typeof i.isDirectional=="boolean"&&(l.directional=i.isDirectional),"summary"in i&&(l.summary=O(i.summary)),"properties"in i&&(l.properties=qo(i.properties)),"timeline"in i&&(l.timeline=i.timeline?qo(i.timeline):null),a&&(l.sourceRef=a),s&&(l.targetRef=s),l.currentVersion=dl(l.currentVersion),l.updatedAt=o,{id:l.id,nodeRef:null,packetRef:null,sourceRef:l.sourceRef,targetRef:l.targetRef,relationType:l.relationType,state:"updated",timeline:l.timeline}}applyDeleteEdgeMutation(e,r){let o=U(r.edgeId).trim();if(!o)throw new Error("local delete_edge mutation is missing edgeId");let n=this.resolveEdgeIndex(e,o),i=e.edges[n];this.ensureEdgeBaseVersion(i,kt(r.baseVersion));let[a]=e.edges.splice(n,1);return{id:a.id,nodeRef:null,packetRef:null,sourceRef:a.sourceRef,targetRef:a.targetRef,relationType:a.relationType,state:"deleted",timeline:a.timeline}}applyCreatePacketMutation(e,r,o){let n=U(r.nodeId).trim();if(!n)throw new Error("local create_packet mutation is missing nodeId");let i=this.resolveNodeIndex(e,n),a=e.nodes[i],s=je(r.packet),c=this.resolvePacketKindForNodeType(a.type,s);if(!c)throw new Error(`local graph packets are not supported for node type: ${a.type}`);let l=this.buildPacketRef({nodeRef:a.id,packetKind:c,packet:s}),u=this.buildPacketPayload(a.type,c,s),p=this.buildPacketSummary(c,s),d="evidenceRefs"in s?V(s.evidenceRefs):V(r.evidenceRefs),f=e.packets.findIndex(h=>h.ref===l);if(f>=0){let h=e.packets[f];return h.currentVersion=dl(h.currentVersion),h.packetKind=c,h.ownerRef=a.id,h.scopeLevel=U(s.scopeLevel,h.scopeLevel),h.scopeRef=this.resolvePacketScopeRef(s),h.authorityLevel="user",h.state="active",h.summaryText=p,h.payload=u,h.evidenceRefIds=d,h.updatedAt=o,a.meta=a.meta??{},a.meta.currentStateRef=h.ref,{id:h.ref,nodeRef:a.id,packetRef:h.ref,sourceRef:null,targetRef:null,relationType:null,state:"updated",timeline:null}}return e.packets.push({id:`packet:${ur(l)}`,currentVersion:1,ref:l,packetKind:c,ownerRef:a.id,scopeLevel:U(s.scopeLevel),scopeRef:this.resolvePacketScopeRef(s),authorityLevel:"user",state:"active",summaryText:p,payload:u,evidenceRefIds:d,updatedAt:o}),a.meta=a.meta??{},a.meta.currentStateRef=l,{id:l,nodeRef:a.id,packetRef:l,sourceRef:null,targetRef:null,relationType:null,state:"created",timeline:null}}applyUpdatePacketMutation(e,r,o){let n=U(r.nodeId).trim();if(!n)throw new Error("local update_packet mutation is missing nodeId");let i=this.resolveNodeIndex(e,n),a=e.nodes[i],s=je(r.packet),c=this.resolvePacketKindForNodeType(a.type,s);if(!c)throw new Error(`local graph packets are not supported for node type: ${a.type}`);let l=O(r.packetRef);if(l){let m=this.resolvePacketIndex(e,l),g=e.packets[m];this.ensurePacketBaseVersion(g,kt(r.baseVersion))}let u=this.buildPacketRef({nodeRef:a.id,packetKind:c,packet:s}),p=this.buildPacketPayload(a.type,c,s),d=this.buildPacketSummary(c,s),f="evidenceRefs"in s?V(s.evidenceRefs):V(r.evidenceRefs),h=e.packets.findIndex(m=>m.ref===u);if(h>=0){let m=e.packets[h];return m.currentVersion=dl(m.currentVersion),m.packetKind=c,m.ownerRef=a.id,m.scopeLevel=U(s.scopeLevel,m.scopeLevel),m.scopeRef=this.resolvePacketScopeRef(s),m.authorityLevel="user",m.state="active",m.summaryText=d,m.payload=p,m.evidenceRefIds=f,m.updatedAt=o,a.meta=a.meta??{},a.meta.currentStateRef=m.ref,{id:m.ref,nodeRef:a.id,packetRef:m.ref,sourceRef:null,targetRef:null,relationType:null,state:"updated",timeline:null}}return e.packets.push({id:`packet:${ur(u)}`,currentVersion:1,ref:u,packetKind:c,ownerRef:a.id,scopeLevel:U(s.scopeLevel),scopeRef:this.resolvePacketScopeRef(s),authorityLevel:"user",state:"active",summaryText:d,payload:p,evidenceRefIds:f,updatedAt:o}),a.meta=a.meta??{},a.meta.currentStateRef=u,{id:u,nodeRef:a.id,packetRef:u,sourceRef:null,targetRef:null,relationType:null,state:"created",timeline:null}}async applyMutation(e){let r=U(e.kind).trim();if(!r)throw new Error("local graph mutation is missing kind");let o=await this.getWritableSnapshotPath(),n=await this.loadSlice(),i=structuredClone(n),a=new Date().toISOString(),s;switch(r){case"create_node":s=this.applyCreateNodeMutation(i,e,a);break;case"update_node":s=this.applyUpdateNodeMutation(i,e,a);break;case"patch_node_timeline":s=this.applyPatchNodeTimelineMutation(i,e,a);break;case"delete_node":s=this.applyDeleteNodeMutation(i,e);break;case"create_edge":s=this.applyCreateEdgeMutation(i,e,a);break;case"create_packet":s=this.applyCreatePacketMutation(i,e,a);break;case"update_edge":s=this.applyUpdateEdgeMutation(i,e,a);break;case"update_packet":s=this.applyUpdatePacketMutation(i,e,a);break;case"delete_edge":s=this.applyDeleteEdgeMutation(i,e);break;default:throw new Error(`local graph mutation kind is not supported: ${r}`)}return i.version={versionToken:`local-snapshot:${a}`,snapshotAt:a},await this.writeSlice(o,i),{result:s,appliedAt:a,snapshotAt:i.version.snapshotAt,versionToken:i.version.versionToken,snapshotPath:o}}async applyProposalItems(e){let r=await this.getWritableSnapshotPath(),o=await this.loadSlice(),n=structuredClone(o),i=new Date().toISOString(),a={},s=[...e.items].sort((c,l)=>(k6[c.opKind]??Number.MAX_SAFE_INTEGER)-(k6[l.opKind]??Number.MAX_SAFE_INTEGER)||c.sequence-l.sequence);for(let c of s)switch(c.opKind){case"create_node":a[c.id]=this.applyCreateNodeItem(n,c,i);break;case"create_edge":a[c.id]=this.applyCreateEdgeItem(n,c,i,e.temporalContext);break;default:throw new Error(`local graph apply does not support proposal opKind: ${c.opKind}`)}return n.version={versionToken:`local-snapshot:${i}`,snapshotAt:i},await this.writeSlice(r,n),{appliedAt:i,snapshotAt:n.version.snapshotAt,versionToken:n.version.versionToken,snapshotPath:r,targetRefsByItemId:a}}async query(e){let r=await this.getResolvedSnapshotPath(),o=await this.loadSlice(),n=[],i=new Map(o.nodes.map(f=>[f.id,f]));for(let f of o.edges)(!i.has(f.sourceRef)||!i.has(f.targetRef))&&n.push(`Local graph edge ${f.id} references missing node refs (${f.sourceRef} -> ${f.targetRef}).`);let a=kK(o,e.filters?.atEpisode),s=a.nodes,c=a.edges,l=a.packets;if(e.filters?.nodeRefs?.length){let f=new Set(e.filters.nodeRefs);s=s.filter(h=>f.has(h.id)),c=c.filter(h=>f.has(h.sourceRef)||f.has(h.targetRef)),l=l.filter(h=>f.has(h.ownerRef)||f.has(h.scopeRef))}if(e.filters?.planes?.length){let f=new Set(e.filters.planes);s=s.filter(h=>f.has(h.plane))}if(e.filters?.nodeTypes?.length){let f=new Set(e.filters.nodeTypes);s=s.filter(h=>f.has(h.type))}s=$K(s,e.filters?.textSearch);let u=new Set(s.map(f=>f.id));c=c.filter(f=>u.has(f.sourceRef)||u.has(f.targetRef)),l=l.filter(f=>u.has(f.ownerRef)||u.has(f.scopeRef));let p=Array.from(new Set([...e.filters?.focusRef?[e.filters.focusRef]:[],...e.filters?.focusRefs??[]].filter(f=>typeof f=="string"&&f.trim().length>0)));if(p.length>0){let f=e.filters?.neighborDepth??(e.mode==="node_detail"?1:0),h=PK(s,c,p,f);s=h.nodes,c=h.edges,l=l.filter(m=>h.allowedRefs.has(m.ownerRef)||h.allowedRefs.has(m.scopeRef))}else c=c.filter(f=>u.has(f.sourceRef)&&u.has(f.targetRef));let d=e.pagination?.limit;if(typeof d=="number"&&d>0){let f=th(s),h=new Set(p),g=(h.size>0?[...f.filter($=>h.has($.id)),...f.filter($=>!h.has($.id))]:f).slice(0,d),b=new Set(g.map($=>$.id));s=g,c=c.filter($=>b.has($.sourceRef)&&b.has($.targetRef)),l=l.filter($=>b.has($.ownerRef)||b.has($.scopeRef))}else s=th(s),c=th(c),l=th(l);return{versionToken:o.version.versionToken,snapshotAt:o.version.snapshotAt,nodeCount:s.length,edgeCount:c.length,packetCount:l.length,nodes:e.include?.nodes===!1?[]:s,edges:e.include?.edges===!1?[]:c,packets:e.include?.packets===!1?[]:l,aggregateDetails:null,warnings:n,meta:{authority:"local-snapshot",readSource:"local-json",projectId:this.projectId,snapshotPath:r?yK(Kn.relative(this.projectRoot,r)):null,revisionHint:o.version.versionToken}}}};var RK=[0,50,100,200,400,800,1600];function EK(t){return!!(t&&typeof t=="object"&&!Array.isArray(t))}function jK(t){let e=mw.join(t,".seojeom"),r=mw.join(t,".ainovel");return P6(e)||!P6(r)?e:r}function rh(t){let e=t.nodes??[],r=t.edges??[],o=t.packets??[];return{versionToken:"versionToken"in t?t.versionToken:void 0,snapshotAt:"snapshotAt"in t?t.snapshotAt:void 0,nodeCount:t.nodeCount??e.length,edgeCount:t.edgeCount??r.length,packetCount:t.packetCount??o.length,nodes:e,edges:r,packets:o,aggregateDetails:t.aggregateDetails??void 0,meta:t.meta,warnings:t.warnings}}async function TK(t){await new Promise(e=>globalThis.setTimeout(e,t))}var nh=class{descriptorPath;fallbackStore;fetchFn;constructor(e){let r=jK(e.projectRoot);this.descriptorPath=mw.join(r,".graph-ipc.json"),this.fallbackStore=new Aa({projectId:e.projectId,projectRoot:e.projectRoot,graphSlicePath:e.graphSlicePath??null}),this.fetchFn=e.fetchFn??fetch}getDescriptorPath(){return this.descriptorPath}async getResolvedSnapshotPath(){return this.fallbackStore.getResolvedSnapshotPath()}async hasReadyDescriptor(){return await this.readDescriptorWithRetry()!=null}async getInfo(){let e=await this.readDescriptorWithRetry();if(!e)return null;try{return await this.requestJson(e,"/graph/info")}catch{return null}}async listNodes(e){return this.requestOrFallback("/graph/list",e,()=>this.fallbackStore.query({mode:"slice",filters:{nodeTypes:e.nodeTypes,planes:e.planes,textSearch:e.labelContains??null},include:{nodes:!0,edges:!1,packets:!1},pagination:{limit:e.limit??50}}))}async search(e){return this.requestOrFallback("/graph/search",e,()=>this.fallbackStore.query({mode:"slice",filters:{nodeTypes:e.nodeTypes,textSearch:e.query},include:{nodes:!0,edges:!0,packets:!0},pagination:{limit:e.limit??20}}))}async neighbors(e){return this.requestOrFallback("/graph/neighbors",e,()=>this.fallbackStore.query({mode:"slice",filters:{focusRef:e.startRef,focusRefs:[e.startRef],neighborDepth:1},include:{nodes:!0,edges:!0,packets:!0},pagination:{limit:100}}))}async traverse(e){return this.requestOrFallback("/graph/traverse",e,()=>this.fallbackStore.query({mode:"node_detail",filters:{focusRef:e.startRef,focusRefs:[e.startRef],neighborDepth:Math.min(Math.max(e.depth,0),2),textSearch:typeof e.filter?.textSearch=="string"?e.filter.textSearch:null,nodeTypes:Array.isArray(e.filter?.nodeTypes)?e.filter.nodeTypes.filter(r=>typeof r=="string"):void 0,atEpisode:typeof e.filter?.atEpisode=="number"?e.filter.atEpisode:null},include:{nodes:!0,edges:!0,packets:!0}}))}async query(e){let r=e.filters??{},o=Array.isArray(r.focusRefs)?r.focusRefs.filter(l=>typeof l=="string"&&l.length>0):typeof r.focusRef=="string"&&r.focusRef.length>0?[r.focusRef]:[],n=Array.isArray(r.nodeRefs)?r.nodeRefs.filter(l=>typeof l=="string"&&l.length>0):[],i=Array.isArray(r.planes)?r.planes.filter(l=>typeof l=="string"&&l.length>0):[],a=Array.isArray(r.nodeTypes)?r.nodeTypes.filter(l=>typeof l=="string"&&l.length>0):[],s=typeof r.textSearch=="string"&&r.textSearch.trim().length>0?r.textSearch:null;return o.length===1&&n.length===0&&i.length===0?this.requestOrFallback("/graph/traverse",{startRef:o[0],depth:Math.min(Math.max(r.neighborDepth??1,0),2),filter:{textSearch:s,nodeTypes:a,atEpisode:typeof r.atEpisode=="number"&&Number.isFinite(r.atEpisode)?r.atEpisode:null}},()=>this.fallbackStore.query(e)):rh(await this.fallbackStore.query(e))}async requestOrFallback(e,r,o){let n=await this.readDescriptorWithRetry();if(!n)return rh(await o());try{return rh(await this.requestJson(n,e,r))}catch{return rh(await o())}}async readDescriptorWithRetry(){for(let e of RK){e>0&&await TK(e);let r=await this.readDescriptorOnce();if(r)return r}return null}async readDescriptorOnce(){try{let e=await IK(this.descriptorPath,"utf8"),r=JSON.parse(e);return!EK(r)||r.protocol!=="http"||typeof r.host!="string"||typeof r.port!="number"||typeof r.bearerToken!="string"||typeof r.startedAt!="string"?null:{protocol:"http",host:r.host,port:r.port,bearerToken:r.bearerToken,startedAt:r.startedAt}}catch{return null}}async requestJson(e,r,o){let n=await this.fetchFn(new URL(r,`${e.protocol}://${e.host}:${e.port}`),{method:o===void 0?"GET":"POST",headers:{Accept:"application/json",Authorization:`Bearer ${e.bearerToken}`,...o===void 0?{}:{"Content-Type":"application/json"}},body:o===void 0?void 0:JSON.stringify(o)});if(!n.ok)throw new Error(`graph IPC request failed: ${n.status} ${r}`);return await n.json()}};import{mkdir as I6,readFile as zK,readdir as OK,rename as AK,stat as R6,writeFile as E6}from"node:fs/promises";import Ht from"node:path";var Uo="---";function CK(t){return t.replace(/_([a-z])/g,(e,r)=>r.toUpperCase())}function NK(t){return t.replace(/[A-Z]/g,e=>`_${e.toLowerCase()}`)}function N6(t){return t.replace(/\\/g,"/")}function D6(t){return t.trim().toLowerCase().replace(/[^\p{L}\p{N}]+/gu,"-").replace(/^-+|-+$/g,"")||"untitled"}function oh(t){return t.replace(/\.[^.]+$/u,"")}function j6(t){let e=t.trim();if(!e)return"";if(e==="null")return null;if(e==="true")return!0;if(e==="false")return!1;if(/^-?\d+(\.\d+)?$/.test(e))return Number(e);if(e.startsWith("{")&&e.endsWith("}")||e.startsWith("[")&&e.endsWith("]"))try{return JSON.parse(e)}catch{return e}if(e.startsWith('"')&&e.endsWith('"')||e.startsWith("'")&&e.endsWith("'"))try{return JSON.parse(e)}catch{return e.slice(1,-1)}return e}function T6(t){let e=t.replace(/\r\n/g,`
109
109
  `);if(!e.startsWith(`${Uo}
110
110
  `))return{data:{},bodyMarkdown:e};let r=e.indexOf(`
111
111
  ${Uo}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "seojeom-mcp",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "description": "Local-first MCP server for Claude/Codex project wiki and graph workflows",
6
6
  "repository": {