@directive-run/ai 0.2.0 → 0.4.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.
Files changed (44) hide show
  1. package/README.md +26 -31
  2. package/dist/anthropic.cjs +1 -1
  3. package/dist/anthropic.cjs.map +1 -1
  4. package/dist/anthropic.d.cts +5 -9
  5. package/dist/anthropic.d.ts +5 -9
  6. package/dist/anthropic.js +1 -1
  7. package/dist/anthropic.js.map +1 -1
  8. package/dist/gemini.cjs +3 -0
  9. package/dist/gemini.cjs.map +1 -0
  10. package/dist/gemini.d.cts +93 -0
  11. package/dist/gemini.d.ts +93 -0
  12. package/dist/gemini.js +3 -0
  13. package/dist/gemini.js.map +1 -0
  14. package/dist/index.cjs +117 -45
  15. package/dist/index.cjs.map +1 -1
  16. package/dist/index.d.cts +1388 -2107
  17. package/dist/index.d.ts +1388 -2107
  18. package/dist/index.js +117 -45
  19. package/dist/index.js.map +1 -1
  20. package/dist/multi-agent-orchestrator-D-WuP4jP.d.ts +2365 -0
  21. package/dist/multi-agent-orchestrator-YFs28JsF.d.cts +2365 -0
  22. package/dist/ollama.cjs.map +1 -1
  23. package/dist/ollama.d.cts +3 -2
  24. package/dist/ollama.d.ts +3 -2
  25. package/dist/ollama.js.map +1 -1
  26. package/dist/openai.cjs +2 -2
  27. package/dist/openai.cjs.map +1 -1
  28. package/dist/openai.d.cts +4 -8
  29. package/dist/openai.d.ts +4 -8
  30. package/dist/openai.js +2 -2
  31. package/dist/openai.js.map +1 -1
  32. package/dist/semantic-cache-F0psCRuz.d.cts +271 -0
  33. package/dist/semantic-cache-F0psCRuz.d.ts +271 -0
  34. package/dist/testing.cjs +42 -7
  35. package/dist/testing.cjs.map +1 -1
  36. package/dist/testing.d.cts +390 -5
  37. package/dist/testing.d.ts +390 -5
  38. package/dist/testing.js +42 -7
  39. package/dist/testing.js.map +1 -1
  40. package/dist/types-D5veI9su.d.cts +1456 -0
  41. package/dist/types-D5veI9su.d.ts +1456 -0
  42. package/package.json +7 -2
  43. package/dist/types-Bbar7yKz.d.cts +0 -304
  44. package/dist/types-Bbar7yKz.d.ts +0 -304
package/dist/testing.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- import { AgentOrchestrator, OrchestratorOptions } from './index.js';
2
- import { A as ApprovalRequest, I as InputGuardrailData, O as OutputGuardrailData, M as Message, T as ToolCallGuardrailData, a as ToolCall, G as GuardrailResult, b as AgentLike, c as AgentRunner, R as RunOptions, d as GuardrailFn, e as GuardrailContext } from './types-Bbar7yKz.js';
1
+ import { w as MultiAgentOrchestrator, x as MultiAgentOrchestratorOptions, c as AgentOrchestrator, O as OrchestratorOptions, H as HealthMonitor, C as MultiplexedStreamChunk, D as DebugTimeline, ah as TaskRegistration, X as ReflectionEvaluator } from './multi-agent-orchestrator-D-WuP4jP.js';
2
+ import { o as ApprovalRequest, u as BreakpointModifications, I as InputGuardrailData, O as OutputGuardrailData, M as Message, T as ToolCallGuardrailData, b2 as ToolCall, ad as GuardrailResult, A as AgentLike, b as AgentRunner, R as RunOptions, v as BreakpointRequest, C as Checkpoint, V as DagExecutionContext, X as DagNodeStatus, aN as RerouteEvent, aS as Scratchpad, a1 as DebugEventType, L as CheckpointStore, aj as InMemoryCheckpointStore, W as DagNode, Z as DagPattern, D as DebugEvent, G as GuardrailFn, aa as GuardrailContext } from './types-D5veI9su.js';
3
3
  import '@directive-run/core';
4
4
  import '@directive-run/core/plugins';
5
5
 
6
6
  /**
7
- * OpenAI Agents Testing Utilities
7
+ * Directive AI Testing Utilities
8
8
  *
9
9
  * Provides testing helpers for:
10
10
  * - Mock agent runners with configurable responses
@@ -14,7 +14,7 @@ import '@directive-run/core/plugins';
14
14
  *
15
15
  * @example
16
16
  * ```typescript
17
- * import { createMockAgentRunner, testGuardrail, createApprovalSimulator } from '@directive-run/ai';
17
+ * import { createMockAgentRunner, testGuardrail, createApprovalSimulator } from '@directive-run/ai/testing';
18
18
  *
19
19
  * describe('MyOrchestrator', () => {
20
20
  * it('should block PII in input', async () => {
@@ -341,5 +341,390 @@ declare function createTimeController(startTime?: number): {
341
341
  set(time: number): void;
342
342
  reset(): void;
343
343
  };
344
+ /** Options for test multi-agent orchestrator */
345
+ interface TestMultiAgentOrchestratorOptions extends Omit<MultiAgentOrchestratorOptions, "runner"> {
346
+ /** Mock responses keyed by agent ID — internally mapped to agent names for the mock runner */
347
+ mockResponses?: Record<string, MockAgentConfig>;
348
+ /** Default mock response for unmatched agents */
349
+ defaultMockResponse?: MockAgentConfig;
350
+ /** Mock tasks keyed by task ID — auto-generates TaskRegistration wrappers */
351
+ mockTasks?: Record<string, {
352
+ output: unknown;
353
+ delay?: number;
354
+ shouldError?: boolean;
355
+ }>;
356
+ }
357
+ /** Test multi-agent orchestrator with additional testing utilities */
358
+ interface TestMultiAgentOrchestrator extends MultiAgentOrchestrator {
359
+ /** The mock runner */
360
+ mockRunner: MockAgentRunner;
361
+ /** Approval simulator */
362
+ approvalSimulator: ApprovalSimulator;
363
+ /** Get recorded agent calls */
364
+ getCalls(): RecordedCall[];
365
+ /** Get approval requests */
366
+ getApprovalRequests(): ApprovalRequest[];
367
+ /** Reset all state */
368
+ resetAll(): void;
369
+ }
370
+ /**
371
+ * Create a test multi-agent orchestrator with mocking and simulation built in.
372
+ *
373
+ * @example
374
+ * ```typescript
375
+ * const test = createTestMultiAgentOrchestrator({
376
+ * agents: {
377
+ * researcher: { agent: { name: 'researcher' } },
378
+ * writer: { agent: { name: 'writer' } },
379
+ * },
380
+ * mockResponses: {
381
+ * researcher: { output: 'Research results', totalTokens: 100 },
382
+ * writer: { output: 'Written article', totalTokens: 200 },
383
+ * },
384
+ * });
385
+ *
386
+ * const result = await test.runAgent('researcher', 'What is AI?');
387
+ * expect(result.output).toBe('Research results');
388
+ * expect(test.getCalls()).toHaveLength(1);
389
+ * ```
390
+ */
391
+ declare function createTestMultiAgentOrchestrator(options: TestMultiAgentOrchestratorOptions): TestMultiAgentOrchestrator;
392
+ /**
393
+ * Assert that a multi-agent orchestrator has specific state.
394
+ *
395
+ * @example
396
+ * ```typescript
397
+ * assertMultiAgentState(orchestrator, {
398
+ * agentStatus: { researcher: 'completed', writer: 'idle' },
399
+ * globalTokens: { min: 0, max: 1000 },
400
+ * pendingHandoffs: 0,
401
+ * });
402
+ * ```
403
+ */
404
+ declare function assertMultiAgentState(orchestrator: MultiAgentOrchestrator, expected: {
405
+ agentStatus?: Record<string, "idle" | "running" | "completed" | "error">;
406
+ taskStatus?: Record<string, string>;
407
+ totalTokens?: {
408
+ agentId?: string;
409
+ min?: number;
410
+ max?: number;
411
+ };
412
+ globalTokens?: {
413
+ min?: number;
414
+ max?: number;
415
+ };
416
+ pendingHandoffs?: number;
417
+ }): void;
418
+ /**
419
+ * Create a mock TaskRegistration for testing.
420
+ *
421
+ * @example
422
+ * ```typescript
423
+ * const task = createMockTask({ result: "processed" }, { delay: 100 });
424
+ * const orchestrator = createTestMultiAgentOrchestrator({
425
+ * agents: { writer: { agent: { name: "writer" } } },
426
+ * tasks: { process: task },
427
+ * });
428
+ * ```
429
+ */
430
+ declare function createMockTask(output: unknown, options?: {
431
+ delay?: number;
432
+ shouldError?: boolean;
433
+ label?: string;
434
+ description?: string;
435
+ }): TaskRegistration;
436
+
437
+ /**
438
+ * Create a test DAG pattern from a simplified node spec.
439
+ *
440
+ * @example
441
+ * ```typescript
442
+ * const pattern = createTestDag({
443
+ * A: { handler: "researcher" },
444
+ * B: { handler: "writer", deps: ["A"] },
445
+ * C: { handler: "reviewer", deps: ["B"] },
446
+ * });
447
+ * ```
448
+ */
449
+ declare function createTestDag<T = unknown>(nodes: Record<string, Pick<DagNode, "handler" | "deps" | "when" | "transform" | "timeout" | "priority">>, merge?: (context: DagExecutionContext) => T | Promise<T>, options?: {
450
+ timeout?: number;
451
+ maxConcurrent?: number;
452
+ onNodeError?: "fail" | "skip-downstream" | "continue";
453
+ }): DagPattern<T>;
454
+ /**
455
+ * Assert that a DAG execution produced the expected node statuses.
456
+ *
457
+ * @example
458
+ * ```typescript
459
+ * assertDagExecution(context, {
460
+ * nodeStatuses: { A: "completed", B: "completed", C: "skipped" },
461
+ * completedNodes: ["A", "B"],
462
+ * skippedNodes: ["C"],
463
+ * });
464
+ * ```
465
+ */
466
+ declare function assertDagExecution(context: DagExecutionContext, expected: {
467
+ nodeStatuses?: Record<string, DagNodeStatus>;
468
+ completedNodes?: string[];
469
+ skippedNodes?: string[];
470
+ errorNodes?: string[];
471
+ outputContains?: Record<string, unknown>;
472
+ }): void;
473
+
474
+ /**
475
+ * Create a test debug timeline pre-populated with events.
476
+ *
477
+ * @example
478
+ * ```typescript
479
+ * const timeline = createTestTimeline([
480
+ * { type: "agent_start", agentId: "researcher", inputLength: 42 },
481
+ * { type: "agent_complete", agentId: "researcher", outputLength: 100, durationMs: 500, totalTokens: 200 },
482
+ * ]);
483
+ *
484
+ * expect(timeline.getEventsForAgent("researcher")).toHaveLength(2);
485
+ * ```
486
+ */
487
+ declare function createTestTimeline(events?: Array<Partial<DebugEvent> & {
488
+ type: DebugEventType;
489
+ }>, options?: {
490
+ maxEvents?: number;
491
+ }): DebugTimeline;
492
+ /**
493
+ * Assert that a debug timeline contains expected events.
494
+ *
495
+ * @example
496
+ * ```typescript
497
+ * assertTimelineEvents(timeline, {
498
+ * totalEvents: 5,
499
+ * eventTypes: ["agent_start", "guardrail_check", "agent_complete"],
500
+ * agentEvents: { researcher: 3, writer: 2 },
501
+ * hasType: "guardrail_check",
502
+ * });
503
+ * ```
504
+ */
505
+ declare function assertTimelineEvents(timeline: DebugTimeline, expected: {
506
+ totalEvents?: number;
507
+ minEvents?: number;
508
+ maxEvents?: number;
509
+ eventTypes?: DebugEventType[];
510
+ agentEvents?: Record<string, number>;
511
+ hasType?: DebugEventType;
512
+ doesNotHaveType?: DebugEventType;
513
+ }): void;
514
+
515
+ /**
516
+ * Create a runner that always fails, useful for testing self-healing.
517
+ *
518
+ * @example
519
+ * ```typescript
520
+ * const failing = createFailingRunner(new Error("Provider down"));
521
+ * const orchestrator = createAgentOrchestrator({
522
+ * runner: failing,
523
+ * selfHealing: { fallbackRunners: [backupRunner] },
524
+ * });
525
+ * ```
526
+ */
527
+ declare function createFailingRunner(error?: Error, options?: {
528
+ delay?: number;
529
+ failAfter?: number;
530
+ }): AgentRunner;
531
+ /**
532
+ * Assert that an agent was rerouted during execution.
533
+ *
534
+ * @example
535
+ * ```typescript
536
+ * const events: RerouteEvent[] = [];
537
+ * const orchestrator = createMultiAgentOrchestrator({
538
+ * selfHealing: {
539
+ * onReroute: (event) => events.push(event),
540
+ * },
541
+ * });
542
+ *
543
+ * // ... trigger reroute ...
544
+ * assertRerouted(events, {
545
+ * fromAgent: "primary",
546
+ * toAgent: "backup",
547
+ * reason: /circuit breaker/i,
548
+ * });
549
+ * ```
550
+ */
551
+ declare function assertRerouted(events: RerouteEvent[], expected: {
552
+ fromAgent?: string;
553
+ toAgent?: string;
554
+ reason?: string | RegExp;
555
+ minReroutes?: number;
556
+ }): void;
557
+ /**
558
+ * Assert the health state of an agent in the health monitor.
559
+ *
560
+ * @example
561
+ * ```typescript
562
+ * assertAgentHealth(monitor, "researcher", {
563
+ * minScore: 70,
564
+ * circuitState: "CLOSED",
565
+ * });
566
+ * ```
567
+ */
568
+ declare function assertAgentHealth(monitor: HealthMonitor, agentId: string, expected: {
569
+ minScore?: number;
570
+ maxScore?: number;
571
+ circuitState?: "CLOSED" | "OPEN" | "HALF_OPEN";
572
+ minSuccessRate?: number;
573
+ }): void;
574
+ /**
575
+ * Create a test checkpoint store (wraps InMemoryCheckpointStore with assertion helpers).
576
+ *
577
+ * @example
578
+ * ```typescript
579
+ * const store = createTestCheckpointStore();
580
+ * const orchestrator = createAgentOrchestrator({ runner, checkpointStore: store });
581
+ * const cp = await orchestrator.checkpoint();
582
+ * expect(store.saved).toHaveLength(1);
583
+ * ```
584
+ */
585
+ declare function createTestCheckpointStore(maxCheckpoints?: number): CheckpointStore & {
586
+ saved: Checkpoint[];
587
+ inner: InMemoryCheckpointStore;
588
+ /** Get the most recently saved checkpoint */
589
+ getLatest: () => Checkpoint | undefined;
590
+ };
591
+ /**
592
+ * Assert that a checkpoint has expected properties.
593
+ */
594
+ declare function assertCheckpoint(checkpoint: Checkpoint, expected: {
595
+ orchestratorType?: "single" | "multi";
596
+ hasTimeline?: boolean;
597
+ hasMemory?: boolean;
598
+ hasSystemExport?: boolean;
599
+ label?: string;
600
+ }): void;
601
+ /** Options for the breakpoint simulator */
602
+ interface BreakpointSimulatorOptions {
603
+ /** Auto-resume after this delay (ms). Default: 0 (immediate) */
604
+ autoResumeDelay?: number;
605
+ /** Modifications to apply on resume */
606
+ modifications?: BreakpointModifications;
607
+ /** If true, cancel instead of resume */
608
+ cancel?: boolean;
609
+ /** Cancel reason */
610
+ cancelReason?: string;
611
+ }
612
+ /**
613
+ * Create a breakpoint simulator that auto-resolves breakpoints.
614
+ *
615
+ * @example
616
+ * ```typescript
617
+ * const simulator = createBreakpointSimulator({ autoResumeDelay: 10 });
618
+ * const orchestrator = createAgentOrchestrator({
619
+ * runner,
620
+ * breakpoints: [{ type: "pre_agent_run" }],
621
+ * onBreakpoint: simulator.handler,
622
+ * });
623
+ * // Run agent — breakpoint fires and auto-resumes
624
+ * await orchestrator.run("test input");
625
+ * expect(simulator.hits).toHaveLength(1);
626
+ * ```
627
+ */
628
+ /** Minimal interface for breakpoint-capable orchestrators */
629
+ interface BreakpointCapable {
630
+ resumeBreakpoint(id: string, modifications?: BreakpointModifications): void;
631
+ cancelBreakpoint(id: string, reason?: string): void;
632
+ }
633
+ declare function createBreakpointSimulator(options?: BreakpointSimulatorOptions): {
634
+ handler: (request: BreakpointRequest) => void;
635
+ hits: BreakpointRequest[];
636
+ attachTo: (orchestrator: BreakpointCapable) => void;
637
+ };
638
+ /**
639
+ * Assert that a breakpoint was hit with expected properties.
640
+ */
641
+ declare function assertBreakpointHit(hits: BreakpointRequest[], expected: {
642
+ type?: string;
643
+ agentId?: string;
644
+ count?: number;
645
+ }): void;
646
+ /**
647
+ * Create a mock SafeParseable schema for testing.
648
+ *
649
+ * @example
650
+ * ```typescript
651
+ * const schema = createMockSchema((data) => typeof data === "object" && data !== null);
652
+ * const orchestrator = createAgentOrchestrator({
653
+ * runner,
654
+ * outputSchema: schema,
655
+ * });
656
+ * ```
657
+ */
658
+ declare function createMockSchema<T>(validate: (data: unknown) => boolean, description?: string): {
659
+ safeParse: (data: unknown) => {
660
+ success: boolean;
661
+ data?: T;
662
+ error?: {
663
+ message: string;
664
+ };
665
+ };
666
+ description?: string;
667
+ };
668
+ /**
669
+ * Collect all chunks from a multiplexed stream into an array.
670
+ *
671
+ * @example
672
+ * ```typescript
673
+ * const { stream } = orchestrator.runParallelStream(["a", "b"], "input", merge);
674
+ * const chunks = await collectMultiplexedStream(stream);
675
+ * expect(chunks.length).toBeGreaterThan(0);
676
+ * ```
677
+ */
678
+ declare function collectMultiplexedStream(stream: AsyncIterable<MultiplexedStreamChunk>): Promise<MultiplexedStreamChunk[]>;
679
+ /**
680
+ * Assert properties of collected multiplexed stream chunks.
681
+ */
682
+ declare function assertMultiplexedStream(chunks: MultiplexedStreamChunk[], expected: {
683
+ agentIds?: string[];
684
+ minChunks?: number;
685
+ hasDone?: boolean;
686
+ hasErrors?: boolean;
687
+ }): void;
688
+ /**
689
+ * Create a test reflection evaluator that passes after N iterations.
690
+ *
691
+ * @example
692
+ * ```typescript
693
+ * const evaluator = createTestReflectionEvaluator({ passAfter: 2 });
694
+ * const reflective = withReflection(runner, { evaluate: evaluator });
695
+ * ```
696
+ */
697
+ declare function createTestReflectionEvaluator(options?: {
698
+ /** Pass after this many evaluations (1 = pass on first try). Default: 1 */
699
+ passAfter?: number;
700
+ /** Feedback to provide on failure */
701
+ feedback?: string;
702
+ /** Score to assign (0-1) */
703
+ score?: number;
704
+ }): ReflectionEvaluator;
705
+ /**
706
+ * Assert that a scratchpad contains expected values.
707
+ *
708
+ * @example
709
+ * ```typescript
710
+ * assertScratchpadState(orchestrator.scratchpad!, {
711
+ * "plan.status": "complete",
712
+ * "research.results": expect.any(Array),
713
+ * });
714
+ * ```
715
+ */
716
+ declare function assertScratchpadState(scratchpad: Scratchpad, expected: Record<string, unknown>): void;
717
+ /**
718
+ * Assert that derived values match expected values.
719
+ *
720
+ * @example
721
+ * ```typescript
722
+ * assertDerivedValues(orchestrator, {
723
+ * totalRuns: 3,
724
+ * allComplete: true,
725
+ * });
726
+ * ```
727
+ */
728
+ declare function assertDerivedValues(orchestrator: MultiAgentOrchestrator, expected: Record<string, unknown>): void;
344
729
 
345
- export { type ApprovalSimulator, type ApprovalSimulatorOptions, type ConstraintSnapshot, type GuardrailTestInput, type GuardrailTestResult, type MockAgentConfig, type MockAgentRunner, type MockAgentRunnerOptions, type RecordedCall, type TestOrchestrator, type TestOrchestratorOptions, assertOrchestratorState, createApprovalSimulator, createConstraintRecorder, createMockAgentRunner, createTestOrchestrator, createTimeController, testGuardrail, testGuardrailBatch };
730
+ export { type ApprovalSimulator, type ApprovalSimulatorOptions, type BreakpointCapable, type BreakpointSimulatorOptions, type ConstraintSnapshot, type GuardrailTestInput, type GuardrailTestResult, type MockAgentConfig, type MockAgentRunner, type MockAgentRunnerOptions, type RecordedCall, type TestMultiAgentOrchestrator, type TestMultiAgentOrchestratorOptions, type TestOrchestrator, type TestOrchestratorOptions, assertAgentHealth, assertBreakpointHit, assertCheckpoint, assertDagExecution, assertDerivedValues, assertMultiAgentState, assertMultiplexedStream, assertOrchestratorState, assertRerouted, assertScratchpadState, assertTimelineEvents, collectMultiplexedStream, createApprovalSimulator, createBreakpointSimulator, createConstraintRecorder, createFailingRunner, createMockAgentRunner, createMockSchema, createMockTask, createTestCheckpointStore, createTestDag, createTestMultiAgentOrchestrator, createTestOrchestrator, createTestReflectionEvaluator, createTestTimeline, createTimeController, testGuardrail, testGuardrailBatch };