-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtask-group.ts
More file actions
315 lines (277 loc) · 9.07 KB
/
task-group.ts
File metadata and controls
315 lines (277 loc) · 9.07 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
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
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import { APIResource } from '../core/resource';
import * as TaskRunAPI from './task-run';
import * as BetaTaskRunAPI from './beta/task-run';
import { APIPromise } from '../core/api-promise';
import { Stream } from '../core/streaming';
import { buildHeaders } from '../internal/headers';
import { RequestOptions } from '../internal/request-options';
import { path } from '../internal/utils/path';
/**
* The Task API executes web research and extraction tasks. Clients submit a natural-language objective with an optional input schema; the service plans retrieval, fetches relevant URLs, and returns outputs that conform to a provided or inferred JSON schema. Supports deep research style queries and can return rich structured JSON outputs. Processors trade-off between cost, latency, and quality. Each processor supports calibrated confidences.
* - Output metadata: citations, excerpts, reasoning, and confidence per field
*
* Task Groups enable batch execution of many independent Task runs with group-level monitoring and failure handling.
* - Submit hundreds or thousands of Tasks as a single group
* - Observe group progress and receive results as they complete
* - Real-time updates via Server-Sent Events (SSE)
* - Add tasks to an existing group while it is running
* - Group-level retry and error aggregation
*/
export class TaskGroup extends APIResource {
/**
* Initiates a TaskGroup to group and track multiple runs.
*/
create(body: TaskGroupCreateParams, options?: RequestOptions): APIPromise<TaskGroup> {
return this._client.post('/v1/tasks/groups', { body, ...options });
}
/**
* Retrieves aggregated status across runs in a TaskGroup.
*/
retrieve(taskGroupID: string, options?: RequestOptions): APIPromise<TaskGroup> {
return this._client.get(path`/v1/tasks/groups/${taskGroupID}`, options);
}
/**
* Initiates multiple task runs within a TaskGroup.
*/
addRuns(
taskGroupID: string,
params: TaskGroupAddRunsParams,
options?: RequestOptions,
): APIPromise<TaskGroupRunResponse> {
const { refresh_status, betas, ...body } = params;
return this._client.post(path`/v1/tasks/groups/${taskGroupID}/runs`, {
query: { refresh_status },
body,
...options,
headers: buildHeaders([
{ ...(betas?.toString() != null ? { 'parallel-beta': betas?.toString() } : undefined) },
options?.headers,
]),
});
}
/**
* Streams events from a TaskGroup: status updates and run completions.
*
* The connection will remain open for up to an hour as long as at least one run in
* the group is still active.
*/
events(
taskGroupID: string,
query: TaskGroupEventsParams | undefined = {},
options?: RequestOptions,
): APIPromise<Stream<TaskGroupEventsResponse>> {
return this._client.get(path`/v1/tasks/groups/${taskGroupID}/events`, {
query,
...options,
headers: buildHeaders([{ Accept: 'text/event-stream' }, options?.headers]),
stream: true,
}) as APIPromise<Stream<TaskGroupEventsResponse>>;
}
/**
* Retrieves task runs in a TaskGroup and optionally their inputs and outputs.
*
* All runs within a TaskGroup are returned as a stream. To get the inputs and/or
* outputs back in the stream, set the corresponding `include_input` and
* `include_output` parameters to `true`.
*
* The stream is resumable using the `event_id` as the cursor. To resume a stream,
* specify the `last_event_id` parameter with the `event_id` of the last event in
* the stream. The stream will resume from the next event after the
* `last_event_id`.
*/
getRuns(
taskGroupID: string,
query: TaskGroupGetRunsParams | undefined = {},
options?: RequestOptions,
): APIPromise<Stream<TaskGroupGetRunsResponse>> {
return this._client.get(path`/v1/tasks/groups/${taskGroupID}/runs`, {
query,
...options,
headers: buildHeaders([{ Accept: 'text/event-stream' }, options?.headers]),
stream: true,
}) as APIPromise<Stream<TaskGroupGetRunsResponse>>;
}
/**
* Retrieves run status by run_id.
*
* This endpoint is equivalent to fetching run status directly using the
* `retrieve()` method or the `tasks/runs` GET endpoint.
*
* The run result is available from the `/result` endpoint.
*/
retrieveRun(
runID: string,
params: TaskGroupRetrieveRunParams,
options?: RequestOptions,
): APIPromise<TaskRunAPI.TaskRun> {
const { taskgroup_id } = params;
return this._client.get(path`/v1/tasks/groups/${taskgroup_id}/runs/${runID}`, options);
}
}
/**
* Response object for a task group, including its status and metadata.
*/
export interface TaskGroup {
/**
* Timestamp of the creation of the group, as an RFC 3339 string.
*/
created_at: string | null;
/**
* Status of the group.
*/
status: TaskGroupStatus;
/**
* ID of the group.
*/
taskgroup_id: string;
/**
* User-provided metadata stored with the group.
*/
metadata?: { [key: string]: string | number | boolean } | null;
}
/**
* Response from adding new task runs to a task group.
*/
export interface TaskGroupRunResponse {
/**
* Cursor for these runs in the event stream at
* taskgroup/events?last_event_id=<event_cursor>. Empty for the first runs in the
* group.
*/
event_cursor: string | null;
/**
* Cursor for these runs in the run stream at
* taskgroup/runs?last_event_id=<run_cursor>. Empty for the first runs in the
* group.
*/
run_cursor: string | null;
/**
* IDs of the newly created runs.
*/
run_ids: Array<string>;
/**
* Status of the group.
*/
status: TaskGroupStatus;
}
/**
* Status of a task group.
*/
export interface TaskGroupStatus {
/**
* True if at least one run in the group is currently active, i.e. status is one of
* {'cancelling', 'queued', 'running'}.
*/
is_active: boolean;
/**
* Timestamp of the last status update to the group, as an RFC 3339 string.
*/
modified_at: string | null;
/**
* Number of task runs in the group.
*/
num_task_runs: number;
/**
* Human-readable status message for the group.
*/
status_message: string | null;
/**
* Number of task runs with each status.
*/
task_run_status_counts: { [key: string]: number };
}
/**
* Event indicating an update to group status.
*/
export interface TaskGroupStatusEvent {
/**
* Cursor to resume the event stream.
*/
event_id: string;
/**
* Task group status object.
*/
status: TaskGroupStatus;
/**
* Event type; always 'task_group_status'.
*/
type: 'task_group_status';
}
/**
* Event indicating an update to group status.
*/
export type TaskGroupEventsResponse = TaskGroupStatusEvent | TaskRunAPI.TaskRunEvent | TaskRunAPI.ErrorEvent;
/**
* Event when a task run transitions to a non-active status.
*
* May indicate completion, cancellation, or failure.
*/
export type TaskGroupGetRunsResponse = TaskRunAPI.TaskRunEvent | TaskRunAPI.ErrorEvent;
export interface TaskGroupCreateParams {
/**
* User-provided metadata stored with the task group.
*/
metadata?: { [key: string]: string | number | boolean } | null;
}
export interface TaskGroupAddRunsParams {
/**
* Body param: List of task runs to execute. Up to 1,000 runs can be specified per
* request. If you'd like to add more runs, split them across multiple TaskGroup
* POST requests.
*/
inputs: Array<TaskRunAPI.RunInput>;
/**
* Query param
*/
refresh_status?: boolean;
/**
* Body param: Specification for a task.
*
* Auto output schemas can be specified by setting `output_schema={"type":"auto"}`.
* Not specifying a TaskSpec is the same as setting an auto output schema.
*
* For convenience bare strings are also accepted as input or output schemas.
*/
default_task_spec?: TaskRunAPI.TaskSpec | null;
/**
* Header param: Optional header to specify the beta version(s) to enable.
*/
betas?: Array<BetaTaskRunAPI.ParallelBeta>;
}
export interface TaskGroupEventsParams {
last_event_id?: string | null;
timeout?: number | null;
}
export interface TaskGroupGetRunsParams {
include_input?: boolean;
include_output?: boolean;
last_event_id?: string | null;
status?:
| 'queued'
| 'action_required'
| 'running'
| 'completed'
| 'failed'
| 'cancelling'
| 'cancelled'
| null;
}
export interface TaskGroupRetrieveRunParams {
taskgroup_id: string;
}
export declare namespace TaskGroup {
export {
type TaskGroup as TaskGroup,
type TaskGroupRunResponse as TaskGroupRunResponse,
type TaskGroupStatus as TaskGroupStatus,
type TaskGroupStatusEvent as TaskGroupStatusEvent,
type TaskGroupEventsResponse as TaskGroupEventsResponse,
type TaskGroupGetRunsResponse as TaskGroupGetRunsResponse,
type TaskGroupCreateParams as TaskGroupCreateParams,
type TaskGroupAddRunsParams as TaskGroupAddRunsParams,
type TaskGroupEventsParams as TaskGroupEventsParams,
type TaskGroupGetRunsParams as TaskGroupGetRunsParams,
type TaskGroupRetrieveRunParams as TaskGroupRetrieveRunParams,
};
}