-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathuseZooGatewayRouterModelsSync.ts
More file actions
59 lines (48 loc) · 1.85 KB
/
Copy pathuseZooGatewayRouterModelsSync.ts
File metadata and controls
59 lines (48 loc) · 1.85 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
import { useCallback, useEffect, useRef } from "react"
import { useQueryClient } from "@tanstack/react-query"
import type { ExtensionMessage, RouterModels } from "@roo-code/types"
import { useExtensionState } from "@src/context/ExtensionStateContext"
import { fetchRouterModels } from "./useRouterModels"
/**
* Keeps zoo-gateway models in the shared routerModels query fresh when credentials
* become available (sign-in or profile seeding) without coupling auth to modelCache.
*/
export function useZooGatewayRouterModelsSync() {
const queryClient = useQueryClient()
const { zooCodeIsAuthenticated } = useExtensionState()
const wasAuthenticatedRef = useRef<boolean | undefined>(undefined)
const syncZooGatewayModels = useCallback(async () => {
if (!zooCodeIsAuthenticated) {
return
}
try {
const partial = await fetchRouterModels("zoo-gateway")
const zooModels = partial["zoo-gateway"]
if (!zooModels || Object.keys(zooModels).length === 0) {
return
}
queryClient.setQueryData<RouterModels>(["routerModels", "all"], (current) =>
current ? { ...current, "zoo-gateway": zooModels } : partial,
)
} catch {
// Ignore: bulk router fetch may still be in flight.
}
}, [queryClient, zooCodeIsAuthenticated])
useEffect(() => {
const onMessage = (event: MessageEvent) => {
const message = event.data as ExtensionMessage
if (message.type === "zooGatewayCredentialsReady") {
void syncZooGatewayModels()
}
}
window.addEventListener("message", onMessage)
return () => window.removeEventListener("message", onMessage)
}, [syncZooGatewayModels])
useEffect(() => {
const wasAuthenticated = wasAuthenticatedRef.current
wasAuthenticatedRef.current = zooCodeIsAuthenticated
if (zooCodeIsAuthenticated && wasAuthenticated === false) {
void syncZooGatewayModels()
}
}, [zooCodeIsAuthenticated, syncZooGatewayModels])
}