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

inject-generate-image.ts

55 lines1.7 KB

1import { injectGeneration } from './inject-generation'
2import { reconstructImageResult } from '@tanstack/ai-client'
3import type { Signal } from '@angular/core'
4import type { ImageGenerationResult } from '@tanstack/ai'
5import type {
6 GenerationClientState,
7 ImageGenerateInput,
8 InferGenerationOutputFromReturn,
9} from '@tanstack/ai-client'
10import type {
11 InjectGenerationOptions,
12 InjectGenerationResult,
13} from './inject-generation'
14​
15export type InjectGenerateImageOptions<TOutput = ImageGenerationResult> = Omit<
16 InjectGenerationOptions<ImageGenerateInput, ImageGenerationResult, TOutput>,
17 'onResult' | 'reconstructResult'
18> & {
19 onResult?: (result: ImageGenerationResult) => TOutput | null | void
21​
22export interface InjectGenerateImageResult<
23 TOutput = ImageGenerationResult,
24> extends Omit<InjectGenerationResult<TOutput>, 'generate'> {
25 generate: (input: ImageGenerateInput) => Promise<void>
26 result: Signal<TOutput | null>
27 isLoading: Signal<boolean>
28 error: Signal<Error | undefined>
29 status: Signal<GenerationClientState>
31​
32export function injectGenerateImage<TTransformed = void>(
33 options: Omit<InjectGenerateImageOptions, 'onResult'> & {
34 onResult?: (result: ImageGenerationResult) => TTransformed
35 },
36): InjectGenerateImageResult<
37 InferGenerationOutputFromReturn<ImageGenerationResult, TTransformed>
38> {
39 const devtools = {
40 ...options.devtools,
41 framework: 'angular' as const,
42 hookName: 'injectGenerateImage',
43 outputKind: 'image' as const,
44 }
45 const generation = injectGeneration<
46 ImageGenerateInput,
47 ImageGenerationResult,
48 TTransformed
49 >({
50 ...options,
51 devtools,
52 reconstructResult: reconstructImageResult,
53 })
54 return generation