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

feat(openai): introducing KnownRealtimeResponse #104

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
44 changes: 26 additions & 18 deletions plugins/openai/src/realtime/api_proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// SPDX-License-Identifier: Apache-2.0

import { ExtractStrict } from "../utility-types";

export const SAMPLE_RATE = 24000;
export const NUM_CHANNELS = 1;
export const IN_FRAME_SIZE = 2400; // 100ms
Expand All @@ -18,13 +20,13 @@ export type InputTranscriptionModel = 'whisper-1' | string; // Open-ended, for f
export type Modality = 'text' | 'audio';
export type ToolChoice = 'auto' | 'none' | 'required' | string;
export type State = 'initializing' | 'listening' | 'thinking' | 'speaking' | string;
export type ResponseStatus =
export type KnownResponseStatus =
| 'in_progress'
| 'completed'
| 'incomplete'
| 'cancelled'
| 'failed'
| string;
| 'failed';
export type ResponseStatus = KnownResponseStatus | string;
export type ClientEventType =
| 'session.update'
| 'input_audio_buffer.append'
Expand Down Expand Up @@ -191,22 +193,28 @@ export interface ConversationResource {
object: 'realtime.conversation';
}

export interface IncompleteResponseStatusDetails {
type: ExtractStrict<KnownResponseStatus, 'incomplete'>;
reason: 'max_output_tokens' | 'content_filter' | string;
}

export interface FailedResponseStatusDetails {
type: ExtractStrict<KnownResponseStatus, 'failed'>;
error?: {
code: 'server_error' | 'rate_limit_exceeded' | string;
message: string;
};
}

export interface CancelledResponseStatusDetails {
type: ExtractStrict<KnownResponseStatus, 'cancelled'>;
reason: 'turn_detected' | 'client_cancelled' | string;
}

export type ResponseStatusDetails =
| {
type: 'incomplete';
reason: 'max_output_tokens' | 'content_filter' | string;
}
| {
type: 'failed';
error?: {
code: 'server_error' | 'rate_limit_exceeded' | string;
message: string;
};
}
| {
type: 'cancelled';
reason: 'turn_detected' | 'client_cancelled' | string;
};
| IncompleteResponseStatusDetails
| FailedResponseStatusDetails
| CancelledResponseStatusDetails;

export interface ResponseResource {
id: string;
Expand Down
26 changes: 25 additions & 1 deletion plugins/openai/src/realtime/realtime_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,38 @@ interface ModelOptions {
baseURL: string;
}

export interface RealtimeResponse {
interface RealtimeResponseBase {
id: string;
status: api_proto.ResponseStatus;
statusDetails: api_proto.ResponseStatusDetails | null;
output: RealtimeOutput[];
doneFut: Future;
}

interface KnownRealtimeResponseBase<T extends api_proto.ResponseStatusDetails>
extends RealtimeResponseBase {
status: T['type'];
statusDetails: T;
}

export interface UnknownRealtimeResponse extends RealtimeResponseBase {
status: string;
statusDetails: null;
}

export type IncompleteRealtimeResponse =
KnownRealtimeResponseBase<api_proto.IncompleteResponseStatusDetails>;
export type FailedRealtimeResponse =
KnownRealtimeResponseBase<api_proto.FailedResponseStatusDetails>;
export type CancelledRealtimeResponse =
KnownRealtimeResponseBase<api_proto.CancelledResponseStatusDetails>;
export type KnownRealtimeResponse =
| IncompleteRealtimeResponse
| FailedRealtimeResponse
| CancelledRealtimeResponse;

export type RealtimeResponse = KnownRealtimeResponse | UnknownRealtimeResponse;

export interface RealtimeOutput {
responseId: string;
itemId: string;
Expand Down
1 change: 1 addition & 0 deletions plugins/openai/src/utility-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type ExtractStrict<T, U extends T> = T extends U ? T : never;
Loading