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

summarize.ts

81 lines2.7 KB

1import { ChatStreamSummarizeAdapter } from '@tanstack/ai/adapters'
2import { getOpenRouterApiKeyFromEnv } from '../utils'
3import { OpenRouterTextAdapter } from './text'
4import type { InferTextProviderOptions } from '@tanstack/ai/adapters'
5import type { OpenRouterConfig } from './text'
6import type { OPENROUTER_CHAT_MODELS } from '../model-meta'
7​
8export type OpenRouterTextModels = (typeof OPENROUTER_CHAT_MODELS)[number]
9​
10/**
11 * Configuration for OpenRouter summarize adapter
12 */
13export interface OpenRouterSummarizeConfig extends OpenRouterConfig {
14 /** Default temperature for summarization (0-2). Defaults to 0.3. */
15 temperature?: number
16 /** Default maximum tokens in the response */
17 maxTokens?: number
19​
20/**
21 * Creates an OpenRouter summarize adapter with explicit API key.
22 * Type resolution happens here at the call site.
23 *
24 * @param model - The model name (e.g., 'openai/gpt-4o-mini', 'anthropic/claude-3-5-sonnet')
25 * @param apiKey - Your OpenRouter API key
26 * @param config - Optional additional configuration
27 * @returns Configured OpenRouter summarize adapter instance with resolved types
28 *
29 * @example
30 * ```typescript
31 * const adapter = createOpenRouterSummarize('openai/gpt-4o-mini', 'sk-or-...');
32 * ```
33 */
34export function createOpenRouterSummarize<TModel extends OpenRouterTextModels>(
35 model: TModel,
36 apiKey: string,
37 config?: Omit<OpenRouterConfig, 'apiKey'>,
38): ChatStreamSummarizeAdapter<
39 TModel,
40 InferTextProviderOptions<OpenRouterTextAdapter<TModel>>
41> {
42 return new ChatStreamSummarizeAdapter(
43 new OpenRouterTextAdapter({ apiKey, ...config }, model),
44 model,
45 'openrouter',
46 )
48​
49/**
50 * Creates an OpenRouter summarize adapter with automatic API key detection from environment variables.
51 * Type resolution happens here at the call site.
52 *
53 * Looks for `OPENROUTER_API_KEY` in:
54 * - `process.env` (Node.js)
55 * - `window.env` (Browser with injected env)
56 *
57 * @param model - The model name (e.g., 'openai/gpt-4o-mini', 'anthropic/claude-3-5-sonnet')
58 * @param config - Optional configuration (excluding apiKey which is auto-detected)
59 * @returns Configured OpenRouter summarize adapter instance with resolved types
60 * @throws Error if OPENROUTER_API_KEY is not found in environment
61 *
62 * @example
63 * ```typescript
64 * // Automatically uses OPENROUTER_API_KEY from environment
65 * const adapter = openRouterSummarize('openai/gpt-4o-mini');
66 *
67 * await summarize({
68 * adapter,
69 * text: "Long article text..."
70 * });
71 * ```
72 */
73export function openRouterSummarize<TModel extends OpenRouterTextModels>(
74 model: TModel,
75 config?: Omit<OpenRouterConfig, 'apiKey'>,
76): ChatStreamSummarizeAdapter<
77 TModel,
78 InferTextProviderOptions<OpenRouterTextAdapter<TModel>>
79> {
80 return createOpenRouterSummarize(model, getOpenRouterApiKeyFromEnv(), config)