@leveragent/e2e-testing 0.1.8 → 0.1.12
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/agentRunner-5M7IDURG.js +43 -0
- package/dist/cli.js +1 -1
- package/package.json +1 -1
- package/prompts/test.md +1 -1
- package/dist/agentRunner-6GC3KHLQ.js +0 -44
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import{CopilotClient as Wt}from"@github/copilot-sdk";import*as At from"readline";import{approveAll as Nt}from"@github/copilot-sdk";import{readFile as It}from"fs/promises";var tt=(t,e)=>t===void 0?e:/^(1|true|yes|on)$/i.test(t.trim()),et=t=>{let e=(t??"high").trim().toLowerCase();switch(e){case"low":case"medium":case"high":case"xhigh":return e;default:return"high"}},ot=async(t,e={})=>{let{trim:o=!0,onError:s}=e;try{let n=await It(t,"utf8");return(o?n.trim():n)||""}catch(n){return s?.(n),""}},L=t=>typeof t=="number"&&Number.isFinite(t)?t:0,st=t=>new Intl.NumberFormat("en-US",{style:"currency",currency:"USD",minimumFractionDigits:4,maximumFractionDigits:6}).format(t),U=t=>`${at}${t}${O}`,j=t=>`${it}${t}${O}`,nt=t=>`${F}${t}`,rt=t=>`${O}${t}`;var Lt="",B="basic",ct=`You are a senior frontend engineer, specialized on E2E testing in a local CLI. Be concise but highly useful.
|
|
3
|
+
Prefer concrete steps, ask clarifying questions only when necessary, and use tools when needed.
|
|
4
|
+
If a tool returns uncertain data, state assumptions clearly. ${Lt}`,lt=1e9,v=Number(process.env.COPILOT_USD_PER_AIU??"0");var Ut="gpt-5.3-codex";var G=1e6,it="\x1B[38;5;245m",O="\x1B[0m",F="\x1B[38;5;110m",at="\x1B[38;5;120m",H=1e4,ut=1e6,pt=new Set([".ts",".tsx",".js",".jsx",".mjs",".cjs",".json",".go",".py",".java",".rb",".rs",".php",".swift",".kt",".kts",".cs",".c",".h",".hpp",".cpp",".cc"]);var Pt="0.1.12",dt=Pt||process.env.npm_package_version||"unknown";var D=process.env.COPILOT_MODEL??Ut,q=et(process.env.COPILOT_REASONING_EFFORT),mt=tt(process.env.COPILOT_RESUME_LAST,!0),W=process.env.COPILOT_SDK_AUTH_TOKEN??process.env.GITHUB_TOKEN??process.env.GH_TOKEN;var kt=(t,e,o)=>{if(o){if(t&&typeof t=="object"){let s=t;if(typeof s.detailedContent=="string")return s.detailedContent;if(typeof s.content=="string")return s.content}return"[no response content]"}if(e&&typeof e=="object"){let s=e;if(typeof s.message=="string")return s.message}return"[tool failed without error details]"},ft=(t,e)=>{let{spinner:o}=e,s=[];return s.push(t.on("assistant.message_delta",n=>{o.stop(),process.stdout.write(U(n.data.deltaContent))})),s.push(t.on("assistant.turn_end",n=>{})),s.push(t.on("session.error",n=>{o.stop(),console.error(`
|
|
5
|
+
[session error]`,n.data)})),s.push(t.on("tool.execution_start",n=>{let r=n.data.toolName??"unknown_tool";o.stop();let f=20,_=JSON.stringify(n.data.arguments,void 0);_.length>f&&(_=_.slice(0,f)+"..."),process.stdout.write(j(`
|
|
6
|
+
[tool call] ${r}(${_})`)),o.start()})),s.push(t.on("tool.execution_complete",n=>{let r=kt(n.data.result,n.data.error,n.data.success),f=n.data.success?"[tool response]":"[tool error]";o.stop(),process.stdout.write(j(`
|
|
7
|
+
${f} ${r}`)),o.start()})),s.push(t.on("assistant.usage",n=>{e.usageTotals.events+=1,e.usageTotals.inputTokens+=L(n.data.inputTokens),e.usageTotals.outputTokens+=L(n.data.outputTokens),e.usageTotals.cacheReadTokens+=L(n.data.cacheReadTokens),e.usageTotals.cacheWriteTokens+=L(n.data.cacheWriteTokens),e.usageTotals.totalNanoAiu+=L(n.data.copilotUsage?.totalNanoAiu)})),s};var Mt=async(t,e)=>new Promise(o=>{t.rl.question(`${F}${e}`,s=>{process.stdout.write(O),o(s)})}),K=(t,e)=>{t.detachSessionHandlers.forEach(o=>o()),t.detachSessionHandlers=ft(e,t)},ht=t=>({model:D,reasoningEffort:q,streaming:!0,tools:t.tools,onPermissionRequest:Nt,onUserInputRequest:async e=>{process.stdout.write(`
|
|
8
|
+
Agent asks: ${e.question}
|
|
9
|
+
`),e.choices&&e.choices.length>0&&process.stdout.write(`Choices: ${e.choices.join(" | ")}
|
|
10
|
+
`);let o=await Mt(t,"You (for agent): ");return{answer:o,wasFreeform:!e.choices?.includes(o)}},workingDirectory:process.cwd(),infiniteSessions:{enabled:!0,backgroundCompactionThreshold:.8,bufferExhaustionThreshold:.95},systemMessage:{content:ct}}),Tt=async t=>{let e=ht(t);if(mt){let s=await t.client.getLastSessionId();if(s)try{let n=await t.client.resumeSession(s,e);return K(t,n),process.stdout.write(`[session] Resumed ${n.sessionId}
|
|
11
|
+
`),n}catch(n){console.error("[session] Resume failed, creating a new session:",n)}}let o=await t.client.createSession(e);return K(t,o),process.stdout.write(`[session] Started ${o.sessionId}
|
|
12
|
+
`),o},gt=async t=>{let e=ht(t),o=t.requireSession(),s=o.sessionId;await o.disconnect();let n=await t.client.createSession(e);return K(t,n),process.stdout.write(`[session] Switched from ${s} to ${n.sessionId}
|
|
13
|
+
`),n};var Et=["|","/","-","\\"],wt=t=>{let e=process.stdout,o=!!(e.isTTY&&process.env.TERM!=="dumb"),s=null,n=0,r=()=>{let $=Et[n%Et.length];if(n+=1,o){e.write(`\r${$}`);return}e.write(`${$}
|
|
14
|
+
`)};return{start:()=>{t()&&(s||(o&&e.write(`
|
|
15
|
+
`),r(),s=setInterval(()=>{r()},90)))},stop:()=>{s&&(clearInterval(s),s=null,n=0,o&&e.write("\r\x1B[K"))}}};import{spawn as Dt}from"child_process";import{promises as w}from"fs";import*as u from"path";var $t=t=>t.split(u.sep).join("/"),_t=async t=>{let e=await w.realpath(t),o=a=>{let i=u.relative(e,a);return i.length===0?".":i},s=a=>{let i=u.relative(e,a);if(i.startsWith("..")||u.isAbsolute(i))throw new Error(`Path is outside the working directory: ${a}`)},n=a=>{let i=a.trim();if(!i)throw new Error("Path must be a non-empty string.");let l=u.resolve(e,i);return s(l),l},r=async a=>{let i=n(a),l=await w.realpath(i);return s(l),l},f=async a=>{let i=await r(a),l=await w.stat(i);if(!l.isFile())throw new Error(`Path is not a regular file: ${o(i)}`);return{absolutePath:i,stats:l}},_=async a=>{let i=n(a),l=u.dirname(i);await w.mkdir(l,{recursive:!0});let T=await w.realpath(l);return s(T),i},$=async({command:a,args:i,commandCwd:l,timeoutMs:T,maxOutputBytes:S})=>new Promise((R,g)=>{let d=Dt(a,i,{cwd:l,shell:!1,env:{...process.env,GIT_PAGER:"cat"}}),b="",E="",x=!1,y=!1,m=!1,p,C=(c,h,P)=>{if(c.length>=P)return{text:c,truncated:!0};let k=P-c.length;return h.length<=k?{text:c+h,truncated:!1}:{text:c+h.slice(0,k),truncated:!0}};d.stdout&&d.stdout.on("data",c=>{let h=C(b,c.toString(),S);b=h.text,h.truncated&&(x=!0)}),d.stderr&&d.stderr.on("data",c=>{let h=C(E,c.toString(),S);E=h.text,h.truncated&&(y=!0)}),d.on("error",c=>{p&&clearTimeout(p),g(c)}),p=setTimeout(()=>{m=!0,d.pid&&(process.kill(d.pid,"SIGTERM"),setTimeout(()=>{d.pid&&process.kill(d.pid,"SIGKILL")},1e3))},T),d.on("close",(c,h)=>{p&&clearTimeout(p),R({exitCode:c,signal:h,stdout:b,stderr:E,stdoutTruncated:x,stderrTruncated:y,timedOut:m})})}),V=async({rootDirectory:a,maxEntries:i,includeHidden:l,maxDepth:T,includeDirectories:S,includeFiles:R})=>{let g=[],d=!1,b=async(E,x)=>{if(g.length>=i||g.length>=H){d=!0;return}let y=await w.readdir(E,{withFileTypes:!0});y.sort((m,p)=>m.name.localeCompare(p.name));for(let m of y){if(g.length>=i||g.length>=H){d=!0;return}if(!l&&m.name.startsWith("."))continue;let p=u.join(E,m.name),C=o(p);if(m.isDirectory()){S&&g.push({path:C,type:"directory"}),x<T&&await b(p,x+1);continue}if(m.isFile()){if(R){let c=await w.stat(p);g.push({path:C,type:"file",sizeBytes:c.size})}continue}if(m.isSymbolicLink()){let c=await w.realpath(p);s(c),(R||S)&&g.push({path:C,type:"symlink",target:o(c)})}}};return await b(a,0),{entries:g,truncated:d}},Rt=async a=>{let i=await r(a),l=await w.stat(i);if(l.isFile())return{files:[i],truncated:!1};if(l.isDirectory()){let T=await V({rootDirectory:i,maxEntries:H,includeHidden:!1,maxDepth:100,includeDirectories:!1,includeFiles:!0});return{files:T.entries.map(S=>u.resolve(e,S.path)),truncated:T.truncated}}throw new Error(`Path is neither a file nor a directory: ${a}`)};return{workspaceRoot:e,toWorkspacePath:o,resolveExistingPath:r,resolveReadableFile:f,resolveWritableFile:_,executeCommand:$,walkDirectory:V,collectTextMatches:async({query:a,searchPath:i,maxResults:l,caseSensitive:T,useRegex:S,globPattern:R,requireCodeFiles:g})=>{let{files:d,truncated:b}=await Rt(i),E=[],x=S?new RegExp(a,T?"g":"gi"):void 0,y=T?a:a.toLowerCase(),m=!1;for(let p of d){if(m)break;let C=u.extname(p).toLowerCase();if(g&&!pt.has(C))continue;let c=o(p);if(!u.matchesGlob($t(c),R))continue;let h=await w.stat(p);if(!h.isFile()||h.size>ut)continue;let P=await w.readFile(p,"utf8");if(P.includes("\0"))continue;let k=P.split(/\r?\n/u);for(let N=0;N<k.length&&!m;N+=1){let I=k[N];if(I.length===0)continue;if(x){x.lastIndex=0;for(let M of I.matchAll(x)){let Z=M.index;if(Z!==void 0&&(E.push({path:c,line:N+1,column:Z+1,preview:I}),E.length>=l)){m=!0;break}}continue}let Q=T?I:I.toLowerCase(),z=0;for(;z<Q.length;){let M=Q.indexOf(y,z);if(M===-1)break;if(E.push({path:c,line:N+1,column:M+1,preview:I}),E.length>=l){m=!0;break}z=M+Math.max(y.length,1)}}}return{matches:E,truncated:b||m}}}};var St=async t=>{let e=await _t(t);return{tools:[]}};var xt=t=>{let e=t.usageTotals.inputTokens+t.usageTotals.outputTokens+t.usageTotals.cacheReadTokens+t.usageTotals.cacheWriteTokens,o=t.usageTotals.totalNanoAiu/lt,s=o*v;B!=="none"&&(process.stdout.write(rt(`
|
|
16
|
+
Session usage summary:
|
|
17
|
+
`)),process.stdout.write(` Input tokens: ${t.usageTotals.inputTokens}
|
|
18
|
+
`),process.stdout.write(` Output tokens: ${t.usageTotals.outputTokens}
|
|
19
|
+
`),B==="detailed"&&(process.stdout.write(` Usage events: ${t.usageTotals.events}
|
|
20
|
+
`),process.stdout.write(` Total tokens: ${e}
|
|
21
|
+
`),process.stdout.write(` Cache read tokens: ${t.usageTotals.cacheReadTokens}
|
|
22
|
+
`),process.stdout.write(` Cache write tokens: ${t.usageTotals.cacheWriteTokens}
|
|
23
|
+
`),process.stdout.write(` Estimated cost: ${o.toFixed(6)} AIU
|
|
24
|
+
`),v>0?process.stdout.write(` Estimated cost (USD): ${st(s)} (COPILOT_USD_PER_AIU=${v})
|
|
25
|
+
`):process.stdout.write(` Estimated cost (USD): set COPILOT_USD_PER_AIU to enable USD estimate
|
|
26
|
+
`)))};var{tools:Ft}=await St(process.cwd()),vt=At.createInterface({input:process.stdin,output:process.stdout}),X=class{constructor(e){this.client=e;this.spinner=wt(()=>!this.isShuttingDown)}session=null;rl=vt;tools=Ft;detachSessionHandlers=[];isShuttingDown=!1;usageTotals={events:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheWriteTokens:0,totalNanoAiu:0};spinner;async init(){let e=await Tt(this);this.setSession(e)}setSession(e){this.session=e}requireSession(){if(!this.session)throw new Error("No active session available.");return this.session}async cleanupAndExit(){if(this.isShuttingDown)return;if(this.isShuttingDown=!0,this.spinner.stop(),this.detachSessionHandlers.forEach(o=>o()),this.detachSessionHandlers=[],this.session)try{await this.session.disconnect()}catch(o){console.error("[shutdown] session disconnect warning",o)}xt(this);try{await this.client.stop()}catch(o){console.error("[shutdown] client stop warning",o)}this.rl.close(),process.exitCode=0,setTimeout(()=>{process.exit(0)},25).unref()}listenToShutdownSignals(){this.rl.on("close",()=>{this.isShuttingDown||this.cleanupAndExit()}),process.on("unhandledRejection",e=>{this.isShuttingDown||console.error("[unhandled rejection]",e)}),process.on("SIGINT",async()=>{this.isShuttingDown||(process.stdout.write(`
|
|
27
|
+
Shutting down...
|
|
28
|
+
`),await this.cleanupAndExit())})}};import{fileURLToPath as Gt}from"url";var bt={init:{url:"https://gist.githubusercontent.com/marcellkiss/8409a64df9fb055544d72bc0629d2338/raw/399b3a5f5f9d2e0128446d776ffb9f2b32021743/init.md"},"custom-test":{url:"https://gist.githubusercontent.com/marcellkiss/029ff544f3ed720971e47b8306e8853c/raw/ff7d36b86193a9e314cb2935968c9ccc0e3bd9ef/custom-test.md"},"diff-test":{url:"https://gist.githubusercontent.com/marcellkiss/fbe9294d4de00c2ef11f1437919353b7/raw/16a8ae53a9019386ef5fea5d321934d5bec70188/diff-test.md"}},yt=async t=>{let e=await t.getAuthStatus();if(!e.isAuthenticated){let o=W?"A token was provided but authentication still failed. Verify your token has Copilot access.":"No token detected. Authenticate with `copilot auth login` (or set GITHUB_TOKEN / GH_TOKEN).";console.error("Copilot authentication is not configured."),e.statusMessage&&console.error(`Status: ${e.statusMessage}`),console.error(o),await t.stop(),process.exit(1)}},Ct=()=>{console.log(`
|
|
29
|
+
System Commands:`),console.log(" /help Show commands"),console.log(" /status Show auth + session info"),console.log(" /models List available models"),console.log(" /new Start a fresh session"),console.log(` exit|quit|q Leave the agent
|
|
30
|
+
`),console.log("----------------------------------------------------------------"),console.log(`
|
|
31
|
+
Test Generation Commands:`),console.log(" /init Init E2E tests from zero"),console.log(" /custom-test Add custom E2E test based on your input"),console.log(" /diff-test Add E2E test based on code diff")},Ot=()=>{console.log(U(`
|
|
32
|
+
\u2705 Welcome to LeverAgent, your E2E testing assistant!`)),console.log(`You're currently using version: ${dt} with the following model: ${D}`),console.log("Type your message and press Enter. Use 'exit', 'quit', or 'q' to quit."),Ct()},Ht=async t=>{let e=Gt(new URL(`../prompts/${t}`,import.meta.url));return await ot(e,{onError:s=>{console.error(`[prompt] could not read ${e}; continuing without startup prompt`,s)}})},qt=async t=>{try{let e=await fetch(t);return e.ok?await e.text():(console.error(`[prompt] could not fetch ${t}; HTTP ${e.status} ${e.statusText}`),"")}catch(e){return console.error(`[prompt] could not fetch ${t}`,e),""}},A=t=>{if(!t.isShuttingDown){if(process.stdin.readableEnded||process.stdin.destroyed){t.cleanupAndExit();return}try{t.rl.question(nt(`
|
|
33
|
+
You: `),async e=>{if(process.stdout.write(O),t.isShuttingDown)return;let o=e.trim().toLowerCase();if(o==="exit"||o==="quit"||o==="q"){await t.cleanupAndExit();return}if(o==="/help"){Ct(),A(t);return}if(o==="/new"){try{t.setSession(await gt(t))}catch(r){console.error("[request failed] could not start a new session",r)}A(t);return}if(o==="/status"){try{let r=await t.client.getAuthStatus(),f=t.requireSession();console.log(`
|
|
34
|
+
Status`),console.log(` Session: ${f.sessionId}`),console.log(` Model: ${D}`),console.log(` Reasoning: ${q}`),console.log(` Request timeout: ${G}ms`),console.log(` Authenticated: ${r.isAuthenticated}`),r.login&&console.log(` Login: ${r.login}`)}catch(r){console.error("[request failed] could not read status",r)}process.stdout.write(`
|
|
35
|
+
`),A(t);return}if(o==="/models"){try{let f=(await t.client.listModels()).slice(0,20);console.log(`
|
|
36
|
+
Available models (first 20):`),f.forEach(_=>{console.log(` - ${_.id}`)})}catch(r){console.error("[request failed] could not list models",r)}process.stdout.write(`
|
|
37
|
+
`),A(t);return}if(!e.trim()){A(t);return}let s=e;o==="/t"&&(s=await Ht("test.md"));let n=Object.keys(bt).find(r=>o.toLowerCase().startsWith(`/${r.toLocaleLowerCase()}`));if(n){let{url:r}=bt[n],f=e.slice(n.length+1).trim();console.log(U(`Matched command "/${n}" with userInput: "${f}"`)),s=await qt(r),s+=`
|
|
38
|
+
|
|
39
|
+
<<USER_INPUT>>
|
|
40
|
+
${f}`}if(!s.trim()){console.error("[prompt] loaded prompt is empty; request was not sent"),A(t);return}process.stdout.write(U("Assistant:")),t.spinner.start();try{await t.requireSession().sendAndWait({prompt:s},G)}catch(r){t.spinner.stop(),r instanceof Error&&r.message.includes("Timeout after")?console.error(`
|
|
41
|
+
[request failed] ${r.message}. Increase COPILOT_REQUEST_TIMEOUT_MS (current: ${G}ms).`):console.error(`
|
|
42
|
+
[request failed]`,r)}t.spinner.stop(),process.stdout.write(`
|
|
43
|
+
`),t.isShuttingDown||A(t)})}catch(e){if(e&&typeof e=="object"&&"code"in e&&e.code==="ERR_USE_AFTER_CLOSE"){t.cleanupAndExit();return}throw e}}};var Y=new Wt({githubToken:W,useLoggedInUser:!0,autoRestart:!1});await Y.start();await yt(Y);var J=new X(Y);await J.init();Ot();A(J);J.listenToShutdownSignals();
|
package/dist/cli.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{existsSync as n}from"fs";import{copyFile as c}from"fs/promises";import{createRequire as a}from"module";import{dirname as p,join as s}from"path";var m=async()=>{let i=a(import.meta.url).resolve("vscode-jsonrpc/package.json"),r=p(i),e=s(r,"node"),t=s(r,"node.js");n(e)||!n(t)||await c(t,e)};try{await m()}catch(o){console.error("[bootstrap warning] could not prepare vscode-jsonrpc compatibility shim",o)}await import("./agentRunner-
|
|
2
|
+
import{existsSync as n}from"fs";import{copyFile as c}from"fs/promises";import{createRequire as a}from"module";import{dirname as p,join as s}from"path";var m=async()=>{let i=a(import.meta.url).resolve("vscode-jsonrpc/package.json"),r=p(i),e=s(r,"node"),t=s(r,"node.js");n(e)||!n(t)||await c(t,e)};try{await m()}catch(o){console.error("[bootstrap warning] could not prepare vscode-jsonrpc compatibility shim",o)}await import("./agentRunner-5M7IDURG.js");
|
package/package.json
CHANGED
package/prompts/test.md
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
Use any kind of tool, which takes a few seconds, but does not have any effect
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import{CopilotClient as qt}from"@github/copilot-sdk";import*as wt from"readline";import{approveAll as kt}from"@github/copilot-sdk";import{readFile as Ot}from"fs/promises";var Q=(t,e)=>t===void 0?e:/^(1|true|yes|on)$/i.test(t.trim()),Z=t=>{let e=(t??"high").trim().toLowerCase();switch(e){case"low":case"medium":case"high":case"xhigh":return e;default:return"high"}},tt=async(t,e={})=>{let{trim:s=!0,onError:n}=e;try{let o=await Ot(t,"utf8");return(s?o.trim():o)||""}catch(o){return n?.(o),""}},I=t=>typeof t=="number"&&Number.isFinite(t)?t:0,et=t=>new Intl.NumberFormat("en-US",{style:"currency",currency:"USD",minimumFractionDigits:4,maximumFractionDigits:6}).format(t);var Rt="",j="basic",st=`You are a senior frontend engineer, specialized on E2E testing in a local CLI. Be concise but highly useful.
|
|
3
|
-
Prefer concrete steps, ask clarifying questions only when necessary, and use tools when needed.
|
|
4
|
-
If a tool returns uncertain data, state assumptions clearly. ${Rt}`,ot=1e9,N=Number(process.env.COPILOT_USD_PER_AIU??"0");var Ct="gpt-5.3-codex";var M=1e6,D="\x1B[0m",$="\x1B[38;5;110m",v=1e4,nt=1e6,rt=new Set([".ts",".tsx",".js",".jsx",".mjs",".cjs",".json",".go",".py",".java",".rb",".rs",".php",".swift",".kt",".kts",".cs",".c",".h",".hpp",".cpp",".cc"]);var It="0.1.8",it=It||process.env.npm_package_version||"unknown";var k=process.env.COPILOT_MODEL??Ct,G=Z(process.env.COPILOT_REASONING_EFFORT),at=Q(process.env.COPILOT_RESUME_LAST,!0),q=process.env.COPILOT_SDK_AUTH_TOKEN??process.env.GITHUB_TOKEN??process.env.GH_TOKEN;var Lt="\x1B[0m",Pt="\x1B[38;5;245m",B=t=>`${Pt}${t}${Lt}`,Ut=t=>{if(t===void 0)return"{}";try{return JSON.stringify(t,null,2)}catch{return"[unserializable arguments]"}},Ft=(t,e,s)=>{if(s){if(t&&typeof t=="object"){let n=t;if(typeof n.detailedContent=="string")return n.detailedContent;if(typeof n.content=="string")return n.content}return"[no response content]"}if(e&&typeof e=="object"){let n=e;if(typeof n.message=="string")return n.message}return"[tool failed without error details]"},ct=(t,e)=>{if(t.waitingForAssistantOutput){if(t.waitingForAssistantOutput=!1,t.waitingForToolResult){e.start("Tool");return}e.stop(!0)}},lt=(t,e)=>{let{uiState:s,spinner:n}=e,o=[];return o.push(t.on("assistant.message_delta",r=>{ct(s,n),process.stdout.write(r.data.deltaContent)})),o.push(t.on("assistant.message",()=>{ct(s,n)})),o.push(t.on("session.idle",()=>{s.waitingForToolResult=!1,s.waitingForAssistantOutput&&(s.waitingForAssistantOutput=!1,n.stop(!1)),process.stdout.write(`
|
|
5
|
-
`)})),o.push(t.on("session.error",r=>{s.waitingForToolResult=!1,s.waitingForAssistantOutput&&(s.waitingForAssistantOutput=!1,n.stop(!1)),console.error(`
|
|
6
|
-
[session error]`,r.data)})),o.push(t.on("tool.execution_start",r=>{let _=r.data.toolName??"unknown_tool",w=Ut(r.data.arguments);s.waitingForToolResult=!0,n.stop(!1),process.stdout.write(B(`
|
|
7
|
-
[tool] ${_}
|
|
8
|
-
`)),process.stdout.write(B(`[tool args] ${w}
|
|
9
|
-
`)),n.start(`Tool: ${_}`)})),o.push(t.on("tool.execution_complete",r=>{s.waitingForToolResult=!1;let _=Ft(r.data.result,r.data.error,r.data.success),w=r.data.success?"[tool response]":"[tool error]";if(process.stdout.write(B(`${w} ${_}
|
|
10
|
-
`)),s.waitingForAssistantOutput){n.start("Assistant");return}n.stop(!1)})),o.push(t.on("assistant.usage",r=>{e.usageTotals.events+=1,e.usageTotals.inputTokens+=I(r.data.inputTokens),e.usageTotals.outputTokens+=I(r.data.outputTokens),e.usageTotals.cacheReadTokens+=I(r.data.cacheReadTokens),e.usageTotals.cacheWriteTokens+=I(r.data.cacheWriteTokens),e.usageTotals.totalNanoAiu+=I(r.data.copilotUsage?.totalNanoAiu)})),o};var Nt=async(t,e)=>new Promise(s=>{t.rl.question(`${$}${e}`,n=>{process.stdout.write(D),s(n)})}),X=(t,e)=>{t.detachSessionHandlers.forEach(s=>s()),t.detachSessionHandlers=lt(e,t)},ut=t=>({model:k,reasoningEffort:G,streaming:!0,tools:t.tools,onPermissionRequest:kt,onUserInputRequest:async e=>{process.stdout.write(`
|
|
11
|
-
Agent asks: ${e.question}
|
|
12
|
-
`),e.choices&&e.choices.length>0&&process.stdout.write(`Choices: ${e.choices.join(" | ")}
|
|
13
|
-
`);let s=await Nt(t,"You (for agent): ");return{answer:s,wasFreeform:!e.choices?.includes(s)}},workingDirectory:process.cwd(),infiniteSessions:{enabled:!0,backgroundCompactionThreshold:.8,bufferExhaustionThreshold:.95},systemMessage:{content:st}}),pt=async t=>{let e=ut(t);if(at){let n=await t.client.getLastSessionId();if(n)try{let o=await t.client.resumeSession(n,e);return X(t,o),process.stdout.write(`[session] Resumed ${o.sessionId}
|
|
14
|
-
`),o}catch(o){console.error("[session] Resume failed, creating a new session:",o)}}let s=await t.client.createSession(e);return X(t,s),process.stdout.write(`[session] Started ${s.sessionId}
|
|
15
|
-
`),s},dt=async t=>{let e=ut(t),s=t.requireSession(),n=s.sessionId;await s.disconnect();let o=await t.client.createSession(e);return X(t,o),process.stdout.write(`[session] Switched from ${n} to ${o.sessionId}
|
|
16
|
-
`),o};var mt=["|","/","-","\\"],ft=t=>{let e=null,s=0,n="Assistant",o=()=>{let w=mt[s%mt.length];s+=1,process.stdout.write(`\r${n}: ${w}`)};return{start:w=>{n=w,t()&&(e||(o(),e=setInterval(()=>{o()},90)))},stop:(w=!1)=>{e&&(clearInterval(e),e=null),s=0,process.stdout.write("\r\x1B[K"),w&&process.stdout.write(`${n}: `)}}};import{spawn as Mt}from"child_process";import{promises as E}from"fs";import*as u from"path";var Dt=t=>t.split(u.sep).join("/"),ht=async t=>{let e=await E.realpath(t),s=a=>{let i=u.relative(e,a);return i.length===0?".":i},n=a=>{let i=u.relative(e,a);if(i.startsWith("..")||u.isAbsolute(i))throw new Error(`Path is outside the working directory: ${a}`)},o=a=>{let i=a.trim();if(!i)throw new Error("Path must be a non-empty string.");let l=u.resolve(e,i);return n(l),l},r=async a=>{let i=o(a),l=await E.realpath(i);return n(l),l},_=async a=>{let i=await r(a),l=await E.stat(i);if(!l.isFile())throw new Error(`Path is not a regular file: ${s(i)}`);return{absolutePath:i,stats:l}},w=async a=>{let i=o(a),l=u.dirname(i);await E.mkdir(l,{recursive:!0});let h=await E.realpath(l);return n(h),i},bt=async({command:a,args:i,commandCwd:l,timeoutMs:h,maxOutputBytes:S})=>new Promise((R,g)=>{let d=Mt(a,i,{cwd:l,shell:!1,env:{...process.env,GIT_PAGER:"cat"}}),b="",T="",A=!1,y=!1,m=!1,p,O=(c,f,L)=>{if(c.length>=L)return{text:c,truncated:!0};let P=L-c.length;return f.length<=P?{text:c+f,truncated:!1}:{text:c+f.slice(0,P),truncated:!0}};d.stdout&&d.stdout.on("data",c=>{let f=O(b,c.toString(),S);b=f.text,f.truncated&&(A=!0)}),d.stderr&&d.stderr.on("data",c=>{let f=O(T,c.toString(),S);T=f.text,f.truncated&&(y=!0)}),d.on("error",c=>{p&&clearTimeout(p),g(c)}),p=setTimeout(()=>{m=!0,d.pid&&(process.kill(d.pid,"SIGTERM"),setTimeout(()=>{d.pid&&process.kill(d.pid,"SIGKILL")},1e3))},h),d.on("close",(c,f)=>{p&&clearTimeout(p),R({exitCode:c,signal:f,stdout:b,stderr:T,stdoutTruncated:A,stderrTruncated:y,timedOut:m})})}),Y=async({rootDirectory:a,maxEntries:i,includeHidden:l,maxDepth:h,includeDirectories:S,includeFiles:R})=>{let g=[],d=!1,b=async(T,A)=>{if(g.length>=i||g.length>=v){d=!0;return}let y=await E.readdir(T,{withFileTypes:!0});y.sort((m,p)=>m.name.localeCompare(p.name));for(let m of y){if(g.length>=i||g.length>=v){d=!0;return}if(!l&&m.name.startsWith("."))continue;let p=u.join(T,m.name),O=s(p);if(m.isDirectory()){S&&g.push({path:O,type:"directory"}),A<h&&await b(p,A+1);continue}if(m.isFile()){if(R){let c=await E.stat(p);g.push({path:O,type:"file",sizeBytes:c.size})}continue}if(m.isSymbolicLink()){let c=await E.realpath(p);n(c),(R||S)&&g.push({path:O,type:"symlink",target:s(c)})}}};return await b(a,0),{entries:g,truncated:d}},yt=async a=>{let i=await r(a),l=await E.stat(i);if(l.isFile())return{files:[i],truncated:!1};if(l.isDirectory()){let h=await Y({rootDirectory:i,maxEntries:v,includeHidden:!1,maxDepth:100,includeDirectories:!1,includeFiles:!0});return{files:h.entries.map(S=>u.resolve(e,S.path)),truncated:h.truncated}}throw new Error(`Path is neither a file nor a directory: ${a}`)};return{workspaceRoot:e,toWorkspacePath:s,resolveExistingPath:r,resolveReadableFile:_,resolveWritableFile:w,executeCommand:bt,walkDirectory:Y,collectTextMatches:async({query:a,searchPath:i,maxResults:l,caseSensitive:h,useRegex:S,globPattern:R,requireCodeFiles:g})=>{let{files:d,truncated:b}=await yt(i),T=[],A=S?new RegExp(a,h?"g":"gi"):void 0,y=h?a:a.toLowerCase(),m=!1;for(let p of d){if(m)break;let O=u.extname(p).toLowerCase();if(g&&!rt.has(O))continue;let c=s(p);if(!u.matchesGlob(Dt(c),R))continue;let f=await E.stat(p);if(!f.isFile()||f.size>nt)continue;let L=await E.readFile(p,"utf8");if(L.includes("\0"))continue;let P=L.split(/\r?\n/u);for(let U=0;U<P.length&&!m;U+=1){let C=P[U];if(C.length===0)continue;if(A){A.lastIndex=0;for(let F of C.matchAll(A)){let V=F.index;if(V!==void 0&&(T.push({path:c,line:U+1,column:V+1,preview:C}),T.length>=l)){m=!0;break}}continue}let J=h?C:C.toLowerCase(),W=0;for(;W<J.length;){let F=J.indexOf(y,W);if(F===-1)break;if(T.push({path:c,line:U+1,column:F+1,preview:C}),T.length>=l){m=!0;break}W=F+Math.max(y.length,1)}}}return{matches:T,truncated:b||m}}}};var gt=async t=>{let e=await ht(t);return{tools:[]}};var Tt=t=>{let e=t.usageTotals.inputTokens+t.usageTotals.outputTokens+t.usageTotals.cacheReadTokens+t.usageTotals.cacheWriteTokens,s=t.usageTotals.totalNanoAiu/ot,n=s*N;j!=="none"&&(process.stdout.write(`
|
|
17
|
-
Session usage summary:
|
|
18
|
-
`),process.stdout.write(` Input tokens: ${t.usageTotals.inputTokens}
|
|
19
|
-
`),process.stdout.write(` Output tokens: ${t.usageTotals.outputTokens}
|
|
20
|
-
`),j==="detailed"&&(process.stdout.write(` Usage events: ${t.usageTotals.events}
|
|
21
|
-
`),process.stdout.write(` Total tokens: ${e}
|
|
22
|
-
`),process.stdout.write(` Cache read tokens: ${t.usageTotals.cacheReadTokens}
|
|
23
|
-
`),process.stdout.write(` Cache write tokens: ${t.usageTotals.cacheWriteTokens}
|
|
24
|
-
`),process.stdout.write(` Estimated cost: ${s.toFixed(6)} AIU
|
|
25
|
-
`),N>0?process.stdout.write(` Estimated cost (USD): ${et(n)} (COPILOT_USD_PER_AIU=${N})
|
|
26
|
-
`):process.stdout.write(` Estimated cost (USD): set COPILOT_USD_PER_AIU to enable USD estimate
|
|
27
|
-
`)))};var{tools:$t}=await gt(process.cwd()),vt=wt.createInterface({input:process.stdin,output:process.stdout}),H=class{constructor(e){this.client=e;this.spinner=ft(()=>!this.isShuttingDown&&!this.isInputClosed)}session=null;rl=vt;tools=$t;detachSessionHandlers=[];isShuttingDown=!1;isInputClosed=!1;uiState={waitingForAssistantOutput:!1,waitingForToolResult:!1};usageTotals={events:0,inputTokens:0,outputTokens:0,cacheReadTokens:0,cacheWriteTokens:0,totalNanoAiu:0};spinner;async init(){let e=await pt(this);this.setSession(e)}setSession(e){this.session=e}requireSession(){if(!this.session)throw new Error("No active session available.");return this.session}async cleanupAndExit(){if(this.isShuttingDown)return;if(this.isShuttingDown=!0,this.uiState.waitingForAssistantOutput=!1,this.uiState.waitingForToolResult=!1,this.spinner.stop(!1),this.detachSessionHandlers.forEach(s=>s()),this.detachSessionHandlers=[],this.session)try{await this.session.disconnect()}catch(s){console.error("[shutdown] session disconnect warning",s)}Tt(this);try{await this.client.stop()}catch(s){console.error("[shutdown] client stop warning",s)}this.rl.close(),process.exitCode=0,setTimeout(()=>{process.exit(0)},25).unref()}listenToShutdownSignals(){this.rl.on("close",()=>{this.isShuttingDown=!0,this.isInputClosed=!0}),process.on("unhandledRejection",e=>{this.isShuttingDown||console.error("[unhandled rejection]",e)}),process.on("SIGINT",async()=>{process.stdout.write(`
|
|
28
|
-
Shutting down...
|
|
29
|
-
`),await this.cleanupAndExit()})}};import{fileURLToPath as Gt}from"url";var St=async t=>{let e=await t.getAuthStatus();if(!e.isAuthenticated){let s=q?"A token was provided but authentication still failed. Verify your token has Copilot access.":"No token detected. Authenticate with `copilot auth login` (or set GITHUB_TOKEN / GH_TOKEN).";console.error("Copilot authentication is not configured."),e.statusMessage&&console.error(`Status: ${e.statusMessage}`),console.error(s),await t.stop(),process.exit(1)}},At=()=>{console.log(`
|
|
30
|
-
System Commands:`),console.log(" /help Show commands"),console.log(" /status Show auth + session info"),console.log(" /models List available models"),console.log(" /new Start a fresh session"),console.log(` exit|quit|q Leave the agent
|
|
31
|
-
`),console.log("----------------------------------------------------------------"),console.log(`
|
|
32
|
-
Test Generation Commands:`),console.log(" /init Init E2E tests from zero"),console.log(" /custom-tests Add custom E2E test based on your input"),console.log(" /diff-test Add E2E test based on code diff")},xt=()=>{console.log(`
|
|
33
|
-
|
|
34
|
-
Welcome to LeverAgent, your E2E testing assistant!`),console.log(`You're currently using version: ${it} with the following model: ${k}`),console.log("Type your message and press Enter. Use 'exit', 'quit', or 'q' to quit."),At()},Et=async t=>{let e=Gt(new URL(`../prompts/${t}`,import.meta.url));return await tt(e,{onError:n=>{console.error(`[prompt] could not read ${e}; continuing without startup prompt`,n)}})},_t=async t=>{try{let e=await fetch(t);return e.ok?await e.text():(console.error(`[prompt] could not fetch ${t}; HTTP ${e.status} ${e.statusText}`),"")}catch(e){return console.error(`[prompt] could not fetch ${t}`,e),""}},x=t=>{if(!(t.isShuttingDown||t.isInputClosed)){if(process.stdin.readableEnded||process.stdin.destroyed){t.isInputClosed=!0,t.cleanupAndExit();return}try{t.rl.question(`${$}You: `,async e=>{if(process.stdout.write(D),t.isShuttingDown||t.isInputClosed)return;let s=e.trim().toLowerCase();if(s==="exit"||s==="quit"||s==="q"){await t.cleanupAndExit();return}if(s==="/help"){At(),x(t);return}if(s==="/new"){try{t.setSession(await dt(t))}catch(o){console.error("[request failed] could not start a new session",o)}x(t);return}if(s==="/status"){try{let o=await t.client.getAuthStatus(),r=t.requireSession();console.log(`
|
|
35
|
-
Status`),console.log(` Session: ${r.sessionId}`),console.log(` Model: ${k}`),console.log(` Reasoning: ${G}`),console.log(` Request timeout: ${M}ms`),console.log(` Authenticated: ${o.isAuthenticated}`),o.login&&console.log(` Login: ${o.login}`)}catch(o){console.error("[request failed] could not read status",o)}process.stdout.write(`
|
|
36
|
-
`),x(t);return}if(s==="/models"){try{let r=(await t.client.listModels()).slice(0,20);console.log(`
|
|
37
|
-
Available models (first 20):`),r.forEach(_=>{console.log(` - ${_.id}`)})}catch(o){console.error("[request failed] could not list models",o)}process.stdout.write(`
|
|
38
|
-
`),x(t);return}if(!e.trim()){x(t);return}let n=e;if(s==="/init")n=await Et("init.md");else if(s.startsWith("/custom-tests")){let o=e.slice(13).trim();n=await _t("https://gist.githubusercontent.com/marcellkiss/029ff544f3ed720971e47b8306e8853c/raw/ff7d36b86193a9e314cb2935968c9ccc0e3bd9ef/custom-test.md"),n+=`
|
|
39
|
-
|
|
40
|
-
<<USER_INPUT>>
|
|
41
|
-
${o}`}else s==="/diff-test"?n=await _t("https://gist.githubusercontent.com/marcellkiss/fbe9294d4de00c2ef11f1437919353b7/raw/16a8ae53a9019386ef5fea5d321934d5bec70188/diff-test.md"):s==="/t"&&(n=await Et("test.md"));if(!n.trim()){console.error("[prompt] loaded prompt is empty; request was not sent"),x(t);return}process.stdout.write("Assistant: "),t.uiState.waitingForAssistantOutput=!0,t.spinner.start("Assistant");try{await t.requireSession().sendAndWait({prompt:n},M)}catch(o){t.uiState.waitingForAssistantOutput=!1,t.spinner.stop(!1),o instanceof Error&&o.message.includes("Timeout after")?console.error(`
|
|
42
|
-
[request failed] ${o.message}. Increase COPILOT_REQUEST_TIMEOUT_MS (current: ${M}ms).`):console.error(`
|
|
43
|
-
[request failed]`,o)}process.stdout.write(`
|
|
44
|
-
`),!t.isShuttingDown&&!t.isInputClosed&&x(t)})}catch(e){if(e&&typeof e=="object"&&"code"in e&&e.code==="ERR_USE_AFTER_CLOSE"){t.isInputClosed=!0,t.cleanupAndExit();return}throw e}}};var K=new qt({githubToken:q,useLoggedInUser:!0,autoRestart:!1});await K.start();await St(K);var z=new H(K);await z.init();xt();x(z);z.listenToShutdownSignals();
|