@agentskit/adapters 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.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as _agentskit_core from '@agentskit/core';
2
- import { AdapterRequest, StreamChunk, AdapterFactory } from '@agentskit/core';
2
+ import { AdapterRequest, StreamChunk, AdapterFactory, StreamSource, EmbedFn } from '@agentskit/core';
3
3
 
4
4
  interface CreateAdapterConfig {
5
5
  send: (request: AdapterRequest) => Promise<ReadableStream | Response>;
@@ -14,11 +14,75 @@ declare function createAdapter(config: CreateAdapterConfig): AdapterFactory;
14
14
 
15
15
  declare function generic(config: GenericAdapterConfig): AdapterFactory;
16
16
 
17
+ /**
18
+ * Retry knobs for adapter fetches. Tunable per call to createStreamSource.
19
+ *
20
+ * Default behavior:
21
+ * - 3 attempts total (1 initial + 2 retries)
22
+ * - exponential backoff: 500ms, 1000ms, 2000ms ... (capped at maxDelayMs)
23
+ * - full jitter on each delay
24
+ * - retry on HTTP 408, 429, 500, 502, 503, 504
25
+ * - retry on network errors (fetch throws)
26
+ * - DO NOT retry on 4xx other than 408/429 (those are bad requests / auth)
27
+ * - retries only the initial fetch — never mid-stream
28
+ * - respects Retry-After header when present
29
+ */
30
+ interface RetryOptions {
31
+ maxAttempts?: number;
32
+ baseDelayMs?: number;
33
+ maxDelayMs?: number;
34
+ jitter?: boolean;
35
+ retryOn?: (info: {
36
+ error?: unknown;
37
+ response?: Response;
38
+ attempt: number;
39
+ }) => boolean;
40
+ /** Hook for tests + logging. Called after every failed attempt. */
41
+ onRetry?: (info: {
42
+ attempt: number;
43
+ delayMs: number;
44
+ reason: string;
45
+ }) => void;
46
+ /** Sleep override for tests. Defaults to setTimeout. */
47
+ sleep?: (ms: number) => Promise<void>;
48
+ }
49
+ /**
50
+ * Run a fetch with retries on transient failures. Returns the final
51
+ * Response (whether successful or not — caller decides), or throws if
52
+ * the AbortSignal fires or all attempts fail with a thrown error.
53
+ */
54
+ declare function fetchWithRetry(doFetch: (signal: AbortSignal) => Promise<Response>, signal: AbortSignal, retryOpt?: RetryOptions): Promise<Response>;
55
+ /**
56
+ * Chunk-splitter that turns one large string into N streamable text chunks.
57
+ * Useful when a provider returns the full response in one shot and you
58
+ * want to feed it to a UI that expects streaming.
59
+ *
60
+ * Default splits by whitespace boundaries with a target chunk size of ~32
61
+ * characters.
62
+ */
63
+ declare function chunkText(text: string, targetSize?: number): string[];
64
+ /**
65
+ * Build a StreamSource from a non-streaming fetch. The adapter is
66
+ * auto-completing: it fetches once, then yields the text as a sequence
67
+ * of chunks so UIs see the same streaming shape they'd see from a
68
+ * native streaming provider.
69
+ *
70
+ * Use this when you're wiring a provider that only has a non-streaming
71
+ * endpoint but you want consumers (useChat, the runtime) to get
72
+ * identical ergonomics.
73
+ */
74
+ declare function simulateStream(doFetch: (signal: AbortSignal) => Promise<Response>, extractText: (response: Response) => Promise<string>, errorLabel: string, options?: {
75
+ chunkSize?: number;
76
+ delayMs?: number;
77
+ retry?: RetryOptions;
78
+ }): StreamSource;
79
+
17
80
  interface AnthropicConfig {
18
81
  apiKey: string;
19
82
  model: string;
20
83
  baseUrl?: string;
21
84
  maxTokens?: number;
85
+ retry?: RetryOptions;
22
86
  }
23
87
  declare function anthropic(config: AnthropicConfig): AdapterFactory;
24
88
 
@@ -26,6 +90,7 @@ interface OpenAIConfig {
26
90
  apiKey: string;
27
91
  model: string;
28
92
  baseUrl?: string;
93
+ retry?: RetryOptions;
29
94
  }
30
95
  declare function openai(config: OpenAIConfig): AdapterFactory;
31
96
 
@@ -33,6 +98,7 @@ interface GeminiConfig {
33
98
  apiKey: string;
34
99
  model: string;
35
100
  baseUrl?: string;
101
+ retry?: RetryOptions;
36
102
  }
37
103
  declare function gemini(config: GeminiConfig): AdapterFactory;
38
104
 
@@ -46,6 +112,7 @@ declare const grok: (config: OpenAICompatibleConfig) => _agentskit_core.AdapterF
46
112
  interface OllamaConfig {
47
113
  model: string;
48
114
  baseUrl?: string;
115
+ retry?: RetryOptions;
49
116
  }
50
117
  declare function ollama(config: OllamaConfig): AdapterFactory;
51
118
 
@@ -74,7 +141,127 @@ declare function langgraph(config: LangGraphConfig): AdapterFactory;
74
141
  interface VercelAIConfig {
75
142
  api: string;
76
143
  headers?: Record<string, string>;
144
+ retry?: RetryOptions;
77
145
  }
78
146
  declare function vercelAI(config: VercelAIConfig): AdapterFactory;
79
147
 
80
- export { type AnthropicConfig, type CreateAdapterConfig, type DeepSeekConfig, type GeminiConfig, type GenericAdapterConfig, type GrokConfig, type KimiConfig, type LangChainConfig, type LangGraphConfig, type OllamaConfig, type OpenAIConfig, type VercelAIConfig, anthropic, createAdapter, deepseek, gemini, generic, grok, kimi, langchain, langgraph, ollama, openai, vercelAI };
148
+ type MockResponse = StreamChunk[] | ((request: AdapterRequest) => StreamChunk[]);
149
+ interface MockAdapterOptions {
150
+ /**
151
+ * Static chunks, a request-aware function, or a sequence of responses
152
+ * (the i-th call returns the i-th item, looping when exhausted).
153
+ */
154
+ response: MockResponse | MockResponse[];
155
+ /** ms between yielded chunks. Default 0 (synchronous). */
156
+ delayMs?: number;
157
+ /** Track every request the adapter received. Useful for assertions. */
158
+ history?: AdapterRequest[];
159
+ }
160
+ /**
161
+ * A deterministic adapter for tests, demos, and dry-run experiments.
162
+ *
163
+ * Conforms to ADR 0001 — Adapter contract:
164
+ * - createSource is pure (A1) — no work until stream() runs
165
+ * - Always emits a terminal chunk (A3)
166
+ * - abort() is safe (A6)
167
+ * - Does not mutate input messages (A7)
168
+ *
169
+ * Examples:
170
+ *
171
+ * // Static
172
+ * const adapter = mockAdapter({
173
+ * response: [
174
+ * { type: 'text', content: 'Hello!' },
175
+ * { type: 'done' },
176
+ * ],
177
+ * })
178
+ *
179
+ * // Request-aware
180
+ * const adapter = mockAdapter({
181
+ * response: req => {
182
+ * const last = req.messages[req.messages.length - 1]?.content ?? ''
183
+ * return [
184
+ * { type: 'text', content: 'Echo: ' + last },
185
+ * { type: 'done' },
186
+ * ]
187
+ * },
188
+ * })
189
+ *
190
+ * // Sequenced — different output each call
191
+ * const adapter = mockAdapter({
192
+ * response: [
193
+ * [{ type: 'text', content: 'first' }, { type: 'done' }],
194
+ * [{ type: 'text', content: 'second' }, { type: 'done' }],
195
+ * ],
196
+ * })
197
+ */
198
+ declare function mockAdapter(options: MockAdapterOptions): AdapterFactory;
199
+ interface RecordedTurn {
200
+ /** ISO timestamp when this turn was recorded. */
201
+ recordedAt: string;
202
+ /** The request that produced this turn. */
203
+ request: AdapterRequest;
204
+ /** Every chunk yielded by the wrapped adapter. */
205
+ chunks: StreamChunk[];
206
+ }
207
+ type RecordingFixture = RecordedTurn[];
208
+ interface RecordingSink {
209
+ push(turn: RecordedTurn): void | Promise<void>;
210
+ }
211
+ /**
212
+ * Wrap a real adapter so every turn is captured to a sink. Use this in
213
+ * dev to build up a fixture, then replay with replayAdapter() in tests.
214
+ */
215
+ declare function recordingAdapter(inner: AdapterFactory, sink: RecordingSink): AdapterFactory;
216
+ /**
217
+ * In-memory recording sink — useful for tests and ephemeral capture.
218
+ */
219
+ declare function inMemorySink(): RecordingSink & {
220
+ fixture: RecordingFixture;
221
+ };
222
+ /**
223
+ * Replay an adapter from a recorded fixture. Each turn maps 1:1 to a
224
+ * recorded entry by index — call N replays fixture[N % fixture.length].
225
+ */
226
+ declare function replayAdapter(fixture: RecordingFixture): AdapterFactory;
227
+
228
+ interface OpenAIEmbedderConfig {
229
+ apiKey: string;
230
+ model?: string;
231
+ baseUrl?: string;
232
+ }
233
+ declare function openaiEmbedder(config: OpenAIEmbedderConfig): EmbedFn;
234
+
235
+ interface GeminiEmbedderConfig {
236
+ apiKey: string;
237
+ model?: string;
238
+ baseUrl?: string;
239
+ }
240
+ declare function geminiEmbedder(config: GeminiEmbedderConfig): EmbedFn;
241
+
242
+ interface OllamaEmbedderConfig {
243
+ model?: string;
244
+ baseUrl?: string;
245
+ }
246
+ declare function ollamaEmbedder(config: OllamaEmbedderConfig): EmbedFn;
247
+
248
+ interface OpenAICompatibleEmbedderConfig {
249
+ apiKey: string;
250
+ model: string;
251
+ baseUrl?: string;
252
+ }
253
+ declare function createOpenAICompatibleEmbedder(provider: string, defaultBaseUrl: string): (config: OpenAICompatibleEmbedderConfig) => EmbedFn;
254
+
255
+ interface DeepSeekEmbedderConfig extends OpenAICompatibleEmbedderConfig {
256
+ }
257
+ declare const deepseekEmbedder: (config: OpenAICompatibleEmbedderConfig) => _agentskit_core.EmbedFn;
258
+
259
+ interface GrokEmbedderConfig extends OpenAICompatibleEmbedderConfig {
260
+ }
261
+ declare const grokEmbedder: (config: OpenAICompatibleEmbedderConfig) => _agentskit_core.EmbedFn;
262
+
263
+ interface KimiEmbedderConfig extends OpenAICompatibleEmbedderConfig {
264
+ }
265
+ declare const kimiEmbedder: (config: OpenAICompatibleEmbedderConfig) => _agentskit_core.EmbedFn;
266
+
267
+ export { type AnthropicConfig, type CreateAdapterConfig, type DeepSeekConfig, type DeepSeekEmbedderConfig, type GeminiConfig, type GeminiEmbedderConfig, type GenericAdapterConfig, type GrokConfig, type GrokEmbedderConfig, type KimiConfig, type KimiEmbedderConfig, type LangChainConfig, type LangGraphConfig, type MockAdapterOptions, type MockResponse, type OllamaConfig, type OllamaEmbedderConfig, type OpenAICompatibleEmbedderConfig, type OpenAIConfig, type OpenAIEmbedderConfig, type RecordedTurn, type RecordingFixture, type RecordingSink, type RetryOptions, type VercelAIConfig, anthropic, chunkText, createAdapter, createOpenAICompatibleEmbedder, deepseek, deepseekEmbedder, fetchWithRetry, gemini, geminiEmbedder, generic, grok, grokEmbedder, inMemorySink, kimi, kimiEmbedder, langchain, langgraph, mockAdapter, ollama, ollamaEmbedder, openai, openaiEmbedder, recordingAdapter, replayAdapter, simulateStream, vercelAI };