import{$c as e,al as t,cl as n,il as r,ll as i,sl as a,tl as o,ul as s}from"./model-selection-p21GBPS6.js";import{b as c}from"./env-overrides-C0vMeCeX.js";import{P as l}from"./plugins-1aNlpDX7.js";import u from"node:crypto";function d(e){return typeof e==`string`&&e.trim()||null}function f(e){return Array.isArray(e)?l(e):[]}function p(e){if(e===void 0)return;if(!e||typeof e!=`object`||Array.isArray(e))return null;let t=e,n=typeof t.argvIndex==`number`&&Number.isInteger(t.argvIndex)&&t.argvIndex>=0?t.argvIndex:null,r=d(t.path),i=d(t.sha256);return n===null||!r||!i?null:{argvIndex:n,path:r,sha256:i}}function m(e){if(!e||typeof e!=`object`||Array.isArray(e))return null;let t=e,n=f(t.argv);if(n.length===0)return null;let r=p(t.mutableFileOperand);if(t.mutableFileOperand!==void 0&&r===null)return null;let i=d(t.commandText)??d(t.rawCommand);return i?{argv:n,cwd:d(t.cwd),commandText:i,commandPreview:d(t.commandPreview),agentId:d(t.agentId),sessionKey:d(t.sessionKey),mutableFileOperand:r??void 0}:null}function h(e){if(!e||typeof e!=`object`||Array.isArray(e))return[];let t=[];for(let[n,r]of Object.entries(e)){if(typeof r!=`string`)continue;let e=c(n,{portable:!0});e&&t.push([e,r])}return t.sort((e,t)=>e[0].localeCompare(t[0])),t}function g(e){return e.length===0?null:u.createHash(`sha256`).update(JSON.stringify(e)).digest(`hex`)}function _(e){let t=h(e);return{envHash:g(t),envKeys:t.map(([e])=>e)}}function v(e){let t=_(e.env);return{binding:{argv:f(e.argv),cwd:d(e.cwd),agentId:d(e.agentId),sessionKey:d(e.sessionKey),envHash:t.envHash},envKeys:t.envKeys}}function y(e,t){if(e.length===0||e.length!==t.length)return!1;for(let n=0;n<e.length;n+=1)if(e[n]!==t[n])return!1;return!0}function b(e){return{ok:!1,code:`APPROVAL_REQUEST_MISMATCH`,message:`approval id does not match request`,details:e}}function x(e){return!e.expectedEnvHash&&!e.actualEnvHash?{ok:!0}:!e.expectedEnvHash&&e.actualEnvHash?{ok:!1,code:`APPROVAL_ENV_BINDING_MISSING`,message:`approval id missing env binding for requested env overrides`,details:{envKeys:e.actualEnvKeys}}:e.expectedEnvHash===e.actualEnvHash?{ok:!0}:{ok:!1,code:`APPROVAL_ENV_MISMATCH`,message:`approval id env binding mismatch`,details:{envKeys:e.actualEnvKeys,expectedEnvHash:e.expectedEnvHash,actualEnvHash:e.actualEnvHash}}}function S(e){return!y(e.expected.argv,e.actual.argv)||e.expected.cwd!==e.actual.cwd||e.expected.agentId!==e.actual.agentId||e.expected.sessionKey!==e.actual.sessionKey?b():x({expectedEnvHash:e.expected.envHash,actualEnvHash:e.actual.envHash,actualEnvKeys:e.actualEnvKeys})}function C(e){return b({envKeys:e.actualEnvKeys})}function w(e){let t={code:e.match.code,runId:e.runId};return e.match.details&&Object.assign(t,e.match.details),{ok:!1,message:e.match.message,details:t}}function T(e){return e.map(e=>e.length===0?`""`:/\s|"/.test(e)?`"${e.replace(/"/g,`\\"`)}"`:e).join(` `)}const E=new Set([`ash`,`bash`,`dash`,`fish`,`ksh`,`powershell`,`pwsh`,`sh`,`zsh`]);function D(e){let n=t(e),r=a(n);return r.kind===`unwrapped`?r.argv:n}function O(e){let t=D(e),a=t[0]?.trim();if(!a)return!1;let o=r(a);if(!E.has(o))return!1;let c=o===`powershell`||o===`pwsh`?s(t,i).valueTokenIndex:s(t,n,{allowCombinedC:!0}).valueTokenIndex;return c===null?!1:t.slice(c+1).some(e=>e.trim().length>0)}function k(t){let n=e(t),r=n.command,i=O(t),a=n.isWrapper&&o(t);return{shellPayload:r,commandText:T(t),previewText:r!==null&&!a&&!i?r.trim():null}}function A(e){return typeof e==`string`&&e.trim().length>0?e.trim():null}function j(e){let t=A(e.rawCommand),n=k(e.argv);if(t){let r=t===n.commandText,i=e.allowLegacyShellText===!0&&n.previewText!==null&&t===n.previewText;if(!r&&!i)return{ok:!1,message:`INVALID_REQUEST: rawCommand does not match command`,details:{code:`RAW_COMMAND_MISMATCH`,rawCommand:t,inferred:n.commandText,formattedArgv:n.commandText}}}return{ok:!0,shellPayload:n.shellPayload,commandText:n.commandText,previewText:n.previewText}}function M(e){let t=A(e.rawCommand),n=Array.isArray(e.command)?e.command:[];if(n.length===0)return t?{ok:!1,message:`rawCommand requires params.command`,details:{code:`MISSING_COMMAND`}}:{ok:!0,argv:[],commandText:``,shellPayload:null,previewText:null};let r=n.map(e=>String(e)),i=j({argv:r,rawCommand:t,allowLegacyShellText:!0});return i.ok?{ok:!0,argv:r,commandText:i.commandText,shellPayload:i.shellPayload,previewText:i.previewText}:{ok:!1,message:i.message,details:i.details??{code:`RAW_COMMAND_MISMATCH`}}}export{C as a,d as c,S as i,f as l,M as n,m as o,v as r,w as s,T as t};
