@chatpanel/events 0.465.0 → 0.466.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/contexts.js ADDED
@@ -0,0 +1 @@
1
+ import{makeRecordId as I,parseRecordId as _}from"./library.js";const X=80,M=600,L=16,k=5e3,A=Object.freeze(["blue","orange","green","purple","pink","teal","gray"]),R=Object.freeze(["chat","note","meeting","brief"]),i=t=>Number.isFinite(Number(t))?Number(t):0,h=t=>t&&!i(t.deletedAt),B=t=>R.includes(_(t)?.kind);function b(t,e){const o=String(t??"").replace(/[\u0000-\u001f\u007f]+/g," ").replace(/\s+/g," ").trim();return o.length>e?o.slice(0,e).trimEnd():o}const S=t=>b(t,X);function C(t){const e=[];for(const o of Array.isArray(t)?t:String(t||"").split(",")){const r=b(o,40).toLowerCase();if(r&&!e.includes(r)&&e.push(r),e.length>=L)break}return e}function K({name:t,color:e=null,about:o="",keywords:r=[],newId:s,now:c=0}={}){if(typeof s!="function")throw new Error("makeContext needs newId()");const n=S(t);if(!n)throw new Error("a context needs a name");return{id:I("context",s()),kind:"context",title:n,createdAt:i(c),updatedAt:i(c),body:{color:A.includes(e)?e:A[0],about:b(o,M),keywords:C(r),members:{}}}}function D(t,{name:e,color:o,about:r,keywords:s}={},{now:c=0}={}){const n={...t?.body||{}};let d=t?.title||"";if(e!==void 0){const f=S(e);f&&(d=f)}return o!==void 0&&A.includes(o)&&(n.color=o),r!==void 0&&(n.about=b(r,M)),s!==void 0&&(n.keywords=C(s)),{...t,title:d,body:n,updatedAt:Math.max(i(c),i(t?.updatedAt))}}function O(t,e,o,r){const s={...t?.body?.members||{}};for(const n of Array.isArray(e)?e:[e]){if(!B(n))continue;const d=s[n];d&&i(d.at)>i(r)||(s[n]={on:!!o,at:i(r)})}const c=Object.keys(s);if(c.length>k){const n=c.filter(d=>!s[d].on).sort((d,f)=>s[d].at-s[f].at);for(const d of n.slice(0,c.length-k))delete s[d]}return{...t,body:{...t?.body||{},members:s},updatedAt:Math.max(i(r),i(t?.updatedAt))}}const F=(t,e,{now:o=0}={})=>O(t,e,!0,o),H=(t,e,{now:o=0}={})=>O(t,e,!1,o);function T(t){const e=t?.body?.members||{};return Object.keys(e).filter(o=>e[o]?.on).sort((o,r)=>i(e[r].at)-i(e[o].at))}function W(t,e){if(!t)return e;if(!e)return t;const o=i(e.updatedAt)>i(t.updatedAt)?e:t,r={...t.body?.members||{}};for(const[c,n]of Object.entries(e.body?.members||{}))(!r[c]||i(n?.at)>i(r[c].at))&&(r[c]={on:!!n?.on,at:i(n?.at)});const s=Math.max(i(t.deletedAt),i(e.deletedAt))||void 0;return{...o,body:{...o.body||{},members:r},updatedAt:Math.max(i(t.updatedAt),i(e.updatedAt)),...s?{deletedAt:s}:{}}}function q(t,e=[]){return e.filter(o=>h(o)&&o.body?.members?.[t]?.on)}function E(t=[],e=[]){const o=new Map(e.filter(h).map(r=>[r.id,r]));return t.filter(h).map(r=>{const s=T(r).map(n=>o.get(n)).filter(Boolean).sort((n,d)=>i(d.updatedAt)-i(n.updatedAt)),c={chat:0,note:0,meeting:0,brief:0};for(const n of s)c[n.kind]=(c[n.kind]||0)+1;return{context:r,counts:c,items:s,last:s.length?i(s[0].updatedAt):i(r.updatedAt)}}).sort((r,s)=>s.last-r.last)}function P(t=[],e=[]){const o=t.filter(h),r=new Set(o.map(d=>d.id)),s=[],c=new Set;for(const{context:d,items:f}of E(e,o)){const a=f.filter(l=>r.has(l.id));if(a.length){for(const l of a)c.add(l.id);s.push({context:d,items:a})}}const n=o.filter(d=>!c.has(d.id));return n.length&&s.push({context:null,items:n}),s}const v=new Set(["the","and","for","with","that","this","from","about","what","when","how","are","was","you","your","our","into","have","has","not","but","can","will","just","new","chat","note","notes","meeting"]),g=t=>String(t||"").toLowerCase().match(/[\p{L}\p{N}][\p{L}\p{N}'-]{2,}/gu)?.filter(e=>!v.has(e))||[];function U({title:t="",text:e="",contexts:o=[],records:r=[],limit:s=3,min:c=.2}={}){const n=new Set(g(`${t} ${String(e).slice(0,4e3)}`));if(!n.size)return[];const d=new Map(r.filter(h).map(a=>[a.id,a])),f=[];for(const a of o.filter(h)){const l=g(a.title),N=(a.body?.keywords||[]).flatMap(u=>g(u)),$=T(a).slice(0,60).map(u=>d.get(u)?.title||"").flatMap(u=>g(u));let p=0;const m=[],y=l.filter(u=>n.has(u));y.length&&(p+=.6*(y.length/l.length),m.push(`names "${y.join(" ")}"`));const w=[...new Set(N.filter(u=>n.has(u)))];w.length&&(p+=Math.min(.5,.2*w.length),m.push(`keywords ${w.slice(0,3).join(", ")}`));const j=new Set($),x=[...n].filter(u=>j.has(u));x.length&&(p+=Math.min(.4,.08*x.length),m.push(`like what it holds (${x.slice(0,3).join(", ")})`)),p>=c&&f.push({id:a.id,title:a.title,score:Math.round(Math.min(1,p)*100)/100,why:m.join("; ")})}return f.sort((a,l)=>l.score-a.score).slice(0,Math.max(1,s))}function Y(t,e=[]){if(!h(t))return{ids:[],line:""};const[o]=E([t],e),r=o?.counts||{},s=[["chat","chats"],["note","notes"],["meeting","meetings"]].filter(([n])=>r[n]).map(([n,d])=>`${r[n]} ${r[n]===1?n:d}`),c=t.body?.about?` — ${t.body.about}`:"";return{ids:(o?.items||[]).map(n=>n.id),line:`The person is working in their context "${t.title}"${c}.${s.length?` It holds ${s.join(", ")}; search them first for anything this question refers to.`:""}`}}export{A as CONTEXT_COLORS,R as CONTEXT_ITEM_KINDS,M as MAX_CONTEXT_ABOUT,X as MAX_CONTEXT_NAME,L as MAX_KEYWORDS,k as MAX_MEMBERS,F as addToContext,S as cleanContextName,E as contextIndex,T as contextMembers,Y as contextScope,q as contextsOf,D as editContext,P as groupByContext,K as makeContext,W as mergeContexts,H as removeFromContext,U as suggestContexts};
package/library.js CHANGED
@@ -1,4 +1,4 @@
1
- class l extends Error{constructor(t){super(t),this.name="LibraryError"}}const y=Object.freeze(["chat","note","meeting","brief"]),g=Object.freeze(["folder"]),p=new Set([...y,...g]);function m(e){const t=String(e||""),n=t.indexOf(":");if(n<=0)return null;const r=t.slice(0,n),o=t.slice(n+1);return!p.has(r)||!o?null:{kind:r,localId:o}}function B(e,t){if(!p.has(e))throw new l(`unknown record kind: ${e}`);const n=String(t||"");if(!n)throw new l("a record id needs a local id");if(n.includes(":"))throw new l(`local id may not contain ":" — got ${n}`);return`${e}:${n}`}function F(e){return m(e)!==null}const h=/`((?:chat|note|meeting|brief):[A-Za-z0-9_-]+)`/g,A=/\[((?:chat|note|meeting|brief):[A-Za-z0-9_-]+)\](?!\()/g,x=/(?<![\w/:.\[`-])((?:chat|note|meeting|brief):[A-Za-z0-9_-]+)(?![\w-])/g,u=/(```[\s\S]*?```)/g;function Z(e){const t=new Set;for(const n of String(e||"").split(u))if(!n.startsWith("```"))for(const r of[h,A,x])for(const o of n.matchAll(r))t.add(o[1]);return[...t]}const R=/`\s*(\[[^\]\n`]+\]\((?:chatpanel-record:|https?:\/\/)[^)\s`]+\))\s*`/g,$=/`\s*(chatpanel-record:(?:chat|note|meeting|brief):[A-Za-z0-9_-]+)\s*`/g,E=Object.freeze({rightarrow:"→",to:"→",leftarrow:"←",gets:"←",leftrightarrow:"↔",Rightarrow:"⇒",Leftarrow:"⇐",Leftrightarrow:"⇔",implies:"⇒",iff:"⇔",uparrow:"↑",downarrow:"↓",mapsto:"↦",longrightarrow:"⟶",times:"×",cdot:"·",div:"÷",pm:"±",mp:"∓",le:"≤",leq:"≤",ge:"≥",geq:"≥",ne:"≠",neq:"≠",approx:"≈",sim:"∼",equiv:"≡",infty:"∞",checkmark:"✓",ldots:"…",dots:"…",cdots:"⋯",degree:"°",circ:"°",bullet:"•",star:"★",alpha:"α",beta:"β",gamma:"γ",delta:"δ",Delta:"Δ",lambda:"λ",mu:"μ",pi:"π",sigma:"σ",Sigma:"Σ",theta:"θ",omega:"ω",in:"∈",notin:"∉",subset:"⊂",cup:"∪",cap:"∩",forall:"∀",exists:"∃",neg:"¬",land:"∧",lor:"∨"}),S=/\$\s*\\([A-Za-z]+)\s*\$/g,_=/\\([A-Za-z]+)(?![A-Za-z])/g;function T(e){const t=String(e??"");return!t.includes("`")&&!t.includes("\\")?t:t.split(u).map(n=>n.startsWith("```")?n:n.replace(R,"$1").replace($,(o,i)=>`[${i.slice(17)}](${i})`).split(/(`[^`\n]*`)/g).map((o,i)=>i%2===1?o:o.replace(S,(c,a)=>E[a]??c).replace(_,(c,a)=>E[a]??c)).join("")).join("")}function K(e,{href:t=r=>`chatpanel-record:${r}`,label:n=r=>r}={}){const r=i=>String(i).replace(/[\[\]]/g,c=>`\\${c}`),o=(i,c)=>{const a=n(c);return a==null?i:`[${r(a)}](${t(c)})`};return T(e).split(u).map(i=>i.startsWith("```")?i:i.replace(h,o).replace(A,o).replace(x,o)).join("")}const w=48;function k(e,t="Untitled"){const n=String(e||"").split(`
1
+ class l extends Error{constructor(t){super(t),this.name="LibraryError"}}const y=Object.freeze(["chat","note","meeting","brief"]),g=Object.freeze(["folder","context"]),p=new Set([...y,...g]);function m(e){const t=String(e||""),n=t.indexOf(":");if(n<=0)return null;const r=t.slice(0,n),o=t.slice(n+1);return!p.has(r)||!o?null:{kind:r,localId:o}}function B(e,t){if(!p.has(e))throw new l(`unknown record kind: ${e}`);const n=String(t||"");if(!n)throw new l("a record id needs a local id");if(n.includes(":"))throw new l(`local id may not contain ":" — got ${n}`);return`${e}:${n}`}function F(e){return m(e)!==null}const h=/`((?:chat|note|meeting|brief):[A-Za-z0-9_-]+)`/g,A=/\[((?:chat|note|meeting|brief):[A-Za-z0-9_-]+)\](?!\()/g,x=/(?<![\w/:.\[`-])((?:chat|note|meeting|brief):[A-Za-z0-9_-]+)(?![\w-])/g,u=/(```[\s\S]*?```)/g;function Z(e){const t=new Set;for(const n of String(e||"").split(u))if(!n.startsWith("```"))for(const r of[h,A,x])for(const o of n.matchAll(r))t.add(o[1]);return[...t]}const R=/`\s*(\[[^\]\n`]+\]\((?:chatpanel-record:|https?:\/\/)[^)\s`]+\))\s*`/g,$=/`\s*(chatpanel-record:(?:chat|note|meeting|brief):[A-Za-z0-9_-]+)\s*`/g,E=Object.freeze({rightarrow:"→",to:"→",leftarrow:"←",gets:"←",leftrightarrow:"↔",Rightarrow:"⇒",Leftarrow:"⇐",Leftrightarrow:"⇔",implies:"⇒",iff:"⇔",uparrow:"↑",downarrow:"↓",mapsto:"↦",longrightarrow:"⟶",times:"×",cdot:"·",div:"÷",pm:"±",mp:"∓",le:"≤",leq:"≤",ge:"≥",geq:"≥",ne:"≠",neq:"≠",approx:"≈",sim:"∼",equiv:"≡",infty:"∞",checkmark:"✓",ldots:"…",dots:"…",cdots:"⋯",degree:"°",circ:"°",bullet:"•",star:"★",alpha:"α",beta:"β",gamma:"γ",delta:"δ",Delta:"Δ",lambda:"λ",mu:"μ",pi:"π",sigma:"σ",Sigma:"Σ",theta:"θ",omega:"ω",in:"∈",notin:"∉",subset:"⊂",cup:"∪",cap:"∩",forall:"∀",exists:"∃",neg:"¬",land:"∧",lor:"∨"}),S=/\$\s*\\([A-Za-z]+)\s*\$/g,_=/\\([A-Za-z]+)(?![A-Za-z])/g;function T(e){const t=String(e??"");return!t.includes("`")&&!t.includes("\\")?t:t.split(u).map(n=>n.startsWith("```")?n:n.replace(R,"$1").replace($,(o,i)=>`[${i.slice(17)}](${i})`).split(/(`[^`\n]*`)/g).map((o,i)=>i%2===1?o:o.replace(S,(c,a)=>E[a]??c).replace(_,(c,a)=>E[a]??c)).join("")).join("")}function K(e,{href:t=r=>`chatpanel-record:${r}`,label:n=r=>r}={}){const r=i=>String(i).replace(/[\[\]]/g,c=>`\\${c}`),o=(i,c)=>{const a=n(c);return a==null?i:`[${r(a)}](${t(c)})`};return T(e).split(u).map(i=>i.startsWith("```")?i:i.replace(h,o).replace(A,o).replace(x,o)).join("")}const w=48;function k(e,t="Untitled"){const n=String(e||"").split(`
2
2
  `).map(r=>r.replace(/^\s*#{1,6}\s*/,"").replace(/[*_`>~]+/g,"").trim()).find(r=>r.length>0);return n?n.length>w?`${n.slice(0,w-1).trimEnd()}…`:n:t}const d=(e,t=0)=>Number.isFinite(Number(e))?Number(e):t,s=e=>typeof e=="string"?e:e==null?"":String(e);function f(e={},{now:t=0}={}){const n=m(e.id);if(!n)throw new l(`not a record id: ${JSON.stringify(e.id)}`);const r=d(e.createdAt,d(e.date,t));return{id:e.id,kind:n.kind,localId:n.localId,title:s(e.title).trim()||L(n.kind,e),tags:j(e.tags),createdAt:r,updatedAt:d(e.updatedAt,r),deletedAt:d(e.deletedAt,0)||0,body:e.body??null,meta:e.meta&&typeof e.meta=="object"?e.meta:{}}}function L(e,t){return e==="note"?k(t?.body?.markdown??t?.body,"Untitled note"):e==="meeting"?"Untitled meeting":e==="brief"?"Untitled brief":e==="folder"?"Untitled folder":"New chat"}function j(e){if(!Array.isArray(e))return[];const t=[];for(const n of e){const r=s(n).trim();r&&!t.includes(r)&&t.push(r)}return t}function M(e){try{return f(e),!0}catch{return!1}}const I={assistant:"Assistant",system:"System",user:"You"};function q(e){const t=f(e);return t.deletedAt||g.includes(t.kind)?null:{id:t.id,type:t.kind,title:t.title,date:t.updatedAt||t.createdAt,text:N(t)}}function N(e){const t=e.kind?e:f(e),n=`${O(t.kind)}: ${t.title}`,r=t.tags.length?`
3
3
  Tags: ${t.tags.join(", ")}`:"";return`${n}${r}
4
4
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chatpanel/events",
3
- "version": "0.465.0",
3
+ "version": "0.466.0",
4
4
  "description": "The canonical ChatPanel event-log and capability contracts — typed durable facts, clock-free deterministic linearization, schema upcasting, and the invariants the replay harness asserts. Pure, dependency-free ESM shared by the ChatPanel extension, gateway and bridge.",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -156,6 +156,7 @@
156
156
  "./stitch.js": "./stitch.js",
157
157
  "./note-merge.js": "./note-merge.js",
158
158
  "./folders.js": "./folders.js",
159
+ "./contexts.js": "./contexts.js",
159
160
  "./html-markdown.js": "./html-markdown.js",
160
161
  "./notes-import.js": "./notes-import.js",
161
162
  "./onenote-source.js": "./onenote-source.js",
@@ -522,6 +523,7 @@
522
523
  "stitch.js",
523
524
  "note-merge.js",
524
525
  "folders.js",
526
+ "contexts.js",
525
527
  "html-markdown.js",
526
528
  "notes-import.js",
527
529
  "onenote-source.js",