roe-typescript 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +189 -0
- package/dist/api/agents.d.ts +87 -0
- package/dist/api/agents.js +263 -0
- package/dist/auth.d.ts +6 -0
- package/dist/auth.js +11 -0
- package/dist/client.d.ts +13 -0
- package/dist/client.js +18 -0
- package/dist/config.d.ts +17 -0
- package/dist/config.js +43 -0
- package/dist/exceptions.d.ts +25 -0
- package/dist/exceptions.js +73 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +9 -0
- package/dist/integration_test.js +686 -0
- package/dist/models/agent.d.ts +78 -0
- package/dist/models/agent.js +40 -0
- package/dist/models/file.d.ts +30 -0
- package/dist/models/file.js +85 -0
- package/dist/models/job.d.ts +37 -0
- package/dist/models/job.js +133 -0
- package/dist/models/responses.d.ts +71 -0
- package/dist/models/responses.js +36 -0
- package/dist/models/user.d.ts +6 -0
- package/dist/models/user.js +1 -0
- package/dist/src/api/agents.js +269 -0
- package/dist/src/auth.js +15 -0
- package/dist/src/client.js +22 -0
- package/dist/src/config.js +47 -0
- package/dist/src/exceptions.js +86 -0
- package/dist/src/models/agent.js +45 -0
- package/dist/src/models/file.js +92 -0
- package/dist/src/models/job.js +138 -0
- package/dist/src/models/responses.js +42 -0
- package/dist/src/models/user.js +2 -0
- package/dist/src/utils/fileDetection.js +46 -0
- package/dist/src/utils/httpClient.js +236 -0
- package/dist/src/utils/pagination.js +18 -0
- package/dist/utils/fileDetection.d.ts +11 -0
- package/dist/utils/fileDetection.js +38 -0
- package/dist/utils/httpClient.d.ts +30 -0
- package/dist/utils/httpClient.js +229 -0
- package/dist/utils/pagination.d.ts +3 -0
- package/dist/utils/pagination.js +14 -0
- package/package.json +36 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AgentsAPI = exports.AgentJobsAPI = exports.AgentVersionsAPI = void 0;
|
|
4
|
+
const pagination_1 = require("../utils/pagination");
|
|
5
|
+
const fileDetection_1 = require("../utils/fileDetection");
|
|
6
|
+
const exceptions_1 = require("../exceptions");
|
|
7
|
+
const agent_1 = require("../models/agent");
|
|
8
|
+
const job_1 = require("../models/job");
|
|
9
|
+
/**
|
|
10
|
+
* Validates that a value is a valid UUID string.
|
|
11
|
+
* @throws BadRequestError if the value is not a valid UUID
|
|
12
|
+
*/
|
|
13
|
+
function validateUuid(value, fieldName) {
|
|
14
|
+
if (!(0, fileDetection_1.isUuidString)(value)) {
|
|
15
|
+
throw new exceptions_1.BadRequestError(`Invalid ${fieldName}: "${value}" is not a valid UUID`, 400);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
class AgentVersionsAPI {
|
|
19
|
+
constructor(agentsApi) {
|
|
20
|
+
this.agentsApi = agentsApi;
|
|
21
|
+
}
|
|
22
|
+
get http() {
|
|
23
|
+
return this.agentsApi.httpClient;
|
|
24
|
+
}
|
|
25
|
+
async list(agentId) {
|
|
26
|
+
validateUuid(agentId, "agentId");
|
|
27
|
+
const data = await this.http.get(`/v1/agents/${agentId}/versions/`);
|
|
28
|
+
return data.map((v) => this.wrap(v));
|
|
29
|
+
}
|
|
30
|
+
async retrieve(agentId, versionId, getSupportsEval) {
|
|
31
|
+
validateUuid(agentId, "agentId");
|
|
32
|
+
validateUuid(versionId, "versionId");
|
|
33
|
+
const params = getSupportsEval === undefined ? undefined : { get_supports_eval: String(getSupportsEval).toLowerCase() };
|
|
34
|
+
const data = await this.http.get(`/v1/agents/${agentId}/versions/${versionId}/`, params);
|
|
35
|
+
return this.wrap(data);
|
|
36
|
+
}
|
|
37
|
+
async retrieveCurrent(agentId) {
|
|
38
|
+
validateUuid(agentId, "agentId");
|
|
39
|
+
const data = await this.http.get(`/v1/agents/${agentId}/versions/current/`);
|
|
40
|
+
return this.wrap(data);
|
|
41
|
+
}
|
|
42
|
+
async create(params) {
|
|
43
|
+
validateUuid(params.agentId, "agentId");
|
|
44
|
+
const payload = {
|
|
45
|
+
input_definitions: params.inputDefinitions ?? [],
|
|
46
|
+
engine_config: params.engineConfig ?? {},
|
|
47
|
+
};
|
|
48
|
+
if (params.versionName !== undefined)
|
|
49
|
+
payload.version_name = params.versionName;
|
|
50
|
+
if (params.description !== undefined)
|
|
51
|
+
payload.description = params.description;
|
|
52
|
+
const data = await this.http.post({
|
|
53
|
+
url: `/v1/agents/${params.agentId}/versions/`,
|
|
54
|
+
json: payload,
|
|
55
|
+
});
|
|
56
|
+
return this.retrieve(params.agentId, data.id);
|
|
57
|
+
}
|
|
58
|
+
async update(agentId, versionId, updates) {
|
|
59
|
+
validateUuid(agentId, "agentId");
|
|
60
|
+
validateUuid(versionId, "versionId");
|
|
61
|
+
const payload = {};
|
|
62
|
+
if (updates.versionName !== undefined)
|
|
63
|
+
payload.version_name = updates.versionName;
|
|
64
|
+
if (updates.description !== undefined)
|
|
65
|
+
payload.description = updates.description;
|
|
66
|
+
await this.http.put(`/v1/agents/${agentId}/versions/${versionId}/`, payload);
|
|
67
|
+
}
|
|
68
|
+
async delete(agentId, versionId) {
|
|
69
|
+
validateUuid(agentId, "agentId");
|
|
70
|
+
validateUuid(versionId, "versionId");
|
|
71
|
+
await this.http.delete(`/v1/agents/${agentId}/versions/${versionId}/`);
|
|
72
|
+
}
|
|
73
|
+
wrap(data) {
|
|
74
|
+
const obj = new agent_1.AgentVersionWithApi(data);
|
|
75
|
+
obj.setAgentsApi(this.agentsApi);
|
|
76
|
+
return obj;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.AgentVersionsAPI = AgentVersionsAPI;
|
|
80
|
+
class AgentJobsAPI {
|
|
81
|
+
constructor(agentsApi) {
|
|
82
|
+
this.agentsApi = agentsApi;
|
|
83
|
+
}
|
|
84
|
+
get http() {
|
|
85
|
+
return this.agentsApi.httpClient;
|
|
86
|
+
}
|
|
87
|
+
async retrieveStatus(jobId) {
|
|
88
|
+
validateUuid(jobId, "jobId");
|
|
89
|
+
return this.http.get(`/v1/agents/jobs/${jobId}/status/`);
|
|
90
|
+
}
|
|
91
|
+
async retrieveResult(jobId) {
|
|
92
|
+
validateUuid(jobId, "jobId");
|
|
93
|
+
return this.http.get(`/v1/agents/jobs/${jobId}/result/`);
|
|
94
|
+
}
|
|
95
|
+
async retrieveStatusMany(jobIds) {
|
|
96
|
+
const results = [];
|
|
97
|
+
for (const chunk of this.iterChunks(jobIds, AgentJobsAPI.MAX_BATCH_SIZE)) {
|
|
98
|
+
if (!chunk.length)
|
|
99
|
+
continue;
|
|
100
|
+
const data = await this.http.post({
|
|
101
|
+
url: "/v1/agents/jobs/statuses/",
|
|
102
|
+
json: { job_ids: chunk },
|
|
103
|
+
});
|
|
104
|
+
results.push(...data);
|
|
105
|
+
}
|
|
106
|
+
return results;
|
|
107
|
+
}
|
|
108
|
+
async retrieveResultMany(jobIds) {
|
|
109
|
+
const results = [];
|
|
110
|
+
for (const chunk of this.iterChunks(jobIds, AgentJobsAPI.MAX_BATCH_SIZE)) {
|
|
111
|
+
if (!chunk.length)
|
|
112
|
+
continue;
|
|
113
|
+
const data = await this.http.post({
|
|
114
|
+
url: "/v1/agents/jobs/results/",
|
|
115
|
+
json: { job_ids: chunk },
|
|
116
|
+
});
|
|
117
|
+
results.push(...data);
|
|
118
|
+
}
|
|
119
|
+
return results;
|
|
120
|
+
}
|
|
121
|
+
async downloadReference(jobId, resourceId, asAttachment = false) {
|
|
122
|
+
validateUuid(jobId, "jobId");
|
|
123
|
+
validateUuid(resourceId, "resourceId");
|
|
124
|
+
const params = asAttachment ? { download: "true" } : undefined;
|
|
125
|
+
return this.http.getBytes(`/v1/agents/jobs/${jobId}/references/${resourceId}/`, params);
|
|
126
|
+
}
|
|
127
|
+
async deleteData(jobId) {
|
|
128
|
+
validateUuid(jobId, "jobId");
|
|
129
|
+
return this.http.post({
|
|
130
|
+
url: `/v1/agents/jobs/${jobId}/delete-data/`,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
*iterChunks(items, size) {
|
|
134
|
+
for (let i = 0; i < items.length; i += size) {
|
|
135
|
+
yield items.slice(i, i + size);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
exports.AgentJobsAPI = AgentJobsAPI;
|
|
140
|
+
AgentJobsAPI.MAX_BATCH_SIZE = 1000;
|
|
141
|
+
class AgentsAPI {
|
|
142
|
+
constructor(config, httpClient) {
|
|
143
|
+
this.config = config;
|
|
144
|
+
this.httpClient = httpClient;
|
|
145
|
+
this.versions = new AgentVersionsAPI(this);
|
|
146
|
+
this.jobs = new AgentJobsAPI(this);
|
|
147
|
+
}
|
|
148
|
+
wrapBase(data) {
|
|
149
|
+
const obj = new agent_1.BaseAgentWithApi(data);
|
|
150
|
+
obj.setAgentsApi(this);
|
|
151
|
+
return obj;
|
|
152
|
+
}
|
|
153
|
+
async list(params) {
|
|
154
|
+
const query = pagination_1.PaginationHelper.buildQueryParams(this.config.organizationId, params?.page, params?.pageSize);
|
|
155
|
+
const resp = await this.httpClient.get("/v1/agents/", query);
|
|
156
|
+
const wrapped = resp.results.map((a) => this.wrapBase(a));
|
|
157
|
+
return { ...resp, results: wrapped };
|
|
158
|
+
}
|
|
159
|
+
async retrieve(agentId) {
|
|
160
|
+
validateUuid(agentId, "agentId");
|
|
161
|
+
const data = await this.httpClient.get(`/v1/agents/${agentId}/`);
|
|
162
|
+
return this.wrapBase(data);
|
|
163
|
+
}
|
|
164
|
+
async create(params) {
|
|
165
|
+
const payload = {
|
|
166
|
+
name: params.name,
|
|
167
|
+
engine_class_id: params.engineClassId,
|
|
168
|
+
organization_id: this.config.organizationId,
|
|
169
|
+
input_definitions: params.inputDefinitions ?? [],
|
|
170
|
+
engine_config: params.engineConfig ?? {},
|
|
171
|
+
};
|
|
172
|
+
if (params.versionName !== undefined)
|
|
173
|
+
payload.version_name = params.versionName;
|
|
174
|
+
if (params.description !== undefined)
|
|
175
|
+
payload.description = params.description;
|
|
176
|
+
const data = await this.httpClient.post({ url: "/v1/agents/", json: payload });
|
|
177
|
+
return this.wrapBase(data);
|
|
178
|
+
}
|
|
179
|
+
async update(agentId, updates) {
|
|
180
|
+
validateUuid(agentId, "agentId");
|
|
181
|
+
const payload = {};
|
|
182
|
+
if (updates.name !== undefined)
|
|
183
|
+
payload.name = updates.name;
|
|
184
|
+
if (updates.disableCache !== undefined)
|
|
185
|
+
payload.disable_cache = updates.disableCache;
|
|
186
|
+
if (updates.cacheFailedJobs !== undefined)
|
|
187
|
+
payload.cache_failed_jobs = updates.cacheFailedJobs;
|
|
188
|
+
const data = await this.httpClient.put(`/v1/agents/${agentId}/`, payload);
|
|
189
|
+
return this.wrapBase(data);
|
|
190
|
+
}
|
|
191
|
+
async delete(agentId) {
|
|
192
|
+
validateUuid(agentId, "agentId");
|
|
193
|
+
await this.httpClient.delete(`/v1/agents/${agentId}/`);
|
|
194
|
+
}
|
|
195
|
+
async duplicate(agentId) {
|
|
196
|
+
validateUuid(agentId, "agentId");
|
|
197
|
+
const data = await this.httpClient.post({
|
|
198
|
+
url: `/v1/agents/${agentId}/duplicate/`,
|
|
199
|
+
});
|
|
200
|
+
return this.wrapBase(data.base_agent);
|
|
201
|
+
}
|
|
202
|
+
async run(params) {
|
|
203
|
+
validateUuid(params.agentId, "agentId");
|
|
204
|
+
const response = await this.httpClient.postWithDynamicInputs(`/v1/agents/run/${params.agentId}/async/`, params.inputs);
|
|
205
|
+
// Handle both string response and object response formats
|
|
206
|
+
const jobId = typeof response === "string" ? response : response?.job_id;
|
|
207
|
+
if (!jobId || typeof jobId !== "string") {
|
|
208
|
+
throw new exceptions_1.RoeAPIException(`Invalid job ID returned from API: ${JSON.stringify(response)}`);
|
|
209
|
+
}
|
|
210
|
+
if (!(0, fileDetection_1.isUuidString)(jobId)) {
|
|
211
|
+
throw new exceptions_1.RoeAPIException(`Invalid job ID format returned from API: ${jobId}`);
|
|
212
|
+
}
|
|
213
|
+
return new job_1.Job({ agentsApi: this, jobId, timeoutSeconds: params.timeoutSeconds });
|
|
214
|
+
}
|
|
215
|
+
async runMany(params) {
|
|
216
|
+
validateUuid(params.agentId, "agentId");
|
|
217
|
+
const allJobIds = [];
|
|
218
|
+
for (const chunk of this.iterChunks(params.batchInputs, AgentsAPI.MAX_BATCH_SIZE)) {
|
|
219
|
+
if (!chunk.length)
|
|
220
|
+
continue;
|
|
221
|
+
const resp = await this.httpClient.post({
|
|
222
|
+
url: `/v1/agents/run/${params.agentId}/async/many/`,
|
|
223
|
+
json: { inputs: chunk },
|
|
224
|
+
});
|
|
225
|
+
// Validate each returned job ID
|
|
226
|
+
for (const jobId of resp) {
|
|
227
|
+
if (!jobId || typeof jobId !== "string") {
|
|
228
|
+
throw new exceptions_1.RoeAPIException(`Invalid job ID returned from batch API: ${JSON.stringify(jobId)}`);
|
|
229
|
+
}
|
|
230
|
+
if (!(0, fileDetection_1.isUuidString)(jobId)) {
|
|
231
|
+
throw new exceptions_1.RoeAPIException(`Invalid job ID format returned from batch API: ${jobId}`);
|
|
232
|
+
}
|
|
233
|
+
allJobIds.push(jobId);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return new job_1.JobBatch({ agentsApi: this, jobIds: allJobIds, timeoutSeconds: params.timeoutSeconds });
|
|
237
|
+
}
|
|
238
|
+
async runSync(agentId, inputs) {
|
|
239
|
+
validateUuid(agentId, "agentId");
|
|
240
|
+
const resp = await this.httpClient.postWithDynamicInputs(`/v1/agents/run/${agentId}/`, inputs);
|
|
241
|
+
return resp;
|
|
242
|
+
}
|
|
243
|
+
async runVersion(params) {
|
|
244
|
+
validateUuid(params.agentId, "agentId");
|
|
245
|
+
validateUuid(params.versionId, "versionId");
|
|
246
|
+
const response = await this.httpClient.postWithDynamicInputs(`/v1/agents/run/${params.agentId}/versions/${params.versionId}/async/`, params.inputs);
|
|
247
|
+
// Handle both string response and object response formats
|
|
248
|
+
const jobId = typeof response === "string" ? response : response?.job_id;
|
|
249
|
+
if (!jobId || typeof jobId !== "string") {
|
|
250
|
+
throw new exceptions_1.RoeAPIException(`Invalid job ID returned from API: ${JSON.stringify(response)}`);
|
|
251
|
+
}
|
|
252
|
+
if (!(0, fileDetection_1.isUuidString)(jobId)) {
|
|
253
|
+
throw new exceptions_1.RoeAPIException(`Invalid job ID format returned from API: ${jobId}`);
|
|
254
|
+
}
|
|
255
|
+
return new job_1.Job({ agentsApi: this, jobId, timeoutSeconds: params.timeoutSeconds });
|
|
256
|
+
}
|
|
257
|
+
async runVersionSync(agentId, versionId, inputs) {
|
|
258
|
+
validateUuid(agentId, "agentId");
|
|
259
|
+
validateUuid(versionId, "versionId");
|
|
260
|
+
return this.httpClient.postWithDynamicInputs(`/v1/agents/run/${agentId}/versions/${versionId}/`, inputs);
|
|
261
|
+
}
|
|
262
|
+
*iterChunks(items, size) {
|
|
263
|
+
for (let i = 0; i < items.length; i += size) {
|
|
264
|
+
yield items.slice(i, i + size);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
exports.AgentsAPI = AgentsAPI;
|
|
269
|
+
AgentsAPI.MAX_BATCH_SIZE = 1000;
|
package/dist/src/auth.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RoeAuth = void 0;
|
|
4
|
+
class RoeAuth {
|
|
5
|
+
constructor(config) {
|
|
6
|
+
this.config = config;
|
|
7
|
+
}
|
|
8
|
+
getHeaders() {
|
|
9
|
+
return {
|
|
10
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
11
|
+
"User-Agent": "roe-typescript/0.1.0",
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.RoeAuth = RoeAuth;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RoeClient = void 0;
|
|
4
|
+
const agents_1 = require("./api/agents");
|
|
5
|
+
const auth_1 = require("./auth");
|
|
6
|
+
const config_1 = require("./config");
|
|
7
|
+
const httpClient_1 = require("./utils/httpClient");
|
|
8
|
+
class RoeClient {
|
|
9
|
+
constructor(input = {}) {
|
|
10
|
+
this.config = config_1.RoeConfig.fromEnv(input);
|
|
11
|
+
this.auth = new auth_1.RoeAuth(this.config);
|
|
12
|
+
this.httpClient = new httpClient_1.RoeHTTPClient(this.config, this.auth);
|
|
13
|
+
this._agents = new agents_1.AgentsAPI(this.config, this.httpClient);
|
|
14
|
+
}
|
|
15
|
+
get agents() {
|
|
16
|
+
return this._agents;
|
|
17
|
+
}
|
|
18
|
+
close() {
|
|
19
|
+
this.httpClient.close();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.RoeClient = RoeClient;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RoeConfig = void 0;
|
|
4
|
+
class RoeConfig {
|
|
5
|
+
constructor(opts) {
|
|
6
|
+
this.apiKey = opts.apiKey;
|
|
7
|
+
this.organizationId = opts.organizationId;
|
|
8
|
+
this.baseUrl = opts.baseUrl;
|
|
9
|
+
this.timeoutMs = opts.timeoutMs;
|
|
10
|
+
this.maxRetries = opts.maxRetries;
|
|
11
|
+
}
|
|
12
|
+
static fromEnv(input = {}) {
|
|
13
|
+
const apiKey = input.apiKey ?? process.env.ROE_API_KEY;
|
|
14
|
+
const organizationId = input.organizationId ?? process.env.ROE_ORGANIZATION_ID;
|
|
15
|
+
const baseUrl = input.baseUrl ?? process.env.ROE_BASE_URL ?? "https://api.roe-ai.com";
|
|
16
|
+
// Parse ROE_TIMEOUT with NaN check to avoid poisoning timeoutMs
|
|
17
|
+
const timeoutSecondsEnvRaw = process.env.ROE_TIMEOUT ? Number(process.env.ROE_TIMEOUT) : undefined;
|
|
18
|
+
const timeoutSecondsEnv = timeoutSecondsEnvRaw !== undefined && !Number.isNaN(timeoutSecondsEnvRaw)
|
|
19
|
+
? timeoutSecondsEnvRaw
|
|
20
|
+
: undefined;
|
|
21
|
+
const timeoutMs = input.timeoutMs ??
|
|
22
|
+
(input.timeoutSeconds !== undefined
|
|
23
|
+
? input.timeoutSeconds * 1000
|
|
24
|
+
: timeoutSecondsEnv !== undefined
|
|
25
|
+
? timeoutSecondsEnv * 1000
|
|
26
|
+
: 60000);
|
|
27
|
+
const envMaxRetries = process.env.ROE_MAX_RETRIES;
|
|
28
|
+
const parsedEnvMaxRetries = envMaxRetries !== undefined && !Number.isNaN(Number(envMaxRetries))
|
|
29
|
+
? Number(envMaxRetries)
|
|
30
|
+
: undefined;
|
|
31
|
+
const maxRetries = input.maxRetries ?? parsedEnvMaxRetries ?? 3;
|
|
32
|
+
if (!apiKey) {
|
|
33
|
+
throw new Error("API key is required. Provide it in constructor or set ROE_API_KEY.");
|
|
34
|
+
}
|
|
35
|
+
if (!organizationId) {
|
|
36
|
+
throw new Error("Organization ID is required. Provide it in constructor or set ROE_ORGANIZATION_ID.");
|
|
37
|
+
}
|
|
38
|
+
return new RoeConfig({
|
|
39
|
+
apiKey,
|
|
40
|
+
organizationId,
|
|
41
|
+
baseUrl,
|
|
42
|
+
timeoutMs,
|
|
43
|
+
maxRetries,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.RoeConfig = RoeConfig;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ServerError = exports.RateLimitError = exports.NotFoundError = exports.ForbiddenError = exports.InsufficientCreditsError = exports.AuthenticationError = exports.BadRequestError = exports.RoeAPIException = void 0;
|
|
4
|
+
exports.getExceptionForStatusCode = getExceptionForStatusCode;
|
|
5
|
+
exports.extractErrorMessage = extractErrorMessage;
|
|
6
|
+
class RoeAPIException extends Error {
|
|
7
|
+
constructor(message, statusCode, response) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = this.constructor.name;
|
|
10
|
+
this.statusCode = statusCode;
|
|
11
|
+
this.response = response;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.RoeAPIException = RoeAPIException;
|
|
15
|
+
class BadRequestError extends RoeAPIException {
|
|
16
|
+
}
|
|
17
|
+
exports.BadRequestError = BadRequestError;
|
|
18
|
+
class AuthenticationError extends RoeAPIException {
|
|
19
|
+
}
|
|
20
|
+
exports.AuthenticationError = AuthenticationError;
|
|
21
|
+
class InsufficientCreditsError extends RoeAPIException {
|
|
22
|
+
}
|
|
23
|
+
exports.InsufficientCreditsError = InsufficientCreditsError;
|
|
24
|
+
class ForbiddenError extends RoeAPIException {
|
|
25
|
+
}
|
|
26
|
+
exports.ForbiddenError = ForbiddenError;
|
|
27
|
+
class NotFoundError extends RoeAPIException {
|
|
28
|
+
}
|
|
29
|
+
exports.NotFoundError = NotFoundError;
|
|
30
|
+
class RateLimitError extends RoeAPIException {
|
|
31
|
+
}
|
|
32
|
+
exports.RateLimitError = RateLimitError;
|
|
33
|
+
class ServerError extends RoeAPIException {
|
|
34
|
+
}
|
|
35
|
+
exports.ServerError = ServerError;
|
|
36
|
+
function getExceptionForStatusCode(status) {
|
|
37
|
+
const map = {
|
|
38
|
+
400: BadRequestError,
|
|
39
|
+
401: AuthenticationError,
|
|
40
|
+
402: InsufficientCreditsError,
|
|
41
|
+
403: ForbiddenError,
|
|
42
|
+
404: NotFoundError,
|
|
43
|
+
429: RateLimitError,
|
|
44
|
+
};
|
|
45
|
+
if (map[status])
|
|
46
|
+
return map[status];
|
|
47
|
+
if (status >= 500)
|
|
48
|
+
return ServerError;
|
|
49
|
+
return RoeAPIException;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Extracts error message from various API response formats.
|
|
53
|
+
* Handles: { detail: string }, { error: string }, { message: string }, or raw string.
|
|
54
|
+
*/
|
|
55
|
+
function extractErrorMessage(data, statusCode) {
|
|
56
|
+
if (typeof data === "string") {
|
|
57
|
+
return data;
|
|
58
|
+
}
|
|
59
|
+
if (typeof data === "object" && data !== null) {
|
|
60
|
+
// Try common error message field names
|
|
61
|
+
const obj = data;
|
|
62
|
+
if (typeof obj.detail === "string")
|
|
63
|
+
return obj.detail;
|
|
64
|
+
if (typeof obj.error === "string")
|
|
65
|
+
return obj.error;
|
|
66
|
+
if (typeof obj.message === "string")
|
|
67
|
+
return obj.message;
|
|
68
|
+
// Handle nested error object
|
|
69
|
+
if (typeof obj.error === "object" && obj.error !== null) {
|
|
70
|
+
const errObj = obj.error;
|
|
71
|
+
if (typeof errObj.message === "string")
|
|
72
|
+
return errObj.message;
|
|
73
|
+
}
|
|
74
|
+
// Handle array of errors
|
|
75
|
+
if (Array.isArray(obj.errors) && obj.errors.length > 0) {
|
|
76
|
+
const firstError = obj.errors[0];
|
|
77
|
+
if (typeof firstError === "string")
|
|
78
|
+
return firstError;
|
|
79
|
+
if (typeof firstError === "object" && firstError !== null && typeof firstError.message === "string") {
|
|
80
|
+
return firstError.message;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
// Fallback to generic message
|
|
85
|
+
return `HTTP ${statusCode} error`;
|
|
86
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AgentVersionWithApi = exports.BaseAgentWithApi = void 0;
|
|
4
|
+
class BaseAgentWithApi {
|
|
5
|
+
constructor(data) {
|
|
6
|
+
Object.assign(this, data);
|
|
7
|
+
}
|
|
8
|
+
setAgentsApi(api) {
|
|
9
|
+
this._agentsApi = api;
|
|
10
|
+
}
|
|
11
|
+
run(inputs) {
|
|
12
|
+
if (!this._agentsApi)
|
|
13
|
+
throw new Error("Agents API not set");
|
|
14
|
+
return this._agentsApi.run({ agentId: this.id, inputs });
|
|
15
|
+
}
|
|
16
|
+
listVersions() {
|
|
17
|
+
if (!this._agentsApi)
|
|
18
|
+
throw new Error("Agents API not set");
|
|
19
|
+
return this._agentsApi.versions.list(this.id);
|
|
20
|
+
}
|
|
21
|
+
getCurrentVersion() {
|
|
22
|
+
if (!this._agentsApi)
|
|
23
|
+
throw new Error("Agents API not set");
|
|
24
|
+
if (!this.current_version_id)
|
|
25
|
+
return null;
|
|
26
|
+
return this._agentsApi.versions.retrieve(this.id, this.current_version_id);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.BaseAgentWithApi = BaseAgentWithApi;
|
|
30
|
+
class AgentVersionWithApi {
|
|
31
|
+
constructor(data) {
|
|
32
|
+
Object.assign(this, data);
|
|
33
|
+
}
|
|
34
|
+
setAgentsApi(api) {
|
|
35
|
+
this._agentsApi = api;
|
|
36
|
+
}
|
|
37
|
+
run(inputs) {
|
|
38
|
+
if (!this._agentsApi)
|
|
39
|
+
throw new Error("Agents API not set");
|
|
40
|
+
if (!this.base_agent?.id)
|
|
41
|
+
throw new Error("AgentVersion missing base_agent id");
|
|
42
|
+
return this._agentsApi.runVersion({ agentId: this.base_agent.id, versionId: this.id, inputs });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.AgentVersionWithApi = AgentVersionWithApi;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.FileUpload = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const mime_types_1 = __importDefault(require("mime-types"));
|
|
10
|
+
const exceptions_1 = require("../exceptions");
|
|
11
|
+
/** Maximum file size: 2GB (aligned with main-roe backend) */
|
|
12
|
+
const MAX_FILE_SIZE = 2 * 1024 * 1024 * 1024;
|
|
13
|
+
class FileUpload {
|
|
14
|
+
constructor(init) {
|
|
15
|
+
this._openedStream = null;
|
|
16
|
+
this.path = init.path;
|
|
17
|
+
this.file = init.file;
|
|
18
|
+
this.filename = init.filename;
|
|
19
|
+
this.mimeType = init.mimeType;
|
|
20
|
+
if (!this.path && !this.file) {
|
|
21
|
+
throw new Error("Either path or file must be provided");
|
|
22
|
+
}
|
|
23
|
+
if (this.path && this.file) {
|
|
24
|
+
throw new Error("Only one of path or file should be provided");
|
|
25
|
+
}
|
|
26
|
+
// Validate file size if path is provided
|
|
27
|
+
if (this.path) {
|
|
28
|
+
try {
|
|
29
|
+
const stat = fs_1.default.statSync(this.path);
|
|
30
|
+
if (stat.size > MAX_FILE_SIZE) {
|
|
31
|
+
throw new exceptions_1.BadRequestError(`File exceeds maximum size of 2GB: ${this.path} (${(stat.size / (1024 * 1024 * 1024)).toFixed(2)}GB)`, 400);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
catch (err) {
|
|
35
|
+
if (err instanceof exceptions_1.BadRequestError)
|
|
36
|
+
throw err;
|
|
37
|
+
throw new Error(`Cannot access file: ${this.path}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
get effectiveFilename() {
|
|
42
|
+
if (this.filename)
|
|
43
|
+
return this.filename;
|
|
44
|
+
if (this.path)
|
|
45
|
+
return path_1.default.basename(this.path);
|
|
46
|
+
return "upload";
|
|
47
|
+
}
|
|
48
|
+
get effectiveMimeType() {
|
|
49
|
+
if (this.mimeType)
|
|
50
|
+
return this.mimeType;
|
|
51
|
+
const guess = mime_types_1.default.lookup(this.effectiveFilename);
|
|
52
|
+
return (guess || "application/octet-stream");
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Opens the file for reading. If a path was provided, creates a new ReadStream.
|
|
56
|
+
* Track opened streams so they can be closed later.
|
|
57
|
+
*/
|
|
58
|
+
open() {
|
|
59
|
+
if (this.file)
|
|
60
|
+
return this.file;
|
|
61
|
+
if (this.path) {
|
|
62
|
+
this._openedStream = fs_1.default.createReadStream(this.path);
|
|
63
|
+
return this._openedStream;
|
|
64
|
+
}
|
|
65
|
+
throw new Error("No file source available");
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Closes any opened file stream to prevent resource leaks.
|
|
69
|
+
* Should be called after the file upload is complete.
|
|
70
|
+
*/
|
|
71
|
+
close() {
|
|
72
|
+
if (this._openedStream) {
|
|
73
|
+
this._openedStream.destroy();
|
|
74
|
+
this._openedStream = null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Returns the file size in bytes, if available.
|
|
79
|
+
*/
|
|
80
|
+
getSize() {
|
|
81
|
+
if (this.path) {
|
|
82
|
+
try {
|
|
83
|
+
return fs_1.default.statSync(this.path).size;
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.FileUpload = FileUpload;
|