@kynver-app/runtime 0.2.166 → 0.2.170

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.
@@ -0,0 +1,5 @@
1
+ import{readFileSync as B,statSync as D}from"node:fs";var u="NOTE-ACK",A=new RegExp(`${u}[:\\s]+\\[?([A-Za-z0-9][A-Za-z0-9_-]{3,80})`,"g");function x(e,t){let n=[];for(let o of e.matchAll(A))t.has(o[1])&&n.push(o[1]);return n}function y(e){if(!e||typeof e!="object")return"";let t=e;if(t.type==="assistant"){let n=t.message?.content;if(typeof n=="string")return n;if(Array.isArray(n))return n.map(o=>o&&typeof o=="object"&&o.type==="text"?String(o.text??""):"").join(`
2
+ `)}return t.item&&t.item.type==="agent_message"&&typeof t.item.text=="string"?t.item.text:""}function h(e,t){let n=new Set(t);if(n.size===0||!e.includes(u))return[];let o=new Set;for(let r of e.split(/\r?\n/)){if(!r.includes(u))continue;let c=r.trim();if(c.startsWith("{")){try{for(let s of x(y(JSON.parse(c)),n))o.add(s)}catch{}continue}for(let s of x(c,n))o.add(s)}return[...o]}function S(e,t){return h(e,t)}function d(e,t={}){let n=t.stop?"STOP \u2014 before you finish: you have an unread note from the owner (sent through Kynver). Read it and act on it now; do not finish until you have.":"NEW NOTE FROM THE OWNER (sent through Kynver while you work). It takes priority over conflicting earlier instructions.",o=e.map((r,c)=>`${c+1}. noteId: ${r.noteId}
3
+ ${r.text.trim()}`);return["=== KYNVER OWNER NOTE ===",n,"",...o,"",`When you have read a note and acted on it, acknowledge it by id: call agent_os_task_note_ack if you have that tool, otherwise write a line in your reply made of the word ${u} followed by the noteId. A note only counts as passed once you acknowledge it.`,"=== END KYNVER OWNER NOTE ==="].join(`
4
+ `)}import{existsSync as w,readFileSync as E,renameSync as O,writeFileSync as _}from"node:fs";var T=600*1e3,j=3,l=()=>({notes:[],stdoutScanOffset:0});function g(e){try{if(!w(e))return l();let t=JSON.parse(E(e,"utf8"));return{notes:Array.isArray(t.notes)?t.notes.filter(n=>!!(n&&typeof n.noteId=="string")):[],stdoutScanOffset:Number.isFinite(t.stdoutScanOffset)?Number(t.stdoutScanOffset):0}}catch{return l()}}function R(e,t){let n=`${e}.${process.pid}.tmp`;_(n,`${JSON.stringify(t,null,2)}
5
+ `,{mode:384}),O(n,e)}var f=e=>!!(e.ackSeenAt||e.ackPostedAt);function I(e,t){return e.notes.filter(n=>f(n)?!1:n.injectedAt?n.injectCount<j&&t.getTime()-Date.parse(n.injectedAt)>=T:!0)}function m(e,t,n){let o=new Set(t);return{...e,notes:e.notes.map(r=>o.has(r.noteId)&&!r.ackSeenAt?{...r,ackSeenAt:n.toISOString()}:r)}}var v=(e,t)=>e?t?Date.parse(e)>=Date.parse(t)?e:t:e:t,p=(e,t)=>e?t?Date.parse(e)<=Date.parse(t)?e:t:e:t;function C(e,t){let n=new Map;for(let o of[...e.notes,...t.notes]){let r=n.get(o.noteId);if(!r){n.set(o.noteId,{...o});continue}n.set(o.noteId,{...r,injectCount:Math.max(r.injectCount??0,o.injectCount??0),injectedAt:v(r.injectedAt,o.injectedAt),stopBlockedAt:p(r.stopBlockedAt,o.stopBlockedAt),ackSeenAt:p(r.ackSeenAt,o.ackSeenAt),ackPostedAt:p(r.ackPostedAt,o.ackPostedAt)})}return{notes:[...n.values()],stdoutScanOffset:Math.max(e.stdoutScanOffset,t.stdoutScanOffset)}}function k(e,t){let n=C(g(e),t);return R(e,n),n}function N(e){let t=e.inbox,n=!1;if(e.transcript){let s=S(e.transcript,t.notes.filter(a=>!f(a)).map(a=>a.noteId));s.length&&(t=m(t,s,e.now),n=!0)}let o=e.now.toISOString();if(e.event==="Stop"||e.event==="SubagentStop"){if(e.event==="SubagentStop")return{output:null,inbox:t,changed:n};let s=t.notes.filter(i=>!f(i)&&!i.stopBlockedAt);if(s.length===0)return{output:null,inbox:t,changed:n};let a=new Set(s.map(i=>i.noteId));return t={...t,notes:t.notes.map(i=>a.has(i.noteId)?{...i,stopBlockedAt:o,injectedAt:o,injectCount:i.injectCount+1}:i)},{output:{decision:"block",reason:d(s,{stop:!0})},inbox:t,changed:!0}}let r=I(t,e.now);if(r.length===0)return{output:null,inbox:t,changed:n};let c=new Set(r.map(s=>s.noteId));return t={...t,notes:t.notes.map(s=>c.has(s.noteId)?{...s,injectedAt:o,injectCount:s.injectCount+1}:s)},{output:{hookSpecificOutput:{hookEventName:e.event,additionalContext:d(r)}},inbox:t,changed:!0}}var b=512*1024;function M(e){let t=process.argv.indexOf(e);return t>=0?process.argv[t+1]:void 0}async function P(e=1e6){let t=[],n=0;for await(let o of process.stdin){let r=Buffer.isBuffer(o)?o:Buffer.from(String(o));if(n+=r.length,n>e)break;t.push(r)}return Buffer.concat(t).toString("utf8")}function K(e){if(typeof e!="string"||!e)return null;try{let t=D(e).size,n=B(e,"utf8");return t>b?n.slice(-b):n}catch{return null}}async function F(){let e=M("--inbox");if(!e)return;let t=JSON.parse(await P()||"{}"),n=typeof t.hook_event_name=="string"?t.hook_event_name:"PostToolUse",o=g(e);if(o.notes.length===0)return;let r=N({event:n,inbox:o,now:new Date,transcript:n==="Stop"?K(t.transcript_path):null,stopHookActive:t.stop_hook_active===!0});r.changed&&k(e,r.inbox),r.output&&process.stdout.write(JSON.stringify(r.output))}F().catch(()=>{}).finally(()=>process.exit(0));