@zibby/core 0.1.40 → 0.1.42
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/agents/base.js +82 -81
- package/dist/framework/agents/assistant-strategy.js +5 -5
- package/dist/framework/agents/claude-strategy.js +7 -7
- package/dist/framework/agents/codex-strategy.js +6 -6
- package/dist/framework/agents/cursor-strategy.js +47 -39
- package/dist/framework/agents/gemini-strategy.js +24 -15
- package/dist/framework/agents/index.js +80 -73
- package/dist/framework/agents/utils/prompt-builder.js +31 -0
- package/dist/framework/code-generator.js +1 -138
- package/dist/framework/context-loader.js +1 -5
- package/dist/framework/graph-compiler.js +1 -143
- package/dist/framework/graph.js +85 -84
- package/dist/framework/index.js +85 -101
- package/dist/framework/node-registry.js +1 -129
- package/dist/framework/node.js +77 -72
- package/dist/framework/output-parser.js +1 -3
- package/dist/framework/skill-registry.js +1 -1
- package/dist/framework/state.js +1 -1
- package/dist/framework/tool-resolver.js +1 -1
- package/dist/index.js +123 -122
- package/dist/package.json +2 -1
- package/dist/utils/mcp-config-writer.js +4 -4
- package/package.json +2 -1
|
@@ -1,34 +1,43 @@
|
|
|
1
|
-
var M=class{constructor(t,
|
|
1
|
+
var M=class{constructor(t,e,s=0){this.name=t,this.description=e,this.priority=s}async invoke(t,e={}){throw new Error("AgentStrategy.invoke() must be implemented by subclass")}canHandle(t){throw new Error("AgentStrategy.canHandle() must be implemented by subclass")}getName(){return this.name}getDescription(){return this.description}getPriority(){return this.priority}};import{execSync as ot,spawn as lt}from"node:child_process";import{zodToJsonSchema as ut}from"zod-to-json-schema";import{existsSync as X,mkdirSync as Z,readFileSync as j,rmSync as ct,writeFileSync as Q}from"node:fs";import{join as b}from"node:path";import G from"chalk";var h={debug:0,info:1,warn:2,error:3,silent:4},K=class{constructor(){this._level=this._getLogLevel()}_getLogLevel(){if(process.env.ZIBBY_DEBUG==="true")return h.debug;if(process.env.ZIBBY_VERBOSE==="true")return h.info;let t=process.env.LOG_LEVEL?.toLowerCase();return t&&t in h?h[t]:h.info}_shouldLog(t){return h[t]>=this._level}_formatMessage(t,e,s={}){let n=new Date().toISOString(),r=`${this._getPrefix(t)} ${e}`;return Object.keys(s).length>0&&(r+=G.dim(` ${JSON.stringify(s)}`)),r}_getPrefix(t){return{debug:G.gray("[DEBUG]"),info:G.cyan("[INFO]"),warn:G.yellow("[WARN]"),error:G.red("\u274C [ERROR]")}[t]||""}debug(t,e){this._shouldLog("debug")&&console.log(this._formatMessage("debug",t,e))}info(t,e){this._shouldLog("info")&&console.log(this._formatMessage("info",t,e))}warn(t,e){this._shouldLog("warn")&&console.warn(this._formatMessage("warn",t,e))}error(t,e){this._shouldLog("error")&&console.error(this._formatMessage("error",t,e))}setLevel(t){t in h&&(this._level=h[t])}getLevel(){return Object.keys(h).find(t=>h[t]===this._level)}},u=new K;var H={ASSISTANT:"gpt-5.4-nano-2026-03-17",CLAUDE:"claude-sonnet-4-6",CURSOR:"auto",CODEX:"o4-mini",GEMINI:"gemini-2.5-pro",OPENAI_POSTPROCESSING:"gpt-4o-mini"};var W={auto:"gemini-2.5-pro","gemini-2.5-pro":"gemini-2.5-pro","gemini-2.5-flash":"gemini-2.5-flash"},gt={CURSOR_AGENT_DEFAULT:1200*1e3,OPENAI_REQUEST:18e4};import{registerSkill as Et,getSkill as q,hasSkill as Ot,getAllSkills as xt,listSkillIds as It}from"@zibby/workflow";import{zodToJsonSchema as it}from"zod-to-json-schema";var k=class{static generateFileOutputInstructions(t,e){let s;typeof t?.parse=="function"?s=it(t,{target:"openApi3"}):s=t;let n=this._buildExample(s);return`
|
|
2
2
|
\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550
|
|
3
3
|
\u{1F6A8} MANDATORY: WRITE RESULT TO FILE \u{1F6A8}
|
|
4
4
|
\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550
|
|
5
5
|
You MUST write your final result as pure JSON to this EXACT file path:
|
|
6
6
|
|
|
7
|
-
${
|
|
7
|
+
${e}
|
|
8
8
|
|
|
9
9
|
Use your file writing tool (WriteFile or ApplyPatch) to create this file.
|
|
10
10
|
DO NOT just output JSON to stdout. The file MUST exist when you finish.
|
|
11
11
|
DO NOT skip this step. The workflow WILL FAIL if the file is missing.
|
|
12
12
|
|
|
13
13
|
Required JSON structure:
|
|
14
|
-
${JSON.stringify(
|
|
14
|
+
${JSON.stringify(n,null,2)}
|
|
15
15
|
|
|
16
16
|
JSON types (strict \u2014 validators reject wrong types):
|
|
17
17
|
- Use bare JSON numbers for numeric fields (e.g. 3000). Do NOT quote them as strings (wrong: "3000").
|
|
18
18
|
- Use true/false without quotes for booleans.
|
|
19
19
|
- Use unquoted null where a field may be null.
|
|
20
20
|
|
|
21
|
-
Rules: valid JSON only, no markdown wrapping, no extra text in the file.`}static _buildExample(t){if(!t)return{};let
|
|
22
|
-
`);this.buffer=r.pop()||"";let n="";for(let i of r)if(i.trim())try{let s=JSON.parse(i);this._emitToolCalls(s);let e=this.extractText(s);if(e){if(this.rawText&&e.startsWith(this.rawText)){let o=e.substring(this.rawText.length);this.rawText=e,n+=o}else(!this.rawText.includes(e)||e.length<20)&&(this.rawText+=e,n+=e);this.tryExtractResult(this.rawText)}else this.isValidResult(s)&&(this.rawText+=`${i}
|
|
23
|
-
`,n+=`${i}
|
|
24
|
-
`,this.extractedResult=s)}catch{if(i.includes('"text"')||i.includes('"content"')){let e=i.match(/"text"\s*:\s*"([^"]*)/),o=i.match(/"content"\s*:\s*"([^"]*)/),l=e?e[1]:o?o[1]:null;l&&!this.rawText.includes(l)&&(n+=l,this.rawText+=l)}}return n||null}flush(){if(!this.buffer.trim())return null;let t="";try{let r=JSON.parse(this.buffer);this._emitToolCalls(r);let n=this.extractText(r);n&&(this.rawText+=n,t+=n,this.tryExtractResult(n))}catch{this.rawText+=this.buffer,t+=this.buffer,this.tryExtractResult(this.buffer)}return this.buffer="",t||null}_emitToolCalls(t){if(!this.onToolCall)return;let r=(e,o)=>{if(!e)return;let l=`${e}:${JSON.stringify(o??{})}`;this._lastToolEmit!==l&&(this._lastToolEmit=l,this.onToolCall(e,o??void 0))},n=e=>{if(e!=null){if(typeof e=="object"&&!Array.isArray(e))return e;if(typeof e=="string")try{return JSON.parse(e)}catch{return}}};if(t.type==="tool_use"||t.type==="tool_call"){if(t.name){r(t.name,n(t.input??t.arguments));return}let e=t.tool_call;if(e&&typeof e=="object"&&!Array.isArray(e)){let o=Object.keys(e);if(o.length===1){let l=o[0],u=e[l],m=u&&typeof u=="object"?u.args??u.input??u:void 0;r(l,n(m))}return}return}if(Array.isArray(t.tool_calls)){for(let e of t.tool_calls)r(e.name,n(e.input??e.arguments));return}let i=t.message??t;if(Array.isArray(i?.tool_calls)){for(let e of i.tool_calls)r(e.name,n(e.input??e.arguments));return}let s=i?.content??t.content;if(Array.isArray(s))for(let e of s)(e.type==="tool_use"||e.type==="tool_call")&&e.name&&r(e.name,n(e.input??e.arguments))}extractText(t){if(t.type==="assistant"&&t.message?.content){let r=t.message.content;if(Array.isArray(r))return r.filter(n=>n.type==="text"&&n.text).map(n=>n.text).join("")}return t.type==="thinking"&&t.text||t.text?t.text:t.content&&typeof t.content=="string"?t.content:t.delta?t.delta:null}tryExtractResult(t){if(!t||typeof t!="string")return;let r=[],n=/```json\s*\n?([\s\S]*?)\n?```/g,i;for(;(i=n.exec(t))!==null;){let p=i[1].trim();try{JSON.parse(p),r.push({text:p,source:"markdown"})}catch{}}let s=0,e=0;for(;s<t.length&&(s=t.indexOf("{",s),s!==-1);){let p=0,h=s;for(let d=s;d<t.length;d++)if(t[d]==="{")p++;else if(t[d]==="}"&&(p--,p===0)){h=d,r.push({text:t.substring(s,h+1),source:"brace"}),e++;break}s=h+1}let o=this.extractedResult,l=o?JSON.stringify(o).length:0,u=0,m=-1;for(let p=0;p<r.length;p++){let h=r[p];try{let d=h.text.replace(/,(\s*[}\]])/g,"$1"),O=JSON.parse(d);this.isValidResult(O)&&(u++,l=JSON.stringify(O).length,o=O,m=p)}catch{}}o&&(this.extractedResult=o)}isValidResult(t){if(!t||typeof t!="object"||Array.isArray(t)||t.session_id||t.timestamp_ms||t.type||t.call_id||t.tool_call||t.result&&typeof t.result=="object"&&(t.result.success&&typeof t.result.success=="object"||t.result.error&&typeof t.result.error=="object")||t.args&&typeof t.args=="object")return!1;if(this.zodSchema)try{return this.zodSchema.parse(t),!0}catch{return!1}return!0}getResult(){return this.extractedResult}getRawText(){return this.rawText}static extractResult(t,r=null){let n=new f;n.zodSchema=r,n.processChunk(t),n.flush();let i=n.getResult();return!i&&process.env.LOG_LEVEL==="debug"&&console.error("[StreamingParser] No result extracted from",t?.length||0,"chars"),i}};function it(f){if(!f)return null;let t=String(f),r=t.match(/```(?:json)?\s*([\s\S]*?)```/i);if(r?.[1])try{return JSON.parse(r[1].trim())}catch{}let n=t.indexOf("{");if(n<0)return null;let i=0,s=!1,e=!1,o=-1;for(let l=n;l<t.length;l++){let u=t[l];if(s){e?e=!1:u==="\\"?e=!0:u==='"'&&(s=!1);continue}if(u==='"'){s=!0;continue}if(u==="{"){i===0&&(o=l),i+=1;continue}if(u==="}"){if(i===0)continue;if(i-=1,i===0&&o>=0){let m=t.slice(o,l+1);try{return JSON.parse(m)}catch{o=-1}}}}return null}function st(f){let t=String(f||"").trim();if(!t)return null;try{return JSON.parse(t)}catch{return it(t)}}function ot(f){try{let t=JSON.parse(f);if(typeof t=="string")return t;if(typeof t?.response=="string")return t.response;if(typeof t?.text=="string")return t.text;if(typeof t?.output=="string")return t.output;if(Array.isArray(t?.candidates)&&t.candidates.length>0){let r=t.candidates[0];if(typeof r?.content=="string")return r.content;if(Array.isArray(r?.content?.parts)){let n=r.content.parts.map(i=>typeof i?.text=="string"?i.text:"").join("");if(n.trim())return n}}}catch{}return f}var X=class extends M{constructor(){super("gemini","Gemini (Google)",70)}canHandle(t){if(!!!(process.env.GEMINI_API_KEY||process.env.GOOGLE_API_KEY))return a.debug("GeminiAgentStrategy: GEMINI_API_KEY or GOOGLE_API_KEY not set"),!1;try{return tt("gemini --version",{encoding:"utf-8",timeout:5e3,stdio:"pipe"}),!0}catch{return a.warn("[Gemini] gemini CLI not found. Install: npm install -g @google/gemini-cli"),!1}}async invoke(t,r={}){let{model:n,workspace:i=process.cwd(),schema:s=null,skills:e=null,sessionPath:o=null,nodeName:l=null,timeout:u=600*1e3}=r,m=n;(!m||m==="auto")&&(m=Y.GEMINI);let p=K[m]||m,h=String(process.env.GEMINI_API_KEY||"").trim(),d=String(process.env.GOOGLE_API_KEY||"").trim(),O=this._resolveSkillsToMcp(e,{sessionPath:o,workspace:i,nodeName:l}),U=Object.keys(O).length>0,I=t,P=s&&typeof s.parse=="function",N=null;if(s){let c;try{let x=P?rt(s,{target:"openAi"}):s;c=JSON.stringify(x,null,2)}catch{c="{}"}if(U){I+=`
|
|
21
|
+
Rules: valid JSON only, no markdown wrapping, no extra text in the file.`}static _buildExample(t){if(!t)return{};let e=t.type;if(e==="object"&&t.properties){let s={};for(let[n,i]of Object.entries(t.properties))s[n]=this._buildExample(i);return s}if(e==="array"&&t.items)return[this._buildExample(t.items)];if(t.enum&&Array.isArray(t.enum)&&t.enum.length>0)return`<${t.enum.join("|")}>`;if(e==="string")return"<string>";if(e==="number"||e==="integer")return 0;if(e==="boolean")return!1;if(t.description)return`<${t.description}>`;if(t.nullable||t.oneOf||t.anyOf){let s=t.oneOf?.find(n=>n.type!=="null")||t.anyOf?.find(n=>n.type!=="null");return s?this._buildExample(s):null}return"<value>"}};var $=class f{constructor(t=""){this.userPrompt=t,this.systemInstructions=[],this.metadata={}}setUserPrompt(t){return this.userPrompt=t,this}appendUserPrompt(t){return this.userPrompt?this.userPrompt+=`
|
|
25
22
|
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
${t}`:this.userPrompt=t,this}addSkillHints(t){return t&&this.systemInstructions.push({type:"skill_hints",content:t,position:"prepend"}),this}addStructuredOutput(t,e){if(t&&e){let s=k.generateFileOutputInstructions(t,e);this.systemInstructions.push({type:"structured_output",content:s,position:"append"}),this.metadata.structuredOutputPath=e}return this}addExtraInstructions(t){if(t?.trim()){let e=`\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
|
|
24
|
+
\u26A0\uFE0F PRIORITY OVERRIDE \u2014 THE FOLLOWING INSTRUCTIONS TAKE PRECEDENCE OVER ALL PREVIOUS CONTENT
|
|
25
|
+
\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
|
|
28
26
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
27
|
+
${t.trim()}`;this.systemInstructions.push({type:"extra_instructions",content:e,position:"append"})}return this}addSkillFragments(t){return t&&t.length>0&&this.systemInstructions.push({type:"skill_fragments",content:t.join(`
|
|
28
|
+
|
|
29
|
+
`),position:"append"}),this}addSystemInstruction(t,e="custom",s="append"){return t&&this.systemInstructions.push({type:e,content:t,position:s}),this}getUserPrompt(){return this.userPrompt}getSystemInstructions(){return this.systemInstructions}getMetadata(){return this.metadata}build(){let t=this.systemInstructions.filter(n=>n.position==="prepend").map(n=>n.content),e=this.systemInstructions.filter(n=>n.position==="append").map(n=>n.content);return[...t,this.userPrompt,...e].filter(Boolean).join(`
|
|
30
|
+
|
|
31
|
+
`)}getStats(){let t=this.userPrompt.length,e=this.build().length;return{userPromptLength:t,fullPromptLength:e,systemInstructionsLength:e-t,instructionCount:this.systemInstructions.length,instructionTypes:this.systemInstructions.map(s=>s.type)}}clone(){let t=new f(this.userPrompt);return t.systemInstructions=[...this.systemInstructions],t.metadata={...this.metadata},t}};var L=class f{constructor(){this.buffer="",this.extractedResult=null,this.rawText="",this.zodSchema=null,this.lastOutputLength=0,this.onToolCall=null,this._lastToolEmit=null}processChunk(t){if(!t)return null;this.buffer+=t;let e=this.buffer.split(`
|
|
32
|
+
`);this.buffer=e.pop()||"";let s="";for(let n of e)if(n.trim())try{let i=JSON.parse(n);this._emitToolCalls(i);let r=this.extractText(i);if(r){if(this.rawText&&r.startsWith(this.rawText)){let o=r.substring(this.rawText.length);this.rawText=r,s+=o}else(!this.rawText.includes(r)||r.length<20)&&(this.rawText+=r,s+=r);this.tryExtractResult(this.rawText)}else this.isValidResult(i)&&(this.rawText+=`${n}
|
|
33
|
+
`,s+=`${n}
|
|
34
|
+
`,this.extractedResult=i)}catch{if(n.includes('"text"')||n.includes('"content"')){let r=n.match(/"text"\s*:\s*"([^"]*)/),o=n.match(/"content"\s*:\s*"([^"]*)/),l=r?r[1]:o?o[1]:null;l&&!this.rawText.includes(l)&&(s+=l,this.rawText+=l)}}return s||null}flush(){if(!this.buffer.trim())return null;let t="";try{let e=JSON.parse(this.buffer);this._emitToolCalls(e);let s=this.extractText(e);s&&(this.rawText+=s,t+=s,this.tryExtractResult(s))}catch{this.rawText+=this.buffer,t+=this.buffer,this.tryExtractResult(this.buffer)}return this.buffer="",t||null}_emitToolCalls(t){if(!this.onToolCall)return;let e=(r,o)=>{if(!r)return;let l=`${r}:${JSON.stringify(o??{})}`;this._lastToolEmit!==l&&(this._lastToolEmit=l,this.onToolCall(r,o??void 0))},s=r=>{if(r!=null){if(typeof r=="object"&&!Array.isArray(r))return r;if(typeof r=="string")try{return JSON.parse(r)}catch{return}}};if(t.type==="tool_use"||t.type==="tool_call"){if(t.name){e(t.name,s(t.input??t.arguments));return}let r=t.tool_call;if(r&&typeof r=="object"&&!Array.isArray(r)){let o=Object.keys(r);if(o.length===1){let l=o[0],c=r[l],m=c&&typeof c=="object"?c.args??c.input??c:void 0;e(l,s(m))}return}return}if(Array.isArray(t.tool_calls)){for(let r of t.tool_calls)e(r.name,s(r.input??r.arguments));return}let n=t.message??t;if(Array.isArray(n?.tool_calls)){for(let r of n.tool_calls)e(r.name,s(r.input??r.arguments));return}let i=n?.content??t.content;if(Array.isArray(i))for(let r of i)(r.type==="tool_use"||r.type==="tool_call")&&r.name&&e(r.name,s(r.input??r.arguments))}extractText(t){if(t.type==="assistant"&&t.message?.content){let e=t.message.content;if(Array.isArray(e))return e.filter(s=>s.type==="text"&&s.text).map(s=>s.text).join("")}return t.type==="thinking"&&t.text||t.text?t.text:t.content&&typeof t.content=="string"?t.content:t.delta?t.delta:null}tryExtractResult(t){if(!t||typeof t!="string")return;let e=[],s=/```json\s*\n?([\s\S]*?)\n?```/g,n;for(;(n=s.exec(t))!==null;){let p=n[1].trim();try{JSON.parse(p),e.push({text:p,source:"markdown"})}catch{}}let i=0,r=0;for(;i<t.length&&(i=t.indexOf("{",i),i!==-1);){let p=0,g=i;for(let d=i;d<t.length;d++)if(t[d]==="{")p++;else if(t[d]==="}"&&(p--,p===0)){g=d,e.push({text:t.substring(i,g+1),source:"brace"}),r++;break}i=g+1}let o=this.extractedResult,l=o?JSON.stringify(o).length:0,c=0,m=-1;for(let p=0;p<e.length;p++){let g=e[p];try{let d=g.text.replace(/,(\s*[}\]])/g,"$1"),O=JSON.parse(d);this.isValidResult(O)&&(c++,l=JSON.stringify(O).length,o=O,m=p)}catch{}}o&&(this.extractedResult=o)}isValidResult(t){if(!t||typeof t!="object"||Array.isArray(t)||t.session_id||t.timestamp_ms||t.type||t.call_id||t.tool_call||t.result&&typeof t.result=="object"&&(t.result.success&&typeof t.result.success=="object"||t.result.error&&typeof t.result.error=="object")||t.args&&typeof t.args=="object")return!1;if(this.zodSchema)try{return this.zodSchema.parse(t),!0}catch{return!1}return!0}getResult(){return this.extractedResult}getRawText(){return this.rawText}static extractResult(t,e=null){let s=new f;s.zodSchema=e,s.processChunk(t),s.flush();let n=s.getResult();return!n&&process.env.LOG_LEVEL==="debug"&&console.error("[StreamingParser] No result extracted from",t?.length||0,"chars"),n}};function at(f){if(!f)return null;let t=String(f),e=t.match(/```(?:json)?\s*([\s\S]*?)```/i);if(e?.[1])try{return JSON.parse(e[1].trim())}catch{}let s=t.indexOf("{");if(s<0)return null;let n=0,i=!1,r=!1,o=-1;for(let l=s;l<t.length;l++){let c=t[l];if(i){r?r=!1:c==="\\"?r=!0:c==='"'&&(i=!1);continue}if(c==='"'){i=!0;continue}if(c==="{"){n===0&&(o=l),n+=1;continue}if(c==="}"){if(n===0)continue;if(n-=1,n===0&&o>=0){let m=t.slice(o,l+1);try{return JSON.parse(m)}catch{o=-1}}}}return null}function pt(f){let t=String(f||"").trim();if(!t)return null;try{return JSON.parse(t)}catch{return at(t)}}function ft(f){try{let t=JSON.parse(f);if(typeof t=="string")return t;if(typeof t?.response=="string")return t.response;if(typeof t?.text=="string")return t.text;if(typeof t?.output=="string")return t.output;if(Array.isArray(t?.candidates)&&t.candidates.length>0){let e=t.candidates[0];if(typeof e?.content=="string")return e.content;if(Array.isArray(e?.content?.parts)){let s=e.content.parts.map(n=>typeof n?.text=="string"?n.text:"").join("");if(s.trim())return s}}}catch{}return f}var tt=class extends M{constructor(){super("gemini","Gemini (Google)",70)}canHandle(t){if(!!!(process.env.GEMINI_API_KEY||process.env.GOOGLE_API_KEY))return u.debug("GeminiAgentStrategy: GEMINI_API_KEY or GOOGLE_API_KEY not set"),!1;try{return ot("gemini --version",{encoding:"utf-8",timeout:5e3,stdio:"pipe"}),!0}catch{return u.warn("[Gemini] gemini CLI not found. Install: npm install -g @google/gemini-cli"),!1}}async invoke(t,e={}){let{model:s,workspace:n=process.cwd(),schema:i=null,skills:r=null,sessionPath:o=null,nodeName:l=null,timeout:c=600*1e3}=e,m=s;(!m||m==="auto")&&(m=H.GEMINI);let p=W[m]||m,g=String(process.env.GEMINI_API_KEY||"").trim(),d=String(process.env.GOOGLE_API_KEY||"").trim(),O=this._resolveSkillsToMcp(r,{sessionPath:o,workspace:n,nodeName:l}),Y=Object.keys(O).length>0,x=new $(t),C=i&&typeof i.parse=="function",N=null;if(i){let a;try{let S=C?ut(i,{target:"openAi"}):i;a=JSON.stringify(S,null,2)}catch{a="{}"}if(Y){x.addSystemInstruction(`Write valid JSON that matches this schema:
|
|
35
|
+
${a}`,"schema_instruction","append");let S=`zibby-result-${Date.now()}.json`,_=b(n,".zibby","tmp");N=b(_,S),Z(_,{recursive:!0}),x.addStructuredOutput(i,N)}else x.addSystemInstruction(`Return ONLY valid JSON (no markdown, no commentary) that matches this schema:
|
|
36
|
+
${a}`,"json_instruction","append")}let et=x.build(),rt=x.getUserPrompt(),P=x.getStats(),z=String(process.env.GEMINI_API_KEY||process.env.GOOGLE_API_KEY||"").trim(),st=z?` | key: ***${z.slice(-4)}`:" | key: not set";console.log(`
|
|
37
|
+
\u25C6 Model: ${p||"auto"}${st}
|
|
38
|
+
`);let R=(await import("chalk")).default;console.log(`
|
|
39
|
+
${R.bold("Prompt sent to LLM:")}`),console.log(R.dim("\u2500".repeat(60))),console.log(R.dim(rt)),console.log(R.dim("\u2500".repeat(60)));let J=this._createGeminiConfigDir(n,O),I=["--output-format","json"];p&&p!=="auto"&&I.push("--model",p);let D=Object.keys(O);if(D.length>0){I.push("--approval-mode","yolo");for(let a of D)I.push("--allowed-mcp-server-names",a);u.info(`[Gemini] Enabling MCP servers: ${D.join(", ")}`)}else r&&r.length>0&&u.warn(`[Gemini] Skills requested but no MCP servers configured: ${r.join(", ")}`);I.push("-p",et);let v={...process.env,GEMINI_CLI_HOME:J};g?(v.GEMINI_API_KEY=g,delete v.GOOGLE_API_KEY):d&&(v.GOOGLE_API_KEY=d,delete v.GEMINI_API_KEY),u.debug(`[Gemini] Command: gemini ${I.slice(0,8).join(" ")}... (${I.length} total args)`),u.debug(`[Gemini] User prompt: ${P.userPromptLength} chars, System instructions: ${P.systemInstructionsLength} chars (${P.instructionCount} blocks), Full: ${P.fullPromptLength} chars`),u.debug(`[Gemini] Config home: ${J}`),u.debug(`[Gemini] GEMINI_CLI_HOME env: ${v.GEMINI_CLI_HOME}`);let U="",A=null;try{U=await new Promise((S,_)=>{let T=lt("gemini",I,{cwd:n,env:v,stdio:["ignore","pipe","pipe"]}),F="",V="",B=setTimeout(()=>{try{T.kill("SIGTERM")}catch{}},c);T.stdout.on("data",E=>{F+=E.toString()}),T.stderr.on("data",E=>{V+=E.toString()}),T.on("error",E=>{clearTimeout(B),_(E)}),T.on("close",E=>{if(clearTimeout(B),E===0)return S(F.trim());_(new Error(`gemini failed with code ${E}: ${(V||F).trim()}`))})})}catch(a){A=a}finally{try{ct(J,{recursive:!0,force:!0})}catch{}}let y=ft(U).trim();if(!i){if(A)throw A;return y}if(N){let a=X(N);if(u.info(`[Gemini] Result file: ${a?"present":"missing"} at ${N}`),a)try{let S=j(N,"utf-8").trim(),_=JSON.parse(S),T=C?i.parse(_):_;return u.info("[Gemini] Structured output recovered from result file"),{raw:y,structured:T}}catch(S){u.warn(`[Gemini] Result file parse/validation failed: ${S.message}`)}else A||u.warn("[Gemini] Result file missing; falling back to stream-parsed JSON")}let w=null;if(i){let a=new L;a.zodSchema=i,a.processChunk(y),a.flush(),w=a.getResult()}if(u.info(`[Gemini] Raw stdout length: ${U.length} chars`),u.info(`[Gemini] Extracted text length: ${y.length} chars`),u.info(`[Gemini] StreamParser result: ${w?"extracted":"null"}`),w||(y.length<2e3?u.info(`[Gemini] Raw text preview:
|
|
40
|
+
${y}`):u.info(`[Gemini] Raw text preview (first 1000 chars):
|
|
41
|
+
${y.slice(0,1e3)}`),w=pt(y)),!w)throw A||(u.error("[Gemini] Failed to extract valid JSON from output"),u.error("\u{1F4A1} Tip: Set strictMode=true in .zibby.config.js for OpenAI proxy fallback"),new Error("Gemini did not return valid JSON for structured output. Enable strictMode for proxy fallback."));let nt=C?i.parse(w):w;return{raw:y,structured:nt}}_resolveSkillsToMcp(t,e={}){if(!Array.isArray(t)||t.length===0)return{};let s={};for(let n of t){let i=q(n);if(!i||typeof i.resolve!="function")continue;let r=i.resolve(e);if(!r)continue;let o=i.cursorKey||i.serverName||n,l={command:r.command};r.args?.length&&(l.args=r.args),r.env&&Object.keys(r.env).length>0&&(l.env=r.env),r.cwd&&(l.cwd=r.cwd),s[o]=l}return s}_createGeminiConfigDir(t,e){let s=`${Date.now()}-${Math.random().toString(16).slice(2,10)}`,n=b(t||process.cwd(),".zibby","tmp",`gemini-home-${s}`),i=b(n,".gemini");Z(i,{recursive:!0});let r=b(i,"settings.json"),o={},l=b(process.env.HOME||"",".gemini","settings.json");if(X(l))try{o=JSON.parse(j(l,"utf-8"))}catch{o={}}let c={...o,mcpServers:{...o.mcpServers&&typeof o.mcpServers=="object"?o.mcpServers:{},...e||{}}};Q(r,`${JSON.stringify(c,null,2)}
|
|
42
|
+
`,"utf-8");let m=b(t||process.cwd(),".zibby","tmp","gemini-settings-debug.json");try{Q(m,`${JSON.stringify(c,null,2)}
|
|
43
|
+
`,"utf-8")}catch{}return u.debug(`[Gemini] Created isolated config with ${Object.keys(c.mcpServers||{}).length} MCP servers`),u.debug(`[Gemini] MCP servers: ${JSON.stringify(Object.keys(c.mcpServers||{}),null,2)}`),n}};export{tt as GeminiAgentStrategy};
|