@tanstack/ai-angular 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/README.md +158 -0
- package/dist/README.md +158 -0
- package/dist/fesm2022/tanstack-ai-angular.mjs +478 -0
- package/dist/fesm2022/tanstack-ai-angular.mjs.map +1 -0
- package/dist/types/tanstack-ai-angular.d.ts +324 -0
- package/package.json +83 -0
- package/src/index.ts +89 -0
- package/src/inject-chat.ts +255 -0
- package/src/inject-generate-audio.ts +130 -0
- package/src/inject-generate-image.ts +58 -0
- package/src/inject-generate-speech.ts +55 -0
- package/src/inject-generate-video.ts +147 -0
- package/src/inject-generation.ts +155 -0
- package/src/inject-summarize.ts +58 -0
- package/src/inject-transcription.ts +66 -0
- package/src/internal/to-reactive.ts +26 -0
- package/src/types.ts +141 -0
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { ChatClient } from '@tanstack/ai-client'
|
|
2
|
+
import { createChatDevtoolsBridge } from '@tanstack/ai-client/devtools'
|
|
3
|
+
import {
|
|
4
|
+
DestroyRef,
|
|
5
|
+
Injector,
|
|
6
|
+
afterNextRender,
|
|
7
|
+
assertInInjectionContext,
|
|
8
|
+
computed,
|
|
9
|
+
effect,
|
|
10
|
+
inject,
|
|
11
|
+
signal,
|
|
12
|
+
} from '@angular/core'
|
|
13
|
+
import { toReactive } from './internal/to-reactive'
|
|
14
|
+
import type {
|
|
15
|
+
AnyClientTool,
|
|
16
|
+
InferSchemaType,
|
|
17
|
+
ModelMessage,
|
|
18
|
+
SchemaInput,
|
|
19
|
+
StreamChunk,
|
|
20
|
+
} from '@tanstack/ai'
|
|
21
|
+
import type {
|
|
22
|
+
ChatClientState,
|
|
23
|
+
ConnectionStatus,
|
|
24
|
+
InferredClientContext,
|
|
25
|
+
StructuredOutputPart,
|
|
26
|
+
} from '@tanstack/ai-client'
|
|
27
|
+
import type {
|
|
28
|
+
DeepPartial,
|
|
29
|
+
InjectChatOptions,
|
|
30
|
+
InjectChatResult,
|
|
31
|
+
MultimodalContent,
|
|
32
|
+
UIMessage,
|
|
33
|
+
} from './types'
|
|
34
|
+
|
|
35
|
+
let nextId = 0
|
|
36
|
+
|
|
37
|
+
export function injectChat<
|
|
38
|
+
TTools extends ReadonlyArray<AnyClientTool> = any,
|
|
39
|
+
TSchema extends SchemaInput | undefined = undefined,
|
|
40
|
+
TContext = InferredClientContext<TTools>,
|
|
41
|
+
>(
|
|
42
|
+
options: InjectChatOptions<
|
|
43
|
+
TTools,
|
|
44
|
+
TSchema,
|
|
45
|
+
TContext
|
|
46
|
+
> = {} as InjectChatOptions<TTools, TSchema, TContext>,
|
|
47
|
+
): InjectChatResult<TTools, TSchema> {
|
|
48
|
+
assertInInjectionContext(injectChat)
|
|
49
|
+
|
|
50
|
+
type Partial = DeepPartial<InferSchemaType<NonNullable<TSchema>>>
|
|
51
|
+
type Final = InferSchemaType<NonNullable<TSchema>>
|
|
52
|
+
|
|
53
|
+
const destroyRef = inject(DestroyRef)
|
|
54
|
+
const injector = inject(Injector)
|
|
55
|
+
const clientId = options.id || `injectChat-${nextId++}`
|
|
56
|
+
|
|
57
|
+
const messages = signal<Array<UIMessage<TTools>>>(
|
|
58
|
+
options.initialMessages || [],
|
|
59
|
+
)
|
|
60
|
+
const isLoading = signal(false)
|
|
61
|
+
const error = signal<Error | undefined>(undefined)
|
|
62
|
+
const status = signal<ChatClientState>('ready')
|
|
63
|
+
const isSubscribed = signal(false)
|
|
64
|
+
const connectionStatus = signal<ConnectionStatus>('disconnected')
|
|
65
|
+
const sessionGenerating = signal(false)
|
|
66
|
+
|
|
67
|
+
// Reactive option sources. Plain values become constant computeds.
|
|
68
|
+
const bodySource =
|
|
69
|
+
options.body !== undefined ? toReactive(options.body) : undefined
|
|
70
|
+
const forwardedPropsSource =
|
|
71
|
+
options.forwardedProps !== undefined
|
|
72
|
+
? toReactive(options.forwardedProps)
|
|
73
|
+
: undefined
|
|
74
|
+
const contextSource =
|
|
75
|
+
options.context !== undefined ? toReactive(options.context) : undefined
|
|
76
|
+
const liveSource =
|
|
77
|
+
options.live !== undefined ? toReactive(options.live) : undefined
|
|
78
|
+
|
|
79
|
+
const transport = options.connection
|
|
80
|
+
? { connection: options.connection }
|
|
81
|
+
: { fetcher: options.fetcher }
|
|
82
|
+
|
|
83
|
+
const client = new ChatClient<TTools, TContext>({
|
|
84
|
+
devtoolsBridgeFactory: createChatDevtoolsBridge,
|
|
85
|
+
...transport,
|
|
86
|
+
id: clientId,
|
|
87
|
+
...(options.initialMessages !== undefined && {
|
|
88
|
+
initialMessages: options.initialMessages,
|
|
89
|
+
}),
|
|
90
|
+
...(options.persistence !== undefined && {
|
|
91
|
+
persistence: options.persistence,
|
|
92
|
+
}),
|
|
93
|
+
...(bodySource !== undefined && { body: bodySource() }),
|
|
94
|
+
...(options.threadId !== undefined && { threadId: options.threadId }),
|
|
95
|
+
...(forwardedPropsSource !== undefined && {
|
|
96
|
+
forwardedProps: forwardedPropsSource(),
|
|
97
|
+
}),
|
|
98
|
+
...(contextSource !== undefined && { context: contextSource() }),
|
|
99
|
+
devtools: {
|
|
100
|
+
...options.devtools,
|
|
101
|
+
framework: 'angular',
|
|
102
|
+
hookName: 'injectChat',
|
|
103
|
+
outputKind: options.outputSchema ? 'structured' : 'chat',
|
|
104
|
+
},
|
|
105
|
+
onResponse: (response) => options.onResponse?.(response),
|
|
106
|
+
onChunk: (chunk: StreamChunk) => options.onChunk?.(chunk),
|
|
107
|
+
onFinish: (message) => options.onFinish?.(message),
|
|
108
|
+
onError: (err) => options.onError?.(err),
|
|
109
|
+
tools: options.tools,
|
|
110
|
+
onCustomEvent: (eventType, data, context) =>
|
|
111
|
+
options.onCustomEvent?.(eventType, data, context),
|
|
112
|
+
...(options.streamProcessor !== undefined && {
|
|
113
|
+
streamProcessor: options.streamProcessor,
|
|
114
|
+
}),
|
|
115
|
+
onMessagesChange: (m: Array<UIMessage<TTools>>) => messages.set(m),
|
|
116
|
+
onLoadingChange: (v: boolean) => isLoading.set(v),
|
|
117
|
+
onStatusChange: (v: ChatClientState) => status.set(v),
|
|
118
|
+
onErrorChange: (v: Error | undefined) => error.set(v),
|
|
119
|
+
onSubscriptionChange: (v: boolean) => isSubscribed.set(v),
|
|
120
|
+
onConnectionStatusChange: (v: ConnectionStatus) => connectionStatus.set(v),
|
|
121
|
+
onSessionGeneratingChange: (v: boolean) => sessionGenerating.set(v),
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
messages.set(client.getMessages())
|
|
125
|
+
|
|
126
|
+
// Sync reactive body / forwardedProps / context to the client.
|
|
127
|
+
if (bodySource || forwardedPropsSource || contextSource) {
|
|
128
|
+
effect(
|
|
129
|
+
() => {
|
|
130
|
+
const newBody = bodySource?.()
|
|
131
|
+
const newForwardedProps = forwardedPropsSource?.()
|
|
132
|
+
const newContext = contextSource?.()
|
|
133
|
+
client.updateOptions({
|
|
134
|
+
...(newBody !== undefined && { body: newBody }),
|
|
135
|
+
...(newForwardedProps !== undefined && {
|
|
136
|
+
forwardedProps: newForwardedProps,
|
|
137
|
+
}),
|
|
138
|
+
...(newContext !== undefined && { context: newContext }),
|
|
139
|
+
})
|
|
140
|
+
},
|
|
141
|
+
{ injector },
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Subscribe / unsubscribe based on reactive `live`.
|
|
146
|
+
if (liveSource) {
|
|
147
|
+
effect(
|
|
148
|
+
() => {
|
|
149
|
+
if (liveSource()) {
|
|
150
|
+
client.subscribe()
|
|
151
|
+
} else {
|
|
152
|
+
client.unsubscribe()
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
{ injector },
|
|
156
|
+
)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
afterNextRender(() => client.mountDevtools(), { injector })
|
|
160
|
+
|
|
161
|
+
destroyRef.onDestroy(() => {
|
|
162
|
+
if (liveSource?.()) {
|
|
163
|
+
client.unsubscribe()
|
|
164
|
+
} else {
|
|
165
|
+
client.stop()
|
|
166
|
+
}
|
|
167
|
+
client.dispose()
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
// Active structured-output part = the one on the assistant message after the
|
|
171
|
+
// latest user message. Ported from ai-vue/src/use-chat.ts.
|
|
172
|
+
const activeStructuredPart = computed<StructuredOutputPart | null>(() => {
|
|
173
|
+
const list = messages()
|
|
174
|
+
let lastUserIndex = -1
|
|
175
|
+
for (let i = list.length - 1; i >= 0; i--) {
|
|
176
|
+
if (list[i]?.role === 'user') {
|
|
177
|
+
lastUserIndex = i
|
|
178
|
+
break
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (lastUserIndex === -1) return null
|
|
182
|
+
for (let i = list.length - 1; i > lastUserIndex; i--) {
|
|
183
|
+
const m = list[i]
|
|
184
|
+
if (m?.role !== 'assistant') continue
|
|
185
|
+
const part = m.parts.find(
|
|
186
|
+
(p): p is StructuredOutputPart => p.type === 'structured-output',
|
|
187
|
+
)
|
|
188
|
+
if (part) return part
|
|
189
|
+
}
|
|
190
|
+
return null
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
const partial = computed<Partial>(() => {
|
|
194
|
+
const part = activeStructuredPart()
|
|
195
|
+
if (!part) return {} as Partial
|
|
196
|
+
const v = part.partial ?? part.data
|
|
197
|
+
return (v ?? {}) as Partial
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
const final = computed<Final | null>(() => {
|
|
201
|
+
const part = activeStructuredPart()
|
|
202
|
+
if (!part || part.status !== 'complete') return null
|
|
203
|
+
return part.data as Final
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
const sendMessage = async (content: string | MultimodalContent) => {
|
|
207
|
+
await client.sendMessage(content)
|
|
208
|
+
}
|
|
209
|
+
const append = async (message: ModelMessage | UIMessage<TTools>) => {
|
|
210
|
+
await client.append(message)
|
|
211
|
+
}
|
|
212
|
+
const reload = async () => {
|
|
213
|
+
await client.reload()
|
|
214
|
+
}
|
|
215
|
+
const stop = () => client.stop()
|
|
216
|
+
const clear = () => client.clear()
|
|
217
|
+
const setMessages = (m: Array<UIMessage<TTools>>) =>
|
|
218
|
+
client.setMessagesManually(m)
|
|
219
|
+
const addToolResult = async (result: {
|
|
220
|
+
toolCallId: string
|
|
221
|
+
tool: string
|
|
222
|
+
output: any
|
|
223
|
+
state?: 'output-available' | 'output-error'
|
|
224
|
+
errorText?: string
|
|
225
|
+
}) => {
|
|
226
|
+
await client.addToolResult(result)
|
|
227
|
+
}
|
|
228
|
+
const addToolApprovalResponse = async (response: {
|
|
229
|
+
id: string
|
|
230
|
+
approved: boolean
|
|
231
|
+
}) => {
|
|
232
|
+
await client.addToolApprovalResponse(response)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// eslint-disable-next-line no-restricted-syntax -- return shape diverges from conditional InjectChatResult<TTools, TSchema>; TS can't structurally narrow the TSchema-gated partial/final signals
|
|
236
|
+
return {
|
|
237
|
+
messages: messages.asReadonly(),
|
|
238
|
+
sendMessage,
|
|
239
|
+
append,
|
|
240
|
+
reload,
|
|
241
|
+
stop,
|
|
242
|
+
isLoading: isLoading.asReadonly(),
|
|
243
|
+
error: error.asReadonly(),
|
|
244
|
+
status: status.asReadonly(),
|
|
245
|
+
isSubscribed: isSubscribed.asReadonly(),
|
|
246
|
+
connectionStatus: connectionStatus.asReadonly(),
|
|
247
|
+
sessionGenerating: sessionGenerating.asReadonly(),
|
|
248
|
+
setMessages,
|
|
249
|
+
clear,
|
|
250
|
+
addToolResult,
|
|
251
|
+
addToolApprovalResponse,
|
|
252
|
+
partial,
|
|
253
|
+
final,
|
|
254
|
+
} as unknown as InjectChatResult<TTools, TSchema>
|
|
255
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { injectGeneration } from './inject-generation'
|
|
2
|
+
import type { AudioGenerationResult, StreamChunk } from '@tanstack/ai'
|
|
3
|
+
import type {
|
|
4
|
+
AIDevtoolsDisplayOptions,
|
|
5
|
+
AudioGenerateInput,
|
|
6
|
+
ConnectConnectionAdapter,
|
|
7
|
+
GenerationClientState,
|
|
8
|
+
GenerationFetcher,
|
|
9
|
+
InferGenerationOutput,
|
|
10
|
+
} from '@tanstack/ai-client'
|
|
11
|
+
import type { Signal } from '@angular/core'
|
|
12
|
+
import type { ReactiveOption } from './internal/to-reactive'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Options for the injectGenerateAudio injectable.
|
|
16
|
+
*
|
|
17
|
+
* @template TOutput - The output type after optional transform (defaults to AudioGenerationResult)
|
|
18
|
+
*/
|
|
19
|
+
export interface InjectGenerateAudioOptions<TOutput = AudioGenerationResult> {
|
|
20
|
+
/** Connect-based adapter for streaming transport (SSE, HTTP stream, custom) */
|
|
21
|
+
connection?: ConnectConnectionAdapter
|
|
22
|
+
/** Direct async function for audio generation */
|
|
23
|
+
fetcher?: GenerationFetcher<AudioGenerateInput, AudioGenerationResult>
|
|
24
|
+
/** Unique identifier for this generation instance */
|
|
25
|
+
id?: string
|
|
26
|
+
/** Additional body parameters to send with connect-based adapter requests. Reactive. */
|
|
27
|
+
body?: ReactiveOption<Record<string, any>>
|
|
28
|
+
/** Display options for TanStack AI Devtools. */
|
|
29
|
+
devtools?: AIDevtoolsDisplayOptions
|
|
30
|
+
/**
|
|
31
|
+
* Callback when audio is generated. Can optionally return a transformed value.
|
|
32
|
+
*
|
|
33
|
+
* - Return a non-null value to transform and store it as the result
|
|
34
|
+
* - Return `null` to keep the previous result unchanged
|
|
35
|
+
* - Return nothing (`void`) to store the raw result as-is
|
|
36
|
+
*/
|
|
37
|
+
onResult?: (result: AudioGenerationResult) => TOutput | null | void
|
|
38
|
+
/** Callback when an error occurs */
|
|
39
|
+
onError?: (error: Error) => void
|
|
40
|
+
/** Callback when progress is reported (0-100) */
|
|
41
|
+
onProgress?: (progress: number, message?: string) => void
|
|
42
|
+
/** Callback for each stream chunk (connect-based adapter mode only) */
|
|
43
|
+
onChunk?: (chunk: StreamChunk) => void
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Return type for the injectGenerateAudio injectable.
|
|
48
|
+
*
|
|
49
|
+
* @template TOutput - The output type (after optional transform)
|
|
50
|
+
*/
|
|
51
|
+
export interface InjectGenerateAudioResult<TOutput = AudioGenerationResult> {
|
|
52
|
+
/** Trigger audio generation */
|
|
53
|
+
generate: (input: AudioGenerateInput) => Promise<void>
|
|
54
|
+
/** The generation result containing audio, or null */
|
|
55
|
+
result: Signal<TOutput | null>
|
|
56
|
+
/** Whether generation is in progress */
|
|
57
|
+
isLoading: Signal<boolean>
|
|
58
|
+
/** Current error, if any */
|
|
59
|
+
error: Signal<Error | undefined>
|
|
60
|
+
/** Current state of the generation */
|
|
61
|
+
status: Signal<GenerationClientState>
|
|
62
|
+
/** Abort the current generation */
|
|
63
|
+
stop: () => void
|
|
64
|
+
/** Clear result, error, and return to idle */
|
|
65
|
+
reset: () => void
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Angular injectable for generating audio (music, sound effects) using AI models.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* import { Component } from '@angular/core'
|
|
74
|
+
* import { injectGenerateAudio } from '@tanstack/ai-angular'
|
|
75
|
+
* import { fetchServerSentEvents } from '@tanstack/ai-client'
|
|
76
|
+
*
|
|
77
|
+
* @Component({
|
|
78
|
+
* selector: 'app-audio',
|
|
79
|
+
* template: `
|
|
80
|
+
* <button (click)="generate({ prompt: 'An upbeat electronic track', duration: 10 })">
|
|
81
|
+
* Generate
|
|
82
|
+
* </button>
|
|
83
|
+
* @if (result()) {
|
|
84
|
+
* <audio [src]="result()!.audio.url" controls></audio>
|
|
85
|
+
* }
|
|
86
|
+
* `,
|
|
87
|
+
* })
|
|
88
|
+
* export class AudioComponent {
|
|
89
|
+
* private gen = injectGenerateAudio({
|
|
90
|
+
* connection: fetchServerSentEvents('/api/generate/audio'),
|
|
91
|
+
* })
|
|
92
|
+
*
|
|
93
|
+
* generate = this.gen.generate
|
|
94
|
+
* result = this.gen.result
|
|
95
|
+
* isLoading = this.gen.isLoading
|
|
96
|
+
* }
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export function injectGenerateAudio<
|
|
100
|
+
TOnResult extends ((result: AudioGenerationResult) => any) | undefined =
|
|
101
|
+
undefined,
|
|
102
|
+
>(
|
|
103
|
+
options: Omit<InjectGenerateAudioOptions, 'onResult'> & {
|
|
104
|
+
onResult?: TOnResult
|
|
105
|
+
},
|
|
106
|
+
): InjectGenerateAudioResult<
|
|
107
|
+
InferGenerationOutput<AudioGenerationResult, TOnResult>
|
|
108
|
+
> {
|
|
109
|
+
const devtools = {
|
|
110
|
+
...options.devtools,
|
|
111
|
+
framework: 'angular',
|
|
112
|
+
hookName: 'injectGenerateAudio',
|
|
113
|
+
outputKind: 'audio' as const,
|
|
114
|
+
}
|
|
115
|
+
const { generate, result, isLoading, error, status, stop, reset } =
|
|
116
|
+
injectGeneration<AudioGenerateInput, AudioGenerationResult, TOnResult>({
|
|
117
|
+
...options,
|
|
118
|
+
devtools,
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
generate: generate as (input: AudioGenerateInput) => Promise<void>,
|
|
123
|
+
result,
|
|
124
|
+
isLoading,
|
|
125
|
+
error,
|
|
126
|
+
status,
|
|
127
|
+
stop,
|
|
128
|
+
reset,
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { injectGeneration } from './inject-generation'
|
|
2
|
+
import type { Signal } from '@angular/core'
|
|
3
|
+
import type { ImageGenerationResult } from '@tanstack/ai'
|
|
4
|
+
import type {
|
|
5
|
+
GenerationClientState,
|
|
6
|
+
ImageGenerateInput,
|
|
7
|
+
InferGenerationOutput,
|
|
8
|
+
} from '@tanstack/ai-client'
|
|
9
|
+
import type { InjectGenerationOptions } from './inject-generation'
|
|
10
|
+
|
|
11
|
+
export type InjectGenerateImageOptions<TOutput = ImageGenerationResult> = Omit<
|
|
12
|
+
InjectGenerationOptions<ImageGenerateInput, ImageGenerationResult, TOutput>,
|
|
13
|
+
'onResult'
|
|
14
|
+
> & {
|
|
15
|
+
onResult?: (result: ImageGenerationResult) => TOutput | null | void
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface InjectGenerateImageResult<TOutput = ImageGenerationResult> {
|
|
19
|
+
generate: (input: ImageGenerateInput) => Promise<void>
|
|
20
|
+
result: Signal<TOutput | null>
|
|
21
|
+
isLoading: Signal<boolean>
|
|
22
|
+
error: Signal<Error | undefined>
|
|
23
|
+
status: Signal<GenerationClientState>
|
|
24
|
+
stop: () => void
|
|
25
|
+
reset: () => void
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function injectGenerateImage<
|
|
29
|
+
TOnResult extends ((result: ImageGenerationResult) => any) | undefined =
|
|
30
|
+
undefined,
|
|
31
|
+
>(
|
|
32
|
+
options: Omit<InjectGenerateImageOptions, 'onResult'> & {
|
|
33
|
+
onResult?: TOnResult
|
|
34
|
+
},
|
|
35
|
+
): InjectGenerateImageResult<
|
|
36
|
+
InferGenerationOutput<ImageGenerationResult, TOnResult>
|
|
37
|
+
> {
|
|
38
|
+
const devtools = {
|
|
39
|
+
...options.devtools,
|
|
40
|
+
framework: 'angular' as const,
|
|
41
|
+
hookName: 'injectGenerateImage',
|
|
42
|
+
outputKind: 'image' as const,
|
|
43
|
+
}
|
|
44
|
+
const { generate, result, isLoading, error, status, stop, reset } =
|
|
45
|
+
injectGeneration<ImageGenerateInput, ImageGenerationResult, TOnResult>({
|
|
46
|
+
...options,
|
|
47
|
+
devtools,
|
|
48
|
+
})
|
|
49
|
+
return {
|
|
50
|
+
generate: generate as (input: ImageGenerateInput) => Promise<void>,
|
|
51
|
+
result,
|
|
52
|
+
isLoading,
|
|
53
|
+
error,
|
|
54
|
+
status,
|
|
55
|
+
stop,
|
|
56
|
+
reset,
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { injectGeneration } from './inject-generation'
|
|
2
|
+
import type { Signal } from '@angular/core'
|
|
3
|
+
import type { TTSResult } from '@tanstack/ai'
|
|
4
|
+
import type {
|
|
5
|
+
GenerationClientState,
|
|
6
|
+
InferGenerationOutput,
|
|
7
|
+
SpeechGenerateInput,
|
|
8
|
+
} from '@tanstack/ai-client'
|
|
9
|
+
import type { InjectGenerationOptions } from './inject-generation'
|
|
10
|
+
|
|
11
|
+
export type InjectGenerateSpeechOptions<TOutput = TTSResult> = Omit<
|
|
12
|
+
InjectGenerationOptions<SpeechGenerateInput, TTSResult, TOutput>,
|
|
13
|
+
'onResult'
|
|
14
|
+
> & {
|
|
15
|
+
onResult?: (result: TTSResult) => TOutput | null | void
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface InjectGenerateSpeechResult<TOutput = TTSResult> {
|
|
19
|
+
generate: (input: SpeechGenerateInput) => Promise<void>
|
|
20
|
+
result: Signal<TOutput | null>
|
|
21
|
+
isLoading: Signal<boolean>
|
|
22
|
+
error: Signal<Error | undefined>
|
|
23
|
+
status: Signal<GenerationClientState>
|
|
24
|
+
stop: () => void
|
|
25
|
+
reset: () => void
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function injectGenerateSpeech<
|
|
29
|
+
TOnResult extends ((result: TTSResult) => any) | undefined = undefined,
|
|
30
|
+
>(
|
|
31
|
+
options: Omit<InjectGenerateSpeechOptions, 'onResult'> & {
|
|
32
|
+
onResult?: TOnResult
|
|
33
|
+
},
|
|
34
|
+
): InjectGenerateSpeechResult<InferGenerationOutput<TTSResult, TOnResult>> {
|
|
35
|
+
const devtools = {
|
|
36
|
+
...options.devtools,
|
|
37
|
+
framework: 'angular' as const,
|
|
38
|
+
hookName: 'injectGenerateSpeech',
|
|
39
|
+
outputKind: 'audio' as const,
|
|
40
|
+
}
|
|
41
|
+
const { generate, result, isLoading, error, status, stop, reset } =
|
|
42
|
+
injectGeneration<SpeechGenerateInput, TTSResult, TOnResult>({
|
|
43
|
+
...options,
|
|
44
|
+
devtools,
|
|
45
|
+
})
|
|
46
|
+
return {
|
|
47
|
+
generate: generate as (input: SpeechGenerateInput) => Promise<void>,
|
|
48
|
+
result,
|
|
49
|
+
isLoading,
|
|
50
|
+
error,
|
|
51
|
+
status,
|
|
52
|
+
stop,
|
|
53
|
+
reset,
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { VideoGenerationClient } from '@tanstack/ai-client'
|
|
2
|
+
import { createVideoDevtoolsBridge } from '@tanstack/ai-client/devtools'
|
|
3
|
+
import {
|
|
4
|
+
DestroyRef,
|
|
5
|
+
Injector,
|
|
6
|
+
afterNextRender,
|
|
7
|
+
assertInInjectionContext,
|
|
8
|
+
effect,
|
|
9
|
+
inject,
|
|
10
|
+
signal,
|
|
11
|
+
} from '@angular/core'
|
|
12
|
+
import { toReactive } from './internal/to-reactive'
|
|
13
|
+
import type { Signal } from '@angular/core'
|
|
14
|
+
import type { ReactiveOption } from './internal/to-reactive'
|
|
15
|
+
import type {
|
|
16
|
+
AIDevtoolsDisplayOptions,
|
|
17
|
+
ConnectConnectionAdapter,
|
|
18
|
+
GenerationClientState,
|
|
19
|
+
GenerationFetcher,
|
|
20
|
+
InferGenerationOutput,
|
|
21
|
+
VideoGenerateInput,
|
|
22
|
+
VideoGenerateResult,
|
|
23
|
+
VideoStatusInfo,
|
|
24
|
+
} from '@tanstack/ai-client'
|
|
25
|
+
import type { StreamChunk } from '@tanstack/ai'
|
|
26
|
+
|
|
27
|
+
let nextId = 0
|
|
28
|
+
|
|
29
|
+
export interface InjectGenerateVideoOptions<TOutput = VideoGenerateResult> {
|
|
30
|
+
connection?: ConnectConnectionAdapter
|
|
31
|
+
fetcher?: GenerationFetcher<VideoGenerateInput, VideoGenerateResult>
|
|
32
|
+
id?: string
|
|
33
|
+
body?: ReactiveOption<Record<string, any>>
|
|
34
|
+
devtools?: AIDevtoolsDisplayOptions
|
|
35
|
+
onResult?: (result: VideoGenerateResult) => TOutput | null | void
|
|
36
|
+
onError?: (error: Error) => void
|
|
37
|
+
onProgress?: (progress: number, message?: string) => void
|
|
38
|
+
onJobCreated?: (jobId: string) => void
|
|
39
|
+
onStatusUpdate?: (status: VideoStatusInfo) => void
|
|
40
|
+
onChunk?: (chunk: StreamChunk) => void
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface InjectGenerateVideoResult<TOutput = VideoGenerateResult> {
|
|
44
|
+
generate: (input: VideoGenerateInput) => Promise<void>
|
|
45
|
+
result: Signal<TOutput | null>
|
|
46
|
+
jobId: Signal<string | null>
|
|
47
|
+
videoStatus: Signal<VideoStatusInfo | null>
|
|
48
|
+
isLoading: Signal<boolean>
|
|
49
|
+
error: Signal<Error | undefined>
|
|
50
|
+
status: Signal<GenerationClientState>
|
|
51
|
+
stop: () => void
|
|
52
|
+
reset: () => void
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function injectGenerateVideo<
|
|
56
|
+
TOnResult extends ((result: VideoGenerateResult) => any) | undefined =
|
|
57
|
+
undefined,
|
|
58
|
+
>(
|
|
59
|
+
options: Omit<InjectGenerateVideoOptions, 'onResult'> & {
|
|
60
|
+
onResult?: TOnResult
|
|
61
|
+
},
|
|
62
|
+
): InjectGenerateVideoResult<
|
|
63
|
+
InferGenerationOutput<VideoGenerateResult, TOnResult>
|
|
64
|
+
> {
|
|
65
|
+
assertInInjectionContext(injectGenerateVideo)
|
|
66
|
+
|
|
67
|
+
type TOutput = InferGenerationOutput<VideoGenerateResult, TOnResult>
|
|
68
|
+
|
|
69
|
+
const destroyRef = inject(DestroyRef)
|
|
70
|
+
const injector = inject(Injector)
|
|
71
|
+
const clientId = options.id || `injectGenerateVideo-${nextId++}`
|
|
72
|
+
|
|
73
|
+
const result = signal<TOutput | null>(null)
|
|
74
|
+
const jobId = signal<string | null>(null)
|
|
75
|
+
const videoStatus = signal<VideoStatusInfo | null>(null)
|
|
76
|
+
const isLoading = signal(false)
|
|
77
|
+
const error = signal<Error | undefined>(undefined)
|
|
78
|
+
const status = signal<GenerationClientState>('idle')
|
|
79
|
+
|
|
80
|
+
const bodySource =
|
|
81
|
+
options.body !== undefined ? toReactive(options.body) : undefined
|
|
82
|
+
|
|
83
|
+
const baseOptions = {
|
|
84
|
+
id: clientId,
|
|
85
|
+
...(bodySource !== undefined && { body: bodySource() }),
|
|
86
|
+
devtoolsBridgeFactory: createVideoDevtoolsBridge,
|
|
87
|
+
devtools: {
|
|
88
|
+
...options.devtools,
|
|
89
|
+
framework: 'angular',
|
|
90
|
+
hookName: 'injectGenerateVideo',
|
|
91
|
+
outputKind: 'video' as const,
|
|
92
|
+
},
|
|
93
|
+
onResult: (r: VideoGenerateResult) => options.onResult?.(r),
|
|
94
|
+
onError: (e: Error) => options.onError?.(e),
|
|
95
|
+
onProgress: (p: number, m?: string) => options.onProgress?.(p, m),
|
|
96
|
+
onChunk: (c: StreamChunk) => options.onChunk?.(c),
|
|
97
|
+
onJobCreated: (id: string) => options.onJobCreated?.(id),
|
|
98
|
+
onStatusUpdate: (s: VideoStatusInfo) => options.onStatusUpdate?.(s),
|
|
99
|
+
onResultChange: (r: TOutput | null) => result.set(r),
|
|
100
|
+
onLoadingChange: (l: boolean) => isLoading.set(l),
|
|
101
|
+
onErrorChange: (e: Error | undefined) => error.set(e),
|
|
102
|
+
onStatusChange: (s: GenerationClientState) => status.set(s),
|
|
103
|
+
onJobIdChange: (id: string | null) => jobId.set(id),
|
|
104
|
+
onVideoStatusChange: (s: VideoStatusInfo | null) => videoStatus.set(s),
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
let client: VideoGenerationClient<TOutput>
|
|
108
|
+
if (options.connection) {
|
|
109
|
+
client = new VideoGenerationClient({
|
|
110
|
+
...baseOptions,
|
|
111
|
+
connection: options.connection,
|
|
112
|
+
})
|
|
113
|
+
} else if (options.fetcher) {
|
|
114
|
+
client = new VideoGenerationClient({
|
|
115
|
+
...baseOptions,
|
|
116
|
+
fetcher: options.fetcher,
|
|
117
|
+
})
|
|
118
|
+
} else {
|
|
119
|
+
throw new Error(
|
|
120
|
+
'injectGenerateVideo requires either a connection or fetcher option',
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (bodySource) {
|
|
125
|
+
effect(
|
|
126
|
+
() => {
|
|
127
|
+
client.updateOptions({ body: bodySource() })
|
|
128
|
+
},
|
|
129
|
+
{ injector },
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
afterNextRender(() => client.mountDevtools(), { injector })
|
|
134
|
+
destroyRef.onDestroy(() => client.dispose())
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
generate: (input: VideoGenerateInput) => client.generate(input),
|
|
138
|
+
result: result.asReadonly(),
|
|
139
|
+
jobId: jobId.asReadonly(),
|
|
140
|
+
videoStatus: videoStatus.asReadonly(),
|
|
141
|
+
isLoading: isLoading.asReadonly(),
|
|
142
|
+
error: error.asReadonly(),
|
|
143
|
+
status: status.asReadonly(),
|
|
144
|
+
stop: () => client.stop(),
|
|
145
|
+
reset: () => client.reset(),
|
|
146
|
+
}
|
|
147
|
+
}
|