-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Expand file tree
/
Copy pathuseProviderModelPullReconcile.ts
More file actions
69 lines (63 loc) · 2.54 KB
/
Copy pathuseProviderModelPullReconcile.ts
File metadata and controls
69 lines (63 loc) · 2.54 KB
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
import { useModels } from '@renderer/hooks/useModel'
import { useProvider } from '@renderer/hooks/useProvider'
import { useMoveProviderToFirst } from '@renderer/pages/settings/ProviderSettings/hooks/useMoveProviderToFirst'
import { useProviderPullReconcile as usePullPreview } from '@renderer/pages/settings/ProviderSettings/hooks/useProviderPullReconcile'
import { enableProviderWhenModelsAvailable } from '@renderer/pages/settings/ProviderSettings/utils/providerEnablement'
import { useCallback, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { usePullReconcileSubmit } from './usePullReconcileSubmit'
/**
* Owns the manual pull-preview/apply drawer lifecycle for one provider.
*/
export function useProviderModelPullReconcile(providerId: string) {
const { t } = useTranslation()
const pullPreview = usePullPreview(providerId)
const [pullReconcileDrawerOpen, setPullReconcileDrawerOpen] = useState(false)
const { provider, updateProvider } = useProvider(providerId)
const { models } = useModels({ providerId })
const providerReorder = useMoveProviderToFirst()
const closePullReconcile = useCallback(() => {
setPullReconcileDrawerOpen(false)
}, [])
const { confirmApply, applyBusy } = usePullReconcileSubmit({
providerId,
onApplyCommitted: closePullReconcile
})
const openPullReconcile = useCallback(async () => {
try {
const next = await pullPreview.fetchPreview()
if (next == null) {
return
}
const hasDiff = next.added.length > 0 || next.missing.length > 0
if (!hasDiff) {
// Up to date: no diff to apply, but the existing local models are valid
// for the new key/host — enable the provider if it is currently disabled.
await enableProviderWhenModelsAvailable(
provider,
updateProvider,
providerReorder,
models.length,
'pull_reconcile_up_to_date'
)
window.toast.success(
`${t('settings.models.manage.fetch_up_to_date')} ${t('settings.models.manage.fetch_up_to_date_hint')}`
)
pullPreview.reset()
return
}
setPullReconcileDrawerOpen(true)
} catch {
/* toast + throw inside fetchPreview */
}
}, [models.length, providerReorder, provider, pullPreview, t, updateProvider])
return {
openPullReconcile,
closePullReconcile,
pullReconcileDrawerOpen,
preview: pullPreview.preview,
applyPullReconcile: confirmApply,
isApplyingPullReconcile: applyBusy,
isBusy: pullPreview.isPreviewLoading || applyBusy
}
}