Files
gamegroup2/frontend/src/api/voice.ts
T

35 lines
1.0 KiB
TypeScript
Raw Normal View History

// src/api/voice.ts
import { pb } from './pocketbase'
const LIVEKIT_URL = import.meta.env.VITE_LIVEKIT_URL || 'ws://192.168.1.14:7880'
export function getLiveKitUrl(): string {
return LIVEKIT_URL
}
export async function fetchVoiceToken(sessionId: string): Promise<string> {
const user = pb.authStore.model
if (!user) throw new Error('未登录')
const token = pb.authStore.token
console.log('[voice] fetching token for session:', sessionId, 'token prefix:', token?.slice(0, 20))
const res = await fetch(`/voice-api/voice-token/${sessionId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: token,
},
})
console.log('[voice] token service response status:', res.status)
if (!res.ok) {
const data = await res.json().catch(() => ({ error: '语音服务暂不可用' }))
console.log('[voice] token service error:', data)
throw new Error(data.error || data.detail || '语音服务暂不可用')
}
const data = await res.json()
return data.token
}