@tanstack/ai-react 0.15.15 → 0.16.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/dist/esm/index.d.ts +5 -1
- package/dist/esm/index.js +6 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/mcp-apps.d.ts +20 -0
- package/dist/esm/mcp-apps.js +52 -0
- package/dist/esm/mcp-apps.js.map +1 -0
- package/dist/esm/use-audio-recorder.d.ts +43 -0
- package/dist/esm/use-audio-recorder.js +49 -0
- package/dist/esm/use-audio-recorder.js.map +1 -0
- package/dist/esm/use-generate-audio.d.ts +4 -4
- package/dist/esm/use-generate-audio.js.map +1 -1
- package/dist/esm/use-generate-image.d.ts +4 -4
- package/dist/esm/use-generate-image.js.map +1 -1
- package/dist/esm/use-generate-speech.d.ts +4 -4
- package/dist/esm/use-generate-speech.js.map +1 -1
- package/dist/esm/use-generate-video.d.ts +4 -4
- package/dist/esm/use-generate-video.js +4 -1
- package/dist/esm/use-generate-video.js.map +1 -1
- package/dist/esm/use-generation.d.ts +4 -4
- package/dist/esm/use-generation.js +4 -1
- package/dist/esm/use-generation.js.map +1 -1
- package/dist/esm/use-mcp-app-bridge.d.ts +25 -0
- package/dist/esm/use-mcp-app-bridge.js +26 -0
- package/dist/esm/use-mcp-app-bridge.js.map +1 -0
- package/dist/esm/use-summarize.d.ts +4 -4
- package/dist/esm/use-summarize.js.map +1 -1
- package/dist/esm/use-transcription.d.ts +4 -4
- package/dist/esm/use-transcription.js +1 -4
- package/dist/esm/use-transcription.js.map +1 -1
- package/package.json +15 -4
- package/src/index.ts +11 -0
- package/src/mcp-apps.tsx +88 -0
- package/src/use-audio-recorder.ts +112 -0
- package/src/use-generate-audio.ts +5 -8
- package/src/use-generate-image.ts +5 -8
- package/src/use-generate-speech.ts +7 -7
- package/src/use-generate-video.ts +18 -9
- package/src/use-generation.ts +17 -6
- package/src/use-mcp-app-bridge.ts +60 -0
- package/src/use-summarize.ts +7 -8
- package/src/use-transcription.ts +9 -11
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { StreamChunk, SummarizationResult } from '@tanstack/ai';
|
|
2
|
-
import { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher,
|
|
2
|
+
import { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutputFromReturn, SummarizeGenerateInput } from '@tanstack/ai-client';
|
|
3
3
|
/**
|
|
4
4
|
* Options for the useSummarize hook.
|
|
5
5
|
*
|
|
@@ -81,6 +81,6 @@ export interface UseSummarizeReturn<TOutput = SummarizationResult> {
|
|
|
81
81
|
* }
|
|
82
82
|
* ```
|
|
83
83
|
*/
|
|
84
|
-
export declare function useSummarize<
|
|
85
|
-
onResult?:
|
|
86
|
-
}): UseSummarizeReturn<
|
|
84
|
+
export declare function useSummarize<TTransformed = void>(options: Omit<UseSummarizeOptions, 'onResult'> & {
|
|
85
|
+
onResult?: (result: SummarizationResult) => TTransformed;
|
|
86
|
+
}): UseSummarizeReturn<InferGenerationOutputFromReturn<SummarizationResult, TTransformed>>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-summarize.js","sources":["../../src/use-summarize.ts"],"sourcesContent":["import { useGeneration } from './use-generation'\nimport type { StreamChunk, SummarizationResult } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n ConnectConnectionAdapter,\n GenerationClientState,\n GenerationFetcher,\n
|
|
1
|
+
{"version":3,"file":"use-summarize.js","sources":["../../src/use-summarize.ts"],"sourcesContent":["import { useGeneration } from './use-generation'\nimport type { StreamChunk, SummarizationResult } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n ConnectConnectionAdapter,\n GenerationClientState,\n GenerationFetcher,\n InferGenerationOutputFromReturn,\n SummarizeGenerateInput,\n} from '@tanstack/ai-client'\n\n/**\n * Options for the useSummarize hook.\n *\n * @template TOutput - The output type after optional transform (defaults to SummarizationResult)\n */\nexport interface UseSummarizeOptions<TOutput = SummarizationResult> {\n /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectConnectionAdapter\n /** Direct async function for summarization */\n fetcher?: GenerationFetcher<SummarizeGenerateInput, SummarizationResult>\n /** Unique identifier for this generation instance */\n id?: string\n /** Additional body parameters to send with connect-based adapter requests */\n body?: Record<string, any>\n /** Display options for TanStack AI Devtools. */\n devtools?: AIDevtoolsDisplayOptions\n /**\n * Callback when summarization is complete. Can optionally return a transformed value.\n *\n * - Return a non-null value to transform and store it as the result\n * - Return `null` to keep the previous result unchanged\n * - Return nothing (`void`) to store the raw result as-is\n */\n onResult?: (result: SummarizationResult) => TOutput | null | void\n /** Callback when an error occurs */\n onError?: (error: Error) => void\n /** Callback when progress is reported (0-100) */\n onProgress?: (progress: number, message?: string) => void\n /** Callback for each stream chunk (connect-based adapter mode only) */\n onChunk?: (chunk: StreamChunk) => void\n}\n\n/**\n * Return type for the useSummarize hook.\n *\n * @template TOutput - The output type (after optional transform)\n */\nexport interface UseSummarizeReturn<TOutput = SummarizationResult> {\n /** Trigger summarization */\n generate: (input: SummarizeGenerateInput) => Promise<void>\n /** The summarization result, or null */\n result: TOutput | null\n /** Whether summarization is in progress */\n isLoading: boolean\n /** Current error, if any */\n error: Error | undefined\n /** Current state of the generation */\n status: GenerationClientState\n /** Abort the current summarization */\n stop: () => void\n /** Clear result, error, and return to idle */\n reset: () => void\n}\n\n/**\n * React hook for summarizing text using AI models.\n *\n * @example\n * ```tsx\n * import { useSummarize } from '@tanstack/ai-react'\n * import { fetchServerSentEvents } from '@tanstack/ai-client'\n *\n * function Summarizer() {\n * const { generate, result, isLoading } = useSummarize({\n * connection: fetchServerSentEvents('/api/summarize'),\n * })\n *\n * return (\n * <div>\n * <button onClick={() => generate({\n * text: 'Long article text...',\n * style: 'bullet-points',\n * maxLength: 200,\n * })}>\n * Summarize\n * </button>\n * {isLoading && <p>Summarizing...</p>}\n * {result && <p>{result.summary}</p>}\n * </div>\n * )\n * }\n * ```\n */\nexport function useSummarize<TTransformed = void>(\n options: Omit<UseSummarizeOptions, 'onResult'> & {\n onResult?: (result: SummarizationResult) => TTransformed\n },\n): UseSummarizeReturn<\n InferGenerationOutputFromReturn<SummarizationResult, TTransformed>\n> {\n const devtools = {\n ...options.devtools,\n framework: 'react',\n hookName: 'useSummarize',\n outputKind: 'text' as const,\n }\n const { generate, result, isLoading, error, status, stop, reset } =\n useGeneration<SummarizeGenerateInput, SummarizationResult, TTransformed>({\n ...options,\n devtools,\n })\n\n return {\n generate: generate as (input: SummarizeGenerateInput) => Promise<void>,\n result,\n isLoading,\n error,\n status,\n stop,\n reset,\n }\n}\n"],"names":[],"mappings":";AA8FO,SAAS,aACd,SAKA;AACA,QAAM,WAAW;AAAA,IACf,GAAG,QAAQ;AAAA,IACX,WAAW;AAAA,IACX,UAAU;AAAA,IACV,YAAY;AAAA,EAAA;AAEd,QAAM,EAAE,UAAU,QAAQ,WAAW,OAAO,QAAQ,MAAM,MAAA,IACxD,cAAyE;AAAA,IACvE,GAAG;AAAA,IACH;AAAA,EAAA,CACD;AAEH,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { StreamChunk, TranscriptionResult } from '@tanstack/ai';
|
|
2
|
-
import { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher,
|
|
2
|
+
import { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutputFromReturn, TranscriptionGenerateInput } from '@tanstack/ai-client';
|
|
3
3
|
/**
|
|
4
4
|
* Options for the useTranscription hook.
|
|
5
5
|
*
|
|
@@ -86,6 +86,6 @@ export interface UseTranscriptionReturn<TOutput = TranscriptionResult> {
|
|
|
86
86
|
* }
|
|
87
87
|
* ```
|
|
88
88
|
*/
|
|
89
|
-
export declare function useTranscription<
|
|
90
|
-
onResult?:
|
|
91
|
-
}): UseTranscriptionReturn<
|
|
89
|
+
export declare function useTranscription<TTransformed = void>(options: Omit<UseTranscriptionOptions, 'onResult'> & {
|
|
90
|
+
onResult?: (result: TranscriptionResult) => TTransformed;
|
|
91
|
+
}): UseTranscriptionReturn<InferGenerationOutputFromReturn<TranscriptionResult, TTransformed>>;
|
|
@@ -6,10 +6,7 @@ function useTranscription(options) {
|
|
|
6
6
|
hookName: "useTranscription",
|
|
7
7
|
outputKind: "text"
|
|
8
8
|
};
|
|
9
|
-
const { generate, result, isLoading, error, status, stop, reset } = useGeneration({
|
|
10
|
-
...options,
|
|
11
|
-
devtools
|
|
12
|
-
});
|
|
9
|
+
const { generate, result, isLoading, error, status, stop, reset } = useGeneration({ ...options, devtools });
|
|
13
10
|
return {
|
|
14
11
|
generate,
|
|
15
12
|
result,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-transcription.js","sources":["../../src/use-transcription.ts"],"sourcesContent":["import { useGeneration } from './use-generation'\nimport type { StreamChunk, TranscriptionResult } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n ConnectConnectionAdapter,\n GenerationClientState,\n GenerationFetcher,\n
|
|
1
|
+
{"version":3,"file":"use-transcription.js","sources":["../../src/use-transcription.ts"],"sourcesContent":["import { useGeneration } from './use-generation'\nimport type { StreamChunk, TranscriptionResult } from '@tanstack/ai'\nimport type {\n AIDevtoolsDisplayOptions,\n ConnectConnectionAdapter,\n GenerationClientState,\n GenerationFetcher,\n InferGenerationOutputFromReturn,\n TranscriptionGenerateInput,\n} from '@tanstack/ai-client'\n\n/**\n * Options for the useTranscription hook.\n *\n * @template TOutput - The output type after optional transform (defaults to TranscriptionResult)\n */\nexport interface UseTranscriptionOptions<TOutput = TranscriptionResult> {\n /** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */\n connection?: ConnectConnectionAdapter\n /** Direct async function for transcription */\n fetcher?: GenerationFetcher<TranscriptionGenerateInput, TranscriptionResult>\n /** Unique identifier for this generation instance */\n id?: string\n /** Additional body parameters to send with connect-based adapter requests */\n body?: Record<string, any>\n /** Display options for TanStack AI Devtools. */\n devtools?: AIDevtoolsDisplayOptions\n /**\n * Callback when transcription is complete. Can optionally return a transformed value.\n *\n * - Return a non-null value to transform and store it as the result\n * - Return `null` to keep the previous result unchanged\n * - Return nothing (`void`) to store the raw result as-is\n */\n onResult?: (result: TranscriptionResult) => TOutput | null | void\n /** Callback when an error occurs */\n onError?: (error: Error) => void\n /** Callback when progress is reported (0-100) */\n onProgress?: (progress: number, message?: string) => void\n /** Callback for each stream chunk (connect-based adapter mode only) */\n onChunk?: (chunk: StreamChunk) => void\n}\n\n/**\n * Return type for the useTranscription hook.\n *\n * @template TOutput - The output type (after optional transform)\n */\nexport interface UseTranscriptionReturn<TOutput = TranscriptionResult> {\n /** Trigger transcription */\n generate: (input: TranscriptionGenerateInput) => Promise<void>\n /** The transcription result, or null */\n result: TOutput | null\n /** Whether transcription is in progress */\n isLoading: boolean\n /** Current error, if any */\n error: Error | undefined\n /** Current state of the generation */\n status: GenerationClientState\n /** Abort the current transcription */\n stop: () => void\n /** Clear result, error, and return to idle */\n reset: () => void\n}\n\n/**\n * React hook for transcribing audio to text using AI models.\n *\n * @example\n * ```tsx\n * import { useTranscription } from '@tanstack/ai-react'\n * import { fetchServerSentEvents } from '@tanstack/ai-client'\n *\n * function Transcriber() {\n * const { generate, result, isLoading } = useTranscription({\n * connection: fetchServerSentEvents('/api/transcribe'),\n * })\n *\n * const handleFile = (e: React.ChangeEvent<HTMLInputElement>) => {\n * const file = e.target.files?.[0]\n * if (file) {\n * const reader = new FileReader()\n * reader.onload = () => {\n * generate({ audio: reader.result as string, language: 'en' })\n * }\n * reader.readAsDataURL(file)\n * }\n * }\n *\n * return (\n * <div>\n * <input type=\"file\" accept=\"audio/*\" onChange={handleFile} />\n * {isLoading && <p>Transcribing...</p>}\n * {result && <p>{result.text}</p>}\n * </div>\n * )\n * }\n * ```\n */\nexport function useTranscription<TTransformed = void>(\n options: Omit<UseTranscriptionOptions, 'onResult'> & {\n onResult?: (result: TranscriptionResult) => TTransformed\n },\n): UseTranscriptionReturn<\n InferGenerationOutputFromReturn<TranscriptionResult, TTransformed>\n> {\n const devtools = {\n ...options.devtools,\n framework: 'react',\n hookName: 'useTranscription',\n outputKind: 'text' as const,\n }\n const { generate, result, isLoading, error, status, stop, reset } =\n useGeneration<\n TranscriptionGenerateInput,\n TranscriptionResult,\n TTransformed\n >({ ...options, devtools })\n\n return {\n generate: generate as (input: TranscriptionGenerateInput) => Promise<void>,\n result,\n isLoading,\n error,\n status,\n stop,\n reset,\n }\n}\n"],"names":[],"mappings":";AAmGO,SAAS,iBACd,SAKA;AACA,QAAM,WAAW;AAAA,IACf,GAAG,QAAQ;AAAA,IACX,WAAW;AAAA,IACX,UAAU;AAAA,IACV,YAAY;AAAA,EAAA;AAEd,QAAM,EAAE,UAAU,QAAQ,WAAW,OAAO,QAAQ,MAAM,MAAA,IACxD,cAIE,EAAE,GAAG,SAAS,UAAU;AAE5B,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.1",
|
|
4
4
|
"description": "React hooks for TanStack AI streaming chat, realtime voice, structured outputs, and media generation.",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,6 +24,10 @@
|
|
|
24
24
|
".": {
|
|
25
25
|
"types": "./dist/esm/index.d.ts",
|
|
26
26
|
"import": "./dist/esm/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./mcp-apps": {
|
|
29
|
+
"types": "./dist/esm/mcp-apps.d.ts",
|
|
30
|
+
"import": "./dist/esm/mcp-apps.js"
|
|
27
31
|
}
|
|
28
32
|
},
|
|
29
33
|
"files": [
|
|
@@ -45,14 +49,21 @@
|
|
|
45
49
|
"media-generation"
|
|
46
50
|
],
|
|
47
51
|
"dependencies": {
|
|
48
|
-
"@tanstack/ai-client": "0.
|
|
52
|
+
"@tanstack/ai-client": "0.19.1"
|
|
49
53
|
},
|
|
50
54
|
"peerDependencies": {
|
|
55
|
+
"@mcp-ui/client": "^7",
|
|
51
56
|
"@types/react": ">=18.0.0",
|
|
52
57
|
"react": ">=18.0.0",
|
|
53
|
-
"@tanstack/ai": "^0.
|
|
58
|
+
"@tanstack/ai": "^0.39.0"
|
|
59
|
+
},
|
|
60
|
+
"peerDependenciesMeta": {
|
|
61
|
+
"@mcp-ui/client": {
|
|
62
|
+
"optional": true
|
|
63
|
+
}
|
|
54
64
|
},
|
|
55
65
|
"devDependencies": {
|
|
66
|
+
"@mcp-ui/client": "^7.1.1",
|
|
56
67
|
"@standard-schema/spec": "^1.1.0",
|
|
57
68
|
"@testing-library/react": "^16.3.0",
|
|
58
69
|
"@types/react": "^19.2.7",
|
|
@@ -60,7 +71,7 @@
|
|
|
60
71
|
"jsdom": "^27.2.0",
|
|
61
72
|
"react": "^19.2.3",
|
|
62
73
|
"vite": "^7.3.3",
|
|
63
|
-
"@tanstack/ai": "0.
|
|
74
|
+
"@tanstack/ai": "0.39.0"
|
|
64
75
|
},
|
|
65
76
|
"scripts": {
|
|
66
77
|
"clean": "premove ./build ./dist",
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { useChat } from './use-chat'
|
|
2
2
|
export { useRealtimeChat } from './use-realtime-chat'
|
|
3
|
+
export { useMcpAppBridge } from './use-mcp-app-bridge'
|
|
4
|
+
export type { UseMcpAppBridgeOptions } from './use-mcp-app-bridge'
|
|
3
5
|
export type {
|
|
4
6
|
DeepPartial,
|
|
5
7
|
UseChatOptions,
|
|
@@ -52,6 +54,12 @@ export type {
|
|
|
52
54
|
UseGenerateVideoReturn,
|
|
53
55
|
} from './use-generate-video'
|
|
54
56
|
|
|
57
|
+
export { useAudioRecorder } from './use-audio-recorder'
|
|
58
|
+
export type {
|
|
59
|
+
UseAudioRecorderOptions,
|
|
60
|
+
UseAudioRecorderReturn,
|
|
61
|
+
} from './use-audio-recorder'
|
|
62
|
+
|
|
55
63
|
// Re-export from ai-client for convenience
|
|
56
64
|
export {
|
|
57
65
|
fetchServerSentEvents,
|
|
@@ -61,6 +69,9 @@ export {
|
|
|
61
69
|
stream,
|
|
62
70
|
rpcStream,
|
|
63
71
|
createChatClientOptions,
|
|
72
|
+
createMcpAppBridge,
|
|
73
|
+
type McpAppBridge,
|
|
74
|
+
type CreateMcpAppBridgeOptions,
|
|
64
75
|
type ChatFetcher,
|
|
65
76
|
type ChatFetcherInput,
|
|
66
77
|
type ChatFetcherOptions,
|
package/src/mcp-apps.tsx
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { AppRenderer } from '@mcp-ui/client'
|
|
2
|
+
import type { UIResourcePart } from '@tanstack/ai'
|
|
3
|
+
import type { McpAppBridge } from '@tanstack/ai-client'
|
|
4
|
+
import type { JSX } from 'react'
|
|
5
|
+
|
|
6
|
+
export interface MCPAppResourceProps {
|
|
7
|
+
/** The ui-resource part from a UIMessage assistant part. */
|
|
8
|
+
part: UIResourcePart
|
|
9
|
+
/**
|
|
10
|
+
* Framework-agnostic bridge for tool calls, prompt sending, and link opening.
|
|
11
|
+
* Omit it to render the widget in display-only mode — iframe interactions
|
|
12
|
+
* that would trigger tool calls or prompts are ignored.
|
|
13
|
+
*/
|
|
14
|
+
bridge?: McpAppBridge
|
|
15
|
+
/** Sandbox iframe configuration — must include the proxy page URL. */
|
|
16
|
+
sandbox: { url: URL }
|
|
17
|
+
/** Optional structured arguments forwarded to the guest UI once it's ready. */
|
|
18
|
+
toolInput?: Record<string, unknown>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
22
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Coalesce an arbitrary bridge result into the `string` required by the
|
|
27
|
+
* `CallToolResult` text content block. `JSON.stringify` is typed to return
|
|
28
|
+
* `string` but actually returns `undefined` for inputs like `undefined` or a
|
|
29
|
+
* function, so we narrow explicitly to avoid `text: undefined`.
|
|
30
|
+
*/
|
|
31
|
+
function resultToText(result: unknown): string {
|
|
32
|
+
if (typeof result === 'string') return result
|
|
33
|
+
// `JSON.stringify`'s lib signature claims `string`, but it returns
|
|
34
|
+
// `undefined` for `undefined`/function inputs; type the call honestly.
|
|
35
|
+
const stringify: (value: unknown) => string | undefined = JSON.stringify
|
|
36
|
+
return stringify(result) ?? 'null'
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function MCPAppResource(props: MCPAppResourceProps): JSX.Element {
|
|
40
|
+
const { bridge } = props
|
|
41
|
+
return (
|
|
42
|
+
<AppRenderer
|
|
43
|
+
toolName={props.part.toolName}
|
|
44
|
+
sandbox={props.sandbox}
|
|
45
|
+
html={props.part.resource.text}
|
|
46
|
+
toolResourceUri={props.part.resource.uri}
|
|
47
|
+
toolInput={props.toolInput}
|
|
48
|
+
onCallTool={
|
|
49
|
+
bridge
|
|
50
|
+
? async ({ name, arguments: args }) => {
|
|
51
|
+
const result = await bridge.callTool({
|
|
52
|
+
serverId: props.part.serverId,
|
|
53
|
+
toolName: name,
|
|
54
|
+
args,
|
|
55
|
+
})
|
|
56
|
+
const structuredContent = isRecord(result) ? result : undefined
|
|
57
|
+
return {
|
|
58
|
+
content: [
|
|
59
|
+
{
|
|
60
|
+
type: 'text' as const,
|
|
61
|
+
text: resultToText(result),
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
structuredContent,
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
: undefined
|
|
68
|
+
}
|
|
69
|
+
onMessage={
|
|
70
|
+
bridge
|
|
71
|
+
? async ({ content }) => {
|
|
72
|
+
const text = content
|
|
73
|
+
.filter(
|
|
74
|
+
(c): c is { type: 'text'; text: string } => c.type === 'text',
|
|
75
|
+
)
|
|
76
|
+
.map((c) => c.text)
|
|
77
|
+
.join('')
|
|
78
|
+
if (text) await bridge.sendPrompt(text)
|
|
79
|
+
return {}
|
|
80
|
+
}
|
|
81
|
+
: undefined
|
|
82
|
+
}
|
|
83
|
+
onOpenLink={
|
|
84
|
+
bridge ? ({ url }) => Promise.resolve(bridge.openLink(url)) : undefined
|
|
85
|
+
}
|
|
86
|
+
/>
|
|
87
|
+
)
|
|
88
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
|
2
|
+
import { AudioRecorder } from '@tanstack/ai-client'
|
|
3
|
+
import type {
|
|
4
|
+
AudioRecorderOptions,
|
|
5
|
+
AudioRecording,
|
|
6
|
+
InferAudioRecordingOutput,
|
|
7
|
+
} from '@tanstack/ai-client'
|
|
8
|
+
|
|
9
|
+
export type UseAudioRecorderOptions<TOnComplete> = AudioRecorderOptions & {
|
|
10
|
+
/**
|
|
11
|
+
* Optional transform applied to the recording when `stop()` resolves. Its
|
|
12
|
+
* (awaited) return value becomes `recording` and the resolved value of
|
|
13
|
+
* `stop()`. Return nothing to keep the raw `AudioRecording`.
|
|
14
|
+
*/
|
|
15
|
+
onComplete?: TOnComplete
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface UseAudioRecorderReturn<TOutput> {
|
|
19
|
+
/** Latest recording (transformed if `onComplete` provided), or null. */
|
|
20
|
+
recording: TOutput | null
|
|
21
|
+
/** True while actively capturing audio. */
|
|
22
|
+
isRecording: boolean
|
|
23
|
+
/** Whether the browser supports recording (getUserMedia + MediaRecorder). */
|
|
24
|
+
isSupported: boolean
|
|
25
|
+
/** Acquire the mic and begin recording. */
|
|
26
|
+
start: () => Promise<void>
|
|
27
|
+
/** Stop and resolve with the completed recording (transformed if `onComplete` provided). */
|
|
28
|
+
stop: () => Promise<TOutput>
|
|
29
|
+
/** Discard the in-progress recording and release the mic. */
|
|
30
|
+
cancel: () => void
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* React hook for recording an audio message. The resolved
|
|
35
|
+
* {@link AudioRecording} carries `.part` (an audio content part for
|
|
36
|
+
* `useChat.sendMessage`) and `.base64` (for the generation hooks).
|
|
37
|
+
*
|
|
38
|
+
* Errors are delivered via `onError`. `start()` and `stop()` also reject on
|
|
39
|
+
* failure (and `stop()` rejects with `Recording cancelled` if the component
|
|
40
|
+
* unmounts while a stop is in flight) — handle one channel, not both.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```tsx
|
|
44
|
+
* const { isRecording, start, stop, recording } = useAudioRecorder()
|
|
45
|
+
* const { sendMessage } = useChat({ connection })
|
|
46
|
+
* // ...
|
|
47
|
+
* const rec = await stop()
|
|
48
|
+
* sendMessage({ content: [rec.part] })
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export function useAudioRecorder<
|
|
52
|
+
TOnComplete extends (recording: AudioRecording) => unknown,
|
|
53
|
+
>(
|
|
54
|
+
options: UseAudioRecorderOptions<TOnComplete>,
|
|
55
|
+
): UseAudioRecorderReturn<InferAudioRecordingOutput<TOnComplete>>
|
|
56
|
+
export function useAudioRecorder(
|
|
57
|
+
options?: UseAudioRecorderOptions<undefined>,
|
|
58
|
+
): UseAudioRecorderReturn<AudioRecording>
|
|
59
|
+
export function useAudioRecorder(
|
|
60
|
+
options: UseAudioRecorderOptions<(recording: AudioRecording) => unknown> = {},
|
|
61
|
+
): UseAudioRecorderReturn<unknown> {
|
|
62
|
+
const [isRecording, setIsRecording] = useState(false)
|
|
63
|
+
const [recording, setRecording] = useState<unknown>(null)
|
|
64
|
+
// Read the freshest callbacks at fire time without recreating the recorder.
|
|
65
|
+
const optionsRef = useRef(options)
|
|
66
|
+
optionsRef.current = options
|
|
67
|
+
|
|
68
|
+
const recorder = useMemo(
|
|
69
|
+
() =>
|
|
70
|
+
new AudioRecorder({
|
|
71
|
+
...(options.audio !== undefined && { audio: options.audio }),
|
|
72
|
+
...(options.mimeType !== undefined && { mimeType: options.mimeType }),
|
|
73
|
+
onError: (err) => optionsRef.current.onError?.(err),
|
|
74
|
+
}),
|
|
75
|
+
// Recorder config (audio/mimeType) is captured once at mount, matching the
|
|
76
|
+
// other hooks' create-once pattern.
|
|
77
|
+
[],
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
useEffect(() => {
|
|
81
|
+
const unsubscribe = recorder.subscribe((state) => {
|
|
82
|
+
setIsRecording(state === 'recording')
|
|
83
|
+
})
|
|
84
|
+
return () => {
|
|
85
|
+
unsubscribe()
|
|
86
|
+
recorder.cancel()
|
|
87
|
+
}
|
|
88
|
+
}, [recorder])
|
|
89
|
+
|
|
90
|
+
const start = useCallback(() => recorder.start(), [recorder])
|
|
91
|
+
const stop = useCallback(async () => {
|
|
92
|
+
const recording = await recorder.stop()
|
|
93
|
+
const transformed = await optionsRef.current.onComplete?.(recording)
|
|
94
|
+
// Only `undefined` (returning nothing) falls back to the raw recording, so
|
|
95
|
+
// a transform that returns null is preserved — matching the inferred type,
|
|
96
|
+
// which excludes only undefined/void/null from the transform's return.
|
|
97
|
+
const output = transformed === undefined ? recording : transformed
|
|
98
|
+
setRecording(() => output)
|
|
99
|
+
return output
|
|
100
|
+
}, [recorder])
|
|
101
|
+
const cancel = useCallback(() => recorder.cancel(), [recorder])
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
recording,
|
|
105
|
+
isRecording,
|
|
106
|
+
// recording is client-only; if SSR'd, gate UI on a mounted flag.
|
|
107
|
+
isSupported: AudioRecorder.isSupported(),
|
|
108
|
+
start,
|
|
109
|
+
stop,
|
|
110
|
+
cancel,
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -6,7 +6,7 @@ import type {
|
|
|
6
6
|
ConnectConnectionAdapter,
|
|
7
7
|
GenerationClientState,
|
|
8
8
|
GenerationFetcher,
|
|
9
|
-
|
|
9
|
+
InferGenerationOutputFromReturn,
|
|
10
10
|
} from '@tanstack/ai-client'
|
|
11
11
|
|
|
12
12
|
/**
|
|
@@ -93,15 +93,12 @@ export interface UseGenerateAudioReturn<TOutput = AudioGenerationResult> {
|
|
|
93
93
|
* }
|
|
94
94
|
* ```
|
|
95
95
|
*/
|
|
96
|
-
export function useGenerateAudio<
|
|
97
|
-
TOnResult extends ((result: AudioGenerationResult) => any) | undefined =
|
|
98
|
-
undefined,
|
|
99
|
-
>(
|
|
96
|
+
export function useGenerateAudio<TTransformed = void>(
|
|
100
97
|
options: Omit<UseGenerateAudioOptions, 'onResult'> & {
|
|
101
|
-
onResult?:
|
|
98
|
+
onResult?: (result: AudioGenerationResult) => TTransformed
|
|
102
99
|
},
|
|
103
100
|
): UseGenerateAudioReturn<
|
|
104
|
-
|
|
101
|
+
InferGenerationOutputFromReturn<AudioGenerationResult, TTransformed>
|
|
105
102
|
> {
|
|
106
103
|
const devtools = {
|
|
107
104
|
...options.devtools,
|
|
@@ -110,7 +107,7 @@ export function useGenerateAudio<
|
|
|
110
107
|
outputKind: 'audio' as const,
|
|
111
108
|
}
|
|
112
109
|
const { generate, result, isLoading, error, status, stop, reset } =
|
|
113
|
-
useGeneration<AudioGenerateInput, AudioGenerationResult,
|
|
110
|
+
useGeneration<AudioGenerateInput, AudioGenerationResult, TTransformed>({
|
|
114
111
|
...options,
|
|
115
112
|
devtools,
|
|
116
113
|
})
|
|
@@ -6,7 +6,7 @@ import type {
|
|
|
6
6
|
GenerationClientState,
|
|
7
7
|
GenerationFetcher,
|
|
8
8
|
ImageGenerateInput,
|
|
9
|
-
|
|
9
|
+
InferGenerationOutputFromReturn,
|
|
10
10
|
} from '@tanstack/ai-client'
|
|
11
11
|
|
|
12
12
|
/**
|
|
@@ -95,15 +95,12 @@ export interface UseGenerateImageReturn<TOutput = ImageGenerationResult> {
|
|
|
95
95
|
* }
|
|
96
96
|
* ```
|
|
97
97
|
*/
|
|
98
|
-
export function useGenerateImage<
|
|
99
|
-
TOnResult extends ((result: ImageGenerationResult) => any) | undefined =
|
|
100
|
-
undefined,
|
|
101
|
-
>(
|
|
98
|
+
export function useGenerateImage<TTransformed = void>(
|
|
102
99
|
options: Omit<UseGenerateImageOptions, 'onResult'> & {
|
|
103
|
-
onResult?:
|
|
100
|
+
onResult?: (result: ImageGenerationResult) => TTransformed
|
|
104
101
|
},
|
|
105
102
|
): UseGenerateImageReturn<
|
|
106
|
-
|
|
103
|
+
InferGenerationOutputFromReturn<ImageGenerationResult, TTransformed>
|
|
107
104
|
> {
|
|
108
105
|
const devtools = {
|
|
109
106
|
...options.devtools,
|
|
@@ -112,7 +109,7 @@ export function useGenerateImage<
|
|
|
112
109
|
outputKind: 'image' as const,
|
|
113
110
|
}
|
|
114
111
|
const { generate, result, isLoading, error, status, stop, reset } =
|
|
115
|
-
useGeneration<ImageGenerateInput, ImageGenerationResult,
|
|
112
|
+
useGeneration<ImageGenerateInput, ImageGenerationResult, TTransformed>({
|
|
116
113
|
...options,
|
|
117
114
|
devtools,
|
|
118
115
|
})
|
|
@@ -5,7 +5,7 @@ import type {
|
|
|
5
5
|
ConnectConnectionAdapter,
|
|
6
6
|
GenerationClientState,
|
|
7
7
|
GenerationFetcher,
|
|
8
|
-
|
|
8
|
+
InferGenerationOutputFromReturn,
|
|
9
9
|
SpeechGenerateInput,
|
|
10
10
|
} from '@tanstack/ai-client'
|
|
11
11
|
|
|
@@ -89,13 +89,13 @@ export interface UseGenerateSpeechReturn<TOutput = TTSResult> {
|
|
|
89
89
|
* }
|
|
90
90
|
* ```
|
|
91
91
|
*/
|
|
92
|
-
export function useGenerateSpeech<
|
|
93
|
-
TOnResult extends ((result: TTSResult) => any) | undefined = undefined,
|
|
94
|
-
>(
|
|
92
|
+
export function useGenerateSpeech<TTransformed = void>(
|
|
95
93
|
options: Omit<UseGenerateSpeechOptions, 'onResult'> & {
|
|
96
|
-
onResult?:
|
|
94
|
+
onResult?: (result: TTSResult) => TTransformed
|
|
97
95
|
},
|
|
98
|
-
): UseGenerateSpeechReturn<
|
|
96
|
+
): UseGenerateSpeechReturn<
|
|
97
|
+
InferGenerationOutputFromReturn<TTSResult, TTransformed>
|
|
98
|
+
> {
|
|
99
99
|
const devtools = {
|
|
100
100
|
...options.devtools,
|
|
101
101
|
framework: 'react',
|
|
@@ -103,7 +103,7 @@ export function useGenerateSpeech<
|
|
|
103
103
|
outputKind: 'audio' as const,
|
|
104
104
|
}
|
|
105
105
|
const { generate, result, isLoading, error, status, stop, reset } =
|
|
106
|
-
useGeneration<SpeechGenerateInput, TTSResult,
|
|
106
|
+
useGeneration<SpeechGenerateInput, TTSResult, TTransformed>({
|
|
107
107
|
...options,
|
|
108
108
|
devtools,
|
|
109
109
|
})
|
|
@@ -7,7 +7,7 @@ import type {
|
|
|
7
7
|
ConnectConnectionAdapter,
|
|
8
8
|
GenerationClientState,
|
|
9
9
|
GenerationFetcher,
|
|
10
|
-
|
|
10
|
+
InferGenerationOutputFromReturn,
|
|
11
11
|
VideoGenerateInput,
|
|
12
12
|
VideoGenerateResult,
|
|
13
13
|
VideoStatusInfo,
|
|
@@ -104,17 +104,20 @@ export interface UseGenerateVideoReturn<TOutput = VideoGenerateResult> {
|
|
|
104
104
|
* }
|
|
105
105
|
* ```
|
|
106
106
|
*/
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
>(
|
|
107
|
+
// `TTransformed` infers from the `onResult` return position so the callback
|
|
108
|
+
// parameter is typed as `VideoGenerateResult` and `result` narrows to the
|
|
109
|
+
// transform's return. See issue #848.
|
|
110
|
+
export function useGenerateVideo<TTransformed = void>(
|
|
111
111
|
options: Omit<UseGenerateVideoOptions, 'onResult'> & {
|
|
112
|
-
onResult?:
|
|
112
|
+
onResult?: (result: VideoGenerateResult) => TTransformed
|
|
113
113
|
},
|
|
114
114
|
): UseGenerateVideoReturn<
|
|
115
|
-
|
|
115
|
+
InferGenerationOutputFromReturn<VideoGenerateResult, TTransformed>
|
|
116
116
|
> {
|
|
117
|
-
type TOutput =
|
|
117
|
+
type TOutput = InferGenerationOutputFromReturn<
|
|
118
|
+
VideoGenerateResult,
|
|
119
|
+
TTransformed
|
|
120
|
+
>
|
|
118
121
|
const hookId = useId()
|
|
119
122
|
const clientId = options.id || hookId
|
|
120
123
|
|
|
@@ -146,7 +149,13 @@ export function useGenerateVideo<
|
|
|
146
149
|
hookName: 'useGenerateVideo',
|
|
147
150
|
outputKind: 'video' as const,
|
|
148
151
|
},
|
|
149
|
-
|
|
152
|
+
// The transform's raw return type (`TTransformed`) and the stored output
|
|
153
|
+
// (`TOutput`, with null/void/undefined stripped) are identical at runtime;
|
|
154
|
+
// the cast bridges the relationship that the conditional type hides.
|
|
155
|
+
onResult: ((r: VideoGenerateResult) =>
|
|
156
|
+
optionsRef.current.onResult?.(r)) as (
|
|
157
|
+
result: VideoGenerateResult,
|
|
158
|
+
) => TOutput | null | void,
|
|
150
159
|
onError: (e: Error) => {
|
|
151
160
|
optionsRef.current.onError?.(e)
|
|
152
161
|
},
|
package/src/use-generation.ts
CHANGED
|
@@ -8,7 +8,7 @@ import type {
|
|
|
8
8
|
GenerationClientOptions,
|
|
9
9
|
GenerationClientState,
|
|
10
10
|
GenerationFetcher,
|
|
11
|
-
|
|
11
|
+
InferGenerationOutputFromReturn,
|
|
12
12
|
} from '@tanstack/ai-client'
|
|
13
13
|
|
|
14
14
|
/**
|
|
@@ -88,16 +88,22 @@ export interface UseGenerationReturn<TOutput> {
|
|
|
88
88
|
* await generate({ prompt: 'Hello' })
|
|
89
89
|
* ```
|
|
90
90
|
*/
|
|
91
|
+
// `TTransformed` infers from the `onResult` return position (a covariant
|
|
92
|
+
// inference site that works even for an optional nested property), which types
|
|
93
|
+
// the callback parameter as `TResult` and narrows `result`. Inferring the
|
|
94
|
+
// whole callback as a defaulted type parameter instead collapses to the
|
|
95
|
+
// default, leaving the parameter `any` — a hard error under `strict`. See
|
|
96
|
+
// issue #848.
|
|
91
97
|
export function useGeneration<
|
|
92
98
|
TInput extends Record<string, any>,
|
|
93
99
|
TResult,
|
|
94
|
-
|
|
100
|
+
TTransformed = void,
|
|
95
101
|
>(
|
|
96
102
|
options: Omit<UseGenerationOptions<TInput, TResult>, 'onResult'> & {
|
|
97
|
-
onResult?:
|
|
103
|
+
onResult?: (result: TResult) => TTransformed
|
|
98
104
|
},
|
|
99
|
-
): UseGenerationReturn<
|
|
100
|
-
type TOutput =
|
|
105
|
+
): UseGenerationReturn<InferGenerationOutputFromReturn<TResult, TTransformed>> {
|
|
106
|
+
type TOutput = InferGenerationOutputFromReturn<TResult, TTransformed>
|
|
101
107
|
const hookId = useId()
|
|
102
108
|
const clientId = options.id || hookId
|
|
103
109
|
|
|
@@ -125,7 +131,12 @@ export function useGeneration<
|
|
|
125
131
|
framework: 'react',
|
|
126
132
|
...opts.devtools,
|
|
127
133
|
},
|
|
128
|
-
|
|
134
|
+
// The transform's raw return type (`TTransformed`) and the stored output
|
|
135
|
+
// (`TOutput`, with null/void/undefined stripped) are identical at runtime;
|
|
136
|
+
// the cast bridges the relationship that the conditional type hides.
|
|
137
|
+
onResult: ((r: TResult) => optionsRef.current.onResult?.(r)) as (
|
|
138
|
+
result: TResult,
|
|
139
|
+
) => TOutput | null | void,
|
|
129
140
|
onError: (e: Error) => {
|
|
130
141
|
optionsRef.current.onError?.(e)
|
|
131
142
|
},
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { useMemo, useRef } from 'react'
|
|
2
|
+
import { createMcpAppBridge } from '@tanstack/ai-client'
|
|
3
|
+
import type {
|
|
4
|
+
CreateMcpAppBridgeOptions,
|
|
5
|
+
McpAppBridge,
|
|
6
|
+
} from '@tanstack/ai-client'
|
|
7
|
+
|
|
8
|
+
export type UseMcpAppBridgeOptions = CreateMcpAppBridgeOptions
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* React wrapper around `createMcpAppBridge` that returns a **stable** bridge for
|
|
12
|
+
* the given `threadId`/`callEndpoint`, while always invoking the latest
|
|
13
|
+
* `chat.sendMessage` and `onLink` (kept in refs). This avoids both recreating
|
|
14
|
+
* the bridge on every render and the stale-closure / `exhaustive-deps` dance
|
|
15
|
+
* you'd otherwise write by hand:
|
|
16
|
+
*
|
|
17
|
+
* ```tsx
|
|
18
|
+
* const { sendMessage } = useChat({ threadId, connection })
|
|
19
|
+
* const bridge = useMcpAppBridge({
|
|
20
|
+
* threadId,
|
|
21
|
+
* callEndpoint: '/api/mcp-apps-call',
|
|
22
|
+
* chat: { sendMessage: async (content) => void sendMessage(content) },
|
|
23
|
+
* onLink: (url) => window.open(url, '_blank', 'noopener,noreferrer'),
|
|
24
|
+
* })
|
|
25
|
+
* // pass `bridge` to <MCPAppResource bridge={bridge} … />
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* The bridge is recreated only when `threadId`, `callEndpoint`, `fetchImpl`, or
|
|
29
|
+
* the *presence* of `onLink` changes — passing a new inline `onLink`/`sendMessage`
|
|
30
|
+
* each render does not churn it.
|
|
31
|
+
*/
|
|
32
|
+
export function useMcpAppBridge(options: UseMcpAppBridgeOptions): McpAppBridge {
|
|
33
|
+
const { threadId, callEndpoint, chat, fetchImpl, onLink } = options
|
|
34
|
+
|
|
35
|
+
// Latest-value refs so the bridge identity stays stable but its callbacks are
|
|
36
|
+
// never stale (the bridge calls `.current` at invocation time, not creation).
|
|
37
|
+
const chatRef = useRef(chat)
|
|
38
|
+
chatRef.current = chat
|
|
39
|
+
const onLinkRef = useRef(onLink)
|
|
40
|
+
onLinkRef.current = onLink
|
|
41
|
+
|
|
42
|
+
// Whether a link handler was supplied governs the bridge's link behavior
|
|
43
|
+
// (forward vs. display-only warn), so it's part of the bridge's identity.
|
|
44
|
+
const hasOnLink = onLink != null
|
|
45
|
+
|
|
46
|
+
return useMemo(
|
|
47
|
+
() =>
|
|
48
|
+
createMcpAppBridge({
|
|
49
|
+
threadId,
|
|
50
|
+
callEndpoint,
|
|
51
|
+
fetchImpl,
|
|
52
|
+
chat: {
|
|
53
|
+
sendMessage: (content, body) =>
|
|
54
|
+
chatRef.current.sendMessage(content, body),
|
|
55
|
+
},
|
|
56
|
+
onLink: hasOnLink ? (url) => onLinkRef.current?.(url) : undefined,
|
|
57
|
+
}),
|
|
58
|
+
[threadId, callEndpoint, fetchImpl, hasOnLink],
|
|
59
|
+
)
|
|
60
|
+
}
|