@dogpile/sdk 0.2.2 → 0.3.1
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/CHANGELOG.md +15 -0
- package/dist/browser/index.js +1044 -507
- package/dist/browser/index.js.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/runtime/broadcast.d.ts +1 -0
- package/dist/runtime/broadcast.d.ts.map +1 -1
- package/dist/runtime/broadcast.js +28 -19
- package/dist/runtime/broadcast.js.map +1 -1
- package/dist/runtime/coordinator.d.ts +1 -0
- package/dist/runtime/coordinator.d.ts.map +1 -1
- package/dist/runtime/coordinator.js +46 -21
- package/dist/runtime/coordinator.js.map +1 -1
- package/dist/runtime/engine.d.ts.map +1 -1
- package/dist/runtime/engine.js +5 -0
- package/dist/runtime/engine.js.map +1 -1
- package/dist/runtime/ids.d.ts +19 -0
- package/dist/runtime/ids.d.ts.map +1 -0
- package/dist/runtime/ids.js +36 -0
- package/dist/runtime/ids.js.map +1 -0
- package/dist/runtime/logger.d.ts +61 -0
- package/dist/runtime/logger.d.ts.map +1 -0
- package/dist/runtime/logger.js +114 -0
- package/dist/runtime/logger.js.map +1 -0
- package/dist/runtime/retry.d.ts +99 -0
- package/dist/runtime/retry.d.ts.map +1 -0
- package/dist/runtime/retry.js +181 -0
- package/dist/runtime/retry.js.map +1 -0
- package/dist/runtime/sequential.d.ts +1 -0
- package/dist/runtime/sequential.d.ts.map +1 -1
- package/dist/runtime/sequential.js +25 -16
- package/dist/runtime/sequential.js.map +1 -1
- package/dist/runtime/shared.d.ts +1 -0
- package/dist/runtime/shared.d.ts.map +1 -1
- package/dist/runtime/shared.js +25 -19
- package/dist/runtime/shared.js.map +1 -1
- package/dist/runtime/termination.d.ts +6 -1
- package/dist/runtime/termination.d.ts.map +1 -1
- package/dist/runtime/termination.js +75 -0
- package/dist/runtime/termination.js.map +1 -1
- package/dist/runtime/tools/built-in.d.ts +99 -0
- package/dist/runtime/tools/built-in.d.ts.map +1 -0
- package/dist/runtime/tools/built-in.js +577 -0
- package/dist/runtime/tools/built-in.js.map +1 -0
- package/dist/runtime/tools/vercel-ai.d.ts +67 -0
- package/dist/runtime/tools/vercel-ai.d.ts.map +1 -0
- package/dist/runtime/tools/vercel-ai.js +148 -0
- package/dist/runtime/tools/vercel-ai.js.map +1 -0
- package/dist/runtime/tools.d.ts +5 -268
- package/dist/runtime/tools.d.ts.map +1 -1
- package/dist/runtime/tools.js +7 -770
- package/dist/runtime/tools.js.map +1 -1
- package/dist/runtime/validation.d.ts.map +1 -1
- package/dist/runtime/validation.js +22 -0
- package/dist/runtime/validation.js.map +1 -1
- package/dist/runtime/wrap-up.d.ts +26 -0
- package/dist/runtime/wrap-up.d.ts.map +1 -0
- package/dist/runtime/wrap-up.js +178 -0
- package/dist/runtime/wrap-up.js.map +1 -0
- package/dist/types/benchmark.d.ts +276 -0
- package/dist/types/benchmark.d.ts.map +1 -0
- package/dist/types/benchmark.js +2 -0
- package/dist/types/benchmark.js.map +1 -0
- package/dist/types/events.d.ts +495 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/events.js +2 -0
- package/dist/types/events.js.map +1 -0
- package/dist/types/replay.d.ts +169 -0
- package/dist/types/replay.d.ts.map +1 -0
- package/dist/types/replay.js +2 -0
- package/dist/types/replay.js.map +1 -0
- package/dist/types.d.ts +74 -935
- package/dist/types.d.ts.map +1 -1
- package/package.json +28 -1
- package/src/index.ts +7 -1
- package/src/runtime/broadcast.ts +50 -35
- package/src/runtime/coordinator.ts +84 -43
- package/src/runtime/engine.ts +6 -0
- package/src/runtime/ids.ts +41 -0
- package/src/runtime/logger.ts +152 -0
- package/src/runtime/retry.ts +270 -0
- package/src/runtime/sequential.ts +46 -31
- package/src/runtime/shared.ts +46 -35
- package/src/runtime/termination.ts +100 -0
- package/src/runtime/tools/built-in.ts +875 -0
- package/src/runtime/tools/vercel-ai.ts +269 -0
- package/src/runtime/tools.ts +60 -1255
- package/src/runtime/validation.ts +25 -0
- package/src/runtime/wrap-up.ts +257 -0
- package/src/types/benchmark.ts +300 -0
- package/src/types/events.ts +544 -0
- package/src/types/replay.ts +201 -0
- package/src/types.ts +174 -994
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { DogpileError } from "../types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Repo-internal id and timing helpers used across all four protocols.
|
|
5
|
+
*
|
|
6
|
+
* Centralized here so a change to id format or fallback semantics happens in
|
|
7
|
+
* exactly one place — switching `protocol` must not change the run-id contract.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Generates a fresh run id using `globalThis.crypto.randomUUID`.
|
|
12
|
+
*
|
|
13
|
+
* Throws a `DogpileError` when no UUID source is available rather than falling
|
|
14
|
+
* back to a millisecond-based id (which collides under back-to-back runs in
|
|
15
|
+
* the same tick). Node 22+, Bun latest, and modern browsers all expose
|
|
16
|
+
* `crypto.randomUUID`; environments without it are unsupported by Dogpile.
|
|
17
|
+
*/
|
|
18
|
+
export function createRunId(): string {
|
|
19
|
+
const random = globalThis.crypto?.randomUUID?.();
|
|
20
|
+
if (typeof random === "string" && random.length > 0) {
|
|
21
|
+
return random;
|
|
22
|
+
}
|
|
23
|
+
throw new DogpileError({
|
|
24
|
+
code: "invalid-configuration",
|
|
25
|
+
message:
|
|
26
|
+
"Dogpile requires globalThis.crypto.randomUUID to mint a run id. " +
|
|
27
|
+
"Run on Node 22+, Bun latest, or a modern browser ESM environment."
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function nowMs(): number {
|
|
32
|
+
return globalThis.performance?.now() ?? Date.now();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function elapsedMs(startedAtMs: number): number {
|
|
36
|
+
return Math.max(0, nowMs() - startedAtMs);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function providerCallIdFor(runId: string, oneBasedIndex: number): string {
|
|
40
|
+
return `${runId}:provider-call:${oneBasedIndex}`;
|
|
41
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import type { JsonValue, StreamEvent, StreamEventSubscriber } from "../types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Severity levels recognized by `consoleLogger` and used as the floor when
|
|
5
|
+
* deciding which events surface through `loggerFromEvents`.
|
|
6
|
+
*/
|
|
7
|
+
export type LogLevel = "debug" | "info" | "warn" | "error";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Minimal structured-logging seam that callers can implement against pino,
|
|
11
|
+
* winston, console, or anything else. Logger calls must be synchronous,
|
|
12
|
+
* non-throwing, and have no return value — Dogpile catches throws and routes
|
|
13
|
+
* them to the same logger's `error` channel rather than failing the run.
|
|
14
|
+
*/
|
|
15
|
+
export interface Logger {
|
|
16
|
+
debug(message: string, fields?: Readonly<Record<string, JsonValue>>): void;
|
|
17
|
+
info(message: string, fields?: Readonly<Record<string, JsonValue>>): void;
|
|
18
|
+
warn(message: string, fields?: Readonly<Record<string, JsonValue>>): void;
|
|
19
|
+
error(message: string, fields?: Readonly<Record<string, JsonValue>>): void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Logger that drops every call. The default when no logger is supplied. */
|
|
23
|
+
export const noopLogger: Logger = {
|
|
24
|
+
debug() {},
|
|
25
|
+
info() {},
|
|
26
|
+
warn() {},
|
|
27
|
+
error() {}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Build a console-backed logger respecting a minimum level.
|
|
32
|
+
*
|
|
33
|
+
* The output format is JSON-on-one-line so it can be piped straight into log
|
|
34
|
+
* collectors. Use `loggerFromEvents` to bridge it to a Dogpile stream handle.
|
|
35
|
+
*/
|
|
36
|
+
export function consoleLogger(options: { readonly level?: LogLevel } = {}): Logger {
|
|
37
|
+
const minLevel = options.level ?? "info";
|
|
38
|
+
const allowed = (level: LogLevel): boolean => LEVEL_ORDER[level] >= LEVEL_ORDER[minLevel];
|
|
39
|
+
const emit = (level: LogLevel, message: string, fields?: Readonly<Record<string, JsonValue>>): void => {
|
|
40
|
+
if (!allowed(level)) return;
|
|
41
|
+
const payload: Record<string, unknown> = { level, message };
|
|
42
|
+
if (fields !== undefined) payload.fields = fields;
|
|
43
|
+
const sink = level === "error" ? console.error : level === "warn" ? console.warn : console.log;
|
|
44
|
+
sink(JSON.stringify(payload));
|
|
45
|
+
};
|
|
46
|
+
return {
|
|
47
|
+
debug: (message, fields) => emit("debug", message, fields),
|
|
48
|
+
info: (message, fields) => emit("info", message, fields),
|
|
49
|
+
warn: (message, fields) => emit("warn", message, fields),
|
|
50
|
+
error: (message, fields) => emit("error", message, fields)
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const LEVEL_ORDER: Record<LogLevel, number> = {
|
|
55
|
+
debug: 0,
|
|
56
|
+
info: 1,
|
|
57
|
+
warn: 2,
|
|
58
|
+
error: 3
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Options for `loggerFromEvents`.
|
|
63
|
+
*/
|
|
64
|
+
export interface LoggerFromEventsOptions {
|
|
65
|
+
/**
|
|
66
|
+
* Restrict logging to the listed event types. By default every
|
|
67
|
+
* lifecycle event is forwarded.
|
|
68
|
+
*/
|
|
69
|
+
readonly include?: ReadonlyArray<StreamEvent["type"]>;
|
|
70
|
+
/**
|
|
71
|
+
* Override the level chosen for a given event type. Useful for elevating
|
|
72
|
+
* tool-result errors to `warn` or downgrading `model-output-chunk` to
|
|
73
|
+
* `debug`.
|
|
74
|
+
*/
|
|
75
|
+
readonly levelFor?: (event: StreamEvent) => LogLevel | undefined;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Bridge a `Logger` to a Dogpile stream handle by returning a
|
|
80
|
+
* `StreamEventSubscriber`. Pass it to `handle.subscribe(...)`.
|
|
81
|
+
*
|
|
82
|
+
* Logger throws are caught and routed to `logger.error` so a misbehaving
|
|
83
|
+
* logger can never crash an in-flight run.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* const handle = Dogpile.stream({ intent, model });
|
|
88
|
+
* handle.subscribe(loggerFromEvents(consoleLogger({ level: "info" })));
|
|
89
|
+
* const result = await handle.result;
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export function loggerFromEvents(
|
|
93
|
+
logger: Logger,
|
|
94
|
+
options: LoggerFromEventsOptions = {}
|
|
95
|
+
): StreamEventSubscriber {
|
|
96
|
+
const includeSet = options.include ? new Set(options.include) : undefined;
|
|
97
|
+
return (event: StreamEvent): void => {
|
|
98
|
+
const eventType = event.type;
|
|
99
|
+
if (includeSet && !includeSet.has(eventType)) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const level = options.levelFor?.(event) ?? defaultLevel(event);
|
|
103
|
+
const message = describeEvent(event);
|
|
104
|
+
const fields = summarizeEvent(event);
|
|
105
|
+
try {
|
|
106
|
+
logger[level](message, fields);
|
|
107
|
+
} catch (cause) {
|
|
108
|
+
try {
|
|
109
|
+
logger.error("dogpile logger threw while handling event", {
|
|
110
|
+
eventType,
|
|
111
|
+
error: cause instanceof Error ? cause.message : String(cause)
|
|
112
|
+
});
|
|
113
|
+
} catch {
|
|
114
|
+
// Swallow — a logger that throws from error() cannot be helped.
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function defaultLevel(event: StreamEvent): LogLevel {
|
|
121
|
+
switch (event.type) {
|
|
122
|
+
case "model-output-chunk":
|
|
123
|
+
return "debug";
|
|
124
|
+
case "budget-stop":
|
|
125
|
+
return "warn";
|
|
126
|
+
case "error":
|
|
127
|
+
return "error";
|
|
128
|
+
case "tool-result": {
|
|
129
|
+
const result = (event as { readonly result?: { readonly type?: string } }).result;
|
|
130
|
+
return result?.type === "error" ? "warn" : "info";
|
|
131
|
+
}
|
|
132
|
+
default:
|
|
133
|
+
return "info";
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function describeEvent(event: StreamEvent): string {
|
|
138
|
+
return `dogpile:${event.type}`;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function summarizeEvent(event: StreamEvent): Readonly<Record<string, JsonValue>> {
|
|
142
|
+
const fields: Record<string, JsonValue> = { eventType: event.type };
|
|
143
|
+
const at = (event as { readonly at?: unknown }).at;
|
|
144
|
+
if (typeof at === "string") fields.at = at;
|
|
145
|
+
const runId = (event as { readonly runId?: unknown }).runId;
|
|
146
|
+
if (typeof runId === "string") fields.runId = runId;
|
|
147
|
+
const agentId = (event as { readonly agentId?: unknown }).agentId;
|
|
148
|
+
if (typeof agentId === "string") fields.agentId = agentId;
|
|
149
|
+
const role = (event as { readonly role?: unknown }).role;
|
|
150
|
+
if (typeof role === "string") fields.role = role;
|
|
151
|
+
return fields;
|
|
152
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DogpileError,
|
|
3
|
+
type ConfiguredModelProvider,
|
|
4
|
+
type DogpileErrorCode,
|
|
5
|
+
type ModelOutputChunk,
|
|
6
|
+
type ModelRequest,
|
|
7
|
+
type ModelResponse
|
|
8
|
+
} from "../types.js";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Default DogpileError codes that `withRetry` retries when no `retryOn`
|
|
12
|
+
* predicate is supplied. These map to the transient provider failures listed
|
|
13
|
+
* in `docs/developer-usage.md`.
|
|
14
|
+
*/
|
|
15
|
+
export const DEFAULT_RETRYABLE_DOGPILE_CODES: readonly DogpileErrorCode[] = [
|
|
16
|
+
"provider-rate-limited",
|
|
17
|
+
"provider-timeout",
|
|
18
|
+
"provider-unavailable"
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
/** Reason passed to `onRetry` and used to drive jitter selection. */
|
|
22
|
+
export type RetryJitterMode = "full" | "none";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Information about a single retry attempt that has just failed and is about
|
|
26
|
+
* to sleep before the next attempt.
|
|
27
|
+
*/
|
|
28
|
+
export interface RetryAttemptInfo {
|
|
29
|
+
/** 1-based index of the attempt that just failed. */
|
|
30
|
+
readonly attempt: number;
|
|
31
|
+
/** Maximum number of attempts the policy will make before giving up. */
|
|
32
|
+
readonly maxAttempts: number;
|
|
33
|
+
/** Sleep duration before the next attempt, in milliseconds. */
|
|
34
|
+
readonly delayMs: number;
|
|
35
|
+
/** The error thrown by the failing attempt. */
|
|
36
|
+
readonly error: unknown;
|
|
37
|
+
/** Provider id of the wrapped provider. */
|
|
38
|
+
readonly providerId: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Caller-supplied retry policy for `withRetry`.
|
|
43
|
+
*
|
|
44
|
+
* The defaults match the conservative, neutrality-preserving recipe in
|
|
45
|
+
* `docs/developer-usage.md`. A caller that wants per-error custom logic
|
|
46
|
+
* (e.g. honor a custom `Retry-After` header from a non-Dogpile error shape)
|
|
47
|
+
* should pass `retryOn` and `delayForError`.
|
|
48
|
+
*/
|
|
49
|
+
export interface RetryPolicy {
|
|
50
|
+
/** Maximum total attempts including the first call. Default: 3. */
|
|
51
|
+
readonly maxAttempts?: number;
|
|
52
|
+
/** Initial backoff delay in milliseconds. Default: 250. */
|
|
53
|
+
readonly baseDelayMs?: number;
|
|
54
|
+
/** Cap on the per-attempt backoff delay. Default: 4000. */
|
|
55
|
+
readonly maxDelayMs?: number;
|
|
56
|
+
/** Jitter strategy. `"full"` uses uniform jitter, `"none"` is deterministic. Default: "full". */
|
|
57
|
+
readonly jitter?: RetryJitterMode;
|
|
58
|
+
/**
|
|
59
|
+
* Predicate deciding whether an error is retryable. Receives the raw error
|
|
60
|
+
* thrown by the wrapped provider; returns `true` to retry, `false` to
|
|
61
|
+
* propagate immediately. Default: matches `DEFAULT_RETRYABLE_DOGPILE_CODES`
|
|
62
|
+
* for `DogpileError`, and treats `AbortError` / `DOMException(AbortError)` /
|
|
63
|
+
* `DogpileError({ code: "aborted" })` as non-retryable.
|
|
64
|
+
*/
|
|
65
|
+
readonly retryOn?: (error: unknown) => boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Optional delay override for a specific error. Return a non-negative number
|
|
68
|
+
* (ms) to override the computed backoff for the next attempt — used to
|
|
69
|
+
* honor server-supplied `Retry-After` semantics. Returning `undefined`
|
|
70
|
+
* keeps the computed backoff.
|
|
71
|
+
*/
|
|
72
|
+
readonly delayForError?: (error: unknown) => number | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* Side-effect callback invoked after each failing attempt that will be
|
|
75
|
+
* retried. Useful for surfacing retries to a logger or metrics system
|
|
76
|
+
* without wrapping the whole event stream.
|
|
77
|
+
*/
|
|
78
|
+
readonly onRetry?: (info: RetryAttemptInfo) => void;
|
|
79
|
+
/**
|
|
80
|
+
* Random source for jitter, primarily for deterministic tests. Must return
|
|
81
|
+
* a value in `[0, 1)`. Default: `Math.random`.
|
|
82
|
+
*/
|
|
83
|
+
readonly random?: () => number;
|
|
84
|
+
/**
|
|
85
|
+
* Sleep implementation, primarily for deterministic tests. Default: a
|
|
86
|
+
* `setTimeout`-backed promise that respects `AbortSignal`.
|
|
87
|
+
*/
|
|
88
|
+
readonly sleep?: (ms: number, signal?: AbortSignal) => Promise<void>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const DEFAULTS = {
|
|
92
|
+
maxAttempts: 3,
|
|
93
|
+
baseDelayMs: 250,
|
|
94
|
+
maxDelayMs: 4_000,
|
|
95
|
+
jitter: "full" as RetryJitterMode
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Wrap a `ConfiguredModelProvider` with a retry policy. The wrapper:
|
|
100
|
+
*
|
|
101
|
+
* - Preserves the provider `id` so traces remain stable.
|
|
102
|
+
* - Retries `generate()` calls when the policy says the error is retryable.
|
|
103
|
+
* - Propagates `AbortSignal` cancellation immediately — never retries after
|
|
104
|
+
* the caller cancels.
|
|
105
|
+
* - Honors a `Retry-After`-style hint exposed via `error.detail.retryAfterMs`
|
|
106
|
+
* when present and the policy did not provide its own `delayForError`.
|
|
107
|
+
* - Forwards `stream()` calls through unchanged — streaming retries cannot be
|
|
108
|
+
* safely automated because partial output may have already been observed.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* const robustProvider = withRetry(rawProvider, {
|
|
113
|
+
* maxAttempts: 4,
|
|
114
|
+
* baseDelayMs: 500,
|
|
115
|
+
* onRetry: ({ attempt, delayMs, error }) => {
|
|
116
|
+
* logger.warn("provider retry", { attempt, delayMs, error });
|
|
117
|
+
* }
|
|
118
|
+
* });
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
export function withRetry(
|
|
122
|
+
provider: ConfiguredModelProvider,
|
|
123
|
+
policy: RetryPolicy = {}
|
|
124
|
+
): ConfiguredModelProvider {
|
|
125
|
+
const settings = {
|
|
126
|
+
maxAttempts: policy.maxAttempts ?? DEFAULTS.maxAttempts,
|
|
127
|
+
baseDelayMs: policy.baseDelayMs ?? DEFAULTS.baseDelayMs,
|
|
128
|
+
maxDelayMs: policy.maxDelayMs ?? DEFAULTS.maxDelayMs,
|
|
129
|
+
jitter: policy.jitter ?? DEFAULTS.jitter,
|
|
130
|
+
retryOn: policy.retryOn ?? defaultRetryOn,
|
|
131
|
+
random: policy.random ?? Math.random,
|
|
132
|
+
sleep: policy.sleep ?? defaultSleep
|
|
133
|
+
};
|
|
134
|
+
if (settings.maxAttempts < 1) {
|
|
135
|
+
throw new DogpileError({
|
|
136
|
+
code: "invalid-configuration",
|
|
137
|
+
message: "withRetry: maxAttempts must be >= 1.",
|
|
138
|
+
detail: { maxAttempts: settings.maxAttempts }
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
if (settings.baseDelayMs < 0 || settings.maxDelayMs < 0) {
|
|
142
|
+
throw new DogpileError({
|
|
143
|
+
code: "invalid-configuration",
|
|
144
|
+
message: "withRetry: delay fields must be non-negative.",
|
|
145
|
+
detail: { baseDelayMs: settings.baseDelayMs, maxDelayMs: settings.maxDelayMs }
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const wrapped: ConfiguredModelProvider = {
|
|
150
|
+
id: provider.id,
|
|
151
|
+
async generate(request: ModelRequest): Promise<ModelResponse> {
|
|
152
|
+
let lastError: unknown;
|
|
153
|
+
for (let attempt = 1; attempt <= settings.maxAttempts; attempt++) {
|
|
154
|
+
if (request.signal?.aborted) {
|
|
155
|
+
throw abortReason(request.signal);
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
return await provider.generate(request);
|
|
159
|
+
} catch (error) {
|
|
160
|
+
lastError = error;
|
|
161
|
+
if (isAbortError(error) || request.signal?.aborted) {
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
const isLastAttempt = attempt >= settings.maxAttempts;
|
|
165
|
+
if (isLastAttempt || !settings.retryOn(error)) {
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
const delayMs = chooseDelay({ attempt, error, settings, policy });
|
|
169
|
+
policy.onRetry?.({
|
|
170
|
+
attempt,
|
|
171
|
+
maxAttempts: settings.maxAttempts,
|
|
172
|
+
delayMs,
|
|
173
|
+
error,
|
|
174
|
+
providerId: provider.id
|
|
175
|
+
});
|
|
176
|
+
await settings.sleep(delayMs, request.signal);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
// Unreachable in practice — the loop either returns or throws — but TS
|
|
180
|
+
// needs an explicit fallthrough.
|
|
181
|
+
throw lastError ?? new DogpileError({
|
|
182
|
+
code: "unknown",
|
|
183
|
+
message: "withRetry: exhausted attempts without throwing or returning."
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
if (typeof provider.stream === "function") {
|
|
189
|
+
const upstreamStream = provider.stream.bind(provider);
|
|
190
|
+
wrapped.stream = (request: ModelRequest): AsyncIterable<ModelOutputChunk> =>
|
|
191
|
+
upstreamStream(request);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return wrapped;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function chooseDelay(args: {
|
|
198
|
+
attempt: number;
|
|
199
|
+
error: unknown;
|
|
200
|
+
settings: { baseDelayMs: number; maxDelayMs: number; jitter: RetryJitterMode; random: () => number };
|
|
201
|
+
policy: RetryPolicy;
|
|
202
|
+
}): number {
|
|
203
|
+
const override = args.policy.delayForError?.(args.error) ?? retryAfterFromError(args.error);
|
|
204
|
+
if (override !== undefined && Number.isFinite(override) && override >= 0) {
|
|
205
|
+
return Math.min(args.settings.maxDelayMs, override);
|
|
206
|
+
}
|
|
207
|
+
const exponential = args.settings.baseDelayMs * 2 ** (args.attempt - 1);
|
|
208
|
+
const capped = Math.min(args.settings.maxDelayMs, exponential);
|
|
209
|
+
if (args.settings.jitter === "none") {
|
|
210
|
+
return capped;
|
|
211
|
+
}
|
|
212
|
+
return Math.floor(capped * args.settings.random());
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function defaultRetryOn(error: unknown): boolean {
|
|
216
|
+
if (isAbortError(error)) return false;
|
|
217
|
+
if (DogpileError.isInstance(error)) {
|
|
218
|
+
if (error.code === "aborted" || error.code === "invalid-configuration") {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
return DEFAULT_RETRYABLE_DOGPILE_CODES.includes(error.code);
|
|
222
|
+
}
|
|
223
|
+
// Treat generic network/transient errors as retryable. Most fetch errors
|
|
224
|
+
// surface as `TypeError` with messages like "fetch failed" / "network".
|
|
225
|
+
if (error instanceof TypeError) return true;
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function isAbortError(error: unknown): boolean {
|
|
230
|
+
if (DogpileError.isInstance(error) && error.code === "aborted") return true;
|
|
231
|
+
if (typeof error === "object" && error !== null) {
|
|
232
|
+
const name = (error as { name?: unknown }).name;
|
|
233
|
+
if (name === "AbortError") return true;
|
|
234
|
+
}
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function abortReason(signal: AbortSignal): unknown {
|
|
239
|
+
return signal.reason ?? new DogpileError({ code: "aborted", message: "Request aborted." });
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function retryAfterFromError(error: unknown): number | undefined {
|
|
243
|
+
if (!DogpileError.isInstance(error)) return undefined;
|
|
244
|
+
const detail = error.detail;
|
|
245
|
+
if (!detail || typeof detail !== "object") return undefined;
|
|
246
|
+
const candidate = (detail as { retryAfterMs?: unknown }).retryAfterMs;
|
|
247
|
+
if (typeof candidate === "number" && Number.isFinite(candidate) && candidate >= 0) {
|
|
248
|
+
return candidate;
|
|
249
|
+
}
|
|
250
|
+
return undefined;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function defaultSleep(ms: number, signal?: AbortSignal): Promise<void> {
|
|
254
|
+
if (ms <= 0) return Promise.resolve();
|
|
255
|
+
return new Promise((resolve, reject) => {
|
|
256
|
+
if (signal?.aborted) {
|
|
257
|
+
reject(abortReason(signal));
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const timer = setTimeout(() => {
|
|
261
|
+
signal?.removeEventListener("abort", onAbort);
|
|
262
|
+
resolve();
|
|
263
|
+
}, ms);
|
|
264
|
+
const onAbort = (): void => {
|
|
265
|
+
clearTimeout(timer);
|
|
266
|
+
reject(abortReason(signal!));
|
|
267
|
+
};
|
|
268
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
269
|
+
});
|
|
270
|
+
}
|
|
@@ -18,6 +18,7 @@ import type {
|
|
|
18
18
|
Tier,
|
|
19
19
|
TranscriptEntry
|
|
20
20
|
} from "../types.js";
|
|
21
|
+
import { createRunId, elapsedMs, nowMs } from "./ids.js";
|
|
21
22
|
import {
|
|
22
23
|
addCost,
|
|
23
24
|
createReplayTraceBudget,
|
|
@@ -37,8 +38,9 @@ import {
|
|
|
37
38
|
import { throwIfAborted } from "./cancellation.js";
|
|
38
39
|
import { isParticipatingDecision, parseAgentDecision } from "./decisions.js";
|
|
39
40
|
import { generateModelTurn } from "./model.js";
|
|
40
|
-
import { evaluateTerminationStop } from "./termination.js";
|
|
41
|
+
import { evaluateTerminationStop, warnOnProtocolTerminationMisconfiguration } from "./termination.js";
|
|
41
42
|
import { createRuntimeToolExecutor, executeModelResponseToolRequests, runtimeToolAvailability } from "./tools.js";
|
|
43
|
+
import { createWrapUpHintController } from "./wrap-up.js";
|
|
42
44
|
|
|
43
45
|
interface SequentialRunOptions {
|
|
44
46
|
readonly intent: string;
|
|
@@ -52,6 +54,7 @@ interface SequentialRunOptions {
|
|
|
52
54
|
readonly seed?: string | number;
|
|
53
55
|
readonly signal?: AbortSignal;
|
|
54
56
|
readonly terminate?: TerminationCondition;
|
|
57
|
+
readonly wrapUpHint?: DogpileOptions["wrapUpHint"];
|
|
55
58
|
readonly emit?: (event: RunEvent) => void;
|
|
56
59
|
}
|
|
57
60
|
|
|
@@ -67,6 +70,15 @@ export async function runSequential(options: SequentialRunOptions): Promise<RunR
|
|
|
67
70
|
const startedAtMs = nowMs();
|
|
68
71
|
let stopped = false;
|
|
69
72
|
let termination: TerminationStopRecord | undefined;
|
|
73
|
+
const wrapUpHint = createWrapUpHintController({
|
|
74
|
+
protocol: options.protocol,
|
|
75
|
+
tier: options.tier,
|
|
76
|
+
...(options.budget ? { budget: options.budget } : {}),
|
|
77
|
+
...(options.terminate ? { terminate: options.terminate } : {}),
|
|
78
|
+
...(options.wrapUpHint ? { wrapUpHint: options.wrapUpHint } : {})
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
warnOnProtocolTerminationMisconfiguration(options.protocol, options.terminate);
|
|
70
82
|
|
|
71
83
|
const emit = (event: RunEvent): void => {
|
|
72
84
|
events.push(event);
|
|
@@ -129,16 +141,27 @@ export async function runSequential(options: SequentialRunOptions): Promise<RunR
|
|
|
129
141
|
turn,
|
|
130
142
|
...toolAvailability
|
|
131
143
|
},
|
|
132
|
-
messages:
|
|
144
|
+
messages: wrapUpHint.inject(
|
|
145
|
+
[
|
|
146
|
+
{
|
|
147
|
+
role: "system",
|
|
148
|
+
content: buildSystemPrompt(agent)
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
role: "user",
|
|
152
|
+
content: input
|
|
153
|
+
}
|
|
154
|
+
],
|
|
133
155
|
{
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
156
|
+
runId,
|
|
157
|
+
protocol: "sequential",
|
|
158
|
+
cost: totalCost,
|
|
159
|
+
events,
|
|
160
|
+
transcript,
|
|
161
|
+
iteration: transcript.length,
|
|
162
|
+
elapsedMs: elapsedMs(startedAtMs)
|
|
140
163
|
}
|
|
141
|
-
|
|
164
|
+
)
|
|
142
165
|
};
|
|
143
166
|
const response = await generateModelTurn({
|
|
144
167
|
model: options.model,
|
|
@@ -277,16 +300,20 @@ export async function runSequential(options: SequentialRunOptions): Promise<RunR
|
|
|
277
300
|
return stopped;
|
|
278
301
|
}
|
|
279
302
|
|
|
280
|
-
const stopRecord = evaluateTerminationStop(
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
303
|
+
const stopRecord = evaluateTerminationStop(
|
|
304
|
+
options.terminate,
|
|
305
|
+
wrapUpHint.context({
|
|
306
|
+
runId,
|
|
307
|
+
protocol: "sequential",
|
|
308
|
+
protocolConfig: options.protocol,
|
|
309
|
+
protocolIteration: transcript.length,
|
|
310
|
+
cost: totalCost,
|
|
311
|
+
events,
|
|
312
|
+
transcript,
|
|
313
|
+
iteration: transcript.length,
|
|
314
|
+
elapsedMs: elapsedMs(startedAtMs)
|
|
315
|
+
})
|
|
316
|
+
);
|
|
290
317
|
|
|
291
318
|
if (!stopRecord) {
|
|
292
319
|
return false;
|
|
@@ -343,15 +370,3 @@ function responseCost(response: ModelResponse): CostSummary {
|
|
|
343
370
|
};
|
|
344
371
|
}
|
|
345
372
|
|
|
346
|
-
function createRunId(): string {
|
|
347
|
-
const random = globalThis.crypto?.randomUUID?.();
|
|
348
|
-
return random ?? `run-${Date.now().toString(36)}`;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
function nowMs(): number {
|
|
352
|
-
return globalThis.performance?.now() ?? Date.now();
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
function elapsedMs(startedAtMs: number): number {
|
|
356
|
-
return Math.max(0, nowMs() - startedAtMs);
|
|
357
|
-
}
|