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