-
Notifications
You must be signed in to change notification settings - Fork 218
Fix #133 Add React Native Support #193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
monadoid
wants to merge
9
commits into
openai:main
Choose a base branch
from
monadoid:add-react-native-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,189
−186
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bedc491
feat(agents-realtime): add React Native WebRTC support via shims
monadoid 44494df
feat(agents-core): add React Native support with platform-specific shims
monadoid a96b7f0
chore: Added changeset and updated readme files.
monadoid 15665c6
Merge remote-tracking branch 'upstream/main' into add-react-native-su…
monadoid a747991
Removed event target shim for react native
monadoid ec54591
Merge upstream main into add-react-native-support to resolve conflicts
monadoid 8d7cb29
Removed event-target-shim dependency
monadoid 1d9cf18
Update pnpm-lock.yaml for CI
monadoid ffdfa9c
Merge branch 'main' into add-react-native-support
monadoid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,6 @@ | ||
--- | ||
'@openai/agents-realtime': minor | ||
'@openai/agents-core': minor | ||
--- | ||
|
||
Add React Native platform support |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -69,6 +69,11 @@ | |
"types": "./dist/shims/shims-node.d.ts", | ||
"default": "./dist/shims/shims-node.mjs" | ||
}, | ||
"react-native": { | ||
"require": "./dist/shims/shims-react-native.js", | ||
"types": "./dist/shims/shims-react-native.d.ts", | ||
"default": "./dist/shims/shims-react-native.mjs" | ||
}, | ||
"require": { | ||
"types": "./dist/shims/shims-node.d.ts", | ||
"default": "./dist/shims/shims-node.js" | ||
|
@@ -89,8 +94,11 @@ | |
}, | ||
"dependencies": { | ||
"@openai/zod": "npm:[email protected] - 3.25.67", | ||
"@ungap/structured-clone": "^1.3.0", | ||
"debug": "^4.4.0", | ||
"openai": "^5.10.1" | ||
"openai": "^5.10.1", | ||
"events": "^3.3.0", | ||
"react-native-uuid": "^2.0.3" | ||
}, | ||
"peerDependencies": { | ||
"zod": "3.25.40 - 3.25.67" | ||
|
@@ -121,7 +129,8 @@ | |
}, | ||
"devDependencies": { | ||
"@types/debug": "^4.1.12", | ||
"zod": "3.25.40 - 3.25.67" | ||
"zod": "3.25.40 - 3.25.67", | ||
"@types/ungap__structured-clone": "^1.2.0" | ||
}, | ||
"files": [ | ||
"dist" | ||
|
This file contains hidden or 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,104 @@ | ||
/// <reference lib="dom" /> | ||
export { EventEmitter, EventEmitterEvents } from './interface'; | ||
import type { EventEmitterEvents, Timeout, Timer } from './interface'; | ||
|
||
import { EventEmitter as NodeEventEmitter } from 'events'; | ||
import structuredClone from '@ungap/structured-clone'; | ||
import uuid from 'react-native-uuid'; | ||
|
||
if (!('structuredClone' in globalThis)) { | ||
// @ts-expect-error - This is the recommended approach from ungap/structured-clone | ||
globalThis.structuredClone = structuredClone; | ||
} | ||
export const randomUUID = (): string => uuid.v4(); | ||
|
||
export function loadEnv(): Record<string, string | undefined> { | ||
return {}; | ||
} | ||
|
||
export class ReactNativeEventEmitter< | ||
Events extends EventEmitterEvents = Record<string, any[]>, | ||
> extends NodeEventEmitter { | ||
override on<K extends keyof Events & (string | symbol)>( | ||
type: K, | ||
listener: (...args: Events[K]) => void, | ||
): this { | ||
// Node's typings accept string | symbol; cast is safe. | ||
return super.on(type as string | symbol, listener); | ||
} | ||
|
||
override off<K extends keyof Events & (string | symbol)>( | ||
type: K, | ||
listener: (...args: Events[K]) => void, | ||
): this { | ||
return super.off(type as string | symbol, listener); | ||
} | ||
|
||
override emit<K extends keyof Events & (string | symbol)>( | ||
type: K, | ||
...args: Events[K] | ||
): boolean { | ||
return super.emit(type as string | symbol, ...args); | ||
} | ||
|
||
override once<K extends keyof Events & (string | symbol)>( | ||
type: K, | ||
listener: (...args: Events[K]) => void, | ||
): this { | ||
return super.once(type as string | symbol, listener); | ||
} | ||
} | ||
|
||
export { ReactNativeEventEmitter as RuntimeEventEmitter }; | ||
|
||
// Streams – placeholders (unused by the SDK on RN) | ||
export const Readable = class {}; | ||
export const ReadableStream = globalThis.ReadableStream; | ||
export const ReadableStreamController = | ||
globalThis.ReadableStreamDefaultController; | ||
export const TransformStream = globalThis.TransformStream; | ||
|
||
export class AsyncLocalStorage { | ||
#ctx: unknown = null; | ||
|
||
run<T>(store: T, fn: () => unknown) { | ||
this.#ctx = store; | ||
return fn(); | ||
} | ||
getStore<T>() { | ||
return this.#ctx as T; | ||
} | ||
enterWith<T>(store: T) { | ||
this.#ctx = store; | ||
} | ||
} | ||
|
||
export function isBrowserEnvironment(): boolean { | ||
return true; | ||
} | ||
|
||
export function isTracingLoopRunningByDefault(): boolean { | ||
return false; | ||
} | ||
|
||
/* MCP not supported on mobile; export browser stubs */ | ||
export { MCPServerStdio, MCPServerStreamableHttp } from './mcp-server/browser'; | ||
|
||
class RNTimer implements Timer { | ||
setTimeout(cb: () => void, ms: number): Timeout { | ||
const id: any = setTimeout(cb, ms); | ||
// RN timers don’t expose ref/unref; shim them | ||
id.ref ??= () => id; | ||
id.unref ??= () => id; | ||
id.hasRef ??= () => true; | ||
id.refresh ??= () => id; | ||
return id; | ||
} | ||
|
||
clearTimeout(id: Timeout | string | number | undefined) { | ||
clearTimeout(id as number); | ||
} | ||
} | ||
|
||
const timer = new RNTimer(); | ||
export { timer }; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,24 @@ | ||
import { | ||
RTCPeerConnection, | ||
RTCIceCandidate, | ||
RTCSessionDescription, | ||
MediaStream, | ||
MediaStreamTrack, | ||
mediaDevices, | ||
registerGlobals, | ||
} from 'react-native-webrtc'; | ||
|
||
registerGlobals(); | ||
|
||
export const WebSocket = global.WebSocket; | ||
export const isBrowserEnvironment = (): boolean => false; | ||
export const useWebSocketProtocols = true; | ||
|
||
export { | ||
RTCPeerConnection, | ||
RTCIceCandidate, | ||
RTCSessionDescription, | ||
MediaStream, | ||
MediaStreamTrack, | ||
mediaDevices, | ||
}; |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we are using the globals which is good so I would say let's drop the
registerGlobals()
that way it's still up to the implementor if they want to register them for additional use cases rather than us magically importing themThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dkundel-openai When I remove the
registerGlobals()
line and test this in expo go on my android, neither the mic nor the speaker work. It does work if I then callregisterGlobals()
within my expo code - I believe this is because there are other places in theagents-realtime
package where the globals are called.It seems to me like our options are:
registerGlobals()
here, and then the user must callregisterGlobals()
within their RN app to get the mic/speaker working.registerGlobals()
here, but make it so that theagents-realtime
package doesn't use the globals anywhere, and have all of the shims export more stuff.registerGlobals()
here, and then the user can optionally callregisterGlobals()
within their RN app if they want to change anything.Maybe I'm missing something though. Could you let me know which you prefer?