Open History to see the last commit
1import { ChatStreamSummarizeAdapter } from '@tanstack/ai/adapters'
2import { getOpenAIApiKeyFromEnv } from '../utils/client'
3import { OpenAITextAdapter } from './text'
4import type { InferTextProviderOptions } from '@tanstack/ai/adapters'
5import type { OpenAIChatModel } from '../model-meta'
6import type { OpenAIClientConfig } from '../utils/client'
7
8/**
9 * Configuration for OpenAI summarize adapter
10 */
11export interface OpenAISummarizeConfig extends OpenAIClientConfig {}
12
13/**
14 * Creates an OpenAI summarize adapter with explicit API key.
15 * Type resolution happens here at the call site.
16 *
17 * @param model - The model name (e.g., 'gpt-4o-mini', 'gpt-4o')
18 * @param apiKey - Your OpenAI API key
19 * @param config - Optional additional configuration
20 * @returns Configured OpenAI summarize adapter instance with resolved types
21 *
22 * @example
23 * ```typescript
24 * const adapter = createOpenaiSummarize('gpt-4o-mini', "sk-...");
25 * ```
26 */
27export function createOpenaiSummarize<TModel extends OpenAIChatModel>(
28 model: TModel,
29 apiKey: string,
30 config?: Omit<OpenAISummarizeConfig, 'apiKey'>,
31): ChatStreamSummarizeAdapter<
32 TModel,
33 InferTextProviderOptions<OpenAITextAdapter<TModel>>
34> {
35 return new ChatStreamSummarizeAdapter(
36 new OpenAITextAdapter({ apiKey, ...config }, model),
37 model,
38 'openai',
39 )
40}
41
42/**
43 * Creates an OpenAI summarize adapter with automatic API key detection from environment variables.
44 * Type resolution happens here at the call site.
45 *
46 * Looks for `OPENAI_API_KEY` in:
47 * - `process.env` (Node.js)
48 * - `window.env` (Browser with injected env)
49 *
50 * @param model - The model name (e.g., 'gpt-4o-mini', 'gpt-4o')
51 * @param config - Optional configuration (excluding apiKey which is auto-detected)
52 * @returns Configured OpenAI summarize adapter instance with resolved types
53 * @throws Error if OPENAI_API_KEY is not found in environment
54 *
55 * @example
56 * ```typescript
57 * // Automatically uses OPENAI_API_KEY from environment
58 * const adapter = openaiSummarize('gpt-4o-mini');
59 *
60 * await summarize({
61 * adapter,
62 * text: "Long article text..."
63 * });
64 * ```
65 */
66export function openaiSummarize<TModel extends OpenAIChatModel>(
67 model: TModel,
68 config?: Omit<OpenAISummarizeConfig, 'apiKey'>,
69): ChatStreamSummarizeAdapter<
70 TModel,
71 InferTextProviderOptions<OpenAITextAdapter<TModel>>
72> {
73 return createOpenaiSummarize(model, getOpenAIApiKeyFromEnv(), config)
74}