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

use-summarize.ts

136 lines4.1 KB

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