Repository navigation
Expand file tree
/
Copy pathcua.ts
More file actions
128 lines (120 loc) · 3.68 KB
/
Copy pathcua.ts
File metadata and controls
128 lines (120 loc) · 3.68 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
import { HyperbrowserError } from "../../client";
import { BasicResponse } from "../../types";
import { POLLING_ATTEMPTS } from "../../types/constants";
import { sleep } from "../../utils";
import { BaseService } from "../base";
import { AgentTaskListParams, AgentTaskListResponse } from "../../types/agents/task";
import {
CuaTaskResponse,
CuaTaskStatusResponse,
StartCuaTaskParams,
StartCuaTaskResponse,
} from "../../types/agents/cua";
export class CuaService extends BaseService {
/**
* List task jobs
* @param params Optional filters and pagination
*/
async list(params: AgentTaskListParams = {}): Promise<AgentTaskListResponse> {
try {
return await this.request<AgentTaskListResponse>("/task/cua", undefined, {
task: params.task,
page: params.page,
limit: params.limit,
});
} catch (error) {
if (error instanceof HyperbrowserError) {
throw error;
}
throw new HyperbrowserError("Failed to list cua task jobs", undefined);
}
}
/**
* Start a new CUA task job
* @param params The parameters for the task job
*/
async start(params: StartCuaTaskParams): Promise<StartCuaTaskResponse> {
try {
return await this.request<StartCuaTaskResponse>("/task/cua", {
method: "POST",
body: JSON.stringify(params),
});
} catch (error) {
if (error instanceof HyperbrowserError) {
throw error;
}
throw new HyperbrowserError("Failed to start CUA task job", undefined);
}
}
/**
* Get the status of a CUA task job
* @param id The ID of the task job to get
*/
async getStatus(id: string): Promise<CuaTaskStatusResponse> {
try {
return await this.request<CuaTaskStatusResponse>(`/task/cua/${id}/status`);
} catch (error) {
if (error instanceof HyperbrowserError) {
throw error;
}
throw new HyperbrowserError(`Failed to get CUA task job ${id} status`, undefined);
}
}
/**
* Get the result of a CUA task job
* @param id The ID of the task job to get
*/
async get(id: string): Promise<CuaTaskResponse> {
try {
return await this.request<CuaTaskResponse>(`/task/cua/${id}`);
} catch (error) {
if (error instanceof HyperbrowserError) {
throw error;
}
throw new HyperbrowserError(`Failed to get CUA task job ${id}`, undefined);
}
}
/**
* Stop a CUA task job
* @param id The ID of the task job to stop
*/
async stop(id: string): Promise<BasicResponse> {
try {
return await this.request<BasicResponse>(`/task/cua/${id}/stop`, { method: "PUT" });
} catch (error) {
if (error instanceof HyperbrowserError) {
throw error;
}
throw new HyperbrowserError(`Failed to stop CUA task job ${id}`, undefined);
}
}
/**
* Start a CUA task job and wait for it to complete
* @param params The parameters for the task job
*/
async startAndWait(params: StartCuaTaskParams): Promise<CuaTaskResponse> {
const job = await this.start(params);
const jobId = job.jobId;
if (!jobId) {
throw new HyperbrowserError("Failed to start CUA task job, could not get job ID");
}
let failures = 0;
while (true) {
try {
const { status } = await this.getStatus(jobId);
if (status === "completed" || status === "failed" || status === "stopped") {
return await this.get(jobId);
}
failures = 0;
} catch (error) {
failures++;
if (failures >= POLLING_ATTEMPTS) {
throw new HyperbrowserError(
`Failed to poll CUA task job ${jobId} after ${POLLING_ATTEMPTS} attempts: ${error}`
);
}
}
await sleep(2000);
}
}
}