2026-04-19 22:24:28 +08:00
|
|
|
// 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
|
2026-04-20 18:08:05 +08:00
|
|
|
console.log('[voice] fetching token for session:', sessionId, 'token prefix:', token?.slice(0, 20))
|
2026-04-19 22:24:28 +08:00
|
|
|
|
|
|
|
|
const res = await fetch(`/voice-api/voice-token/${sessionId}`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
Authorization: token,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
2026-04-20 18:08:05 +08:00
|
|
|
console.log('[voice] token service response status:', res.status)
|
2026-04-19 22:24:28 +08:00
|
|
|
if (!res.ok) {
|
|
|
|
|
const data = await res.json().catch(() => ({ error: '语音服务暂不可用' }))
|
2026-04-20 18:08:05 +08:00
|
|
|
console.log('[voice] token service error:', data)
|
|
|
|
|
throw new Error(data.error || data.detail || '语音服务暂不可用')
|
2026-04-19 22:24:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const data = await res.json()
|
|
|
|
|
return data.token
|
|
|
|
|
}
|