Skip to content

Typed events #43

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/conversation.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import { RealtimeUtils } from './utils.js';
export class RealtimeConversation {
defaultFrequency = 24_000; // 24,000 Hz

EventProcessors = {
/** @type { import('./types').EventProcessors} */
eventProcessors = {
'conversation.item.created': (event) => {
const { item } = event;
// deep copy values
Expand Down Expand Up @@ -240,7 +241,6 @@ export class RealtimeConversation {

/**
* Create a new RealtimeConversation instance
* @returns {RealtimeConversation}
*/
constructor() {
this.clear();
Expand Down Expand Up @@ -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

* @returns {{item: import('./client.js').ItemType | null, delta: ItemContentDeltaType | null}}
*/
processEvent(event, ...args) {
if (!event.event_id) {
Expand All @@ -286,7 +286,7 @@ export class RealtimeConversation {
console.error(event);
throw new Error(`Missing "type" on event`);
}
const eventProcessor = this.EventProcessors[event.type];
const eventProcessor = this.eventProcessors[event.type];
if (!eventProcessor) {
throw new Error(
`Missing conversation event processor for "${event.type}"`,
Expand Down
6 changes: 6 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ export interface ServerEvent {
'input_audio_buffer.speech_started': {
event_id: string;
type: 'input_audio_buffer.speech_started';
item_id: string;
audio_start_ms: number;
}
'input_audio_buffer.speech_stopped': {
event_id: string;
Expand Down Expand Up @@ -306,6 +308,7 @@ export interface ServerEvent {
output_index: number;
call_id: string;
arguments: string;
delta: string;
}
'response.function_call_arguments.done': {
event_id: string;
Expand Down Expand Up @@ -365,3 +368,6 @@ export type Listener = <K extends keyof AllMap>(event: K, listener: (data: AllMa
export type ListenerBool = <K extends keyof AllMap>(event: K, listener: (data: AllMap[K]) => void) => boolean;
export type WaitForNext = <K extends keyof AllMap>(event: K, timeout?: number) => Promise<AllMap[K] | null>;
export type EventNames = keyof AllMap;
export type EventProcessors = {
[K in keyof AllMap]?: (data: AllMap[K]) => void;
};