Skip to content
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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Typed events #43

wants to merge 3 commits into from

Conversation

ZJONSSON
Copy link

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:
image

And same when receiving event, the type of the returned object is based on the eventName (first argument)
image

image

@@ -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}
Copy link
Author

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 || {};
Copy link
Author

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
Copy link
Author

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');
Copy link
Author

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}>}
Copy link
Author

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';
Copy link
Author

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,
Copy link
Author

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

@@ -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}
Copy link
Author

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

@ZJONSSON
Copy link
Author

I did not check in the dist change, just to keep the draft PR clean. But this branch passes tsc and npm run test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant