Skip to content

Commit

Permalink
Merge pull request cloudflare#145 from cloudflare/pass-correlation-id…
Browse files Browse the repository at this point in the history
…-for-openai-calls-session-and-tracks

Provide extra API params and correlation id for openai API calls
  • Loading branch information
third774 authored Dec 20, 2024
2 parents c143f77 + 740d444 commit e356ef3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
2 changes: 2 additions & 0 deletions app/durableObjects/ChatRoom.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ export class ChatRoom extends Server<Env> {
const openAiSession = await CallsNewSession(
this.env.CALLS_APP_ID,
this.env.CALLS_APP_SECRET,
this.env.API_EXTRA_PARAMS,
await this.getMeetingId(),
true
)
const openAiTracksResponse = await openAiSession.NewTracks({
Expand Down
39 changes: 29 additions & 10 deletions app/utils/openai.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,25 @@ export class CallsSession {
sessionId: string
headers: any
endpoint: string
constructor(sessionId: string, headers: any, endpoint: string) {
params: URLSearchParams
constructor(
sessionId: string,
headers: any,
endpoint: string,
apiExtraParams?: string,
meetingId?: string
) {
this.sessionId = sessionId
this.headers = headers
this.endpoint = endpoint
this.params = new URLSearchParams(apiExtraParams)
if (meetingId) {
this.params.set('correlationId', meetingId)
}
}
async NewTracks(body: any): Promise<NewTracksResponse> {
const newTracksURL = new URL(
`${this.endpoint}/sessions/${this.sessionId}/tracks/new?streamDebug&forceTracing=true`
`${this.endpoint}/sessions/${this.sessionId}/tracks/new?${this.params.toString()}`
)
const newTracksResponse = (await fetch(newTracksURL.href, {
method: 'POST',
Expand All @@ -49,7 +60,7 @@ export class CallsSession {
sessionDescription: sdp,
}
const renegotiateURL = new URL(
`${this.endpoint}/sessions/${this.sessionId}/renegotiate?streamDebug&forceTracing=true`
`${this.endpoint}/sessions/${this.sessionId}/renegotiate?${this.params.toString()}`
)
return fetch(renegotiateURL.href, {
method: 'PUT',
Expand All @@ -64,16 +75,20 @@ const baseURL = 'https://rtc.live.cloudflare.com/apps'
export async function CallsNewSession(
appID: string,
appToken: string,
thirdparty: boolean = false
apiExtraParams?: string,
meetingId?: string,
thirdparty = false
): Promise<CallsSession> {
const headers = {
Authorization: `Bearer ${appToken}`,
'Content-Type': 'application/json',
}
const endpoint = `${baseURL}/${appID}`
const newSessionURL = new URL(
`${endpoint}/sessions/new?streamDebug&forceTracing=true`
)
const params = new URLSearchParams(apiExtraParams)
if (meetingId) {
params.set('correlationId', meetingId)
}
const newSessionURL = new URL(`${endpoint}/sessions/new?${params.toString()}`)
if (thirdparty) {
newSessionURL.searchParams.set('thirdparty', 'true')
}
Expand All @@ -89,7 +104,13 @@ export async function CallsNewSession(
})

.then((res) => res.json())) as NewSessionResponse
return new CallsSession(sessionResponse.sessionId, headers, endpoint)
return new CallsSession(
sessionResponse.sessionId,
headers,
endpoint,
apiExtraParams,
meetingId
)
}

export function checkNewTracksResponse(
Expand All @@ -114,10 +135,8 @@ export async function requestOpenAIService(
offer: SessionDescription,
openAiKey: string,
openAiModelEndpoint: string,
// env: Env,
searchParams?: URLSearchParams
): Promise<SessionDescription> {
// const originalRequestURL = new URL(originalRequest.url)
console.log(`Request to: ${openAiModelEndpoint}`)
const endpointURL = new URL(openAiModelEndpoint)
endpointURL.search = searchParams?.toString() ?? ''
Expand Down

0 comments on commit e356ef3

Please sign in to comment.