@fugood/buttress-server 2.26.0-beta.16 → 2.26.0-beta.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -2
- package/lib/agent/codemode-worker.d.ts +1 -0
- package/lib/agent/codemode.d.ts +7 -0
- package/lib/agent/mcp.d.ts +2 -3
- package/lib/agent/sessions.d.ts +21 -8
- package/lib/agent/types.d.ts +16 -0
- package/lib/{cli-CtBbHMYQ.mjs → cli-DMI3uIfZ.mjs} +2 -2
- package/lib/{client-DzfRSFcJ.mjs → client-DEUSDK5d.mjs} +1 -1
- package/lib/codemode-CLA-kViz.mjs +112 -0
- package/lib/codemode-worker.mjs +3 -0
- package/lib/config-DtcwsKrn.mjs +2 -0
- package/lib/index.mjs +66 -63
- package/lib/mcp-BkX8G1Gc.mjs +5 -0
- package/lib/protocol-RJSV46H1.mjs +351 -0
- package/lib/quickjs.wasm +0 -0
- package/lib/{tui-DbQ0zW-C.mjs → tui-Bl2ADqOw.mjs} +1 -1
- package/package.json +9 -8
- package/lib/agent/session-fs.d.ts +0 -3
- package/lib/config-DbRjQnNp.mjs +0 -2
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import{i as e,r as t,t as n}from"./rolldown-runtime-dTnj95Mm.mjs";import{createHash as r}from"node:crypto";import i from"node:path";import{spawn as a}from"node:child_process";import o from"node:process";import{createServer as s}from"node:http";const c=[`codemode`,`deferred`,`direct`,`hidden`],l={"codemode-deferred":`codemode`},u=[`localhost`,`127.0.0.1`,`[::1]`],d=e=>typeof e==`object`&&!!e&&!Array.isArray(e),f=e=>d(e)&&Object.values(e).every(e=>typeof e==`string`),p=e=>typeof e==`string`?l[e]??e:e,m=e=>c.includes(e);function h(e){return`mcp__${String(e).replace(/[^A-Za-z0-9_]/g,`_`)}`}const ee=e=>RegExp(`^${e.split(`*`).map(e=>e.replace(/[.+?^${}()|[\]\\]/g,`\\$&`)).join(`.*`)}$`);function te(e,t,n=`deferred`){let r=d(e?.toolExposure)?e.toolExposure:{},i=Object.hasOwn(r,t)?r[t]:void 0;if(i!==void 0)return i;for(let[e,n]of Object.entries(r))if(e.includes(`*`)&&ee(e).test(t))return n;return e?.exposure??n}const ne=e=>{if(!URL.canParse(e))return!1;let t=new URL(e);return t.protocol===`http:`&&u.includes(t.hostname)&&t.search===``&&t.hash===``};function re(e){if(e===void 0)return;if(!d(e))return`oauth must be an object`;for(let t of[`clientId`,`clientSecret`,`scope`])if(e[t]!==void 0&&typeof e[t]!=`string`)return`oauth.${t} must be a string`;let t=e.callbackPort;if(t!==void 0&&(typeof t!=`number`||!Number.isInteger(t)||t<1||t>65535))return`oauth.callbackPort must be a port number`;if(e.callbackUrl!==void 0){if(typeof e.callbackUrl!=`string`||!ne(e.callbackUrl))return`oauth.callbackUrl must be an http URI on localhost, 127.0.0.1, or [::1] without query or fragment`;let n=new URL(e.callbackUrl).port;if(n&&t!==void 0&&Number(n)!==t)return`oauth.callbackUrl and oauth.callbackPort name different ports`}if(e.clientName!==void 0&&(typeof e.clientName!=`string`||!e.clientName.trim()))return`oauth.clientName must be a non-empty string`;let n=e.authServerMetadataUrl;if(n!==void 0){let e=typeof n==`string`&&URL.canParse(n)?new URL(n):null;if(!(e&&(e.protocol===`https:`||e.protocol===`http:`&&u.includes(e.hostname))))return`oauth.authServerMetadataUrl must be an https URL, or http on localhost, 127.0.0.1, or [::1]`}}function ie(e,t){if(typeof e!=`string`||!e)return`server names must be non-empty strings`;if(!d(t))return`server "${e}" must be an object`;let n={...t};n.exposure!==void 0&&(n.exposure=p(n.exposure)),d(n.toolExposure)&&(n.toolExposure=Object.fromEntries(Object.entries(n.toolExposure).map(([e,t])=>[e,p(t)])));let r=c.map(e=>`"${e}"`).join(`, `);if(n.exposure!==void 0&&!m(n.exposure))return`server "${e}": exposure must be one of ${r}`;if(n.toolExposure!==void 0){if(!d(n.toolExposure))return`server "${e}": toolExposure must map tool names to exposures`;for(let[t,i]of Object.entries(n.toolExposure))if(!m(i))return`server "${e}": toolExposure "${t}" must be one of ${r}`}if(n.enabled!==void 0&&typeof n.enabled!=`boolean`)return`server "${e}": enabled must be a boolean`;if(n.description!==void 0&&typeof n.description!=`string`)return`server "${e}": description must be a string`;if(n.timeout!==void 0&&(typeof n.timeout!=`number`||!(n.timeout>0)))return`server "${e}": timeout must be a positive number of seconds`;let{type:i}=n;if(i===`sse`)return`server "${e}": the legacy SSE transport is not supported; use the streamable HTTP URL`;if(typeof n.url==`string`&&(i===void 0||i===`http`||i===`streamable-http`)){if(!URL.canParse(n.url)||!/^https?:$/.test(new URL(n.url).protocol))return`server "${e}": url must be an http or https URL`;if(n.headers!==void 0&&!f(n.headers))return`server "${e}": headers must map names to strings`;let t=re(n.oauth);return t?`server "${e}": ${t}`:n}return typeof n.command==`string`&&(i===void 0||i===`stdio`)?n.args!==void 0&&!(Array.isArray(n.args)&&n.args.every(e=>typeof e==`string`))?`server "${e}": args must be an array of strings`:n.env!==void 0&&!f(n.env)?`server "${e}": env must map names to strings`:n.cwd!==void 0&&typeof n.cwd!=`string`?`server "${e}": cwd must be a string`:n:`server "${e}" needs either "command" (stdio) or "url" (streamable HTTP)`}const g={parseError:-32700,invalidRequest:-32600,methodNotFound:-32601,invalidParams:-32602,internalError:-32603};var _=class extends Error{code;data;constructor(e,t,n){super(t),this.name=`McpError`,this.code=e,this.data=n}},v=class extends Error{constructor(e=`MCP connection closed`){super(e),this.name=`McpConnectionClosedError`}},ae=class extends Error{timeoutMs;constructor(e){super(`MCP request timed out after ${e}ms`),this.name=`McpTimeoutError`,this.timeoutMs=e}},oe=class extends Error{constructor(e=`MCP request aborted`){super(e),this.name=`AbortError`}};function y(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function b(e){return e instanceof Error?e:Error(String(e))}function x(e){return typeof e==`string`||typeof e==`number`&&Number.isFinite(e)}function S(e){return y(e)&&e.jsonrpc===`2.0`&&x(e.id)&&typeof e.method==`string`}function se(e){return y(e)&&e.jsonrpc===`2.0`&&!(`id`in e)&&typeof e.method==`string`}function C(e){return!y(e)||e.jsonrpc!==`2.0`||!x(e.id)?!1:`result`in e?!(`error`in e):!(`error`in e)||!y(e.error)?!1:typeof e.error.code==`number`&&typeof e.error.message==`string`}function w(e){if(S(e)||se(e)||C(e))return e;throw new _(g.invalidRequest,`Invalid JSON-RPC message`)}const ce=`2025-11-25`,le=[ce,`2025-06-18`,`2025-03-26`,`2024-11-05`],ue=1e3;function de(e){if(!y(e)||typeof e.protocolVersion!=`string`||!y(e.capabilities)||!y(e.serverInfo)||typeof e.serverInfo.name!=`string`||typeof e.serverInfo.version!=`string`||e.instructions!==void 0&&typeof e.instructions!=`string`)throw new _(g.invalidRequest,`Invalid MCP initialize result`);return e}function T(e){return new _(g.invalidRequest,e)}function fe(e,t,n,r){let i=y(n)?n[t]:void 0;if(!y(n)||!Array.isArray(i))throw T(`Invalid MCP ${e} result`);for(let t of i)if(!y(t)||!r(t))throw T(`Invalid entry in MCP ${e} result`);let a=n.nextCursor===null||n.nextCursor===``?void 0:n.nextCursor;if(a!==void 0&&typeof a!=`string`)throw T(`Invalid MCP ${e} cursor`);return{items:i,...a===void 0?{}:{nextCursor:a}}}const pe=e=>typeof e.name==`string`&&y(e.inputSchema),E=e=>typeof e.uri==`string`&&(e.name===void 0||typeof e.name==`string`),me=e=>typeof e.uriTemplate==`string`&&(e.name===void 0||typeof e.name==`string`);function he(e){return{...e,name:e.name??e.uri}}function ge(e){return{...e,name:e.name??e.uriTemplate}}function _e(e){return e.nextCursor===void 0?{}:{nextCursor:e.nextCursor}}function ve(e){if(!y(e)||!Array.isArray(e.contents))throw T(`Invalid MCP resources/read result`);for(let t of e.contents)if(!y(t)||typeof t.uri!=`string`||typeof t.text!=`string`&&typeof t.blob!=`string`)throw T(`Invalid contents in MCP resources/read result`);return e}function ye(e){if(!y(e)||e.content!==void 0&&!Array.isArray(e.content))throw new _(g.invalidRequest,`Invalid MCP tools/call result`);if(e.structuredContent!==void 0&&!y(e.structuredContent))throw new _(g.invalidRequest,`Invalid MCP tools/call structured content`);return e.content===void 0?{...e,content:[]}:e}var be=class{options;state=`idle`;transport;nextRequestId=1;serverInfoValue;serverCapabilitiesValue;instructionsValue;protocolVersionValue;pending=new Map;progressRequests=new Map;incoming=new Map;requestHandlers=new Map;notificationListeners=new Map;errorListeners=new Set;closeListeners=new Set;disposers=[];constructor(e){this.options=Object.freeze({...e}),this.requestHandlers.set(`ping`,()=>({}));let t=e.roots;t&&this.requestHandlers.set(`roots/list`,async()=>({roots:[...typeof t==`function`?await t():t]}))}get connectionState(){return this.state}get serverInfo(){return this.serverInfoValue}get serverCapabilities(){return this.serverCapabilitiesValue}get instructions(){return this.instructionsValue}get protocolVersion(){return this.protocolVersionValue}async connect(e){if(this.state!==`idle`)throw Error(`Cannot connect MCP client in ${this.state} state`);this.state=`connecting`,this.transport=e,this.disposers=[e.onMessage(e=>this.handleMessage(e)),e.onError(e=>this.emitError(e)),e.onClose(()=>this.handleTransportClose())];try{await e.start();let t={...this.options.capabilities};this.options.roots&&t.roots===void 0&&(t.roots={});let n=de(await this.requestInternal(`initialize`,{protocolVersion:this.options.protocolVersion??`2025-11-25`,capabilities:t,clientInfo:{name:this.options.name,version:this.options.version,...this.options.title===void 0?{}:{title:this.options.title}}},{},!0));if(!le.includes(n.protocolVersion))throw Error(`MCP server selected unsupported protocol version ${n.protocolVersion}`);return this.protocolVersionValue=n.protocolVersion,this.serverInfoValue=n.serverInfo,this.serverCapabilitiesValue=n.capabilities,this.instructionsValue=n.instructions,e.setProtocolVersion?.(n.protocolVersion),await this.notifyInternal(`notifications/initialized`,void 0,!0),this.state=`connected`,n}catch(e){throw await this.close().catch(()=>{}),e}}request(e,t,n={}){return this.requestInternal(e,t,n,!1)}notify(e,t){return this.notifyInternal(e,t,!1)}setRequestHandler(e,t){return this.requestHandlers.set(e,t),()=>{this.requestHandlers.get(e)===t&&this.requestHandlers.delete(e)}}onNotification(e,t){let n=this.notificationListeners.get(e)??new Set;return this.notificationListeners.set(e,n),n.add(t),()=>{n.delete(t),n.size===0&&this.notificationListeners.delete(e)}}onError(e){return this.errorListeners.add(e),()=>this.errorListeners.delete(e)}onClose(e){return this.closeListeners.add(e),()=>this.closeListeners.delete(e)}async ping(e={}){await this.request(`ping`,void 0,e)}async listTools(e={}){return await this.listAll(`tools/list`,`tools`,pe,e)}async listResources(e={}){return(await this.listAll(`resources/list`,`resources`,E,e)).map(he)}async listResourcesPage(e,t={}){let n=await this.listPage(`resources/list`,`resources`,E,e,t);return{resources:n.items.map(he),..._e(n)}}async listResourceTemplates(e={}){return(await this.listAll(`resources/templates/list`,`resourceTemplates`,me,e)).map(ge)}async listResourceTemplatesPage(e,t={}){let n=await this.listPage(`resources/templates/list`,`resourceTemplates`,me,e,t);return{resourceTemplates:n.items.map(ge),..._e(n)}}async readResource(e,t={}){return ve(await this.request(`resources/read`,{uri:e},t))}async listPage(e,t,n,r,i){return fe(e,t,await this.request(e,r===void 0?void 0:{cursor:r},i),n)}async listAll(e,t,n,r){let i=[],a=new Set,o;for(let s=0;s<ue;s++){let s=await this.listPage(e,t,n,o,r);if(i.push(...s.items),s.nextCursor===void 0)return i;if(a.has(s.nextCursor))throw Error(`MCP ${e} returned duplicate cursor: ${s.nextCursor}`);a.add(s.nextCursor),o=s.nextCursor}throw Error(`MCP ${e} exceeded ${ue} pages`)}async callTool(e,t,n={}){return ye(await this.request(`tools/call`,{name:e,...t===void 0?{}:{arguments:t}},n))}async close(){let e=this.transport;this.transport=void 0,this.disposeTransportListeners(),this.markClosed(new v),await e?.close()}async requestInternal(e,t,n,r){let i=this.requireTransport(r);if(n.signal?.aborted)throw new oe;let a=this.nextRequestId++,o=n.onProgress?a:void 0,s=o===void 0?t:{...t,_meta:{...y(t?._meta)?t._meta:{},progressToken:o}},c={jsonrpc:`2.0`,id:a,method:e,...s===void 0?{}:{params:s}};return new Promise((t,r)=>{let s={resolve:t,reject:r,timeoutMs:n.timeoutMs??this.options.requestTimeoutMs??3e4,timer:void 0,signal:n.signal,onAbort:()=>this.cancelPending(a,new oe,e!==`initialize`,String(n.signal?.reason??`Aborted`)),cancellable:e!==`initialize`,onProgress:n.onProgress,progressToken:o};this.pending.set(a,s),o!==void 0&&this.progressRequests.set(o,a),n.signal?.addEventListener(`abort`,s.onAbort,{once:!0}),this.armTimeout(a,s),i.send(c).catch(e=>this.cancelPending(a,e,!1))})}async notifyInternal(e,t,n){await this.requireTransport(n).send({jsonrpc:`2.0`,method:e,...t===void 0?{}:{params:t}})}requireTransport(e){if(this.transport&&(this.state===`connected`||e&&this.state===`connecting`))return this.transport;throw new v(`MCP client is ${this.state}`)}handleMessage(e){if(C(e)){this.handleResponse(e);return}if(S(e)){this.handleRequest(e);return}if(se(e)){this.handleNotification(e.method,e.params);return}this.emitError(new _(g.invalidRequest,`Received invalid JSON-RPC message`))}handleResponse(e){let t=this.pending.get(e.id);if(!t){this.emitError(Error(`Received response for unknown MCP request ${String(e.id)}`));return}this.removePending(e.id,t),`error`in e?t.reject(new _(e.error.code,e.error.message,e.error.data)):t.resolve(e.result)}async handleRequest(e){let t=this.transport;if(!t)return;let n=this.requestHandlers.get(e.method);if(!n){await t.send({jsonrpc:`2.0`,id:e.id,error:{code:g.methodNotFound,message:`Method not found: ${e.method}`}}).catch(e=>this.emitError(e));return}let r=new AbortController;this.incoming.set(e.id,r);try{let i=await n(e.params,{signal:r.signal});await t.send({jsonrpc:`2.0`,id:e.id,result:i??{}})}catch(n){let r=n instanceof _?{code:n.code,message:n.message,data:n.data}:{code:g.internalError,message:b(n).message};await t.send({jsonrpc:`2.0`,id:e.id,error:r}).catch(e=>this.emitError(e))}finally{this.incoming.delete(e.id)}}handleNotification(e,t){e===`notifications/progress`?this.handleProgress(t):e===`notifications/cancelled`&&this.handleCancelled(t);for(let n of this.notificationListeners.get(e)??[])try{n(t)}catch(e){this.emitError(e)}}handleProgress(e){if(!y(e)||!x(e.progressToken)||typeof e.progress!=`number`)return;let t=this.progressRequests.get(e.progressToken),n=t===void 0?void 0:this.pending.get(t);if(!(t===void 0||!n)){this.armTimeout(t,n);try{n.onProgress?.(e)}catch(e){this.emitError(e)}}}handleCancelled(e){y(e)&&x(e.requestId)&&this.incoming.get(e.requestId)?.abort(e.reason)}armTimeout(e,t){t.timer&&clearTimeout(t.timer),!(!Number.isFinite(t.timeoutMs)||t.timeoutMs<=0)&&(t.timer=setTimeout(()=>{this.cancelPending(e,new ae(t.timeoutMs),t.cancellable,`Request timed out`)},t.timeoutMs))}cancelPending(e,t,n,r){let i=this.pending.get(e);i&&(this.removePending(e,i),i.reject(t),n&&this.transport&&this.transport.send({jsonrpc:`2.0`,method:`notifications/cancelled`,params:{requestId:e,...r?{reason:r}:{}}}).catch(e=>this.emitError(e)))}removePending(e,t){this.pending.delete(e),t.timer&&clearTimeout(t.timer),t.progressToken!==void 0&&this.progressRequests.delete(t.progressToken),t.signal?.removeEventListener(`abort`,t.onAbort)}rejectPending(e){for(let[t,n]of this.pending)this.removePending(t,n),n.reject(e)}handleTransportClose(){this.markClosed(new v)}markClosed(e){let t=this.state===`closed`;this.state=`closed`,this.rejectPending(e);for(let t of this.incoming.values())t.abort(e);if(this.incoming.clear(),!t)for(let e of[...this.closeListeners])try{e()}catch(e){this.emitError(e)}}emitError(e){let t=b(e);for(let e of this.errorListeners)e(t)}disposeTransportListeners(){for(let e of this.disposers.splice(0))e()}};function xe(e){switch(e.type){case`text`:return{type:`text`,text:e.text};case`image`:return{type:`image`,data:e.data,mimeType:e.mimeType};case`audio`:return{type:`text`,text:`[audio ${e.mimeType} omitted]`};case`resource_link`:return{type:`text`,text:`${e.name}: ${e.uri}`};case`resource`:{let t=e.resource;return`text`in t?{type:`text`,text:t.text}:t.mimeType?.startsWith(`image/`)?{type:`image`,data:t.blob,mimeType:t.mimeType}:{type:`text`,text:`[binary resource ${t.uri} (${t.mimeType??`unknown type`}) omitted]`}}default:return{type:`text`,text:`[unsupported MCP content ${e.type}]`}}}function Se(e){let t=(e.content??[]).map(xe);return t.length===0&&e.structuredContent!==void 0&&t.push({type:`text`,text:JSON.stringify(e.structuredContent,null,2)}),t}var Ce=n(((e,n)=>{n.exports=o,o.sync=s;var r=t(`fs`);function i(e,t){var n=t.pathExt===void 0?process.env.PATHEXT:t.pathExt;if(!n||(n=n.split(`;`),n.indexOf(``)!==-1))return!0;for(var r=0;r<n.length;r++){var i=n[r].toLowerCase();if(i&&e.substr(-i.length).toLowerCase()===i)return!0}return!1}function a(e,t,n){return!e.isSymbolicLink()&&!e.isFile()?!1:i(t,n)}function o(e,t,n){r.stat(e,function(r,i){n(r,!r&&a(i,e,t))})}function s(e,t){return a(r.statSync(e),e,t)}})),we=n(((e,n)=>{n.exports=i,i.sync=a;var r=t(`fs`);function i(e,t,n){r.stat(e,function(e,r){n(e,!e&&o(r,t))})}function a(e,t){return o(r.statSync(e),t)}function o(e,t){return e.isFile()&&s(e,t)}function s(e,t){var n=e.mode,r=e.uid,i=e.gid,a=t.uid===void 0?process.getuid&&process.getuid():t.uid,o=t.gid===void 0?process.getgid&&process.getgid():t.gid,s=64,c=8,l=1,u=s|c;return n&l||n&c&&i===o||n&s&&r===a||n&u&&a===0}})),Te=n(((e,n)=>{t(`fs`);var r=process.platform===`win32`||global.TESTING_WINDOWS?Ce():we();n.exports=i,i.sync=a;function i(e,t,n){if(typeof t==`function`&&(n=t,t={}),!n){if(typeof Promise!=`function`)throw TypeError(`callback not provided`);return new Promise(function(n,r){i(e,t||{},function(e,t){e?r(e):n(t)})})}r(e,t||{},function(e,r){e&&(e.code===`EACCES`||t&&t.ignoreErrors)&&(e=null,r=!1),n(e,r)})}function a(e,t){try{return r.sync(e,t||{})}catch(e){if(t&&t.ignoreErrors||e.code===`EACCES`)return!1;throw e}}})),Ee=n(((e,n)=>{let r=process.platform===`win32`||process.env.OSTYPE===`cygwin`||process.env.OSTYPE===`msys`,i=t(`path`),a=r?`;`:`:`,o=Te(),s=e=>Object.assign(Error(`not found: ${e}`),{code:`ENOENT`}),c=(e,t)=>{let n=t.colon||a,i=e.match(/\//)||r&&e.match(/\\/)?[``]:[...r?[process.cwd()]:[],...(t.path||process.env.PATH||``).split(n)],o=r?t.pathExt||process.env.PATHEXT||`.EXE;.CMD;.BAT;.COM`:``,s=r?o.split(n):[``];return r&&e.indexOf(`.`)!==-1&&s[0]!==``&&s.unshift(``),{pathEnv:i,pathExt:s,pathExtExe:o}},l=(e,t,n)=>{typeof t==`function`&&(n=t,t={}),t||={};let{pathEnv:r,pathExt:a,pathExtExe:l}=c(e,t),u=[],d=n=>new Promise((a,o)=>{if(n===r.length)return t.all&&u.length?a(u):o(s(e));let c=r[n],l=/^".*"$/.test(c)?c.slice(1,-1):c,d=i.join(l,e),p=!l&&/^\.[\\\/]/.test(e)?e.slice(0,2)+d:d;a(f(p,n,0))}),f=(e,n,r)=>new Promise((i,s)=>{if(r===a.length)return i(d(n+1));let c=a[r];o(e+c,{pathExt:l},(a,o)=>{if(!a&&o)if(t.all)u.push(e+c);else return i(e+c);return i(f(e,n,r+1))})});return n?d(0).then(e=>n(null,e),n):d(0)};n.exports=l,l.sync=(e,t)=>{t||={};let{pathEnv:n,pathExt:r,pathExtExe:a}=c(e,t),l=[];for(let s=0;s<n.length;s++){let c=n[s],u=/^".*"$/.test(c)?c.slice(1,-1):c,d=i.join(u,e),f=!u&&/^\.[\\\/]/.test(e)?e.slice(0,2)+d:d;for(let e=0;e<r.length;e++){let n=f+r[e];try{if(o.sync(n,{pathExt:a}))if(t.all)l.push(n);else return n}catch{}}}if(t.all&&l.length)return l;if(t.nothrow)return null;throw s(e)}})),De=n(((e,t)=>{let n=(e={})=>{let t=e.env||process.env;return(e.platform||process.platform)===`win32`?Object.keys(t).reverse().find(e=>e.toUpperCase()===`PATH`)||`Path`:`PATH`};t.exports=n,t.exports.default=n})),Oe=n(((e,n)=>{let r=t(`path`),i=Ee(),a=De();function o(e,t){let n=e.options.env||process.env,o=process.cwd(),s=e.options.cwd!=null,c=s&&process.chdir!==void 0&&!process.chdir.disabled;if(c)try{process.chdir(e.options.cwd)}catch{}let l;try{l=i.sync(e.command,{path:n[a({env:n})],pathExt:t?r.delimiter:void 0})}catch{}finally{c&&process.chdir(o)}return l&&=r.resolve(s?e.options.cwd:``,l),l}function s(e){return o(e)||o(e,!0)}n.exports=s})),ke=n(((e,t)=>{let n=/([()\][%!^"`<>&|;, *?])/g;function r(e){return e=e.replace(n,`^$1`),e}function i(e,t){return e=`${e}`,e=e.replace(/(?=(\\+?)?)\1"/g,`$1$1\\"`),e=e.replace(/(?=(\\+?)?)\1$/,`$1$1`),e=`"${e}"`,e=e.replace(n,`^$1`),t&&(e=e.replace(n,`^$1`)),e}t.exports.command=r,t.exports.argument=i})),Ae=n(((e,t)=>{t.exports=/^#!(.*)/})),je=n(((e,t)=>{let n=Ae();t.exports=(e=``)=>{let t=e.match(n);if(!t)return null;let[r,i]=t[0].replace(/#! ?/,``).split(` `),a=r.split(`/`).pop();return a===`env`?i:i?`${a} ${i}`:a}})),Me=n(((e,n)=>{let r=t(`fs`),i=je();function a(e){let t=Buffer.alloc(150),n;try{n=r.openSync(e,`r`),r.readSync(n,t,0,150,0),r.closeSync(n)}catch{}return i(t.toString())}n.exports=a})),Ne=n(((e,n)=>{let r=t(`path`),i=Oe(),a=ke(),o=Me(),s=process.platform===`win32`,c=/\.(?:com|exe)$/i,l=/node_modules[\\/].bin[\\/][^\\/]+\.cmd$/i;function u(e){e.file=i(e);let t=e.file&&o(e.file);return t?(e.args.unshift(e.file),e.command=t,i(e)):e.file}function d(e){if(!s)return e;let t=u(e),n=!c.test(t);if(e.options.forceShell||n){let n=l.test(t);e.command=r.normalize(e.command),e.command=a.command(e.command),e.args=e.args.map(e=>a.argument(e,n)),e.args=[`/d`,`/s`,`/c`,`"${[e.command].concat(e.args).join(` `)}"`],e.command=process.env.comspec||`cmd.exe`,e.options.windowsVerbatimArguments=!0}return e}function f(e,t,n){t&&!Array.isArray(t)&&(n=t,t=null),t=t?t.slice(0):[],n=Object.assign({},n);let r={command:e,args:t,options:n,file:void 0,original:{command:e,args:t}};return n.shell?r:d(r)}n.exports=f})),Pe=n(((e,t)=>{let n=process.platform===`win32`;function r(e,t){return Object.assign(Error(`${t} ${e.command} ENOENT`),{code:`ENOENT`,errno:`ENOENT`,syscall:`${t} ${e.command}`,path:e.command,spawnargs:e.args})}function i(e,t){if(!n)return;let r=e.emit;e.emit=function(n,i){if(n===`exit`){let n=a(i,t);if(n)return r.call(e,`error`,n)}return r.apply(e,arguments)}}function a(e,t){return n&&e===1&&!t.file?r(t.original,`spawn`):null}function o(e,t){return n&&e===1&&!t.file?r(t.original,`spawnSync`):null}t.exports={hookChildProcess:i,verifyENOENT:a,verifyENOENTSync:o,notFoundError:r}})),Fe=n(((e,n)=>{let r=t(`child_process`),i=Ne(),a=Pe();function o(e,t,n){let o=i(e,t,n),s=r.spawn(o.command,o.args,o.options);return a.hookChildProcess(s,o),s}function s(e,t,n){let o=i(e,t,n),s=r.spawnSync(o.command,o.args,o.options);return s.error=s.error||a.verifyENOENTSync(s.status,o),s}n.exports=o,n.exports.spawn=o,n.exports.sync=s,n.exports._parse=i,n.exports._enoent=a})),Ie=class{messageListeners=new Set;errorListeners=new Set;closeListeners=new Set;closeEmitted=!1;onMessage(e){return this.messageListeners.add(e),()=>this.messageListeners.delete(e)}onError(e){return this.errorListeners.add(e),()=>this.errorListeners.delete(e)}onClose(e){return this.closeListeners.add(e),()=>this.closeListeners.delete(e)}emitMessage(e){for(let t of this.messageListeners)t(e)}emitError(e){let t=b(e);for(let e of this.errorListeners)e(t)}emitClose(){if(!this.closeEmitted){this.closeEmitted=!0;for(let e of this.closeListeners)e()}}},Le=e(Fe(),1);const D=o.platform!==`win32`,O=new Set;let k=!1;function A(e,t){let n=e.pid;if(o.platform===`win32`&&n!==void 0){if(e.exitCode!==null)return;a(`taskkill`,[`/pid`,String(n),`/T`,`/F`],{stdio:`ignore`,windowsHide:!0}).on(`error`,()=>{});return}if(D&&n!==void 0)try{o.kill(-n,t);return}catch{}try{e.kill(t)}catch{}}function Re(){k||(k=!0,o.once(`exit`,()=>{for(let e of O)try{o.kill(-e,`SIGTERM`)}catch{}}))}var ze=class extends Ie{options;child;stdoutBuffer=Buffer.alloc(0);stderrBuffer=Buffer.alloc(0);started=!1;closed=!1;constructor(e){super(),this.options=Object.freeze({...e,args:e.args?[...e.args]:void 0})}get pid(){return this.child?.pid}get stderr(){return this.stderrBuffer.toString(`utf8`)}async start(){if(this.started)throw Error(`MCP stdio transport already started`);if(this.closed)throw new v;this.started=!0;let e=this.options.inheritEnv===!1?{...this.options.env}:{...o.env,...this.options.env},t=(0,Le.default)(this.options.command,this.options.args??[],{cwd:this.options.cwd,env:e,stdio:[`pipe`,`pipe`,this.options.stderr===`inherit`?`inherit`:`pipe`],windowsHide:!0,detached:D});this.child=t;let n=t.pid;D&&n!==void 0&&(Re(),O.add(n),t.once(`exit`,()=>O.delete(n))),t.stdout?.on(`data`,e=>this.handleStdout(e)),t.stdout?.on(`error`,e=>this.emitError(e)),t.stdin?.on(`error`,e=>{this.closed||this.emitError(e)}),t.stderr?.on(`data`,e=>this.handleStderr(e)),t.stderr?.on(`error`,e=>this.emitError(e)),t.on(`close`,()=>{this.child=void 0,this.stdoutBuffer.toString(`utf8`).trim()&&this.emitError(Error(`MCP stdio server closed with an incomplete JSON-RPC message`)),this.stdoutBuffer=Buffer.alloc(0),this.emitClose()}),await new Promise((e,n)=>{let r=()=>{t.off(`error`,i),e()},i=e=>{t.off(`spawn`,r),n(e)};t.once(`spawn`,r),t.once(`error`,i)}),t.on(`error`,e=>{this.closed||this.emitError(e)})}async send(e){let t=this.child?.stdin;if(!this.started||this.closed||!t?.writable)throw new v;let n=`${JSON.stringify(e)}\n`;await new Promise((e,r)=>{t.write(n,t=>t?r(t):e())})}async close(){if(this.closed)return;this.closed=!0;let e=this.child;if(!e){this.emitClose();return}if(e.exitCode!==null||e.signalCode!==null){e.stdin?.end();return}let t=this.options.closeTimeoutMs??2e3;await new Promise(n=>{let r=[];e.once(`close`,()=>{for(let e of r)clearTimeout(e);A(e,`SIGTERM`),n()});let i=Math.min(500,t);r.push(setTimeout(()=>A(e,`SIGTERM`),i)),r.push(setTimeout(()=>A(e,`SIGKILL`),i+t)),e.stdin?.end()})}handleStdout(e){this.stdoutBuffer=Buffer.concat([this.stdoutBuffer,Buffer.isBuffer(e)?e:Buffer.from(e)]);let t=this.options.maxMessageBytes??16777216;for(;;){let e=this.stdoutBuffer.indexOf(10);if(e<0){this.stdoutBuffer.length>t&&(this.stdoutBuffer=Buffer.alloc(0),this.emitError(Error(`MCP stdio message exceeds ${t} bytes`)));return}let n=this.stdoutBuffer.subarray(0,e);if(this.stdoutBuffer=this.stdoutBuffer.subarray(e+1),n.length>t){this.emitError(Error(`MCP stdio message exceeds ${t} bytes`));continue}let r=n.toString(`utf8`).replace(/\r$/,``);if(r.trim())try{this.emitMessage(w(JSON.parse(r)))}catch(e){this.emitError(e)}}}handleStderr(e){let t=Buffer.isBuffer(e)?e:Buffer.from(e),n=this.options.maxStderrBytes??65536;this.stderrBuffer=Buffer.concat([this.stderrBuffer,t]),this.stderrBuffer.length>n&&(this.stderrBuffer=this.stderrBuffer.subarray(-n)),this.options.onStderr?.(t.toString(`utf8`))}};async function Be(e,t){let n=e.getReader(),r=new TextDecoder,i=``,a,o,s=[],c=0,l=t.maxEventBytes??16777216,u=()=>{if(s.length===0){a=void 0,o=void 0;return}let e=s.join(`
|
|
3
|
+
`);t.onEvent({...a?{event:a}:{},data:e,...o?{id:o}:{}}),a=void 0,o=void 0,s=[],c=0},d=e=>{let n=e.endsWith(`\r`)?e.slice(0,-1):e;if(n===``){u();return}if(n.startsWith(`:`))return;let r=n.indexOf(`:`),i=r<0?n:n.slice(0,r),d=r<0?``:n.slice(r+1);if(d.startsWith(` `)&&(d=d.slice(1)),i===`data`){if(c+=Buffer.byteLength(d)+ +(s.length>0),c>l)throw Error(`MCP SSE event exceeds ${l} bytes`);s.push(d)}else i===`event`?a=d:i===`id`&&!d.includes(`\0`)?(o=d,t.onId?.(d)):i===`retry`&&/^\d+$/.test(d)&&t.onRetry?.(Number(d))};try{for(;;){let{done:e,value:t}=await n.read();if(e)break;i+=r.decode(t,{stream:!0});let a=i.indexOf(`
|
|
4
|
+
`);for(;a>=0;)d(i.slice(0,a)),i=i.slice(a+1),a=i.indexOf(`
|
|
5
|
+
`);if(Buffer.byteLength(i)>l)throw Error(`MCP SSE event exceeds ${l} bytes`)}i+=r.decode(),i&&d(i),u()}finally{n.releaseLock()}}var j=class extends Error{status;body;constructor(e,t,n=``){super(t),this.name=`McpHttpError`,this.status=e,this.body=n}},Ve=class extends j{wwwAuthenticate;constructor(e,t=``){super(401,`MCP server requires authentication`,t),this.name=`McpAuthRequiredError`,this.wwwAuthenticate=e.headers.get(`www-authenticate`)}},He=class extends j{constructor(e=``){super(404,`MCP session expired`,e),this.name=`McpSessionExpiredError`}};function Ue(e){return e.headers.get(`content-type`)?.split(`;`,1)[0]?.trim().toLowerCase()}function We(e){return e.status===401||e.status===403&&/(?:^|[\s,])error="?insufficient_scope"?/i.test(e.headers.get(`www-authenticate`)??``)}function Ge(e){return e===408||e===429||e>=500}function M(e){return e.body?.cancel().catch(()=>{})??Promise.resolve()}function Ke(e,t){let n=t.trim(),r=n.length>500?`${n.slice(0,497)}...`:n;return`MCP HTTP request failed with status ${e}${r?`: ${r}`:``}`}var qe=class extends Ie{url;options;fetch;controller=new AbortController;started=!1;closed=!1;sessionIdValue;protocolVersion;getStreamStarted=!1;constructor(e){super(),this.options=Object.freeze({...e,headers:e.headers?{...e.headers}:void 0}),this.url=new URL(e.url);let t=e.fetch??globalThis.fetch;this.fetch=(e,n)=>t(e,n)}get sessionId(){return this.sessionIdValue}async start(){if(this.started)throw Error(`MCP Streamable HTTP transport already started`);if(this.closed)throw new v;this.started=!0}setProtocolVersion(e){this.protocolVersion=e}async send(e){if(!this.started||this.closed)throw new v;let t=await this.authorizedFetch(`POST`,{headers:{accept:`application/json, text/event-stream`,"content-type":`application/json`},body:JSON.stringify(e)});if(await this.checkResponse(t),this.captureSession(t),!S(e)){await M(t),`method`in e&&e.method===`notifications/initialized`&&this.startGetStream();return}if(t.status===202||t.status===204)throw new j(t.status,`MCP server accepted request ${e.method} without a response`);let n=Ue(t);if(n===`application/json`){let e=await t.json();for(let t of Array.isArray(e)?e:[e])this.emitMessage(w(t));return}if(n===`text/event-stream`&&t.body){this.consumeResponseStream(t.body,e.id);return}throw await M(t),new j(t.status,`Unsupported MCP response content type: ${n??`missing`}`)}async close(){if(!this.closed){if(this.closed=!0,this.controller.abort(),this.started&&this.sessionIdValue){let e=new AbortController,t=setTimeout(()=>e.abort(),1e3);try{let{headers:t}=await this.headers();await this.fetch(this.url,{method:`DELETE`,headers:t,signal:e.signal}).then(M).catch(()=>void 0)}catch{}finally{clearTimeout(t)}}this.emitClose()}}async authorizedFetch(e,t){let n=this.options.authProvider?.onUnauthorized?.bind(this.options.authProvider);for(let r=0;;r++){let{headers:i,token:a}=await this.headers(t.headers),o=await this.fetch(this.url,{method:e,headers:i,body:t.body,signal:this.controller.signal});if(r>0||!n||!We(o))return o;try{await n({response:o,serverUrl:this.url,fetch:this.fetch,token:a})}finally{await M(o)}}}async headers(e={}){let t=new Headers(this.options.headers);for(let[n,r]of Object.entries(e))t.set(n,r);this.sessionIdValue&&t.set(`Mcp-Session-Id`,this.sessionIdValue),this.protocolVersion&&t.set(`MCP-Protocol-Version`,this.protocolVersion);let n=await this.options.authProvider?.token();return n&&t.set(`Authorization`,`Bearer ${n}`),{headers:t,...n?{token:n}:{}}}captureSession(e){let t=e.headers.get(`mcp-session-id`);t&&(this.sessionIdValue=t)}async checkResponse(e){if(e.ok)return;let t=(await e.text().catch(()=>``)).slice(0,8192);throw e.status===401?new Ve(e,t):e.status===404&&this.sessionIdValue?new He(t):new j(e.status,Ke(e.status,t),t)}async consumeSse(e,t,n){await Be(e,{maxEventBytes:this.options.maxMessageBytes??16777216,onId:e=>{t.lastEventId=e},onRetry:e=>{t.retryMs=e},onEvent:e=>{if(t.received=!0,!e.data.trim()||e.event!==void 0&&e.event!==`message`)return;let r;try{r=w(JSON.parse(e.data))}catch(e){this.emitError(e);return}n?.(r),this.emitMessage(r)}})}async consumeResponseStream(e,t){let n={lastEventId:void 0,retryMs:void 0,received:!1},r=!1,i=e=>{C(e)&&e.id===t&&(r=!0)},a=e,o;for(let e=0;;){if(a)try{await this.consumeSse(a,n,i),o=void 0}catch(e){o=e}if(r||this.closed)return;if(o!==void 0&&!this.isRetryable(o)||n.lastEventId===void 0||e>=this.maxRetries())break;if(n.received&&(e=0),n.received=!1,!await this.sleep(this.reconnectDelay(e++,n.retryMs)))return;try{a=await this.openSseStream(n.lastEventId)}catch(e){if(o=e,!this.isRetryable(e))break;a=void 0}}if(this.closed)return;let s=o===void 0?`stream ended without a response`:b(o).message;this.emitMessage({jsonrpc:`2.0`,id:t,error:{code:g.internalError,message:`MCP response stream failed: ${s}`}})}startGetStream(){this.options.openGetStream===!1||this.getStreamStarted||this.closed||(this.getStreamStarted=!0,this.runGetStream())}async runGetStream(){let e={lastEventId:void 0,retryMs:void 0,received:!1};for(let t=0;!this.closed;){try{let n=await this.openSseStream(e.lastEventId);if(!n)return;let r=Date.now();await this.consumeSse(n,e),(e.received||Date.now()-r>this.maxDelay())&&(t=0)}catch(e){if(this.closed)return;if(!this.isRetryable(e)){this.emitError(e);return}}if(e.received=!1,t>=this.maxRetries()){this.emitError(Error(`MCP server-to-client stream dropped and could not be reopened`));return}if(!await this.sleep(this.reconnectDelay(t++,e.retryMs)))return}}async openSseStream(e){let t=await this.authorizedFetch(`GET`,{headers:{accept:`text/event-stream`,...e===void 0?{}:{"last-event-id":e}}});if(t.status===405){await M(t);return}await this.checkResponse(t),this.captureSession(t);let n=Ue(t);if(n!==`text/event-stream`||!t.body)throw await M(t),new j(t.status,`Unsupported MCP GET response content type: ${n??`missing`}`);return t.body}isRetryable(e){if(e instanceof j)return Ge(e.status);if(e instanceof TypeError)return!0;let t=e?.code;return typeof t==`string`&&(t.startsWith(`E`)||t.startsWith(`UND_ERR`))}reconnectDelay(e,t){if(t!==void 0)return t;let n=this.options.reconnect?.initialDelayMs??1e3;return Math.min(n*2**e,this.maxDelay())}maxDelay(){return this.options.reconnect?.maxDelayMs??3e4}maxRetries(){return this.options.reconnect?.maxRetries??5}sleep(e){let t=this.controller.signal;return t.aborted?Promise.resolve(!1):new Promise(n=>{let r=()=>{clearTimeout(i),n(!1)},i=setTimeout(()=>{t.removeEventListener(`abort`,r),n(!0)},e);i.unref?.(),t.addEventListener(`abort`,r,{once:!0})})}};const Je=6e4;function N(e){return typeof e?.timeout==`number`&&e.timeout>0?e.timeout*1e3:Je}function Ye(e,{cwd:t,env:n,authProvider:r,fetch:a}){return typeof e.url==`string`&&e.type!==`stdio`?new qe({url:e.url,...e.headers?{headers:e.headers}:{},...r?{authProvider:r}:{},...a?{fetch:a}:{}}):new ze({command:e.command,args:e.args??[],cwd:e.cwd?i.resolve(t??process.cwd(),e.cwd):t,...n?{env:n,inheritEnv:!1}:{env:e.env??{}}})}async function Xe({name:e,config:t,cwd:n,env:r,clientName:i,clientVersion:a=`1.0.0`,authProvider:o,fetch:s}){let c=Ye(t,{cwd:n,env:r,authProvider:o,fetch:s}),l=new be({name:i,version:a,requestTimeoutMs:N(t)});try{return await l.connect(c),{name:e,config:t,client:l,tools:await l.listTools(),instructions:l.instructions,close:()=>l.close()}}catch(e){await l.close().catch(()=>{});let t=c instanceof ze?c.stderr.trim():``;throw t&&e instanceof Error&&(e.message=`${e.message}\n${t.slice(-2e3)}`),e}}function Ze(e,t){return`mcp__${e}__${t}`.replace(/[^A-Za-z0-9_]/g,`_`)}function Qe(e,t,n){let i=r(`sha256`).update(`${t}\0${n}`).digest(`hex`).slice(0,8);return`${e.slice(0,64-i.length-1)}_${i}`}function $e(e,{reserved:t=new Set}={}){let n=e.map(({server:e,tool:t})=>Ze(e,t)),r=new Map;for(let[t,i]of n.entries()){let{server:n,tool:a}=e[t],o=`${n}\0${a}`;r.has(i)||r.set(i,new Set),r.get(i).add(o)}return n.map((n,i)=>{let{server:a,tool:o}=e[i],s=r.get(n).size>1;return n.length<=64&&!s&&!t.has(n)?n:Qe(n,a,o)})}function et(e){return`mcp_${e}`}const tt=48*1024;function P(e){if(e.length<=65536)return e;let t=e.length-tt-16384;return`${e.slice(0,tt)}\n[... ${t} characters truncated ...]\n${e.slice(-16384)}`}const nt=e=>e.some(e=>e.type===`text`&&e.text.trim()!==``);function rt(e,t,n,{limitText:r=P}={}){let i=Se(n).map(e=>e.type===`text`?{type:`text`,text:r(e.text)}:e);n.isError&&!nt(i)&&i.push({type:`text`,text:`MCP tool ${e}/${t} returned an error`});let{_meta:a,...o}=n;return{content:i,details:{server:e,tool:t},structuredContent:o,...n.isError?{isError:!0}:{}}}function F(e,t,n,{limitText:r=P}={}){return{content:[{type:`text`,text:`MCP tool error: ${r(n instanceof Error?n.message:String(n))}`}],details:{server:e,tool:t},isError:!0}}function it(e){return e.ok?`Authorization complete. You may close this window.`:e.details?`${e.message}\n\n${e.details}`:e.message}var I=class e{redirectUrl;server;path;timeoutMs;renderPage;pending=new Map;constructor(e,t,n,r,i){this.server=e,this.redirectUrl=t,this.path=n,this.timeoutMs=r,this.renderPage=i}static async listen(t={}){let n=t.host??`127.0.0.1`,r=t.redirectHost??n,i=t.path??`/callback`,a,o=s((e,t)=>a?.handle(e.url??`/`,t));await new Promise((e,r)=>{o.once(`error`,r),o.listen(t.port??0,n,()=>{o.off(`error`,r),e()})});let c=o.address();if(!c||typeof c==`string`)throw Error(`OAuth callback server did not bind to TCP`);return a=new e(o,`http://${r.includes(`:`)?`[${r}]`:r}:${c.port}${i}`,i,t.timeoutMs??5*6e4,t.renderPage),a}waitForCallback(e){if(this.pending.has(e))throw Error(`OAuth state is already pending`);return new Promise((t,n)=>{let r=setTimeout(()=>{this.pending.delete(e),n(Error(`OAuth callback timed out`))},this.timeoutMs);this.pending.set(e,{resolve:t,reject:n,timer:r})})}async close(){for(let e of this.pending.values())clearTimeout(e.timer),e.reject(Error(`OAuth callback server closed`));this.pending.clear(),await new Promise((e,t)=>{this.server.close(n=>n?t(n):e())})}reply(e,t,n){this.renderPage?(e.writeHead(t,{"content-type":`text/html; charset=utf-8`,"cache-control":`no-store`}),e.end(this.renderPage(n))):e.writeHead(t,{"content-type":`text/plain; charset=utf-8`}).end(it(n))}handle(e,t){let n=new URL(e,this.redirectUrl);if(n.pathname!==this.path){this.reply(t,404,{ok:!1,message:`Not found`});return}let r=n.searchParams.get(`state`),i=r?this.pending.get(r):void 0;if(!r||!i){this.reply(t,400,{ok:!1,message:`Invalid or expired OAuth state`});return}clearTimeout(i.timer),this.pending.delete(r);let a=n.searchParams.get(`error`);if(a){let e=n.searchParams.get(`error_description`)??a;i.reject(Error(e)),this.reply(t,200,{ok:!1,message:`Authorization failed. You may close this window.`,details:e});return}let o=n.searchParams.get(`code`);if(!o){i.reject(Error(`OAuth callback did not include an authorization code`)),this.reply(t,400,{ok:!1,message:`Missing authorization code`});return}let s=n.searchParams.get(`iss`);i.resolve({code:o,state:r,...s?{iss:s}:{}}),this.reply(t,200,{ok:!0})}},L=class extends Error{code;errorUri;constructor(e,t,n){super(t||e),this.name=`OAuthError`,this.code=e,this.errorUri=n}},at=class extends Error{expected;received;constructor(e,t){super(`OAuth issuer mismatch: expected ${JSON.stringify(e)}, received ${t===void 0?`none`:JSON.stringify(t)}`),this.name=`OAuthIssuerMismatchError`,this.expected=e,this.received=t}},ot=class extends Error{endpoint;constructor(e){super(`Refusing to send OAuth credentials to non-HTTPS endpoint ${e}`),this.name=`OAuthInsecureEndpointError`,this.endpoint=e}},st=class extends Error{status;body;constructor(e,t){super(`OAuth dynamic client registration failed with status ${e}: ${t}`),this.name=`OAuthRegistrationError`,this.status=e,this.body=t}},R=class extends Error{constructor(){super(`MCP OAuth authorization requires user interaction`),this.name=`McpOAuthAuthorizationRequiredError`}};function z(e,t){if(!y(e))throw Error(`Invalid ${t}`);return e}function B(e){return Object.fromEntries(Object.entries(e).filter(([,e])=>e!==void 0))}function V(e,t){if(typeof e!=`string`||e.length===0)throw Error(`Invalid ${t}`);return e}function H(e){return e==null||e===``}function U(e,t){if(!H(e))return V(e,t)}function W(e,t){if(e!=null){if(!Array.isArray(e)||e.some(e=>typeof e!=`string`))throw Error(`Invalid ${t}`);return[...e]}}function G(e,t){let n=V(e,t);if(!URL.canParse(n))throw Error(`Invalid ${t}`);let r=new URL(n);if([`javascript:`,`data:`,`vbscript:`].includes(r.protocol))throw Error(`Invalid ${t}`);return n}function ct(e,t){return H(e)?void 0:G(e,t)}function lt(e){let t=z(e,`OAuth protected resource metadata`);return B({...t,resource:G(t.resource,`OAuth protected resource metadata resource`),authorization_servers:W(t.authorization_servers,`authorization_servers`)?.map(e=>G(e,`authorization server URL`)),scopes_supported:W(t.scopes_supported,`scopes_supported`)})}function ut(e){let t=z(e,`authorization server metadata`),n=W(t.response_types_supported,`response_types_supported`);if(!n)throw Error(`Invalid response_types_supported`);return B({...t,issuer:G(t.issuer,`authorization server issuer`),authorization_endpoint:G(t.authorization_endpoint,`authorization endpoint`),token_endpoint:G(t.token_endpoint,`token endpoint`),registration_endpoint:ct(t.registration_endpoint,`registration endpoint`),scopes_supported:W(t.scopes_supported,`scopes_supported`),response_types_supported:n,grant_types_supported:W(t.grant_types_supported,`grant_types_supported`),token_endpoint_auth_methods_supported:W(t.token_endpoint_auth_methods_supported,`token_endpoint_auth_methods_supported`),code_challenge_methods_supported:W(t.code_challenge_methods_supported,`code_challenge_methods_supported`),client_id_metadata_document_supported:typeof t.client_id_metadata_document_supported==`boolean`?t.client_id_metadata_document_supported:void 0,authorization_response_iss_parameter_supported:typeof t.authorization_response_iss_parameter_supported==`boolean`?t.authorization_response_iss_parameter_supported:void 0})}function dt(e){let t=z(e,`OAuth token response`),n=H(t.expires_in)?void 0:Number(t.expires_in);if(n!==void 0&&!Number.isFinite(n))throw Error(`Invalid expires_in`);return B({access_token:V(t.access_token,`access_token`),token_type:V(t.token_type,`token_type`),expires_in:n,scope:U(t.scope,`scope`),refresh_token:U(t.refresh_token,`refresh_token`),id_token:U(t.id_token,`id_token`)})}function ft(e){let t=z(e,`OAuth client registration response`);return B({...t,client_id:V(t.client_id,`client_id`),client_secret:U(t.client_secret,`client_secret`),client_id_issued_at:typeof t.client_id_issued_at==`number`?t.client_id_issued_at:void 0,client_secret_expires_at:typeof t.client_secret_expires_at==`number`?t.client_secret_expires_at:void 0,redirect_uris:W(t.redirect_uris,`redirect_uris`)??[]})}function K(e){e?.body?.cancel().catch(()=>{})}function pt(e){return e>=400&&e<500||e===502}function mt(e){return e.endsWith(`/`)?e.slice(0,-1):e}function q(e,t){let n=e.match(new RegExp(String.raw`(?:^|[,\s])${t}=(?:"([^"]*)"|([^\s,]+))`,`i`));return n?.[1]||n?.[2]||void 0}function ht(e){if(!e)return{};let t=e.trimStart().split(/\s+/,1)[0]?.toLowerCase();if(t!==`bearer`&&t!==`dpop`)return{};let n=q(e,`resource_metadata`),r;if(n)try{r=new URL(n)}catch{}return{resourceMetadataUrl:r,scope:q(e,`scope`),error:q(e,`error`),errorDescription:q(e,`error_description`)}}async function J(e,t,n){return t(e,{headers:{Accept:`application/json`,"MCP-Protocol-Version":n}})}async function gt(e,t={}){let n=new URL(e),r=t.fetch??globalThis.fetch,i=t.protocolVersion??`2025-11-25`,a=await J(t.resourceMetadataUrl?new URL(t.resourceMetadataUrl):new URL(`/.well-known/oauth-protected-resource${mt(n.pathname)}`,n.origin),r,i);if(!t.resourceMetadataUrl&&n.pathname!==`/`&&pt(a.status)&&(K(a),a=await J(new URL(`/.well-known/oauth-protected-resource`,n.origin),r,i)),!a.ok)throw K(a),Error(`HTTP ${a.status} loading OAuth protected resource metadata`);return lt(await a.json())}function _t(e){let t=new URL(e),n=mt(t.pathname),r=[{url:new URL(`/.well-known/oauth-authorization-server${n}`,t.origin),type:`oauth`},{url:new URL(`/.well-known/openid-configuration${n}`,t.origin),type:`oidc`}];return n&&r.push({url:new URL(`${n}/.well-known/openid-configuration`,t.origin),type:`oidc`}),r}async function vt(e,t={}){let n=t.fetch??globalThis.fetch;for(let{url:r}of _t(e)){let i=await J(r,n,t.protocolVersion??`2025-11-25`);if(!i.ok){if(K(i),pt(i.status))continue;throw Error(`HTTP ${i.status} loading authorization server metadata from ${r}`)}let a=ut(await i.json());if(!t.skipIssuerValidation){let t=String(e),n=e=>e.endsWith(`/`)?e.slice(0,-1):e;if(n(a.issuer)!==n(t))throw new at(t,a.issuer)}return a}}async function yt(e,t={}){let n;try{n=await gt(e,{resourceMetadataUrl:t.resourceMetadataUrl,fetch:t.fetch})}catch(e){if(e instanceof TypeError)throw e}if(t.authorizationServerMetadataUrl){let e=t.authorizationServerMetadataUrl,r=await J(e,t.fetch??globalThis.fetch,ce);if(!r.ok)throw K(r),Error(`HTTP ${r.status} loading authorization server metadata from ${e}`);let i=ut(await r.json());return{authorizationServerUrl:i.issuer,authorizationServerMetadata:i,resourceMetadata:n}}let r=n?.authorization_servers?.[0]??String(new URL(`/`,e));return{authorizationServerUrl:r,authorizationServerMetadata:await vt(r,{fetch:t.fetch,skipIssuerValidation:t.skipIssuerValidation}),resourceMetadata:n}}function bt(e){let t=new URL(e);return t.hash=``,t}function xt(e,t){if(!t)return;let n=bt(e),r=new URL(t.resource);if(n.origin!==r.origin)throw Error(`Protected resource ${t.resource} does not match MCP server ${n}`);let i=n.pathname.endsWith(`/`)?n.pathname:`${n.pathname}/`,a=r.pathname.endsWith(`/`)?r.pathname:`${r.pathname}/`;if(!i.startsWith(a))throw Error(`Protected resource ${t.resource} does not match MCP server ${n}`);return t.resource}function St(e){return e===`localhost`||e===`127.0.0.1`||e===`[::1]`||e===`::1`}function Ct(e){let t=new URL(e);if(t.protocol!==`https:`&&!St(t.hostname))throw new ot(t.href);return t}function wt(e,t){let n=`token_endpoint_auth_method`in e?e.token_endpoint_auth_method:void 0;return n&&[`client_secret_basic`,`client_secret_post`,`none`].includes(n)&&(t.length===0||t.includes(n))?n:t.length===0?e.client_secret?`client_secret_basic`:`none`:e.client_secret&&t.includes(`client_secret_basic`)?`client_secret_basic`:e.client_secret&&t.includes(`client_secret_post`)?`client_secret_post`:t.includes(`none`)?`none`:e.client_secret?`client_secret_post`:`none`}function Tt(e,t,n,r){if(e===`client_secret_basic`){if(!t.client_secret)throw Error(`client_secret_basic requires a client secret`);n.set(`Authorization`,`Basic ${Buffer.from(`${t.client_id}:${t.client_secret}`).toString(`base64`)}`)}else r.set(`client_id`,t.client_id),e===`client_secret_post`&&t.client_secret&&r.set(`client_secret`,t.client_secret)}async function Et(){let e=crypto.getRandomValues(new Uint8Array(32)),t=Buffer.from(e).toString(`base64url`),n=await crypto.subtle.digest(`SHA-256`,new TextEncoder().encode(t));return{verifier:t,challenge:Buffer.from(n).toString(`base64url`)}}async function Dt(e,t){let n=t.metadata;if(n&&!n.response_types_supported.includes(`code`))throw Error(`Authorization server does not support authorization codes`);if(n?.code_challenge_methods_supported&&!n.code_challenge_methods_supported.includes(`S256`))throw Error(`Authorization server does not support PKCE S256`);let r=new URL(n?.authorization_endpoint??new URL(`/authorize`,e)),{verifier:i,challenge:a}=await Et();return r.searchParams.set(`response_type`,`code`),r.searchParams.set(`client_id`,t.clientInformation.client_id),r.searchParams.set(`code_challenge`,a),r.searchParams.set(`code_challenge_method`,`S256`),r.searchParams.set(`redirect_uri`,String(t.redirectUrl)),t.state&&r.searchParams.set(`state`,t.state),t.scope&&r.searchParams.set(`scope`,t.scope),t.scope?.split(/\s+/).includes(`offline_access`)&&r.searchParams.set(`prompt`,`consent`),t.resource&&r.searchParams.set(`resource`,t.resource),{authorizationUrl:r,codeVerifier:i}}async function Ot(e,t,n){let r=Ct(t.metadata?.token_endpoint??new URL(`/token`,e)),i=new Headers({Accept:`application/json`,"content-type":`application/x-www-form-urlencoded`});t.resource&&n.set(`resource`,t.resource),t.addClientAuthentication?await t.addClientAuthentication(i,n,r,t.metadata):Tt(wt(t.clientInformation,t.metadata?.token_endpoint_auth_methods_supported??[]),t.clientInformation,i,n);let a=await(t.fetch??globalThis.fetch)(r,{method:`POST`,headers:i,body:n}),o=await a.text(),s;try{s=JSON.parse(o)}catch{}if(y(s)&&typeof s.error==`string`)throw new L(s.error,typeof s.error_description==`string`?s.error_description:s.error,typeof s.error_uri==`string`?s.error_uri:void 0);if(!a.ok)throw new L(`server_error`,`HTTP ${a.status}: ${o}`);return dt(s)}async function kt(e,t){let n=t.metadata?.registration_endpoint;if(t.metadata&&!n)throw Error(`Authorization server does not support dynamic client registration`);let r=await(t.fetch??globalThis.fetch)(new URL(n??new URL(`/register`,e)),{method:`POST`,headers:{Accept:`application/json`,"content-type":`application/json`},body:JSON.stringify({...t.clientMetadata,...t.scope?{scope:t.scope}:{}})});if(!r.ok)throw new st(r.status,await r.text());return ft(await r.json())}async function At(e,t){return Ot(e,t,new URLSearchParams({grant_type:`authorization_code`,code:t.code,code_verifier:t.codeVerifier,redirect_uri:String(t.redirectUrl)}))}async function jt(e,t){let n=await Ot(e,t,new URLSearchParams({grant_type:`refresh_token`,refresh_token:t.refreshToken}));return{refresh_token:t.refreshToken,...n}}function Mt(e,t){return e.scope===void 0&&t?{...e,scope:t}:e}function Nt(e,t){if(!t)return;let n=[e,t].flatMap(e=>e?.split(/\s+/).filter(Boolean)??[]);return[...new Set(n)].join(` `)}async function Y(e,t){let n=t.authorizationServerMetadataUrl&&Ct(t.authorizationServerMetadataUrl),r=n?void 0:await e.discoveryState?.(),i=r?.authorizationServerUrl?{authorizationServerUrl:r.authorizationServerUrl,authorizationServerMetadata:r.authorizationServerMetadata??await vt(r.authorizationServerUrl,{fetch:t.fetch,skipIssuerValidation:t.skipIssuerValidation}),resourceMetadata:r.resourceMetadata}:await yt(t.serverUrl,{resourceMetadataUrl:t.resourceMetadataUrl,authorizationServerMetadataUrl:n,fetch:t.fetch,skipIssuerValidation:t.skipIssuerValidation});n||await e.saveDiscoveryState?.({...i,...t.resourceMetadataUrl?{resourceMetadataUrl:t.resourceMetadataUrl.href}:{}});let a=i.authorizationServerMetadata,o=xt(t.serverUrl,i.resourceMetadata),s=t.scope||i.resourceMetadata?.scopes_supported?.join(` `)||e.clientMetadata.scope,c=await e.clientInformation();if(!c){if(t.authorizationCode)throw Error(`OAuth client information is missing during code exchange`);if(a?.client_id_metadata_document_supported&&e.clientMetadataUrl){let t=new URL(e.clientMetadataUrl);if(t.protocol!==`https:`||t.pathname===`/`)throw Error(`Invalid OAuth client metadata URL`);c={client_id:e.clientMetadataUrl},await e.saveClientInformation?.(c)}else{if(!e.saveClientInformation)throw Error(`OAuth client information cannot be persisted`);c=await kt(i.authorizationServerUrl,{metadata:a,clientMetadata:e.clientMetadata,scope:s,fetch:t.fetch}),await e.saveClientInformation(c)}}let l={metadata:a,clientInformation:c,resource:o,addClientAuthentication:e.addClientAuthentication,fetch:t.fetch};if(t.authorizationCode){let n=t.iss;if(a&&(n!==void 0||a.authorization_response_iss_parameter_supported)&&n!==a.issuer)throw new at(a.issuer,n);let r=await At(i.authorizationServerUrl,{...l,code:t.authorizationCode,codeVerifier:await e.codeVerifier(),redirectUrl:e.redirectUrl});return await e.saveTokens(Mt(r,s)),`AUTHORIZED`}let u=t.skipRefresh?void 0:await e.tokens();if(u?.refresh_token)try{let t=await jt(i.authorizationServerUrl,{...l,refreshToken:u.refresh_token});return await e.saveTokens(Mt(t,u.scope)),`AUTHORIZED`}catch(e){if(e instanceof ot||e instanceof L&&e.code!==`server_error`)throw e}let d=await e.state?.(),f=await Dt(i.authorizationServerUrl,{metadata:a,clientInformation:c,redirectUrl:e.redirectUrl,scope:s,state:d,resource:o});return await e.saveCodeVerifier(f.codeVerifier),await e.redirectToAuthorization(f.authorizationUrl),`REDIRECT`}async function X(e,t){try{return await Y(e,t)}catch(n){if(n instanceof L&&[`invalid_client`,`unauthorized_client`].includes(n.code))return await e.invalidateCredentials?.(`all`),Y(e,t);if(n instanceof L&&n.code===`invalid_grant`)return await e.invalidateCredentials?.(`tokens`),Y(e,t);throw n}}var Pt=class{value;load(){return this.value===void 0?void 0:structuredClone(this.value)}save(e){this.value=structuredClone(e)}},Ft=class{redirectUrl;clientMetadata;serverUrl;configuredClient;store;onRedirect;writes=Promise.resolve();constructor(e){this.serverUrl=String(new URL(e.serverUrl)),this.redirectUrl=String(e.redirectUrl),this.clientMetadata={...e.clientMetadata,redirect_uris:e.clientMetadata.redirect_uris??[this.redirectUrl],grant_types:e.clientMetadata.grant_types??[`authorization_code`,`refresh_token`],response_types:e.clientMetadata.response_types??[`code`],token_endpoint_auth_method:e.clientMetadata.token_endpoint_auth_method??(e.clientSecret?`client_secret_post`:`none`)},this.configuredClient=e.clientId?{client_id:e.clientId,...e.clientSecret?{client_secret:e.clientSecret}:{}}:void 0,this.store=e.store??new Pt,this.onRedirect=e.onRedirect}async state(){let e=(await this.load()).oauthState;if(e)return e;let t=Buffer.from(crypto.getRandomValues(new Uint8Array(32))).toString(`hex`);return await this.update(e=>({...e,oauthState:t})),t}async clientInformation(){return this.configuredClient??(await this.load()).clientInformation}async saveClientInformation(e){this.configuredClient||await this.update(t=>({...t,clientInformation:e}))}async tokens(){return(await this.load()).tokens}async saveTokens(e){let t=e.expires_in===void 0?void 0:Date.now()+e.expires_in*1e3;await this.update(n=>{let r={...n,tokens:e};return t===void 0?delete r.tokensExpireAt:r.tokensExpireAt=t,r})}async redirectToAuthorization(e){await this.onRedirect(e)}async saveCodeVerifier(e){await this.update(t=>({...t,codeVerifier:e}))}async codeVerifier(){let e=(await this.load()).codeVerifier;if(!e)throw Error(`No OAuth PKCE code verifier is stored`);return e}async invalidateCredentials(e){await this.update(t=>{let n={...t};return(e===`all`||e===`client`)&&delete n.clientInformation,(e===`all`||e===`tokens`)&&(delete n.tokens,delete n.tokensExpireAt),(e===`all`||e===`verifier`)&&delete n.codeVerifier,(e===`all`||e===`discovery`)&&delete n.discovery,e===`all`&&delete n.oauthState,n})}async saveDiscoveryState(e){await this.update(t=>({...t,discovery:e}))}async discoveryState(){return(await this.load()).discovery}async load(){return await this.writes,this.own(await this.store.load())}async update(e){this.writes=this.writes.then(async()=>{await this.store.save(e(this.own(await this.store.load())))}),await this.writes}own(e){return e?.serverUrl===this.serverUrl?e:{serverUrl:this.serverUrl}}};const Z=`127.0.0.1`,It=`/callback`,Lt=`http://${Z}${It}`;function Rt(e){return typeof e?.url!=`string`||e.type===`stdio`?!1:!Object.keys(e.headers??{}).some(e=>e.toLowerCase()===`authorization`)}function zt(e,t){return`${e}|${String(new URL(t))}`}function Bt(e){let t=new URL(e.callbackUrl??`http://${Z}${It}`),n=t.hostname.replace(/^\[|\]$/g,``),r=t.port?Number(t.port):e.callbackPort,i;return t.port?i=e.callbackUrl:r!==void 0&&(t.port=String(r),i=t.href),{host:n===`localhost`?Z:n,redirectHost:n,port:r,path:t.pathname,fixedRedirectUrl:i}}function Vt(...e){let t=[...new Set(e.flatMap(e=>e?.split(/\s+/).filter(Boolean)??[]))];return t.length>0?t.join(` `):void 0}const Q=e=>e&&`redirect_uris`in e?e.redirect_uris:[];function Ht(e,t,n,r,i,a){return new Ft({serverUrl:e,redirectUrl:i,clientMetadata:{client_name:n.clientName??r},clientId:n.clientId,clientSecret:n.clientSecret,store:t,onRedirect:a})}const $=new Map;function Ut({serverUrl:e,storeKey:t,store:n,settings:r={},clientName:i,onChallenge:a}){let o=(a,o=globalThis.fetch,s)=>{let c=$.get(t);return c||(c=(async()=>{let t=await n.load();if(t?.tokens?.access_token===a&&(!t?.tokens?.refresh_token||await X(Ht(e,n,r,i,Bt(r).fixedRedirectUrl??Q(t.clientInformation)[0]??Lt,()=>{}),{serverUrl:e,resourceMetadataUrl:s?.resourceMetadataUrl,authorizationServerMetadataUrl:r.authServerMetadataUrl,scope:s?.scope,fetch:(e,t)=>o(e,{...t,signal:AbortSignal.timeout(15e3)})})===`REDIRECT`))throw new R})().finally(()=>$.delete(t)),$.set(t,c)),c};return{token:async()=>{await $.get(t)?.catch(()=>void 0);let e=await n.load(),r=e?.tokens?.access_token;return!(e?.tokensExpireAt!==void 0&&e.tokensExpireAt-3e4<=Date.now())||!e?.tokens?.refresh_token?r:(await o(r).catch(()=>void 0),(await n.load())?.tokens?.access_token)},onUnauthorized:async e=>{let t=ht(e.response.headers.get(`www-authenticate`));if(a?.(t),t.error===`insufficient_scope`)throw new R;await o(e.token,e.fetch,t)}}}async function Wt(e,t,n,r){let i={host:e.host,redirectHost:e.redirectHost,path:e.path,...r?{renderPage:r}:{}};try{return await I.listen({...i,port:t??0})}catch(e){if(n||t===void 0)throw e;return I.listen(i)}}function Gt(e){return new Promise((t,n)=>{let r=()=>n(e.reason??Error(`Sign-in cancelled`));e.aborted?r():e.addEventListener(`abort`,r,{once:!0})})}async function Kt({serverUrl:e,store:t,settings:n={},clientName:r,challenge:i,openUrl:a,signal:o,renderPage:s}){let c=await t.load(),l=i?.error===`insufficient_scope`,u=Bt(n),d=Q(c?.clientInformation)[0],f=await Wt(u,u.port??(d&&Number(new URL(d).port)||void 0),u.port!==void 0,s),p=u.fixedRedirectUrl??f.redirectUrl;try{if(c){let e={...c};delete e.oauthState,!n.clientId&&!Q(c.clientInformation).includes(p)&&(delete e.clientInformation,delete e.tokens,delete e.tokensExpireAt),await t.save(e)}let s,u=Ht(e,t,n,r,p,e=>{s=e}),d={serverUrl:e,resourceMetadataUrl:i?.resourceMetadataUrl,authorizationServerMetadataUrl:n.authServerMetadataUrl,scope:Vt(n.scope,l?Nt(c?.tokens?.scope,i?.scope):i?.scope)};if(await X(u,{...d,skipRefresh:l})===`AUTHORIZED`)return;if(!s)throw Error(`OAuth flow did not produce an authorization URL`);let m=f.waitForCallback(await u.state());m.catch(()=>void 0),await a(s);let{code:h,iss:ee}=await(o?Promise.race([m,Gt(o)]):m);await X(u,{...d,authorizationCode:h,iss:ee})}finally{await f.close()}}const qt=[`readOnlyHint`,`destructiveHint`,`idempotentHint`,`openWorldHint`];function Jt(e){let t=e&&typeof e==`object`?e:{};return{...t,type:t.type??`object`,...t.properties===void 0?{properties:{}}:{}}}function Yt(e){return{type:`object`,properties:{content:{type:`array`,items:{type:`object`}},...e?{structuredContent:e}:{},isError:{type:`boolean`},_meta:{type:`object`}},required:[`content`]}}function Xt(e){let t={};for(let n of qt){let r=e.annotations?.[n];typeof r==`boolean`&&(t[n]=r)}return Object.keys(t).length>0?t:void 0}function Zt({server:e,client:t,tool:n,name:r,exposure:i,config:a,instructions:o,limitText:s}){let c=n.title??n.annotations?.title,l=Xt(n),u=N(a);return{name:r,label:`${e}/${n.name}`,description:n.description?.trim()||c||`MCP tool ${n.name} from server ${e}`,parameters:Jt(n.inputSchema),outputSchema:Yt(n.outputSchema),execute:async(r,i,a,o)=>{try{let r=await t.callTool(n.name,i??{},{signal:a,timeoutMs:u,onProgress:t=>{let r=t.total===void 0?``:`/${t.total}`;o?.({content:[{type:`text`,text:t.message??`Progress ${t.progress}${r}`}],details:{server:e,tool:n.name}})}});return rt(e,n.name,r,{limitText:s})}catch(t){if(a?.aborted)throw t;return F(e,n.name,t,{limitText:s})}},_mcpServer:e,_mcpToolName:n.name,_mcpExposure:i,_mcpNamespace:h(e),...typeof a.description==`string`&&a.description.trim()?{_mcpServerDescription:a.description.trim()}:{},...o?{_mcpServerInstructions:o}:{},...l?{_mcpAnnotations:l}:{},_client:t}}function Qt(e,{defaultExposure:t=`deferred`,reservedNames:n,limitText:r=P}={}){let i=e.flatMap(e=>e.tools.map(n=>({connection:e,tool:n,exposure:te(e.config,n.name,t)}))),a=$e(i.map(({connection:e,tool:t})=>({server:e.name,tool:t.name})),{reserved:n});return i.flatMap(({connection:e,tool:t,exposure:n},i)=>n===`hidden`?[]:[Zt({server:e.name,client:e.client,tool:t,name:a[i],exposure:n,config:e.config,instructions:e.instructions?.trim(),limitText:r})])}export{Je as DEFAULT_MCP_TIMEOUT_MS,c as MCP_EXPOSURES,R as McpOAuthAuthorizationRequiredError,$e as assignMcpToolNames,Xe as connectMcpServer,rt as convertMcpResult,Qt as createMcpAgentTools,Ut as createMcpAuthProvider,te as getMcpToolExposure,et as legacyMcpToolName,P as limitMcpTextDefault,F as mcpCallErrorResult,h as mcpNamespace,zt as mcpOAuthStoreKey,N as mcpRequestTimeoutMs,Ze as mcpToolBaseName,Kt as signInMcpServer,Rt as usesMcpOAuth,ie as validateMcpServerConfig};
|
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const e=256*1024,t=1024*1024,n=`image expects a non-empty image URL string, an object with image_url, or a raw MCP image block`,r=`(function (bridge, toolsJson, globalsJson, storeJson) {
|
|
3
|
+
"use strict";
|
|
4
|
+
const stringify = JSON.stringify;
|
|
5
|
+
const parse = JSON.parse;
|
|
6
|
+
const promiseThen = Promise.prototype.then;
|
|
7
|
+
const ErrorCtor = Error;
|
|
8
|
+
const TypeErrorCtor = TypeError;
|
|
9
|
+
const pending = new Map();
|
|
10
|
+
let nextId = 1;
|
|
11
|
+
let finished = false;
|
|
12
|
+
// Thrown by exit() to unwind the script after it already reported success.
|
|
13
|
+
const EXIT = Object.freeze({});
|
|
14
|
+
|
|
15
|
+
function done(ok, payload, writes) {
|
|
16
|
+
if (finished) return;
|
|
17
|
+
finished = true;
|
|
18
|
+
bridge("done", ok, payload, writes);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function serialize(value) {
|
|
22
|
+
return value === undefined ? undefined : stringify(value);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// QuickJS stacks list frames only. Prefix "Name: message" like V8 so the
|
|
26
|
+
// text reads the same as a Node error, and drop this prelude's frames.
|
|
27
|
+
function errorText(error) {
|
|
28
|
+
const head = error.message ? error.name + ": " + error.message : String(error.name);
|
|
29
|
+
const frames =
|
|
30
|
+
typeof error.stack === "string"
|
|
31
|
+
? error.stack.split("\\n").filter((line) => line.trim() && !line.includes("codemode-prelude.js"))
|
|
32
|
+
: [];
|
|
33
|
+
return [head, ...frames].join("\\n");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function format(value) {
|
|
37
|
+
if (typeof value === "string") return value;
|
|
38
|
+
if (value instanceof ErrorCtor) return errorText(value);
|
|
39
|
+
try {
|
|
40
|
+
const json = stringify(value);
|
|
41
|
+
return json === undefined ? String(value) : json;
|
|
42
|
+
} catch {
|
|
43
|
+
return String(value);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function describeError(error) {
|
|
48
|
+
if (error instanceof ErrorCtor) {
|
|
49
|
+
return stringify({ name: error.name, message: error.message, stack: errorText(error) });
|
|
50
|
+
}
|
|
51
|
+
return stringify({ message: format(error) });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function caller(kind, name, spread) {
|
|
55
|
+
return (...args) =>
|
|
56
|
+
new Promise((resolve, reject) => {
|
|
57
|
+
let json;
|
|
58
|
+
try {
|
|
59
|
+
json = serialize(spread ? args : args[0]);
|
|
60
|
+
} catch (error) {
|
|
61
|
+
reject(error);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const id = nextId++;
|
|
65
|
+
pending.set(id, { resolve, reject });
|
|
66
|
+
bridge(kind, id, name, json);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const tools = Object.create(null);
|
|
71
|
+
const allTools = [];
|
|
72
|
+
for (const { name, jsName, description } of parse(toolsJson)) {
|
|
73
|
+
const fn = caller("call", name);
|
|
74
|
+
// The first tool wins when two names normalize to the same identifier.
|
|
75
|
+
if (!(jsName in tools)) {
|
|
76
|
+
tools[jsName] = fn;
|
|
77
|
+
allTools.push(Object.freeze({ name: jsName, description }));
|
|
78
|
+
}
|
|
79
|
+
if (!(name in tools)) tools[name] = fn;
|
|
80
|
+
}
|
|
81
|
+
Object.freeze(tools);
|
|
82
|
+
Object.freeze(allTools);
|
|
83
|
+
|
|
84
|
+
// Reading a member that does not exist throws an error that names the close matches, instead of
|
|
85
|
+
// a later "not a function". \`in\` checks still work.
|
|
86
|
+
const comparable = (name) => name.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
87
|
+
function guard(target, label, names, hint) {
|
|
88
|
+
return new Proxy(target, {
|
|
89
|
+
get(object, property, receiver) {
|
|
90
|
+
if (typeof property !== "string" || property in object || property in Object.prototype || property === "then" || property === "toJSON") {
|
|
91
|
+
return Reflect.get(object, property, receiver);
|
|
92
|
+
}
|
|
93
|
+
const wanted = comparable(property);
|
|
94
|
+
const exact = names.filter((name) => comparable(name) === wanted);
|
|
95
|
+
const close = exact.length > 0 ? exact : names.filter((name) => wanted && (comparable(name).includes(wanted) || wanted.includes(comparable(name))));
|
|
96
|
+
let message = label + "." + property + " does not exist.";
|
|
97
|
+
if (close.length > 0) message += " Did you mean " + close.slice(0, 5).map((name) => label + "." + name).join(", ") + "?";
|
|
98
|
+
else if (names.length <= 20) message += " Available: " + names.join(", ") + ".";
|
|
99
|
+
if (hint) message += " " + hint;
|
|
100
|
+
message += ' Check for a member with "' + property + '" in ' + label + ".";
|
|
101
|
+
throw new TypeErrorCtor(message);
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
const toolsProxy = guard(
|
|
106
|
+
tools,
|
|
107
|
+
"tools",
|
|
108
|
+
allTools.map((tool) => tool.name),
|
|
109
|
+
"ALL_TOOLS lists every tool; searchTools(query) finds tools by topic.",
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
const namespaces = new Map();
|
|
113
|
+
for (const { name, spread } of parse(globalsJson)) {
|
|
114
|
+
const fn = caller("global", name, spread);
|
|
115
|
+
const dot = name.indexOf(".");
|
|
116
|
+
if (dot === -1) {
|
|
117
|
+
Object.defineProperty(globalThis, name, { value: fn, enumerable: true });
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
const namespace = name.slice(0, dot);
|
|
121
|
+
if (!namespaces.has(namespace)) namespaces.set(namespace, Object.create(null));
|
|
122
|
+
namespaces.get(namespace)[name.slice(dot + 1)] = fn;
|
|
123
|
+
}
|
|
124
|
+
for (const [namespace, members] of namespaces) {
|
|
125
|
+
Object.freeze(members);
|
|
126
|
+
const value = guard(members, namespace, Object.keys(members));
|
|
127
|
+
Object.defineProperty(globalThis, namespace, { value, enumerable: true });
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// key -> JSON text. Sizes count key and JSON characters.
|
|
131
|
+
const stored = new Map(Object.entries(parse(storeJson)));
|
|
132
|
+
const writes = new Map();
|
|
133
|
+
let storedChars = 0;
|
|
134
|
+
for (const [key, json] of stored) storedChars += key.length + json.length;
|
|
135
|
+
|
|
136
|
+
const STORE_HINT =
|
|
137
|
+
"store() is for small state such as IDs or summaries. Show images with image(), keep large data in variables, or write it to a file with a tool.";
|
|
138
|
+
|
|
139
|
+
function checkKey(name, key) {
|
|
140
|
+
if (typeof key !== "string") throw new TypeError(name + "() key must be a string");
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function store(key, value) {
|
|
144
|
+
checkKey("store", key);
|
|
145
|
+
const previous = stored.has(key) ? key.length + stored.get(key).length : 0;
|
|
146
|
+
if (value === undefined) {
|
|
147
|
+
stored.delete(key);
|
|
148
|
+
storedChars -= previous;
|
|
149
|
+
writes.set(key, undefined);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
let json;
|
|
153
|
+
try {
|
|
154
|
+
json = stringify(value);
|
|
155
|
+
} catch (error) {
|
|
156
|
+
throw new TypeError("store(" + stringify(key) + ") value is not JSON-serializable: " + format(error));
|
|
157
|
+
}
|
|
158
|
+
if (json === undefined) {
|
|
159
|
+
throw new TypeError("store(" + stringify(key) + ") value is not JSON-serializable");
|
|
160
|
+
}
|
|
161
|
+
if (json.length > ${e}) {
|
|
162
|
+
throw new RangeError(
|
|
163
|
+
"store(" + stringify(key) + ") value has " + json.length + " characters of JSON, more than the limit of ${e}. " +
|
|
164
|
+
STORE_HINT,
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
const next = storedChars - previous + key.length + json.length;
|
|
168
|
+
if (next > ${t}) {
|
|
169
|
+
throw new RangeError(
|
|
170
|
+
"store is full: stored values would exceed ${t} characters of JSON. Delete keys with store(key, undefined). " +
|
|
171
|
+
STORE_HINT,
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
stored.set(key, json);
|
|
175
|
+
storedChars = next;
|
|
176
|
+
writes.set(key, json);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function load(key) {
|
|
180
|
+
checkKey("load", key);
|
|
181
|
+
const json = stored.get(key);
|
|
182
|
+
return json === undefined ? undefined : parse(json);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function serializeWrites() {
|
|
186
|
+
const entries = [];
|
|
187
|
+
for (const [key, json] of writes) entries.push(json === undefined ? [key] : [key, json]);
|
|
188
|
+
return stringify(entries);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
Object.defineProperty(globalThis, "store", { value: store, enumerable: true });
|
|
192
|
+
Object.defineProperty(globalThis, "load", { value: load, enumerable: true });
|
|
193
|
+
|
|
194
|
+
// Primitives become their string form, everything else JSON.
|
|
195
|
+
function outputText(value) {
|
|
196
|
+
if (value === undefined || value === null || typeof value !== "object" && typeof value !== "function") {
|
|
197
|
+
return String(value);
|
|
198
|
+
}
|
|
199
|
+
const json = stringify(value);
|
|
200
|
+
return json === undefined ? String(value) : json;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function text(value) {
|
|
204
|
+
let rendered;
|
|
205
|
+
try {
|
|
206
|
+
rendered = outputText(value);
|
|
207
|
+
} catch (error) {
|
|
208
|
+
throw new TypeErrorCtor(error instanceof ErrorCtor ? error.message : String(error));
|
|
209
|
+
}
|
|
210
|
+
if (!finished) bridge("output", "text", rendered);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function imageUrl(value) {
|
|
214
|
+
if (typeof value === "string") return value;
|
|
215
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
216
|
+
throw new TypeErrorCtor(${JSON.stringify(n)});
|
|
217
|
+
}
|
|
218
|
+
if (value.image_url !== undefined) {
|
|
219
|
+
if (typeof value.image_url !== "string") throw new TypeErrorCtor(${JSON.stringify(n)});
|
|
220
|
+
return value.image_url;
|
|
221
|
+
}
|
|
222
|
+
if (typeof value.type !== "string") throw new TypeErrorCtor(${JSON.stringify(n)});
|
|
223
|
+
if (value.type !== "image") {
|
|
224
|
+
throw new TypeErrorCtor('image only accepts MCP image blocks, got "' + value.type + '"');
|
|
225
|
+
}
|
|
226
|
+
if (typeof value.data !== "string" || value.data === "") throw new TypeErrorCtor("image expected MCP image data");
|
|
227
|
+
if (value.data.toLowerCase().startsWith("data:")) return value.data;
|
|
228
|
+
return "data:;base64," + value.data;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Base64 of the signatures of the formats providers accept inline (PNG, JPEG except
|
|
232
|
+
// JPEG-LS, GIF, "RIFF....WEBP"). Signatures start at byte 0, so their encodings are prefixes.
|
|
233
|
+
const IMAGE_SIGNATURES = [
|
|
234
|
+
["image/png", /^iVBORw0KGg/],
|
|
235
|
+
["image/jpeg", /^[/]9j[/](?!9)/],
|
|
236
|
+
["image/gif", /^R0lGOD[dl]h/],
|
|
237
|
+
["image/webp", /^UklG.{8}RUJQ/],
|
|
238
|
+
];
|
|
239
|
+
|
|
240
|
+
function image(value) {
|
|
241
|
+
const url = imageUrl(value);
|
|
242
|
+
if (url === "") throw new TypeErrorCtor(${JSON.stringify(n)});
|
|
243
|
+
const colon = url.indexOf(":");
|
|
244
|
+
const scheme = colon === -1 ? "" : url.slice(0, colon).toLowerCase();
|
|
245
|
+
if (scheme === "http" || scheme === "https") {
|
|
246
|
+
throw new TypeErrorCtor("remote image URLs are not supported in tool outputs. Pass a base64 data URI instead");
|
|
247
|
+
}
|
|
248
|
+
const comma = url.indexOf(",");
|
|
249
|
+
const header = comma === -1 ? [] : url.slice(colon + 1, comma).split(";");
|
|
250
|
+
if (scheme !== "data" || comma === -1 || header.slice(1).every((part) => part.toLowerCase() !== "base64")) {
|
|
251
|
+
throw new TypeErrorCtor("invalid image output. Pass a base64 data URI instead");
|
|
252
|
+
}
|
|
253
|
+
// Providers reject the whole request on a bad image, and a persisted image block would be
|
|
254
|
+
// resent on every later turn. Line breaks from wrapped base64 are dropped. The declared type
|
|
255
|
+
// is ignored in favor of the detected one, as providers also reject mismatches.
|
|
256
|
+
const data = url.slice(comma + 1).replace(/\\s+/g, "");
|
|
257
|
+
if (data.length % 4 !== 0 || !/^[A-Za-z0-9+/]+={0,2}$/.test(data)) {
|
|
258
|
+
throw new TypeErrorCtor("invalid image output. The image data is not valid base64 (truncated or corrupted?)");
|
|
259
|
+
}
|
|
260
|
+
const head = data.slice(0, 16);
|
|
261
|
+
const signature = IMAGE_SIGNATURES.find(([, pattern]) => pattern.test(head));
|
|
262
|
+
if (!signature) {
|
|
263
|
+
throw new TypeErrorCtor("invalid image output. The image data is not a PNG, JPEG, GIF, or WebP image");
|
|
264
|
+
}
|
|
265
|
+
if (!finished) bridge("output", "image", data, signature[0]);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function exit() {
|
|
269
|
+
let writesJson;
|
|
270
|
+
try {
|
|
271
|
+
writesJson = serializeWrites();
|
|
272
|
+
} catch (error) {
|
|
273
|
+
done(false, describeError(error));
|
|
274
|
+
throw EXIT;
|
|
275
|
+
}
|
|
276
|
+
done(true, undefined, writesJson);
|
|
277
|
+
throw EXIT;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const console = {};
|
|
281
|
+
for (const level of ["log", "info", "warn", "error", "debug"]) {
|
|
282
|
+
console[level] = (...args) => {
|
|
283
|
+
if (!finished) bridge("output", "text", args.map(format).join(" "));
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
Object.freeze(console);
|
|
287
|
+
|
|
288
|
+
Object.defineProperty(globalThis, "tools", { value: toolsProxy, enumerable: true });
|
|
289
|
+
Object.defineProperty(globalThis, "ALL_TOOLS", { value: allTools, enumerable: true });
|
|
290
|
+
Object.defineProperty(globalThis, "console", { value: console, enumerable: true });
|
|
291
|
+
Object.defineProperty(globalThis, "text", { value: text, enumerable: true });
|
|
292
|
+
Object.defineProperty(globalThis, "image", { value: image, enumerable: true });
|
|
293
|
+
Object.defineProperty(globalThis, "exit", { value: exit, enumerable: true });
|
|
294
|
+
|
|
295
|
+
return {
|
|
296
|
+
settle(id, ok, payload) {
|
|
297
|
+
const entry = pending.get(id);
|
|
298
|
+
if (!entry) return;
|
|
299
|
+
pending.delete(id);
|
|
300
|
+
if (!ok) {
|
|
301
|
+
entry.reject(new ErrorCtor(payload));
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
let value;
|
|
305
|
+
try {
|
|
306
|
+
value = payload === undefined ? undefined : parse(payload);
|
|
307
|
+
} catch (error) {
|
|
308
|
+
entry.reject(error);
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
entry.resolve(value);
|
|
312
|
+
},
|
|
313
|
+
run(fn) {
|
|
314
|
+
let promise;
|
|
315
|
+
try {
|
|
316
|
+
promise = fn(toolsProxy, console);
|
|
317
|
+
} catch (error) {
|
|
318
|
+
done(false, describeError(error));
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
promiseThen.call(
|
|
322
|
+
promise,
|
|
323
|
+
(value) => {
|
|
324
|
+
let json;
|
|
325
|
+
try {
|
|
326
|
+
json = serialize(value);
|
|
327
|
+
} catch (error) {
|
|
328
|
+
done(false, describeError(error));
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
done(true, json, serializeWrites());
|
|
332
|
+
},
|
|
333
|
+
(error) => {
|
|
334
|
+
done(false, describeError(error));
|
|
335
|
+
},
|
|
336
|
+
);
|
|
337
|
+
},
|
|
338
|
+
stalled() {
|
|
339
|
+
if (finished || pending.size > 0) return false;
|
|
340
|
+
done(
|
|
341
|
+
false,
|
|
342
|
+
stringify({
|
|
343
|
+
name: "Error",
|
|
344
|
+
message:
|
|
345
|
+
"The script is waiting on a promise that can never settle: no tool call is pending, and timers do not exist here.",
|
|
346
|
+
}),
|
|
347
|
+
);
|
|
348
|
+
return true;
|
|
349
|
+
},
|
|
350
|
+
};
|
|
351
|
+
})`;function i(e){if(typeof e!=`object`||!e)return!1;let t=e.type;return t===`call`||t===`output`||t===`done`||t===`crash`}function a(e){return typeof e==`object`&&!!e&&e.type===`result`}export{i as n,r,a as t};
|
package/lib/quickjs.wasm
ADDED
|
Binary file
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{a as e,t}from"./client-
|
|
2
|
+
import{a as e,t}from"./client-DEUSDK5d.mjs";import{Container as n,Editor as r,Loader as i,Markdown as a,ProcessTerminal as o,Text as s,TuiMainScreen as c}from"@earendil-works/pi-tui";const l=e=>t=>`\x1b[${e}m${t}\x1b[0m`,u=l(`2`),d=l(`1`),f=l(`36`),p=l(`33`),m=l(`31`),h=l(`32`),g=l(`35`),_={heading:e=>d(f(e)),link:f,linkUrl:e=>u(f(e)),code:p,codeBlock:p,codeBlockBorder:u,quote:u,quoteBorder:u,hr:u,listBullet:f,bold:d,italic:l(`3`),strikethrough:l(`9`),underline:l(`4`)},v={borderColor:u,selectList:{selectedPrefix:f,selectedText:d,description:u,scrollInfo:u,noMatch:u}},y=(e,t)=>e.length>t?`${e.slice(0,t)}…`:e;var b=class{tui;chat;thinking=null;thinkingBuffer=``;answer=null;answerBuffer=``;constructor(e,t){this.tui=e,this.chat=t}touch(){this.tui.requestRender()}appendThinking(e){this.thinking||(this.thinking=new s(``,0,0),this.chat.addChild(this.thinking)),this.thinkingBuffer+=e,this.thinking.setText(u(this.thinkingBuffer.trimEnd())),this.touch()}appendAnswer(e){this.answer||(this.answer=new a(``,0,0,_),this.chat.addChild(this.answer)),this.answerBuffer+=e,this.answer.setText(this.answerBuffer),this.touch()}addLine(e){this.answer=null,this.answerBuffer=``,this.chat.addChild(new s(e,0,0)),this.touch()}};const x=async({connection:a,agentName:l,sessionId:_,fork:x})=>{let S=new o,C=new c(S);S.setTitle(`buttress agent · ${l}`);let w=new n,T=new s(``,0,0),E=new r(C,v);C.addChild(w),C.addChild(T),C.addChild(E),C.setFocus(E);let D=_,O=x,k=null,A=null,j=e=>{T.setText(e),C.requestRender()},M=()=>{A||(A=new i(C,f,u,`thinking…`),w.addChild(A),C.requestRender())},N=()=>{A&&(w.removeChild(A),A=null,C.requestRender())},P=`${d(l)} ${u(`@ ${a.baseUrl}`)}`+(D?` ${u(`(${O?`forking`:`continuing`} ${D})`)}`:``);w.addChild(new s(P,0,0)),j(u(`Enter to send · /new fresh session · /exit or Ctrl+C to quit`));let F=async()=>{C.stop(),await S.drainInput().catch(()=>{}),process.exit(0)};C.addInputListener(e=>{if(e===``)return k?(k.abort(),k=null,{consume:!0}):(F(),{consume:!0})});let I=async n=>{w.addChild(new s(`${f(`>`)} ${n}`,0,0));let r=new b(C,w),i=D;k=new AbortController,M(),j(u(`running · Ctrl+C aborts this turn`));let o;try{o=await e(a,l,{prompt:n,sessionId:D??void 0,fork:O},{signal:k.signal,onFrame:({event:e,payload:t})=>{if(e===`session`&&t?.sessionId){t.sessionId!==D&&(D=t.sessionId,w.addChild(new s(u(`session ${D}`),0,0)));return}if(e!==`agent`)return;N();let n=t?.event;if(t?.type===`message_update`&&n)n.type===`thinking_delta`?r.appendThinking(n.delta??``):n.type===`text_delta`&&r.appendAnswer(n.delta??``);else if(t?.type===`tool_execution_start`){let e=JSON.stringify(t.args??{});r.addLine(`${p(`⚙ ${t.toolName}`)}${u(`(${y(e,120)})`)}`)}else t?.type===`tool_execution_end`?r.addLine(t.isError?m(`✗ ${t.toolName} failed`):u(`✓ ${t.toolName}`)):t?.type===`tool_emit`&&r.addLine(u(` ${t.toolName} → ${t.event}`))}})}catch(e){o={kind:`error`,message:e?.message||String(e),sessionId:D}}if(N(),k=null,o.kind===`result`){let{result:e}=o;D=e.sessionId??D;let t=e.usage||{};w.addChild(new s(u(`— ${e.stopReason} · ${t.totalTurns??`?`} turn(s) · ${t.input??0} in / ${t.output??0} out · session ${D}`),0,0))}else o.kind===`aborted`?(D=o.sessionId??D,w.addChild(new s(p(`(aborted — session is continuable)`),0,0))):o.kind===`error`?(D=o.sessionId??D,w.addChild(new s(m(`Run failed: ${o.message}`),0,0))):w.addChild(new s(m(`Connection lost mid-run.`),0,0));t(i,D)&&(O=!1),j(u(`Enter to send · /new fresh session · /exit or Ctrl+C to quit`)),C.requestRender()};E.onSubmit=e=>{let t=e.trim();if(E.setText(``),t){if(k){j(p(`A turn is still running — Ctrl+C aborts it.`));return}if(t===`/exit`||t===`/quit`){F();return}if(t===`/new`){D=null,O=!1,w.addChild(new s(`${h(`•`)} ${u(`started a fresh session`)}`,0,0)),C.requestRender();return}if(t===`/session`){w.addChild(new s(u(D?`session ${D}`:`no session yet`),0,0)),C.requestRender();return}I(t)}},w.addChild(new s(u(g(`ready`)),0,0)),C.start(),await new Promise(()=>{})};export{x as runAgentTui};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fugood/buttress-server",
|
|
3
|
-
"version": "2.26.0-beta.
|
|
3
|
+
"version": "2.26.0-beta.17",
|
|
4
4
|
"main": "lib/index.mjs",
|
|
5
5
|
"types": "lib/index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
],
|
|
20
20
|
"scripts": {
|
|
21
21
|
"typecheck": "tsc --noEmit",
|
|
22
|
-
"build": "tsdown -c rolldown.config.js --config-loader native && tsc --noCheck --emitDeclarationOnly && cp ../buttress-backend-core/src/backends/mlx-bridge.py lib/mlx-bridge.py",
|
|
22
|
+
"build": "tsdown -c rolldown.config.js --config-loader native && tsc --noCheck --emitDeclarationOnly && cp ../buttress-backend-core/src/backends/mlx-bridge.py lib/mlx-bridge.py && cp ../../node_modules/quickjs-wasi/quickjs.wasm lib/quickjs.wasm",
|
|
23
23
|
"build:dist": "$npm_execpath scripts/build-distribution.js",
|
|
24
24
|
"release": "$npm_execpath scripts/build-distribution.js",
|
|
25
25
|
"release-beta": "$npm_execpath scripts/build-distribution.js --beta",
|
|
@@ -37,9 +37,9 @@
|
|
|
37
37
|
],
|
|
38
38
|
"license": "MIT",
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@earendil-works/pi-agent-core": "^0.
|
|
41
|
-
"@earendil-works/pi-ai": "0.
|
|
42
|
-
"@earendil-works/pi-tui": "^0.
|
|
40
|
+
"@earendil-works/pi-agent-core": "^1.0.0",
|
|
41
|
+
"@earendil-works/pi-ai": "1.0.0",
|
|
42
|
+
"@earendil-works/pi-tui": "^1.0.0",
|
|
43
43
|
"@elysiajs/cors": "^1.1.1",
|
|
44
44
|
"@elysiajs/node": "^1.4.2",
|
|
45
45
|
"@fugood/llama.node": "^1.8.0-rc.8",
|
|
@@ -69,10 +69,11 @@
|
|
|
69
69
|
"zod": "^3.25.76"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
|
-
"@fugood/
|
|
73
|
-
"@fugood/bricks-
|
|
72
|
+
"@fugood/agent-tool-runtime": "^2.26.0-beta.17",
|
|
73
|
+
"@fugood/bricks-cli": "^2.26.0-beta.17",
|
|
74
|
+
"@fugood/bricks-devtools-core": "^2.26.0-beta.17",
|
|
74
75
|
"tsdown": "^0.22.4",
|
|
75
76
|
"typescript": "^7.0.2"
|
|
76
77
|
},
|
|
77
|
-
"gitHead": "
|
|
78
|
+
"gitHead": "40afa429fd4da51508318825f097533c38e9c541"
|
|
78
79
|
}
|
package/lib/config-DbRjQnNp.mjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import e from"node:path";import t from"node:os";import n from"bytes";import r from"ms";import i from"node-machine-id";const a=(e={},t={})=>{let n=Array.isArray(e)?[...e]:{...e};return Object.entries(t||{}).forEach(([e,t])=>{t&&typeof t==`object`&&!Array.isArray(t)?n[e]=a(n[e]||{},t):n[e]=t}),n},o=e=>e&&typeof e==`object`?structuredClone(e):null,s=(e,t)=>{let n=o(e)||{},r=o(t)||{};return a(n,r)},c=(e,t)=>a(structuredClone(e.global),t||{}),l=(e,t,n,r)=>{if(e.generators.length>0){let i=e.generators.filter(e=>e?.type===n);if(i.length>0&&r){let a=i.find(e=>t.getModelIdentifier(n,e)===r);if(a)return c(e,a)}}return Object.keys(e.global).length>0?c(e,{}):null},u={udp:{port:8089,announcements:{enabled:!0,interval:5e3},requests:{enabled:!0,responseDelay:100}},http:{enabled:!0,path:`/buttress/info`,cors:!0}},d=e=>e?e===!0?{...u}:a(u,e):null,f=(e,t)=>{if(!e.generators||e.generators.length===0)return t.map(e=>({type:e}));let n=new Set;return e.generators.forEach(e=>{e.type&&n.add(e.type)}),n.size===0?t.map(e=>({type:e})):Array.from(n).map(e=>({type:e}))},p=(e,t,n)=>e===void 0?n:typeof e==`number`?e:t(e)??n,m=6e4,h=1024*1024*50,g=s=>{let c=i.machineIdSync(),l={server:{id:`buttress-${c}`,name:`Buttress Server (${c.slice(-8)})`,port:2080,temp_file_dir:e.join(t.tmpdir(),`.buttress`),session_timeout:m,max_body_size:h},autodiscover:!1},u=a(l,o(s)||{}),f=Array.isArray(u.generators)?u.generators:[],{server:g,generators:_,autodiscover:v,...y}=u;return{autodiscover:d(v),server:{id:g.id,name:g.name,port:g.port,log_level:g.log_level,temp_file_dir:g.temp_file_dir,max_body_size:p(g.max_body_size,n.parse,h),session_timeout:p(g.session_timeout,e=>r(e),m)},global:y,generators:f}},_=/^[A-Za-z0-9_-]{1,64}$/,v=n=>n===`~`||n.startsWith(`~/`)?e.join(t.homedir(),n.slice(1)):n;var y=class extends Error{constructor(e){super(`[Agents] ${e}`),this.name=`AgentsConfigError`}};const b=(e,t,n)=>{if(t==null)return n;if(t===0||t===!1)return null;if(typeof t==`number`){if(Number.isFinite(t)&&t>0)return t}else if(typeof t==`string`){let e;try{e=r(t)}catch{e=void 0}if(typeof e==`number`&&e>0)return e}throw new y(`agents_options.${e} must be a duration string (e.g. "30d") or a positive number of milliseconds; use 0 to disable it`)},x=(e,t)=>{if(t==null)return{};if(typeof t!=`object`||Array.isArray(t))throw new y(`agent "${e}": mcp_servers must be a table`);let n={};for(let[r,i]of Object.entries(t)){if(!_.test(r))throw new y(`agent "${e}": MCP server name "${r}" must match ${_}`);let t=!!(typeof i?.url==`string`&&i.url),a=!!(typeof i?.command==`string`&&i.command);if(t===a)throw new y(`agent "${e}": MCP server "${r}" needs exactly one of url / command`);n[r]={url:t?i.url:void 0,headers:i?.headers&&typeof i.headers==`object`?i.headers:void 0,command:a?i.command:void 0,args:Array.isArray(i?.args)?i.args.map(String):void 0,env:i?.env&&typeof i.env==`object`?i.env:void 0,optional:i?.optional===!0}}return n},S=(e,t,n)=>{let r=e?.name;if(typeof r!=`string`||!_.test(r))throw new y(`[[agents]] #${t+1}: name is required and must match ${_}`);let i=e?.model;if(typeof i!=`string`||!i.includes(`/`))throw new y(`agent "${r}": model must be "provider/model-id" (e.g. "buttress/ggml-org/gpt-oss-20b-GGUF")`);let a=i.indexOf(`/`),o=i.slice(0,a),s=i.slice(a+1);if(!o||!s)throw new y(`agent "${r}": invalid model reference "${i}"`);if(o===`buttress`&&!n.generators.filter(e=>e.type===`ggml-llm`||e.type===`mlx-llm`).some(e=>(e.model?.repo_id||e.model?.repository)===s))throw new y(`agent "${r}": model "${s}" does not match any configured [[generators]] ggml-llm/mlx-llm repo_id`);if(e?.system_prompt!=null&&e?.system_prompt_file!=null)throw new y(`agent "${r}": set system_prompt or system_prompt_file, not both`);let c=[];if(e?.tools!=null){if(!Array.isArray(e.tools))throw new y(`agent "${r}": tools must be an array of function names`);for(let t of e.tools){if(typeof t!=`string`||!_.test(t))throw new y(`agent "${r}": tool name "${t}" must match ${_} (local function names only)`);if(c.includes(t))throw new y(`agent "${r}": duplicate tool "${t}"`);c.push(t)}}if(e?.local_devices!==void 0&&typeof e.local_devices!=`boolean`)throw new y(`agent "${r}": local_devices must be a boolean`);let l=Number(e?.max_turns??30);if(!Number.isInteger(l)||l<1)throw new y(`agent "${r}": max_turns must be a positive integer`);let u=null;if(e?.max_tokens_per_run!=null&&(u=Number(e.max_tokens_per_run),!Number.isInteger(u)||u<1))throw new y(`agent "${r}": max_tokens_per_run must be a positive integer`);let{name:d,model:f,system_prompt:p,system_prompt_file:m,tools:h,local_devices:g=!1,mcp_servers:v,max_turns:b,max_tokens_per_run:S,thinking:C,...w}=e;return{name:r,provider:o,modelId:s,modelRef:i,systemPrompt:typeof p==`string`?p:null,systemPromptFile:typeof m==`string`?m:null,tools:c,localDevices:g,mcpServers:x(r,v),maxTurns:l,maxTokensPerRun:u,thinking:typeof C==`string`?C:void 0,generation:w}},C=(t,{configDir:n=process.cwd()}={})=>{let r=t.global||{},i=r.agents;if(i==null)return null;if(!Array.isArray(i))throw new y(`agents must be an array of tables ([[agents]])`);if(i.length===0)return null;let a=i.map((e,n)=>S(e,n,t)),o=new Set;for(let e of a){if(o.has(e.name))throw new y(`duplicate agent name "${e.name}"`);o.add(e.name)}let s=r.agents_options||{},c=typeof s.sessions_dir==`string`&&s.sessions_dir?s.sessions_dir:`./.buttress-agent/sessions`,l=v(c),u=e.isAbsolute(l)?l:e.resolve(n,l);for(let t of a)if(t.systemPromptFile){let r=v(t.systemPromptFile);t.systemPromptFile=e.isAbsolute(r)?r:e.resolve(n,r)}let d=Number(s.max_depth??2);if(!Number.isInteger(d)||d<1)throw new y(`agents_options.max_depth must be a positive integer`);let f=s.session_max_count,p=500;if(f===0||f===!1)p=null;else if(f!=null&&(p=Number(f),!Number.isInteger(p)||p<1))throw new y(`agents_options.session_max_count must be a positive integer`);return{agents:a,configDir:n,sessionsDir:u,runtimeTokenFile:e.join(e.dirname(u),`runtime-token`),sessionMaxAgeMs:b(`session_max_age`,s.session_max_age,2592e6),sessionMaxCount:p,maxDepth:d,allowUnauthenticated:s.allow_unauthenticated===!0}};export{g as a,o as i,C as n,l as o,s as r,f as s,y as t};
|