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 /** Unique identifier for this generation instance */
23 id?: 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 * connection: fetchServerSentEvents('/api/summarize'),
77 * })
78 *
79 * return (
80 * <div>
81 * <button onClick={() => generate({
82 * text: 'Long article text...',
83 * style: 'bullet-points',
84 * maxLength: 200,
85 * })}>
86 * Summarize
87 * </button>
88 * {isLoading && <p>Summarizing...</p>}
89 * {result && <p>{result.summary}</p>}
90 * </div>
91 * )
92 * }
93 * ```
94 */
95export function useSummarize<TTransformed = void>(
96 options: Omit<UseSummarizeOptions, 'onResult'> & {
97 onResult?: (result: SummarizationResult) => TTransformed
98 },
99): UseSummarizeReturn<
100 InferGenerationOutputFromReturn<SummarizationResult, TTransformed>
101> {
102 const devtools = {
103 ...options.devtools,
104 // OCTANE DIVERGENCE: identify this binding as Octane (not 'react') in TanStack AI Devtools.
105 framework: 'octane',
106 hookName: 'useSummarize',
107 outputKind: 'text' as const,
108 }
109 const { generate, result, isLoading, error, status, stop, reset } =
110 useGeneration<SummarizeGenerateInput, SummarizationResult, TTransformed>({
111 ...options,
112 devtools,
113 })
114
115 return {
116 generate: generate as (input: SummarizeGenerateInput) => Promise<void>,
117 result,
118 isLoading,
119 error,
120 status,
121 stop,
122 reset,
123 }
124}