Skip to content

Commit c0b94e3

Browse files
authored
update node sdk with streaming/non-streaming return type fix (#5)
* Sync updates from stainless branch: yanxi0830/dev * add examples
1 parent 4dc9bbe commit c0b94e3

File tree

12 files changed

+197
-145
lines changed

12 files changed

+197
-145
lines changed

examples/agents.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { AgentConfig } from "llama-stack-client/resources/shared";
44

5-
const LlamaStackClient = require('llama-stack-client').default;
5+
import LlamaStackClient from 'llama-stack-client';
66
const client = new LlamaStackClient({ baseURL: 'http://localhost:8321' });
77

88
async function main() {

examples/inference.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env -S npm run tsn -T
22

3-
const LlamaStackClient = require('llama-stack-client').default;
3+
import LlamaStackClient from 'llama-stack-client';
44
const client = new LlamaStackClient({ baseURL: 'http://localhost:8321' });
55

66
async function main() {
@@ -22,7 +22,9 @@ async function main() {
2222
stream: true,
2323
});
2424
for await (const chunk of stream) {
25-
process.stdout.write(chunk.event.delta.text || '');
25+
if (chunk.event.delta.type === 'text') {
26+
process.stdout.write(chunk.event.delta.text || '');
27+
}
2628
}
2729
process.stdout.write('\n');
2830
}

src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,16 @@ import {
3333
ListEvalTasksResponse,
3434
} from './resources/eval-tasks';
3535
import {
36+
ChatCompletionResponseStreamChunk,
3637
CompletionResponse,
3738
EmbeddingsResponse,
3839
Inference,
3940
InferenceChatCompletionParams,
4041
InferenceChatCompletionParamsNonStreaming,
4142
InferenceChatCompletionParamsStreaming,
42-
InferenceChatCompletionResponse,
4343
InferenceCompletionParams,
4444
InferenceCompletionParamsNonStreaming,
4545
InferenceCompletionParamsStreaming,
46-
InferenceCompletionResponse,
4746
InferenceEmbeddingsParams,
4847
TokenLogProbs,
4948
} from './resources/inference';
@@ -407,11 +406,10 @@ export declare namespace LlamaStackClient {
407406

408407
export {
409408
Inference as Inference,
409+
type ChatCompletionResponseStreamChunk as ChatCompletionResponseStreamChunk,
410410
type CompletionResponse as CompletionResponse,
411411
type EmbeddingsResponse as EmbeddingsResponse,
412412
type TokenLogProbs as TokenLogProbs,
413-
type InferenceChatCompletionResponse as InferenceChatCompletionResponse,
414-
type InferenceCompletionResponse as InferenceCompletionResponse,
415413
type InferenceChatCompletionParams as InferenceChatCompletionParams,
416414
type InferenceChatCompletionParamsNonStreaming as InferenceChatCompletionParamsNonStreaming,
417415
type InferenceChatCompletionParamsStreaming as InferenceChatCompletionParamsStreaming,
@@ -538,6 +536,7 @@ export declare namespace LlamaStackClient {
538536

539537
export type AgentConfig = API.AgentConfig;
540538
export type BatchCompletion = API.BatchCompletion;
539+
export type ChatCompletionResponse = API.ChatCompletionResponse;
541540
export type CompletionMessage = API.CompletionMessage;
542541
export type ContentDelta = API.ContentDelta;
543542
export type Document = API.Document;

src/resources/agents/agents.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import * as StepsAPI from './steps';
1515
import { StepRetrieveResponse, Steps } from './steps';
1616
import * as TurnAPI from './turn';
1717
import {
18+
AgentTurnResponseStreamChunk,
1819
Turn,
1920
TurnCreateParams,
2021
TurnCreateParamsNonStreaming,
2122
TurnCreateParamsStreaming,
22-
TurnCreateResponse,
2323
TurnResource,
2424
TurnResponseEvent,
2525
TurnResponseEventPayload,
@@ -145,10 +145,10 @@ export declare namespace Agents {
145145

146146
export {
147147
TurnResource as TurnResource,
148+
type AgentTurnResponseStreamChunk as AgentTurnResponseStreamChunk,
148149
type Turn as Turn,
149150
type TurnResponseEvent as TurnResponseEvent,
150151
type TurnResponseEventPayload as TurnResponseEventPayload,
151-
type TurnCreateResponse as TurnCreateResponse,
152152
type TurnCreateParams as TurnCreateParams,
153153
type TurnCreateParamsNonStreaming as TurnCreateParamsNonStreaming,
154154
type TurnCreateParamsStreaming as TurnCreateParamsStreaming,

src/resources/agents/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ export {
2020
export { Steps, type StepRetrieveResponse } from './steps';
2121
export {
2222
TurnResource,
23+
type AgentTurnResponseStreamChunk,
2324
type Turn,
2425
type TurnResponseEvent,
2526
type TurnResponseEventPayload,
26-
type TurnCreateResponse,
2727
type TurnCreateParams,
2828
type TurnCreateParamsNonStreaming,
2929
type TurnCreateParamsStreaming,

src/resources/agents/turn.ts

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,30 @@ export class TurnResource extends APIResource {
1414
sessionId: string,
1515
body: TurnCreateParamsNonStreaming,
1616
options?: Core.RequestOptions,
17-
): APIPromise<TurnCreateResponse>;
17+
): APIPromise<Turn>;
1818
create(
1919
agentId: string,
2020
sessionId: string,
2121
body: TurnCreateParamsStreaming,
2222
options?: Core.RequestOptions,
23-
): APIPromise<Stream<TurnCreateResponse>>;
23+
): APIPromise<Stream<AgentTurnResponseStreamChunk>>;
2424
create(
2525
agentId: string,
2626
sessionId: string,
2727
body: TurnCreateParamsBase,
2828
options?: Core.RequestOptions,
29-
): APIPromise<Stream<TurnCreateResponse> | TurnCreateResponse>;
29+
): APIPromise<Stream<AgentTurnResponseStreamChunk> | Turn>;
3030
create(
3131
agentId: string,
3232
sessionId: string,
3333
body: TurnCreateParams,
3434
options?: Core.RequestOptions,
35-
): APIPromise<TurnCreateResponse> | APIPromise<Stream<TurnCreateResponse>> {
35+
): APIPromise<Turn> | APIPromise<Stream<AgentTurnResponseStreamChunk>> {
3636
return this._client.post(`/v1/agents/${agentId}/session/${sessionId}/turn`, {
3737
body,
3838
...options,
39-
headers: { Accept: 'text/event-stream', ...options?.headers },
4039
stream: body.stream ?? false,
41-
}) as APIPromise<TurnCreateResponse> | APIPromise<Stream<TurnCreateResponse>>;
40+
}) as APIPromise<Turn> | APIPromise<Stream<AgentTurnResponseStreamChunk>>;
4241
}
4342

4443
retrieve(
@@ -51,6 +50,10 @@ export class TurnResource extends APIResource {
5150
}
5251
}
5352

53+
export interface AgentTurnResponseStreamChunk {
54+
event: TurnResponseEvent;
55+
}
56+
5457
export interface Turn {
5558
input_messages: Array<Shared.UserMessage | Shared.ToolResponseMessage>;
5659

@@ -88,22 +91,44 @@ export namespace Turn {
8891

8992
export namespace OutputAttachment {
9093
export interface ImageContentItem {
94+
/**
95+
* Image as a base64 encoded string or an URL
96+
*/
9197
image: ImageContentItem.Image;
9298

99+
/**
100+
* Discriminator type of the content item. Always "image"
101+
*/
93102
type: 'image';
94103
}
95104

96105
export namespace ImageContentItem {
106+
/**
107+
* Image as a base64 encoded string or an URL
108+
*/
97109
export interface Image {
110+
/**
111+
* base64 encoded image data as string
112+
*/
98113
data?: string;
99114

115+
/**
116+
* A URL of the image or data URL in the format of data:image/{type};base64,{data}.
117+
* Note that URL could have length limits.
118+
*/
100119
url?: Shared.URL;
101120
}
102121
}
103122

104123
export interface TextContentItem {
124+
/**
125+
* Text content
126+
*/
105127
text: string;
106128

129+
/**
130+
* Discriminator type of the content item. Always "text"
131+
*/
107132
type: 'text';
108133
}
109134
}
@@ -168,14 +193,6 @@ export namespace TurnResponseEventPayload {
168193
}
169194
}
170195

171-
export type TurnCreateResponse = Turn | TurnCreateResponse.AgentTurnResponseStreamChunk;
172-
173-
export namespace TurnCreateResponse {
174-
export interface AgentTurnResponseStreamChunk {
175-
event: TurnAPI.TurnResponseEvent;
176-
}
177-
}
178-
179196
export type TurnCreateParams = TurnCreateParamsNonStreaming | TurnCreateParamsStreaming;
180197

181198
export interface TurnCreateParamsBase {
@@ -202,22 +219,44 @@ export namespace TurnCreateParams {
202219

203220
export namespace Document {
204221
export interface ImageContentItem {
222+
/**
223+
* Image as a base64 encoded string or an URL
224+
*/
205225
image: ImageContentItem.Image;
206226

227+
/**
228+
* Discriminator type of the content item. Always "image"
229+
*/
207230
type: 'image';
208231
}
209232

210233
export namespace ImageContentItem {
234+
/**
235+
* Image as a base64 encoded string or an URL
236+
*/
211237
export interface Image {
238+
/**
239+
* base64 encoded image data as string
240+
*/
212241
data?: string;
213242

243+
/**
244+
* A URL of the image or data URL in the format of data:image/{type};base64,{data}.
245+
* Note that URL could have length limits.
246+
*/
214247
url?: Shared.URL;
215248
}
216249
}
217250

218251
export interface TextContentItem {
252+
/**
253+
* Text content
254+
*/
219255
text: string;
220256

257+
/**
258+
* Discriminator type of the content item. Always "text"
259+
*/
221260
type: 'text';
222261
}
223262
}
@@ -242,10 +281,10 @@ export interface TurnCreateParamsStreaming extends TurnCreateParamsBase {
242281

243282
export declare namespace TurnResource {
244283
export {
284+
type AgentTurnResponseStreamChunk as AgentTurnResponseStreamChunk,
245285
type Turn as Turn,
246286
type TurnResponseEvent as TurnResponseEvent,
247287
type TurnResponseEventPayload as TurnResponseEventPayload,
248-
type TurnCreateResponse as TurnCreateResponse,
249288
type TurnCreateParams as TurnCreateParams,
250289
type TurnCreateParamsNonStreaming as TurnCreateParamsNonStreaming,
251290
type TurnCreateParamsStreaming as TurnCreateParamsStreaming,

src/resources/batch-inference.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import { APIResource } from '../resource';
44
import * as Core from '../core';
5-
import * as InferenceAPI from './inference';
65
import * as Shared from './shared';
76

87
export class BatchInference extends APIResource {
@@ -22,21 +21,7 @@ export class BatchInference extends APIResource {
2221
}
2322

2423
export interface BatchInferenceChatCompletionResponse {
25-
batch: Array<BatchInferenceChatCompletionResponse.Batch>;
26-
}
27-
28-
export namespace BatchInferenceChatCompletionResponse {
29-
export interface Batch {
30-
/**
31-
* The complete response message
32-
*/
33-
completion_message: Shared.CompletionMessage;
34-
35-
/**
36-
* Optional log probabilities for generated tokens
37-
*/
38-
logprobs?: Array<InferenceAPI.TokenLogProbs>;
39-
}
24+
batch: Array<Shared.ChatCompletionResponse>;
4025
}
4126

4227
export interface BatchInferenceChatCompletionParams {

src/resources/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,10 @@ export {
4848
} from './eval-tasks';
4949
export {
5050
Inference,
51+
type ChatCompletionResponseStreamChunk,
5152
type CompletionResponse,
5253
type EmbeddingsResponse,
5354
type TokenLogProbs,
54-
type InferenceChatCompletionResponse,
55-
type InferenceCompletionResponse,
5655
type InferenceChatCompletionParams,
5756
type InferenceChatCompletionParamsNonStreaming,
5857
type InferenceChatCompletionParamsStreaming,

0 commit comments

Comments
 (0)