Open History to see the last commit
1import { useGeneration } from './use-generation.tsrx'
2import type { StreamChunk, SummarizationResult } from '@tanstack/ai'
3import type {
4 AIDevtoolsDisplayOptions,
5 ConnectConnectionAdapter,
6 GenerationClientState,
7 GenerationFetcher,
8 InferGenerationOutputFromReturn,
9 SummarizeGenerateInput,
10} from '@tanstack/ai-client'
11
12/**
13 * Options for the useSummarize hook.
14 *
15 * @template TOutput - The output type after optional transform (defaults to SummarizationResult)
16 */
17export interface UseSummarizeOptions<TOutput = SummarizationResult> {
18 /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
19 connection?: ConnectConnectionAdapter
20 /** Direct async function for summarization */
21 fetcher?: GenerationFetcher<SummarizeGenerateInput, SummarizationResult>
22 /** Stable scope for this generation. Also the wire and DevTools identity. */
23 threadId: string
24 /** Additional body parameters to send with connect-based adapter requests */
25 body?: Record<string, any>
26 /** Display options for TanStack AI Devtools. */
27 devtools?: AIDevtoolsDisplayOptions
28 /**
29 * Callback when summarization is complete. Can optionally return a transformed value.
30 *
31 * - Return a non-null value to transform and store it as the result
32 * - Return `null` to keep the previous result unchanged
33 * - Return nothing (`void`) to store the raw result as-is
34 */
35 onResult?: (result: SummarizationResult) => TOutput | null | void
36 /** Callback when an error occurs */
37 onError?: (error: Error) => void
38 /** Callback when progress is reported (0-100) */
39 onProgress?: (progress: number, message?: string) => void
40 /** Callback for each stream chunk (connect-based adapter mode only) */
41 onChunk?: (chunk: StreamChunk) => void
42}
43
44/**
45 * Return type for the useSummarize hook.
46 *
47 * @template TOutput - The output type (after optional transform)
48 */
49export interface UseSummarizeReturn<TOutput = SummarizationResult> {
50 /** Trigger summarization */
51 generate: (input: SummarizeGenerateInput) => Promise<void>
52 /** The summarization result, or null */
53 result: TOutput | null
54 /** Whether summarization is in progress */
55 isLoading: boolean
56 /** Current error, if any */
57 error: Error | undefined
58 /** Current state of the generation */
59 status: GenerationClientState
60 /** Abort the current summarization */
61 stop: () => void
62 /** Clear result, error, and return to idle */
63 reset: () => void
64}
65
66/**
67 * Octane hook for summarizing text using AI models.
68 *
69 * @example
70 * ```tsx
71 * import { useSummarize } from '@tanstack/ai-octane'
72 * import { fetchServerSentEvents } from '@tanstack/ai-client'
73 *
74 * function Summarizer() {
75 * const { generate, result, isLoading } = useSummarize({
76 * threadId: 'article-summary',
77 * connection: fetchServerSentEvents('/api/summarize'),
78 * })
79 *
80 * return (
81 * <div>
82 * <button onClick={() => generate({
83 * text: 'Long article text...',
84 * style: 'bullet-points',
85 * maxLength: 200,
86 * })}>
87 * Summarize
88 * </button>
89 * {isLoading && <p>Summarizing...</p>}
90 * {result && <p>{result.summary}</p>}
91 * </div>
92 * )
93 * }
94 * ```
95 */
96export function useSummarize<TTransformed = void>(
97 options: Omit<UseSummarizeOptions, 'onResult'> & {
98 onResult?: (result: SummarizationResult) => TTransformed
99 },
100): UseSummarizeReturn<
101 InferGenerationOutputFromReturn<SummarizationResult, TTransformed>
102> {
103 const devtools = {
104 ...options.devtools,
105 // OCTANE DIVERGENCE: identify this binding as Octane (not 'react') in TanStack AI Devtools.
106 framework: 'octane',
107 hookName: 'useSummarize',
108 outputKind: 'text' as const,
109 }
110 const { generate, result, isLoading, error, status, stop, reset } =
111 useGeneration<SummarizeGenerateInput, SummarizationResult, TTransformed>({
112 ...options,
113 devtools,
114 })
115
116 return {
117 generate: generate as (input: SummarizeGenerateInput) => Promise<void>,
118 result,
119 isLoading,
120 error,
121 status,
122 stop,
123 reset,
124 }
125}