-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathturn.ts
527 lines (453 loc) · 13.7 KB
/
turn.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import { APIResource } from '../../resource';
import { APIPromise } from '../../core';
import * as Core from '../../core';
import * as TurnAPI from './turn';
import * as Shared from '../shared';
import * as AgentsAPI from './agents';
import { Stream } from '../../streaming';
export class TurnResource extends APIResource {
/**
* Create a new turn for an agent.
*/
create(
agentId: string,
sessionId: string,
body: TurnCreateParamsNonStreaming,
options?: Core.RequestOptions,
): APIPromise<Turn>;
create(
agentId: string,
sessionId: string,
body: TurnCreateParamsStreaming,
options?: Core.RequestOptions,
): APIPromise<Stream<AgentTurnResponseStreamChunk>>;
create(
agentId: string,
sessionId: string,
body: TurnCreateParamsBase,
options?: Core.RequestOptions,
): APIPromise<Stream<AgentTurnResponseStreamChunk> | Turn>;
create(
agentId: string,
sessionId: string,
body: TurnCreateParams,
options?: Core.RequestOptions,
): APIPromise<Turn> | APIPromise<Stream<AgentTurnResponseStreamChunk>> {
return this._client.post(`/v1/agents/${agentId}/session/${sessionId}/turn`, {
body,
...options,
stream: body.stream ?? false,
}) as APIPromise<Turn> | APIPromise<Stream<AgentTurnResponseStreamChunk>>;
}
/**
* Retrieve an agent turn by its ID.
*/
retrieve(
agentId: string,
sessionId: string,
turnId: string,
options?: Core.RequestOptions,
): Core.APIPromise<Turn> {
return this._client.get(`/v1/agents/${agentId}/session/${sessionId}/turn/${turnId}`, options);
}
/**
* Resume an agent turn with executed tool call responses. When a Turn has the
* status `awaiting_input` due to pending input from client side tool calls, this
* endpoint can be used to submit the outputs from the tool calls once they are
* ready.
*/
resume(
agentId: string,
sessionId: string,
turnId: string,
body: TurnResumeParamsNonStreaming,
options?: Core.RequestOptions,
): APIPromise<Turn>;
resume(
agentId: string,
sessionId: string,
turnId: string,
body: TurnResumeParamsStreaming,
options?: Core.RequestOptions,
): APIPromise<Stream<AgentTurnResponseStreamChunk>>;
resume(
agentId: string,
sessionId: string,
turnId: string,
body: TurnResumeParamsBase,
options?: Core.RequestOptions,
): APIPromise<Stream<AgentTurnResponseStreamChunk> | Turn>;
resume(
agentId: string,
sessionId: string,
turnId: string,
body: TurnResumeParams,
options?: Core.RequestOptions,
): APIPromise<Turn> | APIPromise<Stream<AgentTurnResponseStreamChunk>> {
return this._client.post(`/v1/agents/${agentId}/session/${sessionId}/turn/${turnId}/resume`, {
body,
...options,
stream: body.stream ?? false,
}) as APIPromise<Turn> | APIPromise<Stream<AgentTurnResponseStreamChunk>>;
}
}
/**
* streamed agent turn completion response.
*/
export interface AgentTurnResponseStreamChunk {
event: TurnResponseEvent;
}
/**
* A single turn in an interaction with an Agentic System.
*/
export interface Turn {
input_messages: Array<Shared.UserMessage | Shared.ToolResponseMessage>;
/**
* A message containing the model's (assistant) response in a chat conversation.
*/
output_message: Shared.CompletionMessage;
session_id: string;
started_at: string;
steps: Array<
| AgentsAPI.InferenceStep
| AgentsAPI.ToolExecutionStep
| AgentsAPI.ShieldCallStep
| AgentsAPI.MemoryRetrievalStep
>;
turn_id: string;
completed_at?: string;
output_attachments?: Array<Turn.OutputAttachment>;
}
export namespace Turn {
/**
* An attachment to an agent turn.
*/
export interface OutputAttachment {
/**
* The content of the attachment.
*/
content:
| string
| OutputAttachment.ImageContentItem
| OutputAttachment.TextContentItem
| Array<Shared.InterleavedContentItem>
| OutputAttachment.URL;
/**
* The MIME type of the attachment.
*/
mime_type: string;
}
export namespace OutputAttachment {
/**
* A image content item
*/
export interface ImageContentItem {
/**
* Image as a base64 encoded string or an URL
*/
image: ImageContentItem.Image;
/**
* Discriminator type of the content item. Always "image"
*/
type: 'image';
}
export namespace ImageContentItem {
/**
* Image as a base64 encoded string or an URL
*/
export interface Image {
/**
* base64 encoded image data as string
*/
data?: string;
/**
* A URL of the image or data URL in the format of data:image/{type};base64,{data}.
* Note that URL could have length limits.
*/
url?: Image.URL;
}
export namespace Image {
/**
* A URL of the image or data URL in the format of data:image/{type};base64,{data}.
* Note that URL could have length limits.
*/
export interface URL {
uri: string;
}
}
}
/**
* A text content item
*/
export interface TextContentItem {
/**
* Text content
*/
text: string;
/**
* Discriminator type of the content item. Always "text"
*/
type: 'text';
}
export interface URL {
uri: string;
}
}
}
export interface TurnResponseEvent {
payload: TurnResponseEventPayload;
}
export type TurnResponseEventPayload =
| TurnResponseEventPayload.AgentTurnResponseStepStartPayload
| TurnResponseEventPayload.AgentTurnResponseStepProgressPayload
| TurnResponseEventPayload.AgentTurnResponseStepCompletePayload
| TurnResponseEventPayload.AgentTurnResponseTurnStartPayload
| TurnResponseEventPayload.AgentTurnResponseTurnCompletePayload
| TurnResponseEventPayload.AgentTurnResponseTurnAwaitingInputPayload;
export namespace TurnResponseEventPayload {
export interface AgentTurnResponseStepStartPayload {
event_type: 'step_start';
step_id: string;
/**
* Type of the step in an agent turn.
*/
step_type: 'inference' | 'tool_execution' | 'shield_call' | 'memory_retrieval';
metadata?: Record<string, boolean | number | string | Array<unknown> | unknown | null>;
}
export interface AgentTurnResponseStepProgressPayload {
delta: Shared.ContentDelta;
event_type: 'step_progress';
step_id: string;
/**
* Type of the step in an agent turn.
*/
step_type: 'inference' | 'tool_execution' | 'shield_call' | 'memory_retrieval';
}
export interface AgentTurnResponseStepCompletePayload {
event_type: 'step_complete';
/**
* An inference step in an agent turn.
*/
step_details:
| AgentsAPI.InferenceStep
| AgentsAPI.ToolExecutionStep
| AgentsAPI.ShieldCallStep
| AgentsAPI.MemoryRetrievalStep;
step_id: string;
/**
* Type of the step in an agent turn.
*/
step_type: 'inference' | 'tool_execution' | 'shield_call' | 'memory_retrieval';
}
export interface AgentTurnResponseTurnStartPayload {
event_type: 'turn_start';
turn_id: string;
}
export interface AgentTurnResponseTurnCompletePayload {
event_type: 'turn_complete';
/**
* A single turn in an interaction with an Agentic System.
*/
turn: TurnAPI.Turn;
}
export interface AgentTurnResponseTurnAwaitingInputPayload {
event_type: 'turn_awaiting_input';
/**
* A single turn in an interaction with an Agentic System.
*/
turn: TurnAPI.Turn;
}
}
export type TurnCreateParams = TurnCreateParamsNonStreaming | TurnCreateParamsStreaming;
export interface TurnCreateParamsBase {
/**
* List of messages to start the turn with.
*/
messages: Array<Shared.UserMessage | Shared.ToolResponseMessage>;
/**
* (Optional) List of documents to create the turn with.
*/
documents?: Array<TurnCreateParams.Document>;
/**
* (Optional) If True, generate an SSE event stream of the response. Defaults to
* False.
*/
stream?: boolean;
/**
* (Optional) The tool configuration to create the turn with, will be used to
* override the agent's tool_config.
*/
tool_config?: TurnCreateParams.ToolConfig;
/**
* (Optional) List of toolgroups to create the turn with, will be used in addition
* to the agent's config toolgroups for the request.
*/
toolgroups?: Array<string | TurnCreateParams.AgentToolGroupWithArgs>;
}
export namespace TurnCreateParams {
/**
* A document to be used by an agent.
*/
export interface Document {
/**
* The content of the document.
*/
content:
| string
| Document.ImageContentItem
| Document.TextContentItem
| Array<Shared.InterleavedContentItem>
| Document.URL;
/**
* The MIME type of the document.
*/
mime_type: string;
}
export namespace Document {
/**
* A image content item
*/
export interface ImageContentItem {
/**
* Image as a base64 encoded string or an URL
*/
image: ImageContentItem.Image;
/**
* Discriminator type of the content item. Always "image"
*/
type: 'image';
}
export namespace ImageContentItem {
/**
* Image as a base64 encoded string or an URL
*/
export interface Image {
/**
* base64 encoded image data as string
*/
data?: string;
/**
* A URL of the image or data URL in the format of data:image/{type};base64,{data}.
* Note that URL could have length limits.
*/
url?: Image.URL;
}
export namespace Image {
/**
* A URL of the image or data URL in the format of data:image/{type};base64,{data}.
* Note that URL could have length limits.
*/
export interface URL {
uri: string;
}
}
}
/**
* A text content item
*/
export interface TextContentItem {
/**
* Text content
*/
text: string;
/**
* Discriminator type of the content item. Always "text"
*/
type: 'text';
}
export interface URL {
uri: string;
}
}
/**
* (Optional) The tool configuration to create the turn with, will be used to
* override the agent's tool_config.
*/
export interface ToolConfig {
/**
* (Optional) Config for how to override the default system prompt. -
* `SystemMessageBehavior.append`: Appends the provided system message to the
* default system prompt. - `SystemMessageBehavior.replace`: Replaces the default
* system prompt with the provided system message. The system message can include
* the string '{{function_definitions}}' to indicate where the function definitions
* should be inserted.
*/
system_message_behavior?: 'append' | 'replace';
/**
* (Optional) Whether tool use is automatic, required, or none. Can also specify a
* tool name to use a specific tool. Defaults to ToolChoice.auto.
*/
tool_choice?: 'auto' | 'required' | 'none' | (string & {});
/**
* (Optional) Instructs the model how to format tool calls. By default, Llama Stack
* will attempt to use a format that is best adapted to the model. -
* `ToolPromptFormat.json`: The tool calls are formatted as a JSON object. -
* `ToolPromptFormat.function_tag`: The tool calls are enclosed in a
* <function=function_name> tag. - `ToolPromptFormat.python_list`: The tool calls
* are output as Python syntax -- a list of function calls.
*/
tool_prompt_format?: 'json' | 'function_tag' | 'python_list';
}
export interface AgentToolGroupWithArgs {
args: Record<string, boolean | number | string | Array<unknown> | unknown | null>;
name: string;
}
export type TurnCreateParamsNonStreaming = TurnAPI.TurnCreateParamsNonStreaming;
export type TurnCreateParamsStreaming = TurnAPI.TurnCreateParamsStreaming;
}
export interface TurnCreateParamsNonStreaming extends TurnCreateParamsBase {
/**
* (Optional) If True, generate an SSE event stream of the response. Defaults to
* False.
*/
stream?: false;
}
export interface TurnCreateParamsStreaming extends TurnCreateParamsBase {
/**
* (Optional) If True, generate an SSE event stream of the response. Defaults to
* False.
*/
stream: true;
}
export type TurnResumeParams = TurnResumeParamsNonStreaming | TurnResumeParamsStreaming;
export interface TurnResumeParamsBase {
/**
* The tool call responses to resume the turn with.
*/
tool_responses: Array<AgentsAPI.ToolResponse>;
/**
* Whether to stream the response.
*/
stream?: boolean;
}
export namespace TurnResumeParams {
export type TurnResumeParamsNonStreaming = TurnAPI.TurnResumeParamsNonStreaming;
export type TurnResumeParamsStreaming = TurnAPI.TurnResumeParamsStreaming;
}
export interface TurnResumeParamsNonStreaming extends TurnResumeParamsBase {
/**
* Whether to stream the response.
*/
stream?: false;
}
export interface TurnResumeParamsStreaming extends TurnResumeParamsBase {
/**
* Whether to stream the response.
*/
stream: true;
}
export declare namespace TurnResource {
export {
type AgentTurnResponseStreamChunk as AgentTurnResponseStreamChunk,
type Turn as Turn,
type TurnResponseEvent as TurnResponseEvent,
type TurnResponseEventPayload as TurnResponseEventPayload,
type TurnCreateParams as TurnCreateParams,
type TurnCreateParamsNonStreaming as TurnCreateParamsNonStreaming,
type TurnCreateParamsStreaming as TurnCreateParamsStreaming,
type TurnResumeParams as TurnResumeParams,
type TurnResumeParamsNonStreaming as TurnResumeParamsNonStreaming,
type TurnResumeParamsStreaming as TurnResumeParamsStreaming,
};
}