@chatpanel/gateway 0.161.0 → 0.162.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chatpanel/gateway",
3
- "version": "0.161.0",
3
+ "version": "0.162.0",
4
4
  "description": "Local privacy gateway — redacts PII out of OpenAI/Anthropic API traffic before it reaches a model, then restores it in the reply. Point opencode, codex, aider, Claude Code, etc. at it.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -24,7 +24,7 @@
24
24
  "dependencies": {
25
25
  "@anthropic-ai/sandbox-runtime": "^0.0.77",
26
26
  "@chatpanel/bridge": "^0.52.0",
27
- "@chatpanel/events": "^0.510.0",
27
+ "@chatpanel/events": "^0.511.0",
28
28
  "@chatpanel/pii": "^0.15.6",
29
29
  "@huggingface/transformers": "^4.2.0",
30
30
  "defuddle": "^0.19.4",
@@ -1 +1 @@
1
- import{makeContext as F,editContext as P,addToContext as W,removeFromContext as Z,contextIndex as D,contextMembers as _,suggestContexts as H,contextScope as K,addRefs as Q,removeRefs as V,setPeople as Y,parseRef as X,CONTEXT_COLORS as p,CONTEXT_NO_ACCESS as U,CONTEXT_ROLES as z,MAX_CONTEXT_INSTRUCTIONS as G}from"@chatpanel/events/contexts.js";import{contextAllows as J,contextFor as q}from"@chatpanel/events/context-access.js";import{toSearchRecord as tt}from"@chatpanel/events/library.js";import{randomUUID as et}from"node:crypto";const nt=/^context:[A-Za-z0-9_-]{1,80}$/,L=/^(chat|note|meeting|brief):[^\s]{1,200}$/,x=Object.freeze({kind:"owner"});class N extends Error{constructor(c,h){super(h),this.status=c}}function it({store:s,notify:c=()=>{},host:h="gateway",now:i=()=>Date.now(),newId:E=()=>et().replace(/-/g,"").slice(0,20),upstreamOk:k=()=>!1,audit:l=()=>{}}={}){const y=()=>s.ofKind("context").filter(t=>!t.deletedAt),S=t=>s.heads(t),u=(t,e=null,n=x)=>{const o=q(n,t,{upstreamOk:r=>k(r,n)});return o?{id:t.id,title:t.title,color:t.body?.color||p[0],about:t.body?.about||"",keywords:t.body?.keywords||[],...e?{counts:e.counts,last:e.last}:{},updatedAt:t.updatedAt,instructions:o.instructions,refs:o.refs,mcp_servers:o.mcp_servers,skills:o.skills,...o.access?{access:o.access}:{}}:null},d=(t,e,n)=>J(t,e,n),f=t=>{const e=s.write([t],{host:h,at:i()});return e.written.length&&c({ids:e.written}),s.get(t.id)||t},m=t=>{if(!nt.test(String(t||"")))return null;const e=s.get(t);return e&&!e.deletedAt?e:null},$=t=>t?.kind==="agent"?{kind:"agent",id:String(t.name||"unknown")}:{kind:"user",id:t?.kind==="person"?String(t.id):t?.kind==="owner"?"owner":"unknown"},C=(t,e,n,o,r)=>(l("access.denied",{resource:String(e||"context:?").slice(0,120),action:n,actor:$(t)}),new N(o,r)),j=(t,e,n)=>l("access.changed",{resource:e.id,actor:$(t),fields:n}),O=(t,e,n)=>{const o=m(e);if(!o)throw new N(404,"no such context");if(!d(t,o,"list"))throw C(t,e,n,404,"no such context");if(!d(t,o,n))throw C(t,e,n,403,`contexts: ${n} needs a role this caller does not have`);return o},R=t=>L.test(t),M=t=>(Array.isArray(t)?t:[]).slice(0,500);return{list(t=x){const e=y().filter(o=>d(t,o,"list")),n=[...new Set(e.flatMap(o=>_(o)))];return D(e,S(n)).map(o=>u(o.context,o,t)).filter(Boolean)},create(t={},e=x){if(e?.kind!=="owner")throw C(e,"context:new","create",403,"contexts: only the owner makes one here");B(t);const n=P(F({name:t.name,color:t.color,about:t.about,keywords:t.keywords,newId:E,now:i()}),ot(t),{now:i()});return u(f(n),null,e)},get(t,e=x){const n=O(e,t,"list");if(!d(e,n,"read"))return{context:u(n,null,e),items:[]};const[o]=D([n],S(_(n)));return{context:u(n,o,e),items:(o?.items||[]).map(r=>({id:r.id,kind:r.kind,title:r.title,updatedAt:r.updatedAt}))}},edit(t,e={},n=x){B(e);const o=e.agents!==void 0||e.noAccess!==void 0,r=O(n,t,o?"manage":"edit"),a=u(f(P(r,e,{now:i()})),null,n);return o&&j(n,r,["agents","noAccess"].filter(A=>e[A]!==void 0)),a},remove(t,e=x){const n=O(e,t,"manage"),o=i();return f({id:n.id,kind:"context",title:n.title,updatedAt:Math.max(o,Number(n.updatedAt)||0),deletedAt:Math.max(o,(Number(n.updatedAt)||0)+1)}),!0},items(t,{add:e=[],remove:n=[]}={},o=x){return this.resources(t,{add:M(e).map(String).filter(R),remove:M(n).map(String).filter(R)},o)},resources(t,{add:e=[],remove:n=[]}={},o=x){let r=O(o,t,"edit");const a=i(),A=w=>{const T=[],I=[];for(const b of M(w))typeof b=="string"&&R(b)?T.push(b):(typeof b=="string"?X(b):b&&X(`${b.type}:${b.id}`))&&I.push(b);return{members:T,refs:I}},g=A(e),v=A(n);return g.members.length&&(r=W(r,g.members,{now:a})),v.members.length&&(r=Z(r,v.members,{now:a})),g.refs.length&&(r=Q(r,g.refs,{now:a})),v.refs.length&&(r=V(r,v.refs,{now:a})),u(f(r),null,o)},people(t,e={},n=x){const o=O(n,t,"manage"),r={};for(const[A,g]of Object.entries(e&&typeof e=="object"?e:{})){if(g!==null&&!z.includes(g))throw new Error(`a role is ${z.join(", ")} or null`);r[A]=g}const a=u(f(Y(o,r,{now:i()})),null,n);return Object.keys(r).length&&j(n,o,["people"]),a},suggest({id:t="",title:e="",text:n="",limit:o=3}={},r=x){let a=String(e||""),A=String(n||"");if(t&&L.test(String(t))){const w=s.get(t),T=w?tt(w):null;a=a||w?.title||"",A=A||T?.text||""}const g=y().filter(w=>d(r,w,"edit")),v=S([...new Set(g.flatMap(w=>_(w)))]);return H({title:a,text:A,contexts:g,records:v,limit:Math.min(8,Math.max(1,Number(o)||3))}).filter(w=>!(t&&g.find(T=>T.id===w.id)?.body?.members?.[t]?.on))},scope(t,e=x){const n=O(e,t,"use");return K(n,S(_(n)))}}}function B(s={}){if(s.instructions!==void 0&&(typeof s.instructions!="string"||s.instructions.length>G))throw new Error(`instructions are a string of at most ${G} characters`);for(const c of["mcp_servers","skills","agents"])if(s[c]!==void 0&&s[c]!==null&&!(Array.isArray(s[c])&&s[c].every(h=>typeof h=="string")))throw new Error(`${c} is a list of names, or null for every one`);if(s.noAccess!==void 0&&!U.includes(s.noAccess))throw new Error(`noAccess is ${U.join(" or ")}`)}const ot=s=>Object.fromEntries(["instructions","mcp_servers","skills","noAccess","agents"].filter(c=>s[c]!==void 0).map(c=>[c,s[c]]));async function dt(s,c,{pathname:h,api:i,readJson:E,sendJson:k,principal:l=x}){if(!h.startsWith("/v1/contexts"))return!1;const y=(u,d,f="invalid_request_error")=>k(c,u,{error:{message:d,type:f}}),S=u=>{const d=u();return d?k(c,200,{ok:!0,context:d}):y(404,"no such context","not_found")};try{if(h==="/v1/contexts"&&s.method==="GET")return k(c,200,{ok:!0,contexts:i.list(l)}),!0;if(h==="/v1/contexts"&&s.method==="POST"){const m=await E(s);return String(m?.name||"").trim()?(k(c,200,{ok:!0,context:i.create(m,l)}),!0):(y(400,"a context needs a name"),!0)}if(h==="/v1/contexts/suggest"&&s.method==="POST")return k(c,200,{ok:!0,suggestions:i.suggest(await E(s),l)}),!0;const u=/^\/v1\/contexts\/(context:[A-Za-z0-9_-]{1,80})(\/items|\/scope|\/resources|\/people)?$/.exec(decodeURIComponent(h));if(!u)return y(404,"no such contexts route","not_found"),!0;const[,d,f]=u;if(!f&&s.method==="GET")return k(c,200,{ok:!0,...i.get(d,l)}),!0;if(!f&&s.method==="PATCH"){const m=await E(s);return S(()=>i.edit(d,m,l)),!0}if(!f&&s.method==="DELETE")return i.remove(d,l),k(c,200,{ok:!0}),!0;if(f==="/items"&&s.method==="POST"){const m=await E(s);return S(()=>i.items(d,m,l)),!0}if(f==="/resources"&&s.method==="POST"){const m=await E(s);return S(()=>i.resources(d,m,l)),!0}if(f==="/people"&&s.method==="POST"){const m=await E(s);return S(()=>i.people(d,m,l)),!0}return f==="/scope"&&s.method==="GET"?(k(c,200,{ok:!0,...i.scope(d,l)}),!0):(y(405,"method not allowed","method_not_allowed"),!0)}catch(u){return u instanceof N?(y(u.status,u.message,u.status===404?"not_found":"forbidden"),!0):(y(400,`contexts: ${u.message}`),!0)}}export{N as ContextDenied,it as createContextsApi,dt as handleContexts};
1
+ import{makeContext as W,editContext as j,addToContext as K,removeFromContext as Z,contextIndex as D,contextMembers as _,suggestContexts as H,contextScope as Q,addRefs as V,removeRefs as Y,setPeople as p,parseRef as U,CONTEXT_COLORS as J,CONTEXT_NO_ACCESS as X,CONTEXT_ROLES as G,MAX_CONTEXT_INSTRUCTIONS as L}from"@chatpanel/events/contexts.js";import{contextAllows as q,contextFor as tt}from"@chatpanel/events/context-access.js";import{contextsForTurn as et,contextTurn as nt}from"@chatpanel/events/context-turn.js";import{toSearchRecord as ot}from"@chatpanel/events/library.js";import{randomUUID as rt}from"node:crypto";const z=/^context:[A-Za-z0-9_-]{1,80}$/,B=/^(chat|note|meeting|brief):[^\s]{1,200}$/,x=Object.freeze({kind:"owner"});class $ extends Error{constructor(c,h){super(h),this.status=c}}function lt({store:r,notify:c=()=>{},host:h="gateway",now:i=()=>Date.now(),newId:E=()=>rt().replace(/-/g,"").slice(0,20),upstreamOk:k=()=>!1,audit:m=()=>{}}={}){const A=()=>r.ofKind("context").filter(t=>!t.deletedAt),y=t=>r.heads(t),u=(t,e=null,n=x)=>{const o=tt(n,t,{upstreamOk:s=>k(s,n)});return o?{id:t.id,title:t.title,color:t.body?.color||J[0],about:t.body?.about||"",keywords:t.body?.keywords||[],...e?{counts:e.counts,last:e.last}:{},updatedAt:t.updatedAt,instructions:o.instructions,refs:o.refs,mcp_servers:o.mcp_servers,skills:o.skills,...o.access?{access:o.access}:{}}:null},d=(t,e,n)=>q(t,e,n),l=t=>{const e=r.write([t],{host:h,at:i()});return e.written.length&&c({ids:e.written}),r.get(t.id)||t},a=t=>{if(!z.test(String(t||"")))return null;const e=r.get(t);return e&&!e.deletedAt?e:null},N=t=>t?.kind==="agent"?{kind:"agent",id:String(t.name||"unknown")}:{kind:"user",id:t?.kind==="person"?String(t.id):t?.kind==="owner"?"owner":"unknown"},C=(t,e,n,o,s)=>(m("access.denied",{resource:String(e||"context:?").slice(0,120),action:n,actor:N(t)}),new $(o,s)),I=(t,e,n)=>m("access.changed",{resource:e.id,actor:N(t),fields:n}),T=(t,e,n)=>{const o=a(e);if(!o)throw new $(404,"no such context");if(!d(t,o,"list"))throw C(t,e,n,404,"no such context");if(!d(t,o,n))throw C(t,e,n,403,`contexts: ${n} needs a role this caller does not have`);return o},R=t=>B.test(t),M=t=>(Array.isArray(t)?t:[]).slice(0,500);return{list(t=x){const e=A().filter(o=>d(t,o,"list")),n=[...new Set(e.flatMap(o=>_(o)))];return D(e,y(n)).map(o=>u(o.context,o,t)).filter(Boolean)},create(t={},e=x){if(e?.kind!=="owner")throw C(e,"context:new","create",403,"contexts: only the owner makes one here");F(t);const n=j(W({name:t.name,color:t.color,about:t.about,keywords:t.keywords,newId:E,now:i()}),st(t),{now:i()});return u(l(n),null,e)},get(t,e=x){const n=T(e,t,"list");if(!d(e,n,"read"))return{context:u(n,null,e),items:[]};const[o]=D([n],y(_(n)));return{context:u(n,o,e),items:(o?.items||[]).map(s=>({id:s.id,kind:s.kind,title:s.title,updatedAt:s.updatedAt}))}},edit(t,e={},n=x){F(e);const o=e.agents!==void 0||e.noAccess!==void 0,s=T(n,t,o?"manage":"edit"),g=u(l(j(s,e,{now:i()})),null,n);return o&&I(n,s,["agents","noAccess"].filter(w=>e[w]!==void 0)),g},remove(t,e=x){const n=T(e,t,"manage"),o=i();return l({id:n.id,kind:"context",title:n.title,updatedAt:Math.max(o,Number(n.updatedAt)||0),deletedAt:Math.max(o,(Number(n.updatedAt)||0)+1)}),!0},items(t,{add:e=[],remove:n=[]}={},o=x){return this.resources(t,{add:M(e).map(String).filter(R),remove:M(n).map(String).filter(R)},o)},resources(t,{add:e=[],remove:n=[]}={},o=x){let s=T(o,t,"edit");const g=i(),w=S=>{const O=[],P=[];for(const b of M(S))typeof b=="string"&&R(b)?O.push(b):(typeof b=="string"?U(b):b&&U(`${b.type}:${b.id}`))&&P.push(b);return{members:O,refs:P}},f=w(e),v=w(n);return f.members.length&&(s=K(s,f.members,{now:g})),v.members.length&&(s=Z(s,v.members,{now:g})),f.refs.length&&(s=V(s,f.refs,{now:g})),v.refs.length&&(s=Y(s,v.refs,{now:g})),u(l(s),null,o)},people(t,e={},n=x){const o=T(n,t,"manage"),s={};for(const[w,f]of Object.entries(e&&typeof e=="object"?e:{})){if(f!==null&&!G.includes(f))throw new Error(`a role is ${G.join(", ")} or null`);s[w]=f}const g=u(l(p(o,s,{now:i()})),null,n);return Object.keys(s).length&&I(n,o,["people"]),g},suggest({id:t="",title:e="",text:n="",limit:o=3}={},s=x){let g=String(e||""),w=String(n||"");if(t&&B.test(String(t))){const S=r.get(t),O=S?ot(S):null;g=g||S?.title||"",w=w||O?.text||""}const f=A().filter(S=>d(s,S,"edit")),v=y([...new Set(f.flatMap(S=>_(S)))]);return H({title:g,text:w,contexts:f,records:v,limit:Math.min(8,Math.max(1,Number(o)||3))}).filter(S=>!(t&&f.find(O=>O.id===S.id)?.body?.members?.[t]?.on))},turn({chat:t="",named:e=[]}={},n=x){const o=A().filter(f=>d(n,f,"use")),s=et(o,{chatId:String(t||""),named:(Array.isArray(e)?e:[]).map(String).filter(f=>z.test(f))});if(!s.length)return null;const g=y([...new Set(s.flatMap(f=>_(f)))]),w=t?String(t).startsWith("chat:")?String(t):`chat:${t}`:"";return nt(s,{records:g,exclude:w})},scope(t,e=x){const n=T(e,t,"use");return Q(n,y(_(n)))}}}function F(r={}){if(r.instructions!==void 0&&(typeof r.instructions!="string"||r.instructions.length>L))throw new Error(`instructions are a string of at most ${L} characters`);for(const c of["mcp_servers","skills","agents"])if(r[c]!==void 0&&r[c]!==null&&!(Array.isArray(r[c])&&r[c].every(h=>typeof h=="string")))throw new Error(`${c} is a list of names, or null for every one`);if(r.noAccess!==void 0&&!X.includes(r.noAccess))throw new Error(`noAccess is ${X.join(" or ")}`)}const st=r=>Object.fromEntries(["instructions","mcp_servers","skills","noAccess","agents"].filter(c=>r[c]!==void 0).map(c=>[c,r[c]]));async function at(r,c,{pathname:h,api:i,readJson:E,sendJson:k,principal:m=x}){if(!h.startsWith("/v1/contexts"))return!1;const A=(u,d,l="invalid_request_error")=>k(c,u,{error:{message:d,type:l}}),y=u=>{const d=u();return d?k(c,200,{ok:!0,context:d}):A(404,"no such context","not_found")};try{if(h==="/v1/contexts"&&r.method==="GET")return k(c,200,{ok:!0,contexts:i.list(m)}),!0;if(h==="/v1/contexts"&&r.method==="POST"){const a=await E(r);return String(a?.name||"").trim()?(k(c,200,{ok:!0,context:i.create(a,m)}),!0):(A(400,"a context needs a name"),!0)}if(h==="/v1/contexts/suggest"&&r.method==="POST")return k(c,200,{ok:!0,suggestions:i.suggest(await E(r),m)}),!0;if(h==="/v1/contexts/turn"&&r.method==="GET"){const a=new URL(r.url,"http://127.0.0.1").searchParams;return k(c,200,{ok:!0,turn:i.turn({chat:a.get("chat")||"",named:String(a.get("named")||"").split(",").filter(Boolean)},m)}),!0}const u=/^\/v1\/contexts\/(context:[A-Za-z0-9_-]{1,80})(\/items|\/scope|\/resources|\/people)?$/.exec(decodeURIComponent(h));if(!u)return A(404,"no such contexts route","not_found"),!0;const[,d,l]=u;if(!l&&r.method==="GET")return k(c,200,{ok:!0,...i.get(d,m)}),!0;if(!l&&r.method==="PATCH"){const a=await E(r);return y(()=>i.edit(d,a,m)),!0}if(!l&&r.method==="DELETE")return i.remove(d,m),k(c,200,{ok:!0}),!0;if(l==="/items"&&r.method==="POST"){const a=await E(r);return y(()=>i.items(d,a,m)),!0}if(l==="/resources"&&r.method==="POST"){const a=await E(r);return y(()=>i.resources(d,a,m)),!0}if(l==="/people"&&r.method==="POST"){const a=await E(r);return y(()=>i.people(d,a,m)),!0}return l==="/scope"&&r.method==="GET"?(k(c,200,{ok:!0,...i.scope(d,m)}),!0):(A(405,"method not allowed","method_not_allowed"),!0)}catch(u){return u instanceof $?(A(u.status,u.message,u.status===404?"not_found":"forbidden"),!0):(A(400,`contexts: ${u.message}`),!0)}}export{$ as ContextDenied,lt as createContextsApi,at as handleContexts};
package/src/version.js CHANGED
@@ -1 +1 @@
1
- const o="0.161.0";export{o as VERSION};
1
+ const o="0.162.0";export{o as VERSION};