import{toErrorMessage}from"#shared/errors.js";import{deepEquals,subagentCallMatches,testRegExp,toolCallMatches}from"#evals/checks/match.js";function defineCheck(t,n){return async r=>{try{return{name:t,...await n(r)}}catch(n){return{name:t,passed:!1,message:toErrorMessage(n)}}}}function completed(){return defineCheck(`completed`,({result:e})=>e.status===`failed`?{passed:!1,message:failureDetail(`run failed`,e.derived.failureCode)}:e.derived.parked?{passed:!1,message:`run parked on ${e.derived.inputRequests.length} unanswered input request(s)`}:{passed:!0})}function waiting(){return defineCheck(`waiting`,({result:e})=>e.derived.parked?{passed:!0}:{passed:!1,message:`expected the run to park on HITL input; it ended "${e.status}" with no pending requests`})}function didNotFail(){return defineCheck(`didNotFail`,({result:e})=>{if(e.status===`failed`)return{passed:!1,message:failureDetail(`run failed`,e.derived.failureCode)};let t=e.events.find(e=>e.type===`turn.failed`||e.type===`step.failed`);return t===void 0?{passed:!0}:{passed:!1,message:`${t.type} (${t.data.code}): ${t.data.message}`}})}function messageIncludes(e){return defineCheck(`messageIncludes(${String(e)})`,({result:t})=>{let n=joinCompletedMessages(t.events);return(typeof e==`string`?n.includes(e):testRegExp(e,n))?{passed:!0}:{passed:!1,message:`assistant messages did not include ${String(e)}; got: ${truncate(n)}`}})}function outputEquals(e){return defineCheck(`outputEquals`,({result:n})=>deepEquals(n.output,e)?{passed:!0}:{passed:!1,message:`output ${truncate(JSON.stringify(n.output))} does not equal expected ${truncate(JSON.stringify(e))}`})}function outputMatches(e){return defineCheck(`outputMatches`,async({result:t})=>{let n=await e[`~standard`].validate(t.output);return!(`issues`in n)||n.issues===void 0?{passed:!0}:{passed:!1,message:`output failed schema validation: ${n.issues.map(e=>e.message).join(`; `)}`}})}function toolCalled(e,t={}){return defineCheck(`toolCalled(${e})`,n=>{let r=n.result.derived.toolCalls.filter(t=>t.name===e),a=r.filter(e=>toolCallMatches(e,t,n));if(t.times===void 0?a.length>0:a.length===t.times)return{passed:!0,metadata:{matchingCalls:a.length}};let o=r.length>0?`observed ${e} calls: ${r.map(e=>truncate(JSON.stringify(e.input))).join(`, `)}`:`observed tools: ${formatToolNames(n)}`;return{passed:!1,message:`${t.times===void 0?`expected a matching call to "${e}"`:`expected exactly ${t.times} matching call(s), found ${a.length}`}; ${o}`}})}function toolNotCalled(e){return defineCheck(`toolNotCalled(${e})`,({result:t})=>{let n=t.derived.toolCalls.filter(t=>t.name===e).length;return n===0?{passed:!0}:{passed:!1,message:`"${e}" was called ${n} time(s)`}})}function toolOrder(e){return defineCheck(`toolOrder(${e.join(` → `)})`,t=>{let n=t.result.derived.toolCalls.map(e=>e.name),r=0;for(let t of n)if(t===e[r]&&(r+=1),r===e.length)break;return r===e.length?{passed:!0}:{passed:!1,message:`missing "${e[r]}" after [${e.slice(0,r).join(`, `)}]; observed order: [${n.join(`, `)}]`}})}function noFailedActions(){return defineCheck(`noFailedActions`,({result:e})=>{let t=e.events.filter(e=>e.type===`action.result`&&(e.data.status===`failed`||e.data.result.isError===!0));if(t.length===0)return{passed:!0};let n=t.map(e=>e.data.result.kind===`tool-result`?e.data.result.toolName:e.data.result.kind);return{passed:!1,message:`${t.length} failed action(s): ${n.join(`, `)}`}})}function subagentCalled(e,t={}){return defineCheck(`subagentCalled(${e})`,r=>{let i=r.result.derived.subagentCalls.filter(t=>t.name===e);return i.filter(e=>subagentCallMatches(e,t,r)).length>0?{passed:!0}:i.length===0?{passed:!1,message:`subagent "${e}" was never called; observed: [${r.result.derived.subagentCalls.map(e=>e.name).join(`, `)}]`,metadata:{observedSubagentCalls:r.result.derived.subagentCalls}}:{passed:!1,message:`subagent "${e}" was called but no call matched the constraints`,metadata:{observedSubagentCalls:i}}})}function event(e,t){return defineCheck(`event(${t})`,({result:n})=>e(n.events)?{passed:!0}:{passed:!1,message:`event predicate "${t}" did not hold`})}function joinCompletedMessages(e){let t=[];for(let n of e)n.type===`message.completed`&&n.data.message!==null&&t.push(n.data.message);return t.join(`
`)}function formatToolNames(e){return`[${e.result.derived.toolCalls.map(e=>e.name).join(`, `)}]`}function failureDetail(e,t){return t===void 0?e:`${e} (code: ${t})`}function truncate(e,t=200){return e===void 0?`undefined`:e.length<=t?e:`${e.slice(0,t)}…`}export{completed,didNotFail,event,messageIncludes,noFailedActions,outputEquals,outputMatches,subagentCalled,toolCalled,toolNotCalled,toolOrder,waiting};