forked from cloudflare/orange
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request cloudflare#142 from cloudflare/openai
Add OpenAI integration ✨
- Loading branch information
Showing
21 changed files
with
790 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { useRoomContext } from '~/hooks/useRoomContext' | ||
import type { ClientMessage, User } from '~/types/Messages' | ||
import { AiPushToTalkButtion } from './AiPushToTalkButton' | ||
import { Button } from './Button' | ||
import { Trigger } from './Dialog' | ||
import { InviteAiDialog } from './InviteAiDialog' | ||
import { RecordAiVoiceActivity } from './RecordAiVoiceActivity' | ||
|
||
function RemoveAiButton() { | ||
const { | ||
room: { websocket }, | ||
} = useRoomContext() | ||
return ( | ||
<Button | ||
onClick={() => | ||
websocket.send( | ||
JSON.stringify({ type: 'disableAi' } satisfies ClientMessage) | ||
) | ||
} | ||
className="text-xs" | ||
displayType="secondary" | ||
> | ||
Remove AI | ||
</Button> | ||
) | ||
} | ||
|
||
export function AiButton(props: { recordActivity: (user: User) => void }) { | ||
const { | ||
room: { | ||
roomState: { | ||
ai: { connectionPending, error }, | ||
users, | ||
}, | ||
}, | ||
} = useRoomContext() | ||
|
||
const aiUser = users.find((u) => u.id === 'ai') | ||
|
||
return ( | ||
<> | ||
{error && <span className="text-red-800 dark:text-red-500">{error}</span>} | ||
{aiUser ? ( | ||
<> | ||
<RemoveAiButton /> | ||
<AiPushToTalkButtion /> | ||
<RecordAiVoiceActivity | ||
user={aiUser} | ||
recordActivity={props.recordActivity} | ||
/> | ||
</> | ||
) : ( | ||
<InviteAiDialog> | ||
<Trigger asChild> | ||
<Button | ||
className="text-xs flex items-center gap-2" | ||
disabled={connectionPending} | ||
> | ||
<span>Invite AI</span> | ||
</Button> | ||
</Trigger> | ||
</InviteAiDialog> | ||
)} | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { useEffect, useMemo, useRef, useState } from 'react' | ||
import { switchMap } from 'rxjs' | ||
import { useStateObservable, useSubscribedState } from '~/hooks/rxjsHooks' | ||
import { useRoomContext } from '~/hooks/useRoomContext' | ||
import type { ClientMessage } from '~/types/Messages' | ||
import { playSound } from '~/utils/playSound' | ||
import { inaudibleAudioTrack$ } from '~/utils/rxjs/inaudibleAudioTrack$' | ||
import { Button } from './Button' | ||
|
||
function useButtonIsHeldDown({ | ||
key, | ||
disabled, | ||
}: { | ||
key: string | ||
disabled: boolean | ||
}) { | ||
const [keyIsHeldDown, setKeyIsHeldDown] = useState(false) | ||
const buttonRef = useRef<HTMLButtonElement>(null) | ||
|
||
useEffect(() => { | ||
const button = buttonRef.current | ||
let timeout = -1 | ||
const setTrue = () => { | ||
if (!disabled) { | ||
setKeyIsHeldDown(true) | ||
clearTimeout(timeout) | ||
} | ||
} | ||
const setFalse = () => { | ||
timeout = window.setTimeout(() => { | ||
setKeyIsHeldDown(false) | ||
}, 200) | ||
} | ||
|
||
const onKeyDown = (e: KeyboardEvent) => { | ||
if (e.key.toLowerCase() === key.toLowerCase()) { | ||
setTrue() | ||
} | ||
} | ||
|
||
const onKeyUp = (e: KeyboardEvent) => { | ||
if (e.key.toLowerCase() === key.toLowerCase()) { | ||
setFalse() | ||
} | ||
} | ||
|
||
document.addEventListener('keydown', onKeyDown) | ||
document.addEventListener('keyup', onKeyUp) | ||
document.addEventListener('blur', setFalse) | ||
button?.addEventListener('pointerdown', setTrue) | ||
button?.addEventListener('pointerup', setFalse) | ||
|
||
return () => { | ||
clearTimeout(timeout) | ||
document.removeEventListener('keydown', onKeyDown) | ||
document.removeEventListener('keyup', onKeyUp) | ||
document.removeEventListener('blur', setFalse) | ||
button?.removeEventListener('pointerdown', setTrue) | ||
button?.removeEventListener('pointerup', setFalse) | ||
} | ||
}, [disabled, key]) | ||
|
||
return [keyIsHeldDown, buttonRef] as const | ||
} | ||
|
||
export function AiPushToTalkButtion() { | ||
const { | ||
peer, | ||
room: { | ||
websocket, | ||
roomState: { | ||
ai: { controllingUser }, | ||
}, | ||
}, | ||
userMedia: { turnMicOn, publicAudioTrack$ }, | ||
} = useRoomContext() | ||
const hasControl = controllingUser === websocket.id | ||
const disabled = !hasControl && controllingUser !== undefined | ||
const [holdingTalkButton, talkButtonRef] = useButtonIsHeldDown({ | ||
key: 'a', | ||
disabled, | ||
}) | ||
|
||
const holdingTalkButton$ = useStateObservable(holdingTalkButton) | ||
const audioTrack$ = useMemo( | ||
() => | ||
holdingTalkButton$.pipe( | ||
switchMap((talking) => | ||
talking ? publicAudioTrack$ : inaudibleAudioTrack$ | ||
) | ||
), | ||
[holdingTalkButton$, publicAudioTrack$] | ||
) | ||
|
||
const pushedAiAudioTrack$ = useMemo( | ||
() => peer.pushTrack(audioTrack$), | ||
[audioTrack$, peer] | ||
) | ||
|
||
const pushedAiAudioTrack = useSubscribedState(pushedAiAudioTrack$) | ||
|
||
useEffect(() => { | ||
if (holdingTalkButton && pushedAiAudioTrack) { | ||
turnMicOn() | ||
console.log('🤖 Requesting ai control') | ||
websocket.send( | ||
JSON.stringify({ | ||
type: 'requestAiControl', | ||
track: pushedAiAudioTrack, | ||
} satisfies ClientMessage) | ||
) | ||
} else { | ||
console.log('🤖 Relinquishing ai control!') | ||
websocket.send( | ||
JSON.stringify({ | ||
type: 'relenquishAiControl', | ||
} satisfies ClientMessage) | ||
) | ||
} | ||
}, [holdingTalkButton, pushedAiAudioTrack, turnMicOn, websocket]) | ||
|
||
useEffect(() => { | ||
if (controllingUser !== undefined) { | ||
playSound('aiReady') | ||
} | ||
}, [controllingUser]) | ||
|
||
return ( | ||
<Button | ||
className="text-xs select-none" | ||
disabled={disabled} | ||
ref={talkButtonRef} | ||
> | ||
{hasControl ? 'Speaking to Ai...' : 'Hold to talk to AI'} | ||
</Button> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { useSearchParams } from '@remix-run/react' | ||
import { useState, type ReactNode } from 'react' | ||
import { useRoomContext } from '~/hooks/useRoomContext' | ||
import type { ClientMessage } from '~/types/Messages' | ||
import { Button } from './Button' | ||
import { Dialog, DialogContent, DialogOverlay, Portal } from './Dialog' | ||
|
||
export function InviteAiDialog(props: { children?: ReactNode }) { | ||
const [open, setOpen] = useState(false) | ||
|
||
const { | ||
room: { websocket }, | ||
} = useRoomContext() | ||
|
||
const [params] = useSearchParams() | ||
|
||
const instructions = params.get('instructions') | ||
const voice = params.get('voice') | ||
|
||
return ( | ||
<Dialog open={open} onOpenChange={setOpen}> | ||
{props.children} | ||
<Portal> | ||
<DialogOverlay /> | ||
<DialogContent> | ||
<form | ||
className="flex flex-col gap-4 mt-8" | ||
onSubmit={(e) => { | ||
e.preventDefault() | ||
websocket.send( | ||
JSON.stringify({ | ||
type: 'enableAi', | ||
...Object.fromEntries(new FormData(e.currentTarget)), | ||
} satisfies ClientMessage) | ||
) | ||
setOpen(false) | ||
}} | ||
> | ||
<div className="flex flex-col gap-2"> | ||
<div> | ||
<label className="font-medium" htmlFor="instructions"> | ||
Instructions | ||
</label> | ||
</div> | ||
|
||
<div> | ||
<textarea | ||
className="bg-gray-100 dark:bg-zinc-800 w-full" | ||
id="instructions" | ||
name="instructions" | ||
rows={15} | ||
defaultValue={ | ||
instructions ?? | ||
`You are a helpful and concise AI assistant for a video chat application called Orange Meets.` | ||
} | ||
/> | ||
</div> | ||
</div> | ||
<div className="flex flex-col gap-2"> | ||
<div> | ||
<label className="font-medium" htmlFor="voice"> | ||
Voice | ||
</label> | ||
</div> | ||
|
||
<div> | ||
<select | ||
className="bg-gray-100 dark:bg-zinc-800 w-full" | ||
id="voice" | ||
name="voice" | ||
defaultValue={voice ?? 'ash'} | ||
> | ||
<option value="ash">Ash</option> | ||
<option value="ballad">Ballad</option> | ||
</select> | ||
</div> | ||
</div> | ||
|
||
<Button type="submit" className="self-end text-xs"> | ||
Invite AI | ||
</Button> | ||
</form> | ||
</DialogContent> | ||
</Portal> | ||
</Dialog> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.