better-codex 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/apps/web/src/config.ts
CHANGED
|
@@ -2,4 +2,28 @@ const FALLBACK_HUB_URL = 'http://127.0.0.1:7711'
|
|
|
2
2
|
|
|
3
3
|
export const HUB_URL =
|
|
4
4
|
import.meta.env.VITE_CODEX_HUB_URL ?? FALLBACK_HUB_URL
|
|
5
|
+
|
|
6
|
+
let cachedToken: string | null = null
|
|
7
|
+
|
|
8
|
+
export const getHubToken = async (): Promise<string> => {
|
|
9
|
+
const envToken = import.meta.env.VITE_CODEX_HUB_TOKEN
|
|
10
|
+
if (envToken) {
|
|
11
|
+
return envToken
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (!cachedToken) {
|
|
15
|
+
try {
|
|
16
|
+
const response = await fetch(`${HUB_URL}/config`)
|
|
17
|
+
if (response.ok) {
|
|
18
|
+
const data = (await response.json()) as { token?: string }
|
|
19
|
+
cachedToken = data.token ?? ''
|
|
20
|
+
}
|
|
21
|
+
} catch {
|
|
22
|
+
console.warn('Failed to fetch hub token from backend')
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return cachedToken ?? ''
|
|
27
|
+
}
|
|
28
|
+
|
|
5
29
|
export const HUB_TOKEN = import.meta.env.VITE_CODEX_HUB_TOKEN ?? ''
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getHubToken, HUB_URL } from '../config'
|
|
2
2
|
|
|
3
3
|
export type HubProfile = {
|
|
4
4
|
id: string
|
|
@@ -128,11 +128,12 @@ class HubClient {
|
|
|
128
128
|
if (this.ws?.readyState === WebSocket.OPEN) {
|
|
129
129
|
return
|
|
130
130
|
}
|
|
131
|
-
|
|
132
|
-
|
|
131
|
+
const token = await getHubToken()
|
|
132
|
+
if (!token) {
|
|
133
|
+
throw new Error('Missing hub token - backend may not be running')
|
|
133
134
|
}
|
|
134
135
|
|
|
135
|
-
const ws = new WebSocket(toWsUrl(HUB_URL,
|
|
136
|
+
const ws = new WebSocket(toWsUrl(HUB_URL, token))
|
|
136
137
|
this.ws = ws
|
|
137
138
|
|
|
138
139
|
ws.onmessage = (event) => {
|