use deepgram ws proxy, simplify env vars

This commit is contained in:
Ramnique Singh 2026-03-24 11:50:22 +05:30
parent 8151769891
commit 02c0fd487f
15 changed files with 170 additions and 61 deletions

View file

@ -42,6 +42,8 @@ import { versionHistory, voice } from '@x/core';
import { classifySchedule, processRowboatInstruction } from '@x/core/dist/knowledge/inline_tasks.js';
import { getBillingInfo } from '@x/core/dist/billing/billing.js';
import { summarizeMeeting } from '@x/core/dist/knowledge/summarize_meeting.js';
import { getAccessToken } from '@x/core/dist/auth/tokens.js';
import { getRowboatConfig } from '@x/core/dist/config/rowboat.js';
/**
* Convert markdown to a styled HTML document for PDF/DOCX export.
@ -144,10 +146,10 @@ export function registerIpcHandlers(handlers: InvokeHandlers) {
ipcMain.handle(channel, async (event, rawArgs) => {
// Validate request payload
const args = ipc.validateRequest(channel, rawArgs);
// Call handler
const result = await handler(event, args);
// Validate response payload
return ipc.validateResponse(channel, result);
});
@ -471,6 +473,21 @@ export function setupIpcHandlers() {
const config = await repo.getClientFacingConfig();
return { config };
},
'account:getRowboat': async () => {
const signedIn = await isSignedIn();
if (!signedIn) {
return { signedIn: false, accessToken: null, config: null };
}
const config = await getRowboatConfig();
try {
const accessToken = await getAccessToken();
return { signedIn: true, accessToken, config };
} catch {
return { signedIn: true, accessToken: null, config };
}
},
'granola:getConfig': async () => {
const repo = container.resolve<IGranolaConfigRepo>('granolaConfigRepo');
const config = await repo.getConfig();
@ -719,9 +736,6 @@ export function setupIpcHandlers() {
'voice:synthesize': async (_event, args) => {
return voice.synthesizeSpeech(args.text);
},
'voice:getDeepgramToken': async () => {
return voice.getDeepgramToken();
},
// Billing handler
'billing:getInfo': async () => {
return await getBillingInfo();

View file

@ -75,7 +75,7 @@ function getClientRegistrationRepo(): IClientRegistrationRepo {
* Get or create OAuth configuration for a provider
*/
async function getProviderConfiguration(provider: string, clientIdOverride?: string): Promise<Configuration> {
const config = getProviderConfig(provider);
const config = await getProviderConfig(provider);
const resolveClientId = async (): Promise<string> => {
if (config.client.mode === 'static' && config.client.clientId) {
return config.client.clientId;
@ -156,7 +156,7 @@ export async function connectProvider(provider: string, clientId?: string): Prom
cancelActiveFlow('new_flow_started');
const oauthRepo = getOAuthRepo();
const providerConfig = getProviderConfig(provider);
const providerConfig = await getProviderConfig(provider);
if (provider === 'google') {
if (!clientId) {

View file

@ -1,4 +1,6 @@
import { useCallback, useRef, useState } from 'react';
import { buildDeepgramListenUrl } from '@/lib/deepgram-listen-url';
import { useRowboatAccount } from '@/hooks/useRowboatAccount';
export type MeetingTranscriptionState = 'idle' | 'connecting' | 'recording' | 'stopping';
@ -101,6 +103,7 @@ function formatTranscript(entries: TranscriptEntry[], date: string, calendarEven
// Hook
// ---------------------------------------------------------------------------
export function useMeetingTranscription(onAutoStop?: () => void) {
const { refresh: refreshRowboatAccount } = useRowboatAccount();
const [state, setState] = useState<MeetingTranscriptionState>('idle');
const wsRef = useRef<WebSocket | null>(null);
const micStreamRef = useRef<MediaStream | null>(null);
@ -188,13 +191,18 @@ export function useMeetingTranscription(onAutoStop?: () => void) {
const usingHeadphones = await detectHeadphones();
console.log(`[meeting] Audio output mode: ${usingHeadphones ? 'headphones' : 'speakers'}`);
// Get Deepgram token
// Rowboat WebSocket + bearer token when signed in; else local Deepgram API key
let ws: WebSocket;
try {
const result = await window.ipc.invoke('voice:getDeepgramToken', null);
if (result) {
console.log('[meeting] Using proxy token');
ws = new WebSocket(DEEPGRAM_LISTEN_URL, ['bearer', result.token]);
const account = await refreshRowboatAccount();
if (
account?.signedIn &&
account.accessToken &&
account.config?.websocketApiUrl
) {
const listenUrl = buildDeepgramListenUrl(account.config.websocketApiUrl, DEEPGRAM_PARAMS);
console.log('[meeting] Using Rowboat WebSocket');
ws = new WebSocket(listenUrl, ['bearer', account.accessToken]);
} else {
const config = await window.ipc.invoke('voice:getConfig', null);
if (!config?.deepgram) {
@ -202,11 +210,11 @@ export function useMeetingTranscription(onAutoStop?: () => void) {
setState('idle');
return null;
}
console.log('[meeting] Using API key');
console.log('[meeting] Using Deepgram API key');
ws = new WebSocket(DEEPGRAM_LISTEN_URL, ['token', config.deepgram.apiKey]);
}
} catch (err) {
console.error('[meeting] Failed to get Deepgram token:', err);
console.error('[meeting] Failed to connect Deepgram:', err);
setState('idle');
return null;
}
@ -389,7 +397,7 @@ export function useMeetingTranscription(onAutoStop?: () => void) {
setState('recording');
return notePath;
}, [state, cleanup, scheduleDebouncedWrite]);
}, [state, cleanup, scheduleDebouncedWrite, refreshRowboatAccount]);
const stop = useCallback(async () => {
if (state !== 'recording') return;

View file

@ -0,0 +1,65 @@
import { z } from 'zod';
import { useCallback, useEffect, useState } from 'react';
import { RowboatApiConfig } from '@x/shared/dist/rowboat-account.js';
interface RowboatAccountState {
signedIn: boolean;
accessToken: string | null;
config: z.infer<typeof RowboatApiConfig> | null;
}
export type RowboatAccountSnapshot = RowboatAccountState;
const DEFAULT_STATE: RowboatAccountState = {
signedIn: false,
accessToken: null,
config: null,
};
export function useRowboatAccount() {
const [state, setState] = useState<RowboatAccountState>(DEFAULT_STATE);
const [isLoading, setIsLoading] = useState<boolean>(true);
const refresh = useCallback(async (): Promise<RowboatAccountSnapshot | null> => {
try {
setIsLoading(true);
const result = await window.ipc.invoke('account:getRowboat', null);
const next: RowboatAccountSnapshot = {
signedIn: result.signedIn,
accessToken: result.accessToken,
config: result.config,
};
setState(next);
return next;
} catch (error) {
console.error('Failed to load Rowboat account state:', error);
setState(DEFAULT_STATE);
return null;
} finally {
setIsLoading(false);
}
}, []);
useEffect(() => {
refresh();
}, [refresh]);
useEffect(() => {
const cleanup = window.ipc.on('oauth:didConnect', (event) => {
if (event.provider !== 'rowboat') {
return;
}
refresh();
});
return cleanup;
}, [refresh]);
return {
signedIn: state.signedIn,
accessToken: state.accessToken,
config: state.config,
isLoading,
refresh,
};
}

View file

@ -1,4 +1,6 @@
import { useCallback, useRef, useState } from 'react';
import { buildDeepgramListenUrl } from '@/lib/deepgram-listen-url';
import { useRowboatAccount } from '@/hooks/useRowboatAccount';
export type VoiceState = 'idle' | 'connecting' | 'listening';
@ -15,6 +17,7 @@ const DEEPGRAM_PARAMS = new URLSearchParams({
const DEEPGRAM_LISTEN_URL = `wss://api.deepgram.com/v1/listen?${DEEPGRAM_PARAMS.toString()}`;
export function useVoiceMode() {
const { refresh: refreshRowboatAccount } = useRowboatAccount();
const [state, setState] = useState<VoiceState>('idle');
const [interimText, setInterimText] = useState('');
const wsRef = useRef<WebSocket | null>(null);
@ -25,16 +28,20 @@ export function useVoiceMode() {
const interimRef = useRef('');
// Connect (or reconnect) the Deepgram WebSocket.
// Fetches a fresh token on each connect — temp tokens have short TTL.
// Refreshes Rowboat account before connect so access token is current.
const connectWs = useCallback(async () => {
if (wsRef.current && (wsRef.current.readyState === WebSocket.OPEN || wsRef.current.readyState === WebSocket.CONNECTING)) return;
let ws: WebSocket;
// Try signed-in proxy token first (passed as query param for JWTs)
const result = await window.ipc.invoke('voice:getDeepgramToken', null);
if (result) {
ws = new WebSocket(DEEPGRAM_LISTEN_URL, ['bearer', result.token]);
const account = await refreshRowboatAccount();
if (
account?.signedIn &&
account.accessToken &&
account.config?.websocketApiUrl
) {
const listenUrl = buildDeepgramListenUrl(account.config.websocketApiUrl, DEEPGRAM_PARAMS);
ws = new WebSocket(listenUrl, ['bearer', account.accessToken]);
} else {
// Fall back to local API key (passed as subprotocol)
const config = await window.ipc.invoke('voice:getConfig', null);
@ -72,7 +79,7 @@ export function useVoiceMode() {
console.log('[voice] WebSocket closed');
wsRef.current = null;
};
}, []);
}, [refreshRowboatAccount]);
// Stop audio capture and close WS
const stopAudioCapture = useCallback(() => {

View file

@ -0,0 +1,10 @@
/**
* Merge Deepgram query params onto a Rowboat WebSocket base URL from account config.
*/
export function buildDeepgramListenUrl(baseWsUrl: string, params: URLSearchParams): string {
const url = new URL("/deepgram/v1/listen", baseWsUrl);
for (const [key, value] of params) {
url.searchParams.set(key, value);
}
return url.toString();
}