-
Notifications
You must be signed in to change notification settings - Fork 228
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
Typed events #43
base: main
Are you sure you want to change the base?
Typed events #43
Conversation
@@ -5,7 +5,6 @@ export class RealtimeAPI extends RealtimeEventHandler { | |||
/** | |||
* Create a new RealtimeAPI instance | |||
* @param {{url?: string, apiKey?: string, dangerouslyAllowAPIKeyInBrowser?: boolean, debug?: boolean}} [settings] | |||
* @returns {RealtimeAPI} |
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 returns
should be defined on the constructor
*/ | ||
send(eventName, data) { | ||
if (!this.isConnected()) { | ||
throw new Error(`RealtimeAPI is not connected`); | ||
} | ||
data = data || {}; |
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.
redundant
@@ -59,7 +59,7 @@ import { RealtimeUtils } from './utils.js'; | |||
/** | |||
* @typedef {Object} InputAudioContentType | |||
* @property {"input_audio"} type | |||
* @property {string} [audio] base64-encoded audio data |
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.
later we check if ArrayBuffer | Int16Array, and if so, we convert to base64. Here we ensure the type allows for ArrayBuffer | Int16Array
@@ -594,11 +598,11 @@ export class RealtimeClient extends RealtimeEventHandler { | |||
this.getTurnDetectionType() === null && | |||
this.inputAudioBuffer.byteLength > 0 | |||
) { | |||
this.realtime.send('input_audio_buffer.commit'); |
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.
Didn't find a way yet to skip the second argument if it's nullable
@@ -643,7 +647,6 @@ export class RealtimeClient extends RealtimeEventHandler { | |||
|
|||
/** | |||
* Utility for waiting for the next `conversation.item.appended` event to be triggered by the server | |||
* @returns {Promise<{item: ItemType}>} |
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.
The return type is implicit from this.waitForNext
method
@@ -0,0 +1,367 @@ | |||
import { EventEmitter } from 'events'; |
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.
This file is a draft and needs to be cleaned up
@@ -2,6 +2,7 @@ | |||
"include": ["index.js"], | |||
"compilerOptions": { | |||
"allowJs": true, | |||
"checkJs": true, |
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.
Brings full type-checking for js project
58ca906
to
40f773d
Compare
@@ -275,7 +275,7 @@ export class RealtimeConversation { | |||
* Process an event from the WebSocket server and compose items | |||
* @param {Object} event | |||
* @param {...any} args | |||
* @returns {item: import('./client.js').ItemType | null, delta: ItemContentDeltaType | null} |
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.
This was a jsdoc error I think - we need double braces to define object
I did not check in the dist change, just to keep the draft PR clean. But this branch passes |
This is an experimental approach to get full types to events, both when sending them and receiving them. I import typescript types with jsdoc require from a types.ts file. There might be some benefit to move the original jsdoc definitions to the types.ts files, making them more flexible and easier to read.
When sending events, the typing of the object depends on the name of the event we are sending:
And same when receiving event, the type of the returned object is based on the eventName (first argument)