@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
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { M as Message$1,
|
|
2
|
-
import {
|
|
1
|
+
import { M as Message$1, q as RunResult, b as AgentLike, m as GuardrailFn, O as OutputGuardrailData, p as StreamingCallbackRunner, c as AgentRunner, r as OrchestratorState, N as NamedGuardrail, I as InputGuardrailData, C as Checkpoint, B as BreakpointModifications, d as BreakpointRequest, s as OrchestratorConstraint, t as OrchestratorResolver, u as GuardrailsConfig, A as ApprovalRequest, v as OrchestratorDebugConfig, w as AgentRetryConfig, x as OrchestratorLifecycleHooks, y as SelfHealingConfig, h as CheckpointStore, z as BreakpointConfig, R as RunOptions, T as ToolCallGuardrailData, P as PatternCheckpointConfig, k as DagPattern, E as GoalPattern, F as GoalNode, J as AgentSelectionStrategy, K as RelaxationTier, L as GoalResult, Q as GoalCheckpointState, U as SequentialCheckpointState, V as SupervisorCheckpointState, W as ReflectCheckpointState, X as DebateCheckpointState, Y as DagCheckpointState, S as Scratchpad, Z as MultiAgentLifecycleHooks, _ as MultiAgentSelfHealingConfig, $ as MultiAgentBreakpointType, a0 as CrossAgentDerivationFn } from './types-DJ09LjZX.js';
|
|
2
|
+
import { System, Requirement, Plugin } from '@directive-run/core';
|
|
3
3
|
import { CircuitBreaker } from '@directive-run/core/plugins';
|
|
4
|
+
import { D as DebugTimeline } from './debug-timeline-L13P-U2I.js';
|
|
5
|
+
import { H as HealthMonitor } from './health-monitor-qL9RNMH3.js';
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
8
|
* Agent Memory System
|
|
@@ -600,89 +602,6 @@ interface MergedTaggedStreamResult {
|
|
|
600
602
|
*/
|
|
601
603
|
declare function mergeTaggedStreams(sources: TaggedSource[]): MergedTaggedStreamResult;
|
|
602
604
|
|
|
603
|
-
/**
|
|
604
|
-
* Debug Timeline — AI-specific event log with snapshot correlation.
|
|
605
|
-
*
|
|
606
|
-
* Records agent lifecycle events (start, complete, error, guardrail checks,
|
|
607
|
-
* approvals, handoffs, patterns) and correlates them with core time-travel
|
|
608
|
-
* snapshots for visual timeline UIs and fork-and-replay debugging.
|
|
609
|
-
*
|
|
610
|
-
* Zero-cost when debug=false — the timeline is simply `null`.
|
|
611
|
-
*
|
|
612
|
-
* @module
|
|
613
|
-
*/
|
|
614
|
-
|
|
615
|
-
/** Callback fired when a new event is recorded */
|
|
616
|
-
type DebugTimelineListener = (event: DebugEvent) => void;
|
|
617
|
-
/** Debug timeline instance */
|
|
618
|
-
interface DebugTimeline {
|
|
619
|
-
/** Record a new event (id is auto-assigned) */
|
|
620
|
-
record(event: Omit<DebugEvent, "id"> & Record<string, unknown>): DebugEvent;
|
|
621
|
-
/** Get all events in order */
|
|
622
|
-
getEvents(): DebugEvent[];
|
|
623
|
-
/** Get events for a specific agent */
|
|
624
|
-
getEventsForAgent(agentId: string): DebugEvent[];
|
|
625
|
-
/** Get events by type with type narrowing */
|
|
626
|
-
getEventsByType<T extends DebugEventType>(type: T): Extract<DebugEvent, {
|
|
627
|
-
type: T;
|
|
628
|
-
}>[];
|
|
629
|
-
/** Get events at a specific snapshot */
|
|
630
|
-
getEventsAtSnapshot(snapshotId: number): DebugEvent[];
|
|
631
|
-
/** Get events in a time range */
|
|
632
|
-
getEventsInRange(startMs: number, endMs: number): DebugEvent[];
|
|
633
|
-
/** Fork from a snapshot — truncates events after it and calls goTo */
|
|
634
|
-
forkFrom(snapshotId: number): void;
|
|
635
|
-
/** Export timeline as JSON */
|
|
636
|
-
export(): string;
|
|
637
|
-
/** Import timeline from JSON */
|
|
638
|
-
import(json: string): void;
|
|
639
|
-
/** Clear all events */
|
|
640
|
-
clear(): void;
|
|
641
|
-
/** Subscribe to new events. Returns unsubscribe function. */
|
|
642
|
-
subscribe(listener: DebugTimelineListener): () => void;
|
|
643
|
-
/** Current number of events */
|
|
644
|
-
readonly length: number;
|
|
645
|
-
}
|
|
646
|
-
/** Options for creating a debug timeline */
|
|
647
|
-
interface DebugTimelineOptions {
|
|
648
|
-
/** Maximum events before eviction. @default 2000 */
|
|
649
|
-
maxEvents?: number;
|
|
650
|
-
/** Callback to get current snapshot ID from the system */
|
|
651
|
-
getSnapshotId?: () => number | null;
|
|
652
|
-
/** Callback to navigate to a snapshot (for forkFrom) */
|
|
653
|
-
goToSnapshot?: (snapshotId: number) => void;
|
|
654
|
-
}
|
|
655
|
-
/**
|
|
656
|
-
* Create a debug timeline for recording and correlating AI events.
|
|
657
|
-
*
|
|
658
|
-
* @example
|
|
659
|
-
* ```typescript
|
|
660
|
-
* const timeline = createDebugTimeline({ maxEvents: 1000 });
|
|
661
|
-
*
|
|
662
|
-
* timeline.record({
|
|
663
|
-
* type: "agent_start",
|
|
664
|
-
* timestamp: Date.now(),
|
|
665
|
-
* agentId: "researcher",
|
|
666
|
-
* snapshotId: null,
|
|
667
|
-
* inputLength: 42,
|
|
668
|
-
* });
|
|
669
|
-
*
|
|
670
|
-
* const agentEvents = timeline.getEventsForAgent("researcher");
|
|
671
|
-
* ```
|
|
672
|
-
*/
|
|
673
|
-
declare function createDebugTimeline(options?: DebugTimelineOptions): DebugTimeline;
|
|
674
|
-
/**
|
|
675
|
-
* Create a Directive plugin that bridges core constraint/resolver events
|
|
676
|
-
* to the debug timeline.
|
|
677
|
-
*
|
|
678
|
-
* @example
|
|
679
|
-
* ```typescript
|
|
680
|
-
* const timeline = createDebugTimeline();
|
|
681
|
-
* const plugin = createDebugTimelinePlugin(timeline, () => system.history?.currentIndex ?? null);
|
|
682
|
-
* ```
|
|
683
|
-
*/
|
|
684
|
-
declare function createDebugTimelinePlugin(timeline: DebugTimeline, getSnapshotId: () => number | null): Plugin;
|
|
685
|
-
|
|
686
605
|
/**
|
|
687
606
|
* P6: Structured Outputs — Schema validation with auto-retry for LLM responses.
|
|
688
607
|
*
|
|
@@ -1063,58 +982,6 @@ interface AgentOrchestrator<F extends Record<string, unknown>> {
|
|
|
1063
982
|
*/
|
|
1064
983
|
declare function createAgentOrchestrator<F extends Record<string, unknown> = Record<string, never>>(options: OrchestratorOptions<F>): AgentOrchestrator<F>;
|
|
1065
984
|
|
|
1066
|
-
/**
|
|
1067
|
-
* Health Monitor — tracks per-agent health metrics for self-healing networks.
|
|
1068
|
-
*
|
|
1069
|
-
* Pure computation module with zero Directive dependency.
|
|
1070
|
-
* Maintains a rolling window of success/failure events and computes a health
|
|
1071
|
-
* score from 0-100 based on configurable weights.
|
|
1072
|
-
*
|
|
1073
|
-
* @module
|
|
1074
|
-
*/
|
|
1075
|
-
|
|
1076
|
-
/** Circuit state values */
|
|
1077
|
-
type HealthCircuitState = "CLOSED" | "OPEN" | "HALF_OPEN";
|
|
1078
|
-
/** Per-agent health metrics */
|
|
1079
|
-
interface AgentHealthMetrics {
|
|
1080
|
-
agentId: string;
|
|
1081
|
-
circuitState: HealthCircuitState;
|
|
1082
|
-
successRate: number;
|
|
1083
|
-
avgLatencyMs: number;
|
|
1084
|
-
recentFailures: number;
|
|
1085
|
-
recentSuccesses: number;
|
|
1086
|
-
healthScore: number;
|
|
1087
|
-
/** Last N error messages (most recent last) */
|
|
1088
|
-
lastErrors: string[];
|
|
1089
|
-
}
|
|
1090
|
-
/** Health monitor instance */
|
|
1091
|
-
interface HealthMonitor {
|
|
1092
|
-
recordSuccess(agentId: string, latencyMs: number): void;
|
|
1093
|
-
recordFailure(agentId: string, latencyMs: number, error?: Error): void;
|
|
1094
|
-
getMetrics(agentId: string): AgentHealthMetrics;
|
|
1095
|
-
getAllMetrics(): Record<string, AgentHealthMetrics>;
|
|
1096
|
-
/** Returns a 0-100 health score. Returns 50 (neutral) when no data is available for the agent. */
|
|
1097
|
-
getHealthScore(agentId: string): number;
|
|
1098
|
-
updateCircuitState(agentId: string, state: HealthCircuitState): void;
|
|
1099
|
-
/** Reset all metrics. Useful for testing. */
|
|
1100
|
-
reset(): void;
|
|
1101
|
-
}
|
|
1102
|
-
/**
|
|
1103
|
-
* Create a health monitor that tracks per-agent metrics.
|
|
1104
|
-
*
|
|
1105
|
-
* @example
|
|
1106
|
-
* ```typescript
|
|
1107
|
-
* const monitor = createHealthMonitor({ windowMs: 30000 });
|
|
1108
|
-
*
|
|
1109
|
-
* monitor.recordSuccess("agent-a", 120);
|
|
1110
|
-
* monitor.recordFailure("agent-a", 5000, new Error("timeout"));
|
|
1111
|
-
*
|
|
1112
|
-
* const score = monitor.getHealthScore("agent-a");
|
|
1113
|
-
* console.log(score); // 0-100
|
|
1114
|
-
* ```
|
|
1115
|
-
*/
|
|
1116
|
-
declare function createHealthMonitor(config?: HealthMonitorConfig): HealthMonitor;
|
|
1117
|
-
|
|
1118
985
|
/**
|
|
1119
986
|
* Agent Reflection / Self-Improvement
|
|
1120
987
|
*
|
|
@@ -1383,7 +1250,7 @@ interface RaceSuccessEntry<T = unknown> {
|
|
|
1383
1250
|
interface RaceResult<T = unknown> {
|
|
1384
1251
|
winnerId: string;
|
|
1385
1252
|
result: T;
|
|
1386
|
-
allResults?:
|
|
1253
|
+
allResults?: RaceSuccessEntry<T>[];
|
|
1387
1254
|
}
|
|
1388
1255
|
/**
|
|
1389
1256
|
* Debate pattern - agents compete, evaluator judges across rounds.
|
|
@@ -1781,4 +1648,4 @@ interface MultiAgentOrchestrator {
|
|
|
1781
1648
|
destroy(): void;
|
|
1782
1649
|
}
|
|
1783
1650
|
|
|
1784
|
-
export { type
|
|
1651
|
+
export { type StreamingGuardrailResult as $, type AgentOrchestrator as A, type BackpressureStrategy as B, type RaceSuccessEntry as C, type DebatePattern as D, type ErrorChunk as E, type ReflectIterationRecord as F, type GuardrailTriggeredChunk as G, type HandoffRequest as H, type ReflectPattern as I, type ReflectionConfig as J, type ReflectionContext as K, type ReflectionEvaluation as L, type MultiAgentOrchestrator as M, ReflectionExhaustedError as N, type OrchestratorOptions as O, type ParallelPattern as P, type RunAgentRequirement as Q, type ReflectionEvaluator as R, type RunCallOptions as S, type TaskRegistration as T, type SafeParseResult as U, type SafeParseable as V, type SequentialPattern as W, type StreamChunk as X, type StreamRunOptions as Y, type StreamRunner as Z, type StreamingGuardrail as _, type MultiAgentOrchestratorOptions as a, type StreamingRunResult as a0, type StructuredOutputConfig as a1, StructuredOutputError as a2, type SupervisorPattern as a3, type TaskContext as a4, type TokenChunk as a5, type ToolEndChunk as a6, type ToolStartChunk as a7, adaptOutputGuardrail as a8, collectTokens as a9, combineStreamingGuardrails as aa, createAgentMemory as ab, createAgentOrchestrator as ac, createHybridStrategy as ad, createKeyPointsSummarizer as ae, createLLMSummarizer as af, createLengthStreamingGuardrail as ag, createPatternStreamingGuardrail as ah, createSlidingWindowStrategy as ai, createStreamingRunner as aj, createTokenBasedStrategy as ak, createToxicityStreamingGuardrail as al, createTruncationSummarizer as am, extractJsonFromOutput as an, filterStream as ao, mapStream as ap, mergeTaggedStreams as aq, tapStream as ar, withReflection as as, withStructuredOutput as at, type MultiplexedStreamChunk as b, type AgentMemory as c, type AgentMemoryConfig as d, type AgentRegistration as e, type AgentRegistry as f, type DebateResult as g, type DoneChunk as h, type ExecutionPattern as i, type HandoffResult as j, type MemoryManageResult as k, type MemoryState as l, type MemoryStrategy as m, type MemoryStrategyConfig as n, type MemoryStrategyResult as o, type MergedTaggedStreamResult as p, type MessageChunk as q, type MessageSummarizer as r, type MultiAgentRunCallOptions as s, type MultiAgentState as t, type MultiplexedStreamResult as u, type OrchestratorStreamChunk as v, type OrchestratorStreamResult as w, type ProgressChunk as x, type RacePattern as y, type RaceResult as z };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
'use strict';var chunkMDXDPECP_cjs=require('./chunk-MDXDPECP.cjs');require('./chunk-WCQK3JOR.cjs');Object.defineProperty(exports,"PredicateFromIntentError",{enumerable:true,get:function(){return chunkMDXDPECP_cjs.a}});Object.defineProperty(exports,"predicateFromIntent",{enumerable:true,get:function(){return chunkMDXDPECP_cjs.b}});Object.defineProperty(exports,"predicateFromIntentRaw",{enumerable:true,get:function(){return chunkMDXDPECP_cjs.c}});Object.defineProperty(exports,"predicateFromIntentWithProvenance",{enumerable:true,get:function(){return chunkMDXDPECP_cjs.g}});Object.defineProperty(exports,"predicateToolSpec",{enumerable:true,get:function(){return chunkMDXDPECP_cjs.f}});Object.defineProperty(exports,"predicateToolSpecAnthropic",{enumerable:true,get:function(){return chunkMDXDPECP_cjs.e}});Object.defineProperty(exports,"predicateToolSpecOpenAI",{enumerable:true,get:function(){return chunkMDXDPECP_cjs.d}});//# sourceMappingURL=predicate.cjs.map
|
|
2
|
+
//# sourceMappingURL=predicate.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"predicate.cjs"}
|
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
import { FactPredicate, SchemaValidationError } from '@directive-run/core';
|
|
2
|
+
import { c as AgentRunner, b as AgentLike } from './types-DJ09LjZX.cjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* predicateFromIntent — let an LLM emit a typed FactPredicate as JSON,
|
|
6
|
+
* structurally + semantically validated before it ever reaches your
|
|
7
|
+
* constraint engine.
|
|
8
|
+
*
|
|
9
|
+
* Pipeline per call attempt:
|
|
10
|
+
*
|
|
11
|
+
* 1. Output-size check (reject before JSON.parse for DoS guard)
|
|
12
|
+
* 2. JSON.parse via extractJsonFromOutput (handles surrounding prose)
|
|
13
|
+
* 3. validatePredicate (structural: closed operator set, depth, JSON safety)
|
|
14
|
+
* 4. Operator-count check
|
|
15
|
+
* 5. validatePredicateAgainstSchema (semantic: operator-on-kind matrix)
|
|
16
|
+
*
|
|
17
|
+
* On any failure: a structured error message — including the offending
|
|
18
|
+
* clause's path, the allowed operators for that fact's kind, and the
|
|
19
|
+
* original schema kinds — is fed back to the LLM in the next attempt.
|
|
20
|
+
*
|
|
21
|
+
* Returns the validated FactPredicate. Throws PredicateFromIntentError
|
|
22
|
+
* on retry exhaustion. NEVER returns a partial / unvalidated predicate.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
interface PredicateFromIntentOptions<_F = Record<string, unknown>> {
|
|
26
|
+
/** Natural-language intent (untrusted user input — sanitize via `redact`). */
|
|
27
|
+
intent: string;
|
|
28
|
+
/**
|
|
29
|
+
* Module schema (must expose builders with `_typeName` or `_kind`).
|
|
30
|
+
* Pass either `{ facts: {...} }` or a bare `Record<string, builder>`.
|
|
31
|
+
*/
|
|
32
|
+
schema: unknown;
|
|
33
|
+
/** AgentRunner from `@directive-run/ai` adapters (createOpenAIRunner, etc.). */
|
|
34
|
+
runner: AgentRunner;
|
|
35
|
+
/**
|
|
36
|
+
* Optional agent override. Default is `{ name: "predicate-emitter" }`
|
|
37
|
+
* with our system prompt; pass `instructions` to append additional
|
|
38
|
+
* context.
|
|
39
|
+
*/
|
|
40
|
+
agent?: AgentLike;
|
|
41
|
+
/**
|
|
42
|
+
* Optional dotted-path namespace; useful for cross-module systems
|
|
43
|
+
* where the LLM should emit a predicate over `auth.token` (default:
|
|
44
|
+
* the schema's root facts).
|
|
45
|
+
*/
|
|
46
|
+
factPath?: string;
|
|
47
|
+
/** Max retries on validation failure. Default 3. */
|
|
48
|
+
maxRetries?: number;
|
|
49
|
+
/**
|
|
50
|
+
* Hard byte cap on the LLM's raw output, BEFORE JSON.parse. Defaults
|
|
51
|
+
* to 64 KiB. A larger predicate is rejected outright; protects
|
|
52
|
+
* against multi-MB-payload DoS where the predicate is technically
|
|
53
|
+
* structurally valid.
|
|
54
|
+
*/
|
|
55
|
+
maxPredicateBytes?: number;
|
|
56
|
+
/**
|
|
57
|
+
* Hard cap on the number of operator clauses in the predicate.
|
|
58
|
+
* Defaults to 256. Protects against `{ $any: [{ x: 1 }, … x100,000 ] }`
|
|
59
|
+
* style operator-count exhaustion.
|
|
60
|
+
*/
|
|
61
|
+
maxOperatorCount?: number;
|
|
62
|
+
/**
|
|
63
|
+
* Optional sanitizer applied to `intent` BEFORE it lands in the
|
|
64
|
+
* system prompt. Useful for stripping or redacting user-controlled
|
|
65
|
+
* content that looks like prompt-injection.
|
|
66
|
+
*/
|
|
67
|
+
redact?: (intent: string) => string;
|
|
68
|
+
/**
|
|
69
|
+
* Hard cap on the length of each `$in` / `$nin` array operand the LLM
|
|
70
|
+
* may emit. Defaults to 1000. Forwarded to
|
|
71
|
+
* `validatePredicateAgainstSchema`'s `maxArrayOperandLength`.
|
|
72
|
+
*/
|
|
73
|
+
maxArrayOperandLength?: number;
|
|
74
|
+
/**
|
|
75
|
+
* Optional `AbortSignal` for cooperative cancellation. (N6)
|
|
76
|
+
*
|
|
77
|
+
* The retry loop checks `signal.aborted` between attempts AND forwards
|
|
78
|
+
* the signal into the runner call itself (`runner(agent, input, { signal })`).
|
|
79
|
+
* Whether the in-flight LLM call honors the signal depends on the runner:
|
|
80
|
+
* fetch-based adapters (the bundled OpenAI / Anthropic / Ollama runners)
|
|
81
|
+
* thread it through to `fetch`, so the network call aborts mid-stream.
|
|
82
|
+
* A custom runner that ignores the signal still delivers cancellation
|
|
83
|
+
* at the next retry boundary.
|
|
84
|
+
*
|
|
85
|
+
* On abort: throws `Error("aborted")`.
|
|
86
|
+
*
|
|
87
|
+
* NOTE: `predicateFromIntent` does NOT limit in-flight calls — callers
|
|
88
|
+
* MUST wrap with a concurrency limiter (e.g. `p-limit`) to bound
|
|
89
|
+
* fan-out under load.
|
|
90
|
+
*/
|
|
91
|
+
signal?: AbortSignal;
|
|
92
|
+
/**
|
|
93
|
+
* When `true`, the {@link PredicateFromIntentProvenance} returned by
|
|
94
|
+
* `predicateFromIntentWithProvenance` omits the raw `intent` string and
|
|
95
|
+
* stores only the SHA-256 `intentHash`. Use this in PII-sensitive
|
|
96
|
+
* contexts where the original intent must not be persisted. (M6)
|
|
97
|
+
*
|
|
98
|
+
* **Default `false` for back-compat.** For PII-sensitive deployments,
|
|
99
|
+
* ALWAYS set `redactIntent: true` — the raw intent may contain
|
|
100
|
+
* user-supplied content (names, emails, medical or financial details,
|
|
101
|
+
* customer messages) that becomes a permanent record in
|
|
102
|
+
* `provenance.intent`. The default is opt-in only because flipping it
|
|
103
|
+
* now would silently strip diagnostic data from existing callers; v2
|
|
104
|
+
* may flip this default (tracked in IDEAS.md).
|
|
105
|
+
*
|
|
106
|
+
* Pair with a `redact:` sanitizer when the intent itself must be
|
|
107
|
+
* scrubbed BEFORE it lands in the LLM prompt — `redactIntent` controls
|
|
108
|
+
* only what enters the provenance record, not what the LLM sees.
|
|
109
|
+
*/
|
|
110
|
+
redactIntent?: boolean;
|
|
111
|
+
}
|
|
112
|
+
interface PredicateFromIntentDiagnostics<F = Record<string, unknown>> {
|
|
113
|
+
/** The validated predicate (`null` if all retries failed). */
|
|
114
|
+
predicate: FactPredicate<F> | null;
|
|
115
|
+
/** Number of LLM calls actually made. */
|
|
116
|
+
attempts: number;
|
|
117
|
+
/** Errors encountered across all attempts (most recent last). */
|
|
118
|
+
errors: ReadonlyArray<{
|
|
119
|
+
attempt: number;
|
|
120
|
+
reason: string;
|
|
121
|
+
details?: readonly SchemaValidationError[];
|
|
122
|
+
}>;
|
|
123
|
+
/** The raw LLM output from the final attempt — useful for debugging. */
|
|
124
|
+
lastRawOutput?: string;
|
|
125
|
+
}
|
|
126
|
+
/** Thrown by `predicateFromIntent` on retry exhaustion. `predicateFromIntentRaw` returns these as a diagnostics payload instead. */
|
|
127
|
+
declare class PredicateFromIntentError extends Error {
|
|
128
|
+
readonly attempts: number;
|
|
129
|
+
readonly errors: ReadonlyArray<{
|
|
130
|
+
attempt: number;
|
|
131
|
+
reason: string;
|
|
132
|
+
details?: readonly SchemaValidationError[];
|
|
133
|
+
}>;
|
|
134
|
+
readonly lastRawOutput: string | undefined;
|
|
135
|
+
readonly name = "PredicateFromIntentError";
|
|
136
|
+
constructor(message: string, attempts: number, errors: ReadonlyArray<{
|
|
137
|
+
attempt: number;
|
|
138
|
+
reason: string;
|
|
139
|
+
details?: readonly SchemaValidationError[];
|
|
140
|
+
}>, lastRawOutput: string | undefined);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Ask an LLM to emit a FactPredicate matching the user's intent, then
|
|
144
|
+
* validate it structurally + semantically before returning. On validation
|
|
145
|
+
* failure, retries with structured error feedback in the next prompt.
|
|
146
|
+
*
|
|
147
|
+
* Throws {@link PredicateFromIntentError} on retry exhaustion. NEVER
|
|
148
|
+
* returns a partial / unvalidated predicate.
|
|
149
|
+
*
|
|
150
|
+
* **Rate limiting:** this function does NOT limit in-flight calls. Wrap
|
|
151
|
+
* with a concurrency limiter (e.g. `p-limit` / `Bottleneck`) before
|
|
152
|
+
* exposing it to user-driven traffic. Pass an `AbortSignal` via `opts.signal`
|
|
153
|
+
* for cooperative cancellation between retry attempts.
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```ts
|
|
157
|
+
* import { createOpenAIRunner } from "@directive-run/ai/openai";
|
|
158
|
+
* import { predicateFromIntent } from "@directive-run/ai";
|
|
159
|
+
*
|
|
160
|
+
* const runner = createOpenAIRunner({ apiKey, model: "gpt-4o-mini" });
|
|
161
|
+
*
|
|
162
|
+
* const predicate = await predicateFromIntent({
|
|
163
|
+
* intent: "checkout is unblocked when the cart total is at least 50",
|
|
164
|
+
* schema: myModule.schema,
|
|
165
|
+
* runner,
|
|
166
|
+
* });
|
|
167
|
+
* // → { cartTotal: { $gte: 50 } }
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
declare function predicateFromIntent<F = Record<string, unknown>>(opts: PredicateFromIntentOptions<F>): Promise<FactPredicate<F>>;
|
|
171
|
+
/**
|
|
172
|
+
* Lower-level variant — returns the validated predicate (or null) plus
|
|
173
|
+
* full diagnostics. Use when you want to surface validation telemetry,
|
|
174
|
+
* preview the LLM's last raw output, or display per-attempt errors in
|
|
175
|
+
* a UI.
|
|
176
|
+
*/
|
|
177
|
+
declare function predicateFromIntentRaw<F = Record<string, unknown>>(opts: PredicateFromIntentOptions<F>): Promise<PredicateFromIntentDiagnostics<F>>;
|
|
178
|
+
interface PredicateToolSpecOptions {
|
|
179
|
+
/** Tool name. Default `"emit_predicate"`. */
|
|
180
|
+
name?: string;
|
|
181
|
+
/** Tool description. Default: a one-liner describing predicate emission. */
|
|
182
|
+
description?: string;
|
|
183
|
+
/** Optional dotted-path namespace to restrict the tool's scope. */
|
|
184
|
+
factPath?: string;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Anthropic Messages API tool shape. Drop into the `tools: [...]` array.
|
|
188
|
+
* Anthropic's API expects `input_schema` at the top level of the tool.
|
|
189
|
+
*/
|
|
190
|
+
interface PredicateToolSpecAnthropic {
|
|
191
|
+
name: string;
|
|
192
|
+
description: string;
|
|
193
|
+
input_schema: {
|
|
194
|
+
type: "object";
|
|
195
|
+
properties: {
|
|
196
|
+
predicate: {
|
|
197
|
+
type: "object";
|
|
198
|
+
};
|
|
199
|
+
};
|
|
200
|
+
required: ["predicate"];
|
|
201
|
+
};
|
|
202
|
+
/** Human-readable schema description — embed in your tool's "description" if your provider concatenates them. */
|
|
203
|
+
schemaSummary: string;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* OpenAI Chat Completions / Responses API tool shape. Drop into the
|
|
207
|
+
* `tools: [...]` array. OpenAI nests the function payload under
|
|
208
|
+
* `function: { name, description, parameters }`.
|
|
209
|
+
*/
|
|
210
|
+
interface PredicateToolSpecOpenAI {
|
|
211
|
+
type: "function";
|
|
212
|
+
function: {
|
|
213
|
+
name: string;
|
|
214
|
+
description: string;
|
|
215
|
+
parameters: {
|
|
216
|
+
type: "object";
|
|
217
|
+
properties: {
|
|
218
|
+
predicate: {
|
|
219
|
+
type: "object";
|
|
220
|
+
};
|
|
221
|
+
};
|
|
222
|
+
required: ["predicate"];
|
|
223
|
+
};
|
|
224
|
+
};
|
|
225
|
+
/** Human-readable schema description — embed in your tool's "description" if your provider concatenates them. */
|
|
226
|
+
schemaSummary: string;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* @deprecated Use {@link PredicateToolSpecAnthropic} (or
|
|
230
|
+
* {@link PredicateToolSpecOpenAI}) directly. Kept as an alias for the
|
|
231
|
+
* pre-split shape that v1.12.x callers depend on; identical to
|
|
232
|
+
* `PredicateToolSpecAnthropic`.
|
|
233
|
+
*/
|
|
234
|
+
type PredicateToolSpec = PredicateToolSpecAnthropic;
|
|
235
|
+
/**
|
|
236
|
+
* OpenAI Chat Completions / Responses API tool spec for predicate
|
|
237
|
+
* emission. Drop the result into your `tools: [...]` array; the model
|
|
238
|
+
* will be told to emit a predicate matching this schema.
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```ts
|
|
242
|
+
* const tool = predicateToolSpecOpenAI(myModule.schema, { name: "set_checkout_rule" });
|
|
243
|
+
*
|
|
244
|
+
* await openai.chat.completions.create({
|
|
245
|
+
* model: "gpt-4o-mini",
|
|
246
|
+
* tools: [tool],
|
|
247
|
+
* messages: [...],
|
|
248
|
+
* });
|
|
249
|
+
* ```
|
|
250
|
+
*/
|
|
251
|
+
declare function predicateToolSpecOpenAI(schema: unknown, opts?: PredicateToolSpecOptions): PredicateToolSpecOpenAI;
|
|
252
|
+
/**
|
|
253
|
+
* Anthropic Messages API tool spec for predicate emission. Drop the
|
|
254
|
+
* result into your `tools: [...]` array; the model will be told to emit
|
|
255
|
+
* a predicate matching this schema.
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* ```ts
|
|
259
|
+
* const tool = predicateToolSpecAnthropic(myModule.schema, { name: "set_checkout_rule" });
|
|
260
|
+
*
|
|
261
|
+
* await anthropic.messages.create({
|
|
262
|
+
* model: "claude-3-5-sonnet-latest",
|
|
263
|
+
* tools: [tool],
|
|
264
|
+
* messages: [...],
|
|
265
|
+
* });
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
268
|
+
declare function predicateToolSpecAnthropic(schema: unknown, opts?: PredicateToolSpecOptions): PredicateToolSpecAnthropic;
|
|
269
|
+
/**
|
|
270
|
+
* @deprecated Use {@link predicateToolSpecAnthropic} (or
|
|
271
|
+
* {@link predicateToolSpecOpenAI}) directly. Back-compat alias for
|
|
272
|
+
* v1.12.x callers — emits the Anthropic shape, unchanged.
|
|
273
|
+
*/
|
|
274
|
+
declare function predicateToolSpec(schema: unknown, opts?: PredicateToolSpecOptions): PredicateToolSpec;
|
|
275
|
+
interface PredicateFromIntentProvenance {
|
|
276
|
+
/**
|
|
277
|
+
* Resolved model name when the caller passes `agent: { model }`,
|
|
278
|
+
* otherwise `"unknown"`.
|
|
279
|
+
*
|
|
280
|
+
* NOTE: most callers omit `agent` (the default `predicate-emitter`
|
|
281
|
+
* agent has no model field set), so `model` is `"unknown"` by default.
|
|
282
|
+
* v1 does NOT read provider-detected model strings from `RunResult` —
|
|
283
|
+
* that requires every adapter to surface `runResult.model`, which the
|
|
284
|
+
* current `AgentRunner` contract does not. Tracked for v2; callers who
|
|
285
|
+
* need provider attribution today should pass `agent: { name, model: "..." }`.
|
|
286
|
+
*/
|
|
287
|
+
readonly model: string;
|
|
288
|
+
/**
|
|
289
|
+
* Sanitized intent string actually sent to the LLM (post-redact). When
|
|
290
|
+
* `redactIntent: true` was passed, this field is omitted entirely — only
|
|
291
|
+
* `intentHash` is populated.
|
|
292
|
+
*/
|
|
293
|
+
readonly intent?: string;
|
|
294
|
+
/**
|
|
295
|
+
* SHA-256 hex hash of the sanitized intent string (or djb2 fallback
|
|
296
|
+
* when `crypto.subtle` is unavailable). Always present — even when
|
|
297
|
+
* `intent` is omitted via `redactIntent`. (M6)
|
|
298
|
+
*/
|
|
299
|
+
readonly intentHash: string;
|
|
300
|
+
/** Number of LLM calls that ran before the final predicate was accepted. */
|
|
301
|
+
readonly attemptCount: number;
|
|
302
|
+
/** ISO timestamp when the predicate was returned. */
|
|
303
|
+
readonly emittedAt: string;
|
|
304
|
+
/**
|
|
305
|
+
* Hash of the VALIDATED predicate (canonicalized via stableStringify
|
|
306
|
+
* before hashing). Two semantically-identical predicates emitted with
|
|
307
|
+
* different whitespace or key order produce the SAME `predicateHash`.
|
|
308
|
+
* Sufficient as a tamper-evident pointer alongside the persisted
|
|
309
|
+
* predicate. (N3)
|
|
310
|
+
*
|
|
311
|
+
* Renamed from `rawOutputHash` in v1.13.x — the old name hashed the
|
|
312
|
+
* raw LLM output string, which made two whitespace-different responses
|
|
313
|
+
* for the same logical predicate hash differently. Callers persisting
|
|
314
|
+
* the old `rawOutputHash` value should re-derive it from the stored
|
|
315
|
+
* predicate using `hashObject(predicate)` from `@directive-run/core/internals`.
|
|
316
|
+
*/
|
|
317
|
+
readonly predicateHash: string;
|
|
318
|
+
}
|
|
319
|
+
interface PredicateFromIntentWithProvenanceResult<F = Record<string, unknown>> {
|
|
320
|
+
readonly predicate: FactPredicate<F>;
|
|
321
|
+
readonly provenance: PredicateFromIntentProvenance;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Like {@link predicateFromIntent} but additionally returns a structured
|
|
325
|
+
* provenance record — the model name, sanitized intent (or its hash),
|
|
326
|
+
* attempt count, timestamp, and a canonical hash of the validated
|
|
327
|
+
* predicate.
|
|
328
|
+
*
|
|
329
|
+
* **Production deployments MUST persist the provenance record alongside
|
|
330
|
+
* the predicate.** Without it, auditing "where did this rule come from?"
|
|
331
|
+
* becomes guesswork.
|
|
332
|
+
*
|
|
333
|
+
* Throws {@link PredicateFromIntentError} on retry exhaustion — same
|
|
334
|
+
* semantics as the un-provenanced variant.
|
|
335
|
+
*
|
|
336
|
+
* **PII guidance (M6):** pass `redactIntent: true` to omit the raw
|
|
337
|
+
* intent from the provenance record and persist only the `intentHash`.
|
|
338
|
+
* Useful when the intent itself is sensitive (medical, financial,
|
|
339
|
+
* customer messages, etc.).
|
|
340
|
+
*
|
|
341
|
+
* **Hash semantics (N3):**
|
|
342
|
+
* - `predicateHash` hashes the VALIDATED predicate object via stable
|
|
343
|
+
* stringification — two whitespace-different LLM outputs that parse to
|
|
344
|
+
* the same predicate produce the same hash.
|
|
345
|
+
* - `intentHash` hashes the sanitized intent STRING (SHA-256 when
|
|
346
|
+
* available, djb2 fallback).
|
|
347
|
+
*
|
|
348
|
+
* @example
|
|
349
|
+
* ```ts
|
|
350
|
+
* const { predicate, provenance } = await predicateFromIntentWithProvenance({
|
|
351
|
+
* intent: "block checkout when cart > 10k",
|
|
352
|
+
* schema: checkoutModule.schema,
|
|
353
|
+
* runner,
|
|
354
|
+
* agent: { name: "predicate-emitter", model: "gpt-4o-mini" },
|
|
355
|
+
* redactIntent: false, // default: store both intent + intentHash
|
|
356
|
+
* });
|
|
357
|
+
*
|
|
358
|
+
* await db.predicates.insert({
|
|
359
|
+
* predicate,
|
|
360
|
+
* model: provenance.model,
|
|
361
|
+
* intent: provenance.intent, // omitted when redactIntent: true
|
|
362
|
+
* intentHash: provenance.intentHash,
|
|
363
|
+
* emittedAt: provenance.emittedAt,
|
|
364
|
+
* predicateHash: provenance.predicateHash,
|
|
365
|
+
* attempts: provenance.attemptCount,
|
|
366
|
+
* });
|
|
367
|
+
* ```
|
|
368
|
+
*/
|
|
369
|
+
declare function predicateFromIntentWithProvenance<F = Record<string, unknown>>(opts: PredicateFromIntentOptions<F>): Promise<PredicateFromIntentWithProvenanceResult<F>>;
|
|
370
|
+
|
|
371
|
+
export { type PredicateFromIntentDiagnostics, PredicateFromIntentError, type PredicateFromIntentOptions, type PredicateFromIntentProvenance, type PredicateFromIntentWithProvenanceResult, type PredicateToolSpec, type PredicateToolSpecAnthropic, type PredicateToolSpecOpenAI, type PredicateToolSpecOptions, predicateFromIntent, predicateFromIntentRaw, predicateFromIntentWithProvenance, predicateToolSpec, predicateToolSpecAnthropic, predicateToolSpecOpenAI };
|