|
1 | 1 | import * as BrowserUse from "../../api/index.js";
|
2 | 2 | import * as core from "../../core/index.js";
|
3 | 3 | import { Tasks } from "../../api/resources/tasks/client/Client.js";
|
| 4 | +import { |
| 5 | + type CreateTaskRequestWithSchema, |
| 6 | + type WrappedTaskResponse, |
| 7 | + type TaskViewWithSchema, |
| 8 | + stringifyStructuredOutput, |
| 9 | + wrapCreateTaskResponse, |
| 10 | + parseStructuredTaskOutput, |
| 11 | +} from "wrapper/lib/parse.js"; |
| 12 | +import { ZodType } from "zod"; |
4 | 13 |
|
5 | 14 | export class BrowserUseTasks extends Tasks {
|
6 | 15 | constructor(options: Tasks.Options) {
|
7 | 16 | super(options);
|
8 | 17 | }
|
9 | 18 |
|
| 19 | + public createTask<T extends ZodType>( |
| 20 | + request: CreateTaskRequestWithSchema<T>, |
| 21 | + requestOptions?: Tasks.RequestOptions, |
| 22 | + ): core.HttpResponsePromise<WrappedTaskResponse<T>>; |
10 | 23 | public createTask(
|
11 | 24 | request: BrowserUse.CreateTaskRequest,
|
12 | 25 | requestOptions?: Tasks.RequestOptions,
|
13 |
| - ): core.HttpResponsePromise<BrowserUse.TaskCreatedResponse> { |
14 |
| - return super.createTask(request, requestOptions); |
| 26 | + ): core.HttpResponsePromise<WrappedTaskResponse<null>>; |
| 27 | + public createTask( |
| 28 | + request: BrowserUse.CreateTaskRequest | CreateTaskRequestWithSchema<ZodType>, |
| 29 | + requestOptions?: Tasks.RequestOptions, |
| 30 | + ): core.HttpResponsePromise<WrappedTaskResponse<ZodType>> | core.HttpResponsePromise<WrappedTaskResponse<null>> { |
| 31 | + if ("schema" in request) { |
| 32 | + const req: BrowserUse.CreateTaskRequest = { |
| 33 | + ...request, |
| 34 | + structuredOutput: stringifyStructuredOutput(request.schema), |
| 35 | + }; |
| 36 | + |
| 37 | + const promise: Promise<core.WithRawResponse<WrappedTaskResponse<ZodType>>> = super |
| 38 | + .createTask(req, requestOptions) |
| 39 | + .withRawResponse() |
| 40 | + .then((res) => { |
| 41 | + const wrapped = wrapCreateTaskResponse(this, res.data, request.schema); |
| 42 | + |
| 43 | + return { |
| 44 | + data: wrapped, |
| 45 | + rawResponse: res.rawResponse, |
| 46 | + }; |
| 47 | + }); |
| 48 | + |
| 49 | + return core.HttpResponsePromise.fromPromise(promise); |
| 50 | + } |
| 51 | + |
| 52 | + const promise: Promise<core.WithRawResponse<WrappedTaskResponse<null>>> = super |
| 53 | + .createTask(request, requestOptions) |
| 54 | + .withRawResponse() |
| 55 | + .then((res) => { |
| 56 | + const wrapped = wrapCreateTaskResponse(this, res.data, null); |
| 57 | + |
| 58 | + return { |
| 59 | + data: wrapped, |
| 60 | + rawResponse: res.rawResponse, |
| 61 | + }; |
| 62 | + }); |
| 63 | + |
| 64 | + return core.HttpResponsePromise.fromPromise(promise); |
15 | 65 | }
|
16 | 66 |
|
17 |
| - /** |
18 |
| - * Get detailed information about a specific AI agent task. |
19 |
| - * |
20 |
| - * Retrieves comprehensive information about a task, including its current status, |
21 |
| - * progress, and detailed execution data. You can choose to get just the status |
22 |
| - * (for quick polling) or full details including steps and file information. |
23 |
| - * |
24 |
| - * Use this endpoint to: |
25 |
| - * |
26 |
| - * - Monitor task progress in real-time |
27 |
| - * - Review completed task results |
28 |
| - * - Debug failed tasks by examining steps |
29 |
| - * - Download output files and logs |
30 |
| - * |
31 |
| - * Args: |
32 |
| - * |
33 |
| - * - task_id: The unique identifier of the agent task |
34 |
| - * |
35 |
| - * Returns: |
36 |
| - * |
37 |
| - * - Complete task information |
38 |
| - * |
39 |
| - * Raises: |
40 |
| - * |
41 |
| - * - 404: If the user agent task doesn't exist |
42 |
| - */ |
43 |
| - retrieve<T extends ZodType>( |
44 |
| - req: { taskId: string; schema: T }, |
45 |
| - options?: RequestOptions, |
46 |
| - ): APIPromise<TaskViewWithSchema<T>>; |
47 |
| - retrieve(taskID: string, options?: RequestOptions): APIPromise<TaskView>; |
48 |
| - retrieve(req: string | { taskId: string; schema: ZodType }, options?: RequestOptions): APIPromise<unknown> { |
49 |
| - if (typeof req === "string") { |
50 |
| - return this._client.get(path`/tasks/${req}`, options); |
| 67 | + public getTask<T extends ZodType>( |
| 68 | + request: { taskId: string; schema: T }, |
| 69 | + requestOptions?: Tasks.RequestOptions, |
| 70 | + ): core.HttpResponsePromise<TaskViewWithSchema<T>>; |
| 71 | + public getTask( |
| 72 | + taskId: string, |
| 73 | + requestOptions?: Tasks.RequestOptions, |
| 74 | + ): core.HttpResponsePromise<BrowserUse.TaskView>; |
| 75 | + public getTask( |
| 76 | + request: { taskId: string; schema: ZodType } | string, |
| 77 | + requestOptions?: Tasks.RequestOptions, |
| 78 | + ): core.HttpResponsePromise<BrowserUse.TaskView | TaskViewWithSchema<ZodType>> { |
| 79 | + if (typeof request === "string") { |
| 80 | + return super.getTask(request, requestOptions); |
51 | 81 | }
|
52 | 82 |
|
53 |
| - const { taskId, schema } = req; |
| 83 | + const { taskId, schema } = request; |
| 84 | + |
| 85 | + const promise: Promise<core.WithRawResponse<TaskViewWithSchema<ZodType>>> = super |
| 86 | + .getTask(taskId, requestOptions) |
| 87 | + .withRawResponse() |
| 88 | + .then((res) => { |
| 89 | + const parsed = parseStructuredTaskOutput(res.data, schema); |
| 90 | + |
| 91 | + return { |
| 92 | + data: parsed, |
| 93 | + rawResponse: res.rawResponse, |
| 94 | + }; |
| 95 | + }); |
54 | 96 |
|
55 |
| - return this._client |
56 |
| - .get(path`/tasks/${taskId}`, options) |
57 |
| - ._thenUnwrap((rsp) => parseStructuredTaskOutput(rsp as TaskView, schema)); |
| 97 | + return core.HttpResponsePromise.fromPromise(promise); |
58 | 98 | }
|
59 | 99 | }
|
0 commit comments