@yourgpt/llm-sdk 2.6.0-beta.0 → 2.6.0-beta.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 (64) hide show
  1. package/README.md +189 -0
  2. package/dist/adapters/index.d.mts +11 -13
  3. package/dist/adapters/index.d.ts +11 -13
  4. package/dist/adapters/index.js +610 -186
  5. package/dist/adapters/index.mjs +610 -186
  6. package/dist/audio-BBSwnjo8.d.mts +92 -0
  7. package/dist/audio-BBSwnjo8.d.ts +92 -0
  8. package/dist/{base-tNgbBaSo.d.mts → base-BICaQaRS.d.mts} +8 -3
  9. package/dist/{base-C58Dsr9p.d.ts → base-CpRPB8u8.d.ts} +8 -3
  10. package/dist/fallback/index.d.mts +4 -4
  11. package/dist/fallback/index.d.ts +4 -4
  12. package/dist/fallback/index.js +75 -39
  13. package/dist/fallback/index.mjs +75 -39
  14. package/dist/index.d.mts +18 -35
  15. package/dist/index.d.ts +18 -35
  16. package/dist/index.js +304 -88
  17. package/dist/index.mjs +302 -89
  18. package/dist/providers/anthropic/index.d.mts +3 -3
  19. package/dist/providers/anthropic/index.d.ts +3 -3
  20. package/dist/providers/anthropic/index.js +123 -7
  21. package/dist/providers/anthropic/index.mjs +123 -7
  22. package/dist/providers/azure/index.d.mts +3 -3
  23. package/dist/providers/azure/index.d.ts +3 -3
  24. package/dist/providers/azure/index.js +101 -4
  25. package/dist/providers/azure/index.mjs +101 -4
  26. package/dist/providers/fireworks/index.d.mts +3 -3
  27. package/dist/providers/fireworks/index.d.ts +3 -3
  28. package/dist/providers/fireworks/index.js +559 -178
  29. package/dist/providers/fireworks/index.mjs +559 -178
  30. package/dist/providers/google/index.d.mts +3 -3
  31. package/dist/providers/google/index.d.ts +3 -3
  32. package/dist/providers/google/index.js +559 -178
  33. package/dist/providers/google/index.mjs +559 -178
  34. package/dist/providers/ollama/index.d.mts +4 -4
  35. package/dist/providers/ollama/index.d.ts +4 -4
  36. package/dist/providers/ollama/index.js +87 -2
  37. package/dist/providers/ollama/index.mjs +87 -2
  38. package/dist/providers/openai/index.d.mts +25 -23
  39. package/dist/providers/openai/index.d.ts +25 -23
  40. package/dist/providers/openai/index.js +1397 -481
  41. package/dist/providers/openai/index.mjs +1396 -482
  42. package/dist/providers/openrouter/index.d.mts +3 -3
  43. package/dist/providers/openrouter/index.d.ts +3 -3
  44. package/dist/providers/openrouter/index.js +559 -178
  45. package/dist/providers/openrouter/index.mjs +559 -178
  46. package/dist/providers/togetherai/index.d.mts +3 -3
  47. package/dist/providers/togetherai/index.d.ts +3 -3
  48. package/dist/providers/togetherai/index.js +559 -178
  49. package/dist/providers/togetherai/index.mjs +559 -178
  50. package/dist/providers/xai/index.d.mts +3 -3
  51. package/dist/providers/xai/index.d.ts +3 -3
  52. package/dist/providers/xai/index.js +559 -178
  53. package/dist/providers/xai/index.mjs +559 -178
  54. package/dist/{types-B6dhnguR.d.mts → types-C3RgRbEC.d.mts} +11 -4
  55. package/dist/{types-CCxPmkmK.d.ts → types-DCFqkJAy.d.ts} +11 -4
  56. package/dist/{types-BQ31QIsA.d.ts → types-DHQEMFHA.d.mts} +3 -11
  57. package/dist/{types-BkQCSiIt.d.mts → types-Dg1Bcg1k.d.mts} +127 -3
  58. package/dist/{types-BkQCSiIt.d.ts → types-Dg1Bcg1k.d.ts} +127 -3
  59. package/dist/{types-BSSiJW2o.d.mts → types-Dikj3En4.d.ts} +3 -11
  60. package/dist/yourgpt/index.d.mts +1 -1
  61. package/dist/yourgpt/index.d.ts +1 -1
  62. package/docs/multimodal-and-audio.md +170 -0
  63. package/package.json +117 -49
  64. package/LICENSE +0 -21
@@ -1,3 +1,26 @@
1
+ // src/core/provider-error.ts
2
+ function errorFromEvent(event) {
3
+ if (event.cause instanceof Error) return event.cause;
4
+ return Object.assign(new Error(event.message), {
5
+ status: event.status,
6
+ code: event.providerCode ?? event.code,
7
+ param: event.param,
8
+ request_id: event.requestId
9
+ });
10
+ }
11
+ function throwIfAborted(signal) {
12
+ if (!signal?.aborted) return;
13
+ const error = new Error("The operation was aborted");
14
+ error.name = "AbortError";
15
+ throw error;
16
+ }
17
+ function isAbortError(error) {
18
+ if (!(error instanceof Error)) return false;
19
+ return error.name === "AbortError" || error.constructor.name === "APIUserAbortError" || /^(aborted|the operation was aborted|the user aborted a request)/i.test(
20
+ error.message
21
+ );
22
+ }
23
+
1
24
  // src/fallback/errors.ts
2
25
  var FallbackExhaustedError = class extends Error {
3
26
  constructor(failures) {
@@ -72,8 +95,24 @@ function calcDelay(base, attempt, backoff) {
72
95
  if (backoff === "fixed") return base;
73
96
  return base * Math.pow(2, attempt - 1);
74
97
  }
75
- function sleep(ms) {
76
- return new Promise((resolve) => setTimeout(resolve, ms));
98
+ function sleep(ms, signal) {
99
+ throwIfAborted(signal);
100
+ return new Promise((resolve, reject) => {
101
+ const onAbort = () => {
102
+ clearTimeout(timer);
103
+ signal?.removeEventListener("abort", onAbort);
104
+ try {
105
+ throwIfAborted(signal);
106
+ } catch (error) {
107
+ reject(error);
108
+ }
109
+ };
110
+ const timer = setTimeout(() => {
111
+ signal?.removeEventListener("abort", onAbort);
112
+ resolve();
113
+ }, ms);
114
+ signal?.addEventListener("abort", onAbort, { once: true });
115
+ });
77
116
  }
78
117
  var FallbackChain = class {
79
118
  constructor(config) {
@@ -89,7 +128,8 @@ var FallbackChain = class {
89
128
  retryBackoff: config.retryBackoff ?? "exponential",
90
129
  onFallback: config.onFallback,
91
130
  onRetry: config.onRetry,
92
- retryableErrors: config.retryableErrors
131
+ retryableErrors: config.retryableErrors,
132
+ fallbackErrors: config.fallbackErrors
93
133
  };
94
134
  }
95
135
  get provider() {
@@ -109,8 +149,12 @@ var FallbackChain = class {
109
149
  await this._config.store.set(ROUND_ROBIN_KEY, next);
110
150
  }
111
151
  _isRetryable(error) {
152
+ if (isAbortError(error)) return false;
112
153
  return this._config.retryableErrors ? this._config.retryableErrors(error) : defaultIsRetryable(error);
113
154
  }
155
+ _canFallback(error) {
156
+ return !isAbortError(error) && (this._isRetryable(error) || this._config.fallbackErrors?.(error) === true);
157
+ }
114
158
  /**
115
159
  * Try streaming from a single adapter, with per-model retries.
116
160
  *
@@ -123,65 +167,52 @@ var FallbackChain = class {
123
167
  */
124
168
  async *_streamWithRetries(adapter, request, out) {
125
169
  const { retries, retryDelay, retryBackoff, onRetry } = this._config;
126
- const maxAttempts = retries + 1;
127
- for (let attempt = 1; attempt <= maxAttempts; attempt++) {
128
- let contentStarted = false;
129
- let failureError = null;
170
+ for (let attempt = 0; attempt <= retries; attempt++) {
171
+ throwIfAborted(request.signal);
172
+ out.retriesAttempted = attempt;
130
173
  try {
131
174
  for await (const chunk of adapter.stream(request)) {
132
- if (chunk.type === "error") {
133
- if (!contentStarted) {
134
- const msg = chunk.message ?? "Unknown error";
135
- failureError = new Error(msg);
136
- break;
137
- }
138
- yield chunk;
139
- return;
140
- }
175
+ throwIfAborted(request.signal);
176
+ if (chunk.type === "error") throw errorFromEvent(chunk);
141
177
  if (chunk.type === "message:start") continue;
142
- contentStarted = true;
178
+ out.contentStarted = true;
143
179
  yield chunk;
144
180
  }
181
+ throwIfAborted(request.signal);
182
+ return;
145
183
  } catch (error) {
146
- if (contentStarted) throw error;
147
- if (!this._isRetryable(error)) throw error;
148
- failureError = error instanceof Error ? error : new Error(String(error));
149
- }
150
- if (failureError === null) return;
151
- if (!this._isRetryable(failureError)) throw failureError;
152
- out.retriesAttempted = attempt - 1;
153
- if (attempt < maxAttempts) {
154
- const delayMs = calcDelay(retryDelay, attempt, retryBackoff);
155
- const retryInfo = {
184
+ throwIfAborted(request.signal);
185
+ if (out.contentStarted || !this._isRetryable(error) || attempt === retries)
186
+ throw error;
187
+ const delayMs = calcDelay(retryDelay, attempt + 1, retryBackoff);
188
+ onRetry?.({
156
189
  model: adapter.model,
157
190
  provider: adapter.provider,
158
- error: failureError,
159
- retryAttempt: attempt,
191
+ error: error instanceof Error ? error : new Error(String(error)),
192
+ retryAttempt: attempt + 1,
160
193
  maxRetries: retries,
161
194
  delayMs
162
- };
163
- onRetry?.(retryInfo);
164
- await sleep(delayMs);
165
- continue;
195
+ });
196
+ await sleep(delayMs, request.signal);
166
197
  }
167
- out.retriesAttempted = retries;
168
- throw failureError;
169
198
  }
170
199
  }
171
200
  async *stream(request) {
172
201
  const { models, onFallback } = this._config;
202
+ throwIfAborted(request.signal);
173
203
  const startIndex = await this._startIndex();
174
204
  const failures = [];
175
205
  for (let i = 0; i < models.length; i++) {
176
206
  const index = (startIndex + i) % models.length;
177
207
  const adapter = models[index];
178
- const out = { retriesAttempted: 0 };
208
+ const out = { retriesAttempted: 0, contentStarted: false };
179
209
  try {
180
210
  yield* this._streamWithRetries(adapter, request, out);
181
211
  await this._advanceIndex(index);
182
212
  return;
183
213
  } catch (error) {
184
- if (!this._isRetryable(error)) throw error;
214
+ throwIfAborted(request.signal);
215
+ if (out.contentStarted || !this._canFallback(error)) throw error;
185
216
  const failure = {
186
217
  model: adapter.model,
187
218
  provider: adapter.provider,
@@ -206,6 +237,7 @@ var FallbackChain = class {
206
237
  }
207
238
  async complete(request) {
208
239
  const { models, onFallback, retries, retryDelay, retryBackoff, onRetry } = this._config;
240
+ throwIfAborted(request.signal);
209
241
  const startIndex = await this._startIndex();
210
242
  const failures = [];
211
243
  for (let i = 0; i < models.length; i++) {
@@ -227,14 +259,18 @@ var FallbackChain = class {
227
259
  let lastError = null;
228
260
  let retriesAttempted = 0;
229
261
  for (let attempt = 1; attempt <= maxAttempts; attempt++) {
262
+ throwIfAborted(request.signal);
230
263
  try {
231
264
  const result = await adapter.complete(request);
265
+ throwIfAborted(request.signal);
232
266
  await this._advanceIndex(index);
233
267
  return result;
234
268
  } catch (error) {
235
- if (!this._isRetryable(error)) throw error;
269
+ throwIfAborted(request.signal);
270
+ if (!this._canFallback(error)) throw error;
236
271
  lastError = error instanceof Error ? error : new Error(String(error));
237
272
  retriesAttempted = attempt - 1;
273
+ if (!this._isRetryable(error)) break;
238
274
  if (attempt < maxAttempts) {
239
275
  const delayMs = calcDelay(retryDelay, attempt, retryBackoff);
240
276
  onRetry?.({
@@ -245,7 +281,7 @@ var FallbackChain = class {
245
281
  maxRetries: retries,
246
282
  delayMs
247
283
  });
248
- await sleep(delayMs);
284
+ await sleep(delayMs, request.signal);
249
285
  }
250
286
  }
251
287
  }
package/dist/index.d.mts CHANGED
@@ -1,13 +1,14 @@
1
- import { G as GenerateTextParams, a as GenerateTextResult, S as StreamTextParams, b as StreamTextResult, T as ToolContext, c as Tool, A as ActionDefinition, d as ToolDefinition, e as ToolProfile, K as KnowledgeBaseConfig, W as WebSearchConfig, f as StorageAdapter, R as ResponseFormat, D as DoneEventMessage, g as StreamEvent, h as ToolCallInfo, i as TokenUsageRaw, P as ProviderToolRuntimeOptions, M as Message, j as ToolResponse, k as StorageMessage } from './types-BkQCSiIt.mjs';
2
- export { a4 as AnthropicProviderToolOptions, a1 as AnthropicToolSelectionHints, p as AssistantMessage, a5 as Citation, C as CoreMessage, V as DEFAULT_CAPABILITIES, m as DoGenerateParams, n as DoGenerateResult, H as ErrorChunk, F as FilePart, E as FinishChunk, N as FinishReason, v as GenerateStep, I as ImagePart, X as LLMConfig, L as LanguageModel, l as ModelCapabilities, a3 as OpenAIProviderToolOptions, a0 as OpenAIToolSelectionHints, O as ResponseOptions, Q as StorageFile, x as StreamChunk, w as StreamPart, o as SystemMessage, y as TextDeltaChunk, s as TextPart, J as TokenUsage, t as ToolCall, z as ToolCallChunk, $ as ToolExecution, Y as ToolLocation, q as ToolMessage, a2 as ToolNativeProviderHints, u as ToolResult, B as ToolResultChunk, Z as UnifiedToolCall, _ as UnifiedToolResult, r as UserContentPart, U as UserMessage } from './types-BkQCSiIt.mjs';
1
+ import { G as GenerateTextParams, a as GenerateTextResult, S as StreamTextParams, b as StreamTextResult, T as ToolContext, c as ToolModelOutput, d as Tool, A as ActionDefinition, e as ToolDefinition, f as ToolProfile, K as KnowledgeBaseConfig, W as WebSearchConfig, g as StorageAdapter, P as ProviderMetadata, O as OpenAITransportOptions, h as OpenAIReasoningEffort, R as ResponseFormat, D as DoneEventMessage, i as StreamEvent, j as ToolCallInfo, k as TokenUsageRaw, l as ProviderToolRuntimeOptions, M as Message, m as ToolResponse, n as StorageMessage } from './types-Dg1Bcg1k.mjs';
2
+ export { a8 as AnthropicProviderToolOptions, a5 as AnthropicToolSelectionHints, s as AssistantMessage, a9 as Citation, C as CoreMessage, _ as DEFAULT_CAPABILITIES, p as DoGenerateParams, q as DoGenerateResult, Q as ErrorChunk, F as FilePart, N as FinishChunk, X as FinishReason, y as GenerateStep, I as ImagePart, $ as LLMConfig, L as LanguageModel, o as ModelCapabilities, a7 as OpenAIProviderToolOptions, ab as OpenAIResponseItem, ac as OpenAIResponseMetadata, aa as OpenAIResponsesOptions, a4 as OpenAIToolSelectionHints, Y as ResponseOptions, Z as StorageFile, B as StreamChunk, z as StreamPart, r as SystemMessage, E as TextDeltaChunk, v as TextPart, V as TokenUsage, w as ToolCall, H as ToolCallChunk, a3 as ToolExecution, a0 as ToolLocation, t as ToolMessage, a6 as ToolNativeProviderHints, ad as ToolOutputPart, x as ToolResult, J as ToolResultChunk, a1 as UnifiedToolCall, a2 as UnifiedToolResult, u as UserContentPart, U as UserMessage } from './types-Dg1Bcg1k.mjs';
3
3
  import { z } from 'zod';
4
- import { A as AIProvider } from './types-BSSiJW2o.mjs';
5
- export { a as AnthropicProviderConfig, f as AnthropicTool, h as AnthropicToolResult, g as AnthropicToolUse, b as AzureProviderConfig, B as BaseProviderConfig, m as GeminiFunctionCall, l as GeminiFunctionDeclaration, n as GeminiFunctionResponse, G as GoogleProviderConfig, d as OllamaModelOptions, c as OllamaProviderConfig, O as OpenAIProviderConfig, i as OpenAITool, j as OpenAIToolCall, k as OpenAIToolResult, P as ProviderCapabilities, e as ProviderFormatter, X as XAIProviderConfig } from './types-BSSiJW2o.mjs';
6
- import { L as LLMAdapter } from './base-tNgbBaSo.mjs';
7
- export { A as AdapterFactory, C as ChatCompletionRequest } from './base-tNgbBaSo.mjs';
4
+ import { A as AIProvider } from './types-DHQEMFHA.mjs';
5
+ export { a as AnthropicProviderConfig, f as AnthropicTool, h as AnthropicToolResult, g as AnthropicToolUse, b as AzureProviderConfig, B as BaseProviderConfig, m as GeminiFunctionCall, l as GeminiFunctionDeclaration, n as GeminiFunctionResponse, G as GoogleProviderConfig, d as OllamaModelOptions, c as OllamaProviderConfig, O as OpenAIProviderConfig, i as OpenAITool, j as OpenAIToolCall, k as OpenAIToolResult, P as ProviderCapabilities, e as ProviderFormatter, X as XAIProviderConfig } from './types-DHQEMFHA.mjs';
6
+ import { L as LLMAdapter } from './base-BICaQaRS.mjs';
7
+ export { A as AdapterFactory, C as ChatCompletionRequest } from './base-BICaQaRS.mjs';
8
8
  import * as hono from 'hono';
9
9
  import { Hono } from 'hono';
10
- export { F as FallbackChainConfig, c as FallbackFailure, d as FallbackInfo, b as RetryBackoff, e as RetryInfo, R as RoutingStore, a as RoutingStrategy } from './types-B6dhnguR.mjs';
10
+ export { F as FallbackChainConfig, c as FallbackFailure, d as FallbackInfo, b as RetryBackoff, e as RetryInfo, R as RoutingStore, a as RoutingStrategy } from './types-C3RgRbEC.mjs';
11
+ export { A as AudioAPIError, f as AudioResponseMetadata, h as AudioWarning, G as GeneratedSpeech, d as SpeechFormat, S as SpeechModel, a as SpeechOptions, e as TranscriptSegment, T as TranscriptionModel, b as TranscriptionOptions, c as TranscriptionResult, g as generateSpeech, t as transcribe } from './audio-BBSwnjo8.mjs';
11
12
 
12
13
  /**
13
14
  * generateText - Generate text using a language model
@@ -77,30 +78,6 @@ declare function generateText(params: GenerateTextParams): Promise<GenerateTextR
77
78
  */
78
79
  declare function streamText(params: StreamTextParams): Promise<StreamTextResult>;
79
80
 
80
- /**
81
- * Tool Helper
82
- *
83
- * Create type-safe tools with Zod schema validation.
84
- *
85
- * @example
86
- * ```ts
87
- * import { tool } from '@yourgpt/llm-sdk';
88
- * import { z } from 'zod';
89
- *
90
- * const weatherTool = tool({
91
- * description: 'Get current weather for a city',
92
- * parameters: z.object({
93
- * city: z.string().describe('City name'),
94
- * unit: z.enum(['celsius', 'fahrenheit']).optional(),
95
- * }),
96
- * execute: async ({ city, unit }) => {
97
- * const data = await fetchWeather(city);
98
- * return { temperature: data.temp, condition: data.condition };
99
- * },
100
- * });
101
- * ```
102
- */
103
-
104
81
  /**
105
82
  * Configuration for creating a tool
106
83
  */
@@ -111,6 +88,8 @@ interface ToolConfig<TParams extends z.ZodType, TResult = unknown> {
111
88
  parameters: TParams;
112
89
  /** Function to execute when the tool is called */
113
90
  execute: (params: z.infer<TParams>, context: ToolContext) => Promise<TResult>;
91
+ /** Map the raw result to explicit text/image content for the model. */
92
+ toModelOutput?: (result: TResult) => ToolModelOutput | undefined;
114
93
  /**
115
94
  * Hide this tool's execution from the chat UI.
116
95
  * When true, tool calls and results won't be displayed to the user,
@@ -149,7 +128,7 @@ declare function tool<TParams extends z.ZodType, TResult = unknown>(config: Tool
149
128
  /**
150
129
  * Format tools for OpenAI API format
151
130
  */
152
- declare function formatToolsForOpenAI(tools: Record<string, Tool>): Array<{
131
+ declare function formatToolsForOpenAI(tools: Record<string, Tool<any, any>>): Array<{
153
132
  type: "function";
154
133
  function: {
155
134
  name: string;
@@ -160,7 +139,7 @@ declare function formatToolsForOpenAI(tools: Record<string, Tool>): Array<{
160
139
  /**
161
140
  * Format tools for Anthropic API format
162
141
  */
163
- declare function formatToolsForAnthropic(tools: Record<string, Tool>): Array<{
142
+ declare function formatToolsForAnthropic(tools: Record<string, Tool<any, any>>): Array<{
164
143
  name: string;
165
144
  description: string;
166
145
  input_schema: object;
@@ -168,7 +147,7 @@ declare function formatToolsForAnthropic(tools: Record<string, Tool>): Array<{
168
147
  /**
169
148
  * Format tools for Google Gemini API format
170
149
  */
171
- declare function formatToolsForGoogle(tools: Record<string, Tool>): Array<{
150
+ declare function formatToolsForGoogle(tools: Record<string, Tool<any, any>>): Array<{
172
151
  functionDeclarations: Array<{
173
152
  name: string;
174
153
  description: string;
@@ -364,6 +343,8 @@ interface MessageAttachment {
364
343
  interface ChatRequest {
365
344
  /** Conversation messages */
366
345
  messages: Array<{
346
+ modelOutput?: ToolModelOutput;
347
+ providerMetadata?: ProviderMetadata;
367
348
  role: string;
368
349
  content: string;
369
350
  /** Attachments like images (for vision support) */
@@ -386,6 +367,8 @@ interface ChatRequest {
386
367
  botId?: string;
387
368
  /** LLM config overrides */
388
369
  config?: {
370
+ openai?: OpenAITransportOptions;
371
+ reasoningEffort?: OpenAIReasoningEffort;
389
372
  temperature?: number;
390
373
  maxTokens?: number;
391
374
  responseFormat?: ResponseFormat;
@@ -1537,4 +1520,4 @@ declare function generateThreadId(): string;
1537
1520
  */
1538
1521
  declare function generateToolCallId(): string;
1539
1522
 
1540
- export { AIProvider, ActionDefinition, type ActionRequest, type AgentLoopOptions, type ChatRequest, type CollectedResult, type CopilotChatResponse, DEFAULT_MAX_ITERATIONS, DoneEventMessage, GenerateResult, type GenerateResultData, GenerateTextParams, GenerateTextResult, LLMAdapter, Message, ProviderToolRuntimeOptions, type RequestContext, Runtime, type RuntimeConfig, StorageAdapter, StorageMessage, StreamEvent, StreamResult, type StreamResultOptions, StreamTextParams, StreamTextResult, TokenUsageRaw, Tool, ToolCallInfo, ToolContext, ToolDefinition, ToolProfile, ToolResponse, type ToolSearchMatch, WebSearchConfig, buildProviderToolOptions, createEventStream, createExpressHandler, createExpressMiddleware, createHonoApp, createNextHandler, createNodeHandler, createRuntime, createSSEHeaders, createSSEResponse, createStreamResult, createTextStreamHeaders, createTextStreamResponse, extractInputMessages, formatSSEData, formatToolsForAnthropic, formatToolsForGoogle, formatToolsForOpenAI, generateMessageId, generateText, generateThreadId, generateToolCallId, mapOutputMessages, pipeSSEToResponse, pipeTextToResponse, runAgentLoop, searchTools, selectTools, shouldExposeToolSearch, streamText, tool };
1523
+ export { AIProvider, ActionDefinition, type ActionRequest, type AgentLoopOptions, type ChatRequest, type CollectedResult, type CopilotChatResponse, DEFAULT_MAX_ITERATIONS, DoneEventMessage, GenerateResult, type GenerateResultData, GenerateTextParams, GenerateTextResult, LLMAdapter, Message, OpenAITransportOptions, ProviderMetadata, ProviderToolRuntimeOptions, type RequestContext, Runtime, type RuntimeConfig, StorageAdapter, StorageMessage, StreamEvent, StreamResult, type StreamResultOptions, StreamTextParams, StreamTextResult, TokenUsageRaw, Tool, ToolCallInfo, ToolContext, ToolDefinition, ToolModelOutput, ToolProfile, ToolResponse, type ToolSearchMatch, WebSearchConfig, buildProviderToolOptions, createEventStream, createExpressHandler, createExpressMiddleware, createHonoApp, createNextHandler, createNodeHandler, createRuntime, createSSEHeaders, createSSEResponse, createStreamResult, createTextStreamHeaders, createTextStreamResponse, extractInputMessages, formatSSEData, formatToolsForAnthropic, formatToolsForGoogle, formatToolsForOpenAI, generateMessageId, generateText, generateThreadId, generateToolCallId, mapOutputMessages, pipeSSEToResponse, pipeTextToResponse, runAgentLoop, searchTools, selectTools, shouldExposeToolSearch, streamText, tool };
package/dist/index.d.ts CHANGED
@@ -1,13 +1,14 @@
1
- import { G as GenerateTextParams, a as GenerateTextResult, S as StreamTextParams, b as StreamTextResult, T as ToolContext, c as Tool, A as ActionDefinition, d as ToolDefinition, e as ToolProfile, K as KnowledgeBaseConfig, W as WebSearchConfig, f as StorageAdapter, R as ResponseFormat, D as DoneEventMessage, g as StreamEvent, h as ToolCallInfo, i as TokenUsageRaw, P as ProviderToolRuntimeOptions, M as Message, j as ToolResponse, k as StorageMessage } from './types-BkQCSiIt.js';
2
- export { a4 as AnthropicProviderToolOptions, a1 as AnthropicToolSelectionHints, p as AssistantMessage, a5 as Citation, C as CoreMessage, V as DEFAULT_CAPABILITIES, m as DoGenerateParams, n as DoGenerateResult, H as ErrorChunk, F as FilePart, E as FinishChunk, N as FinishReason, v as GenerateStep, I as ImagePart, X as LLMConfig, L as LanguageModel, l as ModelCapabilities, a3 as OpenAIProviderToolOptions, a0 as OpenAIToolSelectionHints, O as ResponseOptions, Q as StorageFile, x as StreamChunk, w as StreamPart, o as SystemMessage, y as TextDeltaChunk, s as TextPart, J as TokenUsage, t as ToolCall, z as ToolCallChunk, $ as ToolExecution, Y as ToolLocation, q as ToolMessage, a2 as ToolNativeProviderHints, u as ToolResult, B as ToolResultChunk, Z as UnifiedToolCall, _ as UnifiedToolResult, r as UserContentPart, U as UserMessage } from './types-BkQCSiIt.js';
1
+ import { G as GenerateTextParams, a as GenerateTextResult, S as StreamTextParams, b as StreamTextResult, T as ToolContext, c as ToolModelOutput, d as Tool, A as ActionDefinition, e as ToolDefinition, f as ToolProfile, K as KnowledgeBaseConfig, W as WebSearchConfig, g as StorageAdapter, P as ProviderMetadata, O as OpenAITransportOptions, h as OpenAIReasoningEffort, R as ResponseFormat, D as DoneEventMessage, i as StreamEvent, j as ToolCallInfo, k as TokenUsageRaw, l as ProviderToolRuntimeOptions, M as Message, m as ToolResponse, n as StorageMessage } from './types-Dg1Bcg1k.js';
2
+ export { a8 as AnthropicProviderToolOptions, a5 as AnthropicToolSelectionHints, s as AssistantMessage, a9 as Citation, C as CoreMessage, _ as DEFAULT_CAPABILITIES, p as DoGenerateParams, q as DoGenerateResult, Q as ErrorChunk, F as FilePart, N as FinishChunk, X as FinishReason, y as GenerateStep, I as ImagePart, $ as LLMConfig, L as LanguageModel, o as ModelCapabilities, a7 as OpenAIProviderToolOptions, ab as OpenAIResponseItem, ac as OpenAIResponseMetadata, aa as OpenAIResponsesOptions, a4 as OpenAIToolSelectionHints, Y as ResponseOptions, Z as StorageFile, B as StreamChunk, z as StreamPart, r as SystemMessage, E as TextDeltaChunk, v as TextPart, V as TokenUsage, w as ToolCall, H as ToolCallChunk, a3 as ToolExecution, a0 as ToolLocation, t as ToolMessage, a6 as ToolNativeProviderHints, ad as ToolOutputPart, x as ToolResult, J as ToolResultChunk, a1 as UnifiedToolCall, a2 as UnifiedToolResult, u as UserContentPart, U as UserMessage } from './types-Dg1Bcg1k.js';
3
3
  import { z } from 'zod';
4
- import { A as AIProvider } from './types-BQ31QIsA.js';
5
- export { a as AnthropicProviderConfig, f as AnthropicTool, h as AnthropicToolResult, g as AnthropicToolUse, b as AzureProviderConfig, B as BaseProviderConfig, m as GeminiFunctionCall, l as GeminiFunctionDeclaration, n as GeminiFunctionResponse, G as GoogleProviderConfig, d as OllamaModelOptions, c as OllamaProviderConfig, O as OpenAIProviderConfig, i as OpenAITool, j as OpenAIToolCall, k as OpenAIToolResult, P as ProviderCapabilities, e as ProviderFormatter, X as XAIProviderConfig } from './types-BQ31QIsA.js';
6
- import { L as LLMAdapter } from './base-C58Dsr9p.js';
7
- export { A as AdapterFactory, C as ChatCompletionRequest } from './base-C58Dsr9p.js';
4
+ import { A as AIProvider } from './types-Dikj3En4.js';
5
+ export { a as AnthropicProviderConfig, f as AnthropicTool, h as AnthropicToolResult, g as AnthropicToolUse, b as AzureProviderConfig, B as BaseProviderConfig, m as GeminiFunctionCall, l as GeminiFunctionDeclaration, n as GeminiFunctionResponse, G as GoogleProviderConfig, d as OllamaModelOptions, c as OllamaProviderConfig, O as OpenAIProviderConfig, i as OpenAITool, j as OpenAIToolCall, k as OpenAIToolResult, P as ProviderCapabilities, e as ProviderFormatter, X as XAIProviderConfig } from './types-Dikj3En4.js';
6
+ import { L as LLMAdapter } from './base-CpRPB8u8.js';
7
+ export { A as AdapterFactory, C as ChatCompletionRequest } from './base-CpRPB8u8.js';
8
8
  import * as hono from 'hono';
9
9
  import { Hono } from 'hono';
10
- export { F as FallbackChainConfig, c as FallbackFailure, d as FallbackInfo, b as RetryBackoff, e as RetryInfo, R as RoutingStore, a as RoutingStrategy } from './types-CCxPmkmK.js';
10
+ export { F as FallbackChainConfig, c as FallbackFailure, d as FallbackInfo, b as RetryBackoff, e as RetryInfo, R as RoutingStore, a as RoutingStrategy } from './types-DCFqkJAy.js';
11
+ export { A as AudioAPIError, f as AudioResponseMetadata, h as AudioWarning, G as GeneratedSpeech, d as SpeechFormat, S as SpeechModel, a as SpeechOptions, e as TranscriptSegment, T as TranscriptionModel, b as TranscriptionOptions, c as TranscriptionResult, g as generateSpeech, t as transcribe } from './audio-BBSwnjo8.js';
11
12
 
12
13
  /**
13
14
  * generateText - Generate text using a language model
@@ -77,30 +78,6 @@ declare function generateText(params: GenerateTextParams): Promise<GenerateTextR
77
78
  */
78
79
  declare function streamText(params: StreamTextParams): Promise<StreamTextResult>;
79
80
 
80
- /**
81
- * Tool Helper
82
- *
83
- * Create type-safe tools with Zod schema validation.
84
- *
85
- * @example
86
- * ```ts
87
- * import { tool } from '@yourgpt/llm-sdk';
88
- * import { z } from 'zod';
89
- *
90
- * const weatherTool = tool({
91
- * description: 'Get current weather for a city',
92
- * parameters: z.object({
93
- * city: z.string().describe('City name'),
94
- * unit: z.enum(['celsius', 'fahrenheit']).optional(),
95
- * }),
96
- * execute: async ({ city, unit }) => {
97
- * const data = await fetchWeather(city);
98
- * return { temperature: data.temp, condition: data.condition };
99
- * },
100
- * });
101
- * ```
102
- */
103
-
104
81
  /**
105
82
  * Configuration for creating a tool
106
83
  */
@@ -111,6 +88,8 @@ interface ToolConfig<TParams extends z.ZodType, TResult = unknown> {
111
88
  parameters: TParams;
112
89
  /** Function to execute when the tool is called */
113
90
  execute: (params: z.infer<TParams>, context: ToolContext) => Promise<TResult>;
91
+ /** Map the raw result to explicit text/image content for the model. */
92
+ toModelOutput?: (result: TResult) => ToolModelOutput | undefined;
114
93
  /**
115
94
  * Hide this tool's execution from the chat UI.
116
95
  * When true, tool calls and results won't be displayed to the user,
@@ -149,7 +128,7 @@ declare function tool<TParams extends z.ZodType, TResult = unknown>(config: Tool
149
128
  /**
150
129
  * Format tools for OpenAI API format
151
130
  */
152
- declare function formatToolsForOpenAI(tools: Record<string, Tool>): Array<{
131
+ declare function formatToolsForOpenAI(tools: Record<string, Tool<any, any>>): Array<{
153
132
  type: "function";
154
133
  function: {
155
134
  name: string;
@@ -160,7 +139,7 @@ declare function formatToolsForOpenAI(tools: Record<string, Tool>): Array<{
160
139
  /**
161
140
  * Format tools for Anthropic API format
162
141
  */
163
- declare function formatToolsForAnthropic(tools: Record<string, Tool>): Array<{
142
+ declare function formatToolsForAnthropic(tools: Record<string, Tool<any, any>>): Array<{
164
143
  name: string;
165
144
  description: string;
166
145
  input_schema: object;
@@ -168,7 +147,7 @@ declare function formatToolsForAnthropic(tools: Record<string, Tool>): Array<{
168
147
  /**
169
148
  * Format tools for Google Gemini API format
170
149
  */
171
- declare function formatToolsForGoogle(tools: Record<string, Tool>): Array<{
150
+ declare function formatToolsForGoogle(tools: Record<string, Tool<any, any>>): Array<{
172
151
  functionDeclarations: Array<{
173
152
  name: string;
174
153
  description: string;
@@ -364,6 +343,8 @@ interface MessageAttachment {
364
343
  interface ChatRequest {
365
344
  /** Conversation messages */
366
345
  messages: Array<{
346
+ modelOutput?: ToolModelOutput;
347
+ providerMetadata?: ProviderMetadata;
367
348
  role: string;
368
349
  content: string;
369
350
  /** Attachments like images (for vision support) */
@@ -386,6 +367,8 @@ interface ChatRequest {
386
367
  botId?: string;
387
368
  /** LLM config overrides */
388
369
  config?: {
370
+ openai?: OpenAITransportOptions;
371
+ reasoningEffort?: OpenAIReasoningEffort;
389
372
  temperature?: number;
390
373
  maxTokens?: number;
391
374
  responseFormat?: ResponseFormat;
@@ -1537,4 +1520,4 @@ declare function generateThreadId(): string;
1537
1520
  */
1538
1521
  declare function generateToolCallId(): string;
1539
1522
 
1540
- export { AIProvider, ActionDefinition, type ActionRequest, type AgentLoopOptions, type ChatRequest, type CollectedResult, type CopilotChatResponse, DEFAULT_MAX_ITERATIONS, DoneEventMessage, GenerateResult, type GenerateResultData, GenerateTextParams, GenerateTextResult, LLMAdapter, Message, ProviderToolRuntimeOptions, type RequestContext, Runtime, type RuntimeConfig, StorageAdapter, StorageMessage, StreamEvent, StreamResult, type StreamResultOptions, StreamTextParams, StreamTextResult, TokenUsageRaw, Tool, ToolCallInfo, ToolContext, ToolDefinition, ToolProfile, ToolResponse, type ToolSearchMatch, WebSearchConfig, buildProviderToolOptions, createEventStream, createExpressHandler, createExpressMiddleware, createHonoApp, createNextHandler, createNodeHandler, createRuntime, createSSEHeaders, createSSEResponse, createStreamResult, createTextStreamHeaders, createTextStreamResponse, extractInputMessages, formatSSEData, formatToolsForAnthropic, formatToolsForGoogle, formatToolsForOpenAI, generateMessageId, generateText, generateThreadId, generateToolCallId, mapOutputMessages, pipeSSEToResponse, pipeTextToResponse, runAgentLoop, searchTools, selectTools, shouldExposeToolSearch, streamText, tool };
1523
+ export { AIProvider, ActionDefinition, type ActionRequest, type AgentLoopOptions, type ChatRequest, type CollectedResult, type CopilotChatResponse, DEFAULT_MAX_ITERATIONS, DoneEventMessage, GenerateResult, type GenerateResultData, GenerateTextParams, GenerateTextResult, LLMAdapter, Message, OpenAITransportOptions, ProviderMetadata, ProviderToolRuntimeOptions, type RequestContext, Runtime, type RuntimeConfig, StorageAdapter, StorageMessage, StreamEvent, StreamResult, type StreamResultOptions, StreamTextParams, StreamTextResult, TokenUsageRaw, Tool, ToolCallInfo, ToolContext, ToolDefinition, ToolModelOutput, ToolProfile, ToolResponse, type ToolSearchMatch, WebSearchConfig, buildProviderToolOptions, createEventStream, createExpressHandler, createExpressMiddleware, createHonoApp, createNextHandler, createNodeHandler, createRuntime, createSSEHeaders, createSSEResponse, createStreamResult, createTextStreamHeaders, createTextStreamResponse, extractInputMessages, formatSSEData, formatToolsForAnthropic, formatToolsForGoogle, formatToolsForOpenAI, generateMessageId, generateText, generateThreadId, generateToolCallId, mapOutputMessages, pipeSSEToResponse, pipeTextToResponse, runAgentLoop, searchTools, selectTools, shouldExposeToolSearch, streamText, tool };