-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.ts
More file actions
288 lines (243 loc) · 6.78 KB
/
agent.ts
File metadata and controls
288 lines (243 loc) · 6.78 KB
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
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import { APIResource } from '../core/resource';
import { APIPromise } from '../core/api-promise';
import { Stream } from '../core/streaming';
import { buildHeaders } from '../internal/headers';
import { RequestOptions } from '../internal/request-options';
export class Agent extends APIResource {
/**
* Run an agent on a task or message.
* A new session can be created by omitting the `session` query parameter, or an
* existing session can be continued by specifying the session ID in the `session`
* query parameter.
* The request body includes the message, optional system prompt, mode, MCP server
* configuration, optional rules and whether the response should be streamed.
* The response is a streaming response and returns a sequence of events
* representing the agent’s thoughts and responses.
*/
run(params: AgentRunParams, options?: RequestOptions): APIPromise<Stream<AgentRunResponse>> {
const { session, ...body } = params;
return this._client.post('/', {
query: { session },
body,
...options,
headers: buildHeaders([{ Accept: 'text/event-stream' }, options?.headers]),
stream: true,
}) as APIPromise<Stream<AgentRunResponse>>;
}
}
export type AgentRunResponse = string;
export interface AgentRunParams {
/**
* Body param: The task or message to run the agent with.
*/
message: string;
/**
* Query param: The session ID to continue the agent session conversation. If not
* provided, a new session will be created.
*/
session?: string;
/**
* Body param: A set of agent configurations that enables the agent to transfer
* conversations to other specialized agents. When provided, the main agent will
* have access to seamless handoffs between agents based on the conversation
* context.
*/
agents?: Array<AgentRunParams.Agent>;
/**
* Body param: Whether to run the agent asynchronously on the server. When set to
* true, use callback parameter to receive events.
*/
background?: boolean;
/**
* Body param: A callback endpoint configuration to send agent message events back
* to. Use with background true.
*/
callback?: AgentRunParams.Callback;
/**
* Body param: A set of datastores for the agent to utilize. Each object must
* include a `id` and `name`.
*/
datastores?: Array<AgentRunParams.Datastore>;
/**
* Body param: Configuration for an extra final output event that processes the
* entire agent message thread and produces a structured output based on the
* provided JSON schema.
*/
final_output?: AgentRunParams.FinalOutput;
/**
* Body param: A list of MCP server configurations. Each object must include a
* `serverName` and `serverUrl`.
*/
mcp_servers?: Array<AgentRunParams.McpServer>;
/**
* Body param: The agent mode. Allowed values are `flash`, `fast` or `max`.
* Defaults to `fast` if not supplied.
*/
mode?: 'flash' | 'fast' | 'max';
/**
* Body param: A set of custom actions based on datastore (database) queries.
* Allows you to quickly define actions that the agent can use to query your
* datastores.
*/
queries?: Array<AgentRunParams.Query>;
/**
* Body param: A list of constraints that the agent must follow.
*/
rules?: Array<string>;
/**
* Body param: Whether to stream the agent messages token by token.
*/
streaming_tokens?: boolean;
/**
* Body param: A system prompt to provide system information to the agent.
*/
system?: string;
/**
* Body param: A set of declarative workflows for the agent to execute. Each
* workflow is a DAG (Directed Acyclic Graph) of steps that the agent interprets
* and executes dynamically.
*/
workflows?: Array<AgentRunParams.Workflow>;
}
export namespace AgentRunParams {
export interface Agent {
/**
* Description of what this agent handles
*/
description: string;
/**
* The name of the agent to transfer to
*/
name: string;
}
/**
* A callback endpoint configuration to send agent message events back to. Use with
* background true.
*/
export interface Callback {
/**
* The webhook URL to send events to.
*/
url: string;
/**
* Optional headers to include in the callback request.
*/
headers?: { [key: string]: string };
}
export interface Datastore {
/**
* The ID of the datastore.
*/
id: string;
/**
* The name of the datastore.
*/
name: string;
}
/**
* Configuration for an extra final output event that processes the entire agent
* message thread and produces a structured output based on the provided JSON
* schema.
*/
export interface FinalOutput {
/**
* Name for the final output.
*/
name: string;
/**
* JSON schema defining the structure of the final output.
*/
schema: unknown;
/**
* Whether to enforce strict schema validation.
*/
strict?: boolean;
}
export interface McpServer {
/**
* Name of the MCP server.
*/
serverName: string;
/**
* URL of the MCP server.
*/
serverUrl: string;
}
export interface Query {
/**
* Description of what the query does.
*/
description: string;
/**
* Name of the query action.
*/
name: string;
/**
* The SQL query to execute.
*/
query: string;
}
export interface Workflow {
/**
* Unique identifier for the workflow.
*/
id: string;
/**
* What the workflow accomplishes.
*/
description: string;
/**
* Name of the workflow.
*/
name: string;
/**
* Array of step objects.
*/
steps: Array<Workflow.Step>;
}
export namespace Workflow {
export interface Step {
/**
* Unique identifier for the step.
*/
id: string;
/**
* Array of step IDs that must complete before this step runs.
*/
depends_on: Array<string>;
/**
* What the step should accomplish.
*/
description: string;
/**
* Name of the step.
*/
name: string;
/**
* Whether the step can be skipped if it fails.
*/
optional?: boolean;
/**
* JSON schema for expected output validation.
*/
output_schema?: unknown;
/**
* Retry configuration for the step.
*/
retry_policy?: Step.RetryPolicy;
}
export namespace Step {
/**
* Retry configuration for the step.
*/
export interface RetryPolicy {
backoff?: string;
max_attempts?: number;
}
}
}
}
export declare namespace Agent {
export { type AgentRunResponse as AgentRunResponse, type AgentRunParams as AgentRunParams };
}