@dahawa/hawa-code 1.7.0 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli.js +2 -2
- package/package.json +1 -1
package/cli.js
CHANGED
|
@@ -1493,7 +1493,7 @@ NEVER SUGGEST:
|
|
|
1493
1493
|
|
|
1494
1494
|
Stay silent if the next step isn't obvious from what the user said.
|
|
1495
1495
|
Format: 2-8 words, match the user's style. Or nothing.
|
|
1496
|
-
Reply with ONLY the suggestion, no quotes or explanation.`,qmt=class{async getSuggestionPrompt(e){try{let r=[...e,In(a5i)],o=(await SN(mb(r),["You are a helpful assistant that suggests the next user input based on conversation history. keep answers short, 2 to 8 words"],1024,[],null,{model:Ld,dangerouslySkipPermissions:!1,prependCLISysprompt:!1})).message.content,i="";for(let a of o)a.type==="text"&&(i+=a.text);if(i=i.trim(),!i||i==="(no content)")return null;let s=i.trim().split(/[\s\p{P}]+/u).filter(a=>a.length>0).length;return s>20?(lt(`Suggestion too long (${s} words): "${i}"`),null):i}catch(r){return lt("Error generating suggestion:",r),null}}},eCn=new qmt;function tCn(t){Object.keys(t).forEach(
|
|
1496
|
+
Reply with ONLY the suggestion, no quotes or explanation.`,qmt=class{async getSuggestionPrompt(e){try{let r=[...e,In(a5i)],o=(await SN(mb(r),["You are a helpful assistant that suggests the next user input based on conversation history. keep answers short, 2 to 8 words"],1024,[],null,{model:Ld,dangerouslySkipPermissions:!1,prependCLISysprompt:!1})).message.content,i="";for(let a of o)a.type==="text"&&(i+=a.text);if(i=i.trim(),!i||i==="(no content)")return null;let s=i.trim().split(/[\s\p{P}]+/u).filter(a=>a.length>0).length;return s>20?(lt(`Suggestion too long (${s} words): "${i}"`),null):i}catch(r){return lt("Error generating suggestion:",r),null}}},eCn=new qmt;function tCn(t){let e={...t};return Object.keys(t).forEach(r=>{let n=t[r];if(typeof n=="string")try{let o=JSON.parse(n);typeof o=="object"&&o!==null&&(e[r]=o)}catch{}}),e}function rCn(t){if(typeof t=="string")return{type:"text",text:t};if("type"in t){if(t.type==="text"&&"text"in t)return{type:"text",text:t.text};if(t.type==="image"&&"source"in t)return{type:"image",source:t.source};if(t.type==="search_result"&&"content"in t)return{type:"search_result",content:t.content,context:t.context,title:t.title};if(t.type==="document"&&"source"in t)return{type:"document",source:t.source,context:t.context,title:t.title}}try{return{type:"text",text:JSON.stringify(t)}}catch{return{type:"text",text:t+""}}}function c5i(t){if(Array.isArray(t)&&t.length===1&&(t=t[0]),typeof t=="string")return t;let e=[];if(Array.isArray(t))for(let r of t)e.push(rCn(r));else e.push(rCn(t));return e.length===1&&typeof e[0].type=="string"?e[0].text:e}async function u5i(t,e,r){if(process.env.USER_TYPE!=="ant"||!r||!await jbn()){let i=await e();return t.abortController.signal.aborted?{message:null,shouldSkipPermissionCheck:!1}:{message:i,shouldSkipPermissionCheck:!1}}let[n,o]=await Promise.all([e(),e()]);return t.abortController.signal.aborted?{message:null,shouldSkipPermissionCheck:!1}:o.isApiErrorMessage?{message:n,shouldSkipPermissionCheck:!1}:n.isApiErrorMessage?{message:o,shouldSkipPermissionCheck:!1}:Vbn(n,o)?await r(n,o):{message:n,shouldSkipPermissionCheck:!1}}async function*Dne(t,e,r,n,o,i){Ao.onQueryStart(o.agentId),Zbn.tryStart();let s=[...t];for(;;){for await(let l of QN(s,e,r,n,o,i))yield l,s.push(l);let a=[];da.needNotification()&&a.push(await da.takeNotiMessage());let u=Ao.getSessionContext(o.agentId).takePendingPrompts();if(u.length>0){let l=u.join(`
|
|
1497
1497
|
`);a.push({type:"text",text:l})}if(a.length>0){let l=In(a);yield l,s.push(l);continue}for await(let l of Xbn.tryStop(s,e,r,n,o))yield l,s.push(l);if(s.length>0&&s[s.length-1].type==="assistant")break}o.abortController.signal.aborted||eCn.getSuggestionPrompt(s).then(a=>{a&&bl.emit(Rde,a)}),Ao.onQueryEnd(o.agentId,o.abortController.signal.aborted)}async function*QN(t,e,r,n,o,i){if(ex(),process.env.HAWA_CODE_LAZY_TOOL==="true"){if(!o.agentId){o.options._tools||(o.options._tools=o.options.tools);let M=["ToolLoader","TodoWrite","Skill"];o.options.tools=o.options._tools.filter($=>M.includes($.name)||Ao.getInQueryContext()[bA]?.has($.name))}}else o.agentId||o.options._tools&&(o.options.tools=o.options._tools,delete o.options._tools);let{messages:s,wasCompacted:a}=await Jbn(t,o);a&&(t=s),xl("session:startup",{agentId:o.agentId,messages:t.length,timestamp:Date.now(),context:r||{}});let c=t.filter(M=>M.type==="user").pop();if(c&&c.message.content){let M=c.message.content;typeof M=="string"&&Ybn(M).catch($=>{Le($)})}let{systemPrompt:u,reminders:l}=await Nun(e,r,o.agentId),d={index:-1,message:null};if(l&&l.length>0&&t.length>0)for(let M=t.length-1;M>=0;M--){let $=t[M];if($?.type==="user"){d={index:M,message:$};let V=$,O=l.map(k=>({type:"text",text:k}));t[M]={...V,message:{...V.message,content:typeof V.message.content=="string"?[...O,{type:"text",text:V.message.content}]:V.message.content[0].type==="tool_result"?[...V.message.content,...O]:[...O,...V.message.content]}};break}}let h=Ao.getSessionContext(o.agentId).takePendingPrompts();if(h.length>0&&t.length>0)for(let M=t.length-1;M>=0;M--){let $=t[M];if($?.type==="user"){let V=$,k=`
|
|
1498
1498
|
<system-reminder>
|
|
1499
1499
|
The user sent the following message:
|
|
@@ -1501,7 +1501,7 @@ ${h.join(`
|
|
|
1501
1501
|
`)}
|
|
1502
1502
|
|
|
1503
1503
|
Please address this message and continue with your tasks.
|
|
1504
|
-
</system-reminder>`;typeof V.message.content=="string"?t[M]={...V,message:{...V.message,content:V.message.content+k}}:Array.isArray(V.message.content)&&(t[M]={...V,message:{...V.message,content:[...V.message.content]}},t[M].message.content[0].content+=k);break}}function _(){return SN(mb(t),u,o.options.maxThinkingTokens,o.options.tools,o.abortController.signal,{dangerouslySkipPermissions:o.options.dangerouslySkipPermissions??!1,model:o.options.slowAndCapableModel,prependCLISysprompt:!0})}let E=await u5i(o,_,i);if(E.message===null){yield ms(Q_);return}let A=E.message;if(process.env[fz]==="true")try{A.message.content=A.message.content.map(M=>{if(M.type==="tool_use"){let $=o.options.tools.find(V=>V.name===M.name);if($.isMcp?.())return tCn(M.input),M;if($?.isFixSchema?.()){let V=$.inputSchema.safeParse(M.input);return V.success&&(M.input=V.data),M}}return M})}catch(M){lt(`Error in fixing tool input schema: ${M}`)}let C=E.shouldSkipPermissionCheck;yield A;let v=A.message.content.filter(M=>M.type==="tool_use");if(!v.length)return;let D=[];for await(let M of l5i(v,A,n,o,C))yield M,M.type==="user"&&D.push(M);if(o.abortController.signal.aborted){yield ms(RH);return}let w=D.sort((M,$)=>{let V=v.findIndex(k=>k.id===M.message.content[0].id),O=v.findIndex(k=>k.id===$.message.content[0].id);return V-O});t[d.index]=d.message,yield*await QN([...t,A,...w],e,r,n,o,i)}async function*l5i(t,e,r,n,o){let i=new zNe;for(let s of t){let a=new VNe,c=n.options.tools.find(u=>u.name===s.name);c?a.sync=!c.isReadOnly()||c.needsPermissions(s.input):a.sync=!0,a.callable=()=>d5i(s,new Set(t.map(u=>u.id)),e,r,n,o),i.addTask(a)}for await(let s of i.getResult())yield s}async function*d5i(t,e,r,n,o,i){try{let s=t.name,a=o.options.tools.find(u=>u.name===s);if(!a){ke("tengu_tool_use_error",{error:`No such tool available: ${s}`,messageID:r.message.id,toolName:s,toolUseID:t.id});let u=process.env.HAWA_CODE_LAZY_TOOL==="true"&&!o.agentId;yield In([{type:"tool_result",content:u?`Error: No such tool available: ${s}, Use the ToolLoader to load it`:`Error: No such tool available: ${s}`,is_error:!0,tool_use_id:t.id}]);return}let c=t.input;try{if(o.abortController.signal.aborted){ke("tengu_tool_use_cancelled",{toolName:a.name,toolUseID:t.id}),yield In([rpt(t.id)]);return}for await(let u of f5i(a,t.id,e,c,o,n,r,i))yield u}catch(u){let l=Gmt(u);Le(u),yield In([{type:"tool_result",content:`call tool ${t.id} error ${l} `,is_error:!0,tool_use_id:t.id}])}}catch(s){let a=Gmt(s);yield In([{type:"tool_result",content:`call tool ${t.id} error ${a} `,is_error:!0,tool_use_id:t.id}])}}function p5i(t,e){switch(t){case pi:{let{command:r,timeout:n,run_in_background:o}=pi.inputSchema.parse(e);return{command:r.replace(`cd ${Ye()} && `,""),...n?{timeout:n}:{},...o!==void 0?{run_in_background:o}:{}}}default:return e}}async function*f5i(t,e,r,n,o,i,s,a){let c=t.inputSchema.safeParse(n);if(!c.success){ke("tengu_tool_use_error",{error:`InputValidationError: ${c.error.message}`,messageID:s.message.id,toolName:t.name,toolInput:JSON.stringify(n).slice(0,200)}),yield In([{type:"tool_result",content:`InputValidationError: ${c.error.message}`,is_error:!0,tool_use_id:e}]);return}let u=p5i(t,n),l=await t.validateInput?.(u,o);if(l?.result===!1){ke("tengu_tool_use_error",{error:l?.message.slice(0,2e3),messageID:s.message.id,toolName:t.name,toolInput:JSON.stringify(n).slice(0,200),...l?.meta??{}}),yield In([{type:"tool_result",content:l.message,is_error:!0,tool_use_id:e}]);return}let d=a?{result:!0}:await i(t,u,o,s);if(d.result===!1){yield In([{type:"tool_result",content:d.message,is_error:!0,tool_use_id:e}]);return}try{let f=t.call(u,o,i);for await(let h of f){if(o.abortController.signal.aborted){yield In([rpt(e)]);return}switch(h.type){case"result":if(ke("tengu_tool_use_success",{messageID:s.message.id,toolName:t.name}),typeof h.resultForAssistant=="function"){let _=h.resultForAssistant(e);yield In(_,{data:h.data,resultForAssistant:h.resultForAssistant})}else{let _=c5i(h.resultForAssistant);yield In([{type:"tool_result",content:_,tool_use_id:e}],{data:h.data,resultForAssistant:h.resultForAssistant})}return;case"progress":ke("tengu_tool_use_progress",{messageID:s.message.id,toolName:t.name}),yield Opn(e,r,h.content,h.normalizedMessages,h.tools)}}}catch(f){lt("\u5DE5\u5177\u6267\u884C\u9519\u8BEF\uFF1A",f);let h=Gmt(f);ke("tengu_tool_use_error",{error:h.slice(0,2e3),messageID:s.message.id,toolName:t.name,toolInput:JSON.stringify(n).slice(0,1e3)}),yield In([{type:"tool_result",content:h,is_error:!0,tool_use_id:e}])}}function Gmt(t){if(!(t instanceof Error))return String(t);let e=[t.message];"stderr"in t&&typeof t.stderr=="string"&&e.push(t.stderr),"stdout"in t&&typeof t.stdout=="string"&&e.push(t.stdout);let r=e.filter(Boolean).join(`
|
|
1504
|
+
</system-reminder>`;typeof V.message.content=="string"?t[M]={...V,message:{...V.message,content:V.message.content+k}}:Array.isArray(V.message.content)&&(t[M]={...V,message:{...V.message,content:[...V.message.content]}},t[M].message.content[0].content+=k);break}}function _(){return SN(mb(t),u,o.options.maxThinkingTokens,o.options.tools,o.abortController.signal,{dangerouslySkipPermissions:o.options.dangerouslySkipPermissions??!1,model:o.options.slowAndCapableModel,prependCLISysprompt:!0})}let E=await u5i(o,_,i);if(E.message===null){yield ms(Q_);return}let A=E.message;if(process.env[fz]==="true")try{A.message.content=A.message.content.map(M=>{if(M.type==="tool_use"){let $=o.options.tools.find(V=>V.name===M.name);if($.isMcp?.())return M.input=tCn(M.input),M;if($?.isFixSchema?.()){let V=$.inputSchema.safeParse(M.input);return V.success&&(M.input=V.data),M}}return M})}catch(M){lt(`Error in fixing tool input schema: ${M}`)}let C=E.shouldSkipPermissionCheck;yield A;let v=A.message.content.filter(M=>M.type==="tool_use");if(!v.length)return;let D=[];for await(let M of l5i(v,A,n,o,C))yield M,M.type==="user"&&D.push(M);if(o.abortController.signal.aborted){yield ms(RH);return}let w=D.sort((M,$)=>{let V=v.findIndex(k=>k.id===M.message.content[0].id),O=v.findIndex(k=>k.id===$.message.content[0].id);return V-O});t[d.index]=d.message,yield*await QN([...t,A,...w],e,r,n,o,i)}async function*l5i(t,e,r,n,o){let i=new zNe;for(let s of t){let a=new VNe,c=n.options.tools.find(u=>u.name===s.name);c?a.sync=!c.isReadOnly()||c.needsPermissions(s.input):a.sync=!0,a.callable=()=>d5i(s,new Set(t.map(u=>u.id)),e,r,n,o),i.addTask(a)}for await(let s of i.getResult())yield s}async function*d5i(t,e,r,n,o,i){try{let s=t.name,a=o.options.tools.find(u=>u.name===s);if(!a){ke("tengu_tool_use_error",{error:`No such tool available: ${s}`,messageID:r.message.id,toolName:s,toolUseID:t.id});let u=process.env.HAWA_CODE_LAZY_TOOL==="true"&&!o.agentId;yield In([{type:"tool_result",content:u?`Error: No such tool available: ${s}, Use the ToolLoader to load it`:`Error: No such tool available: ${s}`,is_error:!0,tool_use_id:t.id}]);return}let c=t.input;try{if(o.abortController.signal.aborted){ke("tengu_tool_use_cancelled",{toolName:a.name,toolUseID:t.id}),yield In([rpt(t.id)]);return}for await(let u of f5i(a,t.id,e,c,o,n,r,i))yield u}catch(u){let l=Gmt(u);Le(u),yield In([{type:"tool_result",content:`call tool ${t.id} error ${l} `,is_error:!0,tool_use_id:t.id}])}}catch(s){let a=Gmt(s);yield In([{type:"tool_result",content:`call tool ${t.id} error ${a} `,is_error:!0,tool_use_id:t.id}])}}function p5i(t,e){switch(t){case pi:{let{command:r,timeout:n,run_in_background:o}=pi.inputSchema.parse(e);return{command:r.replace(`cd ${Ye()} && `,""),...n?{timeout:n}:{},...o!==void 0?{run_in_background:o}:{}}}default:return e}}async function*f5i(t,e,r,n,o,i,s,a){let c=t.inputSchema.safeParse(n);if(!c.success){ke("tengu_tool_use_error",{error:`InputValidationError: ${c.error.message}`,messageID:s.message.id,toolName:t.name,toolInput:JSON.stringify(n).slice(0,200)}),yield In([{type:"tool_result",content:`InputValidationError: ${c.error.message}`,is_error:!0,tool_use_id:e}]);return}let u=p5i(t,n),l=await t.validateInput?.(u,o);if(l?.result===!1){ke("tengu_tool_use_error",{error:l?.message.slice(0,2e3),messageID:s.message.id,toolName:t.name,toolInput:JSON.stringify(n).slice(0,200),...l?.meta??{}}),yield In([{type:"tool_result",content:l.message,is_error:!0,tool_use_id:e}]);return}let d=a?{result:!0}:await i(t,u,o,s);if(d.result===!1){yield In([{type:"tool_result",content:d.message,is_error:!0,tool_use_id:e}]);return}try{let f=t.call(u,o,i);for await(let h of f){if(o.abortController.signal.aborted){yield In([rpt(e)]);return}switch(h.type){case"result":if(ke("tengu_tool_use_success",{messageID:s.message.id,toolName:t.name}),typeof h.resultForAssistant=="function"){let _=h.resultForAssistant(e);yield In(_,{data:h.data,resultForAssistant:h.resultForAssistant})}else{let _=c5i(h.resultForAssistant);yield In([{type:"tool_result",content:_,tool_use_id:e}],{data:h.data,resultForAssistant:h.resultForAssistant})}return;case"progress":ke("tengu_tool_use_progress",{messageID:s.message.id,toolName:t.name}),yield Opn(e,r,h.content,h.normalizedMessages,h.tools)}}}catch(f){lt("\u5DE5\u5177\u6267\u884C\u9519\u8BEF\uFF1A",f);let h=Gmt(f);ke("tengu_tool_use_error",{error:h.slice(0,2e3),messageID:s.message.id,toolName:t.name,toolInput:JSON.stringify(n).slice(0,1e3)}),yield In([{type:"tool_result",content:h,is_error:!0,tool_use_id:e}])}}function Gmt(t){if(!(t instanceof Error))return String(t);let e=[t.message];"stderr"in t&&typeof t.stderr=="string"&&e.push(t.stderr),"stdout"in t&&typeof t.stdout=="string"&&e.push(t.stdout);let r=e.filter(Boolean).join(`
|
|
1505
1505
|
`);if(r.length<=1e4)return r;let n=5e3,o=r.slice(0,n),i=r.slice(-n);return`${o}
|
|
1506
1506
|
|
|
1507
1507
|
... [${r.length-1e4} characters truncated] ...
|