@dahawa/hawa-code 1.3.7 → 1.3.8
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/cli.js +1 -1
- package/package.json +1 -1
package/cli.js
CHANGED
|
@@ -552,7 +552,7 @@ Important:
|
|
|
552
552
|
`);let n=r.slice(-LRe);return[ct.grey(`Showing last ${LRe} lines of ${e} total lines`),...n].join(`
|
|
553
553
|
`)}function WG({content:t,lines:e,verbose:r,isError:n}){return YG.createElement(B,{justifyContent:"space-between",width:"100%"},YG.createElement(B,{flexDirection:"row"},YG.createElement(R,null,"\xA0\xA0\u23BF \xA0"),YG.createElement(B,{flexDirection:"column"},YG.createElement(R,{color:n?ce().error:void 0},r?t.trim():myi(t.trim(),e)))))}var YG,elt=p(async()=>{await He();YG=H(ge(),1);Ht();kRe();za()});var L_,_yi,lan,dan=p(async()=>{await He();L_=H(ge(),1);Fi();await ep();Ht();san();await elt();_yi=Ue.object({}).passthrough(),lan={async isEnabled(){return!0},isReadOnly(){return!1},name:"mcp",async description(){return""},async prompt(){return""},inputSchema:_yi,async*call(){yield{type:"result",data:"",resultForAssistant:""}},needsPermissions(){return!0},renderToolUseMessage(t){return Object.entries(t).map(([e,r])=>`${e}: ${JSON.stringify(r)}`).join(", ")},userFacingName:()=>"mcp",renderToolUseRejectedMessage(){return L_.createElement(ii,null)},renderToolResultMessage(t,{verbose:e}){if(Array.isArray(t))return L_.createElement(B,{flexDirection:"column"},t.map((n,o)=>{if(n.type==="image")return L_.createElement(B,{key:o,justifyContent:"space-between",overflowX:"hidden",width:"100%"},L_.createElement(B,{flexDirection:"row"},L_.createElement(R,null,"\xA0\xA0\u23BF \xA0"),L_.createElement(R,null,"[Image]")));let i=n.text.split(`
|
|
554
554
|
`).length;return L_.createElement(WG,{key:o,content:n.text,lines:i,verbose:e})}));if(!t)return L_.createElement(B,{justifyContent:"space-between",overflowX:"hidden",width:"100%"},L_.createElement(B,{flexDirection:"row"},L_.createElement(R,null,"\xA0\xA0\u23BF \xA0"),L_.createElement(R,{color:ce().secondaryText},"(No content)")));let r=t.split(`
|
|
555
|
-
`).length;return L_.createElement(WG,{content:t,lines:r,verbose:e})},renderResultForAssistant(t){return t}}});import{existsSync as fan,readFileSync as man,writeFileSync as han}from"fs";import{join as gan}from"path";function _an(t,e){FRe={name:t,client:e},PS.cache.clear?.(),KG.cache.clear?.(),Zd.clear()}function Eyi(){PS.cache.clear?.(),KG.cache.clear?.(),Zd.clear()}function Ean(t){let e={};if(t)for(let r of t){let[n,...o]=r.split("=");if(!n||o.length===0)throw new Error(`Invalid environment variable format: ${r}, environment variables should be added as: -e KEY1=value1 -e KEY2=value2`);e[n]=o.join("=")}return e}function URe(t){if(!t)return"project";let e=process.env.USER_TYPE==="external"?Syi:Ayi;if(!e.includes(t))throw new Error(`Invalid scope: ${t}. Must be one of: ${e.join(", ")}`);return t}function tlt(t,e,r="project"){if(r==="mcprc"){let n=gan(Ye(),".mcp.json"),o={};if(fan(n))try{let i=man(n,"utf-8"),s=U0(i);s&&typeof s=="object"&&(o=s)}catch{}o[t]=e;try{han(n,JSON.stringify(o,null,2),"utf-8")}catch(i){throw new Error(`Failed to write to .mcp.json: ${i}`)}}else if(r==="global"){let n=tr();n.mcpServers||(n.mcpServers={}),n.mcpServers[t]=e,uo(n)}else{let n=ln();n.mcpServers||(n.mcpServers={}),n.mcpServers[t]=e,ui(n)}}function Aan(t,e="project"){if(e==="mcprc"){let r=gan(Ye(),".mcp.json");if(!fan(r))throw new Error("No .mcp.json file found in this directory");try{let n=man(r,"utf-8"),o=U0(n);if(!o||typeof o!="object"||!o[t])throw new Error(`No MCP server found with name: ${t} in .mcp.json`);delete o[t],han(r,JSON.stringify(o,null,2),"utf-8")}catch(n){throw n instanceof Error?n:new Error(`Failed to remove from .mcp.json: ${n}`)}}else if(e==="global"){let r=tr();if(!r.mcpServers?.[t])throw new Error(`No global MCP server found with name: ${t}`);delete r.mcpServers[t],uo(r)}else{let r=ln();if(!r.mcpServers?.[t])throw new Error(`No local MCP server found with name: ${t}`);delete r.mcpServers[t],ui(r)}}function San(){let t=tr(),e=bR(),r=ln();return{...t.mcpServers??{},...e??{},...r.mcpServers??{}}}function $te(t){let e=ln(),r=bR(),n=tr();if(e.mcpServers?.[t])return{...e.mcpServers[t],scope:"project"};if(r?.[t])return{...r[t],scope:"mcprc"};if(n.mcpServers?.[t])return{...n.mcpServers[t],scope:"global"}}async function yan(t,e){let r=e.type==="sse"?new BRe(new URL(e.url)):new IRe({command:e.command,args:e.args,env:{...process.env,...e.env},stderr:"pipe"}),n=new UG({name:`${Qe}`,version:"0.1.0"},{capabilities:{}}),o=e.timeout??5e3,i=n.connect(r),s=new Promise((a,c)=>{let u=setTimeout(()=>{c(new Error(`Connection to MCP server "${t}" timed out after ${o}ms`))},o);i.then(()=>clearTimeout(u),()=>clearTimeout(u))});return await Promise.race([i,s]),e.type==="stdio"&&r.stderr?.on("data",a=>{let c=a.toString().trim();c&&sL(t,`Server stderr: ${c}`)}),n}function Hte(t){let e=ln();return e.approvedMcprcServers?.includes(t)?"approved":e.rejectedMcprcServers?.includes(t)?"rejected":"pending"}async function Can(t,e,r){let n=await PS();return(await Promise.allSettled(n.map(async i=>{if(i.type!=="connected")return null;try{return(await i.client.getServerCapabilities())?.[r]?{client:i,result:await i.client.request(t,e)}:null}catch(s){return i.type==="connected"&&sL(i.name,`Failed to request '${t.method}': ${s instanceof Error?s.message:String(s)}`),null}}))).filter(i=>i.status==="fulfilled").map(i=>i.value).filter(i=>i!==null)}function pan(t){return t.replaceAll(".","-")}async function yyi({client:{client:t,name:e},tool:r,args:n}){let o=await t.callTool({name:r,arguments:n},ZT);if("isError"in o&&o.isError){let i=`Error calling tool ${r}: ${o.error}`;throw sL(e,i),Error(JSON.stringify(o))}if("toolResult"in o)return String(o.toolResult);if("content"in o&&Array.isArray(o.content))return o.content.map(i=>i.type==="image"?{type:"image",source:{type:"base64",data:String(i.data),media_type:i.mimeType}}:i);throw Error(`Unexpected response format from tool ${r}`)}async function Tyi({name:t,client:e},r){try{return(await e.client.getPrompt({name:t,arguments:r})).messages.map(o=>({role:o.role,content:[o.content.type==="text"?{type:"text",text:o.content.text}:{type:"image",source:{data:String(o.content.data),media_type:o.content.mimeType,type:"base64"}}]}))}catch(n){throw sL(e.name,`Error running command '${t}': ${n instanceof Error?n.message:String(n)}`),n}}var FRe,Ayi,Syi,Tan,ban,PS,KG,van,uN=p(async()=>{yi();_o();wn();uL();Sut();Psn();ian();eb();yi();await dan();at();Ho();P9();Lt();FRe=null;Ayi=["project","global","mcprc"],Syi=["project","global"];Tan=async t=>{Hte(t)==="rejected"&&uGt(t);let r=$te(t);if(!r)return{name:t,type:"failed"};try{let n=await yan(t,r),o=await PS(),i={name:t,client:n,type:"connected"},s=o.findIndex(a=>a.name===t);return s!==-1?o[s]=i:o.push(i),i}catch{return{name:t,type:"failed"}}},ban=()=>(Eyi(),PS()),PS=Jt(async()=>{if(process.env.CI)return[];let t=tr().mcpServers??{},e=bR(),r=ln().mcpServers??{},n=rde(e,(u,l)=>Hte(l)==="approved"),o=rde(e,(u,l)=>Hte(l)==="rejected"),i=Object.keys(o).map(u=>({name:u,type:"disabled"})),s={...t,...n,...r},a=await Promise.all(Object.entries(s).map(async([u,l])=>{try{let d=await yan(u,l);return ke("tengu_mcp_server_connection_succeeded",{}),{name:u,client:d,type:"connected"}}catch(d){return ke("tengu_mcp_server_connection_failed",{}),sL(u,`Connection failed: ${d instanceof Error?d.message:String(d)}`),{name:u,type:"failed"}}})),c=FRe?[{name:FRe.name,client:FRe.client,type:"connected"}]:[];return[...a,...c,...i]});KG=Jt(async()=>{let e=(await Can({method:"tools/list"},zee,"tools")).flatMap(({client:r,result:{tools:n}})=>n.map(o=>({...lan,name:"mcp_"+pan(r.name)+"_"+pan(o.name),async description(){return o.description??""},async prompt(){return o.description??""},inputJSONSchema:o.inputSchema,async*call(i){console.log(o.name,i);let s=await yyi({client:r,tool:o.name,args:i});yield{type:"result",data:s,resultForAssistant:JSON.stringify(s)}},userFacingName(){return`${r.name}:${o.name} (MCP)`}})));return e.forEach(r=>{wt(r.name+"|")}),e});van=Jt(async()=>(await Can({method:"prompts/list"},Vee,"prompts")).flatMap(({client:e,result:r})=>r.prompts?.map(n=>{let o=Object.values(n.arguments??{}).map(i=>i.name);return{type:"prompt",name:"mcp__"+e.name+"__"+n.name,description:n.description??"",isEnabled:!0,isHidden:!1,progressMessage:"running",userFacingName(){return`${e.name}:${n.name} (MCP)`},argNames:o,async getPromptForCommand(i){let s=i.split(" ");return await Tyi({name:n.name,client:e},w8e(o,s))}}})))});import qRe from"path";import byi from"os";import G9 from"fs";function ab(){return GRe.getInstance()}function xan(){return GRe.getInstance().selection_changed}var GRe,JG=p(async()=>{at();Sut();Qin();P9();await uN();wn();Lt();GRe=class t{static instance;currentConnection=null;autoConnectEnabled=!1;selection_changed=null;statusChangeCallback=null;onToolsReload=null;claudeDir;constructor(){this.claudeDir=qRe.join(byi.homedir(),".claude","ide")}findServers(){return G9.existsSync(this.claudeDir)?G9.readdirSync(this.claudeDir).filter(r=>r.endsWith(".lock")).map(r=>{let n=qRe.join(this.claudeDir,r);try{let o=JSON.parse(G9.readFileSync(n,"utf8"));return{port:parseInt(r.replace(".lock","")),authToken:o.authToken,workspaceFolders:o.workspaceFolders||[],ideName:o.ideName,pid:o.pid,lockFile:n}}catch(o){return Xt(`Failed to parse lock file ${r}:`,o),null}}).filter(r=>r!==null):[]}static getInstance(){return t.instance||(t.instance=new t),t.instance}getAvailableServers(){return this.findServers()}getCurrentConnection(){return this.currentConnection}setCurrentConnection(e){this.currentConnection=e}isAutoConnectEnabled(){return this.autoConnectEnabled}setAutoConnectEnabled(e){this.autoConnectEnabled=e}setStatusChangeCallback(e){this.statusChangeCallback=e}notifyReminderText(){if(this.statusChangeCallback){let e=this.selection_changed,r;e.selection.isEmpty?r=`current file : ${e.filePath.split("/").pop()||e.filePath.split("\\").pop()||e.file}`:r=`current file: ${e.filePath.split("/").pop()||e.filePath.split("\\").pop()||e.file} select ${e.selection.end.line-e.selection.start.line+1} rows`,this.statusChangeCallback(r)}}async connectToServer(e){try{wt(`\u{1F517} Connecting to ${e.ideName} at port ${e.port} key${e.authToken} ...`);let r=new UG({name:`${Vi}-ide-client`,version:"1.0.0"}),n=new CRe(`ws://127.0.0.1:${e.port}`,e.authToken,["getDiagnostics","executeCode"]);n.onnotification=(o,i)=>{o.method=="selection_changed"&&(this.selection_changed=o.params,this.notifyReminderText())},wt("\u{1F504} Establishing MCP connection..."),await r.connect(n),wt("\u2705 MCP connection established");try{return this.currentConnection=e,this.mcpClient=r,this.mcpTransport=n,_an("___ide_",r),Promise.all([Zd.getTools(!0)]).then(o=>{this.onToolsReload&&this.onToolsReload(o[0])}).catch(o=>{Cs(o)}),!0}catch(o){throw Xt("\u274C Failed to verify connection by listing tools:",o),await n.close(),o}}catch(r){return Xt(`Failed to connect to IDE server ${e.ideName}: ${r}`),!1}}async disconnect(){try{this.mcpTransport&&(await this.mcpTransport.close(),wt("\u{1F512} MCP transport connection closed")),this.currentConnection=null,this.mcpClient=null,this.mcpTransport=null,Cs("\u2705 IDE connection disconnected")}catch(e){Le(`Error during disconnect: ${e}`),this.currentConnection=null,this.mcpClient=null,this.mcpTransport=null}}hasAvailableConnections(){return this.getAvailableServers().length>0}getMCPClient(){return this.mcpClient||null}async autoConnectToMatchingServer(){try{let e=$l();if(!e)return wt("\u{1F4C1} No current project path available for auto-connect"),!1;wt(`\u{1F50D} Looking for IDE servers matching project path: ${e}`);let r=this.getAvailableServers();if(r.length===0)return wt("\u{1F50D} No IDE servers available for auto-connect"),!1;let n=r.filter(i=>i.workspaceFolders.some(s=>{let a=qRe.normalize(s),c=qRe.normalize(e);return wt(`\u{1F50D} Comparing workspace folder: ${a} with current path: ${c}`),G9.existsSync(a)?G9.statSync(a).ino===G9.statSync(c).ino:(wt(`Project folder "${a}" does not exist, it may have been deleted. IDE connection cannot be established.`),!1)}));if(n.length===0)return wt("\u{1F50D} No matching IDE server found for current project"),!1;let o=n[0];if(n.length>1){wt(`\u{1F50D} Found ${n.length} matching servers, selecting most recently modified one`);let i=0;for(let s of n)try{let a=G9.statSync(s.lockFile);a.mtime.getTime()>i&&(i=a.mtime.getTime(),o=s)}catch(a){wt(`Failed to get modification time for ${s.lockFile}: ${a}`)}}return o?(wt(`\u{1F3AF} Found matching IDE server: ${o.ideName} with workspace: ${o.workspaceFolders.join(", ")}`),!this.currentConnection||this.currentConnection.lockFile!==o.lockFile?(wt("\u{1F504} Attempting auto-connection to matching IDE server..."),await this.connectToServer(o)?(wt(`\u2705 Successfully auto-connected to ${o.ideName}`),!0):(wt(`\u274C Failed to auto-connect to ${o.ideName}`),!1)):(wt("\u2705 Already connected to matching IDE server"),!0)):(wt("\u{1F50D} No matching IDE server found for current project"),!1)}catch(e){return Le(`Error during auto-connect attempt: ${e}`),!1}}async callTool(e,r={}){let n=this.getMCPClient();if(!n)throw new Error("No active IDE connection");try{return await n.callTool({name:e,arguments:r})}catch(o){throw Le(`Failed to call tool ${e}: ${o}`),o}}async callToolOneWay(e,r={}){let n=this.getMCPClient();if(!n)throw new Error("No active IDE connection");try{await n.notification({method:"tools/call",params:{name:e,arguments:r}})}catch(o){throw Le(`Failed to call tool ${e}: ${o}`),o}}async getCurrentSelection(){return this.callTool("getCurrentSelection",{})}async getCurrentFile(){return this.callTool("getCurrentFile",{})}async isConnectionAlive(){try{let e=this.getMCPClient();if(!e||!this.currentConnection)return!1;try{return await e.ping(),Cs("\u{1F50D} Connection alive check: ping successful"),!0}catch(r){return Xt("Connection ping failed:",r),!1}}catch(e){return Xt("Connection check failed:",e),!1}}}});var rlt,Wm,XG=p(()=>{rlt=class{mode;setMode(e,r=!1){this.mode=e,r&&this.setModeUI(e)}isPlanMode(){return this.mode==="plan"}isEditMode(){return this.mode==="edit"}setModeUI(e){}},Wm=new rlt});import{spawn as Cyi}from"child_process";var HRe,Ian=p(async()=>{at();await Qte();HRe=class{constructor(e,r,n){this.config=e;this.projectPath=r;this.rootUri=n}process=null;messageId=0;pendingRequests=new Map;isInitialized=!1;buffer="";synchronizedDocuments=new Set;documentVersions=new Map;async start(){try{if(!await BS.checkCommandExists(this.config.command))throw new Error(`Failed to start LSP server: ${this.config.command} \u4E0D\u5B58\u5728`);if(this.process=Cyi(this.config.command,this.config.args,{cwd:this.projectPath,stdio:["pipe","pipe","pipe"],shell:!0}),!this.process.stdin||!this.process.stdout)throw new Error("Failed to create LSP process pipes");this.process.stdout.on("data",e=>{this.handleData(e.toString())}),this.process.stderr.on("data",e=>{console.error(`LSP stderr: ${e.toString()}`)}),this.process.on("error",e=>{console.error(`LSP process error: ${e.message}`),this.cleanupPendingRequests(e)}),this.process.on("exit",e=>{this.cleanupPendingRequests(new Error("LSP process exited"))}),await this.initialize()}catch(e){throw new Error(`Failed to start LSP server: ${e}`)}}async stop(){if(this.process)try{await this.shutdown(),this.process.kill(),this.process=null,this.isInitialized=!1}catch(e){console.error(`Error stopping LSP server: ${e}`)}}isReady(){return this.isInitialized&&this.process!==null}async goToDefinition(e,r){let n=await this.sendRequest("textDocument/definition",{textDocument:{uri:e},position:r});return this.parseLocations(n)}async findReferences(e,r){let n=await this.sendRequest("textDocument/references",{textDocument:{uri:e},position:r,context:{includeDeclaration:!0}});return this.parseLocations(n)}async hover(e,r){let n=await this.sendRequest("textDocument/hover",{textDocument:{uri:e},position:r});return n?{contents:this.extractHoverContents(n),range:n.range}:null}async documentSymbol(e){let r=await this.sendRequest("textDocument/documentSymbol",{textDocument:{uri:e}});return this.parseSymbols(r)}async workspaceSymbol(e){let r=await this.sendRequest("workspace/symbol",{query:e});return this.parseSymbols(r)}async goToImplementation(e,r){let n=await this.sendRequest("textDocument/implementation",{textDocument:{uri:e},position:r});return this.parseLocations(n)}async prepareCallHierarchy(e,r){let n=await this.sendRequest("textDocument/prepareCallHierarchy",{textDocument:{uri:e},position:r});return this.parseCallHierarchyItems(n)}async incomingCalls(e){let r=await this.sendRequest("callHierarchy/incomingCalls",{item:e});return this.parseIncomingCalls(r)}async outgoingCalls(e){let r=await this.sendRequest("callHierarchy/outgoingCalls",{item:e});return this.parseOutgoingCalls(r)}async openDocument(e,r,n,o){await this.sendNotification("textDocument/didOpen",{textDocument:{uri:e,languageId:r,version:n,text:o}}),this.synchronizedDocuments.add(e),this.documentVersions.set(e,n)}async changeDocument(e,r,n){if(!this.synchronizedDocuments.has(e))throw new Error(`Document ${e} is not synchronized. Call openDocument first.`);await this.sendNotification("textDocument/didChange",{textDocument:{uri:e,version:r},contentChanges:n}),this.documentVersions.set(e,r)}async saveDocument(e,r){if(!this.synchronizedDocuments.has(e))throw new Error(`Document ${e} is not synchronized. Call openDocument first.`);let n={textDocument:{uri:e}};r!==void 0&&(n.text=r),await this.sendNotification("textDocument/didSave",n)}async closeDocument(e){this.synchronizedDocuments.has(e)&&(await this.sendNotification("textDocument/didClose",{textDocument:{uri:e}}),this.synchronizedDocuments.delete(e),this.documentVersions.delete(e))}isDocumentSynchronized(e){return this.synchronizedDocuments.has(e)}getDocumentVersion(e){return this.documentVersions.get(e)}getLanguageIdFromUri(e){switch(e.split(".").pop()?.toLowerCase()){case"ts":return"typescript";case"tsx":return"typescriptreact";case"js":return"javascript";case"jsx":return"javascriptreact";case"py":return"python";case"go":return"go";case"java":return"java";case"c":return"c";case"cpp":case"cc":case"cxx":return"cpp";case"cs":return"csharp";case"php":return"php";case"kt":return"kotlin";case"rs":return"rust";case"rb":return"ruby";case"html":return"html";case"css":return"css";case"scss":case"sass":return"scss";default:return"plaintext"}}async initialize(){let e={processId:process.pid,rootUri:`file://${this.rootUri}`,capabilities:{textDocument:{definition:{dynamicRegistration:!0},references:{dynamicRegistration:!0},hover:{dynamicRegistration:!0},documentSymbol:{dynamicRegistration:!0},implementation:{dynamicRegistration:!0},callHierarchy:{dynamicRegistration:!0}},workspace:{symbol:{dynamicRegistration:!0}}},initializationOptions:this.config.initializationOptions};await this.sendRequest("initialize",e),await this.sendNotification("initialized",{}),this.isInitialized=!0}async shutdown(){this.isInitialized&&(await this.sendRequest("shutdown",{}),await this.sendNotification("exit",{}))}handleData(e){for(this.buffer+=e;;){let r=this.buffer.indexOf(`\r
|
|
555
|
+
`).length;return L_.createElement(WG,{content:t,lines:r,verbose:e})},renderResultForAssistant(t){return t}}});import{existsSync as fan,readFileSync as man,writeFileSync as han}from"fs";import{join as gan}from"path";function _an(t,e){FRe={name:t,client:e},PS.cache.clear?.(),KG.cache.clear?.(),Zd.clear()}function Eyi(){PS.cache.clear?.(),KG.cache.clear?.(),Zd.clear()}function Ean(t){let e={};if(t)for(let r of t){let[n,...o]=r.split("=");if(!n||o.length===0)throw new Error(`Invalid environment variable format: ${r}, environment variables should be added as: -e KEY1=value1 -e KEY2=value2`);e[n]=o.join("=")}return e}function URe(t){if(!t)return"project";let e=process.env.USER_TYPE==="external"?Syi:Ayi;if(!e.includes(t))throw new Error(`Invalid scope: ${t}. Must be one of: ${e.join(", ")}`);return t}function tlt(t,e,r="project"){if(r==="mcprc"){let n=gan(Ye(),".mcp.json"),o={};if(fan(n))try{let i=man(n,"utf-8"),s=U0(i);s&&typeof s=="object"&&(o=s)}catch{}o[t]=e;try{han(n,JSON.stringify(o,null,2),"utf-8")}catch(i){throw new Error(`Failed to write to .mcp.json: ${i}`)}}else if(r==="global"){let n=tr();n.mcpServers||(n.mcpServers={}),n.mcpServers[t]=e,uo(n)}else{let n=ln();n.mcpServers||(n.mcpServers={}),n.mcpServers[t]=e,ui(n)}}function Aan(t,e="project"){if(e==="mcprc"){let r=gan(Ye(),".mcp.json");if(!fan(r))throw new Error("No .mcp.json file found in this directory");try{let n=man(r,"utf-8"),o=U0(n);if(!o||typeof o!="object"||!o[t])throw new Error(`No MCP server found with name: ${t} in .mcp.json`);delete o[t],han(r,JSON.stringify(o,null,2),"utf-8")}catch(n){throw n instanceof Error?n:new Error(`Failed to remove from .mcp.json: ${n}`)}}else if(e==="global"){let r=tr();if(!r.mcpServers?.[t])throw new Error(`No global MCP server found with name: ${t}`);delete r.mcpServers[t],uo(r)}else{let r=ln();if(!r.mcpServers?.[t])throw new Error(`No local MCP server found with name: ${t}`);delete r.mcpServers[t],ui(r)}}function San(){let t=tr(),e=bR(),r=ln();return{...t.mcpServers??{},...e??{},...r.mcpServers??{}}}function $te(t){let e=ln(),r=bR(),n=tr();if(e.mcpServers?.[t])return{...e.mcpServers[t],scope:"project"};if(r?.[t])return{...r[t],scope:"mcprc"};if(n.mcpServers?.[t])return{...n.mcpServers[t],scope:"global"}}async function yan(t,e){let r=e.type==="sse"?new BRe(new URL(e.url)):new IRe({command:e.command,args:e.args,env:{...process.env,...e.env},stderr:"pipe"}),n=new UG({name:`${Qe}`,version:"0.1.0"},{capabilities:{}}),o=e.timeout??5e3,i=n.connect(r),s=new Promise((a,c)=>{let u=setTimeout(()=>{c(new Error(`Connection to MCP server "${t}" timed out after ${o}ms`))},o);i.then(()=>clearTimeout(u),()=>clearTimeout(u))});return await Promise.race([i,s]),e.type==="stdio"&&r.stderr?.on("data",a=>{let c=a.toString().trim();c&&sL(t,`Server stderr: ${c}`)}),n}function Hte(t){let e=ln();return e.approvedMcprcServers?.includes(t)?"approved":e.rejectedMcprcServers?.includes(t)?"rejected":"pending"}async function Can(t,e,r){let n=await PS();return(await Promise.allSettled(n.map(async i=>{if(i.type!=="connected")return null;try{return(await i.client.getServerCapabilities())?.[r]?{client:i,result:await i.client.request(t,e)}:null}catch(s){return i.type==="connected"&&sL(i.name,`Failed to request '${t.method}': ${s instanceof Error?s.message:String(s)}`),null}}))).filter(i=>i.status==="fulfilled").map(i=>i.value).filter(i=>i!==null)}function pan(t){return t.replaceAll(".","-")}async function yyi({client:{client:t,name:e},tool:r,args:n}){let o=await t.callTool({name:r,arguments:n},ZT);if("isError"in o&&o.isError){let i=`Error calling tool ${r}: ${o.error}`;throw sL(e,i),Error(JSON.stringify(o))}if("toolResult"in o)return String(o.toolResult);if("content"in o&&Array.isArray(o.content))return o.content.map(i=>i.type==="image"?{type:"image",source:{type:"base64",data:String(i.data),media_type:i.mimeType}}:i);throw Error(`Unexpected response format from tool ${r}`)}async function Tyi({name:t,client:e},r){try{return(await e.client.getPrompt({name:t,arguments:r})).messages.map(o=>({role:o.role,content:[o.content.type==="text"?{type:"text",text:o.content.text}:{type:"image",source:{data:String(o.content.data),media_type:o.content.mimeType,type:"base64"}}]}))}catch(n){throw sL(e.name,`Error running command '${t}': ${n instanceof Error?n.message:String(n)}`),n}}var FRe,Ayi,Syi,Tan,ban,PS,KG,van,uN=p(async()=>{yi();_o();wn();uL();Sut();Psn();ian();eb();yi();await dan();at();Ho();P9();Lt();FRe=null;Ayi=["project","global","mcprc"],Syi=["project","global"];Tan=async t=>{Hte(t)==="rejected"&&uGt(t);let r=$te(t);if(!r)return{name:t,type:"failed"};try{let n=await yan(t,r),o=await PS(),i={name:t,client:n,type:"connected"},s=o.findIndex(a=>a.name===t);return s!==-1?o[s]=i:o.push(i),i}catch{return{name:t,type:"failed"}}},ban=()=>(Eyi(),PS()),PS=Jt(async()=>{if(process.env.CI)return[];let t=tr().mcpServers??{},e=bR(),r=ln().mcpServers??{},n=rde(e,(u,l)=>Hte(l)==="approved"),o=rde(e,(u,l)=>Hte(l)==="rejected"),i=Object.keys(o).map(u=>({name:u,type:"disabled"})),s={...t,...n,...r},a=await Promise.all(Object.entries(s).map(async([u,l])=>{try{let d=await yan(u,l);return ke("tengu_mcp_server_connection_succeeded",{}),{name:u,client:d,type:"connected"}}catch(d){return ke("tengu_mcp_server_connection_failed",{}),sL(u,`Connection failed: ${d instanceof Error?d.message:String(d)}`),{name:u,type:"failed"}}})),c=FRe?[{name:FRe.name,client:FRe.client,type:"connected"}]:[];return[...a,...c,...i]});KG=Jt(async()=>{let e=(await Can({method:"tools/list"},zee,"tools")).flatMap(({client:r,result:{tools:n}})=>n.map(o=>({...lan,name:"mcp_"+pan(r.name)+"_"+pan(o.name),async description(){return o.description??""},async prompt(){return o.description??""},inputJSONSchema:o.inputSchema,async*call(i){let s=await yyi({client:r,tool:o.name,args:i});yield{type:"result",data:s,resultForAssistant:JSON.stringify(s)}},userFacingName(){return`(MCP) ${r.name.length>10?`...${r.name.slice(-10)}`:r.name}:${o.name}`}})));return e.forEach(r=>{wt(r.name+"|")}),e});van=Jt(async()=>(await Can({method:"prompts/list"},Vee,"prompts")).flatMap(({client:e,result:r})=>r.prompts?.map(n=>{let o=Object.values(n.arguments??{}).map(i=>i.name);return{type:"prompt",name:"mcp__"+e.name+"__"+n.name,description:n.description??"",isEnabled:!0,isHidden:!1,progressMessage:"running",userFacingName(){return`${e.name}:${n.name} (MCP)`},argNames:o,async getPromptForCommand(i){let s=i.split(" ");return await Tyi({name:n.name,client:e},w8e(o,s))}}})))});import qRe from"path";import byi from"os";import G9 from"fs";function ab(){return GRe.getInstance()}function xan(){return GRe.getInstance().selection_changed}var GRe,JG=p(async()=>{at();Sut();Qin();P9();await uN();wn();Lt();GRe=class t{static instance;currentConnection=null;autoConnectEnabled=!1;selection_changed=null;statusChangeCallback=null;onToolsReload=null;claudeDir;constructor(){this.claudeDir=qRe.join(byi.homedir(),".claude","ide")}findServers(){return G9.existsSync(this.claudeDir)?G9.readdirSync(this.claudeDir).filter(r=>r.endsWith(".lock")).map(r=>{let n=qRe.join(this.claudeDir,r);try{let o=JSON.parse(G9.readFileSync(n,"utf8"));return{port:parseInt(r.replace(".lock","")),authToken:o.authToken,workspaceFolders:o.workspaceFolders||[],ideName:o.ideName,pid:o.pid,lockFile:n}}catch(o){return Xt(`Failed to parse lock file ${r}:`,o),null}}).filter(r=>r!==null):[]}static getInstance(){return t.instance||(t.instance=new t),t.instance}getAvailableServers(){return this.findServers()}getCurrentConnection(){return this.currentConnection}setCurrentConnection(e){this.currentConnection=e}isAutoConnectEnabled(){return this.autoConnectEnabled}setAutoConnectEnabled(e){this.autoConnectEnabled=e}setStatusChangeCallback(e){this.statusChangeCallback=e}notifyReminderText(){if(this.statusChangeCallback){let e=this.selection_changed,r;e.selection.isEmpty?r=`current file : ${e.filePath.split("/").pop()||e.filePath.split("\\").pop()||e.file}`:r=`current file: ${e.filePath.split("/").pop()||e.filePath.split("\\").pop()||e.file} select ${e.selection.end.line-e.selection.start.line+1} rows`,this.statusChangeCallback(r)}}async connectToServer(e){try{wt(`\u{1F517} Connecting to ${e.ideName} at port ${e.port} key${e.authToken} ...`);let r=new UG({name:`${Vi}-ide-client`,version:"1.0.0"}),n=new CRe(`ws://127.0.0.1:${e.port}`,e.authToken,["getDiagnostics","executeCode"]);n.onnotification=(o,i)=>{o.method=="selection_changed"&&(this.selection_changed=o.params,this.notifyReminderText())},wt("\u{1F504} Establishing MCP connection..."),await r.connect(n),wt("\u2705 MCP connection established");try{return this.currentConnection=e,this.mcpClient=r,this.mcpTransport=n,_an("___ide_",r),Promise.all([Zd.getTools(!0)]).then(o=>{this.onToolsReload&&this.onToolsReload(o[0])}).catch(o=>{Cs(o)}),!0}catch(o){throw Xt("\u274C Failed to verify connection by listing tools:",o),await n.close(),o}}catch(r){return Xt(`Failed to connect to IDE server ${e.ideName}: ${r}`),!1}}async disconnect(){try{this.mcpTransport&&(await this.mcpTransport.close(),wt("\u{1F512} MCP transport connection closed")),this.currentConnection=null,this.mcpClient=null,this.mcpTransport=null,Cs("\u2705 IDE connection disconnected")}catch(e){Le(`Error during disconnect: ${e}`),this.currentConnection=null,this.mcpClient=null,this.mcpTransport=null}}hasAvailableConnections(){return this.getAvailableServers().length>0}getMCPClient(){return this.mcpClient||null}async autoConnectToMatchingServer(){try{let e=$l();if(!e)return wt("\u{1F4C1} No current project path available for auto-connect"),!1;wt(`\u{1F50D} Looking for IDE servers matching project path: ${e}`);let r=this.getAvailableServers();if(r.length===0)return wt("\u{1F50D} No IDE servers available for auto-connect"),!1;let n=r.filter(i=>i.workspaceFolders.some(s=>{let a=qRe.normalize(s),c=qRe.normalize(e);return wt(`\u{1F50D} Comparing workspace folder: ${a} with current path: ${c}`),G9.existsSync(a)?G9.statSync(a).ino===G9.statSync(c).ino:(wt(`Project folder "${a}" does not exist, it may have been deleted. IDE connection cannot be established.`),!1)}));if(n.length===0)return wt("\u{1F50D} No matching IDE server found for current project"),!1;let o=n[0];if(n.length>1){wt(`\u{1F50D} Found ${n.length} matching servers, selecting most recently modified one`);let i=0;for(let s of n)try{let a=G9.statSync(s.lockFile);a.mtime.getTime()>i&&(i=a.mtime.getTime(),o=s)}catch(a){wt(`Failed to get modification time for ${s.lockFile}: ${a}`)}}return o?(wt(`\u{1F3AF} Found matching IDE server: ${o.ideName} with workspace: ${o.workspaceFolders.join(", ")}`),!this.currentConnection||this.currentConnection.lockFile!==o.lockFile?(wt("\u{1F504} Attempting auto-connection to matching IDE server..."),await this.connectToServer(o)?(wt(`\u2705 Successfully auto-connected to ${o.ideName}`),!0):(wt(`\u274C Failed to auto-connect to ${o.ideName}`),!1)):(wt("\u2705 Already connected to matching IDE server"),!0)):(wt("\u{1F50D} No matching IDE server found for current project"),!1)}catch(e){return Le(`Error during auto-connect attempt: ${e}`),!1}}async callTool(e,r={}){let n=this.getMCPClient();if(!n)throw new Error("No active IDE connection");try{return await n.callTool({name:e,arguments:r})}catch(o){throw Le(`Failed to call tool ${e}: ${o}`),o}}async callToolOneWay(e,r={}){let n=this.getMCPClient();if(!n)throw new Error("No active IDE connection");try{await n.notification({method:"tools/call",params:{name:e,arguments:r}})}catch(o){throw Le(`Failed to call tool ${e}: ${o}`),o}}async getCurrentSelection(){return this.callTool("getCurrentSelection",{})}async getCurrentFile(){return this.callTool("getCurrentFile",{})}async isConnectionAlive(){try{let e=this.getMCPClient();if(!e||!this.currentConnection)return!1;try{return await e.ping(),Cs("\u{1F50D} Connection alive check: ping successful"),!0}catch(r){return Xt("Connection ping failed:",r),!1}}catch(e){return Xt("Connection check failed:",e),!1}}}});var rlt,Wm,XG=p(()=>{rlt=class{mode;setMode(e,r=!1){this.mode=e,r&&this.setModeUI(e)}isPlanMode(){return this.mode==="plan"}isEditMode(){return this.mode==="edit"}setModeUI(e){}},Wm=new rlt});import{spawn as Cyi}from"child_process";var HRe,Ian=p(async()=>{at();await Qte();HRe=class{constructor(e,r,n){this.config=e;this.projectPath=r;this.rootUri=n}process=null;messageId=0;pendingRequests=new Map;isInitialized=!1;buffer="";synchronizedDocuments=new Set;documentVersions=new Map;async start(){try{if(!await BS.checkCommandExists(this.config.command))throw new Error(`Failed to start LSP server: ${this.config.command} \u4E0D\u5B58\u5728`);if(this.process=Cyi(this.config.command,this.config.args,{cwd:this.projectPath,stdio:["pipe","pipe","pipe"],shell:!0}),!this.process.stdin||!this.process.stdout)throw new Error("Failed to create LSP process pipes");this.process.stdout.on("data",e=>{this.handleData(e.toString())}),this.process.stderr.on("data",e=>{console.error(`LSP stderr: ${e.toString()}`)}),this.process.on("error",e=>{console.error(`LSP process error: ${e.message}`),this.cleanupPendingRequests(e)}),this.process.on("exit",e=>{this.cleanupPendingRequests(new Error("LSP process exited"))}),await this.initialize()}catch(e){throw new Error(`Failed to start LSP server: ${e}`)}}async stop(){if(this.process)try{await this.shutdown(),this.process.kill(),this.process=null,this.isInitialized=!1}catch(e){console.error(`Error stopping LSP server: ${e}`)}}isReady(){return this.isInitialized&&this.process!==null}async goToDefinition(e,r){let n=await this.sendRequest("textDocument/definition",{textDocument:{uri:e},position:r});return this.parseLocations(n)}async findReferences(e,r){let n=await this.sendRequest("textDocument/references",{textDocument:{uri:e},position:r,context:{includeDeclaration:!0}});return this.parseLocations(n)}async hover(e,r){let n=await this.sendRequest("textDocument/hover",{textDocument:{uri:e},position:r});return n?{contents:this.extractHoverContents(n),range:n.range}:null}async documentSymbol(e){let r=await this.sendRequest("textDocument/documentSymbol",{textDocument:{uri:e}});return this.parseSymbols(r)}async workspaceSymbol(e){let r=await this.sendRequest("workspace/symbol",{query:e});return this.parseSymbols(r)}async goToImplementation(e,r){let n=await this.sendRequest("textDocument/implementation",{textDocument:{uri:e},position:r});return this.parseLocations(n)}async prepareCallHierarchy(e,r){let n=await this.sendRequest("textDocument/prepareCallHierarchy",{textDocument:{uri:e},position:r});return this.parseCallHierarchyItems(n)}async incomingCalls(e){let r=await this.sendRequest("callHierarchy/incomingCalls",{item:e});return this.parseIncomingCalls(r)}async outgoingCalls(e){let r=await this.sendRequest("callHierarchy/outgoingCalls",{item:e});return this.parseOutgoingCalls(r)}async openDocument(e,r,n,o){await this.sendNotification("textDocument/didOpen",{textDocument:{uri:e,languageId:r,version:n,text:o}}),this.synchronizedDocuments.add(e),this.documentVersions.set(e,n)}async changeDocument(e,r,n){if(!this.synchronizedDocuments.has(e))throw new Error(`Document ${e} is not synchronized. Call openDocument first.`);await this.sendNotification("textDocument/didChange",{textDocument:{uri:e,version:r},contentChanges:n}),this.documentVersions.set(e,r)}async saveDocument(e,r){if(!this.synchronizedDocuments.has(e))throw new Error(`Document ${e} is not synchronized. Call openDocument first.`);let n={textDocument:{uri:e}};r!==void 0&&(n.text=r),await this.sendNotification("textDocument/didSave",n)}async closeDocument(e){this.synchronizedDocuments.has(e)&&(await this.sendNotification("textDocument/didClose",{textDocument:{uri:e}}),this.synchronizedDocuments.delete(e),this.documentVersions.delete(e))}isDocumentSynchronized(e){return this.synchronizedDocuments.has(e)}getDocumentVersion(e){return this.documentVersions.get(e)}getLanguageIdFromUri(e){switch(e.split(".").pop()?.toLowerCase()){case"ts":return"typescript";case"tsx":return"typescriptreact";case"js":return"javascript";case"jsx":return"javascriptreact";case"py":return"python";case"go":return"go";case"java":return"java";case"c":return"c";case"cpp":case"cc":case"cxx":return"cpp";case"cs":return"csharp";case"php":return"php";case"kt":return"kotlin";case"rs":return"rust";case"rb":return"ruby";case"html":return"html";case"css":return"css";case"scss":case"sass":return"scss";default:return"plaintext"}}async initialize(){let e={processId:process.pid,rootUri:`file://${this.rootUri}`,capabilities:{textDocument:{definition:{dynamicRegistration:!0},references:{dynamicRegistration:!0},hover:{dynamicRegistration:!0},documentSymbol:{dynamicRegistration:!0},implementation:{dynamicRegistration:!0},callHierarchy:{dynamicRegistration:!0}},workspace:{symbol:{dynamicRegistration:!0}}},initializationOptions:this.config.initializationOptions};await this.sendRequest("initialize",e),await this.sendNotification("initialized",{}),this.isInitialized=!0}async shutdown(){this.isInitialized&&(await this.sendRequest("shutdown",{}),await this.sendNotification("exit",{}))}handleData(e){for(this.buffer+=e;;){let r=this.buffer.indexOf(`\r
|
|
556
556
|
\r
|
|
557
557
|
`);if(r===-1)break;let n=this.buffer.substring(0,r),o=this.parseContentLength(n);if(o===null)break;let i=r+4,s=i+o;if(this.buffer.length<s)break;let a=this.buffer.substring(i,s);this.buffer=this.buffer.substring(s);try{let c=JSON.parse(a);this.handleMessage(c)}catch(c){Xt(`Failed to parse LSP message: ${c}`,e)}}}parseContentLength(e){let r=e.match(/Content-Length: (\d+)/);return r?parseInt(r[1],10):null}handleMessage(e){if(e.id!==void 0&&(e.result!==void 0||e.error!==void 0)){let r=this.pendingRequests.get(e.id);r&&(this.pendingRequests.delete(e.id),e.error?r.reject(new Error(`LSP error: ${JSON.stringify(e.error)}`)):r.resolve(e.result))}}async sendRequest(e,r){return new Promise((n,o)=>{let i=++this.messageId,s={jsonrpc:"2.0",id:i,method:e,params:r};this.pendingRequests.set(i,{resolve:n,reject:o}),this.sendMessage(s)})}async sendNotification(e,r){let n={jsonrpc:"2.0",method:e,params:r};this.sendMessage(n)}sendMessage(e){if(!this.process||!this.process.stdin)throw new Error("LSP process not running");let r=JSON.stringify(e),n=`Content-Length: ${Buffer.byteLength(r)}\r
|
|
558
558
|
\r
|