Skip to content

Commit 3b3398a

Browse files
committed
fix: user agent
1 parent 75faa50 commit 3b3398a

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

src/Client.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
import * as Core from "./Core";
22
import { Chat } from "./resources/chat";
33

4+
export interface AI21Options {
5+
apiKey: string;
6+
baseURL: string;
7+
via: string | null;
8+
}
49
export class AI21 extends Core.APIClient {
5-
constructor(apiKey: string) {
10+
private _options: AI21Options;
11+
12+
constructor(
13+
apiKey: string = process.env.AI21_API_KEY || '',
14+
baseURL: string = process.env.AI21_BASE_URL || 'https://api.ai21.com/studio/v1',
15+
via: string | null,
16+
) {
17+
const options: AI21Options = {
18+
apiKey,
19+
baseURL,
20+
via,
21+
};
22+
623
super({
7-
baseURL: 'https://api.ai21.com/studio/v1',
24+
baseURL,
825
timeout: 60000,
926
apiKey,
1027
options: {}
1128
});
29+
30+
this._options = options;
1231
}
1332

1433
protected override authHeaders(opts: Core.FinalRequestOptions): Core.Headers {
@@ -17,5 +36,21 @@ export class AI21 extends Core.APIClient {
1736
};
1837
}
1938

39+
protected override defaultHeaders(opts: Core.FinalRequestOptions): Core.Headers {
40+
return {
41+
...super.defaultHeaders(opts),
42+
'User-Agent': this.getUserAgent(),
43+
};
44+
}
45+
46+
protected override getUserAgent(): string {
47+
let userAgent = super.getUserAgent();
48+
49+
if (this._options.via) {
50+
userAgent += ` via ${this._options.via}`;
51+
}
52+
return userAgent;
53+
}
54+
2055
chat: Chat = new Chat(this);
2156
}

src/Core.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,14 @@ export abstract class APIClient {
150150

151151
constructor({
152152
baseURL,
153-
maxRetries = 2,
154-
timeout = 600000, // 10 minutes
155-
// fetch: overridenFetch,
153+
maxRetries = 3,
154+
timeout = 300000,
156155
apiKey,
157156
options,
158157
}: {
159158
baseURL: string;
160159
maxRetries?: number | undefined;
161160
timeout: number | undefined;
162-
// fetch: Fetch | undefined;
163161
apiKey: string;
164162
options: ClientOptions;
165163
}) {
@@ -186,16 +184,15 @@ export abstract class APIClient {
186184
return this.makeRequest('delete', path, opts);
187185
}
188186

189-
private getUserAgent(): string {
190-
return `${this.constructor.name}/JS ${VERSION}`;
187+
protected getUserAgent(): string {
188+
return`AI21 Typescript SDK ${VERSION}`;
191189
}
192190

193191
protected defaultHeaders(opts: FinalRequestOptions): Headers {
194192
return {
195193
Accept: 'application/json',
196194
'Content-Type': 'application/json',
197195
'User-Agent': this.getUserAgent(),
198-
// ...getPlatformHeaders(),
199196
...this.authHeaders(opts),
200197
};
201198
}
@@ -255,18 +252,12 @@ export abstract class APIClient {
255252
return { response, options, controller };
256253
}
257254

258-
protected static isRunningInBrowser(): boolean {
255+
protected static isRunningInBrowser(): boolean {
259256
return (
260257
typeof window !== 'undefined' &&
261258
typeof window.document !== 'undefined' &&
262259
typeof fetch === 'function'
263260
);
264261
}
265262
}
266-
export const readEnv = (env: string): string | undefined => {
267-
if (typeof process !== 'undefined' && process.env) {
268-
return process.env[env]?.trim() ?? undefined;
269-
}
270-
return undefined;
271-
};
272263

0 commit comments

Comments
 (0)