Skip to content

Commit 331e6e6

Browse files
extract TokenSource.custom logic into getSandboxTokenSource util
1 parent 50a44b6 commit 331e6e6

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed

components/app/app.tsx

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { ViewController } from '@/components/app/view-controller';
1313
import { Toaster } from '@/components/livekit/toaster';
1414
import { useAgentErrors } from '@/hooks/useAgentErrors';
1515
import { useDebugMode } from '@/hooks/useDebug';
16+
import { getSandboxTokenSource } from '@/lib/utils';
1617

1718
const IN_DEVELOPMENT = process.env.NODE_ENV !== 'production';
1819

@@ -29,34 +30,9 @@ interface AppProps {
2930

3031
export function App({ appConfig }: AppProps) {
3132
const tokenSource = useMemo(() => {
32-
if (typeof process.env.NEXT_PUBLIC_CONN_DETAILS_ENDPOINT === 'string') {
33-
return TokenSource.custom(async () => {
34-
const url = new URL(process.env.NEXT_PUBLIC_CONN_DETAILS_ENDPOINT!, window.location.origin);
35-
36-
try {
37-
const res = await fetch(url.toString(), {
38-
method: 'POST',
39-
headers: {
40-
'Content-Type': 'application/json',
41-
'X-Sandbox-Id': appConfig.sandboxId ?? '',
42-
},
43-
body: JSON.stringify({
44-
room_config: appConfig.agentName
45-
? {
46-
agents: [{ agent_name: appConfig.agentName }],
47-
}
48-
: undefined,
49-
}),
50-
});
51-
return await res.json();
52-
} catch (error) {
53-
console.error('Error fetching connection details:', error);
54-
throw new Error('Error fetching connection details!');
55-
}
56-
});
57-
}
58-
59-
return TokenSource.endpoint('/api/connection-details');
33+
return typeof process.env.NEXT_PUBLIC_CONN_DETAILS_ENDPOINT === 'string'
34+
? getSandboxTokenSource(appConfig)
35+
: TokenSource.endpoint('/api/connection-details');
6036
}, [appConfig]);
6137

6238
const session = useSession(

lib/utils.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { cache } from 'react';
22
import { type ClassValue, clsx } from 'clsx';
3+
import { TokenSource } from 'livekit-client';
34
import { twMerge } from 'tailwind-merge';
45
import { APP_CONFIG_DEFAULTS } from '@/app-config';
56
import type { AppConfig } from '@/app-config';
@@ -86,3 +87,37 @@ export function getStyles(appConfig: AppConfig) {
8687
.filter(Boolean)
8788
.join('\n');
8889
}
90+
91+
/**
92+
* Get a token source for a sandboxed LiveKit session
93+
* @param appConfig - The app configuration
94+
* @returns A token source for a sandboxed LiveKit session
95+
*/
96+
export function getSandboxTokenSource(appConfig: AppConfig) {
97+
return TokenSource.custom(async () => {
98+
const url = new URL(process.env.NEXT_PUBLIC_CONN_DETAILS_ENDPOINT!, window.location.origin);
99+
const sandboxId = appConfig.sandboxId ?? '';
100+
const roomConfig = appConfig.agentName
101+
? {
102+
agents: [{ agent_name: appConfig.agentName }],
103+
}
104+
: undefined;
105+
106+
try {
107+
const res = await fetch(url.toString(), {
108+
method: 'POST',
109+
headers: {
110+
'Content-Type': 'application/json',
111+
'X-Sandbox-Id': sandboxId,
112+
},
113+
body: JSON.stringify({
114+
room_config: roomConfig,
115+
}),
116+
});
117+
return await res.json();
118+
} catch (error) {
119+
console.error('Error fetching connection details:', error);
120+
throw new Error('Error fetching connection details!');
121+
}
122+
});
123+
}

0 commit comments

Comments
 (0)