Skip to content

Commit 59ca010

Browse files
authored
Sync updates from stainless branch: main (#8)
1 parent 764f35d commit 59ca010

File tree

16 files changed

+252
-11
lines changed

16 files changed

+252
-11
lines changed

src/core.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ export abstract class APIClient {
294294
options: FinalRequestOptions<Req>,
295295
{ retryCount = 0 }: { retryCount?: number } = {},
296296
): { req: RequestInit; url: string; timeout: number } {
297+
options = { ...options };
297298
const { method, path, query, headers: headers = {} } = options;
298299

299300
const body =
@@ -306,9 +307,9 @@ export abstract class APIClient {
306307

307308
const url = this.buildURL(path!, query);
308309
if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);
309-
const timeout = options.timeout ?? this.timeout;
310+
options.timeout = options.timeout ?? this.timeout;
310311
const httpAgent = options.httpAgent ?? this.httpAgent ?? getDefaultAgent(url);
311-
const minAgentTimeout = timeout + 1000;
312+
const minAgentTimeout = options.timeout + 1000;
312313
if (
313314
typeof (httpAgent as any)?.options?.timeout === 'number' &&
314315
minAgentTimeout > ((httpAgent as any).options.timeout ?? 0)
@@ -337,7 +338,7 @@ export abstract class APIClient {
337338
signal: options.signal ?? null,
338339
};
339340

340-
return { req, url, timeout };
341+
return { req, url, timeout: options.timeout };
341342
}
342343

343344
private buildHeaders({
@@ -365,15 +366,22 @@ export abstract class APIClient {
365366
delete reqHeaders['content-type'];
366367
}
367368

368-
// Don't set the retry count header if it was already set or removed through default headers or by the
369-
// caller. We check `defaultHeaders` and `headers`, which can contain nulls, instead of `reqHeaders` to
370-
// account for the removal case.
369+
// Don't set theses headers if they were already set or removed through default headers or by the caller.
370+
// We check `defaultHeaders` and `headers`, which can contain nulls, instead of `reqHeaders` to account
371+
// for the removal case.
371372
if (
372373
getHeader(defaultHeaders, 'x-stainless-retry-count') === undefined &&
373374
getHeader(headers, 'x-stainless-retry-count') === undefined
374375
) {
375376
reqHeaders['x-stainless-retry-count'] = String(retryCount);
376377
}
378+
if (
379+
getHeader(defaultHeaders, 'x-stainless-timeout') === undefined &&
380+
getHeader(headers, 'x-stainless-timeout') === undefined &&
381+
options.timeout
382+
) {
383+
reqHeaders['x-stainless-timeout'] = String(options.timeout);
384+
}
377385

378386
this.validateHeaders(reqHeaders, headers);
379387

src/index.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ import {
159159
} from './resources/tool-runtime/tool-runtime';
160160

161161
export interface ClientOptions {
162+
/**
163+
* Defaults to process.env['LLAMA_STACK_CLIENT_API_KEY'].
164+
*/
165+
apiKey?: string | null | undefined;
166+
162167
/**
163168
* Override the default base URL for the API, e.g., "https://api.example.com/v2/"
164169
*
@@ -220,11 +225,14 @@ export interface ClientOptions {
220225
* API Client for interfacing with the Llama Stack Client API.
221226
*/
222227
export class LlamaStackClient extends Core.APIClient {
228+
apiKey: string | null;
229+
223230
private _options: ClientOptions;
224231

225232
/**
226233
* API Client for interfacing with the Llama Stack Client API.
227234
*
235+
* @param {string | null | undefined} [opts.apiKey=process.env['LLAMA_STACK_CLIENT_API_KEY'] ?? null]
228236
* @param {string} [opts.baseURL=process.env['LLAMA_STACK_CLIENT_BASE_URL'] ?? http://any-hosted-llama-stack.com] - Override the default base URL for the API.
229237
* @param {number} [opts.timeout=1 minute] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
230238
* @param {number} [opts.httpAgent] - An HTTP agent used to manage HTTP(s) connections.
@@ -233,8 +241,13 @@ export class LlamaStackClient extends Core.APIClient {
233241
* @param {Core.Headers} opts.defaultHeaders - Default headers to include with every request to the API.
234242
* @param {Core.DefaultQuery} opts.defaultQuery - Default query parameters to include with every request to the API.
235243
*/
236-
constructor({ baseURL = Core.readEnv('LLAMA_STACK_CLIENT_BASE_URL'), ...opts }: ClientOptions = {}) {
244+
constructor({
245+
baseURL = Core.readEnv('LLAMA_STACK_CLIENT_BASE_URL'),
246+
apiKey = Core.readEnv('LLAMA_STACK_CLIENT_API_KEY') ?? null,
247+
...opts
248+
}: ClientOptions = {}) {
237249
const options: ClientOptions = {
250+
apiKey,
238251
...opts,
239252
baseURL: baseURL || `http://any-hosted-llama-stack.com`,
240253
};
@@ -248,6 +261,8 @@ export class LlamaStackClient extends Core.APIClient {
248261
});
249262

250263
this._options = options;
264+
265+
this.apiKey = apiKey;
251266
}
252267

253268
toolgroups: API.Toolgroups = new API.Toolgroups(this);
@@ -285,6 +300,13 @@ export class LlamaStackClient extends Core.APIClient {
285300
};
286301
}
287302

303+
protected override authHeaders(opts: Core.FinalRequestOptions): Core.Headers {
304+
if (this.apiKey == null) {
305+
return {};
306+
}
307+
return { Authorization: `Bearer ${this.apiKey}` };
308+
}
309+
288310
protected override stringifyQuery(query: Record<string, unknown>): string {
289311
return qs.stringify(query, { arrayFormat: 'comma' });
290312
}

src/resources/agents/agents.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ export class Agents extends APIResource {
4343
}
4444

4545
export interface InferenceStep {
46+
/**
47+
* A message containing the model's (assistant) response in a chat conversation.
48+
*/
4649
model_response: Shared.CompletionMessage;
4750

4851
step_id: string;
@@ -57,6 +60,9 @@ export interface InferenceStep {
5760
}
5861

5962
export interface MemoryRetrievalStep {
63+
/**
64+
* A image content item
65+
*/
6066
inserted_context: Shared.InterleavedContent;
6167

6268
step_id: string;
@@ -105,6 +111,9 @@ export interface ToolExecutionStep {
105111
export interface ToolResponse {
106112
call_id: string;
107113

114+
/**
115+
* A image content item
116+
*/
108117
content: Shared.InterleavedContent;
109118

110119
tool_name: 'brave_search' | 'wolfram_alpha' | 'photogen' | 'code_interpreter' | (string & {});

src/resources/agents/session.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ export class SessionResource extends APIResource {
4141
}
4242
}
4343

44+
/**
45+
* A single session of an interaction with an Agentic System.
46+
*/
4447
export interface Session {
4548
session_id: string;
4649

src/resources/agents/turn.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,24 @@ export class TurnResource extends APIResource {
5050
}
5151
}
5252

53+
/**
54+
* streamed agent turn completion response.
55+
*/
5356
export interface AgentTurnResponseStreamChunk {
5457
event: TurnResponseEvent;
5558
}
5659

60+
/**
61+
* A single turn in an interaction with an Agentic System.
62+
*/
5763
export interface Turn {
5864
input_messages: Array<Shared.UserMessage | Shared.ToolResponseMessage>;
5965

6066
output_attachments: Array<Turn.OutputAttachment>;
6167

68+
/**
69+
* A message containing the model's (assistant) response in a chat conversation.
70+
*/
6271
output_message: Shared.CompletionMessage;
6372

6473
session_id: string;
@@ -79,6 +88,9 @@ export interface Turn {
7988

8089
export namespace Turn {
8190
export interface OutputAttachment {
91+
/**
92+
* A image content item
93+
*/
8294
content:
8395
| string
8496
| OutputAttachment.ImageContentItem
@@ -90,6 +102,9 @@ export namespace Turn {
90102
}
91103

92104
export namespace OutputAttachment {
105+
/**
106+
* A image content item
107+
*/
93108
export interface ImageContentItem {
94109
/**
95110
* Image as a base64 encoded string or an URL
@@ -120,6 +135,9 @@ export namespace Turn {
120135
}
121136
}
122137

138+
/**
139+
* A text content item
140+
*/
123141
export interface TextContentItem {
124142
/**
125143
* Text content
@@ -189,6 +207,9 @@ export namespace TurnResponseEventPayload {
189207
export interface AgentTurnResponseTurnCompletePayload {
190208
event_type: 'turn_complete';
191209

210+
/**
211+
* A single turn in an interaction with an Agentic System.
212+
*/
192213
turn: TurnAPI.Turn;
193214
}
194215
}
@@ -207,6 +228,9 @@ export interface TurnCreateParamsBase {
207228

208229
export namespace TurnCreateParams {
209230
export interface Document {
231+
/**
232+
* A image content item
233+
*/
210234
content:
211235
| string
212236
| Document.ImageContentItem
@@ -218,6 +242,9 @@ export namespace TurnCreateParams {
218242
}
219243

220244
export namespace Document {
245+
/**
246+
* A image content item
247+
*/
221248
export interface ImageContentItem {
222249
/**
223250
* Image as a base64 encoded string or an URL
@@ -248,6 +275,9 @@ export namespace TurnCreateParams {
248275
}
249276
}
250277

278+
/**
279+
* A text content item
280+
*/
251281
export interface TextContentItem {
252282
/**
253283
* Text content

src/resources/batch-inference.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,23 @@ export interface BatchInferenceChatCompletionParams {
3131

3232
logprobs?: BatchInferenceChatCompletionParams.Logprobs;
3333

34+
/**
35+
* Configuration for JSON schema-guided response generation.
36+
*/
3437
response_format?: Shared.ResponseFormat;
3538

3639
sampling_params?: Shared.SamplingParams;
3740

41+
/**
42+
* Whether tool use is required or automatic. This is a hint to the model which may
43+
* not be followed. It depends on the Instruction Following capabilities of the
44+
* model.
45+
*/
3846
tool_choice?: 'auto' | 'required';
3947

48+
/**
49+
* Prompt format for calling custom / zero shot tools.
50+
*/
4051
tool_prompt_format?: 'json' | 'function_tag' | 'python_list';
4152

4253
tools?: Array<BatchInferenceChatCompletionParams.Tool>;
@@ -66,6 +77,9 @@ export interface BatchInferenceCompletionParams {
6677

6778
logprobs?: BatchInferenceCompletionParams.Logprobs;
6879

80+
/**
81+
* Configuration for JSON schema-guided response generation.
82+
*/
6983
response_format?: Shared.ResponseFormat;
7084

7185
sampling_params?: Shared.SamplingParams;

src/resources/eval/eval.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export namespace EvalCandidate {
3434

3535
type: 'model';
3636

37+
/**
38+
* A system message providing instructions or context to the model.
39+
*/
3740
system_message?: Shared.SystemMessage;
3841
}
3942

0 commit comments

Comments
 (0)