import{o as e}from"./home-dir-C44RaPME-B5iLWwCq.mjs";import{createRequire as t}from"node:module";const n=Object.freeze({allowH2:!1});function r(e){return typeof e==`object`&&!!e&&typeof e.Agent==`function`&&typeof e.EnvHttpProxyAgent==`function`&&typeof e.ProxyAgent==`function`&&typeof e.fetch==`function`}function i(){let e=globalThis.__OPENCLAW_TEST_UNDICI_RUNTIME_DEPS__;if(r(e))return e;let n=t(import.meta.url)(`undici`);return{Agent:n.Agent,EnvHttpProxyAgent:n.EnvHttpProxyAgent,FormData:n.FormData,ProxyAgent:n.ProxyAgent,fetch:n.fetch}}function a(e){return e?{...e,...n}:{...n}}function o(e){let{Agent:t}=i();return new t(a(e))}function s(e){let{EnvHttpProxyAgent:t}=i();return new t(a(e))}function c(e){let{ProxyAgent:t}=i();return typeof e==`string`||e instanceof URL?new t(a({uri:e.toString()})):new t(a(e))}function l(e){return e===`commentary`||e===`final_answer`?e:void 0}function u(e){if(typeof e!=`string`||e.trim().length===0)return null;if(!e.startsWith(`{`))return{id:e};try{let t=JSON.parse(e);return t.v===1?{...typeof t.id==`string`?{id:t.id}:{},...l(t.phase)?{phase:l(t.phase)}:{}}:null}catch{return null}}function d(e,t){if(!e||typeof e!=`object`)return;let n=e,r=l(n.phase),i=t?.phase,a=e=>i?e===i:e===void 0,o=t?.sanitizeText,s=t?.joinWith??`
`,c=e=>o?o(e):e,d=e=>e.trim()||void 0;if(typeof n.text==`string`)return a(r)?d(c(n.text)):void 0;if(typeof n.content==`string`)return a(r)?d(c(n.content)):void 0;if(!Array.isArray(n.content))return;let f=n.content.some(e=>{if(!e||typeof e!=`object`)return!1;let t=e;return t.type===`text`?!!u(t.textSignature)?.phase:!1});if(!i&&f)return;let p=n.content.map(e=>{if(!e||typeof e!=`object`)return null;let t=e;if(t.type!==`text`||typeof t.text!=`string`||!a(u(t.textSignature)?.phase??(f?void 0:r)))return null;let n=c(t.text);return n.trim()?n:null}).filter(e=>typeof e==`string`);if(p.length!==0)return d(p.join(s))}function f(e){let t=[];for(let n of e.matchAll(/(^|\n)(```|~~~)[^\n]*\n[\s\S]*?(?:\n\2|$)/g)){let e=(n.index??0)+n[1].length;t.push({start:e,end:e+n[0].length-n[1].length})}for(let n of e.matchAll(/`+[^`]+`+/g)){let e=n.index??0,r=e+n[0].length;t.some(t=>e>=t.start&&r<=t.end)||t.push({start:e,end:r})}return t.sort((e,t)=>e.start-t.start),t}function p(e,t){return t.some(t=>e>=t.start&&e<t.end)}const m=/<[|｜][^|｜]*[|｜]>/g;function h(e,t,n){return n.some(n=>e<n.end&&t>n.start)}function g(e,t){return!!(e&&t&&!/\s/.test(e)&&!/\s/.test(t))}function _(e){if(!e||(m.lastIndex=0,!m.test(e)))return e;m.lastIndex=0;let t=f(e),n=``,r=0;for(let i of e.matchAll(m)){let a=i[0],o=i.index??0,s=o+a.length;n+=e.slice(r,o),p(o,t)||h(o,s,t)?n+=a:g(e[o-1],e[s])&&(n+=` `),r=s}return n+=e.slice(r),n}const v=/<\s*\/?\s*(?:(?:antml:)?(?:think(?:ing)?|thought)|antthinking|final)\b/i,y=/<\s*\/?\s*final\b[^<>]*>/gi,b=/<\s*(\/?)\s*(?:(?:antml:)?(?:think(?:ing)?|thought)|antthinking)\b[^<>]*>/gi;function x(e,t){return t===`none`?e:t===`start`?e.trimStart():e.trim()}function S(e,t){if(!e||!v.test(e))return e;let n=t?.mode??`strict`,r=t?.trim??`both`,i=e;if(y.test(i)){y.lastIndex=0;let e=[],t=f(i);for(let n of i.matchAll(y)){let r=n.index??0;e.push({start:r,length:n[0].length,inCode:p(r,t)})}for(let t=e.length-1;t>=0;t--){let n=e[t];n.inCode||(i=i.slice(0,n.start)+i.slice(n.start+n.length))}}else y.lastIndex=0;let a=f(i);b.lastIndex=0;let o=``,s=0,c=!1;for(let e of i.matchAll(b)){let t=e.index??0,n=e[1]===`/`;p(t,a)||(c?n&&(c=!1):(o+=i.slice(s,t),n||(c=!0)),s=t+e[0].length)}return(!c||n===`preserve`)&&(o+=i.slice(s)),x(o,r)}const C=/<\s*(\/?)\s*relevant[-_]memories\b[^<>]*>/gi,w=/<\s*\/?\s*relevant[-_]memories\b/i,T=/<\s*\/?\s*(?:tool_call|tool_result|function_calls?|function|tool_calls)\b/i,E=new Set([`tool_call`,`tool_result`,`function_call`,`function_calls`,`function`,`tool_calls`]),D=/^(?:\s+[A-Za-z_:][-A-Za-z0-9_:.]*\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"'=<>`]+))*\s*(?:\r?\n\s*)?[[{]/,O=/^\s*(?:\r?\n\s*)?<(?:function|invoke|parameters?|arguments?)\b/i;function k(e,t,n){let r=null,i=!1;for(let a=t;a<n;a+=1){let t=e[a];if(r===null){(t===`"`||t===`'`)&&(r=t);continue}if(i){i=!1;continue}if(t===`\\`){i=!0;continue}t===r&&(r=null)}return r!==null}function A(e){return!e||/\s/.test(e)||e===`/`||e===`>`}function j(e,t){let n=null,r=!1;for(let i=t;i<e.length;i+=1){let t=e[i];if(n!==null){if(r){r=!1;continue}if(t===`\\`){r=!0;continue}t===n&&(n=null);continue}if(t===`"`||t===`'`){n=t;continue}if(t===`<`)return-1;if(t===`>`)return i}return-1}function M(e,t){let n=e.slice(t);return D.test(n)?`json`:O.test(n)?`xml`:null}function N(e,t,n){if(n.tagName!==`function`||n.isClose||n.isSelfClosing||n.isTruncated||!/\bname\s*=/.test(e.slice(n.contentStart,n.end)))return!1;let r=t-1;for(;r>=0&&(e[r]===` `||e[r]===`	`);)--r;return r<0||e[r]===`
`||e[r]===`\r`||/[.!?:]/.test(e[r])}function P(t,n){if(t[n]!==`<`)return null;let r=n+1;for(;r<t.length&&/\s/.test(t[r]);)r+=1;let i=!1;if(t[r]===`/`)for(i=!0,r+=1;r<t.length&&/\s/.test(t[r]);)r+=1;let a=r;for(;r<t.length&&/[A-Za-z_]/.test(t[r]);)r+=1;let o=e(t.slice(a,r));if(!E.has(o)||!A(t[r]))return null;let s=r,c=j(t,r);return c===-1?{contentStart:s,end:t.length,isClose:i,isSelfClosing:!1,tagName:o,isTruncated:!0}:{contentStart:s,end:c+1,isClose:i,isSelfClosing:!i&&/\/\s*$/.test(t.slice(r,c)),tagName:o,isTruncated:!1}}function F(e){if(!e||!T.test(e))return e;let t=f(e),n=``,r=0,i=!1,a=0,o=!1,s=0,c=null,l=new Map;for(let u=0;u<e.length;u+=1){if(e[u]!==`<`||!i&&p(u,t))continue;let d=P(e,u);if(d){if(i)d.isClose&&(d.tagName===c||c===`tool_result`&&d.tagName===`tool_call`)&&(!o||!k(e,a,u))&&(i=!1,o=!1,c=null);else{if(n+=e.slice(r,u),d.isClose){if(d.isTruncated){let t=d.contentStart;n+=e.slice(u,t),r=t,u=Math.max(u,t-1);continue}let t=l.get(d.tagName)??0;t>0&&(n+=e.slice(u,d.end),l.set(d.tagName,t-1)),r=d.end,u=Math.max(u,d.end-1);continue}if(d.isSelfClosing){r=d.end,u=Math.max(u,d.end-1);continue}let t=d.isTruncated?d.contentStart:d.end,f=d.tagName===`tool_call`||d.tagName===`function`?M(e,t):D.test(e.slice(t))?`json`:null,p=d.tagName!==`function`||N(e,u,d);if(!d.isClose&&f&&p){if(i=!0,a=d.end,o=f===`json`,s=u,c=d.tagName,d.isTruncated){r=e.length;break}}else{let t=d.isTruncated?d.contentStart:d.end;n+=e.slice(u,t),d.isTruncated||l.set(d.tagName,(l.get(d.tagName)??0)+1),r=t,u=Math.max(u,t-1);continue}}r=d.end,u=Math.max(u,d.end-1)}}return i?c===`function`&&(n+=e.slice(s)):n+=e.slice(r),n}function I(e){if(!e||!/minimax:tool_call/i.test(e))return e;let t=e.replace(/<invoke\b[^>]*>[\s\S]*?<\/invoke>/gi,``);return t=t.replace(/<\/?minimax:tool_call>/gi,``),t}function L(t){if(!t||!/\[Tool (?:Call|Result)/i.test(t)&&!/\[Historical context/i.test(t))return t;let n=(e,t,n)=>{let{allowLeadingNewlines:r=!1}=n??{},i=t;for(;i<e.length;){let t=e[i];if(t===` `||t===`	`){i+=1;continue}if(r&&(t===`
`||t===`\r`)){i+=1;continue}break}if(i>=e.length)return null;let a=e[i];if(a===`{`||a===`[`){let t=0,n=!1,r=!1;for(let a=i;a<e.length;a+=1){let i=e[a];if(n){r?r=!1:i===`\\`?r=!0:i===`"`&&(n=!1);continue}if(i===`"`){n=!0;continue}if(i===`{`||i===`[`)t+=1;else if((i===`}`||i===`]`)&&(--t,t===0))return a+1}return null}if(a===`"`){let t=!1;for(let n=i+1;n<e.length;n+=1){let r=e[n];if(t){t=!1;continue}if(r===`\\`){t=!0;continue}if(r===`"`)return n+1}return null}let o=i;for(;o<e.length&&e[o]!==`
`&&e[o]!==`\r`;)o+=1;return o},r=(t=>{let r=/\[Tool Call:[^\]]*\]/gi,i=``,a=0;for(let o of t.matchAll(r)){let r=o.index??0;if(r<a)continue;i+=t.slice(a,r);let s=r+o[0].length;for(;s<t.length&&(t[s]===` `||t[s]===`	`);)s+=1;for(t[s]===`\r`&&(s+=1),t[s]===`
`&&(s+=1);s<t.length&&(t[s]===` `||t[s]===`	`);)s+=1;if(e(t.slice(s,s+9))===`arguments`){s+=9,t[s]===`:`&&(s+=1),t[s]===` `&&(s+=1);let e=n(t,s,{allowLeadingNewlines:!0});e!==null&&(s=e)}(t[s]===`
`||t[s]===`\r`)&&(i.endsWith(`
`)||i.endsWith(`\r`)||i.length===0)&&(t[s]===`\r`&&(s+=1),t[s]===`
`&&(s+=1)),a=s}return i+=t.slice(a),i})(t);return r=r.replace(/\[Tool Result for ID[^\]]*\]\n?[\s\S]*?(?=\n*\[Tool |\n*$)/gi,``),r=r.replace(/\[Historical context:[^\]]*\]\n?/gi,``),r.trim()}function R(e){if(!e||!w.test(e))return e;C.lastIndex=0;let t=f(e),n=``,r=0,i=!1;for(let a of e.matchAll(C)){let o=a.index??0;if(p(o,t))continue;let s=a[1]===`/`;i?s&&(i=!1):(n+=e.slice(r,o),s||(i=!0)),r=o+a[0].length}return i||(n+=e.slice(r)),n}const z={delivery:{finalTrim:`both`,reasoningMode:`strict`,reasoningTrim:`both`,stageOrder:`reasoning-last`},history:{finalTrim:`none`,reasoningMode:`strict`,reasoningTrim:`none`,stageOrder:`reasoning-last`},"internal-scaffolding":{finalTrim:`start`,preserveDowngradedToolText:!0,preserveMinimaxToolXml:!0,reasoningMode:`preserve`,reasoningTrim:`start`,stageOrder:`reasoning-first`}};function B(e,t){if(!e)return e;let n=e=>S(e,{mode:t.reasoningMode,trim:t.reasoningTrim}),r=e=>t.finalTrim===`none`?e:t.finalTrim===`start`?e.trimStart():e.trim(),i=e=>{let n=e;return t.preserveMinimaxToolXml||(n=I(n)),n=_(n),n=R(n),n=F(n),t.preserveDowngradedToolText||(n=L(n)),n};return t.stageOrder===`reasoning-first`?r(i(n(e))):r(n(i(e)))}function V(e,t=`delivery`){return B(e,z[t])}function H(e){return V(e,`delivery`)}export{s as a,o as i,V as n,c as o,d as r,i as s,H as t};