Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

adapter.ts

90 lines3.1 KB

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