mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-27 12:51:26 +08:00
feat(hooks): add useQuickCompletion hook for quick model completions
This commit is contained in:
parent
b5b577dc79
commit
684c0a7b63
48
src/renderer/src/hooks/useQuickCompletion.ts
Normal file
48
src/renderer/src/hooks/useQuickCompletion.ts
Normal file
@ -0,0 +1,48 @@
|
||||
import { fetchChatCompletion } from '@renderer/services/ApiService'
|
||||
import { getDefaultAssistant } from '@renderer/services/AssistantService'
|
||||
import type { Assistant, FetchChatCompletionParams } from '@renderer/types'
|
||||
import type { Chunk } from '@renderer/types/chunk'
|
||||
import { useCallback } from 'react'
|
||||
|
||||
import { useDefaultModel } from './useAssistant'
|
||||
|
||||
/**
|
||||
* Parameters for performing a quick completion using the quick model.
|
||||
*/
|
||||
export type QuickCompletionParams = {
|
||||
/**
|
||||
* The prompt text to send to the model.
|
||||
*/
|
||||
prompt: string
|
||||
/**
|
||||
* Callback invoked whenever a new chunk of the streaming response arrives.
|
||||
*/
|
||||
onChunk: (chunk: Chunk) => void
|
||||
/**
|
||||
* Optional partial assistant settings to override the default quick assistant.
|
||||
*/
|
||||
assistantUpdate?: Partial<Assistant>
|
||||
/**
|
||||
* Optional additional parameters to pass to the underlying fetchChatCompletion call.
|
||||
* Excludes prompt, messages, assistant, and onChunkReceived which are handled internally.
|
||||
*/
|
||||
params?: Partial<Omit<FetchChatCompletionParams, 'prompt' | 'messages' | 'assistant' | 'onChunkReceived'>>
|
||||
}
|
||||
|
||||
export const useQuickCompletion = () => {
|
||||
const { quickModel } = useDefaultModel()
|
||||
|
||||
const completion = useCallback(
|
||||
async ({ prompt, onChunk, assistantUpdate, params }: QuickCompletionParams) => {
|
||||
const assistant = {
|
||||
...getDefaultAssistant(),
|
||||
model: quickModel,
|
||||
...assistantUpdate
|
||||
} satisfies Assistant
|
||||
return fetchChatCompletion({ prompt, assistant, onChunkReceived: onChunk, ...params })
|
||||
},
|
||||
[quickModel]
|
||||
)
|
||||
|
||||
return completion
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user