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

text.ts

104 lines4.0 KB

1import OpenAI from 'openai'
2import { OpenAIBaseChatCompletionsTextAdapter } from '@tanstack/openai-base'
3import { withBedrockDefaults } from '../utils/client'
4import type { Modality } from '@tanstack/ai'
5import type { BedrockClientConfig } from '../utils/client'
6import type { BedrockMessageMetadataByModality } from '../message-types'
7import type {
8 BedrockChatModelToolCapabilitiesByName,
9 BedrockChatModels,
10 ResolveInputModalities,
11 ResolveProviderOptions,
12} from '../model-meta'
13import type { OpenAIBaseTextAdapterOptions } from '@tanstack/openai-base'
14​
15export interface BedrockTextConfig
16 extends BedrockClientConfig, OpenAIBaseTextAdapterOptions {}
17​
18export type { ExternalTextProviderOptions as BedrockTextProviderOptions } from '../text/text-provider-options'
19​
20type ResolveToolCapabilities<TModel extends string> =
21 TModel extends keyof BedrockChatModelToolCapabilitiesByName
22 ? NonNullable<BedrockChatModelToolCapabilitiesByName[TModel]>
23 : readonly []
24​
25/**
26 * Bedrock Chat Completions adapter. Drives Bedrock's OpenAI-compatible
27 * `/chat/completions` endpoint via the OpenAI SDK with a baseURL override
28 * (same pattern as ai-groq). Tool conversion, streaming, structured output,
29 * and the agent loop come from the base.
30 */
31export class BedrockTextAdapter<
32 TModel extends BedrockChatModels,
33 // Constraint mirrors ai-groq and the base, which parameterises
34 // `TProviderOptions extends Record<string, any>`. Our default
35 // `ResolveProviderOptions<TModel>` resolves to the `BedrockTextProviderOptions`
36 // interface, which (lacking an implicit index signature) `Record<string,
37 // unknown>` would reject but `Record<string, any>` accepts. This `any` is
38 // confined to the generic constraint (the established ai-groq pattern) — no
39 // value/shape `as` cast is introduced.
40 TProviderOptions extends Record<string, any> = ResolveProviderOptions<TModel>,
41 TInputModalities extends ReadonlyArray<Modality> =
42 ResolveInputModalities<TModel>,
43 TToolCapabilities extends ReadonlyArray<string> =
44 ResolveToolCapabilities<TModel>,
45> extends OpenAIBaseChatCompletionsTextAdapter<
46 TModel,
47 TProviderOptions,
48 TInputModalities,
49 BedrockMessageMetadataByModality,
50 TToolCapabilities
51> {
52 override readonly kind = 'text' as const
53 override readonly name = 'bedrock' as const
54​
55 constructor(config: BedrockTextConfig, model: TModel) {
56 // No `forced` -> honors config.endpoint ('runtime' default, 'mantle' allowed).
57 super(
58 model,
59 'bedrock',
60 new OpenAI(withBedrockDefaults(config, undefined, model)),
61 config,
62 )
63 }
64​
65 /**
66 * Surface reasoning deltas (gpt-oss / Claude reasoning) the OpenAI-compatible
67 * way. Base types the chunk as `unknown`; narrow with runtime guards — no
68 * `as` casts, no `any`.
69 */
70 protected override extractReasoning(
71 chunk: unknown,
72 ): { text: string } | undefined {
73 return readDeltaReasoning(chunk)
74 }
76​
77/** Cast-free narrowing of a Chat Completions chunk's reasoning delta. */
78function readDeltaReasoning(chunk: unknown): { text: string } | undefined {
79 if (typeof chunk !== 'object' || chunk === null || !('choices' in chunk))
80 return undefined
81 if (!Array.isArray(chunk.choices)) return undefined
82 const choice: unknown = chunk.choices[0]
83 if (typeof choice !== 'object' || choice === null || !('delta' in choice))
84 return undefined
85 const delta = choice.delta
86 if (typeof delta !== 'object' || delta === null) return undefined
87 const raw =
88 'reasoning' in delta && typeof delta.reasoning === 'string'
89 ? delta.reasoning
90 : 'reasoning_content' in delta &&
91 typeof delta.reasoning_content === 'string'
92 ? delta.reasoning_content
93 : undefined
94 return raw && raw.length > 0 ? { text: raw } : undefined
96​
97/** Chat adapter with an explicit API key (low-level; the public branching factory delegates here). */
98export function createBedrockChat<TModel extends BedrockChatModels>(
99 model: TModel,
100 apiKey: string,
101 config?: Omit<BedrockTextConfig, 'apiKey'>,
102): BedrockTextAdapter<TModel> {
103 return new BedrockTextAdapter({ ...config, apiKey }, model)