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

summarize.ts

77 lines2.4 KB

1import { ChatStreamSummarizeAdapter } from '@tanstack/ai/adapters'
2import { getGrokApiKeyFromEnv } from '../utils/client'
3import { GrokTextAdapter } from './text'
4import type { InferTextProviderOptions } from '@tanstack/ai/adapters'
5import type { GROK_CHAT_MODELS } from '../model-meta'
6import type { GrokClientConfig } from '../utils/client'
7​
8/**
9 * Configuration for Grok summarize adapter
10 */
11export interface GrokSummarizeConfig extends GrokClientConfig {}
12​
13/** Model type for Grok summarization */
14export type GrokSummarizeModel = (typeof GROK_CHAT_MODELS)[number]
15​
16/**
17 * Creates a Grok summarize adapter with explicit API key.
18 * Type resolution happens here at the call site.
19 *
20 * @param model - The model name (e.g., 'grok-build-0.1')
21 * @param apiKey - Your xAI API key
22 * @param config - Optional additional configuration
23 * @returns Configured Grok summarize adapter instance with resolved types
24 *
25 * @example
26 * ```typescript
27 * const adapter = createGrokSummarize('grok-build-0.1', "xai-...");
28 * ```
29 */
30export function createGrokSummarize<TModel extends GrokSummarizeModel>(
31 model: TModel,
32 apiKey: string,
33 config?: Omit<GrokSummarizeConfig, 'apiKey'>,
34): ChatStreamSummarizeAdapter<
35 TModel,
36 InferTextProviderOptions<GrokTextAdapter<TModel>>
37> {
38 return new ChatStreamSummarizeAdapter(
39 new GrokTextAdapter({ apiKey, ...config }, model),
40 model,
41 'grok',
42 )
44​
45/**
46 * Creates a Grok summarize adapter with automatic API key detection from environment variables.
47 * Type resolution happens here at the call site.
48 *
49 * Looks for `XAI_API_KEY` in:
50 * - `process.env` (Node.js)
51 * - `window.env` (Browser with injected env)
52 *
53 * @param model - The model name (e.g., 'grok-build-0.1')
54 * @param config - Optional configuration (excluding apiKey which is auto-detected)
55 * @returns Configured Grok summarize adapter instance with resolved types
56 * @throws Error if XAI_API_KEY is not found in environment
57 *
58 * @example
59 * ```typescript
60 * // Automatically uses XAI_API_KEY from environment
61 * const adapter = grokSummarize('grok-build-0.1');
62 *
63 * await summarize({
64 * adapter,
65 * text: "Long article text..."
66 * });
67 * ```
68 */
69export function grokSummarize<TModel extends GrokSummarizeModel>(
70 model: TModel,
71 config?: Omit<GrokSummarizeConfig, 'apiKey'>,
72): ChatStreamSummarizeAdapter<
73 TModel,
74 InferTextProviderOptions<GrokTextAdapter<TModel>>
75> {
76 return createGrokSummarize(model, getGrokApiKeyFromEnv(), config)