Skip to content

Commit

Permalink
feat(streaming): add .request_id getter
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertCraigie authored and stainless-app[bot] committed Jan 21, 2025
1 parent 7a68f81 commit e1fd091
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/lib/BetaMessageStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export class BetaMessageStream implements AsyncIterable<BetaMessageStreamEvent>
#errored = false;
#aborted = false;
#catchingPromiseCreated = false;
#request_id: string | null | undefined;

constructor() {
this.#connectedPromise = new Promise<Response | null>((resolve, reject) => {
Expand All @@ -75,6 +76,10 @@ export class BetaMessageStream implements AsyncIterable<BetaMessageStreamEvent>
this.#endPromise.catch(() => {});
}

get request_id(): string | null | undefined {
return this.#request_id;
}

/**
* Returns the `MessageStream` data, the raw `Response` instance and the ID of the request,
* returned vie the `request-id` header which is useful for debugging requests and resporting
Expand Down Expand Up @@ -178,6 +183,7 @@ export class BetaMessageStream implements AsyncIterable<BetaMessageStreamEvent>

protected _connected(response: Response | null) {
if (this.ended) return;
this.#request_id = response?.headers.get('request-id');
this.#resolveConnectedPromise(response);
this._emit('connect');
}
Expand Down
6 changes: 6 additions & 0 deletions src/lib/MessageStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {
#errored = false;
#aborted = false;
#catchingPromiseCreated = false;
#request_id: string | null | undefined;

constructor() {
this.#connectedPromise = new Promise<Response | null>((resolve, reject) => {
Expand All @@ -75,6 +76,10 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {
this.#endPromise.catch(() => {});
}

get request_id(): string | null | undefined {
return this.#request_id;
}

/**
* Returns the `MessageStream` data, the raw `Response` instance and the ID of the request,
* returned vie the `request-id` header which is useful for debugging requests and resporting
Expand Down Expand Up @@ -178,6 +183,7 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {

protected _connected(response: Response | null) {
if (this.ended) return;
this.#request_id = response?.headers.get('request-id');
this.#resolveConnectedPromise(response);
this._emit('connect');
}
Expand Down

0 comments on commit e1fd091

Please sign in to comment.