11import type { OpenAI } from '../..' ;
22import { OpenAIError } from '../../core/error' ;
3+ import { buildHeaders } from '../../internal/headers' ;
4+ import type { RequestOptions } from '../../internal/request-options' ;
35import type {
46 ChatCompletion ,
57 ChatCompletionCreateParams ,
@@ -42,6 +44,7 @@ export class BetaToolRunner<Stream extends boolean> {
4244 #mutated = false ;
4345 /** Current state containing the request parameters */
4446 #state: { params : BetaToolRunnerParams } ;
47+ #options: BetaToolRunnerRequestOptions ;
4548 /** Promise for the last message received from the assistant */
4649 #message?: Promise < ChatCompletion > | undefined ;
4750 /** Cached tool response to avoid redundant executions */
@@ -58,6 +61,7 @@ export class BetaToolRunner<Stream extends boolean> {
5861 constructor (
5962 private client : OpenAI ,
6063 params : BetaToolRunnerParams ,
64+ options ?: BetaToolRunnerRequestOptions ,
6165 ) {
6266 this . #state = {
6367 params : {
@@ -69,6 +73,10 @@ export class BetaToolRunner<Stream extends boolean> {
6973 } ,
7074 } ;
7175
76+ this . #options = {
77+ ...options ,
78+ headers : buildHeaders ( [ { 'x-stainless-helper' : 'BetaToolRunner' } , options ?. headers ] ) ,
79+ } ;
7280 this . #completion = promiseWithResolvers ( ) ;
7381 }
7482
@@ -104,19 +112,22 @@ export class BetaToolRunner<Stream extends boolean> {
104112
105113 const { ...params } = this . #state. params ;
106114 if ( params . stream ) {
107- stream = this . client . beta . chat . completions . stream ( { ...params , stream : true } ) ;
115+ stream = this . client . beta . chat . completions . stream ( { ...params , stream : true } , this . #options ) ;
108116 this . #message = stream . finalMessage ( ) ;
109117 // Make sure that this promise doesn't throw before we get the option to do something about it.
110118 // Error will be caught when we call await this.#message ultimately
111119 this . #message?. catch ( ( ) => { } ) ;
112120 yield stream as any ;
113121 } else {
114- this . #message = this . client . beta . chat . completions . create ( {
115- stream : false ,
116- tools : params . tools ,
117- messages : params . messages ,
118- model : params . model ,
119- } ) ;
122+ this . #message = this . client . beta . chat . completions . create (
123+ {
124+ stream : false ,
125+ tools : params . tools ,
126+ messages : params . messages ,
127+ model : params . model ,
128+ } ,
129+ this . #options,
130+ ) ;
120131 yield this . #message as any ;
121132 }
122133
@@ -428,3 +439,5 @@ export type BetaToolRunnerParams = Simplify<
428439 max_iterations ?: number ;
429440 }
430441> ;
442+
443+ export type BetaToolRunnerRequestOptions = Pick < RequestOptions , 'headers' > ;
0 commit comments