@directive-run/ai 1.13.0 → 1.15.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/dist/anthropic.d.cts +1 -1
- package/dist/anthropic.d.ts +1 -1
- package/dist/chunk-56LYSE63.js +6 -0
- package/dist/chunk-56LYSE63.js.map +1 -0
- package/dist/chunk-5JQ2A3JK.js +2 -0
- package/dist/chunk-5JQ2A3JK.js.map +1 -0
- package/dist/chunk-7DPTXZZI.cjs +67 -0
- package/dist/chunk-7DPTXZZI.cjs.map +1 -0
- package/dist/chunk-BFSRYCOS.js +4 -0
- package/dist/chunk-BFSRYCOS.js.map +1 -0
- package/dist/chunk-C6MQ4WL6.cjs +11 -0
- package/dist/chunk-C6MQ4WL6.cjs.map +1 -0
- package/dist/chunk-CAA6Q4YY.cjs +4 -0
- package/dist/chunk-CAA6Q4YY.cjs.map +1 -0
- package/dist/chunk-D5APTMCT.cjs +16 -0
- package/dist/chunk-D5APTMCT.cjs.map +1 -0
- package/dist/chunk-FBT73WFY.js +2 -0
- package/dist/chunk-FBT73WFY.js.map +1 -0
- package/dist/chunk-FN4GUZAI.js +67 -0
- package/dist/chunk-FN4GUZAI.js.map +1 -0
- package/dist/chunk-GTB6HTZV.js +30 -0
- package/dist/chunk-GTB6HTZV.js.map +1 -0
- package/dist/chunk-HRR45JZX.js +7 -0
- package/dist/chunk-HRR45JZX.js.map +1 -0
- package/dist/chunk-IR3IHBVQ.cjs +2 -0
- package/dist/chunk-IR3IHBVQ.cjs.map +1 -0
- package/dist/chunk-J2Q5KKPN.js +37 -0
- package/dist/chunk-J2Q5KKPN.js.map +1 -0
- package/dist/chunk-K64WKZ22.cjs +2 -0
- package/dist/chunk-K64WKZ22.cjs.map +1 -0
- package/dist/chunk-MDXDPECP.cjs +30 -0
- package/dist/chunk-MDXDPECP.cjs.map +1 -0
- package/dist/chunk-PB276BX2.cjs +6 -0
- package/dist/chunk-PB276BX2.cjs.map +1 -0
- package/dist/chunk-UR5BMWEN.js +2 -0
- package/dist/chunk-UR5BMWEN.js.map +1 -0
- package/dist/chunk-V2FCPNPP.js +11 -0
- package/dist/chunk-V2FCPNPP.js.map +1 -0
- package/dist/chunk-WCQK3JOR.cjs +7 -0
- package/dist/chunk-WCQK3JOR.cjs.map +1 -0
- package/dist/chunk-WOFIBIPW.cjs +2 -0
- package/dist/chunk-WOFIBIPW.cjs.map +1 -0
- package/dist/chunk-XN5LUOVS.cjs +37 -0
- package/dist/chunk-XN5LUOVS.cjs.map +1 -0
- package/dist/chunk-YT4QN3KO.js +16 -0
- package/dist/chunk-YT4QN3KO.js.map +1 -0
- package/dist/debug-timeline-DpnRMnLU.d.cts +87 -0
- package/dist/debug-timeline-L13P-U2I.d.ts +87 -0
- package/dist/devtools.cjs +2 -0
- package/dist/devtools.cjs.map +1 -0
- package/dist/devtools.d.cts +354 -0
- package/dist/devtools.d.ts +354 -0
- package/dist/devtools.js +2 -0
- package/dist/devtools.js.map +1 -0
- package/dist/evals.cjs +2 -0
- package/dist/evals.cjs.map +1 -0
- package/dist/evals.d.cts +361 -0
- package/dist/evals.d.ts +361 -0
- package/dist/evals.js +2 -0
- package/dist/evals.js.map +1 -0
- package/dist/gemini.cjs +1 -1
- package/dist/gemini.cjs.map +1 -1
- package/dist/gemini.d.cts +1 -1
- package/dist/gemini.d.ts +1 -1
- package/dist/gemini.js +1 -1
- package/dist/gemini.js.map +1 -1
- package/dist/guardrails.cjs +2 -0
- package/dist/guardrails.cjs.map +1 -0
- package/dist/guardrails.d.cts +618 -0
- package/dist/guardrails.d.ts +618 -0
- package/dist/guardrails.js +2 -0
- package/dist/guardrails.js.map +1 -0
- package/dist/health-monitor-C6xoXrQz.d.cts +55 -0
- package/dist/health-monitor-qL9RNMH3.d.ts +55 -0
- package/dist/index.cjs +21 -99
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1197 -4735
- package/dist/index.d.ts +1197 -4735
- package/dist/index.js +21 -99
- package/dist/index.js.map +1 -1
- package/dist/mcp.cjs +2 -0
- package/dist/mcp.cjs.map +1 -0
- package/dist/mcp.d.cts +450 -0
- package/dist/mcp.d.ts +450 -0
- package/dist/mcp.js +2 -0
- package/dist/mcp.js.map +1 -0
- package/dist/multi-agent-orchestrator-KFYIDKTF.cjs +2 -0
- package/dist/{multi-agent-orchestrator-KFGTEGE5.cjs.map → multi-agent-orchestrator-KFYIDKTF.cjs.map} +1 -1
- package/dist/multi-agent-orchestrator-ZHSMWIKP.js +2 -0
- package/dist/{multi-agent-orchestrator-4PXNYRHB.js.map → multi-agent-orchestrator-ZHSMWIKP.js.map} +1 -1
- package/dist/multi-agent.cjs +2 -0
- package/dist/multi-agent.cjs.map +1 -0
- package/dist/multi-agent.d.cts +1429 -0
- package/dist/multi-agent.d.ts +1429 -0
- package/dist/multi-agent.js +2 -0
- package/dist/multi-agent.js.map +1 -0
- package/dist/ollama.d.cts +1 -1
- package/dist/ollama.d.ts +1 -1
- package/dist/openai.cjs +1 -1
- package/dist/openai.cjs.map +1 -1
- package/dist/openai.d.cts +2 -2
- package/dist/openai.d.ts +2 -2
- package/dist/openai.js +1 -1
- package/dist/openai.js.map +1 -1
- package/dist/{orchestrator-types-CTfIKk0W.d.ts → orchestrator-types-CbNE7Rjp.d.cts} +6 -139
- package/dist/{orchestrator-types-Bh8r3_Sq.d.cts → orchestrator-types-ZK9lv5Sm.d.ts} +6 -139
- package/dist/predicate.cjs +2 -0
- package/dist/predicate.cjs.map +1 -0
- package/dist/predicate.d.cts +371 -0
- package/dist/predicate.d.ts +371 -0
- package/dist/predicate.js +2 -0
- package/dist/predicate.js.map +1 -0
- package/dist/{semantic-cache-nBpQqILc.d.cts → semantic-cache-DM7ev7NQ.d.cts} +1 -1
- package/dist/{semantic-cache-nBpQqILc.d.ts → semantic-cache-DM7ev7NQ.d.ts} +1 -1
- package/dist/testing.cjs +1 -1
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.cts +4 -2
- package/dist/testing.d.ts +4 -2
- package/dist/testing.js +1 -1
- package/dist/testing.js.map +1 -1
- package/dist/{types-CRmwFnVk.d.cts → types-DJ09LjZX.d.cts} +1 -1
- package/dist/{types-CRmwFnVk.d.ts → types-DJ09LjZX.d.ts} +1 -1
- package/package.json +32 -2
- package/dist/chunk-Q3PQLWBR.js +0 -16
- package/dist/chunk-Q3PQLWBR.js.map +0 -1
- package/dist/chunk-RW4R3O5P.js +0 -72
- package/dist/chunk-RW4R3O5P.js.map +0 -1
- package/dist/chunk-X3VQ5F7D.cjs +0 -72
- package/dist/chunk-X3VQ5F7D.cjs.map +0 -1
- package/dist/chunk-XV2QSBBE.cjs +0 -16
- package/dist/chunk-XV2QSBBE.cjs.map +0 -1
- package/dist/multi-agent-orchestrator-4PXNYRHB.js +0 -2
- package/dist/multi-agent-orchestrator-KFGTEGE5.cjs +0 -2
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {a}from'./chunk-HRR45JZX.js';import {getSchemaFieldKinds,predicateHash,getOperatorsForKind,validatePredicate,validatePredicateAgainstSchema}from'@directive-run/core';function D(t){let e=5381;for(let n=0;n<t.length;n++)e=(e<<5)+e^t.charCodeAt(n);return (e>>>0).toString(16)}async function C(t){try{let e=globalThis.crypto?.subtle;if(e&&typeof e.digest=="function"){let n=new TextEncoder,r=await e.digest("SHA-256",n.encode(t)),o=new Uint8Array(r),a="";for(let i of o)a+=i.toString(16).padStart(2,"0");return a}}catch{}return D(t)}var f=class extends Error{constructor(n,r,o,a){super(n);this.attempts=r;this.errors=o;this.lastRawOutput=a;}name="PredicateFromIntentError"},H=`You emit ONLY a JSON object describing a Directive FactPredicate \u2014 no prose, no markdown fences, no explanation.
|
|
2
|
+
|
|
3
|
+
A FactPredicate is a JSON tree of fact paths and operators:
|
|
4
|
+
- Object form (preferred): { "factName": { "$op": operand }, "otherFact": { "$op": operand } }
|
|
5
|
+
- Combinators: { "$all": [predicateA, predicateB] }, { "$any": [...] }, { "$not": predicate }
|
|
6
|
+
- Bare value (equality shortcut): { "factName": value }
|
|
7
|
+
|
|
8
|
+
Operator set (CLOSED \u2014 only these are valid):
|
|
9
|
+
$eq, $ne, $gt, $gte, $lt, $lte, $in, $nin, $exists, $between,
|
|
10
|
+
$matches, $startsWith, $endsWith, $contains, $changed
|
|
11
|
+
Combinators: $all, $any, $not
|
|
12
|
+
|
|
13
|
+
Each operator is only valid for certain kinds (see below). Emit ONLY operators valid for the fact's kind.
|
|
14
|
+
`;function s(t){let e=t.nullable?" (nullable)":"";switch(t.kind){case "literal":return `literal ${JSON.stringify(t.value)} (${t.primitive})${e}`;case "enum":return `enum ${JSON.stringify(t.values)} (${t.primitive})${e}`;case "array":return `array of ${s(t.element)}${e}`;case "tuple":return `tuple [${t.elements.map(s).join(", ")}]${e}`;case "object":return `object { ${Object.entries(t.shape).map(([n,r])=>`${n}: ${s(r)}`).join(", ")} }${e}`;case "record":return `record<string, ${s(t.value)}>${e}`;case "union":return `union (${t.members.map(s).join(" | ")})${e}`;case "branded":return `branded(${s(t.inner)})${e}`;default:return `${t.kind}${e}`}}function J(t,e){let n=[H];n.push(`
|
|
15
|
+
Facts in this schema (path \u2192 kind \u2192 allowed operators):`);for(let[r,o]of t.entries()){if(e&&!r.startsWith(e))continue;let a=getOperatorsForKind(o);n.push(` ${r}: ${s(o)} \u2014 allowed: ${a.join(", ")}`);}return e&&n.push(`
|
|
16
|
+
The user intent will be over the namespace "${e}". Use only facts at or below that path.`),n.push(`
|
|
17
|
+
Respond with ONLY the JSON predicate object. No prose. No markdown fences. No "Here is...".`),n.join(`
|
|
18
|
+
`)}function M(t,e,n){let r=[];r.push("Your previous response was rejected. Original intent (still applies):"),r.push(` ${t}`);let o=null;if(typeof n=="string")r.push(`
|
|
19
|
+
Reason: ${n}`);else {r.push(`
|
|
20
|
+
Validation errors (fix every one):`),o=new Set;for(let a of n)r.push(` - path "${a.path}", op "${a.op}": ${a.reason}`),a.allowedOps&&a.allowedOps.length>0&&r.push(` \u2192 allowed operators for this fact: ${a.allowedOps.join(", ")}`),o.add(a.path);}if(r.push(`
|
|
21
|
+
Schema reminder:`),o&&o.size>0){let a=0;for(let p of o){let u=e.get(p);if(u){let y=getOperatorsForKind(u);r.push(` ${p}: ${s(u)} \u2014 allowed: ${y.join(", ")}`),a++;}else r.push(` ${p}: (not in schema \u2014 pick from the available facts below)`);}let i=e.size-a;i>0&&r.push(` \u2026and ${i} more fact(s) available \u2014 ask if you need the full list.`);}else for(let[a,i]of e.entries()){let p=getOperatorsForKind(i);r.push(` ${a}: ${s(i)} \u2014 allowed: ${p.join(", ")}`);}return r.push(`
|
|
22
|
+
Emit ONLY a corrected JSON FactPredicate object. No prose.`),r.join(`
|
|
23
|
+
`)}function B(t,e,n){if(t.length>n.maxPredicateBytes)return {ok:false,reason:`Output exceeded maxPredicateBytes=${n.maxPredicateBytes} (got ${t.length}). Emit a smaller predicate.`};let r;try{r=a(t);}catch(a){return {ok:false,reason:`Could not extract JSON from output: ${a instanceof Error?a.message:String(a)}`}}try{validatePredicate(r);}catch(a){return {ok:false,reason:`Structural validation failed: ${a instanceof Error?a.message:String(a)}`}}let o=validatePredicateAgainstSchema(r,e,{maxOperatorCount:n.maxOperatorCount,maxArrayOperandLength:n.maxArrayOperandLength});return o.ok?{ok:true,predicate:r}:{ok:false,reason:`Predicate has ${o.errors.length} schema-validation error(s).`,details:o.errors}}var Y=3,K=65536,V=256,W=1e3;async function X(t){let e=await k(t);if(e.predicate===null)throw new f(`[Directive] predicateFromIntent: failed after ${e.attempts} attempt(s). Last error: ${e.errors[e.errors.length-1]?.reason??"unknown"}`,e.attempts,e.errors,e.lastRawOutput);return e.predicate}async function k(t){let{intent:e,schema:n,runner:r,agent:o,factPath:a,maxRetries:i=Y,maxPredicateBytes:p=K,maxOperatorCount:u=V,maxArrayOperandLength:y=W,redact:w,signal:$}=t;if(typeof e!="string"||e.length===0)throw new Error("[Directive] predicateFromIntent: `intent` must be a non-empty string.");let P=w?w(e):e,l=getSchemaFieldKinds(n);if(l.size===0)throw new Error("[Directive] predicateFromIntent: schema has no introspectable facts. Pass a module schema or a bare facts record.");let R=J(l,a),O=o??{name:"predicate-emitter"},T={...O,instructions:`${O.instructions??""}
|
|
24
|
+
|
|
25
|
+
${R}`.trim()},c=[],b,d=0,I={maxPredicateBytes:p,maxOperatorCount:u,maxArrayOperandLength:y};for(;d<i+1;){if($?.aborted)throw new Error("aborted");d++;let j=d===1?`Intent: ${P}
|
|
26
|
+
|
|
27
|
+
Emit the predicate now.`:M(P,l,c[c.length-1].details??c[c.length-1].reason),m;try{m=await r(T,j,{signal:$});}catch(S){if($?.aborted)throw new Error("aborted");c.push({attempt:d,reason:`LLM runner threw: ${S instanceof Error?S.message:String(S)}`});continue}let F=typeof m.output=="string"?m.output:JSON.stringify(m.output);b=F;let h=B(F,l,I);if(h.ok)return {predicate:h.predicate,attempts:d,errors:c,lastRawOutput:b};c.push({attempt:d,reason:h.reason,details:h.details});}return {predicate:null,attempts:d,errors:c,lastRawOutput:b}}function v(t,e){let n=getSchemaFieldKinds(t),r=[];for(let[o,a]of n.entries()){if(e&&!o.startsWith(e))continue;let i=getOperatorsForKind(a);r.push(`${o}: ${s(a)} \u2014 ops: ${i.join(", ")}`);}return r.join(`
|
|
28
|
+
`)}function E(t,e){return e??`Emit a Directive FactPredicate (JSON tree of facts and operators) matching the user's intent. Schema:
|
|
29
|
+
${t}`}function G(t,e={}){let n=v(t,e.factPath),r=e.name??"emit_predicate",o=E(n,e.description);return {type:"function",function:{name:r,description:o,parameters:{type:"object",properties:{predicate:{type:"object"}},required:["predicate"]}},schemaSummary:n}}function U(t,e={}){let n=v(t,e.factPath),r=e.name??"emit_predicate",o=E(n,e.description);return {name:r,description:o,input_schema:{type:"object",properties:{predicate:{type:"object"}},required:["predicate"]},schemaSummary:n}}function Q(t,e={}){return U(t,e)}async function Z(t){let e=await k(t);if(e.predicate===null)throw new f(`[Directive] predicateFromIntentWithProvenance: failed after ${e.attempts} attempt(s). Last error: ${e.errors[e.errors.length-1]?.reason??"unknown"}`,e.attempts,e.errors,e.lastRawOutput);let n=t.redact?t.redact(t.intent):t.intent,r=t.agent?.model??"unknown",o=predicateHash(e.predicate),a=await C(n),i=t.redactIntent?{model:r,intentHash:a,attemptCount:e.attempts,emittedAt:new Date().toISOString(),predicateHash:o}:{model:r,intent:n,intentHash:a,attemptCount:e.attempts,emittedAt:new Date().toISOString(),predicateHash:o};return {predicate:e.predicate,provenance:i}}export{f as a,X as b,k as c,G as d,U as e,Q as f,Z as g};//# sourceMappingURL=chunk-GTB6HTZV.js.map
|
|
30
|
+
//# sourceMappingURL=chunk-GTB6HTZV.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/predicate-from-intent.ts"],"names":["djb2Hash","str","hash","i","hashStringSha256","raw","subtle","enc","buf","bytes","hex","b","PredicateFromIntentError","message","attempts","errors","lastRawOutput","SYSTEM_PROMPT_HEADER","renderKindForPrompt","node","nullableTag","k","v","buildSystemPrompt","kindMap","factPath","lines","path","ops","getOperatorsForKind","buildErrorFeedback","intent","pathsToShow","e","shown","remaining","validateOneAttempt","rawOutput","opts","parsed","extractJsonFromOutput","err","validatePredicate","schemaResult","validatePredicateAgainstSchema","DEFAULT_MAX_RETRIES","DEFAULT_MAX_PREDICATE_BYTES","DEFAULT_MAX_OPERATOR_COUNT","DEFAULT_MAX_ARRAY_OPERAND_LENGTH","predicateFromIntent","result","predicateFromIntentRaw","rawIntent","schema","runner","agent","maxRetries","maxPredicateBytes","maxOperatorCount","maxArrayOperandLength","redact","signal","getSchemaFieldKinds","systemPrompt","baseAgent","promptAgent","attempt","validateOpts","input","runResult","validated","buildSchemaSummary","buildDescription","summary","override","predicateToolSpecOpenAI","name","description","predicateToolSpecAnthropic","predicateToolSpec","predicateFromIntentWithProvenance","sanitizedIntent","model","predicateHashValue","predicateHash","intentHash","provenance"],"mappings":"6KAmDA,SAASA,CAAAA,CAASC,CAAAA,CAAqB,CACrC,IAAIC,CAAAA,CAAO,IAAA,CACX,IA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,EAAO,OAAS,SAAA,CAK7B6C,CAAAA,CAAqBC,aAAAA,CAAc/E,CAAAA,CAAI,SAAS,CAAA,CAGhDgF,CAAAA,CAAa,MAAMjF,CAAAA,CAAiB6E,CAAe,CAAA,CAEnDK,CAAAA,CAA4ChD,CAAAA,CAAK,YAAA,CACnD,CACE,KAAA,CAAA4C,CAAAA,CACA,UAAA,CAAAG,CAAAA,CACA,YAAA,CAAchF,CAAAA,CAAI,QAAA,CAClB,SAAA,CAAW,IAAI,IAAA,EAAK,CAAE,WAAA,EAAY,CAClC,aAAA,CAAe8E,CACjB,CAAA,CACA,CACE,KAAA,CAAAD,CAAAA,CACA,MAAA,CAAQD,CAAAA,CACR,UAAA,CAAAI,CAAAA,CACA,YAAA,CAAchF,CAAAA,CAAI,QAAA,CAClB,SAAA,CAAW,IAAI,IAAA,GAAO,WAAA,EAAY,CAClC,aAAA,CAAe8E,CACjB,CAAA,CAEJ,OAAO,CACL,SAAA,CAAW9E,CAAAA,CAAI,SAAA,CACf,UAAA,CAAAiF,CACF,CACF","file":"chunk-GTB6HTZV.js","sourcesContent":["/**\n * predicateFromIntent — let an LLM emit a typed FactPredicate as JSON,\n * structurally + semantically validated before it ever reaches your\n * constraint engine.\n *\n * Pipeline per call attempt:\n *\n * 1. Output-size check (reject before JSON.parse for DoS guard)\n * 2. JSON.parse via extractJsonFromOutput (handles surrounding prose)\n * 3. validatePredicate (structural: closed operator set, depth, JSON safety)\n * 4. Operator-count check\n * 5. validatePredicateAgainstSchema (semantic: operator-on-kind matrix)\n *\n * On any failure: a structured error message — including the offending\n * clause's path, the allowed operators for that fact's kind, and the\n * original schema kinds — is fed back to the LLM in the next attempt.\n *\n * Returns the validated FactPredicate. Throws PredicateFromIntentError\n * on retry exhaustion. NEVER returns a partial / unvalidated predicate.\n */\n\nimport {\n type FactPredicate,\n type SchemaKindNode,\n type SchemaValidationError,\n getOperatorsForKind,\n getSchemaFieldKinds,\n predicateHash,\n validatePredicate,\n validatePredicateAgainstSchema,\n} from \"@directive-run/core\";\n\nimport { extractJsonFromOutput } from \"./structured-output.js\";\nimport type { AgentLike, AgentRunner } from \"./types.js\";\n\n// ============================================================================\n// Hashing helpers\n// ============================================================================\n//\n// Provenance hashing has two flavors:\n//\n// - `predicateHash(spec)` — synchronous djb2 of a stable-stringified\n// predicate. Imported from `@directive-run/core` (the supported,\n// semver-stable surface). Two semantically-identical predicates emitted\n// with different whitespace / key-order produce the same hash. (N3)\n//\n// - `hashStringSha256(str)` — async SHA-256 via crypto.subtle when\n// available, djb2 fallback otherwise. Used for `intentHash` —\n// intents are free-form strings, no canonicalization to do.\n//\n\nfunction djb2Hash(str: string): string {\n let hash = 5381;\n for (let i = 0; i < str.length; i++) {\n hash = ((hash << 5) + hash) ^ str.charCodeAt(i);\n }\n\n return (hash >>> 0).toString(16);\n}\n\nasync function hashStringSha256(raw: string): Promise<string> {\n // Prefer crypto.subtle SHA-256 when available (browsers, Node 19+, workers).\n // Fall back to a sync djb2 — collision-prone, but adequate for telemetry\n // alongside the persisted predicate.\n try {\n const subtle = (globalThis as { crypto?: { subtle?: SubtleCrypto } }).crypto\n ?.subtle;\n if (subtle && typeof subtle.digest === \"function\") {\n const enc = new TextEncoder();\n const buf = await subtle.digest(\"SHA-256\", enc.encode(raw));\n const bytes = new Uint8Array(buf);\n let hex = \"\";\n for (const b of bytes) {\n hex += b.toString(16).padStart(2, \"0\");\n }\n\n return hex;\n }\n } catch {\n // fall through to djb2\n }\n\n return djb2Hash(raw);\n}\n\n// ============================================================================\n// Types\n// ============================================================================\n\nexport interface PredicateFromIntentOptions<_F = Record<string, unknown>> {\n /** Natural-language intent (untrusted user input — sanitize via `redact`). */\n intent: string;\n /**\n * Module schema (must expose builders with `_typeName` or `_kind`).\n * Pass either `{ facts: {...} }` or a bare `Record<string, builder>`.\n */\n schema: unknown;\n /** AgentRunner from `@directive-run/ai` adapters (createOpenAIRunner, etc.). */\n runner: AgentRunner;\n /**\n * Optional agent override. Default is `{ name: \"predicate-emitter\" }`\n * with our system prompt; pass `instructions` to append additional\n * context.\n */\n agent?: AgentLike;\n /**\n * Optional dotted-path namespace; useful for cross-module systems\n * where the LLM should emit a predicate over `auth.token` (default:\n * the schema's root facts).\n */\n factPath?: string;\n /** Max retries on validation failure. Default 3. */\n maxRetries?: number;\n /**\n * Hard byte cap on the LLM's raw output, BEFORE JSON.parse. Defaults\n * to 64 KiB. A larger predicate is rejected outright; protects\n * against multi-MB-payload DoS where the predicate is technically\n * structurally valid.\n */\n maxPredicateBytes?: number;\n /**\n * Hard cap on the number of operator clauses in the predicate.\n * Defaults to 256. Protects against `{ $any: [{ x: 1 }, … x100,000 ] }`\n * style operator-count exhaustion.\n */\n maxOperatorCount?: number;\n /**\n * Optional sanitizer applied to `intent` BEFORE it lands in the\n * system prompt. Useful for stripping or redacting user-controlled\n * content that looks like prompt-injection.\n */\n redact?: (intent: string) => string;\n /**\n * Hard cap on the length of each `$in` / `$nin` array operand the LLM\n * may emit. Defaults to 1000. Forwarded to\n * `validatePredicateAgainstSchema`'s `maxArrayOperandLength`.\n */\n maxArrayOperandLength?: number;\n /**\n * Optional `AbortSignal` for cooperative cancellation. (N6)\n *\n * The retry loop checks `signal.aborted` between attempts AND forwards\n * the signal into the runner call itself (`runner(agent, input, { signal })`).\n * Whether the in-flight LLM call honors the signal depends on the runner:\n * fetch-based adapters (the bundled OpenAI / Anthropic / Ollama runners)\n * thread it through to `fetch`, so the network call aborts mid-stream.\n * A custom runner that ignores the signal still delivers cancellation\n * at the next retry boundary.\n *\n * On abort: throws `Error(\"aborted\")`.\n *\n * NOTE: `predicateFromIntent` does NOT limit in-flight calls — callers\n * MUST wrap with a concurrency limiter (e.g. `p-limit`) to bound\n * fan-out under load.\n */\n signal?: AbortSignal;\n /**\n * When `true`, the {@link PredicateFromIntentProvenance} returned by\n * `predicateFromIntentWithProvenance` omits the raw `intent` string and\n * stores only the SHA-256 `intentHash`. Use this in PII-sensitive\n * contexts where the original intent must not be persisted. (M6)\n *\n * **Default `false` for back-compat.** For PII-sensitive deployments,\n * ALWAYS set `redactIntent: true` — the raw intent may contain\n * user-supplied content (names, emails, medical or financial details,\n * customer messages) that becomes a permanent record in\n * `provenance.intent`. The default is opt-in only because flipping it\n * now would silently strip diagnostic data from existing callers; v2\n * may flip this default (tracked in IDEAS.md).\n *\n * Pair with a `redact:` sanitizer when the intent itself must be\n * scrubbed BEFORE it lands in the LLM prompt — `redactIntent` controls\n * only what enters the provenance record, not what the LLM sees.\n */\n redactIntent?: boolean;\n}\n\nexport interface PredicateFromIntentDiagnostics<F = Record<string, unknown>> {\n /** The validated predicate (`null` if all retries failed). */\n predicate: FactPredicate<F> | null;\n /** Number of LLM calls actually made. */\n attempts: number;\n /** Errors encountered across all attempts (most recent last). */\n errors: ReadonlyArray<{\n attempt: number;\n reason: string;\n details?: readonly SchemaValidationError[];\n }>;\n /** The raw LLM output from the final attempt — useful for debugging. */\n lastRawOutput?: string;\n}\n\n/** Thrown by `predicateFromIntent` on retry exhaustion. `predicateFromIntentRaw` returns these as a diagnostics payload instead. */\nexport class PredicateFromIntentError extends Error {\n override readonly name = \"PredicateFromIntentError\";\n constructor(\n message: string,\n public readonly attempts: number,\n public readonly errors: ReadonlyArray<{\n attempt: number;\n reason: string;\n details?: readonly SchemaValidationError[];\n }>,\n public readonly lastRawOutput: string | undefined,\n ) {\n super(message);\n }\n}\n\n// ============================================================================\n// Prompt builder\n// ============================================================================\n\nconst SYSTEM_PROMPT_HEADER = `You emit ONLY a JSON object describing a Directive FactPredicate — no prose, no markdown fences, no explanation.\n\nA FactPredicate is a JSON tree of fact paths and operators:\n- Object form (preferred): { \"factName\": { \"$op\": operand }, \"otherFact\": { \"$op\": operand } }\n- Combinators: { \"$all\": [predicateA, predicateB] }, { \"$any\": [...] }, { \"$not\": predicate }\n- Bare value (equality shortcut): { \"factName\": value }\n\nOperator set (CLOSED — only these are valid):\n $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin, $exists, $between,\n $matches, $startsWith, $endsWith, $contains, $changed\nCombinators: $all, $any, $not\n\nEach operator is only valid for certain kinds (see below). Emit ONLY operators valid for the fact's kind.\n`;\n\nfunction renderKindForPrompt(node: SchemaKindNode): string {\n const nullableTag = node.nullable ? \" (nullable)\" : \"\";\n switch (node.kind) {\n case \"literal\":\n return `literal ${JSON.stringify(node.value)} (${node.primitive})${nullableTag}`;\n case \"enum\":\n return `enum ${JSON.stringify(node.values)} (${node.primitive})${nullableTag}`;\n case \"array\":\n return `array of ${renderKindForPrompt(node.element)}${nullableTag}`;\n case \"tuple\":\n return `tuple [${node.elements.map(renderKindForPrompt).join(\", \")}]${nullableTag}`;\n case \"object\":\n return `object { ${Object.entries(node.shape)\n .map(([k, v]) => `${k}: ${renderKindForPrompt(v)}`)\n .join(\", \")} }${nullableTag}`;\n case \"record\":\n return `record<string, ${renderKindForPrompt(node.value)}>${nullableTag}`;\n case \"union\":\n return `union (${node.members.map(renderKindForPrompt).join(\" | \")})${nullableTag}`;\n case \"branded\":\n return `branded(${renderKindForPrompt(node.inner)})${nullableTag}`;\n default:\n return `${node.kind}${nullableTag}`;\n }\n}\n\nfunction buildSystemPrompt(\n kindMap: Map<string, SchemaKindNode>,\n factPath?: string,\n): string {\n const lines: string[] = [SYSTEM_PROMPT_HEADER];\n\n lines.push(\"\\nFacts in this schema (path → kind → allowed operators):\");\n for (const [path, node] of kindMap.entries()) {\n if (factPath && !path.startsWith(factPath)) continue;\n const ops = getOperatorsForKind(node);\n lines.push(\n ` ${path}: ${renderKindForPrompt(node)} — allowed: ${ops.join(\", \")}`,\n );\n }\n if (factPath) {\n lines.push(\n `\\nThe user intent will be over the namespace \"${factPath}\". Use only facts at or below that path.`,\n );\n }\n\n lines.push(\n '\\nRespond with ONLY the JSON predicate object. No prose. No markdown fences. No \"Here is...\".',\n );\n\n return lines.join(\"\\n\");\n}\n\nfunction buildErrorFeedback(\n intent: string,\n kindMap: Map<string, SchemaKindNode>,\n errors: readonly SchemaValidationError[] | string,\n): string {\n const lines: string[] = [];\n lines.push(\n \"Your previous response was rejected. Original intent (still applies):\",\n );\n lines.push(` ${intent}`);\n\n // M16: When we have structured errors, scope the schema reminder to only\n // the offending fact paths instead of dumping the full schema. Falls back\n // to the full schema for non-structural failures (string errors).\n let pathsToShow: Set<string> | null = null;\n\n if (typeof errors === \"string\") {\n lines.push(`\\nReason: ${errors}`);\n } else {\n lines.push(\"\\nValidation errors (fix every one):\");\n pathsToShow = new Set();\n for (const e of errors) {\n lines.push(` - path \"${e.path}\", op \"${e.op}\": ${e.reason}`);\n if (e.allowedOps && e.allowedOps.length > 0) {\n lines.push(\n ` → allowed operators for this fact: ${e.allowedOps.join(\", \")}`,\n );\n }\n // Collect every path mentioned by an error. If a path isn't in the\n // kindMap (unknown fact), the offending key may be a dotted prefix of\n // a real fact — include the closest known ancestor too.\n pathsToShow.add(e.path);\n }\n }\n\n lines.push(\"\\nSchema reminder:\");\n if (pathsToShow && pathsToShow.size > 0) {\n let shown = 0;\n for (const path of pathsToShow) {\n const node = kindMap.get(path);\n if (node) {\n const ops = getOperatorsForKind(node);\n lines.push(\n ` ${path}: ${renderKindForPrompt(node)} — allowed: ${ops.join(\", \")}`,\n );\n shown++;\n } else {\n lines.push(\n ` ${path}: (not in schema — pick from the available facts below)`,\n );\n }\n }\n const remaining = kindMap.size - shown;\n if (remaining > 0) {\n lines.push(\n ` …and ${remaining} more fact(s) available — ask if you need the full list.`,\n );\n }\n } else {\n for (const [path, node] of kindMap.entries()) {\n const ops = getOperatorsForKind(node);\n lines.push(\n ` ${path}: ${renderKindForPrompt(node)} — allowed: ${ops.join(\", \")}`,\n );\n }\n }\n\n lines.push(\"\\nEmit ONLY a corrected JSON FactPredicate object. No prose.\");\n\n return lines.join(\"\\n\");\n}\n\n// ============================================================================\n// Validation pipeline (one attempt)\n// ============================================================================\n\nfunction validateOneAttempt(\n rawOutput: string,\n kindMap: Map<string, SchemaKindNode>,\n opts: {\n maxPredicateBytes: number;\n maxOperatorCount: number;\n maxArrayOperandLength: number;\n },\n):\n | { ok: true; predicate: unknown }\n | { ok: false; reason: string; details?: readonly SchemaValidationError[] } {\n // 1. Byte cap (pre-parse — kills 10MB-payload DoS)\n if (rawOutput.length > opts.maxPredicateBytes) {\n return {\n ok: false,\n reason: `Output exceeded maxPredicateBytes=${opts.maxPredicateBytes} (got ${rawOutput.length}). Emit a smaller predicate.`,\n };\n }\n\n // 2. JSON parse (handles surrounding prose)\n let parsed: unknown;\n try {\n parsed = extractJsonFromOutput(rawOutput);\n } catch (err) {\n return {\n ok: false,\n reason: `Could not extract JSON from output: ${\n err instanceof Error ? err.message : String(err)\n }`,\n };\n }\n\n // 3. Structural validation (closed operator set, depth, JSON-safety)\n try {\n validatePredicate(parsed);\n } catch (err) {\n return {\n ok: false,\n reason: `Structural validation failed: ${\n err instanceof Error ? err.message : String(err)\n }`,\n };\n }\n\n // 4 + 5. Operator-count cap + array-operand cap + schema validation\n const schemaResult = validatePredicateAgainstSchema(parsed, kindMap, {\n maxOperatorCount: opts.maxOperatorCount,\n maxArrayOperandLength: opts.maxArrayOperandLength,\n });\n\n if (!schemaResult.ok) {\n return {\n ok: false,\n reason: `Predicate has ${schemaResult.errors.length} schema-validation error(s).`,\n details: schemaResult.errors,\n };\n }\n\n return { ok: true, predicate: parsed };\n}\n\n// ============================================================================\n// Public API\n// ============================================================================\n\nconst DEFAULT_MAX_RETRIES = 3;\nconst DEFAULT_MAX_PREDICATE_BYTES = 65_536; // 64 KiB\nconst DEFAULT_MAX_OPERATOR_COUNT = 256;\nconst DEFAULT_MAX_ARRAY_OPERAND_LENGTH = 1000;\n\n/**\n * Ask an LLM to emit a FactPredicate matching the user's intent, then\n * validate it structurally + semantically before returning. On validation\n * failure, retries with structured error feedback in the next prompt.\n *\n * Throws {@link PredicateFromIntentError} on retry exhaustion. NEVER\n * returns a partial / unvalidated predicate.\n *\n * **Rate limiting:** this function does NOT limit in-flight calls. Wrap\n * with a concurrency limiter (e.g. `p-limit` / `Bottleneck`) before\n * exposing it to user-driven traffic. Pass an `AbortSignal` via `opts.signal`\n * for cooperative cancellation between retry attempts.\n *\n * @example\n * ```ts\n * import { createOpenAIRunner } from \"@directive-run/ai/openai\";\n * import { predicateFromIntent } from \"@directive-run/ai\";\n *\n * const runner = createOpenAIRunner({ apiKey, model: \"gpt-4o-mini\" });\n *\n * const predicate = await predicateFromIntent({\n * intent: \"checkout is unblocked when the cart total is at least 50\",\n * schema: myModule.schema,\n * runner,\n * });\n * // → { cartTotal: { $gte: 50 } }\n * ```\n */\nexport async function predicateFromIntent<F = Record<string, unknown>>(\n opts: PredicateFromIntentOptions<F>,\n): Promise<FactPredicate<F>> {\n const result = await predicateFromIntentRaw(opts);\n if (result.predicate === null) {\n throw new PredicateFromIntentError(\n `[Directive] predicateFromIntent: failed after ${result.attempts} attempt(s). Last error: ${\n result.errors[result.errors.length - 1]?.reason ?? \"unknown\"\n }`,\n result.attempts,\n result.errors,\n result.lastRawOutput,\n );\n }\n\n return result.predicate;\n}\n\n/**\n * Lower-level variant — returns the validated predicate (or null) plus\n * full diagnostics. Use when you want to surface validation telemetry,\n * preview the LLM's last raw output, or display per-attempt errors in\n * a UI.\n */\nexport async function predicateFromIntentRaw<F = Record<string, unknown>>(\n opts: PredicateFromIntentOptions<F>,\n): Promise<PredicateFromIntentDiagnostics<F>> {\n const {\n intent: rawIntent,\n schema,\n runner,\n agent,\n factPath,\n maxRetries = DEFAULT_MAX_RETRIES,\n maxPredicateBytes = DEFAULT_MAX_PREDICATE_BYTES,\n maxOperatorCount = DEFAULT_MAX_OPERATOR_COUNT,\n maxArrayOperandLength = DEFAULT_MAX_ARRAY_OPERAND_LENGTH,\n redact,\n signal,\n } = opts;\n\n if (typeof rawIntent !== \"string\" || rawIntent.length === 0) {\n throw new Error(\n \"[Directive] predicateFromIntent: `intent` must be a non-empty string.\",\n );\n }\n\n const intent = redact ? redact(rawIntent) : rawIntent;\n const kindMap = getSchemaFieldKinds(schema);\n\n if (kindMap.size === 0) {\n throw new Error(\n \"[Directive] predicateFromIntent: schema has no introspectable facts. Pass a module schema or a bare facts record.\",\n );\n }\n\n const systemPrompt = buildSystemPrompt(kindMap, factPath);\n const baseAgent: AgentLike = agent ?? { name: \"predicate-emitter\" };\n const promptAgent: AgentLike = {\n ...baseAgent,\n instructions: `${baseAgent.instructions ?? \"\"}\\n\\n${systemPrompt}`.trim(),\n };\n\n const errors: Array<{\n attempt: number;\n reason: string;\n details?: readonly SchemaValidationError[];\n }> = [];\n\n let lastRawOutput: string | undefined;\n let attempt = 0;\n const validateOpts = {\n maxPredicateBytes,\n maxOperatorCount,\n maxArrayOperandLength,\n };\n\n while (attempt < maxRetries + 1) {\n // M7: cooperative cancellation — bail between attempts when aborted.\n if (signal?.aborted) {\n throw new Error(\"aborted\");\n }\n attempt++;\n const input =\n attempt === 1\n ? `Intent: ${intent}\\n\\nEmit the predicate now.`\n : buildErrorFeedback(\n intent,\n kindMap,\n errors[errors.length - 1]!.details ??\n errors[errors.length - 1]!.reason,\n );\n\n let runResult: Awaited<ReturnType<typeof runner>>;\n try {\n // (N6) Forward the abort signal to the runner so in-flight LLM\n // calls can be cancelled mid-fetch. fetch-based adapters honor\n // this via the underlying fetch's `signal` option. Custom runners\n // that ignore the third arg fall back to between-retry cancellation.\n runResult = await runner(promptAgent, input, { signal });\n } catch (err) {\n // If the abort happened during the in-flight call (and the runner\n // honored it), surface that as a plain `Error(\"aborted\")` rather\n // than burning a retry attempt on a cancellation error.\n if (signal?.aborted) {\n throw new Error(\"aborted\");\n }\n errors.push({\n attempt,\n reason: `LLM runner threw: ${err instanceof Error ? err.message : String(err)}`,\n });\n continue;\n }\n\n const rawOutput =\n typeof runResult.output === \"string\"\n ? runResult.output\n : JSON.stringify(runResult.output);\n lastRawOutput = rawOutput;\n\n const validated = validateOneAttempt(rawOutput, kindMap, validateOpts);\n\n if (validated.ok) {\n return {\n predicate: validated.predicate as FactPredicate<F>,\n attempts: attempt,\n errors,\n lastRawOutput,\n };\n }\n\n errors.push({\n attempt,\n reason: validated.reason,\n details: validated.details,\n });\n }\n\n return {\n predicate: null,\n attempts: attempt,\n errors,\n lastRawOutput,\n };\n}\n\n// ============================================================================\n// Tool-spec presets (OpenAI + Anthropic function-calling)\n// ============================================================================\n\nexport interface PredicateToolSpecOptions {\n /** Tool name. Default `\"emit_predicate\"`. */\n name?: string;\n /** Tool description. Default: a one-liner describing predicate emission. */\n description?: string;\n /** Optional dotted-path namespace to restrict the tool's scope. */\n factPath?: string;\n}\n\n/**\n * Anthropic Messages API tool shape. Drop into the `tools: [...]` array.\n * Anthropic's API expects `input_schema` at the top level of the tool.\n */\nexport interface PredicateToolSpecAnthropic {\n name: string;\n description: string;\n input_schema: {\n type: \"object\";\n properties: { predicate: { type: \"object\" } };\n required: [\"predicate\"];\n };\n /** Human-readable schema description — embed in your tool's \"description\" if your provider concatenates them. */\n schemaSummary: string;\n}\n\n/**\n * OpenAI Chat Completions / Responses API tool shape. Drop into the\n * `tools: [...]` array. OpenAI nests the function payload under\n * `function: { name, description, parameters }`.\n */\nexport interface PredicateToolSpecOpenAI {\n type: \"function\";\n function: {\n name: string;\n description: string;\n parameters: {\n type: \"object\";\n properties: { predicate: { type: \"object\" } };\n required: [\"predicate\"];\n };\n };\n /** Human-readable schema description — embed in your tool's \"description\" if your provider concatenates them. */\n schemaSummary: string;\n}\n\n/**\n * @deprecated Use {@link PredicateToolSpecAnthropic} (or\n * {@link PredicateToolSpecOpenAI}) directly. Kept as an alias for the\n * pre-split shape that v1.12.x callers depend on; identical to\n * `PredicateToolSpecAnthropic`.\n */\nexport type PredicateToolSpec = PredicateToolSpecAnthropic;\n\nfunction buildSchemaSummary(\n schema: unknown,\n factPath: string | undefined,\n): string {\n const kindMap = getSchemaFieldKinds(schema);\n const lines: string[] = [];\n for (const [path, node] of kindMap.entries()) {\n if (factPath && !path.startsWith(factPath)) continue;\n const ops = getOperatorsForKind(node);\n lines.push(\n `${path}: ${renderKindForPrompt(node)} — ops: ${ops.join(\", \")}`,\n );\n }\n\n return lines.join(\"\\n\");\n}\n\nfunction buildDescription(summary: string, override?: string): string {\n return (\n override ??\n `Emit a Directive FactPredicate (JSON tree of facts and operators) matching the user's intent. Schema:\\n${summary}`\n );\n}\n\n/**\n * OpenAI Chat Completions / Responses API tool spec for predicate\n * emission. Drop the result into your `tools: [...]` array; the model\n * will be told to emit a predicate matching this schema.\n *\n * @example\n * ```ts\n * const tool = predicateToolSpecOpenAI(myModule.schema, { name: \"set_checkout_rule\" });\n *\n * await openai.chat.completions.create({\n * model: \"gpt-4o-mini\",\n * tools: [tool],\n * messages: [...],\n * });\n * ```\n */\nexport function predicateToolSpecOpenAI(\n schema: unknown,\n opts: PredicateToolSpecOptions = {},\n): PredicateToolSpecOpenAI {\n const summary = buildSchemaSummary(schema, opts.factPath);\n const name = opts.name ?? \"emit_predicate\";\n const description = buildDescription(summary, opts.description);\n\n return {\n type: \"function\",\n function: {\n name,\n description,\n parameters: {\n type: \"object\",\n properties: { predicate: { type: \"object\" } },\n required: [\"predicate\"],\n },\n },\n schemaSummary: summary,\n };\n}\n\n/**\n * Anthropic Messages API tool spec for predicate emission. Drop the\n * result into your `tools: [...]` array; the model will be told to emit\n * a predicate matching this schema.\n *\n * @example\n * ```ts\n * const tool = predicateToolSpecAnthropic(myModule.schema, { name: \"set_checkout_rule\" });\n *\n * await anthropic.messages.create({\n * model: \"claude-3-5-sonnet-latest\",\n * tools: [tool],\n * messages: [...],\n * });\n * ```\n */\nexport function predicateToolSpecAnthropic(\n schema: unknown,\n opts: PredicateToolSpecOptions = {},\n): PredicateToolSpecAnthropic {\n const summary = buildSchemaSummary(schema, opts.factPath);\n const name = opts.name ?? \"emit_predicate\";\n const description = buildDescription(summary, opts.description);\n\n return {\n name,\n description,\n input_schema: {\n type: \"object\",\n properties: { predicate: { type: \"object\" } },\n required: [\"predicate\"],\n },\n schemaSummary: summary,\n };\n}\n\n/**\n * @deprecated Use {@link predicateToolSpecAnthropic} (or\n * {@link predicateToolSpecOpenAI}) directly. Back-compat alias for\n * v1.12.x callers — emits the Anthropic shape, unchanged.\n */\nexport function predicateToolSpec(\n schema: unknown,\n opts: PredicateToolSpecOptions = {},\n): PredicateToolSpec {\n return predicateToolSpecAnthropic(schema, opts);\n}\n\n// ============================================================================\n// Provenance (M24)\n// ============================================================================\n\nexport interface PredicateFromIntentProvenance {\n /**\n * Resolved model name when the caller passes `agent: { model }`,\n * otherwise `\"unknown\"`.\n *\n * NOTE: most callers omit `agent` (the default `predicate-emitter`\n * agent has no model field set), so `model` is `\"unknown\"` by default.\n * v1 does NOT read provider-detected model strings from `RunResult` —\n * that requires every adapter to surface `runResult.model`, which the\n * current `AgentRunner` contract does not. Tracked for v2; callers who\n * need provider attribution today should pass `agent: { name, model: \"...\" }`.\n */\n readonly model: string;\n /**\n * Sanitized intent string actually sent to the LLM (post-redact). When\n * `redactIntent: true` was passed, this field is omitted entirely — only\n * `intentHash` is populated.\n */\n readonly intent?: string;\n /**\n * SHA-256 hex hash of the sanitized intent string (or djb2 fallback\n * when `crypto.subtle` is unavailable). Always present — even when\n * `intent` is omitted via `redactIntent`. (M6)\n */\n readonly intentHash: string;\n /** Number of LLM calls that ran before the final predicate was accepted. */\n readonly attemptCount: number;\n /** ISO timestamp when the predicate was returned. */\n readonly emittedAt: string;\n /**\n * Hash of the VALIDATED predicate (canonicalized via stableStringify\n * before hashing). Two semantically-identical predicates emitted with\n * different whitespace or key order produce the SAME `predicateHash`.\n * Sufficient as a tamper-evident pointer alongside the persisted\n * predicate. (N3)\n *\n * Renamed from `rawOutputHash` in v1.13.x — the old name hashed the\n * raw LLM output string, which made two whitespace-different responses\n * for the same logical predicate hash differently. Callers persisting\n * the old `rawOutputHash` value should re-derive it from the stored\n * predicate using `hashObject(predicate)` from `@directive-run/core/internals`.\n */\n readonly predicateHash: string;\n}\n\nexport interface PredicateFromIntentWithProvenanceResult<\n F = Record<string, unknown>,\n> {\n readonly predicate: FactPredicate<F>;\n readonly provenance: PredicateFromIntentProvenance;\n}\n\n/**\n * Like {@link predicateFromIntent} but additionally returns a structured\n * provenance record — the model name, sanitized intent (or its hash),\n * attempt count, timestamp, and a canonical hash of the validated\n * predicate.\n *\n * **Production deployments MUST persist the provenance record alongside\n * the predicate.** Without it, auditing \"where did this rule come from?\"\n * becomes guesswork.\n *\n * Throws {@link PredicateFromIntentError} on retry exhaustion — same\n * semantics as the un-provenanced variant.\n *\n * **PII guidance (M6):** pass `redactIntent: true` to omit the raw\n * intent from the provenance record and persist only the `intentHash`.\n * Useful when the intent itself is sensitive (medical, financial,\n * customer messages, etc.).\n *\n * **Hash semantics (N3):**\n * - `predicateHash` hashes the VALIDATED predicate object via stable\n * stringification — two whitespace-different LLM outputs that parse to\n * the same predicate produce the same hash.\n * - `intentHash` hashes the sanitized intent STRING (SHA-256 when\n * available, djb2 fallback).\n *\n * @example\n * ```ts\n * const { predicate, provenance } = await predicateFromIntentWithProvenance({\n * intent: \"block checkout when cart > 10k\",\n * schema: checkoutModule.schema,\n * runner,\n * agent: { name: \"predicate-emitter\", model: \"gpt-4o-mini\" },\n * redactIntent: false, // default: store both intent + intentHash\n * });\n *\n * await db.predicates.insert({\n * predicate,\n * model: provenance.model,\n * intent: provenance.intent, // omitted when redactIntent: true\n * intentHash: provenance.intentHash,\n * emittedAt: provenance.emittedAt,\n * predicateHash: provenance.predicateHash,\n * attempts: provenance.attemptCount,\n * });\n * ```\n */\nexport async function predicateFromIntentWithProvenance<\n F = Record<string, unknown>,\n>(\n opts: PredicateFromIntentOptions<F>,\n): Promise<PredicateFromIntentWithProvenanceResult<F>> {\n const raw = await predicateFromIntentRaw(opts);\n if (raw.predicate === null) {\n throw new PredicateFromIntentError(\n `[Directive] predicateFromIntentWithProvenance: failed after ${raw.attempts} attempt(s). Last error: ${\n raw.errors[raw.errors.length - 1]?.reason ?? \"unknown\"\n }`,\n raw.attempts,\n raw.errors,\n raw.lastRawOutput,\n );\n }\n\n const sanitizedIntent = opts.redact ? opts.redact(opts.intent) : opts.intent;\n const model = opts.agent?.model ?? \"unknown\";\n // (N3) Hash the VALIDATED predicate (stable canonical form), not the\n // raw LLM output — two semantically-identical responses with different\n // whitespace / key order produce the same hash. Uses the supported\n // public `predicateHash` helper from `@directive-run/core`.\n const predicateHashValue = predicateHash(raw.predicate);\n // (M6) Always hash the intent; raw intent string is included only when\n // `redactIntent` is not set.\n const intentHash = await hashStringSha256(sanitizedIntent);\n\n const provenance: PredicateFromIntentProvenance = opts.redactIntent\n ? {\n model,\n intentHash,\n attemptCount: raw.attempts,\n emittedAt: new Date().toISOString(),\n predicateHash: predicateHashValue,\n }\n : {\n model,\n intent: sanitizedIntent,\n intentHash,\n attemptCount: raw.attempts,\n emittedAt: new Date().toISOString(),\n predicateHash: predicateHashValue,\n };\n\n return {\n predicate: raw.predicate,\n provenance,\n };\n}\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import {isPrototypeSafe}from'@directive-run/core/internals';function R(t){if(!isPrototypeSafe(t))throw new Error("[Directive] structured-output: extracted JSON contains unsafe prototype keys")}var O=1048576;function k(t){if(t.length>O)throw new Error(`[Directive] Output too large for JSON extraction (${t.length} chars, max ${O}).`);let n=t.trim();try{let e=JSON.parse(n);return R(e),e}catch(e){if(e instanceof Error&&e.message.startsWith("[Directive] structured-output:"))throw e}let r=n.indexOf("{"),s=n.indexOf("["),i,f,l;if(r===-1&&s===-1)throw new Error("[Directive] No JSON object or array found in output");r===-1?(i=s,f="[",l="]"):s===-1?(i=r,f="{",l="}"):(i=Math.min(r,s),f=i===r?"{":"[",l=i===r?"}":"]");let p=0,d=false,g=false;for(let e=i;e<n.length;e++){let a=n[e];if(g){g=false;continue}if(a==="\\"){g=true;continue}if(a==='"'){d=!d;continue}if(!d){if(a===f)p++;else if(a===l&&(p--,p===0)){let u=n.slice(i,e+1),o;try{o=JSON.parse(u);}catch{let h=u.replace(/"(?:[^"\\]|\\.)*"/g,c=>c.replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t"));o=JSON.parse(h);}return R(o),o}}}throw new Error("[Directive] No valid JSON found in output")}function y(t){return t?t.issues&&t.issues.length>0?t.issues.map(n=>n.message).join("; "):t.message??"Validation failed":"Validation failed"}function J(t,n){let{schema:r,maxRetries:s=2,extractJson:i=k,schemaDescription:f}=n;if(!Number.isFinite(s)||s<0)throw new Error("[Directive] withStructuredOutput: maxRetries must be a non-negative finite number.");let l=f??r.description??"the specified JSON schema";return async(p,d,g)=>{let e={...p,instructions:`${p.instructions??""}
|
|
2
|
+
|
|
3
|
+
IMPORTANT: Respond with valid JSON matching ${l}. Output ONLY the JSON object, no additional text or markdown formatting.`},a,u;for(let o=0;o<=s;o++){let h=o===0?d:`${d}
|
|
4
|
+
|
|
5
|
+
Your previous response was not valid JSON. Error: ${u}
|
|
6
|
+
Please try again with valid JSON only.`,c=await t(e,h,g);a=c;let x=typeof c.output=="string"?c.output:JSON.stringify(c.output);try{let m=i(x),w=r.safeParse(m);if(w.success)return {...c,output:w.data};u=y(w.error);}catch(m){u=m instanceof Error?m.message:String(m);}}throw new S(`[Directive] Failed to get valid structured output after ${s+1} attempts: ${u}`,a)}}var S=class extends Error{lastResult;constructor(n,r){super(n),this.name="StructuredOutputError",this.lastResult=r;}};export{k as a,J as b,S as c};//# sourceMappingURL=chunk-HRR45JZX.js.map
|
|
7
|
+
//# sourceMappingURL=chunk-HRR45JZX.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/structured-output.ts"],"names":["assertPrototypeSafe","value","isPrototypeSafe","MAX_EXTRACT_LENGTH","extractJsonFromOutput","output","trimmed","parsed","err","objectStart","arrayStart","start","openChar","closeChar","depth","inString","escaped","i","char","jsonStr","sanitized","match","formatValidationError","error","issue","withStructuredOutput","runner","config","schema","maxRetries","extractJson","schemaDescription","schemaPrompt","agent","input","options","structuredAgent","lastResult","lastError","attempt","effectiveInput","result","outputStr","extracted","StructuredOutputError","message"],"mappings":"4DAsCA,SAASA,CAAAA,CAAoBC,EAAsB,CAC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,CACF,IAAME,EAAYb,CAAAA,CAAYY,CAAS,EACjCnC,CAAAA,CAASqB,CAAAA,CAAO,UAAUe,CAAS,CAAA,CAEzC,GAAIpC,CAAAA,CAAO,OAAA,CACT,OAAO,CACL,GAAGkC,EACH,MAAA,CAAQlC,CAAAA,CAAO,IACjB,CAAA,CAGF+B,CAAAA,CAAYhB,CAAAA,CAAsBf,EAAO,KAAK,EAChD,OAASC,CAAAA,CAAK,CACZ8B,EAAY9B,CAAAA,YAAe,KAAA,CAAQA,EAAI,OAAA,CAAU,MAAA,CAAOA,CAAG,EAC7D,CACF,CAGA,MAAM,IAAIoC,EACR,CAAA,wDAAA,EAA2Df,CAAAA,CAAa,CAAC,CAAA,WAAA,EAAcS,CAAS,CAAA,CAAA,CAChGD,CACF,CACF,CACF,CAGO,IAAMO,CAAAA,CAAN,cAAoC,KAAM,CACtC,WAET,WAAA,CAAYC,CAAAA,CAAiBR,EAAiC,CAC5D,KAAA,CAAMQ,CAAO,CAAA,CACb,IAAA,CAAK,KAAO,uBAAA,CACZ,IAAA,CAAK,UAAA,CAAaR,EACpB,CACF","file":"chunk-HRR45JZX.js","sourcesContent":["/**\n * P6: Structured Outputs — Schema validation with auto-retry for LLM responses.\n *\n * Turns unreliable text output into typed, validated data. Appends JSON schema\n * instructions to the system prompt and retries with error feedback on parse failure.\n *\n * Works with any Zod-compatible schema (any object with a `safeParse` method).\n *\n * @module\n *\n * @example\n * ```typescript\n * import { z } from \"zod\";\n * import { withStructuredOutput, StructuredOutputError } from '@directive-run/ai';\n *\n * const SentimentSchema = z.object({\n * sentiment: z.enum([\"positive\", \"negative\", \"neutral\"]),\n * confidence: z.number().min(0).max(1),\n * });\n *\n * const runner = withStructuredOutput(baseRunner, {\n * schema: SentimentSchema,\n * maxRetries: 2,\n * });\n *\n * const result = await runner(agent, \"Analyze: I love this product!\");\n * // result.output is typed as { sentiment: string; confidence: number }\n * ```\n */\n\nimport { isPrototypeSafe } from \"@directive-run/core/internals\";\nimport type { AgentLike, AgentRunner, RunOptions, RunResult } from \"./types.js\";\n\n/**\n * Guard against prototype pollution in LLM-extracted JSON. LLM output is\n * attacker-controllable via prompt injection — payloads like\n * `{\"__proto__\":{\"polluted\":true}}` would otherwise seed pollution downstream.\n */\nfunction assertPrototypeSafe(value: unknown): void {\n if (!isPrototypeSafe(value)) {\n throw new Error(\n \"[Directive] structured-output: extracted JSON contains unsafe prototype keys\",\n );\n }\n}\n\n// ============================================================================\n// Types\n// ============================================================================\n\n/**\n * Zod-compatible schema duck type — any object with a `safeParse` method.\n *\n * This interface allows structured outputs to work with Zod, Valibot,\n * or any validation library that implements this pattern.\n *\n * @example\n * ```typescript\n * import { z } from \"zod\";\n *\n * // Zod schemas implement SafeParseable automatically\n * const schema = z.object({ name: z.string() });\n *\n * // Custom schema\n * const custom: SafeParseable<{ name: string }> = {\n * safeParse(value) {\n * if (typeof value === \"object\" && value && \"name\" in value) {\n * return { success: true, data: value as { name: string } };\n * }\n * return { success: false, error: { message: \"Missing name field\" } };\n * },\n * };\n * ```\n */\nexport interface SafeParseable<T = unknown> {\n safeParse(value: unknown): SafeParseResult<T>;\n /** Optional: schema description injected into the system prompt. */\n description?: string;\n}\n\nexport interface SafeParseResult<T> {\n success: boolean;\n data?: T;\n error?: { message?: string; issues?: Array<{ message: string }> };\n}\n\nexport interface StructuredOutputConfig<T = unknown> {\n /** Zod-compatible schema with safeParse. */\n schema: SafeParseable<T>;\n /** Max retries on parse/validation failure. @default 2 */\n maxRetries?: number;\n /** Custom JSON extractor. Default: finds first `{...}` or `[...]` in output. */\n extractJson?: (output: string) => unknown;\n /** Schema description to inject into system prompt. Auto-derived from schema.description if available. */\n schemaDescription?: string;\n}\n\n// ============================================================================\n// Helpers\n// ============================================================================\n\n/** Maximum output length to process (1MB). */\nconst MAX_EXTRACT_LENGTH = 1_048_576;\n\n/** Default JSON extractor — finds the first `{...}` or `[...]` in output. */\nexport function extractJsonFromOutput(output: string): unknown {\n if (output.length > MAX_EXTRACT_LENGTH) {\n throw new Error(\n `[Directive] Output too large for JSON extraction (${output.length} chars, max ${MAX_EXTRACT_LENGTH}).`,\n );\n }\n\n const trimmed = output.trim();\n\n // Try direct parse first\n try {\n const parsed = JSON.parse(trimmed);\n assertPrototypeSafe(parsed);\n\n return parsed;\n } catch (err) {\n // Re-throw prototype-safety errors immediately — they are NOT parse errors\n if (\n err instanceof Error &&\n err.message.startsWith(\"[Directive] structured-output:\")\n ) {\n throw err;\n }\n // Continue to extraction on plain parse failures\n }\n\n // Try to find JSON object or array\n const objectStart = trimmed.indexOf(\"{\");\n const arrayStart = trimmed.indexOf(\"[\");\n\n let start: number;\n let openChar: string;\n let closeChar: string;\n\n if (objectStart === -1 && arrayStart === -1) {\n throw new Error(\"[Directive] No JSON object or array found in output\");\n }\n\n if (objectStart === -1) {\n start = arrayStart;\n openChar = \"[\";\n closeChar = \"]\";\n } else if (arrayStart === -1) {\n start = objectStart;\n openChar = \"{\";\n closeChar = \"}\";\n } else {\n start = Math.min(objectStart, arrayStart);\n openChar = start === objectStart ? \"{\" : \"[\";\n closeChar = start === objectStart ? \"}\" : \"]\";\n }\n\n // Find matching closing bracket\n let depth = 0;\n let inString = false;\n let escaped = false;\n\n for (let i = start; i < trimmed.length; i++) {\n const char = trimmed[i]!;\n\n if (escaped) {\n escaped = false;\n continue;\n }\n\n if (char === \"\\\\\") {\n escaped = true;\n continue;\n }\n\n if (char === '\"') {\n inString = !inString;\n continue;\n }\n\n if (inString) {\n continue;\n }\n\n if (char === openChar) {\n depth++;\n } else if (char === closeChar) {\n depth--;\n if (depth === 0) {\n const jsonStr = trimmed.slice(start, i + 1);\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(jsonStr);\n } catch {\n // LLMs emit literal newlines inside JSON string values — escape them\n const sanitized = jsonStr.replace(/\"(?:[^\"\\\\]|\\\\.)*\"/g, (match) =>\n match\n .replace(/\\n/g, \"\\\\n\")\n .replace(/\\r/g, \"\\\\r\")\n .replace(/\\t/g, \"\\\\t\"),\n );\n\n parsed = JSON.parse(sanitized);\n }\n\n assertPrototypeSafe(parsed);\n\n return parsed;\n }\n }\n }\n\n throw new Error(\"[Directive] No valid JSON found in output\");\n}\n\n/** Format validation errors for feedback. */\nfunction formatValidationError(\n error: SafeParseResult<unknown>[\"error\"],\n): string {\n if (!error) {\n return \"Validation failed\";\n }\n\n if (error.issues && error.issues.length > 0) {\n return error.issues.map((issue) => issue.message).join(\"; \");\n }\n\n return error.message ?? \"Validation failed\";\n}\n\n// ============================================================================\n// Wrapper\n// ============================================================================\n\n/**\n * Wrap an AgentRunner with structured output parsing and validation.\n *\n * @example\n * ```typescript\n * import { z } from \"zod\";\n *\n * const SentimentSchema = z.object({\n * sentiment: z.enum([\"positive\", \"negative\", \"neutral\"]),\n * confidence: z.number().min(0).max(1),\n * });\n *\n * const runner = withStructuredOutput(baseRunner, {\n * schema: SentimentSchema,\n * maxRetries: 2,\n * });\n *\n * const result = await runner(agent, \"Analyze: I love this product!\");\n * // result.output is typed as { sentiment: string; confidence: number }\n * ```\n */\nexport function withStructuredOutput<T = unknown>(\n runner: AgentRunner,\n config: StructuredOutputConfig<T>,\n): AgentRunner {\n const {\n schema,\n maxRetries = 2,\n extractJson = extractJsonFromOutput,\n schemaDescription,\n } = config;\n\n // Validate config\n if (!Number.isFinite(maxRetries) || maxRetries < 0) {\n throw new Error(\n \"[Directive] withStructuredOutput: maxRetries must be a non-negative finite number.\",\n );\n }\n\n const schemaPrompt =\n schemaDescription ?? schema.description ?? \"the specified JSON schema\";\n\n // The returned runner produces `T` from the schema. We cast at the\n // boundary to satisfy the `AgentRunner` generic while keeping output type-safe.\n return async <_T = unknown>(\n agent: AgentLike,\n input: string,\n options?: RunOptions,\n ): Promise<RunResult<_T>> => {\n // Append JSON instruction to agent's system prompt\n const structuredAgent: AgentLike = {\n ...agent,\n instructions: `${agent.instructions ?? \"\"}\\n\\nIMPORTANT: Respond with valid JSON matching ${schemaPrompt}. Output ONLY the JSON object, no additional text or markdown formatting.`,\n };\n\n let lastResult: RunResult<unknown> | undefined;\n let lastError: string | undefined;\n\n for (let attempt = 0; attempt <= maxRetries; attempt++) {\n // On retries, append error feedback as additional input\n const effectiveInput =\n attempt === 0\n ? input\n : `${input}\\n\\nYour previous response was not valid JSON. Error: ${lastError}\\nPlease try again with valid JSON only.`;\n\n const result = await runner(structuredAgent, effectiveInput, options);\n lastResult = result;\n\n // Try to extract and validate JSON\n const outputStr =\n typeof result.output === \"string\"\n ? result.output\n : JSON.stringify(result.output);\n\n try {\n const extracted = extractJson(outputStr);\n const parsed = schema.safeParse(extracted);\n\n if (parsed.success) {\n return {\n ...result,\n output: parsed.data as _T,\n };\n }\n\n lastError = formatValidationError(parsed.error);\n } catch (err) {\n lastError = err instanceof Error ? err.message : String(err);\n }\n }\n\n // All retries exhausted — throw with context\n throw new StructuredOutputError(\n `[Directive] Failed to get valid structured output after ${maxRetries + 1} attempts: ${lastError}`,\n lastResult,\n );\n };\n}\n\n/** Error thrown when structured output parsing fails after all retries. */\nexport class StructuredOutputError extends Error {\n readonly lastResult: RunResult<unknown> | undefined;\n\n constructor(message: string, lastResult?: RunResult<unknown>) {\n super(message);\n this.name = \"StructuredOutputError\";\n this.lastResult = lastResult;\n }\n}\n"]}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
'use strict';function L(o){let{transport:c,timeline:v,healthMonitor:i,getSnapshot:f,getBreakpointState:h,onResumeBreakpoint:p,onCancelBreakpoint:b,getScratchpadState:k,getDerivedState:T,onForkFromSnapshot:g,batchSize:M=1,batchIntervalMs:E=50,healthPushIntervalMs:O=0,authenticate:C}=o,w=o.maxClients??50,R=`devtools_${crypto.randomUUID()}`,s=new Set,d=new Set,y=[],D=null,_=null;function t(e,a){try{e.send(JSON.stringify(a));}catch{s.delete(e);}}function l(e){let a=JSON.stringify(e),n=[...s];for(let m of n)try{m.send(a);}catch{s.delete(m);}}function A(){y.length!==0&&(y.length===1?l({type:"event",event:y[0]}):l({type:"event_batch",events:y}),y=[]);}let N=v.subscribe(e=>{if(s.size!==0){if(M<=1){l({type:"event",event:e});return}y.push(e),y.length>=M&&A();}});M>1&&E>0&&(D=setInterval(A,E)),O>0&&i&&(_=setInterval(()=>{s.size>0&&l({type:"health",metrics:i.getAllMetrics()});},O));function B(e,a){let n;try{n=JSON.parse(a);}catch{t(e,{type:"error",code:"INVALID_JSON",message:"Could not parse message"});return}if(!n||typeof n!="object"||typeof n.type!="string"){t(e,{type:"error",code:"INVALID_MESSAGE",message:"Missing type field"});return}let m=Date.now(),x=I.get(e)??0;if(m-x<P){t(e,{type:"error",code:"RATE_LIMITED",message:"Too many requests"});return}if(I.set(e,m),n.type==="authenticate"){if(!C){t(e,{type:"error",code:"UNKNOWN_COMMAND",message:"Authentication not configured on this server"});return}if(!d.has(e)){t(e,{type:"error",code:"ALREADY_AUTHENTICATED",message:"Already authenticated"});return}if(typeof n.token!="string"){t(e,{type:"error",code:"AUTH_FAILED",message:"Missing token"}),e.close(),d.delete(e);return}let r=C(n.token),u=S=>{S?(d.delete(e),s.add(e),t(e,{type:"welcome",version:1,sessionId:R,timestamp:Date.now()})):(t(e,{type:"error",code:"AUTH_FAILED",message:"Invalid token"}),d.delete(e),e.close());};r instanceof Promise?r.then(u).catch(()=>{t(e,{type:"error",code:"AUTH_FAILED",message:"Authentication error"}),d.delete(e),e.close();}):u(r);return}if(d.has(e)){t(e,{type:"error",code:"AUTH_REQUIRED",message:"Authentication required"});return}switch(n.type){case "ping":t(e,{type:"pong",timestamp:Date.now()});break;case "request_snapshot":f?t(e,{type:"snapshot",data:f()}):t(e,{type:"error",code:"NO_SNAPSHOT",message:"Snapshot provider not configured"});break;case "request_health":i?t(e,{type:"health",metrics:i.getAllMetrics()}):t(e,{type:"error",code:"NO_HEALTH",message:"Health monitor not configured"});break;case "request_events":{let r=v.getEvents(),u=n.since,S=u!=null?r.filter(H=>H.id>u):r;t(e,{type:"event_batch",events:S});break}case "request_breakpoints":h?t(e,{type:"breakpoints",state:h()}):t(e,{type:"error",code:"NO_BREAKPOINTS",message:"Breakpoint provider not configured"});break;case "resume_breakpoint":if(p&&typeof n.breakpointId=="string"){let r=n.modifications?{input:n.modifications.input,skip:n.modifications.skip}:void 0;p(n.breakpointId,r);}else t(e,{type:"error",code:"NO_BREAKPOINTS",message:"Breakpoint resume not configured"});break;case "cancel_breakpoint":if(b&&typeof n.breakpointId=="string"){let r=typeof n.reason=="string"?n.reason:void 0;b(n.breakpointId,r);}else t(e,{type:"error",code:"NO_BREAKPOINTS",message:"Breakpoint cancel not configured"});break;case "export_session":t(e,{type:"event_batch",events:v.getEvents()});break;case "import_session":{if(typeof n.data!="string")t(e,{type:"error",code:"INVALID_DATA",message:"Missing data field for import"});else if(n.data.length>10485760)t(e,{type:"error",code:"IMPORT_TOO_LARGE",message:`Import data exceeds ${10485760/1024/1024} MB limit`});else try{v.import(n.data),t(e,{type:"event_batch",events:v.getEvents()});}catch(u){let S=u instanceof Error?u.message:String(u);t(e,{type:"error",code:"IMPORT_FAILED",message:S});}break}case "request_scratchpad":k?t(e,{type:"scratchpad_state",data:k()}):t(e,{type:"error",code:"NO_SCRATCHPAD",message:"Scratchpad provider not configured"});break;case "request_derived":T?t(e,{type:"derived_state",data:T()}):t(e,{type:"error",code:"NO_DERIVED",message:"Derived state provider not configured"});break;case "fork_from_snapshot":{if(g&&typeof n.eventId=="number")try{let r=g(n.eventId);t(e,{type:"fork_complete",eventId:n.eventId,newEventCount:r.newEventCount});}catch(r){let u=r instanceof Error?r.message:String(r);t(e,{type:"error",code:"FORK_FAILED",message:u});}else t(e,{type:"error",code:"NO_FORK",message:"Fork provider not configured"});break}default:t(e,{type:"error",code:"UNKNOWN_COMMAND",message:`Unknown message type: ${String(n.type).slice(0,100)}`});}}let I=new Map,P=50;return c.onConnection((e,a,n)=>{if(s.size+d.size>=w){try{let m={type:"error",code:"MAX_CLIENTS",message:"Connection limit reached"};e.send(JSON.stringify(m));}catch{}e.close();return}C?d.add(e):(s.add(e),t(e,{type:"welcome",version:1,sessionId:R,timestamp:Date.now()})),a(m=>B(e,m)),n(()=>{s.delete(e),d.delete(e),I.delete(e);});}),{get clientCount(){return s.size},broadcast(e){l(e);},pushHealth(){i&&s.size>0&&l({type:"health",metrics:i.getAllMetrics()});},pushBreakpoints(){h&&s.size>0&&l({type:"breakpoints",state:h()});},pushScratchpadUpdate(e,a){s.size>0&&l({type:"scratchpad_update",key:e,value:a});},pushDerivedUpdate(e,a){s.size>0&&l({type:"derived_update",id:e,value:a});},pushTokenStream(e,a,n){s.size>0&&l({type:"token_stream",agentId:e,tokens:a,tokenCount:n});},pushStreamDone(e,a){s.size>0&&l({type:"stream_done",agentId:e,totalTokens:a});},close(){N(),D&&(clearInterval(D),D=null),_&&(clearInterval(_),_=null),A();for(let e of s)try{e.close();}catch{}for(let e of d)try{e.close();}catch{}s.clear(),d.clear(),I.clear(),c.close();}}}async function z(o,c={}){if(!o.timeline)throw new Error("[Directive DevTools] Orchestrator must have debug: true to use DevTools");let v=await F({port:c.port??4040,host:c.host??"localhost"});return L({transport:v,timeline:o.timeline,healthMonitor:o.healthMonitor,healthPushIntervalMs:c.healthPushIntervalMs??5e3,batchSize:c.batchSize,authenticate:c.authenticate,getSnapshot:o.getAllAgentStates?()=>{let i=o.getAllAgentStates();return {timestamp:Date.now(),agents:i,eventCount:o.timeline.getEvents().length}}:void 0,getBreakpointState:o.getPendingBreakpoints?()=>({pending:o.getPendingBreakpoints(),resolved:[],cancelled:[]}):void 0,onResumeBreakpoint:o.resumeBreakpoint,onCancelBreakpoint:o.cancelBreakpoint,getScratchpadState:o.getScratchpadState,getDerivedState:o.getDerivedState,onForkFromSnapshot:o.timeline?.forkFrom?i=>(o.timeline.forkFrom(i),{newEventCount:o.timeline.getEvents().length}):void 0})}async function F(o={}){let c=o.port??4040,v=o.host??"localhost",{WebSocketServer:i}=await import('ws'),f=new i({port:c,host:v,maxPayload:o.maxPayloadBytes??1048576}),h=null;return f.on("connection",p=>{let b=null,k=null,T={send(g){p.readyState===p.OPEN&&p.send(g);},close(){p.close();}};p.on("message",g=>{b&&b(g.toString());}),p.on("close",()=>{k&&k();}),h?.(T,g=>{b=g;},g=>{k=g;});}),{onConnection(p){h=p;},close(){f.close();}}}exports.a=L;exports.b=z;exports.c=F;//# sourceMappingURL=chunk-IR3IHBVQ.cjs.map
|
|
2
|
+
//# sourceMappingURL=chunk-IR3IHBVQ.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/devtools-server.ts"],"names":["createDevToolsServer","config","transport","timeline","healthMonitor","getSnapshot","getBreakpointState","onResumeBreakpoint","onCancelBreakpoint","getScratchpadState","getDerivedState","onForkFromSnapshot","batchSize","batchIntervalMs","healthPushIntervalMs","authenticate","maxClients","sessionId","clients","pendingAuthClients","batchBuffer","batchTimer","healthTimer","sendToClient","client","message","broadcastMessage","data","snapshot","flushBatch","unsubscribeTimeline","event","handleClientMessage","raw","msg","now","last","clientLastMessage","MIN_MESSAGE_INTERVAL_MS","result","handleResult","valid","events","since","filtered","e","mods","safeReason","err","errMsg","onMessage","onClose","key","value","id","agentId","tokens","tokenCount","totalTokens","connectDevTools","orchestrator","options","createWsTransport","agents","eventId","port","host","WebSocketServer","wss","connectionHandler","ws","messageHandler","closeHandler","handler"],"mappings":"aAuOO,SAASA,EACdC,CAAAA,CACgB,CAChB,GAAM,CACJ,SAAA,CAAAC,EACA,QAAA,CAAAC,CAAAA,CACA,aAAA,CAAAC,CAAAA,CACA,YAAAC,CAAAA,CACA,kBAAA,CAAAC,EACA,kBAAA,CAAAC,CAAAA,CACA,mBAAAC,CAAAA,CACA,kBAAA,CAAAC,CAAAA,CACA,eAAA,CAAAC,EACA,kBAAA,CAAAC,CAAAA,CACA,UAAAC,CAAAA,CAAY,CAAA,CACZ,gBAAAC,CAAAA,CAAkB,EAAA,CAClB,qBAAAC,CAAAA,CAAuB,CAAA,CACvB,aAAAC,CACF,CAAA,CAAId,EACEe,CAAAA,CAAaf,CAAAA,CAAO,YAAc,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,KAAAF,CAAAA,CACA,IAAA,CAAAC,EACK,UAAA,CAAYjE,CAAAA,CAAO,eAAA,EAAmB,OAC7C,CAAC,CAAA,CAEGoE,CAAAA,CAMO,KAGX,OAAAD,CAAAA,CAAI,GAAG,YAAA,CAAeE,CAAAA,EAAY,CAChC,IAAIC,EAAkD,IAAA,CAClDC,CAAAA,CAAoC,KAElChD,CAAAA,CAAyB,CAC7B,KAAKG,CAAAA,CAAc,CACb2C,CAAAA,CAAG,UAAA,GAAeA,EAAG,IAAA,EACvBA,CAAAA,CAAG,KAAK3C,CAAI,EAEhB,EACA,KAAA,EAAQ,CACN2C,EAAG,KAAA,GACL,CACF,CAAA,CAEAA,CAAAA,CAAG,GAAG,SAAA,CAAYrC,CAAAA,EAAa,CACzBsC,CAAAA,EACFA,CAAAA,CAAetC,CAAAA,CAAI,QAAA,EAAU,EAEjC,CAAC,EAEDqC,CAAAA,CAAG,EAAA,CAAG,QAAS,IAAM,CACfE,GACFA,CAAAA,GAEJ,CAAC,CAAA,CAEDH,CAAAA,GACE7C,EACCiD,CAAAA,EAAY,CACXF,EAAiBE,EACnB,CAAA,CACCA,CAAAA,EAAY,CACXD,EAAeC,EACjB,CACF,EACF,CAAC,CAAA,CAEM,CACL,YAAA,CAAaA,CAAAA,CAAS,CACpBJ,CAAAA,CAAoBI,EACtB,EACA,KAAA,EAAQ,CACNL,EAAI,KAAA,GACN,CACF,CACF","file":"chunk-IR3IHBVQ.cjs","sourcesContent":["/**\n * DevTools Server — WebSocket-based bridge between orchestrators and DevTools UI.\n *\n * Streams debug timeline events, health metrics, breakpoint state, and system\n * snapshots in real-time to connected DevTools clients. Accepts commands from\n * clients to resume/cancel breakpoints and request snapshots.\n *\n * Transport-agnostic: works with any WebSocket implementation (ws, Bun, Deno)\n * via the {@link DevToolsTransport} interface.\n *\n * @module\n */\n\nimport type { DebugTimeline } from \"./debug-timeline.js\";\nimport type { AgentHealthMetrics, HealthMonitor } from \"./health-monitor.js\";\nimport type { BreakpointState, DebugEvent } from \"./types.js\";\n\n// ============================================================================\n// Transport Interface (WebSocket abstraction)\n// ============================================================================\n\n/** A connected DevTools client */\nexport interface DevToolsClient {\n /** Send a JSON-serializable message to this client */\n send(data: string): void;\n /** Close the connection */\n close(): void;\n}\n\n/**\n * Transport layer for the DevTools server.\n *\n * Implement this interface to bridge any WebSocket library (ws, Bun.serve, Deno.serve).\n *\n * @example Node.js with `ws`:\n * ```typescript\n * import { WebSocketServer } from \"ws\";\n *\n * function createWsTransport(port: number): DevToolsTransport {\n * const wss = new WebSocketServer({ port });\n * let onConnect: ((client: DevToolsClient) => void) | null = null;\n *\n * wss.on(\"connection\", (ws) => {\n * const client: DevToolsClient = {\n * send: (data) => { if (ws.readyState === ws.OPEN) ws.send(data); },\n * close: () => ws.close(),\n * };\n * ws.on(\"message\", (raw) => {\n * if (client._onMessage) client._onMessage(raw.toString());\n * });\n * ws.on(\"close\", () => {\n * if (client._onClose) client._onClose();\n * });\n * onConnect?.(client);\n * });\n *\n * return {\n * onConnection(handler) { onConnect = handler; },\n * close() { wss.close(); },\n * };\n * }\n * ```\n */\nexport interface DevToolsTransport {\n /** Register a handler for new client connections */\n onConnection(\n handler: (\n client: DevToolsClient,\n onMessage: (handler: (data: string) => void) => void,\n onClose: (handler: () => void) => void,\n ) => void,\n ): void;\n /** Shut down the transport */\n close(): void;\n}\n\n// ============================================================================\n// Protocol Messages\n// ============================================================================\n\n/** Messages sent FROM the server TO clients */\nexport type DevToolsServerMessage =\n | { type: \"welcome\"; version: number; sessionId: string; timestamp: number }\n | { type: \"event\"; event: DebugEvent }\n | { type: \"event_batch\"; events: DebugEvent[] }\n | { type: \"snapshot\"; data: DevToolsSnapshot }\n | { type: \"health\"; metrics: Record<string, AgentHealthMetrics> }\n | { type: \"breakpoints\"; state: BreakpointState }\n | { type: \"pong\"; timestamp: number }\n // Scratchpad & derived state\n | { type: \"scratchpad_state\"; data: Record<string, unknown> }\n | { type: \"scratchpad_update\"; key: string; value: unknown }\n | { type: \"derived_state\"; data: Record<string, unknown> }\n | { type: \"derived_update\"; id: string; value: unknown }\n // Fork\n | { type: \"fork_complete\"; eventId: number; newEventCount: number }\n // Token streaming\n | {\n type: \"token_stream\";\n agentId: string;\n tokens: string;\n tokenCount: number;\n }\n | { type: \"stream_done\"; agentId: string; totalTokens: number }\n | { type: \"error\"; code: string; message: string };\n\n/** Messages sent FROM clients TO the server */\nexport type DevToolsClientMessage =\n | { type: \"authenticate\"; token: string }\n | { type: \"request_snapshot\" }\n | { type: \"request_health\" }\n | { type: \"request_events\"; since?: number }\n | { type: \"request_breakpoints\" }\n | {\n type: \"resume_breakpoint\";\n breakpointId: string;\n modifications?: { input?: string; skip?: boolean };\n }\n | { type: \"cancel_breakpoint\"; breakpointId: string; reason?: string }\n | { type: \"export_session\" }\n | { type: \"import_session\"; data: string }\n // Scratchpad & derived requests\n | { type: \"request_scratchpad\" }\n | { type: \"request_derived\" }\n // Fork\n | { type: \"fork_from_snapshot\"; eventId: number }\n | { type: \"ping\" };\n\n/** System snapshot sent to clients on demand */\nexport interface DevToolsSnapshot {\n timestamp: number;\n agents: Record<\n string,\n {\n status: string;\n lastInput?: string;\n lastOutput?: unknown;\n totalTokens: number;\n runCount: number;\n }\n >;\n coordinator?: { globalTokens: number; status: string };\n derived?: Record<string, unknown>;\n eventCount: number;\n}\n\n// ============================================================================\n// Server Configuration\n// ============================================================================\n\n/** Configuration for the DevTools server */\nexport interface DevToolsServerConfig {\n /** Transport to use for WebSocket connections */\n transport: DevToolsTransport;\n /** Debug timeline to subscribe to */\n timeline: DebugTimeline;\n /** Health monitor for metrics (optional) */\n healthMonitor?: HealthMonitor | null;\n /** Callback to get current agent states for snapshots */\n getSnapshot?: () => DevToolsSnapshot;\n /** Callback to get current breakpoint state */\n getBreakpointState?: () => BreakpointState;\n /** Callback to resume a breakpoint */\n onResumeBreakpoint?: (\n id: string,\n modifications?: { input?: string; skip?: boolean },\n ) => void;\n /** Callback to cancel a breakpoint */\n onCancelBreakpoint?: (id: string, reason?: string) => void;\n /** Callback to get current scratchpad state */\n getScratchpadState?: () => Record<string, unknown>;\n /** Callback to get current derived state */\n getDerivedState?: () => Record<string, unknown>;\n /** Callback to fork from a snapshot event */\n onForkFromSnapshot?: (eventId: number) => { newEventCount: number };\n /** Maximum events to batch before flushing. Default: 1 (no batching) */\n batchSize?: number;\n /** Flush interval for batched events (ms). Default: 50 */\n batchIntervalMs?: number;\n /** Health metrics push interval (ms). 0 = no auto-push. Default: 0 */\n healthPushIntervalMs?: number;\n /** Maximum connected clients. Default: 50 */\n maxClients?: number;\n /** Token authentication callback. When set, new connections must send an `authenticate` message before receiving data. */\n authenticate?: (token: string) => boolean | Promise<boolean>;\n}\n\n// ============================================================================\n// Server Instance\n// ============================================================================\n\n/** DevTools server instance */\nexport interface DevToolsServer {\n /** Number of connected clients */\n readonly clientCount: number;\n /** Broadcast a message to all connected clients */\n broadcast(message: DevToolsServerMessage): void;\n /** Push current health metrics to all clients */\n pushHealth(): void;\n /** Push current breakpoint state to all clients */\n pushBreakpoints(): void;\n /** Push a scratchpad key update to all clients */\n pushScratchpadUpdate(key: string, value: unknown): void;\n /** Push a derived value update to all clients */\n pushDerivedUpdate(id: string, value: unknown): void;\n /** Push streaming tokens to all clients */\n pushTokenStream(agentId: string, tokens: string, tokenCount: number): void;\n /** Signal stream completion to all clients */\n pushStreamDone(agentId: string, totalTokens: number): void;\n /** Shut down the server and disconnect all clients */\n close(): void;\n}\n\nconst PROTOCOL_VERSION = 1;\n\n/**\n * Create a DevTools server that bridges orchestrator state to DevTools UI clients.\n *\n * @example\n * ```typescript\n * const server = createDevToolsServer({\n * transport: createWsTransport(4040),\n * timeline: orchestrator.timeline!,\n * healthMonitor: orchestrator.healthMonitor,\n * getSnapshot: () => buildSnapshot(orchestrator),\n * getBreakpointState: () => orchestrator.getBreakpointState(),\n * onResumeBreakpoint: (id, mods) => orchestrator.resumeBreakpoint(id, mods),\n * onCancelBreakpoint: (id, reason) => orchestrator.cancelBreakpoint(id, reason),\n * });\n * ```\n */\nexport function createDevToolsServer(\n config: DevToolsServerConfig,\n): DevToolsServer {\n const {\n transport,\n timeline,\n healthMonitor,\n getSnapshot,\n getBreakpointState,\n onResumeBreakpoint,\n onCancelBreakpoint,\n getScratchpadState,\n getDerivedState,\n onForkFromSnapshot,\n batchSize = 1,\n batchIntervalMs = 50,\n healthPushIntervalMs = 0,\n authenticate,\n } = config;\n const maxClients = config.maxClients ?? 50;\n\n const sessionId = `devtools_${crypto.randomUUID()}`;\n const clients = new Set<DevToolsClient>();\n const pendingAuthClients = new Set<DevToolsClient>();\n let batchBuffer: DebugEvent[] = [];\n let batchTimer: ReturnType<typeof setInterval> | null = null;\n let healthTimer: ReturnType<typeof setInterval> | null = null;\n\n // ------------------------------------------------------------------\n // Helpers\n // ------------------------------------------------------------------\n\n function sendToClient(\n client: DevToolsClient,\n message: DevToolsServerMessage,\n ): void {\n try {\n client.send(JSON.stringify(message));\n } catch {\n // Client may have disconnected — remove silently\n clients.delete(client);\n }\n }\n\n function broadcastMessage(message: DevToolsServerMessage): void {\n const data = JSON.stringify(message);\n const snapshot = [...clients];\n for (const client of snapshot) {\n try {\n client.send(data);\n } catch {\n clients.delete(client);\n }\n }\n }\n\n function flushBatch(): void {\n if (batchBuffer.length === 0) {\n return;\n }\n\n if (batchBuffer.length === 1) {\n broadcastMessage({ type: \"event\", event: batchBuffer[0]! });\n } else {\n broadcastMessage({ type: \"event_batch\", events: batchBuffer });\n }\n\n batchBuffer = [];\n }\n\n // ------------------------------------------------------------------\n // Timeline subscription\n // ------------------------------------------------------------------\n\n const unsubscribeTimeline: () => void = timeline.subscribe(\n (event: DebugEvent) => {\n if (clients.size === 0) {\n return;\n }\n\n if (batchSize <= 1) {\n broadcastMessage({ type: \"event\", event });\n\n return;\n }\n\n batchBuffer.push(event);\n if (batchBuffer.length >= batchSize) {\n flushBatch();\n }\n },\n );\n\n // Start batch flush timer if batching enabled\n if (batchSize > 1 && batchIntervalMs > 0) {\n batchTimer = setInterval(flushBatch, batchIntervalMs);\n }\n\n // Start health push timer if configured\n if (healthPushIntervalMs > 0 && healthMonitor) {\n healthTimer = setInterval(() => {\n if (clients.size > 0) {\n broadcastMessage({\n type: \"health\",\n metrics: healthMonitor.getAllMetrics(),\n });\n }\n }, healthPushIntervalMs);\n }\n\n // ------------------------------------------------------------------\n // Client message handler\n // ------------------------------------------------------------------\n\n function handleClientMessage(client: DevToolsClient, raw: string): void {\n let msg: DevToolsClientMessage;\n try {\n msg = JSON.parse(raw) as DevToolsClientMessage;\n } catch {\n sendToClient(client, {\n type: \"error\",\n code: \"INVALID_JSON\",\n message: \"Could not parse message\",\n });\n\n return;\n }\n\n if (!msg || typeof msg !== \"object\" || typeof msg.type !== \"string\") {\n sendToClient(client, {\n type: \"error\",\n code: \"INVALID_MESSAGE\",\n message: \"Missing type field\",\n });\n\n return;\n }\n\n const now = Date.now();\n const last = clientLastMessage.get(client) ?? 0;\n if (now - last < MIN_MESSAGE_INTERVAL_MS) {\n sendToClient(client, {\n type: \"error\",\n code: \"RATE_LIMITED\",\n message: \"Too many requests\",\n });\n\n return;\n }\n clientLastMessage.set(client, now);\n\n // Handle authenticate message\n if (msg.type === \"authenticate\") {\n if (!authenticate) {\n // No auth configured — treat as unknown command\n sendToClient(client, {\n type: \"error\",\n code: \"UNKNOWN_COMMAND\",\n message: \"Authentication not configured on this server\",\n });\n\n return;\n }\n\n if (!pendingAuthClients.has(client)) {\n // Already authenticated\n sendToClient(client, {\n type: \"error\",\n code: \"ALREADY_AUTHENTICATED\",\n message: \"Already authenticated\",\n });\n\n return;\n }\n\n if (typeof msg.token !== \"string\") {\n sendToClient(client, {\n type: \"error\",\n code: \"AUTH_FAILED\",\n message: \"Missing token\",\n });\n client.close();\n pendingAuthClients.delete(client);\n\n return;\n }\n\n const result = authenticate(msg.token);\n const handleResult = (valid: boolean) => {\n if (valid) {\n pendingAuthClients.delete(client);\n clients.add(client);\n sendToClient(client, {\n type: \"welcome\",\n version: PROTOCOL_VERSION,\n sessionId,\n timestamp: Date.now(),\n });\n } else {\n sendToClient(client, {\n type: \"error\",\n code: \"AUTH_FAILED\",\n message: \"Invalid token\",\n });\n pendingAuthClients.delete(client);\n client.close();\n }\n };\n\n if (result instanceof Promise) {\n result.then(handleResult).catch(() => {\n sendToClient(client, {\n type: \"error\",\n code: \"AUTH_FAILED\",\n message: \"Authentication error\",\n });\n pendingAuthClients.delete(client);\n client.close();\n });\n } else {\n handleResult(result);\n }\n\n return;\n }\n\n // Reject commands from clients pending authentication\n if (pendingAuthClients.has(client)) {\n sendToClient(client, {\n type: \"error\",\n code: \"AUTH_REQUIRED\",\n message: \"Authentication required\",\n });\n\n return;\n }\n\n switch (msg.type) {\n case \"ping\":\n sendToClient(client, { type: \"pong\", timestamp: Date.now() });\n break;\n\n case \"request_snapshot\":\n if (getSnapshot) {\n sendToClient(client, { type: \"snapshot\", data: getSnapshot() });\n } else {\n sendToClient(client, {\n type: \"error\",\n code: \"NO_SNAPSHOT\",\n message: \"Snapshot provider not configured\",\n });\n }\n break;\n\n case \"request_health\":\n if (healthMonitor) {\n sendToClient(client, {\n type: \"health\",\n metrics: healthMonitor.getAllMetrics(),\n });\n } else {\n sendToClient(client, {\n type: \"error\",\n code: \"NO_HEALTH\",\n message: \"Health monitor not configured\",\n });\n }\n break;\n\n case \"request_events\": {\n const events = timeline.getEvents();\n const since = msg.since;\n const filtered =\n since != null ? events.filter((e) => e.id > since) : events;\n sendToClient(client, { type: \"event_batch\", events: filtered });\n break;\n }\n\n case \"request_breakpoints\":\n if (getBreakpointState) {\n sendToClient(client, {\n type: \"breakpoints\",\n state: getBreakpointState(),\n });\n } else {\n sendToClient(client, {\n type: \"error\",\n code: \"NO_BREAKPOINTS\",\n message: \"Breakpoint provider not configured\",\n });\n }\n break;\n\n case \"resume_breakpoint\":\n if (onResumeBreakpoint && typeof msg.breakpointId === \"string\") {\n // Sanitize: only extract known fields to prevent prototype pollution\n const mods = msg.modifications\n ? { input: msg.modifications.input, skip: msg.modifications.skip }\n : undefined;\n onResumeBreakpoint(msg.breakpointId, mods);\n } else {\n sendToClient(client, {\n type: \"error\",\n code: \"NO_BREAKPOINTS\",\n message: \"Breakpoint resume not configured\",\n });\n }\n break;\n\n case \"cancel_breakpoint\":\n if (onCancelBreakpoint && typeof msg.breakpointId === \"string\") {\n const safeReason =\n typeof msg.reason === \"string\" ? msg.reason : undefined;\n onCancelBreakpoint(msg.breakpointId, safeReason);\n } else {\n sendToClient(client, {\n type: \"error\",\n code: \"NO_BREAKPOINTS\",\n message: \"Breakpoint cancel not configured\",\n });\n }\n break;\n\n case \"export_session\":\n sendToClient(client, {\n type: \"event_batch\",\n events: timeline.getEvents(),\n });\n break;\n\n case \"import_session\": {\n const MAX_IMPORT_SIZE = 10 * 1024 * 1024; // ~10M characters (string .length check, not byte size)\n if (typeof msg.data !== \"string\") {\n sendToClient(client, {\n type: \"error\",\n code: \"INVALID_DATA\",\n message: \"Missing data field for import\",\n });\n } else if (msg.data.length > MAX_IMPORT_SIZE) {\n sendToClient(client, {\n type: \"error\",\n code: \"IMPORT_TOO_LARGE\",\n message: `Import data exceeds ${MAX_IMPORT_SIZE / 1024 / 1024} MB limit`,\n });\n } else {\n try {\n timeline.import(msg.data);\n sendToClient(client, {\n type: \"event_batch\",\n events: timeline.getEvents(),\n });\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n sendToClient(client, {\n type: \"error\",\n code: \"IMPORT_FAILED\",\n message: errMsg,\n });\n }\n }\n break;\n }\n\n case \"request_scratchpad\":\n if (getScratchpadState) {\n sendToClient(client, {\n type: \"scratchpad_state\",\n data: getScratchpadState(),\n });\n } else {\n sendToClient(client, {\n type: \"error\",\n code: \"NO_SCRATCHPAD\",\n message: \"Scratchpad provider not configured\",\n });\n }\n break;\n\n case \"request_derived\":\n if (getDerivedState) {\n sendToClient(client, {\n type: \"derived_state\",\n data: getDerivedState(),\n });\n } else {\n sendToClient(client, {\n type: \"error\",\n code: \"NO_DERIVED\",\n message: \"Derived state provider not configured\",\n });\n }\n break;\n\n case \"fork_from_snapshot\": {\n if (onForkFromSnapshot && typeof msg.eventId === \"number\") {\n try {\n const result = onForkFromSnapshot(msg.eventId);\n sendToClient(client, {\n type: \"fork_complete\",\n eventId: msg.eventId,\n newEventCount: result.newEventCount,\n });\n } catch (err) {\n const errMsg = err instanceof Error ? err.message : String(err);\n sendToClient(client, {\n type: \"error\",\n code: \"FORK_FAILED\",\n message: errMsg,\n });\n }\n } else {\n sendToClient(client, {\n type: \"error\",\n code: \"NO_FORK\",\n message: \"Fork provider not configured\",\n });\n }\n break;\n }\n\n default:\n sendToClient(client, {\n type: \"error\",\n code: \"UNKNOWN_COMMAND\",\n message: `Unknown message type: ${String((msg as { type: string }).type).slice(0, 100)}`,\n });\n }\n }\n\n // ------------------------------------------------------------------\n // Rate limiting\n // ------------------------------------------------------------------\n\n const clientLastMessage = new Map<DevToolsClient, number>();\n const MIN_MESSAGE_INTERVAL_MS = 50;\n\n // ------------------------------------------------------------------\n // Connection handler\n // ------------------------------------------------------------------\n\n transport.onConnection((client, onMessage, onClose) => {\n if (clients.size + pendingAuthClients.size >= maxClients) {\n try {\n const msg: DevToolsServerMessage = {\n type: \"error\",\n code: \"MAX_CLIENTS\",\n message: \"Connection limit reached\",\n };\n client.send(JSON.stringify(msg));\n } catch {\n /* ignore */\n }\n client.close();\n\n return;\n }\n\n if (authenticate) {\n // Hold in pending state until client sends authenticate message\n pendingAuthClients.add(client);\n } else {\n // No auth — add directly and send welcome\n clients.add(client);\n sendToClient(client, {\n type: \"welcome\",\n version: PROTOCOL_VERSION,\n sessionId,\n timestamp: Date.now(),\n });\n }\n\n onMessage((data) => handleClientMessage(client, data));\n onClose(() => {\n clients.delete(client);\n pendingAuthClients.delete(client);\n clientLastMessage.delete(client);\n });\n });\n\n // ------------------------------------------------------------------\n // Public interface\n // ------------------------------------------------------------------\n\n return {\n get clientCount(): number {\n return clients.size;\n },\n\n broadcast(message: DevToolsServerMessage): void {\n broadcastMessage(message);\n },\n\n pushHealth(): void {\n if (healthMonitor && clients.size > 0) {\n broadcastMessage({\n type: \"health\",\n metrics: healthMonitor.getAllMetrics(),\n });\n }\n },\n\n pushBreakpoints(): void {\n if (getBreakpointState && clients.size > 0) {\n broadcastMessage({ type: \"breakpoints\", state: getBreakpointState() });\n }\n },\n\n pushScratchpadUpdate(key: string, value: unknown): void {\n if (clients.size > 0) {\n broadcastMessage({ type: \"scratchpad_update\", key, value });\n }\n },\n\n pushDerivedUpdate(id: string, value: unknown): void {\n if (clients.size > 0) {\n broadcastMessage({ type: \"derived_update\", id, value });\n }\n },\n\n pushTokenStream(agentId: string, tokens: string, tokenCount: number): void {\n if (clients.size > 0) {\n broadcastMessage({ type: \"token_stream\", agentId, tokens, tokenCount });\n }\n },\n\n pushStreamDone(agentId: string, totalTokens: number): void {\n if (clients.size > 0) {\n broadcastMessage({ type: \"stream_done\", agentId, totalTokens });\n }\n },\n\n close(): void {\n unsubscribeTimeline();\n\n if (batchTimer) {\n clearInterval(batchTimer);\n batchTimer = null;\n }\n\n if (healthTimer) {\n clearInterval(healthTimer);\n healthTimer = null;\n }\n\n flushBatch();\n\n for (const client of clients) {\n try {\n client.close();\n } catch {\n // Ignore close errors\n }\n }\n for (const client of pendingAuthClients) {\n try {\n client.close();\n } catch {\n // Ignore close errors\n }\n }\n clients.clear();\n pendingAuthClients.clear();\n clientLastMessage.clear();\n\n transport.close();\n },\n };\n}\n\n// ============================================================================\n// Orchestrator Connector\n// ============================================================================\n\n/** Options for connecting DevTools to an orchestrator */\nexport interface ConnectDevToolsOptions {\n /** Port for the WebSocket server. Default: 4040 */\n port?: number;\n /** Host to bind to. Default: \"localhost\" */\n host?: string;\n /** Health metrics push interval (ms). Default: 5000 */\n healthPushIntervalMs?: number;\n /** Event batching size. Default: 1 (no batching) */\n batchSize?: number;\n /** Token authentication callback. When set, clients must authenticate before receiving data. */\n authenticate?: (token: string) => boolean | Promise<boolean>;\n}\n\n/** Minimal orchestrator interface for DevTools connection */\nexport interface DevToolsCompatibleOrchestrator {\n timeline: {\n subscribe: (listener: (event: DebugEvent) => void) => () => void;\n getEvents: () => DebugEvent[];\n import: (json: string) => void;\n export: () => string;\n forkFrom?: (eventId: number) => void;\n } | null;\n healthMonitor?: {\n getAllMetrics: () => Record<string, AgentHealthMetrics>;\n } | null;\n getPendingBreakpoints?: () => Array<{\n id: string;\n type: string;\n agentId: string;\n input: string;\n label?: string;\n requestedAt: number;\n }>;\n resumeBreakpoint?: (\n id: string,\n modifications?: { input?: string; skip?: boolean },\n ) => void;\n cancelBreakpoint?: (id: string, reason?: string) => void;\n getAllAgentStates?: () => Record<\n string,\n {\n status: string;\n lastInput?: string;\n lastOutput?: unknown;\n totalTokens: number;\n runCount: number;\n }\n >;\n /** Get current scratchpad state (multi-agent only) */\n getScratchpadState?: () => Record<string, unknown>;\n /** Get current derived values (multi-agent only) */\n getDerivedState?: () => Record<string, unknown>;\n}\n\n/**\n * Connect DevTools to an orchestrator instance.\n *\n * Convenience function that creates a WebSocket transport and DevTools server,\n * automatically wiring up the orchestrator's timeline, health monitor, and breakpoint system.\n *\n * Requires the `ws` package: `npm install ws`\n *\n * **Security:** Binding to `0.0.0.0` exposes the server to all network interfaces.\n * Only do this behind a firewall or with proper authentication.\n *\n * @example\n * ```typescript\n * const orchestrator = createMultiAgentOrchestrator({ debug: true, ... });\n * const devtools = await connectDevTools(orchestrator, { port: 4040 });\n *\n * // Later, clean up:\n * devtools.close();\n * ```\n */\nexport async function connectDevTools(\n orchestrator: DevToolsCompatibleOrchestrator,\n options: ConnectDevToolsOptions = {},\n): Promise<DevToolsServer> {\n if (!orchestrator.timeline) {\n throw new Error(\n \"[Directive DevTools] Orchestrator must have debug: true to use DevTools\",\n );\n }\n\n const transport = await createWsTransport({\n port: options.port ?? 4040,\n host: options.host ?? \"localhost\",\n });\n\n return createDevToolsServer({\n transport,\n timeline: orchestrator.timeline as unknown as DebugTimeline,\n healthMonitor: orchestrator.healthMonitor as HealthMonitor | undefined,\n healthPushIntervalMs: options.healthPushIntervalMs ?? 5000,\n batchSize: options.batchSize,\n authenticate: options.authenticate,\n getSnapshot: orchestrator.getAllAgentStates\n ? () => {\n const agents = orchestrator.getAllAgentStates!();\n\n return {\n timestamp: Date.now(),\n agents,\n eventCount: orchestrator.timeline!.getEvents().length,\n };\n }\n : undefined,\n getBreakpointState: orchestrator.getPendingBreakpoints\n ? () => ({\n pending: orchestrator.getPendingBreakpoints!(),\n resolved: [],\n cancelled: [],\n })\n : undefined,\n onResumeBreakpoint: orchestrator.resumeBreakpoint,\n onCancelBreakpoint: orchestrator.cancelBreakpoint,\n getScratchpadState: orchestrator.getScratchpadState,\n getDerivedState: orchestrator.getDerivedState,\n onForkFromSnapshot: orchestrator.timeline?.forkFrom\n ? (eventId: number) => {\n orchestrator.timeline!.forkFrom!(eventId);\n const newEventCount = orchestrator.timeline!.getEvents().length;\n\n return { newEventCount };\n }\n : undefined,\n });\n}\n\n// ============================================================================\n// Node.js ws Transport Helper\n// ============================================================================\n\n/**\n * Configuration for the built-in Node.js `ws` transport.\n *\n * Requires the `ws` package to be installed: `npm install ws`\n */\nexport interface WsTransportConfig {\n /** Port to listen on. Default: 4040 */\n port?: number;\n /** Host to bind to. Default: \"localhost\" */\n host?: string;\n /** Maximum incoming message size in bytes. Default: 1048576 (1MB) */\n maxPayloadBytes?: number;\n}\n\n/**\n * Create a DevTools transport using the Node.js `ws` WebSocket library.\n *\n * This is a convenience helper — you can implement {@link DevToolsTransport}\n * with any WebSocket library.\n *\n * @example\n * ```typescript\n * const transport = await createWsTransport({ port: 4040 });\n * const server = createDevToolsServer({ transport, timeline });\n * ```\n */\nexport async function createWsTransport(\n config: WsTransportConfig = {},\n): Promise<DevToolsTransport> {\n const port = config.port ?? 4040;\n const host = config.host ?? \"localhost\";\n\n // Dynamic import so ws is not a hard dependency\n const { WebSocketServer } = await import(\"ws\");\n // maxPayload is supported at runtime but missing from @types/ws ServerOptions\n const wss = new WebSocketServer({\n port,\n host,\n ...{ maxPayload: config.maxPayloadBytes ?? 1_048_576 },\n });\n\n let connectionHandler:\n | ((\n client: DevToolsClient,\n onMessage: (handler: (data: string) => void) => void,\n onClose: (handler: () => void) => void,\n ) => void)\n | null = null;\n\n // biome-ignore lint/suspicious/noExplicitAny: ws types resolved at runtime via dynamic import\n wss.on(\"connection\", (ws: any) => {\n let messageHandler: ((data: string) => void) | null = null;\n let closeHandler: (() => void) | null = null;\n\n const client: DevToolsClient = {\n send(data: string) {\n if (ws.readyState === ws.OPEN) {\n ws.send(data);\n }\n },\n close() {\n ws.close();\n },\n };\n\n ws.on(\"message\", (raw: any) => {\n if (messageHandler) {\n messageHandler(raw.toString());\n }\n });\n\n ws.on(\"close\", () => {\n if (closeHandler) {\n closeHandler();\n }\n });\n\n connectionHandler?.(\n client,\n (handler) => {\n messageHandler = handler;\n },\n (handler) => {\n closeHandler = handler;\n },\n );\n });\n\n return {\n onConnection(handler) {\n connectionHandler = handler;\n },\n close() {\n wss.close();\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {e}from'./chunk-ZFLHWJ56.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
|
+
|
|
3
|
+
Input: {{input}}
|
|
4
|
+
Expected: {{expected}}
|
|
5
|
+
Actual Output: {{output}}
|
|
6
|
+
|
|
7
|
+
Respond with ONLY a JSON object: {"score": <number>, "reason": "<brief explanation>"}`;return {name:"judge",fn:async r=>{let s=Date.now(),a=n.replaceAll("{{input}}",r.testCase.input).replaceAll("{{expected}}",r.testCase.expected??"N/A").replaceAll("{{output}}",String(r.result.output)),t=e$1.timeoutMs??3e4,o=new AbortController,u=setTimeout(()=>o.abort(),t),i=e$1.signal?e([e$1.signal,o.signal]):o.signal;try{let v=(await e$1.runner(e$1.judge,a,{signal:i})).output;if(typeof v!="string")return {score:0,passed:!1,reason:"Judge returned non-string output",durationMs:Date.now()-s};let l=JSON.parse(v),S=Math.max(0,Math.min(1,l.score));return {score:S,passed:S>=.5,reason:l.reason??`Judge score: ${S}`,durationMs:Date.now()-s}}catch(c){return {score:0,passed:false,reason:`Judge error: ${c instanceof Error?c.message:String(c)}`,durationMs:Date.now()-s}}finally{clearTimeout(u);}},threshold:.5,weight:1.5}}function z(e={}){let n=e.mode??"contains",r=e.caseInsensitive??true;return {name:"match",fn:s=>{let a=Date.now(),t=s.testCase.expected;if(!t)return {score:1,passed:true,reason:"No expected output to match",durationMs:Date.now()-a};let o=String(s.result.output),u=r?o.toLowerCase():o,i=r?t.toLowerCase():t,c=false;if(n==="exact")c=u===i;else if(n==="contains")c=u.includes(i);else if(n==="regex"){if(t.length>500)return {score:0,passed:false,reason:`Regex pattern too long (${t.length} chars, max 500)`,durationMs:Date.now()-a};if(/([+*}])\)([+*{])/.test(t)||/([+*}])\]([+*{])/.test(t))return {score:0,passed:false,reason:"Pattern contains dangerous nested quantifiers",durationMs:Date.now()-a};try{c=new RegExp(t,r?"i":"").test(o);}catch{return {score:0,passed:false,reason:`Invalid regex pattern: ${t}`,durationMs:Date.now()-a}}}return {score:c?1:0,passed:c,reason:c?`Output ${n} match`:`Output does not ${n} match expected`,durationMs:Date.now()-a}},threshold:1,weight:1}}var L=`You are evaluating an AI agent's output for faithfulness to the provided context.
|
|
8
|
+
|
|
9
|
+
Faithfulness measures whether all claims in the output are supported by the context.
|
|
10
|
+
Score 1.0 if every claim is grounded in the context.
|
|
11
|
+
Score 0.0 if the output contains fabricated information not in the context.
|
|
12
|
+
|
|
13
|
+
Context: {{context}}
|
|
14
|
+
Agent Output: {{output}}
|
|
15
|
+
|
|
16
|
+
Respond with ONLY a JSON object: {"score": <number 0.0-1.0>, "reason": "<brief explanation>"}`;function X(e$1){return {name:"faithfulness",fn:async n=>{let r=Date.now(),s=n.testCase.context??n.testCase.expected;if(!s)return {score:1,passed:true,reason:"No context provided for faithfulness check",durationMs:Date.now()-r};let a=L.replaceAll("{{context}}",s).replaceAll("{{output}}",String(n.result.output)),t=e$1.timeoutMs??3e4,o=new AbortController,u=setTimeout(()=>o.abort(),t),i=e$1.signal?e([e$1.signal,o.signal]):o.signal;try{let v=(await e$1.runner(e$1.judge,a,{signal:i})).output;if(typeof v!="string")return {score:0,passed:!1,reason:"Judge returned non-string output",durationMs:Date.now()-r};let l=JSON.parse(v),S=Math.max(0,Math.min(1,l.score));return {score:S,passed:S>=.7,reason:l.reason??`Faithfulness score: ${S}`,durationMs:Date.now()-r}}catch(c){return {score:0,passed:false,reason:`Faithfulness eval error: ${c instanceof Error?c.message:String(c)}`,durationMs:Date.now()-r}}finally{clearTimeout(u);}},threshold:.7,weight:1.5}}var $=`You are evaluating an AI agent's output for relevance to the user's question.
|
|
17
|
+
|
|
18
|
+
Relevance measures whether the output directly addresses the question asked.
|
|
19
|
+
Score 1.0 if the output fully and directly answers the question.
|
|
20
|
+
Score 0.0 if the output is completely off-topic or irrelevant.
|
|
21
|
+
|
|
22
|
+
User Question: {{input}}
|
|
23
|
+
Agent Output: {{output}}
|
|
24
|
+
|
|
25
|
+
Respond with ONLY a JSON object: {"score": <number 0.0-1.0>, "reason": "<brief explanation>"}`;function H(e$1){return {name:"relevance",fn:async n=>{let r=Date.now(),s=$.replaceAll("{{input}}",n.testCase.input).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??`Relevance score: ${l}`,durationMs:Date.now()-r}}catch(i){return {score:0,passed:false,reason:`Relevance eval error: ${i instanceof Error?i.message:String(i)}`,durationMs:Date.now()-r}}finally{clearTimeout(o);}},threshold:.7,weight:1.5}}var j=`You are evaluating an AI agent's output for coherence and logical consistency.
|
|
26
|
+
|
|
27
|
+
Coherence measures whether the output is well-structured, logically consistent,
|
|
28
|
+
and flows naturally. Check for contradictions, non-sequiturs, and clarity.
|
|
29
|
+
Score 1.0 if the output is perfectly coherent and well-organized.
|
|
30
|
+
Score 0.0 if the output is incoherent, contradictory, or disorganized.
|
|
31
|
+
|
|
32
|
+
Agent Output: {{output}}
|
|
33
|
+
|
|
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
|
+
${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-J2Q5KKPN.js.map
|
|
37
|
+
//# sourceMappingURL=chunk-J2Q5KKPN.js.map
|
|
@@ -0,0 +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,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,CA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* 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"]}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
'use strict';var core=require('@directive-run/core');var n=class extends Error{code;guardrailName;guardrailType;userMessage;agentName;constructor(e){super(e.message,{cause:e.cause}),this.name="GuardrailError",this.code=e.code,this.guardrailName=e.guardrailName,this.guardrailType=e.guardrailType,this.userMessage=e.userMessage??e.message,this.agentName=e.agentName,Object.defineProperty(this,"input",{value:e.input,enumerable:false,writable:false,configurable:false}),Object.defineProperty(this,"data",{value:e.data,enumerable:false,writable:false,configurable:false});}toJSON(){return {name:this.name,code:this.code,message:this.message,guardrailName:this.guardrailName,guardrailType:this.guardrailType,userMessage:this.userMessage,agentName:this.agentName}}};function g(r){return r instanceof n}var a="__agent",o="__approval",s="__conversation",i="__toolCalls",u="__breakpoints";var d="__scratchpad",l={facts:{[a]:core.t.object(),[o]:core.t.object(),[s]:core.t.array(),[i]:core.t.array(),[u]:core.t.object()},derivations:{},events:{},requirements:{}};exports.a=n;exports.b=g;exports.c=a;exports.d=o;exports.e=s;exports.f=i;exports.g=u;exports.h=d;exports.i=l;//# sourceMappingURL=chunk-K64WKZ22.cjs.map
|
|
2
|
+
//# sourceMappingURL=chunk-K64WKZ22.cjs.map
|