Skip to content

Commit 01fb145

Browse files
authored
Sync updates from stainless branch: ehhuang/dev (#27)
1 parent 3d9d160 commit 01fb145

File tree

5 files changed

+69
-7
lines changed

5 files changed

+69
-7
lines changed

src/core.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ export class APIPromise<T> extends Promise<T> {
184184

185185
export abstract class APIClient {
186186
baseURL: string;
187+
#baseURLOverridden: boolean;
187188
maxRetries: number;
188189
timeout: number;
189190
httpAgent: Agent | undefined;
@@ -193,18 +194,21 @@ export abstract class APIClient {
193194

194195
constructor({
195196
baseURL,
197+
baseURLOverridden,
196198
maxRetries = 2,
197199
timeout = 60000, // 1 minute
198200
httpAgent,
199201
fetch: overriddenFetch,
200202
}: {
201203
baseURL: string;
204+
baseURLOverridden: boolean;
202205
maxRetries?: number | undefined;
203206
timeout: number | undefined;
204207
httpAgent: Agent | undefined;
205208
fetch: Fetch | undefined;
206209
}) {
207210
this.baseURL = baseURL;
211+
this.#baseURLOverridden = baseURLOverridden;
208212
this.maxRetries = validatePositiveInteger('maxRetries', maxRetries);
209213
this.timeout = validatePositiveInteger('timeout', timeout);
210214
this.httpAgent = httpAgent;
@@ -314,7 +318,7 @@ export abstract class APIClient {
314318
{ retryCount = 0 }: { retryCount?: number } = {},
315319
): { req: RequestInit; url: string; timeout: number } {
316320
const options = { ...inputOptions };
317-
const { method, path, query, headers: headers = {} } = options;
321+
const { method, path, query, defaultBaseURL, headers: headers = {} } = options;
318322

319323
const body =
320324
ArrayBuffer.isView(options.body) || (options.__binaryRequest && typeof options.body === 'string') ?
@@ -324,7 +328,7 @@ export abstract class APIClient {
324328
: null;
325329
const contentLength = this.calculateContentLength(body);
326330

327-
const url = this.buildURL(path!, query);
331+
const url = this.buildURL(path!, query, defaultBaseURL);
328332
if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);
329333
options.timeout = options.timeout ?? this.timeout;
330334
const httpAgent = options.httpAgent ?? this.httpAgent ?? getDefaultAgent(url);
@@ -517,11 +521,12 @@ export abstract class APIClient {
517521
return new PagePromise<PageClass, Item>(this, request, Page);
518522
}
519523

520-
buildURL<Req>(path: string, query: Req | null | undefined): string {
524+
buildURL<Req>(path: string, query: Req | null | undefined, defaultBaseURL?: string | undefined): string {
525+
const baseURL = (!this.#baseURLOverridden && defaultBaseURL) || this.baseURL;
521526
const url =
522527
isAbsoluteURL(path) ?
523528
new URL(path)
524-
: new URL(this.baseURL + (this.baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));
529+
: new URL(baseURL + (baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));
525530

526531
const defaultQuery = this.defaultQuery();
527532
if (!isEmptyObj(defaultQuery)) {
@@ -806,6 +811,7 @@ export type RequestOptions<
806811
query?: Req | undefined;
807812
body?: Req | null | undefined;
808813
headers?: Headers | undefined;
814+
defaultBaseURL?: string | undefined;
809815

810816
maxRetries?: number;
811817
stream?: boolean | undefined;
@@ -828,6 +834,7 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
828834
query: true,
829835
body: true,
830836
headers: true,
837+
defaultBaseURL: true,
831838

832839
maxRetries: true,
833840
stream: true,

src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ export class LlamaStackClient extends Core.APIClient {
292292

293293
super({
294294
baseURL: options.baseURL!,
295+
baseURLOverridden: baseURL ? baseURL !== 'http://any-hosted-llama-stack.com' : false,
295296
timeout: options.timeout ?? 60000 /* 1 minute */,
296297
httpAgent: options.httpAgent,
297298
maxRetries: options.maxRetries,
@@ -331,6 +332,13 @@ export class LlamaStackClient extends Core.APIClient {
331332
benchmarks: API.Benchmarks = new API.Benchmarks(this);
332333
files: API.Files = new API.Files(this);
333334

335+
/**
336+
* Check whether the base URL is set to its default.
337+
*/
338+
#baseURLOverridden(): boolean {
339+
return this.baseURL !== 'http://any-hosted-llama-stack.com';
340+
}
341+
334342
protected override defaultQuery(): Core.DefaultQuery | undefined {
335343
return this._options.defaultQuery;
336344
}

src/resources/vector-stores/vector-stores.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export interface VectorStore {
9595

9696
created_at: number;
9797

98-
file_counts: Record<string, number>;
98+
file_counts: VectorStore.FileCounts;
9999

100100
metadata: Record<string, boolean | number | string | Array<unknown> | unknown | null>;
101101

@@ -114,6 +114,20 @@ export interface VectorStore {
114114
name?: string;
115115
}
116116

117+
export namespace VectorStore {
118+
export interface FileCounts {
119+
cancelled: number;
120+
121+
completed: number;
122+
123+
failed: number;
124+
125+
in_progress: number;
126+
127+
total: number;
128+
}
129+
}
130+
117131
/**
118132
* Response from deleting a vector store.
119133
*/
@@ -276,14 +290,25 @@ export interface VectorStoreSearchParams {
276290
/**
277291
* Ranking options for fine-tuning the search results.
278292
*/
279-
ranking_options?: Record<string, boolean | number | string | Array<unknown> | unknown | null>;
293+
ranking_options?: VectorStoreSearchParams.RankingOptions;
280294

281295
/**
282296
* Whether to rewrite the natural language query for vector search (default false)
283297
*/
284298
rewrite_query?: boolean;
285299
}
286300

301+
export namespace VectorStoreSearchParams {
302+
/**
303+
* Ranking options for fine-tuning the search results.
304+
*/
305+
export interface RankingOptions {
306+
ranker?: string;
307+
308+
score_threshold?: number;
309+
}
310+
}
311+
287312
VectorStores.Files = Files;
288313

289314
export declare namespace VectorStores {

tests/api-resources/vector-stores/vector-stores.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ describe('resource vectorStores', () => {
122122
query: 'string',
123123
filters: { foo: true },
124124
max_num_results: 0,
125-
ranking_options: { foo: true },
125+
ranking_options: { ranker: 'ranker', score_threshold: 0 },
126126
rewrite_query: true,
127127
});
128128
});

tests/index.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,28 @@ describe('instantiate client', () => {
171171
const client = new LlamaStackClient({});
172172
expect(client.baseURL).toEqual('http://any-hosted-llama-stack.com');
173173
});
174+
175+
test('in request options', () => {
176+
const client = new LlamaStackClient({});
177+
expect(client.buildURL('/foo', null, 'http://localhost:5000/option')).toEqual(
178+
'http://localhost:5000/option/foo',
179+
);
180+
});
181+
182+
test('in request options overridden by client options', () => {
183+
const client = new LlamaStackClient({ baseURL: 'http://localhost:5000/client' });
184+
expect(client.buildURL('/foo', null, 'http://localhost:5000/option')).toEqual(
185+
'http://localhost:5000/client/foo',
186+
);
187+
});
188+
189+
test('in request options overridden by env variable', () => {
190+
process.env['LLAMA_STACK_BASE_URL'] = 'http://localhost:5000/env';
191+
const client = new LlamaStackClient({});
192+
expect(client.buildURL('/foo', null, 'http://localhost:5000/option')).toEqual(
193+
'http://localhost:5000/env/foo',
194+
);
195+
});
174196
});
175197

176198
test('maxRetries option is correctly set', () => {

0 commit comments

Comments
 (0)