import{createEveDevDispatchSchedulePath}from"#protocol/routes.js";import{toErrorMessage}from"#shared/errors.js";import"#client/client.js";import{EvalSessionManager}from"#evals/session.js";import{setTimeout}from"node:timers/promises";var EveEvalRequirementError=class extends Error{requirement;constructor(e,t){super(t),this.name=`EveEvalRequirementError`,this.requirement=e}};async function resolveEvalTargetHandle(e){await waitForTargetHealth(e.client,e.url);let t=await e.client.info();if(assertAgentInfoShape(t,e.url),e.expectedAgentName!==void 0&&t.agent.name!==e.expectedAgentName)throw Error(`Expected eval target ${JSON.stringify(e.expectedAgentName)} at ${e.url}, but ${JSON.stringify(t.agent.name)} is responding there.`);return createEvalTargetHandle({capabilities:capabilitiesFromInfo(t),client:e.client,kind:e.kind,url:e.url})}function createEvalTargetHandle(e){return createHandle({capabilities:e.capabilities,client:e.client,kind:e.kind,requireDeclared:!1,requirements:[],sessions:void 0,url:e.url})}function scopeEvalTargetHandle(e,t){return createHandle({capabilities:e.capabilities,client:void 0,delegate:e,kind:e.kind,requireDeclared:!0,requirements:t.requirements,sessions:t.sessions,url:e.url})}function createHandle(t){let r=new Set(t.requirements),i=t.delegate,a=t.client,fetchTarget=async(e,t)=>{if(i!==void 0)return await i.fetch(e,t);if(a===void 0)throw Error(`Eval target cannot fetch without a client.`);return await a.fetch(e,t)};return{capabilities:t.capabilities,kind:t.kind,url:t.url,async attachSession(e,r){if(t.sessions!==void 0)return await t.sessions.attachSession(e,r);if(i!==void 0)return await i.attachSession(e,r);if(a===void 0)throw Error(`Eval target cannot attach sessions without a client.`);return await new EvalSessionManager({client:a}).attachSession(e,r)},async dispatchSchedule(n){if(t.requireDeclared&&!r.has(`devRoutes`))throw new EveEvalRequirementError(`devRoutes`,'target.dispatchSchedule() requires declaring `requires: ["devRoutes"]` on the eval or case.');if(!t.capabilities.devRoutes)throw new EveEvalRequirementError(`devRoutes`,`target.dispatchSchedule() requires a target with dev routes enabled.`);let i=await fetchTarget(createEveDevDispatchSchedulePath(n),{method:`POST`});if(!i.ok){let e=await readResponseBodySafely(i);throw Error(`Schedule dispatch failed: ${i.status} ${i.statusText}`+(e.length>0?`, ${e}`:``))}return parseScheduleDispatchResult(await i.json())},async fetch(e,t){return await fetchTarget(e,t)}}}function capabilitiesFromInfo(e){return{devRoutes:e.capabilities?.devRoutes??e.mode===`development`,mockModels:e.capabilities?.mockModels??!1}}async function waitForTargetHealth(e,n){let i=Date.now()+6e4,a;for(;Date.now()<i;)try{await e.health();return}catch(e){a=toErrorMessage(e),await setTimeout(250)}throw Error(`Timed out waiting for eval target health at ${n}.`+(a===void 0?``:` Last error: ${a}`))}function assertAgentInfoShape(e,t){if(e.kind!==`eve-agent-info`||e.version!==1)throw Error(`Eval target ${t} returned an unrecognized /eve/v1/info payload.`)}function parseScheduleDispatchResult(e){if(typeof e!=`object`||!e||!(`scheduleId`in e)||typeof e.scheduleId!=`string`||!(`sessionIds`in e)||!Array.isArray(e.sessionIds)||e.sessionIds.some(e=>typeof e!=`string`))throw Error(`Schedule dispatch returned an unexpected response shape: ${JSON.stringify(e)}`);return{scheduleId:e.scheduleId,sessionIds:[...e.sessionIds]}}async function readResponseBodySafely(e){try{return(await e.text()).trim()}catch{return``}}export{EveEvalRequirementError,createEvalTargetHandle,resolveEvalTargetHandle,scopeEvalTargetHandle};