@tangle-network/agent-runtime 0.25.2 → 0.27.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/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { AgentEvalError, KnowledgeReadinessReport, ControlEvalResult, KnowledgeRequirement } from '@tangle-network/agent-eval';
2
2
  export { AgentEvalError, AgentEvalErrorCode, ConfigError, ControlBudget, ControlDecision, ControlEvalResult, ControlRunResult, ControlStep, DataAcquisitionPlan, JudgeError, KnowledgeReadinessReport, KnowledgeRequirement, NotFoundError, RunRecord, ValidationError } from '@tangle-network/agent-eval';
3
- import { a as AgentBackendInput, b as AgentExecutionBackend, O as OpenAIChatTool, c as OpenAIChatToolChoice, d as AgentBackendContext, R as RuntimeStreamEvent, K as KnowledgeReadinessDecision, e as RunAgentTaskOptions, f as AgentTaskRunResult, g as RunAgentTaskStreamOptions, h as AgentRuntimeEvent, i as AgentTaskStatus, j as RuntimeSessionStore, k as RuntimeSession } from './types-BFgFD_sl.js';
4
- export { l as AgentAdapter, m as AgentKnowledgeProvider, n as AgentRuntimeEventSink, o as AgentTaskContext, A as AgentTaskSpec, B as BackendErrorDetail } from './types-BFgFD_sl.js';
5
- export { O as OtelAttribute, a as OtelExportConfig, b as OtelExporter, c as OtelSpan, d as createOtelExporter, l as loopEventToOtelSpan, m as mcpToolsForRuntimeMcp, e as mcpToolsForRuntimeMcpSubset } from './otel-export-B33Cy_60.js';
6
- export { R as RuntimeRunHandle, a as RuntimeRunPersistenceAdapter, b as RuntimeRunRow, s as startRuntimeRun } from './runtime-run-D5ItCKl_.js';
3
+ import { a as AgentBackendInput, b as AgentExecutionBackend, O as OpenAIChatTool, c as OpenAIChatToolChoice, d as AgentBackendContext, R as RuntimeStreamEvent, K as KnowledgeReadinessDecision, e as RunAgentTaskOptions, f as AgentTaskRunResult, g as RunAgentTaskStreamOptions, h as AgentRuntimeEvent, i as AgentTaskStatus, j as RuntimeSessionStore, k as RuntimeSession } from './types-CsCCryln.js';
4
+ export { l as AgentAdapter, m as AgentKnowledgeProvider, n as AgentRuntimeEventSink, o as AgentTaskContext, A as AgentTaskSpec, B as BackendErrorDetail } from './types-CsCCryln.js';
5
+ export { b as OtelAttribute, c as OtelExportConfig, O as OtelExporter, d as OtelSpan, e as createOtelExporter, l as loopEventToOtelSpan, m as mcpToolsForRuntimeMcp, a as mcpToolsForRuntimeMcpSubset } from './otel-export-B2UBcPV4.js';
6
+ export { R as RuntimeRunHandle, a as RuntimeRunPersistenceAdapter, b as RuntimeRunRow, s as startRuntimeRun } from './runtime-run-B8VIiOhI.js';
7
7
 
8
8
  /**
9
9
  * @stable
@@ -126,6 +126,692 @@ declare function createOpenAICompatibleBackend<TInput extends AgentBackendInput
126
126
  retry?: BackendRetryPolicy;
127
127
  }): AgentExecutionBackend<TInput>;
128
128
 
129
+ /**
130
+ * @stable
131
+ *
132
+ * Per-call resilience policy for participant backends: deadline, retry with
133
+ * backoff, and a circuit breaker. Each policy is applied *around* a single
134
+ * turn's backend invocation, not across the whole conversation — the
135
+ * conversation-level credit cap and `maxTurns` bound the broader run.
136
+ *
137
+ * Deadlines abort the underlying backend stream via `AbortSignal` linkage so
138
+ * the OpenAI/SDK clients tear down their HTTP request cleanly instead of
139
+ * leaking sockets. Retries replay the same logical turn (same `turnId`) so
140
+ * any caching gateway can dedupe. Circuit breakers are *per participant*: A's
141
+ * failures don't open B's breaker.
142
+ */
143
+ /** Pure judgment of whether an error is worth retrying. Defaults: TimeoutError, AbortError, fetch-level network errors. */
144
+ type RetryableErrorPredicate = (err: unknown) => boolean;
145
+ /** Backoff between attempts. Constant ms, or `(attempt: 1-indexed) => ms`. */
146
+ type RetryBackoff = number | ((attempt: number) => number);
147
+ /** Circuit-breaker tuning. `failuresToOpen` consecutive failures opens it; closed only after `cooldownMs`. */
148
+ interface CircuitBreakerConfig {
149
+ failuresToOpen: number;
150
+ cooldownMs: number;
151
+ }
152
+ interface BackendCallPolicy {
153
+ /** Per-attempt wall clock limit. Exceeding fires an AbortSignal and is treated as a retryable failure. */
154
+ perAttemptDeadlineMs?: number;
155
+ /** Number of retries after the first attempt; total attempts = 1 + maxRetries. Default 0. */
156
+ maxRetries?: number;
157
+ /** Backoff between attempts. Default 250ms with jitter. */
158
+ retryBackoffMs?: RetryBackoff;
159
+ /** Custom retry classifier. Defaults to {@link defaultIsRetryable}. */
160
+ isRetryable?: RetryableErrorPredicate;
161
+ /** Circuit breaker that opens after N consecutive failures per participant. */
162
+ circuitBreaker?: CircuitBreakerConfig;
163
+ }
164
+ declare class CircuitOpenError extends Error {
165
+ constructor(participant: string, retryAfterMs: number);
166
+ }
167
+ declare class DeadlineExceededError extends Error {
168
+ constructor(deadlineMs: number);
169
+ }
170
+ /**
171
+ * Default retryable classification — network/timeout class errors. Errors
172
+ * a model deliberately throws (validation, refusal, 4xx) are not retried;
173
+ * those represent real outcomes, not transient infrastructure faults.
174
+ */
175
+ declare const defaultIsRetryable: RetryableErrorPredicate;
176
+ /** Live circuit-breaker state — one instance per (participant, conversation run). */
177
+ declare class CircuitBreakerState {
178
+ private readonly config;
179
+ private consecutiveFailures;
180
+ private openedAt;
181
+ constructor(config: CircuitBreakerConfig | undefined);
182
+ /**
183
+ * Check whether the next call is allowed. Throws `CircuitOpenError` when
184
+ * the breaker is open and the cooldown hasn't elapsed.
185
+ */
186
+ preflight(participant: string, now?: number): void;
187
+ recordSuccess(): void;
188
+ recordFailure(now?: number): void;
189
+ }
190
+ /**
191
+ * Build a per-attempt AbortSignal linked to the parent signal AND fired when
192
+ * the deadline elapses. The returned `dispose()` MUST be called in a
193
+ * `finally` (clears the timer, detaches the listener) so we don't leak.
194
+ *
195
+ * When the deadline fires, the signal's `reason` is a `DeadlineExceededError`
196
+ * — callers can detect timeout-vs-cancel by reading `signal.reason` after
197
+ * the underlying operation throws.
198
+ */
199
+ declare function makePerAttemptSignal(parentSignal: AbortSignal | undefined, deadlineMs: number | undefined): {
200
+ signal: AbortSignal;
201
+ dispose: () => void;
202
+ getDeadlineError(): DeadlineExceededError | undefined;
203
+ };
204
+ /** Compute the delay before the next attempt. Default: 250ms exponential with jitter. */
205
+ declare function computeBackoff(spec: RetryBackoff | undefined, attempt: number): number;
206
+ declare function sleep(ms: number): Promise<void>;
207
+
208
+ /**
209
+ * @stable
210
+ *
211
+ * Cross-gateway forwarding headers — the wire-level contract that makes
212
+ * agent-to-agent communication composable across organizational boundaries.
213
+ * Every header here is read on inbound and re-emitted on outbound, so a chain
214
+ * `caller → A's gateway → A's runtime → B's gateway → B's runtime` ends with
215
+ * B billing the original user, the depth counter monotonically incremented,
216
+ * and the run/turn correlation IDs preserved end-to-end.
217
+ *
218
+ * The actual depth refusal (HTTP 413 at MAX_DEPTH) is enforced by
219
+ * `agent-gateway`'s middleware; this module owns the names + the propagation
220
+ * rules so both sides agree.
221
+ *
222
+ * Full protocol: `docs/agent-bus-protocol.md`.
223
+ */
224
+ /** Standard names — lowercased so Headers maps interop on every runtime. */
225
+ declare const FORWARD_HEADERS: {
226
+ /** Forwarded original-user identity (`Bearer sk-tan-<user>`); downstream gateways bill against this. */
227
+ readonly authorization: "x-tangle-forwarded-authorization";
228
+ /** Monotonically incremented on every gateway hop. Refused at MAX_DEPTH. */
229
+ readonly depth: "x-tangle-forwarded-depth";
230
+ /** Top-level conversation run identifier, propagated through every nested call. */
231
+ readonly runId: "x-tangle-runid";
232
+ /** This call's turn within the run; deterministic + stable across retries. */
233
+ readonly turnId: "x-tangle-turnid";
234
+ /** When the call is *inside* another turn (recursion), the parent turn's id. */
235
+ readonly parentTurnId: "x-tangle-parent-turnid";
236
+ /** Logical conversation peer label at the sending side, for trace stitching. */
237
+ readonly speaker: "x-tangle-speaker";
238
+ };
239
+ type ForwardHeaderName = (typeof FORWARD_HEADERS)[keyof typeof FORWARD_HEADERS];
240
+ /** Hard cap on chained gateway hops; refused beyond this. Default keeps recursion bounded. */
241
+ declare const DEFAULT_MAX_DEPTH = 4;
242
+ /**
243
+ * Read the depth counter off an inbound request. Missing → 0 (caller is the
244
+ * origin). Non-integer → throws — silent coercion would let a bad caller
245
+ * reset depth and bypass the limit.
246
+ */
247
+ declare function readDepth(headers: Readonly<Record<string, string | string[] | undefined>>): number;
248
+ /**
249
+ * Refuse further forwarding when the inbound depth has reached the limit.
250
+ * Callers (the gateway middleware) translate the boolean to an HTTP 413.
251
+ */
252
+ declare function isDepthExceeded(inboundDepth: number, max?: number): boolean;
253
+ /**
254
+ * Build the headers to emit on an outbound participant call, given the
255
+ * conversation's propagation context. Depth is incremented from the inbound
256
+ * value; runId / turnId / speaker stamp the current hop; the user's
257
+ * `Authorization` is preserved verbatim so the downstream gateway bills the
258
+ * right wallet.
259
+ */
260
+ declare function buildForwardHeaders(input: {
261
+ inboundDepth: number;
262
+ forwardedAuthorization?: string;
263
+ runId: string;
264
+ turnId: string;
265
+ parentTurnId?: string;
266
+ speaker: string;
267
+ }): Record<string, string>;
268
+ /**
269
+ * Header bag carried through `AgentBackendContext.propagatedHeaders` so
270
+ * backends that opt in can merge them into their outbound HTTP requests.
271
+ * Distinct from `buildForwardHeaders` so callers can attach extra
272
+ * non-protocol headers (e.g. tracing) without colliding.
273
+ */
274
+ type PropagatedHeaders = Readonly<Record<string, string>>;
275
+
276
+ /**
277
+ * @stable
278
+ *
279
+ * Durable conversation transcript — survives a driver process crash mid-run.
280
+ * The runner journals every committed turn before yielding `turn_end`, so a
281
+ * resumed run replays the same `runId` against the same journal and picks up
282
+ * from the first un-recorded turn. Combined with the deterministic
283
+ * `turnId(runId, index, speaker)`, a retried turn collides with the prior
284
+ * attempt's id and any caching gateway can dedupe.
285
+ *
286
+ * The interface is small enough that a Cloudflare D1 / R2 / postgres adapter
287
+ * is ~30 lines. The in-memory adapter is the default for tests and scratch.
288
+ * The file adapter (JSONL on disk) is the default-durable choice when no
289
+ * upstream store is wired.
290
+ */
291
+
292
+ interface ConversationJournalEntry {
293
+ runId: string;
294
+ startedAt: string;
295
+ /** Set when the run reaches a terminal state. */
296
+ halted?: HaltReason;
297
+ endedAt?: string;
298
+ turns: ConversationTurn[];
299
+ }
300
+ interface ConversationJournal {
301
+ /**
302
+ * Load any prior state for `runId`. Returns `undefined` for a fresh run.
303
+ * Implementations MUST NOT mutate the returned object — the runner clones
304
+ * before continuing — but the runtime treats absence and emptiness
305
+ * identically, so a journal with zero turns is equivalent to "fresh."
306
+ */
307
+ loadRun(runId: string): Promise<ConversationJournalEntry | undefined>;
308
+ /**
309
+ * Initialise journal state for a fresh run. Called once per run, before any
310
+ * `appendTurn`. Idempotent: calling with an existing runId is a no-op if
311
+ * the entry already exists with the same `startedAt`.
312
+ */
313
+ beginRun(runId: string, startedAt: string): Promise<void>;
314
+ /**
315
+ * Append a committed turn. The runner only calls this AFTER the turn's
316
+ * backend stream completed and the credit total has been updated, so an
317
+ * appended turn is observed-committed and never speculative.
318
+ */
319
+ appendTurn(runId: string, turn: ConversationTurn): Promise<void>;
320
+ /**
321
+ * Record the run's terminal halt reason + end time. Once called, the run
322
+ * is observed-final; subsequent `loadRun` returns the same halt.
323
+ */
324
+ recordHalt(runId: string, halt: HaltReason, endedAt: string): Promise<void>;
325
+ }
326
+ declare class InMemoryConversationJournal implements ConversationJournal {
327
+ private readonly entries;
328
+ loadRun(runId: string): Promise<ConversationJournalEntry | undefined>;
329
+ beginRun(runId: string, startedAt: string): Promise<void>;
330
+ appendTurn(runId: string, turn: ConversationTurn): Promise<void>;
331
+ recordHalt(runId: string, halt: HaltReason, endedAt: string): Promise<void>;
332
+ }
333
+ /**
334
+ * JSONL on disk. One line per record; first line is the `begin`, subsequent
335
+ * lines are `turn` records, terminal line is `halt`. Replays the whole file
336
+ * on `loadRun` — cheap for the conversation sizes this is designed for
337
+ * (thousands of turns, not millions). For huge runs, plug in a real DB
338
+ * adapter; the interface is small.
339
+ *
340
+ * Each `appendTurn` / `recordHalt` calls `fsync` after the write so a
341
+ * process crash between writes never loses an acknowledged turn.
342
+ */
343
+ declare class FileConversationJournal implements ConversationJournal {
344
+ private readonly path;
345
+ constructor(path: string);
346
+ loadRun(runId: string): Promise<ConversationJournalEntry | undefined>;
347
+ beginRun(runId: string, startedAt: string): Promise<void>;
348
+ appendTurn(runId: string, turn: ConversationTurn): Promise<void>;
349
+ recordHalt(runId: string, halt: HaltReason, endedAt: string): Promise<void>;
350
+ private appendRecord;
351
+ }
352
+
353
+ /**
354
+ * @stable
355
+ *
356
+ * Public types for multi-agent conversations. A `Conversation` is two-or-more
357
+ * participants taking turns through their own `AgentExecutionBackend`s, driven
358
+ * by a `ConversationPolicy` (turn order, halting, hard credit ceiling).
359
+ *
360
+ * Each participant's backend can resolve to any reachable endpoint —
361
+ * in-process iterable, local cli-bridge, sandbox, router, or a remote
362
+ * agent-gateway — so the same `runConversation` call drives same-machine,
363
+ * same-cloud, and cross-cloud orchestration without code change.
364
+ */
365
+
366
+ /** @stable */
367
+ interface ConversationParticipant {
368
+ /**
369
+ * Stable name used as the speaker label in the transcript. Must be unique
370
+ * within a `Conversation`.
371
+ */
372
+ name: string;
373
+ /**
374
+ * Backend that runs this participant's turn. Reuses the existing
375
+ * `AgentExecutionBackend` contract from `runAgentTaskStream`, so any
376
+ * registered backend (iterable, sandbox, OpenAI-compatible) works without
377
+ * adaptation.
378
+ */
379
+ backend: AgentExecutionBackend;
380
+ /**
381
+ * Optional human label for traces / dashboards. Distinct from `name`, which
382
+ * is the addressing key.
383
+ */
384
+ label?: string;
385
+ /**
386
+ * Optional per-participant override of the conversation's default
387
+ * `callPolicy`. Use to tighten the deadline or raise the retry budget for
388
+ * a participant known to be slow or flaky.
389
+ */
390
+ callPolicy?: BackendCallPolicy;
391
+ /**
392
+ * Who pays for THIS participant's outbound calls?
393
+ *
394
+ * - `'forward-user'` (default) — propagate the caller's
395
+ * `X-Tangle-Forwarded-Authorization` so the downstream gateway bills the
396
+ * original user. Right for pass-through agents that aggregate/route
397
+ * without taking economic risk.
398
+ * - `'agent-owned'` — DO NOT forward the user's auth; the participant's
399
+ * backend uses its own credentials (typically a sk-tan-AGENT or x402
400
+ * wallet baked into the backend at construction). Downstream charges
401
+ * land on the agent, not the user. Right for resold-bundle agents that
402
+ * take margin between their inbound price and their sub-agent costs.
403
+ * - `(state) => AuthSource` — per-turn / per-condition decision, e.g. base
404
+ * sub-services are agent-owned but premium add-ons forward the user.
405
+ *
406
+ * The agent's own credentials live on the backend (set at construction
407
+ * time, e.g. `createOpenAICompatibleBackend({ apiKey })`); this field is
408
+ * purely about *whether to also forward the user's identity downstream*.
409
+ */
410
+ authSource?: AuthSource;
411
+ }
412
+ /** @stable */
413
+ type AuthSource = 'forward-user' | 'agent-owned' | ((state: ConversationDriveState) => 'forward-user' | 'agent-owned');
414
+ /** @stable */
415
+ type TurnOrder = 'alternate' | 'round-robin' | ((state: ConversationDriveState) => number);
416
+ /** @stable */
417
+ interface ConversationDriveState {
418
+ transcript: readonly ConversationTurn[];
419
+ turnIndex: number;
420
+ spentCreditsCents: number;
421
+ }
422
+ /** @stable */
423
+ interface HaltContext extends ConversationDriveState {
424
+ lastTurn: ConversationTurn;
425
+ }
426
+ /** @stable */
427
+ interface HaltSignal {
428
+ halted: true;
429
+ reason: string;
430
+ }
431
+ /** @stable */
432
+ type HaltPredicate = (ctx: HaltContext) => boolean | HaltSignal | Promise<boolean | HaltSignal>;
433
+ /** @stable */
434
+ type HaltReason = {
435
+ kind: 'max_turns';
436
+ turns: number;
437
+ } | {
438
+ kind: 'max_credits';
439
+ spentCents: number;
440
+ capCents: number;
441
+ } | {
442
+ kind: 'predicate';
443
+ reason: string;
444
+ } | {
445
+ kind: 'abort';
446
+ } | {
447
+ kind: 'participant_error';
448
+ participant: string;
449
+ message: string;
450
+ };
451
+ /** @stable */
452
+ interface ConversationPolicy {
453
+ /** Hard cap on speaker-turns. Each call into a participant's backend counts as 1. */
454
+ maxTurns: number;
455
+ /**
456
+ * Hard cap on aggregate credit spend across all participants, in cents.
457
+ * Computed by summing `llm_call.costUsd` from every participant's stream.
458
+ * Unset (`undefined`) means no credit ceiling — the run is bounded only by
459
+ * `maxTurns` and `haltOn`.
460
+ */
461
+ maxCreditsCents?: number;
462
+ /**
463
+ * Speaker selection. Defaults to `'alternate'` for two-participant
464
+ * conversations and `'round-robin'` for any other arity.
465
+ */
466
+ turnOrder?: TurnOrder;
467
+ /**
468
+ * Optional convergence / content-based halt. Called after every turn ends;
469
+ * returning truthy stops the loop with `{ kind: 'predicate', ... }`.
470
+ */
471
+ haltOn?: HaltPredicate;
472
+ /**
473
+ * Default per-turn resilience policy applied to every participant call
474
+ * (deadline, retries, circuit breaker). Individual participants may
475
+ * override via `ConversationParticipant.callPolicy`.
476
+ */
477
+ defaultCallPolicy?: BackendCallPolicy;
478
+ }
479
+ /** @stable */
480
+ interface ConversationTurn {
481
+ index: number;
482
+ speaker: string;
483
+ /**
484
+ * Deterministic turn identifier — stable across retries of the same logical
485
+ * turn so caching gateways and trace backends can dedupe. Shape:
486
+ * `${runId}.t${index}.${speakerSlug}`.
487
+ */
488
+ turnId: string;
489
+ text: string;
490
+ /**
491
+ * Aggregated backend usage for this turn alone. Populated from any
492
+ * `llm_call` stream events the backend emitted; `undefined` when the
493
+ * backend reports no usage.
494
+ */
495
+ usage?: {
496
+ tokensIn?: number;
497
+ tokensOut?: number;
498
+ costUsd?: number;
499
+ latencyMs?: number;
500
+ model?: string;
501
+ };
502
+ /**
503
+ * Number of attempts that ran before this turn committed. `1` is the
504
+ * common case; higher means the call policy retried after transient
505
+ * failures.
506
+ */
507
+ attempts: number;
508
+ startedAt: string;
509
+ endedAt: string;
510
+ }
511
+ /** @stable */
512
+ interface Conversation {
513
+ participants: readonly ConversationParticipant[];
514
+ policy: ConversationPolicy;
515
+ }
516
+ /** @stable */
517
+ interface RunConversationOptions {
518
+ /** First message kicking off the conversation. Routes to the first speaker. */
519
+ seed: string;
520
+ /**
521
+ * Optional run identifier for cross-participant trace correlation. Auto-
522
+ * generated when omitted. Reusing a runId against the same `journal`
523
+ * resumes the prior run — the runner replays the persisted transcript and
524
+ * continues from the first un-recorded turn.
525
+ */
526
+ runId?: string;
527
+ /** Cancellation signal — aborts mid-stream and halts with `{ kind: 'abort' }`. */
528
+ signal?: AbortSignal;
529
+ /**
530
+ * Event sink for per-turn micro-events. Distinct from the result transcript:
531
+ * the sink fires for every text-delta, every turn-start/end, and the
532
+ * conversation-start/end markers. Used to drive SSE / dashboard updates
533
+ * without waiting for the conversation to finish.
534
+ */
535
+ onEvent?: (event: ConversationStreamEvent) => void | Promise<void>;
536
+ /**
537
+ * Optional durable transcript. When set, the runner persists every
538
+ * committed turn before yielding `turn_end`. Reusing the same `runId`
539
+ * against the same journal resumes from the last committed turn — so a
540
+ * driver process crash mid-run loses zero acknowledged turns.
541
+ */
542
+ journal?: ConversationJournal;
543
+ /**
544
+ * Headers to forward verbatim to every participant backend call (gateway
545
+ * propagation: `X-Tangle-Forwarded-Authorization`, run/turn correlation,
546
+ * depth counter). Backends opt in by reading `propagatedHeaders` from
547
+ * their `AgentBackendContext`; backends that ignore the field still work.
548
+ */
549
+ propagatedHeaders?: PropagatedHeaders;
550
+ /**
551
+ * Inbound depth at the point this driver was invoked. The runner
552
+ * increments it on every outbound participant call; gateways refuse at
553
+ * `DEFAULT_MAX_DEPTH`. Default 0 (origin caller).
554
+ */
555
+ inboundDepth?: number;
556
+ /**
557
+ * Parent turn id when this conversation is *inside* another turn (i.e. the
558
+ * driver is itself a participant via `createConversationBackend`). The
559
+ * runner stamps each outbound call with this as `X-Tangle-Parent-TurnId`
560
+ * so trace stitching survives nested orchestration.
561
+ */
562
+ parentTurnId?: string;
563
+ }
564
+ /** @stable */
565
+ interface ConversationResult {
566
+ runId: string;
567
+ transcript: ConversationTurn[];
568
+ turns: number;
569
+ spentCreditsCents: number;
570
+ halted: HaltReason;
571
+ durationMs: number;
572
+ startedAt: string;
573
+ endedAt: string;
574
+ }
575
+ /** @stable */
576
+ type ConversationStreamEvent = {
577
+ type: 'conversation_start';
578
+ runId: string;
579
+ participants: readonly string[];
580
+ seed: string;
581
+ timestamp: string;
582
+ } | {
583
+ type: 'conversation_resumed';
584
+ runId: string;
585
+ participants: readonly string[];
586
+ transcript: readonly ConversationTurn[];
587
+ timestamp: string;
588
+ } | {
589
+ type: 'turn_start';
590
+ runId: string;
591
+ index: number;
592
+ speaker: string;
593
+ turnId: string;
594
+ attempt: number;
595
+ timestamp: string;
596
+ } | {
597
+ type: 'turn_text_delta';
598
+ runId: string;
599
+ index: number;
600
+ speaker: string;
601
+ turnId: string;
602
+ text: string;
603
+ timestamp?: string;
604
+ } | {
605
+ type: 'turn_retry';
606
+ runId: string;
607
+ index: number;
608
+ speaker: string;
609
+ turnId: string;
610
+ attempt: number;
611
+ reason: string;
612
+ timestamp: string;
613
+ } | {
614
+ type: 'turn_end';
615
+ runId: string;
616
+ turn: ConversationTurn;
617
+ timestamp: string;
618
+ } | {
619
+ type: 'conversation_end';
620
+ runId: string;
621
+ result: ConversationResult;
622
+ timestamp: string;
623
+ };
624
+
625
+ /**
626
+ * @stable
627
+ *
628
+ * Wrap a `Conversation` so it satisfies `AgentExecutionBackend`. The result is
629
+ * an addressable "single agent" whose internal behavior is an N-party
630
+ * orchestrated conversation — the recursion primitive that lets a swarm be a
631
+ * participant inside another swarm, or be published behind a single
632
+ * agent-gateway endpoint.
633
+ *
634
+ * Stream events from inner participants are NOT forwarded verbatim. Outer
635
+ * callers see one `text_delta` per inner turn (the turn's full text), tagged
636
+ * with `[speaker] ` prefix so the outer transcript stays attributable. The
637
+ * conversation's `conversation_end` halt reason rides on a `final` event.
638
+ */
639
+
640
+ declare function createConversationBackend(options: {
641
+ conversation: Conversation;
642
+ /** Optional backend kind label. Defaults to `'conversation'`. */
643
+ kind?: string;
644
+ }): AgentExecutionBackend;
645
+
646
+ /**
647
+ * @stable
648
+ *
649
+ * Declarative constructor for a multi-agent `Conversation`. Validates inputs
650
+ * fail-loud at definition time (duplicate participant names, alternate order
651
+ * with ≠2 participants, non-positive `maxTurns`) so misconfiguration is caught
652
+ * before `runConversation` is called and not buried inside a streaming run.
653
+ */
654
+
655
+ declare function defineConversation(input: {
656
+ participants: ConversationParticipant[];
657
+ policy: ConversationPolicy;
658
+ }): Conversation;
659
+
660
+ /**
661
+ * @stable
662
+ *
663
+ * Durable conversation journal backed by any SQL store. Adapter-agnostic by
664
+ * design: callers wire a `SqlAdapter` against their driver of choice (D1,
665
+ * postgres, sqlite, libSQL…) and the same journal implementation persists
666
+ * conversation runs durably across process restarts. The schema is two
667
+ * tables — runs (latest state) + events (append-only log) — so a partial
668
+ * crash in the middle of a turn leaves an unambiguous "last committed turn"
669
+ * to resume from.
670
+ *
671
+ * Why not bake in a specific driver? agent-runtime ships against multiple
672
+ * runtimes (Cloudflare Workers, Node, Bun, Deno) and consumers' fleets have
673
+ * already standardized on one of D1 / postgres / sqlite / libSQL. Adapter
674
+ * indirection costs ~5 lines per driver in the consumer's code and keeps the
675
+ * SDK free of native deps.
676
+ *
677
+ * @example D1 (Cloudflare Workers)
678
+ * import { SqlConversationJournal, d1ToSqlAdapter } from '@tangle-network/agent-runtime'
679
+ * const journal = new SqlConversationJournal(d1ToSqlAdapter(env.DB))
680
+ * await journal.migrate() // once at deploy
681
+ * await runConversation(conv, { seed, journal, runId: 'run_abc' })
682
+ *
683
+ * @example node-postgres
684
+ * import { Pool } from 'pg'
685
+ * const pool = new Pool({ connectionString: process.env.DATABASE_URL })
686
+ * const pg: SqlAdapter = {
687
+ * exec: async (sql, params = []) => {
688
+ * const r = await pool.query(sql, params as never)
689
+ * return { rowsAffected: r.rowCount ?? 0 }
690
+ * },
691
+ * query: async (sql, params = []) => (await pool.query(sql, params as never)).rows,
692
+ * }
693
+ * const journal = new SqlConversationJournal(pg)
694
+ * await journal.migrate()
695
+ */
696
+
697
+ /**
698
+ * Minimal SQL driver shape. Implementations forward to whichever client the
699
+ * deployment already uses; agent-runtime takes no opinion on which.
700
+ *
701
+ * Parameter placeholders MUST be `?` (positional). All adapters listed in the
702
+ * file header accept this convention.
703
+ */
704
+ interface SqlAdapter {
705
+ /** Execute a write statement (INSERT/UPDATE/DELETE/DDL). */
706
+ exec(sql: string, params?: readonly unknown[]): Promise<{
707
+ rowsAffected: number;
708
+ }>;
709
+ /** Execute a read statement (SELECT). Returns rows as plain objects. */
710
+ query<TRow = Record<string, unknown>>(sql: string, params?: readonly unknown[]): Promise<TRow[]>;
711
+ }
712
+ /**
713
+ * Adapt a Cloudflare D1 binding to the SqlAdapter shape. Lives here so D1
714
+ * consumers don't have to write the wrapper themselves; the runtime never
715
+ * imports `@cloudflare/workers-types` directly (peer-style typing).
716
+ */
717
+ declare function d1ToSqlAdapter(db: D1DatabaseLike): SqlAdapter;
718
+ /**
719
+ * Structural type matching the surface of `D1Database` we depend on, so the
720
+ * SDK never imports `@cloudflare/workers-types`. Consumers pass their real
721
+ * `D1Database` from `env.DB` and TS structural compatibility lines it up.
722
+ */
723
+ interface D1DatabaseLike {
724
+ prepare(sql: string): D1StmtLike;
725
+ }
726
+ interface D1StmtLike {
727
+ bind(...params: unknown[]): D1StmtLike;
728
+ run(): Promise<unknown>;
729
+ all<TRow = unknown>(): Promise<{
730
+ results?: TRow[];
731
+ }>;
732
+ }
733
+ /**
734
+ * SQL-backed ConversationJournal. Two tables — runs (one row per runId, holds
735
+ * start/halt timestamps + halt reason) and turns (one row per committed turn,
736
+ * payload is the ConversationTurn JSON). Replays the turns table on
737
+ * `loadRun` and writes append-only per `appendTurn`.
738
+ */
739
+ declare class SqlConversationJournal implements ConversationJournal {
740
+ private readonly db;
741
+ private readonly table;
742
+ /**
743
+ * @param db SQL adapter (D1, postgres, sqlite, libSQL — all work)
744
+ * @param table Table-name prefix; the journal creates `${table}_runs` and
745
+ * `${table}_turns`. Lets multiple journals share a database
746
+ * without colliding (e.g. one per product surface).
747
+ */
748
+ constructor(db: SqlAdapter, table?: string);
749
+ /**
750
+ * Create the journal's tables if absent. Idempotent. Call once at deploy
751
+ * (or at app boot) — running on every request is harmless but adds latency.
752
+ */
753
+ migrate(): Promise<void>;
754
+ loadRun(runId: string): Promise<ConversationJournalEntry | undefined>;
755
+ beginRun(runId: string, startedAt: string): Promise<void>;
756
+ appendTurn(runId: string, turn: ConversationTurn): Promise<void>;
757
+ recordHalt(runId: string, halt: HaltReason, endedAt: string): Promise<void>;
758
+ }
759
+
760
+ /**
761
+ * @stable
762
+ *
763
+ * Conversation orchestrator. Drives N participants in turn through their own
764
+ * `AgentExecutionBackend`s, aggregating per-turn text + usage, enforcing
765
+ * `maxTurns` / `maxCreditsCents` / `haltOn`, and emitting per-event stream
766
+ * markers so callers can plumb the run through SSE without buffering.
767
+ *
768
+ * `runConversation` returns the full result; `runConversationStream` returns
769
+ * an `AsyncIterable<ConversationStreamEvent>` for callers that want to
770
+ * forward events as they arrive. Both share one driving loop.
771
+ *
772
+ * Distributed-systems primitives layered on top of the loop:
773
+ * - **Idempotent turn ids** — `turnId(runId, index, speaker)` stays stable
774
+ * across retries so caching gateways can dedupe.
775
+ * - **Durable journal** — optional `ConversationJournal` persists every
776
+ * committed turn; reusing a runId against the same journal resumes
777
+ * transparently from the last committed turn.
778
+ * - **Per-turn call policy** — deadline, retry-with-backoff, and a
779
+ * per-participant circuit breaker. Retries replay the same logical turn
780
+ * (same `turnId`); the retry loop lives inside the outer generator so
781
+ * deltas yield naturally without cross-coroutine buffering.
782
+ * - **Header propagation** — run/turn/depth headers (+ forwarded user
783
+ * authorization) stamped onto every outbound backend call so downstream
784
+ * gateways can bill the right user and enforce `X-Tangle-Forwarded-Depth`.
785
+ *
786
+ * Credit cap is enforced *between turns*, not mid-stream: a turn that
787
+ * overshoots the cap completes, the cap then halts the conversation before
788
+ * the next turn.
789
+ */
790
+
791
+ declare function runConversation(conversation: Conversation, options: RunConversationOptions): Promise<ConversationResult>;
792
+ declare function runConversationStream(conversation: Conversation, options: RunConversationOptions): AsyncIterable<ConversationStreamEvent>;
793
+
794
+ /**
795
+ * @stable
796
+ *
797
+ * Deterministic turn identifier. Stable across retries of the same logical
798
+ * turn so backends (and any caching gateway in between) can dedupe on it.
799
+ * A retry triggered by a network blip or deadline timeout MUST produce the
800
+ * same `turn_id`; only the underlying attempt count differs.
801
+ *
802
+ * Shape: `${runId}.t${index}.${speakerSlug}` — readable in logs, sortable by
803
+ * turn index, attributable to a speaker. Slugify keeps the speaker portion
804
+ * URL-safe so it can ride in HTTP headers without escaping.
805
+ */
806
+ declare function turnId(runId: string, index: number, speaker: string): string;
807
+ /**
808
+ * Reduce a speaker name to ASCII alphanumerics + dashes. Preserves enough
809
+ * substance to read in a log line; collisions between speakers within a
810
+ * single Conversation are prevented by `defineConversation`'s
811
+ * unique-name check, so the slug only needs to be deterministic, not unique.
812
+ */
813
+ declare function slugifySpeaker(speaker: string): string;
814
+
129
815
  /**
130
816
  * `handleChatTurn` — framework-neutral chat-turn HTTP orchestrator.
131
817
  * Owns the NDJSON `ChatStreamEvent` line protocol, the `session.run.*`
@@ -577,4 +1263,4 @@ declare function readinessServerSentEvent(report: KnowledgeReadinessReport, opti
577
1263
  /** @stable */
578
1264
  declare function runtimeStreamServerSentEvent(event: RuntimeStreamEvent, options?: RuntimeTelemetryOptions & ServerSentEventOptions): string;
579
1265
 
580
- export { AgentBackendContext, AgentBackendInput, AgentExecutionBackend, AgentRuntimeEvent, AgentTaskRunResult, AgentTaskStatus, BackendTransportError, type ChatStreamEvent, type ChatTurnHooks, type ChatTurnIdentity, type ChatTurnProducer, type ChatTurnResult, DEFAULT_ROUTER_BASE_URL, InMemoryRuntimeSessionStore, type ModelInfo, OpenAIChatTool, OpenAIChatToolChoice, type ResolvedChatModel, type RouterEnv, type RunChatTurnInput, type RuntimeEventCollector, RuntimeRunStateError, RuntimeSessionStore, RuntimeStreamEvent, type RuntimeStreamEventCollector, type RuntimeTelemetryOptions, type SanitizedKnowledgeReadinessReport, cleanModelId, createIterableBackend, createOpenAICompatibleBackend, createRuntimeEventCollector, createRuntimeStreamEventCollector, createSandboxPromptBackend, decideKnowledgeReadiness, deriveExecutionId, getModels, handleChatTurn, readinessServerSentEvent, resolveChatModel, resolveRouterBaseUrl, runAgentTask, runAgentTaskStream, runtimeStreamServerSentEvent, sanitizeAgentRuntimeEvent, sanitizeKnowledgeReadinessReport, sanitizeRuntimeStreamEvent, validateChatModelId };
1266
+ export { AgentBackendContext, AgentBackendInput, AgentExecutionBackend, AgentRuntimeEvent, AgentTaskRunResult, AgentTaskStatus, type AuthSource, type BackendCallPolicy, BackendTransportError, type ChatStreamEvent, type ChatTurnHooks, type ChatTurnIdentity, type ChatTurnProducer, type ChatTurnResult, type CircuitBreakerConfig, CircuitBreakerState, CircuitOpenError, type Conversation, type ConversationDriveState, type ConversationJournal, type ConversationJournalEntry, type ConversationParticipant, type ConversationPolicy, type ConversationResult, type ConversationStreamEvent, type ConversationTurn, type D1DatabaseLike, type D1StmtLike, DEFAULT_MAX_DEPTH, DEFAULT_ROUTER_BASE_URL, DeadlineExceededError, FORWARD_HEADERS, FileConversationJournal, type ForwardHeaderName, type HaltContext, type HaltPredicate, type HaltReason, type HaltSignal, InMemoryConversationJournal, InMemoryRuntimeSessionStore, type ModelInfo, OpenAIChatTool, OpenAIChatToolChoice, type PropagatedHeaders, type ResolvedChatModel, type RetryBackoff, type RetryableErrorPredicate, type RouterEnv, type RunChatTurnInput, type RunConversationOptions, type RuntimeEventCollector, RuntimeRunStateError, RuntimeSessionStore, RuntimeStreamEvent, type RuntimeStreamEventCollector, type RuntimeTelemetryOptions, type SanitizedKnowledgeReadinessReport, type SqlAdapter, SqlConversationJournal, type TurnOrder, buildForwardHeaders, cleanModelId, computeBackoff, createConversationBackend, createIterableBackend, createOpenAICompatibleBackend, createRuntimeEventCollector, createRuntimeStreamEventCollector, createSandboxPromptBackend, d1ToSqlAdapter, decideKnowledgeReadiness, defaultIsRetryable, defineConversation, deriveExecutionId, getModels, handleChatTurn, isDepthExceeded, makePerAttemptSignal, readDepth, readinessServerSentEvent, resolveChatModel, resolveRouterBaseUrl, runAgentTask, runAgentTaskStream, runConversation, runConversationStream, runtimeStreamServerSentEvent, sanitizeAgentRuntimeEvent, sanitizeKnowledgeReadinessReport, sanitizeRuntimeStreamEvent, sleep, slugifySpeaker, turnId, validateChatModelId };