@dogpile/sdk 0.4.0 → 0.5.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 (83) hide show
  1. package/CHANGELOG.md +65 -0
  2. package/dist/browser/index.js +726 -176
  3. package/dist/browser/index.js.map +1 -1
  4. package/dist/index.d.ts +3 -1
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +1 -0
  7. package/dist/index.js.map +1 -1
  8. package/dist/providers/openai-compatible.d.ts.map +1 -1
  9. package/dist/providers/openai-compatible.js +1 -0
  10. package/dist/providers/openai-compatible.js.map +1 -1
  11. package/dist/runtime/audit.d.ts +42 -0
  12. package/dist/runtime/audit.d.ts.map +1 -0
  13. package/dist/runtime/audit.js +73 -0
  14. package/dist/runtime/audit.js.map +1 -0
  15. package/dist/runtime/broadcast.d.ts.map +1 -1
  16. package/dist/runtime/broadcast.js +39 -36
  17. package/dist/runtime/broadcast.js.map +1 -1
  18. package/dist/runtime/coordinator.d.ts +5 -0
  19. package/dist/runtime/coordinator.d.ts.map +1 -1
  20. package/dist/runtime/coordinator.js +50 -39
  21. package/dist/runtime/coordinator.js.map +1 -1
  22. package/dist/runtime/defaults.d.ts.map +1 -1
  23. package/dist/runtime/defaults.js +12 -4
  24. package/dist/runtime/defaults.js.map +1 -1
  25. package/dist/runtime/engine.d.ts +17 -4
  26. package/dist/runtime/engine.d.ts.map +1 -1
  27. package/dist/runtime/engine.js +523 -18
  28. package/dist/runtime/engine.js.map +1 -1
  29. package/dist/runtime/health.d.ts +51 -0
  30. package/dist/runtime/health.d.ts.map +1 -0
  31. package/dist/runtime/health.js +85 -0
  32. package/dist/runtime/health.js.map +1 -0
  33. package/dist/runtime/introspection.d.ts +96 -0
  34. package/dist/runtime/introspection.d.ts.map +1 -0
  35. package/dist/runtime/introspection.js +31 -0
  36. package/dist/runtime/introspection.js.map +1 -0
  37. package/dist/runtime/metrics.d.ts +44 -0
  38. package/dist/runtime/metrics.d.ts.map +1 -0
  39. package/dist/runtime/metrics.js +12 -0
  40. package/dist/runtime/metrics.js.map +1 -0
  41. package/dist/runtime/model.d.ts.map +1 -1
  42. package/dist/runtime/model.js +34 -7
  43. package/dist/runtime/model.js.map +1 -1
  44. package/dist/runtime/provenance.d.ts +25 -0
  45. package/dist/runtime/provenance.d.ts.map +1 -0
  46. package/dist/runtime/provenance.js +13 -0
  47. package/dist/runtime/provenance.js.map +1 -0
  48. package/dist/runtime/sequential.d.ts.map +1 -1
  49. package/dist/runtime/sequential.js +39 -36
  50. package/dist/runtime/sequential.js.map +1 -1
  51. package/dist/runtime/shared.d.ts.map +1 -1
  52. package/dist/runtime/shared.js +39 -36
  53. package/dist/runtime/shared.js.map +1 -1
  54. package/dist/runtime/tracing.d.ts +31 -0
  55. package/dist/runtime/tracing.d.ts.map +1 -0
  56. package/dist/runtime/tracing.js +18 -0
  57. package/dist/runtime/tracing.js.map +1 -0
  58. package/dist/types/events.d.ts +10 -4
  59. package/dist/types/events.d.ts.map +1 -1
  60. package/dist/types/replay.d.ts +2 -0
  61. package/dist/types/replay.d.ts.map +1 -1
  62. package/dist/types.d.ts +124 -1
  63. package/dist/types.d.ts.map +1 -1
  64. package/dist/types.js.map +1 -1
  65. package/package.json +39 -1
  66. package/src/index.ts +5 -0
  67. package/src/providers/openai-compatible.ts +1 -0
  68. package/src/runtime/audit.ts +121 -0
  69. package/src/runtime/broadcast.ts +40 -37
  70. package/src/runtime/coordinator.ts +54 -39
  71. package/src/runtime/defaults.ts +13 -4
  72. package/src/runtime/engine.ts +648 -18
  73. package/src/runtime/health.ts +136 -0
  74. package/src/runtime/introspection.ts +122 -0
  75. package/src/runtime/metrics.ts +45 -0
  76. package/src/runtime/model.ts +38 -6
  77. package/src/runtime/provenance.ts +43 -0
  78. package/src/runtime/sequential.ts +40 -37
  79. package/src/runtime/shared.ts +40 -37
  80. package/src/runtime/tracing.ts +35 -0
  81. package/src/types/events.ts +10 -4
  82. package/src/types/replay.ts +2 -0
  83. package/src/types.ts +132 -1
@@ -71,12 +71,14 @@ export interface ModelRequestEvent {
71
71
  readonly runId: string;
72
72
  /** Root-first ancestry chain when bubbled through a parent stream. */
73
73
  readonly parentRunIds?: readonly string[];
74
- /** ISO-8601 event timestamp. */
75
- readonly at: string;
74
+ /** ISO-8601 timestamp immediately before the provider call began. */
75
+ readonly startedAt: string;
76
76
  /** Stable provider call id within the run. */
77
77
  readonly callId: string;
78
78
  /** Configured model provider id receiving the request. */
79
79
  readonly providerId: string;
80
+ /** Resolved model identifier; falls back to provider id when adapter does not set modelId. */
81
+ readonly modelId: string;
80
82
  /** Agent requesting the model call. */
81
83
  readonly agentId: string;
82
84
  /** Agent role for the active model call. */
@@ -101,12 +103,16 @@ export interface ModelResponseEvent {
101
103
  readonly runId: string;
102
104
  /** Root-first ancestry chain when bubbled through a parent stream. */
103
105
  readonly parentRunIds?: readonly string[];
104
- /** ISO-8601 event timestamp. */
105
- readonly at: string;
106
+ /** ISO-8601 timestamp when the provider call started (same value as the paired ModelRequestEvent). */
107
+ readonly startedAt: string;
108
+ /** ISO-8601 timestamp after the provider call completed. Duration = completedAt minus startedAt. */
109
+ readonly completedAt: string;
106
110
  /** Stable provider call id within the run. */
107
111
  readonly callId: string;
108
112
  /** Configured model provider id that produced the response. */
109
113
  readonly providerId: string;
114
+ /** Resolved model identifier; falls back to provider id when adapter does not set modelId. */
115
+ readonly modelId: string;
110
116
  /** Agent that requested the model call. */
111
117
  readonly agentId: string;
112
118
  /** Agent role for the completed model call. */
@@ -181,6 +181,8 @@ export interface ReplayTraceProviderCall {
181
181
  readonly callId: string;
182
182
  /** Configured model provider id. */
183
183
  readonly providerId: string;
184
+ /** Resolved model identifier (same fallback as provenance events). */
185
+ readonly modelId: string;
184
186
  /** ISO-8601 timestamp before the provider call started. */
185
187
  readonly startedAt: string;
186
188
  /** ISO-8601 timestamp after the provider call completed. */
package/src/types.ts CHANGED
@@ -1,3 +1,7 @@
1
+ import type { DogpileTracer } from "./runtime/tracing.js";
2
+ import type { Logger } from "./runtime/logger.js";
3
+ import type { MetricsHook } from "./runtime/metrics.js";
4
+
1
5
  /**
2
6
  * Primitive JSON value accepted in serializable trace metadata.
3
7
  */
@@ -884,6 +888,8 @@ export interface ModelOutputChunk {
884
888
  export interface ConfiguredModelProvider {
885
889
  /** Stable provider id recorded in traces. */
886
890
  readonly id: string;
891
+ /** Optional resolved model identifier. When set, provenance events carry this value; when absent, the SDK falls back to `id`. */
892
+ readonly modelId?: string;
887
893
  /** Generate a response for one protocol-managed model request. */
888
894
  generate(request: ModelRequest): Promise<ModelResponse>;
889
895
  /**
@@ -1652,7 +1658,7 @@ export interface RunMetadata {
1652
1658
  * Result returned by high-level single-call APIs.
1653
1659
  *
1654
1660
  * The returned shape is
1655
- * `{ output, eventLog, transcript, usage, metadata, accounting, trace, cost, quality, evaluation }`.
1661
+ * `{ output, eventLog, transcript, usage, metadata, accounting, trace, cost, quality, evaluation, health }`.
1656
1662
  * `output` is the final synthesized answer, `eventLog` is the complete ordered
1657
1663
  * coordination log, `transcript` is the complete agent-turn transcript,
1658
1664
  * `usage` reports token and dollar accounting, and `metadata` exposes stable
@@ -1661,6 +1667,7 @@ export interface RunMetadata {
1661
1667
  * serializable replay artifact, `cost` is retained as a compatibility alias
1662
1668
  * for `usage`, and `quality` and `evaluation` are present when a judge or
1663
1669
  * benchmark supplies a normalized score and serializable evaluation payload.
1670
+ * `health` exposes machine-readable diagnostics once computed by the runtime.
1664
1671
  */
1665
1672
  export interface RunResult {
1666
1673
  /** Final synthesized answer for the supplied intent. */
@@ -1688,6 +1695,15 @@ export interface RunResult {
1688
1695
  readonly quality?: NormalizedQualityScore;
1689
1696
  /** Optional serializable evaluation data supplied by a caller-owned evaluator. */
1690
1697
  readonly evaluation?: RunEvaluation;
1698
+ /**
1699
+ * Machine-readable health summary for the run, auto-computed from trace events.
1700
+ *
1701
+ * Always present. Re-computed identically by `replay()` from the same trace —
1702
+ * contains no information beyond what the trace events carry. Use
1703
+ * `computeHealth(trace, thresholds)` from `@dogpile/sdk/runtime/health` to
1704
+ * re-compute with custom thresholds on a stored trace.
1705
+ */
1706
+ readonly health: RunHealthSummary;
1691
1707
  }
1692
1708
 
1693
1709
  /**
@@ -1696,6 +1712,69 @@ export interface RunResult {
1696
1712
  */
1697
1713
  export type RunEvaluator = (result: Omit<RunResult, "quality" | "evaluation">) => RunEvaluation | Promise<RunEvaluation>;
1698
1714
 
1715
+ /**
1716
+ * Machine-readable code identifying a health anomaly detected in a completed run.
1717
+ *
1718
+ * - `"runaway-turns"`: an agent exceeded the configured per-agent turn threshold.
1719
+ * - `"budget-near-miss"`: budget utilization exceeded the configured near-miss threshold.
1720
+ * - `"empty-contribution"`: an agent produced an empty (blank/whitespace) output turn.
1721
+ * - `"provider-error-recovered"`: a provider call failed and was retried successfully.
1722
+ * Detection is deferred — computeHealth never emits this code in Phase 7 because no
1723
+ * trace signal exists without an event-shape change. The code is reserved for future use.
1724
+ */
1725
+ export type AnomalyCode =
1726
+ | "runaway-turns"
1727
+ | "budget-near-miss"
1728
+ | "empty-contribution"
1729
+ | "provider-error-recovered";
1730
+
1731
+ /**
1732
+ * A single health anomaly detected in a completed run trace.
1733
+ *
1734
+ * All fields are required except `agentId`, which is present for per-agent anomalies
1735
+ * (`"runaway-turns"`, `"empty-contribution"`, `"provider-error-recovered"`) and absent
1736
+ * for global anomalies (`"budget-near-miss"`).
1737
+ */
1738
+ export interface HealthAnomaly {
1739
+ /** Machine-readable anomaly identifier. */
1740
+ readonly code: AnomalyCode;
1741
+ /** Severity level: `"error"` for runaway turns and empty contributions; `"warning"` for near-miss and recovered patterns. */
1742
+ readonly severity: "warning" | "error";
1743
+ /** Actual measured value that triggered the anomaly (turn count, utilization %, etc.). */
1744
+ readonly value: number;
1745
+ /** Threshold value that was exceeded (for comparison in UIs and alerting). */
1746
+ readonly threshold: number;
1747
+ /** Agent that triggered the anomaly, present for per-agent anomaly codes. */
1748
+ readonly agentId?: string;
1749
+ }
1750
+
1751
+ /**
1752
+ * Machine-readable health summary for a completed run, auto-computed from trace events.
1753
+ *
1754
+ * Always present on {@link RunResult}. Re-computed identically by `replay()` from the
1755
+ * same trace — no information is stored beyond what the trace events contain.
1756
+ *
1757
+ * Use `computeHealth(trace, thresholds)` from `@dogpile/sdk/runtime/health` to re-compute
1758
+ * with custom thresholds.
1759
+ */
1760
+ export interface RunHealthSummary {
1761
+ /** Detected health anomalies. Empty array when no anomalies are found. */
1762
+ readonly anomalies: readonly HealthAnomaly[];
1763
+ /** Derived stats computed from trace events. */
1764
+ readonly stats: {
1765
+ /** Total number of agent-turn events in the trace. */
1766
+ readonly totalTurns: number;
1767
+ /** Number of unique agents that produced at least one turn. */
1768
+ readonly agentCount: number;
1769
+ /**
1770
+ * Budget utilization as a percentage (0–100+).
1771
+ * `null` when no USD cap was configured for the run.
1772
+ * Computed as `(finalCost / maxUsd) * 100`.
1773
+ */
1774
+ readonly budgetUtilizationPct: number | null;
1775
+ };
1776
+ }
1777
+
1699
1778
  /**
1700
1779
  * Mission supplied to a high-level Dogpile workflow call.
1701
1780
  *
@@ -1807,6 +1886,32 @@ export interface DogpileOptions extends BudgetCostTierOptions {
1807
1886
  readonly seed?: string | number;
1808
1887
  /** Optional caller cancellation signal passed to provider-facing model requests. */
1809
1888
  readonly signal?: AbortSignal;
1889
+ /**
1890
+ * Optional duck-typed OTEL-compatible tracer. When provided, the SDK emits
1891
+ * spans for run start/end, sub-run start/end, agent-turn start/end, and
1892
+ * model-call start/end with correct parent-child ancestry. When absent the
1893
+ * run completes with zero span overhead — no allocations, no branch cost.
1894
+ * `replay()` and `replayStream()` ignore this field entirely.
1895
+ * See {@link DogpileTracer} in `@dogpile/sdk/runtime/tracing`.
1896
+ */
1897
+ readonly tracer?: DogpileTracer;
1898
+ /**
1899
+ * Optional callback object for run-completion metrics. When provided,
1900
+ * `onRunComplete` fires with a `RunMetricsSnapshot` at every terminal state
1901
+ * (completed, budget-stopped, or aborted). `onSubRunComplete` fires for each
1902
+ * coordinator-dispatched child run that completes. Hook errors are routed to
1903
+ * `logger.error` (or `console.error` when no logger is provided) and never
1904
+ * propagate into the run result. When absent, zero overhead — no allocations.
1905
+ * See `@dogpile/sdk/runtime/metrics` for the interface.
1906
+ */
1907
+ readonly metricsHook?: MetricsHook;
1908
+ /**
1909
+ * Optional structured logger for SDK-internal diagnostics (hook errors and
1910
+ * future debug/info events). Implement against pino, winston, or any other
1911
+ * logger by satisfying the `Logger` interface from `@dogpile/sdk/runtime/logger`.
1912
+ * When absent, hook errors fall back to `console.error`.
1913
+ */
1914
+ readonly logger?: Logger;
1810
1915
  /**
1811
1916
  * Maximum coordinator → sub-run recursion depth.
1812
1917
  *
@@ -1911,6 +2016,32 @@ export interface EngineOptions {
1911
2016
  readonly seed?: string | number;
1912
2017
  /** Optional caller cancellation signal passed to provider-facing model requests. */
1913
2018
  readonly signal?: AbortSignal;
2019
+ /**
2020
+ * Optional duck-typed OTEL-compatible tracer. When provided, the SDK emits
2021
+ * spans for run start/end, sub-run start/end, agent-turn start/end, and
2022
+ * model-call start/end with correct parent-child ancestry. When absent the
2023
+ * run completes with zero span overhead — no allocations, no branch cost.
2024
+ * `replay()` and `replayStream()` ignore this field entirely.
2025
+ * See {@link DogpileTracer} in `@dogpile/sdk/runtime/tracing`.
2026
+ */
2027
+ readonly tracer?: DogpileTracer;
2028
+ /**
2029
+ * Optional callback object for run-completion metrics. When provided,
2030
+ * `onRunComplete` fires with a `RunMetricsSnapshot` at every terminal state
2031
+ * (completed, budget-stopped, or aborted). `onSubRunComplete` fires for each
2032
+ * coordinator-dispatched child run that completes. Hook errors are routed to
2033
+ * `logger.error` (or `console.error` when no logger is provided) and never
2034
+ * propagate into the run result. When absent, zero overhead — no allocations.
2035
+ * See `@dogpile/sdk/runtime/metrics` for the interface.
2036
+ */
2037
+ readonly metricsHook?: MetricsHook;
2038
+ /**
2039
+ * Optional structured logger for SDK-internal diagnostics (hook errors and
2040
+ * future debug/info events). Implement against pino, winston, or any other
2041
+ * logger by satisfying the `Logger` interface from `@dogpile/sdk/runtime/logger`.
2042
+ * When absent, hook errors fall back to `console.error`.
2043
+ */
2044
+ readonly logger?: Logger;
1914
2045
  /**
1915
2046
  * Maximum coordinator → sub-run recursion depth ceiling.
1916
2047
  *