-
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
bedc491
44494df
a96b7f0
15665c6
a747991
ec54591
8d7cb29
1d9cf18
ffdfa9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/// <reference lib="dom" /> | ||
export { EventEmitter, EventEmitterEvents } from './interface'; | ||
import type { EventEmitterEvents, Timeout, Timer } from './interface'; | ||
|
||
import { EventEmitter as NodeEventEmitter } from 'events'; | ||
import 'event-target-shim'; | ||
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 }; |
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dkundel-openai When I remove the It seems to me like our options are:
Maybe I'm missing something though. Could you let me know which you prefer? |
||
|
||
export const WebSocket = global.WebSocket; | ||
export const isBrowserEnvironment = (): boolean => false; | ||
export const useWebSocketProtocols = true; | ||
|
||
export { | ||
RTCPeerConnection, | ||
RTCIceCandidate, | ||
RTCSessionDescription, | ||
MediaStream, | ||
MediaStreamTrack, | ||
mediaDevices, | ||
}; |
Uh oh!
There was an error while loading. Please reload this page.