Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | /** * React hooks for making LLM calls with progress tracking * * These hooks integrate the LLM client with React Query for proper * state management, caching, and UI feedback. * * @example * ```typescript * import { useLLMCall } from '@/hooks/useLLMCall' * import { z } from 'zod' * * const SentimentSchema = z.object({ * sentiment: z.enum(['positive', 'negative', 'neutral']), * confidence: z.number(), * }) * * function MyComponent() { * const { mutate, progress, isPending, error, data } = useLLMCall(SentimentSchema) * * return ( * <div> * <button onClick={() => mutate({ prompt: 'Analyze: I love this!' })}> * Analyze * </button> * {progress && <div>{progress.message}</div>} * {data && <div>Sentiment: {data.data.sentiment}</div>} * </div> * ) * } * ``` */ import { useState, useCallback } from 'react' import { useMutation, type UseMutationOptions } from '@tanstack/react-query' import type { z } from 'zod' import { llm, type LLMProgress, type LLMResponse } from '@/lib/llm' /** Request options for LLM call (without schema) */ interface LLMCallRequest { prompt: string images?: string[] provider?: string model?: string maxRetries?: number } /** Request options for vision call (requires images) */ interface LLMVisionRequest extends LLMCallRequest { images: string[] } /** * Hook for making type-safe LLM calls with progress tracking * * @param schema - Zod schema for validating the LLM response * @param options - Optional React Query mutation options */ export function useLLMCall<T extends z.ZodType>( schema: T, options?: Omit<UseMutationOptions<LLMResponse<z.infer<T>>, Error, LLMCallRequest>, 'mutationFn'> ) { const [progress, setProgress] = useState<LLMProgress | null>(null) const mutation = useMutation({ mutationFn: async (request: LLMCallRequest) => { setProgress(null) return llm.call({ ...request, schema, onProgress: setProgress, }) }, onSettled: () => { setProgress(null) }, ...options, }) return { ...mutation, progress, } } /** * Hook for making vision (image + text) LLM calls with progress tracking * * @param schema - Zod schema for validating the LLM response * @param options - Optional React Query mutation options * * @example * ```typescript * const { mutate, progress } = useLLMVision(ImageAnalysisSchema) * * mutate({ * prompt: 'Describe this image', * images: ['data:image/jpeg;base64,...'], * }) * ``` */ export function useLLMVision<T extends z.ZodType>( schema: T, options?: Omit<UseMutationOptions<LLMResponse<z.infer<T>>, Error, LLMVisionRequest>, 'mutationFn'> ) { const [progress, setProgress] = useState<LLMProgress | null>(null) const mutation = useMutation({ mutationFn: async (request: LLMVisionRequest) => { setProgress(null) return llm.vision({ ...request, schema, onProgress: setProgress, }) }, onSettled: () => { setProgress(null) }, ...options, }) return { ...mutation, progress, } } /** * Hook for getting LLM client status and configuration * * @example * ```typescript * const { providers, isProviderAvailable, defaultProvider } = useLLMStatus() * * if (!isProviderAvailable('openai')) { * return <div>OpenAI is not configured</div> * } * ``` */ export function useLLMStatus() { const getProviders = useCallback(() => llm.getProviders(), []) const isProviderAvailable = useCallback((name: string) => llm.isProviderAvailable(name), []) const getDefaultProvider = useCallback(() => llm.getDefaultProvider(), []) const getDefaultModel = useCallback((provider?: string) => llm.getDefaultModel(provider), []) return { providers: getProviders(), isProviderAvailable, defaultProvider: getDefaultProvider(), getDefaultModel, } } |