@@ -5,7 +5,7 @@ import { CognitiveRequest, CognitiveResponse, CognitiveStreamChunk, Model } from
55export { CognitiveRequest , CognitiveResponse , CognitiveStreamChunk }
66
77type ClientProps = {
8- baseUrl ?: string
8+ apiUrl ?: string
99 timeout ?: number
1010 botId ?: string
1111 token ?: string
@@ -22,49 +22,52 @@ const isBrowser = () => typeof window !== 'undefined' && typeof window.fetch ===
2222
2323export class CognitiveBeta {
2424 private _axiosClient : AxiosInstance
25- private readonly _config : Required < ClientProps >
25+ private readonly _apiUrl : string
26+ private readonly _timeout : number
27+ private readonly _withCredentials : boolean
28+ private readonly _headers : Record < string , string >
2629
2730 public constructor ( props : ClientProps ) {
28- this . _config = {
29- baseUrl : props . baseUrl || 'https://cognitive.botpress.cloud' ,
30- timeout : props . timeout || 60_001 ,
31- token : props . token || '' ,
32- botId : props . botId || '' ,
33- withCredentials : props . withCredentials || false ,
34- headers : props . headers || { } ,
31+ this . _apiUrl = props . apiUrl || 'https://api.botpress.cloud'
32+ this . _timeout = props . timeout || 60_001
33+ this . _withCredentials = props . withCredentials || false
34+ this . _headers = { ...props . headers }
35+
36+ if ( props . botId ) {
37+ this . _headers [ 'X-Bot-Id' ] = props . botId
38+ }
39+
40+ if ( props . token ) {
41+ this . _headers [ 'Authorization' ] = `Bearer ${ props . token } `
3542 }
3643
3744 this . _axiosClient = axios . create ( {
38- headers : {
39- Authorization : `Bearer ${ this . _config . token } ` ,
40- 'X-Bot-Id' : this . _config . botId ,
41- ...this . _config . headers ,
42- } ,
43- withCredentials : this . _config . withCredentials ,
44- baseURL : this . _config . baseUrl ,
45+ headers : this . _headers ,
46+ withCredentials : this . _withCredentials ,
47+ baseURL : this . _apiUrl ,
4548 } )
4649 }
4750
4851 public async generateText ( input : CognitiveRequest , options : RequestOptions = { } ) {
49- const signal = options . signal ?? AbortSignal . timeout ( this . _config . timeout )
52+ const signal = options . signal ?? AbortSignal . timeout ( this . _timeout )
5053
5154 const { data } = await this . _withServerRetry ( ( ) =>
52- this . _axiosClient . post < CognitiveResponse > ( '/v1 /generate-text' , input , {
55+ this . _axiosClient . post < CognitiveResponse > ( '/v2/cognitive /generate-text' , input , {
5356 signal,
54- timeout : options . timeout ?? this . _config . timeout ,
57+ timeout : options . timeout ?? this . _timeout ,
5558 } )
5659 )
5760
5861 return data
5962 }
6063
6164 public async listModels ( input : void , options : RequestOptions = { } ) {
62- const signal = options . signal ?? AbortSignal . timeout ( this . _config . timeout )
65+ const signal = options . signal ?? AbortSignal . timeout ( this . _timeout )
6366
6467 const { data } = await this . _withServerRetry ( ( ) =>
65- this . _axiosClient . post < Model [ ] > ( '/v1 /models' , input , {
68+ this . _axiosClient . post < Model [ ] > ( '/v2/cognitive /models' , input , {
6669 signal,
67- timeout : options . timeout ?? this . _config . timeout ,
70+ timeout : options . timeout ?? this . _timeout ,
6871 } )
6972 )
7073
@@ -75,18 +78,16 @@ export class CognitiveBeta {
7578 request : CognitiveRequest ,
7679 options : RequestOptions = { }
7780 ) : AsyncGenerator < CognitiveStreamChunk , void , unknown > {
78- const signal = options . signal ?? AbortSignal . timeout ( this . _config . timeout )
81+ const signal = options . signal ?? AbortSignal . timeout ( this . _timeout )
7982
8083 if ( isBrowser ( ) ) {
81- const res = await fetch ( `${ this . _config . baseUrl } /v1 /generate-text-stream` , {
84+ const res = await fetch ( `${ this . _apiUrl } /v2/cognitive /generate-text-stream` , {
8285 method : 'POST' ,
8386 headers : {
84- Authorization : `Bearer ${ this . _config . token } ` ,
85- 'X-Bot-Id' : this . _config . botId ,
87+ ...this . _headers ,
8688 'Content-Type' : 'application/json' ,
87- ...this . _config . headers ,
8889 } ,
89- credentials : this . _config . withCredentials ? 'include' : 'omit' ,
90+ credentials : this . _withCredentials ? 'include' : 'omit' ,
9091 body : JSON . stringify ( { ...request , stream : true } ) ,
9192 signal,
9293 } )
@@ -129,7 +130,7 @@ export class CognitiveBeta {
129130 {
130131 responseType : 'stream' ,
131132 signal,
132- timeout : options . timeout ?? this . _config . timeout ,
133+ timeout : options . timeout ?? this . _timeout ,
133134 }
134135 )
135136 )
0 commit comments