Open History to see the last commit
1import type { ByokClient } from '@tanstack/ai-client/byok'
2import type { Handle } from 'remix/ui'
3
4/**
5 * Subscribe to a BYOK snapshot in Remix setup.
6 *
7 * Call this from a component setup function. The returned getter reads the
8 * latest snapshot. When the client changes, the helper calls `handle.update()`
9 * so the component renders again. It unsubscribes when `handle.signal` aborts.
10 *
11 * @param handle Remix setup handle
12 * @param client BYOK keyring
13 */
14export function createByok(
15 handle: Pick<Handle, 'update' | 'signal'>,
16 client: Pick<ByokClient, 'getSnapshot' | 'subscribe'>,
17) {
18 let snapshot = client.getSnapshot()
19 const unsubscribe = client.subscribe(() => {
20 snapshot = client.getSnapshot()
21 void handle.update()
22 })
23 if (handle.signal.aborted) {
24 unsubscribe()
25 } else {
26 handle.signal.addEventListener('abort', unsubscribe, { once: true })
27 }
28 return () => snapshot
29}