@directive-run/ai 1.23.1 → 1.24.0
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/README.md +27 -0
- package/dist/anthropic.cjs +1 -1
- package/dist/anthropic.cjs.map +1 -1
- package/dist/anthropic.d.cts +33 -1
- package/dist/anthropic.d.ts +33 -1
- package/dist/anthropic.js +1 -1
- package/dist/anthropic.js.map +1 -1
- package/dist/{chunk-QMSLWAMH.cjs → chunk-25OLDNZ3.cjs} +3 -3
- package/dist/{chunk-QMSLWAMH.cjs.map → chunk-25OLDNZ3.cjs.map} +1 -1
- package/dist/chunk-2Y4YWZDV.js +3 -0
- package/dist/chunk-2Y4YWZDV.js.map +1 -0
- package/dist/{chunk-J2Q5KKPN.js → chunk-437ADRES.js} +3 -3
- package/dist/{chunk-J2Q5KKPN.js.map → chunk-437ADRES.js.map} +1 -1
- package/dist/{chunk-2FPL5W2H.cjs → chunk-4LENWYLG.cjs} +7 -7
- package/dist/{chunk-2FPL5W2H.cjs.map → chunk-4LENWYLG.cjs.map} +1 -1
- package/dist/chunk-4TDH6ZTX.cjs +3 -0
- package/dist/chunk-4TDH6ZTX.cjs.map +1 -0
- package/dist/{chunk-K64WKZ22.cjs → chunk-AEX7HKNM.cjs} +2 -2
- package/dist/chunk-AEX7HKNM.cjs.map +1 -0
- package/dist/{chunk-4VP67W7L.js → chunk-CMH5TJGN.js} +4 -4
- package/dist/{chunk-4VP67W7L.js.map → chunk-CMH5TJGN.js.map} +1 -1
- package/dist/{chunk-XGOTVPN6.js → chunk-EUB4NVVN.js} +3 -3
- package/dist/{chunk-XGOTVPN6.js.map → chunk-EUB4NVVN.js.map} +1 -1
- package/dist/{chunk-EW77VF5K.js → chunk-H7UFBMLV.js} +3 -3
- package/dist/{chunk-EW77VF5K.js.map → chunk-H7UFBMLV.js.map} +1 -1
- package/dist/{chunk-FSSCGRAM.js → chunk-L6H6Y6IK.js} +3 -3
- package/dist/{chunk-FSSCGRAM.js.map → chunk-L6H6Y6IK.js.map} +1 -1
- package/dist/{chunk-JMG5GZAH.cjs → chunk-L7QHLH6U.cjs} +4 -4
- package/dist/{chunk-JMG5GZAH.cjs.map → chunk-L7QHLH6U.cjs.map} +1 -1
- package/dist/{chunk-F73AKFDB.cjs → chunk-LPTC3N2Z.cjs} +7 -7
- package/dist/{chunk-F73AKFDB.cjs.map → chunk-LPTC3N2Z.cjs.map} +1 -1
- package/dist/{chunk-UR5BMWEN.js → chunk-XMEJN4FA.js} +2 -2
- package/dist/chunk-XMEJN4FA.js.map +1 -0
- package/dist/{chunk-XN5LUOVS.cjs → chunk-ZUNORULG.cjs} +7 -7
- package/dist/{chunk-XN5LUOVS.cjs.map → chunk-ZUNORULG.cjs.map} +1 -1
- package/dist/{debug-timeline-Sc9cxgxV.d.cts → debug-timeline-B_bXRStF.d.cts} +1 -1
- package/dist/{debug-timeline-C5Dzs2r_.d.ts → debug-timeline-Btt-iaak.d.ts} +1 -1
- package/dist/devtools.d.cts +5 -5
- package/dist/devtools.d.ts +5 -5
- package/dist/evals.cjs +1 -1
- package/dist/evals.d.cts +2 -2
- package/dist/evals.d.ts +2 -2
- package/dist/evals.js +1 -1
- package/dist/gemini.cjs +1 -1
- package/dist/gemini.d.cts +1 -1
- package/dist/gemini.d.ts +1 -1
- package/dist/gemini.js +1 -1
- package/dist/{guardrails-export-BL8ozXSj.d.ts → guardrails-export-lvaBJlt6.d.ts} +1 -1
- package/dist/{guardrails-export-ByaYNGvG.d.cts → guardrails-export-pmpQjIKb.d.cts} +1 -1
- package/dist/guardrails.cjs +1 -1
- package/dist/guardrails.d.cts +2 -2
- package/dist/guardrails.d.ts +2 -2
- package/dist/guardrails.js +1 -1
- package/dist/{health-monitor-C9v10_tj.d.cts → health-monitor-DqAzufyM.d.cts} +1 -1
- package/dist/{health-monitor-J2eKmP--.d.ts → health-monitor-DrlBF6n9.d.ts} +1 -1
- package/dist/index.cjs +4 -4
- package/dist/index.d.cts +12 -8
- package/dist/index.d.ts +12 -8
- package/dist/index.js +1 -1
- package/dist/{multi-agent-orchestrator-ZZ63F75F.js → multi-agent-orchestrator-PI7X7ZGP.js} +2 -2
- package/dist/{multi-agent-orchestrator-ZZ63F75F.js.map → multi-agent-orchestrator-PI7X7ZGP.js.map} +1 -1
- package/dist/multi-agent-orchestrator-Z5NK4HIJ.cjs +2 -0
- package/dist/{multi-agent-orchestrator-DUMT67N5.cjs.map → multi-agent-orchestrator-Z5NK4HIJ.cjs.map} +1 -1
- package/dist/multi-agent.cjs +1 -1
- package/dist/multi-agent.d.cts +6 -6
- package/dist/multi-agent.d.ts +6 -6
- package/dist/multi-agent.js +1 -1
- package/dist/ollama.cjs +1 -1
- package/dist/ollama.d.cts +1 -1
- package/dist/ollama.d.ts +1 -1
- package/dist/ollama.js +1 -1
- package/dist/openai.cjs +1 -1
- package/dist/openai.d.cts +1 -1
- package/dist/openai.d.ts +1 -1
- package/dist/openai.js +1 -1
- package/dist/{orchestrator-types-BT-NIFOe.d.cts → orchestrator-types-B9nrJYFx.d.cts} +3 -3
- package/dist/{orchestrator-types-DMuSD8Vq.d.ts → orchestrator-types-ByqFuu3O.d.ts} +3 -3
- package/dist/predicate.d.cts +1 -1
- package/dist/predicate.d.ts +1 -1
- package/dist/testing.cjs +1 -1
- package/dist/testing.d.cts +4 -4
- package/dist/testing.d.ts +4 -4
- package/dist/testing.js +1 -1
- package/dist/{types-6NoeLyrD.d.cts → types-BzpGfUA2.d.cts} +10 -0
- package/dist/{types-6NoeLyrD.d.ts → types-BzpGfUA2.d.ts} +10 -0
- package/package.json +2 -2
- package/dist/chunk-3WO4MWJM.cjs +0 -3
- package/dist/chunk-3WO4MWJM.cjs.map +0 -1
- package/dist/chunk-K64WKZ22.cjs.map +0 -1
- package/dist/chunk-UR5BMWEN.js.map +0 -1
- package/dist/chunk-ZFLHWJ56.js +0 -3
- package/dist/chunk-ZFLHWJ56.js.map +0 -1
- package/dist/multi-agent-orchestrator-DUMT67N5.cjs +0 -2
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
function x(t){return t.status==="running"}function C(t){return t.pending.length>0}function O(t,e){return t/1e6*e}var y=new Set(["http:","https:"]);function L(t){try{let e=new URL(t);if(!y.has(e.protocol))throw new Error(`[Directive] Invalid baseURL protocol "${e.protocol}" \u2013 only http: and https: are allowed`)}catch(e){throw e instanceof Error&&e.message.startsWith("[Directive]")?e:new Error(`[Directive] Invalid baseURL "${t}" \u2013 must be a valid URL (e.g. "https://api.openai.com/v1")`)}}function S(t){let e=t.filter(s=>s!==void 0);if(e.length===0)return;if(e.length===1)return e[0];let l=AbortSignal.any;if(typeof l=="function")return l(e);let r=new AbortController,p=e.find(s=>s.aborted);if(p)return r.abort(p.reason??void 0),r.signal;for(let s of e)s.addEventListener("abort",()=>{r.abort(s.reason??void 0);},{once:true});return r.signal}function M(t){let{fetch:e=globalThis.fetch,buildRequest:l,parseResponse:r,parseOutput:p,hooks:s}=t,b=p??(o=>{try{return JSON.parse(o)}catch{return o}});return async(o,a,f)=>{let g=Date.now();s?.onBeforeCall?.({agent:o,input:a,timestamp:g});let d=[{role:"user",content:a}];try{let{url:i,init:u}=l(o,a,d),h=S([u.signal??void 0,f?.signal]),m=h?{...u,signal:h}:u,c=await e(i,m);if(!c.ok){let R=await c.text().catch(()=>"");throw new Error(`[Directive] AgentRunner request failed: ${c.status} ${c.statusText}${R?` \u2013 ${R.slice(0,300)}`:""}`)}let n=await r(c,d),k={inputTokens:n.inputTokens??0,outputTokens:n.outputTokens??0,...n.cacheReadTokens!=null?{cacheReadTokens:n.cacheReadTokens}:{},...n.cacheCreationTokens!=null?{cacheCreationTokens:n.cacheCreationTokens}:{}},T={role:"assistant",content:n.text},A=[...d,T];f?.onMessage?.(T);let w=Date.now()-g;return s?.onAfterCall?.({agent:o,input:a,output:n.text,totalTokens:n.totalTokens,tokenUsage:k,durationMs:w,timestamp:Date.now()}),{output:b(n.text),messages:A,toolCalls:[],totalTokens:n.totalTokens,tokenUsage:k}}catch(i){let u=Date.now()-g;throw i instanceof Error&&s?.onError?.({agent:o,input:a,error:i,durationMs:u,timestamp:Date.now()}),i}}}
|
|
2
|
+
export{x as a,C as b,O as c,L as d,S as e,M as f};//# sourceMappingURL=chunk-2Y4YWZDV.js.map
|
|
3
|
+
//# sourceMappingURL=chunk-2Y4YWZDV.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/agent-utils.ts"],"names":["isAgentRunning","state","hasPendingApprovals","estimateCost","tokenUsage","ratePerMillionTokens","ALLOWED_PROTOCOLS","validateBaseURL","baseURL","url","err","combineSignals","signals","live","anyFn","controller","already","createRunner","options","fetchFn","buildRequest","parseResponse","parseOutput","hooks","parse","text","agent","input","runOptions","startTime","messages","init","combined","fetchInit","response","errBody","parsed","assistantMessage","allMessages","durationMs"],"mappings":"AA0BO,SAASA,CAAAA,CAAeC,CAAAA,CAA4B,CACzD,OAAOA,CAAAA,CAAM,SAAW,SAC1B,CAQO,SAASC,CAAAA,CAAoBD,CAAAA,CAA+B,CACjE,OAAOA,CAAAA,CAAM,OAAA,CAAQ,MAAA,CAAS,CAChC,CAiBO,SAASE,EACdC,CAAAA,CACAC,CAAAA,CACQ,CACR,OAAQD,CAAAA,CAAa,GAAA,CAAaC,CACpC,CAMA,IAAMC,CAAAA,CAAoB,IAAI,GAAA,CAAI,CAAC,QAAS,QAAQ,CAAC,CAAA,CAU9C,SAASC,CAAAA,CAAgBC,CAAAA,CAAuB,CACrD,GAAI,CACF,IAAMC,CAAAA,CAAM,IAAI,GAAA,CAAID,CAAO,CAAA,CAC3B,GAAI,CAACF,CAAAA,CAAkB,GAAA,CAAIG,CAAAA,CAAI,QAAQ,CAAA,CACrC,MAAM,IAAI,KAAA,CACR,CAAA,sCAAA,EAAyCA,CAAAA,CAAI,QAAQ,CAAA,0CAAA,CACvD,CAEJ,CAAA,MAASC,CAAAA,CAAK,CACZ,MAAIA,aAAe,KAAA,EAASA,CAAAA,CAAI,OAAA,CAAQ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CAAOjB,CAAAA,CACP,UAAA,CAAA6B,CAAAA,CACA,SAAA,CAAW,IAAA,CAAK,GAAA,EAClB,CAAC,CAAA,CAGG7B,CACR,CACF,CACF","file":"chunk-2Y4YWZDV.js","sourcesContent":["/**\n * Agent utilities — createRunner, estimateCost, state queries, URL validation.\n */\n\nimport type {\n AdapterHooks,\n AgentLike,\n AgentRunner,\n AgentState,\n ApprovalState,\n Message,\n RunOptions,\n RunResult,\n TokenUsage,\n} from \"./types.js\";\n\n// ============================================================================\n// State Query Helpers\n// ============================================================================\n\n/**\n * Check whether an agent is currently executing a run.\n *\n * @param state - The current {@link AgentState} to inspect.\n * @returns `true` when the agent status is `\"running\"`.\n */\nexport function isAgentRunning(state: AgentState): boolean {\n return state.status === \"running\";\n}\n\n/**\n * Check whether there are tool-call approvals waiting for user confirmation.\n *\n * @param state - The current {@link ApprovalState} to inspect.\n * @returns `true` when one or more approvals are pending.\n */\nexport function hasPendingApprovals(state: ApprovalState): boolean {\n return state.pending.length > 0;\n}\n\n// ============================================================================\n// Cost Estimation\n// ============================================================================\n\n/**\n * Estimate the dollar cost of an agent run based on total token usage.\n *\n * @remarks\n * No default rate is provided — callers must supply the current per-million-token\n * price to avoid silently using stale pricing.\n *\n * @param tokenUsage - Total number of tokens consumed (input + output).\n * @param ratePerMillionTokens - Cost in dollars per one million tokens.\n * @returns Estimated cost in dollars.\n */\nexport function estimateCost(\n tokenUsage: number,\n ratePerMillionTokens: number,\n): number {\n return (tokenUsage / 1_000_000) * ratePerMillionTokens;\n}\n\n// ============================================================================\n// Validation Helpers\n// ============================================================================\n\nconst ALLOWED_PROTOCOLS = new Set([\"http:\", \"https:\"]);\n\n/**\n * Validate that a base URL uses the `http:` or `https:` protocol.\n * Throws immediately at adapter creation time (not at call time) to surface\n * configuration errors before any LLM requests are made.\n *\n * @param baseURL - The base URL string to validate.\n * @throws When the URL is malformed or uses a protocol other than `http:` or `https:`.\n */\nexport function validateBaseURL(baseURL: string): void {\n try {\n const url = new URL(baseURL);\n if (!ALLOWED_PROTOCOLS.has(url.protocol)) {\n throw new Error(\n `[Directive] Invalid baseURL protocol \"${url.protocol}\" – only http: and https: are allowed`,\n );\n }\n } catch (err) {\n if (err instanceof Error && err.message.startsWith(\"[Directive]\")) {\n throw err;\n }\n\n throw new Error(\n `[Directive] Invalid baseURL \"${baseURL}\" – must be a valid URL (e.g. \"https://api.openai.com/v1\")`,\n );\n }\n}\n\n// ============================================================================\n// AbortSignal combination helper\n// ============================================================================\n\n/**\n * Combine multiple `AbortSignal`s into one — the resulting signal aborts\n * as soon as ANY input signal aborts. Falls back to a manual controller\n * wiring on runtimes without `AbortSignal.any` (Node < 20).\n *\n * @internal\n */\nexport function combineSignals(\n signals: ReadonlyArray<AbortSignal | undefined>,\n): AbortSignal | undefined {\n const live = signals.filter((s): s is AbortSignal => s !== undefined);\n if (live.length === 0) return undefined;\n if (live.length === 1) return live[0];\n\n // Prefer the standard helper when available (Node 20.3+, modern browsers).\n const anyFn = (\n AbortSignal as unknown as {\n any?: (signals: readonly AbortSignal[]) => AbortSignal;\n }\n ).any;\n if (typeof anyFn === \"function\") {\n return anyFn(live);\n }\n\n // Fallback: manual controller. If any input is already aborted, the\n // result is too — wire listeners for the rest.\n const controller = new AbortController();\n const already = live.find((s) => s.aborted);\n if (already) {\n controller.abort(\n (already as AbortSignal & { reason?: unknown }).reason ?? undefined,\n );\n\n return controller.signal;\n }\n for (const s of live) {\n s.addEventListener(\n \"abort\",\n () => {\n controller.abort(\n (s as AbortSignal & { reason?: unknown }).reason ?? undefined,\n );\n },\n { once: true },\n );\n }\n\n return controller.signal;\n}\n\n// ============================================================================\n// createRunner Helper\n// ============================================================================\n\n/** Parsed response from an LLM provider */\nexport interface ParsedResponse {\n text: string;\n totalTokens: number;\n /** Input token count, when available from the provider */\n inputTokens?: number;\n /** Output token count, when available from the provider */\n outputTokens?: number;\n /** Prompt-cache read token count, when available from the provider */\n cacheReadTokens?: number;\n /** Prompt-cache creation token count, when available from the provider */\n cacheCreationTokens?: number;\n}\n\n/** Options for creating an AgentRunner from buildRequest/parseResponse */\nexport interface CreateRunnerOptions {\n fetch?: typeof globalThis.fetch;\n buildRequest: (\n agent: AgentLike,\n input: string,\n messages: Message[],\n ) => { url: string; init: RequestInit };\n parseResponse: (\n response: Response,\n messages: Message[],\n ) => Promise<ParsedResponse>;\n parseOutput?: <T>(text: string) => T;\n /** Lifecycle hooks for tracing, logging, and metrics */\n hooks?: AdapterHooks;\n}\n\n/**\n * Create an {@link AgentRunner} from `buildRequest`/`parseResponse` helpers, reducing\n * ~50 lines of fetch boilerplate to ~20 lines of configuration.\n *\n * @remarks\n * Supports lifecycle hooks for observability:\n * - `onBeforeCall` fires before each API request\n * - `onAfterCall` fires after a successful response (includes token breakdown)\n * - `onError` fires when the request fails\n *\n * Output parsing defaults to `JSON.parse` with a string fallback. Supply a custom\n * `parseOutput` to override (e.g. for structured output schemas).\n *\n * @param options - Configuration for the runner, including request building, response parsing, and hooks.\n * @returns An {@link AgentRunner} function that performs LLM calls via fetch.\n *\n * @example\n * ```typescript\n * const runClaude = createRunner({\n * buildRequest: (agent, input) => ({\n * url: \"/api/claude\",\n * init: {\n * method: \"POST\",\n * headers: { \"Content-Type\": \"application/json\" },\n * body: JSON.stringify({\n * model: agent.model ?? \"claude-haiku-4-5-20251001\",\n * system: agent.instructions ?? \"\",\n * messages: [{ role: \"user\", content: input }],\n * }),\n * },\n * }),\n * parseResponse: async (res) => {\n * const data = await res.json();\n * const inputTokens = data.usage?.input_tokens ?? 0;\n * const outputTokens = data.usage?.output_tokens ?? 0;\n * return {\n * text: data.content?.[0]?.text ?? \"\",\n * totalTokens: inputTokens + outputTokens,\n * inputTokens,\n * outputTokens,\n * };\n * },\n * hooks: {\n * onAfterCall: ({ durationMs, tokenUsage }) => {\n * console.log(`LLM call: ${durationMs}ms, ${tokenUsage.inputTokens}in/${tokenUsage.outputTokens}out`);\n * },\n * },\n * });\n * ```\n *\n * @public\n */\nexport function createRunner(options: CreateRunnerOptions): AgentRunner {\n const {\n fetch: fetchFn = globalThis.fetch,\n buildRequest,\n parseResponse,\n parseOutput,\n hooks,\n } = options;\n\n const defaultParseOutput = <T>(text: string): T => {\n try {\n return JSON.parse(text) as T;\n } catch {\n return text as unknown as T;\n }\n };\n\n const parse = parseOutput ?? defaultParseOutput;\n\n return async <T = unknown>(\n agent: AgentLike,\n input: string,\n runOptions?: RunOptions,\n ): Promise<RunResult<T>> => {\n const startTime = Date.now();\n hooks?.onBeforeCall?.({ agent, input, timestamp: startTime });\n\n const messages: Message[] = [{ role: \"user\", content: input }];\n\n try {\n const { url, init } = buildRequest(agent, input, messages);\n\n // (Sec MAJOR) Combine signals — `buildRequest` may set\n // `init.signal` (e.g. `AbortSignal.timeout(timeoutMs)`) and the\n // caller may pass their own via `runOptions.signal`. Naively\n // overwriting one with the other silently disables whichever was\n // dropped. `combineSignals` aborts as soon as either fires.\n const combined = combineSignals([\n init.signal ?? undefined,\n runOptions?.signal,\n ]);\n const fetchInit: RequestInit = combined\n ? { ...init, signal: combined }\n : init;\n\n const response = await fetchFn(url, fetchInit);\n\n if (!response.ok) {\n const errBody = await response.text().catch(() => \"\");\n\n throw new Error(\n `[Directive] AgentRunner request failed: ${response.status} ${response.statusText}${errBody ? ` – ${errBody.slice(0, 300)}` : \"\"}`,\n );\n }\n\n const parsed = await parseResponse(response, messages);\n const tokenUsage: TokenUsage = {\n inputTokens: parsed.inputTokens ?? 0,\n outputTokens: parsed.outputTokens ?? 0,\n ...(parsed.cacheReadTokens != null\n ? { cacheReadTokens: parsed.cacheReadTokens }\n : {}),\n ...(parsed.cacheCreationTokens != null\n ? { cacheCreationTokens: parsed.cacheCreationTokens }\n : {}),\n };\n\n const assistantMessage: Message = {\n role: \"assistant\",\n content: parsed.text,\n };\n const allMessages: Message[] = [...messages, assistantMessage];\n\n runOptions?.onMessage?.(assistantMessage);\n\n const durationMs = Date.now() - startTime;\n hooks?.onAfterCall?.({\n agent,\n input,\n output: parsed.text,\n totalTokens: parsed.totalTokens,\n tokenUsage,\n durationMs,\n timestamp: Date.now(),\n });\n\n return {\n output: parse<T>(parsed.text),\n messages: allMessages,\n toolCalls: [],\n totalTokens: parsed.totalTokens,\n tokenUsage,\n };\n } catch (err) {\n const durationMs = Date.now() - startTime;\n if (err instanceof Error) {\n hooks?.onError?.({\n agent,\n input,\n error: err,\n durationMs,\n timestamp: Date.now(),\n });\n }\n\n throw err;\n }\n };\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {e}from'./chunk-
|
|
1
|
+
import {e}from'./chunk-2Y4YWZDV.js';var M=class{constructor(n){this.max=n;if(n<1)throw new Error(`[Directive Evals] concurrency must be >= 1, got ${n}`)}queue=[];active=0;async acquire(n){if(this.active<this.max){this.active++;return}return new Promise((r,s)=>{let a={resolve:r,reject:s};this.queue.push(a),n&&n.addEventListener("abort",()=>{let t=this.queue.indexOf(a);t!==-1&&(this.queue.splice(t,1),s(new Error("Semaphore acquire aborted")));},{once:true});})}release(){if(this.active<=0)return;this.active--;let n=this.queue.shift();n&&(this.active++,n.resolve());}};function N(e,n){return typeof n=="function"?{name:e,fn:n,threshold:.5,weight:1}:{...n,name:e,threshold:n.threshold??.5,weight:n.weight??1}}function P(e,n){let r=0,s=0;for(let[a,t]of Object.entries(e)){let u=n[a]?.weight??1,i=Number.isFinite(t.score)?t.score:0;r+=i*u,s+=u;}return s===0?0:r/s}function T(e,n){let r=0,s=0;for(let[a,t]of Object.entries(e)){let u=n[a]?.weight??1,i=Number.isFinite(t)?t:0;r+=i*u,s+=u;}return s===0?0:r/s}var k={pii:[/\b\d{3}-\d{2}-\d{4}\b/,/\b(?:4\d{3}|5[1-5]\d{2}|6011)\d{12}\b/,/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/],violence:[/\b(kill|murder|attack|bomb|weapon|shoot|stab)\b/i],self_harm:[/\b(suicide|self[- ]harm|cut myself)\b/i],illegal:[/\b(hack into|break into|steal|counterfeit)\b/i]};function _(e){return {name:"cost",fn:n=>{let r=Date.now(),s=n.result.totalTokens,a=s/e.maxTokensPerRun,t;return a<=.5?t=1:a>=2?t=0:t=1-(a-.5)/1.5,{score:t,passed:s<=e.maxTokensPerRun,reason:`${s} tokens (budget: ${e.maxTokensPerRun})`,durationMs:Date.now()-r}},threshold:.5,weight:1}}function J(e){return {name:"latency",fn:n=>{let r=Date.now(),s=n.runDurationMs,a=s/e.maxMs,t;return a<=.5?t=1:a>=2?t=0:t=1-(a-.5)/1.5,{score:t,passed:s<=e.maxMs,reason:`${Math.round(s)}ms (max: ${e.maxMs}ms)`,durationMs:Date.now()-r}},threshold:.5,weight:1}}function F(e){if(e.minLength!==void 0&&e.maxLength!==void 0&&e.minLength>e.maxLength)throw new Error("[Directive Evals] evalOutputLength: minLength must be <= maxLength");return {name:"outputLength",fn:n=>{let r=Date.now(),a=String(n.result.output).length,t=e.minLength??0,o=e.maxLength??Number.POSITIVE_INFINITY,u=a>=t&&a<=o,i;return u?i=1:a<t?i=t>0?Math.max(0,a/t):0:i=o>0&&o!==Number.POSITIVE_INFINITY?Math.max(0,1-(a-o)/o):0,{score:i,passed:u,reason:`${a} chars (range: ${t}-${o===Number.POSITIVE_INFINITY?"\u221E":o})`,durationMs:Date.now()-r}},threshold:.5,weight:1}}function q(e={}){let n;if(e.blockedPatterns)n=e.blockedPatterns;else if(e.categories&&e.categories.length>0){n=[];for(let r of e.categories){let s=k[r];s&&n.push(...s);}}else {n=[];for(let r of Object.values(k))n.push(...r);}return {name:"safety",fn:r=>{let s=Date.now(),a=String(r.result.output),t=[];for(let u of n)u.test(a)&&t.push(u.source);return {score:t.length===0?1:0,passed:t.length===0,reason:t.length===0?"No unsafe patterns detected":`Matched patterns: ${t.join(", ")}`,durationMs:Date.now()-s}},threshold:1,weight:2}}function Y(e){return {name:"structure",fn:n=>{let r=Date.now(),s=n.result.output;if(e.type==="json")try{let t=typeof s=="string"?JSON.parse(s):s;if(!t||typeof t!="object"||Array.isArray(t))return {score:0,passed:!1,reason:"Output is not a valid JSON object",durationMs:Date.now()-r};if(e.requiredKeys&&t&&typeof t=="object"){let o=e.requiredKeys.filter(u=>!Object.hasOwn(t,u));if(o.length>0)return {score:1-o.length/e.requiredKeys.length,passed:!1,reason:`Missing keys: ${o.join(", ")}`,durationMs:Date.now()-r}}return {score:1,passed:!0,reason:"Valid JSON with all required keys",durationMs:Date.now()-r}}catch{return {score:0,passed:false,reason:"Output is not valid JSON",durationMs:Date.now()-r}}let a=String(s);return {score:a.length>0?1:0,passed:a.length>0,reason:a.length>0?"Non-empty output":"Empty output",durationMs:Date.now()-r}},threshold:.5,weight:1}}function G(e$1){let n=e$1.promptTemplate??`You are evaluating an AI agent's output. Score it from 0.0 to 1.0.
|
|
2
2
|
|
|
3
3
|
Input: {{input}}
|
|
4
4
|
Expected: {{expected}}
|
|
@@ -33,5 +33,5 @@ Agent Output: {{output}}
|
|
|
33
33
|
|
|
34
34
|
Respond with ONLY a JSON object: {"score": <number 0.0-1.0>, "reason": "<brief explanation>"}`;function V(e$1){return {name:"coherence",fn:async n=>{let r=Date.now(),s=j.replaceAll("{{output}}",String(n.result.output)),a=e$1.timeoutMs??3e4,t=new AbortController,o=setTimeout(()=>t.abort(),a),u=e$1.signal?e([e$1.signal,t.signal]):t.signal;try{let c=(await e$1.runner(e$1.judge,s,{signal:u})).output;if(typeof c!="string")return {score:0,passed:!1,reason:"Judge returned non-string output",durationMs:Date.now()-r};let v=JSON.parse(c),l=Math.max(0,Math.min(1,v.score));return {score:l,passed:l>=.7,reason:v.reason??`Coherence score: ${l}`,durationMs:Date.now()-r}}catch(i){return {score:0,passed:false,reason:`Coherence eval error: ${i instanceof Error?i.message:String(i)}`,durationMs:Date.now()-r}}finally{clearTimeout(o);}},threshold:.7,weight:1}}function K(e){let{agents:n,runner:r,dataset:s,runOptions:a,concurrency:t=5,timeline:o,onCaseComplete:u,onAgentComplete:i,signal:c}=e;if(s.length===0)throw new Error("[Directive Evals] Dataset must contain at least one test case");let v=new M(t),l={};for(let[p,d]of Object.entries(e.criteria))l[p]=N(p,d);async function S(p,d){try{await v.acquire(c);}catch{let b={};for(let f of Object.keys(l))b[f]={score:0,passed:false,reason:"Evaluation aborted",durationMs:0};return {testCase:d,agentName:p.name,runResult:{output:"",messages:[],toolCalls:[],totalTokens:0},scores:b,overallScore:0,allPassed:false,runDurationMs:0}}try{let b=Date.now(),f;try{f=await r(p,d.input,{...a,signal:c});}catch(y){let h={output:"",messages:[],toolCalls:[],totalTokens:0},E={};for(let O of Object.keys(l))E[O]={score:0,passed:!1,reason:`Agent error: ${y instanceof Error?`${y.name}: ${y.message}`:String(y)}`,durationMs:0};return {testCase:d,agentName:p.name,runResult:h,scores:E,overallScore:0,allPassed:!1,runDurationMs:Date.now()-b}}let w=Date.now()-b,R={agent:p,testCase:d,result:f,runDurationMs:w},g={};for(let[y,h]of Object.entries(l))try{let E=await h.fn(R);g[y]={...E,passed:E.score>=(h.threshold??.5)};}catch(E){g[y]={score:0,passed:!1,reason:`Criterion error: ${E instanceof Error?`${E.name}: ${E.message}`:String(E)}`,durationMs:0};}let m=P(g,l),x=Object.values(g).every(y=>y.passed),A={testCase:d,agentName:p.name,runResult:f,scores:g,overallScore:m,allPassed:x,runDurationMs:w};return o&&o.record({type:"agent_complete",timestamp:Date.now(),agentId:`eval:${p.name}`,outputLength:String(f.output).length,totalTokens:f.totalTokens,durationMs:w,snapshotId:null}),u?.(A),A}finally{v.release();}}function D(p,d){let b={},f={},w=0,R=0,g=0;for(let h of Object.keys(l))b[h]=0,f[h]=0;for(let h of d){w+=h.runResult.totalTokens,R+=h.runDurationMs,h.allPassed&&g++;for(let[E,O]of Object.entries(h.scores))b[E]=(b[E]??0)+O.score,O.passed&&(f[E]=(f[E]??0)+1);}let m=d.length,x={},A={};for(let h of Object.keys(l))x[h]=m>0?(b[h]??0)/m:0,A[h]=m>0?(f[h]??0)/m:0;let y=m>0?T(x,l):0;return {agentName:p,criterionAverages:x,criterionPassRates:A,overallScore:y,passRate:m>0?g/m:0,totalTokens:w,avgLatencyMs:m>0?R/m:0,totalCases:m,passedCases:g}}return {getAgents:()=>[...n],getCriteria:()=>Object.keys(l),getDataset:()=>[...s],async run(){let p=Date.now(),d=[],b={},f=n.map(async g=>{if(c?.aborted)return;let m=await Promise.all(s.map(x=>S(g,x)));return {agent:g,agentResults:m}}),w=await Promise.all(f);for(let g of w){if(!g)continue;d.push(...g.agentResults);let m=D(g.agent.name,g.agentResults);b[g.agent.name]=m,i?.(m);}let R=Date.now();return {summary:b,details:d,durationMs:R-p,totalTokens:d.reduce((g,m)=>g+m.runResult.totalTokens,0),startedAt:p,completedAt:R}},async runAgent(p){let d=n.find(w=>w.name===p);if(!d)throw new Error(`[Directive Evals] Unknown agent: "${p}"`);let b=await Promise.all(s.map(w=>S(d,w))),f=D(d.name,b);return i?.(f),f}}}function W(e,n){let r=[];for(let[s,a]of Object.entries(e.summary))if(n.minScore!==void 0&&a.overallScore<n.minScore&&r.push(`Agent "${s}" score ${a.overallScore.toFixed(3)} < minimum ${n.minScore}`),n.minPassRate!==void 0&&a.passRate<n.minPassRate&&r.push(`Agent "${s}" pass rate ${a.passRate.toFixed(3)} < minimum ${n.minPassRate}`),n.failOn)for(let t of n.failOn){let o=a.criterionPassRates[t];o!==void 0&&o<1&&r.push(`Agent "${s}" criterion "${t}" pass rate ${o.toFixed(3)} < 1.0 (failOn)`);}if(r.length>0)throw new Error(`[Directive Evals] Assertion failed:
|
|
35
35
|
${r.join(`
|
|
36
|
-
`)}`)}export{_ as a,J as b,F as c,q as d,Y as e,G as f,z as g,X as h,H as i,V as j,K as k,W as l};//# sourceMappingURL=chunk-
|
|
37
|
-
//# sourceMappingURL=chunk-
|
|
36
|
+
`)}`)}export{_ as a,J as b,F as c,q as d,Y as e,G as f,z as g,X as h,H as i,V as j,K as k,W as l};//# sourceMappingURL=chunk-437ADRES.js.map
|
|
37
|
+
//# sourceMappingURL=chunk-437ADRES.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/evals.ts"],"names":["EvalSemaphore","max","signal","resolve","reject","entry","idx","next","normalizeCriterion","name","input","computeWeightedScore","scores","criteria","weightedSum","totalWeight","score","weight","safeScore","computeWeightedAverage","averages","avg","safeAvg","SAFETY_CATEGORY_PATTERNS","evalCost","options","context","start","tokens","ratio","evalLatency","duration","evalOutputLength","length","min","withinRange","evalSafety","patterns","category","categoryPatterns","output","matches","pattern","evalStructure","parsed","missing","k","str","evalJudge","template","prompt","timeoutMs","controller","timer","combinedSignal","combineSignals","raw","err","evalMatch","mode","ci","expected","a","b","matched","FAITHFULNESS_PROMPT","evalFaithfulness","refContext","RELEVANCE_PROMPT","evalRelevance","COHERENCE_PROMPT","evalCoherence","createEvalSuite","config","agents","runner","dataset","runOptions","concurrency","timeline","onCaseComplete","onAgentComplete","sem","evaluateCase","agent","testCase","runStart","runResult","errorResult","runDurationMs","evalContext","criterion","overallScore","allPassed","s","caseResult","buildAgentSummary","agentName","caseResults","criterionSums","criterionPasses","totalTokens","totalLatency","passedCases","cr","totalCases","criterionAverages","criterionPassRates","startedAt","allDetails","summary","agentPromises","agentResults","results","agentSummary","completedAt","sum","d","evalAssert","failures","criterionName","passRate"],"mappings":"oCA8LA,IAAMA,CAAAA,CAAN,KAAoB,CAKlB,WAAA,CAA6BC,EAAa,CAAb,IAAA,CAAA,GAAA,CAAAA,CAAAA,CAC3B,GAAIA,CAAAA,CAAM,CAAA,CACR,MAAM,IAAI,KAAA,CAAM,mDAAmDA,CAAG,CAAA,CAAE,CAE5E,CARQ,KAAA,CACN,EAAC,CACK,MAAA,CAAS,CAAA,CAQjB,MAAM,OAAA,CAAQC,CAAAA,CAAqC,CACjD,GAAI,IAAA,CAAK,OAAS,IAAA,CAAK,GAAA,CAAK,CAC1B,IAAA,CAAK,MAAA,EAAA,CAEL,MACF,CAEA,OAAO,IAAI,QAAc,CAACC,CAAAA,CAASC,IAAW,CAC5C,IAAMC,CAAAA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A,EAAaA,CAAAA,CAAW,CAAA,EACvCF,CAAAA,CAAS,IAAA,CACP,CAAA,OAAA,EAAUtB,CAAS,CAAA,aAAA,EAAgBuB,CAAa,CAAA,YAAA,EAAeC,CAAAA,CAAS,OAAA,CAAQ,CAAC,CAAC,CAAA,eAAA,CACpF,EAEJ,CAIJ,GAAIF,CAAAA,CAAS,MAAA,CAAS,CAAA,CACpB,MAAM,IAAI,KAAA,CACR,CAAA;AAAA,EAAwCA,EAAS,IAAA,CAAK;AAAA,CAAI,CAAC,EAC7D,CAEJ","file":"chunk-J2Q5KKPN.js","sourcesContent":["/**\n * Evaluation Framework — Constraint-driven agent evaluation.\n *\n * Define eval criteria as composable functions. Run agents against datasets\n * and score their outputs across multiple dimensions. Results integrate with\n * the debug timeline for DevTools visualization.\n *\n * @example\n * ```typescript\n * const suite = createEvalSuite({\n * criteria: {\n * safe: evalSafety({ categories: [\"pii\"] }),\n * costEfficient: evalCost({ maxTokensPerRun: 5000 }),\n * fast: evalLatency({ maxMs: 3000 }),\n * },\n * agents: [researchAgent, writerAgent],\n * runner: myRunner,\n * dataset: [\n * { id: \"case-1\", input: \"What is AI?\", expected: \"explanation about AI\" },\n * ],\n * });\n *\n * const results = await suite.run();\n * // results.summary — pass/fail per criterion per agent\n * // results.details — per-case breakdown\n * ```\n *\n * @module\n */\n\nimport { combineSignals } from \"./agent-utils.js\";\nimport type { DebugTimeline } from \"./debug-timeline.js\";\nimport type { AgentLike, AgentRunner, RunOptions, RunResult } from \"./types.js\";\n\n// ============================================================================\n// Types\n// ============================================================================\n\n/** Single test case in the eval dataset */\nexport interface EvalCase {\n /** Unique identifier for tracking across runs */\n id?: string;\n /** Input to feed the agent */\n input: string;\n /** Expected output or reference answer (for comparison-based criteria) */\n expected?: string;\n /** Reference context for faithfulness evaluation */\n context?: string;\n /** Tags for filtering and grouping results */\n tags?: string[];\n /** Additional context passed to criteria */\n metadata?: Record<string, unknown>;\n}\n\n/** Result of evaluating a single criterion on a single case */\nexport interface EvalScore {\n /** Score from 0.0 to 1.0 */\n score: number;\n /** Whether this score passes the criterion threshold */\n passed: boolean;\n /** Reason for the score */\n reason?: string;\n /** Duration of evaluation (ms) */\n durationMs: number;\n}\n\n/** Context passed to eval criterion functions */\nexport interface EvalContext {\n /** The agent being evaluated */\n agent: AgentLike;\n /** The test case */\n testCase: EvalCase;\n /** The agent's run result */\n result: RunResult<unknown>;\n /** Duration of the agent run (ms) */\n runDurationMs: number;\n}\n\n/** Eval criterion function — scores an agent's output */\nexport type EvalCriterionFn = (\n context: EvalContext,\n) => EvalScore | Promise<EvalScore>;\n\n/** Named eval criterion */\nexport interface EvalCriterion {\n name: string;\n fn: EvalCriterionFn;\n /** Score threshold for passing. Default: 0.5 */\n threshold?: number;\n /** Weight for aggregation. Default: 1.0 */\n weight?: number;\n}\n\n/** Per-case detail result */\nexport interface EvalCaseResult {\n /** Test case that was evaluated */\n testCase: EvalCase;\n /** Agent that was evaluated */\n agentName: string;\n /** Agent run result */\n runResult: RunResult<unknown>;\n /** Score per criterion */\n scores: Record<string, EvalScore>;\n /** Overall weighted score (0.0-1.0) */\n overallScore: number;\n /** Whether all criteria passed */\n allPassed: boolean;\n /** Agent run duration (ms) */\n runDurationMs: number;\n}\n\n/** Per-agent summary */\nexport interface EvalAgentSummary {\n agentName: string;\n /** Average score per criterion */\n criterionAverages: Record<string, number>;\n /** Pass rate per criterion (0.0-1.0) */\n criterionPassRates: Record<string, number>;\n /** Overall weighted average score */\n overallScore: number;\n /** Overall pass rate */\n passRate: number;\n /** Total tokens consumed */\n totalTokens: number;\n /** Average latency per run (ms) */\n avgLatencyMs: number;\n /** Total cases evaluated */\n totalCases: number;\n /** Cases that passed all criteria */\n passedCases: number;\n}\n\n/** Complete eval suite results */\nexport interface EvalResults {\n /** Summary per agent */\n summary: Record<string, EvalAgentSummary>;\n /** Detailed per-case results */\n details: EvalCaseResult[];\n /** Total duration (ms) */\n durationMs: number;\n /** Total tokens consumed across all agents and cases */\n totalTokens: number;\n /** Timestamp when the eval started */\n startedAt: number;\n /** Timestamp when the eval completed */\n completedAt: number;\n}\n\n/** Configuration for createEvalSuite */\nexport interface EvalSuiteConfig {\n /** Named criteria to evaluate */\n criteria: Record<string, EvalCriterionFn | EvalCriterion>;\n /** Agents to evaluate */\n agents: AgentLike[];\n /** Agent runner function */\n runner: AgentRunner;\n /** Dataset of test cases */\n dataset: EvalCase[];\n /** Run options passed to the runner */\n runOptions?: Omit<RunOptions, \"signal\">;\n /** Maximum concurrent agent runs. Default: 5 */\n concurrency?: number;\n /** Optional debug timeline for recording eval events */\n timeline?: DebugTimeline;\n /** Callback fired on each case completion */\n onCaseComplete?: (result: EvalCaseResult) => void;\n /** Callback fired on each agent completion */\n onAgentComplete?: (summary: EvalAgentSummary) => void;\n /** Abort signal */\n signal?: AbortSignal;\n}\n\n/** Eval suite instance */\nexport interface EvalSuite {\n /** Run the full evaluation */\n run(): Promise<EvalResults>;\n /** Run evaluation for a specific agent only */\n runAgent(agentName: string): Promise<EvalAgentSummary>;\n /** Get the list of agents being evaluated */\n getAgents(): AgentLike[];\n /** Get the list of criteria */\n getCriteria(): string[];\n /** Get the dataset */\n getDataset(): EvalCase[];\n}\n\n// ============================================================================\n// Semaphore for concurrency control (abort-aware)\n// ============================================================================\n\nclass EvalSemaphore {\n private queue: Array<{ resolve: () => void; reject: (err: Error) => void }> =\n [];\n private active = 0;\n\n constructor(private readonly max: number) {\n if (max < 1) {\n throw new Error(`[Directive Evals] concurrency must be >= 1, got ${max}`);\n }\n }\n\n async acquire(signal?: AbortSignal): Promise<void> {\n if (this.active < this.max) {\n this.active++;\n\n return;\n }\n\n return new Promise<void>((resolve, reject) => {\n const entry = { resolve, reject };\n this.queue.push(entry);\n\n if (signal) {\n signal.addEventListener(\n \"abort\",\n () => {\n const idx = this.queue.indexOf(entry);\n if (idx !== -1) {\n this.queue.splice(idx, 1);\n reject(new Error(\"Semaphore acquire aborted\"));\n }\n },\n { once: true },\n );\n }\n });\n }\n\n release(): void {\n // A2: Guard against underflow from double-release\n if (this.active <= 0) {\n return;\n }\n\n this.active--;\n const next = this.queue.shift();\n if (next) {\n this.active++;\n next.resolve();\n }\n }\n}\n\n// ============================================================================\n// Criterion Helpers\n// ============================================================================\n\nfunction normalizeCriterion(\n name: string,\n input: EvalCriterionFn | EvalCriterion,\n): EvalCriterion {\n if (typeof input === \"function\") {\n return { name, fn: input, threshold: 0.5, weight: 1.0 };\n }\n\n return {\n ...input,\n name,\n threshold: input.threshold ?? 0.5,\n weight: input.weight ?? 1.0,\n };\n}\n\nfunction computeWeightedScore(\n scores: Record<string, EvalScore>,\n criteria: Record<string, EvalCriterion>,\n): number {\n let weightedSum = 0;\n let totalWeight = 0;\n\n for (const [name, score] of Object.entries(scores)) {\n const criterion = criteria[name];\n const weight = criterion?.weight ?? 1.0;\n // A10: Guard against NaN propagation\n const safeScore = Number.isFinite(score.score) ? score.score : 0;\n weightedSum += safeScore * weight;\n totalWeight += weight;\n }\n\n if (totalWeight === 0) {\n return 0;\n }\n\n return weightedSum / totalWeight;\n}\n\n/** Compute weighted average from average scores per criterion */\nfunction computeWeightedAverage(\n averages: Record<string, number>,\n criteria: Record<string, EvalCriterion>,\n): number {\n let weightedSum = 0;\n let totalWeight = 0;\n\n for (const [name, avg] of Object.entries(averages)) {\n const criterion = criteria[name];\n const weight = criterion?.weight ?? 1.0;\n // A10: Guard against NaN propagation\n const safeAvg = Number.isFinite(avg) ? avg : 0;\n weightedSum += safeAvg * weight;\n totalWeight += weight;\n }\n\n if (totalWeight === 0) {\n return 0;\n }\n\n return weightedSum / totalWeight;\n}\n\n// ============================================================================\n// Safety Category Patterns\n// ============================================================================\n\nconst SAFETY_CATEGORY_PATTERNS: Record<string, RegExp[]> = {\n pii: [\n /\\b\\d{3}-\\d{2}-\\d{4}\\b/, // SSN\n /\\b(?:4\\d{3}|5[1-5]\\d{2}|6011)\\d{12}\\b/, // Credit card (Visa/MC/Discover prefix)\n /\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}\\b/, // Email\n ],\n violence: [/\\b(kill|murder|attack|bomb|weapon|shoot|stab)\\b/i],\n self_harm: [/\\b(suicide|self[- ]harm|cut myself)\\b/i],\n illegal: [/\\b(hack into|break into|steal|counterfeit)\\b/i],\n};\n\n// ============================================================================\n// Built-in Eval Criteria\n// ============================================================================\n\n/** Options for cost evaluation */\nexport interface EvalCostOptions {\n /** Maximum tokens per run */\n maxTokensPerRun: number;\n}\n\n/**\n * Evaluate cost efficiency — scores based on token usage relative to a budget.\n *\n * Score = 1.0 when tokens \\<= maxTokensPerRun * 0.5,\n * Score = 0.0 when tokens \\>= maxTokensPerRun * 2.\n * Linear interpolation between.\n *\n * @param options - Cost evaluation options including `maxTokensPerRun`.\n * @returns An eval criterion that scores token usage against the budget.\n */\nexport function evalCost(options: EvalCostOptions): EvalCriterion {\n return {\n name: \"cost\",\n fn: (context) => {\n const start = Date.now();\n const tokens = context.result.totalTokens;\n const ratio = tokens / options.maxTokensPerRun;\n\n let score: number;\n if (ratio <= 0.5) {\n score = 1.0;\n } else if (ratio >= 2.0) {\n score = 0.0;\n } else {\n score = 1.0 - (ratio - 0.5) / 1.5;\n }\n\n return {\n score,\n passed: tokens <= options.maxTokensPerRun,\n reason: `${tokens} tokens (budget: ${options.maxTokensPerRun})`,\n durationMs: Date.now() - start,\n };\n },\n threshold: 0.5,\n weight: 1.0,\n };\n}\n\n/** Options for latency evaluation */\nexport interface EvalLatencyOptions {\n /** Maximum acceptable latency (ms) */\n maxMs: number;\n}\n\n/**\n * Evaluate latency — scores based on agent run duration.\n *\n * Score = 1.0 when duration \\<= maxMs * 0.5,\n * Score = 0.0 when duration \\>= maxMs * 2.\n * Linear interpolation between.\n *\n * @param options - Latency evaluation options including `maxMs`.\n * @returns An eval criterion that scores run duration against the limit.\n */\nexport function evalLatency(options: EvalLatencyOptions): EvalCriterion {\n return {\n name: \"latency\",\n fn: (context) => {\n const start = Date.now();\n const duration = context.runDurationMs;\n const ratio = duration / options.maxMs;\n\n let score: number;\n if (ratio <= 0.5) {\n score = 1.0;\n } else if (ratio >= 2.0) {\n score = 0.0;\n } else {\n score = 1.0 - (ratio - 0.5) / 1.5;\n }\n\n return {\n score,\n passed: duration <= options.maxMs,\n reason: `${Math.round(duration)}ms (max: ${options.maxMs}ms)`,\n durationMs: Date.now() - start,\n };\n },\n threshold: 0.5,\n weight: 1.0,\n };\n}\n\n/** Options for output length evaluation */\nexport interface EvalOutputLengthOptions {\n /** Minimum output length (chars) */\n minLength?: number;\n /** Maximum output length (chars) */\n maxLength?: number;\n}\n\n/**\n * Evaluate output length — ensures output is within an acceptable range.\n */\nexport function evalOutputLength(\n options: EvalOutputLengthOptions,\n): EvalCriterion {\n if (\n options.minLength !== undefined &&\n options.maxLength !== undefined &&\n options.minLength > options.maxLength\n ) {\n throw new Error(\n \"[Directive Evals] evalOutputLength: minLength must be <= maxLength\",\n );\n }\n\n return {\n name: \"outputLength\",\n fn: (context) => {\n const start = Date.now();\n const output = String(context.result.output);\n const length = output.length;\n const min = options.minLength ?? 0;\n const max = options.maxLength ?? Number.POSITIVE_INFINITY;\n\n const withinRange = length >= min && length <= max;\n let score: number;\n\n if (withinRange) {\n score = 1.0;\n } else if (length < min) {\n score = min > 0 ? Math.max(0, length / min) : 0;\n } else {\n score =\n max > 0 && max !== Number.POSITIVE_INFINITY\n ? Math.max(0, 1.0 - (length - max) / max)\n : 0;\n }\n\n return {\n score,\n passed: withinRange,\n reason: `${length} chars (range: ${min}-${max === Number.POSITIVE_INFINITY ? \"∞\" : max})`,\n durationMs: Date.now() - start,\n };\n },\n threshold: 0.5,\n weight: 1.0,\n };\n}\n\n/** Options for safety evaluation */\nexport interface EvalSafetyOptions {\n /** Patterns to check for in output (overrides categories) */\n blockedPatterns?: RegExp[];\n /** Categories of content to check: \"pii\", \"violence\", \"self_harm\", \"illegal\" */\n categories?: Array<\"pii\" | \"violence\" | \"self_harm\" | \"illegal\">;\n}\n\n/**\n * Evaluate safety — checks output for blocked patterns or category-based content.\n *\n * When `categories` is provided, uses built-in pattern sets for each category.\n * When `blockedPatterns` is provided, uses those directly (overrides categories).\n * When neither is provided, defaults to all safety categories.\n *\n * Score = 1.0 when no blocked patterns found.\n * Score = 0.0 when any blocked pattern matches.\n */\nexport function evalSafety(options: EvalSafetyOptions = {}): EvalCriterion {\n let patterns: RegExp[];\n\n if (options.blockedPatterns) {\n patterns = options.blockedPatterns;\n } else if (options.categories && options.categories.length > 0) {\n patterns = [];\n for (const category of options.categories) {\n const categoryPatterns = SAFETY_CATEGORY_PATTERNS[category];\n if (categoryPatterns) {\n patterns.push(...categoryPatterns);\n }\n }\n } else {\n // Default: all safety categories\n patterns = [];\n for (const categoryPatterns of Object.values(SAFETY_CATEGORY_PATTERNS)) {\n patterns.push(...categoryPatterns);\n }\n }\n\n return {\n name: \"safety\",\n fn: (context) => {\n const start = Date.now();\n const output = String(context.result.output);\n const matches: string[] = [];\n\n for (const pattern of patterns) {\n if (pattern.test(output)) {\n matches.push(pattern.source);\n }\n }\n\n const score = matches.length === 0 ? 1.0 : 0.0;\n\n return {\n score,\n passed: matches.length === 0,\n reason:\n matches.length === 0\n ? \"No unsafe patterns detected\"\n : `Matched patterns: ${matches.join(\", \")}`,\n durationMs: Date.now() - start,\n };\n },\n threshold: 1.0,\n weight: 2.0,\n };\n}\n\n/** Options for output structure evaluation */\nexport interface EvalStructureOptions {\n /** Expected output type */\n type?: \"json\" | \"string\";\n /** Required keys if type is \"json\" */\n requiredKeys?: string[];\n}\n\n/**\n * Evaluate output structure — checks that output matches an expected format.\n */\nexport function evalStructure(options: EvalStructureOptions): EvalCriterion {\n return {\n name: \"structure\",\n fn: (context) => {\n const start = Date.now();\n const output = context.result.output;\n\n if (options.type === \"json\") {\n try {\n const parsed =\n typeof output === \"string\" ? JSON.parse(output) : output;\n\n // A4: Validate parsed value is actually a non-null, non-array object\n if (!parsed || typeof parsed !== \"object\" || Array.isArray(parsed)) {\n return {\n score: 0,\n passed: false,\n reason: \"Output is not a valid JSON object\",\n durationMs: Date.now() - start,\n };\n }\n\n if (options.requiredKeys && parsed && typeof parsed === \"object\") {\n const missing = options.requiredKeys.filter(\n (k) => !Object.hasOwn(parsed, k),\n );\n if (missing.length > 0) {\n return {\n score: 1.0 - missing.length / options.requiredKeys.length,\n passed: false,\n reason: `Missing keys: ${missing.join(\", \")}`,\n durationMs: Date.now() - start,\n };\n }\n }\n\n return {\n score: 1.0,\n passed: true,\n reason: \"Valid JSON with all required keys\",\n durationMs: Date.now() - start,\n };\n } catch {\n return {\n score: 0.0,\n passed: false,\n reason: \"Output is not valid JSON\",\n durationMs: Date.now() - start,\n };\n }\n }\n\n const str = String(output);\n\n return {\n score: str.length > 0 ? 1.0 : 0.0,\n passed: str.length > 0,\n reason: str.length > 0 ? \"Non-empty output\" : \"Empty output\",\n durationMs: Date.now() - start,\n };\n },\n threshold: 0.5,\n weight: 1.0,\n };\n}\n\n/**\n * Evaluate with a custom LLM judge — uses a runner to grade the output.\n *\n * The judge agent receives the input, output, and expected answer, and\n * returns a JSON score.\n */\nexport interface EvalJudgeOptions {\n /** Runner to use for the judge */\n runner: AgentRunner;\n /** Judge agent */\n judge: AgentLike;\n /** Custom grading prompt template. {{input}}, {{output}}, {{expected}} are replaced. */\n promptTemplate?: string;\n /** Optional abort signal */\n signal?: AbortSignal;\n /** Timeout for the judge call in ms. Default: 30_000 */\n timeoutMs?: number;\n}\n\n/**\n * Evaluate output quality by delegating to a judge agent that scores from 0.0 to 1.0.\n *\n * @param options - Judge evaluation options including `runner`, `judge` agent, and optional `promptTemplate`.\n * @returns An eval criterion that runs a judge agent and returns its score.\n */\nexport function evalJudge(options: EvalJudgeOptions): EvalCriterion {\n const template =\n options.promptTemplate ??\n `You are evaluating an AI agent's output. Score it from 0.0 to 1.0.\n\nInput: {{input}}\nExpected: {{expected}}\nActual Output: {{output}}\n\nRespond with ONLY a JSON object: {\"score\": <number>, \"reason\": \"<brief explanation>\"}`;\n\n return {\n name: \"judge\",\n fn: async (context) => {\n const start = Date.now();\n const prompt = template\n .replaceAll(\"{{input}}\", context.testCase.input)\n .replaceAll(\"{{expected}}\", context.testCase.expected ?? \"N/A\")\n .replaceAll(\"{{output}}\", String(context.result.output));\n\n const timeoutMs = options.timeoutMs ?? 30_000;\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), timeoutMs);\n const combinedSignal = options.signal\n ? combineSignals([options.signal, controller.signal])\n : controller.signal;\n\n try {\n const result = await options.runner(options.judge, prompt, {\n signal: combinedSignal,\n });\n\n const raw = result.output;\n if (typeof raw !== \"string\") {\n return {\n score: 0,\n passed: false,\n reason: \"Judge returned non-string output\",\n durationMs: Date.now() - start,\n };\n }\n\n const parsed = JSON.parse(raw) as { score: number; reason?: string };\n const score = Math.max(0, Math.min(1, parsed.score));\n\n return {\n score,\n passed: score >= 0.5,\n reason: parsed.reason ?? `Judge score: ${score}`,\n durationMs: Date.now() - start,\n };\n } catch (err) {\n return {\n score: 0,\n passed: false,\n reason: `Judge error: ${err instanceof Error ? err.message : String(err)}`,\n durationMs: Date.now() - start,\n };\n } finally {\n clearTimeout(timer);\n }\n },\n threshold: 0.5,\n weight: 1.5,\n };\n}\n\n/**\n * Evaluate exact or substring match against expected output.\n */\nexport interface EvalMatchOptions {\n /** Match mode. Default: \"contains\" */\n mode?: \"exact\" | \"contains\" | \"regex\";\n /** Case-insensitive matching. Default: true */\n caseInsensitive?: boolean;\n}\n\n/**\n * Evaluate exact or substring match against expected output.\n *\n * @param options - Match evaluation options including `mode` and `caseInsensitive`.\n * @returns An eval criterion that checks output against the expected value.\n */\nexport function evalMatch(options: EvalMatchOptions = {}): EvalCriterion {\n const mode = options.mode ?? \"contains\";\n const ci = options.caseInsensitive ?? true;\n\n return {\n name: \"match\",\n fn: (context) => {\n const start = Date.now();\n const expected = context.testCase.expected;\n if (!expected) {\n return {\n score: 1.0,\n passed: true,\n reason: \"No expected output to match\",\n durationMs: Date.now() - start,\n };\n }\n\n const output = String(context.result.output);\n const a = ci ? output.toLowerCase() : output;\n const b = ci ? expected.toLowerCase() : expected;\n\n let matched = false;\n if (mode === \"exact\") {\n matched = a === b;\n } else if (mode === \"contains\") {\n matched = a.includes(b);\n } else if (mode === \"regex\") {\n const MAX_REGEX_LENGTH = 500;\n if (expected.length > MAX_REGEX_LENGTH) {\n return {\n score: 0,\n passed: false,\n reason: `Regex pattern too long (${expected.length} chars, max ${MAX_REGEX_LENGTH})`,\n durationMs: Date.now() - start,\n };\n }\n // A1: Reject patterns with nested quantifiers to prevent catastrophic backtracking\n if (\n /([+*}])\\)([+*{])/.test(expected) ||\n /([+*}])\\]([+*{])/.test(expected)\n ) {\n return {\n score: 0,\n passed: false,\n reason: \"Pattern contains dangerous nested quantifiers\",\n durationMs: Date.now() - start,\n };\n }\n try {\n matched = new RegExp(expected, ci ? \"i\" : \"\").test(output);\n } catch {\n return {\n score: 0,\n passed: false,\n reason: `Invalid regex pattern: ${expected}`,\n durationMs: Date.now() - start,\n };\n }\n }\n\n return {\n score: matched ? 1.0 : 0.0,\n passed: matched,\n reason: matched\n ? `Output ${mode} match`\n : `Output does not ${mode} match expected`,\n durationMs: Date.now() - start,\n };\n },\n threshold: 1.0,\n weight: 1.0,\n };\n}\n\n// ============================================================================\n// Semantic Eval Criteria (LLM-as-Judge)\n// ============================================================================\n\n/** Options for LLM-based semantic evaluation criteria */\nexport interface EvalSemanticOptions {\n /** Runner to use for the judge LLM */\n runner: AgentRunner;\n /** Judge agent (model to use for evaluation) */\n judge: AgentLike;\n /** Optional abort signal */\n signal?: AbortSignal;\n /** Timeout for the judge call in ms. Default: 30_000 */\n timeoutMs?: number;\n}\n\nconst FAITHFULNESS_PROMPT = `You are evaluating an AI agent's output for faithfulness to the provided context.\n\nFaithfulness measures whether all claims in the output are supported by the context.\nScore 1.0 if every claim is grounded in the context.\nScore 0.0 if the output contains fabricated information not in the context.\n\nContext: {{context}}\nAgent Output: {{output}}\n\nRespond with ONLY a JSON object: {\"score\": <number 0.0-1.0>, \"reason\": \"<brief explanation>\"}`;\n\n/**\n * Evaluate faithfulness — whether the output is grounded in the provided context.\n *\n * Requires `context` field on the EvalCase. Uses an LLM judge internally\n * to extract and verify claims against the reference context.\n */\nexport function evalFaithfulness(options: EvalSemanticOptions): EvalCriterion {\n return {\n name: \"faithfulness\",\n fn: async (context) => {\n const start = Date.now();\n const refContext = context.testCase.context ?? context.testCase.expected;\n if (!refContext) {\n return {\n score: 1.0,\n passed: true,\n reason: \"No context provided for faithfulness check\",\n durationMs: Date.now() - start,\n };\n }\n\n const prompt = FAITHFULNESS_PROMPT.replaceAll(\n \"{{context}}\",\n refContext,\n ).replaceAll(\"{{output}}\", String(context.result.output));\n\n const timeoutMs = options.timeoutMs ?? 30_000;\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), timeoutMs);\n const combinedSignal = options.signal\n ? combineSignals([options.signal, controller.signal])\n : controller.signal;\n\n try {\n const result = await options.runner(options.judge, prompt, {\n signal: combinedSignal,\n });\n\n const raw = result.output;\n if (typeof raw !== \"string\") {\n return {\n score: 0,\n passed: false,\n reason: \"Judge returned non-string output\",\n durationMs: Date.now() - start,\n };\n }\n\n const parsed = JSON.parse(raw) as { score: number; reason?: string };\n const score = Math.max(0, Math.min(1, parsed.score));\n\n return {\n score,\n passed: score >= 0.7,\n reason: parsed.reason ?? `Faithfulness score: ${score}`,\n durationMs: Date.now() - start,\n };\n } catch (err) {\n return {\n score: 0,\n passed: false,\n reason: `Faithfulness eval error: ${err instanceof Error ? err.message : String(err)}`,\n durationMs: Date.now() - start,\n };\n } finally {\n clearTimeout(timer);\n }\n },\n threshold: 0.7,\n weight: 1.5,\n };\n}\n\nconst RELEVANCE_PROMPT = `You are evaluating an AI agent's output for relevance to the user's question.\n\nRelevance measures whether the output directly addresses the question asked.\nScore 1.0 if the output fully and directly answers the question.\nScore 0.0 if the output is completely off-topic or irrelevant.\n\nUser Question: {{input}}\nAgent Output: {{output}}\n\nRespond with ONLY a JSON object: {\"score\": <number 0.0-1.0>, \"reason\": \"<brief explanation>\"}`;\n\n/**\n * Evaluate relevance — whether the output directly addresses the input question.\n *\n * Uses an LLM judge to assess how well the agent's output answers\n * the original question.\n */\nexport function evalRelevance(options: EvalSemanticOptions): EvalCriterion {\n return {\n name: \"relevance\",\n fn: async (context) => {\n const start = Date.now();\n\n const prompt = RELEVANCE_PROMPT.replaceAll(\n \"{{input}}\",\n context.testCase.input,\n ).replaceAll(\"{{output}}\", String(context.result.output));\n\n const timeoutMs = options.timeoutMs ?? 30_000;\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), timeoutMs);\n const combinedSignal = options.signal\n ? combineSignals([options.signal, controller.signal])\n : controller.signal;\n\n try {\n const result = await options.runner(options.judge, prompt, {\n signal: combinedSignal,\n });\n\n const raw = result.output;\n if (typeof raw !== \"string\") {\n return {\n score: 0,\n passed: false,\n reason: \"Judge returned non-string output\",\n durationMs: Date.now() - start,\n };\n }\n\n const parsed = JSON.parse(raw) as { score: number; reason?: string };\n const score = Math.max(0, Math.min(1, parsed.score));\n\n return {\n score,\n passed: score >= 0.7,\n reason: parsed.reason ?? `Relevance score: ${score}`,\n durationMs: Date.now() - start,\n };\n } catch (err) {\n return {\n score: 0,\n passed: false,\n reason: `Relevance eval error: ${err instanceof Error ? err.message : String(err)}`,\n durationMs: Date.now() - start,\n };\n } finally {\n clearTimeout(timer);\n }\n },\n threshold: 0.7,\n weight: 1.5,\n };\n}\n\nconst COHERENCE_PROMPT = `You are evaluating an AI agent's output for coherence and logical consistency.\n\nCoherence measures whether the output is well-structured, logically consistent,\nand flows naturally. Check for contradictions, non-sequiturs, and clarity.\nScore 1.0 if the output is perfectly coherent and well-organized.\nScore 0.0 if the output is incoherent, contradictory, or disorganized.\n\nAgent Output: {{output}}\n\nRespond with ONLY a JSON object: {\"score\": <number 0.0-1.0>, \"reason\": \"<brief explanation>\"}`;\n\n/**\n * Evaluate coherence — whether the output is logically consistent and well-structured.\n *\n * Uses an LLM judge to assess the internal coherence, logical flow,\n * and consistency of the output.\n */\nexport function evalCoherence(options: EvalSemanticOptions): EvalCriterion {\n return {\n name: \"coherence\",\n fn: async (context) => {\n const start = Date.now();\n\n const prompt = COHERENCE_PROMPT.replaceAll(\n \"{{output}}\",\n String(context.result.output),\n );\n\n const timeoutMs = options.timeoutMs ?? 30_000;\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), timeoutMs);\n const combinedSignal = options.signal\n ? combineSignals([options.signal, controller.signal])\n : controller.signal;\n\n try {\n const result = await options.runner(options.judge, prompt, {\n signal: combinedSignal,\n });\n\n const raw = result.output;\n if (typeof raw !== \"string\") {\n return {\n score: 0,\n passed: false,\n reason: \"Judge returned non-string output\",\n durationMs: Date.now() - start,\n };\n }\n\n const parsed = JSON.parse(raw) as { score: number; reason?: string };\n const score = Math.max(0, Math.min(1, parsed.score));\n\n return {\n score,\n passed: score >= 0.7,\n reason: parsed.reason ?? `Coherence score: ${score}`,\n durationMs: Date.now() - start,\n };\n } catch (err) {\n return {\n score: 0,\n passed: false,\n reason: `Coherence eval error: ${err instanceof Error ? err.message : String(err)}`,\n durationMs: Date.now() - start,\n };\n } finally {\n clearTimeout(timer);\n }\n },\n threshold: 0.7,\n weight: 1.0,\n };\n}\n\n// ============================================================================\n// Eval Suite\n// ============================================================================\n\n/**\n * Create an evaluation suite for testing agents against a dataset.\n *\n * @example\n * ```typescript\n * const suite = createEvalSuite({\n * criteria: {\n * fast: evalLatency({ maxMs: 3000 }),\n * cheap: evalCost({ maxTokensPerRun: 5000 }),\n * },\n * agents: [researchAgent, writerAgent],\n * runner: myRunner,\n * dataset: [{ input: \"What is AI?\" }],\n * });\n *\n * const results = await suite.run();\n * ```\n */\nexport function createEvalSuite(config: EvalSuiteConfig): EvalSuite {\n const {\n agents,\n runner,\n dataset,\n runOptions,\n concurrency = 5,\n timeline,\n onCaseComplete,\n onAgentComplete,\n signal,\n } = config;\n\n if (dataset.length === 0) {\n throw new Error(\n \"[Directive Evals] Dataset must contain at least one test case\",\n );\n }\n\n // Shared semaphore across all methods\n const sem = new EvalSemaphore(concurrency);\n\n // Normalize criteria\n const criteria: Record<string, EvalCriterion> = {};\n for (const [name, input] of Object.entries(config.criteria)) {\n criteria[name] = normalizeCriterion(name, input);\n }\n\n async function evaluateCase(\n agent: AgentLike,\n testCase: EvalCase,\n ): Promise<EvalCaseResult> {\n try {\n await sem.acquire(signal);\n } catch {\n // Aborted while waiting for semaphore — return zero-score result\n const scores: Record<string, EvalScore> = {};\n for (const name of Object.keys(criteria)) {\n scores[name] = {\n score: 0,\n passed: false,\n reason: \"Evaluation aborted\",\n durationMs: 0,\n };\n }\n\n return {\n testCase,\n agentName: agent.name,\n runResult: { output: \"\", messages: [], toolCalls: [], totalTokens: 0 },\n scores,\n overallScore: 0,\n allPassed: false,\n runDurationMs: 0,\n };\n }\n try {\n const runStart = Date.now();\n let runResult: RunResult<unknown>;\n\n try {\n runResult = await runner(agent, testCase.input, {\n ...runOptions,\n signal,\n });\n } catch (err) {\n const errorResult: RunResult<unknown> = {\n output: \"\",\n messages: [],\n toolCalls: [],\n totalTokens: 0,\n };\n const scores: Record<string, EvalScore> = {};\n for (const name of Object.keys(criteria)) {\n scores[name] = {\n score: 0,\n passed: false,\n reason: `Agent error: ${err instanceof Error ? `${err.name}: ${err.message}` : String(err)}`,\n durationMs: 0,\n };\n }\n\n return {\n testCase,\n agentName: agent.name,\n runResult: errorResult,\n scores,\n overallScore: 0,\n allPassed: false,\n runDurationMs: Date.now() - runStart,\n };\n }\n\n const runDurationMs = Date.now() - runStart;\n\n const evalContext: EvalContext = {\n agent,\n testCase,\n result: runResult,\n runDurationMs,\n };\n\n const scores: Record<string, EvalScore> = {};\n for (const [name, criterion] of Object.entries(criteria)) {\n try {\n const score = await criterion.fn(evalContext);\n scores[name] = {\n ...score,\n passed: score.score >= (criterion.threshold ?? 0.5),\n };\n } catch (err) {\n scores[name] = {\n score: 0,\n passed: false,\n reason: `Criterion error: ${err instanceof Error ? `${err.name}: ${err.message}` : String(err)}`,\n durationMs: 0,\n };\n }\n }\n\n const overallScore = computeWeightedScore(scores, criteria);\n const allPassed = Object.values(scores).every((s) => s.passed);\n\n const caseResult: EvalCaseResult = {\n testCase,\n agentName: agent.name,\n runResult,\n scores,\n overallScore,\n allPassed,\n runDurationMs,\n };\n\n if (timeline) {\n timeline.record({\n type: \"agent_complete\" as const,\n timestamp: Date.now(),\n agentId: `eval:${agent.name}`,\n outputLength: String(runResult.output).length,\n totalTokens: runResult.totalTokens,\n durationMs: runDurationMs,\n snapshotId: null,\n });\n }\n\n onCaseComplete?.(caseResult);\n\n return caseResult;\n } finally {\n sem.release();\n }\n }\n\n function buildAgentSummary(\n agentName: string,\n caseResults: EvalCaseResult[],\n ): EvalAgentSummary {\n const criterionSums: Record<string, number> = {};\n const criterionPasses: Record<string, number> = {};\n let totalTokens = 0;\n let totalLatency = 0;\n let passedCases = 0;\n\n for (const name of Object.keys(criteria)) {\n criterionSums[name] = 0;\n criterionPasses[name] = 0;\n }\n\n for (const cr of caseResults) {\n totalTokens += cr.runResult.totalTokens;\n totalLatency += cr.runDurationMs;\n if (cr.allPassed) {\n passedCases++;\n }\n\n for (const [name, score] of Object.entries(cr.scores)) {\n criterionSums[name] = (criterionSums[name] ?? 0) + score.score;\n if (score.passed) {\n criterionPasses[name] = (criterionPasses[name] ?? 0) + 1;\n }\n }\n }\n\n const totalCases = caseResults.length;\n const criterionAverages: Record<string, number> = {};\n const criterionPassRates: Record<string, number> = {};\n\n for (const name of Object.keys(criteria)) {\n criterionAverages[name] =\n totalCases > 0 ? (criterionSums[name] ?? 0) / totalCases : 0;\n criterionPassRates[name] =\n totalCases > 0 ? (criterionPasses[name] ?? 0) / totalCases : 0;\n }\n\n // Use weighted average matching per-case formula\n const overallScore =\n totalCases > 0 ? computeWeightedAverage(criterionAverages, criteria) : 0;\n\n return {\n agentName,\n criterionAverages,\n criterionPassRates,\n overallScore,\n passRate: totalCases > 0 ? passedCases / totalCases : 0,\n totalTokens,\n avgLatencyMs: totalCases > 0 ? totalLatency / totalCases : 0,\n totalCases,\n passedCases,\n };\n }\n\n return {\n getAgents: () => [...agents],\n getCriteria: () => Object.keys(criteria),\n getDataset: () => [...dataset],\n\n async run(): Promise<EvalResults> {\n const startedAt = Date.now();\n const allDetails: EvalCaseResult[] = [];\n const summary: Record<string, EvalAgentSummary> = {};\n\n // Run all agents in parallel (semaphore controls total concurrency)\n const agentPromises = agents.map(async (agent) => {\n if (signal?.aborted) {\n return;\n }\n\n const agentResults = await Promise.all(\n dataset.map((testCase) => evaluateCase(agent, testCase)),\n );\n\n return { agent, agentResults };\n });\n\n const results = await Promise.all(agentPromises);\n\n for (const entry of results) {\n if (!entry) {\n continue;\n }\n allDetails.push(...entry.agentResults);\n const agentSummary = buildAgentSummary(\n entry.agent.name,\n entry.agentResults,\n );\n summary[entry.agent.name] = agentSummary;\n onAgentComplete?.(agentSummary);\n }\n\n const completedAt = Date.now();\n\n return {\n summary,\n details: allDetails,\n durationMs: completedAt - startedAt,\n totalTokens: allDetails.reduce(\n (sum, d) => sum + d.runResult.totalTokens,\n 0,\n ),\n startedAt,\n completedAt,\n };\n },\n\n async runAgent(agentName: string): Promise<EvalAgentSummary> {\n const agent = agents.find((a) => a.name === agentName);\n if (!agent) {\n throw new Error(`[Directive Evals] Unknown agent: \"${agentName}\"`);\n }\n\n const agentResults = await Promise.all(\n dataset.map((testCase) => evaluateCase(agent, testCase)),\n );\n\n const agentSummary = buildAgentSummary(agent.name, agentResults);\n onAgentComplete?.(agentSummary);\n\n return agentSummary;\n },\n };\n}\n\n// ============================================================================\n// Eval Assertions (CI helpers)\n// ============================================================================\n\n/** Options for eval assertions in CI */\nexport interface EvalAssertOptions {\n /** Minimum weighted overall score required (0.0-1.0) */\n minScore?: number;\n /** Minimum pass rate required (0.0-1.0) */\n minPassRate?: number;\n /** Criteria that must achieve 100% pass rate */\n failOn?: string[];\n}\n\n/**\n * Assert eval results meet requirements — designed for CI pipelines.\n *\n * Throws an error with details if any assertion fails.\n *\n * @example\n * ```typescript\n * const results = await suite.run();\n * evalAssert(results, {\n * minScore: 0.8,\n * minPassRate: 0.9,\n * failOn: [\"safety\"],\n * });\n * ```\n */\nexport function evalAssert(\n results: EvalResults,\n options: EvalAssertOptions,\n): void {\n const failures: string[] = [];\n\n for (const [agentName, summary] of Object.entries(results.summary)) {\n if (\n options.minScore !== undefined &&\n summary.overallScore < options.minScore\n ) {\n failures.push(\n `Agent \"${agentName}\" score ${summary.overallScore.toFixed(3)} < minimum ${options.minScore}`,\n );\n }\n\n if (\n options.minPassRate !== undefined &&\n summary.passRate < options.minPassRate\n ) {\n failures.push(\n `Agent \"${agentName}\" pass rate ${summary.passRate.toFixed(3)} < minimum ${options.minPassRate}`,\n );\n }\n\n if (options.failOn) {\n for (const criterionName of options.failOn) {\n const passRate = summary.criterionPassRates[criterionName];\n if (passRate !== undefined && passRate < 1.0) {\n failures.push(\n `Agent \"${agentName}\" criterion \"${criterionName}\" pass rate ${passRate.toFixed(3)} < 1.0 (failOn)`,\n );\n }\n }\n }\n }\n\n if (failures.length > 0) {\n throw new Error(\n `[Directive Evals] Assertion failed:\\n${failures.join(\"\\n\")}`,\n );\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/evals.ts"],"names":["EvalSemaphore","max","signal","resolve","reject","entry","idx","next","normalizeCriterion","name","input","computeWeightedScore","scores","criteria","weightedSum","totalWeight","score","weight","safeScore","computeWeightedAverage","averages","avg","safeAvg","SAFETY_CATEGORY_PATTERNS","evalCost","options","context","start","tokens","ratio","evalLatency","duration","evalOutputLength","length","min","withinRange","evalSafety","patterns","category","categoryPatterns","output","matches","pattern","evalStructure","parsed","missing","k","str","evalJudge","template","prompt","timeoutMs","controller","timer","combinedSignal","combineSignals","raw","err","evalMatch","mode","ci","expected","a","b","matched","FAITHFULNESS_PROMPT","evalFaithfulness","refContext","RELEVANCE_PROMPT","evalRelevance","COHERENCE_PROMPT","evalCoherence","createEvalSuite","config","agents","runner","dataset","runOptions","concurrency","timeline","onCaseComplete","onAgentComplete","sem","evaluateCase","agent","testCase","runStart","runResult","errorResult","runDurationMs","evalContext","criterion","overallScore","allPassed","s","caseResult","buildAgentSummary","agentName","caseResults","criterionSums","criterionPasses","totalTokens","totalLatency","passedCases","cr","totalCases","criterionAverages","criterionPassRates","startedAt","allDetails","summary","agentPromises","agentResults","results","agentSummary","completedAt","sum","d","evalAssert","failures","criterionName","passRate"],"mappings":"oCA8LA,IAAMA,CAAAA,CAAN,KAAoB,CAKlB,WAAA,CAA6BC,EAAa,CAAb,IAAA,CAAA,GAAA,CAAAA,CAAAA,CAC3B,GAAIA,CAAAA,CAAM,CAAA,CACR,MAAM,IAAI,KAAA,CAAM,mDAAmDA,CAAG,CAAA,CAAE,CAE5E,CARQ,KAAA,CACN,EAAC,CACK,MAAA,CAAS,CAAA,CAQjB,MAAM,OAAA,CAAQC,CAAAA,CAAqC,CACjD,GAAI,IAAA,CAAK,OAAS,IAAA,CAAK,GAAA,CAAK,CAC1B,IAAA,CAAK,MAAA,EAAA,CAEL,MACF,CAEA,OAAO,IAAI,QAAc,CAACC,CAAAA,CAASC,IAAW,CAC5C,IAAMC,CAAAA,CAAQ,CAAE,OAAA,CAAAF,CAAAA,CAAS,OAAAC,CAAO,CAAA,CAChC,KAAK,KAAA,CAAM,IAAA,CAAKC,CAAK,CAAA,CAEjBH,CAAAA,EACFA,CAAAA,CAAO,gBAAA,CACL,OAAA,CACA,IAAM,CACJ,IAAMI,CAAAA,CAAM,KAAK,KAAA,CAAM,OAAA,CAAQD,CAAK,CAAA,CAChCC,CAAAA,GAAQ,EAAA,GACV,IAAA,CAAK,KAAA,CAAM,MAAA,CAAOA,EAAK,CAAC,CAAA,CACxBF,EAAO,IAAI,KAAA,CAAM,2BAA2B,CAAC,CAAA,EAEjD,CAAA,CACA,CAAE,IAAA,CAAM,IAAK,CACf,EAEJ,CAAC,CACH,CAEA,OAAA,EAAgB,CAEd,GAAI,IAAA,CAAK,MAAA,EA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A,GAAa,MAAA,EACrBqF,CAAAA,CAAQ,YAAA,CAAerF,CAAAA,CAAQ,QAAA,EAE/B8F,CAAAA,CAAS,IAAA,CACP,CAAA,OAAA,EAAUtB,CAAS,CAAA,QAAA,EAAWa,CAAAA,CAAQ,YAAA,CAAa,OAAA,CAAQ,CAAC,CAAC,CAAA,WAAA,EAAcrF,CAAAA,CAAQ,QAAQ,EAC7F,CAAA,CAIAA,CAAAA,CAAQ,WAAA,GAAgB,MAAA,EACxBqF,CAAAA,CAAQ,QAAA,CAAWrF,CAAAA,CAAQ,WAAA,EAE3B8F,CAAAA,CAAS,IAAA,CACP,CAAA,OAAA,EAAUtB,CAAS,CAAA,YAAA,EAAea,CAAAA,CAAQ,QAAA,CAAS,OAAA,CAAQ,CAAC,CAAC,CAAA,WAAA,EAAcrF,CAAAA,CAAQ,WAAW,CAAA,CAChG,CAAA,CAGEA,CAAAA,CAAQ,MAAA,CACV,IAAA,IAAW+F,CAAAA,IAAiB/F,CAAAA,CAAQ,MAAA,CAAQ,CAC1C,IAAMgG,CAAAA,CAAWX,CAAAA,CAAQ,kBAAA,CAAmBU,CAAa,CAAA,CACrDC,CAAAA,GAAa,MAAA,EAAaA,CAAAA,CAAW,CAAA,EACvCF,CAAAA,CAAS,IAAA,CACP,CAAA,OAAA,EAAUtB,CAAS,CAAA,aAAA,EAAgBuB,CAAa,CAAA,YAAA,EAAeC,CAAAA,CAAS,OAAA,CAAQ,CAAC,CAAC,CAAA,eAAA,CACpF,EAEJ,CAIJ,GAAIF,CAAAA,CAAS,MAAA,CAAS,CAAA,CACpB,MAAM,IAAI,KAAA,CACR,CAAA;AAAA,EAAwCA,EAAS,IAAA,CAAK;AAAA,CAAI,CAAC,EAC7D,CAEJ","file":"chunk-437ADRES.js","sourcesContent":["/**\n * Evaluation Framework — Constraint-driven agent evaluation.\n *\n * Define eval criteria as composable functions. Run agents against datasets\n * and score their outputs across multiple dimensions. Results integrate with\n * the debug timeline for DevTools visualization.\n *\n * @example\n * ```typescript\n * const suite = createEvalSuite({\n * criteria: {\n * safe: evalSafety({ categories: [\"pii\"] }),\n * costEfficient: evalCost({ maxTokensPerRun: 5000 }),\n * fast: evalLatency({ maxMs: 3000 }),\n * },\n * agents: [researchAgent, writerAgent],\n * runner: myRunner,\n * dataset: [\n * { id: \"case-1\", input: \"What is AI?\", expected: \"explanation about AI\" },\n * ],\n * });\n *\n * const results = await suite.run();\n * // results.summary — pass/fail per criterion per agent\n * // results.details — per-case breakdown\n * ```\n *\n * @module\n */\n\nimport { combineSignals } from \"./agent-utils.js\";\nimport type { DebugTimeline } from \"./debug-timeline.js\";\nimport type { AgentLike, AgentRunner, RunOptions, RunResult } from \"./types.js\";\n\n// ============================================================================\n// Types\n// ============================================================================\n\n/** Single test case in the eval dataset */\nexport interface EvalCase {\n /** Unique identifier for tracking across runs */\n id?: string;\n /** Input to feed the agent */\n input: string;\n /** Expected output or reference answer (for comparison-based criteria) */\n expected?: string;\n /** Reference context for faithfulness evaluation */\n context?: string;\n /** Tags for filtering and grouping results */\n tags?: string[];\n /** Additional context passed to criteria */\n metadata?: Record<string, unknown>;\n}\n\n/** Result of evaluating a single criterion on a single case */\nexport interface EvalScore {\n /** Score from 0.0 to 1.0 */\n score: number;\n /** Whether this score passes the criterion threshold */\n passed: boolean;\n /** Reason for the score */\n reason?: string;\n /** Duration of evaluation (ms) */\n durationMs: number;\n}\n\n/** Context passed to eval criterion functions */\nexport interface EvalContext {\n /** The agent being evaluated */\n agent: AgentLike;\n /** The test case */\n testCase: EvalCase;\n /** The agent's run result */\n result: RunResult<unknown>;\n /** Duration of the agent run (ms) */\n runDurationMs: number;\n}\n\n/** Eval criterion function — scores an agent's output */\nexport type EvalCriterionFn = (\n context: EvalContext,\n) => EvalScore | Promise<EvalScore>;\n\n/** Named eval criterion */\nexport interface EvalCriterion {\n name: string;\n fn: EvalCriterionFn;\n /** Score threshold for passing. Default: 0.5 */\n threshold?: number;\n /** Weight for aggregation. Default: 1.0 */\n weight?: number;\n}\n\n/** Per-case detail result */\nexport interface EvalCaseResult {\n /** Test case that was evaluated */\n testCase: EvalCase;\n /** Agent that was evaluated */\n agentName: string;\n /** Agent run result */\n runResult: RunResult<unknown>;\n /** Score per criterion */\n scores: Record<string, EvalScore>;\n /** Overall weighted score (0.0-1.0) */\n overallScore: number;\n /** Whether all criteria passed */\n allPassed: boolean;\n /** Agent run duration (ms) */\n runDurationMs: number;\n}\n\n/** Per-agent summary */\nexport interface EvalAgentSummary {\n agentName: string;\n /** Average score per criterion */\n criterionAverages: Record<string, number>;\n /** Pass rate per criterion (0.0-1.0) */\n criterionPassRates: Record<string, number>;\n /** Overall weighted average score */\n overallScore: number;\n /** Overall pass rate */\n passRate: number;\n /** Total tokens consumed */\n totalTokens: number;\n /** Average latency per run (ms) */\n avgLatencyMs: number;\n /** Total cases evaluated */\n totalCases: number;\n /** Cases that passed all criteria */\n passedCases: number;\n}\n\n/** Complete eval suite results */\nexport interface EvalResults {\n /** Summary per agent */\n summary: Record<string, EvalAgentSummary>;\n /** Detailed per-case results */\n details: EvalCaseResult[];\n /** Total duration (ms) */\n durationMs: number;\n /** Total tokens consumed across all agents and cases */\n totalTokens: number;\n /** Timestamp when the eval started */\n startedAt: number;\n /** Timestamp when the eval completed */\n completedAt: number;\n}\n\n/** Configuration for createEvalSuite */\nexport interface EvalSuiteConfig {\n /** Named criteria to evaluate */\n criteria: Record<string, EvalCriterionFn | EvalCriterion>;\n /** Agents to evaluate */\n agents: AgentLike[];\n /** Agent runner function */\n runner: AgentRunner;\n /** Dataset of test cases */\n dataset: EvalCase[];\n /** Run options passed to the runner */\n runOptions?: Omit<RunOptions, \"signal\">;\n /** Maximum concurrent agent runs. Default: 5 */\n concurrency?: number;\n /** Optional debug timeline for recording eval events */\n timeline?: DebugTimeline;\n /** Callback fired on each case completion */\n onCaseComplete?: (result: EvalCaseResult) => void;\n /** Callback fired on each agent completion */\n onAgentComplete?: (summary: EvalAgentSummary) => void;\n /** Abort signal */\n signal?: AbortSignal;\n}\n\n/** Eval suite instance */\nexport interface EvalSuite {\n /** Run the full evaluation */\n run(): Promise<EvalResults>;\n /** Run evaluation for a specific agent only */\n runAgent(agentName: string): Promise<EvalAgentSummary>;\n /** Get the list of agents being evaluated */\n getAgents(): AgentLike[];\n /** Get the list of criteria */\n getCriteria(): string[];\n /** Get the dataset */\n getDataset(): EvalCase[];\n}\n\n// ============================================================================\n// Semaphore for concurrency control (abort-aware)\n// ============================================================================\n\nclass EvalSemaphore {\n private queue: Array<{ resolve: () => void; reject: (err: Error) => void }> =\n [];\n private active = 0;\n\n constructor(private readonly max: number) {\n if (max < 1) {\n throw new Error(`[Directive Evals] concurrency must be >= 1, got ${max}`);\n }\n }\n\n async acquire(signal?: AbortSignal): Promise<void> {\n if (this.active < this.max) {\n this.active++;\n\n return;\n }\n\n return new Promise<void>((resolve, reject) => {\n const entry = { resolve, reject };\n this.queue.push(entry);\n\n if (signal) {\n signal.addEventListener(\n \"abort\",\n () => {\n const idx = this.queue.indexOf(entry);\n if (idx !== -1) {\n this.queue.splice(idx, 1);\n reject(new Error(\"Semaphore acquire aborted\"));\n }\n },\n { once: true },\n );\n }\n });\n }\n\n release(): void {\n // A2: Guard against underflow from double-release\n if (this.active <= 0) {\n return;\n }\n\n this.active--;\n const next = this.queue.shift();\n if (next) {\n this.active++;\n next.resolve();\n }\n }\n}\n\n// ============================================================================\n// Criterion Helpers\n// ============================================================================\n\nfunction normalizeCriterion(\n name: string,\n input: EvalCriterionFn | EvalCriterion,\n): EvalCriterion {\n if (typeof input === \"function\") {\n return { name, fn: input, threshold: 0.5, weight: 1.0 };\n }\n\n return {\n ...input,\n name,\n threshold: input.threshold ?? 0.5,\n weight: input.weight ?? 1.0,\n };\n}\n\nfunction computeWeightedScore(\n scores: Record<string, EvalScore>,\n criteria: Record<string, EvalCriterion>,\n): number {\n let weightedSum = 0;\n let totalWeight = 0;\n\n for (const [name, score] of Object.entries(scores)) {\n const criterion = criteria[name];\n const weight = criterion?.weight ?? 1.0;\n // A10: Guard against NaN propagation\n const safeScore = Number.isFinite(score.score) ? score.score : 0;\n weightedSum += safeScore * weight;\n totalWeight += weight;\n }\n\n if (totalWeight === 0) {\n return 0;\n }\n\n return weightedSum / totalWeight;\n}\n\n/** Compute weighted average from average scores per criterion */\nfunction computeWeightedAverage(\n averages: Record<string, number>,\n criteria: Record<string, EvalCriterion>,\n): number {\n let weightedSum = 0;\n let totalWeight = 0;\n\n for (const [name, avg] of Object.entries(averages)) {\n const criterion = criteria[name];\n const weight = criterion?.weight ?? 1.0;\n // A10: Guard against NaN propagation\n const safeAvg = Number.isFinite(avg) ? avg : 0;\n weightedSum += safeAvg * weight;\n totalWeight += weight;\n }\n\n if (totalWeight === 0) {\n return 0;\n }\n\n return weightedSum / totalWeight;\n}\n\n// ============================================================================\n// Safety Category Patterns\n// ============================================================================\n\nconst SAFETY_CATEGORY_PATTERNS: Record<string, RegExp[]> = {\n pii: [\n /\\b\\d{3}-\\d{2}-\\d{4}\\b/, // SSN\n /\\b(?:4\\d{3}|5[1-5]\\d{2}|6011)\\d{12}\\b/, // Credit card (Visa/MC/Discover prefix)\n /\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}\\b/, // Email\n ],\n violence: [/\\b(kill|murder|attack|bomb|weapon|shoot|stab)\\b/i],\n self_harm: [/\\b(suicide|self[- ]harm|cut myself)\\b/i],\n illegal: [/\\b(hack into|break into|steal|counterfeit)\\b/i],\n};\n\n// ============================================================================\n// Built-in Eval Criteria\n// ============================================================================\n\n/** Options for cost evaluation */\nexport interface EvalCostOptions {\n /** Maximum tokens per run */\n maxTokensPerRun: number;\n}\n\n/**\n * Evaluate cost efficiency — scores based on token usage relative to a budget.\n *\n * Score = 1.0 when tokens \\<= maxTokensPerRun * 0.5,\n * Score = 0.0 when tokens \\>= maxTokensPerRun * 2.\n * Linear interpolation between.\n *\n * @param options - Cost evaluation options including `maxTokensPerRun`.\n * @returns An eval criterion that scores token usage against the budget.\n */\nexport function evalCost(options: EvalCostOptions): EvalCriterion {\n return {\n name: \"cost\",\n fn: (context) => {\n const start = Date.now();\n const tokens = context.result.totalTokens;\n const ratio = tokens / options.maxTokensPerRun;\n\n let score: number;\n if (ratio <= 0.5) {\n score = 1.0;\n } else if (ratio >= 2.0) {\n score = 0.0;\n } else {\n score = 1.0 - (ratio - 0.5) / 1.5;\n }\n\n return {\n score,\n passed: tokens <= options.maxTokensPerRun,\n reason: `${tokens} tokens (budget: ${options.maxTokensPerRun})`,\n durationMs: Date.now() - start,\n };\n },\n threshold: 0.5,\n weight: 1.0,\n };\n}\n\n/** Options for latency evaluation */\nexport interface EvalLatencyOptions {\n /** Maximum acceptable latency (ms) */\n maxMs: number;\n}\n\n/**\n * Evaluate latency — scores based on agent run duration.\n *\n * Score = 1.0 when duration \\<= maxMs * 0.5,\n * Score = 0.0 when duration \\>= maxMs * 2.\n * Linear interpolation between.\n *\n * @param options - Latency evaluation options including `maxMs`.\n * @returns An eval criterion that scores run duration against the limit.\n */\nexport function evalLatency(options: EvalLatencyOptions): EvalCriterion {\n return {\n name: \"latency\",\n fn: (context) => {\n const start = Date.now();\n const duration = context.runDurationMs;\n const ratio = duration / options.maxMs;\n\n let score: number;\n if (ratio <= 0.5) {\n score = 1.0;\n } else if (ratio >= 2.0) {\n score = 0.0;\n } else {\n score = 1.0 - (ratio - 0.5) / 1.5;\n }\n\n return {\n score,\n passed: duration <= options.maxMs,\n reason: `${Math.round(duration)}ms (max: ${options.maxMs}ms)`,\n durationMs: Date.now() - start,\n };\n },\n threshold: 0.5,\n weight: 1.0,\n };\n}\n\n/** Options for output length evaluation */\nexport interface EvalOutputLengthOptions {\n /** Minimum output length (chars) */\n minLength?: number;\n /** Maximum output length (chars) */\n maxLength?: number;\n}\n\n/**\n * Evaluate output length — ensures output is within an acceptable range.\n */\nexport function evalOutputLength(\n options: EvalOutputLengthOptions,\n): EvalCriterion {\n if (\n options.minLength !== undefined &&\n options.maxLength !== undefined &&\n options.minLength > options.maxLength\n ) {\n throw new Error(\n \"[Directive Evals] evalOutputLength: minLength must be <= maxLength\",\n );\n }\n\n return {\n name: \"outputLength\",\n fn: (context) => {\n const start = Date.now();\n const output = String(context.result.output);\n const length = output.length;\n const min = options.minLength ?? 0;\n const max = options.maxLength ?? Number.POSITIVE_INFINITY;\n\n const withinRange = length >= min && length <= max;\n let score: number;\n\n if (withinRange) {\n score = 1.0;\n } else if (length < min) {\n score = min > 0 ? Math.max(0, length / min) : 0;\n } else {\n score =\n max > 0 && max !== Number.POSITIVE_INFINITY\n ? Math.max(0, 1.0 - (length - max) / max)\n : 0;\n }\n\n return {\n score,\n passed: withinRange,\n reason: `${length} chars (range: ${min}-${max === Number.POSITIVE_INFINITY ? \"∞\" : max})`,\n durationMs: Date.now() - start,\n };\n },\n threshold: 0.5,\n weight: 1.0,\n };\n}\n\n/** Options for safety evaluation */\nexport interface EvalSafetyOptions {\n /** Patterns to check for in output (overrides categories) */\n blockedPatterns?: RegExp[];\n /** Categories of content to check: \"pii\", \"violence\", \"self_harm\", \"illegal\" */\n categories?: Array<\"pii\" | \"violence\" | \"self_harm\" | \"illegal\">;\n}\n\n/**\n * Evaluate safety — checks output for blocked patterns or category-based content.\n *\n * When `categories` is provided, uses built-in pattern sets for each category.\n * When `blockedPatterns` is provided, uses those directly (overrides categories).\n * When neither is provided, defaults to all safety categories.\n *\n * Score = 1.0 when no blocked patterns found.\n * Score = 0.0 when any blocked pattern matches.\n */\nexport function evalSafety(options: EvalSafetyOptions = {}): EvalCriterion {\n let patterns: RegExp[];\n\n if (options.blockedPatterns) {\n patterns = options.blockedPatterns;\n } else if (options.categories && options.categories.length > 0) {\n patterns = [];\n for (const category of options.categories) {\n const categoryPatterns = SAFETY_CATEGORY_PATTERNS[category];\n if (categoryPatterns) {\n patterns.push(...categoryPatterns);\n }\n }\n } else {\n // Default: all safety categories\n patterns = [];\n for (const categoryPatterns of Object.values(SAFETY_CATEGORY_PATTERNS)) {\n patterns.push(...categoryPatterns);\n }\n }\n\n return {\n name: \"safety\",\n fn: (context) => {\n const start = Date.now();\n const output = String(context.result.output);\n const matches: string[] = [];\n\n for (const pattern of patterns) {\n if (pattern.test(output)) {\n matches.push(pattern.source);\n }\n }\n\n const score = matches.length === 0 ? 1.0 : 0.0;\n\n return {\n score,\n passed: matches.length === 0,\n reason:\n matches.length === 0\n ? \"No unsafe patterns detected\"\n : `Matched patterns: ${matches.join(\", \")}`,\n durationMs: Date.now() - start,\n };\n },\n threshold: 1.0,\n weight: 2.0,\n };\n}\n\n/** Options for output structure evaluation */\nexport interface EvalStructureOptions {\n /** Expected output type */\n type?: \"json\" | \"string\";\n /** Required keys if type is \"json\" */\n requiredKeys?: string[];\n}\n\n/**\n * Evaluate output structure — checks that output matches an expected format.\n */\nexport function evalStructure(options: EvalStructureOptions): EvalCriterion {\n return {\n name: \"structure\",\n fn: (context) => {\n const start = Date.now();\n const output = context.result.output;\n\n if (options.type === \"json\") {\n try {\n const parsed =\n typeof output === \"string\" ? JSON.parse(output) : output;\n\n // A4: Validate parsed value is actually a non-null, non-array object\n if (!parsed || typeof parsed !== \"object\" || Array.isArray(parsed)) {\n return {\n score: 0,\n passed: false,\n reason: \"Output is not a valid JSON object\",\n durationMs: Date.now() - start,\n };\n }\n\n if (options.requiredKeys && parsed && typeof parsed === \"object\") {\n const missing = options.requiredKeys.filter(\n (k) => !Object.hasOwn(parsed, k),\n );\n if (missing.length > 0) {\n return {\n score: 1.0 - missing.length / options.requiredKeys.length,\n passed: false,\n reason: `Missing keys: ${missing.join(\", \")}`,\n durationMs: Date.now() - start,\n };\n }\n }\n\n return {\n score: 1.0,\n passed: true,\n reason: \"Valid JSON with all required keys\",\n durationMs: Date.now() - start,\n };\n } catch {\n return {\n score: 0.0,\n passed: false,\n reason: \"Output is not valid JSON\",\n durationMs: Date.now() - start,\n };\n }\n }\n\n const str = String(output);\n\n return {\n score: str.length > 0 ? 1.0 : 0.0,\n passed: str.length > 0,\n reason: str.length > 0 ? \"Non-empty output\" : \"Empty output\",\n durationMs: Date.now() - start,\n };\n },\n threshold: 0.5,\n weight: 1.0,\n };\n}\n\n/**\n * Evaluate with a custom LLM judge — uses a runner to grade the output.\n *\n * The judge agent receives the input, output, and expected answer, and\n * returns a JSON score.\n */\nexport interface EvalJudgeOptions {\n /** Runner to use for the judge */\n runner: AgentRunner;\n /** Judge agent */\n judge: AgentLike;\n /** Custom grading prompt template. {{input}}, {{output}}, {{expected}} are replaced. */\n promptTemplate?: string;\n /** Optional abort signal */\n signal?: AbortSignal;\n /** Timeout for the judge call in ms. Default: 30_000 */\n timeoutMs?: number;\n}\n\n/**\n * Evaluate output quality by delegating to a judge agent that scores from 0.0 to 1.0.\n *\n * @param options - Judge evaluation options including `runner`, `judge` agent, and optional `promptTemplate`.\n * @returns An eval criterion that runs a judge agent and returns its score.\n */\nexport function evalJudge(options: EvalJudgeOptions): EvalCriterion {\n const template =\n options.promptTemplate ??\n `You are evaluating an AI agent's output. Score it from 0.0 to 1.0.\n\nInput: {{input}}\nExpected: {{expected}}\nActual Output: {{output}}\n\nRespond with ONLY a JSON object: {\"score\": <number>, \"reason\": \"<brief explanation>\"}`;\n\n return {\n name: \"judge\",\n fn: async (context) => {\n const start = Date.now();\n const prompt = template\n .replaceAll(\"{{input}}\", context.testCase.input)\n .replaceAll(\"{{expected}}\", context.testCase.expected ?? \"N/A\")\n .replaceAll(\"{{output}}\", String(context.result.output));\n\n const timeoutMs = options.timeoutMs ?? 30_000;\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), timeoutMs);\n const combinedSignal = options.signal\n ? combineSignals([options.signal, controller.signal])\n : controller.signal;\n\n try {\n const result = await options.runner(options.judge, prompt, {\n signal: combinedSignal,\n });\n\n const raw = result.output;\n if (typeof raw !== \"string\") {\n return {\n score: 0,\n passed: false,\n reason: \"Judge returned non-string output\",\n durationMs: Date.now() - start,\n };\n }\n\n const parsed = JSON.parse(raw) as { score: number; reason?: string };\n const score = Math.max(0, Math.min(1, parsed.score));\n\n return {\n score,\n passed: score >= 0.5,\n reason: parsed.reason ?? `Judge score: ${score}`,\n durationMs: Date.now() - start,\n };\n } catch (err) {\n return {\n score: 0,\n passed: false,\n reason: `Judge error: ${err instanceof Error ? err.message : String(err)}`,\n durationMs: Date.now() - start,\n };\n } finally {\n clearTimeout(timer);\n }\n },\n threshold: 0.5,\n weight: 1.5,\n };\n}\n\n/**\n * Evaluate exact or substring match against expected output.\n */\nexport interface EvalMatchOptions {\n /** Match mode. Default: \"contains\" */\n mode?: \"exact\" | \"contains\" | \"regex\";\n /** Case-insensitive matching. Default: true */\n caseInsensitive?: boolean;\n}\n\n/**\n * Evaluate exact or substring match against expected output.\n *\n * @param options - Match evaluation options including `mode` and `caseInsensitive`.\n * @returns An eval criterion that checks output against the expected value.\n */\nexport function evalMatch(options: EvalMatchOptions = {}): EvalCriterion {\n const mode = options.mode ?? \"contains\";\n const ci = options.caseInsensitive ?? true;\n\n return {\n name: \"match\",\n fn: (context) => {\n const start = Date.now();\n const expected = context.testCase.expected;\n if (!expected) {\n return {\n score: 1.0,\n passed: true,\n reason: \"No expected output to match\",\n durationMs: Date.now() - start,\n };\n }\n\n const output = String(context.result.output);\n const a = ci ? output.toLowerCase() : output;\n const b = ci ? expected.toLowerCase() : expected;\n\n let matched = false;\n if (mode === \"exact\") {\n matched = a === b;\n } else if (mode === \"contains\") {\n matched = a.includes(b);\n } else if (mode === \"regex\") {\n const MAX_REGEX_LENGTH = 500;\n if (expected.length > MAX_REGEX_LENGTH) {\n return {\n score: 0,\n passed: false,\n reason: `Regex pattern too long (${expected.length} chars, max ${MAX_REGEX_LENGTH})`,\n durationMs: Date.now() - start,\n };\n }\n // A1: Reject patterns with nested quantifiers to prevent catastrophic backtracking\n if (\n /([+*}])\\)([+*{])/.test(expected) ||\n /([+*}])\\]([+*{])/.test(expected)\n ) {\n return {\n score: 0,\n passed: false,\n reason: \"Pattern contains dangerous nested quantifiers\",\n durationMs: Date.now() - start,\n };\n }\n try {\n matched = new RegExp(expected, ci ? \"i\" : \"\").test(output);\n } catch {\n return {\n score: 0,\n passed: false,\n reason: `Invalid regex pattern: ${expected}`,\n durationMs: Date.now() - start,\n };\n }\n }\n\n return {\n score: matched ? 1.0 : 0.0,\n passed: matched,\n reason: matched\n ? `Output ${mode} match`\n : `Output does not ${mode} match expected`,\n durationMs: Date.now() - start,\n };\n },\n threshold: 1.0,\n weight: 1.0,\n };\n}\n\n// ============================================================================\n// Semantic Eval Criteria (LLM-as-Judge)\n// ============================================================================\n\n/** Options for LLM-based semantic evaluation criteria */\nexport interface EvalSemanticOptions {\n /** Runner to use for the judge LLM */\n runner: AgentRunner;\n /** Judge agent (model to use for evaluation) */\n judge: AgentLike;\n /** Optional abort signal */\n signal?: AbortSignal;\n /** Timeout for the judge call in ms. Default: 30_000 */\n timeoutMs?: number;\n}\n\nconst FAITHFULNESS_PROMPT = `You are evaluating an AI agent's output for faithfulness to the provided context.\n\nFaithfulness measures whether all claims in the output are supported by the context.\nScore 1.0 if every claim is grounded in the context.\nScore 0.0 if the output contains fabricated information not in the context.\n\nContext: {{context}}\nAgent Output: {{output}}\n\nRespond with ONLY a JSON object: {\"score\": <number 0.0-1.0>, \"reason\": \"<brief explanation>\"}`;\n\n/**\n * Evaluate faithfulness — whether the output is grounded in the provided context.\n *\n * Requires `context` field on the EvalCase. Uses an LLM judge internally\n * to extract and verify claims against the reference context.\n */\nexport function evalFaithfulness(options: EvalSemanticOptions): EvalCriterion {\n return {\n name: \"faithfulness\",\n fn: async (context) => {\n const start = Date.now();\n const refContext = context.testCase.context ?? context.testCase.expected;\n if (!refContext) {\n return {\n score: 1.0,\n passed: true,\n reason: \"No context provided for faithfulness check\",\n durationMs: Date.now() - start,\n };\n }\n\n const prompt = FAITHFULNESS_PROMPT.replaceAll(\n \"{{context}}\",\n refContext,\n ).replaceAll(\"{{output}}\", String(context.result.output));\n\n const timeoutMs = options.timeoutMs ?? 30_000;\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), timeoutMs);\n const combinedSignal = options.signal\n ? combineSignals([options.signal, controller.signal])\n : controller.signal;\n\n try {\n const result = await options.runner(options.judge, prompt, {\n signal: combinedSignal,\n });\n\n const raw = result.output;\n if (typeof raw !== \"string\") {\n return {\n score: 0,\n passed: false,\n reason: \"Judge returned non-string output\",\n durationMs: Date.now() - start,\n };\n }\n\n const parsed = JSON.parse(raw) as { score: number; reason?: string };\n const score = Math.max(0, Math.min(1, parsed.score));\n\n return {\n score,\n passed: score >= 0.7,\n reason: parsed.reason ?? `Faithfulness score: ${score}`,\n durationMs: Date.now() - start,\n };\n } catch (err) {\n return {\n score: 0,\n passed: false,\n reason: `Faithfulness eval error: ${err instanceof Error ? err.message : String(err)}`,\n durationMs: Date.now() - start,\n };\n } finally {\n clearTimeout(timer);\n }\n },\n threshold: 0.7,\n weight: 1.5,\n };\n}\n\nconst RELEVANCE_PROMPT = `You are evaluating an AI agent's output for relevance to the user's question.\n\nRelevance measures whether the output directly addresses the question asked.\nScore 1.0 if the output fully and directly answers the question.\nScore 0.0 if the output is completely off-topic or irrelevant.\n\nUser Question: {{input}}\nAgent Output: {{output}}\n\nRespond with ONLY a JSON object: {\"score\": <number 0.0-1.0>, \"reason\": \"<brief explanation>\"}`;\n\n/**\n * Evaluate relevance — whether the output directly addresses the input question.\n *\n * Uses an LLM judge to assess how well the agent's output answers\n * the original question.\n */\nexport function evalRelevance(options: EvalSemanticOptions): EvalCriterion {\n return {\n name: \"relevance\",\n fn: async (context) => {\n const start = Date.now();\n\n const prompt = RELEVANCE_PROMPT.replaceAll(\n \"{{input}}\",\n context.testCase.input,\n ).replaceAll(\"{{output}}\", String(context.result.output));\n\n const timeoutMs = options.timeoutMs ?? 30_000;\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), timeoutMs);\n const combinedSignal = options.signal\n ? combineSignals([options.signal, controller.signal])\n : controller.signal;\n\n try {\n const result = await options.runner(options.judge, prompt, {\n signal: combinedSignal,\n });\n\n const raw = result.output;\n if (typeof raw !== \"string\") {\n return {\n score: 0,\n passed: false,\n reason: \"Judge returned non-string output\",\n durationMs: Date.now() - start,\n };\n }\n\n const parsed = JSON.parse(raw) as { score: number; reason?: string };\n const score = Math.max(0, Math.min(1, parsed.score));\n\n return {\n score,\n passed: score >= 0.7,\n reason: parsed.reason ?? `Relevance score: ${score}`,\n durationMs: Date.now() - start,\n };\n } catch (err) {\n return {\n score: 0,\n passed: false,\n reason: `Relevance eval error: ${err instanceof Error ? err.message : String(err)}`,\n durationMs: Date.now() - start,\n };\n } finally {\n clearTimeout(timer);\n }\n },\n threshold: 0.7,\n weight: 1.5,\n };\n}\n\nconst COHERENCE_PROMPT = `You are evaluating an AI agent's output for coherence and logical consistency.\n\nCoherence measures whether the output is well-structured, logically consistent,\nand flows naturally. Check for contradictions, non-sequiturs, and clarity.\nScore 1.0 if the output is perfectly coherent and well-organized.\nScore 0.0 if the output is incoherent, contradictory, or disorganized.\n\nAgent Output: {{output}}\n\nRespond with ONLY a JSON object: {\"score\": <number 0.0-1.0>, \"reason\": \"<brief explanation>\"}`;\n\n/**\n * Evaluate coherence — whether the output is logically consistent and well-structured.\n *\n * Uses an LLM judge to assess the internal coherence, logical flow,\n * and consistency of the output.\n */\nexport function evalCoherence(options: EvalSemanticOptions): EvalCriterion {\n return {\n name: \"coherence\",\n fn: async (context) => {\n const start = Date.now();\n\n const prompt = COHERENCE_PROMPT.replaceAll(\n \"{{output}}\",\n String(context.result.output),\n );\n\n const timeoutMs = options.timeoutMs ?? 30_000;\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), timeoutMs);\n const combinedSignal = options.signal\n ? combineSignals([options.signal, controller.signal])\n : controller.signal;\n\n try {\n const result = await options.runner(options.judge, prompt, {\n signal: combinedSignal,\n });\n\n const raw = result.output;\n if (typeof raw !== \"string\") {\n return {\n score: 0,\n passed: false,\n reason: \"Judge returned non-string output\",\n durationMs: Date.now() - start,\n };\n }\n\n const parsed = JSON.parse(raw) as { score: number; reason?: string };\n const score = Math.max(0, Math.min(1, parsed.score));\n\n return {\n score,\n passed: score >= 0.7,\n reason: parsed.reason ?? `Coherence score: ${score}`,\n durationMs: Date.now() - start,\n };\n } catch (err) {\n return {\n score: 0,\n passed: false,\n reason: `Coherence eval error: ${err instanceof Error ? err.message : String(err)}`,\n durationMs: Date.now() - start,\n };\n } finally {\n clearTimeout(timer);\n }\n },\n threshold: 0.7,\n weight: 1.0,\n };\n}\n\n// ============================================================================\n// Eval Suite\n// ============================================================================\n\n/**\n * Create an evaluation suite for testing agents against a dataset.\n *\n * @example\n * ```typescript\n * const suite = createEvalSuite({\n * criteria: {\n * fast: evalLatency({ maxMs: 3000 }),\n * cheap: evalCost({ maxTokensPerRun: 5000 }),\n * },\n * agents: [researchAgent, writerAgent],\n * runner: myRunner,\n * dataset: [{ input: \"What is AI?\" }],\n * });\n *\n * const results = await suite.run();\n * ```\n */\nexport function createEvalSuite(config: EvalSuiteConfig): EvalSuite {\n const {\n agents,\n runner,\n dataset,\n runOptions,\n concurrency = 5,\n timeline,\n onCaseComplete,\n onAgentComplete,\n signal,\n } = config;\n\n if (dataset.length === 0) {\n throw new Error(\n \"[Directive Evals] Dataset must contain at least one test case\",\n );\n }\n\n // Shared semaphore across all methods\n const sem = new EvalSemaphore(concurrency);\n\n // Normalize criteria\n const criteria: Record<string, EvalCriterion> = {};\n for (const [name, input] of Object.entries(config.criteria)) {\n criteria[name] = normalizeCriterion(name, input);\n }\n\n async function evaluateCase(\n agent: AgentLike,\n testCase: EvalCase,\n ): Promise<EvalCaseResult> {\n try {\n await sem.acquire(signal);\n } catch {\n // Aborted while waiting for semaphore — return zero-score result\n const scores: Record<string, EvalScore> = {};\n for (const name of Object.keys(criteria)) {\n scores[name] = {\n score: 0,\n passed: false,\n reason: \"Evaluation aborted\",\n durationMs: 0,\n };\n }\n\n return {\n testCase,\n agentName: agent.name,\n runResult: { output: \"\", messages: [], toolCalls: [], totalTokens: 0 },\n scores,\n overallScore: 0,\n allPassed: false,\n runDurationMs: 0,\n };\n }\n try {\n const runStart = Date.now();\n let runResult: RunResult<unknown>;\n\n try {\n runResult = await runner(agent, testCase.input, {\n ...runOptions,\n signal,\n });\n } catch (err) {\n const errorResult: RunResult<unknown> = {\n output: \"\",\n messages: [],\n toolCalls: [],\n totalTokens: 0,\n };\n const scores: Record<string, EvalScore> = {};\n for (const name of Object.keys(criteria)) {\n scores[name] = {\n score: 0,\n passed: false,\n reason: `Agent error: ${err instanceof Error ? `${err.name}: ${err.message}` : String(err)}`,\n durationMs: 0,\n };\n }\n\n return {\n testCase,\n agentName: agent.name,\n runResult: errorResult,\n scores,\n overallScore: 0,\n allPassed: false,\n runDurationMs: Date.now() - runStart,\n };\n }\n\n const runDurationMs = Date.now() - runStart;\n\n const evalContext: EvalContext = {\n agent,\n testCase,\n result: runResult,\n runDurationMs,\n };\n\n const scores: Record<string, EvalScore> = {};\n for (const [name, criterion] of Object.entries(criteria)) {\n try {\n const score = await criterion.fn(evalContext);\n scores[name] = {\n ...score,\n passed: score.score >= (criterion.threshold ?? 0.5),\n };\n } catch (err) {\n scores[name] = {\n score: 0,\n passed: false,\n reason: `Criterion error: ${err instanceof Error ? `${err.name}: ${err.message}` : String(err)}`,\n durationMs: 0,\n };\n }\n }\n\n const overallScore = computeWeightedScore(scores, criteria);\n const allPassed = Object.values(scores).every((s) => s.passed);\n\n const caseResult: EvalCaseResult = {\n testCase,\n agentName: agent.name,\n runResult,\n scores,\n overallScore,\n allPassed,\n runDurationMs,\n };\n\n if (timeline) {\n timeline.record({\n type: \"agent_complete\" as const,\n timestamp: Date.now(),\n agentId: `eval:${agent.name}`,\n outputLength: String(runResult.output).length,\n totalTokens: runResult.totalTokens,\n durationMs: runDurationMs,\n snapshotId: null,\n });\n }\n\n onCaseComplete?.(caseResult);\n\n return caseResult;\n } finally {\n sem.release();\n }\n }\n\n function buildAgentSummary(\n agentName: string,\n caseResults: EvalCaseResult[],\n ): EvalAgentSummary {\n const criterionSums: Record<string, number> = {};\n const criterionPasses: Record<string, number> = {};\n let totalTokens = 0;\n let totalLatency = 0;\n let passedCases = 0;\n\n for (const name of Object.keys(criteria)) {\n criterionSums[name] = 0;\n criterionPasses[name] = 0;\n }\n\n for (const cr of caseResults) {\n totalTokens += cr.runResult.totalTokens;\n totalLatency += cr.runDurationMs;\n if (cr.allPassed) {\n passedCases++;\n }\n\n for (const [name, score] of Object.entries(cr.scores)) {\n criterionSums[name] = (criterionSums[name] ?? 0) + score.score;\n if (score.passed) {\n criterionPasses[name] = (criterionPasses[name] ?? 0) + 1;\n }\n }\n }\n\n const totalCases = caseResults.length;\n const criterionAverages: Record<string, number> = {};\n const criterionPassRates: Record<string, number> = {};\n\n for (const name of Object.keys(criteria)) {\n criterionAverages[name] =\n totalCases > 0 ? (criterionSums[name] ?? 0) / totalCases : 0;\n criterionPassRates[name] =\n totalCases > 0 ? (criterionPasses[name] ?? 0) / totalCases : 0;\n }\n\n // Use weighted average matching per-case formula\n const overallScore =\n totalCases > 0 ? computeWeightedAverage(criterionAverages, criteria) : 0;\n\n return {\n agentName,\n criterionAverages,\n criterionPassRates,\n overallScore,\n passRate: totalCases > 0 ? passedCases / totalCases : 0,\n totalTokens,\n avgLatencyMs: totalCases > 0 ? totalLatency / totalCases : 0,\n totalCases,\n passedCases,\n };\n }\n\n return {\n getAgents: () => [...agents],\n getCriteria: () => Object.keys(criteria),\n getDataset: () => [...dataset],\n\n async run(): Promise<EvalResults> {\n const startedAt = Date.now();\n const allDetails: EvalCaseResult[] = [];\n const summary: Record<string, EvalAgentSummary> = {};\n\n // Run all agents in parallel (semaphore controls total concurrency)\n const agentPromises = agents.map(async (agent) => {\n if (signal?.aborted) {\n return;\n }\n\n const agentResults = await Promise.all(\n dataset.map((testCase) => evaluateCase(agent, testCase)),\n );\n\n return { agent, agentResults };\n });\n\n const results = await Promise.all(agentPromises);\n\n for (const entry of results) {\n if (!entry) {\n continue;\n }\n allDetails.push(...entry.agentResults);\n const agentSummary = buildAgentSummary(\n entry.agent.name,\n entry.agentResults,\n );\n summary[entry.agent.name] = agentSummary;\n onAgentComplete?.(agentSummary);\n }\n\n const completedAt = Date.now();\n\n return {\n summary,\n details: allDetails,\n durationMs: completedAt - startedAt,\n totalTokens: allDetails.reduce(\n (sum, d) => sum + d.runResult.totalTokens,\n 0,\n ),\n startedAt,\n completedAt,\n };\n },\n\n async runAgent(agentName: string): Promise<EvalAgentSummary> {\n const agent = agents.find((a) => a.name === agentName);\n if (!agent) {\n throw new Error(`[Directive Evals] Unknown agent: \"${agentName}\"`);\n }\n\n const agentResults = await Promise.all(\n dataset.map((testCase) => evaluateCase(agent, testCase)),\n );\n\n const agentSummary = buildAgentSummary(agent.name, agentResults);\n onAgentComplete?.(agentSummary);\n\n return agentSummary;\n },\n };\n}\n\n// ============================================================================\n// Eval Assertions (CI helpers)\n// ============================================================================\n\n/** Options for eval assertions in CI */\nexport interface EvalAssertOptions {\n /** Minimum weighted overall score required (0.0-1.0) */\n minScore?: number;\n /** Minimum pass rate required (0.0-1.0) */\n minPassRate?: number;\n /** Criteria that must achieve 100% pass rate */\n failOn?: string[];\n}\n\n/**\n * Assert eval results meet requirements — designed for CI pipelines.\n *\n * Throws an error with details if any assertion fails.\n *\n * @example\n * ```typescript\n * const results = await suite.run();\n * evalAssert(results, {\n * minScore: 0.8,\n * minPassRate: 0.9,\n * failOn: [\"safety\"],\n * });\n * ```\n */\nexport function evalAssert(\n results: EvalResults,\n options: EvalAssertOptions,\n): void {\n const failures: string[] = [];\n\n for (const [agentName, summary] of Object.entries(results.summary)) {\n if (\n options.minScore !== undefined &&\n summary.overallScore < options.minScore\n ) {\n failures.push(\n `Agent \"${agentName}\" score ${summary.overallScore.toFixed(3)} < minimum ${options.minScore}`,\n );\n }\n\n if (\n options.minPassRate !== undefined &&\n summary.passRate < options.minPassRate\n ) {\n failures.push(\n `Agent \"${agentName}\" pass rate ${summary.passRate.toFixed(3)} < minimum ${options.minPassRate}`,\n );\n }\n\n if (options.failOn) {\n for (const criterionName of options.failOn) {\n const passRate = summary.criterionPassRates[criterionName];\n if (passRate !== undefined && passRate < 1.0) {\n failures.push(\n `Agent \"${agentName}\" criterion \"${criterionName}\" pass rate ${passRate.toFixed(3)} < 1.0 (failOn)`,\n );\n }\n }\n }\n }\n\n if (failures.length > 0) {\n throw new Error(\n `[Directive Evals] Assertion failed:\\n${failures.join(\"\\n\")}`,\n );\n }\n}\n"]}
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var chunkLPTC3N2Z_cjs=require('./chunk-LPTC3N2Z.cjs'),chunkAEX7HKNM_cjs=require('./chunk-AEX7HKNM.cjs'),chunkWOFIBIPW_cjs=require('./chunk-WOFIBIPW.cjs'),chunkWCQK3JOR_cjs=require('./chunk-WCQK3JOR.cjs'),core=require('@directive-run/core'),adapterUtils=require('@directive-run/core/adapter-utils');var Qe=500,Ze=200,et=false;function xt(ve){let{runner:ne,factsSchema:be={},init:we,constraints:tt={},resolvers:nt={},guardrails:q={},onApprovalRequest:le,autoApproveToolCalls:de=true,maxTokenBudget:I,budgetWarningThreshold:re=.8,onBudgetWarning:Te,budgetEstimateTokens:ae,retryInsideCircuit:Re=false,plugins:rt=[],debug:Ae=false,approvalTimeoutMs:pe=3e5,agentRetry:oe,hooks:Se={},memory:M,circuitBreaker:j,selfHealing:R,outputSchema:at,maxSchemaRetries:ot,checkpointStore:Ce,breakpoints:_,onBreakpoint:st,breakpointTimeoutMs:it}=ve,D=typeof Ae=="object"?true:!!Ae,ge=5e3;if(D&&R&&!j&&console.warn("[Directive] selfHealing config has no effect without a circuitBreaker \u2014 fallback behavior requires the circuit breaker to detect failures."),re<0||re>1)throw new Error(`[Directive Orchestrator] budgetWarningThreshold must be between 0 and 1, got ${re}`);if(!de&&!le)throw new Error(`[Directive] Invalid approval configuration: autoApproveToolCalls is false but no onApprovalRequest callback provided. Tool calls would wait for approval indefinitely. Either:
|
|
2
2
|
- Set autoApproveToolCalls: true to auto-approve all tool calls
|
|
3
|
-
- Provide an onApprovalRequest callback to handle approvals programmatically`);function W(e,t){try{Se[e]?.(t);}catch(a){D&&console.debug(`[Directive] hooks.${e} threw:`,a);}}let Oe=["agent","approval","conversation","toolCalls"];for(let e of Object.keys(be))if(Oe.includes(e))throw new Error(`[Directive] Facts schema key "${e}" conflicts with orchestrator state. Reserved keys: ${Oe.join(", ")}. Rename your fact to avoid the collision.`);let ut={facts:{...
|
|
3
|
+
- Provide an onApprovalRequest callback to handle approvals programmatically`);function W(e,t){try{Se[e]?.(t);}catch(a){D&&console.debug(`[Directive] hooks.${e} threw:`,a);}}let Oe=["agent","approval","conversation","toolCalls"];for(let e of Object.keys(be))if(Oe.includes(e))throw new Error(`[Directive] Facts schema key "${e}" conflicts with orchestrator state. Reserved keys: ${Oe.join(", ")}. Rename your fact to avoid the collision.`);let ut={facts:{...chunkAEX7HKNM_cjs.i.facts,...be,__budgetWarningFired:core.t.boolean()},derivations:{},events:{},requirements:{}},Ee,n,_e=chunkLPTC3N2Z_cjs.q(tt);I&&(_e.__budgetLimit={priority:100,when:e=>chunkLPTC3N2Z_cjs.f(e).tokenUsage>I,require:{type:"__PAUSE_BUDGET_EXCEEDED"}});let De=chunkLPTC3N2Z_cjs.r(nt,(e,t,a,r)=>Ee(e,t,a,r),()=>n.facts);De.__pause={requirement:adapterUtils.requirementGuard("__PAUSE_BUDGET_EXCEEDED"),resolve:async(e,t)=>{let a=chunkLPTC3N2Z_cjs.f(t.facts);chunkLPTC3N2Z_cjs.g(t.facts,{...a,status:"paused"});}};let y=null;D&&(y=chunkWOFIBIPW_cjs.a({getSnapshotId:()=>{try{return n.history?.currentIndex??null}catch{return null}},goToSnapshot:e=>{try{n.history?.goTo?.(e);}catch{}}}));let ct=adapterUtils.createCallbackPlugin("directive-ai-callbacks",{}),lt=core.createModule("directive-ai-orchestrator",{schema:ut,init:e=>{if(chunkLPTC3N2Z_cjs.g(e,{status:"idle",currentAgent:null,input:null,output:null,error:null,tokenUsage:0,turnCount:0,startedAt:null,completedAt:null}),chunkLPTC3N2Z_cjs.i(e,{pending:[],approved:[],rejected:[]}),chunkLPTC3N2Z_cjs.k(e,[]),chunkLPTC3N2Z_cjs.m(e,[]),chunkLPTC3N2Z_cjs.o(e,{pending:[],resolved:[],cancelled:[]}),adapterUtils.setBridgeFact(e,"__budgetWarningFired",false),we){let t=chunkLPTC3N2Z_cjs.p(e),a={...e,...t};we(a);}},constraints:_e,resolvers:De}),xe=[...rt,ct];D&&y&&xe.push(chunkWOFIBIPW_cjs.b(y,()=>{try{return n.history?.currentIndex??null}catch{return null}})),n=core.createSystem({module:lt,plugins:xe,history:D?true:void 0}),n.start();let $={count:0};function Be(e,t){if(ae===void 0)return 0;if(typeof ae=="number")return Math.max(0,Math.floor(ae));try{let a=ae(t,e);return Number.isFinite(a)?Math.max(0,Math.floor(a)):0}catch{return 0}}async function Ie(e,t,a,r,s){let c=0;if(I){let g=chunkLPTC3N2Z_cjs.f(n.facts).tokenUsage;if(c=Be(e,t),g+$.count+c>I)throw new Error(`[Directive] Agent budget would be exceeded by this call: current=${g} + in-flight=${$.count} + estimate=${c} > maxTokenBudget=${I}. Raise \`maxTokenBudget\`, lower \`budgetEstimateTokens\`, or reset \`tokenUsage\`.`);$.count+=c;}try{return await dt(e,t,a,r,s)}finally{I&&c>0&&($.count=Math.max(0,$.count-c));}}async function Me(e,t,a){let r=Be(e,t);if(r<=0)return await a();$.count+=r;try{return await a()}finally{$.count=Math.max(0,$.count-r);}}async function $e(e,t,a,r){if(!R)throw e;if(R.fallbackRunners)for(let s of R.fallbackRunners)try{let c={originalAgent:t.name,reroutedTo:"fallback-runner",reason:e instanceof Error?e.message:String(e),timestamp:Date.now()};try{R.onReroute?.(c);}catch{}return y&&y.record({type:"reroute",timestamp:Date.now(),agentId:t.name,snapshotId:null,from:t.name,to:"fallback-runner",reason:e instanceof Error?e.message:String(e)}),await Me(t,a,()=>s(t,a,r))}catch{}if(R.fallbackAgent)try{let s={originalAgent:t.name,reroutedTo:R.fallbackAgent.name,reason:e instanceof Error?e.message:String(e),timestamp:Date.now()};try{R.onReroute?.(s);}catch{}y&&y.record({type:"reroute",timestamp:Date.now(),agentId:t.name,snapshotId:null,from:t.name,to:R.fallbackAgent.name,reason:e instanceof Error?e.message:String(e)});let c=R.fallbackAgent;return await Me(c,a,()=>ne(c,a,r))}catch{}if(R.degradation==="fallback-response"&&R.fallbackResponse!==void 0)return {output:R.fallbackResponse,messages:[],toolCalls:[],totalTokens:0};throw e}async function dt(e,t,a,r,s){if(j&&Re)try{return await j.execute(()=>Fe(e,t,a,r,s))}catch(c){return $e(c,e,t,r)}try{return await Fe(e,t,a,r,s)}catch(c){if(j||R)return $e(c,e,t,r);throw c}}async function Fe(e,t,a,r,s){let c=Date.now();if(M){let o=M.getContextMessages();if(o.length>0){let i=o.map(p=>`${p.role}: ${p.content}`).join(`
|
|
4
4
|
`);e={...e,instructions:`${e.instructions??""}
|
|
5
5
|
|
|
6
6
|
Conversation context:
|
|
7
|
-
${i}`};}}if(ve.metaContext){let o=
|
|
7
|
+
${i}`};}}if(ve.metaContext){let o=chunkLPTC3N2Z_cjs.a(n.inspect());o&&(e={...e,instructions:`${e.instructions??""}
|
|
8
8
|
|
|
9
|
-
${o}`});}if(_&&_.length>0){let o={agentId:e.name,agentName:e.name,input:t,state:n.facts.$store.toObject(),breakpointType:"pre_input_guardrails"},i=await se("pre_input_guardrails",o,s?.signal??r?.signal);if(i?.skip)return {output:void 0,messages:[],toolCalls:[],totalTokens:0};i?.input&&(t=i.input);}let g=s?.inputGuardrails!==void 0?s.inputGuardrails:q.input??[],b=s?.outputGuardrails!==void 0?s.outputGuardrails:q.output??[],F=g.map((o,i)=>
|
|
9
|
+
${o}`});}if(_&&_.length>0){let o={agentId:e.name,agentName:e.name,input:t,state:n.facts.$store.toObject(),breakpointType:"pre_input_guardrails"},i=await se("pre_input_guardrails",o,s?.signal??r?.signal);if(i?.skip)return {output:void 0,messages:[],toolCalls:[],totalTokens:0};i?.input&&(t=i.input);}let g=s?.inputGuardrails!==void 0?s.inputGuardrails:q.input??[],b=s?.outputGuardrails!==void 0?s.outputGuardrails:q.output??[],F=g.map((o,i)=>chunkLPTC3N2Z_cjs.c(o,i,"input"));for(let o of F){let{name:i}=o,p={agentName:e.name,input:t,facts:n.facts.$store.toObject()},O=Date.now(),m=await chunkLPTC3N2Z_cjs.d(o,{input:t,agentName:e.name},p);if(W("onGuardrailCheck",{agentId:e.name,guardrailName:i,guardrailType:"input",passed:m.passed,reason:m.reason,durationMs:Date.now()-O,timestamp:Date.now()}),!m.passed)throw new chunkAEX7HKNM_cjs.a({code:"INPUT_GUARDRAIL_FAILED",message:`Input guardrail "${i}" failed: ${m.reason}`,guardrailName:i,guardrailType:"input",userMessage:m.reason??"Input validation failed",agentName:e.name,input:t});m.transformed!==void 0&&(t=m.transformed);}if(W("onAgentStart",{agentName:e.name,input:t,timestamp:c}),y&&y.record({type:"agent_start",timestamp:Date.now(),agentId:e.name,snapshotId:null,inputLength:t.length,modelId:e.model??void 0,...e.instructions?{instructions:e.instructions.slice(0,ge)}:{},input:t.slice(0,ge)}),n.batch(()=>{let o=chunkLPTC3N2Z_cjs.f(n.facts);chunkLPTC3N2Z_cjs.g(n.facts,{...o,status:"running",currentAgent:e.name,input:t,startedAt:Date.now()});}),_&&_.length>0){let o={agentId:e.name,agentName:e.name,input:t,state:n.facts.$store.toObject(),breakpointType:"pre_agent_run"},i=await se("pre_agent_run",o,s?.signal??r?.signal);if(i?.skip)return {output:void 0,messages:[],toolCalls:[],totalTokens:0};i?.input&&(t=i.input);}let C=s?.outputSchema!==void 0?s.outputSchema:at,A=ne;if(C&&(A=chunkWCQK3JOR_cjs.b(ne,{schema:C,maxRetries:s?.maxSchemaRetries??ot??2})),j&&!Re){let o=A;A=((i,p,O)=>j.execute(()=>o(i,p,O)));}let l=await chunkLPTC3N2Z_cjs.e(A,e,t,{...r,signal:r?.signal,onMessage:o=>{let p=[...chunkLPTC3N2Z_cjs.j(n.facts),o];chunkLPTC3N2Z_cjs.k(n.facts,p.length>Qe?p.slice(-Qe):p),r?.onMessage?.(o);},onToolCall:async o=>{let i=(q.toolCall??[]).map((m,x)=>chunkLPTC3N2Z_cjs.c(m,x,"toolCall"));for(let m of i){let{name:x}=m,L={agentName:e.name,input:t,facts:n.facts.$store.toObject()},d=Date.now(),w=await chunkLPTC3N2Z_cjs.d(m,{toolCall:o,agentName:e.name,input:t},L);if(W("onGuardrailCheck",{agentId:e.name,guardrailName:x,guardrailType:"toolCall",passed:w.passed,reason:w.reason,durationMs:Date.now()-d,timestamp:Date.now()}),!w.passed)throw new chunkAEX7HKNM_cjs.a({code:"TOOL_CALL_GUARDRAIL_FAILED",message:`Tool call guardrail "${x}" failed: ${w.reason}`,guardrailName:x,guardrailType:"toolCall",userMessage:w.reason??"Tool call blocked",data:{toolCall:o},agentName:e.name,input:t})}if(!de){let m=`tool-${o.id}`,x={id:m,type:"tool_call",agentName:e.name,description:`Tool call: ${o.name}`,data:o,requestedAt:Date.now()};n.batch(()=>{let L=chunkLPTC3N2Z_cjs.h(n.facts);chunkLPTC3N2Z_cjs.i(n.facts,{...L,pending:[...L.pending,x]});}),le?.(x),await Le(m,s?.signal??r?.signal);}let O=[...chunkLPTC3N2Z_cjs.l(n.facts),o];chunkLPTC3N2Z_cjs.m(n.facts,O.length>Ze?O.slice(-Ze):O),r?.onToolCall?.(o);}},oe?{...oe,onRetry:(o,i,p)=>{oe.onRetry?.(o,i,p),W("onAgentRetry",{agentName:e.name,input:t,attempt:o,error:i,delayMs:p,timestamp:Date.now()});}}:void 0);if(_&&_.length>0){let o={agentId:e.name,agentName:e.name,input:t,state:n.facts.$store.toObject(),breakpointType:"pre_output_guardrails"},i=await se("pre_output_guardrails",o,s?.signal??r?.signal);if(i?.skip)return {output:void 0,messages:[],toolCalls:[],totalTokens:0};i?.input&&(t=i.input);}let K=b.map((o,i)=>chunkLPTC3N2Z_cjs.c(o,i,"output"));for(let o of K){let{name:i}=o,p={agentName:e.name,input:t,facts:n.facts.$store.toObject()},O=Date.now(),m=await chunkLPTC3N2Z_cjs.d(o,{output:l.output,agentName:e.name,input:t,messages:l.messages},p);if(W("onGuardrailCheck",{agentId:e.name,guardrailName:i,guardrailType:"output",passed:m.passed,reason:m.reason,durationMs:Date.now()-O,timestamp:Date.now()}),!m.passed)throw new chunkAEX7HKNM_cjs.a({code:"OUTPUT_GUARDRAIL_FAILED",message:`Output guardrail "${i}" failed: ${m.reason}`,guardrailName:i,guardrailType:"output",userMessage:m.reason??"Output validation failed",agentName:e.name,input:t});m.transformed!==void 0&&(l.output=m.transformed);}let ie=false,h=0;if(n.batch(()=>{let o=chunkLPTC3N2Z_cjs.f(n.facts),i=o.tokenUsage+l.totalTokens;if(chunkLPTC3N2Z_cjs.g(n.facts,{...o,status:"completed",output:l.output,tokenUsage:i,turnCount:o.turnCount+l.messages.length,completedAt:Date.now()}),I&&Te){h=i/I;let p=adapterUtils.getBridgeFact(n.facts,"__budgetWarningFired");h>=re&&!p&&(adapterUtils.setBridgeFact(n.facts,"__budgetWarningFired",true),ie=true);}}),ie)try{Te({currentTokens:chunkLPTC3N2Z_cjs.f(n.facts).tokenUsage,maxBudget:I,percentage:h});}catch(o){D&&console.debug("[Directive Orchestrator] onBudgetWarning threw:",o);}if(M&&l.messages.length>0)try{M.addMessages(l.messages);}catch(o){D&&console.debug("[Directive] Memory addMessages failed:",o);}if(W("onAgentComplete",{agentName:e.name,input:t,output:l.output,tokenUsage:l.totalTokens,durationMs:Date.now()-c,timestamp:Date.now()}),y){let o=typeof l.output=="string"?l.output:JSON.stringify(l.output);y.record({type:"agent_complete",timestamp:Date.now(),agentId:e.name,snapshotId:null,outputLength:o?.length??0,totalTokens:l.totalTokens,inputTokens:l.tokenUsage?.inputTokens??0,outputTokens:l.tokenUsage?.outputTokens??0,durationMs:Date.now()-c,modelId:e.model??void 0,output:o.slice(0,ge)});}if(_&&_.length>0){let o={agentId:e.name,agentName:e.name,input:t,state:n.facts.$store.toObject(),breakpointType:"post_run"},i=await se("post_run",o,s?.signal??r?.signal);if(i?.skip)return {output:void 0,messages:[],toolCalls:[],totalTokens:0};i?.input&&(t=i.input);}return l}Ee=Ie;let H=new Map,Q=new Map;function pt(e,t){return t?.aborted?Promise.reject(t.reason??new Error("Aborted while waiting for breakpoint")):new Promise((a,r)=>{let s=false,c=null,g=()=>{s||(s=true,c&&(clearTimeout(c),c=null),t&&t.removeEventListener("abort",b),F());},b=()=>{g(),r(t.reason??new Error(`Breakpoint wait for ${e} aborted`));};t&&t.addEventListener("abort",b,{once:true});let F=n.facts.$store.subscribe([chunkAEX7HKNM_cjs.g],()=>{if(s)return;let A=chunkLPTC3N2Z_cjs.n(n.facts);if(A.resolved.includes(e)){g();let l=H.get(e)??null;H.delete(e),a(l);}else if(A.cancelled.includes(e)){g(),H.delete(e);let l=Q.get(e);Q.delete(e),r(new Error(l?`Breakpoint ${e} was cancelled: ${l}`:`Breakpoint ${e} was cancelled`));}}),C=it??3e5;c=setTimeout(()=>{s||(g(),H.delete(e),Q.delete(e),r(new Error(`[Directive] Breakpoint timeout: ${e} not resolved within ${Math.round(C/1e3)}s`)));},C);})}async function se(e,t,a){if(!_||_.length===0)return null;let r=chunkLPTC3N2Z_cjs.u(_,e,t);if(!r)return null;let s=chunkLPTC3N2Z_cjs.t(),c={id:s,type:e,agentId:t.agentId,input:t.input,label:r.label,requestedAt:Date.now()};n.batch(()=>{let b=chunkLPTC3N2Z_cjs.n(n.facts);chunkLPTC3N2Z_cjs.o(n.facts,{...b,pending:[...b.pending,c]});});try{st?.(c);}catch{}try{Se.onBreakpoint?.(c);}catch{}y&&y.record({type:"breakpoint_hit",timestamp:Date.now(),snapshotId:null,agentId:t.agentId,breakpointId:s,breakpointType:e,label:r.label});let g=await pt(s,a);return y&&y.record({type:"breakpoint_resumed",timestamp:Date.now(),snapshotId:null,agentId:t.agentId,breakpointId:s,modified:!!g?.input,skipped:!!g?.skip}),g}function Le(e,t){return t?.aborted?Promise.reject(t.reason??new Error("Aborted while waiting for approval")):new Promise((a,r)=>{let s=false,c=null,g=()=>{s||(s=true,c&&(clearTimeout(c),c=null),t&&t.removeEventListener("abort",b),F());},b=()=>{g(),r(t.reason??new Error(`Approval wait for ${e} aborted`));};t&&t.addEventListener("abort",b,{once:true});let F=n.facts.$store.subscribe([chunkAEX7HKNM_cjs.d],()=>{if(s)return;let C=chunkLPTC3N2Z_cjs.h(n.facts);if(C.approved.includes(e))g(),a();else {let A=C.rejected.find(l=>l.id===e);if(A){g();let l=A.reason?`Request ${e} rejected: ${A.reason}`:`Request ${e} rejected`;r(new Error(l));}}});c=setTimeout(()=>{if(s)return;g();let C=Math.round(pe/1e3);r(new Error(`[Directive] Approval timeout: Request ${e} not resolved within ${C}s.
|
|
10
10
|
Solutions:
|
|
11
11
|
1. Handle via onApprovalRequest callback and call orchestrator.approve()/reject()
|
|
12
12
|
2. Set autoApproveToolCalls: true to auto-approve
|
|
13
13
|
3. Increase approvalTimeoutMs (current: ${pe}ms)
|
|
14
|
-
See: https://directive.run/docs/ai/running-agents`));},pe);})}function Pe(){return {...
|
|
15
|
-
exports.a=xt;//# sourceMappingURL=chunk-
|
|
16
|
-
//# sourceMappingURL=chunk-
|
|
14
|
+
See: https://directive.run/docs/ai/running-agents`));},pe);})}function Pe(){return {...chunkLPTC3N2Z_cjs.p(n.facts)}}return {system:n,get facts(){return Pe()},get totalTokens(){return chunkLPTC3N2Z_cjs.f(n.facts).tokenUsage},get timeline(){return y},async run(e,t,a){return Ie(e,t,Pe(),void 0,a)},runStream(e,t,a={}){let r=new AbortController,s=1e4,c=[],g=[],b=false,F=Date.now(),C=0,A=1e5,l="",K;a.signal&&(K=()=>r.abort(),a.signal.addEventListener("abort",K,{once:true}));let ie=()=>{K&&a.signal&&a.signal.removeEventListener("abort",K);},h=d=>{if(b)return;let w=g.shift();w?w(d):(c.push(d),c.length>s&&c.shift());},o=()=>{b=true,ie();for(let d of g)d(null);g.length=0;},i=null,p=a.liveContext;if(p){if(p.mode!==void 0&&!et&&(et=true,console.warn("[Directive] liveContext.mode is deprecated and has no runtime behavior \u2014 the 1.x landing ships abort-and-emit only. Remove the field; automatic re-prompt semantics will return in a follow-up RFC.")),D&&p.system===n){let Y=["agent","conversation","approvalState"],u=p.keys.filter(k=>Y.some(T=>k.startsWith(T)));u.length>0&&console.warn(`[Directive] liveContext watches bridge-state keys (${u.join(", ")}) on the orchestrator's own system. The orchestrator's own setAgentState / setConversation / approvalState writes will trigger interruptWhen, self-looping the run. Use a separate system for liveContext, or scope \`keys\` to user-fact namespaces.`);}let d=p.notifyOn??"interrupt-only",w=p.interruptWhen??(()=>true),E=new Set(p.keys),X=p.system.facts.$store;if(typeof X?.subscribe=="function"){let Y=X.toObject?.()??{};i=X.subscribe(p.keys,()=>{if(b)return;let u=X.toObject?.()??{},k=[];for(let v of p.keys)u[v]!==Y[v]&&k.push(v);if(Y=u,k.length===0)return;let T=k.filter(v=>E.has(v));if(T.length===0)return;let f=false;try{f=w(u,T);}catch(v){console.error("[Directive] liveContext.interruptWhen threw \u2014 treating as non-interrupt",v),f=false;}try{p.onContextUpdate?.(T);}catch(v){console.error("[Directive] liveContext.onContextUpdate threw",v);}f?(h({type:"interrupted",reason:"liveContext.interruptWhen",partialOutput:l,changedKeys:T}),r.abort()):d==="all-changes"&&h({type:"context_updated",changedKeys:T});});}}let O=()=>{i&&(i(),i=null);},m=(async()=>{h({type:"progress",phase:"starting",message:"Running input guardrails"});try{let d=t,w=(q.input??[]).map((u,k)=>chunkLPTC3N2Z_cjs.c(u,k,"input"));for(let u of w){let{name:k}=u,T={agentName:e.name,input:d,facts:n.facts.$store.toObject()},f=await chunkLPTC3N2Z_cjs.d(u,{input:d,agentName:e.name},T);if(!f.passed)throw h({type:"guardrail_triggered",guardrailName:k,reason:f.reason??"Input validation failed",partialOutput:l,stopped:!0}),new chunkAEX7HKNM_cjs.a({code:"INPUT_GUARDRAIL_FAILED",message:`Input guardrail "${k}" failed: ${f.reason}`,guardrailName:k,guardrailType:"input",userMessage:f.reason??"Input validation failed",agentName:e.name,input:d});f.transformed!==void 0&&(d=f.transformed);}h({type:"progress",phase:"generating",message:"Starting agent"}),n.batch(()=>{let u=chunkLPTC3N2Z_cjs.f(n.facts);chunkLPTC3N2Z_cjs.g(n.facts,{...u,status:"running",currentAgent:e.name,input:d,startedAt:Date.now()});});let E=await chunkLPTC3N2Z_cjs.e(ne,e,d,{signal:r.signal,onMessage:u=>{let k=chunkLPTC3N2Z_cjs.j(n.facts);if(chunkLPTC3N2Z_cjs.k(n.facts,[...k,u]),h({type:"message",message:u}),u.role==="assistant"&&u.content){let T=Math.ceil(u.content.length/4);C+=T,l+=u.content,l.length>A&&(l=l.slice(-A)),h({type:"token",data:u.content,tokenCount:C});}},onToolCall:async u=>{h({type:"tool_start",tool:u.name,toolCallId:u.id,arguments:u.arguments});let k=(q.toolCall??[]).map((f,v)=>chunkLPTC3N2Z_cjs.c(f,v,"toolCall"));for(let f of k){let{name:v}=f,ue={agentName:e.name,input:d,facts:n.facts.$store.toObject()},ce=await chunkLPTC3N2Z_cjs.d(f,{toolCall:u,agentName:e.name,input:d},ue);if(!ce.passed)throw h({type:"guardrail_triggered",guardrailName:v,reason:ce.reason??"Tool call blocked",partialOutput:l,stopped:!0}),new chunkAEX7HKNM_cjs.a({code:"TOOL_CALL_GUARDRAIL_FAILED",message:`Tool call guardrail "${v}" failed: ${ce.reason}`,guardrailName:v,guardrailType:"toolCall",userMessage:ce.reason??"Tool call blocked",data:{toolCall:u},agentName:e.name,input:d})}if(!de){let f=`tool-${u.id}`;h({type:"approval_required",requestId:f,toolName:u.name});let v={id:f,type:"tool_call",agentName:e.name,description:`Tool call: ${u.name}`,data:u,requestedAt:Date.now()};n.batch(()=>{let ue=chunkLPTC3N2Z_cjs.h(n.facts);chunkLPTC3N2Z_cjs.i(n.facts,{...ue,pending:[...ue.pending,v]});}),le?.(v),await Le(f,r.signal),h({type:"approval_resolved",requestId:f,approved:!0});}let T=chunkLPTC3N2Z_cjs.l(n.facts);chunkLPTC3N2Z_cjs.m(n.facts,[...T,u]),u.result&&h({type:"tool_end",tool:u.name,toolCallId:u.id,result:u.result});}},oe);h({type:"progress",phase:"finishing",message:"Running output guardrails"});let X=(q.output??[]).map((u,k)=>chunkLPTC3N2Z_cjs.c(u,k,"output"));for(let u of X){let{name:k}=u,T={agentName:e.name,input:d,facts:n.facts.$store.toObject()},f=await chunkLPTC3N2Z_cjs.d(u,{output:E.output,agentName:e.name,input:d,messages:E.messages},T);if(!f.passed)throw h({type:"guardrail_triggered",guardrailName:k,reason:f.reason??"Output validation failed",partialOutput:typeof E.output=="string"?E.output:"",stopped:!0}),new chunkAEX7HKNM_cjs.a({code:"OUTPUT_GUARDRAIL_FAILED",message:`Output guardrail "${k}" failed: ${f.reason}`,guardrailName:k,guardrailType:"output",userMessage:f.reason??"Output validation failed",agentName:e.name,input:d});f.transformed!==void 0&&(E.output=f.transformed);}n.batch(()=>{let u=chunkLPTC3N2Z_cjs.f(n.facts);chunkLPTC3N2Z_cjs.g(n.facts,{...u,status:"completed",output:E.output,tokenUsage:u.tokenUsage+E.totalTokens,turnCount:u.turnCount+E.messages.length,completedAt:Date.now()});});let Y=Date.now()-F;return h({type:"done",totalTokens:E.totalTokens,duration:Y,droppedTokens:0}),o(),E}catch(d){throw h({type:"error",error:d instanceof Error?d:new Error(String(d))}),o(),d}})();m.catch(()=>{});let x={[Symbol.asyncIterator](){return {async next(){return c.length>0?{done:false,value:c.shift()}:b?{done:true,value:void 0}:new Promise(d=>{g.push(w=>{d(w===null?{done:true,value:void 0}:{done:false,value:w});});})}}}},L=false;return m.finally(()=>{L||O();}).catch(()=>{}),{stream:x,result:m,abort:()=>{r.abort(),O(),o();},interrupt:d=>{L=true,h({type:"interrupted",reason:d??"caller.interrupt",partialOutput:l,changedKeys:[]}),r.abort();}}},async waitForIdle(e){let t=()=>chunkLPTC3N2Z_cjs.f(n.facts).status!=="running";if(t())return;let a=Date.now();for(;!t();){if(e!==void 0&&Date.now()-a>e)throw new Error("[Directive Orchestrator] waitForIdle timed out");await new Promise(r=>setTimeout(r,50));}},approve(e){n.batch(()=>{let t=chunkLPTC3N2Z_cjs.h(n.facts);if(!t.pending.some(s=>s.id===e)){D&&console.debug(`[Directive] approve() ignored: no pending request "${e}"`);return}let a=200,r=[...t.approved,e];chunkLPTC3N2Z_cjs.i(n.facts,{...t,pending:t.pending.filter(s=>s.id!==e),approved:r.length>a?r.slice(-a):r});});},reject(e,t){n.batch(()=>{let a=chunkLPTC3N2Z_cjs.h(n.facts);if(!a.pending.some(g=>g.id===e)){D&&console.debug(`[Directive] reject() ignored: no pending request "${e}"`);return}t&&D&&console.debug(`[Directive] Request ${e} rejected: ${t}`);let r={id:e,reason:t,rejectedAt:Date.now()},s=200,c=[...a.rejected,r];chunkLPTC3N2Z_cjs.i(n.facts,{...a,pending:a.pending.filter(g=>g.id!==e),rejected:c.length>s?c.slice(-s):c});});},pause(){let e=chunkLPTC3N2Z_cjs.f(n.facts);chunkLPTC3N2Z_cjs.g(n.facts,{...e,status:"paused"});},resume(){let e=chunkLPTC3N2Z_cjs.f(n.facts);e.status==="paused"&&chunkLPTC3N2Z_cjs.g(n.facts,{...e,status:e.currentAgent?"running":"idle"});},reset(){n.batch(()=>{chunkLPTC3N2Z_cjs.g(n.facts,{status:"idle",currentAgent:null,input:null,output:null,error:null,tokenUsage:0,turnCount:0,startedAt:null,completedAt:null}),chunkLPTC3N2Z_cjs.i(n.facts,{pending:[],approved:[],rejected:[]}),chunkLPTC3N2Z_cjs.k(n.facts,[]),chunkLPTC3N2Z_cjs.m(n.facts,[]),chunkLPTC3N2Z_cjs.o(n.facts,{pending:[],resolved:[],cancelled:[]}),adapterUtils.setBridgeFact(n.facts,"__budgetWarningFired",false);}),H.clear(),Q.clear();},async checkpoint(e){if(chunkLPTC3N2Z_cjs.f(n.facts).status==="running")throw new Error("[Directive] Cannot checkpoint while agent is running");if(!n.history?.export)throw new Error("[Directive] Checkpointing requires history. Set `debug: true` in orchestrator options.");let a={version:1,id:chunkLPTC3N2Z_cjs.w(),createdAt:new Date().toISOString(),label:e?.label,systemExport:n.history.export(),timelineExport:y?.export()??null,localState:{type:"single"},memoryExport:M?M.export?.()??null:null,orchestratorType:"single"};return Ce&&await Ce.save(a),a},restore(e,t){if(!chunkLPTC3N2Z_cjs.x(e))throw new Error("[Directive] Invalid checkpoint data");if(e.orchestratorType!=="single")throw new Error("[Directive] Cannot restore multi-agent checkpoint in single-agent orchestrator");if(!n.history?.import)throw new Error("[Directive] Restoring a checkpoint requires history. Set `debug: true` in orchestrator options.");n.history.import(e.systemExport),t?.restoreTimeline!==false&&e.timelineExport&&y&&y.import(e.timelineExport),e.memoryExport!==null&&M&&M.import&&M.import(e.memoryExport);},resumeBreakpoint(e,t){t&&H.set(e,t),n.batch(()=>{let a=chunkLPTC3N2Z_cjs.n(n.facts),r=[...a.resolved,e];chunkLPTC3N2Z_cjs.o(n.facts,{...a,pending:a.pending.filter(s=>s.id!==e),resolved:r.length>chunkLPTC3N2Z_cjs.s?r.slice(-chunkLPTC3N2Z_cjs.s):r});});},cancelBreakpoint(e,t){t&&Q.set(e,t),n.batch(()=>{let a=chunkLPTC3N2Z_cjs.n(n.facts),r=[...a.cancelled,e];chunkLPTC3N2Z_cjs.o(n.facts,{...a,pending:a.pending.filter(s=>s.id!==e),cancelled:r.length>chunkLPTC3N2Z_cjs.s?r.slice(-chunkLPTC3N2Z_cjs.s):r});});},getPendingBreakpoints(){return [...chunkLPTC3N2Z_cjs.n(n.facts).pending]},destroy(){n.destroy();}}}
|
|
15
|
+
exports.a=xt;//# sourceMappingURL=chunk-4LENWYLG.cjs.map
|
|
16
|
+
//# sourceMappingURL=chunk-4LENWYLG.cjs.map
|