Open History to see the last commit
1import {
2 OpenAIBaseChatCompletionsTextAdapter,
3 OpenAIBaseResponsesTextAdapter,
4} from '@tanstack/openai-base'
5import type OpenAI from 'openai'
6import type { ClientOptions } from 'openai'
7import type { Modality } from '@tanstack/ai'
8import type { OpenAIMessageMetadataByModality } from '../message-types'
9import type { OpenAIBaseTextAdapterOptions } from '@tanstack/openai-base'
10
11/**
12 * Generic OpenAI-compatible adapter over the Chat Completions API
13 * (`{baseURL}/chat/completions`). Capability type-args are supplied by the
14 * `openaiCompatible` factory from the user's `models` tuple.
15 */
16export class OpenAICompatibleChatAdapter<
17 TModel extends string,
18 TProviderOptions extends Record<string, any> = Record<string, any>,
19 TInputModalities extends ReadonlyArray<Modality> = ReadonlyArray<Modality>,
20 TToolCapabilities extends ReadonlyArray<string> = ReadonlyArray<string>,
21> extends OpenAIBaseChatCompletionsTextAdapter<
22 TModel,
23 TProviderOptions,
24 TInputModalities,
25 OpenAIMessageMetadataByModality,
26 TToolCapabilities
27> {
28 override readonly kind = 'text' as const
29 readonly maxTokensKey = 'max_tokens'
30
31 constructor(
32 client: OpenAI,
33 model: TModel,
34 name: string,
35 options?: OpenAIBaseTextAdapterOptions & Pick<ClientOptions, 'fetch'>,
36 ) {
37 super(model, name, client, options)
38 }
39
40 /**
41 * OpenAI-compatible reasoning providers stream their thinking outside the
42 * OpenAI wire format, on `delta.reasoning_content` (DeepSeek, Qwen, GLM,
43 * Kimi, most vLLM/SGLang deployments), `delta.reasoning` (a smaller set of
44 * gateways), or `delta.reasoning_text` (GitHub Copilot). The base adapter has no reasoning hook by default because plain
45 * Chat Completions carries none, so without this the thinking was dropped
46 * silently and the only way to see it was to monkey-patch the prototype.
47 *
48 * Same shape as the dedicated adapters that already do this
49 * (`@tanstack/ai-cloudflare`, `@tanstack/ai-byteplus`, `@tanstack/ai-groq`).
50 * Providers that send neither field are unaffected.
51 */
52 protected override extractReasoning(
53 chunk: OpenAI.Chat.Completions.ChatCompletionChunk,
54 ): { text: string } | undefined {
55 const delta = chunk.choices?.[0]?.delta as
56 | {
57 reasoning?: unknown
58 reasoning_content?: unknown
59 reasoning_text?: unknown
60 }
61 | undefined
62 const raw =
63 delta?.reasoning_content ?? delta?.reasoning ?? delta?.reasoning_text
64 return typeof raw === 'string' && raw.length > 0 ? { text: raw } : undefined
65 }
66}
67
68/**
69 * Generic OpenAI-compatible adapter over the Responses API
70 * (`{baseURL}/responses`). For the rare compatible provider that implements
71 * Responses (e.g. Azure OpenAI).
72 */
73export class OpenAICompatibleResponsesAdapter<
74 TModel extends string,
75 TProviderOptions extends Record<string, any> = Record<string, any>,
76 TInputModalities extends ReadonlyArray<Modality> = ReadonlyArray<Modality>,
77 TToolCapabilities extends ReadonlyArray<string> = ReadonlyArray<string>,
78> extends OpenAIBaseResponsesTextAdapter<
79 TModel,
80 TProviderOptions,
81 TInputModalities,
82 OpenAIMessageMetadataByModality,
83 TToolCapabilities
84> {
85 override readonly kind = 'text' as const
86 readonly maxTokensKey = 'max_output_tokens'
87
88 constructor(
89 client: OpenAI,
90 model: TModel,
91 name: string,
92 options?: OpenAIBaseTextAdapterOptions & Pick<ClientOptions, 'fetch'>,
93 ) {
94 super(model, name, client, options)
95 }
96}