fix: openrouter 缓存 [] 时启动轮询刷新 /model 列表
Browse files
src/components/SearchableModelPicker.tsx
CHANGED
|
@@ -65,6 +65,60 @@ export function SearchableModelPicker({
|
|
| 65 |
}
|
| 66 |
}, [isFastMode, authVersion])
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
// Filter options based on search query
|
| 69 |
const filteredOptions = React.useMemo(() => {
|
| 70 |
if (!Array.isArray(modelOptions)) {
|
|
|
|
| 65 |
}
|
| 66 |
}, [isFastMode, authVersion])
|
| 67 |
|
| 68 |
+
// Poll for OpenRouter models when cache is empty
|
| 69 |
+
React.useEffect(() => {
|
| 70 |
+
let mounted = true
|
| 71 |
+
let pollTimer: NodeJS.Timeout | null = null
|
| 72 |
+
|
| 73 |
+
async function checkOpenRouterCache() {
|
| 74 |
+
try {
|
| 75 |
+
const { getAPIProvider } = await import('../utils/model/providers.js')
|
| 76 |
+
const provider = getAPIProvider()
|
| 77 |
+
|
| 78 |
+
if (provider === 'openrouter' && modelOptions.length === 0) {
|
| 79 |
+
// Check cache every 500ms
|
| 80 |
+
pollTimer = setInterval(async () => {
|
| 81 |
+
if (!mounted) {
|
| 82 |
+
if (pollTimer) clearInterval(pollTimer)
|
| 83 |
+
return
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
try {
|
| 87 |
+
const { hasOpenRouterModelsCache } = await import('../utils/model/openRouterModels.js')
|
| 88 |
+
if (hasOpenRouterModelsCache()) {
|
| 89 |
+
// Cache is now populated, trigger re-render
|
| 90 |
+
if (mounted) {
|
| 91 |
+
setAppState(prev => ({ ...prev, authVersion: prev.authVersion + 1 }))
|
| 92 |
+
}
|
| 93 |
+
if (pollTimer) clearInterval(pollTimer)
|
| 94 |
+
}
|
| 95 |
+
} catch (error) {
|
| 96 |
+
console.error('Error checking OpenRouter cache:', error)
|
| 97 |
+
if (pollTimer) clearInterval(pollTimer)
|
| 98 |
+
}
|
| 99 |
+
}, 500)
|
| 100 |
+
|
| 101 |
+
// Stop polling after 10 seconds
|
| 102 |
+
setTimeout(() => {
|
| 103 |
+
if (pollTimer && mounted) {
|
| 104 |
+
clearInterval(pollTimer)
|
| 105 |
+
pollTimer = null
|
| 106 |
+
}
|
| 107 |
+
}, 10000)
|
| 108 |
+
}
|
| 109 |
+
} catch (error) {
|
| 110 |
+
console.error('Error checking API provider:', error)
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
checkOpenRouterCache()
|
| 115 |
+
|
| 116 |
+
return () => {
|
| 117 |
+
mounted = false
|
| 118 |
+
if (pollTimer) clearInterval(pollTimer)
|
| 119 |
+
}
|
| 120 |
+
}, [modelOptions.length, setAppState])
|
| 121 |
+
|
| 122 |
// Filter options based on search query
|
| 123 |
const filteredOptions = React.useMemo(() => {
|
| 124 |
if (!Array.isArray(modelOptions)) {
|