@octanejs/tanstack-ai 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +123 -0
- package/package.json +54 -0
- package/src/index.ts +62 -0
- package/src/realtime-types.ts +146 -0
- package/src/types.ts +213 -0
- package/src/use-audio-recorder.tsrx +112 -0
- package/src/use-audio-recorder.tsrx.d.ts +44 -0
- package/src/use-chat.tsrx +363 -0
- package/src/use-chat.tsrx.d.ts +5 -0
- package/src/use-generate-audio.tsrx +125 -0
- package/src/use-generate-audio.tsrx.d.ts +88 -0
- package/src/use-generate-image.tsrx +127 -0
- package/src/use-generate-image.tsrx.d.ts +90 -0
- package/src/use-generate-speech.tsrx +121 -0
- package/src/use-generate-speech.tsrx.d.ts +84 -0
- package/src/use-generate-video.tsrx +245 -0
- package/src/use-generate-video.tsrx.d.ts +95 -0
- package/src/use-generation.tsrx +216 -0
- package/src/use-generation.tsrx.d.ts +81 -0
- package/src/use-mcp-app-bridge.tsrx +60 -0
- package/src/use-mcp-app-bridge.tsrx.d.ts +26 -0
- package/src/use-realtime-chat.tsrx +307 -0
- package/src/use-realtime-chat.tsrx.d.ts +43 -0
- package/src/use-summarize.tsrx +124 -0
- package/src/use-summarize.tsrx.d.ts +87 -0
- package/src/use-transcription.tsrx +131 -0
- package/src/use-transcription.tsrx.d.ts +93 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Declaration companion generated from use-mcp-app-bridge.tsrx.
|
|
2
|
+
import type { CreateMcpAppBridgeOptions, McpAppBridge } from '@tanstack/ai-client';
|
|
3
|
+
export type UseMcpAppBridgeOptions = CreateMcpAppBridgeOptions;
|
|
4
|
+
/**
|
|
5
|
+
* Octane wrapper around `createMcpAppBridge` that returns a **stable** bridge for
|
|
6
|
+
* the given `threadId`/`callEndpoint`, while always invoking the latest
|
|
7
|
+
* `chat.sendMessage` and `onLink` (kept in refs). This avoids both recreating
|
|
8
|
+
* the bridge on every render and the stale-closure / `exhaustive-deps` dance
|
|
9
|
+
* you'd otherwise write by hand:
|
|
10
|
+
*
|
|
11
|
+
* ```tsx
|
|
12
|
+
* const { sendMessage } = useChat({ threadId, connection })
|
|
13
|
+
* const bridge = useMcpAppBridge({
|
|
14
|
+
* threadId,
|
|
15
|
+
* callEndpoint: '/api/mcp-apps-call',
|
|
16
|
+
* chat: { sendMessage: async (content) => void sendMessage(content) },
|
|
17
|
+
* onLink: (url) => window.open(url, '_blank', 'noopener,noreferrer'),
|
|
18
|
+
* })
|
|
19
|
+
* // pass `bridge` to <MCPAppResource bridge={bridge} … />
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* The bridge is recreated only when `threadId`, `callEndpoint`, `fetchImpl`, or
|
|
23
|
+
* the *presence* of `onLink` changes — passing a new inline `onLink`/`sendMessage`
|
|
24
|
+
* each render does not churn it.
|
|
25
|
+
*/
|
|
26
|
+
export declare function useMcpAppBridge(options: UseMcpAppBridgeOptions): McpAppBridge;
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'octane'
|
|
2
|
+
import { RealtimeClient } from '@tanstack/ai-client'
|
|
3
|
+
import type {
|
|
4
|
+
RealtimeMessage,
|
|
5
|
+
RealtimeMode,
|
|
6
|
+
RealtimeSessionConfig,
|
|
7
|
+
RealtimeStatus,
|
|
8
|
+
} from '@tanstack/ai'
|
|
9
|
+
import type {
|
|
10
|
+
UseRealtimeChatOptions,
|
|
11
|
+
UseRealtimeChatReturn,
|
|
12
|
+
} from './realtime-types'
|
|
13
|
+
|
|
14
|
+
// Empty frequency data for when client is not connected
|
|
15
|
+
const emptyFrequencyData = new Uint8Array(128)
|
|
16
|
+
const emptyTimeDomainData = new Uint8Array(128).fill(128)
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Octane hook for realtime voice conversations.
|
|
20
|
+
*
|
|
21
|
+
* Provides a simple interface for voice-to-voice AI interactions
|
|
22
|
+
* with support for multiple providers (OpenAI, ElevenLabs, etc.).
|
|
23
|
+
*
|
|
24
|
+
* @param options - Configuration options including adapter and callbacks
|
|
25
|
+
* @returns Hook return value with state and control methods
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* import { useRealtimeChat } from '@octanejs/tanstack-ai'
|
|
30
|
+
* import { openaiRealtime } from '@tanstack/ai-openai'
|
|
31
|
+
*
|
|
32
|
+
* function VoiceChat() {
|
|
33
|
+
* const {
|
|
34
|
+
* status,
|
|
35
|
+
* mode,
|
|
36
|
+
* messages,
|
|
37
|
+
* connect,
|
|
38
|
+
* disconnect,
|
|
39
|
+
* inputLevel,
|
|
40
|
+
* outputLevel,
|
|
41
|
+
* } = useRealtimeChat({
|
|
42
|
+
* getToken: () => fetch('/api/realtime-token').then(r => r.json()),
|
|
43
|
+
* adapter: openaiRealtime(),
|
|
44
|
+
* })
|
|
45
|
+
*
|
|
46
|
+
* return (
|
|
47
|
+
* <div>
|
|
48
|
+
* <p>Status: {status}</p>
|
|
49
|
+
* <p>Mode: {mode}</p>
|
|
50
|
+
* <button onClick={status === 'idle' ? connect : disconnect}>
|
|
51
|
+
* {status === 'idle' ? 'Start' : 'Stop'}
|
|
52
|
+
* </button>
|
|
53
|
+
* </div>
|
|
54
|
+
* )
|
|
55
|
+
* }
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export function useRealtimeChat(
|
|
59
|
+
options: UseRealtimeChatOptions,
|
|
60
|
+
): UseRealtimeChatReturn {
|
|
61
|
+
// State
|
|
62
|
+
const [status, setStatus] = useState<RealtimeStatus>('idle')
|
|
63
|
+
const [mode, setMode] = useState<RealtimeMode>('idle')
|
|
64
|
+
const [messages, setMessages] = useState<Array<RealtimeMessage>>([])
|
|
65
|
+
const [pendingUserTranscript, setPendingUserTranscript] = useState<
|
|
66
|
+
string | null
|
|
67
|
+
>(null)
|
|
68
|
+
const [pendingAssistantTranscript, setPendingAssistantTranscript] = useState<
|
|
69
|
+
string | null
|
|
70
|
+
>(null)
|
|
71
|
+
const [error, setError] = useState<Error | null>(null)
|
|
72
|
+
const [inputLevel, setInputLevel] = useState(0)
|
|
73
|
+
const [outputLevel, setOutputLevel] = useState(0)
|
|
74
|
+
|
|
75
|
+
// Refs
|
|
76
|
+
const clientRef = useRef<RealtimeClient | null>(null)
|
|
77
|
+
const optionsRef = useRef(options)
|
|
78
|
+
optionsRef.current = options
|
|
79
|
+
const animationFrameRef = useRef<number | null>(null)
|
|
80
|
+
|
|
81
|
+
// Create client instance - use ref to ensure we reuse the same instance
|
|
82
|
+
// This handles React StrictMode double-rendering
|
|
83
|
+
if (!clientRef.current) {
|
|
84
|
+
// Each optional source field is spread conditionally because the
|
|
85
|
+
// `RealtimeClientOptions` target declares strict optionals
|
|
86
|
+
// (`field?: T`) and `exactOptionalPropertyTypes` rejects passing
|
|
87
|
+
// `undefined` for absent values.
|
|
88
|
+
const initial = optionsRef.current
|
|
89
|
+
clientRef.current = new RealtimeClient({
|
|
90
|
+
// OCTANE DIVERGENCE: upstream captures the first transport callbacks.
|
|
91
|
+
// Ref-backed delegates keep the client stable while reconnects and token
|
|
92
|
+
// refreshes observe the latest render's authentication and adapter.
|
|
93
|
+
getToken: () => optionsRef.current.getToken(),
|
|
94
|
+
adapter: {
|
|
95
|
+
get provider() {
|
|
96
|
+
return optionsRef.current.adapter.provider
|
|
97
|
+
},
|
|
98
|
+
connect(token, tools) {
|
|
99
|
+
return optionsRef.current.adapter.connect(token, tools)
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
...(initial.tools !== undefined && { tools: initial.tools }),
|
|
103
|
+
...(initial.instructions !== undefined && {
|
|
104
|
+
instructions: initial.instructions,
|
|
105
|
+
}),
|
|
106
|
+
...(initial.voice !== undefined && { voice: initial.voice }),
|
|
107
|
+
...(initial.autoPlayback !== undefined && {
|
|
108
|
+
autoPlayback: initial.autoPlayback,
|
|
109
|
+
}),
|
|
110
|
+
...(initial.autoCapture !== undefined && {
|
|
111
|
+
autoCapture: initial.autoCapture,
|
|
112
|
+
}),
|
|
113
|
+
...(initial.vadMode !== undefined && { vadMode: initial.vadMode }),
|
|
114
|
+
...(initial.outputModalities !== undefined && {
|
|
115
|
+
outputModalities: initial.outputModalities,
|
|
116
|
+
}),
|
|
117
|
+
...(initial.temperature !== undefined && {
|
|
118
|
+
temperature: initial.temperature,
|
|
119
|
+
}),
|
|
120
|
+
...(initial.maxOutputTokens !== undefined && {
|
|
121
|
+
maxOutputTokens: initial.maxOutputTokens,
|
|
122
|
+
}),
|
|
123
|
+
...(initial.semanticEagerness !== undefined && {
|
|
124
|
+
semanticEagerness: initial.semanticEagerness,
|
|
125
|
+
}),
|
|
126
|
+
onStatusChange: (newStatus) => {
|
|
127
|
+
setStatus(newStatus)
|
|
128
|
+
// OCTANE DIVERGENCE: upstream declares this callback but does not
|
|
129
|
+
// forward status changes beyond its own hook state.
|
|
130
|
+
optionsRef.current.onStatusChange?.(newStatus)
|
|
131
|
+
},
|
|
132
|
+
onModeChange: (newMode) => {
|
|
133
|
+
setMode(newMode)
|
|
134
|
+
optionsRef.current.onModeChange?.(newMode)
|
|
135
|
+
},
|
|
136
|
+
onMessage: (message) => {
|
|
137
|
+
setMessages((prev) => [...prev, message])
|
|
138
|
+
optionsRef.current.onMessage?.(message)
|
|
139
|
+
},
|
|
140
|
+
onUsage: (usage) => {
|
|
141
|
+
optionsRef.current.onUsage?.(usage)
|
|
142
|
+
},
|
|
143
|
+
onGoAway: (timeLeft) => {
|
|
144
|
+
optionsRef.current.onGoAway?.(timeLeft)
|
|
145
|
+
},
|
|
146
|
+
onError: (err) => {
|
|
147
|
+
setError(err)
|
|
148
|
+
optionsRef.current.onError?.(err)
|
|
149
|
+
},
|
|
150
|
+
onConnect: () => {
|
|
151
|
+
setError(null)
|
|
152
|
+
optionsRef.current.onConnect?.()
|
|
153
|
+
},
|
|
154
|
+
onDisconnect: () => {
|
|
155
|
+
optionsRef.current.onDisconnect?.()
|
|
156
|
+
},
|
|
157
|
+
onInterrupted: () => {
|
|
158
|
+
setPendingAssistantTranscript(null)
|
|
159
|
+
optionsRef.current.onInterrupted?.()
|
|
160
|
+
},
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
// Subscribe to state changes for transcripts
|
|
164
|
+
clientRef.current.onStateChange((state) => {
|
|
165
|
+
setPendingUserTranscript(state.pendingUserTranscript)
|
|
166
|
+
setPendingAssistantTranscript(state.pendingAssistantTranscript)
|
|
167
|
+
})
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const client = clientRef.current
|
|
171
|
+
|
|
172
|
+
// Audio level animation loop
|
|
173
|
+
useEffect(() => {
|
|
174
|
+
function updateLevels() {
|
|
175
|
+
if (clientRef.current?.audio) {
|
|
176
|
+
setInputLevel(clientRef.current.audio.inputLevel)
|
|
177
|
+
setOutputLevel(clientRef.current.audio.outputLevel)
|
|
178
|
+
}
|
|
179
|
+
animationFrameRef.current = requestAnimationFrame(updateLevels)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (status === 'connected') {
|
|
183
|
+
updateLevels()
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return () => {
|
|
187
|
+
if (animationFrameRef.current) {
|
|
188
|
+
cancelAnimationFrame(animationFrameRef.current)
|
|
189
|
+
animationFrameRef.current = null
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}, [status])
|
|
193
|
+
|
|
194
|
+
// Cleanup on unmount
|
|
195
|
+
useEffect(() => {
|
|
196
|
+
return () => {
|
|
197
|
+
clientRef.current?.destroy()
|
|
198
|
+
}
|
|
199
|
+
}, [])
|
|
200
|
+
|
|
201
|
+
// Connection methods
|
|
202
|
+
const connect = useCallback(async () => {
|
|
203
|
+
setError(null)
|
|
204
|
+
setMessages([])
|
|
205
|
+
setPendingUserTranscript(null)
|
|
206
|
+
setPendingAssistantTranscript(null)
|
|
207
|
+
await client.connect()
|
|
208
|
+
}, [client])
|
|
209
|
+
|
|
210
|
+
const disconnect = useCallback(async () => {
|
|
211
|
+
await client.disconnect()
|
|
212
|
+
}, [client])
|
|
213
|
+
|
|
214
|
+
// Voice control methods
|
|
215
|
+
const startListening = useCallback(() => {
|
|
216
|
+
client.startListening()
|
|
217
|
+
}, [client])
|
|
218
|
+
|
|
219
|
+
const stopListening = useCallback(() => {
|
|
220
|
+
client.stopListening()
|
|
221
|
+
}, [client])
|
|
222
|
+
|
|
223
|
+
const interrupt = useCallback(() => {
|
|
224
|
+
client.interrupt()
|
|
225
|
+
}, [client])
|
|
226
|
+
|
|
227
|
+
// Text input
|
|
228
|
+
const sendText = useCallback(
|
|
229
|
+
(text: string) => {
|
|
230
|
+
client.sendText(text)
|
|
231
|
+
},
|
|
232
|
+
[client],
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
// Image input
|
|
236
|
+
const sendImage = useCallback(
|
|
237
|
+
(imageData: string, mimeType: string) => {
|
|
238
|
+
client.sendImage(imageData, mimeType)
|
|
239
|
+
},
|
|
240
|
+
[client],
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
// Audio visualization
|
|
244
|
+
const getInputFrequencyData = useCallback(() => {
|
|
245
|
+
return (
|
|
246
|
+
clientRef.current?.audio?.getInputFrequencyData() ?? emptyFrequencyData
|
|
247
|
+
)
|
|
248
|
+
}, [])
|
|
249
|
+
|
|
250
|
+
const getOutputFrequencyData = useCallback(() => {
|
|
251
|
+
return (
|
|
252
|
+
clientRef.current?.audio?.getOutputFrequencyData() ?? emptyFrequencyData
|
|
253
|
+
)
|
|
254
|
+
}, [])
|
|
255
|
+
|
|
256
|
+
const getInputTimeDomainData = useCallback(() => {
|
|
257
|
+
return (
|
|
258
|
+
clientRef.current?.audio?.getInputTimeDomainData() ?? emptyTimeDomainData
|
|
259
|
+
)
|
|
260
|
+
}, [])
|
|
261
|
+
|
|
262
|
+
const getOutputTimeDomainData = useCallback(() => {
|
|
263
|
+
return (
|
|
264
|
+
clientRef.current?.audio?.getOutputTimeDomainData() ?? emptyTimeDomainData
|
|
265
|
+
)
|
|
266
|
+
}, [])
|
|
267
|
+
|
|
268
|
+
const updateSession = useCallback((config: RealtimeSessionConfig) => {
|
|
269
|
+
clientRef.current?.updateSession(config)
|
|
270
|
+
}, [])
|
|
271
|
+
|
|
272
|
+
return {
|
|
273
|
+
// Connection state
|
|
274
|
+
status,
|
|
275
|
+
error,
|
|
276
|
+
connect,
|
|
277
|
+
disconnect,
|
|
278
|
+
|
|
279
|
+
// Conversation state
|
|
280
|
+
mode,
|
|
281
|
+
messages,
|
|
282
|
+
pendingUserTranscript,
|
|
283
|
+
pendingAssistantTranscript,
|
|
284
|
+
|
|
285
|
+
// Voice control
|
|
286
|
+
startListening,
|
|
287
|
+
stopListening,
|
|
288
|
+
interrupt,
|
|
289
|
+
|
|
290
|
+
// Text input
|
|
291
|
+
sendText,
|
|
292
|
+
|
|
293
|
+
// Image input
|
|
294
|
+
sendImage,
|
|
295
|
+
|
|
296
|
+
// Audio visualization
|
|
297
|
+
inputLevel,
|
|
298
|
+
outputLevel,
|
|
299
|
+
getInputFrequencyData,
|
|
300
|
+
getOutputFrequencyData,
|
|
301
|
+
getInputTimeDomainData,
|
|
302
|
+
getOutputTimeDomainData,
|
|
303
|
+
|
|
304
|
+
// Session control
|
|
305
|
+
updateSession,
|
|
306
|
+
}
|
|
307
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Declaration companion generated from use-realtime-chat.tsrx.
|
|
2
|
+
import type { UseRealtimeChatOptions, UseRealtimeChatReturn } from './realtime-types';
|
|
3
|
+
/**
|
|
4
|
+
* Octane hook for realtime voice conversations.
|
|
5
|
+
*
|
|
6
|
+
* Provides a simple interface for voice-to-voice AI interactions
|
|
7
|
+
* with support for multiple providers (OpenAI, ElevenLabs, etc.).
|
|
8
|
+
*
|
|
9
|
+
* @param options - Configuration options including adapter and callbacks
|
|
10
|
+
* @returns Hook return value with state and control methods
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { useRealtimeChat } from '@octanejs/tanstack-ai'
|
|
15
|
+
* import { openaiRealtime } from '@tanstack/ai-openai'
|
|
16
|
+
*
|
|
17
|
+
* function VoiceChat() {
|
|
18
|
+
* const {
|
|
19
|
+
* status,
|
|
20
|
+
* mode,
|
|
21
|
+
* messages,
|
|
22
|
+
* connect,
|
|
23
|
+
* disconnect,
|
|
24
|
+
* inputLevel,
|
|
25
|
+
* outputLevel,
|
|
26
|
+
* } = useRealtimeChat({
|
|
27
|
+
* getToken: () => fetch('/api/realtime-token').then(r => r.json()),
|
|
28
|
+
* adapter: openaiRealtime(),
|
|
29
|
+
* })
|
|
30
|
+
*
|
|
31
|
+
* return (
|
|
32
|
+
* <div>
|
|
33
|
+
* <p>Status: {status}</p>
|
|
34
|
+
* <p>Mode: {mode}</p>
|
|
35
|
+
* <button onClick={status === 'idle' ? connect : disconnect}>
|
|
36
|
+
* {status === 'idle' ? 'Start' : 'Stop'}
|
|
37
|
+
* </button>
|
|
38
|
+
* </div>
|
|
39
|
+
* )
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare function useRealtimeChat(options: UseRealtimeChatOptions): UseRealtimeChatReturn;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { useGeneration } from './use-generation.tsrx'
|
|
2
|
+
import type { StreamChunk, SummarizationResult } from '@tanstack/ai'
|
|
3
|
+
import 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
|
+
*/
|
|
17
|
+
export 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
|
+
*/
|
|
49
|
+
export 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 '@octanejs/tanstack-ai'
|
|
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
|
+
*/
|
|
95
|
+
export 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
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// Declaration companion generated from use-summarize.tsrx.
|
|
2
|
+
import type { StreamChunk, SummarizationResult } from '@tanstack/ai';
|
|
3
|
+
import type { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutputFromReturn, SummarizeGenerateInput } from '@tanstack/ai-client';
|
|
4
|
+
/**
|
|
5
|
+
* Options for the useSummarize hook.
|
|
6
|
+
*
|
|
7
|
+
* @template TOutput - The output type after optional transform (defaults to SummarizationResult)
|
|
8
|
+
*/
|
|
9
|
+
export interface UseSummarizeOptions<TOutput = SummarizationResult> {
|
|
10
|
+
/** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
11
|
+
connection?: ConnectConnectionAdapter;
|
|
12
|
+
/** Direct async function for summarization */
|
|
13
|
+
fetcher?: GenerationFetcher<SummarizeGenerateInput, SummarizationResult>;
|
|
14
|
+
/** Unique identifier for this generation instance */
|
|
15
|
+
id?: string;
|
|
16
|
+
/** Additional body parameters to send with connect-based adapter requests */
|
|
17
|
+
body?: Record<string, any>;
|
|
18
|
+
/** Display options for TanStack AI Devtools. */
|
|
19
|
+
devtools?: AIDevtoolsDisplayOptions;
|
|
20
|
+
/**
|
|
21
|
+
* Callback when summarization is complete. Can optionally return a transformed value.
|
|
22
|
+
*
|
|
23
|
+
* - Return a non-null value to transform and store it as the result
|
|
24
|
+
* - Return `null` to keep the previous result unchanged
|
|
25
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
26
|
+
*/
|
|
27
|
+
onResult?: (result: SummarizationResult) => TOutput | null | void;
|
|
28
|
+
/** Callback when an error occurs */
|
|
29
|
+
onError?: (error: Error) => void;
|
|
30
|
+
/** Callback when progress is reported (0-100) */
|
|
31
|
+
onProgress?: (progress: number, message?: string) => void;
|
|
32
|
+
/** Callback for each stream chunk (connect-based adapter mode only) */
|
|
33
|
+
onChunk?: (chunk: StreamChunk) => void;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Return type for the useSummarize hook.
|
|
37
|
+
*
|
|
38
|
+
* @template TOutput - The output type (after optional transform)
|
|
39
|
+
*/
|
|
40
|
+
export interface UseSummarizeReturn<TOutput = SummarizationResult> {
|
|
41
|
+
/** Trigger summarization */
|
|
42
|
+
generate: (input: SummarizeGenerateInput) => Promise<void>;
|
|
43
|
+
/** The summarization result, or null */
|
|
44
|
+
result: TOutput | null;
|
|
45
|
+
/** Whether summarization is in progress */
|
|
46
|
+
isLoading: boolean;
|
|
47
|
+
/** Current error, if any */
|
|
48
|
+
error: Error | undefined;
|
|
49
|
+
/** Current state of the generation */
|
|
50
|
+
status: GenerationClientState;
|
|
51
|
+
/** Abort the current summarization */
|
|
52
|
+
stop: () => void;
|
|
53
|
+
/** Clear result, error, and return to idle */
|
|
54
|
+
reset: () => void;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Octane hook for summarizing text using AI models.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```tsx
|
|
61
|
+
* import { useSummarize } from '@octanejs/tanstack-ai'
|
|
62
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
63
|
+
*
|
|
64
|
+
* function Summarizer() {
|
|
65
|
+
* const { generate, result, isLoading } = useSummarize({
|
|
66
|
+
* connection: fetchServerSentEvents('/api/summarize'),
|
|
67
|
+
* })
|
|
68
|
+
*
|
|
69
|
+
* return (
|
|
70
|
+
* <div>
|
|
71
|
+
* <button onClick={() => generate({
|
|
72
|
+
* text: 'Long article text...',
|
|
73
|
+
* style: 'bullet-points',
|
|
74
|
+
* maxLength: 200,
|
|
75
|
+
* })}>
|
|
76
|
+
* Summarize
|
|
77
|
+
* </button>
|
|
78
|
+
* {isLoading && <p>Summarizing...</p>}
|
|
79
|
+
* {result && <p>{result.summary}</p>}
|
|
80
|
+
* </div>
|
|
81
|
+
* )
|
|
82
|
+
* }
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export declare function useSummarize<TTransformed = void>(options: Omit<UseSummarizeOptions, 'onResult'> & {
|
|
86
|
+
onResult?: (result: SummarizationResult) => TTransformed;
|
|
87
|
+
}): UseSummarizeReturn<InferGenerationOutputFromReturn<SummarizationResult, TTransformed>>;
|