@tanstack/ai-svelte 0.13.13 → 0.13.14
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/package.json +4 -4
- package/dist/create-chat.svelte.d.ts +0 -34
- package/dist/create-chat.svelte.js +0 -248
- package/dist/create-generate-audio.svelte.d.ts +0 -85
- package/dist/create-generate-audio.svelte.js +0 -57
- package/dist/create-generate-image.svelte.d.ts +0 -94
- package/dist/create-generate-image.svelte.js +0 -66
- package/dist/create-generate-speech.svelte.d.ts +0 -85
- package/dist/create-generate-speech.svelte.js +0 -57
- package/dist/create-generate-video.svelte.d.ts +0 -99
- package/dist/create-generate-video.svelte.js +0 -143
- package/dist/create-generation.svelte.d.ts +0 -96
- package/dist/create-generation.svelte.js +0 -127
- package/dist/create-summarize.svelte.d.ts +0 -89
- package/dist/create-summarize.svelte.js +0 -61
- package/dist/create-transcription.svelte.d.ts +0 -94
- package/dist/create-transcription.svelte.js +0 -66
- package/dist/index.d.ts +0 -17
- package/dist/index.js +0 -11
- package/dist/types.d.ts +0 -157
- package/dist/types.js +0 -3
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import type { StreamChunk, TTSResult } from '@tanstack/ai';
|
|
2
|
-
import type { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutput, SpeechGenerateInput } from '@tanstack/ai-client';
|
|
3
|
-
/**
|
|
4
|
-
* Options for the createGenerateSpeech function.
|
|
5
|
-
*
|
|
6
|
-
* @template TOutput - The output type after optional transform (defaults to TTSResult)
|
|
7
|
-
*/
|
|
8
|
-
export interface CreateGenerateSpeechOptions<TOutput = TTSResult> {
|
|
9
|
-
/** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
10
|
-
connection?: ConnectConnectionAdapter;
|
|
11
|
-
/** Direct async function for speech generation */
|
|
12
|
-
fetcher?: GenerationFetcher<SpeechGenerateInput, TTSResult>;
|
|
13
|
-
/** Unique identifier for this generation instance */
|
|
14
|
-
id?: string;
|
|
15
|
-
/** Additional body parameters to send with connect-based adapter requests */
|
|
16
|
-
body?: Record<string, any>;
|
|
17
|
-
/** Display options for TanStack AI Devtools. */
|
|
18
|
-
devtools?: AIDevtoolsDisplayOptions;
|
|
19
|
-
/**
|
|
20
|
-
* Callback when speech is generated. Can optionally return a transformed value.
|
|
21
|
-
*
|
|
22
|
-
* - Return a non-null value to transform and store it as the result
|
|
23
|
-
* - Return `null` to keep the previous result unchanged
|
|
24
|
-
* - Return nothing (`void`) to store the raw result as-is
|
|
25
|
-
*/
|
|
26
|
-
onResult?: (result: TTSResult) => TOutput | null | void;
|
|
27
|
-
/** Callback when an error occurs */
|
|
28
|
-
onError?: (error: Error) => void;
|
|
29
|
-
/** Callback when progress is reported (0-100) */
|
|
30
|
-
onProgress?: (progress: number, message?: string) => void;
|
|
31
|
-
/** Callback for each stream chunk (connect-based adapter mode only) */
|
|
32
|
-
onChunk?: (chunk: StreamChunk) => void;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Return type for the createGenerateSpeech function.
|
|
36
|
-
*
|
|
37
|
-
* @template TOutput - The output type (after optional transform)
|
|
38
|
-
*/
|
|
39
|
-
export interface CreateGenerateSpeechReturn<TOutput = TTSResult> {
|
|
40
|
-
/** The TTS result containing audio data, or null */
|
|
41
|
-
readonly result: TOutput | null;
|
|
42
|
-
/** Whether generation is in progress */
|
|
43
|
-
readonly isLoading: boolean;
|
|
44
|
-
/** Current error, if any */
|
|
45
|
-
readonly error: Error | undefined;
|
|
46
|
-
/** Current state of the generation */
|
|
47
|
-
readonly status: GenerationClientState;
|
|
48
|
-
/** Trigger speech generation */
|
|
49
|
-
generate: (input: SpeechGenerateInput) => Promise<void>;
|
|
50
|
-
/** Abort the current generation */
|
|
51
|
-
stop: () => void;
|
|
52
|
-
/** Clear result, error, and return to idle */
|
|
53
|
-
reset: () => void;
|
|
54
|
-
/** Update additional body parameters */
|
|
55
|
-
updateBody: (body: Record<string, any>) => void;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Creates a reactive speech generation (text-to-speech) instance for Svelte 5.
|
|
59
|
-
*
|
|
60
|
-
* @example
|
|
61
|
-
* ```svelte
|
|
62
|
-
* <script>
|
|
63
|
-
* import { createGenerateSpeech, fetchServerSentEvents } from '@tanstack/ai-svelte'
|
|
64
|
-
*
|
|
65
|
-
* const speech = createGenerateSpeech({
|
|
66
|
-
* connection: fetchServerSentEvents('/api/generate/speech'),
|
|
67
|
-
* })
|
|
68
|
-
* </script>
|
|
69
|
-
*
|
|
70
|
-
* <div>
|
|
71
|
-
* <button onclick={() => speech.generate({ text: 'Hello world', voice: 'alloy' })}>
|
|
72
|
-
* Generate Speech
|
|
73
|
-
* </button>
|
|
74
|
-
* {#if speech.isLoading}
|
|
75
|
-
* <p>Generating...</p>
|
|
76
|
-
* {/if}
|
|
77
|
-
* {#if speech.result}
|
|
78
|
-
* <audio src={`data:audio/${speech.result.format};base64,${speech.result.audio}`} controls></audio>
|
|
79
|
-
* {/if}
|
|
80
|
-
* </div>
|
|
81
|
-
* ```
|
|
82
|
-
*/
|
|
83
|
-
export declare function createGenerateSpeech<TOnResult extends ((result: TTSResult) => any) | undefined = undefined>(options: Omit<CreateGenerateSpeechOptions, 'onResult'> & {
|
|
84
|
-
onResult?: TOnResult;
|
|
85
|
-
}): CreateGenerateSpeechReturn<InferGenerationOutput<TTSResult, TOnResult>>;
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { createGeneration } from './create-generation.svelte';
|
|
2
|
-
/**
|
|
3
|
-
* Creates a reactive speech generation (text-to-speech) instance for Svelte 5.
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* ```svelte
|
|
7
|
-
* <script>
|
|
8
|
-
* import { createGenerateSpeech, fetchServerSentEvents } from '@tanstack/ai-svelte'
|
|
9
|
-
*
|
|
10
|
-
* const speech = createGenerateSpeech({
|
|
11
|
-
* connection: fetchServerSentEvents('/api/generate/speech'),
|
|
12
|
-
* })
|
|
13
|
-
* </script>
|
|
14
|
-
*
|
|
15
|
-
* <div>
|
|
16
|
-
* <button onclick={() => speech.generate({ text: 'Hello world', voice: 'alloy' })}>
|
|
17
|
-
* Generate Speech
|
|
18
|
-
* </button>
|
|
19
|
-
* {#if speech.isLoading}
|
|
20
|
-
* <p>Generating...</p>
|
|
21
|
-
* {/if}
|
|
22
|
-
* {#if speech.result}
|
|
23
|
-
* <audio src={`data:audio/${speech.result.format};base64,${speech.result.audio}`} controls></audio>
|
|
24
|
-
* {/if}
|
|
25
|
-
* </div>
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
export function createGenerateSpeech(options) {
|
|
29
|
-
const devtools = {
|
|
30
|
-
...options.devtools,
|
|
31
|
-
framework: 'svelte',
|
|
32
|
-
hookName: 'createGenerateSpeech',
|
|
33
|
-
outputKind: 'audio',
|
|
34
|
-
};
|
|
35
|
-
const gen = createGeneration({
|
|
36
|
-
...options,
|
|
37
|
-
devtools,
|
|
38
|
-
});
|
|
39
|
-
return {
|
|
40
|
-
get result() {
|
|
41
|
-
return gen.result;
|
|
42
|
-
},
|
|
43
|
-
get isLoading() {
|
|
44
|
-
return gen.isLoading;
|
|
45
|
-
},
|
|
46
|
-
get error() {
|
|
47
|
-
return gen.error;
|
|
48
|
-
},
|
|
49
|
-
get status() {
|
|
50
|
-
return gen.status;
|
|
51
|
-
},
|
|
52
|
-
generate: gen.generate,
|
|
53
|
-
stop: gen.stop,
|
|
54
|
-
reset: gen.reset,
|
|
55
|
-
updateBody: gen.updateBody,
|
|
56
|
-
};
|
|
57
|
-
}
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import type { StreamChunk } from '@tanstack/ai';
|
|
2
|
-
import type { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutput, VideoGenerateInput, VideoGenerateResult, VideoStatusInfo } from '@tanstack/ai-client';
|
|
3
|
-
/**
|
|
4
|
-
* Options for the createGenerateVideo function.
|
|
5
|
-
*
|
|
6
|
-
* @template TOutput - The output type after optional transform (defaults to VideoGenerateResult)
|
|
7
|
-
*/
|
|
8
|
-
export interface CreateGenerateVideoOptions<TOutput = VideoGenerateResult> {
|
|
9
|
-
/** Connect-based adapter for streaming transport (server handles polling) */
|
|
10
|
-
connection?: ConnectConnectionAdapter;
|
|
11
|
-
/** Direct async function that returns a completed video result */
|
|
12
|
-
fetcher?: GenerationFetcher<VideoGenerateInput, VideoGenerateResult>;
|
|
13
|
-
/** Unique identifier for this generation instance */
|
|
14
|
-
id?: string;
|
|
15
|
-
/** Additional body parameters to send with connect-based adapter requests */
|
|
16
|
-
body?: Record<string, any>;
|
|
17
|
-
/** Display options for TanStack AI Devtools. */
|
|
18
|
-
devtools?: AIDevtoolsDisplayOptions;
|
|
19
|
-
/**
|
|
20
|
-
* Callback when video generation completes. Can optionally return a transformed value.
|
|
21
|
-
*
|
|
22
|
-
* - Return a non-null value to transform and store it as the result
|
|
23
|
-
* - Return `null` to keep the previous result unchanged
|
|
24
|
-
* - Return nothing (`void`) to store the raw result as-is
|
|
25
|
-
*/
|
|
26
|
-
onResult?: (result: VideoGenerateResult) => TOutput | null | void;
|
|
27
|
-
/** Callback when an error occurs */
|
|
28
|
-
onError?: (error: Error) => void;
|
|
29
|
-
/** Callback when progress is reported (0-100) */
|
|
30
|
-
onProgress?: (progress: number, message?: string) => void;
|
|
31
|
-
/** Callback when a video job is created */
|
|
32
|
-
onJobCreated?: (jobId: string) => void;
|
|
33
|
-
/** Callback on each status update */
|
|
34
|
-
onStatusUpdate?: (status: VideoStatusInfo) => void;
|
|
35
|
-
/** Callback for each stream chunk (connect-based adapter mode only) */
|
|
36
|
-
onChunk?: (chunk: StreamChunk) => void;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Return type for the createGenerateVideo function.
|
|
40
|
-
*
|
|
41
|
-
* @template TOutput - The output type (after optional transform)
|
|
42
|
-
*/
|
|
43
|
-
export interface CreateGenerateVideoReturn<TOutput = VideoGenerateResult> {
|
|
44
|
-
/** The final video result (with URL), or null */
|
|
45
|
-
readonly result: TOutput | null;
|
|
46
|
-
/** The current job ID, or null */
|
|
47
|
-
readonly jobId: string | null;
|
|
48
|
-
/** Current video generation status info, or null */
|
|
49
|
-
readonly videoStatus: VideoStatusInfo | null;
|
|
50
|
-
/** Whether generation/polling is in progress */
|
|
51
|
-
readonly isLoading: boolean;
|
|
52
|
-
/** Current error, if any */
|
|
53
|
-
readonly error: Error | undefined;
|
|
54
|
-
/** Current state of the generation */
|
|
55
|
-
readonly status: GenerationClientState;
|
|
56
|
-
/** Trigger video generation */
|
|
57
|
-
generate: (input: VideoGenerateInput) => Promise<void>;
|
|
58
|
-
/** Abort the current generation/polling */
|
|
59
|
-
stop: () => void;
|
|
60
|
-
/** Clear all state and return to idle */
|
|
61
|
-
reset: () => void;
|
|
62
|
-
/** Stop in-flight work and unregister devtools listeners */
|
|
63
|
-
dispose: () => void;
|
|
64
|
-
/** Update additional body parameters */
|
|
65
|
-
updateBody: (body: Record<string, any>) => void;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Creates a reactive video generation instance for Svelte 5.
|
|
69
|
-
*
|
|
70
|
-
* Video generation is asynchronous: a job is created, then polled for status
|
|
71
|
-
* until completion. This function handles the full lifecycle.
|
|
72
|
-
*
|
|
73
|
-
* @example
|
|
74
|
-
* ```svelte
|
|
75
|
-
* <script>
|
|
76
|
-
* import { createGenerateVideo, fetchServerSentEvents } from '@tanstack/ai-svelte'
|
|
77
|
-
*
|
|
78
|
-
* const video = createGenerateVideo({
|
|
79
|
-
* connection: fetchServerSentEvents('/api/generate/video'),
|
|
80
|
-
* onStatusUpdate: (status) => console.log(`Progress: ${status.progress}%`),
|
|
81
|
-
* })
|
|
82
|
-
* </script>
|
|
83
|
-
*
|
|
84
|
-
* <div>
|
|
85
|
-
* <button onclick={() => video.generate({ prompt: 'A flying car over a city' })}>
|
|
86
|
-
* Generate Video
|
|
87
|
-
* </button>
|
|
88
|
-
* {#if video.isLoading && video.videoStatus}
|
|
89
|
-
* <p>Status: {video.videoStatus.status} ({video.videoStatus.progress}%)</p>
|
|
90
|
-
* {/if}
|
|
91
|
-
* {#if video.result}
|
|
92
|
-
* <video src={video.result.url} controls></video>
|
|
93
|
-
* {/if}
|
|
94
|
-
* </div>
|
|
95
|
-
* ```
|
|
96
|
-
*/
|
|
97
|
-
export declare function createGenerateVideo<TOnResult extends ((result: VideoGenerateResult) => any) | undefined = undefined>(options: Omit<CreateGenerateVideoOptions, 'onResult'> & {
|
|
98
|
-
onResult?: TOnResult;
|
|
99
|
-
}): CreateGenerateVideoReturn<InferGenerationOutput<VideoGenerateResult, TOnResult>>;
|
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import { VideoGenerationClient } from '@tanstack/ai-client';
|
|
2
|
-
import { createVideoDevtoolsBridge } from '@tanstack/ai-client/devtools';
|
|
3
|
-
/**
|
|
4
|
-
* Creates a reactive video generation instance for Svelte 5.
|
|
5
|
-
*
|
|
6
|
-
* Video generation is asynchronous: a job is created, then polled for status
|
|
7
|
-
* until completion. This function handles the full lifecycle.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```svelte
|
|
11
|
-
* <script>
|
|
12
|
-
* import { createGenerateVideo, fetchServerSentEvents } from '@tanstack/ai-svelte'
|
|
13
|
-
*
|
|
14
|
-
* const video = createGenerateVideo({
|
|
15
|
-
* connection: fetchServerSentEvents('/api/generate/video'),
|
|
16
|
-
* onStatusUpdate: (status) => console.log(`Progress: ${status.progress}%`),
|
|
17
|
-
* })
|
|
18
|
-
* </script>
|
|
19
|
-
*
|
|
20
|
-
* <div>
|
|
21
|
-
* <button onclick={() => video.generate({ prompt: 'A flying car over a city' })}>
|
|
22
|
-
* Generate Video
|
|
23
|
-
* </button>
|
|
24
|
-
* {#if video.isLoading && video.videoStatus}
|
|
25
|
-
* <p>Status: {video.videoStatus.status} ({video.videoStatus.progress}%)</p>
|
|
26
|
-
* {/if}
|
|
27
|
-
* {#if video.result}
|
|
28
|
-
* <video src={video.result.url} controls></video>
|
|
29
|
-
* {/if}
|
|
30
|
-
* </div>
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
export function createGenerateVideo(options) {
|
|
34
|
-
const clientId = options.id ||
|
|
35
|
-
`video-${Date.now()}-${Math.random().toString(36).substring(7)}`;
|
|
36
|
-
// Create reactive state using Svelte 5 runes
|
|
37
|
-
let result = $state(null);
|
|
38
|
-
let jobId = $state(null);
|
|
39
|
-
let videoStatus = $state(null);
|
|
40
|
-
let isLoading = $state(false);
|
|
41
|
-
let error = $state(undefined);
|
|
42
|
-
let status = $state('idle');
|
|
43
|
-
// `body` uses a conditional spread because `VideoGenerationClientOptions.body`
|
|
44
|
-
// is declared `body?: Record<string, any>` (absent vs. present) under
|
|
45
|
-
// `exactOptionalPropertyTypes`. The optional caller `options.body` may be
|
|
46
|
-
// undefined, in which case we want the key to be absent on the target.
|
|
47
|
-
const baseOptions = {
|
|
48
|
-
id: clientId,
|
|
49
|
-
body: options.body,
|
|
50
|
-
devtoolsBridgeFactory: createVideoDevtoolsBridge,
|
|
51
|
-
devtools: {
|
|
52
|
-
...options.devtools,
|
|
53
|
-
framework: 'svelte',
|
|
54
|
-
hookName: 'createGenerateVideo',
|
|
55
|
-
outputKind: 'video',
|
|
56
|
-
},
|
|
57
|
-
onResult: (r) => options.onResult?.(r),
|
|
58
|
-
onError: (e) => options.onError?.(e),
|
|
59
|
-
onProgress: (p, m) => options.onProgress?.(p, m),
|
|
60
|
-
onChunk: (c) => options.onChunk?.(c),
|
|
61
|
-
onJobCreated: (id) => options.onJobCreated?.(id),
|
|
62
|
-
onStatusUpdate: (s) => options.onStatusUpdate?.(s),
|
|
63
|
-
onResultChange: (r) => {
|
|
64
|
-
result = r;
|
|
65
|
-
},
|
|
66
|
-
onLoadingChange: (l) => {
|
|
67
|
-
isLoading = l;
|
|
68
|
-
},
|
|
69
|
-
onErrorChange: (e) => {
|
|
70
|
-
error = e;
|
|
71
|
-
},
|
|
72
|
-
onStatusChange: (s) => {
|
|
73
|
-
status = s;
|
|
74
|
-
},
|
|
75
|
-
onJobIdChange: (id) => {
|
|
76
|
-
jobId = id;
|
|
77
|
-
},
|
|
78
|
-
onVideoStatusChange: (s) => {
|
|
79
|
-
videoStatus = s;
|
|
80
|
-
},
|
|
81
|
-
};
|
|
82
|
-
let client;
|
|
83
|
-
if (options.connection) {
|
|
84
|
-
client = new VideoGenerationClient({
|
|
85
|
-
...baseOptions,
|
|
86
|
-
connection: options.connection,
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
else if (options.fetcher) {
|
|
90
|
-
client = new VideoGenerationClient({
|
|
91
|
-
...baseOptions,
|
|
92
|
-
fetcher: options.fetcher,
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
throw new Error('createGenerateVideo requires either a connection or fetcher option');
|
|
97
|
-
}
|
|
98
|
-
client.mountDevtools();
|
|
99
|
-
// Note: Cleanup is handled by calling dispose() directly when needed.
|
|
100
|
-
// Unlike React/Vue/Solid, Svelte 5 runes like $effect can only be used
|
|
101
|
-
// during component initialization, so we don't add automatic cleanup here.
|
|
102
|
-
// Users should call video.dispose() in their component's cleanup if needed.
|
|
103
|
-
const generate = async (input) => {
|
|
104
|
-
await client.generate(input);
|
|
105
|
-
};
|
|
106
|
-
const stop = () => {
|
|
107
|
-
client.stop();
|
|
108
|
-
};
|
|
109
|
-
const reset = () => {
|
|
110
|
-
client.reset();
|
|
111
|
-
};
|
|
112
|
-
const dispose = () => {
|
|
113
|
-
client.dispose();
|
|
114
|
-
};
|
|
115
|
-
const updateBody = (newBody) => {
|
|
116
|
-
client.updateOptions({ body: newBody });
|
|
117
|
-
};
|
|
118
|
-
return {
|
|
119
|
-
get result() {
|
|
120
|
-
return result;
|
|
121
|
-
},
|
|
122
|
-
get jobId() {
|
|
123
|
-
return jobId;
|
|
124
|
-
},
|
|
125
|
-
get videoStatus() {
|
|
126
|
-
return videoStatus;
|
|
127
|
-
},
|
|
128
|
-
get isLoading() {
|
|
129
|
-
return isLoading;
|
|
130
|
-
},
|
|
131
|
-
get error() {
|
|
132
|
-
return error;
|
|
133
|
-
},
|
|
134
|
-
get status() {
|
|
135
|
-
return status;
|
|
136
|
-
},
|
|
137
|
-
generate,
|
|
138
|
-
stop,
|
|
139
|
-
reset,
|
|
140
|
-
dispose,
|
|
141
|
-
updateBody,
|
|
142
|
-
};
|
|
143
|
-
}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import type { StreamChunk } from '@tanstack/ai';
|
|
2
|
-
import type { AIDevtoolsDisplayOptions, ConnectConnectionAdapter, GenerationClientState, GenerationFetcher, InferGenerationOutput } from '@tanstack/ai-client';
|
|
3
|
-
/**
|
|
4
|
-
* Options for the createGeneration function.
|
|
5
|
-
*
|
|
6
|
-
* Accepts either a `connection` (streaming transport) or a `fetcher` (direct async call).
|
|
7
|
-
*
|
|
8
|
-
* @template TInput - The input type for the generation request
|
|
9
|
-
* @template TResult - The result type returned by the generation
|
|
10
|
-
* @template TOutput - The output type after optional transform (defaults to TResult)
|
|
11
|
-
*/
|
|
12
|
-
export interface CreateGenerationOptions<TInput, TResult, TOutput = TResult> {
|
|
13
|
-
/** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
14
|
-
connection?: ConnectConnectionAdapter;
|
|
15
|
-
/** Direct async function for one-shot generation (no streaming protocol needed) */
|
|
16
|
-
fetcher?: GenerationFetcher<TInput, TResult>;
|
|
17
|
-
/** Unique identifier for this generation instance */
|
|
18
|
-
id?: string;
|
|
19
|
-
/** Additional body parameters to send with connect-based adapter requests */
|
|
20
|
-
body?: Record<string, any>;
|
|
21
|
-
/** Display options for TanStack AI Devtools. */
|
|
22
|
-
devtools?: AIDevtoolsDisplayOptions;
|
|
23
|
-
/**
|
|
24
|
-
* Callback when a result is received. Can optionally return a transformed value.
|
|
25
|
-
*
|
|
26
|
-
* - Return a non-null value to transform and store it as the result
|
|
27
|
-
* - Return `null` to keep the previous result unchanged
|
|
28
|
-
* - Return nothing (`void`) to store the raw result as-is
|
|
29
|
-
*/
|
|
30
|
-
onResult?: (result: TResult) => TOutput | null | void;
|
|
31
|
-
/** Callback when an error occurs */
|
|
32
|
-
onError?: (error: Error) => void;
|
|
33
|
-
/** Callback when progress is reported (0-100) */
|
|
34
|
-
onProgress?: (progress: number, message?: string) => void;
|
|
35
|
-
/** Callback for each stream chunk (connect-based adapter mode only) */
|
|
36
|
-
onChunk?: (chunk: StreamChunk) => void;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Return type for the createGeneration function.
|
|
40
|
-
*
|
|
41
|
-
* @template TOutput - The output type (after optional transform)
|
|
42
|
-
*/
|
|
43
|
-
export interface CreateGenerationReturn<TOutput> {
|
|
44
|
-
/** The generation result, or null if not yet generated */
|
|
45
|
-
readonly result: TOutput | null;
|
|
46
|
-
/** Whether a generation is currently in progress */
|
|
47
|
-
readonly isLoading: boolean;
|
|
48
|
-
/** Current error, if any */
|
|
49
|
-
readonly error: Error | undefined;
|
|
50
|
-
/** Current state of the generation client */
|
|
51
|
-
readonly status: GenerationClientState;
|
|
52
|
-
/** Trigger a generation request */
|
|
53
|
-
generate: (input: Record<string, any>) => Promise<void>;
|
|
54
|
-
/** Abort the current generation */
|
|
55
|
-
stop: () => void;
|
|
56
|
-
/** Clear result, error, and return to idle */
|
|
57
|
-
reset: () => void;
|
|
58
|
-
/** Stop in-flight work and unregister devtools listeners */
|
|
59
|
-
dispose: () => void;
|
|
60
|
-
/** Update additional body parameters */
|
|
61
|
-
updateBody: (body: Record<string, any>) => void;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Creates a reactive generation instance for Svelte 5.
|
|
65
|
-
*
|
|
66
|
-
* This is the base function used by `createGenerateImage`, `createGenerateSpeech`,
|
|
67
|
-
* `createTranscription`, and `createSummarize`. You can also use it directly
|
|
68
|
-
* for custom generation types.
|
|
69
|
-
*
|
|
70
|
-
* @template TInput - The input type for the generation request
|
|
71
|
-
* @template TResult - The result type returned by the generation
|
|
72
|
-
*
|
|
73
|
-
* @example
|
|
74
|
-
* ```svelte
|
|
75
|
-
* <script>
|
|
76
|
-
* import { createGeneration, fetchServerSentEvents } from '@tanstack/ai-svelte'
|
|
77
|
-
*
|
|
78
|
-
* const gen = createGeneration({
|
|
79
|
-
* connection: fetchServerSentEvents('/api/generate/custom'),
|
|
80
|
-
* })
|
|
81
|
-
* </script>
|
|
82
|
-
*
|
|
83
|
-
* <div>
|
|
84
|
-
* <button onclick={() => gen.generate({ prompt: 'Hello' })}>Generate</button>
|
|
85
|
-
* {#if gen.isLoading}
|
|
86
|
-
* <p>Generating...</p>
|
|
87
|
-
* {/if}
|
|
88
|
-
* {#if gen.result}
|
|
89
|
-
* <p>{JSON.stringify(gen.result)}</p>
|
|
90
|
-
* {/if}
|
|
91
|
-
* </div>
|
|
92
|
-
* ```
|
|
93
|
-
*/
|
|
94
|
-
export declare function createGeneration<TInput extends Record<string, any>, TResult, TOnResult extends ((result: TResult) => any) | undefined = undefined>(options: Omit<CreateGenerationOptions<TInput, TResult>, 'onResult'> & {
|
|
95
|
-
onResult?: TOnResult;
|
|
96
|
-
}): CreateGenerationReturn<InferGenerationOutput<TResult, TOnResult>>;
|
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { GenerationClient } from '@tanstack/ai-client';
|
|
2
|
-
import { createGenerationDevtoolsBridge } from '@tanstack/ai-client/devtools';
|
|
3
|
-
/**
|
|
4
|
-
* Creates a reactive generation instance for Svelte 5.
|
|
5
|
-
*
|
|
6
|
-
* This is the base function used by `createGenerateImage`, `createGenerateSpeech`,
|
|
7
|
-
* `createTranscription`, and `createSummarize`. You can also use it directly
|
|
8
|
-
* for custom generation types.
|
|
9
|
-
*
|
|
10
|
-
* @template TInput - The input type for the generation request
|
|
11
|
-
* @template TResult - The result type returned by the generation
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```svelte
|
|
15
|
-
* <script>
|
|
16
|
-
* import { createGeneration, fetchServerSentEvents } from '@tanstack/ai-svelte'
|
|
17
|
-
*
|
|
18
|
-
* const gen = createGeneration({
|
|
19
|
-
* connection: fetchServerSentEvents('/api/generate/custom'),
|
|
20
|
-
* })
|
|
21
|
-
* </script>
|
|
22
|
-
*
|
|
23
|
-
* <div>
|
|
24
|
-
* <button onclick={() => gen.generate({ prompt: 'Hello' })}>Generate</button>
|
|
25
|
-
* {#if gen.isLoading}
|
|
26
|
-
* <p>Generating...</p>
|
|
27
|
-
* {/if}
|
|
28
|
-
* {#if gen.result}
|
|
29
|
-
* <p>{JSON.stringify(gen.result)}</p>
|
|
30
|
-
* {/if}
|
|
31
|
-
* </div>
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
export function createGeneration(options) {
|
|
35
|
-
const clientId = options.id || `gen-${Date.now()}-${Math.random().toString(36).substring(7)}`;
|
|
36
|
-
// Create reactive state using Svelte 5 runes
|
|
37
|
-
let result = $state(null);
|
|
38
|
-
let isLoading = $state(false);
|
|
39
|
-
let error = $state(undefined);
|
|
40
|
-
let status = $state('idle');
|
|
41
|
-
// `body` uses a conditional spread because `GenerationClientOptions.body`
|
|
42
|
-
// is declared `body?: Record<string, any>` (absent vs. present) under
|
|
43
|
-
// `exactOptionalPropertyTypes`. Assigning `undefined` directly would be
|
|
44
|
-
// rejected — the optional caller `options.body` may be undefined, in which
|
|
45
|
-
// case we want the key to be absent.
|
|
46
|
-
const clientOptions = {
|
|
47
|
-
id: clientId,
|
|
48
|
-
body: options.body,
|
|
49
|
-
devtoolsBridgeFactory: createGenerationDevtoolsBridge,
|
|
50
|
-
devtools: {
|
|
51
|
-
...options.devtools,
|
|
52
|
-
framework: 'svelte',
|
|
53
|
-
hookName: 'createGeneration',
|
|
54
|
-
},
|
|
55
|
-
onResult: (r) => options.onResult?.(r),
|
|
56
|
-
onError: (e) => options.onError?.(e),
|
|
57
|
-
onProgress: (p, m) => options.onProgress?.(p, m),
|
|
58
|
-
onChunk: (c) => options.onChunk?.(c),
|
|
59
|
-
onResultChange: (r) => {
|
|
60
|
-
result = r;
|
|
61
|
-
},
|
|
62
|
-
onLoadingChange: (l) => {
|
|
63
|
-
isLoading = l;
|
|
64
|
-
},
|
|
65
|
-
onErrorChange: (e) => {
|
|
66
|
-
error = e;
|
|
67
|
-
},
|
|
68
|
-
onStatusChange: (s) => {
|
|
69
|
-
status = s;
|
|
70
|
-
},
|
|
71
|
-
};
|
|
72
|
-
let client;
|
|
73
|
-
if (options.connection) {
|
|
74
|
-
client = new GenerationClient({
|
|
75
|
-
...clientOptions,
|
|
76
|
-
connection: options.connection,
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
else if (options.fetcher) {
|
|
80
|
-
client = new GenerationClient({
|
|
81
|
-
...clientOptions,
|
|
82
|
-
fetcher: options.fetcher,
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
throw new Error('createGeneration requires either a connection or fetcher option');
|
|
87
|
-
}
|
|
88
|
-
client.mountDevtools();
|
|
89
|
-
// Note: Cleanup is handled by calling dispose() directly when needed.
|
|
90
|
-
// Unlike React/Vue/Solid, Svelte 5 runes like $effect can only be used
|
|
91
|
-
// during component initialization, so we don't add automatic cleanup here.
|
|
92
|
-
// Users should call gen.dispose() in their component's cleanup if needed.
|
|
93
|
-
const generate = async (input) => {
|
|
94
|
-
await client.generate(input);
|
|
95
|
-
};
|
|
96
|
-
const stop = () => {
|
|
97
|
-
client.stop();
|
|
98
|
-
};
|
|
99
|
-
const reset = () => {
|
|
100
|
-
client.reset();
|
|
101
|
-
};
|
|
102
|
-
const dispose = () => {
|
|
103
|
-
client.dispose();
|
|
104
|
-
};
|
|
105
|
-
const updateBody = (newBody) => {
|
|
106
|
-
client.updateOptions({ body: newBody });
|
|
107
|
-
};
|
|
108
|
-
return {
|
|
109
|
-
get result() {
|
|
110
|
-
return result;
|
|
111
|
-
},
|
|
112
|
-
get isLoading() {
|
|
113
|
-
return isLoading;
|
|
114
|
-
},
|
|
115
|
-
get error() {
|
|
116
|
-
return error;
|
|
117
|
-
},
|
|
118
|
-
get status() {
|
|
119
|
-
return status;
|
|
120
|
-
},
|
|
121
|
-
generate: generate,
|
|
122
|
-
stop,
|
|
123
|
-
reset,
|
|
124
|
-
dispose,
|
|
125
|
-
updateBody,
|
|
126
|
-
};
|
|
127
|
-
}
|