@yourgpt/llm-sdk 2.5.0 → 2.5.1-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/README.md +19 -1
  2. package/dist/adapters/index.d.mts +4 -4
  3. package/dist/adapters/index.d.ts +4 -4
  4. package/dist/adapters/index.js +293 -23
  5. package/dist/adapters/index.mjs +293 -23
  6. package/dist/base-BYQKp9TW.d.mts +263 -0
  7. package/dist/base-Cxq3ni0t.d.ts +263 -0
  8. package/dist/fallback/index.d.mts +4 -4
  9. package/dist/fallback/index.d.ts +4 -4
  10. package/dist/index.d.mts +61 -8
  11. package/dist/index.d.ts +61 -8
  12. package/dist/index.js +71 -0
  13. package/dist/index.mjs +71 -0
  14. package/dist/providers/anthropic/index.d.mts +3 -3
  15. package/dist/providers/anthropic/index.d.ts +3 -3
  16. package/dist/providers/anthropic/index.js +360 -203
  17. package/dist/providers/anthropic/index.mjs +360 -203
  18. package/dist/providers/azure/index.d.mts +3 -3
  19. package/dist/providers/azure/index.d.ts +3 -3
  20. package/dist/providers/azure/index.js +49 -1
  21. package/dist/providers/azure/index.mjs +49 -1
  22. package/dist/providers/fireworks/index.d.mts +1 -1
  23. package/dist/providers/fireworks/index.d.ts +1 -1
  24. package/dist/providers/fireworks/index.js +56 -0
  25. package/dist/providers/fireworks/index.mjs +56 -0
  26. package/dist/providers/google/index.d.mts +3 -3
  27. package/dist/providers/google/index.d.ts +3 -3
  28. package/dist/providers/google/index.js +303 -207
  29. package/dist/providers/google/index.mjs +303 -207
  30. package/dist/providers/ollama/index.d.mts +4 -4
  31. package/dist/providers/ollama/index.d.ts +4 -4
  32. package/dist/providers/ollama/index.js +10 -2
  33. package/dist/providers/ollama/index.mjs +10 -2
  34. package/dist/providers/openai/index.d.mts +3 -3
  35. package/dist/providers/openai/index.d.ts +3 -3
  36. package/dist/providers/openai/index.js +318 -216
  37. package/dist/providers/openai/index.mjs +318 -216
  38. package/dist/providers/openrouter/index.d.mts +3 -3
  39. package/dist/providers/openrouter/index.d.ts +3 -3
  40. package/dist/providers/openrouter/index.js +308 -206
  41. package/dist/providers/openrouter/index.mjs +308 -206
  42. package/dist/providers/togetherai/index.d.mts +3 -3
  43. package/dist/providers/togetherai/index.d.ts +3 -3
  44. package/dist/providers/togetherai/index.js +308 -206
  45. package/dist/providers/togetherai/index.mjs +308 -206
  46. package/dist/providers/xai/index.d.mts +3 -3
  47. package/dist/providers/xai/index.d.ts +3 -3
  48. package/dist/providers/xai/index.js +307 -210
  49. package/dist/providers/xai/index.mjs +307 -210
  50. package/dist/{types-BctsnC3g.d.ts → types-BvkiJ1dd.d.mts} +2 -1
  51. package/dist/{types-38yolWJn.d.ts → types-ChORafYS.d.ts} +1 -1
  52. package/dist/types-D774b0dg.d.mts +1018 -0
  53. package/dist/types-D774b0dg.d.ts +1018 -0
  54. package/dist/{types-DRqxMIjF.d.mts → types-TMilS-Dz.d.ts} +2 -1
  55. package/dist/{types-D4YfrQJR.d.mts → types-mwMhCwOq.d.mts} +1 -1
  56. package/dist/yourgpt/index.d.mts +1 -1
  57. package/dist/yourgpt/index.d.ts +1 -1
  58. package/package.json +1 -1
  59. package/dist/base-D-U61JaB.d.mts +0 -788
  60. package/dist/base-iGi9Va6Z.d.ts +0 -788
  61. package/dist/types-CR8mi9I0.d.mts +0 -417
  62. package/dist/types-CR8mi9I0.d.ts +0 -417
@@ -0,0 +1,1018 @@
1
+ import { z } from 'zod';
2
+
3
+ /**
4
+ * Stream event types for llm-sdk
5
+ * These types are used internally by the SDK for streaming responses
6
+ */
7
+ /**
8
+ * Stream event types
9
+ */
10
+ type StreamEventType = "message:start" | "message:delta" | "message:end" | "thinking:start" | "thinking:delta" | "thinking:end" | "action:start" | "action:args" | "action:end" | "tool_calls" | "tool:result" | "citation" | "loop:iteration" | "loop:complete" | "error" | "thread:created" | "done";
11
+ /**
12
+ * Base event interface
13
+ */
14
+ interface BaseEvent {
15
+ type: StreamEventType;
16
+ }
17
+ /**
18
+ * Message started streaming
19
+ */
20
+ interface MessageStartEvent extends BaseEvent {
21
+ type: "message:start";
22
+ id: string;
23
+ }
24
+ /**
25
+ * Message content delta (incremental update)
26
+ */
27
+ interface MessageDeltaEvent extends BaseEvent {
28
+ type: "message:delta";
29
+ content: string;
30
+ }
31
+ /**
32
+ * Message finished streaming
33
+ */
34
+ interface MessageEndEvent extends BaseEvent {
35
+ type: "message:end";
36
+ }
37
+ /**
38
+ * Thinking/reasoning started (for models like Claude, DeepSeek)
39
+ */
40
+ interface ThinkingStartEvent extends BaseEvent {
41
+ type: "thinking:start";
42
+ }
43
+ /**
44
+ * Thinking content delta
45
+ */
46
+ interface ThinkingDeltaEvent extends BaseEvent {
47
+ type: "thinking:delta";
48
+ content: string;
49
+ }
50
+ /**
51
+ * Thinking finished
52
+ */
53
+ interface ThinkingEndEvent extends BaseEvent {
54
+ type: "thinking:end";
55
+ }
56
+ /**
57
+ * Action/tool execution started
58
+ */
59
+ interface ActionStartEvent extends BaseEvent {
60
+ type: "action:start";
61
+ id: string;
62
+ name: string;
63
+ /** Whether this tool should be hidden from UI */
64
+ hidden?: boolean;
65
+ /** Provider-specific metadata (e.g. Gemini 3 thought_signature) */
66
+ extra_content?: Record<string, unknown>;
67
+ }
68
+ /**
69
+ * Action arguments (streaming)
70
+ */
71
+ interface ActionArgsEvent extends BaseEvent {
72
+ type: "action:args";
73
+ id: string;
74
+ args: string;
75
+ }
76
+ /**
77
+ * Action execution completed
78
+ */
79
+ interface ActionEndEvent extends BaseEvent {
80
+ type: "action:end";
81
+ id: string;
82
+ name?: string;
83
+ result?: unknown;
84
+ error?: string;
85
+ }
86
+ /**
87
+ * Error event
88
+ */
89
+ interface ErrorEvent extends BaseEvent {
90
+ type: "error";
91
+ message: string;
92
+ code?: string;
93
+ }
94
+ /**
95
+ * Tool call information
96
+ */
97
+ interface ToolCallInfo {
98
+ id: string;
99
+ name: string;
100
+ args: Record<string, unknown>;
101
+ /** Whether this tool should be hidden from UI */
102
+ hidden?: boolean;
103
+ /** Provider-specific metadata (e.g. Gemini 3 thought_signature) */
104
+ extra_content?: Record<string, unknown>;
105
+ }
106
+ /**
107
+ * Assistant message with tool calls
108
+ */
109
+ interface AssistantToolMessage {
110
+ role: "assistant";
111
+ content: string | null;
112
+ tool_calls: Array<{
113
+ id: string;
114
+ type: "function";
115
+ function: {
116
+ name: string;
117
+ arguments: string;
118
+ };
119
+ /** Provider-specific metadata (e.g. Gemini 3 thought_signature) */
120
+ extra_content?: Record<string, unknown>;
121
+ }>;
122
+ }
123
+ /**
124
+ * Tool calls event - client should execute and send results
125
+ */
126
+ interface ToolCallsEvent extends BaseEvent {
127
+ type: "tool_calls";
128
+ toolCalls: ToolCallInfo[];
129
+ assistantMessage: AssistantToolMessage;
130
+ }
131
+ /**
132
+ * Tool result event
133
+ */
134
+ interface ToolResultEvent extends BaseEvent {
135
+ type: "tool:result";
136
+ id: string;
137
+ name: string;
138
+ result: ToolResponse;
139
+ }
140
+ /**
141
+ * Loop iteration event
142
+ */
143
+ interface LoopIterationEvent extends BaseEvent {
144
+ type: "loop:iteration";
145
+ iteration: number;
146
+ maxIterations: number;
147
+ }
148
+ /**
149
+ * Loop complete event
150
+ */
151
+ interface LoopCompleteEvent extends BaseEvent {
152
+ type: "loop:complete";
153
+ iterations: number;
154
+ aborted?: boolean;
155
+ maxIterationsReached?: boolean;
156
+ }
157
+ /**
158
+ * Citation from web search (unified format for all providers)
159
+ */
160
+ interface Citation {
161
+ /** Unique citation index (1-based) */
162
+ index: number;
163
+ /** Source URL */
164
+ url: string;
165
+ /** Page title */
166
+ title: string;
167
+ /** Cited text snippet (optional) */
168
+ citedText?: string;
169
+ /** Source domain (extracted from URL) */
170
+ domain?: string;
171
+ /** Favicon URL (generated from domain) */
172
+ favicon?: string;
173
+ }
174
+ /**
175
+ * Citation event - web search returned citations
176
+ */
177
+ interface CitationEvent extends BaseEvent {
178
+ type: "citation";
179
+ citations: Citation[];
180
+ }
181
+ /**
182
+ * Message format for done event (API format with snake_case)
183
+ */
184
+ interface DoneEventMessage {
185
+ role: "assistant" | "tool";
186
+ content: string | null;
187
+ tool_calls?: Array<{
188
+ id: string;
189
+ type: "function";
190
+ function: {
191
+ name: string;
192
+ arguments: string;
193
+ };
194
+ /** Provider-specific metadata (e.g. Gemini 3 thought_signature) */
195
+ extra_content?: Record<string, unknown>;
196
+ }>;
197
+ tool_call_id?: string;
198
+ }
199
+ /**
200
+ * Token usage (snake_case for API compatibility)
201
+ */
202
+ interface TokenUsageRaw {
203
+ prompt_tokens: number;
204
+ completion_tokens: number;
205
+ total_tokens?: number;
206
+ }
207
+ /**
208
+ * Thread/session created — emitted early in the stream, before any message events,
209
+ * so the client can adopt the threadId without waiting for the done chunk.
210
+ */
211
+ interface ThreadCreatedEvent extends BaseEvent {
212
+ type: "thread:created";
213
+ threadId: string;
214
+ }
215
+ /**
216
+ * Stream completed
217
+ */
218
+ interface DoneEvent extends BaseEvent {
219
+ type: "done";
220
+ requiresAction?: boolean;
221
+ messages?: DoneEventMessage[];
222
+ /** Token usage (server-side only, stripped before sending to client) */
223
+ usage?: TokenUsageRaw;
224
+ /** Session ID — present when storage adapter created a session for this request */
225
+ threadId?: string;
226
+ }
227
+ /**
228
+ * Union of all stream events
229
+ */
230
+ type StreamEvent = MessageStartEvent | MessageDeltaEvent | MessageEndEvent | ThinkingStartEvent | ThinkingDeltaEvent | ThinkingEndEvent | ActionStartEvent | ActionArgsEvent | ActionEndEvent | ToolCallsEvent | ToolResultEvent | CitationEvent | LoopIterationEvent | LoopCompleteEvent | ErrorEvent | ThreadCreatedEvent | DoneEvent;
231
+ /**
232
+ * Structured-output / JSON-mode request format.
233
+ *
234
+ * Uses OpenAI's `response_format` shape as the unified surface; each adapter
235
+ * translates to its provider's native field (Anthropic `output_config`,
236
+ * Gemini `responseJsonSchema`, Ollama `format`, etc.).
237
+ */
238
+ type ResponseFormat = {
239
+ type: "json_object";
240
+ } | {
241
+ type: "json_schema";
242
+ json_schema: {
243
+ name: string;
244
+ schema: Record<string, unknown>;
245
+ strict?: boolean;
246
+ };
247
+ };
248
+ /**
249
+ * MCP (Model Context Protocol) server reference for a request.
250
+ *
251
+ * Vendor-neutral shape — each adapter translates to its provider's native
252
+ * field: OpenAI Responses `tools[type=mcp]`, Anthropic `mcp_servers` +
253
+ * `tools[type=mcp_toolset]` (beta `mcp-client-2025-11-20`), or local
254
+ * execution for adapters without native MCP.
255
+ */
256
+ interface McpServerConfig {
257
+ /** Human-readable label sent to the provider (also used as MCP server name). */
258
+ label: string;
259
+ /** MCP server endpoint (HTTP/SSE URL). */
260
+ url: string;
261
+ /** Additional HTTP headers; the `Authorization` value is also extracted for providers that take a separate token field. */
262
+ headers?: Record<string, string>;
263
+ /** Restrict the model to a subset of the MCP server's exposed tools. */
264
+ allowedTools?: string[];
265
+ /** Approval policy for tool invocation. Defaults to "never" (no human-in-the-loop). */
266
+ requireApproval?: "never" | "always";
267
+ }
268
+ /**
269
+ * Reasoning effort knob — normalized across providers.
270
+ *
271
+ * Maps to OpenAI Responses `reasoning.effort`, Anthropic `thinking`
272
+ * (adaptive+effort on Claude 4.6/4.7, budget_tokens on older models),
273
+ * Gemini `thinkingBudget`, xAI `reasoning_effort`. Adapters silently
274
+ * no-op when the underlying model doesn't support reasoning.
275
+ *
276
+ * Use the enum for the common case, or `{ budgetTokens }` / `{ raw }`
277
+ * for provider-specific escape hatches when full fidelity is required.
278
+ */
279
+ type ReasoningEffort = "minimal" | "low" | "medium" | "high" | {
280
+ budgetTokens: number;
281
+ } | {
282
+ raw: Record<string, unknown>;
283
+ };
284
+ /**
285
+ * LLM configuration
286
+ */
287
+ interface LLMConfig {
288
+ temperature?: number;
289
+ maxTokens?: number;
290
+ responseFormat?: ResponseFormat;
291
+ mcpServers?: McpServerConfig[];
292
+ reasoningEffort?: ReasoningEffort;
293
+ }
294
+ /**
295
+ * Tool call format (OpenAI style)
296
+ */
297
+ interface ToolCall$1 {
298
+ id: string;
299
+ type: "function";
300
+ function: {
301
+ name: string;
302
+ arguments: string;
303
+ };
304
+ /** Provider-specific metadata (e.g. Gemini 3 thought_signature in extra_content.google) */
305
+ extra_content?: Record<string, unknown>;
306
+ }
307
+ /**
308
+ * Message role
309
+ */
310
+ type MessageRole = "system" | "user" | "assistant" | "tool";
311
+ /**
312
+ * Message attachment
313
+ */
314
+ interface MessageAttachment {
315
+ type: "image" | "file" | "audio" | "video";
316
+ data?: string;
317
+ url?: string;
318
+ mimeType: string;
319
+ filename?: string;
320
+ }
321
+ /**
322
+ * Message metadata
323
+ */
324
+ interface MessageMetadata {
325
+ thinking?: string;
326
+ attachments?: MessageAttachment[];
327
+ toolName?: string;
328
+ [key: string]: unknown;
329
+ }
330
+ /**
331
+ * Message type (simplified for llm-sdk)
332
+ */
333
+ interface Message {
334
+ id: string;
335
+ thread_id?: string;
336
+ role: MessageRole;
337
+ content: string | null;
338
+ tool_calls?: ToolCall$1[];
339
+ tool_call_id?: string;
340
+ metadata?: MessageMetadata;
341
+ created_at?: Date;
342
+ }
343
+ /**
344
+ * Action parameter definition
345
+ */
346
+ interface ActionParameter {
347
+ type: string;
348
+ description?: string;
349
+ required?: boolean;
350
+ enum?: string[];
351
+ items?: ActionParameter;
352
+ properties?: Record<string, ActionParameter>;
353
+ }
354
+ /**
355
+ * Action definition for tool calling
356
+ */
357
+ interface ActionDefinition<TParams = Record<string, unknown>> {
358
+ name: string;
359
+ description: string;
360
+ parameters?: Record<string, ActionParameter>;
361
+ handler: (params: TParams) => unknown | Promise<unknown>;
362
+ }
363
+ /**
364
+ * Tool location (server or client)
365
+ */
366
+ type ToolLocation = "server" | "client";
367
+ /**
368
+ * Tool execution status
369
+ */
370
+ type ToolExecutionStatus = "pending" | "executing" | "completed" | "error";
371
+ /**
372
+ * Tool response
373
+ */
374
+ interface ToolResponse<T = unknown> {
375
+ success: boolean;
376
+ data?: T;
377
+ error?: string;
378
+ /** Internal: AI response mode override */
379
+ _aiResponseMode?: AIResponseMode;
380
+ /** Internal: AI content for multimodal response (images, etc.) */
381
+ _aiContent?: AIContent[];
382
+ /** Internal: AI context string override */
383
+ _aiContext?: string;
384
+ }
385
+ /**
386
+ * Tool context passed to handlers
387
+ */
388
+ interface ToolContext$1 {
389
+ userId?: string;
390
+ threadId?: string;
391
+ [key: string]: unknown;
392
+ }
393
+ /**
394
+ * AI response mode for tool results
395
+ */
396
+ type AIResponseMode = "none" | "brief" | "full";
397
+ /**
398
+ * AI content structure
399
+ */
400
+ interface AIContent {
401
+ type?: "text" | "image";
402
+ text?: string;
403
+ mediaType?: string;
404
+ data?: string;
405
+ summary?: string;
406
+ details?: string;
407
+ }
408
+ /**
409
+ * JSON Schema for tool input
410
+ */
411
+ interface ToolInputSchema {
412
+ type: "object";
413
+ properties?: Record<string, unknown>;
414
+ required?: string[];
415
+ }
416
+ /**
417
+ * Tool definition
418
+ */
419
+ interface ToolDefinition<TParams = Record<string, unknown>> {
420
+ name: string;
421
+ description: string;
422
+ location: ToolLocation;
423
+ /** Optional logical category for tool search and selective loading. */
424
+ category?: string;
425
+ /** Optional group label for related tools. */
426
+ group?: string;
427
+ title?: string | ((args: TParams) => string);
428
+ inputSchema?: ToolInputSchema;
429
+ handler?: (params: TParams, context?: ToolContext$1) => unknown | Promise<unknown>;
430
+ render?: (props: unknown) => unknown;
431
+ available?: boolean;
432
+ /**
433
+ * Hide this tool's execution from the chat UI.
434
+ * When true, tool calls and results won't be displayed to the user,
435
+ * but the tool will still execute normally.
436
+ * @default false
437
+ */
438
+ hidden?: boolean;
439
+ needsApproval?: boolean;
440
+ approvalMessage?: string | ((params: TParams) => string);
441
+ /** AI response mode for this tool (none, brief, full) */
442
+ aiResponseMode?: AIResponseMode;
443
+ /** AI context string or function to generate context */
444
+ aiContext?: string | ((result: ToolResponse, args: Record<string, unknown>) => string);
445
+ /** Hint that this tool should be loaded lazily when dynamic selection is active. */
446
+ deferLoading?: boolean;
447
+ /** Named profiles this tool belongs to (for example "coding" or "search"). */
448
+ profiles?: string[];
449
+ /** Extra keywords used by lightweight tool search/ranking. */
450
+ searchKeywords?: string[];
451
+ }
452
+ interface ToolProfile {
453
+ include?: string[];
454
+ exclude?: string[];
455
+ }
456
+ interface OpenAIToolSelectionHints {
457
+ /**
458
+ * "single" forces the selected tool when exactly one tool remains after selection.
459
+ * Otherwise the adapter falls back to automatic tool choice.
460
+ */
461
+ toolChoice?: "auto" | "required" | "single";
462
+ /** Set false to disable parallel tool calls on OpenAI-compatible providers. */
463
+ parallelToolCalls?: boolean;
464
+ }
465
+ interface AnthropicToolSelectionHints {
466
+ /**
467
+ * "single" forces the selected tool when exactly one tool remains after selection.
468
+ * Otherwise the adapter falls back to Anthropic's automatic tool choice.
469
+ */
470
+ toolChoice?: "auto" | "any" | "single";
471
+ /** Disable parallel tool use when supported by the Anthropic API. */
472
+ disableParallelToolUse?: boolean;
473
+ }
474
+ interface ToolNativeProviderHints {
475
+ openai?: OpenAIToolSelectionHints;
476
+ anthropic?: AnthropicToolSelectionHints;
477
+ }
478
+ interface OpenAIProviderToolOptions {
479
+ toolChoice?: "auto" | "required" | {
480
+ type: "function";
481
+ name: string;
482
+ };
483
+ parallelToolCalls?: boolean;
484
+ nativeToolSearch?: {
485
+ enabled: boolean;
486
+ useResponsesApi?: boolean;
487
+ };
488
+ }
489
+ interface AnthropicProviderToolOptions {
490
+ toolChoice?: "auto" | "any" | {
491
+ type: "tool";
492
+ name: string;
493
+ };
494
+ disableParallelToolUse?: boolean;
495
+ nativeToolSearch?: {
496
+ enabled: boolean;
497
+ variant: "bm25" | "regex";
498
+ };
499
+ }
500
+ interface ProviderToolRuntimeOptions {
501
+ openai?: OpenAIProviderToolOptions;
502
+ anthropic?: AnthropicProviderToolOptions;
503
+ }
504
+ /**
505
+ * Web search configuration for native provider search
506
+ *
507
+ * Enables native web search for supported providers:
508
+ * - Anthropic: Uses Claude's built-in web search tool
509
+ * - OpenAI: Uses GPT's web search preview
510
+ * - Google: Uses Gemini's Google Search grounding
511
+ *
512
+ * @example
513
+ * ```typescript
514
+ * const runtime = createRuntime({
515
+ * provider: createAnthropic({ apiKey: '...' }),
516
+ * model: 'claude-sonnet-4-20250514',
517
+ * webSearch: true, // Enable with defaults
518
+ * });
519
+ *
520
+ * // Or with configuration
521
+ * const runtime = createRuntime({
522
+ * provider: createOpenAI({ apiKey: '...' }),
523
+ * model: 'gpt-4o',
524
+ * webSearch: {
525
+ * maxUses: 5,
526
+ * allowedDomains: ['docs.anthropic.com', 'openai.com'],
527
+ * },
528
+ * });
529
+ * ```
530
+ */
531
+ interface WebSearchConfig {
532
+ /** Maximum number of search uses per request (default: unlimited) */
533
+ maxUses?: number;
534
+ /** Only search these domains (provider-specific support) */
535
+ allowedDomains?: string[];
536
+ /** Exclude these domains from search (provider-specific support) */
537
+ blockedDomains?: string[];
538
+ /** User location for localized results (Anthropic only) */
539
+ userLocation?: {
540
+ type: "approximate";
541
+ city?: string;
542
+ region?: string;
543
+ country?: string;
544
+ timezone?: string;
545
+ };
546
+ }
547
+ /**
548
+ * Unified tool call format
549
+ */
550
+ interface UnifiedToolCall {
551
+ id: string;
552
+ name: string;
553
+ input: Record<string, unknown>;
554
+ }
555
+ /**
556
+ * Unified tool result format
557
+ */
558
+ interface UnifiedToolResult {
559
+ toolCallId: string;
560
+ content: string;
561
+ success: boolean;
562
+ error?: string;
563
+ }
564
+ /**
565
+ * Tool execution state
566
+ */
567
+ interface ToolExecution {
568
+ id: string;
569
+ name: string;
570
+ args: Record<string, unknown>;
571
+ status: ToolExecutionStatus;
572
+ result?: ToolResponse;
573
+ }
574
+ /**
575
+ * Knowledge base provider
576
+ */
577
+ type KnowledgeBaseProvider = "pinecone" | "qdrant" | "weaviate" | "custom";
578
+ /**
579
+ * Knowledge base configuration
580
+ */
581
+ interface KnowledgeBaseConfig {
582
+ id: string;
583
+ name?: string;
584
+ provider: KnowledgeBaseProvider;
585
+ apiKey?: string;
586
+ index?: string;
587
+ }
588
+
589
+ /**
590
+ * A language model instance that can generate text.
591
+ * This is what provider functions like `openai('gpt-4o')` return.
592
+ */
593
+ interface LanguageModel {
594
+ /** Provider identifier (e.g., 'openai', 'anthropic') */
595
+ readonly provider: string;
596
+ /** Model identifier (e.g., 'gpt-4o', 'claude-3-5-sonnet') */
597
+ readonly modelId: string;
598
+ /** Model capabilities for feature detection */
599
+ readonly capabilities: ModelCapabilities;
600
+ /**
601
+ * Generate a complete response (non-streaming)
602
+ * Used internally by generateText()
603
+ */
604
+ doGenerate(params: DoGenerateParams): Promise<DoGenerateResult>;
605
+ /**
606
+ * Stream a response
607
+ * Used internally by streamText()
608
+ */
609
+ doStream(params: DoGenerateParams): AsyncGenerator<StreamChunk>;
610
+ }
611
+ /**
612
+ * Model capabilities for UI feature flags
613
+ */
614
+ interface ModelCapabilities {
615
+ /** Supports image inputs */
616
+ supportsVision: boolean;
617
+ /** Supports tool/function calling */
618
+ supportsTools: boolean;
619
+ /** Supports streaming responses */
620
+ supportsStreaming: boolean;
621
+ /** Supports JSON mode / structured output */
622
+ supportsJsonMode: boolean;
623
+ /** Supports extended thinking (Claude) */
624
+ supportsThinking: boolean;
625
+ /** Supports PDF document inputs */
626
+ supportsPDF: boolean;
627
+ /** Maximum context tokens */
628
+ maxTokens: number;
629
+ /** Supported image MIME types */
630
+ supportedImageTypes: string[];
631
+ }
632
+ /**
633
+ * Default capabilities for unknown models
634
+ */
635
+ declare const DEFAULT_CAPABILITIES: ModelCapabilities;
636
+ /**
637
+ * Core message types for LLM conversations
638
+ */
639
+ type CoreMessage = SystemMessage | UserMessage | AssistantMessage | ToolMessage;
640
+ interface SystemMessage {
641
+ role: "system";
642
+ content: string;
643
+ }
644
+ interface UserMessage {
645
+ role: "user";
646
+ content: string | UserContentPart[];
647
+ }
648
+ interface AssistantMessage {
649
+ role: "assistant";
650
+ content: string | null;
651
+ toolCalls?: ToolCall[];
652
+ }
653
+ interface ToolMessage {
654
+ role: "tool";
655
+ toolCallId: string;
656
+ content: string;
657
+ }
658
+ /**
659
+ * Content parts for multimodal user messages
660
+ */
661
+ type UserContentPart = TextPart | ImagePart | FilePart | AudioPart;
662
+ interface TextPart {
663
+ type: "text";
664
+ text: string;
665
+ }
666
+ interface ImagePart {
667
+ type: "image";
668
+ /** Base64 data or URL */
669
+ image: string | Uint8Array;
670
+ /** MIME type (e.g., 'image/png') */
671
+ mimeType?: string;
672
+ }
673
+ interface FilePart {
674
+ type: "file";
675
+ /** Base64 data or URL */
676
+ data: string;
677
+ /** MIME type (e.g., 'application/pdf') */
678
+ mimeType: string;
679
+ }
680
+ interface AudioPart {
681
+ type: "input_audio";
682
+ input_audio: {
683
+ /** Base64-encoded audio data */
684
+ data: string;
685
+ /** Audio format (e.g., 'mp3', 'wav', 'ogg', 'webm', 'm4a', 'flac') */
686
+ format: string;
687
+ };
688
+ }
689
+ /**
690
+ * Tool definition with Zod schema support
691
+ */
692
+ interface Tool<TParams = unknown, TResult = unknown> {
693
+ /** Tool description for the LLM */
694
+ description: string;
695
+ /** Zod schema for parameters */
696
+ parameters: z.ZodType<TParams>;
697
+ /** Execute function */
698
+ execute: (params: TParams, context: ToolContext) => Promise<TResult>;
699
+ /**
700
+ * Hide this tool's execution from the chat UI.
701
+ * When true, tool calls and results won't be displayed to the user,
702
+ * but the tool will still execute normally.
703
+ * @default false
704
+ */
705
+ hidden?: boolean;
706
+ }
707
+ /**
708
+ * Context passed to tool execute function
709
+ */
710
+ interface ToolContext {
711
+ /** Abort signal for cancellation */
712
+ abortSignal?: AbortSignal;
713
+ /** Unique tool call ID */
714
+ toolCallId: string;
715
+ /** Optional: messages in conversation */
716
+ messages?: CoreMessage[];
717
+ }
718
+ /**
719
+ * Tool call from LLM response
720
+ */
721
+ interface ToolCall {
722
+ /** Unique ID for this tool call */
723
+ id: string;
724
+ /** Tool name */
725
+ name: string;
726
+ /** Parsed arguments */
727
+ args: Record<string, unknown>;
728
+ }
729
+ /**
730
+ * Tool execution result
731
+ */
732
+ interface ToolResult {
733
+ /** Tool call ID this result corresponds to */
734
+ toolCallId: string;
735
+ /** Result data (will be JSON stringified for LLM) */
736
+ result: unknown;
737
+ }
738
+ /**
739
+ * Parameters for model.doGenerate() and model.doStream()
740
+ */
741
+ interface DoGenerateParams {
742
+ /** Messages to send to LLM */
743
+ messages: CoreMessage[];
744
+ /** Tools available to the LLM (already formatted for provider) */
745
+ tools?: unknown[];
746
+ /** Temperature (0-2) */
747
+ temperature?: number;
748
+ /** Maximum tokens to generate */
749
+ maxTokens?: number;
750
+ /** Structured-output / JSON-mode request format (provider-translated) */
751
+ responseFormat?: ResponseFormat;
752
+ /** MCP servers exposed to the model (provider-translated). */
753
+ mcpServers?: McpServerConfig[];
754
+ /** Reasoning/thinking effort knob (provider-translated). */
755
+ reasoningEffort?: ReasoningEffort;
756
+ /** Abort signal */
757
+ signal?: AbortSignal;
758
+ }
759
+ /**
760
+ * Result from model.doGenerate()
761
+ */
762
+ interface DoGenerateResult {
763
+ /** Generated text content */
764
+ text: string;
765
+ /** Tool calls requested by the LLM */
766
+ toolCalls: ToolCall[];
767
+ /** Why generation stopped */
768
+ finishReason: FinishReason;
769
+ /** Token usage */
770
+ usage: TokenUsage;
771
+ /** Raw provider response (for debugging) */
772
+ rawResponse?: unknown;
773
+ }
774
+ /**
775
+ * Finish reason for generation
776
+ */
777
+ type FinishReason = "stop" | "length" | "tool-calls" | "content-filter" | "error" | "unknown";
778
+ /**
779
+ * Token usage statistics
780
+ */
781
+ interface TokenUsage {
782
+ promptTokens: number;
783
+ completionTokens: number;
784
+ totalTokens: number;
785
+ }
786
+ /**
787
+ * Stream chunk from model.doStream()
788
+ */
789
+ type StreamChunk = TextDeltaChunk | ToolCallChunk | ToolResultChunk | FinishChunk | ErrorChunk;
790
+ interface TextDeltaChunk {
791
+ type: "text-delta";
792
+ text: string;
793
+ }
794
+ interface ToolCallChunk {
795
+ type: "tool-call";
796
+ toolCall: ToolCall;
797
+ }
798
+ interface ToolResultChunk {
799
+ type: "tool-result";
800
+ toolCallId: string;
801
+ result: unknown;
802
+ }
803
+ interface FinishChunk {
804
+ type: "finish";
805
+ finishReason: FinishReason;
806
+ usage?: TokenUsage;
807
+ }
808
+ interface ErrorChunk {
809
+ type: "error";
810
+ error: Error;
811
+ }
812
+ /**
813
+ * Parameters for generateText()
814
+ */
815
+ interface GenerateTextParams {
816
+ /** Language model to use */
817
+ model: LanguageModel;
818
+ /** Simple prompt (converted to user message) */
819
+ prompt?: string;
820
+ /** System prompt */
821
+ system?: string;
822
+ /** Full message history */
823
+ messages?: CoreMessage[];
824
+ /** Tools available to the LLM */
825
+ tools?: Record<string, Tool>;
826
+ /** Maximum agentic steps (tool call loops) */
827
+ maxSteps?: number;
828
+ /** Temperature (0-2) */
829
+ temperature?: number;
830
+ /** Maximum tokens to generate */
831
+ maxTokens?: number;
832
+ /** Structured-output / JSON-mode request format */
833
+ responseFormat?: ResponseFormat;
834
+ /** MCP servers exposed to the model (provider-translated). */
835
+ mcpServers?: McpServerConfig[];
836
+ /** Reasoning/thinking effort knob (provider-translated). */
837
+ reasoningEffort?: ReasoningEffort;
838
+ /** Abort signal */
839
+ signal?: AbortSignal;
840
+ }
841
+ /**
842
+ * Result from generateText()
843
+ */
844
+ interface GenerateTextResult {
845
+ /** Final text output */
846
+ text: string;
847
+ /** Token usage */
848
+ usage: TokenUsage;
849
+ /** Why generation stopped */
850
+ finishReason: FinishReason;
851
+ /** All steps taken (for agentic workflows) */
852
+ steps: GenerateStep[];
853
+ /** All tool calls made across all steps */
854
+ toolCalls: ToolCall[];
855
+ /** All tool results across all steps */
856
+ toolResults: ToolResult[];
857
+ /** Final message list including tool interactions */
858
+ response: {
859
+ messages: CoreMessage[];
860
+ };
861
+ }
862
+ /**
863
+ * A single step in the generation process
864
+ */
865
+ interface GenerateStep {
866
+ /** Text generated in this step */
867
+ text: string;
868
+ /** Tool calls made in this step */
869
+ toolCalls: ToolCall[];
870
+ /** Tool results from this step */
871
+ toolResults: ToolResult[];
872
+ /** Finish reason for this step */
873
+ finishReason: FinishReason;
874
+ /** Token usage for this step */
875
+ usage: TokenUsage;
876
+ }
877
+ /**
878
+ * Parameters for streamText() - same as generateText
879
+ */
880
+ type StreamTextParams = GenerateTextParams;
881
+ /**
882
+ * Result from streamText()
883
+ */
884
+ interface StreamTextResult {
885
+ /** Async iterable of text chunks only */
886
+ textStream: AsyncIterable<string>;
887
+ /** Async iterable of all stream parts */
888
+ fullStream: AsyncIterable<StreamPart>;
889
+ /** Promise that resolves to full text when complete */
890
+ readonly text: Promise<string>;
891
+ /** Promise that resolves to usage when complete */
892
+ readonly usage: Promise<TokenUsage>;
893
+ /** Promise that resolves to finish reason when complete */
894
+ readonly finishReason: Promise<FinishReason>;
895
+ /** Convert to plain text streaming Response */
896
+ toTextStreamResponse(options?: ResponseOptions): Response;
897
+ /** Convert to data stream Response (SSE with tool calls) */
898
+ toDataStreamResponse(options?: ResponseOptions): Response;
899
+ }
900
+ /**
901
+ * Stream part for fullStream
902
+ */
903
+ type StreamPart = {
904
+ type: "text-delta";
905
+ text: string;
906
+ } | {
907
+ type: "tool-call-start";
908
+ toolCallId: string;
909
+ toolName: string;
910
+ } | {
911
+ type: "tool-call-delta";
912
+ toolCallId: string;
913
+ argsText: string;
914
+ } | {
915
+ type: "tool-call-complete";
916
+ toolCall: ToolCall;
917
+ } | {
918
+ type: "tool-result";
919
+ toolCallId: string;
920
+ result: unknown;
921
+ } | {
922
+ type: "step-start";
923
+ step: number;
924
+ } | {
925
+ type: "step-finish";
926
+ step: number;
927
+ finishReason: FinishReason;
928
+ } | {
929
+ type: "finish";
930
+ finishReason: FinishReason;
931
+ usage: TokenUsage;
932
+ } | {
933
+ type: "error";
934
+ error: Error;
935
+ };
936
+ /**
937
+ * Options for Response helpers
938
+ */
939
+ interface ResponseOptions {
940
+ /** Additional headers */
941
+ headers?: Record<string, string>;
942
+ /** Response status (default: 200) */
943
+ status?: number;
944
+ }
945
+ /**
946
+ * Message format for storage adapters.
947
+ * Intentionally simpler than LLM-specific formats — adapters convert as needed.
948
+ */
949
+ interface StorageMessage {
950
+ role: "user" | "assistant" | "system" | "tool";
951
+ content: string;
952
+ toolCalls?: unknown[];
953
+ toolCallId?: string;
954
+ /** Content type for the message — determines how it's stored in the backend */
955
+ contentType?: "text" | "image" | "file";
956
+ /** URL for image/file attachments */
957
+ url?: string;
958
+ metadata?: Record<string, unknown>;
959
+ }
960
+ /**
961
+ * Generic storage adapter interface for session + message persistence.
962
+ *
963
+ * `createYourGPT()` is the default implementation for YourGPT platform.
964
+ * Third-party developers can implement this interface for custom backends.
965
+ *
966
+ * @example
967
+ * ```ts
968
+ * import { createRuntime } from '@yourgpt/llm-sdk'
969
+ * import { createYourGPT } from '@yourgpt/llm-sdk/yourgpt'
970
+ *
971
+ * const runtime = createRuntime({
972
+ * provider: anthropic,
973
+ * model: 'claude-haiku-4-5',
974
+ * storage: createYourGPT({ apiKey, widgetUid }),
975
+ * })
976
+ * // runtime.chat() and runtime.stream() now auto-persist messages
977
+ * ```
978
+ */
979
+ interface StorageAdapter {
980
+ /** Create a new session. Returns session ID to use as threadId. */
981
+ createSession(data?: {
982
+ title?: string;
983
+ metadata?: Record<string, unknown>;
984
+ }): Promise<{
985
+ id: string;
986
+ }>;
987
+ /** Append messages to a session (called sequentially — input before output). */
988
+ saveMessages(sessionId: string, messages: StorageMessage[]): Promise<void>;
989
+ /** List sessions (optional — used for thread picker sync in future). */
990
+ getSessions?(): Promise<{
991
+ id: string;
992
+ title?: string;
993
+ updatedAt?: Date;
994
+ }[]>;
995
+ /** Get messages for a session (optional — used for thread restore in future). */
996
+ getMessages?(sessionId: string): Promise<StorageMessage[]>;
997
+ /**
998
+ * Upload a file to storage. Returns a URL the LLM can reference.
999
+ * When present, the server exposes a /upload endpoint and the client
1000
+ * uses it instead of embedding base64 in the message body.
1001
+ */
1002
+ uploadFile?(file: StorageFile): Promise<{
1003
+ url: string;
1004
+ }>;
1005
+ }
1006
+ /**
1007
+ * File data for upload via StorageAdapter.uploadFile()
1008
+ */
1009
+ interface StorageFile {
1010
+ /** Base64-encoded file data (with or without data URI prefix) */
1011
+ data: string;
1012
+ /** MIME type (e.g., "image/png", "application/pdf") */
1013
+ mimeType: string;
1014
+ /** Original filename */
1015
+ filename?: string;
1016
+ }
1017
+
1018
+ export { type UnifiedToolCall as $, type ActionDefinition as A, type TextDeltaChunk as B, type CoreMessage as C, type DoneEventMessage as D, type ToolCallChunk as E, type FilePart as F, type GenerateTextParams as G, type ToolResultChunk as H, type ImagePart as I, type FinishChunk as J, type KnowledgeBaseConfig as K, type LanguageModel as L, type McpServerConfig as M, type ErrorChunk as N, type TokenUsage as O, type ProviderToolRuntimeOptions as P, type FinishReason as Q, type ResponseFormat as R, type StreamTextParams as S, type ToolContext as T, type UserMessage as U, type ResponseOptions as V, type WebSearchConfig as W, type StorageFile as X, DEFAULT_CAPABILITIES as Y, type LLMConfig as Z, type ToolLocation as _, type GenerateTextResult as a, type UnifiedToolResult as a0, type ToolExecution as a1, type OpenAIToolSelectionHints as a2, type AnthropicToolSelectionHints as a3, type ToolNativeProviderHints as a4, type OpenAIProviderToolOptions as a5, type AnthropicProviderToolOptions as a6, type Citation as a7, type MessageAttachment as a8, type StreamTextResult as b, type Tool as c, type ToolDefinition as d, type ToolProfile as e, type StorageAdapter as f, type ReasoningEffort as g, type StreamEvent as h, type ToolCallInfo as i, type TokenUsageRaw as j, type Message as k, type ToolResponse as l, type StorageMessage as m, type ModelCapabilities as n, type DoGenerateParams as o, type DoGenerateResult as p, type SystemMessage as q, type AssistantMessage as r, type ToolMessage as s, type UserContentPart as t, type TextPart as u, type ToolCall as v, type ToolResult as w, type GenerateStep as x, type StreamPart as y, type StreamChunk as z };