adk-llm-bridge 0.5.0 → 0.5.2

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 (103) hide show
  1. package/dist/index.d.ts +1514 -74
  2. package/dist/index.js +983 -6
  3. package/dist/index.js.map +3 -3
  4. package/package.json +9 -7
  5. package/dist/config.d.ts +0 -164
  6. package/dist/config.d.ts.map +0 -1
  7. package/dist/constants.d.ts +0 -130
  8. package/dist/constants.d.ts.map +0 -1
  9. package/dist/converters/index.d.ts +0 -32
  10. package/dist/converters/index.d.ts.map +0 -1
  11. package/dist/converters/request.d.ts +0 -87
  12. package/dist/converters/request.d.ts.map +0 -1
  13. package/dist/converters/response.d.ts +0 -103
  14. package/dist/converters/response.d.ts.map +0 -1
  15. package/dist/core/base-provider-llm.d.ts +0 -110
  16. package/dist/core/base-provider-llm.d.ts.map +0 -1
  17. package/dist/core/config-resolver.d.ts +0 -39
  18. package/dist/core/config-resolver.d.ts.map +0 -1
  19. package/dist/core/create-provider.d.ts +0 -40
  20. package/dist/core/create-provider.d.ts.map +0 -1
  21. package/dist/core/create-register.d.ts +0 -38
  22. package/dist/core/create-register.d.ts.map +0 -1
  23. package/dist/core/index.d.ts +0 -46
  24. package/dist/core/index.d.ts.map +0 -1
  25. package/dist/core/openai-compatible-llm.d.ts +0 -81
  26. package/dist/core/openai-compatible-llm.d.ts.map +0 -1
  27. package/dist/core/provider-definition.d.ts +0 -66
  28. package/dist/core/provider-definition.d.ts.map +0 -1
  29. package/dist/index.d.ts.map +0 -1
  30. package/dist/providers/ai-gateway/ai-gateway-llm.d.ts +0 -71
  31. package/dist/providers/ai-gateway/ai-gateway-llm.d.ts.map +0 -1
  32. package/dist/providers/ai-gateway/definition.d.ts +0 -8
  33. package/dist/providers/ai-gateway/definition.d.ts.map +0 -1
  34. package/dist/providers/ai-gateway/factory.d.ts +0 -56
  35. package/dist/providers/ai-gateway/factory.d.ts.map +0 -1
  36. package/dist/providers/ai-gateway/index.d.ts +0 -11
  37. package/dist/providers/ai-gateway/index.d.ts.map +0 -1
  38. package/dist/providers/ai-gateway/register.d.ts +0 -63
  39. package/dist/providers/ai-gateway/register.d.ts.map +0 -1
  40. package/dist/providers/anthropic/anthropic-llm.d.ts +0 -106
  41. package/dist/providers/anthropic/anthropic-llm.d.ts.map +0 -1
  42. package/dist/providers/anthropic/constants.d.ts +0 -36
  43. package/dist/providers/anthropic/constants.d.ts.map +0 -1
  44. package/dist/providers/anthropic/converters/request.d.ts +0 -51
  45. package/dist/providers/anthropic/converters/request.d.ts.map +0 -1
  46. package/dist/providers/anthropic/converters/response.d.ts +0 -63
  47. package/dist/providers/anthropic/converters/response.d.ts.map +0 -1
  48. package/dist/providers/anthropic/factory.d.ts +0 -60
  49. package/dist/providers/anthropic/factory.d.ts.map +0 -1
  50. package/dist/providers/anthropic/index.d.ts +0 -24
  51. package/dist/providers/anthropic/index.d.ts.map +0 -1
  52. package/dist/providers/anthropic/register.d.ts +0 -65
  53. package/dist/providers/anthropic/register.d.ts.map +0 -1
  54. package/dist/providers/custom/custom-llm.d.ts +0 -121
  55. package/dist/providers/custom/custom-llm.d.ts.map +0 -1
  56. package/dist/providers/custom/factory.d.ts +0 -145
  57. package/dist/providers/custom/factory.d.ts.map +0 -1
  58. package/dist/providers/custom/index.d.ts +0 -35
  59. package/dist/providers/custom/index.d.ts.map +0 -1
  60. package/dist/providers/index.d.ts +0 -43
  61. package/dist/providers/index.d.ts.map +0 -1
  62. package/dist/providers/openai/constants.d.ts +0 -34
  63. package/dist/providers/openai/constants.d.ts.map +0 -1
  64. package/dist/providers/openai/definition.d.ts +0 -8
  65. package/dist/providers/openai/definition.d.ts.map +0 -1
  66. package/dist/providers/openai/factory.d.ts +0 -61
  67. package/dist/providers/openai/factory.d.ts.map +0 -1
  68. package/dist/providers/openai/index.d.ts +0 -11
  69. package/dist/providers/openai/index.d.ts.map +0 -1
  70. package/dist/providers/openai/openai-llm.d.ts +0 -69
  71. package/dist/providers/openai/openai-llm.d.ts.map +0 -1
  72. package/dist/providers/openai/register.d.ts +0 -65
  73. package/dist/providers/openai/register.d.ts.map +0 -1
  74. package/dist/providers/openrouter/definition.d.ts +0 -8
  75. package/dist/providers/openrouter/definition.d.ts.map +0 -1
  76. package/dist/providers/openrouter/factory.d.ts +0 -61
  77. package/dist/providers/openrouter/factory.d.ts.map +0 -1
  78. package/dist/providers/openrouter/index.d.ts +0 -11
  79. package/dist/providers/openrouter/index.d.ts.map +0 -1
  80. package/dist/providers/openrouter/openrouter-llm.d.ts +0 -97
  81. package/dist/providers/openrouter/openrouter-llm.d.ts.map +0 -1
  82. package/dist/providers/openrouter/register.d.ts +0 -62
  83. package/dist/providers/openrouter/register.d.ts.map +0 -1
  84. package/dist/providers/xai/constants.d.ts +0 -30
  85. package/dist/providers/xai/constants.d.ts.map +0 -1
  86. package/dist/providers/xai/definition.d.ts +0 -8
  87. package/dist/providers/xai/definition.d.ts.map +0 -1
  88. package/dist/providers/xai/factory.d.ts +0 -54
  89. package/dist/providers/xai/factory.d.ts.map +0 -1
  90. package/dist/providers/xai/index.d.ts +0 -11
  91. package/dist/providers/xai/index.d.ts.map +0 -1
  92. package/dist/providers/xai/register.d.ts +0 -64
  93. package/dist/providers/xai/register.d.ts.map +0 -1
  94. package/dist/providers/xai/xai-llm.d.ts +0 -66
  95. package/dist/providers/xai/xai-llm.d.ts.map +0 -1
  96. package/dist/types.d.ts +0 -578
  97. package/dist/types.d.ts.map +0 -1
  98. package/dist/utils/index.d.ts +0 -8
  99. package/dist/utils/index.d.ts.map +0 -1
  100. package/dist/utils/json.d.ts +0 -26
  101. package/dist/utils/json.d.ts.map +0 -1
  102. package/dist/utils/validate.d.ts +0 -51
  103. package/dist/utils/validate.d.ts.map +0 -1
package/dist/index.d.ts CHANGED
@@ -1,75 +1,1515 @@
1
+ import { BaseLlm as BaseLlm2 } from "@google/adk";
2
+ import { BaseLlmConnection, LlmRequest, LlmResponse as LlmResponse2 } from "@google/adk";
3
+ import { BaseLlm } from "@google/adk";
4
+ import { LlmResponse } from "@google/adk";
1
5
  /**
2
- * @license
3
- * Copyright 2025 PAI
4
- * SPDX-License-Identifier: MIT
5
- */
6
- /**
7
- * adk-llm-bridge - Connect Google ADK to 100+ LLM models.
8
- *
9
- * This library bridges Google ADK (Agent Development Kit) to multiple LLM providers
10
- * through OpenAI-compatible APIs, enabling access to models from Anthropic, OpenAI,
11
- * Google, Meta, and more while preserving ADK features like multi-agent orchestration,
12
- * tool calling, and streaming.
13
- *
14
- * ## Supported Providers
15
- *
16
- * - **AI Gateway** (Vercel): Unified gateway for 100+ models
17
- * - **OpenRouter**: Multi-provider routing with fallbacks and price optimization
18
- *
19
- * ## Quick Start
20
- *
21
- * ### Option 1: Factory Functions (Recommended)
22
- *
23
- * ```typescript
24
- * import { AIGateway, OpenRouter } from "adk-llm-bridge";
25
- * import { LlmAgent } from "@google/adk";
26
- *
27
- * const agent = new LlmAgent({
28
- * name: "assistant",
29
- * llm: AIGateway("anthropic/claude-sonnet-4")
30
- * });
31
- * ```
32
- *
33
- * ### Option 2: Registry (for adk-devtools)
34
- *
35
- * ```typescript
36
- * import { registerAIGateway } from "adk-llm-bridge";
37
- * import { LlmAgent } from "@google/adk";
38
- *
39
- * registerAIGateway({ apiKey: process.env.AI_GATEWAY_API_KEY });
40
- *
41
- * const agent = new LlmAgent({
42
- * name: "assistant",
43
- * model: "anthropic/claude-sonnet-4"
44
- * });
45
- * ```
46
- *
47
- * ## Configuration
48
- *
49
- * Configuration is resolved in priority order:
50
- * 1. Instance configuration (passed to constructor/factory)
51
- * 2. Global configuration (via `setProviderConfig`)
52
- * 3. Environment variables
53
- * 4. Default values
54
- *
55
- * @module adk-llm-bridge
56
- *
57
- * @see {@link https://github.com/pailat/adk-llm-bridge|GitHub Repository}
58
- * @see {@link https://google.github.io/adk-docs/|Google ADK Documentation}
59
- */
60
- export { BaseLlm } from "@google/adk";
61
- export { BaseProviderLlm } from "./core/base-provider-llm";
62
- export type { OpenAIClientConfig } from "./core/openai-compatible-llm";
63
- export { OpenAICompatibleLlm } from "./core/openai-compatible-llm";
64
- export { AIGateway, AIGatewayLlm, isAIGatewayRegistered, registerAIGateway, } from "./providers/ai-gateway";
65
- export { isOpenRouterRegistered, OpenRouter, OpenRouterLlm, registerOpenRouter, } from "./providers/openrouter";
66
- export { isOpenAIRegistered, OpenAI, OpenAILlm, registerOpenAI, } from "./providers/openai";
67
- export { isXAIRegistered, registerXAI, XAI, XAILlm } from "./providers/xai";
68
- export { Anthropic, AnthropicLlm, isAnthropicRegistered, registerAnthropic, } from "./providers/anthropic";
69
- export { Custom, CustomLlm, type CustomLlmProviderConfig, createCustomLlm, } from "./providers/custom";
70
- export type { AIGatewayConfig, AnthropicProviderConfig, AnthropicRegisterOptions, BaseProviderConfig, CustomLlmConfig, OpenAIProviderConfig, OpenAIRegisterOptions, OpenRouterConfig, OpenRouterProviderPreferences, OpenRouterRegisterOptions, RegisterOptions, StreamAccumulator, StreamChunkResult, ToolCallAccumulator, XAIProviderConfig, XAIRegisterOptions, } from "./types";
71
- export { DEFAULT_BASE_URL, MODEL_PATTERNS, OPENROUTER_BASE_URL, OPENROUTER_MODEL_PATTERNS, PROVIDER_IDS, } from "./constants";
72
- export { getConfig, getProviderConfig, resetAllConfigs, resetConfig, resetProviderConfig, setConfig, setProviderConfig, } from "./config";
73
- export { convertRequest } from "./converters/request";
74
- export { convertResponse, convertStreamChunk, createStreamAccumulator, } from "./converters/response";
75
- //# sourceMappingURL=index.d.ts.map
6
+ * Base configuration shared by all LLM providers.
7
+ *
8
+ * All provider-specific configurations extend this interface.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const config: BaseProviderConfig = {
13
+ * model: "anthropic/claude-sonnet-4",
14
+ * apiKey: "your-api-key",
15
+ * timeout: 30000
16
+ * };
17
+ * ```
18
+ */
19
+ interface BaseProviderConfig {
20
+ /**
21
+ * The model identifier in provider/model format.
22
+ *
23
+ * @example "anthropic/claude-sonnet-4"
24
+ * @example "openai/gpt-4o"
25
+ */
26
+ model: string;
27
+ /**
28
+ * Base URL for the API endpoint.
29
+ *
30
+ * @defaultValue Provider-specific default URL
31
+ */
32
+ baseURL?: string;
33
+ /**
34
+ * API key for authentication.
35
+ *
36
+ * @defaultValue Value from environment variables
37
+ */
38
+ apiKey?: string;
39
+ /**
40
+ * Request timeout in milliseconds.
41
+ *
42
+ * @defaultValue 60000 (60 seconds)
43
+ */
44
+ timeout?: number;
45
+ /**
46
+ * Maximum number of retry attempts for failed requests.
47
+ *
48
+ * @defaultValue 2
49
+ */
50
+ maxRetries?: number;
51
+ }
52
+ /**
53
+ * Configuration options for the AI Gateway (Vercel) provider.
54
+ *
55
+ * Extends {@link BaseProviderConfig} with no additional options.
56
+ * AI Gateway uses standard OpenAI-compatible configuration.
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * const config: AIGatewayConfig = {
61
+ * model: "anthropic/claude-sonnet-4",
62
+ * apiKey: process.env.AI_GATEWAY_API_KEY
63
+ * };
64
+ * ```
65
+ *
66
+ * @see {@link BaseProviderConfig} for inherited options
67
+ */
68
+ interface AIGatewayConfig extends BaseProviderConfig {}
69
+ /**
70
+ * Options for registering AI Gateway with ADK's LLMRegistry.
71
+ *
72
+ * These options apply to all models created through the registry.
73
+ *
74
+ * @example
75
+ * ```typescript
76
+ * registerAIGateway({
77
+ * apiKey: process.env.AI_GATEWAY_API_KEY,
78
+ * baseURL: "https://custom-gateway.example.com/v1"
79
+ * });
80
+ * ```
81
+ */
82
+ interface RegisterOptions {
83
+ /**
84
+ * Base URL for the API endpoint.
85
+ *
86
+ * @defaultValue "https://ai-gateway.vercel.sh/v1"
87
+ */
88
+ baseURL?: string;
89
+ /**
90
+ * API key for authentication.
91
+ *
92
+ * @defaultValue process.env.AI_GATEWAY_API_KEY || process.env.OPENAI_API_KEY
93
+ */
94
+ apiKey?: string;
95
+ }
96
+ /**
97
+ * OpenRouter provider routing preferences.
98
+ *
99
+ * Controls how OpenRouter selects and routes requests to underlying providers.
100
+ * These preferences are sent as part of the request body.
101
+ *
102
+ * @see {@link https://openrouter.ai/docs#provider-routing|OpenRouter Provider Routing}
103
+ *
104
+ * @example
105
+ * ```typescript
106
+ * const preferences: OpenRouterProviderPreferences = {
107
+ * order: ["Anthropic", "Google"],
108
+ * allow_fallbacks: true,
109
+ * sort: "price"
110
+ * };
111
+ * ```
112
+ */
113
+ interface OpenRouterProviderPreferences {
114
+ /**
115
+ * Preferred provider order.
116
+ *
117
+ * Providers are tried in this order before falling back to others.
118
+ *
119
+ * @example ["Anthropic", "Google", "OpenAI"]
120
+ */
121
+ order?: string[];
122
+ /**
123
+ * Allow fallback to other providers if preferred ones are unavailable.
124
+ *
125
+ * @defaultValue true
126
+ */
127
+ allow_fallbacks?: boolean;
128
+ /**
129
+ * Require providers to support all parameters in the request.
130
+ *
131
+ * If true, providers that don't support certain parameters will be skipped.
132
+ */
133
+ require_parameters?: boolean;
134
+ /**
135
+ * Data collection policy for the request.
136
+ *
137
+ * - `"allow"`: Allow providers to use data for training
138
+ * - `"deny"`: Prevent data collection
139
+ */
140
+ data_collection?: "allow" | "deny";
141
+ /**
142
+ * Sort available providers by criteria.
143
+ *
144
+ * - `"price"`: Cheapest first
145
+ * - `"throughput"`: Highest throughput first
146
+ * - `"latency"`: Lowest latency first
147
+ */
148
+ sort?: "price" | "throughput" | "latency";
149
+ /**
150
+ * Only use these providers.
151
+ *
152
+ * Requests will fail if none of these providers are available.
153
+ *
154
+ * @example ["Anthropic"]
155
+ */
156
+ only?: string[];
157
+ /**
158
+ * Never use these providers.
159
+ *
160
+ * @example ["Together", "Fireworks"]
161
+ */
162
+ ignore?: string[];
163
+ }
164
+ /**
165
+ * Configuration options for the OpenRouter provider.
166
+ *
167
+ * Extends {@link BaseProviderConfig} with OpenRouter-specific options
168
+ * for site attribution and provider routing.
169
+ *
170
+ * @example
171
+ * ```typescript
172
+ * const config: OpenRouterConfig = {
173
+ * model: "anthropic/claude-sonnet-4",
174
+ * apiKey: process.env.OPENROUTER_API_KEY,
175
+ * siteUrl: "https://myapp.com",
176
+ * appName: "My Application",
177
+ * provider: {
178
+ * sort: "price",
179
+ * allow_fallbacks: true
180
+ * }
181
+ * };
182
+ * ```
183
+ *
184
+ * @see {@link BaseProviderConfig} for inherited options
185
+ * @see {@link OpenRouterProviderPreferences} for routing options
186
+ */
187
+ interface OpenRouterConfig extends BaseProviderConfig {
188
+ /**
189
+ * Your site URL for OpenRouter leaderboard rankings.
190
+ *
191
+ * Sent as the `HTTP-Referer` header. Sites with more usage rank higher.
192
+ *
193
+ * @example "https://myapp.com"
194
+ */
195
+ siteUrl?: string;
196
+ /**
197
+ * Your application name for OpenRouter leaderboard rankings.
198
+ *
199
+ * Sent as the `X-Title` header. Appears in OpenRouter's app rankings.
200
+ *
201
+ * @example "My AI Assistant"
202
+ */
203
+ appName?: string;
204
+ /**
205
+ * Provider routing preferences.
206
+ *
207
+ * Controls how OpenRouter selects providers for this request.
208
+ *
209
+ * @see {@link OpenRouterProviderPreferences}
210
+ */
211
+ provider?: OpenRouterProviderPreferences;
212
+ }
213
+ /**
214
+ * Options for registering OpenRouter with ADK's LLMRegistry.
215
+ *
216
+ * These options apply to all models created through the registry.
217
+ *
218
+ * @example
219
+ * ```typescript
220
+ * registerOpenRouter({
221
+ * apiKey: process.env.OPENROUTER_API_KEY,
222
+ * siteUrl: "https://myapp.com",
223
+ * appName: "My Application"
224
+ * });
225
+ * ```
226
+ */
227
+ interface OpenRouterRegisterOptions {
228
+ /**
229
+ * Base URL for the API endpoint.
230
+ *
231
+ * @defaultValue "https://openrouter.ai/api/v1"
232
+ */
233
+ baseURL?: string;
234
+ /**
235
+ * API key for authentication.
236
+ *
237
+ * @defaultValue process.env.OPENROUTER_API_KEY
238
+ */
239
+ apiKey?: string;
240
+ /**
241
+ * Your site URL for OpenRouter leaderboard rankings.
242
+ *
243
+ * @example "https://myapp.com"
244
+ */
245
+ siteUrl?: string;
246
+ /**
247
+ * Your application name for OpenRouter leaderboard rankings.
248
+ *
249
+ * @example "My AI Assistant"
250
+ */
251
+ appName?: string;
252
+ }
253
+ /**
254
+ * Configuration options for custom LLM providers.
255
+ *
256
+ * Use this to connect any API that implements the OpenAI chat completions
257
+ * interface, such as Ollama, LM Studio, vLLM, Azure OpenAI, or self-hosted models.
258
+ *
259
+ * @example
260
+ * ```typescript
261
+ * // Ollama
262
+ * const config: CustomLlmConfig = {
263
+ * name: "ollama",
264
+ * model: "llama3",
265
+ * baseURL: "http://localhost:11434/v1"
266
+ * };
267
+ * ```
268
+ *
269
+ * @example
270
+ * ```typescript
271
+ * // Azure OpenAI
272
+ * const config: CustomLlmConfig = {
273
+ * name: "azure",
274
+ * model: "gpt-4",
275
+ * baseURL: "https://my-resource.openai.azure.com/openai/deployments/gpt-4",
276
+ * headers: { "api-key": process.env.AZURE_API_KEY },
277
+ * queryParams: { "api-version": "2024-02-01" }
278
+ * };
279
+ * ```
280
+ *
281
+ * @see {@link BaseProviderConfig} for inherited options
282
+ * @see {@link createCustomLlm} for the factory function
283
+ */
284
+ interface CustomLlmConfig extends BaseProviderConfig {
285
+ /**
286
+ * Provider name for identification in logs and error messages.
287
+ *
288
+ * Used to generate error codes like `OLLAMA_ERROR` or `AZURE_ERROR`.
289
+ *
290
+ * @defaultValue "CUSTOM"
291
+ * @example "ollama"
292
+ * @example "azure"
293
+ * @example "vllm"
294
+ */
295
+ name?: string;
296
+ /**
297
+ * Additional HTTP headers to include in all requests.
298
+ *
299
+ * Useful for custom authentication schemes or provider-specific headers.
300
+ *
301
+ * @example
302
+ * ```typescript
303
+ * headers: {
304
+ * "api-key": process.env.AZURE_API_KEY,
305
+ * "X-Custom-Header": "value"
306
+ * }
307
+ * ```
308
+ */
309
+ headers?: Record<string, string>;
310
+ /**
311
+ * Query parameters to append to all API requests.
312
+ *
313
+ * Required by some providers like Azure OpenAI for API versioning.
314
+ *
315
+ * @example
316
+ * ```typescript
317
+ * queryParams: {
318
+ * "api-version": "2024-02-01"
319
+ * }
320
+ * ```
321
+ */
322
+ queryParams?: Record<string, string>;
323
+ /**
324
+ * Additional options to include in the request body.
325
+ *
326
+ * These are spread into the chat completion request, allowing
327
+ * provider-specific parameters beyond the standard OpenAI API.
328
+ *
329
+ * @example
330
+ * ```typescript
331
+ * providerOptions: {
332
+ * temperature: 0.7,
333
+ * custom_field: "value"
334
+ * }
335
+ * ```
336
+ */
337
+ providerOptions?: Record<string, unknown>;
338
+ }
339
+ /**
340
+ * Configuration options for the OpenAI provider.
341
+ *
342
+ * Extends {@link BaseProviderConfig} with OpenAI-specific options
343
+ * for organization and project identification.
344
+ *
345
+ * @example
346
+ * ```typescript
347
+ * const config: OpenAIProviderConfig = {
348
+ * model: "gpt-4.1",
349
+ * apiKey: process.env.OPENAI_API_KEY,
350
+ * organization: "org-xxx",
351
+ * project: "proj-xxx"
352
+ * };
353
+ * ```
354
+ *
355
+ * @see {@link BaseProviderConfig} for inherited options
356
+ * @see {@link OpenAI} for the factory function
357
+ */
358
+ interface OpenAIProviderConfig extends BaseProviderConfig {
359
+ /**
360
+ * OpenAI organization ID.
361
+ *
362
+ * Sent as the `OpenAI-Organization` header for organization-scoped requests.
363
+ *
364
+ * @example "org-xxx"
365
+ */
366
+ organization?: string;
367
+ /**
368
+ * OpenAI project ID.
369
+ *
370
+ * Sent as the `OpenAI-Project` header for project-scoped requests.
371
+ *
372
+ * @example "proj-xxx"
373
+ */
374
+ project?: string;
375
+ }
376
+ /**
377
+ * Options for registering OpenAI with ADK's LLMRegistry.
378
+ *
379
+ * These options apply to all models created through the registry.
380
+ *
381
+ * @example
382
+ * ```typescript
383
+ * registerOpenAI({
384
+ * apiKey: process.env.OPENAI_API_KEY,
385
+ * organization: "org-xxx"
386
+ * });
387
+ * ```
388
+ */
389
+ interface OpenAIRegisterOptions {
390
+ /**
391
+ * API key for authentication.
392
+ *
393
+ * @defaultValue process.env.OPENAI_API_KEY
394
+ */
395
+ apiKey?: string;
396
+ /**
397
+ * OpenAI organization ID.
398
+ *
399
+ * @example "org-xxx"
400
+ */
401
+ organization?: string;
402
+ /**
403
+ * OpenAI project ID.
404
+ *
405
+ * @example "proj-xxx"
406
+ */
407
+ project?: string;
408
+ }
409
+ /**
410
+ * Configuration options for the xAI (Grok) provider.
411
+ *
412
+ * Extends {@link BaseProviderConfig} with no additional options.
413
+ * xAI uses standard OpenAI-compatible configuration.
414
+ *
415
+ * @example
416
+ * ```typescript
417
+ * const config: XAIProviderConfig = {
418
+ * model: "grok-4",
419
+ * apiKey: process.env.XAI_API_KEY
420
+ * };
421
+ * ```
422
+ *
423
+ * @see {@link BaseProviderConfig} for inherited options
424
+ * @see {@link XAI} for the factory function
425
+ */
426
+ interface XAIProviderConfig extends BaseProviderConfig {}
427
+ /**
428
+ * Options for registering xAI with ADK's LLMRegistry.
429
+ *
430
+ * These options apply to all models created through the registry.
431
+ *
432
+ * @example
433
+ * ```typescript
434
+ * registerXAI({
435
+ * apiKey: process.env.XAI_API_KEY
436
+ * });
437
+ * ```
438
+ */
439
+ interface XAIRegisterOptions {
440
+ /**
441
+ * API key for authentication.
442
+ *
443
+ * @defaultValue process.env.XAI_API_KEY
444
+ */
445
+ apiKey?: string;
446
+ }
447
+ /**
448
+ * Configuration options for the Anthropic (Claude) provider.
449
+ *
450
+ * Extends {@link BaseProviderConfig} with Anthropic-specific options.
451
+ *
452
+ * @example
453
+ * ```typescript
454
+ * const config: AnthropicProviderConfig = {
455
+ * model: "claude-sonnet-4-5-20250929",
456
+ * apiKey: process.env.ANTHROPIC_API_KEY,
457
+ * maxTokens: 4096
458
+ * };
459
+ * ```
460
+ *
461
+ * @see {@link BaseProviderConfig} for inherited options
462
+ * @see {@link Anthropic} for the factory function
463
+ */
464
+ interface AnthropicProviderConfig extends BaseProviderConfig {
465
+ /**
466
+ * Maximum number of tokens to generate.
467
+ *
468
+ * Required by Anthropic API. If not provided, defaults to 4096.
469
+ *
470
+ * @defaultValue 4096
471
+ */
472
+ maxTokens?: number;
473
+ }
474
+ /**
475
+ * Options for registering Anthropic with ADK's LLMRegistry.
476
+ *
477
+ * These options apply to all models created through the registry.
478
+ *
479
+ * @example
480
+ * ```typescript
481
+ * registerAnthropic({
482
+ * apiKey: process.env.ANTHROPIC_API_KEY,
483
+ * maxTokens: 8192
484
+ * });
485
+ * ```
486
+ */
487
+ interface AnthropicRegisterOptions {
488
+ /**
489
+ * API key for authentication.
490
+ *
491
+ * @defaultValue process.env.ANTHROPIC_API_KEY
492
+ */
493
+ apiKey?: string;
494
+ /**
495
+ * Maximum number of tokens to generate.
496
+ *
497
+ * @defaultValue 4096
498
+ */
499
+ maxTokens?: number;
500
+ }
501
+ /**
502
+ * Accumulator for tool call data during streaming.
503
+ *
504
+ * Used internally to collect partial tool call information
505
+ * as chunks arrive from the API.
506
+ *
507
+ * @internal
508
+ */
509
+ interface ToolCallAccumulator {
510
+ /** Unique identifier for the tool call */
511
+ id: string;
512
+ /** Name of the function being called */
513
+ name: string;
514
+ /** JSON string of accumulated function arguments */
515
+ arguments: string;
516
+ }
517
+ /**
518
+ * Accumulator state for streaming responses.
519
+ *
520
+ * Tracks the accumulated text and tool calls across multiple stream chunks.
521
+ * Created using {@link createStreamAccumulator}.
522
+ *
523
+ * @example
524
+ * ```typescript
525
+ * const accumulator = createStreamAccumulator();
526
+ *
527
+ * for await (const chunk of stream) {
528
+ * const result = convertStreamChunk(chunk, accumulator);
529
+ * if (result.isComplete) {
530
+ * return result.response;
531
+ * }
532
+ * }
533
+ * ```
534
+ *
535
+ * @see {@link createStreamAccumulator}
536
+ * @see {@link convertStreamChunk}
537
+ */
538
+ interface StreamAccumulator {
539
+ /** Accumulated text content from all chunks */
540
+ text: string;
541
+ /** Map of tool call index to accumulated tool call data */
542
+ toolCalls: Map<number, ToolCallAccumulator>;
543
+ }
544
+ /**
545
+ * Result from processing a stream chunk.
546
+ *
547
+ * Contains either a partial update or the final complete response.
548
+ *
549
+ * @see {@link convertStreamChunk}
550
+ */
551
+ interface StreamChunkResult {
552
+ /**
553
+ * The LLM response, present when chunk contains meaningful content.
554
+ *
555
+ * For intermediate chunks, this may be a partial response.
556
+ * For the final chunk, this is the complete accumulated response.
557
+ */
558
+ response?: LlmResponse;
559
+ /**
560
+ * Whether the stream has completed.
561
+ *
562
+ * When `true`, the `response` contains the final accumulated result.
563
+ */
564
+ isComplete: boolean;
565
+ }
566
+ /**
567
+ * Abstract base class for all LLM providers in adk-llm-bridge.
568
+ *
569
+ * Extends ADK's `BaseLlm` and provides common infrastructure for error handling
570
+ * and configuration management. All provider implementations (AI Gateway,
571
+ * OpenRouter, custom providers) should extend this class.
572
+ *
573
+ * @abstract
574
+ *
575
+ * @example
576
+ * ```typescript
577
+ * class CustomLlm extends BaseProviderLlm {
578
+ * static supportedModels = [/^custom\/.+$/];
579
+ *
580
+ * async *generateContentAsync(
581
+ * llmRequest: LlmRequest,
582
+ * stream?: boolean
583
+ * ): AsyncGenerator<LlmResponse, void> {
584
+ * try {
585
+ * // Implementation here
586
+ * yield response;
587
+ * } catch (error) {
588
+ * yield this.createErrorResponse(error, "CUSTOM");
589
+ * }
590
+ * }
591
+ * }
592
+ * ```
593
+ *
594
+ * @see {@link OpenAICompatibleLlm} for OpenAI-compatible API implementations
595
+ */
596
+ declare abstract class BaseProviderLlm extends BaseLlm {
597
+ /**
598
+ * Provider configuration.
599
+ *
600
+ * Contains the resolved configuration including model, API key, base URL, etc.
601
+ *
602
+ * @protected
603
+ */
604
+ protected readonly config: BaseProviderConfig;
605
+ /**
606
+ * Creates a new BaseProviderLlm instance.
607
+ *
608
+ * @param config - Provider configuration options
609
+ */
610
+ constructor(config: BaseProviderConfig);
611
+ /**
612
+ * Establishes a bidirectional streaming connection.
613
+ *
614
+ * This method is required by ADK's BaseLlm interface but is not supported
615
+ * by OpenAI-compatible APIs. It always throws an error.
616
+ *
617
+ * @param _ - The LLM request (unused)
618
+ * @returns Never - always throws
619
+ * @throws Error indicating bidirectional streaming is not supported
620
+ */
621
+ connect(_: LlmRequest): Promise<BaseLlmConnection>;
622
+ /**
623
+ * Creates a standardized error response for ADK.
624
+ *
625
+ * Converts JavaScript errors into ADK-compatible LlmResponse objects
626
+ * with appropriate error codes and messages.
627
+ *
628
+ * @param error - The error that occurred (Error instance or any value)
629
+ * @param prefix - Provider-specific error prefix (e.g., "AI_GATEWAY", "OPENROUTER")
630
+ * @returns An LlmResponse with error information and turnComplete: true
631
+ *
632
+ * @protected
633
+ *
634
+ * @example
635
+ * ```typescript
636
+ * try {
637
+ * // API call
638
+ * } catch (error) {
639
+ * yield this.createErrorResponse(error, "MY_PROVIDER");
640
+ * // Returns: { errorCode: "MY_PROVIDER_ERROR", errorMessage: "...", turnComplete: true }
641
+ * }
642
+ * ```
643
+ */
644
+ protected createErrorResponse(error: unknown, prefix: string): LlmResponse2;
645
+ }
646
+ import { LlmRequest as LlmRequest2, LlmResponse as LlmResponse3 } from "@google/adk";
647
+ import OpenAI from "openai";
648
+ /**
649
+ * @license
650
+ * Copyright 2025 PAI
651
+ * SPDX-License-Identifier: MIT
652
+ */
653
+ /**
654
+ * Declarative provider definition interface.
655
+ *
656
+ * Providers that use OpenAI-compatible APIs differ only in configuration
657
+ * (URLs, env vars, headers), not behavior. This interface captures those
658
+ * differences as data instead of requiring class inheritance.
659
+ *
660
+ * @module core/provider-definition
661
+ */
662
+ /**
663
+ * Environment variable keys for resolving provider configuration.
664
+ */
665
+ interface ProviderEnvKeys {
666
+ /** Env var names to check for API key, in priority order */
667
+ apiKey: string[];
668
+ /** Env var names to check for base URL, in priority order */
669
+ baseURL?: string[];
670
+ }
671
+ /**
672
+ * Declarative definition for an OpenAI-compatible LLM provider.
673
+ *
674
+ * Instead of creating a subclass for each provider, define the provider
675
+ * as a configuration object. The core infrastructure handles the rest.
676
+ *
677
+ * @example
678
+ * ```typescript
679
+ * const MY_PROVIDER: ProviderDefinition = {
680
+ * id: "my-provider",
681
+ * errorPrefix: "MY_PROVIDER",
682
+ * defaultBaseURL: "https://api.myprovider.com/v1",
683
+ * envKeys: { apiKey: ["MY_PROVIDER_API_KEY"] },
684
+ * modelPatterns: [/.+\/.+/],
685
+ * };
686
+ * ```
687
+ */
688
+ interface ProviderDefinition {
689
+ /** Provider ID for the config system (e.g. "ai-gateway", "openrouter") */
690
+ id: string;
691
+ /** Error prefix for error codes (e.g. "AI_GATEWAY" → "AI_GATEWAY_ERROR") */
692
+ errorPrefix: string;
693
+ /** Default base URL when not specified in config or env vars */
694
+ defaultBaseURL: string;
695
+ /** Environment variable names to resolve for apiKey and baseURL */
696
+ envKeys: ProviderEnvKeys;
697
+ /** Model patterns for LLMRegistry matching */
698
+ modelPatterns: (string | RegExp)[];
699
+ /** Build custom HTTP headers from the instance config */
700
+ buildHeaders?: (config: Record<string, unknown>) => Record<string, string>;
701
+ /** Build provider-specific options to merge into request body */
702
+ buildRequestOptions?: (config: Record<string, unknown>) => Record<string, unknown>;
703
+ /**
704
+ * Whether this provider requires an API key at construction time.
705
+ *
706
+ * Cloud providers (AI Gateway, OpenRouter, OpenAI, xAI) should set this
707
+ * to true. Local/self-hosted providers that may not need auth can omit it.
708
+ *
709
+ * @default false
710
+ */
711
+ requireApiKey?: boolean;
712
+ }
713
+ /**
714
+ * Configuration for the underlying OpenAI client.
715
+ *
716
+ * These options are passed directly to the OpenAI SDK constructor.
717
+ */
718
+ interface OpenAIClientConfig {
719
+ /** Base URL for the API endpoint. */
720
+ baseURL: string;
721
+ /** API key for authentication. */
722
+ apiKey: string;
723
+ /** Request timeout in milliseconds. */
724
+ timeout: number;
725
+ /** Maximum number of retry attempts. */
726
+ maxRetries: number;
727
+ /** Additional HTTP headers to include in all requests. */
728
+ defaultHeaders?: Record<string, string>;
729
+ }
730
+ /**
731
+ * Base class for LLM providers that use OpenAI-compatible APIs.
732
+ *
733
+ * Supports two construction modes:
734
+ *
735
+ * 1. **Declarative** (recommended): Pass a {@link ProviderDefinition} that
736
+ * describes the provider's configuration. Config resolution is automatic.
737
+ *
738
+ * 2. **Manual**: Pass a {@link BaseProviderConfig} + {@link OpenAIClientConfig}
739
+ * with pre-resolved values. Used by CustomLlm which has unique URL logic.
740
+ *
741
+ * @example
742
+ * ```typescript
743
+ * // Declarative (most providers)
744
+ * const llm = new OpenAICompatibleLlm(MY_DEFINITION, { model: "gpt-4o" });
745
+ *
746
+ * // Manual (Custom provider)
747
+ * class CustomLlm extends OpenAICompatibleLlm {
748
+ * constructor(config) {
749
+ * super(config, { baseURL, apiKey, timeout, maxRetries });
750
+ * }
751
+ * }
752
+ * ```
753
+ */
754
+ declare class OpenAICompatibleLlm extends BaseProviderLlm {
755
+ /** Model patterns for LLMRegistry — set by createProviderClass(). */
756
+ static supportedModels: (string | RegExp)[];
757
+ /** @protected */
758
+ protected readonly client: OpenAI;
759
+ private readonly _errorPrefix;
760
+ private readonly _getRequestOptions?;
761
+ /** Declarative constructor: definition + config */
762
+ constructor(definition: ProviderDefinition, config: BaseProviderConfig);
763
+ /** Manual constructor: config + clientConfig (for CustomLlm) */
764
+ constructor(config: BaseProviderConfig, clientConfig: OpenAIClientConfig);
765
+ /** Returns the provider error prefix. */
766
+ protected getErrorPrefix(): string;
767
+ /** Returns provider-specific request options. */
768
+ protected getProviderRequestOptions(): Record<string, unknown>;
769
+ generateContentAsync(llmRequest: LlmRequest2, stream?: boolean): AsyncGenerator<LlmResponse3, void>;
770
+ private singleResponse;
771
+ private streamResponse;
772
+ }
773
+ import { BaseLlm as BaseLlm_m11s8s } from "@google/adk";
774
+ declare const AIGatewayLlm: (new (params: {
775
+ model: string;
776
+ }) => BaseLlm_m11s8s) & {
777
+ readonly supportedModels: (string | RegExp)[];
778
+ };
779
+ declare const AIGateway: (model: string, options?: Partial<Omit<BaseProviderConfig, "model">>) => OpenAICompatibleLlm;
780
+ declare const registerAIGateway: (options?: Record<string, unknown>) => void;
781
+ declare const isAIGatewayRegistered: () => boolean;
782
+ import { BaseLlm as BaseLlm_m11s8s2 } from "@google/adk";
783
+ declare const OpenRouterLlm: (new (params: {
784
+ model: string;
785
+ }) => BaseLlm_m11s8s2) & {
786
+ readonly supportedModels: (string | RegExp)[];
787
+ };
788
+ declare const OpenRouter: (model: string, options?: Partial<Omit<BaseProviderConfig, "model">>) => OpenAICompatibleLlm;
789
+ declare const registerOpenRouter: (options?: Record<string, unknown>) => void;
790
+ declare const isOpenRouterRegistered: () => boolean;
791
+ import { BaseLlm as BaseLlm_m11s8s3 } from "@google/adk";
792
+ declare const OpenAILlm: (new (params: {
793
+ model: string;
794
+ }) => BaseLlm_m11s8s3) & {
795
+ readonly supportedModels: (string | RegExp)[];
796
+ };
797
+ declare const OpenAI2: (model: string, options?: Partial<Omit<BaseProviderConfig, "model">>) => OpenAICompatibleLlm;
798
+ declare const registerOpenAI: (options?: Record<string, unknown>) => void;
799
+ declare const isOpenAIRegistered: () => boolean;
800
+ import { BaseLlm as BaseLlm_m11s8s4 } from "@google/adk";
801
+ declare const XAILlm: (new (params: {
802
+ model: string;
803
+ }) => BaseLlm_m11s8s4) & {
804
+ readonly supportedModels: (string | RegExp)[];
805
+ };
806
+ declare const XAI: (model: string, options?: Partial<Omit<BaseProviderConfig, "model">>) => OpenAICompatibleLlm;
807
+ declare const registerXAI: (options?: Record<string, unknown>) => void;
808
+ declare const isXAIRegistered: () => boolean;
809
+ import { LlmRequest as LlmRequest3, LlmResponse as LlmResponse4 } from "@google/adk";
810
+ /**
811
+ * Anthropic (Claude) LLM provider.
812
+ *
813
+ * Provides direct access to Anthropic's Messages API for Claude models.
814
+ * Unlike OpenAI-compatible providers, this uses the native Anthropic SDK
815
+ * with custom request/response converters.
816
+ *
817
+ * Configuration priority (highest to lowest):
818
+ * 1. Instance configuration (passed to constructor)
819
+ * 2. Global configuration (via `setProviderConfig("anthropic", {...})`)
820
+ * 3. Environment variables (`ANTHROPIC_API_KEY`)
821
+ *
822
+ * @example
823
+ * ```typescript
824
+ * // Basic usage
825
+ * const llm = new AnthropicLlm({ model: "claude-sonnet-4-5-20250929" });
826
+ *
827
+ * // With max tokens
828
+ * const llm = new AnthropicLlm({
829
+ * model: "claude-sonnet-4-5-20250929",
830
+ * apiKey: "...",
831
+ * maxTokens: 8192
832
+ * });
833
+ * ```
834
+ *
835
+ * @see {@link Anthropic} for the recommended factory function
836
+ * @see {@link registerAnthropic} for LLMRegistry integration
837
+ */
838
+ declare class AnthropicLlm extends BaseProviderLlm {
839
+ /**
840
+ * Model patterns supported by this provider.
841
+ *
842
+ * Used by ADK's LLMRegistry to match model strings to this provider.
843
+ * Matches: claude-*
844
+ *
845
+ * @static
846
+ */
847
+ static readonly supportedModels: RegExp[];
848
+ /**
849
+ * The Anthropic SDK client instance.
850
+ *
851
+ * @private
852
+ */
853
+ private readonly client;
854
+ /**
855
+ * Maximum tokens to generate in responses.
856
+ *
857
+ * @private
858
+ */
859
+ private readonly maxTokens;
860
+ /**
861
+ * Creates a new Anthropic LLM instance.
862
+ *
863
+ * @param config - Configuration options for the Anthropic provider
864
+ *
865
+ * @example
866
+ * ```typescript
867
+ * const llm = new AnthropicLlm({
868
+ * model: "claude-sonnet-4-5-20250929",
869
+ * apiKey: process.env.ANTHROPIC_API_KEY,
870
+ * maxTokens: 4096
871
+ * });
872
+ * ```
873
+ */
874
+ constructor(config: AnthropicProviderConfig);
875
+ /**
876
+ * Generates content from the Anthropic API.
877
+ *
878
+ * Converts the ADK request to Anthropic format, makes the API call,
879
+ * and converts the response back to ADK format.
880
+ *
881
+ * @param llmRequest - The ADK LLM request
882
+ * @param stream - Whether to stream the response (default: false)
883
+ * @returns An async generator yielding LLM responses
884
+ */
885
+ generateContentAsync(llmRequest: LlmRequest3, stream?: boolean): AsyncGenerator<LlmResponse4, void>;
886
+ /**
887
+ * Builds the shared request parameters for the Anthropic API.
888
+ *
889
+ * @private
890
+ */
891
+ private buildRequestParams;
892
+ /**
893
+ * Makes a single (non-streaming) API request.
894
+ *
895
+ * @private
896
+ */
897
+ private singleResponse;
898
+ /**
899
+ * Makes a streaming API request and yields responses as they arrive.
900
+ *
901
+ * @private
902
+ */
903
+ private streamResponse;
904
+ }
905
+ /**
906
+ * Creates an Anthropic (Claude) LLM instance.
907
+ *
908
+ * @param model - The Claude model to use
909
+ * @param options - Optional configuration options
910
+ * @returns A configured Anthropic LLM instance
911
+ */
912
+ declare function Anthropic3(model: string, options?: Omit<AnthropicProviderConfig, "model">): AnthropicLlm;
913
+ declare function registerAnthropic(options?: AnthropicRegisterOptions): void;
914
+ declare function isAnthropicRegistered(): boolean;
915
+ /**
916
+ * Configuration type with required baseURL for custom providers.
917
+ *
918
+ * Unlike built-in providers (AI Gateway, OpenRouter) which have default URLs,
919
+ * custom providers require an explicit baseURL.
920
+ */
921
+ type CustomLlmProviderConfig = CustomLlmConfig & {
922
+ /**
923
+ * Base URL for the API endpoint (required).
924
+ *
925
+ * @example "http://localhost:11434/v1" // Ollama
926
+ * @example "http://localhost:8000/v1" // vLLM
927
+ * @example "https://my-resource.openai.azure.com/openai/deployments/gpt-4" // Azure
928
+ */
929
+ baseURL: string;
930
+ };
931
+ /**
932
+ * LLM implementation for any API that supports the chat completions interface.
933
+ *
934
+ * This provider allows connecting to any API that implements the OpenAI
935
+ * chat completions interface. Use this for:
936
+ *
937
+ * - **Local models**: Ollama, LM Studio, vLLM, llama.cpp
938
+ * - **Cloud providers**: Azure OpenAI, Together AI, Anyscale
939
+ * - **Self-hosted**: Any compatible server
940
+ *
941
+ * Unlike AI Gateway and OpenRouter, this provider:
942
+ * - Requires explicit `baseURL` configuration
943
+ * - Does not use environment variables for defaults
944
+ * - Supports custom headers and query parameters
945
+ *
946
+ * @example
947
+ * ```typescript
948
+ * // Ollama
949
+ * const llm = new CustomLlm({
950
+ * name: "ollama",
951
+ * model: "llama3",
952
+ * baseURL: "http://localhost:11434/v1"
953
+ * });
954
+ * ```
955
+ *
956
+ * @example
957
+ * ```typescript
958
+ * // Azure OpenAI
959
+ * const llm = new CustomLlm({
960
+ * name: "azure",
961
+ * model: "gpt-4",
962
+ * baseURL: "https://my-resource.openai.azure.com/openai/deployments/gpt-4",
963
+ * headers: { "api-key": process.env.AZURE_API_KEY },
964
+ * queryParams: { "api-version": "2024-02-01" }
965
+ * });
966
+ * ```
967
+ *
968
+ * @see {@link createCustomLlm} for the factory function
969
+ * @see {@link Custom} for the shorthand alias
970
+ */
971
+ declare class CustomLlm extends OpenAICompatibleLlm {
972
+ /**
973
+ * Model patterns supported by this provider.
974
+ *
975
+ * Accepts any model identifier since different APIs use different naming conventions.
976
+ *
977
+ * @static
978
+ */
979
+ static readonly supportedModels: RegExp[];
980
+ /**
981
+ * Provider name used for error prefixes.
982
+ *
983
+ * @private
984
+ */
985
+ private readonly providerName;
986
+ /**
987
+ * Provider-specific options to include in requests.
988
+ *
989
+ * @private
990
+ */
991
+ private readonly providerOptionsConfig?;
992
+ /**
993
+ * Creates a new custom LLM provider instance.
994
+ *
995
+ * @param config - Configuration options including model, baseURL, and optional settings
996
+ *
997
+ * @example
998
+ * ```typescript
999
+ * const llm = new CustomLlm({
1000
+ * name: "vllm",
1001
+ * model: "meta-llama/Llama-3-8b",
1002
+ * baseURL: "http://localhost:8000/v1"
1003
+ * });
1004
+ * ```
1005
+ */
1006
+ constructor(config: CustomLlmProviderConfig);
1007
+ /**
1008
+ * Returns the error prefix for this provider.
1009
+ *
1010
+ * Sanitizes the provider name to create a valid error code prefix.
1011
+ * For example, "my-provider" becomes "MY_PROVIDER".
1012
+ *
1013
+ * @returns The sanitized provider name in uppercase
1014
+ * @protected
1015
+ */
1016
+ protected getErrorPrefix(): string;
1017
+ /**
1018
+ * Returns provider-specific options to include in requests.
1019
+ *
1020
+ * These options are spread into the chat completion request body,
1021
+ * allowing custom parameters beyond the standard OpenAI API.
1022
+ *
1023
+ * @returns The configured provider options or an empty object
1024
+ * @protected
1025
+ */
1026
+ protected getProviderRequestOptions(): Record<string, unknown>;
1027
+ }
1028
+ /**
1029
+ * Creates a custom LLM instance.
1030
+ *
1031
+ * This is the recommended way to connect to any API that implements
1032
+ * the chat completions interface. It provides a clean, functional API.
1033
+ *
1034
+ * @param config - Configuration options including model, baseURL, and optional settings
1035
+ * @returns A configured CustomLlm instance
1036
+ *
1037
+ * @example
1038
+ * ```typescript
1039
+ * // Ollama
1040
+ * const llm = createCustomLlm({
1041
+ * name: "ollama",
1042
+ * model: "llama3",
1043
+ * baseURL: "http://localhost:11434/v1"
1044
+ * });
1045
+ * ```
1046
+ *
1047
+ * @example
1048
+ * ```typescript
1049
+ * // Azure OpenAI
1050
+ * const llm = createCustomLlm({
1051
+ * name: "azure",
1052
+ * model: "gpt-4",
1053
+ * baseURL: "https://my-resource.openai.azure.com/openai/deployments/gpt-4",
1054
+ * apiKey: process.env.AZURE_API_KEY,
1055
+ * headers: { "api-key": process.env.AZURE_API_KEY },
1056
+ * queryParams: { "api-version": "2024-02-01" }
1057
+ * });
1058
+ * ```
1059
+ *
1060
+ * @example
1061
+ * ```typescript
1062
+ * // vLLM / LM Studio
1063
+ * const llm = createCustomLlm({
1064
+ * baseURL: "http://localhost:8000/v1",
1065
+ * model: "meta-llama/Llama-3-8b"
1066
+ * });
1067
+ * ```
1068
+ *
1069
+ * @example
1070
+ * ```typescript
1071
+ * // With provider-specific options
1072
+ * const llm = createCustomLlm({
1073
+ * name: "custom",
1074
+ * model: "my-model",
1075
+ * baseURL: "https://api.example.com/v1",
1076
+ * apiKey: "sk-...",
1077
+ * providerOptions: {
1078
+ * temperature: 0.7,
1079
+ * custom_param: "value"
1080
+ * }
1081
+ * });
1082
+ * ```
1083
+ *
1084
+ * @example
1085
+ * ```typescript
1086
+ * // Use with ADK agent
1087
+ * import { LlmAgent } from "@google/adk";
1088
+ *
1089
+ * const agent = new LlmAgent({
1090
+ * name: "assistant",
1091
+ * model: createCustomLlm({
1092
+ * name: "ollama",
1093
+ * model: "llama3",
1094
+ * baseURL: "http://localhost:11434/v1"
1095
+ * }),
1096
+ * instruction: "You are a helpful assistant."
1097
+ * });
1098
+ * ```
1099
+ *
1100
+ * @see {@link CustomLlm} for direct class usage
1101
+ * @see {@link Custom} for a shorthand alias
1102
+ */
1103
+ declare function createCustomLlm(config: CustomLlmProviderConfig): CustomLlm;
1104
+ /**
1105
+ * Configuration options for the Custom factory (model is specified separately).
1106
+ */
1107
+ type CustomOptions = Omit<CustomLlmConfig, "model"> & {
1108
+ /**
1109
+ * Base URL for the API endpoint (required).
1110
+ */
1111
+ baseURL: string;
1112
+ };
1113
+ /**
1114
+ * Creates a custom LLM instance with a shorthand syntax.
1115
+ *
1116
+ * This is an alias for `createCustomLlm` that takes the model
1117
+ * as the first argument, similar to the `AIGateway` and `OpenRouter`
1118
+ * factory functions.
1119
+ *
1120
+ * @param model - The model identifier
1121
+ * @param options - Configuration options including baseURL and optional settings
1122
+ * @returns A configured CustomLlm instance
1123
+ *
1124
+ * @example
1125
+ * ```typescript
1126
+ * // Simple usage
1127
+ * const llm = Custom("llama3", {
1128
+ * baseURL: "http://localhost:11434/v1"
1129
+ * });
1130
+ * ```
1131
+ *
1132
+ * @example
1133
+ * ```typescript
1134
+ * // With full options
1135
+ * const llm = Custom("gpt-4", {
1136
+ * name: "azure",
1137
+ * baseURL: "https://my-resource.openai.azure.com/openai/deployments/gpt-4",
1138
+ * apiKey: process.env.AZURE_API_KEY,
1139
+ * headers: { "api-key": process.env.AZURE_API_KEY },
1140
+ * queryParams: { "api-version": "2024-02-01" }
1141
+ * });
1142
+ * ```
1143
+ *
1144
+ * @example
1145
+ * ```typescript
1146
+ * // Use with ADK agent
1147
+ * import { LlmAgent } from "@google/adk";
1148
+ *
1149
+ * const agent = new LlmAgent({
1150
+ * name: "assistant",
1151
+ * model: Custom("llama3", { baseURL: "http://localhost:11434/v1" }),
1152
+ * instruction: "You are a helpful assistant."
1153
+ * });
1154
+ * ```
1155
+ *
1156
+ * @see {@link createCustomLlm} for the full configuration syntax
1157
+ */
1158
+ declare function Custom(model: string, options: CustomOptions): CustomLlm;
1159
+ /**
1160
+ * @license
1161
+ * Copyright 2025 PAI
1162
+ * SPDX-License-Identifier: MIT
1163
+ */
1164
+ /**
1165
+ * Constants and default values for adk-llm-bridge.
1166
+ *
1167
+ * This module contains all constant values including default URLs,
1168
+ * timeouts, environment variable names, and model patterns.
1169
+ *
1170
+ * @module constants
1171
+ */
1172
+ /**
1173
+ * Default base URL for the Vercel AI Gateway API.
1174
+ *
1175
+ * @constant
1176
+ * @see {@link https://vercel.com/ai-gateway|Vercel AI Gateway}
1177
+ */
1178
+ declare const DEFAULT_BASE_URL = "https://ai-gateway.vercel.sh/v1";
1179
+ /**
1180
+ * Model patterns for AI Gateway model validation.
1181
+ *
1182
+ * Matches any model identifier with the format "provider/model".
1183
+ * AI Gateway validates actual model availability at runtime.
1184
+ *
1185
+ * Note: Do not include ^ or $ anchors - ADK's LLMRegistry adds them automatically.
1186
+ *
1187
+ * @constant
1188
+ * @example
1189
+ * ```typescript
1190
+ * MODEL_PATTERNS[0].test("anthropic/claude-sonnet-4"); // true
1191
+ * MODEL_PATTERNS[0].test("invalid"); // false
1192
+ * ```
1193
+ */
1194
+ declare const MODEL_PATTERNS: (string | RegExp)[];
1195
+ /**
1196
+ * Default base URL for the OpenRouter API.
1197
+ *
1198
+ * @constant
1199
+ * @see {@link https://openrouter.ai/docs|OpenRouter Documentation}
1200
+ */
1201
+ declare const OPENROUTER_BASE_URL = "https://openrouter.ai/api/v1";
1202
+ /**
1203
+ * Model patterns for OpenRouter model validation.
1204
+ *
1205
+ * Uses the same "provider/model" format as AI Gateway.
1206
+ *
1207
+ * Note: Do not include ^ or $ anchors - ADK's LLMRegistry adds them automatically.
1208
+ *
1209
+ * @constant
1210
+ * @example
1211
+ * ```typescript
1212
+ * OPENROUTER_MODEL_PATTERNS[0].test("anthropic/claude-sonnet-4"); // true
1213
+ * ```
1214
+ */
1215
+ declare const OPENROUTER_MODEL_PATTERNS: (string | RegExp)[];
1216
+ /**
1217
+ * Unique identifiers for each provider.
1218
+ *
1219
+ * Used internally for configuration management and registry operations.
1220
+ *
1221
+ * @constant
1222
+ */
1223
+ declare const PROVIDER_IDS: {
1224
+ /** Identifier for the AI Gateway provider */
1225
+ readonly AI_GATEWAY: "ai-gateway";
1226
+ /** Identifier for the OpenRouter provider */
1227
+ readonly OPENROUTER: "openrouter";
1228
+ };
1229
+ /**
1230
+ * Mapping of provider identifiers to their configuration types.
1231
+ *
1232
+ * @internal
1233
+ */
1234
+ type ProviderConfigMap = {
1235
+ "ai-gateway": RegisterOptions;
1236
+ openrouter: OpenRouterRegisterOptions;
1237
+ openai: OpenAIRegisterOptions;
1238
+ xai: XAIRegisterOptions;
1239
+ anthropic: AnthropicRegisterOptions;
1240
+ };
1241
+ /**
1242
+ * Valid provider type identifiers.
1243
+ *
1244
+ * @internal
1245
+ */
1246
+ type ProviderType = keyof ProviderConfigMap;
1247
+ /**
1248
+ * Sets global configuration for a specific provider.
1249
+ *
1250
+ * This configuration is used as a fallback when creating LLM instances
1251
+ * without explicit configuration. Instance configuration always takes
1252
+ * precedence over global configuration.
1253
+ *
1254
+ * @param provider - The provider identifier ("ai-gateway" or "openrouter")
1255
+ * @param options - Configuration options for the provider
1256
+ *
1257
+ * @example
1258
+ * ```typescript
1259
+ * // Configure AI Gateway globally
1260
+ * setProviderConfig("ai-gateway", {
1261
+ * apiKey: "your-api-key",
1262
+ * baseURL: "https://custom-gateway.example.com/v1"
1263
+ * });
1264
+ * ```
1265
+ *
1266
+ * @example
1267
+ * ```typescript
1268
+ * // Configure OpenRouter with site attribution
1269
+ * setProviderConfig("openrouter", {
1270
+ * apiKey: "your-api-key",
1271
+ * siteUrl: "https://myapp.com",
1272
+ * appName: "My Application"
1273
+ * });
1274
+ * ```
1275
+ */
1276
+ declare function setProviderConfig<T extends ProviderType>(provider: T, options: ProviderConfigMap[T]): void;
1277
+ /**
1278
+ * Gets the current global configuration for a specific provider.
1279
+ *
1280
+ * @param provider - The provider identifier ("ai-gateway" or "openrouter")
1281
+ * @returns The current configuration, or `undefined` if not set
1282
+ *
1283
+ * @example
1284
+ * ```typescript
1285
+ * const config = getProviderConfig("ai-gateway");
1286
+ * if (config?.apiKey) {
1287
+ * console.log("AI Gateway is configured");
1288
+ * }
1289
+ * ```
1290
+ */
1291
+ declare function getProviderConfig<T extends ProviderType>(provider: T): Readonly<ProviderConfigMap[T]> | undefined;
1292
+ /**
1293
+ * Resets the global configuration for a specific provider.
1294
+ *
1295
+ * After calling this, the provider will fall back to environment variables
1296
+ * or default values.
1297
+ *
1298
+ * @param provider - The provider identifier ("ai-gateway" or "openrouter")
1299
+ *
1300
+ * @example
1301
+ * ```typescript
1302
+ * resetProviderConfig("ai-gateway");
1303
+ * ```
1304
+ */
1305
+ declare function resetProviderConfig(provider: ProviderType): void;
1306
+ /**
1307
+ * Resets all provider configurations.
1308
+ *
1309
+ * Useful for testing or when you need to clear all global state.
1310
+ *
1311
+ * @example
1312
+ * ```typescript
1313
+ * // In test teardown
1314
+ * afterEach(() => {
1315
+ * resetAllConfigs();
1316
+ * });
1317
+ * ```
1318
+ */
1319
+ declare function resetAllConfigs(): void;
1320
+ /**
1321
+ * Sets global configuration for AI Gateway.
1322
+ *
1323
+ * @param options - Configuration options
1324
+ *
1325
+ * @deprecated Use {@link setProviderConfig | setProviderConfig("ai-gateway", options)} instead.
1326
+ * This function will be removed in a future major version.
1327
+ *
1328
+ * @example
1329
+ * ```typescript
1330
+ * // Old way (deprecated)
1331
+ * setConfig({ apiKey: "..." });
1332
+ *
1333
+ * // New way
1334
+ * setProviderConfig("ai-gateway", { apiKey: "..." });
1335
+ * ```
1336
+ */
1337
+ declare function setConfig(options: RegisterOptions): void;
1338
+ /**
1339
+ * Gets global configuration for AI Gateway.
1340
+ *
1341
+ * @returns The current AI Gateway configuration
1342
+ *
1343
+ * @deprecated Use {@link getProviderConfig | getProviderConfig("ai-gateway")} instead.
1344
+ * This function will be removed in a future major version.
1345
+ */
1346
+ declare function getConfig(): Readonly<RegisterOptions>;
1347
+ /**
1348
+ * Resets global configuration for AI Gateway.
1349
+ *
1350
+ * @deprecated Use {@link resetProviderConfig | resetProviderConfig("ai-gateway")} instead.
1351
+ * This function will be removed in a future major version.
1352
+ */
1353
+ declare function resetConfig(): void;
1354
+ import { LlmRequest as LlmRequest5 } from "@google/adk";
1355
+ import OpenAI3 from "openai";
1356
+ /**
1357
+ * Result of converting an ADK LlmRequest to OpenAI format.
1358
+ *
1359
+ * Contains the converted messages array and optional tools array
1360
+ * ready for use with the OpenAI chat completions API.
1361
+ */
1362
+ interface ConvertedRequest {
1363
+ /**
1364
+ * Array of OpenAI-format chat messages.
1365
+ *
1366
+ * Includes system, user, assistant, and tool messages
1367
+ * converted from ADK Content objects.
1368
+ */
1369
+ messages: OpenAI3.ChatCompletionMessageParam[];
1370
+ /**
1371
+ * Array of OpenAI-format tool definitions.
1372
+ *
1373
+ * Converted from ADK function declarations with schema normalization.
1374
+ */
1375
+ tools?: OpenAI3.ChatCompletionTool[];
1376
+ }
1377
+ /**
1378
+ * Converts an ADK LlmRequest to OpenAI chat completion format.
1379
+ *
1380
+ * This function handles:
1381
+ * - System instruction extraction
1382
+ * - User and model message conversion
1383
+ * - Function call and response handling
1384
+ * - Tool/function declaration conversion
1385
+ * - Schema normalization (Gemini UPPERCASE to OpenAI lowercase types)
1386
+ *
1387
+ * @param llmRequest - The ADK LlmRequest to convert
1388
+ * @returns The converted request with messages and optional tools
1389
+ *
1390
+ * @example
1391
+ * ```typescript
1392
+ * import { convertRequest } from "adk-llm-bridge";
1393
+ *
1394
+ * const adkRequest: LlmRequest = {
1395
+ * contents: [{ role: "user", parts: [{ text: "Hello!" }] }],
1396
+ * config: { systemInstruction: "You are a helpful assistant." }
1397
+ * };
1398
+ *
1399
+ * const { messages, tools } = convertRequest(adkRequest);
1400
+ * // messages = [
1401
+ * // { role: "system", content: "You are a helpful assistant." },
1402
+ * // { role: "user", content: "Hello!" }
1403
+ * // ]
1404
+ * ```
1405
+ *
1406
+ * @example
1407
+ * ```typescript
1408
+ * // With tools/functions
1409
+ * const adkRequest: LlmRequest = {
1410
+ * contents: [...],
1411
+ * config: {
1412
+ * tools: [{
1413
+ * functionDeclarations: [{
1414
+ * name: "get_weather",
1415
+ * description: "Get current weather",
1416
+ * parameters: { type: "OBJECT", properties: { city: { type: "STRING" } } }
1417
+ * }]
1418
+ * }]
1419
+ * }
1420
+ * };
1421
+ *
1422
+ * const { messages, tools } = convertRequest(adkRequest);
1423
+ * // tools[0].function.parameters.type = "object" (normalized from "OBJECT")
1424
+ * ```
1425
+ */
1426
+ declare function convertRequest(llmRequest: LlmRequest5): ConvertedRequest;
1427
+ import { LlmResponse as LlmResponse6 } from "@google/adk";
1428
+ import OpenAI4 from "openai";
1429
+ /**
1430
+ * Converts an OpenAI chat completion response to ADK LlmResponse format.
1431
+ *
1432
+ * Handles:
1433
+ * - Text content extraction
1434
+ * - Tool/function call conversion
1435
+ * - Usage metadata mapping
1436
+ *
1437
+ * @param response - The OpenAI ChatCompletion response
1438
+ * @returns The converted ADK LlmResponse
1439
+ *
1440
+ * @example
1441
+ * ```typescript
1442
+ * import { convertResponse } from "adk-llm-bridge";
1443
+ *
1444
+ * const openaiResponse = await client.chat.completions.create({...});
1445
+ * const adkResponse = convertResponse(openaiResponse);
1446
+ *
1447
+ * if (adkResponse.content?.parts) {
1448
+ * for (const part of adkResponse.content.parts) {
1449
+ * if (part.text) console.log(part.text);
1450
+ * if (part.functionCall) console.log("Tool call:", part.functionCall.name);
1451
+ * }
1452
+ * }
1453
+ * ```
1454
+ */
1455
+ declare function convertResponse(response: OpenAI4.ChatCompletion): LlmResponse6;
1456
+ /**
1457
+ * Processes a streaming chunk and returns the appropriate response.
1458
+ *
1459
+ * This function accumulates partial data (text and tool calls) across
1460
+ * multiple chunks and returns:
1461
+ * - Partial responses for text content (streamed immediately)
1462
+ * - Complete responses when finish_reason is received
1463
+ *
1464
+ * Tool calls are accumulated and only returned in the final response
1465
+ * because their arguments arrive in fragments across multiple chunks.
1466
+ *
1467
+ * @param chunk - The OpenAI streaming chunk
1468
+ * @param acc - The stream accumulator for tracking partial data
1469
+ * @returns Object containing optional response and completion status
1470
+ *
1471
+ * @example
1472
+ * ```typescript
1473
+ * import { createStreamAccumulator, convertStreamChunk } from "adk-llm-bridge";
1474
+ *
1475
+ * const accumulator = createStreamAccumulator();
1476
+ *
1477
+ * for await (const chunk of stream) {
1478
+ * const { response, isComplete } = convertStreamChunk(chunk, accumulator);
1479
+ *
1480
+ * if (response?.content?.parts?.[0]?.text) {
1481
+ * // Stream text to user immediately
1482
+ * process.stdout.write(response.content.parts[0].text);
1483
+ * }
1484
+ *
1485
+ * if (isComplete) {
1486
+ * // Final response with complete tool calls
1487
+ * return response;
1488
+ * }
1489
+ * }
1490
+ * ```
1491
+ */
1492
+ declare function convertStreamChunk(chunk: OpenAI4.ChatCompletionChunk, acc: StreamAccumulator): StreamChunkResult;
1493
+ /**
1494
+ * Creates a new stream accumulator for tracking partial responses.
1495
+ *
1496
+ * The accumulator stores:
1497
+ * - Accumulated text content
1498
+ * - Partial tool call data (indexed by position)
1499
+ *
1500
+ * Use with {@link convertStreamChunk} to process streaming responses.
1501
+ *
1502
+ * @returns A fresh StreamAccumulator instance
1503
+ *
1504
+ * @example
1505
+ * ```typescript
1506
+ * const accumulator = createStreamAccumulator();
1507
+ *
1508
+ * for await (const chunk of stream) {
1509
+ * const result = convertStreamChunk(chunk, accumulator);
1510
+ * // accumulator state is updated automatically
1511
+ * }
1512
+ * ```
1513
+ */
1514
+ declare function createStreamAccumulator(): StreamAccumulator;
1515
+ export { setProviderConfig, setConfig, resetProviderConfig, resetConfig, resetAllConfigs, registerXAI, registerOpenRouter, registerOpenAI, registerAnthropic, registerAIGateway, isXAIRegistered, isOpenRouterRegistered, isOpenAIRegistered, isAnthropicRegistered, isAIGatewayRegistered, getProviderConfig, getConfig, createStreamAccumulator, createCustomLlm, convertStreamChunk, convertResponse, convertRequest, XAIRegisterOptions, XAIProviderConfig, XAILlm, XAI, ToolCallAccumulator, StreamChunkResult, StreamAccumulator, RegisterOptions, PROVIDER_IDS, OpenRouterRegisterOptions, OpenRouterProviderPreferences, OpenRouterLlm, OpenRouterConfig, OpenRouter, OpenAIRegisterOptions, OpenAIProviderConfig, OpenAILlm, OpenAICompatibleLlm, OpenAIClientConfig, OpenAI2 as OpenAI, OPENROUTER_MODEL_PATTERNS, OPENROUTER_BASE_URL, MODEL_PATTERNS, DEFAULT_BASE_URL, CustomLlmProviderConfig, CustomLlmConfig, CustomLlm, Custom, BaseProviderLlm, BaseProviderConfig, BaseLlm2 as BaseLlm, AnthropicRegisterOptions, AnthropicProviderConfig, AnthropicLlm, Anthropic3 as Anthropic, AIGatewayLlm, AIGatewayConfig, AIGateway };