@micdrop/openai 1.7.1 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -16,12 +16,18 @@ type OpenaiOptions = {
16
16
  };
17
17
  declare class OpenaiAgent extends Agent<OpenaiAgentOptions> {
18
18
  private openai;
19
- private abortController?;
20
19
  constructor(options: OpenaiAgentOptions);
21
- protected generateAnswer(stream: PassThrough, stepCount?: number, tryCount?: number): Promise<void>;
20
+ protected generateAnswer(stream: PassThrough, signal: AbortSignal): Promise<void>;
21
+ /**
22
+ * Runs one request, writing its text to the stream and running its tools.
23
+ *
24
+ * It throws the error of the request when it failed before producing
25
+ * anything, which can then be retried, and a {@link StepInterruptedError}
26
+ * once something was written or done.
27
+ */
28
+ private generateStep;
22
29
  private buildInput;
23
30
  private buildTools;
24
- cancel(): void;
25
31
  }
26
32
 
27
33
  /**
@@ -147,13 +153,10 @@ interface OpenaiRealtimeOptions extends RealtimeOptions {
147
153
  maxRetry?: number;
148
154
  }
149
155
  declare class OpenaiRealtime extends Realtime<OpenaiRealtimeOptions> {
150
- private socket?;
151
- private connected;
152
- private everConnected;
153
- private destroyed;
156
+ private socket;
157
+ private connection;
154
158
  private pending;
155
- private reconnectTimeout?;
156
- private retryCount;
159
+ private requestCount;
157
160
  private inputResampler;
158
161
  private outputResampler;
159
162
  private generation;
@@ -174,14 +177,29 @@ declare class OpenaiRealtime extends Realtime<OpenaiRealtimeOptions> {
174
177
  protected cancelAnswer(): void;
175
178
  protected updateTools(): void;
176
179
  protected injectMessage(role: 'user' | 'assistant' | 'system', text: string): void;
180
+ /**
181
+ * Asks for a response, tagged so its `response.created` tells which request
182
+ * it answers. Matching them by order alone would shift every later response
183
+ * onto the wrong request as soon as one is refused.
184
+ */
177
185
  private createResponse;
186
+ /**
187
+ * Sends an event, or holds it until the connection opens.
188
+ * @returns False when it was dropped, the connection being given up on
189
+ */
178
190
  private send;
179
191
  private messageItem;
180
192
  private sendSessionUpdate;
181
- private connect;
182
- private initWS;
183
- private closeSocket;
184
- private reconnect;
193
+ /**
194
+ * Sets the session up, then sends what waited for it. A new connection
195
+ * starts from an empty conversation, so a reconnection sends it again first.
196
+ */
197
+ private onOpen;
198
+ /**
199
+ * Forgets what belonged to the session that is gone: its responses, and the
200
+ * answer in progress, which is lost with it.
201
+ */
202
+ private resetSession;
185
203
  private isIgnored;
186
204
  private handleMessage;
187
205
  private onResponseDone;
@@ -204,24 +222,47 @@ interface OpenaiSTTOptions {
204
222
  }
205
223
  declare class OpenaiSTT extends STT {
206
224
  private options;
207
- private socket?;
208
- private initPromise;
209
- private reconnectTimeout?;
210
- private connectionTimeout?;
211
- private retryCount;
212
- private destroyed;
213
- private transcriptionTimeout?;
214
- private audioChunksPending;
215
- private resampler;
225
+ private socket;
226
+ private utteranceCount;
227
+ private utterances;
228
+ private unacknowledged;
216
229
  constructor(options: OpenaiSTTOptions);
230
+ /**
231
+ * Transcribes one utterance, and emits its transcript once, possibly empty
232
+ * when none comes in time.
233
+ *
234
+ * After the connection was given up on, the next utterance tries it again.
235
+ */
217
236
  transcribe(audioStream: Readable): void;
218
237
  destroy(): void;
219
238
  private sendSessionUpdate;
220
- private initWS;
239
+ /**
240
+ * Configures the session, then sends again what a previous connection did
241
+ * not get transcribed. The OpenAI buffer of a new session is empty, so each
242
+ * utterance is replayed through a fresh resampler, which reproduces the exact
243
+ * same stream, and committed again if it was.
244
+ */
245
+ private onOpen;
246
+ /** Every retry failed: the audio waiting for a transcript is handed back */
247
+ private onFailed;
221
248
  private sendAudioChunk;
222
- private handleMessage;
223
- private reconnect;
224
249
  private commitAudio;
250
+ /**
251
+ * Settles the transcript of an utterance, the only one it gets.
252
+ *
253
+ * The API transcribes each utterance on its own and may finish a short one
254
+ * before a long one spoken earlier, so transcripts go out in the order the
255
+ * utterances were spoken.
256
+ */
257
+ private finish;
258
+ private remove;
259
+ /**
260
+ * The utterance a transcript event is about. The API names the item, and
261
+ * the item of each utterance is known from the acknowledgement of its
262
+ * commit. A transcript of an utterance that timed out matches none.
263
+ */
264
+ private findUtterance;
265
+ private handleMessage;
225
266
  }
226
267
 
227
268
  /**
package/dist/index.d.ts CHANGED
@@ -16,12 +16,18 @@ type OpenaiOptions = {
16
16
  };
17
17
  declare class OpenaiAgent extends Agent<OpenaiAgentOptions> {
18
18
  private openai;
19
- private abortController?;
20
19
  constructor(options: OpenaiAgentOptions);
21
- protected generateAnswer(stream: PassThrough, stepCount?: number, tryCount?: number): Promise<void>;
20
+ protected generateAnswer(stream: PassThrough, signal: AbortSignal): Promise<void>;
21
+ /**
22
+ * Runs one request, writing its text to the stream and running its tools.
23
+ *
24
+ * It throws the error of the request when it failed before producing
25
+ * anything, which can then be retried, and a {@link StepInterruptedError}
26
+ * once something was written or done.
27
+ */
28
+ private generateStep;
22
29
  private buildInput;
23
30
  private buildTools;
24
- cancel(): void;
25
31
  }
26
32
 
27
33
  /**
@@ -147,13 +153,10 @@ interface OpenaiRealtimeOptions extends RealtimeOptions {
147
153
  maxRetry?: number;
148
154
  }
149
155
  declare class OpenaiRealtime extends Realtime<OpenaiRealtimeOptions> {
150
- private socket?;
151
- private connected;
152
- private everConnected;
153
- private destroyed;
156
+ private socket;
157
+ private connection;
154
158
  private pending;
155
- private reconnectTimeout?;
156
- private retryCount;
159
+ private requestCount;
157
160
  private inputResampler;
158
161
  private outputResampler;
159
162
  private generation;
@@ -174,14 +177,29 @@ declare class OpenaiRealtime extends Realtime<OpenaiRealtimeOptions> {
174
177
  protected cancelAnswer(): void;
175
178
  protected updateTools(): void;
176
179
  protected injectMessage(role: 'user' | 'assistant' | 'system', text: string): void;
180
+ /**
181
+ * Asks for a response, tagged so its `response.created` tells which request
182
+ * it answers. Matching them by order alone would shift every later response
183
+ * onto the wrong request as soon as one is refused.
184
+ */
177
185
  private createResponse;
186
+ /**
187
+ * Sends an event, or holds it until the connection opens.
188
+ * @returns False when it was dropped, the connection being given up on
189
+ */
178
190
  private send;
179
191
  private messageItem;
180
192
  private sendSessionUpdate;
181
- private connect;
182
- private initWS;
183
- private closeSocket;
184
- private reconnect;
193
+ /**
194
+ * Sets the session up, then sends what waited for it. A new connection
195
+ * starts from an empty conversation, so a reconnection sends it again first.
196
+ */
197
+ private onOpen;
198
+ /**
199
+ * Forgets what belonged to the session that is gone: its responses, and the
200
+ * answer in progress, which is lost with it.
201
+ */
202
+ private resetSession;
185
203
  private isIgnored;
186
204
  private handleMessage;
187
205
  private onResponseDone;
@@ -204,24 +222,47 @@ interface OpenaiSTTOptions {
204
222
  }
205
223
  declare class OpenaiSTT extends STT {
206
224
  private options;
207
- private socket?;
208
- private initPromise;
209
- private reconnectTimeout?;
210
- private connectionTimeout?;
211
- private retryCount;
212
- private destroyed;
213
- private transcriptionTimeout?;
214
- private audioChunksPending;
215
- private resampler;
225
+ private socket;
226
+ private utteranceCount;
227
+ private utterances;
228
+ private unacknowledged;
216
229
  constructor(options: OpenaiSTTOptions);
230
+ /**
231
+ * Transcribes one utterance, and emits its transcript once, possibly empty
232
+ * when none comes in time.
233
+ *
234
+ * After the connection was given up on, the next utterance tries it again.
235
+ */
217
236
  transcribe(audioStream: Readable): void;
218
237
  destroy(): void;
219
238
  private sendSessionUpdate;
220
- private initWS;
239
+ /**
240
+ * Configures the session, then sends again what a previous connection did
241
+ * not get transcribed. The OpenAI buffer of a new session is empty, so each
242
+ * utterance is replayed through a fresh resampler, which reproduces the exact
243
+ * same stream, and committed again if it was.
244
+ */
245
+ private onOpen;
246
+ /** Every retry failed: the audio waiting for a transcript is handed back */
247
+ private onFailed;
221
248
  private sendAudioChunk;
222
- private handleMessage;
223
- private reconnect;
224
249
  private commitAudio;
250
+ /**
251
+ * Settles the transcript of an utterance, the only one it gets.
252
+ *
253
+ * The API transcribes each utterance on its own and may finish a short one
254
+ * before a long one spoken earlier, so transcripts go out in the order the
255
+ * utterances were spoken.
256
+ */
257
+ private finish;
258
+ private remove;
259
+ /**
260
+ * The utterance a transcript event is about. The API names the item, and
261
+ * the item of each utterance is known from the acknowledgement of its
262
+ * commit. A transcript of an utterance that timed out matches none.
263
+ */
264
+ private findUtterance;
265
+ private handleMessage;
225
266
  }
226
267
 
227
268
  /**