@agimon-ai/model-proxy-mcp 0.2.6 → 0.2.7

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/dist/cli.cjs CHANGED
@@ -1,3 +1,4 @@
1
1
  #!/usr/bin/env node
2
2
  const e=require(`./stdio-B-atmzW4.cjs`);let t=require(`node:fs`),n=require(`node:path`);n=e.u(n);let r=require(`node:url`),i=require(`commander`),a=require(`node:child_process`),o=require(`node:crypto`),s=require(`node:fs/promises`);s=e.u(s);let c=require(`node:os`);c=e.u(c);let l=require(`node:util`),u=require(`@hono/node-server`);var d=class{async check(e){try{let t=await fetch(`http://127.0.0.1:${e}/health`);return t.ok?{healthy:!0,serviceName:(await t.json()).service}:{healthy:!1,error:`Health check failed with status ${t.status}`}}catch(e){return{healthy:!1,error:e instanceof Error?e.message:String(e)}}}};const f=[`pnpm-workspace.yaml`,`nx.json`,`.git`],p=25,m=`sh`,h=`lsof -n -P -sTCP:LISTEN`,g=5e3,_=250,ee=1e3,te=`SIGTERM`,ne=`SIGKILL`,re=`model-proxy-mcp-http`,v=`utf8`,y=(0,l.promisify)(a.execFile);function b(e=process.cwd()){let r=n.default.resolve(e);for(;;){for(let e of f)if((0,t.existsSync)(n.default.join(r,e)))return r;let e=n.default.dirname(r);if(e===r)return process.cwd();r=e}}var x=class{repositoryPath=b(process.cwd());serviceName=e.l;stateFilePath=n.default.join(c.default.tmpdir(),`model-proxy-mcp-http-${Buffer.from(b(process.cwd())).toString(`hex`)}.json`);constructor(e=new d){this.healthCheck=e}createEmptyStatus(e={}){return{running:!1,scope:`default`,activeProfileId:null,auth:{configured:!1,authFilePath:``},profiles:[],slotModels:{},...e}}async getRegistration(){try{let e=await s.default.readFile(this.stateFilePath,v),t=JSON.parse(e);return t.port?t:null}catch(e){if(e.code===`ENOENT`)return null;throw e}}async releaseService(e){let t=await this.getRegistration();e&&t?.pid&&t.pid!==e||await s.default.rm(this.stateFilePath,{force:!0})}async listListeningProcesses(e){try{let{stdout:t}=await y(`sh`,[`-lc`,`lsof -n -P -sTCP:LISTEN -iTCP:${e} || true`],{encoding:`utf8`});return t.split(`
3
- `).slice(1).map(e=>e.trim()).filter(Boolean).map(e=>e.split(/\s+/)).map(t=>({pid:Number.parseInt(t[1]||``,10),port:e})).filter(e=>Number.isInteger(e.pid)&&e.pid>0)}catch{return[]}}async findHealthyServicePort(e){for(let t=0;t<=25;t+=1){let n=e+t,r=await this.healthCheck.check(n);if(r.healthy&&r.serviceName===this.serviceName)return n}return null}async clearPortListeners(e,t){let n=await this.listListeningProcesses(e);for(let e of n)t&&e.pid===t||await this.killProcess(e.pid)}async registerService(e,t){await s.default.writeFile(this.stateFilePath,JSON.stringify({port:e,pid:t}),v)}async findAvailablePort(t){let n=Math.min(e.c,t),r=Math.max(e.c+1e3,t);for(let e=t;e<=r;e+=1)if((await this.listListeningProcesses(e)).length===0)return e;for(let e=n;e<t;e+=1)if((await this.listListeningProcesses(e)).length===0)return e;throw Error(`No available port found from ${t}`)}async fileExists(e){try{return await s.default.access(e),!0}catch{return!1}}async resolveCliPath(){let e=n.default.dirname((0,r.fileURLToPath)(require(`url`).pathToFileURL(__filename).href)),t=n.default.resolve(e,`cli.mjs`);if(await this.fileExists(t))return{cliPath:t,runtime:`node`};let i=n.default.resolve(e,`..`,`cli.mjs`);if(await this.fileExists(i))return{cliPath:i,runtime:`node`};let a=n.default.join(this.repositoryPath,`packages`,`mcp`,`model-proxy-mcp`,`dist`,`cli.mjs`);if(await this.fileExists(a))return{cliPath:a,runtime:`node`};for(let t of[n.default.resolve(e,`cli.ts`),n.default.resolve(e,`..`,`cli.ts`)])if(await this.fileExists(t))return{cliPath:t,runtime:`bun`};let o=n.default.join(this.repositoryPath,`packages`,`mcp`,`model-proxy-mcp`,`src`,`cli.ts`);if(await this.fileExists(o))return{cliPath:o,runtime:`bun`};throw Error(`Cannot find model-proxy-mcp CLI`)}async startHttpServer(e){let{cliPath:t,runtime:n}=await this.resolveCliPath(),r=(0,a.spawn)(n===`bun`?process.versions.bun?process.execPath:`bun`:`node`,n===`bun`?[`run`,t,`http-serve`,`--port`,String(e)]:[t,`http-serve`,`--port`,String(e)],{detached:!0,stdio:`ignore`,env:{...process.env,NODE_ENV:process.env.NODE_ENV||`development`}});if(r.unref(),!r.pid)throw Error(`Failed to spawn HTTP server`);return r.pid}async killProcess(e){try{process.kill(e,`SIGTERM`),await new Promise(e=>setTimeout(e,1e3));try{process.kill(e,0),process.kill(e,`SIGKILL`)}catch{return}}catch{return}}async waitForHealthy(e){let t=Date.now()+5e3,n=`Server failed health check after startup`;for(;Date.now()<t;){let t=await this.healthCheck.check(e);if(t.healthy&&t.serviceName===this.serviceName)return{healthy:!0};n=t.error||`Unexpected service on port ${e}`,await new Promise(e=>setTimeout(e,250))}return{healthy:!1,error:n}}async ensureRunning(t=e.c){try{let e=await this.findHealthyServicePort(t);if(e!==null){let t=(await this.listListeningProcesses(e))[0]?.pid;return await this.registerService(e,t??process.pid),this.createEmptyStatus({running:!0,port:e,pid:t})}let n=await this.getRegistration();if(n){let e=await this.healthCheck.check(n.port);if(e.healthy&&e.serviceName===this.serviceName)return this.createEmptyStatus({running:!0,port:n.port,pid:n.pid});n.pid&&await this.killProcess(n.pid),await this.clearPortListeners(n.port,n.pid),await this.releaseService(n.pid)}await this.clearPortListeners(t);let r=await this.findAvailablePort(t),i=await this.startHttpServer(r),a=await this.waitForHealthy(r);return a.healthy?(await this.registerService(r,i),this.createEmptyStatus({running:!0,port:r,pid:i})):(await this.killProcess(i),await this.clearPortListeners(r,i),await this.releaseService(i),this.createEmptyStatus({error:a.error||`Server failed health check after startup`}))}catch(e){return this.createEmptyStatus({error:e instanceof Error?e.message:String(e)})}}async stop(){let t=await this.getRegistration(),n=!1;t?.pid&&(await this.killProcess(t.pid),n=!0),t&&(await this.clearPortListeners(t.port,t.pid),await this.releaseService(t.pid));let r=await this.findHealthyServicePort(e.c);if(r!==null){let e=await this.listListeningProcesses(r);for(let t of e)await this.killProcess(t.pid),n=!0;await this.releaseService()}return n}async getStatus(){let t=await this.getRegistration();if(t&&(await this.healthCheck.check(t.port)).healthy)return this.createEmptyStatus({running:!0,port:t.port,pid:t.pid});let n=await this.findHealthyServicePort(e.c);if(n!==null){let e=(await this.listListeningProcesses(n))[0]?.pid;return e&&await this.registerService(n,e),this.createEmptyStatus({running:!0,port:n,pid:e})}return this.createEmptyStatus({error:t?`Registered server is unhealthy`:`No HTTP server registered`})}};const S=`claude`,C=`default`,ie=`.claude-sessions`,ae=`scope`,oe=3,w=`utf8`,se=`inherit`,ce=10,T=`[model-proxy-mcp]`,E=`Recovery: verify HOME, proxy port, and that \`${S}\` is on PATH.`,le=`--resume`,ue=`--session-id`,de=`--dangerously-skip-permissions`,fe=`ccproxy-opus`,pe=`ccproxy-sonnet`,me=`ccproxy-haiku`,he=`ccproxy-subagent`,ge=`model-proxy-mcp.yaml`,_e=`MODEL_PROXY_MCP_SCOPE`,ve=`MODEL_PROXY_MCP_SLOT`,ye=`default`;var D=class extends Error{constructor(e,t,n){super(e,n),this.code=t,this.name=`ClaudeCommandError`}},O=class extends D{constructor(e,t){super(e,`CLAUDE_COMMAND_CONFIG_ERROR`,t),this.name=`ClaudeCommandConfigError`}},k=class extends D{constructor(e,t){super(e,`CLAUDE_COMMAND_VALIDATION_ERROR`,t),this.name=`ClaudeCommandValidationError`}},A=class extends D{constructor(e,t){super(e,`CLAUDE_COMMAND_LAUNCH_ERROR`,t),this.name=`ClaudeCommandLaunchError`}};function j(e){return e.trim().replace(/[^a-zA-Z0-9._-]+/g,`-`).replace(/^-+|-+$/g,``)||`default`}function M(){return`scope-${(0,o.randomBytes)(3).toString(`hex`)}`}function N(e,t,n=process.env){return{...n,ANTHROPIC_BASE_URL:`http://127.0.0.1:${e}/scopes/${t}`,ANTHROPIC_DEFAULT_OPUS_MODEL:n.ANTHROPIC_DEFAULT_OPUS_MODEL||`ccproxy-opus`,ANTHROPIC_DEFAULT_SONNET_MODEL:n.ANTHROPIC_DEFAULT_SONNET_MODEL||`ccproxy-sonnet`,ANTHROPIC_DEFAULT_HAIKU_MODEL:n.ANTHROPIC_DEFAULT_HAIKU_MODEL||`ccproxy-haiku`,CLAUDE_CODE_SUBAGENT_MODEL:n.CLAUDE_CODE_SUBAGENT_MODEL||`ccproxy-subagent`,MODEL_PROXY_MCP_SCOPE:n.MODEL_PROXY_MCP_SCOPE||t,MODEL_PROXY_MCP_SLOT:n.MODEL_PROXY_MCP_SLOT||`default`}}async function P(e,t=process.cwd()){let r=e?n.default.resolve(t,e):n.default.join(t,`model-proxy-mcp.yaml`);try{if(!(await s.default.stat(r)).isFile()){if(e)throw new O(`Scope seed config path is not a file: ${r}`);return}return r}catch(t){if(t.code===`ENOENT`){if(e)throw new O(`Scope seed config file not found: ${r}`,{cause:t});return}throw t instanceof O?t:new O(`Failed to read scope seed config file: ${r}`,{cause:t})}}function F(){let e=process.env.HOME||process.env.USERPROFILE;if(!e)throw new O(`HOME or USERPROFILE is not set`);return n.default.join(e,`.claude-sessions`)}async function I(){let e=F();return await s.default.mkdir(e,{recursive:!0}),e}async function L(e){try{return(await s.default.readFile(e,w)).trim()||null}catch(t){if(t.code===`ENOENT`)return null;throw new O(`Failed to read Claude session file: ${e}`,{cause:t})}}async function R(e,t){let r=await I(),i=n.default.join(r,e);if(t)try{await s.default.rm(i,{force:!0})}catch(e){throw new O(`Failed to clear Claude session file: ${i}`,{cause:e})}let a=await L(i);if(a)return{sessionId:a,resume:!0};let c=(0,o.randomUUID)().toLowerCase();try{await s.default.writeFile(i,`${c}\n`,w)}catch(e){throw new O(`Failed to write Claude session file: ${i}`,{cause:e})}return{sessionId:c,resume:!1}}async function z(t,n,r,i,o){let s=await P(o);await new e.o().ensureConfig(t,s);let c=await new x().ensureRunning(n);if(!c.running||!c.port)throw new A(c.error||`Failed to start model proxy HTTP server`);let{sessionId:l,resume:u}=await R(t,r),d=N(c.port,t),f=[`--dangerously-skip-permissions`,...u?[`--resume`,l]:[`--session-id`,l],...i];return console.log(`${T} Scope: ${t}`),console.log(`${T} Proxy: ${d.ANTHROPIC_BASE_URL}`),console.log(`${T} Session: ${l}${u?` (resume)`:` (new)`}`),s&&console.log(`${T} Scope config seed: ${s}`),new Promise((e,t)=>{let n=(0,a.spawn)(S,f,{stdio:`inherit`,env:d});n.on(`error`,e=>{t(new A(`Failed to launch ${S}`,{cause:e}))}),n.on(`exit`,(n,r)=>{if(r){t(new A(`${S} exited with signal ${r}`));return}e(n??0)})})}const B=new i.Command(`claude`).description(`Launch Claude Code through the model proxy`).option(`-s, --scope <scope>`,`Proxy scope to use`).option(`-p, --port <port>`,`Preferred HTTP port for the proxy`,String(e.c)).option(`-c, --config-file <path>`,`Seed a new scope from the specified scope config file`).option(`--config <path>`,`Alias for --config-file`).option(`--clear-session`,`Start with a fresh Claude session for the selected scope`,!1).allowUnknownOption(!0).allowExcessArguments(!0).argument(`[claudeArgs...]`).action(async(e,t)=>{try{let n=j(t.scope||M()),r=Number.parseInt(t.port,10);if(!Number.isInteger(r)||r<=0)throw new k(`Invalid port: ${t.port}`);let i=await z(n,r,t.clearSession,e,t.configFile??t.config);process.exit(i)}catch(e){let t=e instanceof D?e:new A(`Failed to launch Claude`,{cause:e});console.error(`${T} ${t.code}: ${t.message}`),console.error(`${T} ${E}`),t.cause&&console.error(`${T} Cause:`,t.cause),process.exit(1)}}),V=`127.0.0.1`,H=new i.Command(`http-serve`).description(`Start the Claude-compatible HTTP model proxy server`).option(`-p, --port <port>`,`Port to listen on`,String(e.c)).action(async t=>{try{let n=Number.parseInt(t.port,10);if(!Number.isInteger(n)||n<=0)throw Error(`Invalid port: ${t.port}`);let r=new e.i;await r.ensureConfig();let i=(0,u.serve)({fetch:e.r(r).fetch,port:n,hostname:V});console.log(`model-proxy-mcp listening on http://${V}:${n}`),console.log(`Claude base URL: http://${V}:${n}`);let a=e=>{console.log(`\n${e} received. Shutting down...`),i.close(),process.exit(0)};process.once(`SIGINT`,()=>a(`SIGINT`)),process.once(`SIGTERM`,()=>a(`SIGTERM`))}catch(e){console.error(`Failed to start HTTP server:`,e),process.exit(1)}}),be=`default`,xe=`MODEL_PROXY_MCP_SCOPE`,U=new i.Command(`mcp-serve`).description(`Start MCP server with stdio transport`).option(`--cleanup`,`Stop HTTP server on shutdown`,!1).option(`-p, --port <port>`,`Port for HTTP server`,String(e.c)).action(async t=>{try{let n=Number.parseInt(t.port,10);if(!Number.isInteger(n)||n<=0)throw Error(`Invalid port: ${t.port}`);let r=new e.i;await r.ensureConfig(process.env.MODEL_PROXY_MCP_SCOPE||`default`);let i=new x,a=await i.ensureRunning(n);a.running||console.error(`Warning: HTTP server failed to start: ${a.error}`);let o=new e.t(e.n(r)),s=async e=>{console.error(`\nReceived ${e}, shutting down gracefully...`),await o.stop(),t.cleanup&&a.running&&await i.stop(),process.exit(0)};process.once(`SIGINT`,()=>void s(`SIGINT`).catch(e=>{console.error(`Failed to shut down after SIGINT:`,e),process.exit(1)})),process.once(`SIGTERM`,()=>void s(`SIGTERM`).catch(e=>{console.error(`Failed to shut down after SIGTERM:`,e),process.exit(1)})),await o.start()}catch(e){console.error(`Failed to start MCP server:`,e),process.exit(1)}}),W=new i.Command(`start`).description(`Start HTTP and/or MCP server for the model proxy`).option(`--mcp-only`,`Start only the MCP server`,!1).option(`--http-only`,`Start only the HTTP server`,!1).option(`-p, --port <port>`,`Port for HTTP server`,String(e.c)).action(async t=>{try{let n=new x,r=Number.parseInt(t.port,10),i=!t.mcpOnly,a=!t.httpOnly;if(i){let e=await n.ensureRunning(r);e.running||(console.error(`HTTP server failed to start: ${e.error}`),process.exit(1)),console.log(`HTTP server running on http://127.0.0.1:${e.port}`)}if(a){let t=new e.t(e.n());await t.start();let n=async e=>{console.error(`\n${e} received. Shutting down...`),await t.stop(),process.exit(0)};process.on(`SIGINT`,()=>n(`SIGINT`)),process.on(`SIGTERM`,()=>n(`SIGTERM`))}else process.exit(0)}catch(e){console.error(`Failed to start services:`,e),process.exit(1)}}),G=`default`,K=`status`,Se=`Show proxy server and profile status`,q=`Failed to read status`;var J=class extends Error{code=`STATUS_CHECK_FAILED`;constructor(e){super(q,{cause:e instanceof Error?e:Error(String(e))}),this.name=`StatusCommandError`}};const Y=new i.Command(K).description(`Show proxy server and profile status`).option(`-s, --scope <scope>`,`Configuration scope`,`default`).action(async t=>{try{let n=await new x().getStatus(),r=await new e.i().getStatus(t.scope,n.port,n.pid);r.running=n.running,r.error=n.error,e.s.info(JSON.stringify(r,null,2)),process.exit(0)}catch(n){let r=new J(n);e.s.error(q,{command:K,code:r.code,scope:t.scope,cause:r.cause}),process.exit(1)}}),X=new i.Command(`stop`).description(`Stop the background HTTP server`).action(async()=>{try{let e=await new x().stop();console.log(e?`HTTP server stopped`:`No HTTP server running`),process.exit(0)}catch(e){console.error(`Failed to stop HTTP server:`,e),process.exit(1)}}),Z=(0,n.dirname)((0,r.fileURLToPath)(require(`url`).pathToFileURL(__filename).href)),Q=JSON.parse((0,t.readFileSync)((0,n.join)(Z,`../package.json`),`utf-8`));async function $(){let e=new i.Command;e.name(`model-proxy-mcp`).description(`Claude-compatible model proxy MCP`).version(Q.version),e.addCommand(W),e.addCommand(X),e.addCommand(Y),e.addCommand(B),e.addCommand(H),e.addCommand(U),await e.parseAsync(process.argv)}$();
3
+ `).slice(1).map(e=>e.trim()).filter(Boolean).map(e=>e.split(/\s+/)).map(t=>({pid:Number.parseInt(t[1]||``,10),port:e})).filter(e=>Number.isInteger(e.pid)&&e.pid>0)}catch{return[]}}async findHealthyServicePort(e){for(let t=0;t<=25;t+=1){let n=e+t,r=await this.healthCheck.check(n);if(r.healthy&&r.serviceName===this.serviceName)return n}return null}async clearPortListeners(e,t){let n=await this.listListeningProcesses(e);for(let e of n)t&&e.pid===t||await this.killProcess(e.pid)}async registerService(e,t){await s.default.writeFile(this.stateFilePath,JSON.stringify({port:e,pid:t}),v)}async findAvailablePort(t){let n=Math.min(e.c,t),r=Math.max(e.c+1e3,t);for(let e=t;e<=r;e+=1)if((await this.listListeningProcesses(e)).length===0)return e;for(let e=n;e<t;e+=1)if((await this.listListeningProcesses(e)).length===0)return e;throw Error(`No available port found from ${t}`)}async fileExists(e){try{return await s.default.access(e),!0}catch{return!1}}async resolveCliPath(){let e=n.default.dirname((0,r.fileURLToPath)(require(`url`).pathToFileURL(__filename).href)),t=n.default.resolve(e,`cli.mjs`);if(await this.fileExists(t))return{cliPath:t,runtime:`node`};let i=n.default.resolve(e,`..`,`cli.mjs`);if(await this.fileExists(i))return{cliPath:i,runtime:`node`};let a=n.default.join(this.repositoryPath,`packages`,`mcp`,`model-proxy-mcp`,`dist`,`cli.mjs`);if(await this.fileExists(a))return{cliPath:a,runtime:`node`};for(let t of[n.default.resolve(e,`cli.ts`),n.default.resolve(e,`..`,`cli.ts`)])if(await this.fileExists(t))return{cliPath:t,runtime:`bun`};let o=n.default.join(this.repositoryPath,`packages`,`mcp`,`model-proxy-mcp`,`src`,`cli.ts`);if(await this.fileExists(o))return{cliPath:o,runtime:`bun`};throw Error(`Cannot find model-proxy-mcp CLI`)}async startHttpServer(e){let{cliPath:t,runtime:n}=await this.resolveCliPath(),r=(0,a.spawn)(n===`bun`?process.versions.bun?process.execPath:`bun`:`node`,n===`bun`?[`run`,t,`http-serve`,`--port`,String(e)]:[t,`http-serve`,`--port`,String(e)],{detached:!0,stdio:`ignore`,env:{...process.env,NODE_ENV:process.env.NODE_ENV||`development`}});if(r.unref(),!r.pid)throw Error(`Failed to spawn HTTP server`);return r.pid}async killProcess(e){try{process.kill(e,`SIGTERM`),await new Promise(e=>setTimeout(e,1e3));try{process.kill(e,0),process.kill(e,`SIGKILL`)}catch{return}}catch{return}}async waitForHealthy(e){let t=Date.now()+5e3,n=`Server failed health check after startup`;for(;Date.now()<t;){let t=await this.healthCheck.check(e);if(t.healthy&&t.serviceName===this.serviceName)return{healthy:!0};n=t.error||`Unexpected service on port ${e}`,await new Promise(e=>setTimeout(e,250))}return{healthy:!1,error:n}}async ensureRunning(t=e.c){try{let e=await this.findHealthyServicePort(t);if(e!==null){let t=(await this.listListeningProcesses(e))[0]?.pid;return await this.registerService(e,t??process.pid),this.createEmptyStatus({running:!0,port:e,pid:t})}let n=await this.getRegistration();if(n){let e=await this.healthCheck.check(n.port);if(e.healthy&&e.serviceName===this.serviceName)return this.createEmptyStatus({running:!0,port:n.port,pid:n.pid});n.pid&&await this.killProcess(n.pid),await this.clearPortListeners(n.port,n.pid),await this.releaseService(n.pid)}await this.clearPortListeners(t);let r=await this.findAvailablePort(t),i=await this.startHttpServer(r),a=await this.waitForHealthy(r);return a.healthy?(await this.registerService(r,i),this.createEmptyStatus({running:!0,port:r,pid:i})):(await this.killProcess(i),await this.clearPortListeners(r,i),await this.releaseService(i),this.createEmptyStatus({error:a.error||`Server failed health check after startup`}))}catch(e){return this.createEmptyStatus({error:e instanceof Error?e.message:String(e)})}}async stop(){let t=await this.getRegistration(),n=!1;t?.pid&&(await this.killProcess(t.pid),n=!0),t&&(await this.clearPortListeners(t.port,t.pid),await this.releaseService(t.pid));let r=await this.findHealthyServicePort(e.c);if(r!==null){let e=await this.listListeningProcesses(r);for(let t of e)await this.killProcess(t.pid),n=!0;await this.releaseService()}return n}async getStatus(){let t=await this.getRegistration();if(t&&(await this.healthCheck.check(t.port)).healthy)return this.createEmptyStatus({running:!0,port:t.port,pid:t.pid});let n=await this.findHealthyServicePort(e.c);if(n!==null){let e=(await this.listListeningProcesses(n))[0]?.pid;return e&&await this.registerService(n,e),this.createEmptyStatus({running:!0,port:n,pid:e})}return this.createEmptyStatus({error:t?`Registered server is unhealthy`:`No HTTP server registered`})}};const S=`claude`,C=`default`,ie=`.claude-sessions`,ae=`scope`,oe=3,w=`utf8`,se=`inherit`,ce=10,T=`[model-proxy-mcp]`,E=`Recovery: verify HOME, proxy port, and that \`${S}\` is on PATH.`,le=`--resume`,ue=`--session-id`,de=`--dangerously-skip-permissions`,fe=`ccproxy-opus`,pe=`ccproxy-sonnet`,me=`ccproxy-haiku`,he=`ccproxy-subagent`,ge=`model-proxy-mcp.yaml`,_e=`MODEL_PROXY_MCP_SCOPE`,ve=`MODEL_PROXY_MCP_SLOT`,ye=`default`;var D=class extends Error{constructor(e,t,n){super(e,n),this.code=t,this.name=`ClaudeCommandError`}},O=class extends D{constructor(e,t){super(e,`CLAUDE_COMMAND_CONFIG_ERROR`,t),this.name=`ClaudeCommandConfigError`}},k=class extends D{constructor(e,t){super(e,`CLAUDE_COMMAND_VALIDATION_ERROR`,t),this.name=`ClaudeCommandValidationError`}},A=class extends D{constructor(e,t){super(e,`CLAUDE_COMMAND_LAUNCH_ERROR`,t),this.name=`ClaudeCommandLaunchError`}};function j(e){return e.trim().replace(/[^a-zA-Z0-9._-]+/g,`-`).replace(/^-+|-+$/g,``)||`default`}function M(){return`scope-${(0,o.randomBytes)(3).toString(`hex`)}`}function N(e,t,n=process.env){return{...n,ANTHROPIC_BASE_URL:`http://127.0.0.1:${e}/scopes/${t}`,ANTHROPIC_DEFAULT_OPUS_MODEL:n.ANTHROPIC_DEFAULT_OPUS_MODEL||`ccproxy-opus`,ANTHROPIC_DEFAULT_SONNET_MODEL:n.ANTHROPIC_DEFAULT_SONNET_MODEL||`ccproxy-sonnet`,ANTHROPIC_DEFAULT_HAIKU_MODEL:n.ANTHROPIC_DEFAULT_HAIKU_MODEL||`ccproxy-haiku`,CLAUDE_CODE_SUBAGENT_MODEL:n.CLAUDE_CODE_SUBAGENT_MODEL||`ccproxy-subagent`,MODEL_PROXY_MCP_SCOPE:n.MODEL_PROXY_MCP_SCOPE||t,MODEL_PROXY_MCP_SLOT:n.MODEL_PROXY_MCP_SLOT||`default`}}async function P(e,t=process.cwd()){let r=e?n.default.resolve(t,e):n.default.join(t,`model-proxy-mcp.yaml`);try{if(!(await s.default.stat(r)).isFile()){if(e)throw new O(`Scope seed config path is not a file: ${r}`);return}return r}catch(t){if(t.code===`ENOENT`){if(e)throw new O(`Scope seed config file not found: ${r}`,{cause:t});return}throw t instanceof O?t:new O(`Failed to read scope seed config file: ${r}`,{cause:t})}}function F(){let e=process.env.HOME||process.env.USERPROFILE;if(!e)throw new O(`HOME or USERPROFILE is not set`);return n.default.join(e,`.claude-sessions`)}async function I(){let e=F();return await s.default.mkdir(e,{recursive:!0}),e}async function L(e){try{return(await s.default.readFile(e,w)).trim()||null}catch(t){if(t.code===`ENOENT`)return null;throw new O(`Failed to read Claude session file: ${e}`,{cause:t})}}async function R(e,t){let r=await I(),i=n.default.join(r,e);if(t)try{await s.default.rm(i,{force:!0})}catch(e){throw new O(`Failed to clear Claude session file: ${i}`,{cause:e})}let a=await L(i);if(a)return{sessionId:a,resume:!0};let c=(0,o.randomUUID)().toLowerCase();try{await s.default.writeFile(i,`${c}\n`,w)}catch(e){throw new O(`Failed to write Claude session file: ${i}`,{cause:e})}return{sessionId:c,resume:!1}}async function z(t,n,r,i,o){let s=await P(o);await new e.o().ensureConfig(t,s);let c=await new x().ensureRunning(n);if(!c.running||!c.port)throw new A(c.error||`Failed to start model proxy HTTP server`);let{sessionId:l,resume:u}=await R(t,r),d=N(c.port,t),f=[`--dangerously-skip-permissions`,...u?[`--resume`,l]:[`--session-id`,l],...i];return console.log(`${T} Scope: ${t}`),console.log(`${T} Proxy: ${d.ANTHROPIC_BASE_URL}`),console.log(`${T} Session: ${l}${u?` (resume)`:` (new)`}`),s&&console.log(`${T} Scope config seed: ${s}`),new Promise((e,t)=>{let n=(0,a.spawn)(S,f,{stdio:`inherit`,env:d});n.on(`error`,e=>{t(new A(`Failed to launch ${S}`,{cause:e}))}),n.on(`exit`,(n,r)=>{if(r){t(new A(`${S} exited with signal ${r}`));return}e(n??0)})})}const B=new i.Command(`claude`).description(`Launch Claude Code through the model proxy`).option(`-s, --scope <scope>`,`Proxy scope to use`).option(`-p, --port <port>`,`Preferred HTTP port for the proxy`,String(e.c)).option(`-c, --config-file <path>`,`Seed a new scope from the specified scope config file`).option(`--config <path>`,`Alias for --config-file`).option(`--clear-session`,`Start with a fresh Claude session for the selected scope`,!1).allowUnknownOption(!0).allowExcessArguments(!0).argument(`[claudeArgs...]`).action(async(e,t)=>{try{let n=j(t.scope||M()),r=Number.parseInt(t.port,10);if(!Number.isInteger(r)||r<=0)throw new k(`Invalid port: ${t.port}`);let i=await z(n,r,t.clearSession,e,t.configFile??t.config);process.exit(i)}catch(e){let t=e instanceof D?e:new A(`Failed to launch Claude`,{cause:e});console.error(`${T} ${t.code}: ${t.message}`),console.error(`${T} ${E}`),t.cause&&console.error(`${T} Cause:`,t.cause),process.exit(1)}}),V=`127.0.0.1`,H=new i.Command(`http-serve`).description(`Start the Claude-compatible HTTP model proxy server`).option(`-p, --port <port>`,`Port to listen on`,String(e.c)).action(async t=>{try{let n=Number.parseInt(t.port,10);if(!Number.isInteger(n)||n<=0)throw Error(`Invalid port: ${t.port}`);let r=new e.i;await r.ensureConfig();let i=(0,u.serve)({fetch:e.r(r).fetch,port:n,hostname:V});console.log(`model-proxy-mcp listening on http://${V}:${n}`),console.log(`Claude base URL: http://${V}:${n}`);let a=e=>{console.log(`\n${e} received. Shutting down...`),i.close(),process.exit(0)};process.once(`SIGINT`,()=>a(`SIGINT`)),process.once(`SIGTERM`,()=>a(`SIGTERM`))}catch(e){console.error(`Failed to start HTTP server:`,e),process.exit(1)}}),be=`default`,xe=`MODEL_PROXY_MCP_SCOPE`,U=new i.Command(`mcp-serve`).description(`Start MCP server with stdio transport`).option(`--cleanup`,`Stop HTTP server on shutdown`,!1).option(`-p, --port <port>`,`Port for HTTP server`,String(e.c)).action(async t=>{try{let n=Number.parseInt(t.port,10);if(!Number.isInteger(n)||n<=0)throw Error(`Invalid port: ${t.port}`);let r=new e.i;await r.ensureConfig(process.env.MODEL_PROXY_MCP_SCOPE||`default`);let i=new x,a=await i.ensureRunning(n);a.running||console.error(`Warning: HTTP server failed to start: ${a.error}`);let o=new e.t(e.n(r)),s=async e=>{console.error(`\nReceived ${e}, shutting down gracefully...`),await o.stop(),t.cleanup&&a.running&&await i.stop(),process.exit(0)};process.once(`SIGINT`,()=>void s(`SIGINT`).catch(e=>{console.error(`Failed to shut down after SIGINT:`,e),process.exit(1)})),process.once(`SIGTERM`,()=>void s(`SIGTERM`).catch(e=>{console.error(`Failed to shut down after SIGTERM:`,e),process.exit(1)})),await o.start()}catch(e){console.error(`Failed to start MCP server:`,e),process.exit(1)}}),W=new i.Command(`start`).description(`Start HTTP and/or MCP server for the model proxy`).option(`--mcp-only`,`Start only the MCP server`,!1).option(`--http-only`,`Start only the HTTP server`,!1).option(`-p, --port <port>`,`Port for HTTP server`,String(e.c)).action(async t=>{try{let n=new x,r=Number.parseInt(t.port,10),i=!t.mcpOnly,a=!t.httpOnly;if(i){let e=await n.ensureRunning(r);e.running||(console.error(`HTTP server failed to start: ${e.error}`),process.exit(1)),console.log(`HTTP server running on http://127.0.0.1:${e.port}`)}if(a){let t=new e.t(e.n());await t.start();let n=async e=>{console.error(`\n${e} received. Shutting down...`),await t.stop(),process.exit(0)};process.on(`SIGINT`,()=>n(`SIGINT`)),process.on(`SIGTERM`,()=>n(`SIGTERM`))}else process.exit(0)}catch(e){console.error(`Failed to start services:`,e),process.exit(1)}}),G=`default`,K=`status`,Se=`Show proxy server and profile status`,q=`Failed to read status`;var J=class extends Error{code=`STATUS_CHECK_FAILED`;constructor(e){super(q,{cause:e instanceof Error?e:Error(String(e))}),this.name=`StatusCommandError`}};const Y=new i.Command(K).description(`Show proxy server and profile status`).option(`-s, --scope <scope>`,`Configuration scope`,`default`).action(async t=>{try{let n=await new x().getStatus(),r=await new e.i().getStatus(t.scope,n.port,n.pid);r.running=n.running,r.error=n.error,e.s.info(JSON.stringify(r,null,2)),process.exit(0)}catch(n){let r=new J(n);e.s.error(q,{command:K,code:r.code,scope:t.scope,cause:r.cause}),process.exit(1)}}),X=new i.Command(`stop`).description(`Stop the background HTTP server`).action(async()=>{try{let e=await new x().stop();console.log(e?`HTTP server stopped`:`No HTTP server running`),process.exit(0)}catch(e){console.error(`Failed to stop HTTP server:`,e),process.exit(1)}}),Z=(0,n.dirname)((0,r.fileURLToPath)(require(`url`).pathToFileURL(__filename).href)),Q=JSON.parse((0,t.readFileSync)((0,n.join)(Z,`../package.json`),`utf-8`));async function $(){let e=new i.Command;e.name(`model-proxy-mcp`).description(`Claude-compatible model proxy MCP`).version(Q.version),e.addCommand(W),e.addCommand(X),e.addCommand(Y),e.addCommand(B),e.addCommand(H),e.addCommand(U),await e.parseAsync(process.argv)}$();
4
+ //# sourceMappingURL=cli.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.cjs","names":["SIGTERM_SIGNAL: NodeJS.Signals","SIGKILL_SIGNAL: NodeJS.Signals","STATE_FILE_ENCODING: BufferEncoding","execFile","path","DEFAULT_SERVICE_NAME","os","fs","DEFAULT_HTTP_PORT","health","DEFAULT_SCOPE","TEXT_ENCODING: BufferEncoding","MODEL_PROXY_SCOPE_ENV","code: string","path","fs","ProfileStore","Command","DEFAULT_HTTP_PORT","Command","DEFAULT_HTTP_PORT","GatewayService","createHttpServer","DEFAULT_SCOPE","Command","DEFAULT_HTTP_PORT","GatewayService","StdioTransportHandler","createServer","Command","DEFAULT_HTTP_PORT","StdioTransportHandler","createServer","Command","GatewayService","Command","__dirname","Command"],"sources":["../src/services/HttpServerHealthCheck.ts","../src/services/HttpServerManager.ts","../src/commands/claude.ts","../src/commands/http-serve.ts","../src/commands/mcp-serve.ts","../src/commands/start.ts","../src/commands/status.ts","../src/commands/stop.ts","../src/cli.ts"],"sourcesContent":["export interface HealthCheckResult {\n healthy: boolean;\n serviceName?: string;\n error?: string;\n}\n\nexport class HttpServerHealthCheck {\n async check(port: number): Promise<HealthCheckResult> {\n try {\n const response = await fetch(`http://127.0.0.1:${port}/health`);\n if (!response.ok) {\n return { healthy: false, error: `Health check failed with status ${response.status}` };\n }\n\n const payload = (await response.json()) as { service?: string };\n return {\n healthy: true,\n serviceName: payload.service,\n };\n } catch (error) {\n return {\n healthy: false,\n error: error instanceof Error ? error.message : String(error),\n };\n }\n }\n}\n","import { execFile, spawn } from 'node:child_process';\nimport { existsSync } from 'node:fs';\nimport fs from 'node:fs/promises';\nimport os from 'node:os';\nimport path from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport { promisify } from 'node:util';\nimport { DEFAULT_HTTP_PORT, DEFAULT_SERVICE_NAME } from '../constants/defaults.js';\nimport type { GatewayStatus } from '../types/index.js';\nimport { HttpServerHealthCheck } from './HttpServerHealthCheck.js';\n\nconst WORKSPACE_MARKERS = ['pnpm-workspace.yaml', 'nx.json', '.git'];\nconst PORT_SCAN_RANGE = 25;\nconst SHELL_BINARY = 'sh';\nconst LSOF_COMMAND_PREFIX = 'lsof -n -P -sTCP:LISTEN';\nconst HEALTH_CHECK_STABILIZE_MS = 5000;\nconst HEALTH_CHECK_POLL_INTERVAL_MS = 250;\nconst PROCESS_SHUTDOWN_WAIT_MS = 1000;\nconst SIGTERM_SIGNAL: NodeJS.Signals = 'SIGTERM';\nconst SIGKILL_SIGNAL: NodeJS.Signals = 'SIGKILL';\nconst STATE_FILE_PREFIX = 'model-proxy-mcp-http';\nconst STATE_FILE_ENCODING: BufferEncoding = 'utf8';\nconst execFileAsync = promisify(execFile);\n\nfunction resolveWorkspaceRoot(startPath = process.cwd()): string {\n let currentDir = path.resolve(startPath);\n\n while (true) {\n for (const marker of WORKSPACE_MARKERS) {\n if (existsSync(path.join(currentDir, marker))) {\n return currentDir;\n }\n }\n\n const parentDir = path.dirname(currentDir);\n if (parentDir === currentDir) {\n return process.cwd();\n }\n currentDir = parentDir;\n }\n}\n\ninterface ServiceRegistration {\n port: number;\n pid?: number;\n}\n\ninterface PortListener {\n port: number;\n pid: number;\n}\n\ntype CliRuntime = 'node' | 'bun';\n\nexport class HttpServerManager {\n private readonly repositoryPath = resolveWorkspaceRoot(process.cwd());\n private readonly serviceName = DEFAULT_SERVICE_NAME;\n private readonly stateFilePath = path.join(\n os.tmpdir(),\n `${STATE_FILE_PREFIX}-${Buffer.from(resolveWorkspaceRoot(process.cwd())).toString('hex')}.json`,\n );\n\n constructor(private readonly healthCheck = new HttpServerHealthCheck()) {}\n\n private createEmptyStatus(overrides: Partial<GatewayStatus> = {}): GatewayStatus {\n return {\n running: false,\n scope: 'default',\n activeProfileId: null,\n auth: { configured: false, authFilePath: '' },\n profiles: [],\n slotModels: {},\n ...overrides,\n };\n }\n\n private async getRegistration(): Promise<ServiceRegistration | null> {\n try {\n const rawState = await fs.readFile(this.stateFilePath, STATE_FILE_ENCODING);\n const parsed = JSON.parse(rawState) as ServiceRegistration;\n return parsed.port ? parsed : null;\n } catch (error) {\n const nodeError = error as NodeJS.ErrnoException;\n if (nodeError.code === 'ENOENT') {\n return null;\n }\n throw error;\n }\n }\n\n private async releaseService(pid?: number): Promise<void> {\n const registration = await this.getRegistration();\n if (pid && registration?.pid && registration.pid !== pid) {\n return;\n }\n await fs.rm(this.stateFilePath, { force: true });\n }\n\n private async listListeningProcesses(port: number): Promise<PortListener[]> {\n try {\n const { stdout } = await execFileAsync(SHELL_BINARY, ['-lc', `${LSOF_COMMAND_PREFIX} -iTCP:${port} || true`], {\n encoding: 'utf8',\n });\n return stdout\n .split('\\n')\n .slice(1)\n .map((line) => line.trim())\n .filter(Boolean)\n .map((line) => line.split(/\\s+/))\n .map((parts) => ({\n pid: Number.parseInt(parts[1] || '', 10),\n port,\n }))\n .filter((listener) => Number.isInteger(listener.pid) && listener.pid > 0);\n } catch (error) {\n return [];\n }\n }\n\n private async findHealthyServicePort(startPort: number): Promise<number | null> {\n for (let offset = 0; offset <= PORT_SCAN_RANGE; offset += 1) {\n const candidatePort = startPort + offset;\n const health = await this.healthCheck.check(candidatePort);\n if (health.healthy && health.serviceName === this.serviceName) {\n return candidatePort;\n }\n }\n\n return null;\n }\n\n private async clearPortListeners(port: number, preservePid?: number): Promise<void> {\n const listeners = await this.listListeningProcesses(port);\n for (const listener of listeners) {\n if (preservePid && listener.pid === preservePid) {\n continue;\n }\n await this.killProcess(listener.pid);\n }\n }\n\n private async registerService(port: number, pid: number): Promise<void> {\n await fs.writeFile(this.stateFilePath, JSON.stringify({ port, pid }), STATE_FILE_ENCODING);\n }\n\n private async findAvailablePort(startPort: number): Promise<number> {\n const minimumPort = Math.min(DEFAULT_HTTP_PORT, startPort);\n const maximumPort = Math.max(DEFAULT_HTTP_PORT + 1000, startPort);\n\n for (let candidatePort = startPort; candidatePort <= maximumPort; candidatePort += 1) {\n const listeners = await this.listListeningProcesses(candidatePort);\n if (listeners.length === 0) {\n return candidatePort;\n }\n }\n\n for (let candidatePort = minimumPort; candidatePort < startPort; candidatePort += 1) {\n const listeners = await this.listListeningProcesses(candidatePort);\n if (listeners.length === 0) {\n return candidatePort;\n }\n }\n\n throw new Error(`No available port found from ${startPort}`);\n }\n\n private async fileExists(filePath: string): Promise<boolean> {\n try {\n await fs.access(filePath);\n return true;\n } catch {\n return false;\n }\n }\n\n private async resolveCliPath(): Promise<{ cliPath: string; runtime: CliRuntime }> {\n const currentDir = path.dirname(fileURLToPath(import.meta.url));\n\n // Bundled dist output: cli.mjs lives in the same directory as this file\n const sameDirCliPath = path.resolve(currentDir, 'cli.mjs');\n if (await this.fileExists(sameDirCliPath)) {\n return { cliPath: sameDirCliPath, runtime: 'node' };\n }\n\n // Unbundled dist output: this file is in dist/services/, cli.mjs is in dist/\n const parentDirCliPath = path.resolve(currentDir, '..', 'cli.mjs');\n if (await this.fileExists(parentDirCliPath)) {\n return { cliPath: parentDirCliPath, runtime: 'node' };\n }\n\n // Workspace fallbacks for monorepo development\n const repoDistCliPath = path.join(this.repositoryPath, 'packages', 'mcp', 'model-proxy-mcp', 'dist', 'cli.mjs');\n if (await this.fileExists(repoDistCliPath)) {\n return { cliPath: repoDistCliPath, runtime: 'node' };\n }\n\n // Dev mode: source cli.ts next to this file or one level up\n for (const relPath of [path.resolve(currentDir, 'cli.ts'), path.resolve(currentDir, '..', 'cli.ts')]) {\n if (await this.fileExists(relPath)) {\n return { cliPath: relPath, runtime: 'bun' };\n }\n }\n\n const repoSrcCliPath = path.join(this.repositoryPath, 'packages', 'mcp', 'model-proxy-mcp', 'src', 'cli.ts');\n if (await this.fileExists(repoSrcCliPath)) {\n return { cliPath: repoSrcCliPath, runtime: 'bun' };\n }\n\n throw new Error('Cannot find model-proxy-mcp CLI');\n }\n\n private async startHttpServer(port: number): Promise<number> {\n const { cliPath, runtime } = await this.resolveCliPath();\n\n const command = runtime === 'bun' ? (process.versions.bun ? process.execPath : 'bun') : 'node';\n const args =\n runtime === 'bun'\n ? ['run', cliPath, 'http-serve', '--port', String(port)]\n : [cliPath, 'http-serve', '--port', String(port)];\n\n const child = spawn(command, args, {\n detached: true,\n stdio: 'ignore',\n env: {\n ...process.env,\n NODE_ENV: process.env.NODE_ENV || 'development',\n },\n });\n\n child.unref();\n if (!child.pid) {\n throw new Error('Failed to spawn HTTP server');\n }\n\n return child.pid;\n }\n\n private async killProcess(pid: number): Promise<void> {\n try {\n process.kill(pid, SIGTERM_SIGNAL);\n await new Promise((resolve) => setTimeout(resolve, PROCESS_SHUTDOWN_WAIT_MS));\n try {\n process.kill(pid, 0);\n process.kill(pid, SIGKILL_SIGNAL);\n } catch {\n return;\n }\n } catch {\n return;\n }\n }\n\n private async waitForHealthy(port: number): Promise<{ healthy: boolean; error?: string }> {\n const deadline = Date.now() + HEALTH_CHECK_STABILIZE_MS;\n let lastError = 'Server failed health check after startup';\n\n while (Date.now() < deadline) {\n const health = await this.healthCheck.check(port);\n if (health.healthy && health.serviceName === this.serviceName) {\n return { healthy: true };\n }\n\n lastError = health.error || `Unexpected service on port ${port}`;\n await new Promise((resolve) => setTimeout(resolve, HEALTH_CHECK_POLL_INTERVAL_MS));\n }\n\n return { healthy: false, error: lastError };\n }\n\n async ensureRunning(port = DEFAULT_HTTP_PORT): Promise<GatewayStatus> {\n try {\n const healthyPort = await this.findHealthyServicePort(port);\n if (healthyPort !== null) {\n const healthyListeners = await this.listListeningProcesses(healthyPort);\n const healthyPid = healthyListeners[0]?.pid;\n await this.registerService(healthyPort, healthyPid ?? process.pid);\n return this.createEmptyStatus({ running: true, port: healthyPort, pid: healthyPid });\n }\n\n const existing = await this.getRegistration();\n if (existing) {\n const health = await this.healthCheck.check(existing.port);\n if (health.healthy && health.serviceName === this.serviceName) {\n return this.createEmptyStatus({ running: true, port: existing.port, pid: existing.pid });\n }\n\n if (existing.pid) {\n await this.killProcess(existing.pid);\n }\n await this.clearPortListeners(existing.port, existing.pid);\n await this.releaseService(existing.pid);\n }\n\n await this.clearPortListeners(port);\n const availablePort = await this.findAvailablePort(port);\n const pid = await this.startHttpServer(availablePort);\n const health = await this.waitForHealthy(availablePort);\n if (!health.healthy) {\n await this.killProcess(pid);\n await this.clearPortListeners(availablePort, pid);\n await this.releaseService(pid);\n return this.createEmptyStatus({ error: health.error || 'Server failed health check after startup' });\n }\n\n await this.registerService(availablePort, pid);\n return this.createEmptyStatus({ running: true, port: availablePort, pid });\n } catch (error) {\n return this.createEmptyStatus({ error: error instanceof Error ? error.message : String(error) });\n }\n }\n\n async stop(): Promise<boolean> {\n const registration = await this.getRegistration();\n let stopped = false;\n\n if (registration?.pid) {\n await this.killProcess(registration.pid);\n stopped = true;\n }\n if (registration) {\n await this.clearPortListeners(registration.port, registration.pid);\n await this.releaseService(registration.pid);\n }\n\n const healthyPort = await this.findHealthyServicePort(DEFAULT_HTTP_PORT);\n if (healthyPort !== null) {\n const listeners = await this.listListeningProcesses(healthyPort);\n for (const listener of listeners) {\n await this.killProcess(listener.pid);\n stopped = true;\n }\n await this.releaseService();\n }\n\n return stopped;\n }\n\n async getStatus(): Promise<GatewayStatus> {\n const registration = await this.getRegistration();\n if (registration) {\n const health = await this.healthCheck.check(registration.port);\n if (health.healthy) {\n return this.createEmptyStatus({\n running: true,\n port: registration.port,\n pid: registration.pid,\n });\n }\n }\n\n const healthyPort = await this.findHealthyServicePort(DEFAULT_HTTP_PORT);\n if (healthyPort !== null) {\n const listeners = await this.listListeningProcesses(healthyPort);\n const pid = listeners[0]?.pid;\n if (pid) {\n await this.registerService(healthyPort, pid);\n }\n return this.createEmptyStatus({\n running: true,\n port: healthyPort,\n pid,\n });\n }\n\n return this.createEmptyStatus({\n error: registration ? 'Registered server is unhealthy' : 'No HTTP server registered',\n });\n }\n}\n","import { spawn } from 'node:child_process';\nimport { randomBytes, randomUUID } from 'node:crypto';\nimport fs from 'node:fs/promises';\nimport path from 'node:path';\nimport { Command } from 'commander';\nimport { DEFAULT_HTTP_PORT } from '../constants/defaults.js';\nimport { HttpServerManager } from '../services/HttpServerManager.js';\nimport { ProfileStore } from '../services/ProfileStore.js';\n\nconst CLAUDE_BINARY = 'claude';\nconst DEFAULT_SCOPE = 'default';\nconst SESSIONS_DIRECTORY_NAME = '.claude-sessions';\nconst RANDOM_SCOPE_PREFIX = 'scope';\nconst RANDOM_SCOPE_BYTES = 3;\nconst TEXT_ENCODING: BufferEncoding = 'utf8';\nconst STDIO_INHERIT = 'inherit';\nconst DECIMAL_RADIX = 10;\nconst LOG_PREFIX = '[model-proxy-mcp]';\nconst RECOVERY_MESSAGE = `Recovery: verify HOME, proxy port, and that \\`${CLAUDE_BINARY}\\` is on PATH.`;\nconst ARG_RESUME = '--resume';\nconst ARG_SESSION_ID = '--session-id';\nconst ARG_SKIP_PERMISSIONS = '--dangerously-skip-permissions';\nconst MODEL_ALIAS_OPUS = 'ccproxy-opus';\nconst MODEL_ALIAS_SONNET = 'ccproxy-sonnet';\nconst MODEL_ALIAS_HAIKU = 'ccproxy-haiku';\nconst MODEL_ALIAS_SUBAGENT = 'ccproxy-subagent';\nconst DEFAULT_SCOPE_SEED_FILE = 'model-proxy-mcp.yaml';\nconst MODEL_PROXY_SCOPE_ENV = 'MODEL_PROXY_MCP_SCOPE';\nconst MODEL_PROXY_SLOT_ENV = 'MODEL_PROXY_MCP_SLOT';\nconst DEFAULT_MODEL_PROXY_SLOT = 'default';\n\nclass ClaudeCommandError extends Error {\n constructor(\n message: string,\n readonly code: string,\n options?: {\n cause?: unknown;\n },\n ) {\n super(message, options);\n this.name = 'ClaudeCommandError';\n }\n}\n\nclass ClaudeCommandConfigError extends ClaudeCommandError {\n constructor(message: string, options?: { cause?: unknown }) {\n super(message, 'CLAUDE_COMMAND_CONFIG_ERROR', options);\n this.name = 'ClaudeCommandConfigError';\n }\n}\n\nclass ClaudeCommandValidationError extends ClaudeCommandError {\n constructor(message: string, options?: { cause?: unknown }) {\n super(message, 'CLAUDE_COMMAND_VALIDATION_ERROR', options);\n this.name = 'ClaudeCommandValidationError';\n }\n}\n\nclass ClaudeCommandLaunchError extends ClaudeCommandError {\n constructor(message: string, options?: { cause?: unknown }) {\n super(message, 'CLAUDE_COMMAND_LAUNCH_ERROR', options);\n this.name = 'ClaudeCommandLaunchError';\n }\n}\n\nexport function sanitizeScope(scope: string): string {\n const sanitized = scope\n .trim()\n .replace(/[^a-zA-Z0-9._-]+/g, '-')\n .replace(/^-+|-+$/g, '');\n return sanitized || DEFAULT_SCOPE;\n}\n\nexport function generateScopeName(): string {\n return `${RANDOM_SCOPE_PREFIX}-${randomBytes(RANDOM_SCOPE_BYTES).toString('hex')}`;\n}\n\nexport function buildClaudeEnvironment(\n port: number,\n scope: string,\n baseEnvironment: NodeJS.ProcessEnv = process.env,\n): NodeJS.ProcessEnv {\n return {\n ...baseEnvironment,\n ANTHROPIC_BASE_URL: `http://127.0.0.1:${port}/scopes/${scope}`,\n ANTHROPIC_DEFAULT_OPUS_MODEL: baseEnvironment.ANTHROPIC_DEFAULT_OPUS_MODEL || MODEL_ALIAS_OPUS,\n ANTHROPIC_DEFAULT_SONNET_MODEL: baseEnvironment.ANTHROPIC_DEFAULT_SONNET_MODEL || MODEL_ALIAS_SONNET,\n ANTHROPIC_DEFAULT_HAIKU_MODEL: baseEnvironment.ANTHROPIC_DEFAULT_HAIKU_MODEL || MODEL_ALIAS_HAIKU,\n CLAUDE_CODE_SUBAGENT_MODEL: baseEnvironment.CLAUDE_CODE_SUBAGENT_MODEL || MODEL_ALIAS_SUBAGENT,\n MODEL_PROXY_MCP_SCOPE: baseEnvironment[MODEL_PROXY_SCOPE_ENV] || scope,\n MODEL_PROXY_MCP_SLOT: baseEnvironment[MODEL_PROXY_SLOT_ENV] || DEFAULT_MODEL_PROXY_SLOT,\n };\n}\n\nexport async function resolveScopeSeedConfigPath(\n configFile: string | undefined,\n workingDirectory = process.cwd(),\n): Promise<string | undefined> {\n const candidate = configFile\n ? path.resolve(workingDirectory, configFile)\n : path.join(workingDirectory, DEFAULT_SCOPE_SEED_FILE);\n\n try {\n const stats = await fs.stat(candidate);\n if (!stats.isFile()) {\n if (configFile) {\n throw new ClaudeCommandConfigError(`Scope seed config path is not a file: ${candidate}`);\n }\n return undefined;\n }\n return candidate;\n } catch (error) {\n const nodeError = error as NodeJS.ErrnoException;\n if (nodeError.code === 'ENOENT') {\n if (configFile) {\n throw new ClaudeCommandConfigError(`Scope seed config file not found: ${candidate}`, { cause: error });\n }\n return undefined;\n }\n if (error instanceof ClaudeCommandConfigError) {\n throw error;\n }\n throw new ClaudeCommandConfigError(`Failed to read scope seed config file: ${candidate}`, { cause: error });\n }\n}\n\nfunction getSessionsDirectory(): string {\n const homeDirectory = process.env.HOME || process.env.USERPROFILE;\n if (!homeDirectory) {\n throw new ClaudeCommandConfigError('HOME or USERPROFILE is not set');\n }\n\n return path.join(homeDirectory, SESSIONS_DIRECTORY_NAME);\n}\n\nasync function ensureSessionsDirectory(): Promise<string> {\n const sessionsDirectory = getSessionsDirectory();\n await fs.mkdir(sessionsDirectory, { recursive: true });\n return sessionsDirectory;\n}\n\nasync function readSessionId(sessionFile: string): Promise<string | null> {\n try {\n const sessionId = (await fs.readFile(sessionFile, TEXT_ENCODING)).trim();\n return sessionId || null;\n } catch (error) {\n const nodeError = error as NodeJS.ErrnoException;\n if (nodeError.code === 'ENOENT') {\n return null;\n }\n throw new ClaudeCommandConfigError(`Failed to read Claude session file: ${sessionFile}`, { cause: error });\n }\n}\n\nasync function resolveSessionId(scope: string, clearSession: boolean): Promise<{ sessionId: string; resume: boolean }> {\n const sessionsDirectory = await ensureSessionsDirectory();\n const sessionFile = path.join(sessionsDirectory, scope);\n\n if (clearSession) {\n try {\n await fs.rm(sessionFile, { force: true });\n } catch (error) {\n throw new ClaudeCommandConfigError(`Failed to clear Claude session file: ${sessionFile}`, { cause: error });\n }\n }\n\n const existingSessionId = await readSessionId(sessionFile);\n if (existingSessionId) {\n return { sessionId: existingSessionId, resume: true };\n }\n\n const sessionId = randomUUID().toLowerCase();\n try {\n await fs.writeFile(sessionFile, `${sessionId}\\n`, TEXT_ENCODING);\n } catch (error) {\n throw new ClaudeCommandConfigError(`Failed to write Claude session file: ${sessionFile}`, { cause: error });\n }\n return { sessionId, resume: false };\n}\n\nasync function launchClaude(\n scope: string,\n port: number,\n clearSession: boolean,\n claudeArgs: string[],\n configFile?: string,\n): Promise<number> {\n const scopeSeedConfigPath = await resolveScopeSeedConfigPath(configFile);\n const profileStore = new ProfileStore();\n await profileStore.ensureConfig(scope, scopeSeedConfigPath);\n\n const serverManager = new HttpServerManager();\n const status = await serverManager.ensureRunning(port);\n\n if (!status.running || !status.port) {\n throw new ClaudeCommandLaunchError(status.error || 'Failed to start model proxy HTTP server');\n }\n\n const { sessionId, resume } = await resolveSessionId(scope, clearSession);\n const env = buildClaudeEnvironment(status.port, scope);\n const args = [\n ARG_SKIP_PERMISSIONS,\n ...(resume ? [ARG_RESUME, sessionId] : [ARG_SESSION_ID, sessionId]),\n ...claudeArgs,\n ];\n\n console.log(`${LOG_PREFIX} Scope: ${scope}`);\n console.log(`${LOG_PREFIX} Proxy: ${env.ANTHROPIC_BASE_URL}`);\n console.log(`${LOG_PREFIX} Session: ${sessionId}${resume ? ' (resume)' : ' (new)'}`);\n if (scopeSeedConfigPath) {\n console.log(`${LOG_PREFIX} Scope config seed: ${scopeSeedConfigPath}`);\n }\n\n return new Promise<number>((resolve, reject) => {\n const child = spawn(CLAUDE_BINARY, args, {\n stdio: STDIO_INHERIT,\n env,\n });\n\n child.on('error', (error) => {\n reject(\n new ClaudeCommandLaunchError(`Failed to launch ${CLAUDE_BINARY}`, {\n cause: error,\n }),\n );\n });\n\n child.on('exit', (code, signal) => {\n if (signal) {\n reject(new ClaudeCommandLaunchError(`${CLAUDE_BINARY} exited with signal ${signal}`));\n return;\n }\n resolve(code ?? 0);\n });\n });\n}\n\nexport const claudeCommand = new Command('claude')\n .description('Launch Claude Code through the model proxy')\n .option('-s, --scope <scope>', 'Proxy scope to use')\n .option('-p, --port <port>', 'Preferred HTTP port for the proxy', String(DEFAULT_HTTP_PORT))\n .option('-c, --config-file <path>', 'Seed a new scope from the specified scope config file')\n .option('--config <path>', 'Alias for --config-file')\n .option('--clear-session', 'Start with a fresh Claude session for the selected scope', false)\n .allowUnknownOption(true)\n .allowExcessArguments(true)\n .argument('[claudeArgs...]')\n .action(\n async (\n claudeArgs: string[],\n options: { scope?: string; port: string; clearSession: boolean; configFile?: string; config?: string },\n ) => {\n try {\n const resolvedScope = sanitizeScope(options.scope || generateScopeName());\n const port = Number.parseInt(options.port, DECIMAL_RADIX);\n if (!Number.isInteger(port) || port <= 0) {\n throw new ClaudeCommandValidationError(`Invalid port: ${options.port}`);\n }\n\n const exitCode = await launchClaude(\n resolvedScope,\n port,\n options.clearSession,\n claudeArgs,\n options.configFile ?? options.config,\n );\n process.exit(exitCode);\n } catch (error) {\n const commandError =\n error instanceof ClaudeCommandError\n ? error\n : new ClaudeCommandLaunchError('Failed to launch Claude', { cause: error });\n console.error(`${LOG_PREFIX} ${commandError.code}: ${commandError.message}`);\n console.error(`${LOG_PREFIX} ${RECOVERY_MESSAGE}`);\n if (commandError.cause) {\n console.error(`${LOG_PREFIX} Cause:`, commandError.cause);\n }\n process.exit(1);\n }\n },\n );\n","import { serve } from '@hono/node-server';\nimport { Command } from 'commander';\nimport { DEFAULT_HTTP_PORT } from '../constants/defaults.js';\nimport { createHttpServer } from '../server/http.js';\nimport { GatewayService } from '../services/GatewayService.js';\n\nconst LOCAL_HOST = '127.0.0.1';\ntype HttpServeOptions = { port: string };\n\nexport const httpServeCommand = new Command('http-serve')\n .description('Start the Claude-compatible HTTP model proxy server')\n .option('-p, --port <port>', 'Port to listen on', String(DEFAULT_HTTP_PORT))\n .action(async (options: HttpServeOptions) => {\n try {\n const port = Number.parseInt(options.port, 10);\n if (!Number.isInteger(port) || port <= 0) {\n throw new Error(`Invalid port: ${options.port}`);\n }\n\n const gatewayService = new GatewayService();\n await gatewayService.ensureConfig();\n const app = createHttpServer(gatewayService);\n const server = serve({ fetch: app.fetch, port, hostname: LOCAL_HOST });\n\n console.log(`model-proxy-mcp listening on http://${LOCAL_HOST}:${port}`);\n console.log(`Claude base URL: http://${LOCAL_HOST}:${port}`);\n\n const shutdown = (signal: string) => {\n console.log(`\\n${signal} received. Shutting down...`);\n server.close();\n process.exit(0);\n };\n\n process.once('SIGINT', () => shutdown('SIGINT'));\n process.once('SIGTERM', () => shutdown('SIGTERM'));\n } catch (error) {\n console.error('Failed to start HTTP server:', error);\n process.exit(1);\n }\n });\n","import { Command } from 'commander';\nimport { DEFAULT_HTTP_PORT } from '../constants/defaults.js';\nimport { createServer } from '../server/index.js';\nimport { GatewayService } from '../services/GatewayService.js';\nimport { HttpServerManager } from '../services/HttpServerManager.js';\nimport { StdioTransportHandler } from '../transports/stdio.js';\n\nconst DEFAULT_SCOPE = 'default';\nconst MODEL_PROXY_SCOPE_ENV = 'MODEL_PROXY_MCP_SCOPE';\n\ntype McpServeOptions = { cleanup: boolean; port: string };\n\nexport const mcpServeCommand = new Command('mcp-serve')\n .description('Start MCP server with stdio transport')\n .option('--cleanup', 'Stop HTTP server on shutdown', false)\n .option('-p, --port <port>', 'Port for HTTP server', String(DEFAULT_HTTP_PORT))\n .action(async (options: McpServeOptions) => {\n try {\n const port = Number.parseInt(options.port, 10);\n if (!Number.isInteger(port) || port <= 0) {\n throw new Error(`Invalid port: ${options.port}`);\n }\n\n const gatewayService = new GatewayService();\n await gatewayService.ensureConfig(process.env[MODEL_PROXY_SCOPE_ENV] || DEFAULT_SCOPE);\n\n const manager = new HttpServerManager();\n const httpStatus = await manager.ensureRunning(port);\n if (!httpStatus.running) {\n console.error(`Warning: HTTP server failed to start: ${httpStatus.error}`);\n }\n\n const handler = new StdioTransportHandler(createServer(gatewayService));\n\n const shutdown = async (signal: string) => {\n console.error(`\\nReceived ${signal}, shutting down gracefully...`);\n await handler.stop();\n if (options.cleanup && httpStatus.running) {\n await manager.stop();\n }\n process.exit(0);\n };\n\n process.once(\n 'SIGINT',\n () =>\n void shutdown('SIGINT').catch((error) => {\n console.error('Failed to shut down after SIGINT:', error);\n process.exit(1);\n }),\n );\n process.once(\n 'SIGTERM',\n () =>\n void shutdown('SIGTERM').catch((error) => {\n console.error('Failed to shut down after SIGTERM:', error);\n process.exit(1);\n }),\n );\n\n await handler.start();\n } catch (error) {\n console.error('Failed to start MCP server:', error);\n process.exit(1);\n }\n });\n","import { Command } from 'commander';\nimport { DEFAULT_HTTP_PORT } from '../constants/defaults.js';\nimport { createServer } from '../server/index.js';\nimport { HttpServerManager } from '../services/HttpServerManager.js';\nimport { StdioTransportHandler } from '../transports/stdio.js';\n\nexport const startCommand = new Command('start')\n .description('Start HTTP and/or MCP server for the model proxy')\n .option('--mcp-only', 'Start only the MCP server', false)\n .option('--http-only', 'Start only the HTTP server', false)\n .option('-p, --port <port>', 'Port for HTTP server', String(DEFAULT_HTTP_PORT))\n .action(async (options) => {\n try {\n const httpServerManager = new HttpServerManager();\n const port = Number.parseInt(options.port, 10);\n const startHttp = !options.mcpOnly;\n const startMcp = !options.httpOnly;\n\n if (startHttp) {\n const status = await httpServerManager.ensureRunning(port);\n if (!status.running) {\n console.error(`HTTP server failed to start: ${status.error}`);\n process.exit(1);\n }\n console.log(`HTTP server running on http://127.0.0.1:${status.port}`);\n }\n\n if (startMcp) {\n const handler = new StdioTransportHandler(createServer());\n await handler.start();\n const shutdown = async (signal: string) => {\n console.error(`\\n${signal} received. Shutting down...`);\n await handler.stop();\n process.exit(0);\n };\n process.on('SIGINT', () => shutdown('SIGINT'));\n process.on('SIGTERM', () => shutdown('SIGTERM'));\n } else {\n process.exit(0);\n }\n } catch (error) {\n console.error('Failed to start services:', error);\n process.exit(1);\n }\n });\n","import { Command } from 'commander';\nimport { GatewayService } from '../services/GatewayService.js';\nimport { HttpServerManager } from '../services/HttpServerManager.js';\nimport { consoleLogger } from '../services/logger.js';\n\nconst DEFAULT_SCOPE = 'default';\nconst STATUS_COMMAND_NAME = 'status';\nconst STATUS_COMMAND_DESCRIPTION = 'Show proxy server and profile status';\nconst STATUS_ERROR_MESSAGE = 'Failed to read status';\n\nclass StatusCommandError extends Error {\n readonly code = 'STATUS_CHECK_FAILED';\n\n constructor(cause: unknown) {\n super(STATUS_ERROR_MESSAGE, { cause: cause instanceof Error ? cause : new Error(String(cause)) });\n this.name = 'StatusCommandError';\n }\n}\n\nexport const statusCommand = new Command(STATUS_COMMAND_NAME)\n .description(STATUS_COMMAND_DESCRIPTION)\n .option('-s, --scope <scope>', 'Configuration scope', DEFAULT_SCOPE)\n .action(async (options: { scope: string }) => {\n try {\n const manager = new HttpServerManager();\n const serverStatus = await manager.getStatus();\n const gateway = new GatewayService();\n const status = await gateway.getStatus(options.scope, serverStatus.port, serverStatus.pid);\n status.running = serverStatus.running;\n status.error = serverStatus.error;\n consoleLogger.info(JSON.stringify(status, null, 2));\n process.exit(0);\n } catch (error) {\n const statusError = new StatusCommandError(error);\n consoleLogger.error(STATUS_ERROR_MESSAGE, {\n command: STATUS_COMMAND_NAME,\n code: statusError.code,\n scope: options.scope,\n cause: statusError.cause,\n });\n process.exit(1);\n }\n });\n","import { Command } from 'commander';\nimport { HttpServerManager } from '../services/HttpServerManager.js';\n\nexport const stopCommand = new Command('stop').description('Stop the background HTTP server').action(async () => {\n try {\n const manager = new HttpServerManager();\n const stopped = await manager.stop();\n console.log(stopped ? 'HTTP server stopped' : 'No HTTP server running');\n process.exit(0);\n } catch (error) {\n console.error('Failed to stop HTTP server:', error);\n process.exit(1);\n }\n});\n","#!/usr/bin/env node\nimport { readFileSync } from 'node:fs';\nimport { dirname, join } from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport { Command } from 'commander';\nimport { claudeCommand } from './commands/claude.js';\nimport { httpServeCommand } from './commands/http-serve.js';\nimport { mcpServeCommand } from './commands/mcp-serve.js';\nimport { startCommand } from './commands/start.js';\nimport { statusCommand } from './commands/status.js';\nimport { stopCommand } from './commands/stop.js';\n\nconst __dirname = dirname(fileURLToPath(import.meta.url));\nconst packageJson = JSON.parse(readFileSync(join(__dirname, '../package.json'), 'utf-8'));\n\nasync function main() {\n const program = new Command();\n program.name('model-proxy-mcp').description('Claude-compatible model proxy MCP').version(packageJson.version);\n program.addCommand(startCommand);\n program.addCommand(stopCommand);\n program.addCommand(statusCommand);\n program.addCommand(claudeCommand);\n program.addCommand(httpServeCommand);\n program.addCommand(mcpServeCommand);\n await program.parseAsync(process.argv);\n}\n\nmain();\n"],"mappings":";8UAMA,IAAa,EAAb,KAAmC,CACjC,MAAM,MAAM,EAA0C,CACpD,GAAI,CACF,IAAM,EAAW,MAAM,MAAM,oBAAoB,EAAK,SAAS,CAM/D,OALK,EAAS,GAKP,CACL,QAAS,GACT,aAHe,MAAM,EAAS,MAAM,EAGf,QACtB,CAPQ,CAAE,QAAS,GAAO,MAAO,mCAAmC,EAAS,SAAU,OAQjF,EAAO,CACd,MAAO,CACL,QAAS,GACT,MAAO,aAAiB,MAAQ,EAAM,QAAU,OAAO,EAAM,CAC9D,ICZP,MAAM,EAAoB,CAAC,sBAAuB,UAAW,OAAO,CAC9D,EAAkB,GAClB,EAAe,KACf,EAAsB,0BACtB,EAA4B,IAC5B,EAAgC,IAChC,GAA2B,IAC3BA,GAAiC,UACjCC,GAAiC,UACjC,GAAoB,uBACpBC,EAAsC,OACtC,GAAA,EAAA,EAAA,WAA0BC,EAAAA,SAAS,CAEzC,SAAS,EAAqB,EAAY,QAAQ,KAAK,CAAU,CAC/D,IAAI,EAAaC,EAAAA,QAAK,QAAQ,EAAU,CAExC,OAAa,CACX,IAAK,IAAM,KAAU,EACnB,IAAA,EAAA,EAAA,Y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W,EAAa,CAChC,EAAQ,WAAW,EAAY,CAC/B,EAAQ,WAAW,EAAc,CACjC,EAAQ,WAAW,EAAc,CACjC,EAAQ,WAAW,EAAiB,CACpC,EAAQ,WAAW,EAAgB,CACnC,MAAM,EAAQ,WAAW,QAAQ,KAAK,CAGxC,GAAM"}
package/dist/cli.mjs CHANGED
@@ -1,3 +1,4 @@
1
1
  #!/usr/bin/env node
2
2
  import{c as e,i as t,l as n,n as r,o as i,r as a,s as o,t as s}from"./stdio-BxbGlLys.mjs";import{existsSync as c,readFileSync as l}from"node:fs";import u,{dirname as d,join as f}from"node:path";import{fileURLToPath as p}from"node:url";import{Command as m}from"commander";import{execFile as h,spawn as g}from"node:child_process";import{randomBytes as _,randomUUID as v}from"node:crypto";import y from"node:fs/promises";import b from"node:os";import{promisify as x}from"node:util";import{serve as S}from"@hono/node-server";var C=class{async check(e){try{let t=await fetch(`http://127.0.0.1:${e}/health`);return t.ok?{healthy:!0,serviceName:(await t.json()).service}:{healthy:!1,error:`Health check failed with status ${t.status}`}}catch(e){return{healthy:!1,error:e instanceof Error?e.message:String(e)}}}};const w=[`pnpm-workspace.yaml`,`nx.json`,`.git`],T=`utf8`,E=x(h);function D(e=process.cwd()){let t=u.resolve(e);for(;;){for(let e of w)if(c(u.join(t,e)))return t;let e=u.dirname(t);if(e===t)return process.cwd();t=e}}var O=class{repositoryPath=D(process.cwd());serviceName=n;stateFilePath=u.join(b.tmpdir(),`model-proxy-mcp-http-${Buffer.from(D(process.cwd())).toString(`hex`)}.json`);constructor(e=new C){this.healthCheck=e}createEmptyStatus(e={}){return{running:!1,scope:`default`,activeProfileId:null,auth:{configured:!1,authFilePath:``},profiles:[],slotModels:{},...e}}async getRegistration(){try{let e=await y.readFile(this.stateFilePath,T),t=JSON.parse(e);return t.port?t:null}catch(e){if(e.code===`ENOENT`)return null;throw e}}async releaseService(e){let t=await this.getRegistration();e&&t?.pid&&t.pid!==e||await y.rm(this.stateFilePath,{force:!0})}async listListeningProcesses(e){try{let{stdout:t}=await E(`sh`,[`-lc`,`lsof -n -P -sTCP:LISTEN -iTCP:${e} || true`],{encoding:`utf8`});return t.split(`
3
- `).slice(1).map(e=>e.trim()).filter(Boolean).map(e=>e.split(/\s+/)).map(t=>({pid:Number.parseInt(t[1]||``,10),port:e})).filter(e=>Number.isInteger(e.pid)&&e.pid>0)}catch{return[]}}async findHealthyServicePort(e){for(let t=0;t<=25;t+=1){let n=e+t,r=await this.healthCheck.check(n);if(r.healthy&&r.serviceName===this.serviceName)return n}return null}async clearPortListeners(e,t){let n=await this.listListeningProcesses(e);for(let e of n)t&&e.pid===t||await this.killProcess(e.pid)}async registerService(e,t){await y.writeFile(this.stateFilePath,JSON.stringify({port:e,pid:t}),T)}async findAvailablePort(t){let n=Math.min(e,t),r=Math.max(e+1e3,t);for(let e=t;e<=r;e+=1)if((await this.listListeningProcesses(e)).length===0)return e;for(let e=n;e<t;e+=1)if((await this.listListeningProcesses(e)).length===0)return e;throw Error(`No available port found from ${t}`)}async fileExists(e){try{return await y.access(e),!0}catch{return!1}}async resolveCliPath(){let e=u.dirname(p(import.meta.url)),t=u.resolve(e,`cli.mjs`);if(await this.fileExists(t))return{cliPath:t,runtime:`node`};let n=u.resolve(e,`..`,`cli.mjs`);if(await this.fileExists(n))return{cliPath:n,runtime:`node`};let r=u.join(this.repositoryPath,`packages`,`mcp`,`model-proxy-mcp`,`dist`,`cli.mjs`);if(await this.fileExists(r))return{cliPath:r,runtime:`node`};for(let t of[u.resolve(e,`cli.ts`),u.resolve(e,`..`,`cli.ts`)])if(await this.fileExists(t))return{cliPath:t,runtime:`bun`};let i=u.join(this.repositoryPath,`packages`,`mcp`,`model-proxy-mcp`,`src`,`cli.ts`);if(await this.fileExists(i))return{cliPath:i,runtime:`bun`};throw Error(`Cannot find model-proxy-mcp CLI`)}async startHttpServer(e){let{cliPath:t,runtime:n}=await this.resolveCliPath(),r=g(n===`bun`?process.versions.bun?process.execPath:`bun`:`node`,n===`bun`?[`run`,t,`http-serve`,`--port`,String(e)]:[t,`http-serve`,`--port`,String(e)],{detached:!0,stdio:`ignore`,env:{...process.env,NODE_ENV:process.env.NODE_ENV||`development`}});if(r.unref(),!r.pid)throw Error(`Failed to spawn HTTP server`);return r.pid}async killProcess(e){try{process.kill(e,`SIGTERM`),await new Promise(e=>setTimeout(e,1e3));try{process.kill(e,0),process.kill(e,`SIGKILL`)}catch{return}}catch{return}}async waitForHealthy(e){let t=Date.now()+5e3,n=`Server failed health check after startup`;for(;Date.now()<t;){let t=await this.healthCheck.check(e);if(t.healthy&&t.serviceName===this.serviceName)return{healthy:!0};n=t.error||`Unexpected service on port ${e}`,await new Promise(e=>setTimeout(e,250))}return{healthy:!1,error:n}}async ensureRunning(t=e){try{let e=await this.findHealthyServicePort(t);if(e!==null){let t=(await this.listListeningProcesses(e))[0]?.pid;return await this.registerService(e,t??process.pid),this.createEmptyStatus({running:!0,port:e,pid:t})}let n=await this.getRegistration();if(n){let e=await this.healthCheck.check(n.port);if(e.healthy&&e.serviceName===this.serviceName)return this.createEmptyStatus({running:!0,port:n.port,pid:n.pid});n.pid&&await this.killProcess(n.pid),await this.clearPortListeners(n.port,n.pid),await this.releaseService(n.pid)}await this.clearPortListeners(t);let r=await this.findAvailablePort(t),i=await this.startHttpServer(r),a=await this.waitForHealthy(r);return a.healthy?(await this.registerService(r,i),this.createEmptyStatus({running:!0,port:r,pid:i})):(await this.killProcess(i),await this.clearPortListeners(r,i),await this.releaseService(i),this.createEmptyStatus({error:a.error||`Server failed health check after startup`}))}catch(e){return this.createEmptyStatus({error:e instanceof Error?e.message:String(e)})}}async stop(){let t=await this.getRegistration(),n=!1;t?.pid&&(await this.killProcess(t.pid),n=!0),t&&(await this.clearPortListeners(t.port,t.pid),await this.releaseService(t.pid));let r=await this.findHealthyServicePort(e);if(r!==null){let e=await this.listListeningProcesses(r);for(let t of e)await this.killProcess(t.pid),n=!0;await this.releaseService()}return n}async getStatus(){let t=await this.getRegistration();if(t&&(await this.healthCheck.check(t.port)).healthy)return this.createEmptyStatus({running:!0,port:t.port,pid:t.pid});let n=await this.findHealthyServicePort(e);if(n!==null){let e=(await this.listListeningProcesses(n))[0]?.pid;return e&&await this.registerService(n,e),this.createEmptyStatus({running:!0,port:n,pid:e})}return this.createEmptyStatus({error:t?`Registered server is unhealthy`:`No HTTP server registered`})}};const k=`claude`,A=`utf8`,j=`[model-proxy-mcp]`,M=`Recovery: verify HOME, proxy port, and that \`${k}\` is on PATH.`;var N=class extends Error{constructor(e,t,n){super(e,n),this.code=t,this.name=`ClaudeCommandError`}},P=class extends N{constructor(e,t){super(e,`CLAUDE_COMMAND_CONFIG_ERROR`,t),this.name=`ClaudeCommandConfigError`}},F=class extends N{constructor(e,t){super(e,`CLAUDE_COMMAND_VALIDATION_ERROR`,t),this.name=`ClaudeCommandValidationError`}},I=class extends N{constructor(e,t){super(e,`CLAUDE_COMMAND_LAUNCH_ERROR`,t),this.name=`ClaudeCommandLaunchError`}};function L(e){return e.trim().replace(/[^a-zA-Z0-9._-]+/g,`-`).replace(/^-+|-+$/g,``)||`default`}function R(){return`scope-${_(3).toString(`hex`)}`}function z(e,t,n=process.env){return{...n,ANTHROPIC_BASE_URL:`http://127.0.0.1:${e}/scopes/${t}`,ANTHROPIC_DEFAULT_OPUS_MODEL:n.ANTHROPIC_DEFAULT_OPUS_MODEL||`ccproxy-opus`,ANTHROPIC_DEFAULT_SONNET_MODEL:n.ANTHROPIC_DEFAULT_SONNET_MODEL||`ccproxy-sonnet`,ANTHROPIC_DEFAULT_HAIKU_MODEL:n.ANTHROPIC_DEFAULT_HAIKU_MODEL||`ccproxy-haiku`,CLAUDE_CODE_SUBAGENT_MODEL:n.CLAUDE_CODE_SUBAGENT_MODEL||`ccproxy-subagent`,MODEL_PROXY_MCP_SCOPE:n.MODEL_PROXY_MCP_SCOPE||t,MODEL_PROXY_MCP_SLOT:n.MODEL_PROXY_MCP_SLOT||`default`}}async function B(e,t=process.cwd()){let n=e?u.resolve(t,e):u.join(t,`model-proxy-mcp.yaml`);try{if(!(await y.stat(n)).isFile()){if(e)throw new P(`Scope seed config path is not a file: ${n}`);return}return n}catch(t){if(t.code===`ENOENT`){if(e)throw new P(`Scope seed config file not found: ${n}`,{cause:t});return}throw t instanceof P?t:new P(`Failed to read scope seed config file: ${n}`,{cause:t})}}function V(){let e=process.env.HOME||process.env.USERPROFILE;if(!e)throw new P(`HOME or USERPROFILE is not set`);return u.join(e,`.claude-sessions`)}async function H(){let e=V();return await y.mkdir(e,{recursive:!0}),e}async function U(e){try{return(await y.readFile(e,A)).trim()||null}catch(t){if(t.code===`ENOENT`)return null;throw new P(`Failed to read Claude session file: ${e}`,{cause:t})}}async function W(e,t){let n=await H(),r=u.join(n,e);if(t)try{await y.rm(r,{force:!0})}catch(e){throw new P(`Failed to clear Claude session file: ${r}`,{cause:e})}let i=await U(r);if(i)return{sessionId:i,resume:!0};let a=v().toLowerCase();try{await y.writeFile(r,`${a}\n`,A)}catch(e){throw new P(`Failed to write Claude session file: ${r}`,{cause:e})}return{sessionId:a,resume:!1}}async function G(e,t,n,r,a){let o=await B(a);await new i().ensureConfig(e,o);let s=await new O().ensureRunning(t);if(!s.running||!s.port)throw new I(s.error||`Failed to start model proxy HTTP server`);let{sessionId:c,resume:l}=await W(e,n),u=z(s.port,e),d=[`--dangerously-skip-permissions`,...l?[`--resume`,c]:[`--session-id`,c],...r];return console.log(`${j} Scope: ${e}`),console.log(`${j} Proxy: ${u.ANTHROPIC_BASE_URL}`),console.log(`${j} Session: ${c}${l?` (resume)`:` (new)`}`),o&&console.log(`${j} Scope config seed: ${o}`),new Promise((e,t)=>{let n=g(k,d,{stdio:`inherit`,env:u});n.on(`error`,e=>{t(new I(`Failed to launch ${k}`,{cause:e}))}),n.on(`exit`,(n,r)=>{if(r){t(new I(`${k} exited with signal ${r}`));return}e(n??0)})})}const K=new m(`claude`).description(`Launch Claude Code through the model proxy`).option(`-s, --scope <scope>`,`Proxy scope to use`).option(`-p, --port <port>`,`Preferred HTTP port for the proxy`,String(e)).option(`-c, --config-file <path>`,`Seed a new scope from the specified scope config file`).option(`--config <path>`,`Alias for --config-file`).option(`--clear-session`,`Start with a fresh Claude session for the selected scope`,!1).allowUnknownOption(!0).allowExcessArguments(!0).argument(`[claudeArgs...]`).action(async(e,t)=>{try{let n=L(t.scope||R()),r=Number.parseInt(t.port,10);if(!Number.isInteger(r)||r<=0)throw new F(`Invalid port: ${t.port}`);let i=await G(n,r,t.clearSession,e,t.configFile??t.config);process.exit(i)}catch(e){let t=e instanceof N?e:new I(`Failed to launch Claude`,{cause:e});console.error(`${j} ${t.code}: ${t.message}`),console.error(`${j} ${M}`),t.cause&&console.error(`${j} Cause:`,t.cause),process.exit(1)}}),q=`127.0.0.1`,J=new m(`http-serve`).description(`Start the Claude-compatible HTTP model proxy server`).option(`-p, --port <port>`,`Port to listen on`,String(e)).action(async e=>{try{let n=Number.parseInt(e.port,10);if(!Number.isInteger(n)||n<=0)throw Error(`Invalid port: ${e.port}`);let r=new t;await r.ensureConfig();let i=S({fetch:a(r).fetch,port:n,hostname:q});console.log(`model-proxy-mcp listening on http://${q}:${n}`),console.log(`Claude base URL: http://${q}:${n}`);let o=e=>{console.log(`\n${e} received. Shutting down...`),i.close(),process.exit(0)};process.once(`SIGINT`,()=>o(`SIGINT`)),process.once(`SIGTERM`,()=>o(`SIGTERM`))}catch(e){console.error(`Failed to start HTTP server:`,e),process.exit(1)}}),Y=new m(`mcp-serve`).description(`Start MCP server with stdio transport`).option(`--cleanup`,`Stop HTTP server on shutdown`,!1).option(`-p, --port <port>`,`Port for HTTP server`,String(e)).action(async e=>{try{let n=Number.parseInt(e.port,10);if(!Number.isInteger(n)||n<=0)throw Error(`Invalid port: ${e.port}`);let i=new t;await i.ensureConfig(process.env.MODEL_PROXY_MCP_SCOPE||`default`);let a=new O,o=await a.ensureRunning(n);o.running||console.error(`Warning: HTTP server failed to start: ${o.error}`);let c=new s(r(i)),l=async t=>{console.error(`\nReceived ${t}, shutting down gracefully...`),await c.stop(),e.cleanup&&o.running&&await a.stop(),process.exit(0)};process.once(`SIGINT`,()=>void l(`SIGINT`).catch(e=>{console.error(`Failed to shut down after SIGINT:`,e),process.exit(1)})),process.once(`SIGTERM`,()=>void l(`SIGTERM`).catch(e=>{console.error(`Failed to shut down after SIGTERM:`,e),process.exit(1)})),await c.start()}catch(e){console.error(`Failed to start MCP server:`,e),process.exit(1)}}),X=new m(`start`).description(`Start HTTP and/or MCP server for the model proxy`).option(`--mcp-only`,`Start only the MCP server`,!1).option(`--http-only`,`Start only the HTTP server`,!1).option(`-p, --port <port>`,`Port for HTTP server`,String(e)).action(async e=>{try{let t=new O,n=Number.parseInt(e.port,10),i=!e.mcpOnly,a=!e.httpOnly;if(i){let e=await t.ensureRunning(n);e.running||(console.error(`HTTP server failed to start: ${e.error}`),process.exit(1)),console.log(`HTTP server running on http://127.0.0.1:${e.port}`)}if(a){let e=new s(r());await e.start();let t=async t=>{console.error(`\n${t} received. Shutting down...`),await e.stop(),process.exit(0)};process.on(`SIGINT`,()=>t(`SIGINT`)),process.on(`SIGTERM`,()=>t(`SIGTERM`))}else process.exit(0)}catch(e){console.error(`Failed to start services:`,e),process.exit(1)}}),Z=`status`,Q=`Failed to read status`;var $=class extends Error{code=`STATUS_CHECK_FAILED`;constructor(e){super(Q,{cause:e instanceof Error?e:Error(String(e))}),this.name=`StatusCommandError`}};const ee=new m(Z).description(`Show proxy server and profile status`).option(`-s, --scope <scope>`,`Configuration scope`,`default`).action(async e=>{try{let n=await new O().getStatus(),r=await new t().getStatus(e.scope,n.port,n.pid);r.running=n.running,r.error=n.error,o.info(JSON.stringify(r,null,2)),process.exit(0)}catch(t){let n=new $(t);o.error(Q,{command:Z,code:n.code,scope:e.scope,cause:n.cause}),process.exit(1)}}),te=new m(`stop`).description(`Stop the background HTTP server`).action(async()=>{try{let e=await new O().stop();console.log(e?`HTTP server stopped`:`No HTTP server running`),process.exit(0)}catch(e){console.error(`Failed to stop HTTP server:`,e),process.exit(1)}}),ne=d(p(import.meta.url)),re=JSON.parse(l(f(ne,`../package.json`),`utf-8`));async function ie(){let e=new m;e.name(`model-proxy-mcp`).description(`Claude-compatible model proxy MCP`).version(re.version),e.addCommand(X),e.addCommand(te),e.addCommand(ee),e.addCommand(K),e.addCommand(J),e.addCommand(Y),await e.parseAsync(process.argv)}ie();export{};
3
+ `).slice(1).map(e=>e.trim()).filter(Boolean).map(e=>e.split(/\s+/)).map(t=>({pid:Number.parseInt(t[1]||``,10),port:e})).filter(e=>Number.isInteger(e.pid)&&e.pid>0)}catch{return[]}}async findHealthyServicePort(e){for(let t=0;t<=25;t+=1){let n=e+t,r=await this.healthCheck.check(n);if(r.healthy&&r.serviceName===this.serviceName)return n}return null}async clearPortListeners(e,t){let n=await this.listListeningProcesses(e);for(let e of n)t&&e.pid===t||await this.killProcess(e.pid)}async registerService(e,t){await y.writeFile(this.stateFilePath,JSON.stringify({port:e,pid:t}),T)}async findAvailablePort(t){let n=Math.min(e,t),r=Math.max(e+1e3,t);for(let e=t;e<=r;e+=1)if((await this.listListeningProcesses(e)).length===0)return e;for(let e=n;e<t;e+=1)if((await this.listListeningProcesses(e)).length===0)return e;throw Error(`No available port found from ${t}`)}async fileExists(e){try{return await y.access(e),!0}catch{return!1}}async resolveCliPath(){let e=u.dirname(p(import.meta.url)),t=u.resolve(e,`cli.mjs`);if(await this.fileExists(t))return{cliPath:t,runtime:`node`};let n=u.resolve(e,`..`,`cli.mjs`);if(await this.fileExists(n))return{cliPath:n,runtime:`node`};let r=u.join(this.repositoryPath,`packages`,`mcp`,`model-proxy-mcp`,`dist`,`cli.mjs`);if(await this.fileExists(r))return{cliPath:r,runtime:`node`};for(let t of[u.resolve(e,`cli.ts`),u.resolve(e,`..`,`cli.ts`)])if(await this.fileExists(t))return{cliPath:t,runtime:`bun`};let i=u.join(this.repositoryPath,`packages`,`mcp`,`model-proxy-mcp`,`src`,`cli.ts`);if(await this.fileExists(i))return{cliPath:i,runtime:`bun`};throw Error(`Cannot find model-proxy-mcp CLI`)}async startHttpServer(e){let{cliPath:t,runtime:n}=await this.resolveCliPath(),r=g(n===`bun`?process.versions.bun?process.execPath:`bun`:`node`,n===`bun`?[`run`,t,`http-serve`,`--port`,String(e)]:[t,`http-serve`,`--port`,String(e)],{detached:!0,stdio:`ignore`,env:{...process.env,NODE_ENV:process.env.NODE_ENV||`development`}});if(r.unref(),!r.pid)throw Error(`Failed to spawn HTTP server`);return r.pid}async killProcess(e){try{process.kill(e,`SIGTERM`),await new Promise(e=>setTimeout(e,1e3));try{process.kill(e,0),process.kill(e,`SIGKILL`)}catch{return}}catch{return}}async waitForHealthy(e){let t=Date.now()+5e3,n=`Server failed health check after startup`;for(;Date.now()<t;){let t=await this.healthCheck.check(e);if(t.healthy&&t.serviceName===this.serviceName)return{healthy:!0};n=t.error||`Unexpected service on port ${e}`,await new Promise(e=>setTimeout(e,250))}return{healthy:!1,error:n}}async ensureRunning(t=e){try{let e=await this.findHealthyServicePort(t);if(e!==null){let t=(await this.listListeningProcesses(e))[0]?.pid;return await this.registerService(e,t??process.pid),this.createEmptyStatus({running:!0,port:e,pid:t})}let n=await this.getRegistration();if(n){let e=await this.healthCheck.check(n.port);if(e.healthy&&e.serviceName===this.serviceName)return this.createEmptyStatus({running:!0,port:n.port,pid:n.pid});n.pid&&await this.killProcess(n.pid),await this.clearPortListeners(n.port,n.pid),await this.releaseService(n.pid)}await this.clearPortListeners(t);let r=await this.findAvailablePort(t),i=await this.startHttpServer(r),a=await this.waitForHealthy(r);return a.healthy?(await this.registerService(r,i),this.createEmptyStatus({running:!0,port:r,pid:i})):(await this.killProcess(i),await this.clearPortListeners(r,i),await this.releaseService(i),this.createEmptyStatus({error:a.error||`Server failed health check after startup`}))}catch(e){return this.createEmptyStatus({error:e instanceof Error?e.message:String(e)})}}async stop(){let t=await this.getRegistration(),n=!1;t?.pid&&(await this.killProcess(t.pid),n=!0),t&&(await this.clearPortListeners(t.port,t.pid),await this.releaseService(t.pid));let r=await this.findHealthyServicePort(e);if(r!==null){let e=await this.listListeningProcesses(r);for(let t of e)await this.killProcess(t.pid),n=!0;await this.releaseService()}return n}async getStatus(){let t=await this.getRegistration();if(t&&(await this.healthCheck.check(t.port)).healthy)return this.createEmptyStatus({running:!0,port:t.port,pid:t.pid});let n=await this.findHealthyServicePort(e);if(n!==null){let e=(await this.listListeningProcesses(n))[0]?.pid;return e&&await this.registerService(n,e),this.createEmptyStatus({running:!0,port:n,pid:e})}return this.createEmptyStatus({error:t?`Registered server is unhealthy`:`No HTTP server registered`})}};const k=`claude`,A=`utf8`,j=`[model-proxy-mcp]`,M=`Recovery: verify HOME, proxy port, and that \`${k}\` is on PATH.`;var N=class extends Error{constructor(e,t,n){super(e,n),this.code=t,this.name=`ClaudeCommandError`}},P=class extends N{constructor(e,t){super(e,`CLAUDE_COMMAND_CONFIG_ERROR`,t),this.name=`ClaudeCommandConfigError`}},F=class extends N{constructor(e,t){super(e,`CLAUDE_COMMAND_VALIDATION_ERROR`,t),this.name=`ClaudeCommandValidationError`}},I=class extends N{constructor(e,t){super(e,`CLAUDE_COMMAND_LAUNCH_ERROR`,t),this.name=`ClaudeCommandLaunchError`}};function L(e){return e.trim().replace(/[^a-zA-Z0-9._-]+/g,`-`).replace(/^-+|-+$/g,``)||`default`}function R(){return`scope-${_(3).toString(`hex`)}`}function z(e,t,n=process.env){return{...n,ANTHROPIC_BASE_URL:`http://127.0.0.1:${e}/scopes/${t}`,ANTHROPIC_DEFAULT_OPUS_MODEL:n.ANTHROPIC_DEFAULT_OPUS_MODEL||`ccproxy-opus`,ANTHROPIC_DEFAULT_SONNET_MODEL:n.ANTHROPIC_DEFAULT_SONNET_MODEL||`ccproxy-sonnet`,ANTHROPIC_DEFAULT_HAIKU_MODEL:n.ANTHROPIC_DEFAULT_HAIKU_MODEL||`ccproxy-haiku`,CLAUDE_CODE_SUBAGENT_MODEL:n.CLAUDE_CODE_SUBAGENT_MODEL||`ccproxy-subagent`,MODEL_PROXY_MCP_SCOPE:n.MODEL_PROXY_MCP_SCOPE||t,MODEL_PROXY_MCP_SLOT:n.MODEL_PROXY_MCP_SLOT||`default`}}async function B(e,t=process.cwd()){let n=e?u.resolve(t,e):u.join(t,`model-proxy-mcp.yaml`);try{if(!(await y.stat(n)).isFile()){if(e)throw new P(`Scope seed config path is not a file: ${n}`);return}return n}catch(t){if(t.code===`ENOENT`){if(e)throw new P(`Scope seed config file not found: ${n}`,{cause:t});return}throw t instanceof P?t:new P(`Failed to read scope seed config file: ${n}`,{cause:t})}}function V(){let e=process.env.HOME||process.env.USERPROFILE;if(!e)throw new P(`HOME or USERPROFILE is not set`);return u.join(e,`.claude-sessions`)}async function H(){let e=V();return await y.mkdir(e,{recursive:!0}),e}async function U(e){try{return(await y.readFile(e,A)).trim()||null}catch(t){if(t.code===`ENOENT`)return null;throw new P(`Failed to read Claude session file: ${e}`,{cause:t})}}async function W(e,t){let n=await H(),r=u.join(n,e);if(t)try{await y.rm(r,{force:!0})}catch(e){throw new P(`Failed to clear Claude session file: ${r}`,{cause:e})}let i=await U(r);if(i)return{sessionId:i,resume:!0};let a=v().toLowerCase();try{await y.writeFile(r,`${a}\n`,A)}catch(e){throw new P(`Failed to write Claude session file: ${r}`,{cause:e})}return{sessionId:a,resume:!1}}async function G(e,t,n,r,a){let o=await B(a);await new i().ensureConfig(e,o);let s=await new O().ensureRunning(t);if(!s.running||!s.port)throw new I(s.error||`Failed to start model proxy HTTP server`);let{sessionId:c,resume:l}=await W(e,n),u=z(s.port,e),d=[`--dangerously-skip-permissions`,...l?[`--resume`,c]:[`--session-id`,c],...r];return console.log(`${j} Scope: ${e}`),console.log(`${j} Proxy: ${u.ANTHROPIC_BASE_URL}`),console.log(`${j} Session: ${c}${l?` (resume)`:` (new)`}`),o&&console.log(`${j} Scope config seed: ${o}`),new Promise((e,t)=>{let n=g(k,d,{stdio:`inherit`,env:u});n.on(`error`,e=>{t(new I(`Failed to launch ${k}`,{cause:e}))}),n.on(`exit`,(n,r)=>{if(r){t(new I(`${k} exited with signal ${r}`));return}e(n??0)})})}const K=new m(`claude`).description(`Launch Claude Code through the model proxy`).option(`-s, --scope <scope>`,`Proxy scope to use`).option(`-p, --port <port>`,`Preferred HTTP port for the proxy`,String(e)).option(`-c, --config-file <path>`,`Seed a new scope from the specified scope config file`).option(`--config <path>`,`Alias for --config-file`).option(`--clear-session`,`Start with a fresh Claude session for the selected scope`,!1).allowUnknownOption(!0).allowExcessArguments(!0).argument(`[claudeArgs...]`).action(async(e,t)=>{try{let n=L(t.scope||R()),r=Number.parseInt(t.port,10);if(!Number.isInteger(r)||r<=0)throw new F(`Invalid port: ${t.port}`);let i=await G(n,r,t.clearSession,e,t.configFile??t.config);process.exit(i)}catch(e){let t=e instanceof N?e:new I(`Failed to launch Claude`,{cause:e});console.error(`${j} ${t.code}: ${t.message}`),console.error(`${j} ${M}`),t.cause&&console.error(`${j} Cause:`,t.cause),process.exit(1)}}),q=`127.0.0.1`,J=new m(`http-serve`).description(`Start the Claude-compatible HTTP model proxy server`).option(`-p, --port <port>`,`Port to listen on`,String(e)).action(async e=>{try{let n=Number.parseInt(e.port,10);if(!Number.isInteger(n)||n<=0)throw Error(`Invalid port: ${e.port}`);let r=new t;await r.ensureConfig();let i=S({fetch:a(r).fetch,port:n,hostname:q});console.log(`model-proxy-mcp listening on http://${q}:${n}`),console.log(`Claude base URL: http://${q}:${n}`);let o=e=>{console.log(`\n${e} received. Shutting down...`),i.close(),process.exit(0)};process.once(`SIGINT`,()=>o(`SIGINT`)),process.once(`SIGTERM`,()=>o(`SIGTERM`))}catch(e){console.error(`Failed to start HTTP server:`,e),process.exit(1)}}),Y=new m(`mcp-serve`).description(`Start MCP server with stdio transport`).option(`--cleanup`,`Stop HTTP server on shutdown`,!1).option(`-p, --port <port>`,`Port for HTTP server`,String(e)).action(async e=>{try{let n=Number.parseInt(e.port,10);if(!Number.isInteger(n)||n<=0)throw Error(`Invalid port: ${e.port}`);let i=new t;await i.ensureConfig(process.env.MODEL_PROXY_MCP_SCOPE||`default`);let a=new O,o=await a.ensureRunning(n);o.running||console.error(`Warning: HTTP server failed to start: ${o.error}`);let c=new s(r(i)),l=async t=>{console.error(`\nReceived ${t}, shutting down gracefully...`),await c.stop(),e.cleanup&&o.running&&await a.stop(),process.exit(0)};process.once(`SIGINT`,()=>void l(`SIGINT`).catch(e=>{console.error(`Failed to shut down after SIGINT:`,e),process.exit(1)})),process.once(`SIGTERM`,()=>void l(`SIGTERM`).catch(e=>{console.error(`Failed to shut down after SIGTERM:`,e),process.exit(1)})),await c.start()}catch(e){console.error(`Failed to start MCP server:`,e),process.exit(1)}}),X=new m(`start`).description(`Start HTTP and/or MCP server for the model proxy`).option(`--mcp-only`,`Start only the MCP server`,!1).option(`--http-only`,`Start only the HTTP server`,!1).option(`-p, --port <port>`,`Port for HTTP server`,String(e)).action(async e=>{try{let t=new O,n=Number.parseInt(e.port,10),i=!e.mcpOnly,a=!e.httpOnly;if(i){let e=await t.ensureRunning(n);e.running||(console.error(`HTTP server failed to start: ${e.error}`),process.exit(1)),console.log(`HTTP server running on http://127.0.0.1:${e.port}`)}if(a){let e=new s(r());await e.start();let t=async t=>{console.error(`\n${t} received. Shutting down...`),await e.stop(),process.exit(0)};process.on(`SIGINT`,()=>t(`SIGINT`)),process.on(`SIGTERM`,()=>t(`SIGTERM`))}else process.exit(0)}catch(e){console.error(`Failed to start services:`,e),process.exit(1)}}),Z=`status`,Q=`Failed to read status`;var $=class extends Error{code=`STATUS_CHECK_FAILED`;constructor(e){super(Q,{cause:e instanceof Error?e:Error(String(e))}),this.name=`StatusCommandError`}};const ee=new m(Z).description(`Show proxy server and profile status`).option(`-s, --scope <scope>`,`Configuration scope`,`default`).action(async e=>{try{let n=await new O().getStatus(),r=await new t().getStatus(e.scope,n.port,n.pid);r.running=n.running,r.error=n.error,o.info(JSON.stringify(r,null,2)),process.exit(0)}catch(t){let n=new $(t);o.error(Q,{command:Z,code:n.code,scope:e.scope,cause:n.cause}),process.exit(1)}}),te=new m(`stop`).description(`Stop the background HTTP server`).action(async()=>{try{let e=await new O().stop();console.log(e?`HTTP server stopped`:`No HTTP server running`),process.exit(0)}catch(e){console.error(`Failed to stop HTTP server:`,e),process.exit(1)}}),ne=d(p(import.meta.url)),re=JSON.parse(l(f(ne,`../package.json`),`utf-8`));async function ie(){let e=new m;e.name(`model-proxy-mcp`).description(`Claude-compatible model proxy MCP`).version(re.version),e.addCommand(X),e.addCommand(te),e.addCommand(ee),e.addCommand(K),e.addCommand(J),e.addCommand(Y),await e.parseAsync(process.argv)}ie();export{};
4
+ //# sourceMappingURL=cli.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.mjs","names":["SIGTERM_SIGNAL: NodeJS.Signals","SIGKILL_SIGNAL: NodeJS.Signals","STATE_FILE_ENCODING: BufferEncoding","fs","health","DEFAULT_SCOPE","TEXT_ENCODING: BufferEncoding","MODEL_PROXY_SCOPE_ENV","code: string","fs","DEFAULT_SCOPE"],"sources":["../src/services/HttpServerHealthCheck.ts","../src/services/HttpServerManager.ts","../src/commands/claude.ts","../src/commands/http-serve.ts","../src/commands/mcp-serve.ts","../src/commands/start.ts","../src/commands/status.ts","../src/commands/stop.ts","../src/cli.ts"],"sourcesContent":["export interface HealthCheckResult {\n healthy: boolean;\n serviceName?: string;\n error?: string;\n}\n\nexport class HttpServerHealthCheck {\n async check(port: number): Promise<HealthCheckResult> {\n try {\n const response = await fetch(`http://127.0.0.1:${port}/health`);\n if (!response.ok) {\n return { healthy: false, error: `Health check failed with status ${response.status}` };\n }\n\n const payload = (await response.json()) as { service?: string };\n return {\n healthy: true,\n serviceName: payload.service,\n };\n } catch (error) {\n return {\n healthy: false,\n error: error instanceof Error ? error.message : String(error),\n };\n }\n }\n}\n","import { execFile, spawn } from 'node:child_process';\nimport { existsSync } from 'node:fs';\nimport fs from 'node:fs/promises';\nimport os from 'node:os';\nimport path from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport { promisify } from 'node:util';\nimport { DEFAULT_HTTP_PORT, DEFAULT_SERVICE_NAME } from '../constants/defaults.js';\nimport type { GatewayStatus } from '../types/index.js';\nimport { HttpServerHealthCheck } from './HttpServerHealthCheck.js';\n\nconst WORKSPACE_MARKERS = ['pnpm-workspace.yaml', 'nx.json', '.git'];\nconst PORT_SCAN_RANGE = 25;\nconst SHELL_BINARY = 'sh';\nconst LSOF_COMMAND_PREFIX = 'lsof -n -P -sTCP:LISTEN';\nconst HEALTH_CHECK_STABILIZE_MS = 5000;\nconst HEALTH_CHECK_POLL_INTERVAL_MS = 250;\nconst PROCESS_SHUTDOWN_WAIT_MS = 1000;\nconst SIGTERM_SIGNAL: NodeJS.Signals = 'SIGTERM';\nconst SIGKILL_SIGNAL: NodeJS.Signals = 'SIGKILL';\nconst STATE_FILE_PREFIX = 'model-proxy-mcp-http';\nconst STATE_FILE_ENCODING: BufferEncoding = 'utf8';\nconst execFileAsync = promisify(execFile);\n\nfunction resolveWorkspaceRoot(startPath = process.cwd()): string {\n let currentDir = path.resolve(startPath);\n\n while (true) {\n for (const marker of WORKSPACE_MARKERS) {\n if (existsSync(path.join(currentDir, marker))) {\n return currentDir;\n }\n }\n\n const parentDir = path.dirname(currentDir);\n if (parentDir === currentDir) {\n return process.cwd();\n }\n currentDir = parentDir;\n }\n}\n\ninterface ServiceRegistration {\n port: number;\n pid?: number;\n}\n\ninterface PortListener {\n port: number;\n pid: number;\n}\n\ntype CliRuntime = 'node' | 'bun';\n\nexport class HttpServerManager {\n private readonly repositoryPath = resolveWorkspaceRoot(process.cwd());\n private readonly serviceName = DEFAULT_SERVICE_NAME;\n private readonly stateFilePath = path.join(\n os.tmpdir(),\n `${STATE_FILE_PREFIX}-${Buffer.from(resolveWorkspaceRoot(process.cwd())).toString('hex')}.json`,\n );\n\n constructor(private readonly healthCheck = new HttpServerHealthCheck()) {}\n\n private createEmptyStatus(overrides: Partial<GatewayStatus> = {}): GatewayStatus {\n return {\n running: false,\n scope: 'default',\n activeProfileId: null,\n auth: { configured: false, authFilePath: '' },\n profiles: [],\n slotModels: {},\n ...overrides,\n };\n }\n\n private async getRegistration(): Promise<ServiceRegistration | null> {\n try {\n const rawState = await fs.readFile(this.stateFilePath, STATE_FILE_ENCODING);\n const parsed = JSON.parse(rawState) as ServiceRegistration;\n return parsed.port ? parsed : null;\n } catch (error) {\n const nodeError = error as NodeJS.ErrnoException;\n if (nodeError.code === 'ENOENT') {\n return null;\n }\n throw error;\n }\n }\n\n private async releaseService(pid?: number): Promise<void> {\n const registration = await this.getRegistration();\n if (pid && registration?.pid && registration.pid !== pid) {\n return;\n }\n await fs.rm(this.stateFilePath, { force: true });\n }\n\n private async listListeningProcesses(port: number): Promise<PortListener[]> {\n try {\n const { stdout } = await execFileAsync(SHELL_BINARY, ['-lc', `${LSOF_COMMAND_PREFIX} -iTCP:${port} || true`], {\n encoding: 'utf8',\n });\n return stdout\n .split('\\n')\n .slice(1)\n .map((line) => line.trim())\n .filter(Boolean)\n .map((line) => line.split(/\\s+/))\n .map((parts) => ({\n pid: Number.parseInt(parts[1] || '', 10),\n port,\n }))\n .filter((listener) => Number.isInteger(listener.pid) && listener.pid > 0);\n } catch (error) {\n return [];\n }\n }\n\n private async findHealthyServicePort(startPort: number): Promise<number | null> {\n for (let offset = 0; offset <= PORT_SCAN_RANGE; offset += 1) {\n const candidatePort = startPort + offset;\n const health = await this.healthCheck.check(candidatePort);\n if (health.healthy && health.serviceName === this.serviceName) {\n return candidatePort;\n }\n }\n\n return null;\n }\n\n private async clearPortListeners(port: number, preservePid?: number): Promise<void> {\n const listeners = await this.listListeningProcesses(port);\n for (const listener of listeners) {\n if (preservePid && listener.pid === preservePid) {\n continue;\n }\n await this.killProcess(listener.pid);\n }\n }\n\n private async registerService(port: number, pid: number): Promise<void> {\n await fs.writeFile(this.stateFilePath, JSON.stringify({ port, pid }), STATE_FILE_ENCODING);\n }\n\n private async findAvailablePort(startPort: number): Promise<number> {\n const minimumPort = Math.min(DEFAULT_HTTP_PORT, startPort);\n const maximumPort = Math.max(DEFAULT_HTTP_PORT + 1000, startPort);\n\n for (let candidatePort = startPort; candidatePort <= maximumPort; candidatePort += 1) {\n const listeners = await this.listListeningProcesses(candidatePort);\n if (listeners.length === 0) {\n return candidatePort;\n }\n }\n\n for (let candidatePort = minimumPort; candidatePort < startPort; candidatePort += 1) {\n const listeners = await this.listListeningProcesses(candidatePort);\n if (listeners.length === 0) {\n return candidatePort;\n }\n }\n\n throw new Error(`No available port found from ${startPort}`);\n }\n\n private async fileExists(filePath: string): Promise<boolean> {\n try {\n await fs.access(filePath);\n return true;\n } catch {\n return false;\n }\n }\n\n private async resolveCliPath(): Promise<{ cliPath: string; runtime: CliRuntime }> {\n const currentDir = path.dirname(fileURLToPath(import.meta.url));\n\n // Bundled dist output: cli.mjs lives in the same directory as this file\n const sameDirCliPath = path.resolve(currentDir, 'cli.mjs');\n if (await this.fileExists(sameDirCliPath)) {\n return { cliPath: sameDirCliPath, runtime: 'node' };\n }\n\n // Unbundled dist output: this file is in dist/services/, cli.mjs is in dist/\n const parentDirCliPath = path.resolve(currentDir, '..', 'cli.mjs');\n if (await this.fileExists(parentDirCliPath)) {\n return { cliPath: parentDirCliPath, runtime: 'node' };\n }\n\n // Workspace fallbacks for monorepo development\n const repoDistCliPath = path.join(this.repositoryPath, 'packages', 'mcp', 'model-proxy-mcp', 'dist', 'cli.mjs');\n if (await this.fileExists(repoDistCliPath)) {\n return { cliPath: repoDistCliPath, runtime: 'node' };\n }\n\n // Dev mode: source cli.ts next to this file or one level up\n for (const relPath of [path.resolve(currentDir, 'cli.ts'), path.resolve(currentDir, '..', 'cli.ts')]) {\n if (await this.fileExists(relPath)) {\n return { cliPath: relPath, runtime: 'bun' };\n }\n }\n\n const repoSrcCliPath = path.join(this.repositoryPath, 'packages', 'mcp', 'model-proxy-mcp', 'src', 'cli.ts');\n if (await this.fileExists(repoSrcCliPath)) {\n return { cliPath: repoSrcCliPath, runtime: 'bun' };\n }\n\n throw new Error('Cannot find model-proxy-mcp CLI');\n }\n\n private async startHttpServer(port: number): Promise<number> {\n const { cliPath, runtime } = await this.resolveCliPath();\n\n const command = runtime === 'bun' ? (process.versions.bun ? process.execPath : 'bun') : 'node';\n const args =\n runtime === 'bun'\n ? ['run', cliPath, 'http-serve', '--port', String(port)]\n : [cliPath, 'http-serve', '--port', String(port)];\n\n const child = spawn(command, args, {\n detached: true,\n stdio: 'ignore',\n env: {\n ...process.env,\n NODE_ENV: process.env.NODE_ENV || 'development',\n },\n });\n\n child.unref();\n if (!child.pid) {\n throw new Error('Failed to spawn HTTP server');\n }\n\n return child.pid;\n }\n\n private async killProcess(pid: number): Promise<void> {\n try {\n process.kill(pid, SIGTERM_SIGNAL);\n await new Promise((resolve) => setTimeout(resolve, PROCESS_SHUTDOWN_WAIT_MS));\n try {\n process.kill(pid, 0);\n process.kill(pid, SIGKILL_SIGNAL);\n } catch {\n return;\n }\n } catch {\n return;\n }\n }\n\n private async waitForHealthy(port: number): Promise<{ healthy: boolean; error?: string }> {\n const deadline = Date.now() + HEALTH_CHECK_STABILIZE_MS;\n let lastError = 'Server failed health check after startup';\n\n while (Date.now() < deadline) {\n const health = await this.healthCheck.check(port);\n if (health.healthy && health.serviceName === this.serviceName) {\n return { healthy: true };\n }\n\n lastError = health.error || `Unexpected service on port ${port}`;\n await new Promise((resolve) => setTimeout(resolve, HEALTH_CHECK_POLL_INTERVAL_MS));\n }\n\n return { healthy: false, error: lastError };\n }\n\n async ensureRunning(port = DEFAULT_HTTP_PORT): Promise<GatewayStatus> {\n try {\n const healthyPort = await this.findHealthyServicePort(port);\n if (healthyPort !== null) {\n const healthyListeners = await this.listListeningProcesses(healthyPort);\n const healthyPid = healthyListeners[0]?.pid;\n await this.registerService(healthyPort, healthyPid ?? process.pid);\n return this.createEmptyStatus({ running: true, port: healthyPort, pid: healthyPid });\n }\n\n const existing = await this.getRegistration();\n if (existing) {\n const health = await this.healthCheck.check(existing.port);\n if (health.healthy && health.serviceName === this.serviceName) {\n return this.createEmptyStatus({ running: true, port: existing.port, pid: existing.pid });\n }\n\n if (existing.pid) {\n await this.killProcess(existing.pid);\n }\n await this.clearPortListeners(existing.port, existing.pid);\n await this.releaseService(existing.pid);\n }\n\n await this.clearPortListeners(port);\n const availablePort = await this.findAvailablePort(port);\n const pid = await this.startHttpServer(availablePort);\n const health = await this.waitForHealthy(availablePort);\n if (!health.healthy) {\n await this.killProcess(pid);\n await this.clearPortListeners(availablePort, pid);\n await this.releaseService(pid);\n return this.createEmptyStatus({ error: health.error || 'Server failed health check after startup' });\n }\n\n await this.registerService(availablePort, pid);\n return this.createEmptyStatus({ running: true, port: availablePort, pid });\n } catch (error) {\n return this.createEmptyStatus({ error: error instanceof Error ? error.message : String(error) });\n }\n }\n\n async stop(): Promise<boolean> {\n const registration = await this.getRegistration();\n let stopped = false;\n\n if (registration?.pid) {\n await this.killProcess(registration.pid);\n stopped = true;\n }\n if (registration) {\n await this.clearPortListeners(registration.port, registration.pid);\n await this.releaseService(registration.pid);\n }\n\n const healthyPort = await this.findHealthyServicePort(DEFAULT_HTTP_PORT);\n if (healthyPort !== null) {\n const listeners = await this.listListeningProcesses(healthyPort);\n for (const listener of listeners) {\n await this.killProcess(listener.pid);\n stopped = true;\n }\n await this.releaseService();\n }\n\n return stopped;\n }\n\n async getStatus(): Promise<GatewayStatus> {\n const registration = await this.getRegistration();\n if (registration) {\n const health = await this.healthCheck.check(registration.port);\n if (health.healthy) {\n return this.createEmptyStatus({\n running: true,\n port: registration.port,\n pid: registration.pid,\n });\n }\n }\n\n const healthyPort = await this.findHealthyServicePort(DEFAULT_HTTP_PORT);\n if (healthyPort !== null) {\n const listeners = await this.listListeningProcesses(healthyPort);\n const pid = listeners[0]?.pid;\n if (pid) {\n await this.registerService(healthyPort, pid);\n }\n return this.createEmptyStatus({\n running: true,\n port: healthyPort,\n pid,\n });\n }\n\n return this.createEmptyStatus({\n error: registration ? 'Registered server is unhealthy' : 'No HTTP server registered',\n });\n }\n}\n","import { spawn } from 'node:child_process';\nimport { randomBytes, randomUUID } from 'node:crypto';\nimport fs from 'node:fs/promises';\nimport path from 'node:path';\nimport { Command } from 'commander';\nimport { DEFAULT_HTTP_PORT } from '../constants/defaults.js';\nimport { HttpServerManager } from '../services/HttpServerManager.js';\nimport { ProfileStore } from '../services/ProfileStore.js';\n\nconst CLAUDE_BINARY = 'claude';\nconst DEFAULT_SCOPE = 'default';\nconst SESSIONS_DIRECTORY_NAME = '.claude-sessions';\nconst RANDOM_SCOPE_PREFIX = 'scope';\nconst RANDOM_SCOPE_BYTES = 3;\nconst TEXT_ENCODING: BufferEncoding = 'utf8';\nconst STDIO_INHERIT = 'inherit';\nconst DECIMAL_RADIX = 10;\nconst LOG_PREFIX = '[model-proxy-mcp]';\nconst RECOVERY_MESSAGE = `Recovery: verify HOME, proxy port, and that \\`${CLAUDE_BINARY}\\` is on PATH.`;\nconst ARG_RESUME = '--resume';\nconst ARG_SESSION_ID = '--session-id';\nconst ARG_SKIP_PERMISSIONS = '--dangerously-skip-permissions';\nconst MODEL_ALIAS_OPUS = 'ccproxy-opus';\nconst MODEL_ALIAS_SONNET = 'ccproxy-sonnet';\nconst MODEL_ALIAS_HAIKU = 'ccproxy-haiku';\nconst MODEL_ALIAS_SUBAGENT = 'ccproxy-subagent';\nconst DEFAULT_SCOPE_SEED_FILE = 'model-proxy-mcp.yaml';\nconst MODEL_PROXY_SCOPE_ENV = 'MODEL_PROXY_MCP_SCOPE';\nconst MODEL_PROXY_SLOT_ENV = 'MODEL_PROXY_MCP_SLOT';\nconst DEFAULT_MODEL_PROXY_SLOT = 'default';\n\nclass ClaudeCommandError extends Error {\n constructor(\n message: string,\n readonly code: string,\n options?: {\n cause?: unknown;\n },\n ) {\n super(message, options);\n this.name = 'ClaudeCommandError';\n }\n}\n\nclass ClaudeCommandConfigError extends ClaudeCommandError {\n constructor(message: string, options?: { cause?: unknown }) {\n super(message, 'CLAUDE_COMMAND_CONFIG_ERROR', options);\n this.name = 'ClaudeCommandConfigError';\n }\n}\n\nclass ClaudeCommandValidationError extends ClaudeCommandError {\n constructor(message: string, options?: { cause?: unknown }) {\n super(message, 'CLAUDE_COMMAND_VALIDATION_ERROR', options);\n this.name = 'ClaudeCommandValidationError';\n }\n}\n\nclass ClaudeCommandLaunchError extends ClaudeCommandError {\n constructor(message: string, options?: { cause?: unknown }) {\n super(message, 'CLAUDE_COMMAND_LAUNCH_ERROR', options);\n this.name = 'ClaudeCommandLaunchError';\n }\n}\n\nexport function sanitizeScope(scope: string): string {\n const sanitized = scope\n .trim()\n .replace(/[^a-zA-Z0-9._-]+/g, '-')\n .replace(/^-+|-+$/g, '');\n return sanitized || DEFAULT_SCOPE;\n}\n\nexport function generateScopeName(): string {\n return `${RANDOM_SCOPE_PREFIX}-${randomBytes(RANDOM_SCOPE_BYTES).toString('hex')}`;\n}\n\nexport function buildClaudeEnvironment(\n port: number,\n scope: string,\n baseEnvironment: NodeJS.ProcessEnv = process.env,\n): NodeJS.ProcessEnv {\n return {\n ...baseEnvironment,\n ANTHROPIC_BASE_URL: `http://127.0.0.1:${port}/scopes/${scope}`,\n ANTHROPIC_DEFAULT_OPUS_MODEL: baseEnvironment.ANTHROPIC_DEFAULT_OPUS_MODEL || MODEL_ALIAS_OPUS,\n ANTHROPIC_DEFAULT_SONNET_MODEL: baseEnvironment.ANTHROPIC_DEFAULT_SONNET_MODEL || MODEL_ALIAS_SONNET,\n ANTHROPIC_DEFAULT_HAIKU_MODEL: baseEnvironment.ANTHROPIC_DEFAULT_HAIKU_MODEL || MODEL_ALIAS_HAIKU,\n CLAUDE_CODE_SUBAGENT_MODEL: baseEnvironment.CLAUDE_CODE_SUBAGENT_MODEL || MODEL_ALIAS_SUBAGENT,\n MODEL_PROXY_MCP_SCOPE: baseEnvironment[MODEL_PROXY_SCOPE_ENV] || scope,\n MODEL_PROXY_MCP_SLOT: baseEnvironment[MODEL_PROXY_SLOT_ENV] || DEFAULT_MODEL_PROXY_SLOT,\n };\n}\n\nexport async function resolveScopeSeedConfigPath(\n configFile: string | undefined,\n workingDirectory = process.cwd(),\n): Promise<string | undefined> {\n const candidate = configFile\n ? path.resolve(workingDirectory, configFile)\n : path.join(workingDirectory, DEFAULT_SCOPE_SEED_FILE);\n\n try {\n const stats = await fs.stat(candidate);\n if (!stats.isFile()) {\n if (configFile) {\n throw new ClaudeCommandConfigError(`Scope seed config path is not a file: ${candidate}`);\n }\n return undefined;\n }\n return candidate;\n } catch (error) {\n const nodeError = error as NodeJS.ErrnoException;\n if (nodeError.code === 'ENOENT') {\n if (configFile) {\n throw new ClaudeCommandConfigError(`Scope seed config file not found: ${candidate}`, { cause: error });\n }\n return undefined;\n }\n if (error instanceof ClaudeCommandConfigError) {\n throw error;\n }\n throw new ClaudeCommandConfigError(`Failed to read scope seed config file: ${candidate}`, { cause: error });\n }\n}\n\nfunction getSessionsDirectory(): string {\n const homeDirectory = process.env.HOME || process.env.USERPROFILE;\n if (!homeDirectory) {\n throw new ClaudeCommandConfigError('HOME or USERPROFILE is not set');\n }\n\n return path.join(homeDirectory, SESSIONS_DIRECTORY_NAME);\n}\n\nasync function ensureSessionsDirectory(): Promise<string> {\n const sessionsDirectory = getSessionsDirectory();\n await fs.mkdir(sessionsDirectory, { recursive: true });\n return sessionsDirectory;\n}\n\nasync function readSessionId(sessionFile: string): Promise<string | null> {\n try {\n const sessionId = (await fs.readFile(sessionFile, TEXT_ENCODING)).trim();\n return sessionId || null;\n } catch (error) {\n const nodeError = error as NodeJS.ErrnoException;\n if (nodeError.code === 'ENOENT') {\n return null;\n }\n throw new ClaudeCommandConfigError(`Failed to read Claude session file: ${sessionFile}`, { cause: error });\n }\n}\n\nasync function resolveSessionId(scope: string, clearSession: boolean): Promise<{ sessionId: string; resume: boolean }> {\n const sessionsDirectory = await ensureSessionsDirectory();\n const sessionFile = path.join(sessionsDirectory, scope);\n\n if (clearSession) {\n try {\n await fs.rm(sessionFile, { force: true });\n } catch (error) {\n throw new ClaudeCommandConfigError(`Failed to clear Claude session file: ${sessionFile}`, { cause: error });\n }\n }\n\n const existingSessionId = await readSessionId(sessionFile);\n if (existingSessionId) {\n return { sessionId: existingSessionId, resume: true };\n }\n\n const sessionId = randomUUID().toLowerCase();\n try {\n await fs.writeFile(sessionFile, `${sessionId}\\n`, TEXT_ENCODING);\n } catch (error) {\n throw new ClaudeCommandConfigError(`Failed to write Claude session file: ${sessionFile}`, { cause: error });\n }\n return { sessionId, resume: false };\n}\n\nasync function launchClaude(\n scope: string,\n port: number,\n clearSession: boolean,\n claudeArgs: string[],\n configFile?: string,\n): Promise<number> {\n const scopeSeedConfigPath = await resolveScopeSeedConfigPath(configFile);\n const profileStore = new ProfileStore();\n await profileStore.ensureConfig(scope, scopeSeedConfigPath);\n\n const serverManager = new HttpServerManager();\n const status = await serverManager.ensureRunning(port);\n\n if (!status.running || !status.port) {\n throw new ClaudeCommandLaunchError(status.error || 'Failed to start model proxy HTTP server');\n }\n\n const { sessionId, resume } = await resolveSessionId(scope, clearSession);\n const env = buildClaudeEnvironment(status.port, scope);\n const args = [\n ARG_SKIP_PERMISSIONS,\n ...(resume ? [ARG_RESUME, sessionId] : [ARG_SESSION_ID, sessionId]),\n ...claudeArgs,\n ];\n\n console.log(`${LOG_PREFIX} Scope: ${scope}`);\n console.log(`${LOG_PREFIX} Proxy: ${env.ANTHROPIC_BASE_URL}`);\n console.log(`${LOG_PREFIX} Session: ${sessionId}${resume ? ' (resume)' : ' (new)'}`);\n if (scopeSeedConfigPath) {\n console.log(`${LOG_PREFIX} Scope config seed: ${scopeSeedConfigPath}`);\n }\n\n return new Promise<number>((resolve, reject) => {\n const child = spawn(CLAUDE_BINARY, args, {\n stdio: STDIO_INHERIT,\n env,\n });\n\n child.on('error', (error) => {\n reject(\n new ClaudeCommandLaunchError(`Failed to launch ${CLAUDE_BINARY}`, {\n cause: error,\n }),\n );\n });\n\n child.on('exit', (code, signal) => {\n if (signal) {\n reject(new ClaudeCommandLaunchError(`${CLAUDE_BINARY} exited with signal ${signal}`));\n return;\n }\n resolve(code ?? 0);\n });\n });\n}\n\nexport const claudeCommand = new Command('claude')\n .description('Launch Claude Code through the model proxy')\n .option('-s, --scope <scope>', 'Proxy scope to use')\n .option('-p, --port <port>', 'Preferred HTTP port for the proxy', String(DEFAULT_HTTP_PORT))\n .option('-c, --config-file <path>', 'Seed a new scope from the specified scope config file')\n .option('--config <path>', 'Alias for --config-file')\n .option('--clear-session', 'Start with a fresh Claude session for the selected scope', false)\n .allowUnknownOption(true)\n .allowExcessArguments(true)\n .argument('[claudeArgs...]')\n .action(\n async (\n claudeArgs: string[],\n options: { scope?: string; port: string; clearSession: boolean; configFile?: string; config?: string },\n ) => {\n try {\n const resolvedScope = sanitizeScope(options.scope || generateScopeName());\n const port = Number.parseInt(options.port, DECIMAL_RADIX);\n if (!Number.isInteger(port) || port <= 0) {\n throw new ClaudeCommandValidationError(`Invalid port: ${options.port}`);\n }\n\n const exitCode = await launchClaude(\n resolvedScope,\n port,\n options.clearSession,\n claudeArgs,\n options.configFile ?? options.config,\n );\n process.exit(exitCode);\n } catch (error) {\n const commandError =\n error instanceof ClaudeCommandError\n ? error\n : new ClaudeCommandLaunchError('Failed to launch Claude', { cause: error });\n console.error(`${LOG_PREFIX} ${commandError.code}: ${commandError.message}`);\n console.error(`${LOG_PREFIX} ${RECOVERY_MESSAGE}`);\n if (commandError.cause) {\n console.error(`${LOG_PREFIX} Cause:`, commandError.cause);\n }\n process.exit(1);\n }\n },\n );\n","import { serve } from '@hono/node-server';\nimport { Command } from 'commander';\nimport { DEFAULT_HTTP_PORT } from '../constants/defaults.js';\nimport { createHttpServer } from '../server/http.js';\nimport { GatewayService } from '../services/GatewayService.js';\n\nconst LOCAL_HOST = '127.0.0.1';\ntype HttpServeOptions = { port: string };\n\nexport const httpServeCommand = new Command('http-serve')\n .description('Start the Claude-compatible HTTP model proxy server')\n .option('-p, --port <port>', 'Port to listen on', String(DEFAULT_HTTP_PORT))\n .action(async (options: HttpServeOptions) => {\n try {\n const port = Number.parseInt(options.port, 10);\n if (!Number.isInteger(port) || port <= 0) {\n throw new Error(`Invalid port: ${options.port}`);\n }\n\n const gatewayService = new GatewayService();\n await gatewayService.ensureConfig();\n const app = createHttpServer(gatewayService);\n const server = serve({ fetch: app.fetch, port, hostname: LOCAL_HOST });\n\n console.log(`model-proxy-mcp listening on http://${LOCAL_HOST}:${port}`);\n console.log(`Claude base URL: http://${LOCAL_HOST}:${port}`);\n\n const shutdown = (signal: string) => {\n console.log(`\\n${signal} received. Shutting down...`);\n server.close();\n process.exit(0);\n };\n\n process.once('SIGINT', () => shutdown('SIGINT'));\n process.once('SIGTERM', () => shutdown('SIGTERM'));\n } catch (error) {\n console.error('Failed to start HTTP server:', error);\n process.exit(1);\n }\n });\n","import { Command } from 'commander';\nimport { DEFAULT_HTTP_PORT } from '../constants/defaults.js';\nimport { createServer } from '../server/index.js';\nimport { GatewayService } from '../services/GatewayService.js';\nimport { HttpServerManager } from '../services/HttpServerManager.js';\nimport { StdioTransportHandler } from '../transports/stdio.js';\n\nconst DEFAULT_SCOPE = 'default';\nconst MODEL_PROXY_SCOPE_ENV = 'MODEL_PROXY_MCP_SCOPE';\n\ntype McpServeOptions = { cleanup: boolean; port: string };\n\nexport const mcpServeCommand = new Command('mcp-serve')\n .description('Start MCP server with stdio transport')\n .option('--cleanup', 'Stop HTTP server on shutdown', false)\n .option('-p, --port <port>', 'Port for HTTP server', String(DEFAULT_HTTP_PORT))\n .action(async (options: McpServeOptions) => {\n try {\n const port = Number.parseInt(options.port, 10);\n if (!Number.isInteger(port) || port <= 0) {\n throw new Error(`Invalid port: ${options.port}`);\n }\n\n const gatewayService = new GatewayService();\n await gatewayService.ensureConfig(process.env[MODEL_PROXY_SCOPE_ENV] || DEFAULT_SCOPE);\n\n const manager = new HttpServerManager();\n const httpStatus = await manager.ensureRunning(port);\n if (!httpStatus.running) {\n console.error(`Warning: HTTP server failed to start: ${httpStatus.error}`);\n }\n\n const handler = new StdioTransportHandler(createServer(gatewayService));\n\n const shutdown = async (signal: string) => {\n console.error(`\\nReceived ${signal}, shutting down gracefully...`);\n await handler.stop();\n if (options.cleanup && httpStatus.running) {\n await manager.stop();\n }\n process.exit(0);\n };\n\n process.once(\n 'SIGINT',\n () =>\n void shutdown('SIGINT').catch((error) => {\n console.error('Failed to shut down after SIGINT:', error);\n process.exit(1);\n }),\n );\n process.once(\n 'SIGTERM',\n () =>\n void shutdown('SIGTERM').catch((error) => {\n console.error('Failed to shut down after SIGTERM:', error);\n process.exit(1);\n }),\n );\n\n await handler.start();\n } catch (error) {\n console.error('Failed to start MCP server:', error);\n process.exit(1);\n }\n });\n","import { Command } from 'commander';\nimport { DEFAULT_HTTP_PORT } from '../constants/defaults.js';\nimport { createServer } from '../server/index.js';\nimport { HttpServerManager } from '../services/HttpServerManager.js';\nimport { StdioTransportHandler } from '../transports/stdio.js';\n\nexport const startCommand = new Command('start')\n .description('Start HTTP and/or MCP server for the model proxy')\n .option('--mcp-only', 'Start only the MCP server', false)\n .option('--http-only', 'Start only the HTTP server', false)\n .option('-p, --port <port>', 'Port for HTTP server', String(DEFAULT_HTTP_PORT))\n .action(async (options) => {\n try {\n const httpServerManager = new HttpServerManager();\n const port = Number.parseInt(options.port, 10);\n const startHttp = !options.mcpOnly;\n const startMcp = !options.httpOnly;\n\n if (startHttp) {\n const status = await httpServerManager.ensureRunning(port);\n if (!status.running) {\n console.error(`HTTP server failed to start: ${status.error}`);\n process.exit(1);\n }\n console.log(`HTTP server running on http://127.0.0.1:${status.port}`);\n }\n\n if (startMcp) {\n const handler = new StdioTransportHandler(createServer());\n await handler.start();\n const shutdown = async (signal: string) => {\n console.error(`\\n${signal} received. Shutting down...`);\n await handler.stop();\n process.exit(0);\n };\n process.on('SIGINT', () => shutdown('SIGINT'));\n process.on('SIGTERM', () => shutdown('SIGTERM'));\n } else {\n process.exit(0);\n }\n } catch (error) {\n console.error('Failed to start services:', error);\n process.exit(1);\n }\n });\n","import { Command } from 'commander';\nimport { GatewayService } from '../services/GatewayService.js';\nimport { HttpServerManager } from '../services/HttpServerManager.js';\nimport { consoleLogger } from '../services/logger.js';\n\nconst DEFAULT_SCOPE = 'default';\nconst STATUS_COMMAND_NAME = 'status';\nconst STATUS_COMMAND_DESCRIPTION = 'Show proxy server and profile status';\nconst STATUS_ERROR_MESSAGE = 'Failed to read status';\n\nclass StatusCommandError extends Error {\n readonly code = 'STATUS_CHECK_FAILED';\n\n constructor(cause: unknown) {\n super(STATUS_ERROR_MESSAGE, { cause: cause instanceof Error ? cause : new Error(String(cause)) });\n this.name = 'StatusCommandError';\n }\n}\n\nexport const statusCommand = new Command(STATUS_COMMAND_NAME)\n .description(STATUS_COMMAND_DESCRIPTION)\n .option('-s, --scope <scope>', 'Configuration scope', DEFAULT_SCOPE)\n .action(async (options: { scope: string }) => {\n try {\n const manager = new HttpServerManager();\n const serverStatus = await manager.getStatus();\n const gateway = new GatewayService();\n const status = await gateway.getStatus(options.scope, serverStatus.port, serverStatus.pid);\n status.running = serverStatus.running;\n status.error = serverStatus.error;\n consoleLogger.info(JSON.stringify(status, null, 2));\n process.exit(0);\n } catch (error) {\n const statusError = new StatusCommandError(error);\n consoleLogger.error(STATUS_ERROR_MESSAGE, {\n command: STATUS_COMMAND_NAME,\n code: statusError.code,\n scope: options.scope,\n cause: statusError.cause,\n });\n process.exit(1);\n }\n });\n","import { Command } from 'commander';\nimport { HttpServerManager } from '../services/HttpServerManager.js';\n\nexport const stopCommand = new Command('stop').description('Stop the background HTTP server').action(async () => {\n try {\n const manager = new HttpServerManager();\n const stopped = await manager.stop();\n console.log(stopped ? 'HTTP server stopped' : 'No HTTP server running');\n process.exit(0);\n } catch (error) {\n console.error('Failed to stop HTTP server:', error);\n process.exit(1);\n }\n});\n","#!/usr/bin/env node\nimport { readFileSync } from 'node:fs';\nimport { dirname, join } from 'node:path';\nimport { fileURLToPath } from 'node:url';\nimport { Command } from 'commander';\nimport { claudeCommand } from './commands/claude.js';\nimport { httpServeCommand } from './commands/http-serve.js';\nimport { mcpServeCommand } from './commands/mcp-serve.js';\nimport { startCommand } from './commands/start.js';\nimport { statusCommand } from './commands/status.js';\nimport { stopCommand } from './commands/stop.js';\n\nconst __dirname = dirname(fileURLToPath(import.meta.url));\nconst packageJson = JSON.parse(readFileSync(join(__dirname, '../package.json'), 'utf-8'));\n\nasync function main() {\n const program = new Command();\n program.name('model-proxy-mcp').description('Claude-compatible model proxy MCP').version(packageJson.version);\n program.addCommand(startCommand);\n program.addCommand(stopCommand);\n program.addCommand(statusCommand);\n program.addCommand(claudeCommand);\n program.addCommand(httpServeCommand);\n program.addCommand(mcpServeCommand);\n await program.parseAsync(process.argv);\n}\n\nmain();\n"],"mappings":";ygBAMA,IAAa,EAAb,KAAmC,CACjC,MAAM,MAAM,EAA0C,CACpD,GAAI,CACF,IAAM,EAAW,MAAM,MAAM,oBAAoB,EAAK,SAAS,CAM/D,OALK,EAAS,GAKP,CACL,QAAS,GACT,aAHe,MAAM,EAAS,MAAM,EAGf,QACtB,CAPQ,CAAE,QAAS,GAAO,MAAO,mCAAmC,EAAS,SAAU,OAQjF,EAAO,CACd,MAAO,CACL,QAAS,GACT,MAAO,aAAiB,MAAQ,EAAM,QAAU,OAAO,EAAM,CAC9D,ICZP,MAAM,EAAoB,CAAC,sBAAuB,UAAW,OAAO,CAU9DE,EAAsC,OACtC,EAAgB,EAAU,EAAS,CAEzC,SAAS,EAAqB,EAAY,QAAQ,KAAK,CAAU,CAC/D,IAAI,EAAa,EAAK,QAAQ,EAAU,CAExC,OAAa,CACX,IAAK,IAAM,KAAU,EACnB,GAAI,EAAW,EAAK,KAAK,EAAY,EAAO,CAAC,CAC3C,OAAO,EAIX,IAAM,EAAY,EAAK,QAAQ,EAAW,CAC1C,GAAI,IAAc,EAChB,OAAO,QAAQ,KAAK,CAEtB,EAAa,GAgBjB,IAAa,EAAb,KAA+B,CAC7B,eAAkC,EAAqB,QAAQ,KAAK,CAAC,CACrE,YAA+B,EAC/B,cAAiC,EAAK,KACpC,EAAG,QAAQ,CACX,wBAAwB,OAAO,KAAK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AAC,QAAQ,GAAY,QAAQ,CAC7G,EAAQ,WAAW,EAAa,CAChC,EAAQ,WAAW,GAAY,CAC/B,EAAQ,WAAW,GAAc,CACjC,EAAQ,WAAW,EAAc,CACjC,EAAQ,WAAW,EAAiB,CACpC,EAAQ,WAAW,EAAgB,CACnC,MAAM,EAAQ,WAAW,QAAQ,KAAK,CAGxC,IAAM"}
@@ -1129,4 +1129,5 @@ var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=
1129
1129
  <\/script>
1130
1130
  </body>
1131
1131
  </html>
1132
- `,{headers:{"content-type":`text/html; charset=utf-8`}})),t.get(`/admin/scopes`,async t=>t.json({scopes:await e.listScopes()})),t.get(`/admin/config`,async t=>{let r=n(t.req.query(`scope`));return t.json(await e.getAdminConfig(r))}),t.get(`/admin/profiles`,async t=>{let r=n(t.req.query(`scope`));return t.json({profiles:await e.listProfiles(r)})}),t.get(`/admin/current-model`,async t=>{let r=n(t.req.query(`scope`)),i=t.req.query(`slot`)||`default`;return t.json({profile:await e.getCurrentModel(r,i)})}),t.get(`/admin/history`,async t=>{let r=n(t.req.query(`scope`)),i=h.z.coerce.number().int().positive().max(200).default(50).parse(t.req.query(`limit`)??50),a=t.req.query(`cursor`)||void 0;return t.json(await e.listHistory(r,i,a))}),t.get(`/admin/history/stats`,async t=>{let r=n(t.req.query(`scope`));return t.json(await e.getHistoryStats(r))}),t.put(`/admin/profiles/:id`,async t=>{let r=En.parse(await t.req.json()),i=n(t.req.query(`scope`)),a=await e.upsertProfile({...r,id:t.req.param(`id`),providerType:r.providerType??null},i);return t.json(a)}),t.put(`/admin/active-profile`,async t=>{let r=n(t.req.query(`scope`)),i=h.z.object({profileId:h.z.string().min(1),slot:Cn.default(`default`)}).parse(await t.req.json()),a=await e.setActiveProfile(i.profileId,r,i.slot);return t.json(a)}),t.put(`/admin/current-model`,async t=>{let r=n(t.req.query(`scope`)),i=h.z.object({model:h.z.string().min(1),slot:Cn.default(`default`)}).parse(await t.req.json()),a=await e.switchModel(i.model,r,i.slot);return t.json(a)}),t.put(`/admin/config`,async t=>{let r=n(t.req.query(`scope`)),i=Tn.parse(await t.req.json());return t.json(await e.updateAdminConfig(i,r))}),t.delete(`/admin/history`,async t=>{let r=n(t.req.query(`scope`));return t.json(await e.clearHistory(r))}),t}const On=`default`,kn=`default`,An=`MODEL_PROXY_MCP_SCOPE`,jn=`MODEL_PROXY_MCP_SLOT`,Q=h.z.enum([`default`,`sonnet`,`opus`,`haiku`,`subagent`]);function Mn(e=new bn,t=process.env){let n=e=>{let n=e?.scope;return typeof n==`string`&&n.trim()?n:t.MODEL_PROXY_MCP_SCOPE||`default`},r=e=>{let n=typeof e?.slot==`string`?e.slot:t.MODEL_PROXY_MCP_SLOT||`default`;return Q.parse(n)},i=new ne.Server({name:`model-proxy-mcp`,version:`0.1.0`},{capabilities:{tools:{}}});return i.setRequestHandler(re.ListToolsRequestSchema,async()=>({tools:[{name:`get_proxy_status`,description:`Get the HTTP proxy status, auth status, and active profile.`,inputSchema:{type:`object`,properties:{scope:{type:`string`}},additionalProperties:!1}},{name:`list_profiles`,description:`List all configured model proxy profiles.`,inputSchema:{type:`object`,properties:{scope:{type:`string`}},additionalProperties:!1}},{name:`get_active_profile`,description:`Get the active model proxy profile.`,inputSchema:{type:`object`,properties:{scope:{type:`string`},slot:{type:`string`,enum:Q.options}},additionalProperties:!1}},{name:`get_current_model`,description:`Get the currently active model and profile settings.`,inputSchema:{type:`object`,properties:{scope:{type:`string`},slot:{type:`string`,enum:Q.options}},additionalProperties:!1}},{name:`set_model_target`,description:`Set the scoped slot main target directly by provider, model, and reasoning effort.`,inputSchema:{type:`object`,properties:{provider:{type:`string`},model:{type:`string`},reasoningEffort:{type:`string`,enum:[`minimal`,`low`,`medium`,`high`]},scope:{type:`string`},slot:{type:`string`,enum:Q.options},thinkingDisabled:{type:`boolean`}},required:[`provider`,`model`,`reasoningEffort`],additionalProperties:!1}},{name:`upsert_profile`,description:`Create or update a Codex model profile.`,inputSchema:{type:`object`,properties:{id:{type:`string`},label:{type:`string`},provider:{type:`string`},model:{type:`string`},endpoint:{type:`string`},reasoningEffort:{type:`string`,enum:[`minimal`,`low`,`medium`,`high`]},enabled:{type:`boolean`},scope:{type:`string`}},required:[`id`,`label`,`model`,`endpoint`,`reasoningEffort`,`enabled`],additionalProperties:!1}}]})),i.setRequestHandler(re.CallToolRequestSchema,async t=>{let{name:i,arguments:a}=t.params;try{switch(i){case`get_proxy_status`:return $(JSON.stringify(await e.getStatus(n(a)),null,2));case`list_profiles`:return $(JSON.stringify(await e.listProfiles(n(a)),null,2));case`get_active_profile`:return $(JSON.stringify(await e.getActiveProfile(n(a),r(a)),null,2));case`get_current_model`:return $(JSON.stringify(await e.getCurrentModel(n(a),r(a)),null,2));case`set_model_target`:{let t=n(a),i=r(a),o=await e.getAdminConfig(t);return $(JSON.stringify(await e.updateAdminConfig({models:{[i]:{main:{provider:String(a?.provider),model:String(a?.model),reasoningEffort:h.z.enum([`minimal`,`low`,`medium`,`high`]).parse(String(a?.reasoningEffort)),thinkingDisabled:a?.thinkingDisabled===void 0?!1:!!a.thinkingDisabled},fallbacks:o.scopeModels[i]?.fallbacks??[]}}},t),null,2))}case`upsert_profile`:return $(JSON.stringify(await e.upsertProfile({id:String(a?.id),label:String(a?.label),provider:String(a?.provider||`chatgpt-codex`),providerType:null,model:String(a?.model),endpoint:String(a?.endpoint),reasoningEffort:String(a?.reasoningEffort),enabled:!!a?.enabled},n(a)),null,2));default:throw Error(`Unknown tool: ${i}`)}}catch(e){return{content:[{type:`text`,text:`Error executing tool ${i}: ${e instanceof Error?e.message:String(e)}`}],isError:!0}}}),i}function $(e){return{content:[{type:`text`,text:e}]}}var Nn=class{transport=null;constructor(e){this.server=e}async start(){this.transport=new ie.StdioServerTransport,await this.server.connect(this.transport)}async stop(){this.transport&&=(await this.transport.close(),null)}};Object.defineProperty(exports,`a`,{enumerable:!0,get:function(){return Ot}}),Object.defineProperty(exports,`c`,{enumerable:!0,get:function(){return 43191}}),Object.defineProperty(exports,`i`,{enumerable:!0,get:function(){return bn}}),Object.defineProperty(exports,`l`,{enumerable:!0,get:function(){return y}}),Object.defineProperty(exports,`n`,{enumerable:!0,get:function(){return Mn}}),Object.defineProperty(exports,`o`,{enumerable:!0,get:function(){return ke}}),Object.defineProperty(exports,`r`,{enumerable:!0,get:function(){return Dn}}),Object.defineProperty(exports,`s`,{enumerable:!0,get:function(){return b}}),Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return Nn}}),Object.defineProperty(exports,`u`,{enumerable:!0,get:function(){return s}});
1132
+ `,{headers:{"content-type":`text/html; charset=utf-8`}})),t.get(`/admin/scopes`,async t=>t.json({scopes:await e.listScopes()})),t.get(`/admin/config`,async t=>{let r=n(t.req.query(`scope`));return t.json(await e.getAdminConfig(r))}),t.get(`/admin/profiles`,async t=>{let r=n(t.req.query(`scope`));return t.json({profiles:await e.listProfiles(r)})}),t.get(`/admin/current-model`,async t=>{let r=n(t.req.query(`scope`)),i=t.req.query(`slot`)||`default`;return t.json({profile:await e.getCurrentModel(r,i)})}),t.get(`/admin/history`,async t=>{let r=n(t.req.query(`scope`)),i=h.z.coerce.number().int().positive().max(200).default(50).parse(t.req.query(`limit`)??50),a=t.req.query(`cursor`)||void 0;return t.json(await e.listHistory(r,i,a))}),t.get(`/admin/history/stats`,async t=>{let r=n(t.req.query(`scope`));return t.json(await e.getHistoryStats(r))}),t.put(`/admin/profiles/:id`,async t=>{let r=En.parse(await t.req.json()),i=n(t.req.query(`scope`)),a=await e.upsertProfile({...r,id:t.req.param(`id`),providerType:r.providerType??null},i);return t.json(a)}),t.put(`/admin/active-profile`,async t=>{let r=n(t.req.query(`scope`)),i=h.z.object({profileId:h.z.string().min(1),slot:Cn.default(`default`)}).parse(await t.req.json()),a=await e.setActiveProfile(i.profileId,r,i.slot);return t.json(a)}),t.put(`/admin/current-model`,async t=>{let r=n(t.req.query(`scope`)),i=h.z.object({model:h.z.string().min(1),slot:Cn.default(`default`)}).parse(await t.req.json()),a=await e.switchModel(i.model,r,i.slot);return t.json(a)}),t.put(`/admin/config`,async t=>{let r=n(t.req.query(`scope`)),i=Tn.parse(await t.req.json());return t.json(await e.updateAdminConfig(i,r))}),t.delete(`/admin/history`,async t=>{let r=n(t.req.query(`scope`));return t.json(await e.clearHistory(r))}),t}const On=`default`,kn=`default`,An=`MODEL_PROXY_MCP_SCOPE`,jn=`MODEL_PROXY_MCP_SLOT`,Q=h.z.enum([`default`,`sonnet`,`opus`,`haiku`,`subagent`]);function Mn(e=new bn,t=process.env){let n=e=>{let n=e?.scope;return typeof n==`string`&&n.trim()?n:t.MODEL_PROXY_MCP_SCOPE||`default`},r=e=>{let n=typeof e?.slot==`string`?e.slot:t.MODEL_PROXY_MCP_SLOT||`default`;return Q.parse(n)},i=new ne.Server({name:`model-proxy-mcp`,version:`0.1.0`},{capabilities:{tools:{}}});return i.setRequestHandler(re.ListToolsRequestSchema,async()=>({tools:[{name:`get_proxy_status`,description:`Get the HTTP proxy status, auth status, and active profile.`,inputSchema:{type:`object`,properties:{scope:{type:`string`}},additionalProperties:!1}},{name:`list_profiles`,description:`List all configured model proxy profiles.`,inputSchema:{type:`object`,properties:{scope:{type:`string`}},additionalProperties:!1}},{name:`get_active_profile`,description:`Get the active model proxy profile.`,inputSchema:{type:`object`,properties:{scope:{type:`string`},slot:{type:`string`,enum:Q.options}},additionalProperties:!1}},{name:`get_current_model`,description:`Get the currently active model and profile settings.`,inputSchema:{type:`object`,properties:{scope:{type:`string`},slot:{type:`string`,enum:Q.options}},additionalProperties:!1}},{name:`set_model_target`,description:`Set the scoped slot main target directly by provider, model, and reasoning effort.`,inputSchema:{type:`object`,properties:{provider:{type:`string`},model:{type:`string`},reasoningEffort:{type:`string`,enum:[`minimal`,`low`,`medium`,`high`]},scope:{type:`string`},slot:{type:`string`,enum:Q.options},thinkingDisabled:{type:`boolean`}},required:[`provider`,`model`,`reasoningEffort`],additionalProperties:!1}},{name:`upsert_profile`,description:`Create or update a Codex model profile.`,inputSchema:{type:`object`,properties:{id:{type:`string`},label:{type:`string`},provider:{type:`string`},model:{type:`string`},endpoint:{type:`string`},reasoningEffort:{type:`string`,enum:[`minimal`,`low`,`medium`,`high`]},enabled:{type:`boolean`},scope:{type:`string`}},required:[`id`,`label`,`model`,`endpoint`,`reasoningEffort`,`enabled`],additionalProperties:!1}}]})),i.setRequestHandler(re.CallToolRequestSchema,async t=>{let{name:i,arguments:a}=t.params;try{switch(i){case`get_proxy_status`:return $(JSON.stringify(await e.getStatus(n(a)),null,2));case`list_profiles`:return $(JSON.stringify(await e.listProfiles(n(a)),null,2));case`get_active_profile`:return $(JSON.stringify(await e.getActiveProfile(n(a),r(a)),null,2));case`get_current_model`:return $(JSON.stringify(await e.getCurrentModel(n(a),r(a)),null,2));case`set_model_target`:{let t=n(a),i=r(a),o=await e.getAdminConfig(t);return $(JSON.stringify(await e.updateAdminConfig({models:{[i]:{main:{provider:String(a?.provider),model:String(a?.model),reasoningEffort:h.z.enum([`minimal`,`low`,`medium`,`high`]).parse(String(a?.reasoningEffort)),thinkingDisabled:a?.thinkingDisabled===void 0?!1:!!a.thinkingDisabled},fallbacks:o.scopeModels[i]?.fallbacks??[]}}},t),null,2))}case`upsert_profile`:return $(JSON.stringify(await e.upsertProfile({id:String(a?.id),label:String(a?.label),provider:String(a?.provider||`chatgpt-codex`),providerType:null,model:String(a?.model),endpoint:String(a?.endpoint),reasoningEffort:String(a?.reasoningEffort),enabled:!!a?.enabled},n(a)),null,2));default:throw Error(`Unknown tool: ${i}`)}}catch(e){return{content:[{type:`text`,text:`Error executing tool ${i}: ${e instanceof Error?e.message:String(e)}`}],isError:!0}}}),i}function $(e){return{content:[{type:`text`,text:e}]}}var Nn=class{transport=null;constructor(e){this.server=e}async start(){this.transport=new ie.StdioServerTransport,await this.server.connect(this.transport)}async stop(){this.transport&&=(await this.transport.close(),null)}};Object.defineProperty(exports,`a`,{enumerable:!0,get:function(){return Ot}}),Object.defineProperty(exports,`c`,{enumerable:!0,get:function(){return 43191}}),Object.defineProperty(exports,`i`,{enumerable:!0,get:function(){return bn}}),Object.defineProperty(exports,`l`,{enumerable:!0,get:function(){return y}}),Object.defineProperty(exports,`n`,{enumerable:!0,get:function(){return Mn}}),Object.defineProperty(exports,`o`,{enumerable:!0,get:function(){return ke}}),Object.defineProperty(exports,`r`,{enumerable:!0,get:function(){return Dn}}),Object.defineProperty(exports,`s`,{enumerable:!0,get:function(){return b}}),Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return Nn}}),Object.defineProperty(exports,`u`,{enumerable:!0,get:function(){return s}});
1133
+ //# sourceMappingURL=stdio-B-atmzW4.cjs.map