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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/dist/adapters/index.d.mts +4 -4
  2. package/dist/adapters/index.d.ts +4 -4
  3. package/dist/adapters/index.js +156 -13
  4. package/dist/adapters/index.mjs +156 -13
  5. package/dist/base-C58Dsr9p.d.ts +259 -0
  6. package/dist/base-tNgbBaSo.d.mts +259 -0
  7. package/dist/fallback/index.d.mts +4 -4
  8. package/dist/fallback/index.d.ts +4 -4
  9. package/dist/index.d.mts +8 -7
  10. package/dist/index.d.ts +8 -7
  11. package/dist/index.js +12 -0
  12. package/dist/index.mjs +12 -0
  13. package/dist/providers/anthropic/index.d.mts +3 -3
  14. package/dist/providers/anthropic/index.d.ts +3 -3
  15. package/dist/providers/anthropic/index.js +271 -195
  16. package/dist/providers/anthropic/index.mjs +271 -195
  17. package/dist/providers/azure/index.d.mts +3 -3
  18. package/dist/providers/azure/index.d.ts +3 -3
  19. package/dist/providers/azure/index.js +49 -1
  20. package/dist/providers/azure/index.mjs +49 -1
  21. package/dist/providers/fireworks/index.d.mts +1 -1
  22. package/dist/providers/fireworks/index.d.ts +1 -1
  23. package/dist/providers/fireworks/index.js +56 -0
  24. package/dist/providers/fireworks/index.mjs +56 -0
  25. package/dist/providers/google/index.d.mts +3 -3
  26. package/dist/providers/google/index.d.ts +3 -3
  27. package/dist/providers/google/index.js +252 -205
  28. package/dist/providers/google/index.mjs +252 -205
  29. package/dist/providers/ollama/index.d.mts +4 -4
  30. package/dist/providers/ollama/index.d.ts +4 -4
  31. package/dist/providers/ollama/index.js +10 -2
  32. package/dist/providers/ollama/index.mjs +10 -2
  33. package/dist/providers/openai/index.d.mts +3 -3
  34. package/dist/providers/openai/index.d.ts +3 -3
  35. package/dist/providers/openai/index.js +267 -214
  36. package/dist/providers/openai/index.mjs +267 -214
  37. package/dist/providers/openrouter/index.d.mts +3 -3
  38. package/dist/providers/openrouter/index.d.ts +3 -3
  39. package/dist/providers/openrouter/index.js +257 -204
  40. package/dist/providers/openrouter/index.mjs +257 -204
  41. package/dist/providers/togetherai/index.d.mts +3 -3
  42. package/dist/providers/togetherai/index.d.ts +3 -3
  43. package/dist/providers/togetherai/index.js +257 -204
  44. package/dist/providers/togetherai/index.mjs +257 -204
  45. package/dist/providers/xai/index.d.mts +3 -3
  46. package/dist/providers/xai/index.d.ts +3 -3
  47. package/dist/providers/xai/index.js +256 -208
  48. package/dist/providers/xai/index.mjs +256 -208
  49. package/dist/{types-D4YfrQJR.d.mts → types-B6dhnguR.d.mts} +1 -1
  50. package/dist/{types-DRqxMIjF.d.mts → types-BQ31QIsA.d.ts} +2 -1
  51. package/dist/{types-BctsnC3g.d.ts → types-BSSiJW2o.d.mts} +2 -1
  52. package/dist/{base-D-U61JaB.d.mts → types-BkQCSiIt.d.mts} +388 -213
  53. package/dist/{base-iGi9Va6Z.d.ts → types-BkQCSiIt.d.ts} +388 -213
  54. package/dist/{types-38yolWJn.d.ts → types-CCxPmkmK.d.ts} +1 -1
  55. package/dist/yourgpt/index.d.mts +1 -1
  56. package/dist/yourgpt/index.d.ts +1 -1
  57. package/package.json +1 -1
  58. package/dist/types-CR8mi9I0.d.mts +0 -417
  59. package/dist/types-CR8mi9I0.d.ts +0 -417
@@ -1,4 +1,4 @@
1
- import { t as TokenUsage } from './types-CR8mi9I0.js';
1
+ import { z } from 'zod';
2
2
 
3
3
  /**
4
4
  * Stream event types for llm-sdk
@@ -228,17 +228,35 @@ interface DoneEvent extends BaseEvent {
228
228
  * Union of all stream events
229
229
  */
230
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
+ };
231
248
  /**
232
249
  * LLM configuration
233
250
  */
234
251
  interface LLMConfig {
235
252
  temperature?: number;
236
253
  maxTokens?: number;
254
+ responseFormat?: ResponseFormat;
237
255
  }
238
256
  /**
239
257
  * Tool call format (OpenAI style)
240
258
  */
241
- interface ToolCall {
259
+ interface ToolCall$1 {
242
260
  id: string;
243
261
  type: "function";
244
262
  function: {
@@ -279,7 +297,7 @@ interface Message {
279
297
  thread_id?: string;
280
298
  role: MessageRole;
281
299
  content: string | null;
282
- tool_calls?: ToolCall[];
300
+ tool_calls?: ToolCall$1[];
283
301
  tool_call_id?: string;
284
302
  metadata?: MessageMetadata;
285
303
  created_at?: Date;
@@ -329,7 +347,7 @@ interface ToolResponse<T = unknown> {
329
347
  /**
330
348
  * Tool context passed to handlers
331
349
  */
332
- interface ToolContext {
350
+ interface ToolContext$1 {
333
351
  userId?: string;
334
352
  threadId?: string;
335
353
  [key: string]: unknown;
@@ -370,7 +388,7 @@ interface ToolDefinition<TParams = Record<string, unknown>> {
370
388
  group?: string;
371
389
  title?: string | ((args: TParams) => string);
372
390
  inputSchema?: ToolInputSchema;
373
- handler?: (params: TParams, context?: ToolContext) => unknown | Promise<unknown>;
391
+ handler?: (params: TParams, context?: ToolContext$1) => unknown | Promise<unknown>;
374
392
  render?: (props: unknown) => unknown;
375
393
  available?: boolean;
376
394
  /**
@@ -531,258 +549,415 @@ interface KnowledgeBaseConfig {
531
549
  }
532
550
 
533
551
  /**
534
- * Request-level LLM configuration overrides
535
- */
536
- interface RequestLLMConfig {
537
- model?: string;
538
- temperature?: number;
539
- maxTokens?: number;
540
- }
541
- /**
542
- * Chat completion request
552
+ * A language model instance that can generate text.
553
+ * This is what provider functions like `openai('gpt-4o')` return.
543
554
  */
544
- interface ChatCompletionRequest {
545
- /** Conversation messages */
546
- messages: Message[];
555
+ interface LanguageModel {
556
+ /** Provider identifier (e.g., 'openai', 'anthropic') */
557
+ readonly provider: string;
558
+ /** Model identifier (e.g., 'gpt-4o', 'claude-3-5-sonnet') */
559
+ readonly modelId: string;
560
+ /** Model capabilities for feature detection */
561
+ readonly capabilities: ModelCapabilities;
547
562
  /**
548
- * Raw provider-formatted messages (for agent loop with tool calls)
549
- * When provided, these are used instead of converting from Message[]
550
- * This allows passing messages with tool_calls and tool role
563
+ * Generate a complete response (non-streaming)
564
+ * Used internally by generateText()
551
565
  */
552
- rawMessages?: Array<Record<string, unknown>>;
553
- /** Available actions/tools */
554
- actions?: ActionDefinition[];
555
- /** Full tool definitions for provider-native tool search / deferred loading paths. */
556
- toolDefinitions?: ToolDefinition[];
557
- /** System prompt */
558
- systemPrompt?: string;
559
- /** LLM configuration overrides */
560
- config?: RequestLLMConfig;
561
- /** Abort signal for cancellation */
562
- signal?: AbortSignal;
566
+ doGenerate(params: DoGenerateParams): Promise<DoGenerateResult>;
563
567
  /**
564
- * Enable native web search for the provider.
565
- * When true or configured, the provider's native search is enabled.
568
+ * Stream a response
569
+ * Used internally by streamText()
566
570
  */
567
- webSearch?: boolean | WebSearchConfig;
568
- /** Optional provider-specific tool policy hints derived from runtime selection. */
569
- providerToolOptions?: ProviderToolRuntimeOptions;
570
- /** Enable adapter-level provider payload logging. */
571
- debug?: boolean;
571
+ doStream(params: DoGenerateParams): AsyncGenerator<StreamChunk>;
572
572
  }
573
573
  /**
574
- * Non-streaming completion result
574
+ * Model capabilities for UI feature flags
575
+ */
576
+ interface ModelCapabilities {
577
+ /** Supports image inputs */
578
+ supportsVision: boolean;
579
+ /** Supports tool/function calling */
580
+ supportsTools: boolean;
581
+ /** Supports streaming responses */
582
+ supportsStreaming: boolean;
583
+ /** Supports JSON mode / structured output */
584
+ supportsJsonMode: boolean;
585
+ /** Supports extended thinking (Claude) */
586
+ supportsThinking: boolean;
587
+ /** Supports PDF document inputs */
588
+ supportsPDF: boolean;
589
+ /** Maximum context tokens */
590
+ maxTokens: number;
591
+ /** Supported image MIME types */
592
+ supportedImageTypes: string[];
593
+ }
594
+ /**
595
+ * Default capabilities for unknown models
596
+ */
597
+ declare const DEFAULT_CAPABILITIES: ModelCapabilities;
598
+ /**
599
+ * Core message types for LLM conversations
575
600
  */
576
- interface CompletionResult {
577
- /** Text content */
601
+ type CoreMessage = SystemMessage | UserMessage | AssistantMessage | ToolMessage;
602
+ interface SystemMessage {
603
+ role: "system";
604
+ content: string;
605
+ }
606
+ interface UserMessage {
607
+ role: "user";
608
+ content: string | UserContentPart[];
609
+ }
610
+ interface AssistantMessage {
611
+ role: "assistant";
612
+ content: string | null;
613
+ toolCalls?: ToolCall[];
614
+ }
615
+ interface ToolMessage {
616
+ role: "tool";
617
+ toolCallId: string;
578
618
  content: string;
579
- /** Tool calls */
580
- toolCalls: Array<{
581
- id: string;
582
- name: string;
583
- args: Record<string, unknown>;
584
- /** Provider-specific metadata (e.g. Gemini 3 thought_signature in extra_content.google) */
585
- extra_content?: Record<string, unknown>;
586
- }>;
587
- /** Thinking content (if extended thinking enabled) */
588
- thinking?: string;
589
- /** Token usage for billing/tracking */
590
- usage?: TokenUsage;
591
- /** Raw provider response for debugging */
592
- rawResponse: Record<string, unknown>;
593
619
  }
594
620
  /**
595
- * Base LLM adapter interface
621
+ * Content parts for multimodal user messages
596
622
  */
597
- interface LLMAdapter {
598
- /** Provider name */
599
- readonly provider: string;
600
- /** Model name */
601
- readonly model: string;
602
- /**
603
- * Stream a chat completion
604
- */
605
- stream(request: ChatCompletionRequest): AsyncGenerator<StreamEvent>;
623
+ type UserContentPart = TextPart | ImagePart | FilePart;
624
+ interface TextPart {
625
+ type: "text";
626
+ text: string;
627
+ }
628
+ interface ImagePart {
629
+ type: "image";
630
+ /** Base64 data or URL */
631
+ image: string | Uint8Array;
632
+ /** MIME type (e.g., 'image/png') */
633
+ mimeType?: string;
634
+ }
635
+ interface FilePart {
636
+ type: "file";
637
+ /** Base64 data or URL */
638
+ data: string;
639
+ /** MIME type (e.g., 'application/pdf') */
640
+ mimeType: string;
641
+ }
642
+ /**
643
+ * Tool definition with Zod schema support
644
+ */
645
+ interface Tool<TParams = unknown, TResult = unknown> {
646
+ /** Tool description for the LLM */
647
+ description: string;
648
+ /** Zod schema for parameters */
649
+ parameters: z.ZodType<TParams>;
650
+ /** Execute function */
651
+ execute: (params: TParams, context: ToolContext) => Promise<TResult>;
606
652
  /**
607
- * Non-streaming chat completion (for debugging/comparison)
653
+ * Hide this tool's execution from the chat UI.
654
+ * When true, tool calls and results won't be displayed to the user,
655
+ * but the tool will still execute normally.
656
+ * @default false
608
657
  */
609
- complete?(request: ChatCompletionRequest): Promise<CompletionResult>;
658
+ hidden?: boolean;
610
659
  }
611
660
  /**
612
- * Adapter factory function type
661
+ * Context passed to tool execute function
613
662
  */
614
- type AdapterFactory = (config: LLMConfig) => LLMAdapter;
663
+ interface ToolContext {
664
+ /** Abort signal for cancellation */
665
+ abortSignal?: AbortSignal;
666
+ /** Unique tool call ID */
667
+ toolCallId: string;
668
+ /** Optional: messages in conversation */
669
+ messages?: CoreMessage[];
670
+ }
615
671
  /**
616
- * Convert messages to provider format (simple text only)
672
+ * Tool call from LLM response
617
673
  */
618
- declare function formatMessages(messages: Message[], systemPrompt?: string): Array<{
619
- role: string;
620
- content: string;
621
- }>;
674
+ interface ToolCall {
675
+ /** Unique ID for this tool call */
676
+ id: string;
677
+ /** Tool name */
678
+ name: string;
679
+ /** Parsed arguments */
680
+ args: Record<string, unknown>;
681
+ }
622
682
  /**
623
- * Convert actions to OpenAI tool format
683
+ * Tool execution result
624
684
  */
625
- declare function formatTools(actions: ActionDefinition[]): Array<{
626
- type: "function";
627
- function: {
628
- name: string;
629
- description: string;
630
- parameters: object;
631
- };
632
- }>;
685
+ interface ToolResult {
686
+ /** Tool call ID this result corresponds to */
687
+ toolCallId: string;
688
+ /** Result data (will be JSON stringified for LLM) */
689
+ result: unknown;
690
+ }
633
691
  /**
634
- * Content block types for multimodal messages
692
+ * Parameters for model.doGenerate() and model.doStream()
635
693
  */
636
- type AnthropicContentBlock = {
637
- type: "text";
638
- text: string;
639
- } | {
640
- type: "image";
641
- source: {
642
- type: "base64";
643
- media_type: string;
644
- data: string;
645
- } | {
646
- type: "url";
647
- url: string;
648
- };
649
- } | {
650
- type: "document";
651
- source: {
652
- type: "base64";
653
- media_type: string;
654
- data: string;
655
- } | {
656
- type: "url";
657
- url: string;
658
- };
659
- };
660
- type OpenAIContentBlock = {
661
- type: "text";
662
- text: string;
663
- } | {
664
- type: "image_url";
665
- image_url: {
666
- url: string;
667
- detail?: "low" | "high" | "auto";
668
- };
669
- };
694
+ interface DoGenerateParams {
695
+ /** Messages to send to LLM */
696
+ messages: CoreMessage[];
697
+ /** Tools available to the LLM (already formatted for provider) */
698
+ tools?: unknown[];
699
+ /** Temperature (0-2) */
700
+ temperature?: number;
701
+ /** Maximum tokens to generate */
702
+ maxTokens?: number;
703
+ /** Structured-output / JSON-mode request format (provider-translated) */
704
+ responseFormat?: ResponseFormat;
705
+ /** Abort signal */
706
+ signal?: AbortSignal;
707
+ }
670
708
  /**
671
- * Check if a message has image attachments
672
- * Supports both new format (metadata.attachments) and legacy (attachments)
709
+ * Result from model.doGenerate()
673
710
  */
674
- declare function hasImageAttachments(message: Message): boolean;
711
+ interface DoGenerateResult {
712
+ /** Generated text content */
713
+ text: string;
714
+ /** Tool calls requested by the LLM */
715
+ toolCalls: ToolCall[];
716
+ /** Why generation stopped */
717
+ finishReason: FinishReason;
718
+ /** Token usage */
719
+ usage: TokenUsage;
720
+ /** Raw provider response (for debugging) */
721
+ rawResponse?: unknown;
722
+ }
675
723
  /**
676
- * Check if a message has media attachments (images or PDFs)
724
+ * Finish reason for generation
677
725
  */
678
- declare function hasMediaAttachments(message: Message): boolean;
726
+ type FinishReason = "stop" | "length" | "tool-calls" | "content-filter" | "error" | "unknown";
679
727
  /**
680
- * Convert MessageAttachment to Anthropic image content block
681
- *
682
- * Anthropic format:
683
- * {
684
- * type: "image",
685
- * source: {
686
- * type: "base64",
687
- * media_type: "image/png",
688
- * data: "base64data..."
689
- * }
690
- * }
691
- */
692
- declare function attachmentToAnthropicImage(attachment: MessageAttachment): AnthropicContentBlock | null;
693
- /**
694
- * Convert MessageAttachment to OpenAI image_url content block
695
- *
696
- * OpenAI format:
697
- * {
698
- * type: "image_url",
699
- * image_url: {
700
- * url: "data:image/png;base64,..."
701
- * }
702
- * }
728
+ * Token usage statistics
703
729
  */
704
- declare function attachmentToOpenAIImage(attachment: MessageAttachment): OpenAIContentBlock | null;
730
+ interface TokenUsage {
731
+ promptTokens: number;
732
+ completionTokens: number;
733
+ totalTokens: number;
734
+ }
705
735
  /**
706
- * Convert MessageAttachment (PDF) to Anthropic document content block
707
- *
708
- * Anthropic format:
709
- * {
710
- * type: "document",
711
- * source: {
712
- * type: "base64",
713
- * media_type: "application/pdf",
714
- * data: "base64data..."
715
- * }
716
- * }
736
+ * Stream chunk from model.doStream()
717
737
  */
718
- declare function attachmentToAnthropicDocument(attachment: MessageAttachment): AnthropicContentBlock | null;
738
+ type StreamChunk = TextDeltaChunk | ToolCallChunk | ToolResultChunk | FinishChunk | ErrorChunk;
739
+ interface TextDeltaChunk {
740
+ type: "text-delta";
741
+ text: string;
742
+ }
743
+ interface ToolCallChunk {
744
+ type: "tool-call";
745
+ toolCall: ToolCall;
746
+ }
747
+ interface ToolResultChunk {
748
+ type: "tool-result";
749
+ toolCallId: string;
750
+ result: unknown;
751
+ }
752
+ interface FinishChunk {
753
+ type: "finish";
754
+ finishReason: FinishReason;
755
+ usage?: TokenUsage;
756
+ }
757
+ interface ErrorChunk {
758
+ type: "error";
759
+ error: Error;
760
+ }
719
761
  /**
720
- * Convert a Message to Anthropic multimodal content blocks
762
+ * Parameters for generateText()
721
763
  */
722
- declare function messageToAnthropicContent(message: Message): string | AnthropicContentBlock[];
764
+ interface GenerateTextParams {
765
+ /** Language model to use */
766
+ model: LanguageModel;
767
+ /** Simple prompt (converted to user message) */
768
+ prompt?: string;
769
+ /** System prompt */
770
+ system?: string;
771
+ /** Full message history */
772
+ messages?: CoreMessage[];
773
+ /** Tools available to the LLM */
774
+ tools?: Record<string, Tool>;
775
+ /** Maximum agentic steps (tool call loops) */
776
+ maxSteps?: number;
777
+ /** Temperature (0-2) */
778
+ temperature?: number;
779
+ /** Maximum tokens to generate */
780
+ maxTokens?: number;
781
+ /** Structured-output / JSON-mode request format */
782
+ responseFormat?: ResponseFormat;
783
+ /** Abort signal */
784
+ signal?: AbortSignal;
785
+ }
723
786
  /**
724
- * Convert a Message to OpenAI multimodal content blocks
787
+ * Result from generateText()
725
788
  */
726
- declare function messageToOpenAIContent(message: Message): string | OpenAIContentBlock[];
789
+ interface GenerateTextResult {
790
+ /** Final text output */
791
+ text: string;
792
+ /** Token usage */
793
+ usage: TokenUsage;
794
+ /** Why generation stopped */
795
+ finishReason: FinishReason;
796
+ /** All steps taken (for agentic workflows) */
797
+ steps: GenerateStep[];
798
+ /** All tool calls made across all steps */
799
+ toolCalls: ToolCall[];
800
+ /** All tool results across all steps */
801
+ toolResults: ToolResult[];
802
+ /** Final message list including tool interactions */
803
+ response: {
804
+ messages: CoreMessage[];
805
+ };
806
+ }
727
807
  /**
728
- * Anthropic content block types (extended for tools)
808
+ * A single step in the generation process
729
809
  */
730
- type AnthropicToolUseBlock = {
731
- type: "tool_use";
732
- id: string;
733
- name: string;
734
- input: Record<string, unknown>;
735
- };
736
- type AnthropicToolResultBlock = {
737
- type: "tool_result";
738
- tool_use_id: string;
739
- content: string;
810
+ interface GenerateStep {
811
+ /** Text generated in this step */
812
+ text: string;
813
+ /** Tool calls made in this step */
814
+ toolCalls: ToolCall[];
815
+ /** Tool results from this step */
816
+ toolResults: ToolResult[];
817
+ /** Finish reason for this step */
818
+ finishReason: FinishReason;
819
+ /** Token usage for this step */
820
+ usage: TokenUsage;
821
+ }
822
+ /**
823
+ * Parameters for streamText() - same as generateText
824
+ */
825
+ type StreamTextParams = GenerateTextParams;
826
+ /**
827
+ * Result from streamText()
828
+ */
829
+ interface StreamTextResult {
830
+ /** Async iterable of text chunks only */
831
+ textStream: AsyncIterable<string>;
832
+ /** Async iterable of all stream parts */
833
+ fullStream: AsyncIterable<StreamPart>;
834
+ /** Promise that resolves to full text when complete */
835
+ readonly text: Promise<string>;
836
+ /** Promise that resolves to usage when complete */
837
+ readonly usage: Promise<TokenUsage>;
838
+ /** Promise that resolves to finish reason when complete */
839
+ readonly finishReason: Promise<FinishReason>;
840
+ /** Convert to plain text streaming Response */
841
+ toTextStreamResponse(options?: ResponseOptions): Response;
842
+ /** Convert to data stream Response (SSE with tool calls) */
843
+ toDataStreamResponse(options?: ResponseOptions): Response;
844
+ }
845
+ /**
846
+ * Stream part for fullStream
847
+ */
848
+ type StreamPart = {
849
+ type: "text-delta";
850
+ text: string;
851
+ } | {
852
+ type: "tool-call-start";
853
+ toolCallId: string;
854
+ toolName: string;
855
+ } | {
856
+ type: "tool-call-delta";
857
+ toolCallId: string;
858
+ argsText: string;
859
+ } | {
860
+ type: "tool-call-complete";
861
+ toolCall: ToolCall;
862
+ } | {
863
+ type: "tool-result";
864
+ toolCallId: string;
865
+ result: unknown;
866
+ } | {
867
+ type: "step-start";
868
+ step: number;
869
+ } | {
870
+ type: "step-finish";
871
+ step: number;
872
+ finishReason: FinishReason;
873
+ } | {
874
+ type: "finish";
875
+ finishReason: FinishReason;
876
+ usage: TokenUsage;
877
+ } | {
878
+ type: "error";
879
+ error: Error;
740
880
  };
741
- type AnthropicMessageContent = string | Array<AnthropicContentBlock | AnthropicToolUseBlock | AnthropicToolResultBlock>;
742
881
  /**
743
- * Format messages for Anthropic with full tool support
744
- * Handles: text, images, tool_use, and tool_result
745
- *
746
- * Key differences from OpenAI:
747
- * - tool_calls become tool_use blocks in assistant content
748
- * - tool results become tool_result blocks in user content
749
- */
750
- declare function formatMessagesForAnthropic(messages: Message[], systemPrompt?: string): {
751
- system: string;
752
- messages: Array<{
753
- role: "user" | "assistant";
754
- content: AnthropicMessageContent;
755
- }>;
756
- };
882
+ * Options for Response helpers
883
+ */
884
+ interface ResponseOptions {
885
+ /** Additional headers */
886
+ headers?: Record<string, string>;
887
+ /** Response status (default: 200) */
888
+ status?: number;
889
+ }
757
890
  /**
758
- * OpenAI message format with tool support
891
+ * Message format for storage adapters.
892
+ * Intentionally simpler than LLM-specific formats — adapters convert as needed.
759
893
  */
760
- type OpenAIMessage = {
761
- role: "system";
894
+ interface StorageMessage {
895
+ role: "user" | "assistant" | "system" | "tool";
762
896
  content: string;
763
- } | {
764
- role: "user";
765
- content: string | OpenAIContentBlock[];
766
- } | {
767
- role: "assistant";
768
- content: string | null;
769
- tool_calls?: Array<{
897
+ toolCalls?: unknown[];
898
+ toolCallId?: string;
899
+ /** Content type for the message — determines how it's stored in the backend */
900
+ contentType?: "text" | "image" | "file";
901
+ /** URL for image/file attachments */
902
+ url?: string;
903
+ metadata?: Record<string, unknown>;
904
+ }
905
+ /**
906
+ * Generic storage adapter interface for session + message persistence.
907
+ *
908
+ * `createYourGPT()` is the default implementation for YourGPT platform.
909
+ * Third-party developers can implement this interface for custom backends.
910
+ *
911
+ * @example
912
+ * ```ts
913
+ * import { createRuntime } from '@yourgpt/llm-sdk'
914
+ * import { createYourGPT } from '@yourgpt/llm-sdk/yourgpt'
915
+ *
916
+ * const runtime = createRuntime({
917
+ * provider: anthropic,
918
+ * model: 'claude-haiku-4-5',
919
+ * storage: createYourGPT({ apiKey, widgetUid }),
920
+ * })
921
+ * // runtime.chat() and runtime.stream() now auto-persist messages
922
+ * ```
923
+ */
924
+ interface StorageAdapter {
925
+ /** Create a new session. Returns session ID to use as threadId. */
926
+ createSession(data?: {
927
+ title?: string;
928
+ metadata?: Record<string, unknown>;
929
+ }): Promise<{
770
930
  id: string;
771
- type: "function";
772
- function: {
773
- name: string;
774
- arguments: string;
775
- };
776
931
  }>;
777
- } | {
778
- role: "tool";
779
- content: string;
780
- tool_call_id: string;
781
- };
932
+ /** Append messages to a session (called sequentially — input before output). */
933
+ saveMessages(sessionId: string, messages: StorageMessage[]): Promise<void>;
934
+ /** List sessions (optional — used for thread picker sync in future). */
935
+ getSessions?(): Promise<{
936
+ id: string;
937
+ title?: string;
938
+ updatedAt?: Date;
939
+ }[]>;
940
+ /** Get messages for a session (optional — used for thread restore in future). */
941
+ getMessages?(sessionId: string): Promise<StorageMessage[]>;
942
+ /**
943
+ * Upload a file to storage. Returns a URL the LLM can reference.
944
+ * When present, the server exposes a /upload endpoint and the client
945
+ * uses it instead of embedding base64 in the message body.
946
+ */
947
+ uploadFile?(file: StorageFile): Promise<{
948
+ url: string;
949
+ }>;
950
+ }
782
951
  /**
783
- * Format messages for OpenAI with full tool support
784
- * Handles: text, images, tool_calls, and tool results
952
+ * File data for upload via StorageAdapter.uploadFile()
785
953
  */
786
- declare function formatMessagesForOpenAI(messages: Message[], systemPrompt?: string): OpenAIMessage[];
954
+ interface StorageFile {
955
+ /** Base64-encoded file data (with or without data URI prefix) */
956
+ data: string;
957
+ /** MIME type (e.g., "image/png", "application/pdf") */
958
+ mimeType: string;
959
+ /** Original filename */
960
+ filename?: string;
961
+ }
787
962
 
788
- export { type ActionDefinition as A, type AnthropicContentBlock as B, type ChatCompletionRequest as C, type DoneEventMessage as D, type OpenAIContentBlock as E, type KnowledgeBaseConfig as K, type LLMAdapter as L, type Message as M, type OpenAIToolSelectionHints as O, type ProviderToolRuntimeOptions as P, type StreamEvent as S, type ToolDefinition as T, type UnifiedToolCall as U, type WebSearchConfig as W, type ToolProfile as a, type ToolCallInfo as b, type TokenUsageRaw as c, type ToolResponse as d, type AdapterFactory as e, type LLMConfig as f, type ToolLocation as g, type UnifiedToolResult as h, type ToolExecution as i, type AnthropicToolSelectionHints as j, type ToolNativeProviderHints as k, type OpenAIProviderToolOptions as l, type AnthropicProviderToolOptions as m, type Citation as n, type CompletionResult as o, formatMessages as p, formatTools as q, formatMessagesForAnthropic as r, formatMessagesForOpenAI as s, messageToAnthropicContent as t, messageToOpenAIContent as u, hasImageAttachments as v, hasMediaAttachments as w, attachmentToAnthropicImage as x, attachmentToAnthropicDocument as y, attachmentToOpenAIImage as z };
963
+ export { type ToolExecution as $, type ActionDefinition as A, type ToolResultChunk as B, type CoreMessage as C, type DoneEventMessage as D, type FinishChunk as E, type FilePart as F, type GenerateTextParams as G, type ErrorChunk as H, type ImagePart as I, type TokenUsage as J, type KnowledgeBaseConfig as K, type LanguageModel as L, type Message as M, type FinishReason as N, type ResponseOptions as O, type ProviderToolRuntimeOptions as P, type StorageFile as Q, type ResponseFormat as R, type StreamTextParams as S, type ToolContext as T, type UserMessage as U, DEFAULT_CAPABILITIES as V, type WebSearchConfig as W, type LLMConfig as X, type ToolLocation as Y, type UnifiedToolCall as Z, type UnifiedToolResult as _, type GenerateTextResult as a, type OpenAIToolSelectionHints as a0, type AnthropicToolSelectionHints as a1, type ToolNativeProviderHints as a2, type OpenAIProviderToolOptions as a3, type AnthropicProviderToolOptions as a4, type Citation as a5, type MessageAttachment as a6, type StreamTextResult as b, type Tool as c, type ToolDefinition as d, type ToolProfile as e, type StorageAdapter as f, type StreamEvent as g, type ToolCallInfo as h, type TokenUsageRaw as i, type ToolResponse as j, type StorageMessage as k, type ModelCapabilities as l, type DoGenerateParams as m, type DoGenerateResult as n, type SystemMessage as o, type AssistantMessage as p, type ToolMessage as q, type UserContentPart as r, type TextPart as s, type ToolCall as t, type ToolResult as u, type GenerateStep as v, type StreamPart as w, type StreamChunk as x, type TextDeltaChunk as y, type ToolCallChunk as z };