@directive-run/ai 1.14.0 → 1.16.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.
Files changed (75) hide show
  1. package/dist/{chunk-DN5NAJM6.js → chunk-56LYSE63.js} +6 -6
  2. package/dist/chunk-56LYSE63.js.map +1 -0
  3. package/dist/{chunk-A22KLB23.cjs → chunk-7DPTXZZI.cjs} +22 -22
  4. package/dist/chunk-7DPTXZZI.cjs.map +1 -0
  5. package/dist/{chunk-QRXWLD6H.js → chunk-BFSRYCOS.js} +2 -2
  6. package/dist/{chunk-QRXWLD6H.js.map → chunk-BFSRYCOS.js.map} +1 -1
  7. package/dist/{chunk-LKY4K5TV.cjs → chunk-C6MQ4WL6.cjs} +2 -2
  8. package/dist/{chunk-LKY4K5TV.cjs.map → chunk-C6MQ4WL6.cjs.map} +1 -1
  9. package/dist/{chunk-A5K77UDX.cjs → chunk-CAA6Q4YY.cjs} +2 -2
  10. package/dist/{chunk-A5K77UDX.cjs.map → chunk-CAA6Q4YY.cjs.map} +1 -1
  11. package/dist/chunk-D5APTMCT.cjs +16 -0
  12. package/dist/chunk-D5APTMCT.cjs.map +1 -0
  13. package/dist/{chunk-LXUMJKGJ.js → chunk-FN4GUZAI.js} +22 -22
  14. package/dist/chunk-FN4GUZAI.js.map +1 -0
  15. package/dist/{chunk-POBEEJR6.js → chunk-GTB6HTZV.js} +7 -7
  16. package/dist/chunk-GTB6HTZV.js.map +1 -0
  17. package/dist/{chunk-UR4ZGO7V.js → chunk-HRR45JZX.js} +4 -4
  18. package/dist/chunk-HRR45JZX.js.map +1 -0
  19. package/dist/{chunk-PD772MSE.cjs → chunk-MDXDPECP.cjs} +7 -7
  20. package/dist/chunk-MDXDPECP.cjs.map +1 -0
  21. package/dist/{chunk-FABDFT74.cjs → chunk-PB276BX2.cjs} +6 -6
  22. package/dist/chunk-PB276BX2.cjs.map +1 -0
  23. package/dist/{chunk-NNAQ4ZH2.js → chunk-V2FCPNPP.js} +2 -2
  24. package/dist/{chunk-NNAQ4ZH2.js.map → chunk-V2FCPNPP.js.map} +1 -1
  25. package/dist/{chunk-FFRWQNK7.cjs → chunk-WCQK3JOR.cjs} +4 -4
  26. package/dist/chunk-WCQK3JOR.cjs.map +1 -0
  27. package/dist/{chunk-IUGSMTBE.js → chunk-YT4QN3KO.js} +8 -8
  28. package/dist/chunk-YT4QN3KO.js.map +1 -0
  29. package/dist/gemini.cjs +1 -1
  30. package/dist/gemini.cjs.map +1 -1
  31. package/dist/gemini.js +1 -1
  32. package/dist/gemini.js.map +1 -1
  33. package/dist/guardrails.cjs +1 -1
  34. package/dist/guardrails.js +1 -1
  35. package/dist/index.cjs +5 -5
  36. package/dist/index.cjs.map +1 -1
  37. package/dist/index.d.cts +1 -1
  38. package/dist/index.d.ts +1 -1
  39. package/dist/index.js +2 -2
  40. package/dist/index.js.map +1 -1
  41. package/dist/mcp.cjs +1 -1
  42. package/dist/mcp.js +1 -1
  43. package/dist/multi-agent-orchestrator-KFYIDKTF.cjs +2 -0
  44. package/dist/{multi-agent-orchestrator-Y5U4JCFO.cjs.map → multi-agent-orchestrator-KFYIDKTF.cjs.map} +1 -1
  45. package/dist/{multi-agent-orchestrator-QWWQKKGX.js → multi-agent-orchestrator-ZHSMWIKP.js} +2 -2
  46. package/dist/{multi-agent-orchestrator-QWWQKKGX.js.map → multi-agent-orchestrator-ZHSMWIKP.js.map} +1 -1
  47. package/dist/multi-agent.cjs +1 -1
  48. package/dist/multi-agent.d.cts +2 -2
  49. package/dist/multi-agent.d.ts +2 -2
  50. package/dist/multi-agent.js +1 -1
  51. package/dist/openai.cjs +1 -1
  52. package/dist/openai.cjs.map +1 -1
  53. package/dist/openai.js +1 -1
  54. package/dist/openai.js.map +1 -1
  55. package/dist/{orchestrator-types-DeIMRLR7.d.cts → orchestrator-types-CbNE7Rjp.d.cts} +1 -1
  56. package/dist/{orchestrator-types-DGBhL6mc.d.ts → orchestrator-types-ZK9lv5Sm.d.ts} +1 -1
  57. package/dist/predicate.cjs +1 -1
  58. package/dist/predicate.js +1 -1
  59. package/dist/testing.cjs +1 -1
  60. package/dist/testing.d.cts +1 -1
  61. package/dist/testing.d.ts +1 -1
  62. package/dist/testing.js +1 -1
  63. package/package.json +2 -2
  64. package/dist/chunk-2TD4ZSGZ.cjs +0 -16
  65. package/dist/chunk-2TD4ZSGZ.cjs.map +0 -1
  66. package/dist/chunk-A22KLB23.cjs.map +0 -1
  67. package/dist/chunk-DN5NAJM6.js.map +0 -1
  68. package/dist/chunk-FABDFT74.cjs.map +0 -1
  69. package/dist/chunk-FFRWQNK7.cjs.map +0 -1
  70. package/dist/chunk-IUGSMTBE.js.map +0 -1
  71. package/dist/chunk-LXUMJKGJ.js.map +0 -1
  72. package/dist/chunk-PD772MSE.cjs.map +0 -1
  73. package/dist/chunk-POBEEJR6.js.map +0 -1
  74. package/dist/chunk-UR4ZGO7V.js.map +0 -1
  75. package/dist/multi-agent-orchestrator-Y5U4JCFO.cjs +0 -2
@@ -7,5 +7,5 @@ Provide a real 'clientFactory' option to connect to actual MCP servers:
7
7
  const adapter = createMCPAdapter({
8
8
  servers: [...],
9
9
  clientFactory: (config) => new Client(config),
10
- });`):v&&console.debug("[Directive MCP] Using stub client for development. Provide 'clientFactory' for production use."));let b=i.clientFactory??(e=>O(e,v)),A=i.approvalTimeoutMs??3e5,n={servers:new Map,toolConstraints:new Map(Object.entries(c)),rateLimiters:new Map,pendingApprovals:new Map,approvedRequests:new Set,rejectedRequests:new Set},E=0,d=new Map,M=new Map;function D(e){return new Promise((t,r)=>{if(n.approvedRequests.has(e)){n.approvedRequests.delete(e),n.pendingApprovals.delete(e),s.onApprovalResolved?.(e,true),t();return}if(n.rejectedRequests.has(e)){n.rejectedRequests.delete(e),n.pendingApprovals.delete(e);let l=M.get(e);M.delete(e),s.onApprovalResolved?.(e,false),r(new Error(`[Directive MCP] Tool call request ${e} was rejected${l?`: ${l}`:""}`));return}let o=setTimeout(()=>{d.delete(e),n.pendingApprovals.delete(e),r(new Error(`[Directive MCP] Approval timeout: Request ${e} was not approved or rejected within ${A}ms. Call adapter.approve("${e}") or adapter.reject("${e}") to resolve.`));},A);d.set(e,{resolve:t,reject:r,timeoutId:o});})}function $(e,t,r){let o=d.get(e);o?(clearTimeout(o.timeoutId),d.delete(e),n.pendingApprovals.delete(e),s.onApprovalResolved?.(e,t),t?o.resolve():o.reject(new Error(`[Directive MCP] Tool call request ${e} was rejected${r?`: ${r}`:""}`))):(t?n.approvedRequests.add(e):(n.rejectedRequests.add(e),r&&M.set(e,r)),setTimeout(()=>{n.approvedRequests.delete(e),n.rejectedRequests.delete(e),M.delete(e),n.pendingApprovals.delete(e);},A));}let w=new Map;for(let e of a)n.servers.set(e.name,{config:e,client:null,tools:[],resources:[],status:"disconnected"}),w.set(e.name,{timer:null,attempts:0,maxAttempts:e.retry?.maxAttempts??10,baseDelay:e.retry?.backoffMs??5e3});function x(e){let t=/[;|&$`><]/;if(e.command&&t.test(e.command))throw new Error(`[Directive MCP] Stdio command for server '${e.name}' contains shell metacharacters: '${e.command}'. This may indicate a command injection vulnerability. Use 'args' for command arguments instead.`);if(e.args){for(let r of e.args)if(t.test(r))throw new Error(`[Directive MCP] Stdio argument for server '${e.name}' contains shell metacharacters: '${r}'. This may indicate a command injection vulnerability.`)}}async function S(e){let t=n.servers.get(e);if(!t)throw new Error(`Unknown MCP server: ${e}`);if(t.status!=="connected"){t.config.transport==="stdio"&&x(t.config),t.status="connecting";try{let r=b(t.config);await r.connect(),t.client=r,t.status="connected";let o=w.get(e);o&&(o.attempts=0,o.timer&&(clearTimeout(o.timer),o.timer=null)),r.getCapabilities().tools&&(t.tools=await r.listTools()),r.getCapabilities().resources&&(t.resources=await r.listResources()),t.lastSync=Date.now(),s.onConnect?.(e);}catch(r){if(t.status="error",t.error=r instanceof Error?r:new Error(String(r)),s.onError?.(e,t.error),m){let o=w.get(e);if(o&&o.attempts<o.maxAttempts){o.attempts++;let l=Math.min(o.baseDelay*2**(o.attempts-1)+Math.random()*1e3,6e4);o.timer=setTimeout(()=>{o.timer=null,S(e).catch(()=>{});},l);}else o&&console.error(`[Directive MCP] Max reconnect attempts (${o.maxAttempts}) reached for server '${e}'. Call adapter.connectServer("${e}") to retry manually.`);}throw t.error}}}async function T(e){let t=w.get(e);t?.timer&&(clearTimeout(t.timer),t.timer=null,t.attempts=0);let r=n.servers.get(e);if(!(!r||!r.client))try{await r.client.disconnect();}finally{r.status="disconnected",r.client=null,s.onDisconnect?.(e);}}async function k(e,t,r,o){let l=n.servers.get(e);if(!l)throw new Error(`[Directive MCP] Unknown server '${e}'. Available servers: ${Array.from(n.servers.keys()).join(", ")||"(none)"}`);if(!l.client)throw new Error(`[Directive MCP] Server '${e}' is not connected. Call 'adapter.connect()' or 'adapter.connectServer("${e}")' first.`);let f=`${e}.${t}`,u=n.toolConstraints.get(f);if(u){if(u.rateLimit){let p=n.rateLimiters.get(f);if(!_(n.rateLimiters,f,u.rateLimit)){let C=p?.resetTime?new Date(p.resetTime).toISOString():"unknown";throw new Error(`[Directive MCP] Rate limit exceeded for '${f}': ${p?.count??0}/${u.rateLimit} requests per minute. Resets at ${C}.`)}}if(u.maxArgSize){let p=JSON.stringify(r).length;if(p>u.maxArgSize)throw new Error(`Arguments exceed max size (${p} > ${u.maxArgSize})`)}if(u.when&&!await u.when(o,r))throw new Error(`Constraint not satisfied for ${f}`);if(u.requireApproval){let p=`approval-${++E}-${Date.now()}`,C={id:p,server:e,tool:t,args:r,requestedAt:Date.now()};n.pendingApprovals.set(p,C),s.onApprovalRequest?.(C),await D(p);}}s.onToolCall?.(e,t,r);let R=await Promise.race([l.client.callTool(t,r),new Promise((p,C)=>setTimeout(()=>C(new Error(`Tool call timeout: ${f}`)),u?.timeout??3e4))]);return s.onToolResult?.(e,t,R),R}let j={name:"mcp-adapter",onInit:async()=>{P&&await Promise.all(Array.from(n.servers.keys()).map(e=>S(e).catch(t=>console.error(`Failed to connect to ${e}:`,t))));},onDestroy:async()=>{for(let e of w.values())e.timer&&(clearTimeout(e.timer),e.timer=null);for(let[,e]of d)clearTimeout(e.timeoutId),e.reject(new Error("[Directive MCP] Adapter destroyed while awaiting approval"));d.clear(),await Promise.all(Array.from(n.servers.keys()).map(e=>T(e).catch(t=>console.error(`Failed to disconnect from ${e}:`,t))));}};async function L(e){for(let t of g)for(let[r,o]of n.servers)if(o.client){for(let l of o.resources)if(typeof t.pattern=="string"?U(l.uri,t.pattern):t.pattern.test(l.uri))try{let u=await o.client.readResource(l.uri),R=u.contents[0]?.text??"",p=t.transform?t.transform(R):R;e[t.factKey]=p,s.onResourceUpdate?.(r,l.uri,u);}catch(u){console.error(`Failed to sync resource ${l.uri}:`,u);}}}return {plugin:j,async connect(){await Promise.all(Array.from(n.servers.keys()).map(S));},connectServer:S,async disconnect(){await Promise.all(Array.from(n.servers.keys()).map(T));},disconnectServer:T,getTools(){let e=new Map;for(let[t,r]of n.servers)e.set(t,r.tools);return e},getResources(){let e=new Map;for(let[t,r]of n.servers)e.set(t,r.resources);return e},async callTool(e,t,r,o){return k(e,t,r,o)},async callToolDirect(e,t,r){if(!h)throw new Error("[Directive] callToolDirect is disabled by default. Pass { allowDirectCalls: true } to enable unconstrained tool calls.");process.env.NODE_ENV!=="production"&&console.warn("[Directive] callToolDirect bypasses all constraints (rate limits, approvals, timeouts).");let o=n.servers.get(e);if(!o)throw new Error(`[Directive MCP] Unknown server '${e}'. Available servers: ${Array.from(n.servers.keys()).join(", ")||"(none)"}`);if(!o.client)throw new Error(`[Directive MCP] Server '${e}' is not connected. Call 'adapter.connect()' or 'adapter.connectServer("${e}")' first.`);s.onToolCall?.(e,t,r);let l=await o.client.callTool(t,r);return s.onToolResult?.(e,t,l),l},async readResource(e,t){let r=n.servers.get(e);if(!r)throw new Error(`[Directive MCP] Unknown server '${e}'. Available servers: ${Array.from(n.servers.keys()).join(", ")||"(none)"}`);if(!r.client)throw new Error(`[Directive MCP] Server '${e}' is not connected. Call 'adapter.connect()' or 'adapter.connectServer("${e}")' first.`);let o=await r.client.readResource(t);return s.onResourceUpdate?.(e,t,o),o},syncResources:L,getServerStatus(e){return n.servers.get(e)},getAllServerStatuses(){return new Map(n.servers)},approve(e){if(!n.pendingApprovals.get(e)&&!d.has(e))throw new Error(`[Directive MCP] No pending approval request with ID '${e}'. Pending requests: ${Array.from(n.pendingApprovals.keys()).join(", ")||"(none)"}`);$(e,true);},reject(e,t){if(!n.pendingApprovals.get(e)&&!d.has(e))throw new Error(`[Directive MCP] No pending approval request with ID '${e}'. Pending requests: ${Array.from(n.pendingApprovals.keys()).join(", ")||"(none)"}`);$(e,false,t);},getPendingApprovals(){return Array.from(n.pendingApprovals.values())},getRejectionReason(e){return M.get(e)}}}var y=new Map,N=200;function U(i,a){let c=y.get(a);if(!c){let g=a.replace(/\*\*/g,"\0GLOBSTAR\0").replace(/\*/g,"\0STAR\0").replace(/\?/g,"\0QUESTION\0").replace(/[.+^${}()|[\]\\]/g,"\\$&").replace(/\0GLOBSTAR\0/g,".*").replace(/\0STAR\0/g,"[^/]*").replace(/\0QUESTION\0/g,".");if(c=new RegExp(`^${g}$`),y.size>=N){let s=y.keys().next().value;s!==void 0&&y.delete(s);}y.set(a,c);}return c.test(i)}function G(i){let a=[];for(let[c,g]of i)for(let s of g)a.push({type:"function",function:{name:`${c}.${s.name}`,description:s.description??`Tool: ${s.name}`,parameters:s.inputSchema}});return a}function z(i,a,c){return {type:"MCP_CALL_TOOL",server:i,tool:a,args:c}}function B(i,a){return {type:"MCP_READ_RESOURCE",server:i,uri:a}}function K(i,a,c){return {type:"MCP_GET_PROMPT",server:i,prompt:a,args:c}}function W(i,a){return {type:"MCP_SYNC_RESOURCES",server:i,pattern:a}}exports.a=F;exports.b=G;exports.c=z;exports.d=B;exports.e=K;exports.f=W;//# sourceMappingURL=chunk-LKY4K5TV.cjs.map
11
- //# sourceMappingURL=chunk-LKY4K5TV.cjs.map
10
+ });`):v&&console.debug("[Directive MCP] Using stub client for development. Provide 'clientFactory' for production use."));let b=i.clientFactory??(e=>O(e,v)),A=i.approvalTimeoutMs??3e5,n={servers:new Map,toolConstraints:new Map(Object.entries(c)),rateLimiters:new Map,pendingApprovals:new Map,approvedRequests:new Set,rejectedRequests:new Set},E=0,d=new Map,M=new Map;function D(e){return new Promise((t,r)=>{if(n.approvedRequests.has(e)){n.approvedRequests.delete(e),n.pendingApprovals.delete(e),s.onApprovalResolved?.(e,true),t();return}if(n.rejectedRequests.has(e)){n.rejectedRequests.delete(e),n.pendingApprovals.delete(e);let l=M.get(e);M.delete(e),s.onApprovalResolved?.(e,false),r(new Error(`[Directive MCP] Tool call request ${e} was rejected${l?`: ${l}`:""}`));return}let o=setTimeout(()=>{d.delete(e),n.pendingApprovals.delete(e),r(new Error(`[Directive MCP] Approval timeout: Request ${e} was not approved or rejected within ${A}ms. Call adapter.approve("${e}") or adapter.reject("${e}") to resolve.`));},A);d.set(e,{resolve:t,reject:r,timeoutId:o});})}function $(e,t,r){let o=d.get(e);o?(clearTimeout(o.timeoutId),d.delete(e),n.pendingApprovals.delete(e),s.onApprovalResolved?.(e,t),t?o.resolve():o.reject(new Error(`[Directive MCP] Tool call request ${e} was rejected${r?`: ${r}`:""}`))):(t?n.approvedRequests.add(e):(n.rejectedRequests.add(e),r&&M.set(e,r)),setTimeout(()=>{n.approvedRequests.delete(e),n.rejectedRequests.delete(e),M.delete(e),n.pendingApprovals.delete(e);},A));}let w=new Map;for(let e of a)n.servers.set(e.name,{config:e,client:null,tools:[],resources:[],status:"disconnected"}),w.set(e.name,{timer:null,attempts:0,maxAttempts:e.retry?.maxAttempts??10,baseDelay:e.retry?.backoffMs??5e3});function x(e){let t=/[;|&$`><]/;if(e.command&&t.test(e.command))throw new Error(`[Directive MCP] Stdio command for server '${e.name}' contains shell metacharacters: '${e.command}'. This may indicate a command injection vulnerability. Use 'args' for command arguments instead.`);if(e.args){for(let r of e.args)if(t.test(r))throw new Error(`[Directive MCP] Stdio argument for server '${e.name}' contains shell metacharacters: '${r}'. This may indicate a command injection vulnerability.`)}}async function S(e){let t=n.servers.get(e);if(!t)throw new Error(`Unknown MCP server: ${e}`);if(t.status!=="connected"){t.config.transport==="stdio"&&x(t.config),t.status="connecting";try{let r=b(t.config);await r.connect(),t.client=r,t.status="connected";let o=w.get(e);o&&(o.attempts=0,o.timer&&(clearTimeout(o.timer),o.timer=null)),r.getCapabilities().tools&&(t.tools=await r.listTools()),r.getCapabilities().resources&&(t.resources=await r.listResources()),t.lastSync=Date.now(),s.onConnect?.(e);}catch(r){if(t.status="error",t.error=r instanceof Error?r:new Error(String(r)),s.onError?.(e,t.error),m){let o=w.get(e);if(o&&o.attempts<o.maxAttempts){o.attempts++;let l=Math.min(o.baseDelay*2**(o.attempts-1)+Math.random()*1e3,6e4);o.timer=setTimeout(()=>{o.timer=null,S(e).catch(()=>{});},l);}else o&&console.error(`[Directive MCP] Max reconnect attempts (${o.maxAttempts}) reached for server '${e}'. Call adapter.connectServer("${e}") to retry manually.`);}throw t.error}}}async function T(e){let t=w.get(e);t?.timer&&(clearTimeout(t.timer),t.timer=null,t.attempts=0);let r=n.servers.get(e);if(!(!r||!r.client))try{await r.client.disconnect();}finally{r.status="disconnected",r.client=null,s.onDisconnect?.(e);}}async function k(e,t,r,o){let l=n.servers.get(e);if(!l)throw new Error(`[Directive MCP] Unknown server '${e}'. Available servers: ${Array.from(n.servers.keys()).join(", ")||"(none)"}`);if(!l.client)throw new Error(`[Directive MCP] Server '${e}' is not connected. Call 'adapter.connect()' or 'adapter.connectServer("${e}")' first.`);let f=`${e}.${t}`,u=n.toolConstraints.get(f);if(u){if(u.rateLimit){let p=n.rateLimiters.get(f);if(!_(n.rateLimiters,f,u.rateLimit)){let C=p?.resetTime?new Date(p.resetTime).toISOString():"unknown";throw new Error(`[Directive MCP] Rate limit exceeded for '${f}': ${p?.count??0}/${u.rateLimit} requests per minute. Resets at ${C}.`)}}if(u.maxArgSize){let p=JSON.stringify(r).length;if(p>u.maxArgSize)throw new Error(`Arguments exceed max size (${p} > ${u.maxArgSize})`)}if(u.when&&!await u.when(o,r))throw new Error(`Constraint not satisfied for ${f}`);if(u.requireApproval){let p=`approval-${++E}-${Date.now()}`,C={id:p,server:e,tool:t,args:r,requestedAt:Date.now()};n.pendingApprovals.set(p,C),s.onApprovalRequest?.(C),await D(p);}}s.onToolCall?.(e,t,r);let R=await Promise.race([l.client.callTool(t,r),new Promise((p,C)=>setTimeout(()=>C(new Error(`Tool call timeout: ${f}`)),u?.timeout??3e4))]);return s.onToolResult?.(e,t,R),R}let j={name:"mcp-adapter",onInit:async()=>{P&&await Promise.all(Array.from(n.servers.keys()).map(e=>S(e).catch(t=>console.error(`Failed to connect to ${e}:`,t))));},onDestroy:async()=>{for(let e of w.values())e.timer&&(clearTimeout(e.timer),e.timer=null);for(let[,e]of d)clearTimeout(e.timeoutId),e.reject(new Error("[Directive MCP] Adapter destroyed while awaiting approval"));d.clear(),await Promise.all(Array.from(n.servers.keys()).map(e=>T(e).catch(t=>console.error(`Failed to disconnect from ${e}:`,t))));}};async function L(e){for(let t of g)for(let[r,o]of n.servers)if(o.client){for(let l of o.resources)if(typeof t.pattern=="string"?U(l.uri,t.pattern):t.pattern.test(l.uri))try{let u=await o.client.readResource(l.uri),R=u.contents[0]?.text??"",p=t.transform?t.transform(R):R;e[t.factKey]=p,s.onResourceUpdate?.(r,l.uri,u);}catch(u){console.error(`Failed to sync resource ${l.uri}:`,u);}}}return {plugin:j,async connect(){await Promise.all(Array.from(n.servers.keys()).map(S));},connectServer:S,async disconnect(){await Promise.all(Array.from(n.servers.keys()).map(T));},disconnectServer:T,getTools(){let e=new Map;for(let[t,r]of n.servers)e.set(t,r.tools);return e},getResources(){let e=new Map;for(let[t,r]of n.servers)e.set(t,r.resources);return e},async callTool(e,t,r,o){return k(e,t,r,o)},async callToolDirect(e,t,r){if(!h)throw new Error("[Directive] callToolDirect is disabled by default. Pass { allowDirectCalls: true } to enable unconstrained tool calls.");process.env.NODE_ENV!=="production"&&console.warn("[Directive] callToolDirect bypasses all constraints (rate limits, approvals, timeouts).");let o=n.servers.get(e);if(!o)throw new Error(`[Directive MCP] Unknown server '${e}'. Available servers: ${Array.from(n.servers.keys()).join(", ")||"(none)"}`);if(!o.client)throw new Error(`[Directive MCP] Server '${e}' is not connected. Call 'adapter.connect()' or 'adapter.connectServer("${e}")' first.`);s.onToolCall?.(e,t,r);let l=await o.client.callTool(t,r);return s.onToolResult?.(e,t,l),l},async readResource(e,t){let r=n.servers.get(e);if(!r)throw new Error(`[Directive MCP] Unknown server '${e}'. Available servers: ${Array.from(n.servers.keys()).join(", ")||"(none)"}`);if(!r.client)throw new Error(`[Directive MCP] Server '${e}' is not connected. Call 'adapter.connect()' or 'adapter.connectServer("${e}")' first.`);let o=await r.client.readResource(t);return s.onResourceUpdate?.(e,t,o),o},syncResources:L,getServerStatus(e){return n.servers.get(e)},getAllServerStatuses(){return new Map(n.servers)},approve(e){if(!n.pendingApprovals.get(e)&&!d.has(e))throw new Error(`[Directive MCP] No pending approval request with ID '${e}'. Pending requests: ${Array.from(n.pendingApprovals.keys()).join(", ")||"(none)"}`);$(e,true);},reject(e,t){if(!n.pendingApprovals.get(e)&&!d.has(e))throw new Error(`[Directive MCP] No pending approval request with ID '${e}'. Pending requests: ${Array.from(n.pendingApprovals.keys()).join(", ")||"(none)"}`);$(e,false,t);},getPendingApprovals(){return Array.from(n.pendingApprovals.values())},getRejectionReason(e){return M.get(e)}}}var y=new Map,N=200;function U(i,a){let c=y.get(a);if(!c){let g=a.replace(/\*\*/g,"\0GLOBSTAR\0").replace(/\*/g,"\0STAR\0").replace(/\?/g,"\0QUESTION\0").replace(/[.+^${}()|[\]\\]/g,"\\$&").replace(/\0GLOBSTAR\0/g,".*").replace(/\0STAR\0/g,"[^/]*").replace(/\0QUESTION\0/g,".");if(c=new RegExp(`^${g}$`),y.size>=N){let s=y.keys().next().value;s!==void 0&&y.delete(s);}y.set(a,c);}return c.test(i)}function G(i){let a=[];for(let[c,g]of i)for(let s of g)a.push({type:"function",function:{name:`${c}.${s.name}`,description:s.description??`Tool: ${s.name}`,parameters:s.inputSchema}});return a}function z(i,a,c){return {type:"MCP_CALL_TOOL",server:i,tool:a,args:c}}function B(i,a){return {type:"MCP_READ_RESOURCE",server:i,uri:a}}function K(i,a,c){return {type:"MCP_GET_PROMPT",server:i,prompt:a,args:c}}function W(i,a){return {type:"MCP_SYNC_RESOURCES",server:i,pattern:a}}exports.a=F;exports.b=G;exports.c=z;exports.d=B;exports.e=K;exports.f=W;//# sourceMappingURL=chunk-C6MQ4WL6.cjs.map
11
+ //# sourceMappingURL=chunk-C6MQ4WL6.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/mcp.ts"],"names":["createStubClient","config","debug","connected","tools","resources","log","msg","args","name","uri","checkRateLimit","rateLimiters","key","limit","now","limiter","createMCPAdapter","servers","toolConstraints","resourceMappings","events","autoConnect","autoReconnect","allowDirectCalls","clientFactory","serverConfig","approvalTimeoutMs","state","approvalCounter","approvalWaiters","rejectionReasons","waitForApproval","requestId","resolve","reject","reason","timeoutId","resolveApproval","approved","waiter","reconnectState","validateStdioCommand","shellMetachars","arg","connectServer","serverState","client","rState","error","delay","disconnectServer","callToolWithConstraints","server","tool","facts","constraintKey","constraint","resetAt","argSize","approvalRequest","result","_","plugin","e","syncResources","mapping","serverName","resource","matchGlob","content","value","globCache","MAX_GLOB_CACHE_SIZE","str","pattern","regex","regexPattern","firstKey","convertToolsForLLM","serverTools","mcpCallTool","mcpReadResource","mcpGetPrompt","prompt","mcpSyncResources"],"mappings":"aAiKA,SAASA,CAAAA,CAAiBC,CAAAA,CAAyBC,CAAAA,CAAQ,KAAA,CAAkB,CAC3E,IAAIC,CAAAA,CAAY,KAAA,CACVC,CAAAA,CAAmB,EAAC,CACpBC,CAAAA,CAA2B,EAAC,CAE5BC,CAAAA,CAAMJ,CAAAA,CACR,CAACK,CAAAA,CAAAA,GAAgBC,CAAAA,GACf,OAAA,CAAQ,KAAA,CAAM,CAAA,WAAA,EA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ASoB,EACd5B,CAAAA,CACAC,CAAAA,CACA9C,EACwB,CACxB,OAAO,CAAE,IAAA,CAAM,eAAA,CAAiB,MAAA,CAAA6C,CAAAA,CAAQ,IAAA,CAAAC,CAAAA,CAAM,KAAA9C,CAAK,CACrD,CAKO,SAAS0E,CAAAA,CACd7B,EACA3C,CAAAA,CAC4B,CAC5B,OAAO,CAAE,IAAA,CAAM,mBAAA,CAAqB,OAAA2C,CAAAA,CAAQ,GAAA,CAAA3C,CAAI,CAClD,CAKO,SAASyE,CAAAA,CACd9B,CAAAA,CACA+B,CAAAA,CACA5E,CAAAA,CACyB,CACzB,OAAO,CAAE,IAAA,CAAM,gBAAA,CAAkB,OAAA6C,CAAAA,CAAQ,MAAA,CAAA+B,EAAQ,IAAA,CAAA5E,CAAK,CACxD,CAKO,SAAS6E,CAAAA,CACdhC,EACAsB,CAAAA,CAC6B,CAC7B,OAAO,CAAE,IAAA,CAAM,qBAAsB,MAAA,CAAAtB,CAAAA,CAAQ,OAAA,CAAAsB,CAAQ,CACvD","file":"chunk-LKY4K5TV.cjs","sourcesContent":["/**\n * MCP Adapter - Model Context Protocol Integration for Directive\n *\n * Provides seamless integration between Directive's constraint system and MCP servers:\n * - MCP tools become Directive resolvers with constraint-driven access control\n * - MCP resources sync to Directive facts\n * - MCP prompts available through requirements\n *\n * @example\n * ```typescript\n * import { createMCPAdapter } from '@directive-run/ai';\n *\n * const mcpAdapter = createMCPAdapter({\n * servers: [\n * { name: 'filesystem', transport: 'stdio', command: 'mcp-server-filesystem' },\n * { name: 'github', transport: 'sse', url: 'https://mcp.github.com' }\n * ],\n * toolConstraints: {\n * 'filesystem.write': { requireApproval: true },\n * 'github.create_pr': { when: (facts) => facts.reviewComplete }\n * }\n * });\n *\n * const system = createSystem({\n * module: myModule,\n * plugins: [mcpAdapter.plugin]\n * });\n * ```\n */\n\nimport type { Plugin } from \"@directive-run/core\";\nimport type {\n MCPAdapterConfig,\n MCPApprovalRequest,\n MCPCallToolRequirement,\n MCPClient,\n MCPGetPromptRequirement,\n MCPReadResourceRequirement,\n MCPResource,\n MCPResourceMapping,\n MCPResourceResult,\n MCPServerConfig,\n MCPSyncResourcesRequirement,\n MCPTool,\n MCPToolConstraint,\n MCPToolResult,\n} from \"./mcp-types.js\";\n\n// ============================================================================\n// MCP Adapter State\n// ============================================================================\n\n/** State of an MCP server connection */\ninterface MCPServerState {\n config: MCPServerConfig;\n client: MCPClient | null;\n tools: MCPTool[];\n resources: MCPResource[];\n status: \"disconnected\" | \"connecting\" | \"connected\" | \"error\";\n error?: Error;\n lastSync?: number;\n}\n\n/** Internal state for the adapter */\ninterface MCPAdapterState {\n servers: Map<string, MCPServerState>;\n toolConstraints: Map<string, MCPToolConstraint>;\n resourceMappings: MCPResourceMapping[];\n rateLimiters: Map<string, { count: number; resetTime: number }>;\n /** Pending approval requests */\n pendingApprovals: Map<string, MCPApprovalRequest>;\n /** Approved request IDs */\n approvedRequests: Set<string>;\n /** Rejected request IDs */\n rejectedRequests: Set<string>;\n}\n\n// ============================================================================\n// MCP Adapter Instance\n// ============================================================================\n\n/** MCP Adapter instance */\nexport interface MCPAdapter {\n /** Plugin to add to Directive system */\n plugin: Plugin;\n /** Connect to all configured servers */\n connect(): Promise<void>;\n /** Connect to a specific server */\n connectServer(name: string): Promise<void>;\n /** Disconnect from all servers */\n disconnect(): Promise<void>;\n /** Disconnect from a specific server */\n disconnectServer(name: string): Promise<void>;\n /** Get all available tools across all servers */\n getTools(): Map<string, MCPTool[]>;\n /** Get all available resources across all servers */\n getResources(): Map<string, MCPResource[]>;\n /**\n * Call a tool with constraint checking (recommended).\n * Applies rate limits, argument size limits, approval workflow, and custom constraints.\n * @param server - Server name\n * @param tool - Tool name\n * @param args - Tool arguments\n * @param facts - Current facts for constraint evaluation\n */\n callTool(\n server: string,\n tool: string,\n args: Record<string, unknown>,\n facts: Record<string, unknown>,\n ): Promise<MCPToolResult>;\n /**\n * Call a tool directly, bypassing all constraints (rate limits, approvals, etc.).\n * Use only for trusted internal calls where constraint checking is not needed.\n */\n callToolDirect(\n server: string,\n tool: string,\n args: Record<string, unknown>,\n ): Promise<MCPToolResult>;\n /** Read a resource directly */\n readResource(server: string, uri: string): Promise<MCPResourceResult>;\n /** Sync resources to facts */\n syncResources(facts: Record<string, unknown>): Promise<void>;\n /** Get server status */\n getServerStatus(name: string): MCPServerState | undefined;\n /** Get all server statuses */\n getAllServerStatuses(): Map<string, MCPServerState>;\n /** Approve a pending tool call request */\n approve(requestId: string): void;\n /** Reject a pending tool call request */\n reject(requestId: string, reason?: string): void;\n /** Get pending approval requests */\n getPendingApprovals(): MCPApprovalRequest[];\n /** Get the rejection reason for a request (if available) */\n getRejectionReason(requestId: string): string | undefined;\n}\n\n// ============================================================================\n// Default MCP Client (Stub)\n// ============================================================================\n\n/**\n * Create a stub MCP client for development/testing.\n *\n * **Important:** This stub is for development only. In production, provide\n * a real MCP client via the `clientFactory` option:\n *\n * @example\n * ```typescript\n * import { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\n *\n * const adapter = createMCPAdapter({\n * servers: [...],\n * clientFactory: (config) => new Client(config),\n * });\n * ```\n *\n * @param config - Server configuration\n * @param debug - Enable debug logging (default: false)\n */\nfunction createStubClient(config: MCPServerConfig, debug = false): MCPClient {\n let connected = false;\n const tools: MCPTool[] = [];\n const resources: MCPResource[] = [];\n\n const log = debug\n ? (msg: string, ...args: unknown[]) =>\n console.debug(`[MCP Stub] ${msg}`, ...args)\n : () => {};\n\n return {\n async connect() {\n log(`Connecting to ${config.name} (${config.transport})`);\n connected = true;\n },\n async disconnect() {\n connected = false;\n },\n isConnected() {\n return connected;\n },\n getCapabilities() {\n return { tools: true, resources: true, prompts: true };\n },\n async listTools() {\n return tools;\n },\n async callTool(name: string, args: Record<string, unknown>) {\n log(`Calling tool ${name}`, args);\n return {\n content: [{ type: \"text\" as const, text: `Stub result for ${name}` }],\n };\n },\n async listResources() {\n return resources;\n },\n async readResource(uri: string) {\n log(`Reading resource ${uri}`);\n return {\n contents: [{ uri, text: `Stub content for ${uri}` }],\n };\n },\n async listPrompts() {\n return [];\n },\n async getPrompt(name: string) {\n return {\n messages: [\n {\n role: \"user\" as const,\n content: { type: \"text\" as const, text: `Stub prompt ${name}` },\n },\n ],\n };\n },\n };\n}\n\n// ============================================================================\n// Rate Limiter\n// ============================================================================\n\nfunction checkRateLimit(\n rateLimiters: Map<string, { count: number; resetTime: number }>,\n key: string,\n limit: number,\n): boolean {\n const now = Date.now();\n const limiter = rateLimiters.get(key);\n\n if (!limiter || now > limiter.resetTime) {\n rateLimiters.set(key, { count: 1, resetTime: now + 60000 });\n return true;\n }\n\n if (limiter.count >= limit) {\n return false;\n }\n\n limiter.count++;\n return true;\n}\n\n// ============================================================================\n// MCP Adapter Factory\n// ============================================================================\n\n/**\n * Create an MCP adapter for Directive integration.\n *\n * @example\n * ```typescript\n * const adapter = createMCPAdapter({\n * servers: [\n * { name: 'fs', transport: 'stdio', command: 'mcp-server-filesystem' },\n * ],\n * toolConstraints: {\n * 'fs.write_file': {\n * requireApproval: true,\n * maxArgSize: 10000,\n * timeout: 30000,\n * },\n * },\n * resourceMappings: [\n * {\n * pattern: 'file://*.json',\n * factKey: 'jsonFiles',\n * mode: 'poll',\n * pollInterval: 5000,\n * },\n * ],\n * });\n *\n * // Add to system\n * const system = createSystem({\n * module: myModule,\n * plugins: [adapter.plugin],\n * });\n *\n * // Connect to servers\n * await adapter.connect();\n * ```\n */\nexport function createMCPAdapter(config: MCPAdapterConfig): MCPAdapter {\n const {\n servers,\n toolConstraints = {},\n resourceMappings = [],\n events = {},\n autoConnect = false,\n autoReconnect = true,\n debug = false,\n allowDirectCalls = false,\n } = config;\n\n // Warn if using stub client in production\n const usingStubClient = !config.clientFactory;\n if (usingStubClient) {\n const isProduction =\n typeof process !== \"undefined\" && process.env?.NODE_ENV === \"production\";\n if (isProduction) {\n console.warn(\n \"[Directive MCP] WARNING: Using stub MCP client in production!\\n\" +\n \"The stub client returns mock data and does not connect to real MCP servers.\\n\" +\n \"Provide a real 'clientFactory' option to connect to actual MCP servers:\\n\\n\" +\n \" import { Client } from '@modelcontextprotocol/sdk/client/index.js';\\n\\n\" +\n \" const adapter = createMCPAdapter({\\n\" +\n \" servers: [...],\\n\" +\n \" clientFactory: (config) => new Client(config),\\n\" +\n \" });\",\n );\n } else if (debug) {\n console.debug(\n \"[Directive MCP] Using stub client for development. \" +\n \"Provide 'clientFactory' for production use.\",\n );\n }\n }\n\n const clientFactory =\n config.clientFactory ??\n ((serverConfig: MCPServerConfig) => createStubClient(serverConfig, debug));\n const approvalTimeoutMs = config.approvalTimeoutMs ?? 300000;\n\n // Initialize state\n const state: MCPAdapterState = {\n servers: new Map(),\n toolConstraints: new Map(Object.entries(toolConstraints)),\n resourceMappings,\n rateLimiters: new Map(),\n pendingApprovals: new Map(),\n approvedRequests: new Set(),\n rejectedRequests: new Set(),\n };\n\n // Approval ID counter\n let approvalCounter = 0;\n\n // Promise-based approval waiting (no polling)\n const approvalWaiters = new Map<\n string,\n {\n resolve: () => void;\n reject: (error: Error) => void;\n timeoutId: ReturnType<typeof setTimeout>;\n }\n >();\n\n // Rejection reasons storage\n const rejectionReasons = new Map<string, string>();\n\n // Wait for approval with timeout - Promise-based, no polling\n function waitForApproval(requestId: string): Promise<void> {\n return new Promise((resolve, reject) => {\n // Check if already resolved\n if (state.approvedRequests.has(requestId)) {\n state.approvedRequests.delete(requestId);\n state.pendingApprovals.delete(requestId);\n events.onApprovalResolved?.(requestId, true);\n resolve();\n return;\n }\n if (state.rejectedRequests.has(requestId)) {\n state.rejectedRequests.delete(requestId);\n state.pendingApprovals.delete(requestId);\n const reason = rejectionReasons.get(requestId);\n rejectionReasons.delete(requestId);\n events.onApprovalResolved?.(requestId, false);\n reject(\n new Error(\n `[Directive MCP] Tool call request ${requestId} was rejected${reason ? `: ${reason}` : \"\"}`,\n ),\n );\n return;\n }\n\n // Set up timeout\n const timeoutId = setTimeout(() => {\n approvalWaiters.delete(requestId);\n state.pendingApprovals.delete(requestId);\n reject(\n new Error(\n `[Directive MCP] Approval timeout: Request ${requestId} was not approved or rejected within ${approvalTimeoutMs}ms. ` +\n `Call adapter.approve(\"${requestId}\") or adapter.reject(\"${requestId}\") to resolve.`,\n ),\n );\n }, approvalTimeoutMs);\n\n // Store waiter for later resolution\n approvalWaiters.set(requestId, { resolve, reject, timeoutId });\n });\n }\n\n // Resolve an approval (called by approve/reject)\n function resolveApproval(\n requestId: string,\n approved: boolean,\n reason?: string,\n ): void {\n const waiter = approvalWaiters.get(requestId);\n if (waiter) {\n clearTimeout(waiter.timeoutId);\n approvalWaiters.delete(requestId);\n state.pendingApprovals.delete(requestId);\n events.onApprovalResolved?.(requestId, approved);\n\n if (approved) {\n waiter.resolve();\n } else {\n waiter.reject(\n new Error(\n `[Directive MCP] Tool call request ${requestId} was rejected${reason ? `: ${reason}` : \"\"}`,\n ),\n );\n }\n } else {\n // Waiter not yet created, store for immediate resolution with TTL cleanup\n if (approved) {\n state.approvedRequests.add(requestId);\n } else {\n state.rejectedRequests.add(requestId);\n if (reason) {\n rejectionReasons.set(requestId, reason);\n }\n }\n // Auto-cleanup pre-resolved state after approval timeout to prevent memory leaks\n setTimeout(() => {\n state.approvedRequests.delete(requestId);\n state.rejectedRequests.delete(requestId);\n rejectionReasons.delete(requestId);\n state.pendingApprovals.delete(requestId);\n }, approvalTimeoutMs);\n }\n }\n\n // Track reconnect state per server\n const reconnectState = new Map<\n string,\n {\n timer: ReturnType<typeof setTimeout> | null;\n attempts: number;\n maxAttempts: number;\n baseDelay: number;\n }\n >();\n\n // Initialize server states\n for (const serverConfig of servers) {\n state.servers.set(serverConfig.name, {\n config: serverConfig,\n client: null,\n tools: [],\n resources: [],\n status: \"disconnected\",\n });\n reconnectState.set(serverConfig.name, {\n timer: null,\n attempts: 0,\n maxAttempts: serverConfig.retry?.maxAttempts ?? 10,\n baseDelay: serverConfig.retry?.backoffMs ?? 5000,\n });\n }\n\n // Validate stdio command to prevent shell injection\n function validateStdioCommand(serverConfig: MCPServerConfig): void {\n const shellMetachars = /[;|&$`><]/;\n\n if (serverConfig.command) {\n if (shellMetachars.test(serverConfig.command)) {\n throw new Error(\n `[Directive MCP] Stdio command for server '${serverConfig.name}' contains shell metacharacters: '${serverConfig.command}'. ` +\n `This may indicate a command injection vulnerability. Use 'args' for command arguments instead.`,\n );\n }\n }\n\n if (serverConfig.args) {\n for (const arg of serverConfig.args) {\n if (shellMetachars.test(arg)) {\n throw new Error(\n `[Directive MCP] Stdio argument for server '${serverConfig.name}' contains shell metacharacters: '${arg}'. ` +\n `This may indicate a command injection vulnerability.`,\n );\n }\n }\n }\n }\n\n // Connect to a server\n async function connectServer(name: string): Promise<void> {\n const serverState = state.servers.get(name);\n if (!serverState) {\n throw new Error(`Unknown MCP server: ${name}`);\n }\n\n if (serverState.status === \"connected\") {\n return;\n }\n\n // Validate stdio commands before connecting\n if (serverState.config.transport === \"stdio\") {\n validateStdioCommand(serverState.config);\n }\n\n serverState.status = \"connecting\";\n\n try {\n const client = clientFactory(serverState.config);\n await client.connect();\n\n serverState.client = client;\n serverState.status = \"connected\";\n\n // Reset reconnect state on successful connection\n const rState = reconnectState.get(name);\n if (rState) {\n rState.attempts = 0;\n if (rState.timer) {\n clearTimeout(rState.timer);\n rState.timer = null;\n }\n }\n\n // Fetch available tools and resources\n if (client.getCapabilities().tools) {\n serverState.tools = await client.listTools();\n }\n if (client.getCapabilities().resources) {\n serverState.resources = await client.listResources();\n }\n\n serverState.lastSync = Date.now();\n events.onConnect?.(name);\n } catch (error) {\n serverState.status = \"error\";\n serverState.error =\n error instanceof Error ? error : new Error(String(error));\n events.onError?.(name, serverState.error);\n\n if (autoReconnect) {\n const rState = reconnectState.get(name);\n if (rState && rState.attempts < rState.maxAttempts) {\n rState.attempts++;\n // Exponential backoff with jitter, capped at 60s\n const delay = Math.min(\n rState.baseDelay * 2 ** (rState.attempts - 1) +\n Math.random() * 1000,\n 60000,\n );\n rState.timer = setTimeout(() => {\n rState.timer = null;\n connectServer(name).catch(() => {}); // Error handled in next iteration\n }, delay);\n } else if (rState) {\n console.error(\n `[Directive MCP] Max reconnect attempts (${rState.maxAttempts}) reached for server '${name}'. ` +\n `Call adapter.connectServer(\"${name}\") to retry manually.`,\n );\n }\n }\n\n throw serverState.error;\n }\n }\n\n // Disconnect from a server\n async function disconnectServer(name: string): Promise<void> {\n // Clear any pending reconnect timer\n const rState = reconnectState.get(name);\n if (rState?.timer) {\n clearTimeout(rState.timer);\n rState.timer = null;\n rState.attempts = 0;\n }\n\n const serverState = state.servers.get(name);\n if (!serverState || !serverState.client) {\n return;\n }\n\n try {\n await serverState.client.disconnect();\n } finally {\n serverState.status = \"disconnected\";\n serverState.client = null;\n events.onDisconnect?.(name);\n }\n }\n\n // Call a tool with constraint checking\n async function callToolWithConstraints(\n server: string,\n tool: string,\n args: Record<string, unknown>,\n facts: Record<string, unknown>,\n ): Promise<MCPToolResult> {\n const serverState = state.servers.get(server);\n if (!serverState) {\n throw new Error(\n `[Directive MCP] Unknown server '${server}'. ` +\n `Available servers: ${Array.from(state.servers.keys()).join(\", \") || \"(none)\"}`,\n );\n }\n if (!serverState.client) {\n throw new Error(\n `[Directive MCP] Server '${server}' is not connected. ` +\n `Call 'adapter.connect()' or 'adapter.connectServer(\"${server}\")' first.`,\n );\n }\n\n const constraintKey = `${server}.${tool}`;\n const constraint = state.toolConstraints.get(constraintKey);\n\n // Check constraints\n if (constraint) {\n // Check rate limit\n if (constraint.rateLimit) {\n const limiter = state.rateLimiters.get(constraintKey);\n if (\n !checkRateLimit(\n state.rateLimiters,\n constraintKey,\n constraint.rateLimit,\n )\n ) {\n const resetAt = limiter?.resetTime\n ? new Date(limiter.resetTime).toISOString()\n : \"unknown\";\n throw new Error(\n `[Directive MCP] Rate limit exceeded for '${constraintKey}': ` +\n `${limiter?.count ?? 0}/${constraint.rateLimit} requests per minute. ` +\n `Resets at ${resetAt}.`,\n );\n }\n }\n\n // Check max arg size\n if (constraint.maxArgSize) {\n const argSize = JSON.stringify(args).length;\n if (argSize > constraint.maxArgSize) {\n throw new Error(\n `Arguments exceed max size (${argSize} > ${constraint.maxArgSize})`,\n );\n }\n }\n\n // Check custom constraint\n if (constraint.when) {\n const allowed = await constraint.when(facts, args);\n if (!allowed) {\n throw new Error(`Constraint not satisfied for ${constraintKey}`);\n }\n }\n\n // Check if approval is required\n if (constraint.requireApproval) {\n const requestId = `approval-${++approvalCounter}-${Date.now()}`;\n const approvalRequest: MCPApprovalRequest = {\n id: requestId,\n server,\n tool,\n args,\n requestedAt: Date.now(),\n };\n\n state.pendingApprovals.set(requestId, approvalRequest);\n events.onApprovalRequest?.(approvalRequest);\n\n // Wait for approval or rejection\n await waitForApproval(requestId);\n }\n }\n\n events.onToolCall?.(server, tool, args);\n\n // Call the tool\n const result = await Promise.race([\n serverState.client.callTool(tool, args),\n new Promise<never>((_, reject) =>\n setTimeout(\n () => reject(new Error(`Tool call timeout: ${constraintKey}`)),\n constraint?.timeout ?? 30000,\n ),\n ),\n ]);\n\n events.onToolResult?.(server, tool, result);\n\n return result;\n }\n\n // Create plugin\n const plugin: Plugin = {\n name: \"mcp-adapter\",\n\n onInit: async () => {\n if (autoConnect) {\n await Promise.all(\n Array.from(state.servers.keys()).map((name) =>\n connectServer(name).catch((e) =>\n console.error(`Failed to connect to ${name}:`, e),\n ),\n ),\n );\n }\n },\n\n onDestroy: async () => {\n // Clear all reconnect timers\n for (const rState of reconnectState.values()) {\n if (rState.timer) {\n clearTimeout(rState.timer);\n rState.timer = null;\n }\n }\n\n // Reject all pending approval waiters\n for (const [, waiter] of approvalWaiters) {\n clearTimeout(waiter.timeoutId);\n waiter.reject(\n new Error(\n \"[Directive MCP] Adapter destroyed while awaiting approval\",\n ),\n );\n }\n approvalWaiters.clear();\n\n await Promise.all(\n Array.from(state.servers.keys()).map((name) =>\n disconnectServer(name).catch((e) =>\n console.error(`Failed to disconnect from ${name}:`, e),\n ),\n ),\n );\n },\n };\n\n // Sync resources to facts\n async function syncResources(facts: Record<string, unknown>): Promise<void> {\n for (const mapping of resourceMappings) {\n for (const [serverName, serverState] of state.servers) {\n if (!serverState.client) continue;\n\n for (const resource of serverState.resources) {\n // Check if resource matches pattern\n const matches =\n typeof mapping.pattern === \"string\"\n ? matchGlob(resource.uri, mapping.pattern)\n : mapping.pattern.test(resource.uri);\n\n if (matches) {\n try {\n const result = await serverState.client.readResource(\n resource.uri,\n );\n const content = result.contents[0]?.text ?? \"\";\n const value = mapping.transform\n ? mapping.transform(content)\n : content;\n\n facts[mapping.factKey] = value;\n events.onResourceUpdate?.(serverName, resource.uri, result);\n } catch (error) {\n console.error(`Failed to sync resource ${resource.uri}:`, error);\n }\n }\n }\n }\n }\n }\n\n return {\n plugin,\n\n async connect() {\n await Promise.all(Array.from(state.servers.keys()).map(connectServer));\n },\n\n connectServer,\n\n async disconnect() {\n await Promise.all(Array.from(state.servers.keys()).map(disconnectServer));\n },\n\n disconnectServer,\n\n getTools() {\n const tools = new Map<string, MCPTool[]>();\n for (const [name, serverState] of state.servers) {\n tools.set(name, serverState.tools);\n }\n return tools;\n },\n\n getResources() {\n const resources = new Map<string, MCPResource[]>();\n for (const [name, serverState] of state.servers) {\n resources.set(name, serverState.resources);\n }\n return resources;\n },\n\n async callTool(server, tool, args, facts) {\n return callToolWithConstraints(server, tool, args, facts);\n },\n\n async callToolDirect(server, tool, args) {\n if (!allowDirectCalls) {\n throw new Error(\n \"[Directive] callToolDirect is disabled by default. Pass { allowDirectCalls: true } to enable unconstrained tool calls.\",\n );\n }\n if (process.env.NODE_ENV !== \"production\") {\n console.warn(\n \"[Directive] callToolDirect bypasses all constraints (rate limits, approvals, timeouts).\",\n );\n }\n const serverState = state.servers.get(server);\n if (!serverState) {\n throw new Error(\n `[Directive MCP] Unknown server '${server}'. ` +\n `Available servers: ${Array.from(state.servers.keys()).join(\", \") || \"(none)\"}`,\n );\n }\n if (!serverState.client) {\n throw new Error(\n `[Directive MCP] Server '${server}' is not connected. ` +\n `Call 'adapter.connect()' or 'adapter.connectServer(\"${server}\")' first.`,\n );\n }\n events.onToolCall?.(server, tool, args);\n const result = await serverState.client.callTool(tool, args);\n events.onToolResult?.(server, tool, result);\n return result;\n },\n\n async readResource(server, uri) {\n const serverState = state.servers.get(server);\n if (!serverState) {\n throw new Error(\n `[Directive MCP] Unknown server '${server}'. ` +\n `Available servers: ${Array.from(state.servers.keys()).join(\", \") || \"(none)\"}`,\n );\n }\n if (!serverState.client) {\n throw new Error(\n `[Directive MCP] Server '${server}' is not connected. ` +\n `Call 'adapter.connect()' or 'adapter.connectServer(\"${server}\")' first.`,\n );\n }\n const result = await serverState.client.readResource(uri);\n events.onResourceUpdate?.(server, uri, result);\n return result;\n },\n\n syncResources,\n\n getServerStatus(name) {\n return state.servers.get(name);\n },\n\n getAllServerStatuses() {\n return new Map(state.servers);\n },\n\n approve(requestId: string) {\n const request = state.pendingApprovals.get(requestId);\n if (!request && !approvalWaiters.has(requestId)) {\n throw new Error(\n `[Directive MCP] No pending approval request with ID '${requestId}'. ` +\n `Pending requests: ${Array.from(state.pendingApprovals.keys()).join(\", \") || \"(none)\"}`,\n );\n }\n resolveApproval(requestId, true);\n },\n\n reject(requestId: string, reason?: string) {\n const request = state.pendingApprovals.get(requestId);\n if (!request && !approvalWaiters.has(requestId)) {\n throw new Error(\n `[Directive MCP] No pending approval request with ID '${requestId}'. ` +\n `Pending requests: ${Array.from(state.pendingApprovals.keys()).join(\", \") || \"(none)\"}`,\n );\n }\n resolveApproval(requestId, false, reason);\n },\n\n getPendingApprovals() {\n return Array.from(state.pendingApprovals.values());\n },\n\n getRejectionReason(requestId: string): string | undefined {\n return rejectionReasons.get(requestId);\n },\n };\n}\n\n// ============================================================================\n// Utility Functions\n// ============================================================================\n\n/** Cache for compiled glob patterns to avoid repeated regex compilation */\nconst globCache = new Map<string, RegExp>();\nconst MAX_GLOB_CACHE_SIZE = 200;\n\n/** Simple glob matching (supports * and **) with caching */\nfunction matchGlob(str: string, pattern: string): boolean {\n let regex = globCache.get(pattern);\n if (!regex) {\n // Escape all regex metacharacters first, then convert glob wildcards\n const regexPattern = pattern\n .replace(/\\*\\*/g, \"\\0GLOBSTAR\\0\")\n .replace(/\\*/g, \"\\0STAR\\0\")\n .replace(/\\?/g, \"\\0QUESTION\\0\")\n .replace(/[.+^${}()|[\\]\\\\]/g, \"\\\\$&\")\n .replace(/\\0GLOBSTAR\\0/g, \".*\")\n .replace(/\\0STAR\\0/g, \"[^/]*\")\n .replace(/\\0QUESTION\\0/g, \".\");\n regex = new RegExp(`^${regexPattern}$`);\n\n // LRU eviction: delete oldest entry when cache is full\n if (globCache.size >= MAX_GLOB_CACHE_SIZE) {\n const firstKey = globCache.keys().next().value;\n if (firstKey !== undefined) globCache.delete(firstKey);\n }\n globCache.set(pattern, regex);\n }\n return regex.test(str);\n}\n\n/**\n * Convert MCP tools to a format suitable for LLM tool calling.\n *\n * @example\n * ```typescript\n * const adapter = createMCPAdapter({ servers: [...] });\n * await adapter.connect();\n *\n * const tools = adapter.getTools();\n * const llmTools = convertToolsForLLM(tools);\n * // Use with OpenAI/Anthropic/etc.\n * ```\n */\nexport function convertToolsForLLM(tools: Map<string, MCPTool[]>): Array<{\n type: \"function\";\n function: {\n name: string;\n description: string;\n parameters: Record<string, unknown>;\n };\n}> {\n const result: Array<{\n type: \"function\";\n function: {\n name: string;\n description: string;\n parameters: Record<string, unknown>;\n };\n }> = [];\n\n for (const [server, serverTools] of tools) {\n for (const tool of serverTools) {\n result.push({\n type: \"function\",\n function: {\n name: `${server}.${tool.name}`,\n description: tool.description ?? `Tool: ${tool.name}`,\n parameters: tool.inputSchema,\n },\n });\n }\n }\n\n return result;\n}\n\n/**\n * Create a requirement to call an MCP tool.\n *\n * @example\n * ```typescript\n * const req = mcpCallTool('filesystem', 'read_file', { path: '/etc/hosts' });\n * // { type: 'MCP_CALL_TOOL', server: 'filesystem', tool: 'read_file', args: { path: '/etc/hosts' } }\n * ```\n */\nexport function mcpCallTool(\n server: string,\n tool: string,\n args: Record<string, unknown>,\n): MCPCallToolRequirement {\n return { type: \"MCP_CALL_TOOL\", server, tool, args };\n}\n\n/**\n * Create a requirement to read an MCP resource.\n */\nexport function mcpReadResource(\n server: string,\n uri: string,\n): MCPReadResourceRequirement {\n return { type: \"MCP_READ_RESOURCE\", server, uri };\n}\n\n/**\n * Create a requirement to get an MCP prompt.\n */\nexport function mcpGetPrompt(\n server: string,\n prompt: string,\n args?: Record<string, string>,\n): MCPGetPromptRequirement {\n return { type: \"MCP_GET_PROMPT\", server, prompt, args };\n}\n\n/**\n * Create a requirement to sync MCP resources.\n */\nexport function mcpSyncResources(\n server?: string,\n pattern?: string | RegExp,\n): MCPSyncResourcesRequirement {\n return { type: \"MCP_SYNC_RESOURCES\", server, pattern };\n}\n\n// Re-export types\nexport * from \"./mcp-types.js\";\n"]}
1
+ {"version":3,"sources":["../src/mcp.ts"],"names":["createStubClient","config","debug","connected","tools","resources","log","msg","args","name","uri","checkRateLimit","rateLimiters","key","limit","now","limiter","createMCPAdapter","servers","toolConstraints","resourceMappings","events","autoConnect","autoReconnect","allowDirectCalls","clientFactory","serverConfig","approvalTimeoutMs","state","approvalCounter","approvalWaiters","rejectionReasons","waitForApproval","requestId","resolve","reject","reason","timeoutId","resolveApproval","approved","waiter","reconnectState","validateStdioCommand","shellMetachars","arg","connectServer","serverState","client","rState","error","delay","disconnectServer","callToolWithConstraints","server","tool","facts","constraintKey","constraint","resetAt","argSize","approvalRequest","result","_","plugin","e","syncResources","mapping","serverName","resource","matchGlob","content","value","globCache","MAX_GLOB_CACHE_SIZE","str","pattern","regex","regexPattern","firstKey","convertToolsForLLM","serverTools","mcpCallTool","mcpReadResource","mcpGetPrompt","prompt","mcpSyncResources"],"mappings":"aAiKA,SAASA,CAAAA,CAAiBC,CAAAA,CAAyBC,CAAAA,CAAQ,KAAA,CAAkB,CAC3E,IAAIC,CAAAA,CAAY,KAAA,CACVC,CAAAA,CAAmB,EAAC,CACpBC,CAAAA,CAA2B,EAAC,CAE5BC,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EAASA,EAAK,IAAI,CAAA,CAAA,CACnD,WAAYA,CAAAA,CAAK,WACnB,CACF,CAAC,CAAA,CAIL,OAAOO,CACT,CAWO,SAASoB,EACd5B,CAAAA,CACAC,CAAAA,CACA9C,EACwB,CACxB,OAAO,CAAE,IAAA,CAAM,eAAA,CAAiB,MAAA,CAAA6C,CAAAA,CAAQ,IAAA,CAAAC,CAAAA,CAAM,KAAA9C,CAAK,CACrD,CAKO,SAAS0E,CAAAA,CACd7B,EACA3C,CAAAA,CAC4B,CAC5B,OAAO,CAAE,IAAA,CAAM,mBAAA,CAAqB,OAAA2C,CAAAA,CAAQ,GAAA,CAAA3C,CAAI,CAClD,CAKO,SAASyE,CAAAA,CACd9B,CAAAA,CACA+B,CAAAA,CACA5E,CAAAA,CACyB,CACzB,OAAO,CAAE,IAAA,CAAM,gBAAA,CAAkB,OAAA6C,CAAAA,CAAQ,MAAA,CAAA+B,EAAQ,IAAA,CAAA5E,CAAK,CACxD,CAKO,SAAS6E,CAAAA,CACdhC,EACAsB,CAAAA,CAC6B,CAC7B,OAAO,CAAE,IAAA,CAAM,qBAAsB,MAAA,CAAAtB,CAAAA,CAAQ,OAAA,CAAAsB,CAAQ,CACvD","file":"chunk-C6MQ4WL6.cjs","sourcesContent":["/**\n * MCP Adapter - Model Context Protocol Integration for Directive\n *\n * Provides seamless integration between Directive's constraint system and MCP servers:\n * - MCP tools become Directive resolvers with constraint-driven access control\n * - MCP resources sync to Directive facts\n * - MCP prompts available through requirements\n *\n * @example\n * ```typescript\n * import { createMCPAdapter } from '@directive-run/ai';\n *\n * const mcpAdapter = createMCPAdapter({\n * servers: [\n * { name: 'filesystem', transport: 'stdio', command: 'mcp-server-filesystem' },\n * { name: 'github', transport: 'sse', url: 'https://mcp.github.com' }\n * ],\n * toolConstraints: {\n * 'filesystem.write': { requireApproval: true },\n * 'github.create_pr': { when: (facts) => facts.reviewComplete }\n * }\n * });\n *\n * const system = createSystem({\n * module: myModule,\n * plugins: [mcpAdapter.plugin]\n * });\n * ```\n */\n\nimport type { Plugin } from \"@directive-run/core\";\nimport type {\n MCPAdapterConfig,\n MCPApprovalRequest,\n MCPCallToolRequirement,\n MCPClient,\n MCPGetPromptRequirement,\n MCPReadResourceRequirement,\n MCPResource,\n MCPResourceMapping,\n MCPResourceResult,\n MCPServerConfig,\n MCPSyncResourcesRequirement,\n MCPTool,\n MCPToolConstraint,\n MCPToolResult,\n} from \"./mcp-types.js\";\n\n// ============================================================================\n// MCP Adapter State\n// ============================================================================\n\n/** State of an MCP server connection */\ninterface MCPServerState {\n config: MCPServerConfig;\n client: MCPClient | null;\n tools: MCPTool[];\n resources: MCPResource[];\n status: \"disconnected\" | \"connecting\" | \"connected\" | \"error\";\n error?: Error;\n lastSync?: number;\n}\n\n/** Internal state for the adapter */\ninterface MCPAdapterState {\n servers: Map<string, MCPServerState>;\n toolConstraints: Map<string, MCPToolConstraint>;\n resourceMappings: MCPResourceMapping[];\n rateLimiters: Map<string, { count: number; resetTime: number }>;\n /** Pending approval requests */\n pendingApprovals: Map<string, MCPApprovalRequest>;\n /** Approved request IDs */\n approvedRequests: Set<string>;\n /** Rejected request IDs */\n rejectedRequests: Set<string>;\n}\n\n// ============================================================================\n// MCP Adapter Instance\n// ============================================================================\n\n/** MCP Adapter instance */\nexport interface MCPAdapter {\n /** Plugin to add to Directive system */\n plugin: Plugin;\n /** Connect to all configured servers */\n connect(): Promise<void>;\n /** Connect to a specific server */\n connectServer(name: string): Promise<void>;\n /** Disconnect from all servers */\n disconnect(): Promise<void>;\n /** Disconnect from a specific server */\n disconnectServer(name: string): Promise<void>;\n /** Get all available tools across all servers */\n getTools(): Map<string, MCPTool[]>;\n /** Get all available resources across all servers */\n getResources(): Map<string, MCPResource[]>;\n /**\n * Call a tool with constraint checking (recommended).\n * Applies rate limits, argument size limits, approval workflow, and custom constraints.\n * @param server - Server name\n * @param tool - Tool name\n * @param args - Tool arguments\n * @param facts - Current facts for constraint evaluation\n */\n callTool(\n server: string,\n tool: string,\n args: Record<string, unknown>,\n facts: Record<string, unknown>,\n ): Promise<MCPToolResult>;\n /**\n * Call a tool directly, bypassing all constraints (rate limits, approvals, etc.).\n * Use only for trusted internal calls where constraint checking is not needed.\n */\n callToolDirect(\n server: string,\n tool: string,\n args: Record<string, unknown>,\n ): Promise<MCPToolResult>;\n /** Read a resource directly */\n readResource(server: string, uri: string): Promise<MCPResourceResult>;\n /** Sync resources to facts */\n syncResources(facts: Record<string, unknown>): Promise<void>;\n /** Get server status */\n getServerStatus(name: string): MCPServerState | undefined;\n /** Get all server statuses */\n getAllServerStatuses(): Map<string, MCPServerState>;\n /** Approve a pending tool call request */\n approve(requestId: string): void;\n /** Reject a pending tool call request */\n reject(requestId: string, reason?: string): void;\n /** Get pending approval requests */\n getPendingApprovals(): MCPApprovalRequest[];\n /** Get the rejection reason for a request (if available) */\n getRejectionReason(requestId: string): string | undefined;\n}\n\n// ============================================================================\n// Default MCP Client (Stub)\n// ============================================================================\n\n/**\n * Create a stub MCP client for development/testing.\n *\n * **Important:** This stub is for development only. In production, provide\n * a real MCP client via the `clientFactory` option:\n *\n * @example\n * ```typescript\n * import { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\n *\n * const adapter = createMCPAdapter({\n * servers: [...],\n * clientFactory: (config) => new Client(config),\n * });\n * ```\n *\n * @param config - Server configuration\n * @param debug - Enable debug logging (default: false)\n */\nfunction createStubClient(config: MCPServerConfig, debug = false): MCPClient {\n let connected = false;\n const tools: MCPTool[] = [];\n const resources: MCPResource[] = [];\n\n const log = debug\n ? (msg: string, ...args: unknown[]) =>\n console.debug(`[MCP Stub] ${msg}`, ...args)\n : () => {};\n\n return {\n async connect() {\n log(`Connecting to ${config.name} (${config.transport})`);\n connected = true;\n },\n async disconnect() {\n connected = false;\n },\n isConnected() {\n return connected;\n },\n getCapabilities() {\n return { tools: true, resources: true, prompts: true };\n },\n async listTools() {\n return tools;\n },\n async callTool(name: string, args: Record<string, unknown>) {\n log(`Calling tool ${name}`, args);\n return {\n content: [{ type: \"text\" as const, text: `Stub result for ${name}` }],\n };\n },\n async listResources() {\n return resources;\n },\n async readResource(uri: string) {\n log(`Reading resource ${uri}`);\n return {\n contents: [{ uri, text: `Stub content for ${uri}` }],\n };\n },\n async listPrompts() {\n return [];\n },\n async getPrompt(name: string) {\n return {\n messages: [\n {\n role: \"user\" as const,\n content: { type: \"text\" as const, text: `Stub prompt ${name}` },\n },\n ],\n };\n },\n };\n}\n\n// ============================================================================\n// Rate Limiter\n// ============================================================================\n\nfunction checkRateLimit(\n rateLimiters: Map<string, { count: number; resetTime: number }>,\n key: string,\n limit: number,\n): boolean {\n const now = Date.now();\n const limiter = rateLimiters.get(key);\n\n if (!limiter || now > limiter.resetTime) {\n rateLimiters.set(key, { count: 1, resetTime: now + 60000 });\n return true;\n }\n\n if (limiter.count >= limit) {\n return false;\n }\n\n limiter.count++;\n return true;\n}\n\n// ============================================================================\n// MCP Adapter Factory\n// ============================================================================\n\n/**\n * Create an MCP adapter for Directive integration.\n *\n * @example\n * ```typescript\n * const adapter = createMCPAdapter({\n * servers: [\n * { name: 'fs', transport: 'stdio', command: 'mcp-server-filesystem' },\n * ],\n * toolConstraints: {\n * 'fs.write_file': {\n * requireApproval: true,\n * maxArgSize: 10000,\n * timeout: 30000,\n * },\n * },\n * resourceMappings: [\n * {\n * pattern: 'file://*.json',\n * factKey: 'jsonFiles',\n * mode: 'poll',\n * pollInterval: 5000,\n * },\n * ],\n * });\n *\n * // Add to system\n * const system = createSystem({\n * module: myModule,\n * plugins: [adapter.plugin],\n * });\n *\n * // Connect to servers\n * await adapter.connect();\n * ```\n */\nexport function createMCPAdapter(config: MCPAdapterConfig): MCPAdapter {\n const {\n servers,\n toolConstraints = {},\n resourceMappings = [],\n events = {},\n autoConnect = false,\n autoReconnect = true,\n debug = false,\n allowDirectCalls = false,\n } = config;\n\n // Warn if using stub client in production\n const usingStubClient = !config.clientFactory;\n if (usingStubClient) {\n const isProduction =\n typeof process !== \"undefined\" && process.env?.NODE_ENV === \"production\";\n if (isProduction) {\n console.warn(\n \"[Directive MCP] WARNING: Using stub MCP client in production!\\n\" +\n \"The stub client returns mock data and does not connect to real MCP servers.\\n\" +\n \"Provide a real 'clientFactory' option to connect to actual MCP servers:\\n\\n\" +\n \" import { Client } from '@modelcontextprotocol/sdk/client/index.js';\\n\\n\" +\n \" const adapter = createMCPAdapter({\\n\" +\n \" servers: [...],\\n\" +\n \" clientFactory: (config) => new Client(config),\\n\" +\n \" });\",\n );\n } else if (debug) {\n console.debug(\n \"[Directive MCP] Using stub client for development. \" +\n \"Provide 'clientFactory' for production use.\",\n );\n }\n }\n\n const clientFactory =\n config.clientFactory ??\n ((serverConfig: MCPServerConfig) => createStubClient(serverConfig, debug));\n const approvalTimeoutMs = config.approvalTimeoutMs ?? 300000;\n\n // Initialize state\n const state: MCPAdapterState = {\n servers: new Map(),\n toolConstraints: new Map(Object.entries(toolConstraints)),\n resourceMappings,\n rateLimiters: new Map(),\n pendingApprovals: new Map(),\n approvedRequests: new Set(),\n rejectedRequests: new Set(),\n };\n\n // Approval ID counter\n let approvalCounter = 0;\n\n // Promise-based approval waiting (no polling)\n const approvalWaiters = new Map<\n string,\n {\n resolve: () => void;\n reject: (error: Error) => void;\n timeoutId: ReturnType<typeof setTimeout>;\n }\n >();\n\n // Rejection reasons storage\n const rejectionReasons = new Map<string, string>();\n\n // Wait for approval with timeout - Promise-based, no polling\n function waitForApproval(requestId: string): Promise<void> {\n return new Promise((resolve, reject) => {\n // Check if already resolved\n if (state.approvedRequests.has(requestId)) {\n state.approvedRequests.delete(requestId);\n state.pendingApprovals.delete(requestId);\n events.onApprovalResolved?.(requestId, true);\n resolve();\n return;\n }\n if (state.rejectedRequests.has(requestId)) {\n state.rejectedRequests.delete(requestId);\n state.pendingApprovals.delete(requestId);\n const reason = rejectionReasons.get(requestId);\n rejectionReasons.delete(requestId);\n events.onApprovalResolved?.(requestId, false);\n reject(\n new Error(\n `[Directive MCP] Tool call request ${requestId} was rejected${reason ? `: ${reason}` : \"\"}`,\n ),\n );\n return;\n }\n\n // Set up timeout\n const timeoutId = setTimeout(() => {\n approvalWaiters.delete(requestId);\n state.pendingApprovals.delete(requestId);\n reject(\n new Error(\n `[Directive MCP] Approval timeout: Request ${requestId} was not approved or rejected within ${approvalTimeoutMs}ms. ` +\n `Call adapter.approve(\"${requestId}\") or adapter.reject(\"${requestId}\") to resolve.`,\n ),\n );\n }, approvalTimeoutMs);\n\n // Store waiter for later resolution\n approvalWaiters.set(requestId, { resolve, reject, timeoutId });\n });\n }\n\n // Resolve an approval (called by approve/reject)\n function resolveApproval(\n requestId: string,\n approved: boolean,\n reason?: string,\n ): void {\n const waiter = approvalWaiters.get(requestId);\n if (waiter) {\n clearTimeout(waiter.timeoutId);\n approvalWaiters.delete(requestId);\n state.pendingApprovals.delete(requestId);\n events.onApprovalResolved?.(requestId, approved);\n\n if (approved) {\n waiter.resolve();\n } else {\n waiter.reject(\n new Error(\n `[Directive MCP] Tool call request ${requestId} was rejected${reason ? `: ${reason}` : \"\"}`,\n ),\n );\n }\n } else {\n // Waiter not yet created, store for immediate resolution with TTL cleanup\n if (approved) {\n state.approvedRequests.add(requestId);\n } else {\n state.rejectedRequests.add(requestId);\n if (reason) {\n rejectionReasons.set(requestId, reason);\n }\n }\n // Auto-cleanup pre-resolved state after approval timeout to prevent memory leaks\n setTimeout(() => {\n state.approvedRequests.delete(requestId);\n state.rejectedRequests.delete(requestId);\n rejectionReasons.delete(requestId);\n state.pendingApprovals.delete(requestId);\n }, approvalTimeoutMs);\n }\n }\n\n // Track reconnect state per server\n const reconnectState = new Map<\n string,\n {\n timer: ReturnType<typeof setTimeout> | null;\n attempts: number;\n maxAttempts: number;\n baseDelay: number;\n }\n >();\n\n // Initialize server states\n for (const serverConfig of servers) {\n state.servers.set(serverConfig.name, {\n config: serverConfig,\n client: null,\n tools: [],\n resources: [],\n status: \"disconnected\",\n });\n reconnectState.set(serverConfig.name, {\n timer: null,\n attempts: 0,\n maxAttempts: serverConfig.retry?.maxAttempts ?? 10,\n baseDelay: serverConfig.retry?.backoffMs ?? 5000,\n });\n }\n\n // Validate stdio command to prevent shell injection\n function validateStdioCommand(serverConfig: MCPServerConfig): void {\n const shellMetachars = /[;|&$`><]/;\n\n if (serverConfig.command) {\n if (shellMetachars.test(serverConfig.command)) {\n throw new Error(\n `[Directive MCP] Stdio command for server '${serverConfig.name}' contains shell metacharacters: '${serverConfig.command}'. This may indicate a command injection vulnerability. Use 'args' for command arguments instead.`,\n );\n }\n }\n\n if (serverConfig.args) {\n for (const arg of serverConfig.args) {\n if (shellMetachars.test(arg)) {\n throw new Error(\n `[Directive MCP] Stdio argument for server '${serverConfig.name}' contains shell metacharacters: '${arg}'. This may indicate a command injection vulnerability.`,\n );\n }\n }\n }\n }\n\n // Connect to a server\n async function connectServer(name: string): Promise<void> {\n const serverState = state.servers.get(name);\n if (!serverState) {\n throw new Error(`Unknown MCP server: ${name}`);\n }\n\n if (serverState.status === \"connected\") {\n return;\n }\n\n // Validate stdio commands before connecting\n if (serverState.config.transport === \"stdio\") {\n validateStdioCommand(serverState.config);\n }\n\n serverState.status = \"connecting\";\n\n try {\n const client = clientFactory(serverState.config);\n await client.connect();\n\n serverState.client = client;\n serverState.status = \"connected\";\n\n // Reset reconnect state on successful connection\n const rState = reconnectState.get(name);\n if (rState) {\n rState.attempts = 0;\n if (rState.timer) {\n clearTimeout(rState.timer);\n rState.timer = null;\n }\n }\n\n // Fetch available tools and resources\n if (client.getCapabilities().tools) {\n serverState.tools = await client.listTools();\n }\n if (client.getCapabilities().resources) {\n serverState.resources = await client.listResources();\n }\n\n serverState.lastSync = Date.now();\n events.onConnect?.(name);\n } catch (error) {\n serverState.status = \"error\";\n serverState.error =\n error instanceof Error ? error : new Error(String(error));\n events.onError?.(name, serverState.error);\n\n if (autoReconnect) {\n const rState = reconnectState.get(name);\n if (rState && rState.attempts < rState.maxAttempts) {\n rState.attempts++;\n // Exponential backoff with jitter, capped at 60s\n const delay = Math.min(\n rState.baseDelay * 2 ** (rState.attempts - 1) +\n Math.random() * 1000,\n 60000,\n );\n rState.timer = setTimeout(() => {\n rState.timer = null;\n connectServer(name).catch(() => {}); // Error handled in next iteration\n }, delay);\n } else if (rState) {\n console.error(\n `[Directive MCP] Max reconnect attempts (${rState.maxAttempts}) reached for server '${name}'. ` +\n `Call adapter.connectServer(\"${name}\") to retry manually.`,\n );\n }\n }\n\n throw serverState.error;\n }\n }\n\n // Disconnect from a server\n async function disconnectServer(name: string): Promise<void> {\n // Clear any pending reconnect timer\n const rState = reconnectState.get(name);\n if (rState?.timer) {\n clearTimeout(rState.timer);\n rState.timer = null;\n rState.attempts = 0;\n }\n\n const serverState = state.servers.get(name);\n if (!serverState || !serverState.client) {\n return;\n }\n\n try {\n await serverState.client.disconnect();\n } finally {\n serverState.status = \"disconnected\";\n serverState.client = null;\n events.onDisconnect?.(name);\n }\n }\n\n // Call a tool with constraint checking\n async function callToolWithConstraints(\n server: string,\n tool: string,\n args: Record<string, unknown>,\n facts: Record<string, unknown>,\n ): Promise<MCPToolResult> {\n const serverState = state.servers.get(server);\n if (!serverState) {\n throw new Error(\n `[Directive MCP] Unknown server '${server}'. ` +\n `Available servers: ${Array.from(state.servers.keys()).join(\", \") || \"(none)\"}`,\n );\n }\n if (!serverState.client) {\n throw new Error(\n `[Directive MCP] Server '${server}' is not connected. ` +\n `Call 'adapter.connect()' or 'adapter.connectServer(\"${server}\")' first.`,\n );\n }\n\n const constraintKey = `${server}.${tool}`;\n const constraint = state.toolConstraints.get(constraintKey);\n\n // Check constraints\n if (constraint) {\n // Check rate limit\n if (constraint.rateLimit) {\n const limiter = state.rateLimiters.get(constraintKey);\n if (\n !checkRateLimit(\n state.rateLimiters,\n constraintKey,\n constraint.rateLimit,\n )\n ) {\n const resetAt = limiter?.resetTime\n ? new Date(limiter.resetTime).toISOString()\n : \"unknown\";\n throw new Error(\n `[Directive MCP] Rate limit exceeded for '${constraintKey}': ` +\n `${limiter?.count ?? 0}/${constraint.rateLimit} requests per minute. ` +\n `Resets at ${resetAt}.`,\n );\n }\n }\n\n // Check max arg size\n if (constraint.maxArgSize) {\n const argSize = JSON.stringify(args).length;\n if (argSize > constraint.maxArgSize) {\n throw new Error(\n `Arguments exceed max size (${argSize} > ${constraint.maxArgSize})`,\n );\n }\n }\n\n // Check custom constraint\n if (constraint.when) {\n const allowed = await constraint.when(facts, args);\n if (!allowed) {\n throw new Error(`Constraint not satisfied for ${constraintKey}`);\n }\n }\n\n // Check if approval is required\n if (constraint.requireApproval) {\n const requestId = `approval-${++approvalCounter}-${Date.now()}`;\n const approvalRequest: MCPApprovalRequest = {\n id: requestId,\n server,\n tool,\n args,\n requestedAt: Date.now(),\n };\n\n state.pendingApprovals.set(requestId, approvalRequest);\n events.onApprovalRequest?.(approvalRequest);\n\n // Wait for approval or rejection\n await waitForApproval(requestId);\n }\n }\n\n events.onToolCall?.(server, tool, args);\n\n // Call the tool\n const result = await Promise.race([\n serverState.client.callTool(tool, args),\n new Promise<never>((_, reject) =>\n setTimeout(\n () => reject(new Error(`Tool call timeout: ${constraintKey}`)),\n constraint?.timeout ?? 30000,\n ),\n ),\n ]);\n\n events.onToolResult?.(server, tool, result);\n\n return result;\n }\n\n // Create plugin\n const plugin: Plugin = {\n name: \"mcp-adapter\",\n\n onInit: async () => {\n if (autoConnect) {\n await Promise.all(\n Array.from(state.servers.keys()).map((name) =>\n connectServer(name).catch((e) =>\n console.error(`Failed to connect to ${name}:`, e),\n ),\n ),\n );\n }\n },\n\n onDestroy: async () => {\n // Clear all reconnect timers\n for (const rState of reconnectState.values()) {\n if (rState.timer) {\n clearTimeout(rState.timer);\n rState.timer = null;\n }\n }\n\n // Reject all pending approval waiters\n for (const [, waiter] of approvalWaiters) {\n clearTimeout(waiter.timeoutId);\n waiter.reject(\n new Error(\n \"[Directive MCP] Adapter destroyed while awaiting approval\",\n ),\n );\n }\n approvalWaiters.clear();\n\n await Promise.all(\n Array.from(state.servers.keys()).map((name) =>\n disconnectServer(name).catch((e) =>\n console.error(`Failed to disconnect from ${name}:`, e),\n ),\n ),\n );\n },\n };\n\n // Sync resources to facts\n async function syncResources(facts: Record<string, unknown>): Promise<void> {\n for (const mapping of resourceMappings) {\n for (const [serverName, serverState] of state.servers) {\n if (!serverState.client) continue;\n\n for (const resource of serverState.resources) {\n // Check if resource matches pattern\n const matches =\n typeof mapping.pattern === \"string\"\n ? matchGlob(resource.uri, mapping.pattern)\n : mapping.pattern.test(resource.uri);\n\n if (matches) {\n try {\n const result = await serverState.client.readResource(\n resource.uri,\n );\n const content = result.contents[0]?.text ?? \"\";\n const value = mapping.transform\n ? mapping.transform(content)\n : content;\n\n facts[mapping.factKey] = value;\n events.onResourceUpdate?.(serverName, resource.uri, result);\n } catch (error) {\n console.error(`Failed to sync resource ${resource.uri}:`, error);\n }\n }\n }\n }\n }\n }\n\n return {\n plugin,\n\n async connect() {\n await Promise.all(Array.from(state.servers.keys()).map(connectServer));\n },\n\n connectServer,\n\n async disconnect() {\n await Promise.all(Array.from(state.servers.keys()).map(disconnectServer));\n },\n\n disconnectServer,\n\n getTools() {\n const tools = new Map<string, MCPTool[]>();\n for (const [name, serverState] of state.servers) {\n tools.set(name, serverState.tools);\n }\n return tools;\n },\n\n getResources() {\n const resources = new Map<string, MCPResource[]>();\n for (const [name, serverState] of state.servers) {\n resources.set(name, serverState.resources);\n }\n return resources;\n },\n\n async callTool(server, tool, args, facts) {\n return callToolWithConstraints(server, tool, args, facts);\n },\n\n async callToolDirect(server, tool, args) {\n if (!allowDirectCalls) {\n throw new Error(\n \"[Directive] callToolDirect is disabled by default. Pass { allowDirectCalls: true } to enable unconstrained tool calls.\",\n );\n }\n if (process.env.NODE_ENV !== \"production\") {\n console.warn(\n \"[Directive] callToolDirect bypasses all constraints (rate limits, approvals, timeouts).\",\n );\n }\n const serverState = state.servers.get(server);\n if (!serverState) {\n throw new Error(\n `[Directive MCP] Unknown server '${server}'. ` +\n `Available servers: ${Array.from(state.servers.keys()).join(\", \") || \"(none)\"}`,\n );\n }\n if (!serverState.client) {\n throw new Error(\n `[Directive MCP] Server '${server}' is not connected. ` +\n `Call 'adapter.connect()' or 'adapter.connectServer(\"${server}\")' first.`,\n );\n }\n events.onToolCall?.(server, tool, args);\n const result = await serverState.client.callTool(tool, args);\n events.onToolResult?.(server, tool, result);\n return result;\n },\n\n async readResource(server, uri) {\n const serverState = state.servers.get(server);\n if (!serverState) {\n throw new Error(\n `[Directive MCP] Unknown server '${server}'. ` +\n `Available servers: ${Array.from(state.servers.keys()).join(\", \") || \"(none)\"}`,\n );\n }\n if (!serverState.client) {\n throw new Error(\n `[Directive MCP] Server '${server}' is not connected. ` +\n `Call 'adapter.connect()' or 'adapter.connectServer(\"${server}\")' first.`,\n );\n }\n const result = await serverState.client.readResource(uri);\n events.onResourceUpdate?.(server, uri, result);\n return result;\n },\n\n syncResources,\n\n getServerStatus(name) {\n return state.servers.get(name);\n },\n\n getAllServerStatuses() {\n return new Map(state.servers);\n },\n\n approve(requestId: string) {\n const request = state.pendingApprovals.get(requestId);\n if (!request && !approvalWaiters.has(requestId)) {\n throw new Error(\n `[Directive MCP] No pending approval request with ID '${requestId}'. ` +\n `Pending requests: ${Array.from(state.pendingApprovals.keys()).join(\", \") || \"(none)\"}`,\n );\n }\n resolveApproval(requestId, true);\n },\n\n reject(requestId: string, reason?: string) {\n const request = state.pendingApprovals.get(requestId);\n if (!request && !approvalWaiters.has(requestId)) {\n throw new Error(\n `[Directive MCP] No pending approval request with ID '${requestId}'. ` +\n `Pending requests: ${Array.from(state.pendingApprovals.keys()).join(\", \") || \"(none)\"}`,\n );\n }\n resolveApproval(requestId, false, reason);\n },\n\n getPendingApprovals() {\n return Array.from(state.pendingApprovals.values());\n },\n\n getRejectionReason(requestId: string): string | undefined {\n return rejectionReasons.get(requestId);\n },\n };\n}\n\n// ============================================================================\n// Utility Functions\n// ============================================================================\n\n/** Cache for compiled glob patterns to avoid repeated regex compilation */\nconst globCache = new Map<string, RegExp>();\nconst MAX_GLOB_CACHE_SIZE = 200;\n\n/** Simple glob matching (supports * and **) with caching */\nfunction matchGlob(str: string, pattern: string): boolean {\n let regex = globCache.get(pattern);\n if (!regex) {\n // Escape all regex metacharacters first, then convert glob wildcards\n const regexPattern = pattern\n .replace(/\\*\\*/g, \"\\0GLOBSTAR\\0\")\n .replace(/\\*/g, \"\\0STAR\\0\")\n .replace(/\\?/g, \"\\0QUESTION\\0\")\n .replace(/[.+^${}()|[\\]\\\\]/g, \"\\\\$&\")\n .replace(/\\0GLOBSTAR\\0/g, \".*\")\n .replace(/\\0STAR\\0/g, \"[^/]*\")\n .replace(/\\0QUESTION\\0/g, \".\");\n regex = new RegExp(`^${regexPattern}$`);\n\n // LRU eviction: delete oldest entry when cache is full\n if (globCache.size >= MAX_GLOB_CACHE_SIZE) {\n const firstKey = globCache.keys().next().value;\n if (firstKey !== undefined) globCache.delete(firstKey);\n }\n globCache.set(pattern, regex);\n }\n return regex.test(str);\n}\n\n/**\n * Convert MCP tools to a format suitable for LLM tool calling.\n *\n * @example\n * ```typescript\n * const adapter = createMCPAdapter({ servers: [...] });\n * await adapter.connect();\n *\n * const tools = adapter.getTools();\n * const llmTools = convertToolsForLLM(tools);\n * // Use with OpenAI/Anthropic/etc.\n * ```\n */\nexport function convertToolsForLLM(tools: Map<string, MCPTool[]>): Array<{\n type: \"function\";\n function: {\n name: string;\n description: string;\n parameters: Record<string, unknown>;\n };\n}> {\n const result: Array<{\n type: \"function\";\n function: {\n name: string;\n description: string;\n parameters: Record<string, unknown>;\n };\n }> = [];\n\n for (const [server, serverTools] of tools) {\n for (const tool of serverTools) {\n result.push({\n type: \"function\",\n function: {\n name: `${server}.${tool.name}`,\n description: tool.description ?? `Tool: ${tool.name}`,\n parameters: tool.inputSchema,\n },\n });\n }\n }\n\n return result;\n}\n\n/**\n * Create a requirement to call an MCP tool.\n *\n * @example\n * ```typescript\n * const req = mcpCallTool('filesystem', 'read_file', { path: '/etc/hosts' });\n * // { type: 'MCP_CALL_TOOL', server: 'filesystem', tool: 'read_file', args: { path: '/etc/hosts' } }\n * ```\n */\nexport function mcpCallTool(\n server: string,\n tool: string,\n args: Record<string, unknown>,\n): MCPCallToolRequirement {\n return { type: \"MCP_CALL_TOOL\", server, tool, args };\n}\n\n/**\n * Create a requirement to read an MCP resource.\n */\nexport function mcpReadResource(\n server: string,\n uri: string,\n): MCPReadResourceRequirement {\n return { type: \"MCP_READ_RESOURCE\", server, uri };\n}\n\n/**\n * Create a requirement to get an MCP prompt.\n */\nexport function mcpGetPrompt(\n server: string,\n prompt: string,\n args?: Record<string, string>,\n): MCPGetPromptRequirement {\n return { type: \"MCP_GET_PROMPT\", server, prompt, args };\n}\n\n/**\n * Create a requirement to sync MCP resources.\n */\nexport function mcpSyncResources(\n server?: string,\n pattern?: string | RegExp,\n): MCPSyncResourcesRequirement {\n return { type: \"MCP_SYNC_RESOURCES\", server, pattern };\n}\n\n// Re-export types\nexport * from \"./mcp-types.js\";\n"]}
@@ -1,4 +1,4 @@
1
1
  'use strict';var chunkK64WKZ22_cjs=require('./chunk-K64WKZ22.cjs'),internals=require('@directive-run/core/internals');function K(i){let{patterns:a=[/\b\d{3}-\d{2}-\d{4}\b/g,/\b\d{16}\b/g,/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/gi],redact:r=false,redactReplacement:t="[REDACTED]"}=i;return e=>{let n=e.input,s=false;for(let c of a)c.lastIndex=0,c.test(n)&&(s=true,r&&(c.lastIndex=0,n=n.replace(c,t)));return s&&!r?{passed:false,reason:"Input contains PII"}:{passed:true,transformed:r&&s?n:void 0}}}function ee(i){let{checkFn:a,message:r="Content flagged by moderation"}=i;return async t=>{let e="output"in t?typeof t.output=="string"?t.output:JSON.stringify(t.output):t.input,n=await a(e);return {passed:!n,reason:n?r:void 0}}}function te(i){let{maxTokensPerMinute:a=1e5,maxRequestsPerMinute:r=60}=i,t=Math.max(r,1e3),e=[],n=[],s=6e4;function c(d,u){let l=0,g=d.length;for(;l<g;){let p=l+g>>>1;(d[p]??0)<u?l=p+1:g=p;}return l}let o=(d,u)=>{let l=Date.now(),g=l-s,p=c(e,g);p>0&&(e=e.slice(p));let b=c(n,g);b>0&&(n=n.slice(b));let h=u.facts[chunkK64WKZ22_cjs.c]?.tokenUsage??0,y=e.length,I=n.length;return y+h>a?{passed:false,reason:"Token rate limit exceeded"}:I>=r?{passed:false,reason:"Request rate limit exceeded"}:(n.length<t&&n.push(l),e.length<t&&e.push(l),{passed:true})};return o.reset=()=>{e=[],n=[];},o}function ne(i){let{allowlist:a,denylist:r,caseSensitive:t=false}=i,e=a?.map(s=>t?s:s.toLowerCase()),n=r?.map(s=>t?s:s.toLowerCase());return s=>{let c=t?s.toolCall.name:s.toolCall.name.toLowerCase();return e&&!e.includes(c)?{passed:false,reason:`Tool "${s.toolCall.name}" not in allowlist`}:n?.includes(c)?{passed:false,reason:`Tool "${s.toolCall.name}" is blocked`}:{passed:true}}}function re(i){let{validate:a,errorPrefix:r="Output schema validation failed"}=i;return t=>{let e=a(t.output);return typeof e=="boolean"?{passed:e,reason:e?void 0:r}:e.valid?{passed:true}:{passed:false,reason:e.errors?.length?`${r}: ${e.errors.join("; ")}`:r}}}function ie(i){let{type:a,requiredFields:r=[],minLength:t,maxLength:e,minStringLength:n,maxStringLength:s}=i;return c=>{let o=c.output;switch(a){case "string":return typeof o!="string"?{passed:false,reason:`Expected string, got ${typeof o}`}:n!==void 0&&o.length<n?{passed:false,reason:`String too short: ${o.length} < ${n}`}:s!==void 0&&o.length>s?{passed:false,reason:`String too long: ${o.length} > ${s}`}:{passed:true};case "number":return typeof o!="number"||Number.isNaN(o)?{passed:false,reason:`Expected number, got ${typeof o}`}:{passed:true};case "boolean":return typeof o!="boolean"?{passed:false,reason:`Expected boolean, got ${typeof o}`}:{passed:true};case "object":if(typeof o!="object"||o===null||Array.isArray(o))return {passed:false,reason:`Expected object, got ${Array.isArray(o)?"array":typeof o}`};for(let d of r)if(!(d in o))return {passed:false,reason:`Missing required field: ${d}`};return {passed:true};case "array":return Array.isArray(o)?t!==void 0&&o.length<t?{passed:false,reason:`Array too short: ${o.length} < ${t}`}:e!==void 0&&o.length>e?{passed:false,reason:`Array too long: ${o.length} > ${e}`}:{passed:true}:{passed:false,reason:`Expected array, got ${typeof o}`};default:return {passed:false,reason:`Unknown type: ${a}`}}}}function ae(i){let{maxCharacters:a,maxTokens:r,estimateTokens:t=e=>Math.ceil(e.length/4)}=i;return e=>{let n=internals.safeStringify(e.output);if(a!==void 0&&n.length>a)return {passed:false,reason:`Output too long: ${n.length} characters (max: ${a})`};if(r!==void 0){let s=t(n);if(s>r)return {passed:false,reason:`Output too long: ~${s} tokens (max: ${r})`}}return {passed:true}}}function se(i){let{blockedPatterns:a,caseSensitive:r=false}=i;a.length===0&&console.warn("[Directive] createContentFilterGuardrail: blockedPatterns is empty \u2014 no content will be filtered");let t=a.map(e=>{if(e instanceof RegExp)return e;let n=e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return new RegExp(n,r?"g":"gi")});return e=>{let n=internals.safeStringify(e.output);for(let s of t)if(s.lastIndex=0,s.test(n))return {passed:false,reason:`Output contains blocked content matching: ${s.source}`};return {passed:true}}}var M=[{type:"ssn",pattern:/\b(\d{3}[-\s]?\d{2}[-\s]?\d{4})\b/g,validate:i=>{let a=i.replace(/[-\s]/g,"");return !(a.startsWith("000")||a.startsWith("666")||a.startsWith("9")||a.slice(3,5)==="00"||a.slice(5)==="0000")},confidence:.95},{type:"credit_card",pattern:/\b((?:\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4})|\d{13,19})\b/g,validate:i=>{let a=i.replace(/[-\s]/g,"");if(a.length<13||a.length>19)return false;let r=0,t=false;for(let e=a.length-1;e>=0;e--){let n=a[e];if(!n)continue;let s=Number.parseInt(n,10);t&&(s*=2,s>9&&(s-=9)),r+=s,t=!t;}return r%10===0},confidence:.95},{type:"email",pattern:/\b([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,})\b/gi,confidence:.9},{type:"phone",pattern:/\b(\+?1?[-.\s]?\(?[0-9]{3}\)?[-.\s]?[0-9]{3}[-.\s]?[0-9]{4})\b/g,validate:i=>{let a=i.replace(/\D/g,"");return a.length>=10&&a.length<=11},confidence:.8},{type:"date_of_birth",pattern:/\b(born|dob|birth.?date|date.?of.?birth)[:.\s]+(\d{1,4}[-/]\d{1,2}[-/]\d{1,4})\b/gi,valueGroup:2,confidence:.85},{type:"ip_address",pattern:/\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b/g,validate:i=>{let r=i.split(".").map(n=>Number.parseInt(n,10));if(r.some(n=>Number.isNaN(n)||n<0||n>255))return false;let[t,e]=r;return !(t===10||t===172&&e>=16&&e<=31||t===192&&e===168||t===127||t===169&&e===254||i==="0.0.0.0"||i==="255.255.255.255")},confidence:.9},{type:"bank_account",pattern:/\b(account|acct)[\s#:]+(\d{8,17})\b/gi,valueGroup:2,confidence:.7},{type:"passport",pattern:/\b(passport)[\s#:]+([A-Z0-9]{6,9})\b/gi,valueGroup:2,confidence:.75},{type:"driver_license",pattern:/\b(driver'?s?\s*licen[cs]e|dl)[\s#:]+([A-Z0-9]{5,15})\b/gi,valueGroup:2,confidence:.7},{type:"medical_id",pattern:/\b(mrn|medical.?record|patient.?id)[\s#:]+([A-Z0-9-]{6,15})\b/gi,valueGroup:2,confidence:.7},{type:"national_id",pattern:/\b(national.?id|nin|identity.?number|identity.?card|id.?number)[\s#:]+([A-Z0-9-]{6,20})\b/gi,valueGroup:2,confidence:.85}];function F(i){let a=[],r="street|st|avenue|ave|road|rd|drive|dr|lane|ln|court|ct|way|boulevard|blvd|circle|cir|place|pl",t=new RegExp(`\\b(\\d{1,5}\\s+(?:\\w+\\s+){1,4}(?:${r})\\b[^\\n]{0,50}\\b[A-Z]{2}\\s+\\d{5}(?:-\\d{4})?)\\b`,"gi"),e;for(;(e=t.exec(i))!==null;)a.push({type:"address",value:e[0],position:{start:e.index,end:e.index+e[0].length},confidence:.7});return a}var D=["mr","mrs","ms","miss","dr","prof","sir","madam","name is","called","known as","signed by","from","dear","hi","hello","contact","recipient"];function O(i){let a=[],r=D.join("|"),t=new RegExp(`\\b(${r})[.,:]?\\s+([A-Z][a-z]{1,20}(?:\\s[A-Z][a-z]{1,20}){0,2})\\b`,"gi"),e;for(;(e=t.exec(i))!==null;){let n=e[2],s=e[1];n&&(n.split(/\s+/).length>=2||s&&D.some(c=>s.toLowerCase().includes(c)))&&a.push({type:"name",value:n,position:{start:e.index,end:e.index+e[0].length},confidence:.6,context:e[0]});}return a}var N=1e5;function L(i,a){let r=[],t=new RegExp(i.pattern.source,i.pattern.flags),e=i.valueGroup??1,n;for(;(n=t.exec(a))!==null;){let s=n[e]??n[0],c=n.index,o=n.index+n[0].length,d=a.slice(Math.max(0,c-20),o+20);i.validate&&!i.validate(s,d)||r.push({type:i.type,value:s,position:{start:c,end:o},confidence:i.confidence,context:d});}return r}var P={name:"regex",async detect(i,a){if(i.length>N)throw new Error(`[Directive] Input exceeds maximum length of ${N} characters for PII detection. Truncate input or process in chunks.`);let r=[],t=new Set(a);for(let e of M)t.has(e.type)&&r.push(...L(e,i));return t.has("address")&&r.push(...F(i)),t.has("name")&&r.push(...O(i)),r}},z=new Set(["ssn","credit_card","date_of_birth","medical_id","national_id","passport","driver_license","bank_account"]);function H(i,a){let{start:r,end:t}=i.position;return Number.isInteger(r)&&Number.isInteger(t)&&r>=0&&r<t&&t<=a}function U(i,a){switch(a){case "placeholder":return "[REDACTED]";case "typed":return z.has(i.type)?"[REDACTED]":`[${i.type.toUpperCase()}]`;case "masked":{if(i.type!=="credit_card")return "****";let r=i.value.replace(/\D/g,"");return `****${r.length>4?r.slice(-4):""}`}case "hashed":return `[HASH:${V(i.value)}]`}}function _(i,a,r="typed"){let e=[...a.filter(o=>H(o,i.length))].sort((o,d)=>{if(d.confidence!==o.confidence)return d.confidence-o.confidence;let u=o.position.end-o.position.start,l=d.position.end-d.position.start;return l!==u?l-u:o.position.start-d.position.start}),n=[];for(let o of e)n.some(u=>o.position.start<u.position.end&&u.position.start<o.position.end)||n.push(o);let s=n.sort((o,d)=>d.position.start-o.position.start),c=i;for(let o of s){let d=U(o,r);c=c.slice(0,o.position.start)+d+c.slice(o.position.end);}return c}function V(i){let t=2166136261;for(let e=0;e<i.length;e++)t^=i.charCodeAt(e),t=Math.imul(t,16777619);return (t>>>0).toString(16).padStart(8,"0")}var G=["ssn","credit_card","email","phone","date_of_birth","bank_account"];function B(i={}){let{types:a=G,detector:r="regex",redact:t=false,redactionStyle:e="typed",minConfidence:n=.7,onDetected:s,allowlist:c=[],minItemsToBlock:o=1,detectorTimeout:d=5e3}=i,u=r==="regex"?P:r,l=new Set(c.map(p=>p.toLowerCase().trim()));async function g(p,b){if(u===P)return u.detect(p,b);let m,f=u.detect(p,b),h=new Promise((y,I)=>{m=setTimeout(()=>I(new Error(`[Directive] PII detector '${u.name}' timed out after ${d}ms`)),d);});f.catch(()=>{}),h.catch(()=>{});try{return await Promise.race([f,h])}finally{clearTimeout(m);}}return async p=>{let m=(await g(p.input,a)).filter(f=>!(f.confidence<n||l.has(f.value.toLowerCase().trim())));if(m.length>0&&s?.(m),m.length>=o){if(t)return {passed:true,transformed:_(p.input,m,e)};let f={};for(let y of m)f[y.type]=(f[y.type]||0)+1;return {passed:false,reason:`PII detected (${Object.entries(f).map(([y,I])=>`${y}: ${I}`).join(", ")})`}}return {passed:true}}}function ce(i={}){let a=B(i);return async(r,t)=>{let e=typeof r.output=="string"?r.output:JSON.stringify(r.output);return a({input:e,agentName:r.agentName},t)}}async function Z(i,a={}){let{types:r=G,detector:t="regex",minConfidence:e=.7,timeout:n=5e3}=a,s=t==="regex"?P:t,c;if(s===P)c=await s.detect(i,r);else {let u,l=s.detect(i,r),g=new Promise((p,b)=>{u=setTimeout(()=>b(new Error(`[Directive] PII detector '${s.name}' timed out after ${n}ms`)),n);});l.catch(()=>{}),g.catch(()=>{});try{c=await Promise.race([l,g]);}finally{clearTimeout(u);}}let o=c.filter(u=>u.confidence>=e),d={};for(let u of o)d[u.type]=(d[u.type]||0)+1;return {detected:o.length>0,items:o,typeCounts:d}}async function de(i,a={}){let r=await Z(i,a);return r.detected?{...r,redactedText:_(i,r.items,a.style??"typed")}:{...r}}var w=[{pattern:/ignore\s+(all\s+)?(previous|prior|above|earlier)\s+(instructions?|prompts?|rules?|guidelines?)/i,name:"ignore-previous",severity:"critical",category:"instruction_override"},{pattern:/disregard\s+(all\s+)?(previous|prior|above|earlier)\s+(instructions?|prompts?)/i,name:"disregard-previous",severity:"critical",category:"instruction_override"},{pattern:/forget\s+(all\s+)?(previous|prior|above|earlier)\s+(instructions?|prompts?)/i,name:"forget-previous",severity:"critical",category:"instruction_override"},{pattern:/override\s+(the\s+)?(system|base)\s+(prompt|instructions?)/i,name:"override-system",severity:"critical",category:"instruction_override"},{pattern:/\bDAN\s+(mode|jailbreak)\b/i,name:"dan-mode",severity:"critical",category:"jailbreak"},{pattern:/\bjailbreak(ed)?\s*(mode)?\b/i,name:"jailbreak-keyword",severity:"high",category:"jailbreak"},{pattern:/developer\s+mode\s+(enabled|activated|on)/i,name:"developer-mode",severity:"critical",category:"jailbreak"},{pattern:/pretend\s+(you\s+)?(are|can|have)\s+(no\s+)?(restrictions?|limits?|boundaries?|ethics)/i,name:"pretend-no-restrictions",severity:"high",category:"jailbreak"},{pattern:/you\s+(now\s+)?have\s+no\s+(ethical\s+)?(restrictions?|guidelines?|boundaries?)/i,name:"no-restrictions",severity:"high",category:"jailbreak"},{pattern:/you\s+are\s+now\s+(a|an)\s+\w+\s+(that|who)\s+(can|will|must)/i,name:"role-assignment",severity:"medium",category:"role_manipulation"},{pattern:/from\s+now\s+on,?\s+(you\s+)?(will|must|should)\s+(only\s+)?/i,name:"from-now-on",severity:"medium",category:"role_manipulation"},{pattern:/^(system|assistant|user):\s*/im,name:"fake-role-marker",severity:"high",category:"context_manipulation"},{pattern:/<\|?(system|endofprompt|im_start|im_end)\|?>/i,name:"special-token-injection",severity:"critical",category:"context_manipulation"},{pattern:/```(system|assistant|instructions?)\n/i,name:"markdown-code-injection",severity:"medium",category:"delimiter_injection"},{pattern:/<system>|<\/system>|<instructions?>|<\/instructions?>/i,name:"xml-tag-injection",severity:"high",category:"delimiter_injection"},{pattern:/fetch\s+(content\s+)?(from|at)\s+(the\s+)?url/i,name:"url-fetch-instruction",severity:"medium",category:"indirect_injection"},{pattern:/execute\s+(the\s+)?(code|script|command)\s+(from|in|at)/i,name:"execute-from-source",severity:"high",category:"indirect_injection"}],k=[...w,{pattern:/act\s+as\s+(if\s+)?(you\s+)?(were|are|can)/i,name:"act-as",severity:"low",category:"role_manipulation"},{pattern:/new\s+instructions?:/i,name:"new-instructions",severity:"medium",category:"instruction_override"},{pattern:/\[system\]|\[admin\]|\[developer\]/i,name:"bracket-role-marker",severity:"medium",category:"context_manipulation"},{pattern:/base64|rot13|decode\s+(this|the)/i,name:"encoding-reference",severity:"low",category:"encoding_evasion"},{pattern:/\u200b|\u200c|\u200d|\u2060|\ufeff/,name:"zero-width-chars",severity:"medium",category:"encoding_evasion"}],A=1e5;function $(i,a=w){if(i.length>A)throw new Error(`[Directive] Input exceeds maximum length of ${A} characters for injection detection. Truncate input or process in chunks.`);let r=[];for(let{pattern:s,name:c,severity:o,category:d}of a){let l=new RegExp(s.source,s.flags).exec(i);l&&r.push({name:c,category:d,severity:o,match:l[0],position:l.index});}let t={low:10,medium:25,high:50,critical:100},e=r.reduce((s,c)=>s+t[c.severity],0),n=Math.min(100,e);return {detected:r.length>0,patterns:r,riskScore:n}}function q(i,a=w){let r=i.replace(/\u200b|\u200c|\u200d|\u2060|\ufeff/g,""),t=a.map(d=>`(${d.pattern.source})`);if(t.length===0)return r;let e=a.some(d=>d.pattern.flags.includes("g")),n=a.some(d=>d.pattern.flags.includes("i")),s=a.some(d=>d.pattern.flags.includes("m")),c=`${e?"g":""}${n?"i":""}${s?"m":""}`,o=new RegExp(t.join("|"),c||"gi");return r=r.replace(o,"[REDACTED]"),r}function W(i={}){let{additionalPatterns:a=[],replacePatterns:r,strictMode:t=false,blockThreshold:e=50,sanitize:n=false,onBlocked:s,ignoreCategories:c=[]}=i,o;if(r?o=r:o=t?[...k]:[...w],o=[...o,...a],c.length>0){let d=new Set(c);o=o.filter(u=>!d.has(u.category));}return d=>{let u=$(d.input,o);if(u.detected&&u.riskScore>=e){if(s?.(d.input,u),n)return {passed:true,transformed:q(d.input,o)};let l=u.patterns.sort((g,p)=>{let b={critical:0,high:1,medium:2,low:3};return b[g.severity]-b[p.severity]}).slice(0,3).map(g=>g.name).join(", ");return {passed:false,reason:`Prompt injection detected (risk: ${u.riskScore}%, patterns: ${l})`}}return {passed:true}}}function ue(i,a){return `[UNTRUSTED_CONTENT source="${a}"]
2
2
  ${i}
3
- [/UNTRUSTED_CONTENT]`}function me(i){let{baseGuardrail:a=W({strictMode:true,blockThreshold:25}),additionalPatterns:r=[]}=i,t=/\[UNTRUSTED_CONTENT source="([^"]+)"\]([\s\S]*?)\[\/UNTRUSTED_CONTENT\]/g;return async(e,n)=>{let s=await a(e,n);if(!s.passed)return s;let c=e.input.matchAll(t);for(let o of c){let[,d,u]=o;if(!u)continue;let l=$(u,[...k,...r]);if(l.detected&&l.riskScore>=25)return {passed:false,reason:`Untrusted content from "${d}" contains potential injection (risk: ${l.riskScore}%)`}}return {passed:true}}}function v(i,a){if(i.length!==a.length)throw new Error(`Vector dimensions must match: ${i.length} vs ${a.length}`);let r=0,t=0,e=0;for(let n=0;n<i.length;n++){let s=i[n],c=a[n];r+=s*c,t+=s*s,e+=c*c;}return t=Math.sqrt(t),e=Math.sqrt(e),t===0||e===0?0:r/(t*e)}function J(i,a,r,t){let e=null;for(let n of a){if(t&&n.agentName&&n.agentName!==t)continue;let s=v(i,n.queryEmbedding);s>=r&&(!e||s>e.similarity)&&(e={entry:n,similarity:s});}return e}function Y(){let i=new Map;function a(r){let t=i.get(r);return t||(t=new Map,i.set(r,t)),t}return {async getEntries(r){return Array.from(a(r).values())},async addEntry(r,t){a(r).set(t.id,t);},async updateEntry(r,t,e){let n=a(r),s=n.get(t);s&&n.set(t,{...s,...e});},async removeEntry(r,t){a(r).delete(t);},async clear(r){i.delete(r);}}}function ge(i){let{embedder:a,similarityThreshold:r=.9,maxCacheSize:t=1e3,ttlMs:e=36e5,namespace:n="default",storage:s=Y(),onHit:c,onMiss:o,onError:d,perAgent:u=false}=i,l={totalEntries:0,totalHits:0,totalMisses:0,hitRate:0,avgSimilarityOnHit:0,oldestEntry:null,newestEntry:null},g=0;function p(m){if(l.totalEntries=m.length,l.hitRate=l.totalHits+l.totalMisses>0?l.totalHits/(l.totalHits+l.totalMisses):0,l.avgSimilarityOnHit=l.totalHits>0?g/l.totalHits:0,m.length>0){let f=m.map(h=>h.createdAt);l.oldestEntry=Math.min(...f),l.newestEntry=Math.max(...f);}else l.oldestEntry=null,l.newestEntry=null;}async function b(){let m=await s.getEntries(n),f=Date.now();for(let y of m)f-y.createdAt>e&&await s.removeEntry(n,y.id);let h=await s.getEntries(n);if(h.length>t){let I=h.sort((E,S)=>E.accessedAt-S.accessedAt).slice(0,h.length-t);for(let E of I)await s.removeEntry(n,E.id);}}return {async lookup(m,f){let h=Date.now();try{let y=await a(m),I=await s.getEntries(n),E=J(y,I,r,u?f:void 0);return E?(await s.updateEntry(n,E.entry.id,{accessedAt:Date.now(),accessCount:E.entry.accessCount+1}),l.totalHits++,g+=E.similarity,p(I),c?.(E.entry,E.similarity),{hit:!0,entry:E.entry,similarity:E.similarity,latencyMs:Date.now()-h}):(l.totalMisses++,p(I),o?.(m),{hit:!1,latencyMs:Date.now()-h})}catch(y){return l.totalMisses++,d?.(y instanceof Error?y:new Error(String(y))),{hit:false,latencyMs:Date.now()-h}}},async store(m,f,h,y={}){let I=await a(m),E={id:globalThis.crypto?.randomUUID?.()??`${Date.now()}-${Math.random().toString(36).slice(2,11)}`,query:m,queryEmbedding:I,response:f,metadata:y,createdAt:Date.now(),accessedAt:Date.now(),accessCount:0,agentName:u?h:void 0};await s.addEntry(n,E),await b();let S=await s.getEntries(n);p(S);},async invalidate(m){let f=await s.getEntries(n),h=0;for(let I of f)m(I)&&(await s.removeEntry(n,I.id),h++);let y=await s.getEntries(n);return p(y),h},async clear(){await s.clear(n),l={totalEntries:0,totalHits:0,totalMisses:0,hitRate:0,avgSimilarityOnHit:0,oldestEntry:null,newestEntry:null},g=0;},getStats(){return {...l}},async export(){return s.getEntries(n)},async import(m){for(let h of m)await s.addEntry(n,h);await b();let f=await s.getEntries(n);p(f);}}}function fe(i){let{cache:a}=i;return async r=>{let t=await a.lookup(r.input,r.agentName);return t.hit&&t.entry?{passed:false,cacheHit:true,reason:`Cache hit (similarity: ${(t.similarity*100).toFixed(1)}%)`,cachedResponse:t.entry.response,similarity:t.similarity}:{passed:true,cacheHit:false}}}function he(i=128){return async a=>{let r=new Array(i).fill(0);for(let e=0;e<a.length;e++){let n=a.charCodeAt(e);r[e%i]+=n/256;}let t=Math.sqrt(r.reduce((e,n)=>e+n*n,0));if(t>0)for(let e=0;e<i;e++)r[e]/=t;return r}}function ye(i){let{batchSize:a=20,embedBatch:r,maxWaitMs:t=50}=i;if(a<1||!Number.isFinite(a))throw new Error(`[Directive SemanticCache] batchSize must be >= 1, got ${a}`);let e=[],n=null,s=false;async function c(){if(e.length===0)return;let o=e;e=[],n&&(clearTimeout(n),n=null);try{let d=o.map(l=>l.text),u=await r(d);if(u.length!==o.length)throw new Error(`[Directive SemanticCache] embedBatch returned ${u.length} embeddings for ${o.length} texts. The embedBatch function must return exactly one embedding per input text.`);for(let l=0;l<o.length;l++)o[l].resolve(u[l]);}catch(d){let u=d instanceof Error?d:new Error(String(d));for(let l of o)l.reject(u);}}return {async embed(o){if(s)throw new Error("BatchedEmbedder has been destroyed");return new Promise((d,u)=>{e.push({text:o,resolve:d,reject:u}),e.length>=a?c():n||(n=setTimeout(c,t));})},async flush(){await c();},destroy(){s=true,n&&(clearTimeout(n),n=null);let o=e;e=[];let d=new Error("BatchedEmbedder destroyed");for(let u of o)u.reject(d);}}}function Ee(){let i=new Map,a=null;function r(t){if(a===null)a=t.length;else if(t.length!==a)throw new Error(`[Directive ANNIndex] Dimension mismatch: expected ${a}, got ${t.length}`)}return {add(t,e){r(e),i.set(t,e);},remove(t){i.delete(t);},search(t,e,n=0){if(a!==null&&t.length!==a)throw new Error(`[Directive ANNIndex] Query dimension mismatch: expected ${a}, got ${t.length}`);let s=[];for(let[c,o]of i){let d=v(t,o);d>=n&&s.push({id:c,similarity:d});}return s.sort((c,o)=>o.similarity-c.similarity),s.slice(0,e)},rebuild(){},size(){return i.size},clear(){i.clear(),a=null;},needsRebuild(){return false}}}function j(i,a){return 1-v(i,a)}function T(i,a){if(i.length===0)return null;let r=Math.floor(a()*i.length),t=i[r],e=i.filter((u,l)=>l!==r);if(e.length===0)return {id:t.id,embedding:t.embedding,mu:0,left:null,right:null};let n=e.map(u=>({item:u,distance:j(t.embedding,u.embedding)}));n.sort((u,l)=>u.distance-l.distance);let s=Math.floor(n.length/2),c=n[s].distance,o=n.slice(0,s).map(u=>u.item),d=n.slice(s).map(u=>u.item);return {id:t.id,embedding:t.embedding,mu:c,left:T(o,a),right:T(d,a)}}function x(i,a,r,t,e,n){if(!i)return;let s=j(a,i.embedding),c=1-s;c>=t&&(e.push({id:i.id,similarity:c}),e.sort((o,d)=>d.similarity-o.similarity),e.length>r&&e.pop(),e.length===r&&(n.value=1-e[e.length-1].similarity)),s<i.mu?(x(i.left,a,r,t,e,n),s+n.value>=i.mu&&x(i.right,a,r,t,e,n)):(x(i.right,a,r,t,e,n),s-n.value<=i.mu&&x(i.left,a,r,t,e,n));}function ve(i={}){let{random:a=Math.random}=i,r=new Map,t=null,e=false,n=null;function s(c){if(n===null)n=c.length;else if(c.length!==n)throw new Error(`[Directive ANNIndex] Dimension mismatch: expected ${n}, got ${c.length}`)}return {add(c,o){s(o),r.set(c,o),e=true;},remove(c){r.delete(c),e=true;},search(c,o,d=0){if(n!==null&&c.length!==n)throw new Error(`[Directive ANNIndex] Query dimension mismatch: expected ${n}, got ${c.length}`);if(e||!t){let g=[];for(let[p,b]of r){let m=v(c,b);m>=d&&g.push({id:p,similarity:m});}return g.sort((p,b)=>b.similarity-p.similarity),g.slice(0,o)}let u=[],l={value:Number.POSITIVE_INFINITY};return x(t,c,o,d,u,l),u},rebuild(){let c=Array.from(r.entries()).map(([o,d])=>({id:o,embedding:d}));t=T(c,a),e=false;},size(){return r.size},clear(){r.clear(),t=null,e=false,n=null;},needsRebuild(){return e}}}exports.A=Ee;exports.B=ve;exports.a=K;exports.b=ee;exports.c=te;exports.d=ne;exports.e=re;exports.f=ie;exports.g=ae;exports.h=se;exports.i=_;exports.j=B;exports.k=ce;exports.l=Z;exports.m=de;exports.n=w;exports.o=k;exports.p=$;exports.q=q;exports.r=W;exports.s=ue;exports.t=me;exports.u=v;exports.v=Y;exports.w=ge;exports.x=fe;exports.y=he;exports.z=ye;//# sourceMappingURL=chunk-A5K77UDX.cjs.map
4
- //# sourceMappingURL=chunk-A5K77UDX.cjs.map
3
+ [/UNTRUSTED_CONTENT]`}function me(i){let{baseGuardrail:a=W({strictMode:true,blockThreshold:25}),additionalPatterns:r=[]}=i,t=/\[UNTRUSTED_CONTENT source="([^"]+)"\]([\s\S]*?)\[\/UNTRUSTED_CONTENT\]/g;return async(e,n)=>{let s=await a(e,n);if(!s.passed)return s;let c=e.input.matchAll(t);for(let o of c){let[,d,u]=o;if(!u)continue;let l=$(u,[...k,...r]);if(l.detected&&l.riskScore>=25)return {passed:false,reason:`Untrusted content from "${d}" contains potential injection (risk: ${l.riskScore}%)`}}return {passed:true}}}function v(i,a){if(i.length!==a.length)throw new Error(`Vector dimensions must match: ${i.length} vs ${a.length}`);let r=0,t=0,e=0;for(let n=0;n<i.length;n++){let s=i[n],c=a[n];r+=s*c,t+=s*s,e+=c*c;}return t=Math.sqrt(t),e=Math.sqrt(e),t===0||e===0?0:r/(t*e)}function J(i,a,r,t){let e=null;for(let n of a){if(t&&n.agentName&&n.agentName!==t)continue;let s=v(i,n.queryEmbedding);s>=r&&(!e||s>e.similarity)&&(e={entry:n,similarity:s});}return e}function Y(){let i=new Map;function a(r){let t=i.get(r);return t||(t=new Map,i.set(r,t)),t}return {async getEntries(r){return Array.from(a(r).values())},async addEntry(r,t){a(r).set(t.id,t);},async updateEntry(r,t,e){let n=a(r),s=n.get(t);s&&n.set(t,{...s,...e});},async removeEntry(r,t){a(r).delete(t);},async clear(r){i.delete(r);}}}function ge(i){let{embedder:a,similarityThreshold:r=.9,maxCacheSize:t=1e3,ttlMs:e=36e5,namespace:n="default",storage:s=Y(),onHit:c,onMiss:o,onError:d,perAgent:u=false}=i,l={totalEntries:0,totalHits:0,totalMisses:0,hitRate:0,avgSimilarityOnHit:0,oldestEntry:null,newestEntry:null},g=0;function p(m){if(l.totalEntries=m.length,l.hitRate=l.totalHits+l.totalMisses>0?l.totalHits/(l.totalHits+l.totalMisses):0,l.avgSimilarityOnHit=l.totalHits>0?g/l.totalHits:0,m.length>0){let f=m.map(h=>h.createdAt);l.oldestEntry=Math.min(...f),l.newestEntry=Math.max(...f);}else l.oldestEntry=null,l.newestEntry=null;}async function b(){let m=await s.getEntries(n),f=Date.now();for(let y of m)f-y.createdAt>e&&await s.removeEntry(n,y.id);let h=await s.getEntries(n);if(h.length>t){let I=h.sort((E,S)=>E.accessedAt-S.accessedAt).slice(0,h.length-t);for(let E of I)await s.removeEntry(n,E.id);}}return {async lookup(m,f){let h=Date.now();try{let y=await a(m),I=await s.getEntries(n),E=J(y,I,r,u?f:void 0);return E?(await s.updateEntry(n,E.entry.id,{accessedAt:Date.now(),accessCount:E.entry.accessCount+1}),l.totalHits++,g+=E.similarity,p(I),c?.(E.entry,E.similarity),{hit:!0,entry:E.entry,similarity:E.similarity,latencyMs:Date.now()-h}):(l.totalMisses++,p(I),o?.(m),{hit:!1,latencyMs:Date.now()-h})}catch(y){return l.totalMisses++,d?.(y instanceof Error?y:new Error(String(y))),{hit:false,latencyMs:Date.now()-h}}},async store(m,f,h,y={}){let I=await a(m),E={id:globalThis.crypto?.randomUUID?.()??`${Date.now()}-${Math.random().toString(36).slice(2,11)}`,query:m,queryEmbedding:I,response:f,metadata:y,createdAt:Date.now(),accessedAt:Date.now(),accessCount:0,agentName:u?h:void 0};await s.addEntry(n,E),await b();let S=await s.getEntries(n);p(S);},async invalidate(m){let f=await s.getEntries(n),h=0;for(let I of f)m(I)&&(await s.removeEntry(n,I.id),h++);let y=await s.getEntries(n);return p(y),h},async clear(){await s.clear(n),l={totalEntries:0,totalHits:0,totalMisses:0,hitRate:0,avgSimilarityOnHit:0,oldestEntry:null,newestEntry:null},g=0;},getStats(){return {...l}},async export(){return s.getEntries(n)},async import(m){for(let h of m)await s.addEntry(n,h);await b();let f=await s.getEntries(n);p(f);}}}function fe(i){let{cache:a}=i;return async r=>{let t=await a.lookup(r.input,r.agentName);return t.hit&&t.entry?{passed:false,cacheHit:true,reason:`Cache hit (similarity: ${(t.similarity*100).toFixed(1)}%)`,cachedResponse:t.entry.response,similarity:t.similarity}:{passed:true,cacheHit:false}}}function he(i=128){return async a=>{let r=new Array(i).fill(0);for(let e=0;e<a.length;e++){let n=a.charCodeAt(e);r[e%i]+=n/256;}let t=Math.sqrt(r.reduce((e,n)=>e+n*n,0));if(t>0)for(let e=0;e<i;e++)r[e]/=t;return r}}function ye(i){let{batchSize:a=20,embedBatch:r,maxWaitMs:t=50}=i;if(a<1||!Number.isFinite(a))throw new Error(`[Directive SemanticCache] batchSize must be >= 1, got ${a}`);let e=[],n=null,s=false;async function c(){if(e.length===0)return;let o=e;e=[],n&&(clearTimeout(n),n=null);try{let d=o.map(l=>l.text),u=await r(d);if(u.length!==o.length)throw new Error(`[Directive SemanticCache] embedBatch returned ${u.length} embeddings for ${o.length} texts. The embedBatch function must return exactly one embedding per input text.`);for(let l=0;l<o.length;l++)o[l].resolve(u[l]);}catch(d){let u=d instanceof Error?d:new Error(String(d));for(let l of o)l.reject(u);}}return {async embed(o){if(s)throw new Error("BatchedEmbedder has been destroyed");return new Promise((d,u)=>{e.push({text:o,resolve:d,reject:u}),e.length>=a?c():n||(n=setTimeout(c,t));})},async flush(){await c();},destroy(){s=true,n&&(clearTimeout(n),n=null);let o=e;e=[];let d=new Error("BatchedEmbedder destroyed");for(let u of o)u.reject(d);}}}function Ee(){let i=new Map,a=null;function r(t){if(a===null)a=t.length;else if(t.length!==a)throw new Error(`[Directive ANNIndex] Dimension mismatch: expected ${a}, got ${t.length}`)}return {add(t,e){r(e),i.set(t,e);},remove(t){i.delete(t);},search(t,e,n=0){if(a!==null&&t.length!==a)throw new Error(`[Directive ANNIndex] Query dimension mismatch: expected ${a}, got ${t.length}`);let s=[];for(let[c,o]of i){let d=v(t,o);d>=n&&s.push({id:c,similarity:d});}return s.sort((c,o)=>o.similarity-c.similarity),s.slice(0,e)},rebuild(){},size(){return i.size},clear(){i.clear(),a=null;},needsRebuild(){return false}}}function j(i,a){return 1-v(i,a)}function T(i,a){if(i.length===0)return null;let r=Math.floor(a()*i.length),t=i[r],e=i.filter((u,l)=>l!==r);if(e.length===0)return {id:t.id,embedding:t.embedding,mu:0,left:null,right:null};let n=e.map(u=>({item:u,distance:j(t.embedding,u.embedding)}));n.sort((u,l)=>u.distance-l.distance);let s=Math.floor(n.length/2),c=n[s].distance,o=n.slice(0,s).map(u=>u.item),d=n.slice(s).map(u=>u.item);return {id:t.id,embedding:t.embedding,mu:c,left:T(o,a),right:T(d,a)}}function x(i,a,r,t,e,n){if(!i)return;let s=j(a,i.embedding),c=1-s;c>=t&&(e.push({id:i.id,similarity:c}),e.sort((o,d)=>d.similarity-o.similarity),e.length>r&&e.pop(),e.length===r&&(n.value=1-e[e.length-1].similarity)),s<i.mu?(x(i.left,a,r,t,e,n),s+n.value>=i.mu&&x(i.right,a,r,t,e,n)):(x(i.right,a,r,t,e,n),s-n.value<=i.mu&&x(i.left,a,r,t,e,n));}function ve(i={}){let{random:a=Math.random}=i,r=new Map,t=null,e=false,n=null;function s(c){if(n===null)n=c.length;else if(c.length!==n)throw new Error(`[Directive ANNIndex] Dimension mismatch: expected ${n}, got ${c.length}`)}return {add(c,o){s(o),r.set(c,o),e=true;},remove(c){r.delete(c),e=true;},search(c,o,d=0){if(n!==null&&c.length!==n)throw new Error(`[Directive ANNIndex] Query dimension mismatch: expected ${n}, got ${c.length}`);if(e||!t){let g=[];for(let[p,b]of r){let m=v(c,b);m>=d&&g.push({id:p,similarity:m});}return g.sort((p,b)=>b.similarity-p.similarity),g.slice(0,o)}let u=[],l={value:Number.POSITIVE_INFINITY};return x(t,c,o,d,u,l),u},rebuild(){let c=Array.from(r.entries()).map(([o,d])=>({id:o,embedding:d}));t=T(c,a),e=false;},size(){return r.size},clear(){r.clear(),t=null,e=false,n=null;},needsRebuild(){return e}}}exports.A=Ee;exports.B=ve;exports.a=K;exports.b=ee;exports.c=te;exports.d=ne;exports.e=re;exports.f=ie;exports.g=ae;exports.h=se;exports.i=_;exports.j=B;exports.k=ce;exports.l=Z;exports.m=de;exports.n=w;exports.o=k;exports.p=$;exports.q=q;exports.r=W;exports.s=ue;exports.t=me;exports.u=v;exports.v=Y;exports.w=ge;exports.x=fe;exports.y=he;exports.z=ye;//# sourceMappingURL=chunk-CAA6Q4YY.cjs.map
4
+ //# sourceMappingURL=chunk-CAA6Q4YY.cjs.map