@zibby/skills 0.1.38 → 0.1.39

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/index.js CHANGED
@@ -1,27 +1,27 @@
1
- import{registerSkill as I}from"@zibby/agent-workflow";import{createRequire as Pr}from"module";import{join as Jr}from"path";var Ur=Pr(import.meta.url);function qr(){if(process.env.MCP_BROWSER_PATH)return process.env.MCP_BROWSER_PATH;try{return Ur.resolve("@zibby/mcp-browser/dist/bin/mcp-browser-zibby.js")}catch{return null}}var Mr="1280x720",Dr="1280x720";function Br({headless:s}={}){if(s===!0)return!0;if(s===!1)return!1;let t=process.env.ZIBBY_HEADLESS;return t==="1"||String(t).toLowerCase()==="true"}function Kr(s,t){let e=(s||[]).filter(r=>r!=="--headless");return t?[...e,"--headless"]:e}var it={id:"browser",serverName:"playwright",cursorKey:"playwright-official",allowedTools:["mcp__playwright__*"],sessionEnvKey:"ZIBBY_SESSION_INFO",description:"Playwright Browser MCP Server",envKeys:[],tools:[],promptFragment:`Execute this test using the browser tools available to you. You MUST make actual browser tool calls \u2014 do not fabricate results.
1
+ import{registerSkill as O}from"@zibby/agent-workflow";import{createRequire as Kr}from"module";import{join as Gr}from"path";var Fr=Kr(import.meta.url);function zr(){if(process.env.MCP_BROWSER_PATH)return process.env.MCP_BROWSER_PATH;try{return Fr.resolve("@zibby/mcp-browser/dist/bin/mcp-browser-zibby.js")}catch{return null}}var Hr="1280x720",Wr="1280x720";function Yr({headless:s}={}){if(s===!0)return!0;if(s===!1)return!1;let t=process.env.ZIBBY_HEADLESS;return t==="1"||String(t).toLowerCase()==="true"}function Zr(s,t){let e=(s||[]).filter(r=>r!=="--headless");return t?[...e,"--headless"]:e}var ct={id:"browser",serverName:"playwright",cursorKey:"playwright-official",allowedTools:["mcp__playwright__*"],sessionEnvKey:"ZIBBY_SESSION_INFO",description:"Playwright Browser MCP Server",envKeys:[],tools:[],promptFragment:`Execute this test using the browser tools available to you. You MUST make actual browser tool calls \u2014 do not fabricate results.
2
2
  If you DO NOT have access to browser tools \u2192 return {"success": false, "steps": [], "browserClosed": false, "notes": "No browser tools available"}.
3
- DO NOT return success: true unless you ACTUALLY called browser tools.`,resolve({sessionPath:s,workspace:t,nodeName:e,headless:r}={}){let i=qr(),n=s&&e?Jr(s,e):null,o=n||s||t||"test-results",c=Br({headless:r}),a={};if(n&&(a.ZIBBY_NODE_SESSION_PATH=n),s&&(a.ZIBBY_SESSION_PATH=s),!i)throw new Error(`@zibby/mcp-browser is not installed.
3
+ DO NOT return success: true unless you ACTUALLY called browser tools.`,resolve({sessionPath:s,workspace:t,nodeName:e,headless:r}={}){let i=zr(),n=s&&e?Gr(s,e):null,o=n||s||t||"test-results",c=Yr({headless:r}),a={};if(n&&(a.ZIBBY_NODE_SESSION_PATH=n),s&&(a.ZIBBY_SESSION_PATH=s),!i)throw new Error(`@zibby/mcp-browser is not installed.
4
4
  Cloud: verify the Fargate image has it (packages/Dockerfile installs it globally alongside @zibby/cli).
5
5
  Local: \`npm install @zibby/mcp-browser\` in your workflow, or use the global @zibby/cli install (which pulls it transitively).
6
- Override: set MCP_BROWSER_PATH to the path of mcp-browser-zibby.js.`);return{command:"node",args:Kr([i,"--isolated",`--save-video=${Mr}`,`--viewport-size=${Dr}`,`--output-dir=${o}`],c),env:a}}};import{createRequire as Fr}from"module";import{resolveIntegrationToken as zr,clearTokenCache as Wr}from"@zibby/core/backend-client.js";var R=Object.freeze({SENTRY:"sentry",JIRA:"jira",GITHUB:"github",GITLAB:"gitlab",SLACK:"slack",LARK:"lark",OPENAI_BILLING:"openai_billing",ANTHROPIC_BILLING:"anthropic_billing",CURSOR_ADMIN:"cursor_admin",NOTION:"notion",PLANE:"plane",LINEAR:"linear"}),Gr=Object.freeze({sentry:{id:"sentry",name:"Sentry",connectPath:"/integrations?provider=sentry"},jira:{id:"jira",name:"Jira",connectPath:"/integrations?provider=jira"},github:{id:"github",name:"GitHub",connectPath:"/integrations?provider=github"},gitlab:{id:"gitlab",name:"GitLab",connectPath:"/integrations?provider=gitlab"},slack:{id:"slack",name:"Slack",connectPath:"/integrations?provider=slack"},lark:{id:"lark",name:"Lark",connectPath:"/integrations?provider=lark"},openai_billing:{id:"openai_billing",name:"OpenAI Admin",connectPath:"/integrations?provider=openai_billing"},anthropic_billing:{id:"anthropic_billing",name:"Anthropic Admin",connectPath:"/integrations?provider=anthropic_billing"},cursor_admin:{id:"cursor_admin",name:"Cursor Admin",connectPath:"/integrations?provider=cursor_admin"},notion:{id:"notion",name:"Notion",connectPath:"/integrations?provider=notion"},plane:{id:"plane",name:"Plane",connectPath:"/integrations?provider=plane"},linear:{id:"linear",name:"Linear",connectPath:"/integrations?provider=linear"}});var Hr=Fr(import.meta.url);function Yr(){if(process.env.MCP_JIRA_PATH)return process.env.MCP_JIRA_PATH;try{return Hr.resolve("@zibby/mcp-jira/index.js")}catch{return null}}var Zr=new Set(["paragraph","heading","bulletList","orderedList","listItem","blockquote","codeBlock","rule","table","tableRow","tableCell","tableHeader","mediaSingle","panel"]);function Vr(s,t){if(!t||!t.length)return s;let e=s;for(let r of t)r.type==="strong"?e=`**${e}**`:r.type==="em"?e=`_${e}_`:r.type==="code"?e=`\`${e}\``:r.type==="strike"?e=`~~${e}~~`:r.type==="link"&&r.attrs?.href&&(e=`[${e}](${r.attrs.href})`);return e}function he(s,t=0){if(!Array.isArray(s))return"";let e=[];for(let r of s){if(r.type==="text"){e.push(Vr(r.text||"",r.marks));continue}if(r.type==="hardBreak"){e.push(`
6
+ Override: set MCP_BROWSER_PATH to the path of mcp-browser-zibby.js.`);return{command:"node",args:Zr([i,"--isolated",`--save-video=${Hr}`,`--viewport-size=${Wr}`,`--output-dir=${o}`],c),env:a}}};import{createRequire as Qr}from"module";import{resolveIntegrationToken as Xr,clearTokenCache as es}from"@zibby/core/backend-client.js";var A=Object.freeze({SENTRY:"sentry",JIRA:"jira",GITHUB:"github",GITLAB:"gitlab",SLACK:"slack",LARK:"lark",OPENAI_BILLING:"openai_billing",ANTHROPIC_BILLING:"anthropic_billing",CURSOR_ADMIN:"cursor_admin",NOTION:"notion",PLANE:"plane",LINEAR:"linear"}),Vr=Object.freeze({sentry:{id:"sentry",name:"Sentry",connectPath:"/integrations?provider=sentry"},jira:{id:"jira",name:"Jira",connectPath:"/integrations?provider=jira"},github:{id:"github",name:"GitHub",connectPath:"/integrations?provider=github"},gitlab:{id:"gitlab",name:"GitLab",connectPath:"/integrations?provider=gitlab"},slack:{id:"slack",name:"Slack",connectPath:"/integrations?provider=slack"},lark:{id:"lark",name:"Lark",connectPath:"/integrations?provider=lark"},openai_billing:{id:"openai_billing",name:"OpenAI Admin",connectPath:"/integrations?provider=openai_billing"},anthropic_billing:{id:"anthropic_billing",name:"Anthropic Admin",connectPath:"/integrations?provider=anthropic_billing"},cursor_admin:{id:"cursor_admin",name:"Cursor Admin",connectPath:"/integrations?provider=cursor_admin"},notion:{id:"notion",name:"Notion",connectPath:"/integrations?provider=notion"},plane:{id:"plane",name:"Plane",connectPath:"/integrations?provider=plane"},linear:{id:"linear",name:"Linear",connectPath:"/integrations?provider=linear"}});var ts=Qr(import.meta.url);function rs(){if(process.env.MCP_JIRA_PATH)return process.env.MCP_JIRA_PATH;try{return ts.resolve("@zibby/mcp-jira/index.js")}catch{return null}}var ss=new Set(["paragraph","heading","bulletList","orderedList","listItem","blockquote","codeBlock","rule","table","tableRow","tableCell","tableHeader","mediaSingle","panel"]);function is(s,t){if(!t||!t.length)return s;let e=s;for(let r of t)r.type==="strong"?e=`**${e}**`:r.type==="em"?e=`_${e}_`:r.type==="code"?e=`\`${e}\``:r.type==="strike"?e=`~~${e}~~`:r.type==="link"&&r.attrs?.href&&(e=`[${e}](${r.attrs.href})`);return e}function ge(s,t=0){if(!Array.isArray(s))return"";let e=[];for(let r of s){if(r.type==="text"){e.push(is(r.text||"",r.marks));continue}if(r.type==="hardBreak"){e.push(`
7
7
  `);continue}if(r.type==="rule"){e.push(`
8
8
  ---
9
- `);continue}let i=r.content?he(r.content,t+1):"";if(r.type==="listItem")e.push(i);else if(r.type==="bulletList"){let n=(r.content||[]).map(o=>`- ${he(o.content||[],t+1).trim()}`);e.push(`
9
+ `);continue}let i=r.content?ge(r.content,t+1):"";if(r.type==="listItem")e.push(i);else if(r.type==="bulletList"){let n=(r.content||[]).map(o=>`- ${ge(o.content||[],t+1).trim()}`);e.push(`
10
10
  ${n.join(`
11
11
  `)}
12
- `)}else if(r.type==="orderedList"){let n=(r.content||[]).map((o,c)=>`${c+1}. ${he(o.content||[],t+1).trim()}`);e.push(`
12
+ `)}else if(r.type==="orderedList"){let n=(r.content||[]).map((o,c)=>`${c+1}. ${ge(o.content||[],t+1).trim()}`);e.push(`
13
13
  ${n.join(`
14
14
  `)}
15
15
  `)}else if(r.type==="heading"){let n=r.attrs?.level||2;e.push(`
16
16
 
17
17
  ${"#".repeat(n)} ${i.trim()}
18
18
 
19
- `)}else Zr.has(r.type)?e.push(`
19
+ `)}else ss.has(r.type)?e.push(`
20
20
 
21
21
  ${i}
22
22
  `):e.push(i)}return e.join("").replace(/\n{3,}/g,`
23
23
 
24
- `)}function re(s){return String(s||"").toLowerCase().replace(/\s+/g,"").replace(/[()\-_::"'`]/g,"")}function Te(s){return re(s).replace(/[a-z0-9]+/g,"")}function ge(s,t){let e=re(s),r=re(t);if(!e||!r)return 0;if(e===r)return 1;if(e.length===1||r.length===1)return e===r?1:0;let i=l=>{let p=new Map;for(let d=0;d<l.length-1;d++){let m=l.slice(d,d+2);p.set(m,(p.get(m)||0)+1)}return p},n=i(e),o=i(r),c=0,a=0,u=0;for(let l of n.values())a+=l;for(let l of o.values())u+=l;for(let[l,p]of n.entries()){let d=o.get(l)||0;c+=Math.min(p,d)}return 2*c/Math.max(1,a+u)}function H(s){return String(s||"").toLowerCase().replace(/\s+/g,"").replace(/[()\-_::"'`]/g,"")}function Qr(s,t=[]){let e=Array.isArray(t)?t:[];if(e.length===0)return{requested:s||null,resolved:null,strategy:"none"};let r=e.filter(c=>!c.subtask),i=r.length>0?r:e,n=H(s);if(n){let c=i.find(l=>H(l.name)===n);if(c)return{requested:s,resolved:c,strategy:"exact"};let a={task:["task","\u4EFB\u52A1","\u4E8B\u9879","to do","todo"],story:["story","\u7528\u6237\u6545\u4E8B","\u9700\u6C42"],bug:["bug","\u7F3A\u9677","\u95EE\u9898"],improvement:["improvement","\u4F18\u5316","\u6539\u8FDB"],epic:["epic","\u53F2\u8BD7"]};for(let l of Object.values(a)){if(!l.some(d=>H(d)===n))continue;let p=i.find(d=>l.some(m=>H(m)===H(d.name)));if(p)return{requested:s,resolved:p,strategy:"alias"}}let u=i.map(l=>({t:l,score:ge(s,l.name)})).sort((l,p)=>p.score-l.score);if(u[0]&&u[0].score>=.5)return{requested:s,resolved:u[0].t,strategy:"fuzzy"}}let o=["task","story","bug","improvement","epic"];for(let c of o){let a=i.find(u=>H(u.name)===c);if(a)return{requested:s||null,resolved:a,strategy:"default-preferred"}}return{requested:s||null,resolved:i[0],strategy:"default-first"}}async function nt(s){let t=`projectKeys=${encodeURIComponent(s)}&expand=projects.issuetypes`,e=await A(`/rest/api/3/issue/createmeta?${t}`),r=Array.isArray(e?.projects)?e.projects:[],n=r.find(c=>String(c?.key||"").toUpperCase()===String(s||"").toUpperCase())||r[0]||null;return(Array.isArray(n?.issuetypes)?n.issuetypes:[]).map(c=>({id:c.id,name:c.name,subtask:!!c.subtask,description:c.description||null}))}async function ot(s,t){if(!s)throw new Error("projectKey is required");let e="sprint is not EMPTY";t==="active"?e="sprint in openSprints()":t==="closed"?e="sprint in closedSprints()":t==="future"&&(e="sprint in futureSprints()");let r=`project = ${s} AND ${e} ORDER BY updated DESC`,i=`jql=${encodeURIComponent(r)}&maxResults=100&fields=customfield_10020`,n=await A(`/rest/api/3/search/jql?${i}`),o=new Map;for(let c of n.issues||[])for(let a of c.fields?.customfield_10020||[])a&&!o.has(a.id)&&o.set(a.id,{id:a.id,name:a.name,state:a.state,boardId:a.boardId||null,startDate:a.startDate||null,endDate:a.endDate||null,goal:a.goal||null});return[...o.values()].sort((c,a)=>{let u={active:0,future:1,closed:2},l=(u[c.state]??3)-(u[a.state]??3);return l!==0?l:String(a.startDate||"").localeCompare(String(c.startDate||""))})}function Xr(s,{sprintId:t,sprintName:e,target:r}={}){let i=Array.isArray(s)?s:[];if(!i.length)return{sprint:null,selectedBy:"none"};if(t!=null&&String(t).trim()!=="")return{sprint:i.find(c=>String(c.id)===String(t))||null,selectedBy:"id"};if(e&&String(e).trim()){let o=String(e).trim(),c=i.find(u=>String(u.name||"").toLowerCase()===o.toLowerCase());if(c)return{sprint:c,selectedBy:"name-exact"};let a=i.map(u=>({s:u,score:ge(o,u.name||"")})).sort((u,l)=>l.score-u.score);return a[0]&&a[0].score>=.5?{sprint:a[0].s,selectedBy:"name-fuzzy"}:{sprint:null,selectedBy:"name-none"}}let n=String(r||"current").trim().toLowerCase();return n==="active"||n==="current"||n==="latest"?{sprint:i[0],selectedBy:n}:{sprint:i[0],selectedBy:"default"}}function es(s,t){let e=s?.fields?.customfield_10020;return Array.isArray(e)?e.some(r=>String(r?.id)===String(t)):!1}async function ts({issueKey:s,projectKey:t,sprintId:e,attempts:r=3,delayMs:i=450}){let n=[];for(let o=0;o<r;o++){try{let c=`project = ${t} AND key = ${s} AND sprint = ${e}`,a=`jql=${encodeURIComponent(c)}&maxResults=1&fields=key,status`,u=await A(`/rest/api/3/search/jql?${a}`);if(Number(u?.total||0)>0)return n.push({attempt:o+1,jql:!0,issueField:null}),{ok:!0,method:"jql",traces:n};let p=await A(`/rest/api/3/issue/${s}?fields=customfield_10020,status`),d=es(p,e);if(n.push({attempt:o+1,jql:!1,issueField:d}),d)return{ok:!0,method:"issue_field",traces:n}}catch(c){n.push({attempt:o+1,error:String(c?.message||c)})}o<r-1&&await new Promise(c=>setTimeout(c,i))}return{ok:!1,method:"none",traces:n}}async function Ee({issueKey:s,projectKey:t,sprintId:e,sprintName:r,target:i}){if(!s)return{ok:!1,error:"issueKey is required"};let n=t;if(!n&&(n=(await A(`/rest/api/3/issue/${s}?fields=project`))?.fields?.project?.key||null,!n))return{ok:!1,error:`Could not resolve project for ${s}`};let o=await ot(n,"active");if(!o.length)return{ok:!1,error:`No assignable active sprint found for project ${n}`};let{sprint:c,selectedBy:a}=Xr(o,{sprintId:e,sprintName:r,target:i});if(!c)return{ok:!1,error:`No matching sprint found in ${n}`,requested:{sprintId:e??null,sprintName:r??null,target:i??"current"},availableSprints:o.map(p=>({id:p.id,name:p.name,state:p.state}))};await A(`/rest/api/3/issue/${s}`,{method:"PUT",body:{fields:{customfield_10020:Number(c.id)}}});let u=await ts({issueKey:s,projectKey:n,sprintId:c.id}),l=u.ok;return{ok:l,issueKey:s,projectKey:n,sprintId:c.id,sprintName:c.name,selectedBy:a,verifiedBy:u.method,verified:l,verificationTrace:u.traces,warning:l?null:`Sprint assignment attempted but verification did not find ${s} in sprint ${c.id}`}}async function A(s,t={}){let e=async()=>{let{token:r,cloudId:i}=await zr("jira");if(typeof r!="string"||!r)throw new Error(`Invalid jira token type: ${typeof r}`);if(!i)throw new Error("Invalid jira cloudId: missing");let n=`https://api.atlassian.com/ex/jira/${i}${s}`,o=await fetch(n,{method:t.method||"GET",headers:{Authorization:`Bearer ${r}`,Accept:"application/json",...t.body?{"Content-Type":"application/json"}:{},...t.headers},body:t.body?JSON.stringify(t.body):void 0});if(!o.ok){let a=await o.text().catch(()=>"");throw new Error(`Jira API ${o.status}: ${a.slice(0,300)}`)}let c=await o.text().catch(()=>"");if(!c||!c.trim())return{};try{return JSON.parse(c)}catch{return{raw:c}}};try{return await e()}catch(r){let i=String(r?.message||r||"").toLowerCase();if(!(i.includes("token")||i.includes("401")||i.includes("403")||i.includes("substring")))throw r;return Wr("jira"),e()}}var at={id:"jira",serverName:"jira",allowedTools:["mcp__jira__*"],requiresIntegration:R.JIRA,envKeys:["ATLASSIAN_ACCESS_TOKEN","ATLASSIAN_CLOUD_ID"],description:"Zibby Jira MCP Server (OAuth Bearer)",promptFragment:`## Jira (connected)
24
+ `)}function ie(s){return String(s||"").toLowerCase().replace(/\s+/g,"").replace(/[()\-_::"'`]/g,"")}function xe(s){return ie(s).replace(/[a-z0-9]+/g,"")}function _e(s,t){let e=ie(s),r=ie(t);if(!e||!r)return 0;if(e===r)return 1;if(e.length===1||r.length===1)return e===r?1:0;let i=l=>{let d=new Map;for(let p=0;p<l.length-1;p++){let m=l.slice(p,p+2);d.set(m,(d.get(m)||0)+1)}return d},n=i(e),o=i(r),c=0,a=0,u=0;for(let l of n.values())a+=l;for(let l of o.values())u+=l;for(let[l,d]of n.entries()){let p=o.get(l)||0;c+=Math.min(d,p)}return 2*c/Math.max(1,a+u)}function Z(s){return String(s||"").toLowerCase().replace(/\s+/g,"").replace(/[()\-_::"'`]/g,"")}function ns(s,t=[]){let e=Array.isArray(t)?t:[];if(e.length===0)return{requested:s||null,resolved:null,strategy:"none"};let r=e.filter(c=>!c.subtask),i=r.length>0?r:e,n=Z(s);if(n){let c=i.find(l=>Z(l.name)===n);if(c)return{requested:s,resolved:c,strategy:"exact"};let a={task:["task","\u4EFB\u52A1","\u4E8B\u9879","to do","todo"],story:["story","\u7528\u6237\u6545\u4E8B","\u9700\u6C42"],bug:["bug","\u7F3A\u9677","\u95EE\u9898"],improvement:["improvement","\u4F18\u5316","\u6539\u8FDB"],epic:["epic","\u53F2\u8BD7"]};for(let l of Object.values(a)){if(!l.some(p=>Z(p)===n))continue;let d=i.find(p=>l.some(m=>Z(m)===Z(p.name)));if(d)return{requested:s,resolved:d,strategy:"alias"}}let u=i.map(l=>({t:l,score:_e(s,l.name)})).sort((l,d)=>d.score-l.score);if(u[0]&&u[0].score>=.5)return{requested:s,resolved:u[0].t,strategy:"fuzzy"}}let o=["task","story","bug","improvement","epic"];for(let c of o){let a=i.find(u=>Z(u.name)===c);if(a)return{requested:s||null,resolved:a,strategy:"default-preferred"}}return{requested:s||null,resolved:i[0],strategy:"default-first"}}async function lt(s){let t=`projectKeys=${encodeURIComponent(s)}&expand=projects.issuetypes`,e=await $(`/rest/api/3/issue/createmeta?${t}`),r=Array.isArray(e?.projects)?e.projects:[],n=r.find(c=>String(c?.key||"").toUpperCase()===String(s||"").toUpperCase())||r[0]||null;return(Array.isArray(n?.issuetypes)?n.issuetypes:[]).map(c=>({id:c.id,name:c.name,subtask:!!c.subtask,description:c.description||null}))}async function ut(s,t){if(!s)throw new Error("projectKey is required");let e="sprint is not EMPTY";t==="active"?e="sprint in openSprints()":t==="closed"?e="sprint in closedSprints()":t==="future"&&(e="sprint in futureSprints()");let r=`project = ${s} AND ${e} ORDER BY updated DESC`,i=`jql=${encodeURIComponent(r)}&maxResults=100&fields=customfield_10020`,n=await $(`/rest/api/3/search/jql?${i}`),o=new Map;for(let c of n.issues||[])for(let a of c.fields?.customfield_10020||[])a&&!o.has(a.id)&&o.set(a.id,{id:a.id,name:a.name,state:a.state,boardId:a.boardId||null,startDate:a.startDate||null,endDate:a.endDate||null,goal:a.goal||null});return[...o.values()].sort((c,a)=>{let u={active:0,future:1,closed:2},l=(u[c.state]??3)-(u[a.state]??3);return l!==0?l:String(a.startDate||"").localeCompare(String(c.startDate||""))})}function os(s,{sprintId:t,sprintName:e,target:r}={}){let i=Array.isArray(s)?s:[];if(!i.length)return{sprint:null,selectedBy:"none"};if(t!=null&&String(t).trim()!=="")return{sprint:i.find(c=>String(c.id)===String(t))||null,selectedBy:"id"};if(e&&String(e).trim()){let o=String(e).trim(),c=i.find(u=>String(u.name||"").toLowerCase()===o.toLowerCase());if(c)return{sprint:c,selectedBy:"name-exact"};let a=i.map(u=>({s:u,score:_e(o,u.name||"")})).sort((u,l)=>l.score-u.score);return a[0]&&a[0].score>=.5?{sprint:a[0].s,selectedBy:"name-fuzzy"}:{sprint:null,selectedBy:"name-none"}}let n=String(r||"current").trim().toLowerCase();return n==="active"||n==="current"||n==="latest"?{sprint:i[0],selectedBy:n}:{sprint:i[0],selectedBy:"default"}}function as(s,t){let e=s?.fields?.customfield_10020;return Array.isArray(e)?e.some(r=>String(r?.id)===String(t)):!1}async function cs({issueKey:s,projectKey:t,sprintId:e,attempts:r=3,delayMs:i=450}){let n=[];for(let o=0;o<r;o++){try{let c=`project = ${t} AND key = ${s} AND sprint = ${e}`,a=`jql=${encodeURIComponent(c)}&maxResults=1&fields=key,status`,u=await $(`/rest/api/3/search/jql?${a}`);if(Number(u?.total||0)>0)return n.push({attempt:o+1,jql:!0,issueField:null}),{ok:!0,method:"jql",traces:n};let d=await $(`/rest/api/3/issue/${s}?fields=customfield_10020,status`),p=as(d,e);if(n.push({attempt:o+1,jql:!1,issueField:p}),p)return{ok:!0,method:"issue_field",traces:n}}catch(c){n.push({attempt:o+1,error:String(c?.message||c)})}o<r-1&&await new Promise(c=>setTimeout(c,i))}return{ok:!1,method:"none",traces:n}}async function Ce({issueKey:s,projectKey:t,sprintId:e,sprintName:r,target:i}){if(!s)return{ok:!1,error:"issueKey is required"};let n=t;if(!n&&(n=(await $(`/rest/api/3/issue/${s}?fields=project`))?.fields?.project?.key||null,!n))return{ok:!1,error:`Could not resolve project for ${s}`};let o=await ut(n,"active");if(!o.length)return{ok:!1,error:`No assignable active sprint found for project ${n}`};let{sprint:c,selectedBy:a}=os(o,{sprintId:e,sprintName:r,target:i});if(!c)return{ok:!1,error:`No matching sprint found in ${n}`,requested:{sprintId:e??null,sprintName:r??null,target:i??"current"},availableSprints:o.map(d=>({id:d.id,name:d.name,state:d.state}))};await $(`/rest/api/3/issue/${s}`,{method:"PUT",body:{fields:{customfield_10020:Number(c.id)}}});let u=await cs({issueKey:s,projectKey:n,sprintId:c.id}),l=u.ok;return{ok:l,issueKey:s,projectKey:n,sprintId:c.id,sprintName:c.name,selectedBy:a,verifiedBy:u.method,verified:l,verificationTrace:u.traces,warning:l?null:`Sprint assignment attempted but verification did not find ${s} in sprint ${c.id}`}}async function $(s,t={}){let e=async()=>{let{token:r,cloudId:i}=await Xr("jira");if(typeof r!="string"||!r)throw new Error(`Invalid jira token type: ${typeof r}`);if(!i)throw new Error("Invalid jira cloudId: missing");let n=`https://api.atlassian.com/ex/jira/${i}${s}`,o=await fetch(n,{method:t.method||"GET",headers:{Authorization:`Bearer ${r}`,Accept:"application/json",...t.body?{"Content-Type":"application/json"}:{},...t.headers},body:t.body?JSON.stringify(t.body):void 0});if(!o.ok){let a=await o.text().catch(()=>"");throw new Error(`Jira API ${o.status}: ${a.slice(0,300)}`)}let c=await o.text().catch(()=>"");if(!c||!c.trim())return{};try{return JSON.parse(c)}catch{return{raw:c}}};try{return await e()}catch(r){let i=String(r?.message||r||"").toLowerCase();if(!(i.includes("token")||i.includes("401")||i.includes("403")||i.includes("substring")))throw r;return es("jira"),e()}}var dt={id:"jira",serverName:"jira",allowedTools:["mcp__jira__*"],requiresIntegration:A.JIRA,envKeys:["ATLASSIAN_ACCESS_TOKEN","ATLASSIAN_CLOUD_ID"],description:"Zibby Jira MCP Server (OAuth Bearer)",promptFragment:`## Jira (connected)
25
25
  You have direct access to the user's Jira. Use these tools proactively:
26
26
 
27
27
  ### Issue tools
@@ -71,11 +71,11 @@ When user asks to move/transition ticket status:
71
71
  3. Pick the correct transition from returned list (match by "to" status name, not guesswork), then call jira_transition_issue with transitionId.
72
72
  4. Call jira_get_issue(issueKey) to verify final status before claiming success.
73
73
  5. If target wording differs (e.g. \u5DF2\u7ECF\u9A8C\u6536 vs \u5DF2\u9A8C\u6536), try toStatus first; only ask user to confirm when no reasonable match exists.
74
- 6. IMPORTANT: When target is clear, complete transition + verification in SAME turn. Do NOT stop after listing options.`,resolve(){let s=Yr();if(!s)return null;let t={};for(let e of this.envKeys)process.env[e]&&(t[e]=process.env[e]);return process.env.ATLASSIAN_INSTANCE_URL&&(t.ATLASSIAN_INSTANCE_URL=process.env.ATLASSIAN_INSTANCE_URL),{command:"node",args:[s],env:t,description:this.description}},async handleToolCall(s,t){try{switch(s){case"jira_list_projects":{let e=await A("/rest/api/3/project"),r=(Array.isArray(e)?e:[]).map(i=>({id:i.id,key:i.key,name:i.name,style:i.style}));return JSON.stringify({count:r.length,projects:r})}case"jira_list_statuses":{let{projectKey:e}=t||{};if(e){let n=await A(`/rest/api/3/project/${encodeURIComponent(e)}/statuses`),o=Array.isArray(n)?n:[],c=new Map;for(let u of o)for(let l of u.statuses||[])l?.id&&(c.has(l.id)||c.set(l.id,{id:l.id,name:l.name,category:l.statusCategory?.name||null}));let a=[...c.values()].sort((u,l)=>String(u.name).localeCompare(String(l.name)));return JSON.stringify({scope:"project",projectKey:e,count:a.length,statuses:a})}let r=await A("/rest/api/3/status"),i=(Array.isArray(r)?r:[]).map(n=>({id:n.id,name:n.name,category:n.statusCategory?.name||null})).sort((n,o)=>String(n.name).localeCompare(String(o.name)));return JSON.stringify({scope:"global",count:i.length,statuses:i})}case"jira_list_issue_types":{let{projectKey:e}=t||{};if(!e)return JSON.stringify({error:"projectKey is required"});let r=await nt(e);return JSON.stringify({projectKey:e,count:r.length,issueTypes:r})}case"jira_search":{let e=t.jql||"",r=t.maxResults||20;e.replace(/\s*ORDER\s+BY\s+.*/i,"").trim()||(e=`created >= -365d ${e}`.trim());let n=`jql=${encodeURIComponent(e)}&maxResults=${r}&fields=summary,status,assignee,priority,updated,issuetype,project`,c=((await A(`/rest/api/3/search/jql?${n}`)).issues||[]).map(a=>({key:a.key,project:a.fields?.project?.key,summary:a.fields?.summary,status:a.fields?.status?.name,assignee:a.fields?.assignee?.displayName||"Unassigned",priority:a.fields?.priority?.name,type:a.fields?.issuetype?.name}));return JSON.stringify({count:c.length,issues:c})}case"jira_get_issue":{let e=t.issueKey;if(!e)return JSON.stringify({error:"issueKey is required"});let r=await A(`/rest/api/3/issue/${e}`);return JSON.stringify({key:r.key,project:r.fields?.project?.key,summary:r.fields?.summary,description:r.fields?.description,status:r.fields?.status?.name,assignee:r.fields?.assignee?.displayName||"Unassigned",priority:r.fields?.priority?.name,type:r.fields?.issuetype?.name,labels:r.fields?.labels,created:r.fields?.created,updated:r.fields?.updated})}case"jira_create_issue":{let{projectKey:e,summary:r,issueType:i,description:n,priority:o,labels:c,assigneeId:a,moveToSprint:u,moveToActiveSprint:l,sprintId:p,sprintName:d,target:m}=t;if(!e||!r)return JSON.stringify({error:"projectKey and summary are required"});let f={requested:i||null,resolved:null,strategy:"none"},y=[];try{y=await nt(e),f=Qr(i,y)}catch{}let _={project:{key:e},summary:r,issuetype:f?.resolved?.id?{id:f.resolved.id}:{name:i||"Task"}};n&&(_.description={type:"doc",version:1,content:[{type:"paragraph",content:[{type:"text",text:n}]}]}),o&&(_.priority={name:o}),c?.length&&(_.labels=c),a&&(_.assignee={id:a});let g=await A("/rest/api/3/issue",{method:"POST",body:{fields:_}}),b={ok:!0,key:g.key,id:g.id,self:g.self};return f?.resolved&&(b.issueType=f.resolved.name,b.issueTypeResolution=f.strategy,f.strategy!=="exact"&&f.requested&&H(f.requested)!==H(f.resolved.name)&&(b.issueTypeWarning=`Requested "${f.requested}" is not available in ${e}; used "${f.resolved.name}" instead.`)),y.length>0&&(b.availableIssueTypes=y.map(h=>h.name)),(u||l)&&(b.sprintMove=await Ee({issueKey:g.key,projectKey:e,sprintId:p,sprintName:d,target:m})),JSON.stringify(b)}case"jira_list_sprints":{let{projectKey:e,state:r}=t,i=await ot(e,r);return JSON.stringify({count:i.length,sprints:i})}case"jira_move_to_active_sprint":{let{issueKey:e,projectKey:r,sprintId:i,sprintName:n,target:o}=t||{},c=await Ee({issueKey:e,projectKey:r,sprintId:i,sprintName:n,target:o||"current"});return JSON.stringify(c)}case"jira_move_issue_to_sprint":{let{issueKey:e,projectKey:r,sprintId:i,sprintName:n,target:o}=t||{},c=await Ee({issueKey:e,projectKey:r,sprintId:i,sprintName:n,target:o});return JSON.stringify(c)}case"jira_get_sprint_issues":{let{sprintName:e,sprintId:r,projectKey:i,status:n,maxResults:o}=t;if(!e&&!r)return JSON.stringify({error:"sprintName or sprintId is required"});let c=o||50,a=r?`sprint = ${r}`:`sprint = "${e}"`,u=i?`project = ${i} AND `:"",l=n?` AND status = "${n}"`:"",p=`${u}${a}${l} ORDER BY status ASC, priority DESC`,d=`jql=${encodeURIComponent(p)}&maxResults=${c}&fields=summary,status,assignee,priority,issuetype,project`,m=await A(`/rest/api/3/search/jql?${d}`),f=(m.issues||[]).map(_=>({key:_.key,project:_.fields?.project?.key,summary:_.fields?.summary,status:_.fields?.status?.name,assignee:_.fields?.assignee?.displayName||"Unassigned",priority:_.fields?.priority?.name,type:_.fields?.issuetype?.name})),y={};for(let _ of f)y[_.status]=(y[_.status]||0)+1;return JSON.stringify({count:f.length,total:m.total||f.length,statusCounts:y,issues:f})}case"jira_get_comments":{let{issueKey:e,maxResults:r}=t;if(!e)return JSON.stringify({error:"issueKey is required"});let n=await A(`/rest/api/3/issue/${e}/comment?maxResults=${r||50}&orderBy=-created`),o=(n.comments||[]).map(c=>{let a="";return c.body?.content&&(a=he(c.body.content)),{id:c.id,author:c.author?.displayName||"Unknown",body:a,created:c.created,updated:c.updated}});return JSON.stringify({count:o.length,total:n.total||o.length,comments:o})}case"jira_add_comment":{let{issueKey:e,body:r}=t;return!e||!r?JSON.stringify({error:"issueKey and body are required"}):(await A(`/rest/api/3/issue/${e}/comment`,{method:"POST",body:{body:{type:"doc",version:1,content:[{type:"paragraph",content:[{type:"text",text:r}]}]}}}),JSON.stringify({ok:!0,issueKey:e}))}case"jira_edit_issue":{let{issueKey:e,fields:r}=t;return!e||!r?JSON.stringify({error:"issueKey and fields are required"}):(await A(`/rest/api/3/issue/${e}`,{method:"PUT",body:{fields:r}}),JSON.stringify({ok:!0,issueKey:e}))}case"jira_transition_issue":{let{issueKey:e,transitionId:r,toStatus:i,statusName:n,status:o}=t;if(!e)return JSON.stringify({error:"issueKey is required"});let c=String(i||n||o||"").trim();if(!r&&!c){let p=((await A(`/rest/api/3/issue/${e}/transitions`)).transitions||[]).map(d=>({id:d.id,name:d.name,to:d.to?.name}));return JSON.stringify({ok:!1,error:"transitionId or toStatus is required",issueKey:e,availableTransitions:p})}let a=r;if(!a){let p=(await A(`/rest/api/3/issue/${e}/transitions`)).transitions||[],d=re(c),m=p.find(f=>re(f?.name||"")===d||re(f?.to?.name||"")===d);if(!m){let f=Te(c);f.length>=2&&(m=p.find(y=>{let _=Te(y?.name||""),g=Te(y?.to?.name||""),b=_.length>=2&&(_.includes(f)||f.includes(_)),h=g.length>=2&&(g.includes(f)||f.includes(g));return b||h}))}if(!m){let f=p.map(b=>{let h=ge(c,b?.name||""),v=ge(c,b?.to?.name||"");return{t:b,score:Math.max(h,v)}}).sort((b,h)=>h.score-b.score),y=f[0],_=f[1];y&&y.score>=.45&&(!_||y.score-_.score>=.12)&&(m=y.t)}if(!m?.id)return JSON.stringify({ok:!1,error:`No transition matches target status: "${c}"`,issueKey:e,availableTransitions:p.map(f=>({id:f.id,name:f.name,to:f.to?.name}))});a=m.id}await A(`/rest/api/3/issue/${e}/transitions`,{method:"POST",body:{transition:{id:a}}});let u=await A(`/rest/api/3/issue/${e}?fields=status`);return JSON.stringify({ok:!0,issueKey:e,transitionId:a,statusAfter:u?.fields?.status?.name||null})}default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(e){return JSON.stringify({error:e.message})}},tools:[{name:"jira_list_projects",description:"List all Jira projects accessible to the user",input_schema:{type:"object",properties:{}}},{name:"jira_list_statuses",description:"List Jira statuses. Use projectKey to get statuses applicable in that project workflow.",input_schema:{type:"object",properties:{projectKey:{type:"string",description:"Optional project key (e.g. PROJ). If omitted, returns global status catalog."}}}},{name:"jira_list_issue_types",description:"List issue types allowed for issue creation in the given project.",input_schema:{type:"object",properties:{projectKey:{type:"string",description:"Project key, e.g. PROJ"}},required:["projectKey"]}},{name:"jira_search",description:"Search Jira issues using JQL",input_schema:{type:"object",properties:{jql:{type:"string",description:'JQL query string, e.g. "project = PROJ AND status = Open"'},maxResults:{type:"number",description:"Max results to return (default 20)"}},required:["jql"]}},{name:"jira_get_issue",description:"Get details of a specific Jira issue",input_schema:{type:"object",properties:{issueKey:{type:"string",description:"Issue key, e.g. PROJ-123"}},required:["issueKey"]}},{name:"jira_create_issue",description:"Create a new Jira issue",input_schema:{type:"object",properties:{projectKey:{type:"string",description:"Project key, e.g. PROJ"},summary:{type:"string",description:"Issue title/summary"},issueType:{type:"string",description:"Issue type (default: Task). Common: Task, Bug, Story, Epic"},description:{type:"string",description:"Issue description (plain text)"},priority:{type:"string",description:"Priority name, e.g. High, Medium, Low"},labels:{type:"array",items:{type:"string"},description:"Array of label strings"},assigneeId:{type:"string",description:"Atlassian account ID to assign to"},moveToSprint:{type:"boolean",description:"If true, move created issue to a sprint and verify."},moveToActiveSprint:{type:"boolean",description:"Backward-compatible alias for moveToSprint."},sprintId:{type:"number",description:"Optional sprint id for placement."},sprintName:{type:"string",description:"Optional sprint name for placement."},target:{type:"string",description:"Placement target when sprintId/sprintName omitted: current|active|latest (default: current)."}},required:["projectKey","summary"]}},{name:"jira_list_sprints",description:"List sprints for a Jira project (returns sprint names, IDs, states, dates)",input_schema:{type:"object",properties:{projectKey:{type:"string",description:"Project key, e.g. PROJ"},state:{type:"string",description:"Filter: active, closed, future. Omit for all."}},required:["projectKey"]}},{name:"jira_get_sprint_issues",description:"Get all issues in a sprint, optionally filtered by status column name",input_schema:{type:"object",properties:{sprintName:{type:"string",description:"Sprint name (from jira_list_sprints). Use this OR sprintId."},sprintId:{type:"number",description:"Sprint ID (from jira_list_sprints). Use this OR sprintName."},projectKey:{type:"string",description:"Project key to scope the search (optional)"},status:{type:"string",description:'Filter by status name (e.g. "\u8FDB\u884C\u4E2D", "\u6D4B\u8BD5", "Done")'},maxResults:{type:"number",description:"Max issues to return (default 50)"}}}},{name:"jira_move_to_active_sprint",description:"Backward-compatible alias: move issue to sprint target and verify membership.",input_schema:{type:"object",properties:{issueKey:{type:"string",description:"Issue key, e.g. PROJ-123"},projectKey:{type:"string",description:"Optional project key. If omitted, inferred from issue."},sprintId:{type:"number",description:"Optional sprint id."},sprintName:{type:"string",description:"Optional sprint name."},target:{type:"string",description:"Target when sprintId/sprintName omitted: current|active|latest (default: current)."}},required:["issueKey"]}},{name:"jira_move_issue_to_sprint",description:"Move an issue to a sprint by id/name/target and verify membership.",input_schema:{type:"object",properties:{issueKey:{type:"string",description:"Issue key, e.g. PROJ-123"},projectKey:{type:"string",description:"Optional project key. If omitted, inferred from issue."},sprintId:{type:"number",description:"Optional sprint id."},sprintName:{type:"string",description:"Optional sprint name."},target:{type:"string",description:"Target when sprintId/sprintName omitted: current|active|latest (default: current)."}},required:["issueKey"]}},{name:"jira_get_comments",description:"Get comments on a Jira issue (newest first)",input_schema:{type:"object",properties:{issueKey:{type:"string",description:"Issue key, e.g. PROJ-123"},maxResults:{type:"number",description:"Max comments to return (default 50)"}},required:["issueKey"]}},{name:"jira_add_comment",description:"Add a comment to a Jira issue",input_schema:{type:"object",properties:{issueKey:{type:"string",description:"Issue key, e.g. PROJ-123"},body:{type:"string",description:"Comment text (plain text)"}},required:["issueKey","body"]}},{name:"jira_edit_issue",description:"Update fields on a Jira issue (summary, story points, labels, priority)",input_schema:{type:"object",properties:{issueKey:{type:"string",description:"Issue key, e.g. PROJ-123"},fields:{type:"object",description:"Object of field names to values",additionalProperties:!0}},required:["issueKey","fields"]}},{name:"jira_transition_issue",description:"Move a Jira issue to a different status. Always pass toStatus when user gave a target; only pass issueKey alone when you explicitly need to list transitions.",input_schema:{type:"object",properties:{issueKey:{type:"string",description:"Issue key, e.g. PROJ-123"},transitionId:{type:"string",description:"Transition ID to perform (optional if toStatus is provided)"},toStatus:{type:"string",description:'Target status/column name (e.g. "\u5DF2\u7ECF\u9A8C\u6536", "Done", "In Progress"). If provided, tool resolves matching transition automatically.'}},required:["issueKey"]}}]};import{existsSync as rs}from"fs";import{fileURLToPath as ss}from"url";import{dirname as is,resolve as ns}from"path";import{resolveIntegrationToken as ct}from"@zibby/core/backend-client.js";function os(){if(process.env.MCP_SKILL_PATH)return process.env.MCP_SKILL_PATH;let s=is(ss(import.meta.url)),t=ns(s,"..","bin","mcp-skill.mjs");return rs(t)?t:null}async function S(s,t={}){let{token:e}=await ct("github"),r=s.startsWith("https://")?s:`https://api.github.com${s}`,i={Authorization:`Bearer ${e}`,Accept:t.accept||"application/vnd.github.v3+json","User-Agent":"Zibby-App",...t.body?{"Content-Type":"application/json"}:{}},n=await fetch(r,{method:t.method||"GET",headers:i,body:t.body?JSON.stringify(t.body):void 0});if(!n.ok){let o=await n.text().catch(()=>"");throw new Error(`GitHub API ${n.status}: ${o.slice(0,300)}`)}return t.raw?n.text():n.json()}var lt={id:"github",serverName:"github",allowedTools:["mcp__github__*"],requiresIntegration:R.GITHUB,envKeys:["GITHUB_TOKEN"],description:"GitHub \u2014 issues, PRs, commits, code search, file reading",promptFragment:`## GitHub (connected)
74
+ 6. IMPORTANT: When target is clear, complete transition + verification in SAME turn. Do NOT stop after listing options.`,resolve(){let s=rs();if(!s)return null;let t={};for(let e of this.envKeys)process.env[e]&&(t[e]=process.env[e]);return process.env.ATLASSIAN_INSTANCE_URL&&(t.ATLASSIAN_INSTANCE_URL=process.env.ATLASSIAN_INSTANCE_URL),{command:"node",args:[s],env:t,description:this.description}},async handleToolCall(s,t){try{switch(s){case"jira_list_projects":{let e=await $("/rest/api/3/project"),r=(Array.isArray(e)?e:[]).map(i=>({id:i.id,key:i.key,name:i.name,style:i.style}));return JSON.stringify({count:r.length,projects:r})}case"jira_list_statuses":{let{projectKey:e}=t||{};if(e){let n=await $(`/rest/api/3/project/${encodeURIComponent(e)}/statuses`),o=Array.isArray(n)?n:[],c=new Map;for(let u of o)for(let l of u.statuses||[])l?.id&&(c.has(l.id)||c.set(l.id,{id:l.id,name:l.name,category:l.statusCategory?.name||null}));let a=[...c.values()].sort((u,l)=>String(u.name).localeCompare(String(l.name)));return JSON.stringify({scope:"project",projectKey:e,count:a.length,statuses:a})}let r=await $("/rest/api/3/status"),i=(Array.isArray(r)?r:[]).map(n=>({id:n.id,name:n.name,category:n.statusCategory?.name||null})).sort((n,o)=>String(n.name).localeCompare(String(o.name)));return JSON.stringify({scope:"global",count:i.length,statuses:i})}case"jira_list_issue_types":{let{projectKey:e}=t||{};if(!e)return JSON.stringify({error:"projectKey is required"});let r=await lt(e);return JSON.stringify({projectKey:e,count:r.length,issueTypes:r})}case"jira_search":{let e=t.jql||"",r=t.maxResults||20;e.replace(/\s*ORDER\s+BY\s+.*/i,"").trim()||(e=`created >= -365d ${e}`.trim());let n=`jql=${encodeURIComponent(e)}&maxResults=${r}&fields=summary,status,assignee,priority,updated,issuetype,project`,c=((await $(`/rest/api/3/search/jql?${n}`)).issues||[]).map(a=>({key:a.key,project:a.fields?.project?.key,summary:a.fields?.summary,status:a.fields?.status?.name,assignee:a.fields?.assignee?.displayName||"Unassigned",priority:a.fields?.priority?.name,type:a.fields?.issuetype?.name}));return JSON.stringify({count:c.length,issues:c})}case"jira_get_issue":{let e=t.issueKey;if(!e)return JSON.stringify({error:"issueKey is required"});let r=await $(`/rest/api/3/issue/${e}`);return JSON.stringify({key:r.key,project:r.fields?.project?.key,summary:r.fields?.summary,description:r.fields?.description,status:r.fields?.status?.name,assignee:r.fields?.assignee?.displayName||"Unassigned",priority:r.fields?.priority?.name,type:r.fields?.issuetype?.name,labels:r.fields?.labels,created:r.fields?.created,updated:r.fields?.updated})}case"jira_create_issue":{let{projectKey:e,summary:r,issueType:i,description:n,priority:o,labels:c,assigneeId:a,moveToSprint:u,moveToActiveSprint:l,sprintId:d,sprintName:p,target:m}=t;if(!e||!r)return JSON.stringify({error:"projectKey and summary are required"});let f={requested:i||null,resolved:null,strategy:"none"},y=[];try{y=await lt(e),f=ns(i,y)}catch{}let _={project:{key:e},summary:r,issuetype:f?.resolved?.id?{id:f.resolved.id}:{name:i||"Task"}};n&&(_.description={type:"doc",version:1,content:[{type:"paragraph",content:[{type:"text",text:n}]}]}),o&&(_.priority={name:o}),c?.length&&(_.labels=c),a&&(_.assignee={id:a});let g=await $("/rest/api/3/issue",{method:"POST",body:{fields:_}}),b={ok:!0,key:g.key,id:g.id,self:g.self};return f?.resolved&&(b.issueType=f.resolved.name,b.issueTypeResolution=f.strategy,f.strategy!=="exact"&&f.requested&&Z(f.requested)!==Z(f.resolved.name)&&(b.issueTypeWarning=`Requested "${f.requested}" is not available in ${e}; used "${f.resolved.name}" instead.`)),y.length>0&&(b.availableIssueTypes=y.map(h=>h.name)),(u||l)&&(b.sprintMove=await Ce({issueKey:g.key,projectKey:e,sprintId:d,sprintName:p,target:m})),JSON.stringify(b)}case"jira_list_sprints":{let{projectKey:e,state:r}=t,i=await ut(e,r);return JSON.stringify({count:i.length,sprints:i})}case"jira_move_to_active_sprint":{let{issueKey:e,projectKey:r,sprintId:i,sprintName:n,target:o}=t||{},c=await Ce({issueKey:e,projectKey:r,sprintId:i,sprintName:n,target:o||"current"});return JSON.stringify(c)}case"jira_move_issue_to_sprint":{let{issueKey:e,projectKey:r,sprintId:i,sprintName:n,target:o}=t||{},c=await Ce({issueKey:e,projectKey:r,sprintId:i,sprintName:n,target:o});return JSON.stringify(c)}case"jira_get_sprint_issues":{let{sprintName:e,sprintId:r,projectKey:i,status:n,maxResults:o}=t;if(!e&&!r)return JSON.stringify({error:"sprintName or sprintId is required"});let c=o||50,a=r?`sprint = ${r}`:`sprint = "${e}"`,u=i?`project = ${i} AND `:"",l=n?` AND status = "${n}"`:"",d=`${u}${a}${l} ORDER BY status ASC, priority DESC`,p=`jql=${encodeURIComponent(d)}&maxResults=${c}&fields=summary,status,assignee,priority,issuetype,project`,m=await $(`/rest/api/3/search/jql?${p}`),f=(m.issues||[]).map(_=>({key:_.key,project:_.fields?.project?.key,summary:_.fields?.summary,status:_.fields?.status?.name,assignee:_.fields?.assignee?.displayName||"Unassigned",priority:_.fields?.priority?.name,type:_.fields?.issuetype?.name})),y={};for(let _ of f)y[_.status]=(y[_.status]||0)+1;return JSON.stringify({count:f.length,total:m.total||f.length,statusCounts:y,issues:f})}case"jira_get_comments":{let{issueKey:e,maxResults:r}=t;if(!e)return JSON.stringify({error:"issueKey is required"});let n=await $(`/rest/api/3/issue/${e}/comment?maxResults=${r||50}&orderBy=-created`),o=(n.comments||[]).map(c=>{let a="";return c.body?.content&&(a=ge(c.body.content)),{id:c.id,author:c.author?.displayName||"Unknown",body:a,created:c.created,updated:c.updated}});return JSON.stringify({count:o.length,total:n.total||o.length,comments:o})}case"jira_add_comment":{let{issueKey:e,body:r}=t;return!e||!r?JSON.stringify({error:"issueKey and body are required"}):(await $(`/rest/api/3/issue/${e}/comment`,{method:"POST",body:{body:{type:"doc",version:1,content:[{type:"paragraph",content:[{type:"text",text:r}]}]}}}),JSON.stringify({ok:!0,issueKey:e}))}case"jira_edit_issue":{let{issueKey:e,fields:r}=t;return!e||!r?JSON.stringify({error:"issueKey and fields are required"}):(await $(`/rest/api/3/issue/${e}`,{method:"PUT",body:{fields:r}}),JSON.stringify({ok:!0,issueKey:e}))}case"jira_transition_issue":{let{issueKey:e,transitionId:r,toStatus:i,statusName:n,status:o}=t;if(!e)return JSON.stringify({error:"issueKey is required"});let c=String(i||n||o||"").trim();if(!r&&!c){let d=((await $(`/rest/api/3/issue/${e}/transitions`)).transitions||[]).map(p=>({id:p.id,name:p.name,to:p.to?.name}));return JSON.stringify({ok:!1,error:"transitionId or toStatus is required",issueKey:e,availableTransitions:d})}let a=r;if(!a){let d=(await $(`/rest/api/3/issue/${e}/transitions`)).transitions||[],p=ie(c),m=d.find(f=>ie(f?.name||"")===p||ie(f?.to?.name||"")===p);if(!m){let f=xe(c);f.length>=2&&(m=d.find(y=>{let _=xe(y?.name||""),g=xe(y?.to?.name||""),b=_.length>=2&&(_.includes(f)||f.includes(_)),h=g.length>=2&&(g.includes(f)||f.includes(g));return b||h}))}if(!m){let f=d.map(b=>{let h=_e(c,b?.name||""),S=_e(c,b?.to?.name||"");return{t:b,score:Math.max(h,S)}}).sort((b,h)=>h.score-b.score),y=f[0],_=f[1];y&&y.score>=.45&&(!_||y.score-_.score>=.12)&&(m=y.t)}if(!m?.id)return JSON.stringify({ok:!1,error:`No transition matches target status: "${c}"`,issueKey:e,availableTransitions:d.map(f=>({id:f.id,name:f.name,to:f.to?.name}))});a=m.id}await $(`/rest/api/3/issue/${e}/transitions`,{method:"POST",body:{transition:{id:a}}});let u=await $(`/rest/api/3/issue/${e}?fields=status`);return JSON.stringify({ok:!0,issueKey:e,transitionId:a,statusAfter:u?.fields?.status?.name||null})}default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(e){return JSON.stringify({error:e.message})}},tools:[{name:"jira_list_projects",description:"List all Jira projects accessible to the user",input_schema:{type:"object",properties:{}}},{name:"jira_list_statuses",description:"List Jira statuses. Use projectKey to get statuses applicable in that project workflow.",input_schema:{type:"object",properties:{projectKey:{type:"string",description:"Optional project key (e.g. PROJ). If omitted, returns global status catalog."}}}},{name:"jira_list_issue_types",description:"List issue types allowed for issue creation in the given project.",input_schema:{type:"object",properties:{projectKey:{type:"string",description:"Project key, e.g. PROJ"}},required:["projectKey"]}},{name:"jira_search",description:"Search Jira issues using JQL",input_schema:{type:"object",properties:{jql:{type:"string",description:'JQL query string, e.g. "project = PROJ AND status = Open"'},maxResults:{type:"number",description:"Max results to return (default 20)"}},required:["jql"]}},{name:"jira_get_issue",description:"Get details of a specific Jira issue",input_schema:{type:"object",properties:{issueKey:{type:"string",description:"Issue key, e.g. PROJ-123"}},required:["issueKey"]}},{name:"jira_create_issue",description:"Create a new Jira issue",input_schema:{type:"object",properties:{projectKey:{type:"string",description:"Project key, e.g. PROJ"},summary:{type:"string",description:"Issue title/summary"},issueType:{type:"string",description:"Issue type (default: Task). Common: Task, Bug, Story, Epic"},description:{type:"string",description:"Issue description (plain text)"},priority:{type:"string",description:"Priority name, e.g. High, Medium, Low"},labels:{type:"array",items:{type:"string"},description:"Array of label strings"},assigneeId:{type:"string",description:"Atlassian account ID to assign to"},moveToSprint:{type:"boolean",description:"If true, move created issue to a sprint and verify."},moveToActiveSprint:{type:"boolean",description:"Backward-compatible alias for moveToSprint."},sprintId:{type:"number",description:"Optional sprint id for placement."},sprintName:{type:"string",description:"Optional sprint name for placement."},target:{type:"string",description:"Placement target when sprintId/sprintName omitted: current|active|latest (default: current)."}},required:["projectKey","summary"]}},{name:"jira_list_sprints",description:"List sprints for a Jira project (returns sprint names, IDs, states, dates)",input_schema:{type:"object",properties:{projectKey:{type:"string",description:"Project key, e.g. PROJ"},state:{type:"string",description:"Filter: active, closed, future. Omit for all."}},required:["projectKey"]}},{name:"jira_get_sprint_issues",description:"Get all issues in a sprint, optionally filtered by status column name",input_schema:{type:"object",properties:{sprintName:{type:"string",description:"Sprint name (from jira_list_sprints). Use this OR sprintId."},sprintId:{type:"number",description:"Sprint ID (from jira_list_sprints). Use this OR sprintName."},projectKey:{type:"string",description:"Project key to scope the search (optional)"},status:{type:"string",description:'Filter by status name (e.g. "\u8FDB\u884C\u4E2D", "\u6D4B\u8BD5", "Done")'},maxResults:{type:"number",description:"Max issues to return (default 50)"}}}},{name:"jira_move_to_active_sprint",description:"Backward-compatible alias: move issue to sprint target and verify membership.",input_schema:{type:"object",properties:{issueKey:{type:"string",description:"Issue key, e.g. PROJ-123"},projectKey:{type:"string",description:"Optional project key. If omitted, inferred from issue."},sprintId:{type:"number",description:"Optional sprint id."},sprintName:{type:"string",description:"Optional sprint name."},target:{type:"string",description:"Target when sprintId/sprintName omitted: current|active|latest (default: current)."}},required:["issueKey"]}},{name:"jira_move_issue_to_sprint",description:"Move an issue to a sprint by id/name/target and verify membership.",input_schema:{type:"object",properties:{issueKey:{type:"string",description:"Issue key, e.g. PROJ-123"},projectKey:{type:"string",description:"Optional project key. If omitted, inferred from issue."},sprintId:{type:"number",description:"Optional sprint id."},sprintName:{type:"string",description:"Optional sprint name."},target:{type:"string",description:"Target when sprintId/sprintName omitted: current|active|latest (default: current)."}},required:["issueKey"]}},{name:"jira_get_comments",description:"Get comments on a Jira issue (newest first)",input_schema:{type:"object",properties:{issueKey:{type:"string",description:"Issue key, e.g. PROJ-123"},maxResults:{type:"number",description:"Max comments to return (default 50)"}},required:["issueKey"]}},{name:"jira_add_comment",description:"Add a comment to a Jira issue",input_schema:{type:"object",properties:{issueKey:{type:"string",description:"Issue key, e.g. PROJ-123"},body:{type:"string",description:"Comment text (plain text)"}},required:["issueKey","body"]}},{name:"jira_edit_issue",description:"Update fields on a Jira issue (summary, story points, labels, priority)",input_schema:{type:"object",properties:{issueKey:{type:"string",description:"Issue key, e.g. PROJ-123"},fields:{type:"object",description:"Object of field names to values",additionalProperties:!0}},required:["issueKey","fields"]}},{name:"jira_transition_issue",description:"Move a Jira issue to a different status. Always pass toStatus when user gave a target; only pass issueKey alone when you explicitly need to list transitions.",input_schema:{type:"object",properties:{issueKey:{type:"string",description:"Issue key, e.g. PROJ-123"},transitionId:{type:"string",description:"Transition ID to perform (optional if toStatus is provided)"},toStatus:{type:"string",description:'Target status/column name (e.g. "\u5DF2\u7ECF\u9A8C\u6536", "Done", "In Progress"). If provided, tool resolves matching transition automatically.'}},required:["issueKey"]}}]};import{existsSync as ls}from"fs";import{fileURLToPath as us}from"url";import{dirname as ds,resolve as ps}from"path";import{resolveIntegrationToken as pt}from"@zibby/core/backend-client.js";function ms(){if(process.env.MCP_SKILL_PATH)return process.env.MCP_SKILL_PATH;let s=ds(us(import.meta.url)),t=ps(s,"..","bin","mcp-skill.mjs");return ls(t)?t:null}async function v(s,t={}){let{token:e}=await pt("github"),r=s.startsWith("https://")?s:`https://api.github.com${s}`,i={Authorization:`Bearer ${e}`,Accept:t.accept||"application/vnd.github.v3+json","User-Agent":"Zibby-App",...t.body?{"Content-Type":"application/json"}:{}},n=await fetch(r,{method:t.method||"GET",headers:i,body:t.body?JSON.stringify(t.body):void 0});if(!n.ok){let o=await n.text().catch(()=>"");throw new Error(`GitHub API ${n.status}: ${o.slice(0,300)}`)}return t.raw?n.text():n.json()}var mt={id:"github",serverName:"github",allowedTools:["mcp__github__*"],requiresIntegration:A.GITHUB,envKeys:["GITHUB_TOKEN"],description:"GitHub \u2014 issues, PRs, commits, code search, file reading",promptFragment:`## GitHub (connected)
75
75
  You have access to the user's GitHub repositories. Available tools:
76
76
 
77
77
  ### Discovery
78
- - github_list_repos: Lists ALL accessible repos (personal + orgs, private + public, up to 200 repos)
78
+ - github_list_repos: Lists ALL accessible repos (personal + orgs, private + public, up to 200 repos). Use to find a RELATED repo worth cloning for cross-repo context.
79
79
  - github_search_repos: Search for a specific repo by name (e.g., "electron", "my-app")
80
80
  - github_get_user: Get authenticated user's profile
81
81
  - github_list_orgs: List organizations with accessible repos
@@ -93,6 +93,9 @@ You have access to the user's GitHub repositories. Available tools:
93
93
  - github_get_pr_diff: Get PR diff
94
94
  - github_list_pr_files: List PR changed files
95
95
  - github_list_pr_comments: Get PR comments
96
+ - github_get_review_thread: Read a review-comment THREAD (root + replies) with its diff context, given any comment id in it
97
+ - github_reply_review_thread: Reply IN-THREAD to an existing review-comment thread (conversational reply, not a new review)
98
+ - github_reply_issue_comment: Reply on the PR's top-level conversation (for non-inline/summary comments)
96
99
  - github_create_review: Post a review on a PR \u2014 a summary body plus optional inline comments on specific file/line positions, with an event (COMMENT, APPROVE, or REQUEST_CHANGES)
97
100
  - github_create_issue: Create new issue
98
101
  - github_list_issues: List issues in a repo (filter by state/labels/since cursor) \u2014 excludes PRs
@@ -109,16 +112,21 @@ When user says "check out repo-name" or "clone repo-name":
109
112
  3. STOP. Do not offer to inspect files or ask what to do next.
110
113
 
111
114
  When user just wants to "look at" or "read" files (not clone):
112
- - Use github_get_file to read individual files via API`,resolve(){let s=os();if(!s)return{command:null,args:[],env:{},description:this.description};let t={};for(let e of this.envKeys)process.env[e]&&(t[e]=process.env[e]);return{type:"stdio",command:"node",args:[s,"../dist/github.js","githubSkill"],env:t,description:this.description,alwaysLoad:!0}},async handleToolCall(s,t){try{switch(s){case"github_search_issues":{let e=t.query;if(!e)return JSON.stringify({error:"query is required"});let r=await S(`/search/issues?q=${encodeURIComponent(e)}&per_page=${t.limit||20}`),i=(r.items||[]).map(n=>({number:n.number,title:n.title,state:n.state,repo:n.repository_url?.split("/").slice(-2).join("/"),url:n.html_url,user:n.user?.login,isPR:!!n.pull_request,labels:(n.labels||[]).map(o=>o.name),createdAt:n.created_at}));return JSON.stringify({total:r.total_count,items:i})}case"github_search_code":{let e=t.query;if(!e)return JSON.stringify({error:"query is required"});let r=t.repo?`+repo:${t.repo}`:"",i=t.language?`+language:${t.language}`:"",n=await S(`/search/code?q=${encodeURIComponent(e)}${r}${i}&per_page=${t.limit||15}`),o=(n.items||[]).map(c=>({name:c.name,path:c.path,repo:c.repository?.full_name,url:c.html_url,score:c.score}));return JSON.stringify({total:n.total_count,items:o})}case"github_get_pr":{let{owner:e,repo:r,number:i}=t;if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and number are required"});let n=await S(`/repos/${e}/${r}/pulls/${i}`);return JSON.stringify({number:n.number,title:n.title,state:n.state,merged:n.merged,body:n.body?.slice(0,5e3),user:n.user?.login,branch:n.head?.ref,headSha:n.head?.sha,base:n.base?.ref,changedFiles:n.changed_files,additions:n.additions,deletions:n.deletions,createdAt:n.created_at,mergedAt:n.merged_at,url:n.html_url,labels:(n.labels||[]).map(o=>o.name)})}case"github_get_pr_diff":{let{owner:e,repo:r,number:i}=t;if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and number are required"});let n=await S(`/repos/${e}/${r}/pulls/${i}`,{accept:"application/vnd.github.v3.diff",raw:!0}),o=n.length>15e3;return JSON.stringify({number:i,diff:o?n.slice(0,15e3):n,truncated:o,totalLength:n.length})}case"github_list_pr_files":{let{owner:e,repo:r,number:i}=t;if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and number are required"});let n=await S(`/repos/${e}/${r}/pulls/${i}/files?per_page=100`);return JSON.stringify({total:n.length,files:n.map(o=>({filename:o.filename,status:o.status,additions:o.additions,deletions:o.deletions,patch:o.patch?.slice(0,3e3)}))})}case"github_list_pr_comments":{let{owner:e,repo:r,number:i}=t;if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and number are required"});let n=await S(`/repos/${e}/${r}/pulls/${i}/comments?per_page=50`),o=await S(`/repos/${e}/${r}/issues/${i}/comments?per_page=50`),c=[...n.map(a=>({type:"review",user:a.user?.login,body:a.body?.slice(0,1e3),path:a.path,line:a.line,createdAt:a.created_at})),...o.map(a=>({type:"issue",user:a.user?.login,body:a.body?.slice(0,1e3),createdAt:a.created_at}))].sort((a,u)=>new Date(a.createdAt)-new Date(u.createdAt));return JSON.stringify({total:c.length,comments:c})}case"github_create_review":{let{owner:e,repo:r,number:i,body:n,event:o,comments:c}=t||{};if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and number are required"});let a=(o||"COMMENT").toUpperCase();if(!["COMMENT","APPROVE","REQUEST_CHANGES"].includes(a))return JSON.stringify({error:`event must be COMMENT, APPROVE, or REQUEST_CHANGES (got ${o})`});let u=Array.isArray(c)?c.filter(d=>d&&d.path&&d.body&&(d.line!=null||d.position!=null)).map(d=>{let m={path:d.path,body:String(d.body)};return d.line!=null?(m.line=Number(d.line),m.side=d.side==="LEFT"?"LEFT":"RIGHT"):m.position=Number(d.position),m}):[];if(a!=="APPROVE"&&!n&&u.length===0)return JSON.stringify({error:"a COMMENT or REQUEST_CHANGES review needs a body and/or inline comments"});let l={event:a};n&&(l.body=String(n)),u.length>0&&(l.comments=u);let p=await S(`/repos/${e}/${r}/pulls/${i}/reviews`,{method:"POST",body:l});return JSON.stringify({ok:!0,id:p.id,state:p.state,event:a,commentsPosted:u.length,url:p.html_url})}case"github_list_commits":{let{owner:e,repo:r,branch:i,path:n,limit:o}=t;if(!e||!r)return JSON.stringify({error:"owner and repo are required"});let c=`/repos/${e}/${r}/commits?per_page=${o||20}`;i&&(c+=`&sha=${encodeURIComponent(i)}`),n&&(c+=`&path=${encodeURIComponent(n)}`);let a=await S(c);return JSON.stringify({total:a.length,commits:a.map(u=>({sha:u.sha?.slice(0,8),fullSha:u.sha,message:u.commit?.message?.slice(0,300),author:u.commit?.author?.name,date:u.commit?.author?.date,url:u.html_url}))})}case"github_get_commit":{let{owner:e,repo:r,sha:i}=t;if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and sha are required"});let n=await S(`/repos/${e}/${r}/commits/${i}`);return JSON.stringify({sha:n.sha?.slice(0,8),message:n.commit?.message,author:n.commit?.author?.name,date:n.commit?.author?.date,stats:n.stats,files:(n.files||[]).map(o=>({filename:o.filename,status:o.status,additions:o.additions,deletions:o.deletions,patch:o.patch?.slice(0,3e3)}))})}case"github_get_file":{let{owner:e,repo:r,path:i,ref:n}=t;if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and path are required"});let o=`/repos/${e}/${r}/contents/${encodeURIComponent(i)}`;n&&(o+=`?ref=${encodeURIComponent(n)}`);let c=await S(o);if(c.type!=="file")return Array.isArray(c)?JSON.stringify({type:"directory",path:i,entries:c.map(l=>({name:l.name,type:l.type,size:l.size,path:l.path}))}):JSON.stringify({error:`Not a file: ${c.type}`});let a=Buffer.from(c.content||"","base64").toString("utf-8"),u=a.length>2e4;return JSON.stringify({path:c.path,size:c.size,sha:c.sha?.slice(0,8),content:u?a.slice(0,2e4):a,truncated:u})}case"github_get_user":try{let e=await S("/installation/repositories?per_page=1");if(e.repositories&&e.repositories.length>0){let r=e.repositories[0],i=r.owner.login,n=r.owner.type,o=n==="Organization"?`/orgs/${i}`:`/users/${i}`,c=await S(o);return JSON.stringify({login:c.login,name:c.name||c.login,avatar:c.avatar_url,bio:c.bio||c.description,type:n,isOrg:n==="Organization",publicRepos:c.public_repos,message:"Showing GitHub App installation owner (GitHub Apps cannot access /user endpoint)"})}return JSON.stringify({error:"No repositories accessible to this GitHub App installation"})}catch(e){return JSON.stringify({error:`GitHub App cannot access /user endpoint. Use github_list_repos instead. (${e.message})`})}case"github_list_orgs":try{let r=(await S("/installation/repositories?per_page=100")).repositories||[],i=new Map;for(let o of r)o.owner.type==="Organization"&&(i.has(o.owner.login)||i.set(o.owner.login,{login:o.owner.login,description:null,url:o.owner.url}));let n=Array.from(i.values());return JSON.stringify({count:n.length,orgs:n,message:"Extracted from accessible repositories (GitHub Apps cannot access /user/orgs directly)"})}catch(e){return JSON.stringify({error:`GitHub App cannot list orgs via /user/orgs. Error: ${e.message}`})}case"github_clone":{let f=function(g){let b=g.replace(/^~(?=$|\/|\\)/,m);return c(b)},{owner:e,repo:r,destination:i}=t;if(!e||!r)return JSON.stringify({error:"owner and repo are required"});let{execSync:n}=await import("child_process"),{join:o,resolve:c}=await import("path"),{existsSync:a,mkdirSync:u}=await import("fs"),{homedir:l,platform:p}=await import("os"),{token:d}=await ct("github"),m=l(),y=i?f(i):o(m,"zibby-repos"),_=o(y,r);if(u(y,{recursive:!0}),a(_))return JSON.stringify({error:`Directory ${_} already exists. Remove it first or use a different destination.`,existingPath:_});try{let g=`https://x-access-token:${d}@github.com/${e}/${r}.git`;n(`git clone ${g} "${_}"`,{stdio:"pipe"});let b=p()==="win32",h;return b?h=n(`dir "${_}"`,{encoding:"utf-8",shell:"cmd.exe"}):h=n(`ls -la "${_}"`,{encoding:"utf-8"}),JSON.stringify({success:!0,path:_,message:`Cloned ${e}/${r} to ${_}`,contents:h.split(`
115
+ - Use github_get_file to read individual files via API`,resolve(){let s=ms();if(!s)return{command:null,args:[],env:{},description:this.description};let t={};for(let e of this.envKeys)process.env[e]&&(t[e]=process.env[e]);return{type:"stdio",command:"node",args:[s,"../dist/github.js","githubSkill"],env:t,description:this.description,alwaysLoad:!0}},async handleToolCall(s,t){try{switch(s){case"github_search_issues":{let e=t.query;if(!e)return JSON.stringify({error:"query is required"});let r=await v(`/search/issues?q=${encodeURIComponent(e)}&per_page=${t.limit||20}`),i=(r.items||[]).map(n=>({number:n.number,title:n.title,state:n.state,repo:n.repository_url?.split("/").slice(-2).join("/"),url:n.html_url,user:n.user?.login,isPR:!!n.pull_request,labels:(n.labels||[]).map(o=>o.name),createdAt:n.created_at}));return JSON.stringify({total:r.total_count,items:i})}case"github_search_code":{let e=t.query;if(!e)return JSON.stringify({error:"query is required"});let r=t.repo?`+repo:${t.repo}`:"",i=t.language?`+language:${t.language}`:"",n=await v(`/search/code?q=${encodeURIComponent(e)}${r}${i}&per_page=${t.limit||15}`),o=(n.items||[]).map(c=>({name:c.name,path:c.path,repo:c.repository?.full_name,url:c.html_url,score:c.score}));return JSON.stringify({total:n.total_count,items:o})}case"github_get_pr":{let{owner:e,repo:r,number:i}=t;if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and number are required"});let n=await v(`/repos/${e}/${r}/pulls/${i}`);return JSON.stringify({number:n.number,title:n.title,state:n.state,merged:n.merged,body:n.body?.slice(0,5e3),user:n.user?.login,branch:n.head?.ref,headSha:n.head?.sha,base:n.base?.ref,changedFiles:n.changed_files,additions:n.additions,deletions:n.deletions,createdAt:n.created_at,mergedAt:n.merged_at,url:n.html_url,labels:(n.labels||[]).map(o=>o.name)})}case"github_get_pr_diff":{let{owner:e,repo:r,number:i}=t;if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and number are required"});let n=await v(`/repos/${e}/${r}/pulls/${i}`,{accept:"application/vnd.github.v3.diff",raw:!0}),o=n.length>15e3;return JSON.stringify({number:i,diff:o?n.slice(0,15e3):n,truncated:o,totalLength:n.length})}case"github_list_pr_files":{let{owner:e,repo:r,number:i}=t;if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and number are required"});let n=await v(`/repos/${e}/${r}/pulls/${i}/files?per_page=100`);return JSON.stringify({total:n.length,files:n.map(o=>({filename:o.filename,status:o.status,additions:o.additions,deletions:o.deletions,patch:o.patch?.slice(0,3e3)}))})}case"github_list_pr_comments":{let{owner:e,repo:r,number:i}=t;if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and number are required"});let n=await v(`/repos/${e}/${r}/pulls/${i}/comments?per_page=50`),o=await v(`/repos/${e}/${r}/issues/${i}/comments?per_page=50`),c=[...n.map(a=>({type:"review",user:a.user?.login,body:a.body?.slice(0,1e3),path:a.path,line:a.line,createdAt:a.created_at})),...o.map(a=>({type:"issue",user:a.user?.login,body:a.body?.slice(0,1e3),createdAt:a.created_at}))].sort((a,u)=>new Date(a.createdAt)-new Date(u.createdAt));return JSON.stringify({total:c.length,comments:c})}case"github_get_review_thread":{let{owner:e,repo:r,number:i,commentId:n}=t||{};if(!e||!r||!i||!n)return JSON.stringify({error:"owner, repo, number, and commentId are required"});let o=await v(`/repos/${e}/${r}/pulls/comments/${n}`),c=o.in_reply_to_id||o.id,a=[];try{a=await v(`/repos/${e}/${r}/pulls/${i}/comments?per_page=100`)}catch{a=[o]}(!Array.isArray(a)||a.length===0)&&(a=[o]);let u=a.filter(p=>p.id===c||p.in_reply_to_id===c).sort((p,m)=>new Date(p.created_at)-new Date(m.created_at)),l=u.length?u:[o],d=l.find(p=>p.id===c)||l[0];return JSON.stringify({rootCommentId:c,path:d.path,line:d.line??d.original_line??null,side:d.side||"RIGHT",diffHunk:typeof d.diff_hunk=="string"?d.diff_hunk.slice(0,3e3):null,commitId:d.commit_id||d.original_commit_id||null,notes:l.map(p=>({id:p.id,user:p.user?.login,body:(p.body||"").slice(0,4e3),createdAt:p.created_at,isRoot:p.id===c,url:p.html_url}))})}case"github_reply_review_thread":{let{owner:e,repo:r,number:i,commentId:n,body:o}=t||{};if(!e||!r||!i||!n||!o)return JSON.stringify({error:"owner, repo, number, commentId, and body are required"});let c=await v(`/repos/${e}/${r}/pulls/${i}/comments/${n}/replies`,{method:"POST",body:{body:String(o)}});return JSON.stringify({ok:!0,id:c.id,url:c.html_url,inReplyTo:c.in_reply_to_id})}case"github_reply_issue_comment":{let{owner:e,repo:r,number:i,body:n}=t||{};if(!e||!r||!i||!n)return JSON.stringify({error:"owner, repo, number, and body are required"});let o=await v(`/repos/${e}/${r}/issues/${i}/comments`,{method:"POST",body:{body:String(n)}});return JSON.stringify({ok:!0,id:o.id,url:o.html_url})}case"github_create_review":{let{owner:e,repo:r,number:i,body:n,event:o,comments:c}=t||{};if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and number are required"});let a=(o||"COMMENT").toUpperCase();if(!["COMMENT","APPROVE","REQUEST_CHANGES"].includes(a))return JSON.stringify({error:`event must be COMMENT, APPROVE, or REQUEST_CHANGES (got ${o})`});let u=Array.isArray(c)?c.filter(p=>p&&p.path&&p.body&&(p.line!=null||p.position!=null)).map(p=>{let m={path:p.path,body:String(p.body)};return p.line!=null?(m.line=Number(p.line),m.side=p.side==="LEFT"?"LEFT":"RIGHT"):m.position=Number(p.position),m}):[];if(a!=="APPROVE"&&!n&&u.length===0)return JSON.stringify({error:"a COMMENT or REQUEST_CHANGES review needs a body and/or inline comments"});let l={event:a};n&&(l.body=String(n)),u.length>0&&(l.comments=u);let d=await v(`/repos/${e}/${r}/pulls/${i}/reviews`,{method:"POST",body:l});return JSON.stringify({ok:!0,id:d.id,state:d.state,event:a,commentsPosted:u.length,url:d.html_url})}case"github_list_commits":{let{owner:e,repo:r,branch:i,path:n,limit:o}=t;if(!e||!r)return JSON.stringify({error:"owner and repo are required"});let c=`/repos/${e}/${r}/commits?per_page=${o||20}`;i&&(c+=`&sha=${encodeURIComponent(i)}`),n&&(c+=`&path=${encodeURIComponent(n)}`);let a=await v(c);return JSON.stringify({total:a.length,commits:a.map(u=>({sha:u.sha?.slice(0,8),fullSha:u.sha,message:u.commit?.message?.slice(0,300),author:u.commit?.author?.name,date:u.commit?.author?.date,url:u.html_url}))})}case"github_get_commit":{let{owner:e,repo:r,sha:i}=t;if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and sha are required"});let n=await v(`/repos/${e}/${r}/commits/${i}`);return JSON.stringify({sha:n.sha?.slice(0,8),message:n.commit?.message,author:n.commit?.author?.name,date:n.commit?.author?.date,stats:n.stats,files:(n.files||[]).map(o=>({filename:o.filename,status:o.status,additions:o.additions,deletions:o.deletions,patch:o.patch?.slice(0,3e3)}))})}case"github_get_file":{let{owner:e,repo:r,path:i,ref:n}=t;if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and path are required"});let o=`/repos/${e}/${r}/contents/${encodeURIComponent(i)}`;n&&(o+=`?ref=${encodeURIComponent(n)}`);let c=await v(o);if(c.type!=="file")return Array.isArray(c)?JSON.stringify({type:"directory",path:i,entries:c.map(l=>({name:l.name,type:l.type,size:l.size,path:l.path}))}):JSON.stringify({error:`Not a file: ${c.type}`});let a=Buffer.from(c.content||"","base64").toString("utf-8"),u=a.length>2e4;return JSON.stringify({path:c.path,size:c.size,sha:c.sha?.slice(0,8),content:u?a.slice(0,2e4):a,truncated:u})}case"github_get_user":try{let e=await v("/installation/repositories?per_page=1");if(e.repositories&&e.repositories.length>0){let r=e.repositories[0],i=r.owner.login,n=r.owner.type,o=n==="Organization"?`/orgs/${i}`:`/users/${i}`,c=await v(o);return JSON.stringify({login:c.login,name:c.name||c.login,avatar:c.avatar_url,bio:c.bio||c.description,type:n,isOrg:n==="Organization",publicRepos:c.public_repos,message:"Showing GitHub App installation owner (GitHub Apps cannot access /user endpoint)"})}return JSON.stringify({error:"No repositories accessible to this GitHub App installation"})}catch(e){return JSON.stringify({error:`GitHub App cannot access /user endpoint. Use github_list_repos instead. (${e.message})`})}case"github_list_orgs":try{let r=(await v("/installation/repositories?per_page=100")).repositories||[],i=new Map;for(let o of r)o.owner.type==="Organization"&&(i.has(o.owner.login)||i.set(o.owner.login,{login:o.owner.login,description:null,url:o.owner.url}));let n=Array.from(i.values());return JSON.stringify({count:n.length,orgs:n,message:"Extracted from accessible repositories (GitHub Apps cannot access /user/orgs directly)"})}catch(e){return JSON.stringify({error:`GitHub App cannot list orgs via /user/orgs. Error: ${e.message}`})}case"github_clone":{let f=function(g){let b=g.replace(/^~(?=$|\/|\\)/,m);return c(b)},{owner:e,repo:r,destination:i}=t;if(!e||!r)return JSON.stringify({error:"owner and repo are required"});let{execSync:n}=await import("child_process"),{join:o,resolve:c}=await import("path"),{existsSync:a,mkdirSync:u}=await import("fs"),{homedir:l,platform:d}=await import("os"),{token:p}=await pt("github"),m=l(),y=i?f(i):o(m,"zibby-repos"),_=o(y,r);if(u(y,{recursive:!0}),a(_))return JSON.stringify({error:`Directory ${_} already exists. Remove it first or use a different destination.`,existingPath:_});try{let g=`https://x-access-token:${p}@github.com/${e}/${r}.git`;n(`git clone ${g} "${_}"`,{stdio:"pipe"});let b=d()==="win32",h;return b?h=n(`dir "${_}"`,{encoding:"utf-8",shell:"cmd.exe"}):h=n(`ls -la "${_}"`,{encoding:"utf-8"}),JSON.stringify({success:!0,path:_,message:`Cloned ${e}/${r} to ${_}`,contents:h.split(`
113
116
  `).slice(0,30).join(`
114
- `),instructions:"IMPORTANT: Show the contents field to the user - it contains the directory listing."})}catch(g){return JSON.stringify({error:`Clone failed: ${g.message}`})}}case"github_search_repos":{let{query:e,limit:r}=t;if(!e)return JSON.stringify({error:"query is required"});let i=await this.handleToolCall("github_list_repos",{limit:200},{}),n=JSON.parse(i);if(n.error)return JSON.stringify(n);let o=e.toLowerCase(),c=n.repos.filter(a=>a.name.toLowerCase().includes(o)||a.fullName.toLowerCase().includes(o)||a.description&&a.description.toLowerCase().includes(o));return JSON.stringify({query:e,count:c.length,repos:c.slice(0,r||20)})}case"github_list_repos":{let{owner:e,type:r,sort:i,direction:n,limit:o}=t,c=100,a=o||200,u=[];if(!e){let f=1,y=!0;for(;y&&u.length<a;){let h=`/installation/repositories?per_page=${c}&page=${f}`,T=(await S(h)).repositories||[];if(T.length===0)break;u=u.concat(T),y=T.length===c,f++}let _=u.slice(0,a).map(h=>({name:h.name,fullName:h.full_name,private:h.private,description:h.description,language:h.language,defaultBranch:h.default_branch,updatedAt:h.updated_at,stars:h.stargazers_count,url:h.html_url})),g=_.filter(h=>h.private).length,b=_.filter(h=>!h.private).length;return JSON.stringify({count:_.length,repos:_,privateCount:g,publicCount:b,message:`Found ${g} private and ${b} public repos`})}let l=await S(`/orgs/${e}`).then(()=>!0).catch(()=>!1),p=1,d=!0;for(;d&&u.length<a;){let f;l?f=`/orgs/${e}/repos?per_page=${c}&page=${p}&type=${r||"all"}&sort=${i||"updated"}&direction=${n||"desc"}`:f=`/users/${e}/repos?per_page=${c}&page=${p}&type=${r||"all"}&sort=${i||"updated"}&direction=${n||"desc"}`;let y=await S(f),_=Array.isArray(y)?y:[];if(_.length===0)break;u=u.concat(_),d=_.length===c,p++}let m=u.slice(0,a).map(f=>({name:f.name,fullName:f.full_name,private:f.private,description:f.description,language:f.language,defaultBranch:f.default_branch,updatedAt:f.updated_at,stars:f.stargazers_count,url:f.html_url}));return JSON.stringify({count:m.length,repos:m})}case"github_create_issue":{let{owner:e,repo:r,title:i,body:n}=t;if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and title are required"});let o=await S(`/repos/${e}/${r}/issues`,{method:"POST",body:{title:i,body:n||""}});return JSON.stringify({number:o.number,url:o.html_url,title:o.title})}case"github_list_issues":{let{owner:e,repo:r,state:i,labels:n,since:o,assignee:c,sort:a,direction:u,limit:l}=t||{};if(!e||!r)return JSON.stringify({error:"owner and repo are required"});let p=new URLSearchParams;p.set("state",i||"open"),p.set("per_page",String(l||30)),p.set("sort",a||"updated"),p.set("direction",u||"desc"),n&&p.set("labels",Array.isArray(n)?n.join(","):n),o&&p.set("since",o),c&&p.set("assignee",c);let d=await S(`/repos/${e}/${r}/issues?${p.toString()}`),m=(Array.isArray(d)?d:[]).filter(f=>!f.pull_request).map(f=>({number:f.number,title:f.title,state:f.state,labels:(f.labels||[]).map(y=>typeof y=="string"?y:y.name),assignee:f.assignee?.login||null,assignees:(f.assignees||[]).map(y=>y.login),user:f.user?.login,comments:f.comments,url:f.html_url,createdAt:f.created_at,updatedAt:f.updated_at}));return JSON.stringify({count:m.length,issues:m})}case"github_get_issue":{let{owner:e,repo:r,number:i}=t||{};if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and number are required"});let n=await S(`/repos/${e}/${r}/issues/${i}`);return n.pull_request?JSON.stringify({error:`#${i} is a pull request, not an issue`,isPR:!0}):JSON.stringify({number:n.number,title:n.title,body:n.body||"",state:n.state,stateReason:n.state_reason||null,labels:(n.labels||[]).map(o=>typeof o=="string"?o:o.name),assignee:n.assignee?.login||null,assignees:(n.assignees||[]).map(o=>o.login),user:n.user?.login,milestone:n.milestone?.title||null,comments:n.comments,url:n.html_url,createdAt:n.created_at,updatedAt:n.updated_at,closedAt:n.closed_at})}case"github_get_issue_comments":{let{owner:e,repo:r,number:i,limit:n}=t||{};if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and number are required"});let o=await S(`/repos/${e}/${r}/issues/${i}/comments?per_page=${n||100}`),c=(Array.isArray(o)?o:[]).map(a=>({id:a.id,user:a.user?.login,body:a.body||"",createdAt:a.created_at,updatedAt:a.updated_at,url:a.html_url}));return JSON.stringify({count:c.length,comments:c})}case"github_add_issue_comment":{let{owner:e,repo:r,number:i,body:n}=t||{};if(!e||!r||!i||!n)return JSON.stringify({error:"owner, repo, number, and body are required"});let o=await S(`/repos/${e}/${r}/issues/${i}/comments`,{method:"POST",body:{body:n}});return JSON.stringify({ok:!0,id:o.id,url:o.html_url})}case"github_close_issue":{let{owner:e,repo:r,number:i,stateReason:n}=t||{};if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and number are required"});let o={state:"closed"};n&&(o.state_reason=n);let c=await S(`/repos/${e}/${r}/issues/${i}`,{method:"PATCH",body:o});return JSON.stringify({ok:!0,number:c.number,state:c.state,stateReason:c.state_reason||null,url:c.html_url})}case"github_reopen_issue":{let{owner:e,repo:r,number:i}=t||{};if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and number are required"});let n=await S(`/repos/${e}/${r}/issues/${i}`,{method:"PATCH",body:{state:"open"}});return JSON.stringify({ok:!0,number:n.number,state:n.state,url:n.html_url})}case"github_label_issue":{let{owner:e,repo:r,number:i,labels:n,mode:o}=t||{};if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and number are required"});let c=Array.isArray(n)?n:n?[n]:[];if(!c.length)return JSON.stringify({error:"labels (string or array) is required"});let a=o||"add";if(a==="set"){let l=await S(`/repos/${e}/${r}/issues/${i}`,{method:"PATCH",body:{labels:c}});return JSON.stringify({ok:!0,number:l.number,labels:(l.labels||[]).map(p=>typeof p=="string"?p:p.name)})}if(a==="remove"){for(let p of c)await S(`/repos/${e}/${r}/issues/${i}/labels/${encodeURIComponent(p)}`,{method:"DELETE"});let l=await S(`/repos/${e}/${r}/issues/${i}`);return JSON.stringify({ok:!0,number:l.number,labels:(l.labels||[]).map(p=>typeof p=="string"?p:p.name)})}let u=await S(`/repos/${e}/${r}/issues/${i}/labels`,{method:"POST",body:{labels:c}});return JSON.stringify({ok:!0,number:i,labels:(Array.isArray(u)?u:[]).map(l=>typeof l=="string"?l:l.name)})}default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(e){return JSON.stringify({error:e.message})}},tools:[{name:"github_get_user",description:"Get the authenticated GitHub user profile and their organizations",input_schema:{type:"object",properties:{}}},{name:"github_list_orgs",description:"List GitHub organizations the authenticated user belongs to",input_schema:{type:"object",properties:{}}},{name:"github_list_repos",description:"List repositories for a user or org. If no owner given, lists the authenticated user's repos.",input_schema:{type:"object",properties:{owner:{type:"string",description:"Org or user login. Omit to list your own repos."},type:{type:"string",enum:["all","public","private","forks","sources","member"],description:"Filter by type (default: all)"},sort:{type:"string",enum:["created","updated","pushed","full_name"],description:"Sort field (default: updated)"},direction:{type:"string",enum:["asc","desc"],description:"Sort direction (default: desc)"},limit:{type:"number",description:"Max repos to return (default: 30)"}}}},{name:"github_clone",description:'Clone a GitHub repository to the local filesystem. Use when user says "check out" or "clone" a repo.',input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner (user or org name)"},repo:{type:"string",description:"Repository name"},destination:{type:"string",description:"Destination directory. Accepts absolute paths, ~-prefixed paths, or relative names. Defaults to ~/zibby-repos/<repo>."}},required:["owner","repo"]}},{name:"github_search_repos",description:"Search accessible repositories by name or description. Use this when the user asks to find a specific repo.",input_schema:{type:"object",properties:{query:{type:"string",description:'Search term to match against repo name or description (e.g., "electron", "my-app")'},limit:{type:"number",description:"Max results (default: 20)"}},required:["query"]}},{name:"github_search_issues",description:"Search GitHub issues and pull requests",input_schema:{type:"object",properties:{query:{type:"string",description:'GitHub search query (e.g. "SCRUM-123", "login bug repo:org/app")'},limit:{type:"number",description:"Max results (default: 20)"}},required:["query"]}},{name:"github_search_code",description:"Search code across GitHub repositories by keyword",input_schema:{type:"object",properties:{query:{type:"string",description:'Code search query (e.g. "handleLogin", "class AuthService")'},repo:{type:"string",description:'Scope to a specific repo (e.g. "org/app"). Optional.'},language:{type:"string",description:'Filter by language (e.g. "javascript", "python"). Optional.'},limit:{type:"number",description:"Max results (default: 15)"}},required:["query"]}},{name:"github_get_pr",description:"Get details of a pull request \u2014 title, description, branch, stats",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"PR number"}},required:["owner","repo","number"]}},{name:"github_get_pr_diff",description:"Get the unified diff of a pull request \u2014 the actual code changes",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"PR number"}},required:["owner","repo","number"]}},{name:"github_list_pr_files",description:"List files changed in a PR with per-file patches",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"PR number"}},required:["owner","repo","number"]}},{name:"github_list_pr_comments",description:"Get all review and issue comments on a PR",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"PR number"}},required:["owner","repo","number"]}},{name:"github_create_review",description:"Post a review on a pull request: a summary body plus optional inline comments anchored to file/line, with an event (COMMENT, APPROVE, or REQUEST_CHANGES). Use this to deliver a code review back to the PR.",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"PR number"},body:{type:"string",description:"The review summary (markdown). Shown as the top-level review comment."},event:{type:"string",enum:["COMMENT","APPROVE","REQUEST_CHANGES"],description:"Review verdict. Default COMMENT (no approval state). Use REQUEST_CHANGES for blocking issues."},comments:{type:"array",description:"Optional inline comments, each anchored to a changed line.",items:{type:"object",properties:{path:{type:"string",description:"File path as it appears in the diff"},line:{type:"number",description:"Line number in the file's NEW version (the right side of the diff)"},side:{type:"string",enum:["LEFT","RIGHT"],description:"RIGHT (new) or LEFT (old). Default RIGHT."},body:{type:"string",description:"The inline comment text (markdown)"}},required:["path","line","body"]}}},required:["owner","repo","number"]}},{name:"github_list_commits",description:"List recent commits on a branch, optionally filtered by file path",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},branch:{type:"string",description:"Branch name (default: repo default branch)"},path:{type:"string",description:"Filter commits touching this file path"},limit:{type:"number",description:"Max commits (default: 20)"}},required:["owner","repo"]}},{name:"github_get_commit",description:"Get details of a specific commit \u2014 message, stats, file diffs",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},sha:{type:"string",description:"Commit SHA (full or short)"}},required:["owner","repo","sha"]}},{name:"github_get_file",description:"Read a file (or list a directory) from a GitHub repo. Works on any branch/ref.",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},path:{type:"string",description:'File or directory path (e.g. "src/auth/login.ts")'},ref:{type:"string",description:"Branch, tag, or commit SHA (default: repo default branch)"}},required:["owner","repo","path"]}},{name:"github_create_issue",description:"Create a GitHub issue",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},title:{type:"string",description:"Issue title"},body:{type:"string",description:"Issue body (markdown)"}},required:["owner","repo","title"]}},{name:"github_list_issues",description:"List issues in a repo (excludes pull requests). Filter by state, labels, and an updated-since cursor for polling.",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},state:{type:"string",enum:["open","closed","all"],description:"Filter by state (default: open)"},labels:{type:"array",items:{type:"string"},description:"Only issues carrying ALL of these labels"},since:{type:"string",description:"ISO-8601 timestamp; only issues updated at/after this (polling cursor)"},assignee:{type:"string",description:'Filter by assignee login, "none", or "*"'},sort:{type:"string",enum:["created","updated","comments"],description:"Sort field (default: updated)"},direction:{type:"string",enum:["asc","desc"],description:"Sort direction (default: desc)"},limit:{type:"number",description:"Max issues (default: 30, max 100 per page)"}},required:["owner","repo"]}},{name:"github_get_issue",description:"Get a single GitHub issue with full detail (title, body, state, labels, assignee, url)",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"Issue number"}},required:["owner","repo","number"]}},{name:"github_get_issue_comments",description:"Get the comment thread on a GitHub issue (chronological)",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"Issue number"},limit:{type:"number",description:"Max comments (default: 100)"}},required:["owner","repo","number"]}},{name:"github_add_issue_comment",description:"Add a comment to a GitHub issue. Also the way to record a PR link on an issue (post a markdown link).",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"Issue number"},body:{type:"string",description:"Comment body (markdown)"}},required:["owner","repo","number","body"]}},{name:"github_close_issue",description:"Close a GitHub issue. Optionally set the close reason (completed or not_planned).",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"Issue number"},stateReason:{type:"string",enum:["completed","not_planned"],description:"Why the issue was closed (optional)"}},required:["owner","repo","number"]}},{name:"github_reopen_issue",description:"Reopen a closed GitHub issue",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"Issue number"}},required:["owner","repo","number"]}},{name:"github_label_issue",description:"Add, set (replace all), or remove labels on a GitHub issue. Labels back state-like transitions on GitHub.",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"Issue number"},labels:{type:"array",items:{type:"string"},description:"Label name(s)"},mode:{type:"string",enum:["add","set","remove"],description:"add appends, set replaces all, remove deletes (default: add)"}},required:["owner","repo","number","labels"]}}]};import{existsSync as as}from"fs";import{fileURLToPath as cs}from"url";import{dirname as ls,resolve as us}from"path";function ps(){if(process.env.MCP_SKILL_PATH)return process.env.MCP_SKILL_PATH;let s=ls(cs(import.meta.url)),t=us(s,"..","bin","mcp-skill.mjs");return as(t)?t:null}function ds(){let s=process.env.GITLAB_API_URL;if(s)return s.replace(/\/+$/,"");let t=(process.env.GITLAB_URL||process.env.GITLAB_INSTANCE_URL||"https://gitlab.com").trim().replace(/\/+$/,"");return/\/api\/v\d+$/.test(t)?t:`${t}/api/v4`}function ms(){if(process.env.GITLAB_OAUTH_TOKEN)return{Authorization:`Bearer ${process.env.GITLAB_OAUTH_TOKEN}`};let s=process.env.GITLAB_TOKEN;if(!s)throw new Error("GitLab is not connected: set GITLAB_TOKEN (personal/project access token, api scope) or GITLAB_OAUTH_TOKEN.");return{"PRIVATE-TOKEN":s}}async function L(s,t={}){let e=/^https?:\/\//.test(s)?s:`${ds()}${s}`,r={Accept:"application/json","User-Agent":"Zibby-App",...ms(),...t.body?{"Content-Type":"application/json"}:{}},i=await fetch(e,{method:t.method||"GET",headers:r,body:t.body?JSON.stringify(t.body):void 0});if(!i.ok){let n=await i.text().catch(()=>"");throw new Error(`GitLab API ${i.status}: ${n.slice(0,300)}`)}return t.raw?i.text():i.json()}function D(s){let t=String(s);return/^\d+$/.test(t)?t:encodeURIComponent(t)}var ut={id:"gitlab",serverName:"gitlab",allowedTools:["mcp__gitlab__*"],requiresIntegration:R.GITLAB,envKeys:["GITLAB_TOKEN","GITLAB_OAUTH_TOKEN","GITLAB_INSTANCE_URL","GITLAB_API_URL"],description:"GitLab \u2014 merge requests, diffs, MR reviews/discussions, issues",promptFragment:`## GitLab (connected)
117
+ `),instructions:"IMPORTANT: Show the contents field to the user - it contains the directory listing."})}catch(g){return JSON.stringify({error:`Clone failed: ${g.message}`})}}case"github_search_repos":{let{query:e,limit:r}=t;if(!e)return JSON.stringify({error:"query is required"});let i=await this.handleToolCall("github_list_repos",{limit:200},{}),n=JSON.parse(i);if(n.error)return JSON.stringify(n);let o=e.toLowerCase(),c=n.repos.filter(a=>a.name.toLowerCase().includes(o)||a.fullName.toLowerCase().includes(o)||a.description&&a.description.toLowerCase().includes(o));return JSON.stringify({query:e,count:c.length,repos:c.slice(0,r||20)})}case"github_list_repos":{let{owner:e,type:r,sort:i,direction:n,limit:o,query:c}=t,a=100,u=o||200,l=[],d=h=>({name:h.name,fullName:h.full_name,private:h.private,description:h.description,language:h.language,defaultBranch:h.default_branch,updatedAt:h.updated_at,stars:h.stargazers_count,url:h.html_url,fullPath:h.full_name,webUrl:h.html_url,visibility:h.visibility||(h.private?"private":"public")}),p=h=>{if(!c)return!0;let S=String(c).toLowerCase();return h.name&&h.name.toLowerCase().includes(S)||h.fullName&&h.fullName.toLowerCase().includes(S)||h.description&&h.description.toLowerCase().includes(S)};if(!e){let h=1,S=!0;for(;S&&l.length<u;){let q=`/installation/repositories?per_page=${a}&page=${h}`,Le=(await v(q)).repositories||[];if(Le.length===0)break;l=l.concat(Le),S=Le.length===a,h++}let j=l.map(d).filter(p),Y=j.slice(0,u),Ee=j.length>Y.length,R=Y.filter(q=>q.private).length,I=Y.filter(q=>!q.private).length;return JSON.stringify({count:Y.length,repos:Y,truncated:Ee,privateCount:R,publicCount:I,message:`Found ${R} private and ${I} public repos`})}let m=await v(`/orgs/${e}`).then(()=>!0).catch(()=>!1),f=1,y=!0;for(;y&&l.length<u;){let h;m?h=`/orgs/${e}/repos?per_page=${a}&page=${f}&type=${r||"all"}&sort=${i||"updated"}&direction=${n||"desc"}`:h=`/users/${e}/repos?per_page=${a}&page=${f}&type=${r||"all"}&sort=${i||"updated"}&direction=${n||"desc"}`;let S=await v(h),j=Array.isArray(S)?S:[];if(j.length===0)break;l=l.concat(j),y=j.length===a,f++}let _=l.map(d).filter(p),g=_.slice(0,u),b=_.length>g.length;return JSON.stringify({count:g.length,repos:g,truncated:b})}case"github_create_issue":{let{owner:e,repo:r,title:i,body:n}=t;if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and title are required"});let o=await v(`/repos/${e}/${r}/issues`,{method:"POST",body:{title:i,body:n||""}});return JSON.stringify({number:o.number,url:o.html_url,title:o.title})}case"github_list_issues":{let{owner:e,repo:r,state:i,labels:n,since:o,assignee:c,sort:a,direction:u,limit:l}=t||{};if(!e||!r)return JSON.stringify({error:"owner and repo are required"});let d=new URLSearchParams;d.set("state",i||"open"),d.set("per_page",String(l||30)),d.set("sort",a||"updated"),d.set("direction",u||"desc"),n&&d.set("labels",Array.isArray(n)?n.join(","):n),o&&d.set("since",o),c&&d.set("assignee",c);let p=await v(`/repos/${e}/${r}/issues?${d.toString()}`),m=(Array.isArray(p)?p:[]).filter(f=>!f.pull_request).map(f=>({number:f.number,title:f.title,state:f.state,labels:(f.labels||[]).map(y=>typeof y=="string"?y:y.name),assignee:f.assignee?.login||null,assignees:(f.assignees||[]).map(y=>y.login),user:f.user?.login,comments:f.comments,url:f.html_url,createdAt:f.created_at,updatedAt:f.updated_at}));return JSON.stringify({count:m.length,issues:m})}case"github_get_issue":{let{owner:e,repo:r,number:i}=t||{};if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and number are required"});let n=await v(`/repos/${e}/${r}/issues/${i}`);return n.pull_request?JSON.stringify({error:`#${i} is a pull request, not an issue`,isPR:!0}):JSON.stringify({number:n.number,title:n.title,body:n.body||"",state:n.state,stateReason:n.state_reason||null,labels:(n.labels||[]).map(o=>typeof o=="string"?o:o.name),assignee:n.assignee?.login||null,assignees:(n.assignees||[]).map(o=>o.login),user:n.user?.login,milestone:n.milestone?.title||null,comments:n.comments,url:n.html_url,createdAt:n.created_at,updatedAt:n.updated_at,closedAt:n.closed_at})}case"github_get_issue_comments":{let{owner:e,repo:r,number:i,limit:n}=t||{};if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and number are required"});let o=await v(`/repos/${e}/${r}/issues/${i}/comments?per_page=${n||100}`),c=(Array.isArray(o)?o:[]).map(a=>({id:a.id,user:a.user?.login,body:a.body||"",createdAt:a.created_at,updatedAt:a.updated_at,url:a.html_url}));return JSON.stringify({count:c.length,comments:c})}case"github_add_issue_comment":{let{owner:e,repo:r,number:i,body:n}=t||{};if(!e||!r||!i||!n)return JSON.stringify({error:"owner, repo, number, and body are required"});let o=await v(`/repos/${e}/${r}/issues/${i}/comments`,{method:"POST",body:{body:n}});return JSON.stringify({ok:!0,id:o.id,url:o.html_url})}case"github_close_issue":{let{owner:e,repo:r,number:i,stateReason:n}=t||{};if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and number are required"});let o={state:"closed"};n&&(o.state_reason=n);let c=await v(`/repos/${e}/${r}/issues/${i}`,{method:"PATCH",body:o});return JSON.stringify({ok:!0,number:c.number,state:c.state,stateReason:c.state_reason||null,url:c.html_url})}case"github_reopen_issue":{let{owner:e,repo:r,number:i}=t||{};if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and number are required"});let n=await v(`/repos/${e}/${r}/issues/${i}`,{method:"PATCH",body:{state:"open"}});return JSON.stringify({ok:!0,number:n.number,state:n.state,url:n.html_url})}case"github_label_issue":{let{owner:e,repo:r,number:i,labels:n,mode:o}=t||{};if(!e||!r||!i)return JSON.stringify({error:"owner, repo, and number are required"});let c=Array.isArray(n)?n:n?[n]:[];if(!c.length)return JSON.stringify({error:"labels (string or array) is required"});let a=o||"add";if(a==="set"){let l=await v(`/repos/${e}/${r}/issues/${i}`,{method:"PATCH",body:{labels:c}});return JSON.stringify({ok:!0,number:l.number,labels:(l.labels||[]).map(d=>typeof d=="string"?d:d.name)})}if(a==="remove"){for(let d of c)await v(`/repos/${e}/${r}/issues/${i}/labels/${encodeURIComponent(d)}`,{method:"DELETE"});let l=await v(`/repos/${e}/${r}/issues/${i}`);return JSON.stringify({ok:!0,number:l.number,labels:(l.labels||[]).map(d=>typeof d=="string"?d:d.name)})}let u=await v(`/repos/${e}/${r}/issues/${i}/labels`,{method:"POST",body:{labels:c}});return JSON.stringify({ok:!0,number:i,labels:(Array.isArray(u)?u:[]).map(l=>typeof l=="string"?l:l.name)})}default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(e){return JSON.stringify({error:e.message})}},tools:[{name:"github_get_user",description:"Get the authenticated GitHub user profile and their organizations",input_schema:{type:"object",properties:{}}},{name:"github_list_orgs",description:"List GitHub organizations the authenticated user belongs to",input_schema:{type:"object",properties:{}}},{name:"github_list_repos",description:"List the repositories this token/installation can access (omit owner) \u2014 or a specific user/org's repos (pass owner). Use this to discover a RELATED repo worth cloning when a change's correctness depends on another accessible repo. Each repo carries a normalized { fullPath, name, webUrl, defaultBranch, visibility } shape (identical to gitlab_list_projects) alongside legacy fields, plus a truncated flag.",input_schema:{type:"object",properties:{owner:{type:"string",description:"Org or user login. Omit to list every repo your token/installation can access."},query:{type:"string",description:"Optional term matched against repo name/full-name/description"},type:{type:"string",enum:["all","public","private","forks","sources","member"],description:"Filter by type (default: all)"},sort:{type:"string",enum:["created","updated","pushed","full_name"],description:"Sort field (default: updated)"},direction:{type:"string",enum:["asc","desc"],description:"Sort direction (default: desc)"},limit:{type:"number",description:"Max repos to return (default: 200, hard-capped at the fetch ceiling)"}}}},{name:"github_clone",description:'Clone a GitHub repository to the local filesystem. Use when user says "check out" or "clone" a repo.',input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner (user or org name)"},repo:{type:"string",description:"Repository name"},destination:{type:"string",description:"Destination directory. Accepts absolute paths, ~-prefixed paths, or relative names. Defaults to ~/zibby-repos/<repo>."}},required:["owner","repo"]}},{name:"github_search_repos",description:"Search accessible repositories by name or description. Use this when the user asks to find a specific repo.",input_schema:{type:"object",properties:{query:{type:"string",description:'Search term to match against repo name or description (e.g., "electron", "my-app")'},limit:{type:"number",description:"Max results (default: 20)"}},required:["query"]}},{name:"github_search_issues",description:"Search GitHub issues and pull requests",input_schema:{type:"object",properties:{query:{type:"string",description:'GitHub search query (e.g. "SCRUM-123", "login bug repo:org/app")'},limit:{type:"number",description:"Max results (default: 20)"}},required:["query"]}},{name:"github_search_code",description:"Search code across GitHub repositories by keyword",input_schema:{type:"object",properties:{query:{type:"string",description:'Code search query (e.g. "handleLogin", "class AuthService")'},repo:{type:"string",description:'Scope to a specific repo (e.g. "org/app"). Optional.'},language:{type:"string",description:'Filter by language (e.g. "javascript", "python"). Optional.'},limit:{type:"number",description:"Max results (default: 15)"}},required:["query"]}},{name:"github_get_pr",description:"Get details of a pull request \u2014 title, description, branch, stats",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"PR number"}},required:["owner","repo","number"]}},{name:"github_get_pr_diff",description:"Get the unified diff of a pull request \u2014 the actual code changes",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"PR number"}},required:["owner","repo","number"]}},{name:"github_list_pr_files",description:"List files changed in a PR with per-file patches",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"PR number"}},required:["owner","repo","number"]}},{name:"github_list_pr_comments",description:"Get all review and issue comments on a PR",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"PR number"}},required:["owner","repo","number"]}},{name:"github_get_review_thread",description:"Read a PR review-comment THREAD given any comment id in it: the root review comment + all its replies, plus the anchored diff context (file, line, the original diff hunk). Use this to understand a human's reply to a previous review comment before replying in-thread.",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"PR number"},commentId:{type:"number",description:"Any review-comment id in the thread (the root or any reply)"}},required:["owner","repo","number","commentId"]}},{name:"github_reply_review_thread",description:"Reply IN-THREAD to an existing PR review-comment thread (a conversational reply nested under the thread the human commented on \u2014 NOT a fresh full review). Pass any comment id in the thread.",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"PR number"},commentId:{type:"number",description:"Any review-comment id in the thread to reply to"},body:{type:"string",description:"The reply text (markdown)"}},required:["owner","repo","number","commentId","body"]}},{name:"github_reply_issue_comment",description:"Post a reply on a PR's top-level conversation (a new issue comment on the PR). Use when the human replied to a non-inline/summary comment rather than an inline review thread. Quote or @-mention for context since issue comments are not threaded.",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"PR number"},body:{type:"string",description:"The reply text (markdown)"}},required:["owner","repo","number","body"]}},{name:"github_create_review",description:"Post a review on a pull request: a summary body plus optional inline comments anchored to file/line, with an event (COMMENT, APPROVE, or REQUEST_CHANGES). Use this to deliver a code review back to the PR.",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"PR number"},body:{type:"string",description:"The review summary (markdown). Shown as the top-level review comment."},event:{type:"string",enum:["COMMENT","APPROVE","REQUEST_CHANGES"],description:"Review verdict. Default COMMENT (no approval state). Use REQUEST_CHANGES for blocking issues."},comments:{type:"array",description:"Optional inline comments, each anchored to a changed line.",items:{type:"object",properties:{path:{type:"string",description:"File path as it appears in the diff"},line:{type:"number",description:"Line number in the file's NEW version (the right side of the diff)"},side:{type:"string",enum:["LEFT","RIGHT"],description:"RIGHT (new) or LEFT (old). Default RIGHT."},body:{type:"string",description:"The inline comment text (markdown)"}},required:["path","line","body"]}}},required:["owner","repo","number"]}},{name:"github_list_commits",description:"List recent commits on a branch, optionally filtered by file path",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},branch:{type:"string",description:"Branch name (default: repo default branch)"},path:{type:"string",description:"Filter commits touching this file path"},limit:{type:"number",description:"Max commits (default: 20)"}},required:["owner","repo"]}},{name:"github_get_commit",description:"Get details of a specific commit \u2014 message, stats, file diffs",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},sha:{type:"string",description:"Commit SHA (full or short)"}},required:["owner","repo","sha"]}},{name:"github_get_file",description:"Read a file (or list a directory) from a GitHub repo. Works on any branch/ref.",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},path:{type:"string",description:'File or directory path (e.g. "src/auth/login.ts")'},ref:{type:"string",description:"Branch, tag, or commit SHA (default: repo default branch)"}},required:["owner","repo","path"]}},{name:"github_create_issue",description:"Create a GitHub issue",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},title:{type:"string",description:"Issue title"},body:{type:"string",description:"Issue body (markdown)"}},required:["owner","repo","title"]}},{name:"github_list_issues",description:"List issues in a repo (excludes pull requests). Filter by state, labels, and an updated-since cursor for polling.",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},state:{type:"string",enum:["open","closed","all"],description:"Filter by state (default: open)"},labels:{type:"array",items:{type:"string"},description:"Only issues carrying ALL of these labels"},since:{type:"string",description:"ISO-8601 timestamp; only issues updated at/after this (polling cursor)"},assignee:{type:"string",description:'Filter by assignee login, "none", or "*"'},sort:{type:"string",enum:["created","updated","comments"],description:"Sort field (default: updated)"},direction:{type:"string",enum:["asc","desc"],description:"Sort direction (default: desc)"},limit:{type:"number",description:"Max issues (default: 30, max 100 per page)"}},required:["owner","repo"]}},{name:"github_get_issue",description:"Get a single GitHub issue with full detail (title, body, state, labels, assignee, url)",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"Issue number"}},required:["owner","repo","number"]}},{name:"github_get_issue_comments",description:"Get the comment thread on a GitHub issue (chronological)",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"Issue number"},limit:{type:"number",description:"Max comments (default: 100)"}},required:["owner","repo","number"]}},{name:"github_add_issue_comment",description:"Add a comment to a GitHub issue. Also the way to record a PR link on an issue (post a markdown link).",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"Issue number"},body:{type:"string",description:"Comment body (markdown)"}},required:["owner","repo","number","body"]}},{name:"github_close_issue",description:"Close a GitHub issue. Optionally set the close reason (completed or not_planned).",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"Issue number"},stateReason:{type:"string",enum:["completed","not_planned"],description:"Why the issue was closed (optional)"}},required:["owner","repo","number"]}},{name:"github_reopen_issue",description:"Reopen a closed GitHub issue",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"Issue number"}},required:["owner","repo","number"]}},{name:"github_label_issue",description:"Add, set (replace all), or remove labels on a GitHub issue. Labels back state-like transitions on GitHub.",input_schema:{type:"object",properties:{owner:{type:"string",description:"Repository owner"},repo:{type:"string",description:"Repository name"},number:{type:"number",description:"Issue number"},labels:{type:"array",items:{type:"string"},description:"Label name(s)"},mode:{type:"string",enum:["add","set","remove"],description:"add appends, set replaces all, remove deletes (default: add)"}},required:["owner","repo","number","labels"]}}]};import{existsSync as fs}from"fs";import{fileURLToPath as ys}from"url";import{dirname as hs,resolve as gs}from"path";function _s(){if(process.env.MCP_SKILL_PATH)return process.env.MCP_SKILL_PATH;let s=hs(ys(import.meta.url)),t=gs(s,"..","bin","mcp-skill.mjs");return fs(t)?t:null}function bs(){let s=process.env.GITLAB_API_URL;if(s)return s.replace(/\/+$/,"");let t=(process.env.GITLAB_URL||process.env.GITLAB_INSTANCE_URL||"https://gitlab.com").trim().replace(/\/+$/,"");return/\/api\/v\d+$/.test(t)?t:`${t}/api/v4`}function ws(){if(process.env.GITLAB_OAUTH_TOKEN)return{Authorization:`Bearer ${process.env.GITLAB_OAUTH_TOKEN}`};let s=process.env.GITLAB_TOKEN;if(!s)throw new Error("GitLab is not connected: set GITLAB_TOKEN (personal/project access token, api scope) or GITLAB_OAUTH_TOKEN.");return{"PRIVATE-TOKEN":s}}async function E(s,t={}){let e=/^https?:\/\//.test(s)?s:`${bs()}${s}`,r={Accept:"application/json","User-Agent":"Zibby-App",...ws(),...t.body?{"Content-Type":"application/json"}:{}},i=await fetch(e,{method:t.method||"GET",headers:r,body:t.body?JSON.stringify(t.body):void 0});if(!i.ok){let n=await i.text().catch(()=>"");throw new Error(`GitLab API ${i.status}: ${n.slice(0,300)}`)}return t.raw?i.text():i.json()}function J(s){let t=String(s);return/^\d+$/.test(t)?t:encodeURIComponent(t)}var ft={id:"gitlab",serverName:"gitlab",allowedTools:["mcp__gitlab__*"],requiresIntegration:A.GITLAB,envKeys:["GITLAB_TOKEN","GITLAB_OAUTH_TOKEN","GITLAB_INSTANCE_URL","GITLAB_API_URL"],description:"GitLab \u2014 merge requests, diffs, MR reviews/discussions, issues",promptFragment:`## GitLab (connected)
115
118
  You have access to the user's GitLab projects via the REST API (cloud gitlab.com OR self-hosted). A "merge request" (MR) is GitLab's pull request. An MR is addressed by a PROJECT (numeric id OR full path like "group/repo") and an \`iid\` (the per-project MR number shown in the URL). For projects, prefer the full path form ("group/subgroup/repo") \u2014 it's what users have. Available tools:
116
119
 
120
+ ### Discovery
121
+ - gitlab_list_projects: List the projects this token can access (the ones you're a member of), optionally filtered by a search query. Use to find a RELATED project worth cloning for cross-repo context.
122
+
117
123
  ### Merge requests
118
124
  - gitlab_get_mr: Get an MR's details (title, description, author, source/target branch, state, web url, diff_refs)
119
125
  - gitlab_get_mr_changes: Get the MR's changed files with per-file diffs \u2014 THIS is the code to review
120
126
  - gitlab_list_mrs: List a project's merge requests (filter by state: opened|closed|merged|all)
121
127
  - gitlab_list_mr_notes: Get the discussion/notes thread on an MR
128
+ - gitlab_get_discussion: Read a single MR discussion (thread) by id \u2014 all its notes + the file/line it is anchored to
129
+ - gitlab_reply_discussion: Reply IN-THREAD to an existing MR discussion (conversational reply, not a new review)
122
130
  - gitlab_post_mr_note: Post a general (non-inline) comment on an MR
123
131
  - gitlab_post_mr_discussion: Post an INLINE review comment anchored to a file + line in the MR diff. Needs diff_refs (pass them from gitlab_get_mr / gitlab_get_mr_changes, or omit and the tool fetches them). Provide newLine for added/changed lines (or oldLine for removed/context lines).
124
132
 
@@ -129,7 +137,7 @@ You have access to the user's GitLab projects via the REST API (cloud gitlab.com
129
137
 
130
138
  ### Notes
131
139
  - A code-review flow is: gitlab_get_mr (context + diff_refs) \u2192 gitlab_get_mr_changes (the diff) \u2192 gitlab_post_mr_discussion per inline finding \u2192 gitlab_post_mr_note for the summary.
132
- - If an inline position is rejected by GitLab (bad line anchor), fall back to gitlab_post_mr_note with the file/line in the text.`,resolve(){let s=ps();if(!s)return{command:null,args:[],env:{},description:this.description};let t={};for(let e of this.envKeys)process.env[e]&&(t[e]=process.env[e]);return{type:"stdio",command:"node",args:[s,"../dist/gitlab.js","gitlabSkill"],env:t,description:this.description,alwaysLoad:!0}},async handleToolCall(s,t){try{switch(s){case"gitlab_get_mr":{let{projectId:e,iid:r}=t||{};if(!e||!r)return JSON.stringify({error:"projectId and iid are required"});let i=await L(`/projects/${D(e)}/merge_requests/${r}`);return JSON.stringify({iid:i.iid,projectId:i.project_id,title:i.title,description:(i.description||"").slice(0,5e3),state:i.state,author:i.author?.username,sourceBranch:i.source_branch,targetBranch:i.target_branch,draft:i.draft??i.work_in_progress??!1,mergeStatus:i.merge_status,changesCount:i.changes_count,labels:Array.isArray(i.labels)?i.labels:[],webUrl:i.web_url,createdAt:i.created_at,updatedAt:i.updated_at,mergedAt:i.merged_at,diffRefs:i.diff_refs||null})}case"gitlab_get_mr_changes":{let{projectId:e,iid:r}=t||{};if(!e||!r)return JSON.stringify({error:"projectId and iid are required"});let i=await L(`/projects/${D(e)}/merge_requests/${r}/changes`),n=Array.isArray(i.changes)?i.changes:[];return JSON.stringify({iid:i.iid,total:n.length,diffRefs:i.diff_refs||null,files:n.map(o=>({oldPath:o.old_path,newPath:o.new_path,newFile:!!o.new_file,deletedFile:!!o.deleted_file,renamedFile:!!o.renamed_file,diff:typeof o.diff=="string"?o.diff.slice(0,3e3):""}))})}case"gitlab_list_mrs":{let{projectId:e,state:r,targetBranch:i,sourceBranch:n,authorUsername:o,labels:c,search:a,sort:u,orderBy:l,limit:p}=t||{};if(!e)return JSON.stringify({error:"projectId is required"});let d=new URLSearchParams;d.set("state",r||"opened"),d.set("per_page",String(p||20)),d.set("order_by",l||"updated_at"),d.set("sort",u||"desc"),i&&d.set("target_branch",i),n&&d.set("source_branch",n),o&&d.set("author_username",o),c&&d.set("labels",Array.isArray(c)?c.join(","):c),a&&d.set("search",a);let m=await L(`/projects/${D(e)}/merge_requests?${d.toString()}`),f=(Array.isArray(m)?m:[]).map(y=>({iid:y.iid,title:y.title,state:y.state,author:y.author?.username,sourceBranch:y.source_branch,targetBranch:y.target_branch,draft:y.draft??y.work_in_progress??!1,labels:Array.isArray(y.labels)?y.labels:[],webUrl:y.web_url,createdAt:y.created_at,updatedAt:y.updated_at}));return JSON.stringify({count:f.length,mergeRequests:f})}case"gitlab_list_mr_notes":{let{projectId:e,iid:r,limit:i}=t||{};if(!e||!r)return JSON.stringify({error:"projectId and iid are required"});let n=await L(`/projects/${D(e)}/merge_requests/${r}/notes?per_page=${i||50}&sort=asc&order_by=created_at`);return JSON.stringify({total:Array.isArray(n)?n.length:0,notes:(Array.isArray(n)?n:[]).map(o=>({id:o.id,author:o.author?.username,body:(o.body||"").slice(0,1e3),system:!!o.system,createdAt:o.created_at}))})}case"gitlab_post_mr_note":{let{projectId:e,iid:r,body:i}=t||{};if(!e||!r||!i)return JSON.stringify({error:"projectId, iid, and body are required"});let n=await L(`/projects/${D(e)}/merge_requests/${r}/notes`,{method:"POST",body:{body:String(i)}});return JSON.stringify({ok:!0,id:n.id,createdAt:n.created_at})}case"gitlab_post_mr_discussion":{let{projectId:e,iid:r,path:i,oldPath:n,newLine:o,oldLine:c,body:a}=t||{};if(!e||!r||!i||!a)return JSON.stringify({error:"projectId, iid, path, and body are required"});if(o==null&&c==null)return JSON.stringify({error:"newLine (added/changed line) or oldLine (removed/context line) is required to anchor an inline comment"});let u=D(e),l=t.diffRefs||null;if(l||(l=(await L(`/projects/${u}/merge_requests/${r}`)).diff_refs||null),!l||!l.head_sha)return JSON.stringify({error:"could not resolve diff_refs for this MR \u2014 cannot anchor an inline comment. Use gitlab_post_mr_note instead."});let p={base_sha:l.base_sha,start_sha:l.start_sha,head_sha:l.head_sha,position_type:"text",new_path:i,old_path:n||i};o!=null&&(p.new_line=Number(o)),c!=null&&(p.old_line=Number(c));try{let d=await L(`/projects/${u}/merge_requests/${r}/discussions`,{method:"POST",body:{body:String(a),position:p}});return JSON.stringify({ok:!0,discussionId:d.id})}catch(d){return JSON.stringify({ok:!1,error:`inline anchor rejected (${d.message}). The line must be part of the MR diff. Fall back to gitlab_post_mr_note with the file/line in the text.`})}}case"gitlab_create_mr_review":{let{projectId:e,iid:r,body:i,comments:n}=t||{};if(!e||!r)return JSON.stringify({error:"projectId and iid are required"});let o=D(e),c=Array.isArray(n)?n.filter(d=>d&&d.path&&d.body&&(d.newLine!=null||d.oldLine!=null)):[];if(!i&&c.length===0)return JSON.stringify({error:"a review needs a body and/or inline comments"});let a=t.diffRefs||null;c.length>0&&!a&&(a=(await L(`/projects/${o}/merge_requests/${r}`)).diff_refs||null);let u=!1;i&&(await L(`/projects/${o}/merge_requests/${r}/notes`,{method:"POST",body:{body:String(i)}}),u=!0);let l=0,p=[];if(c.length>0&&a)for(let d of c){let m={base_sha:a.base_sha,start_sha:a.start_sha,head_sha:a.head_sha,position_type:"text",new_path:d.path,old_path:d.oldPath||d.path};d.newLine!=null&&(m.new_line=Number(d.newLine)),d.oldLine!=null&&(m.old_line=Number(d.oldLine));try{await L(`/projects/${o}/merge_requests/${r}/discussions`,{method:"POST",body:{body:String(d.body),position:m}}),l+=1}catch(f){p.push(`${d.path}:${d.newLine??d.oldLine} \u2014 ${f.message}`)}}else c.length>0&&!a&&p.push("no diff_refs available \u2014 inline comments skipped (pass diffRefs from gitlab_get_mr)");return JSON.stringify({ok:!0,notePosted:u,inlinePosted:l,inlineErrors:p.length?p:void 0})}case"gitlab_list_issues":{let{projectId:e,state:r,labels:i,assigneeUsername:n,authorUsername:o,updatedAfter:c,search:a,sort:u,orderBy:l,limit:p}=t||{};if(!e)return JSON.stringify({error:"projectId is required"});let d=new URLSearchParams;d.set("state",r||"opened"),d.set("per_page",String(p||30)),d.set("order_by",l||"updated_at"),d.set("sort",u||"desc"),i&&d.set("labels",Array.isArray(i)?i.join(","):i),n&&d.set("assignee_username",n),o&&d.set("author_username",o),c&&d.set("updated_after",c),a&&d.set("search",a);let m=await L(`/projects/${D(e)}/issues?${d.toString()}`),f=(Array.isArray(m)?m:[]).map(y=>({iid:y.iid,title:y.title,state:y.state,labels:Array.isArray(y.labels)?y.labels:[],author:y.author?.username,assignees:(y.assignees||[]).map(_=>_.username),userNotesCount:y.user_notes_count,webUrl:y.web_url,createdAt:y.created_at,updatedAt:y.updated_at}));return JSON.stringify({count:f.length,issues:f})}case"gitlab_get_issue":{let{projectId:e,iid:r}=t||{};if(!e||!r)return JSON.stringify({error:"projectId and iid are required"});let i=await L(`/projects/${D(e)}/issues/${r}`);return JSON.stringify({iid:i.iid,projectId:i.project_id,title:i.title,description:(i.description||"").slice(0,5e3),state:i.state,labels:Array.isArray(i.labels)?i.labels:[],author:i.author?.username,assignees:(i.assignees||[]).map(n=>n.username),milestone:i.milestone?.title||null,webUrl:i.web_url,createdAt:i.created_at,updatedAt:i.updated_at,closedAt:i.closed_at})}case"gitlab_add_issue_comment":{let{projectId:e,iid:r,body:i}=t||{};if(!e||!r||!i)return JSON.stringify({error:"projectId, iid, and body are required"});let n=await L(`/projects/${D(e)}/issues/${r}/notes`,{method:"POST",body:{body:String(i)}});return JSON.stringify({ok:!0,id:n.id,createdAt:n.created_at})}default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(e){return JSON.stringify({error:e.message})}},tools:[{name:"gitlab_get_mr",description:"Get a GitLab merge request \u2014 title, description, branches, state, author, web url, and diff_refs (needed to anchor inline review comments).",input_schema:{type:"object",properties:{projectId:{type:"string",description:'Project numeric id OR full path (e.g. "group/repo")'},iid:{type:"number",description:"Merge request iid (the per-project MR number in the URL)"}},required:["projectId","iid"]}},{name:"gitlab_get_mr_changes",description:"Get the changed files of a GitLab merge request with per-file diffs \u2014 the actual code changes to review. Also returns diff_refs for inline comments.",input_schema:{type:"object",properties:{projectId:{type:"string",description:'Project numeric id OR full path (e.g. "group/repo")'},iid:{type:"number",description:"Merge request iid"}},required:["projectId","iid"]}},{name:"gitlab_list_mrs",description:"List a GitLab project's merge requests, filtered by state and other criteria. Returns newest-updated first.",input_schema:{type:"object",properties:{projectId:{type:"string",description:'Project numeric id OR full path (e.g. "group/repo")'},state:{type:"string",enum:["opened","closed","merged","locked","all"],description:"Filter by state (default: opened)"},targetBranch:{type:"string",description:"Filter by target branch"},sourceBranch:{type:"string",description:"Filter by source branch"},authorUsername:{type:"string",description:"Filter by author username"},labels:{type:"array",items:{type:"string"},description:"Only MRs carrying ALL of these labels"},search:{type:"string",description:"Search title and description"},orderBy:{type:"string",enum:["created_at","updated_at","title"],description:"Sort field (default: updated_at)"},sort:{type:"string",enum:["asc","desc"],description:"Sort direction (default: desc)"},limit:{type:"number",description:"Max MRs (default: 20)"}},required:["projectId"]}},{name:"gitlab_list_mr_notes",description:"List the discussion notes on a GitLab merge request (chronological).",input_schema:{type:"object",properties:{projectId:{type:"string",description:"Project numeric id OR full path"},iid:{type:"number",description:"Merge request iid"},limit:{type:"number",description:"Max notes (default 50)"}},required:["projectId","iid"]}},{name:"gitlab_post_mr_note",description:"Post a general (non-inline) comment on a GitLab merge request. Use for a review summary or a top-level remark.",input_schema:{type:"object",properties:{projectId:{type:"string",description:"Project numeric id OR full path"},iid:{type:"number",description:"Merge request iid"},body:{type:"string",description:"Comment body (markdown)"}},required:["projectId","iid","body"]}},{name:"gitlab_post_mr_discussion",description:"Post an INLINE review comment anchored to a file + line in a GitLab merge request diff. Provide newLine (added/changed line) or oldLine (removed/context line). Pass diffRefs from gitlab_get_mr/gitlab_get_mr_changes, or omit to have the tool fetch them. If the line anchor is rejected, fall back to gitlab_post_mr_note.",input_schema:{type:"object",properties:{projectId:{type:"string",description:"Project numeric id OR full path"},iid:{type:"number",description:"Merge request iid"},path:{type:"string",description:"New file path as it appears in the diff"},oldPath:{type:"string",description:"Old file path (defaults to path)"},newLine:{type:"number",description:"Line number in the NEW version of the file (for added/changed lines)"},oldLine:{type:"number",description:"Line number in the OLD version (for removed/context lines)"},body:{type:"string",description:"The inline comment text (markdown)"},diffRefs:{type:"object",description:"The MR diff_refs ({ base_sha, start_sha, head_sha }) from gitlab_get_mr. Omit and the tool fetches them."}},required:["projectId","iid","path","body"]}},{name:"gitlab_create_mr_review",description:"Post a full review on a GitLab merge request in one call: a summary note plus optional inline comments anchored to file/line in the diff. Convenience wrapper over gitlab_post_mr_note + gitlab_post_mr_discussion.",input_schema:{type:"object",properties:{projectId:{type:"string",description:"Project numeric id OR full path"},iid:{type:"number",description:"Merge request iid"},body:{type:"string",description:"The review summary (markdown). Posted as a top-level MR note."},diffRefs:{type:"object",description:"The MR diff_refs ({ base_sha, start_sha, head_sha }) from gitlab_get_mr \u2014 required to anchor inline comments. Omit and the tool fetches them."},comments:{type:"array",description:"Optional inline comments, each anchored to a changed line in a file.",items:{type:"object",properties:{path:{type:"string",description:"New file path as it appears in the diff"},oldPath:{type:"string",description:"Old file path (defaults to path)"},newLine:{type:"number",description:"Line number in the NEW version of the file (for added/changed lines)"},oldLine:{type:"number",description:"Line number in the OLD version (for removed/context lines)"},body:{type:"string",description:"The inline comment text (markdown)"}},required:["path","body"]}}},required:["projectId","iid"]}},{name:"gitlab_list_issues",description:"List a GitLab project's issues, filtered by state, labels, and an updatedAfter polling cursor. Returns newest-updated first.",input_schema:{type:"object",properties:{projectId:{type:"string",description:'Project numeric id OR full path (e.g. "group/repo")'},state:{type:"string",enum:["opened","closed","all"],description:"Filter by state (default: opened)"},labels:{type:"array",items:{type:"string"},description:"Only issues carrying ALL of these labels"},assigneeUsername:{type:"string",description:"Filter by assignee username"},authorUsername:{type:"string",description:"Filter by author username"},updatedAfter:{type:"string",description:"ISO-8601 timestamp; only issues updated after this (polling cursor)"},search:{type:"string",description:"Search title and description"},orderBy:{type:"string",enum:["created_at","updated_at"],description:"Sort field (default: updated_at)"},sort:{type:"string",enum:["asc","desc"],description:"Sort direction (default: desc)"},limit:{type:"number",description:"Max issues (default: 30)"}},required:["projectId"]}},{name:"gitlab_get_issue",description:"Get a single GitLab issue with full detail (title, description, state, labels, assignees, web url).",input_schema:{type:"object",properties:{projectId:{type:"string",description:"Project numeric id OR full path"},iid:{type:"number",description:"Issue iid (the per-project issue number in the URL)"}},required:["projectId","iid"]}},{name:"gitlab_add_issue_comment",description:"Add a comment to a GitLab issue. Also the way to record an MR link on a ticket (post a markdown link).",input_schema:{type:"object",properties:{projectId:{type:"string",description:"Project numeric id OR full path"},iid:{type:"number",description:"Issue iid"},body:{type:"string",description:"Comment body (markdown)"}},required:["projectId","iid","body"]}}]};import{existsSync as fs}from"fs";import{fileURLToPath as ys}from"url";import{dirname as hs,resolve as gs}from"path";function _s(){if(process.env.MCP_SKILL_PATH)return process.env.MCP_SKILL_PATH;let s=hs(ys(import.meta.url)),t=gs(s,"..","bin","mcp-skill.mjs");return fs(t)?t:null}var bs=process.env.LINEAR_API_URL||"https://api.linear.app/graphql";function ks(){if(process.env.LINEAR_OAUTH_TOKEN)return`Bearer ${process.env.LINEAR_OAUTH_TOKEN}`;let s=process.env.LINEAR_API_KEY;if(!s)throw new Error("Linear is not connected: set LINEAR_API_KEY (personal API key) or LINEAR_OAUTH_TOKEN.");return s}async function U(s,t={}){let e=await fetch(bs,{method:"POST",headers:{Authorization:ks(),"Content-Type":"application/json"},body:JSON.stringify({query:s,variables:t})});if(!e.ok){let i=await e.text().catch(()=>"");throw new Error(`Linear API ${e.status}: ${i.slice(0,300)}`)}let r=await e.json().catch(()=>null);if(!r)throw new Error("Linear API returned a non-JSON body");if(Array.isArray(r.errors)&&r.errors.length){let i=r.errors.map(n=>n?.message||String(n)).join("; ");throw new Error(`Linear GraphQL error: ${i.slice(0,300)}`)}return r.data}function ce(s){return String(s||"").toLowerCase().replace(/\s+/g,"").replace(/[()\-_::"'`]/g,"")}function ws(s,t){let e=ce(s),r=ce(t);if(!e||!r)return 0;if(e===r)return 1;if(e.length===1||r.length===1)return e===r?1:0;let i=l=>{let p=new Map;for(let d=0;d<l.length-1;d++){let m=l.slice(d,d+2);p.set(m,(p.get(m)||0)+1)}return p},n=i(e),o=i(r),c=0,a=0,u=0;for(let l of n.values())a+=l;for(let l of o.values())u+=l;for(let[l,p]of n.entries())c+=Math.min(p,o.get(l)||0);return 2*c/Math.max(1,a+u)}function Ss(s,t){let e=Array.isArray(s)?s:[];if(!e.length)return{state:null,strategy:"no-states"};let r=ce(t);if(!r)return{state:null,strategy:"no-target"};let i=e.find(c=>ce(c.name)===r);if(i)return{state:i,strategy:"exact"};let n={backlog:["backlog"],unstarted:["todo","unstarted","open"],started:["inprogress","started","doing","wip"],completed:["done","completed","closed","resolved","fixed"],canceled:["canceled","cancelled","wontfix","won'tfix"],triage:["triage"]};for(let[c,a]of Object.entries(n)){if(!a.some(l=>ce(l)===r))continue;let u=e.find(l=>l.type===c);if(u)return{state:u,strategy:"type-alias"}}let o=e.map(c=>({s:c,score:ws(t,c.name)})).sort((c,a)=>a.score-c.score);return o[0]&&o[0].score>=.5?{state:o[0].s,strategy:"fuzzy"}:{state:null,strategy:"no-match"}}var vs=`
140
+ - If an inline position is rejected by GitLab (bad line anchor), fall back to gitlab_post_mr_note with the file/line in the text.`,resolve(){let s=_s();if(!s)return{command:null,args:[],env:{},description:this.description};let t={};for(let e of this.envKeys)process.env[e]&&(t[e]=process.env[e]);return{type:"stdio",command:"node",args:[s,"../dist/gitlab.js","gitlabSkill"],env:t,description:this.description,alwaysLoad:!0}},async handleToolCall(s,t){try{switch(s){case"gitlab_get_mr":{let{projectId:e,iid:r}=t||{};if(!e||!r)return JSON.stringify({error:"projectId and iid are required"});let i=await E(`/projects/${J(e)}/merge_requests/${r}`);return JSON.stringify({iid:i.iid,projectId:i.project_id,title:i.title,description:(i.description||"").slice(0,5e3),state:i.state,author:i.author?.username,sourceBranch:i.source_branch,targetBranch:i.target_branch,draft:i.draft??i.work_in_progress??!1,mergeStatus:i.merge_status,changesCount:i.changes_count,labels:Array.isArray(i.labels)?i.labels:[],webUrl:i.web_url,createdAt:i.created_at,updatedAt:i.updated_at,mergedAt:i.merged_at,diffRefs:i.diff_refs||null})}case"gitlab_get_mr_changes":{let{projectId:e,iid:r}=t||{};if(!e||!r)return JSON.stringify({error:"projectId and iid are required"});let i=await E(`/projects/${J(e)}/merge_requests/${r}/changes`),n=Array.isArray(i.changes)?i.changes:[];return JSON.stringify({iid:i.iid,total:n.length,diffRefs:i.diff_refs||null,files:n.map(o=>({oldPath:o.old_path,newPath:o.new_path,newFile:!!o.new_file,deletedFile:!!o.deleted_file,renamedFile:!!o.renamed_file,diff:typeof o.diff=="string"?o.diff.slice(0,3e3):""}))})}case"gitlab_list_mrs":{let{projectId:e,state:r,targetBranch:i,sourceBranch:n,authorUsername:o,labels:c,search:a,sort:u,orderBy:l,limit:d}=t||{};if(!e)return JSON.stringify({error:"projectId is required"});let p=new URLSearchParams;p.set("state",r||"opened"),p.set("per_page",String(d||20)),p.set("order_by",l||"updated_at"),p.set("sort",u||"desc"),i&&p.set("target_branch",i),n&&p.set("source_branch",n),o&&p.set("author_username",o),c&&p.set("labels",Array.isArray(c)?c.join(","):c),a&&p.set("search",a);let m=await E(`/projects/${J(e)}/merge_requests?${p.toString()}`),f=(Array.isArray(m)?m:[]).map(y=>({iid:y.iid,title:y.title,state:y.state,author:y.author?.username,sourceBranch:y.source_branch,targetBranch:y.target_branch,draft:y.draft??y.work_in_progress??!1,labels:Array.isArray(y.labels)?y.labels:[],webUrl:y.web_url,createdAt:y.created_at,updatedAt:y.updated_at}));return JSON.stringify({count:f.length,mergeRequests:f})}case"gitlab_list_mr_notes":{let{projectId:e,iid:r,limit:i}=t||{};if(!e||!r)return JSON.stringify({error:"projectId and iid are required"});let n=await E(`/projects/${J(e)}/merge_requests/${r}/notes?per_page=${i||50}&sort=asc&order_by=created_at`);return JSON.stringify({total:Array.isArray(n)?n.length:0,notes:(Array.isArray(n)?n:[]).map(o=>({id:o.id,author:o.author?.username,body:(o.body||"").slice(0,1e3),system:!!o.system,createdAt:o.created_at}))})}case"gitlab_post_mr_note":{let{projectId:e,iid:r,body:i}=t||{};if(!e||!r||!i)return JSON.stringify({error:"projectId, iid, and body are required"});let n=await E(`/projects/${J(e)}/merge_requests/${r}/notes`,{method:"POST",body:{body:String(i)}});return JSON.stringify({ok:!0,id:n.id,createdAt:n.created_at})}case"gitlab_post_mr_discussion":{let{projectId:e,iid:r,path:i,oldPath:n,newLine:o,oldLine:c,body:a}=t||{};if(!e||!r||!i||!a)return JSON.stringify({error:"projectId, iid, path, and body are required"});if(o==null&&c==null)return JSON.stringify({error:"newLine (added/changed line) or oldLine (removed/context line) is required to anchor an inline comment"});let u=J(e),l=t.diffRefs||null;if(l||(l=(await E(`/projects/${u}/merge_requests/${r}`)).diff_refs||null),!l||!l.head_sha)return JSON.stringify({error:"could not resolve diff_refs for this MR \u2014 cannot anchor an inline comment. Use gitlab_post_mr_note instead."});let d={base_sha:l.base_sha,start_sha:l.start_sha,head_sha:l.head_sha,position_type:"text",new_path:i,old_path:n||i};o!=null&&(d.new_line=Number(o)),c!=null&&(d.old_line=Number(c));try{let p=await E(`/projects/${u}/merge_requests/${r}/discussions`,{method:"POST",body:{body:String(a),position:d}});return JSON.stringify({ok:!0,discussionId:p.id})}catch(p){return JSON.stringify({ok:!1,error:`inline anchor rejected (${p.message}). The line must be part of the MR diff. Fall back to gitlab_post_mr_note with the file/line in the text.`})}}case"gitlab_create_mr_review":{let{projectId:e,iid:r,body:i,comments:n}=t||{};if(!e||!r)return JSON.stringify({error:"projectId and iid are required"});let o=J(e),c=Array.isArray(n)?n.filter(p=>p&&p.path&&p.body&&(p.newLine!=null||p.oldLine!=null)):[];if(!i&&c.length===0)return JSON.stringify({error:"a review needs a body and/or inline comments"});let a=t.diffRefs||null;c.length>0&&!a&&(a=(await E(`/projects/${o}/merge_requests/${r}`)).diff_refs||null);let u=!1;i&&(await E(`/projects/${o}/merge_requests/${r}/notes`,{method:"POST",body:{body:String(i)}}),u=!0);let l=0,d=[];if(c.length>0&&a)for(let p of c){let m={base_sha:a.base_sha,start_sha:a.start_sha,head_sha:a.head_sha,position_type:"text",new_path:p.path,old_path:p.oldPath||p.path};p.newLine!=null&&(m.new_line=Number(p.newLine)),p.oldLine!=null&&(m.old_line=Number(p.oldLine));try{await E(`/projects/${o}/merge_requests/${r}/discussions`,{method:"POST",body:{body:String(p.body),position:m}}),l+=1}catch(f){d.push(`${p.path}:${p.newLine??p.oldLine} \u2014 ${f.message}`)}}else c.length>0&&!a&&d.push("no diff_refs available \u2014 inline comments skipped (pass diffRefs from gitlab_get_mr)");return JSON.stringify({ok:!0,notePosted:u,inlinePosted:l,inlineErrors:d.length?d:void 0})}case"gitlab_get_discussion":{let{projectId:e,iid:r,discussionId:i}=t||{};if(!e||!r||!i)return JSON.stringify({error:"projectId, iid, and discussionId are required"});let n=await E(`/projects/${J(e)}/merge_requests/${r}/discussions/${encodeURIComponent(i)}`),o=Array.isArray(n.notes)?n.notes:[],c=o.find(u=>u.position)||null,a=c?c.position:null;return JSON.stringify({discussionId:n.id,individualNote:!!n.individual_note,path:a&&(a.new_path||a.old_path)||null,newLine:a?a.new_line??null:null,oldLine:a?a.old_line??null:null,diffRefs:a?{base_sha:a.base_sha,start_sha:a.start_sha,head_sha:a.head_sha}:null,notes:o.map(u=>({id:u.id,author:u.author?.username,body:(u.body||"").slice(0,4e3),system:!!u.system,createdAt:u.created_at}))})}case"gitlab_reply_discussion":{let{projectId:e,iid:r,discussionId:i,body:n}=t||{};if(!e||!r||!i||!n)return JSON.stringify({error:"projectId, iid, discussionId, and body are required"});let o=await E(`/projects/${J(e)}/merge_requests/${r}/discussions/${encodeURIComponent(i)}/notes`,{method:"POST",body:{body:String(n)}});return JSON.stringify({ok:!0,id:o.id,createdAt:o.created_at})}case"gitlab_list_projects":{let{query:e,limit:r}=t||{},i=Math.min(Number(r)>0?Number(r):50,200),n=new URLSearchParams;n.set("membership","true"),n.set("simple","true"),n.set("order_by","last_activity_at"),n.set("sort","desc"),n.set("per_page",String(Math.min(i+1,100))),e&&n.set("search",String(e));let o=await E(`/projects?${n.toString()}`),c=Array.isArray(o)?o:[],a=c.length>i,u=c.slice(0,i).map(l=>({fullPath:l.path_with_namespace,name:l.name,webUrl:l.web_url,defaultBranch:l.default_branch||null,visibility:l.visibility||null}));return JSON.stringify({count:u.length,truncated:a,projects:u})}case"gitlab_list_issues":{let{projectId:e,state:r,labels:i,assigneeUsername:n,authorUsername:o,updatedAfter:c,search:a,sort:u,orderBy:l,limit:d}=t||{};if(!e)return JSON.stringify({error:"projectId is required"});let p=new URLSearchParams;p.set("state",r||"opened"),p.set("per_page",String(d||30)),p.set("order_by",l||"updated_at"),p.set("sort",u||"desc"),i&&p.set("labels",Array.isArray(i)?i.join(","):i),n&&p.set("assignee_username",n),o&&p.set("author_username",o),c&&p.set("updated_after",c),a&&p.set("search",a);let m=await E(`/projects/${J(e)}/issues?${p.toString()}`),f=(Array.isArray(m)?m:[]).map(y=>({iid:y.iid,title:y.title,state:y.state,labels:Array.isArray(y.labels)?y.labels:[],author:y.author?.username,assignees:(y.assignees||[]).map(_=>_.username),userNotesCount:y.user_notes_count,webUrl:y.web_url,createdAt:y.created_at,updatedAt:y.updated_at}));return JSON.stringify({count:f.length,issues:f})}case"gitlab_get_issue":{let{projectId:e,iid:r}=t||{};if(!e||!r)return JSON.stringify({error:"projectId and iid are required"});let i=await E(`/projects/${J(e)}/issues/${r}`);return JSON.stringify({iid:i.iid,projectId:i.project_id,title:i.title,description:(i.description||"").slice(0,5e3),state:i.state,labels:Array.isArray(i.labels)?i.labels:[],author:i.author?.username,assignees:(i.assignees||[]).map(n=>n.username),milestone:i.milestone?.title||null,webUrl:i.web_url,createdAt:i.created_at,updatedAt:i.updated_at,closedAt:i.closed_at})}case"gitlab_add_issue_comment":{let{projectId:e,iid:r,body:i}=t||{};if(!e||!r||!i)return JSON.stringify({error:"projectId, iid, and body are required"});let n=await E(`/projects/${J(e)}/issues/${r}/notes`,{method:"POST",body:{body:String(i)}});return JSON.stringify({ok:!0,id:n.id,createdAt:n.created_at})}default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(e){return JSON.stringify({error:e.message})}},tools:[{name:"gitlab_list_projects",description:"List the GitLab projects this token can access (the projects you are a member of), optionally filtered by a search query. Use this to discover a RELATED project worth cloning when a change's correctness depends on another accessible repo. Returns a normalized list of { fullPath, name, webUrl, defaultBranch, visibility } and a truncated flag.",input_schema:{type:"object",properties:{query:{type:"string",description:"Optional search term matched against project name/path"},limit:{type:"number",description:"Max projects (default 50, hard max 200)"}}}},{name:"gitlab_get_mr",description:"Get a GitLab merge request \u2014 title, description, branches, state, author, web url, and diff_refs (needed to anchor inline review comments).",input_schema:{type:"object",properties:{projectId:{type:"string",description:'Project numeric id OR full path (e.g. "group/repo")'},iid:{type:"number",description:"Merge request iid (the per-project MR number in the URL)"}},required:["projectId","iid"]}},{name:"gitlab_get_mr_changes",description:"Get the changed files of a GitLab merge request with per-file diffs \u2014 the actual code changes to review. Also returns diff_refs for inline comments.",input_schema:{type:"object",properties:{projectId:{type:"string",description:'Project numeric id OR full path (e.g. "group/repo")'},iid:{type:"number",description:"Merge request iid"}},required:["projectId","iid"]}},{name:"gitlab_list_mrs",description:"List a GitLab project's merge requests, filtered by state and other criteria. Returns newest-updated first.",input_schema:{type:"object",properties:{projectId:{type:"string",description:'Project numeric id OR full path (e.g. "group/repo")'},state:{type:"string",enum:["opened","closed","merged","locked","all"],description:"Filter by state (default: opened)"},targetBranch:{type:"string",description:"Filter by target branch"},sourceBranch:{type:"string",description:"Filter by source branch"},authorUsername:{type:"string",description:"Filter by author username"},labels:{type:"array",items:{type:"string"},description:"Only MRs carrying ALL of these labels"},search:{type:"string",description:"Search title and description"},orderBy:{type:"string",enum:["created_at","updated_at","title"],description:"Sort field (default: updated_at)"},sort:{type:"string",enum:["asc","desc"],description:"Sort direction (default: desc)"},limit:{type:"number",description:"Max MRs (default: 20)"}},required:["projectId"]}},{name:"gitlab_list_mr_notes",description:"List the discussion notes on a GitLab merge request (chronological).",input_schema:{type:"object",properties:{projectId:{type:"string",description:"Project numeric id OR full path"},iid:{type:"number",description:"Merge request iid"},limit:{type:"number",description:"Max notes (default 50)"}},required:["projectId","iid"]}},{name:"gitlab_post_mr_note",description:"Post a general (non-inline) comment on a GitLab merge request. Use for a review summary or a top-level remark.",input_schema:{type:"object",properties:{projectId:{type:"string",description:"Project numeric id OR full path"},iid:{type:"number",description:"Merge request iid"},body:{type:"string",description:"Comment body (markdown)"}},required:["projectId","iid","body"]}},{name:"gitlab_post_mr_discussion",description:"Post an INLINE review comment anchored to a file + line in a GitLab merge request diff. Provide newLine (added/changed line) or oldLine (removed/context line). Pass diffRefs from gitlab_get_mr/gitlab_get_mr_changes, or omit to have the tool fetch them. If the line anchor is rejected, fall back to gitlab_post_mr_note.",input_schema:{type:"object",properties:{projectId:{type:"string",description:"Project numeric id OR full path"},iid:{type:"number",description:"Merge request iid"},path:{type:"string",description:"New file path as it appears in the diff"},oldPath:{type:"string",description:"Old file path (defaults to path)"},newLine:{type:"number",description:"Line number in the NEW version of the file (for added/changed lines)"},oldLine:{type:"number",description:"Line number in the OLD version (for removed/context lines)"},body:{type:"string",description:"The inline comment text (markdown)"},diffRefs:{type:"object",description:"The MR diff_refs ({ base_sha, start_sha, head_sha }) from gitlab_get_mr. Omit and the tool fetches them."}},required:["projectId","iid","path","body"]}},{name:"gitlab_create_mr_review",description:"Post a full review on a GitLab merge request in one call: a summary note plus optional inline comments anchored to file/line in the diff. Convenience wrapper over gitlab_post_mr_note + gitlab_post_mr_discussion.",input_schema:{type:"object",properties:{projectId:{type:"string",description:"Project numeric id OR full path"},iid:{type:"number",description:"Merge request iid"},body:{type:"string",description:"The review summary (markdown). Posted as a top-level MR note."},diffRefs:{type:"object",description:"The MR diff_refs ({ base_sha, start_sha, head_sha }) from gitlab_get_mr \u2014 required to anchor inline comments. Omit and the tool fetches them."},comments:{type:"array",description:"Optional inline comments, each anchored to a changed line in a file.",items:{type:"object",properties:{path:{type:"string",description:"New file path as it appears in the diff"},oldPath:{type:"string",description:"Old file path (defaults to path)"},newLine:{type:"number",description:"Line number in the NEW version of the file (for added/changed lines)"},oldLine:{type:"number",description:"Line number in the OLD version (for removed/context lines)"},body:{type:"string",description:"The inline comment text (markdown)"}},required:["path","body"]}}},required:["projectId","iid"]}},{name:"gitlab_get_discussion",description:"Read a single GitLab merge-request DISCUSSION (thread) by its discussion id: all notes in order plus the diff position (file + line) it is anchored to. Use this to understand a human's reply to a previous review discussion before replying in-thread.",input_schema:{type:"object",properties:{projectId:{type:"string",description:"Project numeric id OR full path"},iid:{type:"number",description:"Merge request iid"},discussionId:{type:"string",description:"The discussion id (from the Note Hook payload or gitlab_list_mr_notes)"}},required:["projectId","iid","discussionId"]}},{name:"gitlab_reply_discussion",description:"Reply IN-THREAD to an existing GitLab merge-request discussion (a conversational reply appended to the SAME thread \u2014 NOT a fresh review). Use after gitlab_get_discussion to answer a human's reply to a review comment.",input_schema:{type:"object",properties:{projectId:{type:"string",description:"Project numeric id OR full path"},iid:{type:"number",description:"Merge request iid"},discussionId:{type:"string",description:"The discussion id to reply to"},body:{type:"string",description:"The reply text (markdown)"}},required:["projectId","iid","discussionId","body"]}},{name:"gitlab_list_issues",description:"List a GitLab project's issues, filtered by state, labels, and an updatedAfter polling cursor. Returns newest-updated first.",input_schema:{type:"object",properties:{projectId:{type:"string",description:'Project numeric id OR full path (e.g. "group/repo")'},state:{type:"string",enum:["opened","closed","all"],description:"Filter by state (default: opened)"},labels:{type:"array",items:{type:"string"},description:"Only issues carrying ALL of these labels"},assigneeUsername:{type:"string",description:"Filter by assignee username"},authorUsername:{type:"string",description:"Filter by author username"},updatedAfter:{type:"string",description:"ISO-8601 timestamp; only issues updated after this (polling cursor)"},search:{type:"string",description:"Search title and description"},orderBy:{type:"string",enum:["created_at","updated_at"],description:"Sort field (default: updated_at)"},sort:{type:"string",enum:["asc","desc"],description:"Sort direction (default: desc)"},limit:{type:"number",description:"Max issues (default: 30)"}},required:["projectId"]}},{name:"gitlab_get_issue",description:"Get a single GitLab issue with full detail (title, description, state, labels, assignees, web url).",input_schema:{type:"object",properties:{projectId:{type:"string",description:"Project numeric id OR full path"},iid:{type:"number",description:"Issue iid (the per-project issue number in the URL)"}},required:["projectId","iid"]}},{name:"gitlab_add_issue_comment",description:"Add a comment to a GitLab issue. Also the way to record an MR link on a ticket (post a markdown link).",input_schema:{type:"object",properties:{projectId:{type:"string",description:"Project numeric id OR full path"},iid:{type:"number",description:"Issue iid"},body:{type:"string",description:"Comment body (markdown)"}},required:["projectId","iid","body"]}}]};import{existsSync as ks}from"fs";import{fileURLToPath as Ss}from"url";import{dirname as vs,resolve as Is}from"path";function Ns(){if(process.env.MCP_SKILL_PATH)return process.env.MCP_SKILL_PATH;let s=vs(Ss(import.meta.url)),t=Is(s,"..","bin","mcp-skill.mjs");return ks(t)?t:null}var Rs=process.env.LINEAR_API_URL||"https://api.linear.app/graphql";function Os(){if(process.env.LINEAR_OAUTH_TOKEN)return`Bearer ${process.env.LINEAR_OAUTH_TOKEN}`;let s=process.env.LINEAR_API_KEY;if(!s)throw new Error("Linear is not connected: set LINEAR_API_KEY (personal API key) or LINEAR_OAUTH_TOKEN.");return s}async function M(s,t={}){let e=await fetch(Rs,{method:"POST",headers:{Authorization:Os(),"Content-Type":"application/json"},body:JSON.stringify({query:s,variables:t})});if(!e.ok){let i=await e.text().catch(()=>"");throw new Error(`Linear API ${e.status}: ${i.slice(0,300)}`)}let r=await e.json().catch(()=>null);if(!r)throw new Error("Linear API returned a non-JSON body");if(Array.isArray(r.errors)&&r.errors.length){let i=r.errors.map(n=>n?.message||String(n)).join("; ");throw new Error(`Linear GraphQL error: ${i.slice(0,300)}`)}return r.data}function le(s){return String(s||"").toLowerCase().replace(/\s+/g,"").replace(/[()\-_::"'`]/g,"")}function As(s,t){let e=le(s),r=le(t);if(!e||!r)return 0;if(e===r)return 1;if(e.length===1||r.length===1)return e===r?1:0;let i=l=>{let d=new Map;for(let p=0;p<l.length-1;p++){let m=l.slice(p,p+2);d.set(m,(d.get(m)||0)+1)}return d},n=i(e),o=i(r),c=0,a=0,u=0;for(let l of n.values())a+=l;for(let l of o.values())u+=l;for(let[l,d]of n.entries())c+=Math.min(d,o.get(l)||0);return 2*c/Math.max(1,a+u)}function $s(s,t){let e=Array.isArray(s)?s:[];if(!e.length)return{state:null,strategy:"no-states"};let r=le(t);if(!r)return{state:null,strategy:"no-target"};let i=e.find(c=>le(c.name)===r);if(i)return{state:i,strategy:"exact"};let n={backlog:["backlog"],unstarted:["todo","unstarted","open"],started:["inprogress","started","doing","wip"],completed:["done","completed","closed","resolved","fixed"],canceled:["canceled","cancelled","wontfix","won'tfix"],triage:["triage"]};for(let[c,a]of Object.entries(n)){if(!a.some(l=>le(l)===r))continue;let u=e.find(l=>l.type===c);if(u)return{state:u,strategy:"type-alias"}}let o=e.map(c=>({s:c,score:As(t,c.name)})).sort((c,a)=>a.score-c.score);return o[0]&&o[0].score>=.5?{state:o[0].s,strategy:"fuzzy"}:{state:null,strategy:"no-match"}}var js=`
133
141
  id
134
142
  identifier
135
143
  number
@@ -143,7 +151,7 @@ You have access to the user's GitLab projects via the REST API (cloud gitlab.com
143
151
  assignee { id name displayName email }
144
152
  labels { nodes { id name color } }
145
153
  team { id key name }
146
- `,dt={id:"linear",serverName:"linear",allowedTools:["mcp__linear__*"],requiresIntegration:R.LINEAR,envKeys:["LINEAR_API_KEY","LINEAR_OAUTH_TOKEN"],description:"Linear \u2014 issues, comments, workflow states (GraphQL API key)",promptFragment:`## Linear (connected)
154
+ `,ht={id:"linear",serverName:"linear",allowedTools:["mcp__linear__*"],requiresIntegration:A.LINEAR,envKeys:["LINEAR_API_KEY","LINEAR_OAUTH_TOKEN"],description:"Linear \u2014 issues, comments, workflow states (GraphQL API key)",promptFragment:`## Linear (connected)
147
155
  You have direct access to the user's Linear workspace (GraphQL API). Tools:
148
156
 
149
157
  ### Discovery
@@ -161,32 +169,32 @@ You have direct access to the user's Linear workspace (GraphQL API). Tools:
161
169
 
162
170
  ### Notes
163
171
  - Always resolve a team first when you need states or want to create/move issues by state name \u2014 states only make sense within their team.
164
- - Issue identifier (ENG-123) and internal id (uuid) are both accepted by get/update tools.`,resolve(){let s={};for(let e of this.envKeys)process.env[e]&&(s[e]=process.env[e]);process.env.LINEAR_API_URL&&(s.LINEAR_API_URL=process.env.LINEAR_API_URL);let t=_s();return t?{type:"stdio",command:"node",args:[t,"../dist/linear.js","linearSkill"],env:s,description:this.description,alwaysLoad:!0}:{command:null,args:[],env:s,description:this.description}},async handleToolCall(s,t){try{switch(s){case"linear_list_teams":{let r=(await U(`
172
+ - Issue identifier (ENG-123) and internal id (uuid) are both accepted by get/update tools.`,resolve(){let s={};for(let e of this.envKeys)process.env[e]&&(s[e]=process.env[e]);process.env.LINEAR_API_URL&&(s.LINEAR_API_URL=process.env.LINEAR_API_URL);let t=Ns();return t?{type:"stdio",command:"node",args:[t,"../dist/linear.js","linearSkill"],env:s,description:this.description,alwaysLoad:!0}:{command:null,args:[],env:s,description:this.description}},async handleToolCall(s,t){try{switch(s){case"linear_list_teams":{let r=(await M(`
165
173
  query Teams($first: Int) {
166
174
  teams(first: $first) {
167
175
  nodes { id key name description }
168
176
  }
169
177
  }
170
- `,{first:t?.limit||50}))?.teams?.nodes||[];return JSON.stringify({count:r.length,teams:r})}case"linear_list_states":{let{teamId:e,teamKey:r}=t||{},i=e;if(!i&&r&&(i=await pt(r)),i){let a=(await U(`
178
+ `,{first:t?.limit||50}))?.teams?.nodes||[];return JSON.stringify({count:r.length,teams:r})}case"linear_list_states":{let{teamId:e,teamKey:r}=t||{},i=e;if(!i&&r&&(i=await yt(r)),i){let a=(await M(`
171
179
  query States($teamId: String!) {
172
180
  team(id: $teamId) {
173
181
  id key name
174
182
  states { nodes { id name type color position } }
175
183
  }
176
184
  }
177
- `,{teamId:i}))?.team,u=(a?.states?.nodes||[]).slice().sort((l,p)=>(l.position||0)-(p.position||0));return JSON.stringify({team:a?{id:a.id,key:a.key,name:a.name}:null,count:u.length,states:u})}let o=(await U(`
185
+ `,{teamId:i}))?.team,u=(a?.states?.nodes||[]).slice().sort((l,d)=>(l.position||0)-(d.position||0));return JSON.stringify({team:a?{id:a.id,key:a.key,name:a.name}:null,count:u.length,states:u})}let o=(await M(`
178
186
  query AllStates($first: Int) {
179
187
  workflowStates(first: $first) {
180
188
  nodes { id name type color team { id key name } }
181
189
  }
182
190
  }
183
- `,{first:t?.limit||200}))?.workflowStates?.nodes||[];return JSON.stringify({scope:"workspace",count:o.length,states:o})}case"linear_list_labels":{let{teamId:e}=t||{},i=(await U(`
191
+ `,{first:t?.limit||200}))?.workflowStates?.nodes||[];return JSON.stringify({scope:"workspace",count:o.length,states:o})}case"linear_list_labels":{let{teamId:e}=t||{},i=(await M(`
184
192
  query Labels($first: Int, $filter: IssueLabelFilter) {
185
193
  issueLabels(first: $first, filter: $filter) {
186
194
  nodes { id name color team { id key } }
187
195
  }
188
196
  }
189
- `,{first:t?.limit||100,filter:e?{team:{id:{eq:e}}}:void 0}))?.issueLabels?.nodes||[];return JSON.stringify({count:i.length,labels:i})}case"linear_list_issues":{let{teamId:e,teamKey:r,stateId:i,stateName:n,label:o,assigneeId:c,updatedAfter:a,limit:u}=t||{},l={},p=e;!p&&r&&(p=await pt(r)),p&&(l.team={id:{eq:p}}),i?l.state={id:{eq:i}}:n&&(l.state={name:{eqIgnoreCase:n}}),o&&(l.labels={name:{eqIgnoreCase:o}}),c&&(l.assignee={id:{eq:c}}),a&&(l.updatedAt={gt:a});let m=((await U(`
197
+ `,{first:t?.limit||100,filter:e?{team:{id:{eq:e}}}:void 0}))?.issueLabels?.nodes||[];return JSON.stringify({count:i.length,labels:i})}case"linear_list_issues":{let{teamId:e,teamKey:r,stateId:i,stateName:n,label:o,assigneeId:c,updatedAfter:a,limit:u}=t||{},l={},d=e;!d&&r&&(d=await yt(r)),d&&(l.team={id:{eq:d}}),i?l.state={id:{eq:i}}:n&&(l.state={name:{eqIgnoreCase:n}}),o&&(l.labels={name:{eqIgnoreCase:o}}),c&&(l.assignee={id:{eq:c}}),a&&(l.updatedAt={gt:a});let m=((await M(`
190
198
  query Issues($first: Int, $filter: IssueFilter, $orderBy: PaginationOrderBy) {
191
199
  issues(first: $first, filter: $filter, orderBy: $orderBy) {
192
200
  nodes {
@@ -198,74 +206,74 @@ You have direct access to the user's Linear workspace (GraphQL API). Tools:
198
206
  }
199
207
  }
200
208
  }
201
- `,{first:u||30,filter:Object.keys(l).length?l:void 0,orderBy:"updatedAt"}))?.issues?.nodes||[]).map(f=>({id:f.id,identifier:f.identifier,number:f.number,title:f.title,url:f.url,priority:f.priority,state:f.state?.name,stateType:f.state?.type,assignee:f.assignee?.displayName||null,labels:(f.labels?.nodes||[]).map(y=>y.name),team:f.team?.key,createdAt:f.createdAt,updatedAt:f.updatedAt}));return JSON.stringify({count:m.length,issues:m})}case"linear_get_issue":{let e=t?.issueId||t?.identifier||t?.issueKey;if(!e)return JSON.stringify({error:"issueId or identifier is required"});let r=await ae(e);return JSON.stringify(r?{id:r.id,identifier:r.identifier,number:r.number,title:r.title,description:r.description||"",url:r.url,priority:r.priority,state:r.state?.name,stateId:r.state?.id,stateType:r.state?.type,assignee:r.assignee?.displayName||r.assignee?.name||null,assigneeId:r.assignee?.id||null,labels:(r.labels?.nodes||[]).map(i=>i.name),team:r.team?{id:r.team.id,key:r.team.key,name:r.team.name}:null,createdAt:r.createdAt,updatedAt:r.updatedAt}:{error:`Issue not found: ${e}`})}case"linear_get_comments":{let e=t?.issueId||t?.identifier||t?.issueKey;if(!e)return JSON.stringify({error:"issueId or identifier is required"});let r=await ae(e,`
209
+ `,{first:u||30,filter:Object.keys(l).length?l:void 0,orderBy:"updatedAt"}))?.issues?.nodes||[]).map(f=>({id:f.id,identifier:f.identifier,number:f.number,title:f.title,url:f.url,priority:f.priority,state:f.state?.name,stateType:f.state?.type,assignee:f.assignee?.displayName||null,labels:(f.labels?.nodes||[]).map(y=>y.name),team:f.team?.key,createdAt:f.createdAt,updatedAt:f.updatedAt}));return JSON.stringify({count:m.length,issues:m})}case"linear_get_issue":{let e=t?.issueId||t?.identifier||t?.issueKey;if(!e)return JSON.stringify({error:"issueId or identifier is required"});let r=await ce(e);return JSON.stringify(r?{id:r.id,identifier:r.identifier,number:r.number,title:r.title,description:r.description||"",url:r.url,priority:r.priority,state:r.state?.name,stateId:r.state?.id,stateType:r.state?.type,assignee:r.assignee?.displayName||r.assignee?.name||null,assigneeId:r.assignee?.id||null,labels:(r.labels?.nodes||[]).map(i=>i.name),team:r.team?{id:r.team.id,key:r.team.key,name:r.team.name}:null,createdAt:r.createdAt,updatedAt:r.updatedAt}:{error:`Issue not found: ${e}`})}case"linear_get_comments":{let e=t?.issueId||t?.identifier||t?.issueKey;if(!e)return JSON.stringify({error:"issueId or identifier is required"});let r=await ce(e,`
202
210
  id identifier
203
211
  comments(first: ${Number(t?.limit)||50}) {
204
212
  nodes { id body createdAt updatedAt user { id name displayName } }
205
213
  }
206
- `);if(!r)return JSON.stringify({error:`Issue not found: ${e}`});let i=(r.comments?.nodes||[]).map(n=>({id:n.id,author:n.user?.displayName||n.user?.name||"Unknown",body:n.body||"",createdAt:n.createdAt,updatedAt:n.updatedAt})).sort((n,o)=>String(o.createdAt).localeCompare(String(n.createdAt)));return JSON.stringify({count:i.length,issue:r.identifier,comments:i})}case"linear_add_comment":{let e=t?.issueId||t?.identifier||t?.issueKey,r=t?.body;if(!e||!r)return JSON.stringify({error:"issueId/identifier and body are required"});let i=await ae(e,"id identifier");if(!i)return JSON.stringify({error:`Issue not found: ${e}`});let o=(await U(`
214
+ `);if(!r)return JSON.stringify({error:`Issue not found: ${e}`});let i=(r.comments?.nodes||[]).map(n=>({id:n.id,author:n.user?.displayName||n.user?.name||"Unknown",body:n.body||"",createdAt:n.createdAt,updatedAt:n.updatedAt})).sort((n,o)=>String(o.createdAt).localeCompare(String(n.createdAt)));return JSON.stringify({count:i.length,issue:r.identifier,comments:i})}case"linear_add_comment":{let e=t?.issueId||t?.identifier||t?.issueKey,r=t?.body;if(!e||!r)return JSON.stringify({error:"issueId/identifier and body are required"});let i=await ce(e,"id identifier");if(!i)return JSON.stringify({error:`Issue not found: ${e}`});let o=(await M(`
207
215
  mutation AddComment($input: CommentCreateInput!) {
208
216
  commentCreate(input: $input) {
209
217
  success
210
218
  comment { id url createdAt }
211
219
  }
212
220
  }
213
- `,{input:{issueId:i.id,body:r}}))?.commentCreate;return JSON.stringify({ok:!!o?.success,commentId:o?.comment?.id,url:o?.comment?.url})}case"linear_update_state":{let e=t?.issueId||t?.identifier||t?.issueKey,{stateId:r,stateName:i,toStatus:n,status:o}=t||{};if(!e)return JSON.stringify({error:"issueId or identifier is required"});let c=await ae(e,`
221
+ `,{input:{issueId:i.id,body:r}}))?.commentCreate;return JSON.stringify({ok:!!o?.success,commentId:o?.comment?.id,url:o?.comment?.url})}case"linear_update_state":{let e=t?.issueId||t?.identifier||t?.issueKey,{stateId:r,stateName:i,toStatus:n,status:o}=t||{};if(!e)return JSON.stringify({error:"issueId or identifier is required"});let c=await ce(e,`
214
222
  id identifier
215
223
  state { id name type }
216
224
  team { id key states { nodes { id name type position } } }
217
- `);if(!c)return JSON.stringify({error:`Issue not found: ${e}`});let a=r,u=r?{strategy:"explicit-id"}:null;if(!a){let d=String(i||n||o||"").trim(),m=(c.team?.states?.nodes||[]).slice().sort((y,_)=>(y.position||0)-(_.position||0));if(!d)return JSON.stringify({ok:!1,error:"stateId or stateName/toStatus is required",issue:c.identifier,availableStates:m.map(y=>({id:y.id,name:y.name,type:y.type}))});let f=Ss(m,d);if(!f.state)return JSON.stringify({ok:!1,error:`No workflow state matches "${d}" in team ${c.team?.key}`,issue:c.identifier,availableStates:m.map(y=>({id:y.id,name:y.name,type:y.type}))});a=f.state.id,u={strategy:f.strategy,matchedName:f.state.name}}let p=(await U(`
225
+ `);if(!c)return JSON.stringify({error:`Issue not found: ${e}`});let a=r,u=r?{strategy:"explicit-id"}:null;if(!a){let p=String(i||n||o||"").trim(),m=(c.team?.states?.nodes||[]).slice().sort((y,_)=>(y.position||0)-(_.position||0));if(!p)return JSON.stringify({ok:!1,error:"stateId or stateName/toStatus is required",issue:c.identifier,availableStates:m.map(y=>({id:y.id,name:y.name,type:y.type}))});let f=$s(m,p);if(!f.state)return JSON.stringify({ok:!1,error:`No workflow state matches "${p}" in team ${c.team?.key}`,issue:c.identifier,availableStates:m.map(y=>({id:y.id,name:y.name,type:y.type}))});a=f.state.id,u={strategy:f.strategy,matchedName:f.state.name}}let d=(await M(`
218
226
  mutation MoveIssue($id: String!, $input: IssueUpdateInput!) {
219
227
  issueUpdate(id: $id, input: $input) {
220
228
  success
221
229
  issue { id identifier state { id name type } }
222
230
  }
223
231
  }
224
- `,{id:c.id,input:{stateId:a}}))?.issueUpdate;return JSON.stringify({ok:!!p?.success,issue:p?.issue?.identifier||c.identifier,stateAfter:p?.issue?.state?.name||null,stateTypeAfter:p?.issue?.state?.type||null,resolution:u})}case"linear_link_attachment":{let e=t?.issueId||t?.identifier||t?.issueKey,{url:r,title:i,subtitle:n}=t||{};if(!e||!r)return JSON.stringify({error:"issueId/identifier and url are required"});let o=await ae(e,"id identifier");if(!o)return JSON.stringify({error:`Issue not found: ${e}`});let a=(await U(`
232
+ `,{id:c.id,input:{stateId:a}}))?.issueUpdate;return JSON.stringify({ok:!!d?.success,issue:d?.issue?.identifier||c.identifier,stateAfter:d?.issue?.state?.name||null,stateTypeAfter:d?.issue?.state?.type||null,resolution:u})}case"linear_link_attachment":{let e=t?.issueId||t?.identifier||t?.issueKey,{url:r,title:i,subtitle:n}=t||{};if(!e||!r)return JSON.stringify({error:"issueId/identifier and url are required"});let o=await ce(e,"id identifier");if(!o)return JSON.stringify({error:`Issue not found: ${e}`});let a=(await M(`
225
233
  mutation LinkAttachment($input: AttachmentCreateInput!) {
226
234
  attachmentCreate(input: $input) {
227
235
  success
228
236
  attachment { id url title }
229
237
  }
230
238
  }
231
- `,{input:{issueId:o.id,url:r,title:i||r,subtitle:n||void 0}}))?.attachmentCreate;return JSON.stringify({ok:!!a?.success,attachmentId:a?.attachment?.id,url:a?.attachment?.url})}default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(e){return JSON.stringify({error:e.message})}},tools:[{name:"linear_list_teams",description:"List Linear teams (id, key, name). Needed to scope workflow states and issue queries.",input_schema:{type:"object",properties:{limit:{type:"number",description:"Max teams (default: 50)"}}}},{name:"linear_list_states",description:"List a team's workflow states (id, name, type: backlog|unstarted|started|completed|canceled|triage). Linear states are PER-TEAM. Omit team to list all states across the workspace.",input_schema:{type:"object",properties:{teamId:{type:"string",description:"Team uuid"},teamKey:{type:"string",description:"Team key (e.g. ENG); resolved to an id if teamId omitted"},limit:{type:"number",description:"Max states when listing workspace-wide (default: 200)"}}}},{name:"linear_list_labels",description:"List issue labels, optionally scoped to a team.",input_schema:{type:"object",properties:{teamId:{type:"string",description:"Optional team uuid to scope labels"},limit:{type:"number",description:"Max labels (default: 100)"}}}},{name:"linear_list_issues",description:"List/poll Linear issues filtered by team, state, label, assignee, and an updatedAfter cursor. Returns newest-updated first.",input_schema:{type:"object",properties:{teamId:{type:"string",description:"Team uuid"},teamKey:{type:"string",description:"Team key (e.g. ENG); resolved if teamId omitted"},stateId:{type:"string",description:"Filter by workflow state uuid"},stateName:{type:"string",description:"Filter by state name (case-insensitive)"},label:{type:"string",description:"Filter by label name (case-insensitive)"},assigneeId:{type:"string",description:"Filter by assignee uuid"},updatedAfter:{type:"string",description:"ISO-8601 timestamp; only issues updated after this (polling cursor)"},limit:{type:"number",description:"Max issues (default: 30)"}}}},{name:"linear_get_issue",description:"Get a single Linear issue by identifier (e.g. ENG-123) or internal uuid \u2014 title, description, state, labels, assignee, url.",input_schema:{type:"object",properties:{identifier:{type:"string",description:"Issue identifier, e.g. ENG-123"},issueId:{type:"string",description:"Internal issue uuid (alternative to identifier)"}}}},{name:"linear_get_comments",description:"Get comments on a Linear issue (newest first).",input_schema:{type:"object",properties:{identifier:{type:"string",description:"Issue identifier, e.g. ENG-123"},issueId:{type:"string",description:"Internal issue uuid (alternative to identifier)"},limit:{type:"number",description:"Max comments (default: 50)"}}}},{name:"linear_add_comment",description:"Add a comment to a Linear issue (markdown supported).",input_schema:{type:"object",properties:{identifier:{type:"string",description:"Issue identifier, e.g. ENG-123"},issueId:{type:"string",description:"Internal issue uuid (alternative to identifier)"},body:{type:"string",description:"Comment body (markdown)"}},required:["body"]}},{name:"linear_update_state",description:"Move a Linear issue to a different workflow state. Pass a state NAME (toStatus/stateName) and the tool resolves it to the issue's team's matching state id (exact -> type-alias -> fuzzy), or pass stateId directly. Linear has no transitions \u2014 this sets the state.",input_schema:{type:"object",properties:{identifier:{type:"string",description:"Issue identifier, e.g. ENG-123"},issueId:{type:"string",description:"Internal issue uuid (alternative to identifier)"},stateId:{type:"string",description:"Target workflow state uuid (skips name resolution)"},stateName:{type:"string",description:'Target state name (e.g. "In Progress", "Done")'},toStatus:{type:"string",description:"Alias for stateName"}}}},{name:"linear_link_attachment",description:"Attach a URL (e.g. a GitHub PR) to a Linear issue via native attachments. Use this for PR links; fall back to linear_add_comment if it fails.",input_schema:{type:"object",properties:{identifier:{type:"string",description:"Issue identifier, e.g. ENG-123"},issueId:{type:"string",description:"Internal issue uuid (alternative to identifier)"},url:{type:"string",description:"The URL to attach (e.g. a PR link)"},title:{type:"string",description:"Attachment title (defaults to the URL)"},subtitle:{type:"string",description:"Optional attachment subtitle"}},required:["url"]}}]};async function pt(s){return(await U(`
239
+ `,{input:{issueId:o.id,url:r,title:i||r,subtitle:n||void 0}}))?.attachmentCreate;return JSON.stringify({ok:!!a?.success,attachmentId:a?.attachment?.id,url:a?.attachment?.url})}default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(e){return JSON.stringify({error:e.message})}},tools:[{name:"linear_list_teams",description:"List Linear teams (id, key, name). Needed to scope workflow states and issue queries.",input_schema:{type:"object",properties:{limit:{type:"number",description:"Max teams (default: 50)"}}}},{name:"linear_list_states",description:"List a team's workflow states (id, name, type: backlog|unstarted|started|completed|canceled|triage). Linear states are PER-TEAM. Omit team to list all states across the workspace.",input_schema:{type:"object",properties:{teamId:{type:"string",description:"Team uuid"},teamKey:{type:"string",description:"Team key (e.g. ENG); resolved to an id if teamId omitted"},limit:{type:"number",description:"Max states when listing workspace-wide (default: 200)"}}}},{name:"linear_list_labels",description:"List issue labels, optionally scoped to a team.",input_schema:{type:"object",properties:{teamId:{type:"string",description:"Optional team uuid to scope labels"},limit:{type:"number",description:"Max labels (default: 100)"}}}},{name:"linear_list_issues",description:"List/poll Linear issues filtered by team, state, label, assignee, and an updatedAfter cursor. Returns newest-updated first.",input_schema:{type:"object",properties:{teamId:{type:"string",description:"Team uuid"},teamKey:{type:"string",description:"Team key (e.g. ENG); resolved if teamId omitted"},stateId:{type:"string",description:"Filter by workflow state uuid"},stateName:{type:"string",description:"Filter by state name (case-insensitive)"},label:{type:"string",description:"Filter by label name (case-insensitive)"},assigneeId:{type:"string",description:"Filter by assignee uuid"},updatedAfter:{type:"string",description:"ISO-8601 timestamp; only issues updated after this (polling cursor)"},limit:{type:"number",description:"Max issues (default: 30)"}}}},{name:"linear_get_issue",description:"Get a single Linear issue by identifier (e.g. ENG-123) or internal uuid \u2014 title, description, state, labels, assignee, url.",input_schema:{type:"object",properties:{identifier:{type:"string",description:"Issue identifier, e.g. ENG-123"},issueId:{type:"string",description:"Internal issue uuid (alternative to identifier)"}}}},{name:"linear_get_comments",description:"Get comments on a Linear issue (newest first).",input_schema:{type:"object",properties:{identifier:{type:"string",description:"Issue identifier, e.g. ENG-123"},issueId:{type:"string",description:"Internal issue uuid (alternative to identifier)"},limit:{type:"number",description:"Max comments (default: 50)"}}}},{name:"linear_add_comment",description:"Add a comment to a Linear issue (markdown supported).",input_schema:{type:"object",properties:{identifier:{type:"string",description:"Issue identifier, e.g. ENG-123"},issueId:{type:"string",description:"Internal issue uuid (alternative to identifier)"},body:{type:"string",description:"Comment body (markdown)"}},required:["body"]}},{name:"linear_update_state",description:"Move a Linear issue to a different workflow state. Pass a state NAME (toStatus/stateName) and the tool resolves it to the issue's team's matching state id (exact -> type-alias -> fuzzy), or pass stateId directly. Linear has no transitions \u2014 this sets the state.",input_schema:{type:"object",properties:{identifier:{type:"string",description:"Issue identifier, e.g. ENG-123"},issueId:{type:"string",description:"Internal issue uuid (alternative to identifier)"},stateId:{type:"string",description:"Target workflow state uuid (skips name resolution)"},stateName:{type:"string",description:'Target state name (e.g. "In Progress", "Done")'},toStatus:{type:"string",description:"Alias for stateName"}}}},{name:"linear_link_attachment",description:"Attach a URL (e.g. a GitHub PR) to a Linear issue via native attachments. Use this for PR links; fall back to linear_add_comment if it fails.",input_schema:{type:"object",properties:{identifier:{type:"string",description:"Issue identifier, e.g. ENG-123"},issueId:{type:"string",description:"Internal issue uuid (alternative to identifier)"},url:{type:"string",description:"The URL to attach (e.g. a PR link)"},title:{type:"string",description:"Attachment title (defaults to the URL)"},subtitle:{type:"string",description:"Optional attachment subtitle"}},required:["url"]}}]};async function yt(s){return(await M(`
232
240
  query TeamByKey($filter: TeamFilter) {
233
241
  teams(first: 1, filter: $filter) { nodes { id key } }
234
242
  }
235
- `,{filter:{key:{eq:s}}}))?.teams?.nodes?.[0]?.id||null}async function ae(s,t=vs){let e=String(s).trim(),r=/^([A-Za-z][A-Za-z0-9]*)-(\d+)$/.exec(e);if(r){let n=r[1].toUpperCase(),o=Number(r[2]);return(await U(`
243
+ `,{filter:{key:{eq:s}}}))?.teams?.nodes?.[0]?.id||null}async function ce(s,t=js){let e=String(s).trim(),r=/^([A-Za-z][A-Za-z0-9]*)-(\d+)$/.exec(e);if(r){let n=r[1].toUpperCase(),o=Number(r[2]);return(await M(`
236
244
  query IssueByIdentifier($filter: IssueFilter) {
237
245
  issues(first: 1, filter: $filter) {
238
246
  nodes { ${t} }
239
247
  }
240
248
  }
241
- `,{filter:{number:{eq:o},team:{key:{eq:n}}}}))?.issues?.nodes?.[0]||null}return(await U(`
249
+ `,{filter:{number:{eq:o},team:{key:{eq:n}}}}))?.issues?.nodes?.[0]||null}return(await M(`
242
250
  query IssueById($id: String!) {
243
251
  issue(id: $id) { ${t} }
244
252
  }
245
- `,{id:e}))?.issue||null}var mt={id:"plane",serverName:"plane",allowedTools:["mcp__plane__*"],requiresIntegration:R.PLANE,envKeys:["PLANE_API_KEY","PLANE_WORKSPACE_SLUG","PLANE_BASE_URL"],description:"Plane \u2014 projects, work items, cycles, modules, epics, comments (official MCP, API key)",tools:[],promptFragment:`## Plane (connected)
253
+ `,{id:e}))?.issue||null}var gt={id:"plane",serverName:"plane",allowedTools:["mcp__plane__*"],requiresIntegration:A.PLANE,envKeys:["PLANE_API_KEY","PLANE_WORKSPACE_SLUG","PLANE_BASE_URL"],description:"Plane \u2014 projects, work items, cycles, modules, epics, comments (official MCP, API key)",tools:[],promptFragment:`## Plane (connected)
246
254
  You have direct access to the user's Plane workspace via the official Plane MCP server. All Plane tools are available under the mcp__plane__* namespace \u2014 use them proactively to read and write projects, work items (issues), cycles, modules, epics, sub-issues, comments, labels, states, pages, and workspace data.
247
255
 
248
256
  - List/get projects and work items, then create/update/delete or search work items as needed.
249
257
  - For status changes, read the project's available states first, then set the work item's state.
250
258
  - Cycles and modules group work items \u2014 list them to scope queries before drilling into items.
251
- - Always operate within the connected workspace; the workspace slug and base URL are pre-configured (Plane Cloud, self-hosted, or Zibby-hosted all work transparently).`,resolve(){let s={};for(let t of this.envKeys)process.env[t]&&(s[t]=process.env[t]);return{type:"stdio",command:"uvx",args:["plane-mcp-server","stdio"],env:s,description:this.description}}};import{existsSync as Ns}from"fs";import{fileURLToPath as Is}from"url";import{dirname as Os,resolve as Rs}from"path";import{resolveIntegrationToken as As}from"@zibby/core/backend-client.js";function $s(){if(process.env.MCP_SLACK_PATH)return process.env.MCP_SLACK_PATH;let s=Os(Is(import.meta.url)),t=Rs(s,"..","bin","mcp-slack.mjs");return Ns(t)?t:null}async function J(s,t={}){let{token:e}=await As("slack"),r=["conversations.list","users.list","users.profile.get","users.lookupByEmail","usergroups.list","usergroups.users.list","conversations.history","conversations.replies"].includes(s),i=`https://slack.com/api/${s}`,n={Authorization:`Bearer ${e}`},o;if(r){let u=new URLSearchParams(t).toString();u&&(i+=`?${u}`)}else n["Content-Type"]="application/json; charset=utf-8",o=JSON.stringify(t);let a=await(await fetch(i,{method:r?"GET":"POST",headers:n,body:o})).json();if(!a.ok)throw new Error(`Slack API error: ${a.error}`);return a}var q={id:"slack",serverName:"slack",allowedTools:["mcp__slack__*"],requiresIntegration:R.SLACK,envKeys:["SLACK_BOT_TOKEN","SLACK_TEAM_ID"],description:"Slack MCP Server",promptFragment:`## Slack (connected)
259
+ - Always operate within the connected workspace; the workspace slug and base URL are pre-configured (Plane Cloud, self-hosted, or Zibby-hosted all work transparently).`,resolve(){let s={};for(let t of this.envKeys)process.env[t]&&(s[t]=process.env[t]);return{type:"stdio",command:"uvx",args:["plane-mcp-server","stdio"],env:s,description:this.description}}};import{existsSync as Ts}from"fs";import{fileURLToPath as Es}from"url";import{dirname as Ls,resolve as xs}from"path";import{resolveIntegrationToken as Cs}from"@zibby/core/backend-client.js";function Ps(){if(process.env.MCP_SLACK_PATH)return process.env.MCP_SLACK_PATH;let s=Ls(Es(import.meta.url)),t=xs(s,"..","bin","mcp-slack.mjs");return Ts(t)?t:null}async function U(s,t={}){let{token:e}=await Cs("slack"),r=["conversations.list","users.list","users.profile.get","users.lookupByEmail","usergroups.list","usergroups.users.list","conversations.history","conversations.replies"].includes(s),i=`https://slack.com/api/${s}`,n={Authorization:`Bearer ${e}`},o;if(r){let u=new URLSearchParams(t).toString();u&&(i+=`?${u}`)}else n["Content-Type"]="application/json; charset=utf-8",o=JSON.stringify(t);let a=await(await fetch(i,{method:r?"GET":"POST",headers:n,body:o})).json();if(!a.ok)throw new Error(`Slack API error: ${a.error}`);return a}var D={id:"slack",serverName:"slack",allowedTools:["mcp__slack__*"],requiresIntegration:A.SLACK,envKeys:["SLACK_BOT_TOKEN","SLACK_TEAM_ID"],description:"Slack MCP Server",promptFragment:`## Slack (connected)
252
260
  You have access to the user's Slack workspace. Use these tools:
253
261
  - slack_list_channels, slack_post_message, slack_reply_to_thread
254
262
  - slack_add_reaction, slack_get_channel_history, slack_get_thread_replies
255
263
  - slack_get_users, slack_get_user_profile
256
264
  - slack_lookup_user_by_email (precise email\u2192user_id, prefer this over scanning slack_get_users)
257
- - slack_list_usergroups, slack_get_usergroup_members (workspace-defined teams like @oncall, @platform)`,resolve(){let s=$s();if(!s)return null;let t={};for(let e of["PROJECT_API_TOKEN","ZIBBY_USER_TOKEN","ZIBBY_ACCOUNT_API_URL","ZIBBY_ENV","ZIBBY_PROD_ACCOUNT_API_URL","PROGRESS_API_URL","EXECUTION_ID","PROJECT_ID","STAGE"])process.env[e]&&(t[e]=process.env[e]);for(let e of this.envKeys)process.env[e]&&(t[e]=process.env[e]);return{type:"stdio",command:"node",args:[s],env:t,alwaysLoad:!0}},async handleToolCall(s,t){try{switch(s){case"slack_list_channels":{let e=await J("conversations.list",{types:"public_channel",limit:100});return JSON.stringify({channels:(e.channels||[]).map(r=>({id:r.id,name:r.name,topic:r.topic?.value}))})}case"slack_post_message":{if(!t.channel||!t.text)return JSON.stringify({error:"channel and text are required"});let e=await J("chat.postMessage",{channel:t.channel,text:t.text,...t.blocks?{blocks:t.blocks}:{}});return JSON.stringify({ok:!0,ts:e.ts,channel:e.channel})}case"slack_reply_to_thread":{if(!t.channel||!t.thread_ts||!t.text)return JSON.stringify({error:"channel, thread_ts, and text are required"});let e=await J("chat.postMessage",{channel:t.channel,thread_ts:t.thread_ts,text:t.text});return JSON.stringify({ok:!0,ts:e.ts})}case"slack_add_reaction":return!t.channel||!t.timestamp||!t.reaction?JSON.stringify({error:"channel, timestamp, and reaction are required"}):(await J("reactions.add",{channel:t.channel,timestamp:t.timestamp,name:t.reaction}),JSON.stringify({ok:!0}));case"slack_get_channel_history":{if(!t.channel)return JSON.stringify({error:"channel is required"});let e=await J("conversations.history",{channel:t.channel,limit:t.limit||20});return JSON.stringify({messages:(e.messages||[]).map(r=>({user:r.user,text:r.text,ts:r.ts}))})}case"slack_get_thread_replies":{if(!t.channel||!t.thread_ts)return JSON.stringify({error:"channel and thread_ts are required"});let e=await J("conversations.replies",{channel:t.channel,ts:t.thread_ts});return JSON.stringify({messages:(e.messages||[]).map(r=>({user:r.user,text:r.text,ts:r.ts}))})}case"slack_get_users":{let e=await J("users.list",{limit:100});return JSON.stringify({users:(e.members||[]).filter(r=>!r.is_bot&&!r.deleted).map(r=>({id:r.id,name:r.real_name||r.name}))})}case"slack_get_user_profile":{if(!t.user_id)return JSON.stringify({error:"user_id is required"});let e=await J("users.profile.get",{user:t.user_id});return JSON.stringify({profile:e.profile})}case"slack_lookup_user_by_email":{if(!t.email)return JSON.stringify({error:"email is required"});try{let e=await J("users.lookupByEmail",{email:t.email});return JSON.stringify({ok:!0,user:{id:e.user?.id,name:e.user?.real_name||e.user?.name,email:e.user?.profile?.email||t.email}})}catch(e){if(/users_not_found/.test(e.message))return JSON.stringify({ok:!1,reason:"users_not_found"});throw e}}case"slack_list_usergroups":{let e=await J("usergroups.list",{});return JSON.stringify({usergroups:(e.usergroups||[]).map(r=>({id:r.id,handle:r.handle,name:r.name,description:r.description||"",user_count:Number(r.user_count||0)}))})}case"slack_get_usergroup_members":{if(!t.usergroup)return JSON.stringify({error:"usergroup id is required"});let e=await J("usergroups.users.list",{usergroup:t.usergroup});return JSON.stringify({users:e.users||[]})}case"slack_search_users":{if(!t.query||typeof t.query!="string")return JSON.stringify({error:"query is required"});let e=t.query.trim().toLowerCase();if(!e)return JSON.stringify({ok:!0,matches:[]});let r=Math.max(1,Math.min(Number(t.limit)||5,25)),i=[],n,o=5;for(let a=0;a<o;a+=1){let u={limit:200};n&&(u.cursor=n);let l=await J("users.list",u);for(let p of l.members||[])p.deleted||p.is_bot||i.push(p);if(n=l.response_metadata?.next_cursor,!n)break}let c=[];for(let a of i){let u=(a.real_name||"").toLowerCase(),l=(a.profile?.display_name||"").toLowerCase(),p=(a.name||"").toLowerCase(),d=0;u.includes(e)&&(d+=100-Math.abs(u.length-e.length)),l.includes(e)&&(d+=60-Math.abs(l.length-e.length)),p.includes(e)&&(d+=30-Math.abs(p.length-e.length)),(u===e||l===e)&&(d+=200),d>0&&c.push({id:a.id,name:a.real_name||a.profile?.display_name||a.name,email:a.profile?.email||void 0,_score:d})}return c.sort((a,u)=>u._score-a._score),JSON.stringify({ok:!0,matches:c.slice(0,r).map(({_score:a,...u})=>u),scanned:i.length})}default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(e){return JSON.stringify({error:e.message})}},tools:[{name:"slack_list_channels",description:"List public channels in the workspace",input_schema:{type:"object",properties:{}}},{name:"slack_post_message",description:"Post a message to a Slack channel or DM. Pass `blocks` (Block Kit) for a rich card; `text` is the required notification fallback.",input_schema:{type:"object",properties:{channel:{type:"string",description:"Channel ID or name"},text:{type:"string",description:"Notification/fallback text (required)"},blocks:{type:"array",description:"Block Kit blocks for rich formatting (optional). Each block is a Slack Block Kit object (header/section/divider/context). section blocks may carry a button accessory with a url."}},required:["channel","text"]}},{name:"slack_reply_to_thread",description:"Reply to a specific message thread",input_schema:{type:"object",properties:{channel:{type:"string",description:"Channel ID"},thread_ts:{type:"string",description:"Thread timestamp"},text:{type:"string",description:"Reply text"}},required:["channel","thread_ts","text"]}},{name:"slack_add_reaction",description:"Add an emoji reaction to a message",input_schema:{type:"object",properties:{channel:{type:"string",description:"Channel ID"},timestamp:{type:"string",description:"Message timestamp"},reaction:{type:"string",description:"Emoji name without colons"}},required:["channel","timestamp","reaction"]}},{name:"slack_get_channel_history",description:"Get recent messages from a channel",input_schema:{type:"object",properties:{channel:{type:"string",description:"Channel ID"},limit:{type:"number",description:"Number of messages"}},required:["channel"]}},{name:"slack_get_thread_replies",description:"Get all replies in a message thread",input_schema:{type:"object",properties:{channel:{type:"string",description:"Channel ID"},thread_ts:{type:"string",description:"Thread timestamp"}},required:["channel","thread_ts"]}},{name:"slack_get_users",description:"List workspace users with basic profiles",input_schema:{type:"object",properties:{}}},{name:"slack_get_user_profile",description:"Get detailed profile for a specific user",input_schema:{type:"object",properties:{user_id:{type:"string",description:"Slack user ID"}},required:["user_id"]}},{name:"slack_lookup_user_by_email",description:"Find a Slack user by email. Returns { ok:true, user:{id,name,email} } on hit, { ok:false } when no user has that email. Prefer this over slack_get_users for email-based routing \u2014 single API call, exact match.",input_schema:{type:"object",properties:{email:{type:"string",description:"Email address to look up"}},required:["email"]}},{name:"slack_list_usergroups",description:"List workspace-defined user groups (e.g. @oncall, @platform). Each item has { id, handle, name, description, user_count }. Use the id with slack_get_usergroup_members to expand the membership.",input_schema:{type:"object",properties:{}}},{name:"slack_get_usergroup_members",description:"List user IDs that belong to a Slack usergroup. Pair with slack_post_message to DM each member, or use the group id directly in a channel message as <!subteam^ID> to @-mention.",input_schema:{type:"object",properties:{usergroup:{type:"string",description:"Usergroup id, e.g. S012ABC"}},required:["usergroup"]}},{name:"slack_search_users",description:'Fuzzy-search workspace users by display name or real name. Use when the user said something like "send to Sam" without an email. Returns up to `limit` ranked matches { id, name, email }. Slack has no native name-search API \u2014 this scans paginated users.list + does substring scoring (real_name > display_name > name). For large workspaces consider higher limit + ask the user to confirm if multiple hit.',input_schema:{type:"object",properties:{query:{type:"string",description:"Substring to match against names (case-insensitive)"},limit:{type:"number",description:"Max matches to return (default 5, max 25)"}},required:["query"]}}]};import{existsSync as js}from"fs";import{fileURLToPath as Ts}from"url";import{dirname as Es,resolve as Ls}from"path";import{resolveIntegrationToken as xs}from"@zibby/core/backend-client.js";function Cs(){if(process.env.MCP_LARK_PATH)return process.env.MCP_LARK_PATH;let s=Es(Ts(import.meta.url)),t=Ls(s,"..","bin","mcp-lark.mjs");return js(t)?t:null}var Ps=6e3*1e3,le=null;async function Js(){let{appId:s,appSecret:t,host:e}=await xs("lark");if(le&&le.appId===s&&le.expiresAt>Date.now())return{token:le.token,host:e};let i=await(await fetch(`${e}/open-apis/auth/v3/tenant_access_token/internal`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({app_id:s,app_secret:t})})).json();if(i.code!==0)throw new Error(`Lark tenant_access_token failed: ${i.msg||i.code}`);return le={token:i.tenant_access_token,expiresAt:Date.now()+Ps,appId:s},{token:i.tenant_access_token,host:e}}async function Q(s,t,e={}){let{token:r,host:i}=await Js(),n=`${i}${t}`,o={method:s,headers:{Authorization:`Bearer ${r}`,"Content-Type":"application/json; charset=utf-8"}};s!=="GET"&&(o.body=JSON.stringify(e));let a=await(await fetch(n,o)).json();if(a.code!==0)throw new Error(`Lark API ${t} error: ${a.msg||a.code}`);return a.data||{}}function ft(s){return JSON.stringify({text:s})}function Us(s){return!s||typeof s!="string"||s.startsWith("oc_")?"chat_id":s.startsWith("ou_")?"open_id":s.startsWith("on_")?"union_id":s.startsWith("cli_")?"app_id":s.includes("@")?"email":"chat_id"}var B={id:"lark",serverName:"lark",allowedTools:["mcp__lark__*"],requiresIntegration:R.LARK,description:"Lark / Feishu messaging \u2014 send messages and reply in threads.",envKeys:[],promptFragment:`## Lark (connected)
265
+ - slack_list_usergroups, slack_get_usergroup_members (workspace-defined teams like @oncall, @platform)`,resolve(){let s=Ps();if(!s)return null;let t={};for(let e of["PROJECT_API_TOKEN","ZIBBY_USER_TOKEN","ZIBBY_ACCOUNT_API_URL","ZIBBY_ENV","ZIBBY_PROD_ACCOUNT_API_URL","PROGRESS_API_URL","EXECUTION_ID","PROJECT_ID","STAGE"])process.env[e]&&(t[e]=process.env[e]);for(let e of this.envKeys)process.env[e]&&(t[e]=process.env[e]);return{type:"stdio",command:"node",args:[s],env:t,alwaysLoad:!0}},async handleToolCall(s,t){try{switch(s){case"slack_list_channels":{let e=await U("conversations.list",{types:"public_channel",limit:100});return JSON.stringify({channels:(e.channels||[]).map(r=>({id:r.id,name:r.name,topic:r.topic?.value}))})}case"slack_post_message":{if(!t.channel||!t.text)return JSON.stringify({error:"channel and text are required"});let e=await U("chat.postMessage",{channel:t.channel,text:t.text,...t.blocks?{blocks:t.blocks}:{}});return JSON.stringify({ok:!0,ts:e.ts,channel:e.channel})}case"slack_reply_to_thread":{if(!t.channel||!t.thread_ts||!t.text)return JSON.stringify({error:"channel, thread_ts, and text are required"});let e=await U("chat.postMessage",{channel:t.channel,thread_ts:t.thread_ts,text:t.text});return JSON.stringify({ok:!0,ts:e.ts})}case"slack_add_reaction":return!t.channel||!t.timestamp||!t.reaction?JSON.stringify({error:"channel, timestamp, and reaction are required"}):(await U("reactions.add",{channel:t.channel,timestamp:t.timestamp,name:t.reaction}),JSON.stringify({ok:!0}));case"slack_get_channel_history":{if(!t.channel)return JSON.stringify({error:"channel is required"});let e=await U("conversations.history",{channel:t.channel,limit:t.limit||20});return JSON.stringify({messages:(e.messages||[]).map(r=>({user:r.user,text:r.text,ts:r.ts}))})}case"slack_get_thread_replies":{if(!t.channel||!t.thread_ts)return JSON.stringify({error:"channel and thread_ts are required"});let e=await U("conversations.replies",{channel:t.channel,ts:t.thread_ts});return JSON.stringify({messages:(e.messages||[]).map(r=>({user:r.user,text:r.text,ts:r.ts}))})}case"slack_get_users":{let e=await U("users.list",{limit:100});return JSON.stringify({users:(e.members||[]).filter(r=>!r.is_bot&&!r.deleted).map(r=>({id:r.id,name:r.real_name||r.name}))})}case"slack_get_user_profile":{if(!t.user_id)return JSON.stringify({error:"user_id is required"});let e=await U("users.profile.get",{user:t.user_id});return JSON.stringify({profile:e.profile})}case"slack_lookup_user_by_email":{if(!t.email)return JSON.stringify({error:"email is required"});try{let e=await U("users.lookupByEmail",{email:t.email});return JSON.stringify({ok:!0,user:{id:e.user?.id,name:e.user?.real_name||e.user?.name,email:e.user?.profile?.email||t.email}})}catch(e){if(/users_not_found/.test(e.message))return JSON.stringify({ok:!1,reason:"users_not_found"});throw e}}case"slack_list_usergroups":{let e=await U("usergroups.list",{});return JSON.stringify({usergroups:(e.usergroups||[]).map(r=>({id:r.id,handle:r.handle,name:r.name,description:r.description||"",user_count:Number(r.user_count||0)}))})}case"slack_get_usergroup_members":{if(!t.usergroup)return JSON.stringify({error:"usergroup id is required"});let e=await U("usergroups.users.list",{usergroup:t.usergroup});return JSON.stringify({users:e.users||[]})}case"slack_search_users":{if(!t.query||typeof t.query!="string")return JSON.stringify({error:"query is required"});let e=t.query.trim().toLowerCase();if(!e)return JSON.stringify({ok:!0,matches:[]});let r=Math.max(1,Math.min(Number(t.limit)||5,25)),i=[],n,o=5;for(let a=0;a<o;a+=1){let u={limit:200};n&&(u.cursor=n);let l=await U("users.list",u);for(let d of l.members||[])d.deleted||d.is_bot||i.push(d);if(n=l.response_metadata?.next_cursor,!n)break}let c=[];for(let a of i){let u=(a.real_name||"").toLowerCase(),l=(a.profile?.display_name||"").toLowerCase(),d=(a.name||"").toLowerCase(),p=0;u.includes(e)&&(p+=100-Math.abs(u.length-e.length)),l.includes(e)&&(p+=60-Math.abs(l.length-e.length)),d.includes(e)&&(p+=30-Math.abs(d.length-e.length)),(u===e||l===e)&&(p+=200),p>0&&c.push({id:a.id,name:a.real_name||a.profile?.display_name||a.name,email:a.profile?.email||void 0,_score:p})}return c.sort((a,u)=>u._score-a._score),JSON.stringify({ok:!0,matches:c.slice(0,r).map(({_score:a,...u})=>u),scanned:i.length})}default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(e){return JSON.stringify({error:e.message})}},tools:[{name:"slack_list_channels",description:"List public channels in the workspace",input_schema:{type:"object",properties:{}}},{name:"slack_post_message",description:"Post a message to a Slack channel or DM. Pass `blocks` (Block Kit) for a rich card; `text` is the required notification fallback.",input_schema:{type:"object",properties:{channel:{type:"string",description:"Channel ID or name"},text:{type:"string",description:"Notification/fallback text (required)"},blocks:{type:"array",description:"Block Kit blocks for rich formatting (optional). Each block is a Slack Block Kit object (header/section/divider/context). section blocks may carry a button accessory with a url."}},required:["channel","text"]}},{name:"slack_reply_to_thread",description:"Reply to a specific message thread",input_schema:{type:"object",properties:{channel:{type:"string",description:"Channel ID"},thread_ts:{type:"string",description:"Thread timestamp"},text:{type:"string",description:"Reply text"}},required:["channel","thread_ts","text"]}},{name:"slack_add_reaction",description:"Add an emoji reaction to a message",input_schema:{type:"object",properties:{channel:{type:"string",description:"Channel ID"},timestamp:{type:"string",description:"Message timestamp"},reaction:{type:"string",description:"Emoji name without colons"}},required:["channel","timestamp","reaction"]}},{name:"slack_get_channel_history",description:"Get recent messages from a channel",input_schema:{type:"object",properties:{channel:{type:"string",description:"Channel ID"},limit:{type:"number",description:"Number of messages"}},required:["channel"]}},{name:"slack_get_thread_replies",description:"Get all replies in a message thread",input_schema:{type:"object",properties:{channel:{type:"string",description:"Channel ID"},thread_ts:{type:"string",description:"Thread timestamp"}},required:["channel","thread_ts"]}},{name:"slack_get_users",description:"List workspace users with basic profiles",input_schema:{type:"object",properties:{}}},{name:"slack_get_user_profile",description:"Get detailed profile for a specific user",input_schema:{type:"object",properties:{user_id:{type:"string",description:"Slack user ID"}},required:["user_id"]}},{name:"slack_lookup_user_by_email",description:"Find a Slack user by email. Returns { ok:true, user:{id,name,email} } on hit, { ok:false } when no user has that email. Prefer this over slack_get_users for email-based routing \u2014 single API call, exact match.",input_schema:{type:"object",properties:{email:{type:"string",description:"Email address to look up"}},required:["email"]}},{name:"slack_list_usergroups",description:"List workspace-defined user groups (e.g. @oncall, @platform). Each item has { id, handle, name, description, user_count }. Use the id with slack_get_usergroup_members to expand the membership.",input_schema:{type:"object",properties:{}}},{name:"slack_get_usergroup_members",description:"List user IDs that belong to a Slack usergroup. Pair with slack_post_message to DM each member, or use the group id directly in a channel message as <!subteam^ID> to @-mention.",input_schema:{type:"object",properties:{usergroup:{type:"string",description:"Usergroup id, e.g. S012ABC"}},required:["usergroup"]}},{name:"slack_search_users",description:'Fuzzy-search workspace users by display name or real name. Use when the user said something like "send to Sam" without an email. Returns up to `limit` ranked matches { id, name, email }. Slack has no native name-search API \u2014 this scans paginated users.list + does substring scoring (real_name > display_name > name). For large workspaces consider higher limit + ask the user to confirm if multiple hit.',input_schema:{type:"object",properties:{query:{type:"string",description:"Substring to match against names (case-insensitive)"},limit:{type:"number",description:"Max matches to return (default 5, max 25)"}},required:["query"]}}]};import{existsSync as Js}from"fs";import{fileURLToPath as Us}from"url";import{dirname as qs,resolve as Ms}from"path";import{resolveIntegrationToken as Ds}from"@zibby/core/backend-client.js";function Bs(){if(process.env.MCP_LARK_PATH)return process.env.MCP_LARK_PATH;let s=qs(Us(import.meta.url)),t=Ms(s,"..","bin","mcp-lark.mjs");return Js(t)?t:null}var Ks=6e3*1e3,ue=null;async function Gs(){let{appId:s,appSecret:t,host:e}=await Ds("lark");if(ue&&ue.appId===s&&ue.expiresAt>Date.now())return{token:ue.token,host:e};let i=await(await fetch(`${e}/open-apis/auth/v3/tenant_access_token/internal`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({app_id:s,app_secret:t})})).json();if(i.code!==0)throw new Error(`Lark tenant_access_token failed: ${i.msg||i.code}`);return ue={token:i.tenant_access_token,expiresAt:Date.now()+Ks,appId:s},{token:i.tenant_access_token,host:e}}async function ee(s,t,e={}){let{token:r,host:i}=await Gs(),n=`${i}${t}`,o={method:s,headers:{Authorization:`Bearer ${r}`,"Content-Type":"application/json; charset=utf-8"}};s!=="GET"&&(o.body=JSON.stringify(e));let a=await(await fetch(n,o)).json();if(a.code!==0)throw new Error(`Lark API ${t} error: ${a.msg||a.code}`);return a.data||{}}function _t(s){return JSON.stringify({text:s})}function Fs(s){return!s||typeof s!="string"||s.startsWith("oc_")?"chat_id":s.startsWith("ou_")?"open_id":s.startsWith("on_")?"union_id":s.startsWith("cli_")?"app_id":s.includes("@")?"email":"chat_id"}var K={id:"lark",serverName:"lark",allowedTools:["mcp__lark__*"],requiresIntegration:A.LARK,description:"Lark / Feishu messaging \u2014 send messages and reply in threads.",envKeys:[],promptFragment:`## Lark (connected)
258
266
  You can send messages and replies on Lark. Use:
259
267
  - lark_send_message: post a message to a chat, user, or DM
260
268
  - lark_reply: reply to an existing message (threaded)
261
269
  - lark_list_chats: list chats the bot is a member of
262
270
  - lark_get_chat_history: fetch recent messages in a chat
263
271
  - lark_lookup_user_by_email: resolve an email \u2192 open_id for direct DM (prefer this over emailing through lark_send_message when the agent has a user_id already)
264
- When responding to an incoming event, prefer lark_reply with the source message_id so the response threads cleanly.`,resolve(){let s=Cs();if(!s)return null;let t={};for(let e of["PROJECT_API_TOKEN","ZIBBY_USER_TOKEN","ZIBBY_ACCOUNT_API_URL","ZIBBY_ENV","ZIBBY_PROD_ACCOUNT_API_URL","PROGRESS_API_URL","EXECUTION_ID","PROJECT_ID","STAGE"])process.env[e]&&(t[e]=process.env[e]);return{type:"stdio",command:"node",args:[s],env:t,alwaysLoad:!0}},tools:[{name:"lark_send_message",description:"Send a text message to a Lark chat, user, or DM. receive_id can be a chat_id (oc_*), open_id (ou_*), union_id (on_*), or email.",input_schema:{type:"object",properties:{receive_id:{type:"string",description:"Target id: chat_id (oc_*), open_id (ou_*), union_id (on_*), or email"},text:{type:"string",description:"Message text"}},required:["receive_id","text"]}},{name:"lark_reply",description:"Reply to an existing Lark message (creates a thread). Use the message_id from the inbound event.",input_schema:{type:"object",properties:{message_id:{type:"string",description:"Lark message id (om_*) to reply to"},text:{type:"string",description:"Reply text"}},required:["message_id","text"]}},{name:"lark_list_chats",description:"List chats (groups + DMs) the bot is a member of.",input_schema:{type:"object",properties:{page_size:{type:"number",description:"Max results (default 50)"}}}},{name:"lark_get_chat_history",description:"Fetch recent messages in a chat.",input_schema:{type:"object",properties:{chat_id:{type:"string",description:"Chat id (oc_*)"},page_size:{type:"number",description:"Max messages (default 20)"}},required:["chat_id"]}},{name:"lark_lookup_user_by_email",description:"Resolve an email address to a Lark user id (open_id). Returns { ok:true, user:{open_id,email,name} } on hit, { ok:false } if no Lark user has that email. Use the open_id as `receive_id` in lark_send_message to DM.",input_schema:{type:"object",properties:{email:{type:"string",description:"Email address to look up"}},required:["email"]}},{name:"lark_search_users",description:'Fuzzy-search users by name across chats the bot is a member of. Lark has no public org-wide user search API for bots \u2014 this walks the bot\'s chat memberships and matches names client-side. Best for "send to Sam" style routing where you have a name but no email. Returns up to `limit` ranked matches { open_id, name }.',input_schema:{type:"object",properties:{query:{type:"string",description:"Substring to match against user names (case-insensitive)"},limit:{type:"number",description:"Max matches to return (default 5, max 25)"}},required:["query"]}}],async handleToolCall(s,t){try{switch(s){case"lark_send_message":{if(!t.receive_id||!t.text)return JSON.stringify({error:"receive_id and text are required"});let e=Us(t.receive_id),r=await Q("POST",`/open-apis/im/v1/messages?receive_id_type=${e}`,{receive_id:t.receive_id,msg_type:"text",content:ft(t.text)});return JSON.stringify({ok:!0,message_id:r.message_id})}case"lark_reply":{if(!t.message_id||!t.text)return JSON.stringify({error:"message_id and text are required"});let e=await Q("POST",`/open-apis/im/v1/messages/${encodeURIComponent(t.message_id)}/reply`,{msg_type:"text",content:ft(t.text)});return JSON.stringify({ok:!0,message_id:e.message_id})}case"lark_list_chats":{let e=t.page_size||50,i=((await Q("GET",`/open-apis/im/v1/chats?page_size=${e}`)).items||[]).map(n=>({chat_id:n.chat_id,name:n.name,description:n.description,owner_id:n.owner_id,chat_mode:n.chat_mode}));return JSON.stringify({chats:i})}case"lark_get_chat_history":{if(!t.chat_id)return JSON.stringify({error:"chat_id is required"});let e=t.page_size||20,i=((await Q("GET",`/open-apis/im/v1/messages?container_id_type=chat&container_id=${encodeURIComponent(t.chat_id)}&page_size=${e}&sort_type=ByCreateTimeDesc`)).items||[]).map(n=>({message_id:n.message_id,sender_id:n.sender?.id,sender_type:n.sender?.sender_type,msg_type:n.msg_type,content:n.body?.content,create_time:n.create_time}));return JSON.stringify({messages:i})}case"lark_lookup_user_by_email":{if(!t.email)return JSON.stringify({error:"email is required"});let r=((await Q("POST","/open-apis/contact/v3/users/batch_get_id?user_id_type=open_id",{emails:[t.email]})).user_list||[]).find(i=>i.email===t.email&&i.user_id);return JSON.stringify(r?{ok:!0,user:{open_id:r.user_id,email:r.email,name:r.name||void 0}}:{ok:!1,reason:"no_lark_user_for_email"})}case"lark_search_users":{if(!t.query||typeof t.query!="string")return JSON.stringify({error:"query is required"});let e=t.query.trim().toLowerCase();if(!e)return JSON.stringify({ok:!0,matches:[]});let r=Math.max(1,Math.min(Number(t.limit)||5,25)),i=200,o=((await Q("GET","/open-apis/im/v1/chats?page_size=100")).items||[]).map(l=>l.chat_id),c=new Set,a=[];for(let l of o){if(a.length>=i)break;try{let p=await Q("GET",`/open-apis/im/v1/chats/${encodeURIComponent(l)}/members?member_id_type=open_id&page_size=100`);for(let d of p.items||[])if(!(!d.member_id||c.has(d.member_id))&&(c.add(d.member_id),a.push({open_id:d.member_id,name:d.name||""}),a.length>=i))break}catch(p){console.warn(`[lark] member scan failed for ${l}: ${p.message}`)}}let u=[];for(let l of a){let p=(l.name||"").toLowerCase();if(!p)continue;let d=0;p.includes(e)&&(d+=100-Math.abs(p.length-e.length)),p===e&&(d+=200),d>0&&u.push({open_id:l.open_id,name:l.name,_score:d})}return u.sort((l,p)=>p._score-l._score),JSON.stringify({ok:!0,matches:u.slice(0,r).map(({_score:l,...p})=>p),scanned:a.length})}default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(e){return JSON.stringify({error:e.message})}}};var yt={id:"chat_notify",description:"Chat notification meta-skill \u2014 routes to whichever messaging integration (Slack OR Lark) the user has configured for this project.",envKeys:[...q.envKeys||[],...B.envKeys||[]],get serverName(){if(process.env.SLACK_CHANNEL)return q.serverName;if(process.env.LARK_RECEIVE_ID)return B.serverName},get allowedTools(){return process.env.SLACK_CHANNEL?q.allowedTools||[]:process.env.LARK_RECEIVE_ID?B.allowedTools||[]:[]},promptFragment:`## Chat notifications (Slack OR Lark \u2014 at least one connected)
272
+ When responding to an incoming event, prefer lark_reply with the source message_id so the response threads cleanly.`,resolve(){let s=Bs();if(!s)return null;let t={};for(let e of["PROJECT_API_TOKEN","ZIBBY_USER_TOKEN","ZIBBY_ACCOUNT_API_URL","ZIBBY_ENV","ZIBBY_PROD_ACCOUNT_API_URL","PROGRESS_API_URL","EXECUTION_ID","PROJECT_ID","STAGE"])process.env[e]&&(t[e]=process.env[e]);return{type:"stdio",command:"node",args:[s],env:t,alwaysLoad:!0}},tools:[{name:"lark_send_message",description:"Send a text message to a Lark chat, user, or DM. receive_id can be a chat_id (oc_*), open_id (ou_*), union_id (on_*), or email.",input_schema:{type:"object",properties:{receive_id:{type:"string",description:"Target id: chat_id (oc_*), open_id (ou_*), union_id (on_*), or email"},text:{type:"string",description:"Message text"}},required:["receive_id","text"]}},{name:"lark_reply",description:"Reply to an existing Lark message (creates a thread). Use the message_id from the inbound event.",input_schema:{type:"object",properties:{message_id:{type:"string",description:"Lark message id (om_*) to reply to"},text:{type:"string",description:"Reply text"}},required:["message_id","text"]}},{name:"lark_list_chats",description:"List chats (groups + DMs) the bot is a member of.",input_schema:{type:"object",properties:{page_size:{type:"number",description:"Max results (default 50)"}}}},{name:"lark_get_chat_history",description:"Fetch recent messages in a chat.",input_schema:{type:"object",properties:{chat_id:{type:"string",description:"Chat id (oc_*)"},page_size:{type:"number",description:"Max messages (default 20)"}},required:["chat_id"]}},{name:"lark_lookup_user_by_email",description:"Resolve an email address to a Lark user id (open_id). Returns { ok:true, user:{open_id,email,name} } on hit, { ok:false } if no Lark user has that email. Use the open_id as `receive_id` in lark_send_message to DM.",input_schema:{type:"object",properties:{email:{type:"string",description:"Email address to look up"}},required:["email"]}},{name:"lark_search_users",description:'Fuzzy-search users by name across chats the bot is a member of. Lark has no public org-wide user search API for bots \u2014 this walks the bot\'s chat memberships and matches names client-side. Best for "send to Sam" style routing where you have a name but no email. Returns up to `limit` ranked matches { open_id, name }.',input_schema:{type:"object",properties:{query:{type:"string",description:"Substring to match against user names (case-insensitive)"},limit:{type:"number",description:"Max matches to return (default 5, max 25)"}},required:["query"]}}],async handleToolCall(s,t){try{switch(s){case"lark_send_message":{if(!t.receive_id||!t.text)return JSON.stringify({error:"receive_id and text are required"});let e=Fs(t.receive_id),r=await ee("POST",`/open-apis/im/v1/messages?receive_id_type=${e}`,{receive_id:t.receive_id,msg_type:"text",content:_t(t.text)});return JSON.stringify({ok:!0,message_id:r.message_id})}case"lark_reply":{if(!t.message_id||!t.text)return JSON.stringify({error:"message_id and text are required"});let e=await ee("POST",`/open-apis/im/v1/messages/${encodeURIComponent(t.message_id)}/reply`,{msg_type:"text",content:_t(t.text)});return JSON.stringify({ok:!0,message_id:e.message_id})}case"lark_list_chats":{let e=t.page_size||50,i=((await ee("GET",`/open-apis/im/v1/chats?page_size=${e}`)).items||[]).map(n=>({chat_id:n.chat_id,name:n.name,description:n.description,owner_id:n.owner_id,chat_mode:n.chat_mode}));return JSON.stringify({chats:i})}case"lark_get_chat_history":{if(!t.chat_id)return JSON.stringify({error:"chat_id is required"});let e=t.page_size||20,i=((await ee("GET",`/open-apis/im/v1/messages?container_id_type=chat&container_id=${encodeURIComponent(t.chat_id)}&page_size=${e}&sort_type=ByCreateTimeDesc`)).items||[]).map(n=>({message_id:n.message_id,sender_id:n.sender?.id,sender_type:n.sender?.sender_type,msg_type:n.msg_type,content:n.body?.content,create_time:n.create_time}));return JSON.stringify({messages:i})}case"lark_lookup_user_by_email":{if(!t.email)return JSON.stringify({error:"email is required"});let r=((await ee("POST","/open-apis/contact/v3/users/batch_get_id?user_id_type=open_id",{emails:[t.email]})).user_list||[]).find(i=>i.email===t.email&&i.user_id);return JSON.stringify(r?{ok:!0,user:{open_id:r.user_id,email:r.email,name:r.name||void 0}}:{ok:!1,reason:"no_lark_user_for_email"})}case"lark_search_users":{if(!t.query||typeof t.query!="string")return JSON.stringify({error:"query is required"});let e=t.query.trim().toLowerCase();if(!e)return JSON.stringify({ok:!0,matches:[]});let r=Math.max(1,Math.min(Number(t.limit)||5,25)),i=200,o=((await ee("GET","/open-apis/im/v1/chats?page_size=100")).items||[]).map(l=>l.chat_id),c=new Set,a=[];for(let l of o){if(a.length>=i)break;try{let d=await ee("GET",`/open-apis/im/v1/chats/${encodeURIComponent(l)}/members?member_id_type=open_id&page_size=100`);for(let p of d.items||[])if(!(!p.member_id||c.has(p.member_id))&&(c.add(p.member_id),a.push({open_id:p.member_id,name:p.name||""}),a.length>=i))break}catch(d){console.warn(`[lark] member scan failed for ${l}: ${d.message}`)}}let u=[];for(let l of a){let d=(l.name||"").toLowerCase();if(!d)continue;let p=0;d.includes(e)&&(p+=100-Math.abs(d.length-e.length)),d===e&&(p+=200),p>0&&u.push({open_id:l.open_id,name:l.name,_score:p})}return u.sort((l,d)=>d._score-l._score),JSON.stringify({ok:!0,matches:u.slice(0,r).map(({_score:l,...d})=>d),scanned:a.length})}default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(e){return JSON.stringify({error:e.message})}}};var bt={id:"chat_notify",description:"Chat notification meta-skill \u2014 routes to whichever messaging integration (Slack OR Lark) the user has configured for this project.",envKeys:[...D.envKeys||[],...K.envKeys||[]],get serverName(){if(process.env.SLACK_CHANNEL)return D.serverName;if(process.env.LARK_RECEIVE_ID)return K.serverName},get allowedTools(){return process.env.SLACK_CHANNEL?D.allowedTools||[]:process.env.LARK_RECEIVE_ID?K.allowedTools||[]:[]},promptFragment:`## Chat notifications (Slack OR Lark \u2014 at least one connected)
265
273
  You can post chat messages via either:
266
274
  - slack_post_message (channel, text) \u2014 Slack, when SLACK_CHANNEL is set
267
275
  - lark_send_message (receive_id, text) \u2014 Lark, when LARK_RECEIVE_ID is set
268
- Use whichever the user has configured.`,resolve(s){return process.env.SLACK_CHANNEL&&typeof q.resolve=="function"?q.resolve(s):process.env.LARK_RECEIVE_ID&&typeof B.resolve=="function"?B.resolve(s):null},async handleToolCall(s,t,e){return typeof s=="string"&&s.startsWith("slack_")?q.handleToolCall(s,t,e):typeof s=="string"&&s.startsWith("lark_")?B.handleToolCall(s,t,e):JSON.stringify({error:`chat_notify: unknown tool "${s}". Expected slack_* or lark_*.`})},get tools(){return[...q.tools||[],...B.tools||[]]}};import{createRequire as qs}from"module";import{execFileSync as Ms}from"child_process";import{join as ht}from"path";import{existsSync as Ds}from"fs";var Bs=qs(import.meta.url);function Ks(){if(process.env.MCP_MEMORY_PATH)return process.env.MCP_MEMORY_PATH;try{return Bs.resolve("@zibby/ui-memory/mcp-server")}catch{return null}}var gt={id:"memory",serverName:"memory",allowedTools:["mcp__memory__*"],envKeys:[],description:"Zibby Memory MCP Server (test history, selectors, page model)",async middleware(){try{let{createMemoryMiddleware:s}=await import("@zibby/ui-memory");return s()}catch{return null}},promptFragment:`BEFORE executing browser actions:
276
+ Use whichever the user has configured.`,resolve(s){return process.env.SLACK_CHANNEL&&typeof D.resolve=="function"?D.resolve(s):process.env.LARK_RECEIVE_ID&&typeof K.resolve=="function"?K.resolve(s):null},async handleToolCall(s,t,e){return typeof s=="string"&&s.startsWith("slack_")?D.handleToolCall(s,t,e):typeof s=="string"&&s.startsWith("lark_")?K.handleToolCall(s,t,e):JSON.stringify({error:`chat_notify: unknown tool "${s}". Expected slack_* or lark_*.`})},get tools(){return[...D.tools||[],...K.tools||[]]}};import{createRequire as zs}from"module";import{execFileSync as Hs}from"child_process";import{join as wt}from"path";import{existsSync as Ws}from"fs";var Ys=zs(import.meta.url);function Zs(){if(process.env.MCP_MEMORY_PATH)return process.env.MCP_MEMORY_PATH;try{return Ys.resolve("@zibby/ui-memory/mcp-server")}catch{return null}}var kt={id:"memory",serverName:"memory",allowedTools:["mcp__memory__*"],envKeys:[],description:"Zibby Memory MCP Server (test history, selectors, page model)",async middleware(){try{let{createMemoryMiddleware:s}=await import("@zibby/ui-memory");return s()}catch{return null}},promptFragment:`BEFORE executing browser actions:
269
277
  - Review any test memory/history above. Prefer selectors proven to work.
270
278
  - If a previous run failed, avoid the same approach.
271
279
  - After setup/login completes, navigate directly to the target page instead of clicking through menus.
@@ -277,18 +285,18 @@ DURING execution \u2014 when a selector fails and you switch to a fallback:
277
285
  AFTER completing the test, you MUST call memory_save_insight at least once:
278
286
  - Save any useful finding: reliable selectors, timing quirks, navigation patterns, workarounds.
279
287
  - Category: selector_tip | timing | navigation | workaround | flaky | general
280
- - Be specific \u2014 future runs will read your insights.`,resolve(){let s=Ks();if(!s)throw new Error(`\u274C Memory MCP server not found
288
+ - Be specific \u2014 future runs will read your insights.`,resolve(){let s=Zs();if(!s)throw new Error(`\u274C Memory MCP server not found
281
289
 
282
290
  Install @zibby/ui-memory:
283
- npm install @zibby/ui-memory`);let t=ht(process.cwd(),".zibby","memory");if(!Ds(ht(t,".dolt")))throw new Error(`\u274C Memory database not initialized
291
+ npm install @zibby/ui-memory`);let t=wt(process.cwd(),".zibby","memory");if(!Ws(wt(t,".dolt")))throw new Error(`\u274C Memory database not initialized
284
292
 
285
293
  Run:
286
- zibby init --mem`);try{let e=Ms("dolt",["sql","-q","SELECT COUNT(*) AS cnt FROM test_runs","-r","json"],{cwd:t,encoding:"utf-8",timeout:5e3}),r=JSON.parse(e.trim()).rows||[];if(!r[0]||r[0].cnt===0)return console.log("[memory] Database empty \u2014 memory tools activate after first completed run"),null}catch(e){throw new Error(`\u274C Dolt not found or memory database error
294
+ zibby init --mem`);try{let e=Hs("dolt",["sql","-q","SELECT COUNT(*) AS cnt FROM test_runs","-r","json"],{cwd:t,encoding:"utf-8",timeout:5e3}),r=JSON.parse(e.trim()).rows||[];if(!r[0]||r[0].cnt===0)return console.log("[memory] Database empty \u2014 memory tools activate after first completed run"),null}catch(e){throw new Error(`\u274C Dolt not found or memory database error
287
295
 
288
296
  Install Dolt:
289
297
  https://docs.dolthub.com/introduction/installation
290
298
 
291
- Error: ${e.message}`,{cause:e})}return{command:"node",args:[s,"--db-path",t],description:this.description}},tools:[{name:"memory_get_test_history",description:"Query recent test runs with pass/fail results and timing",input_schema:{type:"object",properties:{specPath:{type:"string",description:"Filter by spec path (substring match)"},limit:{type:"number",description:"Max results (default 10)"}}}},{name:"memory_get_selectors",description:"Query known selectors for a page with stability metrics",input_schema:{type:"object",properties:{pageUrl:{type:"string",description:"Filter by page URL (substring match)"},limit:{type:"number",description:"Max results (default 20)"}}}},{name:"memory_get_page_model",description:"Query page structure \u2014 elements, roles, selectors",input_schema:{type:"object",properties:{url:{type:"string",description:"Filter by page URL (substring match)"},limit:{type:"number",description:"Max results (default 20)"}}}},{name:"memory_get_navigation",description:"Query known page-to-page transitions",input_schema:{type:"object",properties:{fromUrl:{type:"string",description:"Filter by source URL (substring match)"},limit:{type:"number",description:"Max results (default 20)"}}}},{name:"memory_save_insight",description:"Save a useful observation for future runs (selector tips, timing, workarounds)",input_schema:{type:"object",properties:{category:{type:"string",enum:["selector_tip","timing","navigation","workaround","flaky","general"],description:"Type of insight"},content:{type:"string",description:"The insight text \u2014 be specific and actionable"},specPath:{type:"string",description:"Related spec path"},sessionId:{type:"string",description:"Current session ID"}},required:["category","content"]}}]};import{existsSync as Gs,readFileSync as Fs}from"fs";import{homedir as zs}from"os";import{join as Ws}from"path";import{spawn as Hs}from"child_process";var X={jira:{description:"Jira issue search, details, comments, transitions",integrationProvider:"jira",envKeys:[],setupInstructions:`To connect Jira:
299
+ Error: ${e.message}`,{cause:e})}return{command:"node",args:[s,"--db-path",t],description:this.description}},tools:[{name:"memory_get_test_history",description:"Query recent test runs with pass/fail results and timing",input_schema:{type:"object",properties:{specPath:{type:"string",description:"Filter by spec path (substring match)"},limit:{type:"number",description:"Max results (default 10)"}}}},{name:"memory_get_selectors",description:"Query known selectors for a page with stability metrics",input_schema:{type:"object",properties:{pageUrl:{type:"string",description:"Filter by page URL (substring match)"},limit:{type:"number",description:"Max results (default 20)"}}}},{name:"memory_get_page_model",description:"Query page structure \u2014 elements, roles, selectors",input_schema:{type:"object",properties:{url:{type:"string",description:"Filter by page URL (substring match)"},limit:{type:"number",description:"Max results (default 20)"}}}},{name:"memory_get_navigation",description:"Query known page-to-page transitions",input_schema:{type:"object",properties:{fromUrl:{type:"string",description:"Filter by source URL (substring match)"},limit:{type:"number",description:"Max results (default 20)"}}}},{name:"memory_save_insight",description:"Save a useful observation for future runs (selector tips, timing, workarounds)",input_schema:{type:"object",properties:{category:{type:"string",enum:["selector_tip","timing","navigation","workaround","flaky","general"],description:"Type of insight"},content:{type:"string",description:"The insight text \u2014 be specific and actionable"},specPath:{type:"string",description:"Related spec path"},sessionId:{type:"string",description:"Current session ID"}},required:["category","content"]}}]};import{existsSync as Vs,readFileSync as Qs}from"fs";import{homedir as Xs}from"os";import{join as ei}from"path";import{spawn as ti}from"child_process";var te={jira:{description:"Jira issue search, details, comments, transitions",integrationProvider:"jira",envKeys:[],setupInstructions:`To connect Jira:
292
300
  1. Go to Settings \u2192 Integrations (https://studio.zibby.dev/integrations)
293
301
  2. Click "Connect Jira" and authorize via Atlassian OAuth
294
302
  3. After OAuth completes, ask me to install Jira again`},github:{description:"GitHub issues, PRs, repository management",integrationProvider:"github",envKeys:[],setupInstructions:`To connect GitHub:
@@ -300,15 +308,15 @@ AFTER completing the test, you MUST call memory_save_insight at least once:
300
308
  3. After OAuth completes, ask me to install Slack again`},sentry:{description:"Sentry error tracking \u2014 projects, issues, events",integrationProvider:"sentry",envKeys:[],setupInstructions:`To connect Sentry:
301
309
  1. Go to Settings \u2192 Integrations (https://studio.zibby.dev/integrations)
302
310
  2. Click "Connect Sentry" and authorize
303
- 3. After OAuth completes, ask me to install Sentry again`},runner:{description:"Run zibby test workflows from chat (parallel supported)",envKeys:[],setupInstructions:"Ready to use. Runs zibby test workflows as background processes \u2014 each with its own browser and session."},browser:{description:"Playwright browser automation (navigate, click, fill, screenshot)",envKeys:[],setupInstructions:"Ready to use. Starts a Playwright browser for web automation."},memory:{description:"Test memory database (Dolt) \u2014 history, selectors, insights",envKeys:[],setupInstructions:"Ready to use. Requires Dolt (https://docs.dolthub.com/introduction/installation) and a memory DB via `zibby init --mem`."},"chat-memory":{description:"Persistent chat memory \u2014 remembers facts, decisions, and task history across sessions (Dolt-backed)",envKeys:[],setupInstructions:'Ready to use. Requires Dolt installed. Tables auto-create on first use. Install with: "add chat memory" or "install chat-memory".'},git:{description:"Clone and explore git repositories locally for codebase analysis",envKeys:[],setupInstructions:"Ready to use. Clone repos with git_checkout, explore with git_explore. Auto-authenticates with GitHub/GitLab tokens."}};function Ys(){let s=["## Available Skills"];for(let[t,e]of Object.entries(X)){let r=e.integrationProvider?`integration: ${e.integrationProvider}`:"ready";s.push(`- ${t}: ${e.description} [${r}]`)}return s.push(""),s.push("Use the install_skill / uninstall_skill / list_available_skills tools to manage skills."),s.push(`Zibby third party Integration settings page: ${Le()}`),s.push(""),s.push("## Tool-First Policy (mandatory)"),s.push("CRITICAL RULES \u2014 follow these strictly:"),s.push("1. When user asks to do something and a matching skill is available but not installed, IMMEDIATELY call install_skill. Never ask for credentials or confirmation first."),s.push(`2. If install_skill succeeds, the skill's tools are now available. Use them RIGHT AWAY in the same turn \u2014 don't just say "it's connected", actually call the tools.`),s.push("3. If install_skill reports needsIntegration, tell the user to connect via the integration URL and try again after."),s.push("4. When the relevant skill is already installed, use its tools directly \u2014 don't ask for IDs or keys. Each skill's own instructions explain the workflow."),s.push("5. If a task needs multiple skills (e.g. data from one + execution from another), install all of them, then follow each skill's workflow instructions."),s.join(`
304
- `)}function Zs(){if(process.env.ZIBBY_USER_TOKEN)return process.env.ZIBBY_USER_TOKEN;try{let s=Ws(zs(),".zibby","config.json");return Gs(s)&&JSON.parse(Fs(s,"utf-8")).sessionToken||null}catch{return null}}function Vs(){return(process.env.ZIBBY_API_URL||process.env.ZIBBY_PROD_API_URL||"https://api-prod.zibby.app").replace(/\/$/,"")}function Le(){return`${(process.env.ZIBBY_FRONTEND_URL||process.env.ZIBBY_PROD_FRONTEND_URL||"https://studio.zibby.dev").replace(/\/$/,"")}/integrations`}function Qs(s){try{let t=process.platform;return Hs(t==="darwin"?"open":t==="win32"?"cmd":"xdg-open",t==="win32"?["/c","start","",s]:[s],{detached:!0,stdio:"ignore"}).unref(),!0}catch{return!1}}async function Xs(){let s=Zs();if(!s)return{checked:!1,statuses:null,reason:"no-session-token"};try{let t=await fetch(`${Vs()}/integrations/status`,{method:"GET",headers:{Authorization:`Bearer ${s}`}});return t.ok?{checked:!0,statuses:await t.json()||{},reason:null}:{checked:!1,statuses:null,reason:`status-${t.status}`}}catch{return{checked:!1,statuses:null,reason:"network-error"}}}function _t(s,t){if(!t||!s)return{connected:null};let e=s[t];return!e||typeof e.connected!="boolean"?{connected:null,details:e||null}:{connected:e.connected,details:e}}var bt={id:"skill-installer",description:"Live skill installation for chat sessions",envKeys:[],catalog:X,promptFragment:Ys,tools:[{name:"install_skill",description:"Install a skill into the current chat session so its tools become available",input_schema:{type:"object",properties:{skillId:{type:"string",description:'Skill identifier to install (e.g. "jira", "github", "browser", "memory")'}},required:["skillId"]}},{name:"uninstall_skill",description:"Remove a skill from the current chat session",input_schema:{type:"object",properties:{skillId:{type:"string",description:"Skill identifier to remove"}},required:["skillId"]}},{name:"list_available_skills",description:"List all skills that can be installed, with their env-var readiness status",input_schema:{type:"object",properties:{}}}],async handleToolCall(s,t,e){let{activeSkills:r}=e,i=await Xs();if(s==="list_available_skills"){let n=Object.entries(X).map(([o,c])=>{let a=r.includes(o),u=_t(i.statuses,c.integrationProvider);return{id:o,description:c.description,installed:a,integrationProvider:c.integrationProvider||void 0,integrationConnected:u.connected,setupInstructions:u.connected===!1?c.setupInstructions:void 0}});return JSON.stringify({skills:n})}if(s==="install_skill"){let{skillId:n}=t;if(!n)return JSON.stringify({ok:!1,error:"skillId is required"});if(r.includes(n)){let l=X[n],{getSkill:p}=await import("@zibby/agent-workflow"),m=(p(n)?.tools||[]).map(f=>f.name);return JSON.stringify({ok:!0,alreadyInstalled:!0,skillId:n,description:l?.description,availableTools:m,integrationUrl:l?.integrationProvider?Le():void 0,hint:`${n} is already active. Tools available: ${m.join(", ")}. Use them directly.`})}if(!X[n])return JSON.stringify({ok:!1,error:`Unknown skill "${n}". Available: ${Object.keys(X).join(", ")}`});let o=X[n];if(o.integrationProvider){let l=_t(i.statuses,o.integrationProvider),p=Le();if(i.checked&&l.connected===!1){let d=Qs(p);return JSON.stringify({ok:!1,error:`${o.integrationProvider} is not connected for this Zibby account yet`,needsIntegration:!0,integrationUrl:p,openedBrowser:d,setupInstructions:`Please connect ${o.integrationProvider} first at ${p}. After you finish OAuth, ask me to install ${n} again.`})}}r.push(n);let{getSkill:c}=await import("@zibby/agent-workflow"),u=(c(n)?.tools||[]).map(l=>l.name);return JSON.stringify({ok:!0,installed:n,description:o.description,availableTools:u,hint:`${n} is now active. You now have these tools: ${u.join(", ")}. Use them immediately to help the user \u2014 don't just confirm installation.`})}if(s==="uninstall_skill"){let{skillId:n}=t;if(!n)return JSON.stringify({ok:!1,error:"skillId is required"});if(n==="skill-installer")return JSON.stringify({ok:!1,error:"Cannot uninstall the skill installer"});let o=r.indexOf(n);return o===-1?JSON.stringify({ok:!1,error:`${n} is not installed`}):(r.splice(o,1),JSON.stringify({ok:!0,uninstalled:n}))}return JSON.stringify({error:`Unknown tool: ${s}`})},resolve(){return null}};import{readFileSync as ei,readdirSync as ti,statSync as wt,writeFileSync as ri,mkdirSync as si}from"fs";import{join as St,resolve as ii,relative as ni}from"path";import{execSync as vt}from"child_process";var kt=256*1024,oi=64*1024,Nt={id:"core-tools",description:"File read/write, directory listing, shell commands, open URLs, wait for async operations",envKeys:[],tools:[{name:"read_file",description:"Read the contents of a file. Returns the text content.",input_schema:{type:"object",properties:{path:{type:"string",description:"File path (relative to cwd or absolute)"}},required:["path"]}},{name:"write_file",description:"Write content to a file. Creates parent directories if needed.",input_schema:{type:"object",properties:{path:{type:"string",description:"File path (relative to cwd or absolute)"},content:{type:"string",description:"Content to write"}},required:["path","content"]}},{name:"list_directory",description:"List files and directories in a path. Returns names with type indicators (/ for dirs).",input_schema:{type:"object",properties:{path:{type:"string",description:"Directory path (relative to cwd or absolute). Defaults to cwd."}}}},{name:"run_command",description:"Run a shell command and return its output. Use for grep, git, npm, etc.",input_schema:{type:"object",properties:{command:{type:"string",description:"Shell command to execute"},cwd:{type:"string",description:"Working directory (optional, defaults to project root)"}},required:["command"]}},{name:"open_url",description:"Open a URL in the user's default browser. Use for OAuth flows, documentation, integration setup pages.",input_schema:{type:"object",properties:{url:{type:"string",description:"URL to open"}},required:["url"]}},{name:"wait",description:"Wait for N seconds. Use this for async operations (tests, builds, deploys) \u2014 wait, then check status again.",input_schema:{type:"object",properties:{seconds:{type:"number",description:"Seconds to wait (default: 5, max: 300)"},reason:{type:"string",description:"Why waiting (for logging/clarity)"}}}}],async handleToolCall(s,t,e){let r=e?.options?.workspace||process.cwd();try{switch(s){case"read_file":return ai(t,r);case"write_file":return ci(t,r);case"list_directory":return li(t,r);case"run_command":return ui(t,r);case"open_url":return pi(t);case"wait":return await di(t,e?.options?.signal);default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(i){return JSON.stringify({error:i.message})}},resolve(){return null}};function _e(s,t){return ii(t,s)}function ai(s,t){let e=_e(s.path,t),r=wt(e);return r.size>kt?JSON.stringify({error:`File too large (${(r.size/1024).toFixed(0)}KB). Max: ${kt/1024}KB`}):ei(e,"utf-8")}function ci(s,t){let e=_e(s.path,t),r=St(e,"..");return si(r,{recursive:!0}),ri(e,s.content,"utf-8"),JSON.stringify({ok:!0,path:ni(t,e)})}function li(s,t){let e=_e(s.path||".",t);return ti(e).map(i=>{try{return wt(St(e,i)).isDirectory()?`${i}/`:i}catch{return i}}).join(`
305
- `)}function ui(s,t){let e=s.cwd?_e(s.cwd,t):t;return vt(s.command,{cwd:e,encoding:"utf-8",timeout:3e4,maxBuffer:oi,stdio:["pipe","pipe","pipe"]})||"(no output)"}function pi(s){let{url:t}=s;if(!t||!t.startsWith("http://")&&!t.startsWith("https://"))return JSON.stringify({error:"Invalid URL \u2014 must start with http:// or https://"});let e=process.platform,r=e==="darwin"?"open":e==="win32"?"start":"xdg-open";try{return vt(`${r} "${t}"`,{stdio:"ignore",timeout:5e3}),JSON.stringify({ok:!0,opened:t})}catch{return JSON.stringify({ok:!1,error:`Could not open browser. Please visit: ${t}`})}}async function di(s,t){let e=Math.min(Math.max(s.seconds||5,1),300),r=s.reason||"async operation",i=500,n=Date.now()+e*1e3;for(;Date.now()<n;){if(t?.aborted)return JSON.stringify({ok:!0,waited:Math.round((e*1e3-(n-Date.now()))/1e3),reason:r,interrupted:!0});await new Promise(o=>setTimeout(o,Math.min(i,n-Date.now())))}return JSON.stringify({ok:!0,waited:e,reason:r})}import{existsSync as mi}from"fs";import{fileURLToPath as fi}from"url";import{dirname as yi,resolve as hi}from"path";import{resolveIntegrationToken as It}from"@zibby/core/backend-client.js";function gi(){if(process.env.MCP_SENTRY_PATH)return process.env.MCP_SENTRY_PATH;let s=yi(fi(import.meta.url)),t=hi(s,"..","bin","mcp-sentry.mjs");return mi(t)?t:null}async function Ot(s,t={}){let{token:e,organizationSlug:r}=await It("sentry"),i=`https://sentry.io/api/0/organizations/${r}${s}`,n=await fetch(i,{method:t.method||"GET",headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"}});if(!n.ok){let o=await n.text().catch(()=>"");throw new Error(`Sentry API ${n.status}: ${o.slice(0,300)}`)}return n.json()}async function _i(){return Ot("/projects/?per_page=50")}async function bi({query:s="is:unresolved",sort:t="date",project:e,limit:r=25}={}){let i=`/issues/?query=${encodeURIComponent(s)}&sort=${t}&per_page=${r}`;return e&&(i+=`&project=${encodeURIComponent(e)}`),Ot(i)}async function ki(s){if(!s)throw new Error("sentryGetIssue: issueId is required");let{token:t}=await It("sentry"),e=await fetch(`https://sentry.io/api/0/issues/${s}/`,{headers:{Authorization:`Bearer ${t}`}});if(!e.ok){let r=await e.text().catch(()=>"");throw new Error(`Sentry API ${e.status}: ${r.slice(0,300)}`)}return e.json()}var be={id:"sentry",serverName:"sentry",allowedTools:["mcp__sentry__*"],requiresIntegration:R.SENTRY,description:"Sentry error tracking \u2014 projects, issues, events",envKeys:[],tools:[],promptFragment:`## Sentry (connected)
311
+ 3. After OAuth completes, ask me to install Sentry again`},runner:{description:"Run zibby test workflows from chat (parallel supported)",envKeys:[],setupInstructions:"Ready to use. Runs zibby test workflows as background processes \u2014 each with its own browser and session."},browser:{description:"Playwright browser automation (navigate, click, fill, screenshot)",envKeys:[],setupInstructions:"Ready to use. Starts a Playwright browser for web automation."},memory:{description:"Test memory database (Dolt) \u2014 history, selectors, insights",envKeys:[],setupInstructions:"Ready to use. Requires Dolt (https://docs.dolthub.com/introduction/installation) and a memory DB via `zibby init --mem`."},"chat-memory":{description:"Persistent chat memory \u2014 remembers facts, decisions, and task history across sessions (Dolt-backed)",envKeys:[],setupInstructions:'Ready to use. Requires Dolt installed. Tables auto-create on first use. Install with: "add chat memory" or "install chat-memory".'},git:{description:"Clone and explore git repositories locally for codebase analysis",envKeys:[],setupInstructions:"Ready to use. Clone repos with git_checkout, explore with git_explore. Auto-authenticates with GitHub/GitLab tokens."}};function ri(){let s=["## Available Skills"];for(let[t,e]of Object.entries(te)){let r=e.integrationProvider?`integration: ${e.integrationProvider}`:"ready";s.push(`- ${t}: ${e.description} [${r}]`)}return s.push(""),s.push("Use the install_skill / uninstall_skill / list_available_skills tools to manage skills."),s.push(`Zibby third party Integration settings page: ${Pe()}`),s.push(""),s.push("## Tool-First Policy (mandatory)"),s.push("CRITICAL RULES \u2014 follow these strictly:"),s.push("1. When user asks to do something and a matching skill is available but not installed, IMMEDIATELY call install_skill. Never ask for credentials or confirmation first."),s.push(`2. If install_skill succeeds, the skill's tools are now available. Use them RIGHT AWAY in the same turn \u2014 don't just say "it's connected", actually call the tools.`),s.push("3. If install_skill reports needsIntegration, tell the user to connect via the integration URL and try again after."),s.push("4. When the relevant skill is already installed, use its tools directly \u2014 don't ask for IDs or keys. Each skill's own instructions explain the workflow."),s.push("5. If a task needs multiple skills (e.g. data from one + execution from another), install all of them, then follow each skill's workflow instructions."),s.join(`
312
+ `)}function si(){if(process.env.ZIBBY_USER_TOKEN)return process.env.ZIBBY_USER_TOKEN;try{let s=ei(Xs(),".zibby","config.json");return Vs(s)&&JSON.parse(Qs(s,"utf-8")).sessionToken||null}catch{return null}}function ii(){return(process.env.ZIBBY_API_URL||process.env.ZIBBY_PROD_API_URL||"https://api-prod.zibby.app").replace(/\/$/,"")}function Pe(){return`${(process.env.ZIBBY_FRONTEND_URL||process.env.ZIBBY_PROD_FRONTEND_URL||"https://studio.zibby.dev").replace(/\/$/,"")}/integrations`}function ni(s){try{let t=process.platform;return ti(t==="darwin"?"open":t==="win32"?"cmd":"xdg-open",t==="win32"?["/c","start","",s]:[s],{detached:!0,stdio:"ignore"}).unref(),!0}catch{return!1}}async function oi(){let s=si();if(!s)return{checked:!1,statuses:null,reason:"no-session-token"};try{let t=await fetch(`${ii()}/integrations/status`,{method:"GET",headers:{Authorization:`Bearer ${s}`}});return t.ok?{checked:!0,statuses:await t.json()||{},reason:null}:{checked:!1,statuses:null,reason:`status-${t.status}`}}catch{return{checked:!1,statuses:null,reason:"network-error"}}}function St(s,t){if(!t||!s)return{connected:null};let e=s[t];return!e||typeof e.connected!="boolean"?{connected:null,details:e||null}:{connected:e.connected,details:e}}var vt={id:"skill-installer",description:"Live skill installation for chat sessions",envKeys:[],catalog:te,promptFragment:ri,tools:[{name:"install_skill",description:"Install a skill into the current chat session so its tools become available",input_schema:{type:"object",properties:{skillId:{type:"string",description:'Skill identifier to install (e.g. "jira", "github", "browser", "memory")'}},required:["skillId"]}},{name:"uninstall_skill",description:"Remove a skill from the current chat session",input_schema:{type:"object",properties:{skillId:{type:"string",description:"Skill identifier to remove"}},required:["skillId"]}},{name:"list_available_skills",description:"List all skills that can be installed, with their env-var readiness status",input_schema:{type:"object",properties:{}}}],async handleToolCall(s,t,e){let{activeSkills:r}=e,i=await oi();if(s==="list_available_skills"){let n=Object.entries(te).map(([o,c])=>{let a=r.includes(o),u=St(i.statuses,c.integrationProvider);return{id:o,description:c.description,installed:a,integrationProvider:c.integrationProvider||void 0,integrationConnected:u.connected,setupInstructions:u.connected===!1?c.setupInstructions:void 0}});return JSON.stringify({skills:n})}if(s==="install_skill"){let{skillId:n}=t;if(!n)return JSON.stringify({ok:!1,error:"skillId is required"});if(r.includes(n)){let l=te[n],{getSkill:d}=await import("@zibby/agent-workflow"),m=(d(n)?.tools||[]).map(f=>f.name);return JSON.stringify({ok:!0,alreadyInstalled:!0,skillId:n,description:l?.description,availableTools:m,integrationUrl:l?.integrationProvider?Pe():void 0,hint:`${n} is already active. Tools available: ${m.join(", ")}. Use them directly.`})}if(!te[n])return JSON.stringify({ok:!1,error:`Unknown skill "${n}". Available: ${Object.keys(te).join(", ")}`});let o=te[n];if(o.integrationProvider){let l=St(i.statuses,o.integrationProvider),d=Pe();if(i.checked&&l.connected===!1){let p=ni(d);return JSON.stringify({ok:!1,error:`${o.integrationProvider} is not connected for this Zibby account yet`,needsIntegration:!0,integrationUrl:d,openedBrowser:p,setupInstructions:`Please connect ${o.integrationProvider} first at ${d}. After you finish OAuth, ask me to install ${n} again.`})}}r.push(n);let{getSkill:c}=await import("@zibby/agent-workflow"),u=(c(n)?.tools||[]).map(l=>l.name);return JSON.stringify({ok:!0,installed:n,description:o.description,availableTools:u,hint:`${n} is now active. You now have these tools: ${u.join(", ")}. Use them immediately to help the user \u2014 don't just confirm installation.`})}if(s==="uninstall_skill"){let{skillId:n}=t;if(!n)return JSON.stringify({ok:!1,error:"skillId is required"});if(n==="skill-installer")return JSON.stringify({ok:!1,error:"Cannot uninstall the skill installer"});let o=r.indexOf(n);return o===-1?JSON.stringify({ok:!1,error:`${n} is not installed`}):(r.splice(o,1),JSON.stringify({ok:!0,uninstalled:n}))}return JSON.stringify({error:`Unknown tool: ${s}`})},resolve(){return null}};import{readFileSync as ai,readdirSync as ci,statSync as Nt,writeFileSync as li,mkdirSync as ui}from"fs";import{join as Rt,resolve as di,relative as pi}from"path";import{execSync as Ot}from"child_process";var It=256*1024,mi=64*1024,At={id:"core-tools",description:"File read/write, directory listing, shell commands, open URLs, wait for async operations",envKeys:[],tools:[{name:"read_file",description:"Read the contents of a file. Returns the text content.",input_schema:{type:"object",properties:{path:{type:"string",description:"File path (relative to cwd or absolute)"}},required:["path"]}},{name:"write_file",description:"Write content to a file. Creates parent directories if needed.",input_schema:{type:"object",properties:{path:{type:"string",description:"File path (relative to cwd or absolute)"},content:{type:"string",description:"Content to write"}},required:["path","content"]}},{name:"list_directory",description:"List files and directories in a path. Returns names with type indicators (/ for dirs).",input_schema:{type:"object",properties:{path:{type:"string",description:"Directory path (relative to cwd or absolute). Defaults to cwd."}}}},{name:"run_command",description:"Run a shell command and return its output. Use for grep, git, npm, etc.",input_schema:{type:"object",properties:{command:{type:"string",description:"Shell command to execute"},cwd:{type:"string",description:"Working directory (optional, defaults to project root)"}},required:["command"]}},{name:"open_url",description:"Open a URL in the user's default browser. Use for OAuth flows, documentation, integration setup pages.",input_schema:{type:"object",properties:{url:{type:"string",description:"URL to open"}},required:["url"]}},{name:"wait",description:"Wait for N seconds. Use this for async operations (tests, builds, deploys) \u2014 wait, then check status again.",input_schema:{type:"object",properties:{seconds:{type:"number",description:"Seconds to wait (default: 5, max: 300)"},reason:{type:"string",description:"Why waiting (for logging/clarity)"}}}}],async handleToolCall(s,t,e){let r=e?.options?.workspace||process.cwd();try{switch(s){case"read_file":return fi(t,r);case"write_file":return yi(t,r);case"list_directory":return hi(t,r);case"run_command":return gi(t,r);case"open_url":return _i(t);case"wait":return await bi(t,e?.options?.signal);default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(i){return JSON.stringify({error:i.message})}},resolve(){return null}};function be(s,t){return di(t,s)}function fi(s,t){let e=be(s.path,t),r=Nt(e);return r.size>It?JSON.stringify({error:`File too large (${(r.size/1024).toFixed(0)}KB). Max: ${It/1024}KB`}):ai(e,"utf-8")}function yi(s,t){let e=be(s.path,t),r=Rt(e,"..");return ui(r,{recursive:!0}),li(e,s.content,"utf-8"),JSON.stringify({ok:!0,path:pi(t,e)})}function hi(s,t){let e=be(s.path||".",t);return ci(e).map(i=>{try{return Nt(Rt(e,i)).isDirectory()?`${i}/`:i}catch{return i}}).join(`
313
+ `)}function gi(s,t){let e=s.cwd?be(s.cwd,t):t;return Ot(s.command,{cwd:e,encoding:"utf-8",timeout:3e4,maxBuffer:mi,stdio:["pipe","pipe","pipe"]})||"(no output)"}function _i(s){let{url:t}=s;if(!t||!t.startsWith("http://")&&!t.startsWith("https://"))return JSON.stringify({error:"Invalid URL \u2014 must start with http:// or https://"});let e=process.platform,r=e==="darwin"?"open":e==="win32"?"start":"xdg-open";try{return Ot(`${r} "${t}"`,{stdio:"ignore",timeout:5e3}),JSON.stringify({ok:!0,opened:t})}catch{return JSON.stringify({ok:!1,error:`Could not open browser. Please visit: ${t}`})}}async function bi(s,t){let e=Math.min(Math.max(s.seconds||5,1),300),r=s.reason||"async operation",i=500,n=Date.now()+e*1e3;for(;Date.now()<n;){if(t?.aborted)return JSON.stringify({ok:!0,waited:Math.round((e*1e3-(n-Date.now()))/1e3),reason:r,interrupted:!0});await new Promise(o=>setTimeout(o,Math.min(i,n-Date.now())))}return JSON.stringify({ok:!0,waited:e,reason:r})}import{existsSync as wi}from"fs";import{fileURLToPath as ki}from"url";import{dirname as Si,resolve as vi}from"path";import{resolveIntegrationToken as $t}from"@zibby/core/backend-client.js";function Ii(){if(process.env.MCP_SENTRY_PATH)return process.env.MCP_SENTRY_PATH;let s=Si(ki(import.meta.url)),t=vi(s,"..","bin","mcp-sentry.mjs");return wi(t)?t:null}async function jt(s,t={}){let{token:e,organizationSlug:r}=await $t("sentry"),i=`https://sentry.io/api/0/organizations/${r}${s}`,n=await fetch(i,{method:t.method||"GET",headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"}});if(!n.ok){let o=await n.text().catch(()=>"");throw new Error(`Sentry API ${n.status}: ${o.slice(0,300)}`)}return n.json()}async function Ni(){return jt("/projects/?per_page=50")}async function Ri({query:s="is:unresolved",sort:t="date",project:e,limit:r=25}={}){let i=`/issues/?query=${encodeURIComponent(s)}&sort=${t}&per_page=${r}`;return e&&(i+=`&project=${encodeURIComponent(e)}`),jt(i)}async function Oi(s){if(!s)throw new Error("sentryGetIssue: issueId is required");let{token:t}=await $t("sentry"),e=await fetch(`https://sentry.io/api/0/issues/${s}/`,{headers:{Authorization:`Bearer ${t}`}});if(!e.ok){let r=await e.text().catch(()=>"");throw new Error(`Sentry API ${e.status}: ${r.slice(0,300)}`)}return e.json()}var we={id:"sentry",serverName:"sentry",allowedTools:["mcp__sentry__*"],requiresIntegration:A.SENTRY,description:"Sentry error tracking \u2014 projects, issues, events",envKeys:[],tools:[],promptFragment:`## Sentry (connected)
306
314
  You have access to the user's Sentry. Use these tools:
307
315
  - sentry_list_projects: List projects in the organization
308
316
  - sentry_list_issues: List errors/issues (supports Sentry search query, project filter, sort)
309
- - sentry_get_issue: Get detailed info about a specific issue (requires issueId)`,resolve(){let s=gi();if(!s)return null;let t={};for(let e of["PROJECT_API_TOKEN","ZIBBY_USER_TOKEN","ZIBBY_ACCOUNT_API_URL","ZIBBY_ENV","ZIBBY_PROD_ACCOUNT_API_URL","PROGRESS_API_URL","EXECUTION_ID","PROJECT_ID","STAGE"])process.env[e]&&(t[e]=process.env[e]);return{type:"stdio",command:"node",args:[s],env:t,alwaysLoad:!0}},async handleToolCall(s,t={}){try{switch(s){case"sentry_list_projects":{let e=await _i();return JSON.stringify({projects:e.map(r=>({slug:r.slug,name:r.name,platform:r.platform}))})}case"sentry_list_issues":{let e=await bi({query:t.query,sort:t.sort,project:t.project,limit:t.limit});return JSON.stringify({issues:e.map(r=>({id:r.id,title:r.title,culprit:r.culprit,count:r.count,firstSeen:r.firstSeen,lastSeen:r.lastSeen,level:r.level,status:r.status}))})}case"sentry_get_issue":{let e=await ki(t.issueId);return JSON.stringify({id:e.id,title:e.title,culprit:e.culprit,metadata:e.metadata,count:e.count,userCount:e.userCount,firstSeen:e.firstSeen,lastSeen:e.lastSeen,level:e.level,status:e.status,project:{slug:e.project?.slug,name:e.project?.name}})}default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(e){return JSON.stringify({error:e.message})}},toolsForAssistant:[{name:"sentry_list_projects",description:"List Sentry projects",input_schema:{type:"object",properties:{}}},{name:"sentry_list_issues",description:"List Sentry issues (errors)",input_schema:{type:"object",properties:{project:{type:"string",description:"Project slug (optional)"},query:{type:"string",description:"Sentry search query (default: is:unresolved)"},sort:{type:"string",description:"Sort order: date, new, priority, freq, user (default: date)"},limit:{type:"number",description:"Max issues to return (default 25)"}}}},{name:"sentry_get_issue",description:"Get details of a specific Sentry issue",input_schema:{type:"object",properties:{issueId:{type:"string",description:"Sentry issue ID"}},required:["issueId"]}}]};be.tools=be.toolsForAssistant;import{spawn as Pt}from"child_process";import{writeFileSync as wi,mkdirSync as Rt,existsSync as K,readdirSync as ve,readFileSync as Se,unlinkSync as Si,createWriteStream as vi,statSync as Ni}from"fs";import{resolve as ee,join as M}from"path";import{resolveMaxParallelRuns as Jt}from"@zibby/core/utils/parallel-config.js";import{zibbyScratchSpecsDir as Ii}from"@zibby/core/constants/zibby-scratch.js";var Pe="sessions",Je=".zibby/output",ke=process.env.ZIBBY_RUNNER_NODE_PROGRESS==="1",Oi=process.env.ZIBBY_RUNNER_STATUS_STREAM==="1",Ut=process.env.ZIBBY_RUNNER_SPAWN_LOGS==="1",x=new Map,Y=[],Ri=0,xe=0,At=3e3;function qt(){return`run_${++Ri}_${Date.now().toString(36)}`}function $t(s){let t=Math.floor(s/1e3);return t<60?`${t}s`:`${Math.floor(t/60)}m ${t%60}s`}function Mt(s){return s.replace(/\x1b\[[0-9;]*[a-zA-Z]/g,"")}function E(s,t,e){if(!Oi)return;let r=`
317
+ - sentry_get_issue: Get detailed info about a specific issue (requires issueId)`,resolve(){let s=Ii();if(!s)return null;let t={};for(let e of["PROJECT_API_TOKEN","ZIBBY_USER_TOKEN","ZIBBY_ACCOUNT_API_URL","ZIBBY_ENV","ZIBBY_PROD_ACCOUNT_API_URL","PROGRESS_API_URL","EXECUTION_ID","PROJECT_ID","STAGE"])process.env[e]&&(t[e]=process.env[e]);return{type:"stdio",command:"node",args:[s],env:t,alwaysLoad:!0}},async handleToolCall(s,t={}){try{switch(s){case"sentry_list_projects":{let e=await Ni();return JSON.stringify({projects:e.map(r=>({slug:r.slug,name:r.name,platform:r.platform}))})}case"sentry_list_issues":{let e=await Ri({query:t.query,sort:t.sort,project:t.project,limit:t.limit});return JSON.stringify({issues:e.map(r=>({id:r.id,title:r.title,culprit:r.culprit,count:r.count,firstSeen:r.firstSeen,lastSeen:r.lastSeen,level:r.level,status:r.status}))})}case"sentry_get_issue":{let e=await Oi(t.issueId);return JSON.stringify({id:e.id,title:e.title,culprit:e.culprit,metadata:e.metadata,count:e.count,userCount:e.userCount,firstSeen:e.firstSeen,lastSeen:e.lastSeen,level:e.level,status:e.status,project:{slug:e.project?.slug,name:e.project?.name}})}default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(e){return JSON.stringify({error:e.message})}},toolsForAssistant:[{name:"sentry_list_projects",description:"List Sentry projects",input_schema:{type:"object",properties:{}}},{name:"sentry_list_issues",description:"List Sentry issues (errors)",input_schema:{type:"object",properties:{project:{type:"string",description:"Project slug (optional)"},query:{type:"string",description:"Sentry search query (default: is:unresolved)"},sort:{type:"string",description:"Sort order: date, new, priority, freq, user (default: date)"},limit:{type:"number",description:"Max issues to return (default 25)"}}}},{name:"sentry_get_issue",description:"Get details of a specific Sentry issue",input_schema:{type:"object",properties:{issueId:{type:"string",description:"Sentry issue ID"}},required:["issueId"]}}]};we.tools=we.toolsForAssistant;import{spawn as Mt}from"child_process";import{writeFileSync as Ai,mkdirSync as Tt,existsSync as G,readdirSync as Ie,readFileSync as ve,unlinkSync as $i,createWriteStream as ji,statSync as Ti}from"fs";import{resolve as re,join as B}from"path";import{resolveMaxParallelRuns as Dt}from"@zibby/core/utils/parallel-config.js";import{zibbyScratchSpecsDir as Ei}from"@zibby/core/constants/zibby-scratch.js";var qe="sessions",Me=".zibby/output",ke=process.env.ZIBBY_RUNNER_NODE_PROGRESS==="1",Li=process.env.ZIBBY_RUNNER_STATUS_STREAM==="1",Bt=process.env.ZIBBY_RUNNER_SPAWN_LOGS==="1",x=new Map,V=[],xi=0,Je=0,Et=3e3;function Kt(){return`run_${++xi}_${Date.now().toString(36)}`}function Lt(s){let t=Math.floor(s/1e3);return t<60?`${t}s`:`${Math.floor(t/60)}m ${t%60}s`}function Gt(s){return s.replace(/\x1b\[[0-9;]*[a-zA-Z]/g,"")}function L(s,t,e){if(!Li)return;let r=`
310
318
  ${t} [${s}] ${e}
311
- `;try{process.stderr.write(r)}catch{}}function Ue(){Y.length=0;for(let[,s]of x)if(s.status==="queued"&&(s.status="cancelled"),s.status==="running"&&s._child)try{s._child.kill("SIGTERM")}catch{}}process.on("exit",Ue);process.on("SIGINT",()=>{Ue(),process.exit(0)});process.on("SIGTERM",()=>{Ue(),process.exit(0)});var Dt={id:"runner",description:"Run zibby test workflows from chat (parallel supported)",envKeys:[],promptFragment:`## Test Runner
319
+ `;try{process.stderr.write(r)}catch{}}function De(){V.length=0;for(let[,s]of x)if(s.status==="queued"&&(s.status="cancelled"),s.status==="running"&&s._child)try{s._child.kill("SIGTERM")}catch{}}process.on("exit",De);process.on("SIGINT",()=>{De(),process.exit(0)});process.on("SIGTERM",()=>{De(),process.exit(0)});var Ft={id:"runner",description:"Run zibby test workflows from chat (parallel supported)",envKeys:[],promptFragment:`## Test Runner
312
320
  You can run zibby test workflows directly from chat:
313
321
 
314
322
  **CRITICAL: When user asks to test a ticket:**
@@ -445,35 +453,35 @@ Each run generates:
445
453
  - events.json: All browser events
446
454
  - raw_stream_output.txt: Agent log
447
455
 
448
- Use run_artifacts({ runId, type }) and run_diagnose({ runId }) to inspect and explain failures.`,resolve(){return null},async handleToolCall(s,t,e){let r=e?.options?.workspace||process.cwd();try{switch(s){case"run_generate":return await Ai(t,r);case"run_test":return await xi(t,r,e);case"run_status":return Ci(t);case"run_cancel":return Pi(t);case"run_artifacts":return qi(t,r);case"run_diagnose":return Mi(t,r);case"list_specs":return Di(t,r);default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(i){return JSON.stringify({error:i.message})}},tools:[{name:"run_generate",description:"Generate specs from codebase. CRITICAL: DO NOT USE if ticket has test steps in comments. Only use when: (1) NO steps in ticket AND (2) local codebase exists (not external URLs). For tickets with steps, use run_test with inline format.",input_schema:{type:"object",properties:{ticket:{type:"string",description:"Jira ticket key (e.g. SCRUM-123). Auto-fetches ticket details."},description:{type:"string",description:"Ticket description text (use if no Jira key available)"},input:{type:"string",description:"Path to a file containing ticket/requirements text"},repo:{type:"string",description:"Path to the codebase (default: current directory)"},agent:{type:"string",description:"Optional agent override (cursor, gemini, claude, codex, assistant). Omit to use configured agent."},output:{type:"string",description:"Output directory for spec files (default: test-specs)"}}}},{name:"run_test",description:"Start a test (async, returns runId). spec = file path, or inline:+steps, or a Jira-shaped issue key (e.g. PROJ-123): when Jira is connected, the runner loads that issue's description+comments into an inline spec. After starting, tell the user and let them ask for progress via run_status.",input_schema:{type:"object",properties:{spec:{type:"string",description:"Workspace file path; or inline:+steps; or Jira issue key (KEY-123) to auto-fetch from Jira when the jira skill is active."},ticketKey:{type:"string",description:"Optional label (e.g. SCRUM-123). If spec is an issue key, this defaults to that key."},agent:{type:"string",description:"Optional agent override (cursor, gemini, claude, codex, assistant). Omit to use configured agent."},headless:{type:"boolean",description:"Run browser headless (default false)"},workflow:{type:"string",description:"Workflow override (e.g. quick-smoke)"}},required:["spec"]}},{name:"run_status",description:'Instant progress check \u2014 returns immediately. Use this whenever user asks about test progress. ALWAYS use runId="all".',input_schema:{type:"object",properties:{runId:{type:"string",description:'Use "all" to see all runs in this session (recommended). Or a specific run ID if known.'}},required:["runId"]}},{name:"run_cancel",description:'Cancel/kill a running test. ONLY use when the USER explicitly asks to cancel or stop a run. NEVER auto-cancel \u2014 tests take 1-5 minutes and "running" is normal. Use runId="all" to cancel all active runs.',input_schema:{type:"object",properties:{runId:{type:"string",description:'Run ID to cancel, or "all" to cancel all active runs'}},required:["runId"]}},{name:"run_artifacts",description:"Read artifacts from a test run session. Can list files, read results/events/logs, or search across all sessions.",input_schema:{type:"object",properties:{runId:{type:"string",description:"Run ID from run_test. Omit to search across all sessions."},type:{type:"string",enum:["list","result","events","log","search"],description:'What to retrieve: "list" = all files in session, "result" = result.json, "events" = events.json, "log" = raw output tail, "search" = search text across sessions'},node:{type:"string",description:'Node name to read from (e.g. "execute_live", "generate_script"). Default: "execute_live"'},query:{type:"string",description:'Search text (only for type="search"). Searches across all session logs/events.'},tail:{type:"number",description:"Number of characters from end of log to return (default: 3000)"}},required:["type"]}},{name:"run_diagnose",description:"Diagnose one or all runs, especially failed ones. Uses run logs + known error patterns and returns likely root cause with suggested next action.",input_schema:{type:"object",properties:{runId:{type:"string",description:'Run ID from run_test, or "all" (default) to diagnose all known runs'},tail:{type:"number",description:"Characters of run log tail to inspect (default: 2000)"}}}},{name:"list_specs",description:"List available test spec files in the project",input_schema:{type:"object",properties:{directory:{type:"string",description:'Directory to scan (default: "test-specs")'}}}}]};function Ce(){let s=0;for(let[,t]of x)t.status==="running"&&s++;return s}function jt(){for(;Y.length>0;){let s=Jt(Y[0]?.context?.options?.config);if(Ce()>=s)break;let{args:t,cwd:e,context:r}=Y.shift();Bt(t,e,r)}}async function Ai(s,t){let{ticket:e,description:r,input:i,repo:n,agent:o,output:c}=s,a=["generate"];e&&a.push("--ticket",e),r&&a.push("--description",r),i&&a.push("--input",i),n&&a.push("--repo",n),c&&a.push("--output",c);let u=["assistant","cursor","claude","codex","gemini"],l=o||process.env.AGENT_TYPE,p=l&&u.includes(l)?l:null;p&&a.push("--agent",p);let d=e||"generate";return E(d,"\u{1F9EA}","Starting test spec generation (real agent with codebase access)..."),new Promise(m=>{Ut&&console.error(`[zibby:spawn] skill=run_generate parentPid=${process.pid} \u2192 child zibby ${a.map(g=>/\s/.test(g)?JSON.stringify(g):g).join(" ")} cwd=${t}`);let f=Pt("zibby",a,{cwd:t,env:{...process.env},stdio:["ignore","pipe","pipe"],detached:!1}),y="",_="";f.stdout.on("data",g=>{let b=g.toString();y+=b;for(let h of b.split(`
449
- `)){let v=Mt(h).trim();v.startsWith("\u2705")?E(d,"\u2705",v.slice(2).trim()):v.startsWith("\u2713")&&E(d,"\u2714",v.slice(2).trim())}}),f.stderr.on("data",g=>{_+=g.toString()}),f.on("close",g=>{if(g!==0){E(d,"\u274C",`Generation failed (exit ${g})`),m(JSON.stringify({error:`zibby generate failed with exit code ${g}`,stderr:_.slice(-1e3)}));return}let b=ee(t,c||"test-specs"),h=[];try{let v=e?e.toLowerCase().replace(/[^a-z0-9]+/g,"-"):"";h=ve(b).filter(T=>T.endsWith(".txt")&&(!v||T.startsWith(v))).map(T=>M(b,T))}catch{}E(d,"\u2705",`Generated ${h.length} test spec files`),m(JSON.stringify({success:!0,ticketKey:e||null,specFiles:h.map(v=>v.replace(`${t}/`,"")),total:h.length,message:`Generated ${h.length} specs. Now call run_test for each file.`}))}),f.on("error",g=>{E(d,"\u274C",`Spawn error: ${g.message}`),m(JSON.stringify({error:g.message}))})})}var Tt=1e5,Et=/^[A-Z][A-Z0-9]+-\d+$/,$i=new Set(["paragraph","heading","bulletList","orderedList","listItem","blockquote","codeBlock","rule","table","tableRow","tableCell","tableHeader","mediaSingle","panel"]);function ji(s,t){if(!t||!t.length)return s;let e=s;for(let r of t)r.type==="strong"?e=`**${e}**`:r.type==="em"?e=`_${e}_`:r.type==="code"?e=`\`${e}\``:r.type==="strike"?e=`~~${e}~~`:r.type==="link"&&r.attrs?.href&&(e=`[${e}](${r.attrs.href})`);return e}function we(s,t=0){if(!Array.isArray(s))return"";let e=[];for(let r of s){if(r.type==="text"){e.push(ji(r.text||"",r.marks));continue}if(r.type==="hardBreak"){e.push(`
456
+ Use run_artifacts({ runId, type }) and run_diagnose({ runId }) to inspect and explain failures.`,resolve(){return null},async handleToolCall(s,t,e){let r=e?.options?.workspace||process.cwd();try{switch(s){case"run_generate":return await Ci(t,r);case"run_test":return await Di(t,r,e);case"run_status":return Bi(t);case"run_cancel":return Ki(t);case"run_artifacts":return zi(t,r);case"run_diagnose":return Hi(t,r);case"list_specs":return Wi(t,r);default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(i){return JSON.stringify({error:i.message})}},tools:[{name:"run_generate",description:"Generate specs from codebase. CRITICAL: DO NOT USE if ticket has test steps in comments. Only use when: (1) NO steps in ticket AND (2) local codebase exists (not external URLs). For tickets with steps, use run_test with inline format.",input_schema:{type:"object",properties:{ticket:{type:"string",description:"Jira ticket key (e.g. SCRUM-123). Auto-fetches ticket details."},description:{type:"string",description:"Ticket description text (use if no Jira key available)"},input:{type:"string",description:"Path to a file containing ticket/requirements text"},repo:{type:"string",description:"Path to the codebase (default: current directory)"},agent:{type:"string",description:"Optional agent override (cursor, gemini, claude, codex, assistant). Omit to use configured agent."},output:{type:"string",description:"Output directory for spec files (default: test-specs)"}}}},{name:"run_test",description:"Start a test (async, returns runId). spec = file path, or inline:+steps, or a Jira-shaped issue key (e.g. PROJ-123): when Jira is connected, the runner loads that issue's description+comments into an inline spec. After starting, tell the user and let them ask for progress via run_status.",input_schema:{type:"object",properties:{spec:{type:"string",description:"Workspace file path; or inline:+steps; or Jira issue key (KEY-123) to auto-fetch from Jira when the jira skill is active."},ticketKey:{type:"string",description:"Optional label (e.g. SCRUM-123). If spec is an issue key, this defaults to that key."},agent:{type:"string",description:"Optional agent override (cursor, gemini, claude, codex, assistant). Omit to use configured agent."},headless:{type:"boolean",description:"Run browser headless (default false)"},workflow:{type:"string",description:"Workflow override (e.g. quick-smoke)"}},required:["spec"]}},{name:"run_status",description:'Instant progress check \u2014 returns immediately. Use this whenever user asks about test progress. ALWAYS use runId="all".',input_schema:{type:"object",properties:{runId:{type:"string",description:'Use "all" to see all runs in this session (recommended). Or a specific run ID if known.'}},required:["runId"]}},{name:"run_cancel",description:'Cancel/kill a running test. ONLY use when the USER explicitly asks to cancel or stop a run. NEVER auto-cancel \u2014 tests take 1-5 minutes and "running" is normal. Use runId="all" to cancel all active runs.',input_schema:{type:"object",properties:{runId:{type:"string",description:'Run ID to cancel, or "all" to cancel all active runs'}},required:["runId"]}},{name:"run_artifacts",description:"Read artifacts from a test run session. Can list files, read results/events/logs, or search across all sessions.",input_schema:{type:"object",properties:{runId:{type:"string",description:"Run ID from run_test. Omit to search across all sessions."},type:{type:"string",enum:["list","result","events","log","search"],description:'What to retrieve: "list" = all files in session, "result" = result.json, "events" = events.json, "log" = raw output tail, "search" = search text across sessions'},node:{type:"string",description:'Node name to read from (e.g. "execute_live", "generate_script"). Default: "execute_live"'},query:{type:"string",description:'Search text (only for type="search"). Searches across all session logs/events.'},tail:{type:"number",description:"Number of characters from end of log to return (default: 3000)"}},required:["type"]}},{name:"run_diagnose",description:"Diagnose one or all runs, especially failed ones. Uses run logs + known error patterns and returns likely root cause with suggested next action.",input_schema:{type:"object",properties:{runId:{type:"string",description:'Run ID from run_test, or "all" (default) to diagnose all known runs'},tail:{type:"number",description:"Characters of run log tail to inspect (default: 2000)"}}}},{name:"list_specs",description:"List available test spec files in the project",input_schema:{type:"object",properties:{directory:{type:"string",description:'Directory to scan (default: "test-specs")'}}}}]};function Ue(){let s=0;for(let[,t]of x)t.status==="running"&&s++;return s}function xt(){for(;V.length>0;){let s=Dt(V[0]?.context?.options?.config);if(Ue()>=s)break;let{args:t,cwd:e,context:r}=V.shift();zt(t,e,r)}}async function Ci(s,t){let{ticket:e,description:r,input:i,repo:n,agent:o,output:c}=s,a=["generate"];e&&a.push("--ticket",e),r&&a.push("--description",r),i&&a.push("--input",i),n&&a.push("--repo",n),c&&a.push("--output",c);let u=["assistant","cursor","claude","codex","gemini"],l=o||process.env.AGENT_TYPE,d=l&&u.includes(l)?l:null;d&&a.push("--agent",d);let p=e||"generate";return L(p,"\u{1F9EA}","Starting test spec generation (real agent with codebase access)..."),new Promise(m=>{Bt&&console.error(`[zibby:spawn] skill=run_generate parentPid=${process.pid} \u2192 child zibby ${a.map(g=>/\s/.test(g)?JSON.stringify(g):g).join(" ")} cwd=${t}`);let f=Mt("zibby",a,{cwd:t,env:{...process.env},stdio:["ignore","pipe","pipe"],detached:!1}),y="",_="";f.stdout.on("data",g=>{let b=g.toString();y+=b;for(let h of b.split(`
457
+ `)){let S=Gt(h).trim();S.startsWith("\u2705")?L(p,"\u2705",S.slice(2).trim()):S.startsWith("\u2713")&&L(p,"\u2714",S.slice(2).trim())}}),f.stderr.on("data",g=>{_+=g.toString()}),f.on("close",g=>{if(g!==0){L(p,"\u274C",`Generation failed (exit ${g})`),m(JSON.stringify({error:`zibby generate failed with exit code ${g}`,stderr:_.slice(-1e3)}));return}let b=re(t,c||"test-specs"),h=[];try{let S=e?e.toLowerCase().replace(/[^a-z0-9]+/g,"-"):"";h=Ie(b).filter(j=>j.endsWith(".txt")&&(!S||j.startsWith(S))).map(j=>B(b,j))}catch{}L(p,"\u2705",`Generated ${h.length} test spec files`),m(JSON.stringify({success:!0,ticketKey:e||null,specFiles:h.map(S=>S.replace(`${t}/`,"")),total:h.length,message:`Generated ${h.length} specs. Now call run_test for each file.`}))}),f.on("error",g=>{L(p,"\u274C",`Spawn error: ${g.message}`),m(JSON.stringify({error:g.message}))})})}var Ct=1e5,Pt=/^[A-Z][A-Z0-9]+-\d+$/,Pi=new Set(["paragraph","heading","bulletList","orderedList","listItem","blockquote","codeBlock","rule","table","tableRow","tableCell","tableHeader","mediaSingle","panel"]);function Ji(s,t){if(!t||!t.length)return s;let e=s;for(let r of t)r.type==="strong"?e=`**${e}**`:r.type==="em"?e=`_${e}_`:r.type==="code"?e=`\`${e}\``:r.type==="strike"?e=`~~${e}~~`:r.type==="link"&&r.attrs?.href&&(e=`[${e}](${r.attrs.href})`);return e}function Se(s,t=0){if(!Array.isArray(s))return"";let e=[];for(let r of s){if(r.type==="text"){e.push(Ji(r.text||"",r.marks));continue}if(r.type==="hardBreak"){e.push(`
450
458
  `);continue}if(r.type==="rule"){e.push(`
451
459
  ---
452
- `);continue}let i=r.content?we(r.content,t+1):"";if(r.type==="listItem")e.push(i);else if(r.type==="bulletList"){let n=(r.content||[]).map(o=>`- ${we(o.content||[],t+1).trim()}`);e.push(`
460
+ `);continue}let i=r.content?Se(r.content,t+1):"";if(r.type==="listItem")e.push(i);else if(r.type==="bulletList"){let n=(r.content||[]).map(o=>`- ${Se(o.content||[],t+1).trim()}`);e.push(`
453
461
  ${n.join(`
454
462
  `)}
455
- `)}else if(r.type==="orderedList"){let n=(r.content||[]).map((o,c)=>`${c+1}. ${we(o.content||[],t+1).trim()}`);e.push(`
463
+ `)}else if(r.type==="orderedList"){let n=(r.content||[]).map((o,c)=>`${c+1}. ${Se(o.content||[],t+1).trim()}`);e.push(`
456
464
  ${n.join(`
457
465
  `)}
458
466
  `)}else if(r.type==="heading"){let n=r.attrs?.level||2;e.push(`
459
467
 
460
468
  ${"#".repeat(n)} ${i.trim()}
461
469
 
462
- `)}else $i.has(r.type)?e.push(`
470
+ `)}else Pi.has(r.type)?e.push(`
463
471
 
464
472
  ${i}
465
473
  `):e.push(i)}return e.join("").replace(/\n{3,}/g,`
466
474
 
467
- `)}function Ti(s){return s==null||s===""?"":typeof s=="string"?s.trim():typeof s=="object"&&Array.isArray(s.content)?we(s.content).trim():""}async function Ei(s){let{getSkill:t}=await import("@zibby/agent-workflow"),e=t("jira");if(!e||typeof e.handleToolCall!="function")return null;try{let r=await e.handleToolCall("jira_get_issue",{issueKey:s}),i=JSON.parse(r);if(i?.error)return null;let n=await e.handleToolCall("jira_get_comments",{issueKey:s,maxResults:50}),o=JSON.parse(n);if(o?.error)return null;let c=Ti(i.description),a=[];c&&a.push(c);let u=Array.isArray(o.comments)?o.comments:[];if(u.length>0){let p=u.map(d=>String(d.body||"").trim()).filter(Boolean).join(`
475
+ `)}function Ui(s){return s==null||s===""?"":typeof s=="string"?s.trim():typeof s=="object"&&Array.isArray(s.content)?Se(s.content).trim():""}async function qi(s){let{getSkill:t}=await import("@zibby/agent-workflow"),e=t("jira");if(!e||typeof e.handleToolCall!="function")return null;try{let r=await e.handleToolCall("jira_get_issue",{issueKey:s}),i=JSON.parse(r);if(i?.error)return null;let n=await e.handleToolCall("jira_get_comments",{issueKey:s,maxResults:50}),o=JSON.parse(n);if(o?.error)return null;let c=Ui(i.description),a=[];c&&a.push(c);let u=Array.isArray(o.comments)?o.comments:[];if(u.length>0){let d=u.map(p=>String(p.body||"").trim()).filter(Boolean).join(`
468
476
 
469
- `);p&&a.push(p)}let l=a.join(`
477
+ `);d&&a.push(d)}let l=a.join(`
470
478
 
471
- `).trim();return l?(l.length>Tt&&(l=`${l.slice(0,Tt)}
479
+ `).trim();return l?(l.length>Ct&&(l=`${l.slice(0,Ct)}
472
480
 
473
- ...[truncated]`),{inlineSpec:`inline:${l}`,issueKey:s}):null}catch{return null}}function Li(s,t){try{let e=JSON.parse(s);return JSON.stringify({...e,...t})}catch{return s}}async function xi(s,t,e){let r={...s},i=String(r.spec??"").trim();if(!i)return JSON.stringify({error:"spec is required"});let n=null;if(Et.test(i)&&!i.startsWith("inline:")){let l=await Ei(i);l&&(i=l.inlineSpec,r.spec=i,String(r.ticketKey||"").trim()||(r.ticketKey=l.issueKey),n=l.issueKey)}let o=String(r.ticketKey||"").trim();if(o){for(let[l,p]of x.entries())if(p?.ticketKey===o&&!(p?.status!=="running"&&p?.status!=="queued"))return JSON.stringify({runId:l,ticketKey:o,status:p.status,reused:!0,message:`A run for ${o} is already ${p.status}. Reusing existing run instead of starting a duplicate.`})}if(!i.startsWith("inline:")){let l=ee(t,i);if(!K(l))return Et.test(i)?JSON.stringify({error:`Invalid run_test spec: "${i}" is an issue id, not a spec.`,reason:"Jira auto-load was attempted but did not return usable text, or Jira is not configured.",doNext:["Confirm the jira skill is active and authenticated.",'Or call tracker tools yourself, then run_test with spec: "inline:" + steps.'],validExample:{spec:"inline:1. Open https://example.com \u2026 2. Verify \u2026",ticketKey:i},invalidExample:{spec:i,ticketKey:i}}):JSON.stringify({error:`Test spec not found: ${i}`,hint:'If this should be issue steps, load the issue with your tracker tools first, then run_test with spec: "inline:" + steps. Otherwise use a real file path.'})}let c=Jt(e?.options?.config);if(Ce()>=c){let l=qt(),p=r.ticketKey||l,d={runId:l,spec:r.ticketKey?`${r.ticketKey}: ${r.spec}`:r.spec,ticketKey:r.ticketKey||null,status:"queued",startTime:Date.now(),exitCode:null,output:"",error:""};x.set(l,d),Y.push({args:{...r,_queuedRunId:l},cwd:t,context:e}),E(p,"\u23F3",`Queued (${Ce()}/${c} running, ${Y.length} queued)`);let m={runId:l,spec:d.spec,ticketKey:d.ticketKey,status:"queued",message:`Queued \u2014 will start when a slot opens (max ${c} concurrent).`};return n&&(m.resolvedFromJiraIssue=n,m.message+=` (spec built from Jira ${n})`),JSON.stringify(m)}let a=Date.now()-xe;a<At&&xe>0&&await new Promise(l=>setTimeout(l,At-a)),xe=Date.now();let u=Bt(r,t,e);return n?Li(u,{resolvedFromJiraIssue:n,message:`Spec was loaded from Jira issue ${n} (description + comments).`}):u}function Bt(s,t,e){let{spec:r,ticketKey:i,agent:n,headless:o,workflow:c,_queuedRunId:a}=s,u=a||qt(),l=r,p=!1;if(r.startsWith("inline:")){p=!0;let $=Ii(t);Rt($,{recursive:!0}),l=M($,`${u}.txt`),wi(l,r.slice(7).trim(),"utf-8")}let d=ee(t,".zibby","output","runs");Rt(d,{recursive:!0});let m=M(d,`${u}.log`),f=vi(m,{flags:"a"}),_=n&&["assistant","cursor","claude","codex","gemini"].includes(n)?n:null,g=["test",l];_&&g.push("--agent",_),o&&g.push("--headless"),c&&g.push("--workflow",c),Ut&&console.error(`[zibby:spawn] skill=run_test parentPid=${process.pid} \u2192 child zibby ${g.map($=>/\s/.test($)?JSON.stringify($):$).join(" ")} cwd=${t}`);let b=Pt("zibby",g,{cwd:t,env:{...process.env,ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS:"1"},stdio:["ignore","pipe","pipe"],detached:!1}),h={runId:u,spec:i?`${i}: ${r}`:r,ticketKey:i||null,specPath:l,logPath:m,isInline:p,pid:b.pid,status:"running",output:"",error:"",startTime:Date.now(),exitCode:null,currentNode:null,completedNodes:[]},v=i||u,T="";function st($){let O=Mt($).trim();if(!O)return;if(O.startsWith("__WORKFLOW_GRAPH_LOG__")){try{let j=JSON.parse(O.slice(22));j.phase==="node_begin"?h.currentNode=j.node:j.phase==="node_end"&&(j.node&&!h.completedNodes.includes(j.node)&&h.completedNodes.push(j.node),h.currentNode===j.node&&(h.currentNode=null))}catch{}return}let oe=O.match(/Session\s+(\S+)/);if(oe&&!h.sessionId&&(h.sessionId=oe[1],h.sessionPath=ee(t,Je,Pe,h.sessionId)),O.startsWith("\u250C ")||O.startsWith("\u250C ")){let j=O.slice(2).trim();h.currentNode=j,ke&&E(v,"\u25B6",`${j}`)}else if(O.startsWith("\u2514 ")||O.startsWith("\u2514 ")){let j=O.slice(2).trim();j.startsWith("done")?(h.currentNode&&!h.completedNodes.includes(h.currentNode)&&h.completedNodes.push(h.currentNode),ke&&E(v,"\u2714",`${h.currentNode||"node"} done ${j.replace("done","").trim()}`),h.currentNode=null):j.startsWith("failed")&&(ke&&E(v,"\u2718",`${h.currentNode||"node"} failed ${j.replace("failed","").trim()}`),h.currentNode=null)}else O.includes("Workflow completed")&&(h.currentNode=null,ke&&E(v,"\u2714",`Workflow completed (${$t(Date.now()-h.startTime)})`))}function Cr($){let O=$.toString();h.output+=O,f.write(O),h.output.length>5e4&&(h.output=h.output.slice(-3e4)),T+=O;let oe=T.split(`
474
- `);T=oe.pop();for(let j of oe)st(j)}return b.stdout.on("data",Cr),b.stderr.on("data",$=>{let O=$.toString();h.error+=O,f.write(O),h.error.length>2e4&&(h.error=h.error.slice(-1e4))}),b.on("close",$=>{h.status=$===0?"passed":"failed",h.exitCode=$,h.endTime=Date.now(),T&&st(T),f.end();let O=$t(Date.now()-h.startTime);if($===0?E(v,"\u2705",`Passed (${O})`):E(v,"\u274C",`Failed (${O})`),h.isInline)try{Si(h.specPath)}catch{}jt()}),b.on("error",$=>{h.status="error",h.error+=`
475
- Spawn error: ${$.message}`,E(v,"\u274C",`Spawn error: ${$.message}`),f.end(),jt()}),h._child=b,x.set(u,h),JSON.stringify({runId:u,spec:h.spec,ticketKey:h.ticketKey,status:"running",pid:b.pid,logFile:m})}function Lt(s){let t=Math.round(((s.endTime||Date.now())-s.startTime)/1e3),e=s.completedNodes||[],r=s.currentNode||null;if(s.status!=="running")return{elapsed:t,stage:s.status,completedNodes:e,currentNode:null};let i;return r?(i=`Actively executing node "${r}"`,e.length&&(i+=` (completed: ${e.join(", ")})`)):e.length?i=`Between nodes (completed: ${e.join(", ")})`:i="Starting up (initializing workflow)",i+=`. Elapsed: ${t}s. This is normal progress \u2014 do not cancel.`,{elapsed:t,stage:"running",currentNode:r,completedNodes:e,progress:i}}function Ci(s){let{runId:t}=s;if(!t)return JSON.stringify({error:"runId is required"});if(t==="all"){let n=[...x.entries()].map(([l,p])=>{let d=Lt(p),m={runId:l,spec:p.spec,ticketKey:p.ticketKey,status:p.status,elapsed:d.elapsed,exitCode:p.exitCode,sessionId:p.sessionId||null};return p.status==="running"?(m.currentNode=d.currentNode,m.completedNodes=d.completedNodes,m.progress=d.progress):m.outputTail=p.output.slice(-500),m}),o=n.filter(l=>l.status==="running").length,c=n.filter(l=>l.status==="passed").length,a=n.filter(l=>l.status==="failed").length,u={total:n.length,running:o,passed:c,failed:a,runs:n};return o>0&&(u._hint="All running tests are progressing normally through their workflow nodes. Do NOT cancel, diagnose, or interpret as stuck. Just tell the user they are still running."),JSON.stringify(u)}let e=x.get(t);if(!e)return JSON.stringify({error:`Run not found: ${t}`});let r=Lt(e),i={runId:t,spec:e.spec,ticketKey:e.ticketKey,status:e.status,elapsed:r.elapsed,exitCode:e.exitCode,sessionId:e.sessionId||null};return e.status==="running"?(i.currentNode=r.currentNode,i.completedNodes=r.completedNodes,i.progress=r.progress):(i.outputTail=e.output.slice(-1e3),i.errorTail=e.error.slice(-500)),e.status==="running"&&(i._hint="This run is actively progressing. Do NOT cancel, diagnose, or assume stuck. Just tell the user it is still running."),JSON.stringify(i)}function xt(s,t){if(t.status==="queued"){let e=Y.findIndex(r=>r.args._queuedRunId===s);return e>=0&&Y.splice(e,1),t.status="cancelled",t.endTime=Date.now(),{ok:!0,runId:s,status:"cancelled"}}if(t.status!=="running")return{ok:!1,runId:s,error:`Run is not active (status: ${t.status})`};try{return t._child.kill("SIGTERM"),t.status="cancelled",t.endTime=Date.now(),{ok:!0,runId:s,status:"cancelled"}}catch(e){return{ok:!1,runId:s,error:`Failed to cancel: ${e.message}`}}}function Pi(s){let{runId:t}=s;if(!t)return JSON.stringify({error:"runId is required"});if(t==="all"){let r=[];for(let[i,n]of x.entries())(n.status==="running"||n.status==="queued")&&r.push(xt(i,n));return r.length===0?JSON.stringify({ok:!0,message:"No active runs to cancel"}):JSON.stringify({ok:!0,cancelled:r.length,results:r})}let e=x.get(t);return JSON.stringify(e?xt(t,e):{error:`Run not found: ${t}`})}function Ji(s,t){let e=x.get(s);if(e?.sessionPath&&K(e.sessionPath))return e.sessionPath;if(e?.sessionId){let r=ee(t,Je,Pe,e.sessionId);if(K(r))return r}return null}function Kt(s,t=""){let e=[];if(!K(s))return e;for(let r of ve(s,{withFileTypes:!0})){let i=t?`${t}/${r.name}`:r.name;if(r.isDirectory())e.push(...Kt(M(s,r.name),i));else{let n=Ni(M(s,r.name));e.push({path:i,size:n.size})}}return e}function Ct(s){if(!K(s))return null;try{return JSON.parse(Se(s,"utf-8"))}catch{return null}}function Gt(s,t=2e3){if(!s||!K(s))return"";try{return Se(s,"utf-8").slice(-Math.max(200,Number(t)||2e3))}catch{return""}}function Ui({run:s,logTail:t,errorTail:e}){let i=`${t||""}
476
- ${e||""}`.toLowerCase(),n={runId:s?.runId||null,status:s?.status||null,exitCode:s?.exitCode??null,likelyCause:"Unknown failure",confidence:"low",nextStep:'Call run_artifacts({ runId, type: "log" }) with larger tail and inspect full logs.'};return s?.status==="running"||s?.status==="queued"?{...n,likelyCause:"Run is still active; no terminal failure to diagnose yet.",confidence:"high",nextStep:'Call run_status({ runId: "all" }) to check progress.'}:i.includes("test spec not found")?{...n,likelyCause:"Invalid spec input: run_test received a non-existent spec path.",confidence:"high",nextStep:"Use spec as inline:... or a real file path from list_specs. For ticket keys, fetch steps first via Jira then build inline spec."}:i.includes("unknown command")&&i.includes("'run'")?{...n,likelyCause:"CLI command mismatch (`zibby run` unsupported in current CLI).",confidence:"high",nextStep:"Use `zibby test ...` spawn path (runner should already do this)."}:i.includes("missing openai_api_key")||i.includes("didn't provide an api key")||i.includes("401")?{...n,likelyCause:"Provider authentication/config issue (API key/proxy auth missing or rejected).",confidence:"medium",nextStep:"Verify proxy/token env and auth mode, then retry once configuration is valid."}:i.includes("spawn error")||i.includes("enoent")?{...n,likelyCause:"Failed to spawn CLI process (binary/path/environment issue).",confidence:"medium",nextStep:"Confirm `zibby` is installed and available in PATH for the chat process."}:i.includes("security command failed")||i.includes("security process exited with code: 45")||i.includes("password not found for account")?{...n,likelyCause:"Cursor agent keychain/auth failed during preflight (often transient, more common under parallel starts).",confidence:"high",nextStep:'Retry failed ticket sequentially (not parallel), or run with a different agent via run_test({ ..., agent: "codex" }).'}:n}function qi(s,t){let{runId:e,type:r,node:i="execute_live",query:n,tail:o=3e3}=s;if(r==="search"){if(!n)return JSON.stringify({error:'query is required for type="search"'});let a=ee(t,Je,Pe);if(!K(a))return JSON.stringify({matches:[],message:"No sessions found"});let u=[],l=n.toLowerCase();for(let p of ve(a,{withFileTypes:!0})){if(!p.isDirectory())continue;let d=M(a,p.name),m=[{file:"execute_live/result.json",label:"result"},{file:"execute_live/events.json",label:"events"},{file:"execute_live/raw_stream_output.txt",label:"log"},{file:"generate_script/raw_stream_output.txt",label:"script_log"},{file:"title.txt",label:"title"}];for(let{file:f,label:y}of m){let _=M(d,f);if(K(_))try{let g=Se(_,"utf-8");if(g.toLowerCase().includes(l)){let b=g.toLowerCase().indexOf(l),h=Math.max(0,b-100),v=Math.min(g.length,b+n.length+100);u.push({sessionId:p.name,artifact:y,snippet:g.slice(h,v)})}}catch{}}if(u.length>=20)break}return JSON.stringify({query:n,matches:u,total:u.length})}if(!e)return JSON.stringify({error:"runId is required for this type"});if(r==="log"){let a=x.get(e),u=Gt(a?.logPath,o);if(u)return JSON.stringify({runId:e,source:"run-log",totalLength:u.length,tail:u})}let c=Ji(e,t);if(!c)return JSON.stringify({error:`No session found for run ${e}. The run may still be starting.`});switch(r){case"list":{let a=Kt(c);return JSON.stringify({sessionId:c.split("/").pop(),files:a,total:a.length})}case"result":{let a=Ct(M(c,i,"result.json"));return JSON.stringify(a?{sessionId:c.split("/").pop(),node:i,result:a}:{error:`No result.json found in ${i}`})}case"events":{let a=Ct(M(c,i,"events.json"));if(!a)return JSON.stringify({error:`No events.json found in ${i}`});let u=Array.isArray(a)?a:a.events||[];return JSON.stringify({sessionId:c.split("/").pop(),node:i,totalEvents:u.length,events:u.slice(-50)})}case"log":{let a=M(c,i,"raw_stream_output.txt");if(!K(a))return JSON.stringify({error:`No log found in ${i}`});let u=Se(a,"utf-8");return JSON.stringify({sessionId:c.split("/").pop(),node:i,totalLength:u.length,tail:u.slice(-o)})}default:return JSON.stringify({error:`Unknown artifact type: ${r}. Use: list, result, events, log, search`})}}function Mi(s,t){let e=String(s?.runId||"all"),r=Number(s?.tail||2e3),i=e==="all"?[...x.keys()]:[e];if(i.length===0)return JSON.stringify({error:"No runs available to diagnose. Call run_test first."});let n=i.map(a=>{let u=x.get(a);if(!u)return{runId:a,error:`Run not found: ${a}`};let l=Gt(u.logPath,r),p=String(u.error||"").slice(-Math.max(200,r));return{...Ui({run:u,logTail:l,errorTail:p}),ticketKey:u.ticketKey||null,spec:u.spec,logTail:l,errorTail:p}}),o=n.filter(a=>a.status==="failed"||a.status==="error"),c=n.filter(a=>a.status==="running"||a.status==="queued");return JSON.stringify({total:n.length,failed:o.length,active:c.length,diagnoses:n})}function Di(s,t){let e=s?.directory||"test-specs",r=ee(t,e);if(!K(r))return JSON.stringify({specs:[],directory:e,message:`Directory not found: ${e}`});try{let n=function(o,c){for(let a of ve(o,{withFileTypes:!0})){let u=c?`${c}/${a.name}`:a.name;a.isDirectory()?n(M(o,a.name),u):(a.name.endsWith(".txt")||a.name.endsWith(".md"))&&i.push(u)}},i=[];return n(r,""),JSON.stringify({specs:i.map(o=>`${e}/${o}`),total:i.length,directory:e})}catch(i){return JSON.stringify({error:i.message})}}import{spawn as Bi}from"child_process";import{existsSync as G,mkdirSync as Ki,readdirSync as Ft,statSync as Gi,readFileSync as Fi}from"fs";import{resolve as qe,join as F,basename as zi}from"path";var Me=".zibby/repos";function Ne(s,t,e={}){return new Promise((r,i)=>{let n=Bi(s,{cwd:t,shell:!0,env:{...process.env,GIT_TERMINAL_PROMPT:"0",...e}}),o="",c="";n.stdout.on("data",a=>{o+=a.toString()}),n.stderr.on("data",a=>{c+=a.toString()}),n.on("close",a=>{a!==0?i(new Error(`Exit ${a}: ${c.trim()||o.trim()}`)):r(o.trim())}),n.on("error",a=>i(a))})}var zt={id:"git",description:"Clone and manage git repositories for codebase analysis",envKeys:["GITHUB_TOKEN","GITLAB_TOKEN"],promptFragment:`## Git Repositories
481
+ ...[truncated]`),{inlineSpec:`inline:${l}`,issueKey:s}):null}catch{return null}}function Mi(s,t){try{let e=JSON.parse(s);return JSON.stringify({...e,...t})}catch{return s}}async function Di(s,t,e){let r={...s},i=String(r.spec??"").trim();if(!i)return JSON.stringify({error:"spec is required"});let n=null;if(Pt.test(i)&&!i.startsWith("inline:")){let l=await qi(i);l&&(i=l.inlineSpec,r.spec=i,String(r.ticketKey||"").trim()||(r.ticketKey=l.issueKey),n=l.issueKey)}let o=String(r.ticketKey||"").trim();if(o){for(let[l,d]of x.entries())if(d?.ticketKey===o&&!(d?.status!=="running"&&d?.status!=="queued"))return JSON.stringify({runId:l,ticketKey:o,status:d.status,reused:!0,message:`A run for ${o} is already ${d.status}. Reusing existing run instead of starting a duplicate.`})}if(!i.startsWith("inline:")){let l=re(t,i);if(!G(l))return Pt.test(i)?JSON.stringify({error:`Invalid run_test spec: "${i}" is an issue id, not a spec.`,reason:"Jira auto-load was attempted but did not return usable text, or Jira is not configured.",doNext:["Confirm the jira skill is active and authenticated.",'Or call tracker tools yourself, then run_test with spec: "inline:" + steps.'],validExample:{spec:"inline:1. Open https://example.com \u2026 2. Verify \u2026",ticketKey:i},invalidExample:{spec:i,ticketKey:i}}):JSON.stringify({error:`Test spec not found: ${i}`,hint:'If this should be issue steps, load the issue with your tracker tools first, then run_test with spec: "inline:" + steps. Otherwise use a real file path.'})}let c=Dt(e?.options?.config);if(Ue()>=c){let l=Kt(),d=r.ticketKey||l,p={runId:l,spec:r.ticketKey?`${r.ticketKey}: ${r.spec}`:r.spec,ticketKey:r.ticketKey||null,status:"queued",startTime:Date.now(),exitCode:null,output:"",error:""};x.set(l,p),V.push({args:{...r,_queuedRunId:l},cwd:t,context:e}),L(d,"\u23F3",`Queued (${Ue()}/${c} running, ${V.length} queued)`);let m={runId:l,spec:p.spec,ticketKey:p.ticketKey,status:"queued",message:`Queued \u2014 will start when a slot opens (max ${c} concurrent).`};return n&&(m.resolvedFromJiraIssue=n,m.message+=` (spec built from Jira ${n})`),JSON.stringify(m)}let a=Date.now()-Je;a<Et&&Je>0&&await new Promise(l=>setTimeout(l,Et-a)),Je=Date.now();let u=zt(r,t,e);return n?Mi(u,{resolvedFromJiraIssue:n,message:`Spec was loaded from Jira issue ${n} (description + comments).`}):u}function zt(s,t,e){let{spec:r,ticketKey:i,agent:n,headless:o,workflow:c,_queuedRunId:a}=s,u=a||Kt(),l=r,d=!1;if(r.startsWith("inline:")){d=!0;let R=Ei(t);Tt(R,{recursive:!0}),l=B(R,`${u}.txt`),Ai(l,r.slice(7).trim(),"utf-8")}let p=re(t,".zibby","output","runs");Tt(p,{recursive:!0});let m=B(p,`${u}.log`),f=ji(m,{flags:"a"}),_=n&&["assistant","cursor","claude","codex","gemini"].includes(n)?n:null,g=["test",l];_&&g.push("--agent",_),o&&g.push("--headless"),c&&g.push("--workflow",c),Bt&&console.error(`[zibby:spawn] skill=run_test parentPid=${process.pid} \u2192 child zibby ${g.map(R=>/\s/.test(R)?JSON.stringify(R):R).join(" ")} cwd=${t}`);let b=Mt("zibby",g,{cwd:t,env:{...process.env,ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS:"1"},stdio:["ignore","pipe","pipe"],detached:!1}),h={runId:u,spec:i?`${i}: ${r}`:r,ticketKey:i||null,specPath:l,logPath:m,isInline:d,pid:b.pid,status:"running",output:"",error:"",startTime:Date.now(),exitCode:null,currentNode:null,completedNodes:[]},S=i||u,j="";function Y(R){let I=Gt(R).trim();if(!I)return;if(I.startsWith("__WORKFLOW_GRAPH_LOG__")){try{let T=JSON.parse(I.slice(22));T.phase==="node_begin"?h.currentNode=T.node:T.phase==="node_end"&&(T.node&&!h.completedNodes.includes(T.node)&&h.completedNodes.push(T.node),h.currentNode===T.node&&(h.currentNode=null))}catch{}return}let q=I.match(/Session\s+(\S+)/);if(q&&!h.sessionId&&(h.sessionId=q[1],h.sessionPath=re(t,Me,qe,h.sessionId)),I.startsWith("\u250C ")||I.startsWith("\u250C ")){let T=I.slice(2).trim();h.currentNode=T,ke&&L(S,"\u25B6",`${T}`)}else if(I.startsWith("\u2514 ")||I.startsWith("\u2514 ")){let T=I.slice(2).trim();T.startsWith("done")?(h.currentNode&&!h.completedNodes.includes(h.currentNode)&&h.completedNodes.push(h.currentNode),ke&&L(S,"\u2714",`${h.currentNode||"node"} done ${T.replace("done","").trim()}`),h.currentNode=null):T.startsWith("failed")&&(ke&&L(S,"\u2718",`${h.currentNode||"node"} failed ${T.replace("failed","").trim()}`),h.currentNode=null)}else I.includes("Workflow completed")&&(h.currentNode=null,ke&&L(S,"\u2714",`Workflow completed (${Lt(Date.now()-h.startTime)})`))}function Ee(R){let I=R.toString();h.output+=I,f.write(I),h.output.length>5e4&&(h.output=h.output.slice(-3e4)),j+=I;let q=j.split(`
482
+ `);j=q.pop();for(let T of q)Y(T)}return b.stdout.on("data",Ee),b.stderr.on("data",R=>{let I=R.toString();h.error+=I,f.write(I),h.error.length>2e4&&(h.error=h.error.slice(-1e4))}),b.on("close",R=>{h.status=R===0?"passed":"failed",h.exitCode=R,h.endTime=Date.now(),j&&Y(j),f.end();let I=Lt(Date.now()-h.startTime);if(R===0?L(S,"\u2705",`Passed (${I})`):L(S,"\u274C",`Failed (${I})`),h.isInline)try{$i(h.specPath)}catch{}xt()}),b.on("error",R=>{h.status="error",h.error+=`
483
+ Spawn error: ${R.message}`,L(S,"\u274C",`Spawn error: ${R.message}`),f.end(),xt()}),h._child=b,x.set(u,h),JSON.stringify({runId:u,spec:h.spec,ticketKey:h.ticketKey,status:"running",pid:b.pid,logFile:m})}function Jt(s){let t=Math.round(((s.endTime||Date.now())-s.startTime)/1e3),e=s.completedNodes||[],r=s.currentNode||null;if(s.status!=="running")return{elapsed:t,stage:s.status,completedNodes:e,currentNode:null};let i;return r?(i=`Actively executing node "${r}"`,e.length&&(i+=` (completed: ${e.join(", ")})`)):e.length?i=`Between nodes (completed: ${e.join(", ")})`:i="Starting up (initializing workflow)",i+=`. Elapsed: ${t}s. This is normal progress \u2014 do not cancel.`,{elapsed:t,stage:"running",currentNode:r,completedNodes:e,progress:i}}function Bi(s){let{runId:t}=s;if(!t)return JSON.stringify({error:"runId is required"});if(t==="all"){let n=[...x.entries()].map(([l,d])=>{let p=Jt(d),m={runId:l,spec:d.spec,ticketKey:d.ticketKey,status:d.status,elapsed:p.elapsed,exitCode:d.exitCode,sessionId:d.sessionId||null};return d.status==="running"?(m.currentNode=p.currentNode,m.completedNodes=p.completedNodes,m.progress=p.progress):m.outputTail=d.output.slice(-500),m}),o=n.filter(l=>l.status==="running").length,c=n.filter(l=>l.status==="passed").length,a=n.filter(l=>l.status==="failed").length,u={total:n.length,running:o,passed:c,failed:a,runs:n};return o>0&&(u._hint="All running tests are progressing normally through their workflow nodes. Do NOT cancel, diagnose, or interpret as stuck. Just tell the user they are still running."),JSON.stringify(u)}let e=x.get(t);if(!e)return JSON.stringify({error:`Run not found: ${t}`});let r=Jt(e),i={runId:t,spec:e.spec,ticketKey:e.ticketKey,status:e.status,elapsed:r.elapsed,exitCode:e.exitCode,sessionId:e.sessionId||null};return e.status==="running"?(i.currentNode=r.currentNode,i.completedNodes=r.completedNodes,i.progress=r.progress):(i.outputTail=e.output.slice(-1e3),i.errorTail=e.error.slice(-500)),e.status==="running"&&(i._hint="This run is actively progressing. Do NOT cancel, diagnose, or assume stuck. Just tell the user it is still running."),JSON.stringify(i)}function Ut(s,t){if(t.status==="queued"){let e=V.findIndex(r=>r.args._queuedRunId===s);return e>=0&&V.splice(e,1),t.status="cancelled",t.endTime=Date.now(),{ok:!0,runId:s,status:"cancelled"}}if(t.status!=="running")return{ok:!1,runId:s,error:`Run is not active (status: ${t.status})`};try{return t._child.kill("SIGTERM"),t.status="cancelled",t.endTime=Date.now(),{ok:!0,runId:s,status:"cancelled"}}catch(e){return{ok:!1,runId:s,error:`Failed to cancel: ${e.message}`}}}function Ki(s){let{runId:t}=s;if(!t)return JSON.stringify({error:"runId is required"});if(t==="all"){let r=[];for(let[i,n]of x.entries())(n.status==="running"||n.status==="queued")&&r.push(Ut(i,n));return r.length===0?JSON.stringify({ok:!0,message:"No active runs to cancel"}):JSON.stringify({ok:!0,cancelled:r.length,results:r})}let e=x.get(t);return JSON.stringify(e?Ut(t,e):{error:`Run not found: ${t}`})}function Gi(s,t){let e=x.get(s);if(e?.sessionPath&&G(e.sessionPath))return e.sessionPath;if(e?.sessionId){let r=re(t,Me,qe,e.sessionId);if(G(r))return r}return null}function Ht(s,t=""){let e=[];if(!G(s))return e;for(let r of Ie(s,{withFileTypes:!0})){let i=t?`${t}/${r.name}`:r.name;if(r.isDirectory())e.push(...Ht(B(s,r.name),i));else{let n=Ti(B(s,r.name));e.push({path:i,size:n.size})}}return e}function qt(s){if(!G(s))return null;try{return JSON.parse(ve(s,"utf-8"))}catch{return null}}function Wt(s,t=2e3){if(!s||!G(s))return"";try{return ve(s,"utf-8").slice(-Math.max(200,Number(t)||2e3))}catch{return""}}function Fi({run:s,logTail:t,errorTail:e}){let i=`${t||""}
484
+ ${e||""}`.toLowerCase(),n={runId:s?.runId||null,status:s?.status||null,exitCode:s?.exitCode??null,likelyCause:"Unknown failure",confidence:"low",nextStep:'Call run_artifacts({ runId, type: "log" }) with larger tail and inspect full logs.'};return s?.status==="running"||s?.status==="queued"?{...n,likelyCause:"Run is still active; no terminal failure to diagnose yet.",confidence:"high",nextStep:'Call run_status({ runId: "all" }) to check progress.'}:i.includes("test spec not found")?{...n,likelyCause:"Invalid spec input: run_test received a non-existent spec path.",confidence:"high",nextStep:"Use spec as inline:... or a real file path from list_specs. For ticket keys, fetch steps first via Jira then build inline spec."}:i.includes("unknown command")&&i.includes("'run'")?{...n,likelyCause:"CLI command mismatch (`zibby run` unsupported in current CLI).",confidence:"high",nextStep:"Use `zibby test ...` spawn path (runner should already do this)."}:i.includes("missing openai_api_key")||i.includes("didn't provide an api key")||i.includes("401")?{...n,likelyCause:"Provider authentication/config issue (API key/proxy auth missing or rejected).",confidence:"medium",nextStep:"Verify proxy/token env and auth mode, then retry once configuration is valid."}:i.includes("spawn error")||i.includes("enoent")?{...n,likelyCause:"Failed to spawn CLI process (binary/path/environment issue).",confidence:"medium",nextStep:"Confirm `zibby` is installed and available in PATH for the chat process."}:i.includes("security command failed")||i.includes("security process exited with code: 45")||i.includes("password not found for account")?{...n,likelyCause:"Cursor agent keychain/auth failed during preflight (often transient, more common under parallel starts).",confidence:"high",nextStep:'Retry failed ticket sequentially (not parallel), or run with a different agent via run_test({ ..., agent: "codex" }).'}:n}function zi(s,t){let{runId:e,type:r,node:i="execute_live",query:n,tail:o=3e3}=s;if(r==="search"){if(!n)return JSON.stringify({error:'query is required for type="search"'});let a=re(t,Me,qe);if(!G(a))return JSON.stringify({matches:[],message:"No sessions found"});let u=[],l=n.toLowerCase();for(let d of Ie(a,{withFileTypes:!0})){if(!d.isDirectory())continue;let p=B(a,d.name),m=[{file:"execute_live/result.json",label:"result"},{file:"execute_live/events.json",label:"events"},{file:"execute_live/raw_stream_output.txt",label:"log"},{file:"generate_script/raw_stream_output.txt",label:"script_log"},{file:"title.txt",label:"title"}];for(let{file:f,label:y}of m){let _=B(p,f);if(G(_))try{let g=ve(_,"utf-8");if(g.toLowerCase().includes(l)){let b=g.toLowerCase().indexOf(l),h=Math.max(0,b-100),S=Math.min(g.length,b+n.length+100);u.push({sessionId:d.name,artifact:y,snippet:g.slice(h,S)})}}catch{}}if(u.length>=20)break}return JSON.stringify({query:n,matches:u,total:u.length})}if(!e)return JSON.stringify({error:"runId is required for this type"});if(r==="log"){let a=x.get(e),u=Wt(a?.logPath,o);if(u)return JSON.stringify({runId:e,source:"run-log",totalLength:u.length,tail:u})}let c=Gi(e,t);if(!c)return JSON.stringify({error:`No session found for run ${e}. The run may still be starting.`});switch(r){case"list":{let a=Ht(c);return JSON.stringify({sessionId:c.split("/").pop(),files:a,total:a.length})}case"result":{let a=qt(B(c,i,"result.json"));return JSON.stringify(a?{sessionId:c.split("/").pop(),node:i,result:a}:{error:`No result.json found in ${i}`})}case"events":{let a=qt(B(c,i,"events.json"));if(!a)return JSON.stringify({error:`No events.json found in ${i}`});let u=Array.isArray(a)?a:a.events||[];return JSON.stringify({sessionId:c.split("/").pop(),node:i,totalEvents:u.length,events:u.slice(-50)})}case"log":{let a=B(c,i,"raw_stream_output.txt");if(!G(a))return JSON.stringify({error:`No log found in ${i}`});let u=ve(a,"utf-8");return JSON.stringify({sessionId:c.split("/").pop(),node:i,totalLength:u.length,tail:u.slice(-o)})}default:return JSON.stringify({error:`Unknown artifact type: ${r}. Use: list, result, events, log, search`})}}function Hi(s,t){let e=String(s?.runId||"all"),r=Number(s?.tail||2e3),i=e==="all"?[...x.keys()]:[e];if(i.length===0)return JSON.stringify({error:"No runs available to diagnose. Call run_test first."});let n=i.map(a=>{let u=x.get(a);if(!u)return{runId:a,error:`Run not found: ${a}`};let l=Wt(u.logPath,r),d=String(u.error||"").slice(-Math.max(200,r));return{...Fi({run:u,logTail:l,errorTail:d}),ticketKey:u.ticketKey||null,spec:u.spec,logTail:l,errorTail:d}}),o=n.filter(a=>a.status==="failed"||a.status==="error"),c=n.filter(a=>a.status==="running"||a.status==="queued");return JSON.stringify({total:n.length,failed:o.length,active:c.length,diagnoses:n})}function Wi(s,t){let e=s?.directory||"test-specs",r=re(t,e);if(!G(r))return JSON.stringify({specs:[],directory:e,message:`Directory not found: ${e}`});try{let n=function(o,c){for(let a of Ie(o,{withFileTypes:!0})){let u=c?`${c}/${a.name}`:a.name;a.isDirectory()?n(B(o,a.name),u):(a.name.endsWith(".txt")||a.name.endsWith(".md"))&&i.push(u)}},i=[];return n(r,""),JSON.stringify({specs:i.map(o=>`${e}/${o}`),total:i.length,directory:e})}catch(i){return JSON.stringify({error:i.message})}}import{spawn as Yi}from"child_process";import{existsSync as F,mkdirSync as Zi,readdirSync as Yt,statSync as Vi,readFileSync as Qi}from"fs";import{resolve as Be,join as z,basename as Xi}from"path";var Ke=".zibby/repos";function Ne(s,t,e={}){return new Promise((r,i)=>{let n=Yi(s,{cwd:t,shell:!0,env:{...process.env,GIT_TERMINAL_PROMPT:"0",...e}}),o="",c="";n.stdout.on("data",a=>{o+=a.toString()}),n.stderr.on("data",a=>{c+=a.toString()}),n.on("close",a=>{a!==0?i(new Error(`Exit ${a}: ${c.trim()||o.trim()}`)):r(o.trim())}),n.on("error",a=>i(a))})}var Zt={id:"git",description:"Clone and manage git repositories for codebase analysis",envKeys:["GITHUB_TOKEN","GITLAB_TOKEN"],promptFragment:`## Git Repositories
477
485
  You can clone and explore git repositories locally for codebase analysis:
478
486
  - git_checkout: Clone a repo (or pull if already cloned). Supports GitHub and GitLab with auto-auth.
479
487
  - git_list_repos: List locally cloned repos
@@ -484,7 +492,7 @@ When a test ticket lacks context, use this workflow:
484
492
  2. Use git_explore to understand the project structure
485
493
  3. Use shell commands (grep, cat) to read specific files for deeper understanding
486
494
  4. Use GitHub/GitLab skills to read related PRs and commits
487
- 5. Build well-informed test specs and save them to files before running tests`,resolve(){return null},async handleToolCall(s,t,e){let r=e?.options?.workspace||process.cwd();try{switch(s){case"git_checkout":return await Wi(t,r);case"git_list_repos":return Hi(t,r);case"git_explore":return Yi(t,r);default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(i){return JSON.stringify({error:i.message})}},tools:[{name:"git_checkout",description:"Clone a git repository locally (or pull latest if already cloned). Auto-authenticates with GitHub/GitLab tokens if available.",input_schema:{type:"object",properties:{url:{type:"string",description:'Repository URL (e.g. "https://github.com/org/repo" or "org/repo" shorthand for GitHub)'},branch:{type:"string",description:"Branch to checkout (default: repo default branch)"},shallow:{type:"boolean",description:"Shallow clone with depth 1 (default: true, faster)"},name:{type:"string",description:"Local directory name override (default: repo name from URL)"}},required:["url"]}},{name:"git_list_repos",description:"List locally cloned repositories",input_schema:{type:"object",properties:{}}},{name:"git_explore",description:"Quick structural overview of a cloned repo: key files, package.json info, directory tree (top 2 levels), detected framework/language",input_schema:{type:"object",properties:{repo:{type:"string",description:"Repo name (as listed by git_list_repos)"},depth:{type:"number",description:"Directory tree depth (default: 2)"}},required:["repo"]}}]};async function Wi(s,t){let{url:e,branch:r,shallow:i=!0,name:n}=s;!e.includes("://")&&!e.startsWith("git@")&&(e=`https://github.com/${e}`);let o=n||zi(e.replace(/\.git$/,"")),c=qe(t,Me);Ki(c,{recursive:!0});let a=F(c,o),u=e,l=process.env.GITHUB_TOKEN,p=process.env.GITLAB_TOKEN,d=process.env.GITLAB_URL;if(e.includes("github.com")&&l)u=e.replace("https://github.com",`https://x-access-token:${l}@github.com`);else if(p&&d)try{let y=new URL(d).host;e.includes(y)&&(u=e.replace(`https://${y}`,`https://oauth2:${p}@${y}`))}catch{}if(G(F(a,".git"))){let y=r?`git -C "${a}" fetch origin ${r} && git -C "${a}" checkout ${r} && git -C "${a}" pull origin ${r}`:`git -C "${a}" pull`;await Ne(y,t);let _=await Ne(`git -C "${a}" log -1 --format="%h %s"`,t);return JSON.stringify({action:"updated",repo:o,path:a,branch:r||"default",head:_})}let m=["git","clone"];i&&m.push("--depth","1"),r&&m.push("--branch",r),m.push(`"${u}"`,`"${a}"`),await Ne(m.join(" "),t);let f=await Ne(`git -C "${a}" log -1 --format="%h %s"`,t);return JSON.stringify({action:"cloned",repo:o,path:a,branch:r||"default",shallow:i,head:f})}function Hi(s,t){let e=qe(t,Me);if(!G(e))return JSON.stringify({repos:[],message:"No repos cloned yet"});let r=[];for(let i of Ft(e,{withFileTypes:!0})){if(!i.isDirectory())continue;let n=F(e,i.name);if(!G(F(n,".git")))continue;let o=Gi(n);r.push({name:i.name,path:n,lastModified:o.mtime.toISOString()})}return JSON.stringify({repos:r,total:r.length,directory:e})}function Yi(s,t){let{repo:e,depth:r=2}=s,i=qe(t,Me,e);if(!G(i))return JSON.stringify({error:`Repo not found: ${e}. Run git_checkout first.`});let n={repo:e,path:i},o=F(i,"package.json");if(G(o))try{let m=JSON.parse(Fi(o,"utf-8"));n.packageJson={name:m.name,version:m.version,scripts:m.scripts?Object.keys(m.scripts):[],dependencies:m.dependencies?Object.keys(m.dependencies).slice(0,30):[],devDependencies:m.devDependencies?Object.keys(m.devDependencies).slice(0,20):[]},m.dependencies?.react?n.framework="React":m.dependencies?.next?n.framework="Next.js":m.dependencies?.vue?n.framework="Vue":m.dependencies?.angular?n.framework="Angular":m.dependencies?.express?n.framework="Express":m.dependencies?.fastify&&(n.framework="Fastify")}catch{}let c=F(i,"pyproject.toml");G(c)&&(n.language="Python");let a=F(i,"go.mod");G(a)&&(n.language="Go");let u=F(i,"Cargo.toml");G(u)&&(n.language="Rust"),G(o)&&(n.language=n.language||"JavaScript/TypeScript");let l=[];function p(m,f,y){if(y>r)return;let _;try{_=Ft(m,{withFileTypes:!0})}catch{return}let g=_.filter(b=>!b.name.startsWith(".")&&b.name!=="node_modules"&&b.name!=="__pycache__"&&b.name!=="dist"&&b.name!=="build"&&b.name!==".git").sort((b,h)=>b.isDirectory()!==h.isDirectory()?b.isDirectory()?-1:1:b.name.localeCompare(h.name));for(let b of g){let h=b.isDirectory();l.push(`${f}${h?"\u{1F4C1}":"\u{1F4C4}"} ${b.name}`),h&&y<r&&p(F(m,b.name),`${f} `,y+1)}}p(i,"",1),n.tree=l.slice(0,80),l.length>80&&(n.treeTruncated=!0);let d=["README.md","README.rst","src/App.tsx","src/App.jsx","src/App.js","src/routes.tsx","src/routes.js","app/routes.tsx","app/routes.js","src/index.tsx","src/index.ts","src/main.tsx","src/main.ts","pages/_app.tsx","pages/_app.js","app/layout.tsx","docker-compose.yml","Dockerfile",".env.example"];return n.keyFilesFound=d.filter(m=>G(F(i,m))),JSON.stringify(n)}import{execFileSync as Vt}from"child_process";import{existsSync as ze,mkdirSync as Qt}from"fs";import{join as z,basename as Zi}from"path";import{randomBytes as Vi}from"crypto";import{pathToFileURL as pe}from"url";import{createRequire as Xt}from"module";var Ke=".zibby/memory",er="dolt",tr={encoding:"utf-8",stdio:["pipe","pipe","pipe"],timeout:15e3},Ge="mem0",De=new Map,Ie=new Map,Oe=!1,Qi=Xt(import.meta.url),je=()=>Vi(8).toString("hex"),te=()=>new Date().toISOString(),Xi=[`CREATE TABLE IF NOT EXISTS chat_memory (
495
+ 5. Build well-informed test specs and save them to files before running tests`,resolve(){return null},async handleToolCall(s,t,e){let r=e?.options?.workspace||process.cwd();try{switch(s){case"git_checkout":return await en(t,r);case"git_list_repos":return tn(t,r);case"git_explore":return rn(t,r);default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(i){return JSON.stringify({error:i.message})}},tools:[{name:"git_checkout",description:"Clone a git repository locally (or pull latest if already cloned). Auto-authenticates with GitHub/GitLab tokens if available.",input_schema:{type:"object",properties:{url:{type:"string",description:'Repository URL (e.g. "https://github.com/org/repo" or "org/repo" shorthand for GitHub)'},branch:{type:"string",description:"Branch to checkout (default: repo default branch)"},shallow:{type:"boolean",description:"Shallow clone with depth 1 (default: true, faster)"},name:{type:"string",description:"Local directory name override (default: repo name from URL)"}},required:["url"]}},{name:"git_list_repos",description:"List locally cloned repositories",input_schema:{type:"object",properties:{}}},{name:"git_explore",description:"Quick structural overview of a cloned repo: key files, package.json info, directory tree (top 2 levels), detected framework/language",input_schema:{type:"object",properties:{repo:{type:"string",description:"Repo name (as listed by git_list_repos)"},depth:{type:"number",description:"Directory tree depth (default: 2)"}},required:["repo"]}}]};async function en(s,t){let{url:e,branch:r,shallow:i=!0,name:n}=s;!e.includes("://")&&!e.startsWith("git@")&&(e=`https://github.com/${e}`);let o=n||Xi(e.replace(/\.git$/,"")),c=Be(t,Ke);Zi(c,{recursive:!0});let a=z(c,o),u=e,l=process.env.GITHUB_TOKEN,d=process.env.GITLAB_TOKEN,p=process.env.GITLAB_URL;if(e.includes("github.com")&&l)u=e.replace("https://github.com",`https://x-access-token:${l}@github.com`);else if(d&&p)try{let y=new URL(p).host;e.includes(y)&&(u=e.replace(`https://${y}`,`https://oauth2:${d}@${y}`))}catch{}if(F(z(a,".git"))){let y=r?`git -C "${a}" fetch origin ${r} && git -C "${a}" checkout ${r} && git -C "${a}" pull origin ${r}`:`git -C "${a}" pull`;await Ne(y,t);let _=await Ne(`git -C "${a}" log -1 --format="%h %s"`,t);return JSON.stringify({action:"updated",repo:o,path:a,branch:r||"default",head:_})}let m=["git","clone"];i&&m.push("--depth","1"),r&&m.push("--branch",r),m.push(`"${u}"`,`"${a}"`),await Ne(m.join(" "),t);let f=await Ne(`git -C "${a}" log -1 --format="%h %s"`,t);return JSON.stringify({action:"cloned",repo:o,path:a,branch:r||"default",shallow:i,head:f})}function tn(s,t){let e=Be(t,Ke);if(!F(e))return JSON.stringify({repos:[],message:"No repos cloned yet"});let r=[];for(let i of Yt(e,{withFileTypes:!0})){if(!i.isDirectory())continue;let n=z(e,i.name);if(!F(z(n,".git")))continue;let o=Vi(n);r.push({name:i.name,path:n,lastModified:o.mtime.toISOString()})}return JSON.stringify({repos:r,total:r.length,directory:e})}function rn(s,t){let{repo:e,depth:r=2}=s,i=Be(t,Ke,e);if(!F(i))return JSON.stringify({error:`Repo not found: ${e}. Run git_checkout first.`});let n={repo:e,path:i},o=z(i,"package.json");if(F(o))try{let m=JSON.parse(Qi(o,"utf-8"));n.packageJson={name:m.name,version:m.version,scripts:m.scripts?Object.keys(m.scripts):[],dependencies:m.dependencies?Object.keys(m.dependencies).slice(0,30):[],devDependencies:m.devDependencies?Object.keys(m.devDependencies).slice(0,20):[]},m.dependencies?.react?n.framework="React":m.dependencies?.next?n.framework="Next.js":m.dependencies?.vue?n.framework="Vue":m.dependencies?.angular?n.framework="Angular":m.dependencies?.express?n.framework="Express":m.dependencies?.fastify&&(n.framework="Fastify")}catch{}let c=z(i,"pyproject.toml");F(c)&&(n.language="Python");let a=z(i,"go.mod");F(a)&&(n.language="Go");let u=z(i,"Cargo.toml");F(u)&&(n.language="Rust"),F(o)&&(n.language=n.language||"JavaScript/TypeScript");let l=[];function d(m,f,y){if(y>r)return;let _;try{_=Yt(m,{withFileTypes:!0})}catch{return}let g=_.filter(b=>!b.name.startsWith(".")&&b.name!=="node_modules"&&b.name!=="__pycache__"&&b.name!=="dist"&&b.name!=="build"&&b.name!==".git").sort((b,h)=>b.isDirectory()!==h.isDirectory()?b.isDirectory()?-1:1:b.name.localeCompare(h.name));for(let b of g){let h=b.isDirectory();l.push(`${f}${h?"\u{1F4C1}":"\u{1F4C4}"} ${b.name}`),h&&y<r&&d(z(m,b.name),`${f} `,y+1)}}d(i,"",1),n.tree=l.slice(0,80),l.length>80&&(n.treeTruncated=!0);let p=["README.md","README.rst","src/App.tsx","src/App.jsx","src/App.js","src/routes.tsx","src/routes.js","app/routes.tsx","app/routes.js","src/index.tsx","src/index.ts","src/main.tsx","src/main.ts","pages/_app.tsx","pages/_app.js","app/layout.tsx","docker-compose.yml","Dockerfile",".env.example"];return n.keyFilesFound=p.filter(m=>F(z(i,m))),JSON.stringify(n)}import{execFileSync as tr}from"child_process";import{existsSync as Ye,mkdirSync as rr}from"fs";import{join as H,basename as sn}from"path";import{randomBytes as nn}from"crypto";import{pathToFileURL as pe}from"url";import{createRequire as sr}from"module";var ze=".zibby/memory",ir="dolt",nr={encoding:"utf-8",stdio:["pipe","pipe","pipe"],timeout:15e3},He="mem0",Ge=new Map,Re=new Map,Oe=!1,on=sr(import.meta.url),Te=()=>nn(8).toString("hex"),se=()=>new Date().toISOString(),an=[`CREATE TABLE IF NOT EXISTS chat_memory (
488
496
  id VARCHAR(64) PRIMARY KEY,
489
497
  memory_key VARCHAR(160),
490
498
  category VARCHAR(32) NOT NULL,
@@ -515,10 +523,10 @@ When a test ticket lacks context, use this workflow:
515
523
  tasks_failed INT DEFAULT 0,
516
524
  key_facts TEXT,
517
525
  created_at VARCHAR(32) NOT NULL
518
- )`],Wt=new Set;function C(s,t){return Vt(er,t,{...tr,cwd:s})}function Z(s,t){try{let e=C(s,["sql","-q",t,"-r","json"]);return JSON.parse(e.trim()).rows||[]}catch{return[]}}function P(s,t){C(s,["sql","-q",t])}function Re(s){if(Wt.has(s))return!0;if(!ze(z(s,".dolt"))){if(!en())return!1;Qt(s,{recursive:!0}),C(s,["init","--name","Zibby Chat Memory","--email","chat@zibby.app"])}let t=`${Xi.join(`;
519
- `)};`;P(s,t);try{P(s,"ALTER TABLE chat_memory ADD COLUMN tier VARCHAR(16) DEFAULT 'mid'")}catch{}try{P(s,"ALTER TABLE chat_memory ADD COLUMN memory_key VARCHAR(160)")}catch{}return Wt.add(s),!0}function en(){try{return Vt(er,["version"],{...tr,timeout:5e3}),!0}catch{return!1}}function w(s){return s==null?"NULL":`'${String(s).replace(/'/g,"''")}'`}function Fe(s){return String(s||"").toLowerCase().replace(/[“”]/g,'"').replace(/[‘’]/g,"'").replace(/[\s_-]+/g," ").replace(/[^\w\s"']/g,"").replace(/\s+/g," ").trim()}function se(s){return s==="long"?3:s==="mid"?2:s==="short"?1:0}function We(s,t){let e=["short","mid","long"].includes(s)?s:"mid";return new Set(["fact","decision","preference","credential","url","workaround"]).has(String(t||"").toLowerCase())&&e==="short"?"mid":e}function rr(s){let t=new Map;for(let e of s||[]){let r=Fe(e.content),i=e.memory_key?`key:${e.memory_key}`:r?`norm:${r}`:"";if(!i)continue;let n=t.get(i);if(!n){t.set(i,e);continue}let o=se(n.tier),c=se(e.tier);if(c>o){t.set(i,e);continue}c===o&&Number(e.relevance||0)>Number(n.relevance||0)&&t.set(i,e)}return[...t.values()]}function $e(s,t){let e=String(s??"");return e.length<=t?e:t<=1?e.slice(0,t):`${e.slice(0,t-1)}\u2026`}function ue(s,t){let e={recentSessions:Array.isArray(s?.recentSessions)?s.recentSessions:[],topMemories:Array.isArray(s?.topMemories)?s.topMemories:[],taskStats:Array.isArray(s?.taskStats)?s.taskStats:[],ticketFilter:s?.ticketFilter||null,backend:t||String(s?.backend||Ge),error:s?.error||null};return e.backend==="mem0"?{...e,recentSessions:[],taskStats:[]}:e}function Ht(s){let t=[];if(s.recentSessions?.length>0){t.push("Recent sessions:");for(let e of s.recentSessions.slice(0,3))e?.summary?.trim()&&t.push(`- ${$e(e.summary,150)}${e.tickets?` [${e.tickets}]`:""}`)}if(s.topMemories?.length>0){t.push("Known facts:");for(let e of s.topMemories.slice(0,10)){let r=e.tier==="long"?"\u2605":"\xB7";t.push(`${r} [${e.category}] ${$e(e.content,120)}`)}}return t.length===0?"":`## Memory Context
526
+ )`],Vt=new Set;function C(s,t){return tr(ir,t,{...nr,cwd:s})}function Q(s,t){try{let e=C(s,["sql","-q",t,"-r","json"]);return JSON.parse(e.trim()).rows||[]}catch{return[]}}function P(s,t){C(s,["sql","-q",t])}function Ae(s){if(Vt.has(s))return!0;if(!Ye(H(s,".dolt"))){if(!cn())return!1;rr(s,{recursive:!0}),C(s,["init","--name","Zibby Chat Memory","--email","chat@zibby.app"])}let t=`${an.join(`;
527
+ `)};`;P(s,t);try{P(s,"ALTER TABLE chat_memory ADD COLUMN tier VARCHAR(16) DEFAULT 'mid'")}catch{}try{P(s,"ALTER TABLE chat_memory ADD COLUMN memory_key VARCHAR(160)")}catch{}return Vt.add(s),!0}function cn(){try{return tr(ir,["version"],{...nr,timeout:5e3}),!0}catch{return!1}}function k(s){return s==null?"NULL":`'${String(s).replace(/'/g,"''")}'`}function We(s){return String(s||"").toLowerCase().replace(/[“”]/g,'"').replace(/[‘’]/g,"'").replace(/[\s_-]+/g," ").replace(/[^\w\s"']/g,"").replace(/\s+/g," ").trim()}function ne(s){return s==="long"?3:s==="mid"?2:s==="short"?1:0}function Ze(s,t){let e=["short","mid","long"].includes(s)?s:"mid";return new Set(["fact","decision","preference","credential","url","workaround"]).has(String(t||"").toLowerCase())&&e==="short"?"mid":e}function or(s){let t=new Map;for(let e of s||[]){let r=We(e.content),i=e.memory_key?`key:${e.memory_key}`:r?`norm:${r}`:"";if(!i)continue;let n=t.get(i);if(!n){t.set(i,e);continue}let o=ne(n.tier),c=ne(e.tier);if(c>o){t.set(i,e);continue}c===o&&Number(e.relevance||0)>Number(n.relevance||0)&&t.set(i,e)}return[...t.values()]}function je(s,t){let e=String(s??"");return e.length<=t?e:t<=1?e.slice(0,t):`${e.slice(0,t-1)}\u2026`}function de(s,t){let e={recentSessions:Array.isArray(s?.recentSessions)?s.recentSessions:[],topMemories:Array.isArray(s?.topMemories)?s.topMemories:[],taskStats:Array.isArray(s?.taskStats)?s.taskStats:[],ticketFilter:s?.ticketFilter||null,backend:t||String(s?.backend||He),error:s?.error||null};return e.backend==="mem0"?{...e,recentSessions:[],taskStats:[]}:e}function Qt(s){let t=[];if(s.recentSessions?.length>0){t.push("Recent sessions:");for(let e of s.recentSessions.slice(0,3))e?.summary?.trim()&&t.push(`- ${je(e.summary,150)}${e.tickets?` [${e.tickets}]`:""}`)}if(s.topMemories?.length>0){t.push("Known facts:");for(let e of s.topMemories.slice(0,10)){let r=e.tier==="long"?"\u2605":"\xB7";t.push(`${r} [${e.category}] ${je(e.content,120)}`)}}return t.length===0?"":`## Memory Context
520
528
  ${t.join(`
521
- `)}`}function Ae(s){return{backend:s.backend,recentSessions:s.recentSessions.slice(0,3).map(t=>({summary:$e(String(t?.summary||""),160),tickets:t?.tickets||null,created_at:t?.created_at||null})),topMemories:s.topMemories.slice(0,8).map(t=>({category:t?.category||null,tier:t?.tier||null,content:$e(String(t?.content||""),140),source:t?.source||null})),taskStats:s.taskStats,error:s.error||null}}async function tn(s,t){let e=String(process.env.ZIBBY_MEMORY_BACKEND||"").trim().toLowerCase();if(e==="mem0"||e==="dolt")return e;let r=String(t?.options?.memoryBackend||t?.options?.config?.memory?.backend||"").trim().toLowerCase();if(r==="mem0"||r==="dolt")return r;if(Ie.has(s))return Ie.get(s);try{let i=z(s,".zibby.config.mjs");if(ze(i)){let n=await import(pe(i).href),o=String(n?.default?.memory?.backend||"").trim().toLowerCase();if(o==="mem0"||o==="dolt")return Ie.set(s,o),o}}catch{}return Ie.set(s,Ge),Ge}function sr(s){let t=String(process.env.ZIBBY_MEMORY_USER_ID||"").trim();return t||`workspace:${Zi(s||process.cwd())}`}var rn="mem0";function ir(s){let t=z(s,Ke,rn);return{dir:t,vectorDbPath:z(t,"vectors.db"),historyDbPath:z(t,"history.db")}}function sn(s){let t=String(process.env.ZIBBY_MEM0_OPENAI_BASE_URL||"").trim();if(!t)return null;let e=String(process.env.ZIBBY_MEM0_API_KEY||process.env.ZIBBY_USER_TOKEN||process.env.OPENAI_API_KEY||"").trim(),r=String(process.env.ZIBBY_MEM0_LLM_MODEL||"gpt-4.1-mini").trim(),i=String(process.env.ZIBBY_MEM0_EMBEDDER_MODEL||"text-embedding-3-small").trim(),n=Number(process.env.ZIBBY_MEM0_EMBEDDING_DIMS||1536),{vectorDbPath:o,historyDbPath:c}=ir(s||process.cwd());return{llm:{provider:"openai",config:{model:r,baseURL:t,...e?{apiKey:e}:{}}},embedder:{provider:"openai",config:{model:i,embeddingDims:n,baseURL:t,...e?{apiKey:e}:{}}},vectorStore:{provider:"memory",config:{dimension:n,dbPath:o}},historyDbPath:c}}async function nr(s){let t=s||process.cwd();if(De.has(t))return De.get(t);let e;try{let c=Xt(pe(z(t,"package.json")).href).resolve("mem0ai/oss");e=await import(pe(c).href)}catch{try{let o=Qi.resolve("mem0ai/oss");e=await import(pe(o).href)}catch(o){throw new Error(`Cannot find package 'mem0ai' for workspace "${t}". Install in that project: npm install mem0ai. (${o.message})`,{cause:o})}}let r=e?.Memory;if(!r)throw new Error("mem0ai/oss does not export Memory");let i=sn(t);if(i)try{Qt(ir(t).dir,{recursive:!0})}catch{}let n=i?new r(i):new r;return De.set(t,n),n}function Yt(s,t="mid"){return(Array.isArray(s)?s:Array.isArray(s?.results)?s.results:[]).map(r=>({id:r?.id||je(),memory_key:r?.metadata?.memoryKey||r?.metadata?.memory_key||null,category:r?.metadata?.category||"fact",content:r?.memory||r?.content||"",source:r?.metadata?.source||"mem0",ticket_key:r?.metadata?.ticketKey||r?.metadata?.ticket_key||null,tier:We(r?.metadata?.tier||t,r?.metadata?.category||"fact"),relevance:Number(r?.score??r?.metadata?.relevance??.8),created_at:r?.created_at||r?.metadata?.created_at||te()})).filter(r=>String(r.content||"").trim().length>0)}var ie={id:"dolt",store:(s,t)=>ar(s,t),recall:(s,t)=>un(s,t),brief:(s,t)=>pn(s,t),endSession:(s,t)=>lr(s,t),logTask:(s,t)=>ur(s,t),taskHistory:(s,t)=>pr(s,t)},nn={id:"mem0",store:(s,t,e)=>ln(s,t,e),recall:(s,t,e)=>cr(s,t,e),brief:(s,t,e)=>dn(s,t,e),endSession:(s,t)=>lr(s,t),logTask:(s,t)=>ur(s,t),taskHistory:(s,t)=>pr(s,t)},on={dolt:ie,mem0:nn};async function Zt(s,t){let e=await tn(s,t);return on[e]||ie}var or={id:"chat-memory",description:"Persistent chat memory and task history (Dolt-backed)",envKeys:[],promptFragment:`## Chat Memory (persistent)
529
+ `)}`}function $e(s){return{backend:s.backend,recentSessions:s.recentSessions.slice(0,3).map(t=>({summary:je(String(t?.summary||""),160),tickets:t?.tickets||null,created_at:t?.created_at||null})),topMemories:s.topMemories.slice(0,8).map(t=>({category:t?.category||null,tier:t?.tier||null,content:je(String(t?.content||""),140),source:t?.source||null})),taskStats:s.taskStats,error:s.error||null}}async function ln(s,t){let e=String(process.env.ZIBBY_MEMORY_BACKEND||"").trim().toLowerCase();if(e==="mem0"||e==="dolt")return e;let r=String(t?.options?.memoryBackend||t?.options?.config?.memory?.backend||"").trim().toLowerCase();if(r==="mem0"||r==="dolt")return r;if(Re.has(s))return Re.get(s);try{let i=H(s,".zibby.config.mjs");if(Ye(i)){let n=await import(pe(i).href),o=String(n?.default?.memory?.backend||"").trim().toLowerCase();if(o==="mem0"||o==="dolt")return Re.set(s,o),o}}catch{}return Re.set(s,He),He}function ar(s){let t=String(process.env.ZIBBY_MEMORY_USER_ID||"").trim();return t||`workspace:${sn(s||process.cwd())}`}var un="mem0";function cr(s){let t=H(s,ze,un);return{dir:t,vectorDbPath:H(t,"vectors.db"),historyDbPath:H(t,"history.db")}}function dn(s){let t=String(process.env.ZIBBY_MEM0_OPENAI_BASE_URL||"").trim();if(!t)return null;let e=String(process.env.ZIBBY_MEM0_API_KEY||process.env.ZIBBY_USER_TOKEN||process.env.OPENAI_API_KEY||"").trim(),r=String(process.env.ZIBBY_MEM0_LLM_MODEL||"gpt-4.1-mini").trim(),i=String(process.env.ZIBBY_MEM0_EMBEDDER_MODEL||"text-embedding-3-small").trim(),n=Number(process.env.ZIBBY_MEM0_EMBEDDING_DIMS||1536),{vectorDbPath:o,historyDbPath:c}=cr(s||process.cwd());return{llm:{provider:"openai",config:{model:r,baseURL:t,...e?{apiKey:e}:{}}},embedder:{provider:"openai",config:{model:i,embeddingDims:n,baseURL:t,...e?{apiKey:e}:{}}},vectorStore:{provider:"memory",config:{dimension:n,dbPath:o}},historyDbPath:c}}async function lr(s){let t=s||process.cwd();if(Ge.has(t))return Ge.get(t);let e;try{let c=sr(pe(H(t,"package.json")).href).resolve("mem0ai/oss");e=await import(pe(c).href)}catch{try{let o=on.resolve("mem0ai/oss");e=await import(pe(o).href)}catch(o){throw new Error(`Cannot find package 'mem0ai' for workspace "${t}". Install in that project: npm install mem0ai. (${o.message})`,{cause:o})}}let r=e?.Memory;if(!r)throw new Error("mem0ai/oss does not export Memory");let i=dn(t);if(i)try{rr(cr(t).dir,{recursive:!0})}catch{}let n=i?new r(i):new r;return Ge.set(t,n),n}function Xt(s,t="mid"){return(Array.isArray(s)?s:Array.isArray(s?.results)?s.results:[]).map(r=>({id:r?.id||Te(),memory_key:r?.metadata?.memoryKey||r?.metadata?.memory_key||null,category:r?.metadata?.category||"fact",content:r?.memory||r?.content||"",source:r?.metadata?.source||"mem0",ticket_key:r?.metadata?.ticketKey||r?.metadata?.ticket_key||null,tier:Ze(r?.metadata?.tier||t,r?.metadata?.category||"fact"),relevance:Number(r?.score??r?.metadata?.relevance??.8),created_at:r?.created_at||r?.metadata?.created_at||se()})).filter(r=>String(r.content||"").trim().length>0)}var oe={id:"dolt",store:(s,t)=>dr(s,t),recall:(s,t)=>gn(s,t),brief:(s,t)=>_n(s,t),endSession:(s,t)=>mr(s,t),logTask:(s,t)=>fr(s,t),taskHistory:(s,t)=>yr(s,t)},pn={id:"mem0",store:(s,t,e)=>hn(s,t,e),recall:(s,t,e)=>pr(s,t,e),brief:(s,t,e)=>bn(s,t,e),endSession:(s,t)=>mr(s,t),logTask:(s,t)=>fr(s,t),taskHistory:(s,t)=>yr(s,t)},mn={dolt:oe,mem0:pn};async function er(s,t){let e=await ln(s,t);return mn[e]||oe}var ur={id:"chat-memory",description:"Persistent chat memory and task history (Dolt-backed)",envKeys:[],promptFragment:`## Chat Memory (persistent)
522
530
  You have persistent memory across sessions. Use it to avoid losing context:
523
531
  - **memory_store**: Save important facts, decisions, or context. Anything worth remembering.
524
532
  - **memory_recall**: Search your memory by keyword or category. Use this at the START of conversations to recall relevant context.
@@ -534,40 +542,41 @@ You have persistent memory across sessions. Use it to avoid losing context:
534
542
  - When the user's request is complete: call memory_end_session
535
543
 
536
544
  ### Categories for memory_store
537
- fact, decision, context, insight, credential, url, error, workaround`,resolve(){return null},async buildPromptContext(s,t={}){let e=s?.options?.workspace||process.cwd(),r=z(e,Ke),i=await Zt(e,s),n=i.id;if(n==="dolt"&&!Re(r)){let o="Dolt not available. Install: brew install dolt (macOS) or see https://docs.dolthub.com/introduction/installation";return{backend:n,brief:ue({backend:n,error:o},n),promptContext:"",debugPreview:Ae(ue({backend:n,error:o},n)),error:o}}try{let o=await i.brief(t,r,e),c=JSON.parse(o||"{}"),a=ue({...c,backend:n},n);return{backend:n,brief:a,promptContext:Ht(a),debugPreview:Ae(a),error:a.error||null}}catch(o){if(n==="mem0"&&i!==ie&&Re(r)){if(!Oe){Oe=!0;try{process.stderr.write(`[chat-memory] mem0 backend unavailable (${o?.message||o}); degrading to dolt for this run
538
- `)}catch{}}try{let u=await ie.brief(t,r,e),l=JSON.parse(u||"{}"),p=ue({...l,backend:"dolt"},"dolt");return{backend:"dolt",brief:p,promptContext:Ht(p),debugPreview:Ae(p),error:p.error||null,degradedFrom:"mem0"}}catch{}}let c=String(o?.message||o),a=ue({backend:n,error:c},n);return{backend:n,brief:a,promptContext:"",debugPreview:Ae(a),error:c}}},async handleToolCall(s,t,e){let r=e?.options?.workspace||process.cwd(),i=z(r,Ke),n=await Zt(r,e),o=n.id;if((o==="dolt"||["memory_end_session","task_log","task_history"].includes(s))&&!Re(i))return JSON.stringify({error:"Dolt not available. Install: brew install dolt (macOS) or see https://docs.dolthub.com/introduction/installation"});let a=u=>{switch(s){case"memory_store":return u.store(t,i,r);case"memory_recall":return u.recall(t,i,r);case"memory_brief":return u.brief(t,i,r);case"memory_end_session":return u.endSession(t,i,r);case"task_log":return u.logTask(t,i,r);case"task_history":return u.taskHistory(t,i,r);default:return JSON.stringify({error:`Unknown tool: ${s}`})}};try{return await a(n)}catch(u){if(o==="mem0"&&n!==ie){if(Re(i)){if(!Oe){Oe=!0;try{process.stderr.write(`[chat-memory] mem0 backend unavailable (${u.message}); degrading to dolt for this run
539
- `)}catch{}}try{return await a(ie)}catch(l){return JSON.stringify({error:l.message,backend:"dolt",degradedFrom:"mem0"})}}return JSON.stringify({error:`mem0 unavailable (${u.message}); dolt fallback also unavailable`,backend:"mem0"})}return JSON.stringify({error:u.message})}},tools:[{name:"memory_store",description:"Save a fact, decision, or context to persistent memory. Survives across sessions.",input_schema:{type:"object",properties:{memoryKey:{type:"string",description:"Stable semantic identity key (e.g. user.jira.default_board)"},content:{type:"string",description:"The information to remember"},category:{type:"string",enum:["fact","decision","context","insight","preference","credential","url","error","workaround"],description:"Category of memory"},tier:{type:"string",enum:["short","mid","long"],description:"Memory tier: short (session/24h), mid (days/weeks), long (permanent)"},source:{type:"string",description:'Where this info came from (e.g. "jira", "github", "user", "test_run")'},ticketKey:{type:"string",description:"Related ticket key (optional)"},infer:{type:"boolean",description:"true = LLM distills/dedupes facts (costs tokens); false = store raw, embed-only, free",default:!1}},required:["content","category"]}},{name:"memory_recall",description:"Search persistent memory by keyword, category, ticket, or tier. Returns matching facts and context.",input_schema:{type:"object",properties:{query:{type:"string",description:"Search text (matches content)"},category:{type:"string",description:"Filter by category"},ticketKey:{type:"string",description:"Filter by ticket key"},tier:{type:"string",enum:["short","mid","long"],description:"Filter by memory tier"},limit:{type:"number",description:"Max results (default: 20)"}}}},{name:"memory_brief",description:"Get a compact briefing: recent session summaries + top relevant facts. Call at the start of a conversation.",input_schema:{type:"object",properties:{ticketKey:{type:"string",description:"Focus briefing on a specific ticket (optional)"}}}},{name:"memory_end_session",description:"End the current session and save a summary for future recall. Call when a task is complete.",input_schema:{type:"object",properties:{summary:{type:"string",description:"What happened in this session (1-3 sentences)"},tickets:{type:"string",description:"Comma-separated ticket keys covered"},tasksRun:{type:"number",description:"Number of tasks/tests run"},tasksPassed:{type:"number",description:"Number passed"},tasksFailed:{type:"number",description:"Number failed"},keyFacts:{type:"string",description:"Key facts worth remembering from this session (semicolon-separated)"}},required:["summary"]}},{name:"task_log",description:"Record a completed task (test run, analysis, generation) to persistent history.",input_schema:{type:"object",properties:{title:{type:"string",description:"Task description"},type:{type:"string",enum:["test_run","generate","analysis","research","other"],description:"Task type"},status:{type:"string",enum:["passed","failed","cancelled","error"],description:"Outcome"},ticketKey:{type:"string",description:"Related ticket key"},specPath:{type:"string",description:"Spec file path (if test run)"},resultSummary:{type:"string",description:"Brief result description"}},required:["title","type","status"]}},{name:"task_history",description:"Query past tasks by ticket, status, or type. See what was done before.",input_schema:{type:"object",properties:{ticketKey:{type:"string",description:"Filter by ticket key"},type:{type:"string",description:"Filter by task type"},status:{type:"string",description:"Filter by status"},limit:{type:"number",description:"Max results (default: 20)"}}}}]};function ar(s,t){let{content:e,category:r,source:i,ticketKey:n,tier:o,memoryKey:c}=s;if(!e||!r)return JSON.stringify({error:"content and category are required"});let a=Fe(e);if(!a)return JSON.stringify({error:"content is empty after normalization"});let u=We(o,r),l=u==="long"?1:u==="mid"?.8:.5,p=String(c||"").trim().slice(0,160);if(p){let g=Z(t,`SELECT id, tier, relevance
545
+ fact, decision, context, insight, credential, url, error, workaround`,resolve(){return null},async buildPromptContext(s,t={}){let e=s?.options?.workspace||process.cwd(),r=H(e,ze),i=await er(e,s),n=i.id;if(n==="dolt"&&!Ae(r)){let o="Dolt not available. Install: brew install dolt (macOS) or see https://docs.dolthub.com/introduction/installation";return{backend:n,brief:de({backend:n,error:o},n),promptContext:"",debugPreview:$e(de({backend:n,error:o},n)),error:o}}try{let o=await i.brief(t,r,e),c=JSON.parse(o||"{}"),a=de({...c,backend:n},n);return{backend:n,brief:a,promptContext:Qt(a),debugPreview:$e(a),error:a.error||null}}catch(o){if(n==="mem0"&&i!==oe&&Ae(r)){if(!Oe){Oe=!0;try{process.stderr.write(`[chat-memory] mem0 backend unavailable (${o?.message||o}); degrading to dolt for this run
546
+ `)}catch{}}try{let u=await oe.brief(t,r,e),l=JSON.parse(u||"{}"),d=de({...l,backend:"dolt"},"dolt");return{backend:"dolt",brief:d,promptContext:Qt(d),debugPreview:$e(d),error:d.error||null,degradedFrom:"mem0"}}catch{}}let c=String(o?.message||o),a=de({backend:n,error:c},n);return{backend:n,brief:a,promptContext:"",debugPreview:$e(a),error:c}}},async handleToolCall(s,t,e){let r=e?.options?.workspace||process.cwd(),i=H(r,ze),n=await er(r,e),o=n.id;if((o==="dolt"||["memory_end_session","task_log","task_history"].includes(s))&&!Ae(i))return JSON.stringify({error:"Dolt not available. Install: brew install dolt (macOS) or see https://docs.dolthub.com/introduction/installation"});let a=u=>{switch(s){case"memory_store":return u.store(t,i,r);case"memory_recall":return u.recall(t,i,r);case"memory_brief":return u.brief(t,i,r);case"memory_end_session":return u.endSession(t,i,r);case"task_log":return u.logTask(t,i,r);case"task_history":return u.taskHistory(t,i,r);default:return JSON.stringify({error:`Unknown tool: ${s}`})}};try{return await a(n)}catch(u){if(o==="mem0"&&n!==oe){if(Ae(i)){if(!Oe){Oe=!0;try{process.stderr.write(`[chat-memory] mem0 backend unavailable (${u.message}); degrading to dolt for this run
547
+ `)}catch{}}try{return await a(oe)}catch(l){return JSON.stringify({error:l.message,backend:"dolt",degradedFrom:"mem0"})}}return JSON.stringify({error:`mem0 unavailable (${u.message}); dolt fallback also unavailable`,backend:"mem0"})}return JSON.stringify({error:u.message})}},tools:[{name:"memory_store",description:"Save a fact, decision, or context to persistent memory. Survives across sessions.",input_schema:{type:"object",properties:{memoryKey:{type:"string",description:"Stable semantic identity key (e.g. user.jira.default_board)"},content:{type:"string",description:"The information to remember"},category:{type:"string",enum:["fact","decision","context","insight","preference","credential","url","error","workaround"],description:"Category of memory"},tier:{type:"string",enum:["short","mid","long"],description:"Memory tier: short (session/24h), mid (days/weeks), long (permanent)"},source:{type:"string",description:'Where this info came from (e.g. "jira", "github", "user", "test_run")'},ticketKey:{type:"string",description:"Related ticket key (optional)"},infer:{type:"boolean",description:"true = LLM distills/dedupes facts (costs tokens); false = store raw, embed-only, free",default:!1}},required:["content","category"]}},{name:"memory_recall",description:"Search persistent memory by keyword, category, ticket, or tier. Returns matching facts and context.",input_schema:{type:"object",properties:{query:{type:"string",description:"Search text (matches content)"},category:{type:"string",description:"Filter by category"},ticketKey:{type:"string",description:"Filter by ticket key"},tier:{type:"string",enum:["short","mid","long"],description:"Filter by memory tier"},limit:{type:"number",description:"Max results (default: 20)"}}}},{name:"memory_brief",description:"Get a compact briefing: recent session summaries + top relevant facts. Call at the start of a conversation.",input_schema:{type:"object",properties:{ticketKey:{type:"string",description:"Focus briefing on a specific ticket (optional)"}}}},{name:"memory_end_session",description:"End the current session and save a summary for future recall. Call when a task is complete.",input_schema:{type:"object",properties:{summary:{type:"string",description:"What happened in this session (1-3 sentences)"},tickets:{type:"string",description:"Comma-separated ticket keys covered"},tasksRun:{type:"number",description:"Number of tasks/tests run"},tasksPassed:{type:"number",description:"Number passed"},tasksFailed:{type:"number",description:"Number failed"},keyFacts:{type:"string",description:"Key facts worth remembering from this session (semicolon-separated)"}},required:["summary"]}},{name:"task_log",description:"Record a completed task (test run, analysis, generation) to persistent history.",input_schema:{type:"object",properties:{title:{type:"string",description:"Task description"},type:{type:"string",enum:["test_run","generate","analysis","research","other"],description:"Task type"},status:{type:"string",enum:["passed","failed","cancelled","error"],description:"Outcome"},ticketKey:{type:"string",description:"Related ticket key"},specPath:{type:"string",description:"Spec file path (if test run)"},resultSummary:{type:"string",description:"Brief result description"}},required:["title","type","status"]}},{name:"task_history",description:"Query past tasks by ticket, status, or type. See what was done before.",input_schema:{type:"object",properties:{ticketKey:{type:"string",description:"Filter by ticket key"},type:{type:"string",description:"Filter by task type"},status:{type:"string",description:"Filter by status"},limit:{type:"number",description:"Max results (default: 20)"}}}}]};function dr(s,t){let{content:e,category:r,source:i,ticketKey:n,tier:o,memoryKey:c}=s;if(!e||!r)return JSON.stringify({error:"content and category are required"});let a=We(e);if(!a)return JSON.stringify({error:"content is empty after normalization"});let u=Ze(o,r),l=u==="long"?1:u==="mid"?.8:.5,d=String(c||"").trim().slice(0,160);if(d){let g=Q(t,`SELECT id, tier, relevance
540
548
  FROM chat_memory
541
- WHERE memory_key = ${w(p)}
549
+ WHERE memory_key = ${k(d)}
542
550
  ORDER BY created_at DESC
543
- LIMIT 1`)[0];if(g){let b=String(g.tier||"mid"),h=Number(g.relevance||0),v=se(u)>se(b)?u:b,T=Math.max(l,h);P(t,`UPDATE chat_memory
544
- SET content = ${w(e)},
545
- category = ${w(r)},
546
- source = ${w(i)},
547
- ticket_key = ${w(n)},
548
- tier = ${w(v)},
549
- relevance = ${T},
550
- created_at = ${w(te())}
551
- WHERE id = ${w(g.id)}`);try{C(t,["add","."]),C(t,["commit","-m",`memory upsert: ${r} \u2014 ${String(e).slice(0,60)}`])}catch{}return JSON.stringify({ok:!0,id:g.id,category:r,tier:v,memoryKey:p,upserted:!0})}}let m=Z(t,`SELECT id, content, tier, relevance
551
+ LIMIT 1`)[0];if(g){let b=String(g.tier||"mid"),h=Number(g.relevance||0),S=ne(u)>ne(b)?u:b,j=Math.max(l,h);P(t,`UPDATE chat_memory
552
+ SET content = ${k(e)},
553
+ category = ${k(r)},
554
+ source = ${k(i)},
555
+ ticket_key = ${k(n)},
556
+ tier = ${k(S)},
557
+ relevance = ${j},
558
+ created_at = ${k(se())}
559
+ WHERE id = ${k(g.id)}`);try{C(t,["add","."]),C(t,["commit","-m",`memory upsert: ${r} \u2014 ${String(e).slice(0,60)}`])}catch{}return JSON.stringify({ok:!0,id:g.id,category:r,tier:S,memoryKey:d,upserted:!0})}}let m=Q(t,`SELECT id, content, tier, relevance
552
560
  FROM chat_memory
553
- WHERE category = ${w(r)}
561
+ WHERE category = ${k(r)}
554
562
  ORDER BY created_at DESC
555
- LIMIT 200`).find(_=>Fe(_.content)===a);if(m){let _=String(m.tier||"mid"),g=Number(m.relevance||0),b=se(u)>se(_),h=l>g;if(b||h){P(t,`UPDATE chat_memory
556
- SET tier = ${w(b?u:_)},
563
+ LIMIT 200`).find(_=>We(_.content)===a);if(m){let _=String(m.tier||"mid"),g=Number(m.relevance||0),b=ne(u)>ne(_),h=l>g;if(b||h){P(t,`UPDATE chat_memory
564
+ SET tier = ${k(b?u:_)},
557
565
  relevance = ${Math.max(l,g)}
558
- WHERE id = ${w(m.id)}`);try{C(t,["add","."]),C(t,["commit","-m",`memory promote: ${r} \u2014 ${String(e).slice(0,60)}`])}catch{}return JSON.stringify({ok:!0,id:m.id,category:r,tier:b?u:_,deduped:!0,promoted:!0})}return JSON.stringify({ok:!0,id:m.id,category:r,tier:_,deduped:!0,promoted:!1})}let f=je(),y=process.env.ZIBBY_CHAT_SESSION_ID||null;P(t,`INSERT INTO chat_memory (id, memory_key, category, content, source, ticket_key, session_id, tier, relevance, created_at)
559
- VALUES (${w(f)}, ${w(p||null)}, ${w(r)}, ${w(e)}, ${w(i)}, ${w(n)}, ${w(y)}, ${w(u)}, ${l}, ${w(te())})`);try{C(t,["add","."]),C(t,["commit","-m",`memory: ${r} \u2014 ${e.slice(0,60)}`])}catch{}return JSON.stringify({ok:!0,id:f,category:r,tier:u,memoryKey:p||null,stored:e.slice(0,100)})}function an(s){let t=String(s||"").trim().toLowerCase();return t==="1"||t==="true"||t==="yes"||t==="on"}var Be=new Map;async function cn(s,t){if(typeof s=="boolean")return s;if(process.env.ZIBBY_MEM0_INFER!=null&&String(process.env.ZIBBY_MEM0_INFER).trim()!=="")return an(process.env.ZIBBY_MEM0_INFER);let e=t||process.cwd();if(Be.has(e))return Be.get(e);let r=!1;try{let i=z(e,".zibby.config.mjs");ze(i)&&(r=(await import(pe(i).href))?.default?.memory?.infer===!0)}catch{}return Be.set(e,r),r}async function ln(s,t,e){let{content:r,category:i,source:n,ticketKey:o,tier:c,memoryKey:a,infer:u}=s;if(!r||!i)return JSON.stringify({error:"content and category are required"});try{let l=await nr(e),p=sr(e),d=We(c,i),m=await cn(u,e);return await l.add([{role:"user",content:String(r)}],{userId:p,infer:m,metadata:{memoryKey:a||null,category:i,tier:d,source:n||"zibby-chat",ticketKey:o||null,created_at:te()}}),JSON.stringify({ok:!0,backend:"mem0",userId:p,category:i,tier:d,infer:m,memoryKey:a||null,stored:String(r).slice(0,100)})}catch(l){throw new Error(`mem0 store failed: ${l.message}. If mem0 is not installed, run: npm install mem0ai`,{cause:l})}}function un(s,t){let{query:e,category:r,ticketKey:i,tier:n,limit:o=20}=s,c=[];e&&c.push(`content LIKE ${w(`%${e}%`)}`),r&&c.push(`category = ${w(r)}`),i&&c.push(`ticket_key = ${w(i)}`),n&&c.push(`tier = ${w(n)}`);let u=`SELECT id, memory_key, category, content, source, ticket_key, tier, relevance, created_at
566
+ WHERE id = ${k(m.id)}`);try{C(t,["add","."]),C(t,["commit","-m",`memory promote: ${r} \u2014 ${String(e).slice(0,60)}`])}catch{}return JSON.stringify({ok:!0,id:m.id,category:r,tier:b?u:_,deduped:!0,promoted:!0})}return JSON.stringify({ok:!0,id:m.id,category:r,tier:_,deduped:!0,promoted:!1})}let f=Te(),y=process.env.ZIBBY_CHAT_SESSION_ID||null;P(t,`INSERT INTO chat_memory (id, memory_key, category, content, source, ticket_key, session_id, tier, relevance, created_at)
567
+ VALUES (${k(f)}, ${k(d||null)}, ${k(r)}, ${k(e)}, ${k(i)}, ${k(n)}, ${k(y)}, ${k(u)}, ${l}, ${k(se())})`);try{C(t,["add","."]),C(t,["commit","-m",`memory: ${r} \u2014 ${e.slice(0,60)}`])}catch{}return JSON.stringify({ok:!0,id:f,category:r,tier:u,memoryKey:d||null,stored:e.slice(0,100)})}function fn(s){let t=String(s||"").trim().toLowerCase();return t==="1"||t==="true"||t==="yes"||t==="on"}var Fe=new Map;async function yn(s,t){if(typeof s=="boolean")return s;if(process.env.ZIBBY_MEM0_INFER!=null&&String(process.env.ZIBBY_MEM0_INFER).trim()!=="")return fn(process.env.ZIBBY_MEM0_INFER);let e=t||process.cwd();if(Fe.has(e))return Fe.get(e);let r=!1;try{let i=H(e,".zibby.config.mjs");Ye(i)&&(r=(await import(pe(i).href))?.default?.memory?.infer===!0)}catch{}return Fe.set(e,r),r}async function hn(s,t,e){let{content:r,category:i,source:n,ticketKey:o,tier:c,memoryKey:a,infer:u}=s;if(!r||!i)return JSON.stringify({error:"content and category are required"});try{let l=await lr(e),d=ar(e),p=Ze(c,i),m=await yn(u,e);return await l.add([{role:"user",content:String(r)}],{userId:d,infer:m,metadata:{memoryKey:a||null,category:i,tier:p,source:n||"zibby-chat",ticketKey:o||null,created_at:se()}}),JSON.stringify({ok:!0,backend:"mem0",userId:d,category:i,tier:p,infer:m,memoryKey:a||null,stored:String(r).slice(0,100)})}catch(l){throw new Error(`mem0 store failed: ${l.message}. If mem0 is not installed, run: npm install mem0ai`,{cause:l})}}function gn(s,t){let{query:e,category:r,ticketKey:i,tier:n,limit:o=20}=s,c=[];e&&c.push(`content LIKE ${k(`%${e}%`)}`),r&&c.push(`category = ${k(r)}`),i&&c.push(`ticket_key = ${k(i)}`),n&&c.push(`tier = ${k(n)}`);let u=`SELECT id, memory_key, category, content, source, ticket_key, tier, relevance, created_at
560
568
  FROM chat_memory ${c.length>0?`WHERE ${c.join(" AND ")}`:""}
561
569
  ORDER BY relevance DESC, created_at DESC
562
- LIMIT ${o}`,l=Z(t,u);return JSON.stringify({total:l.length,memories:l})}async function cr(s,t,e){let{query:r,category:i,ticketKey:n,tier:o,limit:c=20}=s;try{let a=await nr(e),u=sr(e),l=[];if(r&&String(r).trim()){let p=await a.search(String(r),{filters:{user_id:u},topK:c});l=Yt(p)}else{let p=await a.getAll({filters:{user_id:u},topK:Math.max(c,50)});l=Yt(p)}return i&&(l=l.filter(p=>p.category===i)),n&&(l=l.filter(p=>p.ticket_key===n)),o&&(l=l.filter(p=>p.tier===o)),l=l.slice(0,c),JSON.stringify({total:l.length,memories:l,backend:"mem0"})}catch(a){throw new Error(`mem0 recall failed: ${a.message}. If mem0 is not installed, run: npm install mem0ai`,{cause:a})}}function pn(s,t){let{ticketKey:e}=s;fn(t);let i=Z(t,`SELECT session_id, summary, tickets, tasks_run, tasks_passed, tasks_failed, created_at
563
- FROM chat_sessions ORDER BY created_at DESC LIMIT 5`),n=e?`AND ticket_key = ${w(e)}`:"",o=Z(t,`SELECT memory_key, category, content, source, tier, relevance, created_at FROM chat_memory
564
- WHERE tier = 'long' ${n} ORDER BY relevance DESC, created_at DESC LIMIT 10`),c=Z(t,`SELECT memory_key, category, content, source, tier, relevance, created_at FROM chat_memory
565
- WHERE tier = 'mid' ${n} ORDER BY relevance DESC, created_at DESC LIMIT 8`),u=Z(t,`SELECT type, status, COUNT(*) as cnt FROM chat_tasks
566
- GROUP BY type, status ORDER BY cnt DESC LIMIT 10`),l=rr([...o,...c]);return JSON.stringify({recentSessions:i,topMemories:l,taskStats:u,ticketFilter:e||null})}async function dn(s,t,e){let{ticketKey:r}=s,i=await cr({limit:80},t,e),n=JSON.parse(i||"{}"),o=Array.isArray(n.memories)?n.memories:[];r&&(o=o.filter(d=>d.ticket_key===r));let c=d=>{let m=Date.parse(String(d?.created_at||""))||0;return Number(d?.relevance||0)*1e12+m},a=(d,m)=>c(m)-c(d),u=o.filter(d=>d.tier==="long").sort(a).slice(0,10),l=o.filter(d=>d.tier==="mid").sort(a).slice(0,8),p=rr([...u,...l]);return JSON.stringify({recentSessions:[],topMemories:p,taskStats:[],ticketFilter:r||null,backend:"mem0"})}function lr(s,t){let{summary:e,tickets:r,tasksRun:i=0,tasksPassed:n=0,tasksFailed:o=0,keyFacts:c}=s;if(!e)return JSON.stringify({error:"summary is required"});let a=process.env.ZIBBY_CHAT_SESSION_ID||`session_${je()}`;if(P(t,`INSERT INTO chat_sessions (session_id, summary, tickets, tasks_run, tasks_passed, tasks_failed, key_facts, created_at)
567
- VALUES (${w(a)}, ${w(e)}, ${w(r)}, ${i}, ${n}, ${o}, ${w(c)}, ${w(te())})`),c)for(let u of c.split(";").map(l=>l.trim()).filter(Boolean))ar({content:u,category:"fact",source:"session_summary",tier:"mid"},t);mn(t);try{C(t,["add","."]),C(t,["commit","-m",`session end: ${e.slice(0,60)}`])}catch{}return JSON.stringify({ok:!0,sessionId:a,summary:e.slice(0,200)})}function ur(s,t){let{title:e,type:r,status:i,ticketKey:n,specPath:o,resultSummary:c}=s;if(!e||!r||!i)return JSON.stringify({error:"title, type, and status are required"});let a=je(),u=process.env.ZIBBY_CHAT_SESSION_ID||null;P(t,`INSERT INTO chat_tasks (id, ticket_key, type, title, status, spec_path, session_id, result_summary, created_at, finished_at)
568
- VALUES (${w(a)}, ${w(n)}, ${w(r)}, ${w(e)}, ${w(i)}, ${w(o)}, ${w(u)}, ${w(c)}, ${w(te())}, ${w(te())})`);try{C(t,["add","."]),C(t,["commit","-m",`task: ${i} \u2014 ${e.slice(0,60)}`])}catch{}return JSON.stringify({ok:!0,id:a,title:e,type:r,status:i})}function pr(s,t){let{ticketKey:e,type:r,status:i,limit:n=20}=s,o=[];e&&o.push(`ticket_key = ${w(e)}`),r&&o.push(`type = ${w(r)}`),i&&o.push(`status = ${w(i)}`);let a=`SELECT id, ticket_key, type, title, status, spec_path, result_summary, created_at, finished_at
570
+ LIMIT ${o}`,l=Q(t,u);return JSON.stringify({total:l.length,memories:l})}async function pr(s,t,e){let{query:r,category:i,ticketKey:n,tier:o,limit:c=20}=s;try{let a=await lr(e),u=ar(e),l=[];if(r&&String(r).trim()){let d=await a.search(String(r),{filters:{user_id:u},topK:c});l=Xt(d)}else{let d=await a.getAll({filters:{user_id:u},topK:Math.max(c,50)});l=Xt(d)}return i&&(l=l.filter(d=>d.category===i)),n&&(l=l.filter(d=>d.ticket_key===n)),o&&(l=l.filter(d=>d.tier===o)),l=l.slice(0,c),JSON.stringify({total:l.length,memories:l,backend:"mem0"})}catch(a){throw new Error(`mem0 recall failed: ${a.message}. If mem0 is not installed, run: npm install mem0ai`,{cause:a})}}function _n(s,t){let{ticketKey:e}=s;kn(t);let i=Q(t,`SELECT session_id, summary, tickets, tasks_run, tasks_passed, tasks_failed, created_at
571
+ FROM chat_sessions ORDER BY created_at DESC LIMIT 5`),n=e?`AND ticket_key = ${k(e)}`:"",o=Q(t,`SELECT memory_key, category, content, source, tier, relevance, created_at FROM chat_memory
572
+ WHERE tier = 'long' ${n} ORDER BY relevance DESC, created_at DESC LIMIT 10`),c=Q(t,`SELECT memory_key, category, content, source, tier, relevance, created_at FROM chat_memory
573
+ WHERE tier = 'mid' ${n} ORDER BY relevance DESC, created_at DESC LIMIT 8`),u=Q(t,`SELECT type, status, COUNT(*) as cnt FROM chat_tasks
574
+ GROUP BY type, status ORDER BY cnt DESC LIMIT 10`),l=or([...o,...c]);return JSON.stringify({recentSessions:i,topMemories:l,taskStats:u,ticketFilter:e||null})}async function bn(s,t,e){let{ticketKey:r}=s,i=await pr({limit:80},t,e),n=JSON.parse(i||"{}"),o=Array.isArray(n.memories)?n.memories:[];r&&(o=o.filter(p=>p.ticket_key===r));let c=p=>{let m=Date.parse(String(p?.created_at||""))||0;return Number(p?.relevance||0)*1e12+m},a=(p,m)=>c(m)-c(p),u=o.filter(p=>p.tier==="long").sort(a).slice(0,10),l=o.filter(p=>p.tier==="mid").sort(a).slice(0,8),d=or([...u,...l]);return JSON.stringify({recentSessions:[],topMemories:d,taskStats:[],ticketFilter:r||null,backend:"mem0"})}function mr(s,t){let{summary:e,tickets:r,tasksRun:i=0,tasksPassed:n=0,tasksFailed:o=0,keyFacts:c}=s;if(!e)return JSON.stringify({error:"summary is required"});let a=process.env.ZIBBY_CHAT_SESSION_ID||`session_${Te()}`;if(P(t,`INSERT INTO chat_sessions (session_id, summary, tickets, tasks_run, tasks_passed, tasks_failed, key_facts, created_at)
575
+ VALUES (${k(a)}, ${k(e)}, ${k(r)}, ${i}, ${n}, ${o}, ${k(c)}, ${k(se())})`),c)for(let u of c.split(";").map(l=>l.trim()).filter(Boolean))dr({content:u,category:"fact",source:"session_summary",tier:"mid"},t);wn(t);try{C(t,["add","."]),C(t,["commit","-m",`session end: ${e.slice(0,60)}`])}catch{}return JSON.stringify({ok:!0,sessionId:a,summary:e.slice(0,200)})}function fr(s,t){let{title:e,type:r,status:i,ticketKey:n,specPath:o,resultSummary:c}=s;if(!e||!r||!i)return JSON.stringify({error:"title, type, and status are required"});let a=Te(),u=process.env.ZIBBY_CHAT_SESSION_ID||null;P(t,`INSERT INTO chat_tasks (id, ticket_key, type, title, status, spec_path, session_id, result_summary, created_at, finished_at)
576
+ VALUES (${k(a)}, ${k(n)}, ${k(r)}, ${k(e)}, ${k(i)}, ${k(o)}, ${k(u)}, ${k(c)}, ${k(se())}, ${k(se())})`);try{C(t,["add","."]),C(t,["commit","-m",`task: ${i} \u2014 ${e.slice(0,60)}`])}catch{}return JSON.stringify({ok:!0,id:a,title:e,type:r,status:i})}function yr(s,t){let{ticketKey:e,type:r,status:i,limit:n=20}=s,o=[];e&&o.push(`ticket_key = ${k(e)}`),r&&o.push(`type = ${k(r)}`),i&&o.push(`status = ${k(i)}`);let a=`SELECT id, ticket_key, type, title, status, spec_path, result_summary, created_at, finished_at
569
577
  FROM chat_tasks ${o.length>0?`WHERE ${o.join(" AND ")}`:""}
570
- ORDER BY created_at DESC LIMIT ${n}`,u=Z(t,a);return JSON.stringify({total:u.length,tasks:u})}function mn(s){try{P(s,"UPDATE chat_memory SET relevance = relevance * 0.98 WHERE tier = 'long' AND relevance > 0.5"),P(s,"UPDATE chat_memory SET relevance = relevance * 0.90 WHERE tier = 'mid' AND relevance > 0.1"),P(s,"UPDATE chat_memory SET relevance = relevance * 0.70 WHERE tier = 'short' AND relevance > 0.05"),P(s,"DELETE FROM chat_memory WHERE relevance < 0.05")}catch{}}function fn(s){try{let t=new Date(Date.now()-864e5).toISOString();P(s,`DELETE FROM chat_memory WHERE tier = 'short' AND created_at < ${w(t)}`)}catch{}}import{existsSync as dr,readFileSync as yn}from"node:fs";import{homedir as hn}from"node:os";import{join as gn,dirname as _n,resolve as bn}from"node:path";import{fileURLToPath as kn}from"node:url";function wn(){if(process.env.MCP_SKILL_PATH)return process.env.MCP_SKILL_PATH;let s=_n(kn(import.meta.url)),t=bn(s,"..","bin","mcp-skill.mjs");return dr(t)?t:null}function Sn(){if(process.env.PROJECT_API_TOKEN)return process.env.PROJECT_API_TOKEN;if(process.env.ZIBBY_USER_TOKEN)return process.env.ZIBBY_USER_TOKEN;try{let s=gn(hn(),".zibby","config.json");return dr(s)&&JSON.parse(yn(s,"utf-8")).sessionToken||null}catch{return null}}function vn(){return process.env.ZIBBY_ACCOUNT_API_URL?process.env.ZIBBY_ACCOUNT_API_URL.replace(/\/$/,""):(process.env.ZIBBY_ENV||"prod")==="local"?"http://localhost:3001":process.env.ZIBBY_PROD_ACCOUNT_API_URL||"https://api-prod.zibby.app"}async function He(s,t){let e=Sn();if(!e)throw new Error("No backend credential (PROJECT_API_TOKEN). Review memory is only available inside a Zibby run.");let r=`${vn()}/memory/review`,i=await fetch(r,{method:"POST",headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"},body:JSON.stringify({op:s,...t})});if(!i.ok){let n=await i.text().catch(()=>"");throw new Error(`Review memory ${s} failed (${i.status}): ${n.slice(0,300)}`)}return i.json()}var mr={id:"review-memory",serverName:"review_memory",allowedTools:["mcp__review_memory__*"],description:"Review memory \u2014 persist & recall per-PR (configurable-scope) review notes across stateless runs",promptFragment:`## Review Memory (per-PR, configurable scope)
578
+ ORDER BY created_at DESC LIMIT ${n}`,u=Q(t,a);return JSON.stringify({total:u.length,tasks:u})}function wn(s){try{P(s,"UPDATE chat_memory SET relevance = relevance * 0.98 WHERE tier = 'long' AND relevance > 0.5"),P(s,"UPDATE chat_memory SET relevance = relevance * 0.90 WHERE tier = 'mid' AND relevance > 0.1"),P(s,"UPDATE chat_memory SET relevance = relevance * 0.70 WHERE tier = 'short' AND relevance > 0.05"),P(s,"DELETE FROM chat_memory WHERE relevance < 0.05")}catch{}}function kn(s){try{let t=new Date(Date.now()-864e5).toISOString();P(s,`DELETE FROM chat_memory WHERE tier = 'short' AND created_at < ${k(t)}`)}catch{}}import{existsSync as Xe,readFileSync as gr,mkdirSync as Sn,writeFileSync as vn,renameSync as In}from"node:fs";import{homedir as Nn}from"node:os";import{join as _r,dirname as br,resolve as wr}from"node:path";import{fileURLToPath as Rn}from"node:url";function On(){if(process.env.MCP_SKILL_PATH)return process.env.MCP_SKILL_PATH;let s=br(Rn(import.meta.url)),t=wr(s,"..","bin","mcp-skill.mjs");return Xe(t)?t:null}function An(){if(process.env.PROJECT_API_TOKEN)return process.env.PROJECT_API_TOKEN;if(process.env.ZIBBY_USER_TOKEN)return process.env.ZIBBY_USER_TOKEN;try{let s=_r(Nn(),".zibby","config.json");return Xe(s)&&JSON.parse(gr(s,"utf-8")).sessionToken||null}catch{return null}}function $n(){return process.env.ZIBBY_ACCOUNT_API_URL?process.env.ZIBBY_ACCOUNT_API_URL.replace(/\/$/,""):(process.env.ZIBBY_ENV||"prod")==="local"?"http://localhost:3001":process.env.ZIBBY_PROD_ACCOUNT_API_URL||"https://api-prod.zibby.app"}async function jn(s,t){let e=An();if(!e)throw new Error("No backend credential (PROJECT_API_TOKEN). Review memory is only available inside a Zibby run.");let r=`${$n()}/credits/review-memory`,i=await fetch(r,{method:"POST",headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"},body:JSON.stringify({op:s,...t})});if(!i.ok){let n=await i.text().catch(()=>"");throw new Error(`Review memory ${s} failed (${i.status}): ${n.slice(0,300)}`)}return i.json()}function Tn(){let s=process.env.ZIBBY_SELF_HOST;return s!=null&&s!==""&&s!=="0"&&s.toLowerCase()!=="false"}function En(){if(!Tn()||(process.env.ZIBBY_REVIEW_MEMORY_BACKEND||"").trim().toLowerCase()==="none")return null;let t=process.env.ZIBBY_REVIEW_MEMORY_PATH,e=t&&t.trim()?t.trim():_r(".zibby","review-memory.json");return wr(process.cwd(),e)}function Ve(s){try{if(!Xe(s))return{};let t=gr(s,"utf-8");if(!t.trim())return{};let e=JSON.parse(t);return e&&typeof e=="object"&&!Array.isArray(e)?e:{}}catch{return{}}}function Ln(s,t){let e=br(s);Sn(e,{recursive:!0});let r=`${s}.tmp-${process.pid}-${Date.now()}`;vn(r,`${JSON.stringify(t,null,2)}
579
+ `,"utf-8"),In(r,s)}function hr(s){return!s||typeof s!="object"?null:{scope:s.scope,content:s.content,metadata:s.metadata||null,headSha:s.headSha||null,createdAt:s.createdAt||null,updatedAt:s.updatedAt||null}}function xn(s,t,e){if(t==="store"){let r=Ve(s),i=new Date().toISOString(),n=r[e.scope],o={scope:e.scope,content:e.content,metadata:e.metadata!=null?e.metadata:void 0,headSha:e.headSha!=null?e.headSha:void 0,createdAt:n&&n.createdAt||i,updatedAt:i};return r[e.scope]=o,Ln(s,r),{stored:!0,scope:e.scope,headSha:o.headSha||null,updatedAt:i}}if(t==="recall"){let i=Ve(s)[e.scope];return i?{found:!0,memory:hr(i)}:{found:!1,memory:null}}if(t==="recall-prefix"){let r=Ve(s),i=Object.values(r).filter(n=>n&&typeof n.scope=="string"&&n.scope.startsWith(e.scopePrefix)).map(hr).slice(0,25);return{count:i.length,truncated:!1,memories:i}}return{error:`Unknown op: ${t}`}}async function Qe(s,t){let e=En();return e?xn(e,s,t):jn(s,t)}var kr={id:"review-memory",serverName:"review_memory",allowedTools:["mcp__review_memory__*"],description:"Review memory \u2014 persist & recall per-PR (configurable-scope) review notes across stateless runs",promptFragment:`## Review Memory (per-PR, configurable scope)
571
580
  Persist what you pulled, found, and reasoned during a review so a FOLLOW-UP
572
581
  run (a fresh stateless task replying on the same PR) can recall it. Storage is
573
582
  keyed by a \`scope\` STRING you choose \u2014 nothing is hardcoded to per-PR:
@@ -584,7 +593,7 @@ Tools:
584
593
  (e.g. "review:owner/repo#") \u2014 e.g. to see prior reviews across a repo.
585
594
  - review_memory_store: At the END (or when you learn something durable), store
586
595
  a concise note under the PR's scope. Pass headSha so a later run knows which
587
- commit the note describes. Overwrites the prior note for that exact scope.`,resolve(){let s=wn();if(!s)return{command:null,args:[],env:{},description:this.description};let t={};for(let e of["PROJECT_API_TOKEN","ZIBBY_ACCOUNT_API_URL","ZIBBY_ENV","ZIBBY_PROD_ACCOUNT_API_URL","ZIBBY_USER_TOKEN"])process.env[e]&&(t[e]=process.env[e]);return{type:"stdio",command:"node",args:[s,"../dist/reviewMemory.js","reviewMemorySkill"],env:t,description:this.description,alwaysLoad:!0}},async handleToolCall(s,t){try{switch(s){case"review_memory_recall":{let e=typeof t?.scope=="string"?t.scope.trim():"";if(!e)return JSON.stringify({error:"scope is required"});let r=await He("recall",{scope:e});return JSON.stringify(r)}case"review_memory_recall_prefix":{let e=typeof t?.scopePrefix=="string"?t.scopePrefix.trim():"";if(!e)return JSON.stringify({error:"scopePrefix is required"});let r=await He("recall-prefix",{scopePrefix:e});return JSON.stringify(r)}case"review_memory_store":{let e=typeof t?.scope=="string"?t.scope.trim():"";if(!e)return JSON.stringify({error:"scope is required"});if(typeof t?.content!="string"||t.content.length===0)return JSON.stringify({error:"content is required (non-empty string)"});let r={scope:e,content:t.content};t.metadata!=null&&(r.metadata=t.metadata),t.headSha!=null&&(r.headSha=String(t.headSha));let i=await He("store",r);return JSON.stringify(i)}default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(e){return JSON.stringify({error:e.message})}},tools:[{name:"review_memory_recall",description:'Recall the prior review note for a scope (exact match). Use at the start of a review to build on what an earlier run pulled/found/reasoned. scope is the storage key \u2014 e.g. "review:owner/repo#42" for per-PR.',input_schema:{type:"object",properties:{scope:{type:"string",description:'Storage key. Per-PR: "review:owner/repo#<prNumber>". Per-repo: "repo:owner/repo". Per-org: "org:owner". Or any custom string.'},query:{type:"string",description:"Optional free-text hint (reserved for future semantic recall; v1 ignores it \u2014 recall is exact-scope)."}},required:["scope"]}},{name:"review_memory_recall_prefix",description:'List review notes whose scope STARTS WITH a prefix (e.g. "review:owner/repo#" to see all prior PR reviews in a repo). Capped at 25 most-relevant.',input_schema:{type:"object",properties:{scopePrefix:{type:"string",description:'Scope prefix to match, e.g. "review:owner/repo#" or "repo:owner/".'}},required:["scopePrefix"]}},{name:"review_memory_store",description:"Store (overwrite) the review note for a scope so a follow-up run can recall it. Write a concise summary of what you pulled, found, and reasoned. Pass headSha so a later run knows which commit the note describes.",input_schema:{type:"object",properties:{scope:{type:"string",description:'Storage key. Per-PR: "review:owner/repo#<prNumber>". Or per-repo/per-org/custom. Same key you recall by.'},content:{type:"string",description:"The note: what was pulled, what was found, the reasoning. Free-form markdown/text."},metadata:{type:"object",description:"Optional structured metadata (e.g. {filesReviewed, verdict, severity})."},headSha:{type:"string",description:"Optional PR head commit SHA this note describes \u2014 lets a later run detect new commits."}},required:["scope","content"]}}]};import{existsSync as W,readFileSync as me,readdirSync as Ye,mkdirSync as Nn,writeFileSync as ne,statSync as hr}from"fs";import{join as N,resolve as Ze,relative as gr,dirname as _r}from"path";import{fileURLToPath as In}from"url";import{createRequire as On}from"module";var Rn=On(import.meta.url),An=`## Workflow Builder
596
+ commit the note describes. Overwrites the prior note for that exact scope.`,resolve(){let s=On();if(!s)return{command:null,args:[],env:{},description:this.description};let t={};for(let e of["PROJECT_API_TOKEN","ZIBBY_ACCOUNT_API_URL","ZIBBY_ENV","ZIBBY_PROD_ACCOUNT_API_URL","ZIBBY_USER_TOKEN","ZIBBY_SELF_HOST","ZIBBY_REVIEW_MEMORY_BACKEND","ZIBBY_REVIEW_MEMORY_PATH"])process.env[e]&&(t[e]=process.env[e]);return{type:"stdio",command:"node",args:[s,"../dist/reviewMemory.js","reviewMemorySkill"],env:t,description:this.description,alwaysLoad:!0}},async handleToolCall(s,t){try{switch(s){case"review_memory_recall":{let e=typeof t?.scope=="string"?t.scope.trim():"";if(!e)return JSON.stringify({error:"scope is required"});let r=await Qe("recall",{scope:e});return JSON.stringify(r)}case"review_memory_recall_prefix":{let e=typeof t?.scopePrefix=="string"?t.scopePrefix.trim():"";if(!e)return JSON.stringify({error:"scopePrefix is required"});let r=await Qe("recall-prefix",{scopePrefix:e});return JSON.stringify(r)}case"review_memory_store":{let e=typeof t?.scope=="string"?t.scope.trim():"";if(!e)return JSON.stringify({error:"scope is required"});if(typeof t?.content!="string"||t.content.length===0)return JSON.stringify({error:"content is required (non-empty string)"});let r={scope:e,content:t.content};t.metadata!=null&&(r.metadata=t.metadata),t.headSha!=null&&(r.headSha=String(t.headSha));let i=await Qe("store",r);return JSON.stringify(i)}default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(e){return JSON.stringify({error:e.message})}},tools:[{name:"review_memory_recall",description:'Recall the prior review note for a scope (exact match). Use at the start of a review to build on what an earlier run pulled/found/reasoned. scope is the storage key \u2014 e.g. "review:owner/repo#42" for per-PR.',input_schema:{type:"object",properties:{scope:{type:"string",description:'Storage key. Per-PR: "review:owner/repo#<prNumber>". Per-repo: "repo:owner/repo". Per-org: "org:owner". Or any custom string.'},query:{type:"string",description:"Optional free-text hint (reserved for future semantic recall; v1 ignores it \u2014 recall is exact-scope)."}},required:["scope"]}},{name:"review_memory_recall_prefix",description:'List review notes whose scope STARTS WITH a prefix (e.g. "review:owner/repo#" to see all prior PR reviews in a repo). Capped at 25 most-relevant.',input_schema:{type:"object",properties:{scopePrefix:{type:"string",description:'Scope prefix to match, e.g. "review:owner/repo#" or "repo:owner/".'}},required:["scopePrefix"]}},{name:"review_memory_store",description:"Store (overwrite) the review note for a scope so a follow-up run can recall it. Write a concise summary of what you pulled, found, and reasoned. Pass headSha so a later run knows which commit the note describes.",input_schema:{type:"object",properties:{scope:{type:"string",description:'Storage key. Per-PR: "review:owner/repo#<prNumber>". Or per-repo/per-org/custom. Same key you recall by.'},content:{type:"string",description:"The note: what was pulled, what was found, the reasoning. Free-form markdown/text."},metadata:{type:"object",description:"Optional structured metadata (e.g. {filesReviewed, verdict, severity})."},headSha:{type:"string",description:"Optional PR head commit SHA this note describes \u2014 lets a later run detect new commits."}},required:["scope","content"]}}]};import{existsSync as W,readFileSync as fe,readdirSync as et,mkdirSync as Cn,writeFileSync as ae,statSync as Ir}from"fs";import{join as N,resolve as tt,relative as Nr,dirname as Rr}from"path";import{fileURLToPath as Pn}from"url";import{createRequire as Jn}from"module";var Un=Jn(import.meta.url),qn=`## Workflow Builder
588
597
 
589
598
  You can help users build custom AI workflows using the Zibby workflow framework.
590
599
 
@@ -694,9 +703,9 @@ Call with no arguments to see all available topics.
694
703
  - Workflow names must be kebab-case (e.g., ticket-triage, pr-review).
695
704
  - State flows through: each node's validated output is stored under its name in state (e.g., state.classify_ticket).
696
705
  - Downstream nodes reference upstream outputs in their prompt function (e.g., \\\`\\\${JSON.stringify(state.classify_ticket, null, 2)}\\\`).
697
- - Nodes can declare skills to get MCP tool access \u2014 the framework handles server lifecycle automatically.`,br=/^[a-z][a-z0-9-]{0,62}[a-z0-9]$/;function kr(s){return`${s.split("-").map(t=>t.charAt(0).toUpperCase()+t.slice(1)).join("")}Workflow`}function de(s){return`${s.replace(/_([a-z])/g,(t,e)=>e.toUpperCase())}Node`}function $n(s){let t=s?.agent;return t?t.provider?t.provider:t.gemini?"gemini":t.codex?"codex":t.claude?"claude":t.cursor?"cursor":process.env.AGENT_TYPE||"cursor":process.env.AGENT_TYPE||"cursor"}async function jn(s){let t=Ze(s,".zibby.config.mjs");if(!W(t))return{};try{return(await import(t)).default||{}}catch{return{}}}function Tn(){try{let s=_r(Rn.resolve("@zibby/core/package.json")),t=N(s,"templates","browser-test-automation"),e=me(N(t,"nodes","preflight.mjs"),"utf-8"),r=me(N(t,"graph.mjs"),"utf-8");return{preflight:e,graph:r}}catch{return null}}var fr=_r(In(import.meta.url));function wr(){let s=Ze(fr,"..","..","..","docsite","docs");if(W(s))return s;let t=Ze(fr,"..","docs");return W(t)?t:null}function yr(){let s=wr();if(!s)return[];try{let t=(e,r="")=>{let i=[];for(let n of Ye(e)){let o=N(e,n);try{if(hr(o).isDirectory())i=i.concat(t(o,`${r}${n}/`));else if(n.endsWith(".md")){let c=`${r}${n.replace(/\.md$/,"")}`;i.push(c)}}catch{}}return i};return t(s)}catch{return[]}}function Sr(s){let t=wr();if(!t)return null;let e=N(t,`${s}.md`);if(!W(e))return null;try{return me(e,"utf-8")}catch{return null}}function En(s){let t=s.nodes.map(o=>{let c=o.inputFields?.length?`Input fields: ${o.inputFields.join(", ")}`:"Input: receives full state",a=o.outputFields?.length?`Output fields: ${o.outputFields.join(", ")}`:"Output: determined by task",u=o.skills?.length?`Skills: ${o.skills.join(", ")}`:"";return`- ${o.name}: ${o.description}. ${c}. ${a}.${u?` ${u}`:""}`}).join(`
706
+ - Nodes can declare skills to get MCP tool access \u2014 the framework handles server lifecycle automatically.`,Or=/^[a-z][a-z0-9-]{0,62}[a-z0-9]$/;function Ar(s){return`${s.split("-").map(t=>t.charAt(0).toUpperCase()+t.slice(1)).join("")}Workflow`}function me(s){return`${s.replace(/_([a-z])/g,(t,e)=>e.toUpperCase())}Node`}function Mn(s){let t=s?.agent;return t?t.provider?t.provider:t.gemini?"gemini":t.codex?"codex":t.claude?"claude":t.cursor?"cursor":process.env.AGENT_TYPE||"cursor":process.env.AGENT_TYPE||"cursor"}async function Dn(s){let t=tt(s,".zibby.config.mjs");if(!W(t))return{};try{return(await import(t)).default||{}}catch{return{}}}function Bn(){try{let s=Rr(Un.resolve("@zibby/core/package.json")),t=N(s,"templates","browser-test-automation"),e=fe(N(t,"nodes","preflight.mjs"),"utf-8"),r=fe(N(t,"graph.mjs"),"utf-8");return{preflight:e,graph:r}}catch{return null}}var Sr=Rr(Pn(import.meta.url));function $r(){let s=tt(Sr,"..","..","..","docsite","docs");if(W(s))return s;let t=tt(Sr,"..","docs");return W(t)?t:null}function vr(){let s=$r();if(!s)return[];try{let t=(e,r="")=>{let i=[];for(let n of et(e)){let o=N(e,n);try{if(Ir(o).isDirectory())i=i.concat(t(o,`${r}${n}/`));else if(n.endsWith(".md")){let c=`${r}${n.replace(/\.md$/,"")}`;i.push(c)}}catch{}}return i};return t(s)}catch{return[]}}function jr(s){let t=$r();if(!t)return null;let e=N(t,`${s}.md`);if(!W(e))return null;try{return fe(e,"utf-8")}catch{return null}}function Kn(s){let t=s.nodes.map(o=>{let c=o.inputFields?.length?`Input fields: ${o.inputFields.join(", ")}`:"Input: receives full state",a=o.outputFields?.length?`Output fields: ${o.outputFields.join(", ")}`:"Output: determined by task",u=o.skills?.length?`Skills: ${o.skills.join(", ")}`:"";return`- ${o.name}: ${o.description}. ${c}. ${a}.${u?` ${u}`:""}`}).join(`
698
707
  `),e=s.edges.map(o=>o.condition?`- ${o.from} \u2192 ${o.to} (conditional: ${o.condition})`:`- ${o.from} \u2192 ${o.to}`).join(`
699
- `),r=Tn(),i=Sr("custom-workflows"),n="";return r&&(n+=`
708
+ `),r=Bn(),i=jr("custom-workflows"),n="";return r&&(n+=`
700
709
  ## Real working examples from the Zibby framework
701
710
 
702
711
  ### Example node (preflight.mjs) \u2014 a prompt-only node with Zod schema and onComplete hook:
@@ -773,7 +782,7 @@ Return a JSON object with this exact structure:
773
782
  }
774
783
  }
775
784
 
776
- IMPORTANT: Return ONLY valid JSON. No markdown fences, no explanation outside the JSON.`}async function vr(s,t){let e=await jn(t),r=$n(e);try{let{invokeAgent:i}=await import("@zibby/core"),n=En(s),o=await i(n,{state:{agentType:r,config:e,cwd:t,workspace:t}},{model:e?.agent?.[r]?.model||"auto",workspace:t,config:e,timeout:12e4}),a=(typeof o=="string"?o:o?.raw||JSON.stringify(o?.structured||o)).match(/\{[\s\S]*\}/);if(!a)throw new Error("Agent did not return valid JSON");return JSON.parse(a[0])}catch(i){return console.warn(`Agent code generation failed (${i.message}), using templates`),Ln(s)}}function Ln(s){let t={};for(let e of s.nodes){let r=de(e.name),i=`${e.name.split("_").map(a=>a.charAt(0).toUpperCase()+a.slice(1)).join("")}OutputSchema`,n=e.outputFields?.length?e.outputFields.map(a=>` ${a}: z.string().describe('${a}'),`).join(`
785
+ IMPORTANT: Return ONLY valid JSON. No markdown fences, no explanation outside the JSON.`}async function Tr(s,t){let e=await Dn(t),r=Mn(e);try{let{invokeAgent:i}=await import("@zibby/core"),n=Kn(s),o=await i(n,{state:{agentType:r,config:e,cwd:t,workspace:t}},{model:e?.agent?.[r]?.model||"auto",workspace:t,config:e,timeout:12e4}),a=(typeof o=="string"?o:o?.raw||JSON.stringify(o?.structured||o)).match(/\{[\s\S]*\}/);if(!a)throw new Error("Agent did not return valid JSON");return JSON.parse(a[0])}catch(i){return console.warn(`Agent code generation failed (${i.message}), using templates`),Gn(s)}}function Gn(s){let t={};for(let e of s.nodes){let r=me(e.name),i=`${e.name.split("_").map(a=>a.charAt(0).toUpperCase()+a.slice(1)).join("")}OutputSchema`,n=e.outputFields?.length?e.outputFields.map(a=>` ${a}: z.string().describe('${a}'),`).join(`
777
786
  `):` summary: z.string().describe('Summary of the result'),
778
787
  status: z.enum(['ok', 'warn', 'error']).describe('Overall status'),`,o=s.edges.filter(a=>a.to===e.name&&a.from!=="START").map(a=>`Previous step (${a.from}): \${JSON.stringify(state.${a.from} || {}, null, 2)}`).join(`
779
788
  `),c=o?`${e.description}
@@ -795,20 +804,20 @@ export const ${r} = {
795
804
  prompt: (state) => \`${c}\`,
796
805
  outputSchema: ${i},
797
806
  };
798
- `}}return{nodes:t}}function xn(s,t,e,r){let i=t.toLowerCase(),n=kr(i),o=N(s,".zibby","workflows",i),c=N(o,"nodes");Nn(c,{recursive:!0});let a=e.nodes.map(g=>g.name);for(let g of e.nodes){let b=r.nodes?.[g.name]?.code;b&&ne(N(c,`${g.name.replace(/_/g,"-")}.mjs`),b,"utf-8")}let u=a.map(g=>{let b=de(g),h=g.replace(/_/g,"-");return`export { ${b} } from './${h}.mjs';`});ne(N(c,"index.mjs"),`${u.join(`
807
+ `}}return{nodes:t}}function Fn(s,t,e,r){let i=t.toLowerCase(),n=Ar(i),o=N(s,".zibby","workflows",i),c=N(o,"nodes");Cn(c,{recursive:!0});let a=e.nodes.map(g=>g.name);for(let g of e.nodes){let b=r.nodes?.[g.name]?.code;b&&ae(N(c,`${g.name.replace(/_/g,"-")}.mjs`),b,"utf-8")}let u=a.map(g=>{let b=me(g),h=g.replace(/_/g,"-");return`export { ${b} } from './${h}.mjs';`});ae(N(c,"index.mjs"),`${u.join(`
799
808
  `)}
800
- `,"utf-8");let l=a[0],p=a.map(g=>de(g)).join(", "),d=a.map(g=>` graph.addNode('${g}', ${de(g)});`).join(`
809
+ `,"utf-8");let l=a[0],d=a.map(g=>me(g)).join(", "),p=a.map(g=>` graph.addNode('${g}', ${me(g)});`).join(`
801
810
  `),m=e.edges.map(g=>g.condition?` graph.addConditionalEdges('${g.from}', (state) => {
802
811
  ${g.condition}
803
812
  });`:` graph.addEdge('${g.from}', '${g.to}');`).join(`
804
813
  `),f=`import { WorkflowAgent, WorkflowGraph } from '@zibby/core';
805
- import { ${p} } from './nodes/index.mjs';
814
+ import { ${d} } from './nodes/index.mjs';
806
815
 
807
816
  export class ${n} extends WorkflowAgent {
808
817
  buildGraph() {
809
818
  const graph = new WorkflowGraph();
810
819
 
811
- ${d}
820
+ ${p}
812
821
 
813
822
  graph.setEntryPoint('${l}');
814
823
  ${m}
@@ -820,16 +829,16 @@ ${m}
820
829
  console.log(\`[${i}] workflow complete \u2014 success: \${result.success !== false}\`);
821
830
  }
822
831
  }
823
- `;ne(N(o,"graph.mjs"),f,"utf-8");let y={name:i,description:e.description||`${n} workflow`,entryClass:n,triggers:{api:!0}};ne(N(o,"workflow.json"),`${JSON.stringify(y,null,2)}
824
- `,"utf-8");let _=["graph.mjs","workflow.json","nodes/index.mjs",...a.map(g=>`nodes/${g.replace(/_/g,"-")}.mjs`)];return{workflowDir:gr(s,o),files:_,className:n,slug:i}}async function Cn(s){let{name:t,description:e,nodes:r,edges:i}=s;if(!t||!br.test(t.toLowerCase()))return JSON.stringify({error:`Invalid workflow name "${t}". Must be kebab-case, 2-64 chars, lowercase letters/numbers/hyphens.`});if(!r||r.length===0)return JSON.stringify({error:"At least one node is required."});let n={name:t.toLowerCase(),description:e||`${kr(t.toLowerCase())} workflow`,nodes:r.map(o=>({name:o.name.replace(/-/g,"_"),description:o.description||`Process ${o.name}`,inputFields:o.inputFields||[],outputFields:o.outputFields||[]})),edges:i||[]};if(n.edges.length===0&&n.nodes.length>0){for(let o=0;o<n.nodes.length-1;o++)n.edges.push({from:n.nodes[o].name,to:n.nodes[o+1].name});n.edges.push({from:n.nodes[n.nodes.length-1].name,to:"END"})}return JSON.stringify({ok:!0,spec:n,message:`Workflow "${n.name}" designed with ${n.nodes.length} node(s). Call build_workflow to generate the code.`,preview:{nodes:n.nodes.map(o=>o.name),flow:n.edges.map(o=>o.condition?`${o.from} \u2192(if ${o.condition})\u2192 ${o.to}`:`${o.from} \u2192 ${o.to}`)}})}async function Pn(s,t){let{name:e,spec:r}=s,i=(e||r?.name||"").toLowerCase();if(!i||!br.test(i))return JSON.stringify({error:`Invalid workflow name "${i}".`});if(!r||!r.nodes||r.nodes.length===0)return JSON.stringify({error:"spec with nodes is required. Call design_workflow first."});let n=N(t,".zibby","workflows",i);if(W(n))return JSON.stringify({error:`Workflow "${i}" already exists at .zibby/workflows/${i}/. Delete it first or choose a different name.`});let o=await vr(r,t),c=xn(t,i,r,o);return JSON.stringify({ok:!0,...c,message:`Workflow "${i}" created at ${c.workflowDir}/`,nextSteps:[`Test locally: zibby start ${i}`,`Deploy to cloud: zibby deploy ${i} --project <project-id>`,`Tail logs: zibby logs --workflow ${i} --project <project-id>`]})}async function Jn(s,t){let{workflowName:e,nodeName:r,description:i,inputFields:n,outputFields:o}=s,c=(e||"").toLowerCase(),a=(r||"").replace(/-/g,"_"),u=N(t,".zibby","workflows",c);if(!W(u))return JSON.stringify({error:`Workflow "${c}" not found. Create it first with build_workflow.`});let l={name:c,description:"",nodes:[{name:a,description:i||`Process ${a}`,inputFields:n||[],outputFields:o||[]}],edges:[]},d=(await vr(l,t)).nodes?.[a]?.code;if(!d)return JSON.stringify({error:"Failed to generate node code."});let m=N(u,"nodes"),f=`${a.replace(/_/g,"-")}.mjs`;ne(N(m,f),d,"utf-8");let y=N(m,"index.mjs"),_=de(a),g=`export { ${_} } from './${a.replace(/_/g,"-")}.mjs';
825
- `,b=W(y)?me(y,"utf-8"):"";return b.includes(_)||ne(y,b+g,"utf-8"),JSON.stringify({ok:!0,file:`nodes/${f}`,exportName:_,message:`Node "${a}" added. Update graph.mjs to wire it into the graph.`})}async function Un(s,t){let{name:e,projectId:r}=s,i=(e||"").toLowerCase();if(!i)return JSON.stringify({error:"Workflow name is required."});if(!r)return JSON.stringify({error:"projectId is required."});let n=N(t,".zibby","workflows",i);if(!W(n))return JSON.stringify({error:`Workflow "${i}" not found at .zibby/workflows/${i}/`});try{let{execSync:o}=await import("child_process"),c=o(`node "${N(t,"packages/cli/bin/zibby.js")}" deploy ${i} --project ${r}`,{cwd:t,encoding:"utf-8",timeout:3e4,stdio:["pipe","pipe","pipe"]});return JSON.stringify({ok:!0,output:c.trim()})}catch{try{let{execSync:c}=await import("child_process"),a=c(`npx zibby deploy ${i} --project ${r}`,{cwd:t,encoding:"utf-8",timeout:3e4,stdio:["pipe","pipe","pipe"]});return JSON.stringify({ok:!0,output:a.trim()})}catch(c){return JSON.stringify({error:`Deploy failed: ${c.message}`})}}}function qn(s){let t=N(s,".zibby","workflows");if(!W(t))return JSON.stringify({workflows:[],message:"No workflows found. Use build_workflow to create one."});let r=Ye(t).filter(i=>{try{return hr(N(t,i)).isDirectory()}catch{return!1}}).map(i=>{let n=N(t,i,"workflow.json"),o={};try{o=JSON.parse(me(n,"utf-8"))}catch{}let c=N(t,i,"nodes"),a=0;try{a=Ye(c).filter(u=>u.endsWith(".mjs")&&u!=="index.mjs").length}catch{}return{name:i,description:o.description||"",nodeCount:a,path:gr(s,N(t,i))}});return JSON.stringify({workflows:r})}var Nr={id:"workflow-builder",description:"Build, scaffold, and deploy custom AI workflows via conversation",envKeys:[],promptFragment:An,tools:[{name:"design_workflow",description:"Design a workflow spec (nodes, edges, descriptions) for the user to review before building. Call this after understanding requirements.",input_schema:{type:"object",properties:{name:{type:"string",description:"Workflow name in kebab-case (e.g., ticket-triage)"},description:{type:"string",description:"What the workflow does"},nodes:{type:"array",items:{type:"object",properties:{name:{type:"string",description:"Node name in snake_case (e.g., classify_ticket)"},description:{type:"string",description:"What this node does \u2014 be specific about input/output"},inputFields:{type:"array",items:{type:"string"},description:"Key fields this node reads from state"},outputFields:{type:"array",items:{type:"string"},description:"Key fields this node produces"}},required:["name","description"]},description:"Workflow nodes (processing steps)"},edges:{type:"array",items:{type:"object",properties:{from:{type:"string",description:"Source node name"},to:{type:"string",description:'Target node name (or "END")'},condition:{type:"string",description:"JS expression for conditional routing (optional)"}},required:["from","to"]},description:"Edges connecting nodes. If omitted, nodes are wired linearly."}},required:["name","description","nodes"]}},{name:"build_workflow",description:"Generate real workflow code on disk from a design spec. Uses the configured AI agent for high-quality code generation.",input_schema:{type:"object",properties:{name:{type:"string",description:"Workflow name (from design_workflow)"},spec:{type:"object",description:"The full spec object returned by design_workflow",properties:{name:{type:"string"},description:{type:"string"},nodes:{type:"array",items:{type:"object"}},edges:{type:"array",items:{type:"object"}}}}},required:["name","spec"]}},{name:"add_node",description:"Add a new node to an existing workflow. Generates the node file and updates the barrel export.",input_schema:{type:"object",properties:{workflowName:{type:"string",description:"Existing workflow name (kebab-case)"},nodeName:{type:"string",description:"New node name (snake_case)"},description:{type:"string",description:"What this node does"},inputFields:{type:"array",items:{type:"string"},description:"Fields read from state"},outputFields:{type:"array",items:{type:"string"},description:"Fields produced"}},required:["workflowName","nodeName","description"]}},{name:"deploy_workflow",description:"Deploy a workflow to Zibby Cloud. Returns the trigger URL.",input_schema:{type:"object",properties:{name:{type:"string",description:"Workflow name to deploy"},projectId:{type:"string",description:"Target project ID"}},required:["name","projectId"]}},{name:"list_workflows",description:"List all local workflows in .zibby/workflows/.",input_schema:{type:"object",properties:{}}},{name:"explore_framework_docs",description:"Read Zibby framework documentation on demand. Call this before building complex workflows or when you need details on advanced patterns (middleware, conditional routing, skills, deployment, CLI commands).",input_schema:{type:"object",properties:{topic:{type:"string",description:'Doc topic to read (e.g., "workflow", "custom-workflows", "cli-reference", "packages/core", "packages/skills", "integrations/jira"). Call with no topic to list all available docs.'}}}}],async handleToolCall(s,t,e){let r=e?.options?.workspace||process.cwd();try{switch(s){case"design_workflow":return await Cn(t);case"build_workflow":return await Pn(t,r);case"add_node":return await Jn(t,r);case"deploy_workflow":return await Un(t,r);case"list_workflows":return qn(r);case"explore_framework_docs":{let i=(t.topic||"").trim();if(!i){let o=yr();return JSON.stringify({available:o,hint:"Call again with a topic to read its content."})}let n=Sr(i);if(!n){let o=yr();return JSON.stringify({error:`Doc "${i}" not found.`,available:o})}return JSON.stringify({topic:i,content:n})}default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(i){return JSON.stringify({error:i.message})}},resolve(){return null}};import{resolveIntegrationToken as Mn}from"@zibby/core/backend-client.js";var Qe=Object.freeze({id:"openai_billing",requiresIntegration:R.OPENAI_BILLING,description:"OpenAI organization billing/usage admin API (paste sk-admin-... key)"}),Xe=Object.freeze({id:"anthropic_billing",requiresIntegration:R.ANTHROPIC_BILLING,description:"Anthropic organization cost/usage admin API (paste sk-ant-admin-... key)"}),et=Object.freeze({id:"cursor_admin",requiresIntegration:R.CURSOR_ADMIN,description:"Cursor Team/Enterprise admin API (paste admin key)"});function Ir(s){return Math.floor(s/1e3)}function Ve(s){return new Date(s).toISOString().slice(0,10)}function Or(s){return new Date(s).toISOString()}async function fe(s){let t=await Mn(s);if(!t?.token)throw new Error(`${s} token resolver returned no token`);return t.token}async function Rr({startMs:s,endMs:t,groupBy:e=["project_id","line_item"]}){let r=await fe("openai_billing"),i=[],n=0,o=e.map(a=>`group_by[]=${encodeURIComponent(a)}`).join("&"),c=null;for(let a=0;a<50;a++){let l=`https://api.openai.com/v1/organization/costs?${[`start_time=${Ir(s)}`,`end_time=${Ir(t)}`,"bucket_width=1d","limit=180",o,c?`page=${encodeURIComponent(c)}`:""].filter(Boolean).join("&")}`,p=await fetch(l,{headers:{Authorization:`Bearer ${r}`}});if(!p.ok){let m=await p.text().catch(()=>"");throw new Error(`OpenAI costs API ${p.status}: ${m.slice(0,200)}`)}let d=await p.json();for(let m of d.data||[]){n+=1;let f=Ve((m.start_time||0)*1e3);for(let y of m.results||[])i.push({provider:"openai",day:f,costUsd:Number(y.amount?.value??0),projectId:y.project_id||void 0,apiKeyId:y.api_key_id||void 0,model:y.line_item||void 0})}if(!d.has_more||!d.next_page)break;c=d.next_page}return{ok:!0,items:i,rawBuckets:n}}async function Dn(){let s=await fe("openai_billing"),e=await fetch("https://api.openai.com/v1/organization/projects?limit=100",{headers:{Authorization:`Bearer ${s}`}});if(!e.ok){let n=await e.text().catch(()=>"");throw new Error(`OpenAI projects API ${e.status}: ${n.slice(0,200)}`)}let r=await e.json(),i=new Map;for(let n of r.data||[])i.set(n.id,n.name);return i}async function Ar({startMs:s,endMs:t,groupBy:e=["workspace_id"]}){let r=await fe("anthropic_billing"),i=[],n=0,o=e.map(a=>`group_by[]=${encodeURIComponent(a)}`).join("&"),c=null;for(let a=0;a<50;a++){let l=`https://api.anthropic.com/v1/organizations/cost_report?${[`starting_at=${encodeURIComponent(Or(s))}`,`ending_at=${encodeURIComponent(Or(t))}`,"bucket=1d","limit=100",o,c?`page=${encodeURIComponent(c)}`:""].filter(Boolean).join("&")}`,p=await fetch(l,{headers:{"x-api-key":r,"anthropic-version":"2023-06-01"}});if(!p.ok){let m=await p.text().catch(()=>"");throw new Error(`Anthropic cost_report ${p.status}: ${m.slice(0,200)}`)}let d=await p.json();for(let m of d.data||[]){n+=1;let f=(m.starting_at||"").slice(0,10);for(let y of m.results||[])i.push({provider:"anthropic",day:f,costUsd:Number(y.amount??y.cost??0),workspaceId:y.workspace_id||void 0,apiKeyId:y.api_key_id||void 0,model:y.model||void 0,tokensIn:y.uncached_input_tokens!=null?Number(y.uncached_input_tokens):void 0,tokensOut:y.output_tokens!=null?Number(y.output_tokens):void 0,cachedTokens:y.cached_input_tokens!=null?Number(y.cached_input_tokens):void 0})}if(!d.has_more||!d.next_page)break;c=d.next_page}return{ok:!0,items:i,rawBuckets:n}}async function Bn(){let s=await fe("anthropic_billing"),e=await fetch("https://api.anthropic.com/v1/organizations/workspaces?limit=100",{headers:{"x-api-key":s,"anthropic-version":"2023-06-01"}});if(!e.ok){let n=await e.text().catch(()=>"");throw new Error(`Anthropic workspaces ${e.status}: ${n.slice(0,200)}`)}let r=await e.json(),i=new Map;for(let n of r.data||[])i.set(n.id,n.name);return i}async function $r({startMs:s,endMs:t}){let e=await fe("cursor_admin"),r=Ve(s),i=Ve(t),n=`https://api.cursor.com/teams/daily-usage-data?startDate=${r}&endDate=${i}`,o=await fetch(n,{headers:{Authorization:`Bearer ${e}`}});if(!o.ok){let l=await o.text().catch(()=>"");throw new Error(`Cursor daily-usage ${o.status}: ${l.slice(0,200)}`)}let c=await o.json(),a=[],u=0;for(let l of c.data||[]){u+=1;let p=l.date;for(let d of l.userMetrics||[]){for(let m of d.modelUsage||[]){let f=Number(m.acceptedLines??0),y=Number(m.suggestedLines??0);a.push({provider:"cursor",day:p,costUsd:Number(m.totalCents??0)/100,userEmail:d.email,model:m.model,requestCount:Number(m.requestCount??0),acceptanceRate:y>0?f/y:void 0})}(!d.modelUsage||d.modelUsage.length===0)&&a.push({provider:"cursor",day:p,costUsd:Number(d.totalCents??0)/100,userEmail:d.email})}}return{ok:!0,items:a,rawBuckets:u}}async function Kn({startMs:s,endMs:t}){let[e,r,i]=await Promise.allSettled([Rr({startMs:s,endMs:t}),Ar({startMs:s,endMs:t}),$r({startMs:s,endMs:t})]),n=l=>l.status==="fulfilled"?l.value:{ok:!1,error:l.reason?.message||String(l.reason),items:[]},o=n(e),c=n(r),a=n(i),u=[{provider:"openai",totalUsd:o.items.reduce((l,p)=>l+(p.costUsd||0),0)},{provider:"anthropic",totalUsd:c.items.reduce((l,p)=>l+(p.costUsd||0),0)},{provider:"cursor",totalUsd:a.items.reduce((l,p)=>l+(p.costUsd||0),0)}];return{openai:o,anthropic:c,cursor:a,totals:u}}function Gn(s,t){let e=new Map;for(let r of s){let i=t(r);if(!i)continue;let n=e.get(i)||{key:i,totalUsd:0,count:0};n.totalUsd+=r.costUsd||0,n.count+=1,e.set(i,n)}return[...e.values()].sort((r,i)=>i.totalUsd-r.totalUsd)}function Fn(s){if(!s.length)return{mean:0,stddev:0};let t=s.reduce((r,i)=>r+i,0)/s.length,e=s.reduce((r,i)=>r+(i-t)**2,0)/s.length;return{mean:t,stddev:Math.sqrt(e)}}import{z as k}from"zod";var ye=["ok","info","warn","critical"],zn=k.object({primary:k.string().min(1).max(200).describe('Headline number or phrase (e.g. "$8,240"). Rendered in large/bold.'),delta:k.object({value:k.string().max(40).describe('Delta vs baseline (e.g. "+12% wow"). Free-form string.'),direction:k.enum(["up","down","flat"]).optional(),severity:k.enum(ye).optional().describe("Color severity for the delta (warn/critical highlights regressions).")}).optional().describe("Optional comparison vs baseline. Renders inline next to primary."),summary:k.string().max(800).optional().describe('One-sentence narrative ("why this number"). Plain prose.')}),Wn=k.object({kind:k.literal("trend"),title:k.string().max(120).optional(),labels:k.array(k.string().max(60)).min(2).max(20).describe('Bucket labels (e.g. ["Week-3", "Week-2", "Week-1", "This wk"]).'),values:k.array(k.number()).min(2).max(20).describe("Numeric values, one per label. Must match labels.length."),highlight:k.enum(["last","max","min","none"]).default("last").optional().describe("Which bucket to visually highlight in the rendered card."),severity:k.enum(ye).optional()}),Hn=k.object({kind:k.literal("table"),title:k.string().max(120).optional(),headers:k.array(k.string().max(40)).min(1).max(8),rows:k.array(k.array(k.union([k.string().max(200),k.number()])).min(1).max(8)).max(40).describe("2D matrix. Each inner array must have headers.length entries.")}),Yn=k.object({kind:k.literal("callouts"),title:k.string().max(120).optional(),tone:k.enum(ye).default("info").optional(),items:k.array(k.string().min(1).max(600)).min(1).max(10).describe("Each item renders as a bullet with a severity emoji.")}),Zn=k.object({kind:k.literal("breakdown"),title:k.string().max(120).optional(),rows:k.array(k.object({label:k.string().min(1).max(80),value:k.string().min(1).max(80),sub:k.string().max(120).optional(),severity:k.enum(ye).optional()})).min(1).max(20)}),Vn=k.object({kind:k.literal("paragraph"),title:k.string().max(120).optional(),text:k.string().min(1).max(3e3)}),Qn=k.discriminatedUnion("kind",[Wn,Hn,Yn,Zn,Vn]),tt=k.object({title:k.string().min(1).max(200).describe('Card title (e.g. "Weekly AI Spend Report").'),subtitle:k.string().max(200).optional().describe('Date range or smaller header (e.g. "May 13 \u2014 May 20").'),headline:zn,sections:k.array(Qn).max(20).default([]).superRefine((s,t)=>{s.forEach((e,r)=>{e.kind==="trend"&&e.labels.length!==e.values.length&&t.addIssue({code:k.ZodIssueCode.custom,path:[r,"values"],message:"labels.length must equal values.length"}),e.kind==="table"&&!e.rows.every(i=>i.length===e.headers.length)&&t.addIssue({code:k.ZodIssueCode.custom,path:[r,"rows"],message:"every row must have headers.length entries"})})}),footer:k.object({viewUrl:k.string().url().optional().describe('Optional "View in Zibby" button URL.'),rerunUrl:k.string().url().optional().describe('Optional "Run again" button URL.')}).optional()}),V=Object.freeze({ok:"\u{1F7E2}",info:"\u{1F535}",warn:"\u{1F7E0}",critical:"\u{1F534}"}),jr=Object.freeze({up:"\u2191",down:"\u2193",flat:"\u2192"}),Xn=Object.freeze({ok:"green",info:"blue",warn:"orange",critical:"red"});function Tr(s,t,e=12){if(!Number.isFinite(s)||!Number.isFinite(t)||t<=0)return"";let r=Math.max(0,Math.min(1,s/t)),i=Math.round(r*e);return"\u2593".repeat(i)+"\u2591".repeat(e-i)}function rt(s,t){let e=String(s);return e.length>=t?e:e+" ".repeat(t-e.length)}function Er(s,t){let e=String(s);return e.length>=t?e:" ".repeat(t-e.length)+e}function Lr({headers:s,rows:t}){let e=s.map((o,c)=>{let a=Math.max(String(o).length,...t.map(u=>String(u[c]??"").length));return Math.min(a,32)}),r=o=>o.map((c,a)=>rt(c,e[a])).join(" "),i=e.map(o=>"\u2500".repeat(o)).join(" ");return"```\n"+[r(s),i,...t.map(o=>r(o))].join(`
826
- `)+"\n```"}function eo(s){let t=tt.parse(s),e=[];e.push({type:"header",text:{type:"plain_text",text:t.title.slice(0,150),emoji:!0}}),t.subtitle&&e.push({type:"context",elements:[{type:"mrkdwn",text:t.subtitle}]});let r=[`*${t.headline.primary}*`];if(t.headline.delta){let n=jr[t.headline.delta.direction]||"",o=t.headline.delta.severity?V[t.headline.delta.severity]:"";r.push(`${n} ${t.headline.delta.value} ${o}`.trim())}let i=r.join(" ");t.headline.summary&&(i+=`
827
- `+t.headline.summary),e.push({type:"section",text:{type:"mrkdwn",text:i}});for(let n of t.sections)switch(e.push({type:"divider"}),n.title&&e.push({type:"section",text:{type:"mrkdwn",text:`*${n.title}*`}}),n.kind){case"trend":{let o=Math.max(...n.values),c=n.labels.map((a,u)=>{let l=n.values[u],p=Tr(l,o),m=(n.highlight==="last"&&u===n.labels.length-1||n.highlight==="max"&&l===o||n.highlight==="min"&&l===Math.min(...n.values))&&n.severity?` ${V[n.severity]}`:"";return`${rt(a,10)} ${Er(l.toLocaleString(),8)} ${p}${m}`});e.push({type:"section",text:{type:"mrkdwn",text:"```\n"+c.join(`
828
- `)+"\n```"}});break}case"table":{e.push({type:"section",text:{type:"mrkdwn",text:Lr(n)}});break}case"callouts":{let o=V[n.tone||"info"];e.push({type:"section",text:{type:"mrkdwn",text:n.items.map(c=>`${o} ${c}`).join(`
832
+ `;ae(N(o,"graph.mjs"),f,"utf-8");let y={name:i,description:e.description||`${n} workflow`,entryClass:n,triggers:{api:!0}};ae(N(o,"workflow.json"),`${JSON.stringify(y,null,2)}
833
+ `,"utf-8");let _=["graph.mjs","workflow.json","nodes/index.mjs",...a.map(g=>`nodes/${g.replace(/_/g,"-")}.mjs`)];return{workflowDir:Nr(s,o),files:_,className:n,slug:i}}async function zn(s){let{name:t,description:e,nodes:r,edges:i}=s;if(!t||!Or.test(t.toLowerCase()))return JSON.stringify({error:`Invalid workflow name "${t}". Must be kebab-case, 2-64 chars, lowercase letters/numbers/hyphens.`});if(!r||r.length===0)return JSON.stringify({error:"At least one node is required."});let n={name:t.toLowerCase(),description:e||`${Ar(t.toLowerCase())} workflow`,nodes:r.map(o=>({name:o.name.replace(/-/g,"_"),description:o.description||`Process ${o.name}`,inputFields:o.inputFields||[],outputFields:o.outputFields||[]})),edges:i||[]};if(n.edges.length===0&&n.nodes.length>0){for(let o=0;o<n.nodes.length-1;o++)n.edges.push({from:n.nodes[o].name,to:n.nodes[o+1].name});n.edges.push({from:n.nodes[n.nodes.length-1].name,to:"END"})}return JSON.stringify({ok:!0,spec:n,message:`Workflow "${n.name}" designed with ${n.nodes.length} node(s). Call build_workflow to generate the code.`,preview:{nodes:n.nodes.map(o=>o.name),flow:n.edges.map(o=>o.condition?`${o.from} \u2192(if ${o.condition})\u2192 ${o.to}`:`${o.from} \u2192 ${o.to}`)}})}async function Hn(s,t){let{name:e,spec:r}=s,i=(e||r?.name||"").toLowerCase();if(!i||!Or.test(i))return JSON.stringify({error:`Invalid workflow name "${i}".`});if(!r||!r.nodes||r.nodes.length===0)return JSON.stringify({error:"spec with nodes is required. Call design_workflow first."});let n=N(t,".zibby","workflows",i);if(W(n))return JSON.stringify({error:`Workflow "${i}" already exists at .zibby/workflows/${i}/. Delete it first or choose a different name.`});let o=await Tr(r,t),c=Fn(t,i,r,o);return JSON.stringify({ok:!0,...c,message:`Workflow "${i}" created at ${c.workflowDir}/`,nextSteps:[`Test locally: zibby start ${i}`,`Deploy to cloud: zibby deploy ${i} --project <project-id>`,`Tail logs: zibby logs --workflow ${i} --project <project-id>`]})}async function Wn(s,t){let{workflowName:e,nodeName:r,description:i,inputFields:n,outputFields:o}=s,c=(e||"").toLowerCase(),a=(r||"").replace(/-/g,"_"),u=N(t,".zibby","workflows",c);if(!W(u))return JSON.stringify({error:`Workflow "${c}" not found. Create it first with build_workflow.`});let l={name:c,description:"",nodes:[{name:a,description:i||`Process ${a}`,inputFields:n||[],outputFields:o||[]}],edges:[]},p=(await Tr(l,t)).nodes?.[a]?.code;if(!p)return JSON.stringify({error:"Failed to generate node code."});let m=N(u,"nodes"),f=`${a.replace(/_/g,"-")}.mjs`;ae(N(m,f),p,"utf-8");let y=N(m,"index.mjs"),_=me(a),g=`export { ${_} } from './${a.replace(/_/g,"-")}.mjs';
834
+ `,b=W(y)?fe(y,"utf-8"):"";return b.includes(_)||ae(y,b+g,"utf-8"),JSON.stringify({ok:!0,file:`nodes/${f}`,exportName:_,message:`Node "${a}" added. Update graph.mjs to wire it into the graph.`})}async function Yn(s,t){let{name:e,projectId:r}=s,i=(e||"").toLowerCase();if(!i)return JSON.stringify({error:"Workflow name is required."});if(!r)return JSON.stringify({error:"projectId is required."});let n=N(t,".zibby","workflows",i);if(!W(n))return JSON.stringify({error:`Workflow "${i}" not found at .zibby/workflows/${i}/`});try{let{execSync:o}=await import("child_process"),c=o(`node "${N(t,"packages/cli/bin/zibby.js")}" deploy ${i} --project ${r}`,{cwd:t,encoding:"utf-8",timeout:3e4,stdio:["pipe","pipe","pipe"]});return JSON.stringify({ok:!0,output:c.trim()})}catch{try{let{execSync:c}=await import("child_process"),a=c(`npx zibby deploy ${i} --project ${r}`,{cwd:t,encoding:"utf-8",timeout:3e4,stdio:["pipe","pipe","pipe"]});return JSON.stringify({ok:!0,output:a.trim()})}catch(c){return JSON.stringify({error:`Deploy failed: ${c.message}`})}}}function Zn(s){let t=N(s,".zibby","workflows");if(!W(t))return JSON.stringify({workflows:[],message:"No workflows found. Use build_workflow to create one."});let r=et(t).filter(i=>{try{return Ir(N(t,i)).isDirectory()}catch{return!1}}).map(i=>{let n=N(t,i,"workflow.json"),o={};try{o=JSON.parse(fe(n,"utf-8"))}catch{}let c=N(t,i,"nodes"),a=0;try{a=et(c).filter(u=>u.endsWith(".mjs")&&u!=="index.mjs").length}catch{}return{name:i,description:o.description||"",nodeCount:a,path:Nr(s,N(t,i))}});return JSON.stringify({workflows:r})}var Er={id:"workflow-builder",description:"Build, scaffold, and deploy custom AI workflows via conversation",envKeys:[],promptFragment:qn,tools:[{name:"design_workflow",description:"Design a workflow spec (nodes, edges, descriptions) for the user to review before building. Call this after understanding requirements.",input_schema:{type:"object",properties:{name:{type:"string",description:"Workflow name in kebab-case (e.g., ticket-triage)"},description:{type:"string",description:"What the workflow does"},nodes:{type:"array",items:{type:"object",properties:{name:{type:"string",description:"Node name in snake_case (e.g., classify_ticket)"},description:{type:"string",description:"What this node does \u2014 be specific about input/output"},inputFields:{type:"array",items:{type:"string"},description:"Key fields this node reads from state"},outputFields:{type:"array",items:{type:"string"},description:"Key fields this node produces"}},required:["name","description"]},description:"Workflow nodes (processing steps)"},edges:{type:"array",items:{type:"object",properties:{from:{type:"string",description:"Source node name"},to:{type:"string",description:'Target node name (or "END")'},condition:{type:"string",description:"JS expression for conditional routing (optional)"}},required:["from","to"]},description:"Edges connecting nodes. If omitted, nodes are wired linearly."}},required:["name","description","nodes"]}},{name:"build_workflow",description:"Generate real workflow code on disk from a design spec. Uses the configured AI agent for high-quality code generation.",input_schema:{type:"object",properties:{name:{type:"string",description:"Workflow name (from design_workflow)"},spec:{type:"object",description:"The full spec object returned by design_workflow",properties:{name:{type:"string"},description:{type:"string"},nodes:{type:"array",items:{type:"object"}},edges:{type:"array",items:{type:"object"}}}}},required:["name","spec"]}},{name:"add_node",description:"Add a new node to an existing workflow. Generates the node file and updates the barrel export.",input_schema:{type:"object",properties:{workflowName:{type:"string",description:"Existing workflow name (kebab-case)"},nodeName:{type:"string",description:"New node name (snake_case)"},description:{type:"string",description:"What this node does"},inputFields:{type:"array",items:{type:"string"},description:"Fields read from state"},outputFields:{type:"array",items:{type:"string"},description:"Fields produced"}},required:["workflowName","nodeName","description"]}},{name:"deploy_workflow",description:"Deploy a workflow to Zibby Cloud. Returns the trigger URL.",input_schema:{type:"object",properties:{name:{type:"string",description:"Workflow name to deploy"},projectId:{type:"string",description:"Target project ID"}},required:["name","projectId"]}},{name:"list_workflows",description:"List all local workflows in .zibby/workflows/.",input_schema:{type:"object",properties:{}}},{name:"explore_framework_docs",description:"Read Zibby framework documentation on demand. Call this before building complex workflows or when you need details on advanced patterns (middleware, conditional routing, skills, deployment, CLI commands).",input_schema:{type:"object",properties:{topic:{type:"string",description:'Doc topic to read (e.g., "workflow", "custom-workflows", "cli-reference", "packages/core", "packages/skills", "integrations/jira"). Call with no topic to list all available docs.'}}}}],async handleToolCall(s,t,e){let r=e?.options?.workspace||process.cwd();try{switch(s){case"design_workflow":return await zn(t);case"build_workflow":return await Hn(t,r);case"add_node":return await Wn(t,r);case"deploy_workflow":return await Yn(t,r);case"list_workflows":return Zn(r);case"explore_framework_docs":{let i=(t.topic||"").trim();if(!i){let o=vr();return JSON.stringify({available:o,hint:"Call again with a topic to read its content."})}let n=jr(i);if(!n){let o=vr();return JSON.stringify({error:`Doc "${i}" not found.`,available:o})}return JSON.stringify({topic:i,content:n})}default:return JSON.stringify({error:`Unknown tool: ${s}`})}}catch(i){return JSON.stringify({error:i.message})}},resolve(){return null}};import{resolveIntegrationToken as Vn}from"@zibby/core/backend-client.js";var st=Object.freeze({id:"openai_billing",requiresIntegration:A.OPENAI_BILLING,description:"OpenAI organization billing/usage admin API (paste sk-admin-... key)"}),it=Object.freeze({id:"anthropic_billing",requiresIntegration:A.ANTHROPIC_BILLING,description:"Anthropic organization cost/usage admin API (paste sk-ant-admin-... key)"}),nt=Object.freeze({id:"cursor_admin",requiresIntegration:A.CURSOR_ADMIN,description:"Cursor Team/Enterprise admin API (paste admin key)"});function Lr(s){return Math.floor(s/1e3)}function rt(s){return new Date(s).toISOString().slice(0,10)}function xr(s){return new Date(s).toISOString()}async function ye(s){let t=await Vn(s);if(!t?.token)throw new Error(`${s} token resolver returned no token`);return t.token}async function Cr({startMs:s,endMs:t,groupBy:e=["project_id","line_item"]}){let r=await ye("openai_billing"),i=[],n=0,o=e.map(a=>`group_by[]=${encodeURIComponent(a)}`).join("&"),c=null;for(let a=0;a<50;a++){let l=`https://api.openai.com/v1/organization/costs?${[`start_time=${Lr(s)}`,`end_time=${Lr(t)}`,"bucket_width=1d","limit=180",o,c?`page=${encodeURIComponent(c)}`:""].filter(Boolean).join("&")}`,d=await fetch(l,{headers:{Authorization:`Bearer ${r}`}});if(!d.ok){let m=await d.text().catch(()=>"");throw new Error(`OpenAI costs API ${d.status}: ${m.slice(0,200)}`)}let p=await d.json();for(let m of p.data||[]){n+=1;let f=rt((m.start_time||0)*1e3);for(let y of m.results||[])i.push({provider:"openai",day:f,costUsd:Number(y.amount?.value??0),projectId:y.project_id||void 0,apiKeyId:y.api_key_id||void 0,model:y.line_item||void 0})}if(!p.has_more||!p.next_page)break;c=p.next_page}return{ok:!0,items:i,rawBuckets:n}}async function Qn(){let s=await ye("openai_billing"),e=await fetch("https://api.openai.com/v1/organization/projects?limit=100",{headers:{Authorization:`Bearer ${s}`}});if(!e.ok){let n=await e.text().catch(()=>"");throw new Error(`OpenAI projects API ${e.status}: ${n.slice(0,200)}`)}let r=await e.json(),i=new Map;for(let n of r.data||[])i.set(n.id,n.name);return i}async function Pr({startMs:s,endMs:t,groupBy:e=["workspace_id"]}){let r=await ye("anthropic_billing"),i=[],n=0,o=e.map(a=>`group_by[]=${encodeURIComponent(a)}`).join("&"),c=null;for(let a=0;a<50;a++){let l=`https://api.anthropic.com/v1/organizations/cost_report?${[`starting_at=${encodeURIComponent(xr(s))}`,`ending_at=${encodeURIComponent(xr(t))}`,"bucket=1d","limit=100",o,c?`page=${encodeURIComponent(c)}`:""].filter(Boolean).join("&")}`,d=await fetch(l,{headers:{"x-api-key":r,"anthropic-version":"2023-06-01"}});if(!d.ok){let m=await d.text().catch(()=>"");throw new Error(`Anthropic cost_report ${d.status}: ${m.slice(0,200)}`)}let p=await d.json();for(let m of p.data||[]){n+=1;let f=(m.starting_at||"").slice(0,10);for(let y of m.results||[])i.push({provider:"anthropic",day:f,costUsd:Number(y.amount??y.cost??0),workspaceId:y.workspace_id||void 0,apiKeyId:y.api_key_id||void 0,model:y.model||void 0,tokensIn:y.uncached_input_tokens!=null?Number(y.uncached_input_tokens):void 0,tokensOut:y.output_tokens!=null?Number(y.output_tokens):void 0,cachedTokens:y.cached_input_tokens!=null?Number(y.cached_input_tokens):void 0})}if(!p.has_more||!p.next_page)break;c=p.next_page}return{ok:!0,items:i,rawBuckets:n}}async function Xn(){let s=await ye("anthropic_billing"),e=await fetch("https://api.anthropic.com/v1/organizations/workspaces?limit=100",{headers:{"x-api-key":s,"anthropic-version":"2023-06-01"}});if(!e.ok){let n=await e.text().catch(()=>"");throw new Error(`Anthropic workspaces ${e.status}: ${n.slice(0,200)}`)}let r=await e.json(),i=new Map;for(let n of r.data||[])i.set(n.id,n.name);return i}async function Jr({startMs:s,endMs:t}){let e=await ye("cursor_admin"),r=rt(s),i=rt(t),n=`https://api.cursor.com/teams/daily-usage-data?startDate=${r}&endDate=${i}`,o=await fetch(n,{headers:{Authorization:`Bearer ${e}`}});if(!o.ok){let l=await o.text().catch(()=>"");throw new Error(`Cursor daily-usage ${o.status}: ${l.slice(0,200)}`)}let c=await o.json(),a=[],u=0;for(let l of c.data||[]){u+=1;let d=l.date;for(let p of l.userMetrics||[]){for(let m of p.modelUsage||[]){let f=Number(m.acceptedLines??0),y=Number(m.suggestedLines??0);a.push({provider:"cursor",day:d,costUsd:Number(m.totalCents??0)/100,userEmail:p.email,model:m.model,requestCount:Number(m.requestCount??0),acceptanceRate:y>0?f/y:void 0})}(!p.modelUsage||p.modelUsage.length===0)&&a.push({provider:"cursor",day:d,costUsd:Number(p.totalCents??0)/100,userEmail:p.email})}}return{ok:!0,items:a,rawBuckets:u}}async function eo({startMs:s,endMs:t}){let[e,r,i]=await Promise.allSettled([Cr({startMs:s,endMs:t}),Pr({startMs:s,endMs:t}),Jr({startMs:s,endMs:t})]),n=l=>l.status==="fulfilled"?l.value:{ok:!1,error:l.reason?.message||String(l.reason),items:[]},o=n(e),c=n(r),a=n(i),u=[{provider:"openai",totalUsd:o.items.reduce((l,d)=>l+(d.costUsd||0),0)},{provider:"anthropic",totalUsd:c.items.reduce((l,d)=>l+(d.costUsd||0),0)},{provider:"cursor",totalUsd:a.items.reduce((l,d)=>l+(d.costUsd||0),0)}];return{openai:o,anthropic:c,cursor:a,totals:u}}function to(s,t){let e=new Map;for(let r of s){let i=t(r);if(!i)continue;let n=e.get(i)||{key:i,totalUsd:0,count:0};n.totalUsd+=r.costUsd||0,n.count+=1,e.set(i,n)}return[...e.values()].sort((r,i)=>i.totalUsd-r.totalUsd)}function ro(s){if(!s.length)return{mean:0,stddev:0};let t=s.reduce((r,i)=>r+i,0)/s.length,e=s.reduce((r,i)=>r+(i-t)**2,0)/s.length;return{mean:t,stddev:Math.sqrt(e)}}import{z as w}from"zod";var he=["ok","info","warn","critical"],so=w.object({primary:w.string().min(1).max(200).describe('Headline number or phrase (e.g. "$8,240"). Rendered in large/bold.'),delta:w.object({value:w.string().max(40).describe('Delta vs baseline (e.g. "+12% wow"). Free-form string.'),direction:w.enum(["up","down","flat"]).optional(),severity:w.enum(he).optional().describe("Color severity for the delta (warn/critical highlights regressions).")}).optional().describe("Optional comparison vs baseline. Renders inline next to primary."),summary:w.string().max(800).optional().describe('One-sentence narrative ("why this number"). Plain prose.')}),io=w.object({kind:w.literal("trend"),title:w.string().max(120).optional(),labels:w.array(w.string().max(60)).min(2).max(20).describe('Bucket labels (e.g. ["Week-3", "Week-2", "Week-1", "This wk"]).'),values:w.array(w.number()).min(2).max(20).describe("Numeric values, one per label. Must match labels.length."),highlight:w.enum(["last","max","min","none"]).default("last").optional().describe("Which bucket to visually highlight in the rendered card."),severity:w.enum(he).optional()}),no=w.object({kind:w.literal("table"),title:w.string().max(120).optional(),headers:w.array(w.string().max(40)).min(1).max(8),rows:w.array(w.array(w.union([w.string().max(200),w.number()])).min(1).max(8)).max(40).describe("2D matrix. Each inner array must have headers.length entries.")}),oo=w.object({kind:w.literal("callouts"),title:w.string().max(120).optional(),tone:w.enum(he).default("info").optional(),items:w.array(w.string().min(1).max(600)).min(1).max(10).describe("Each item renders as a bullet with a severity emoji.")}),ao=w.object({kind:w.literal("breakdown"),title:w.string().max(120).optional(),rows:w.array(w.object({label:w.string().min(1).max(80),value:w.string().min(1).max(80),sub:w.string().max(120).optional(),severity:w.enum(he).optional()})).min(1).max(20)}),co=w.object({kind:w.literal("paragraph"),title:w.string().max(120).optional(),text:w.string().min(1).max(3e3)}),lo=w.discriminatedUnion("kind",[io,no,oo,ao,co]),ot=w.object({title:w.string().min(1).max(200).describe('Card title (e.g. "Weekly AI Spend Report").'),subtitle:w.string().max(200).optional().describe('Date range or smaller header (e.g. "May 13 \u2014 May 20").'),headline:so,sections:w.array(lo).max(20).default([]).superRefine((s,t)=>{s.forEach((e,r)=>{e.kind==="trend"&&e.labels.length!==e.values.length&&t.addIssue({code:w.ZodIssueCode.custom,path:[r,"values"],message:"labels.length must equal values.length"}),e.kind==="table"&&!e.rows.every(i=>i.length===e.headers.length)&&t.addIssue({code:w.ZodIssueCode.custom,path:[r,"rows"],message:"every row must have headers.length entries"})})}),footer:w.object({viewUrl:w.string().url().optional().describe('Optional "View in Zibby" button URL.'),rerunUrl:w.string().url().optional().describe('Optional "Run again" button URL.')}).optional()}),X=Object.freeze({ok:"\u{1F7E2}",info:"\u{1F535}",warn:"\u{1F7E0}",critical:"\u{1F534}"}),Ur=Object.freeze({up:"\u2191",down:"\u2193",flat:"\u2192"}),uo=Object.freeze({ok:"green",info:"blue",warn:"orange",critical:"red"});function qr(s,t,e=12){if(!Number.isFinite(s)||!Number.isFinite(t)||t<=0)return"";let r=Math.max(0,Math.min(1,s/t)),i=Math.round(r*e);return"\u2593".repeat(i)+"\u2591".repeat(e-i)}function at(s,t){let e=String(s);return e.length>=t?e:e+" ".repeat(t-e.length)}function Mr(s,t){let e=String(s);return e.length>=t?e:" ".repeat(t-e.length)+e}function Dr({headers:s,rows:t}){let e=s.map((o,c)=>{let a=Math.max(String(o).length,...t.map(u=>String(u[c]??"").length));return Math.min(a,32)}),r=o=>o.map((c,a)=>at(c,e[a])).join(" "),i=e.map(o=>"\u2500".repeat(o)).join(" ");return"```\n"+[r(s),i,...t.map(o=>r(o))].join(`
835
+ `)+"\n```"}function po(s){let t=ot.parse(s),e=[];e.push({type:"header",text:{type:"plain_text",text:t.title.slice(0,150),emoji:!0}}),t.subtitle&&e.push({type:"context",elements:[{type:"mrkdwn",text:t.subtitle}]});let r=[`*${t.headline.primary}*`];if(t.headline.delta){let n=Ur[t.headline.delta.direction]||"",o=t.headline.delta.severity?X[t.headline.delta.severity]:"";r.push(`${n} ${t.headline.delta.value} ${o}`.trim())}let i=r.join(" ");t.headline.summary&&(i+=`
836
+ `+t.headline.summary),e.push({type:"section",text:{type:"mrkdwn",text:i}});for(let n of t.sections)switch(e.push({type:"divider"}),n.title&&e.push({type:"section",text:{type:"mrkdwn",text:`*${n.title}*`}}),n.kind){case"trend":{let o=Math.max(...n.values),c=n.labels.map((a,u)=>{let l=n.values[u],d=qr(l,o),m=(n.highlight==="last"&&u===n.labels.length-1||n.highlight==="max"&&l===o||n.highlight==="min"&&l===Math.min(...n.values))&&n.severity?` ${X[n.severity]}`:"";return`${at(a,10)} ${Mr(l.toLocaleString(),8)} ${d}${m}`});e.push({type:"section",text:{type:"mrkdwn",text:"```\n"+c.join(`
837
+ `)+"\n```"}});break}case"table":{e.push({type:"section",text:{type:"mrkdwn",text:Dr(n)}});break}case"callouts":{let o=X[n.tone||"info"];e.push({type:"section",text:{type:"mrkdwn",text:n.items.map(c=>`${o} ${c}`).join(`
829
838
  `)}});break}case"breakdown":{let o=n.rows.map(c=>({type:"mrkdwn",text:`*${c.label}*
830
839
  ${c.value}${c.sub?`
831
- _${c.sub}_`:""}${c.severity?` ${V[c.severity]}`:""}`}));for(let c=0;c<o.length;c+=10)e.push({type:"section",fields:o.slice(c,c+10)});break}case"paragraph":{e.push({type:"section",text:{type:"mrkdwn",text:n.text}});break}}if(t.footer&&(t.footer.viewUrl||t.footer.rerunUrl)){let n=[];t.footer.viewUrl&&n.push({type:"button",text:{type:"plain_text",text:"View in Zibby"},url:t.footer.viewUrl,style:"primary"}),t.footer.rerunUrl&&n.push({type:"button",text:{type:"plain_text",text:"Run again"},url:t.footer.rerunUrl}),e.push({type:"divider"}),e.push({type:"actions",elements:n})}return e}function to(s){let t=tt.parse(s),e=[],r=[`**${t.headline.primary}**`];if(t.headline.delta){let o=jr[t.headline.delta.direction]||"",c=t.headline.delta.severity?V[t.headline.delta.severity]:"";r.push(`${o} ${t.headline.delta.value} ${c}`.trim())}let i=r.join(" ");t.headline.summary&&(i+=`
832
- `+t.headline.summary),e.push({tag:"div",text:{tag:"lark_md",content:i}});for(let o of t.sections)switch(e.push({tag:"hr"}),o.title&&e.push({tag:"div",text:{tag:"lark_md",content:`**${o.title}**`}}),o.kind){case"trend":{let c=Math.max(...o.values),a=o.labels.map((u,l)=>{let p=o.values[l],d=Tr(p,c),f=(o.highlight==="last"&&l===o.labels.length-1||o.highlight==="max"&&p===c||o.highlight==="min"&&p===Math.min(...o.values))&&o.severity?` ${V[o.severity]}`:"";return`${rt(u,10)} ${Er(p.toLocaleString(),8)} ${d}${f}`});e.push({tag:"div",text:{tag:"lark_md",content:"```\n"+a.join(`
833
- `)+"\n```"}});break}case"table":{e.push({tag:"div",text:{tag:"lark_md",content:Lr(o)}});break}case"callouts":{let c=V[o.tone||"info"];e.push({tag:"div",text:{tag:"lark_md",content:o.items.map(a=>`${c} ${a}`).join(`
834
- `)}});break}case"breakdown":{let c=o.rows.map(a=>{let u=a.severity?` ${V[a.severity]}`:"",l=a.sub?` *${a.sub}*`:"";return`**${a.label}** ${a.value}${l}${u}`});e.push({tag:"div",text:{tag:"lark_md",content:c.join(`
835
- `)}});break}case"paragraph":{e.push({tag:"div",text:{tag:"lark_md",content:o.text}});break}}if(t.footer&&(t.footer.viewUrl||t.footer.rerunUrl)){let o=[];t.footer.viewUrl&&o.push({tag:"button",text:{tag:"plain_text",content:"View in Zibby"},url:t.footer.viewUrl,type:"primary"}),t.footer.rerunUrl&&o.push({tag:"button",text:{tag:"plain_text",content:"Run again"},url:t.footer.rerunUrl,type:"default"}),e.push({tag:"hr"}),e.push({tag:"action",actions:o})}let n="blue";return t.headline.delta?.severity&&(n=Xn[t.headline.delta.severity]||"blue"),{config:{wide_screen_mode:!0},header:{title:{tag:"plain_text",content:t.title.slice(0,200)},subtitle:t.subtitle?{tag:"plain_text",content:t.subtitle.slice(0,200)}:void 0,template:n},elements:e}}var Xa=Object.freeze({ok:"green_background",info:"blue_background",warn:"orange_background",critical:"red_background"}),ec=Object.freeze({ok:"\u{1F7E2}",info:"\u2139\uFE0F",warn:"\u26A0\uFE0F",critical:"\u{1F6A8}"});import{createRequire as ro}from"module";import{fileURLToPath as so}from"url";import{registerHandlers as io}from"@zibby/core/function-skill-registry.js";import{registerSkill as no}from"@zibby/agent-workflow";var oo=ro(import.meta.url);function ao(){try{return oo.resolve("@zibby/core/function-bridge.js")}catch{return null}}var co=import.meta.url;function lo(){let s=Error.prepareStackTrace;try{Error.prepareStackTrace=(r,i)=>i;let e=new Error().stack;for(let r=2;r<e.length;r++){let i=e[r].getFileName();if(i&&i!==co&&!i.startsWith("node:"))return i.startsWith("file://")?so(i):i}return null}finally{Error.prepareStackTrace=s}}function uo(s){if(!s||typeof s!="object")return{type:"object",properties:{},required:[]};let t={},e=[];for(let[r,i]of Object.entries(s))if(typeof i=="string")t[r]={type:i},e.push(r);else{let{required:n,...o}=i;t[r]=o,n!==!1&&e.push(r)}return{type:"object",properties:t,required:e}}function po(s,t,e){if(typeof e.handler!="function")throw new Error(`Skill "${s}" must have a handler function`);let r={[s]:e.handler},i=[{name:s,description:e.description||"",input_schema:uo(e.input)}];return io(s,r,i),{id:s,type:"function",serverName:s,allowedTools:[`mcp__${s}__*`],description:e.description||`Function skill: ${s}`,envKeys:[],tools:i,resolve(){let n=ao();return n?{command:"node",args:[n,t,s]}:null}}}function mo(s,t){return{id:s,type:"mcp",serverName:t.serverName||s,allowedTools:t.allowedTools||[`mcp__${t.serverName||s}__*`],description:t.description||`MCP skill: ${s}`,envKeys:t.envKeys||[],tools:t.tools||[],resolve:t.resolve,...t.cursorKey&&{cursorKey:t.cursorKey},...t.sessionEnvKey&&{sessionEnvKey:t.sessionEnvKey}}}function xr(s,t){let e;if("handler"in t){if(typeof t.handler!="function")throw new Error(`Skill "${s}" must have a handler function`);let r=lo();if(!r)throw new Error(`Could not resolve caller file for skill "${s}".`);e=po(s,r,t)}else if(typeof t.resolve=="function")e=mo(s,t);else throw new Error(`Skill "${s}" must have either a handler (function skill) or resolve (MCP skill).`);return no(e),e}var fo=xr;import{registerSkill as Lc,getSkill as xc,hasSkill as Cc,getAllSkills as Pc,listSkillIds as Jc}from"@zibby/agent-workflow";I(it);I(at);I(lt);I(ut);I(dt);I(mt);I(q);I(B);I(yt);I(be);I(gt);I(Dt);I(zt);I(bt);I(Nt);I(or);I(mr);I(Nr);I(Qe);I(Xe);I(et);I({...q,id:"slack_notify"});var Rc={BROWSER:"browser",JIRA:"jira",GITHUB:"github",GITLAB:"gitlab",LINEAR:"linear",PLANE:"plane",GIT:"git",SLACK:"slack",LARK:"lark",CHAT_NOTIFY:"chat_notify",SENTRY:"sentry",MEMORY:"memory",RUNNER:"runner",SKILL_INSTALLER:"skill-installer",CORE_TOOLS:"core-tools",CHAT_MEMORY:"chat-memory",REVIEW_MEMORY:"review-memory",WORKFLOW_BUILDER:"workflow-builder",OPENAI_BILLING:"openai_billing",ANTHROPIC_BILLING:"anthropic_billing",CURSOR_ADMIN:"cursor_admin"};export{R as INTEGRATIONS,Gr as INTEGRATION_REGISTRY,ye as REPORT_SEVERITIES,Rc as SKILLS,Xe as anthropicBillingSkill,it as browserSkill,or as chatMemorySkill,yt as chatNotifySkill,Nt as coreToolsSkill,et as cursorAdminSkill,Kn as fetchAllProviders,Ar as fetchAnthropicCosts,Bn as fetchAnthropicWorkspaces,$r as fetchCursorSpend,Rr as fetchOpenAICosts,Dn as fetchOpenAIProjects,fo as functionSkill,Pc as getAllSkills,xc as getSkill,zt as gitSkill,lt as githubSkill,ut as gitlabSkill,Gn as groupByKey,Cc as hasSkill,at as jiraSkill,B as larkSkill,dt as linearSkill,Jc as listSkillIds,Fn as meanStddev,gt as memorySkill,Qe as openaiBillingSkill,mt as planeSkill,Lc as registerSkill,tt as reportObjectSchema,eo as reportToBlockKit,to as reportToLarkCard,mr as reviewMemorySkill,Dt as runnerSkill,be as sentrySkill,xr as skill,bt as skillInstallerSkill,q as slackSkill,Dt as testRunnerSkill,Nr as workflowBuilderSkill};
840
+ _${c.sub}_`:""}${c.severity?` ${X[c.severity]}`:""}`}));for(let c=0;c<o.length;c+=10)e.push({type:"section",fields:o.slice(c,c+10)});break}case"paragraph":{e.push({type:"section",text:{type:"mrkdwn",text:n.text}});break}}if(t.footer&&(t.footer.viewUrl||t.footer.rerunUrl)){let n=[];t.footer.viewUrl&&n.push({type:"button",text:{type:"plain_text",text:"View in Zibby"},url:t.footer.viewUrl,style:"primary"}),t.footer.rerunUrl&&n.push({type:"button",text:{type:"plain_text",text:"Run again"},url:t.footer.rerunUrl}),e.push({type:"divider"}),e.push({type:"actions",elements:n})}return e}function mo(s){let t=ot.parse(s),e=[],r=[`**${t.headline.primary}**`];if(t.headline.delta){let o=Ur[t.headline.delta.direction]||"",c=t.headline.delta.severity?X[t.headline.delta.severity]:"";r.push(`${o} ${t.headline.delta.value} ${c}`.trim())}let i=r.join(" ");t.headline.summary&&(i+=`
841
+ `+t.headline.summary),e.push({tag:"div",text:{tag:"lark_md",content:i}});for(let o of t.sections)switch(e.push({tag:"hr"}),o.title&&e.push({tag:"div",text:{tag:"lark_md",content:`**${o.title}**`}}),o.kind){case"trend":{let c=Math.max(...o.values),a=o.labels.map((u,l)=>{let d=o.values[l],p=qr(d,c),f=(o.highlight==="last"&&l===o.labels.length-1||o.highlight==="max"&&d===c||o.highlight==="min"&&d===Math.min(...o.values))&&o.severity?` ${X[o.severity]}`:"";return`${at(u,10)} ${Mr(d.toLocaleString(),8)} ${p}${f}`});e.push({tag:"div",text:{tag:"lark_md",content:"```\n"+a.join(`
842
+ `)+"\n```"}});break}case"table":{e.push({tag:"div",text:{tag:"lark_md",content:Dr(o)}});break}case"callouts":{let c=X[o.tone||"info"];e.push({tag:"div",text:{tag:"lark_md",content:o.items.map(a=>`${c} ${a}`).join(`
843
+ `)}});break}case"breakdown":{let c=o.rows.map(a=>{let u=a.severity?` ${X[a.severity]}`:"",l=a.sub?` *${a.sub}*`:"";return`**${a.label}** ${a.value}${l}${u}`});e.push({tag:"div",text:{tag:"lark_md",content:c.join(`
844
+ `)}});break}case"paragraph":{e.push({tag:"div",text:{tag:"lark_md",content:o.text}});break}}if(t.footer&&(t.footer.viewUrl||t.footer.rerunUrl)){let o=[];t.footer.viewUrl&&o.push({tag:"button",text:{tag:"plain_text",content:"View in Zibby"},url:t.footer.viewUrl,type:"primary"}),t.footer.rerunUrl&&o.push({tag:"button",text:{tag:"plain_text",content:"Run again"},url:t.footer.rerunUrl,type:"default"}),e.push({tag:"hr"}),e.push({tag:"action",actions:o})}let n="blue";return t.headline.delta?.severity&&(n=uo[t.headline.delta.severity]||"blue"),{config:{wide_screen_mode:!0},header:{title:{tag:"plain_text",content:t.title.slice(0,200)},subtitle:t.subtitle?{tag:"plain_text",content:t.subtitle.slice(0,200)}:void 0,template:n},elements:e}}var uc=Object.freeze({ok:"green_background",info:"blue_background",warn:"orange_background",critical:"red_background"}),dc=Object.freeze({ok:"\u{1F7E2}",info:"\u2139\uFE0F",warn:"\u26A0\uFE0F",critical:"\u{1F6A8}"});import{createRequire as fo}from"module";import{fileURLToPath as yo}from"url";import{registerHandlers as ho}from"@zibby/core/function-skill-registry.js";import{registerSkill as go}from"@zibby/agent-workflow";var _o=fo(import.meta.url);function bo(){try{return _o.resolve("@zibby/core/function-bridge.js")}catch{return null}}var wo=import.meta.url;function ko(){let s=Error.prepareStackTrace;try{Error.prepareStackTrace=(r,i)=>i;let e=new Error().stack;for(let r=2;r<e.length;r++){let i=e[r].getFileName();if(i&&i!==wo&&!i.startsWith("node:"))return i.startsWith("file://")?yo(i):i}return null}finally{Error.prepareStackTrace=s}}function So(s){if(!s||typeof s!="object")return{type:"object",properties:{},required:[]};let t={},e=[];for(let[r,i]of Object.entries(s))if(typeof i=="string")t[r]={type:i},e.push(r);else{let{required:n,...o}=i;t[r]=o,n!==!1&&e.push(r)}return{type:"object",properties:t,required:e}}function vo(s,t,e){if(typeof e.handler!="function")throw new Error(`Skill "${s}" must have a handler function`);let r={[s]:e.handler},i=[{name:s,description:e.description||"",input_schema:So(e.input)}];return ho(s,r,i),{id:s,type:"function",serverName:s,allowedTools:[`mcp__${s}__*`],description:e.description||`Function skill: ${s}`,envKeys:[],tools:i,resolve(){let n=bo();return n?{command:"node",args:[n,t,s]}:null}}}function Io(s,t){return{id:s,type:"mcp",serverName:t.serverName||s,allowedTools:t.allowedTools||[`mcp__${t.serverName||s}__*`],description:t.description||`MCP skill: ${s}`,envKeys:t.envKeys||[],tools:t.tools||[],resolve:t.resolve,...t.cursorKey&&{cursorKey:t.cursorKey},...t.sessionEnvKey&&{sessionEnvKey:t.sessionEnvKey}}}function Br(s,t){let e;if("handler"in t){if(typeof t.handler!="function")throw new Error(`Skill "${s}" must have a handler function`);let r=ko();if(!r)throw new Error(`Could not resolve caller file for skill "${s}".`);e=vo(s,r,t)}else if(typeof t.resolve=="function")e=Io(s,t);else throw new Error(`Skill "${s}" must have either a handler (function skill) or resolve (MCP skill).`);return go(e),e}var No=Br;import{registerSkill as Gc,getSkill as Fc,hasSkill as zc,getAllSkills as Hc,listSkillIds as Wc}from"@zibby/agent-workflow";O(ct);O(dt);O(mt);O(ft);O(ht);O(gt);O(D);O(K);O(bt);O(we);O(kt);O(Ft);O(Zt);O(vt);O(At);O(ur);O(kr);O(Er);O(st);O(it);O(nt);O({...D,id:"slack_notify"});var Uc={BROWSER:"browser",JIRA:"jira",GITHUB:"github",GITLAB:"gitlab",LINEAR:"linear",PLANE:"plane",GIT:"git",SLACK:"slack",LARK:"lark",CHAT_NOTIFY:"chat_notify",SENTRY:"sentry",MEMORY:"memory",RUNNER:"runner",SKILL_INSTALLER:"skill-installer",CORE_TOOLS:"core-tools",CHAT_MEMORY:"chat-memory",REVIEW_MEMORY:"review-memory",WORKFLOW_BUILDER:"workflow-builder",OPENAI_BILLING:"openai_billing",ANTHROPIC_BILLING:"anthropic_billing",CURSOR_ADMIN:"cursor_admin"};export{A as INTEGRATIONS,Vr as INTEGRATION_REGISTRY,he as REPORT_SEVERITIES,Uc as SKILLS,it as anthropicBillingSkill,ct as browserSkill,ur as chatMemorySkill,bt as chatNotifySkill,At as coreToolsSkill,nt as cursorAdminSkill,eo as fetchAllProviders,Pr as fetchAnthropicCosts,Xn as fetchAnthropicWorkspaces,Jr as fetchCursorSpend,Cr as fetchOpenAICosts,Qn as fetchOpenAIProjects,No as functionSkill,Hc as getAllSkills,Fc as getSkill,Zt as gitSkill,mt as githubSkill,ft as gitlabSkill,to as groupByKey,zc as hasSkill,dt as jiraSkill,K as larkSkill,ht as linearSkill,Wc as listSkillIds,ro as meanStddev,kt as memorySkill,st as openaiBillingSkill,gt as planeSkill,Gc as registerSkill,ot as reportObjectSchema,po as reportToBlockKit,mo as reportToLarkCard,kr as reviewMemorySkill,Ft as runnerSkill,we as sentrySkill,Br as skill,vt as skillInstallerSkill,D as slackSkill,Ft as testRunnerSkill,Er as workflowBuilderSkill};