@relevanceai/sdk 3.0.0-alpha.2 → 3.0.0-alpha.3
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/README.md +422 -193
- package/esm/agent.d.ts +20 -8
- package/esm/agent.js +32 -12
- package/esm/client.d.ts +4 -8
- package/esm/client.js +11 -12
- package/esm/emitter.d.ts +16 -0
- package/esm/emitter.js +15 -0
- package/{script/events.d.ts → esm/event.d.ts} +10 -13
- package/esm/{events.js → event.js} +0 -6
- package/esm/message/agent-error.d.ts +6 -0
- package/esm/message/agent-error.js +3 -0
- package/esm/message/agent.d.ts +9 -0
- package/esm/message/agent.js +9 -0
- package/esm/message/task.d.ts +42 -0
- package/esm/message/task.js +38 -0
- package/esm/message/tool.d.ts +108 -0
- package/esm/message/tool.js +109 -0
- package/esm/message/user.d.ts +20 -0
- package/esm/message/user.js +19 -0
- package/esm/mod.d.ts +6 -3
- package/esm/mod.js +1 -0
- package/esm/task.d.ts +50 -18
- package/esm/task.js +228 -65
- package/esm/utils.d.ts +1 -5
- package/esm/utils.js +1 -13
- package/package.json +1 -1
- package/script/agent.d.ts +20 -8
- package/script/agent.js +32 -12
- package/script/client.d.ts +4 -8
- package/script/client.js +11 -12
- package/script/emitter.d.ts +16 -0
- package/script/emitter.js +19 -0
- package/{esm/events.d.ts → script/event.d.ts} +10 -13
- package/script/{events.js → event.js} +1 -8
- package/script/message/agent-error.d.ts +6 -0
- package/script/message/agent-error.js +7 -0
- package/script/message/agent.d.ts +9 -0
- package/script/message/agent.js +13 -0
- package/script/message/task.d.ts +42 -0
- package/script/message/task.js +42 -0
- package/script/message/tool.d.ts +108 -0
- package/script/message/tool.js +113 -0
- package/script/message/user.d.ts +20 -0
- package/script/message/user.js +23 -0
- package/script/mod.d.ts +6 -3
- package/script/mod.js +3 -1
- package/script/task.d.ts +50 -18
- package/script/task.js +228 -65
- package/script/utils.d.ts +1 -5
- package/script/utils.js +1 -14
- package/esm/agent-task.d.ts +0 -61
- package/esm/agent-task.js +0 -112
- package/esm/message.d.ts +0 -18
- package/esm/message.js +0 -18
- package/script/agent-task.d.ts +0 -61
- package/script/agent-task.js +0 -116
- package/script/message.d.ts +0 -18
- package/script/message.js +0 -22
package/script/task.js
CHANGED
|
@@ -1,91 +1,257 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Task = void 0;
|
|
3
|
+
exports.Task = exports.resetSubscribeBackoff = void 0;
|
|
4
|
+
const agent_js_1 = require("./agent.js");
|
|
4
5
|
const client_js_1 = require("./client.js");
|
|
5
|
-
const
|
|
6
|
+
const emitter_js_1 = require("./emitter.js");
|
|
7
|
+
const event_js_1 = require("./event.js");
|
|
8
|
+
const agent_error_js_1 = require("./message/agent-error.js");
|
|
9
|
+
const agent_js_2 = require("./message/agent.js");
|
|
10
|
+
const tool_js_1 = require("./message/tool.js");
|
|
11
|
+
const user_js_1 = require("./message/user.js");
|
|
6
12
|
const utils_js_1 = require("./utils.js");
|
|
7
|
-
|
|
8
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Converts an AgentTaskState to a simplified TaskStatus.
|
|
15
|
+
*
|
|
16
|
+
* @internal
|
|
17
|
+
*
|
|
18
|
+
* @param {AgentTaskState} state The agent task state to convert.
|
|
19
|
+
* @returns {TaskStatus} The simplified task status.
|
|
20
|
+
*/
|
|
21
|
+
function stateToStatus(state) {
|
|
22
|
+
switch (state) {
|
|
23
|
+
case "paused":
|
|
24
|
+
case "idle":
|
|
25
|
+
return "idle";
|
|
26
|
+
case "starting-up":
|
|
27
|
+
case "waiting-for-capacity":
|
|
28
|
+
case "queued-for-approval":
|
|
29
|
+
case "queued-for-rerun":
|
|
30
|
+
return "queued";
|
|
31
|
+
case "running":
|
|
32
|
+
return "running";
|
|
33
|
+
case "pending-approval":
|
|
34
|
+
case "escalated":
|
|
35
|
+
return "action";
|
|
36
|
+
case "timed-out":
|
|
37
|
+
return "error";
|
|
38
|
+
case "cancelled":
|
|
39
|
+
case "completed":
|
|
40
|
+
return "complete";
|
|
41
|
+
case "unrecoverable":
|
|
42
|
+
case "errored-pending-approval":
|
|
43
|
+
return "error";
|
|
44
|
+
default:
|
|
45
|
+
throw new Error(`unhandled task state: ${state}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.resetSubscribeBackoff = Symbol("resetSubscribeBackoff");
|
|
49
|
+
class Task extends emitter_js_1.Emitter {
|
|
50
|
+
static async get(id, agentOrAgentId, client = client_js_1.Client.default()) {
|
|
51
|
+
const agent = typeof agentOrAgentId === "string"
|
|
52
|
+
? await agent_js_1.Agent.get(agentOrAgentId, client)
|
|
53
|
+
: agentOrAgentId;
|
|
54
|
+
const metadata = await Task.#fetchMetadata(id, agent.id, client);
|
|
55
|
+
return new Task(metadata, agent, client);
|
|
56
|
+
}
|
|
57
|
+
static #fetchMetadata(id, agentId, client) {
|
|
58
|
+
return client.fetch(`/agents/${agentId}/tasks/${id}/metadata`).then(({ metadata }) => metadata);
|
|
59
|
+
}
|
|
60
|
+
#controller;
|
|
61
|
+
#backoffMs = 1000;
|
|
62
|
+
#lastUpdatedAt;
|
|
63
|
+
#delayController;
|
|
64
|
+
#metadata;
|
|
65
|
+
agent;
|
|
9
66
|
client;
|
|
10
|
-
|
|
11
|
-
listenController;
|
|
12
|
-
constructor(subject, id = undefined, client = client_js_1.Client.default()) {
|
|
67
|
+
constructor(metadata, agent, client = client_js_1.Client.default()) {
|
|
13
68
|
super();
|
|
14
|
-
this
|
|
69
|
+
this.#metadata = metadata;
|
|
70
|
+
this.agent = agent;
|
|
15
71
|
this.client = client;
|
|
16
|
-
this.#id = id;
|
|
17
72
|
}
|
|
18
73
|
get id() {
|
|
19
|
-
return this.#
|
|
74
|
+
return this.#metadata.knowledge_set;
|
|
75
|
+
}
|
|
76
|
+
get title() {
|
|
77
|
+
return this.#metadata.conversation.title ?? "";
|
|
78
|
+
}
|
|
79
|
+
get status() {
|
|
80
|
+
return stateToStatus(this.#metadata.conversation.state);
|
|
20
81
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
82
|
+
isRunning() {
|
|
83
|
+
switch (this.status) {
|
|
84
|
+
case "queued":
|
|
85
|
+
case "running":
|
|
86
|
+
return true;
|
|
87
|
+
default:
|
|
88
|
+
return false;
|
|
24
89
|
}
|
|
25
|
-
// @ts-ignore: allow assignment to readonly in this special case
|
|
26
|
-
this.#id = id;
|
|
27
|
-
this.dispatchEvent(new events_js_1.TaskStartEvent(id, status));
|
|
28
90
|
}
|
|
29
|
-
|
|
30
|
-
|
|
91
|
+
async getMessages({ from = new Date(0) } = {}) {
|
|
92
|
+
const url = `/agents/${this.agent.id}/tasks/${this.id}/view`;
|
|
93
|
+
const res = await this.client.fetch(url, {
|
|
94
|
+
method: "POST",
|
|
95
|
+
body: JSON.stringify({
|
|
96
|
+
page_size: 1_000, // @todo: pagination
|
|
97
|
+
cursor: {
|
|
98
|
+
after: from.toISOString(),
|
|
99
|
+
},
|
|
100
|
+
}),
|
|
101
|
+
});
|
|
102
|
+
// message should be in ascending order
|
|
103
|
+
return res.results.reverse().map((data) => {
|
|
104
|
+
switch (data.content.type) {
|
|
105
|
+
case "agent-error":
|
|
106
|
+
return new agent_error_js_1.AgentErrorMessage(data);
|
|
107
|
+
case "agent-message":
|
|
108
|
+
return new agent_js_2.AgentMessage(data);
|
|
109
|
+
case "tool-run":
|
|
110
|
+
return new tool_js_1.ToolMessage(data);
|
|
111
|
+
case "user-message":
|
|
112
|
+
return new user_js_1.UserMessage(data);
|
|
113
|
+
default:
|
|
114
|
+
throw new Error("unknown message response");
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
async refresh() {
|
|
119
|
+
this.#metadata = await Task.#fetchMetadata(this.id, this.agent.id, this.client);
|
|
120
|
+
}
|
|
121
|
+
[exports.resetSubscribeBackoff]() {
|
|
122
|
+
this.#backoffMs = 1000;
|
|
123
|
+
this.#delayController?.abort();
|
|
124
|
+
}
|
|
125
|
+
subscribe() {
|
|
126
|
+
if (this.isSubscribed()) {
|
|
31
127
|
return;
|
|
32
128
|
}
|
|
33
|
-
this
|
|
34
|
-
const signal = this.
|
|
35
|
-
let currentStatus =
|
|
129
|
+
this.#controller = new AbortController();
|
|
130
|
+
const signal = this.#controller.signal;
|
|
131
|
+
let currentStatus = this.status;
|
|
36
132
|
const messagesCursor = new Date(0);
|
|
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
|
-
|
|
133
|
+
const emitted = new Set();
|
|
134
|
+
let lastMessage = null;
|
|
135
|
+
const pendingTools = new Map();
|
|
136
|
+
// Initialize backoff and tracking
|
|
137
|
+
this.#backoffMs = 1000;
|
|
138
|
+
this.#lastUpdatedAt = this.#metadata.update_date;
|
|
139
|
+
void (async () => {
|
|
140
|
+
while (this.isSubscribed() && !signal.aborted) {
|
|
141
|
+
try {
|
|
142
|
+
const [, result] = await Promise.allSettled([
|
|
143
|
+
this.refresh(),
|
|
144
|
+
this.getMessages({ from: messagesCursor }),
|
|
145
|
+
]);
|
|
146
|
+
if (!this.isSubscribed() || signal.aborted) {
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
const messages = result.status === "fulfilled" ? result.value : [];
|
|
150
|
+
// Track if any changes occurred
|
|
151
|
+
let hasChanges = false;
|
|
152
|
+
// Check for status changes
|
|
153
|
+
if (this.status !== currentStatus) {
|
|
154
|
+
currentStatus = this.status;
|
|
155
|
+
this.dispatchEvent(new event_js_1.TaskStatusEvent(this.status));
|
|
156
|
+
hasChanges = true;
|
|
157
|
+
}
|
|
158
|
+
// Check for metadata update_date changes
|
|
159
|
+
if (this.#metadata.update_date !== this.#lastUpdatedAt) {
|
|
160
|
+
this.#lastUpdatedAt = this.#metadata.update_date;
|
|
161
|
+
hasChanges = true;
|
|
162
|
+
}
|
|
163
|
+
// Process messages
|
|
164
|
+
if (messages.length) {
|
|
165
|
+
hasChanges = true;
|
|
166
|
+
for (const message of messages) {
|
|
167
|
+
if (emitted.has(message.id)) {
|
|
168
|
+
switch (message.type) {
|
|
169
|
+
case "agent-error":
|
|
170
|
+
case "agent-message":
|
|
171
|
+
case "user-message":
|
|
172
|
+
// don't re-fire
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
emitted.add(message.id);
|
|
177
|
+
switch (message.type) {
|
|
178
|
+
case "agent-error":
|
|
179
|
+
this.dispatchEvent(new event_js_1.TaskErrorEvent(message));
|
|
180
|
+
break;
|
|
181
|
+
case "tool-run": {
|
|
182
|
+
const { status } = message;
|
|
183
|
+
if (pendingTools.get(message.id)?.status == status) {
|
|
184
|
+
// no change to the tool status
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (["pending", "running"].includes(status)) {
|
|
188
|
+
pendingTools.set(message.id, message);
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
pendingTools.delete(message.id);
|
|
192
|
+
}
|
|
193
|
+
this.dispatchEvent(new event_js_1.TaskUpdateEvent(message));
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
case "agent-message":
|
|
197
|
+
case "user-message":
|
|
198
|
+
this.dispatchEvent(new event_js_1.TaskMessageEvent(message));
|
|
199
|
+
}
|
|
200
|
+
lastMessage = message;
|
|
201
|
+
}
|
|
202
|
+
// +1 the api treats after inclusively
|
|
203
|
+
let nextCursor = messages.at(-1).createdAt.getTime() + 1;
|
|
204
|
+
// set the cursor as the earliest pending tool
|
|
205
|
+
for (const pending of pendingTools.values()) {
|
|
206
|
+
if (nextCursor > pending.createdAt.getTime()) {
|
|
207
|
+
nextCursor = pending.createdAt.getTime();
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
messagesCursor.setTime(nextCursor);
|
|
211
|
+
}
|
|
212
|
+
// Apply backoff logic
|
|
213
|
+
if (hasChanges) {
|
|
214
|
+
// Reset backoff on any changes
|
|
215
|
+
this.#backoffMs = 1000;
|
|
216
|
+
}
|
|
217
|
+
else if (!this.isRunning() && lastMessage?.isAgent()) {
|
|
218
|
+
// Apply exponential backoff when idle with last message from agent
|
|
219
|
+
this.#backoffMs = Math.min(this.#backoffMs * 2, 60000);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
finally {
|
|
223
|
+
// Wait for the backoff period or abort signal
|
|
224
|
+
if (!signal.aborted) {
|
|
225
|
+
// Create a new controller for this delay that can be aborted independently
|
|
226
|
+
this.#delayController = new AbortController();
|
|
227
|
+
await Promise.race([
|
|
228
|
+
(0, utils_js_1.delay)(this.#backoffMs),
|
|
229
|
+
(0, utils_js_1.abortPromise)(AbortSignal.any([
|
|
230
|
+
signal,
|
|
231
|
+
this.#delayController.signal,
|
|
232
|
+
])),
|
|
233
|
+
]);
|
|
67
234
|
}
|
|
68
235
|
}
|
|
69
|
-
messagesCursor.setTime(
|
|
70
|
-
// +1 the api treats after inclusively
|
|
71
|
-
messages.at(-1).createdAt.getTime() + 1);
|
|
72
236
|
}
|
|
73
|
-
}
|
|
237
|
+
})();
|
|
74
238
|
}
|
|
75
|
-
|
|
76
|
-
return this
|
|
239
|
+
isSubscribed() {
|
|
240
|
+
return this.#controller !== undefined;
|
|
77
241
|
}
|
|
78
|
-
|
|
79
|
-
this
|
|
80
|
-
this
|
|
242
|
+
unsubscribe() {
|
|
243
|
+
this.#delayController?.abort();
|
|
244
|
+
this.#controller?.abort();
|
|
245
|
+
this.#delayController = undefined;
|
|
246
|
+
this.#controller = undefined;
|
|
81
247
|
}
|
|
82
248
|
addEventListener(type, listener, options) {
|
|
83
|
-
this.
|
|
249
|
+
this.subscribe();
|
|
84
250
|
const signal = AbortSignal.any([
|
|
85
251
|
...(options && typeof options === "object" && options.signal
|
|
86
252
|
? [options.signal]
|
|
87
253
|
: []),
|
|
88
|
-
this.
|
|
254
|
+
this.#controller.signal,
|
|
89
255
|
]);
|
|
90
256
|
const capture = typeof options === "boolean"
|
|
91
257
|
? options
|
|
@@ -93,8 +259,5 @@ class Task extends EventTarget {
|
|
|
93
259
|
const addOptions = Object.assign({}, options, { signal, capture });
|
|
94
260
|
super.addEventListener(type, listener, addOptions);
|
|
95
261
|
}
|
|
96
|
-
removeEventListener(type, listener, options) {
|
|
97
|
-
super.removeEventListener(type, listener, options);
|
|
98
|
-
}
|
|
99
262
|
}
|
|
100
263
|
exports.Task = Task;
|
package/script/utils.d.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
export declare function abortPromise(signal: AbortSignal, reject?: boolean): Promise<void>;
|
|
2
|
-
export declare function delay(timeout: number): Promise<void>;
|
|
3
|
-
export declare function runInterval(runner: () => Promise<void> | void, interval: number, { signal, }?: {
|
|
4
|
-
signal?: AbortSignal;
|
|
5
|
-
immediate?: boolean;
|
|
6
|
-
}): Promise<void>;
|
|
2
|
+
export declare function delay(timeout: number | (() => number)): Promise<void>;
|
|
7
3
|
export declare function cleanPath(path: string, version?: string): string;
|
|
8
4
|
export declare function randomUUID(): Promise<any>;
|
package/script/utils.js
CHANGED
|
@@ -35,26 +35,13 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.abortPromise = abortPromise;
|
|
37
37
|
exports.delay = delay;
|
|
38
|
-
exports.runInterval = runInterval;
|
|
39
38
|
exports.cleanPath = cleanPath;
|
|
40
39
|
exports.randomUUID = randomUUID;
|
|
41
40
|
function abortPromise(signal, reject) {
|
|
42
41
|
return new Promise((res, rej) => signal.addEventListener("abort", () => reject ? rej() : res()));
|
|
43
42
|
}
|
|
44
43
|
function delay(timeout) {
|
|
45
|
-
return new Promise((done) => setTimeout(done, timeout));
|
|
46
|
-
}
|
|
47
|
-
async function runInterval(runner, interval, { signal, } = {}) {
|
|
48
|
-
while (true) {
|
|
49
|
-
if (signal?.aborted) {
|
|
50
|
-
break;
|
|
51
|
-
}
|
|
52
|
-
await runner();
|
|
53
|
-
await Promise.race([
|
|
54
|
-
delay(interval),
|
|
55
|
-
signal ? abortPromise(signal) : new Promise(() => { }),
|
|
56
|
-
]);
|
|
57
|
-
}
|
|
44
|
+
return new Promise((done) => setTimeout(done, typeof timeout === "number" ? timeout : timeout()));
|
|
58
45
|
}
|
|
59
46
|
function cleanPath(path, version = "latest") {
|
|
60
47
|
return `/${version}/${path.trim().replace(/^\/+/, "")}`;
|
package/esm/agent-task.d.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import type { Agent } from "./agent.js";
|
|
2
|
-
import { TaskMessage } from "./message.js";
|
|
3
|
-
import { Task, type TaskStatus } from "./task.js";
|
|
4
|
-
type AgentTaskEvents = {
|
|
5
|
-
start: {
|
|
6
|
-
status: TaskStatus;
|
|
7
|
-
};
|
|
8
|
-
status: {
|
|
9
|
-
status: TaskStatus;
|
|
10
|
-
};
|
|
11
|
-
message: {
|
|
12
|
-
message: TaskMessage<"agent-message" | "user-message">;
|
|
13
|
-
};
|
|
14
|
-
update: {
|
|
15
|
-
message: TaskMessage<"tool-run">;
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
export type AgentTaskState = "idle" | "starting-up" | "running" | "pending-approval" | "waiting-for-capacity" | "cancelled" | "timed-out" | "escalated" | "unrecoverable" | "paused" | "completed" | "errored-pending-approval" | "queued-for-approval" | "queued-for-rerun";
|
|
19
|
-
/**
|
|
20
|
-
* AgentTask represents a conversation task with an AI agent. It extends the
|
|
21
|
-
* base Task class with agent-specific functionality for sending messages and
|
|
22
|
-
* retrieving conversation history.
|
|
23
|
-
*
|
|
24
|
-
* @see {@link Task} for the base task functionality.
|
|
25
|
-
* @see {@link Agent} for the agent this task is associated with.
|
|
26
|
-
*
|
|
27
|
-
* @class AgentTask
|
|
28
|
-
* @extends Task<Agent, AgentTaskEvents>
|
|
29
|
-
*/
|
|
30
|
-
export declare class AgentTask extends Task<Agent, AgentTaskEvents> {
|
|
31
|
-
/**
|
|
32
|
-
* Sends a message to the agent. This method triggers the agent with the
|
|
33
|
-
* message and updates the task ID if this is the first message.
|
|
34
|
-
*
|
|
35
|
-
* Note: This method is asynchronous but doesn't return a promise. Use event
|
|
36
|
-
* listeners to track the response.
|
|
37
|
-
*
|
|
38
|
-
* @param {string} message
|
|
39
|
-
*/
|
|
40
|
-
sendMessage(message: string): void;
|
|
41
|
-
/**
|
|
42
|
-
* Fetches the current status of the task from the API.
|
|
43
|
-
*
|
|
44
|
-
* @returns {Promise<TaskStatus>} The current task status.
|
|
45
|
-
* @throws {Error} if the agent or task ID is missing.
|
|
46
|
-
*/
|
|
47
|
-
fetchStatus(): Promise<TaskStatus>;
|
|
48
|
-
/**
|
|
49
|
-
* Fetches messages from the conversation.
|
|
50
|
-
*
|
|
51
|
-
* @param {Object} [options] Optional fetch options.
|
|
52
|
-
* @param {Date} [options.from] Fetch messages after this timestamp.
|
|
53
|
-
*
|
|
54
|
-
* @returns {Promise<TaskMessage[]>} Array of messages in ascending order.
|
|
55
|
-
* @throws {Error} if the agent or task ID is missing.
|
|
56
|
-
*/
|
|
57
|
-
fetchMessages({ from }?: {
|
|
58
|
-
from?: Date;
|
|
59
|
-
}): Promise<TaskMessage[]>;
|
|
60
|
-
}
|
|
61
|
-
export {};
|
package/esm/agent-task.js
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { TaskMessage } from "./message.js";
|
|
2
|
-
import { Task } from "./task.js";
|
|
3
|
-
/**
|
|
4
|
-
* Converts an AgentTaskState to a simplified TaskStatus.
|
|
5
|
-
*
|
|
6
|
-
* @internal
|
|
7
|
-
*
|
|
8
|
-
* @param {AgentTaskState} state The agent task state to convert.
|
|
9
|
-
* @returns {TaskStatus} The simplified task status.
|
|
10
|
-
*/
|
|
11
|
-
function stateToStatus(state) {
|
|
12
|
-
switch (state) {
|
|
13
|
-
case "paused":
|
|
14
|
-
case "idle":
|
|
15
|
-
return "idle";
|
|
16
|
-
case "starting-up":
|
|
17
|
-
case "waiting-for-capacity":
|
|
18
|
-
case "queued-for-approval":
|
|
19
|
-
case "queued-for-rerun":
|
|
20
|
-
return "queued";
|
|
21
|
-
case "running":
|
|
22
|
-
return "running";
|
|
23
|
-
case "pending-approval":
|
|
24
|
-
case "escalated":
|
|
25
|
-
return "action";
|
|
26
|
-
case "timed-out":
|
|
27
|
-
return "error";
|
|
28
|
-
case "cancelled":
|
|
29
|
-
case "completed":
|
|
30
|
-
return "complete";
|
|
31
|
-
case "unrecoverable":
|
|
32
|
-
case "errored-pending-approval":
|
|
33
|
-
return "error";
|
|
34
|
-
default:
|
|
35
|
-
throw new Error(`unhandled task state: ${state}`);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* AgentTask represents a conversation task with an AI agent. It extends the
|
|
40
|
-
* base Task class with agent-specific functionality for sending messages and
|
|
41
|
-
* retrieving conversation history.
|
|
42
|
-
*
|
|
43
|
-
* @see {@link Task} for the base task functionality.
|
|
44
|
-
* @see {@link Agent} for the agent this task is associated with.
|
|
45
|
-
*
|
|
46
|
-
* @class AgentTask
|
|
47
|
-
* @extends Task<Agent, AgentTaskEvents>
|
|
48
|
-
*/
|
|
49
|
-
export class AgentTask extends Task {
|
|
50
|
-
/**
|
|
51
|
-
* Sends a message to the agent. This method triggers the agent with the
|
|
52
|
-
* message and updates the task ID if this is the first message.
|
|
53
|
-
*
|
|
54
|
-
* Note: This method is asynchronous but doesn't return a promise. Use event
|
|
55
|
-
* listeners to track the response.
|
|
56
|
-
*
|
|
57
|
-
* @param {string} message
|
|
58
|
-
*/
|
|
59
|
-
sendMessage(message) {
|
|
60
|
-
this.subject.trigger(message, this.id || undefined).then(({ id, state }) => {
|
|
61
|
-
// started
|
|
62
|
-
if (!this.id) {
|
|
63
|
-
this.setId(id, stateToStatus(state));
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Fetches the current status of the task from the API.
|
|
69
|
-
*
|
|
70
|
-
* @returns {Promise<TaskStatus>} The current task status.
|
|
71
|
-
* @throws {Error} if the agent or task ID is missing.
|
|
72
|
-
*/
|
|
73
|
-
async fetchStatus() {
|
|
74
|
-
if (!this.subject.id) {
|
|
75
|
-
throw new Error("expecting agent id");
|
|
76
|
-
}
|
|
77
|
-
if (!this.id) {
|
|
78
|
-
return "not-started";
|
|
79
|
-
}
|
|
80
|
-
const url = `/agents/${this.subject.id}/tasks/${this.id}/metadata`;
|
|
81
|
-
const res = await this.client.fetch(url);
|
|
82
|
-
return stateToStatus(res.metadata.conversation.state);
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Fetches messages from the conversation.
|
|
86
|
-
*
|
|
87
|
-
* @param {Object} [options] Optional fetch options.
|
|
88
|
-
* @param {Date} [options.from] Fetch messages after this timestamp.
|
|
89
|
-
*
|
|
90
|
-
* @returns {Promise<TaskMessage[]>} Array of messages in ascending order.
|
|
91
|
-
* @throws {Error} if the agent or task ID is missing.
|
|
92
|
-
*/
|
|
93
|
-
async fetchMessages({ from = new Date(0) } = {}) {
|
|
94
|
-
if (!this.subject.id) {
|
|
95
|
-
throw new Error("expecting agent id");
|
|
96
|
-
}
|
|
97
|
-
if (!this.id) {
|
|
98
|
-
throw new Error("expecting task id");
|
|
99
|
-
}
|
|
100
|
-
const url = `/agents/${this.subject.id}/tasks/${this.id}/view`;
|
|
101
|
-
const res = await this.client.fetch(url, {
|
|
102
|
-
method: "POST",
|
|
103
|
-
body: JSON.stringify({
|
|
104
|
-
cursor: {
|
|
105
|
-
after: from.toISOString(),
|
|
106
|
-
},
|
|
107
|
-
}),
|
|
108
|
-
});
|
|
109
|
-
// message should be in ascending order
|
|
110
|
-
return res.results.reverse().map((data) => new TaskMessage(data));
|
|
111
|
-
}
|
|
112
|
-
}
|
package/esm/message.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
type TaskMessageType = "user-message" | "agent-message" | "tool-run" | "agent-error";
|
|
2
|
-
export type MessageData = {
|
|
3
|
-
item_id: string;
|
|
4
|
-
insert_date_: string;
|
|
5
|
-
content: {
|
|
6
|
-
type: TaskMessageType;
|
|
7
|
-
text: string;
|
|
8
|
-
};
|
|
9
|
-
};
|
|
10
|
-
export declare class TaskMessage<T extends TaskMessageType = TaskMessageType> {
|
|
11
|
-
#private;
|
|
12
|
-
constructor(data: MessageData);
|
|
13
|
-
get id(): string;
|
|
14
|
-
get type(): T;
|
|
15
|
-
get createdAt(): Date;
|
|
16
|
-
get text(): string;
|
|
17
|
-
}
|
|
18
|
-
export {};
|
package/esm/message.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export class TaskMessage {
|
|
2
|
-
#data;
|
|
3
|
-
constructor(data) {
|
|
4
|
-
this.#data = data;
|
|
5
|
-
}
|
|
6
|
-
get id() {
|
|
7
|
-
return this.#data.item_id;
|
|
8
|
-
}
|
|
9
|
-
get type() {
|
|
10
|
-
return this.#data.content.type;
|
|
11
|
-
}
|
|
12
|
-
get createdAt() {
|
|
13
|
-
return new Date(this.#data.insert_date_);
|
|
14
|
-
}
|
|
15
|
-
get text() {
|
|
16
|
-
return this.#data.content.text;
|
|
17
|
-
}
|
|
18
|
-
}
|
package/script/agent-task.d.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import type { Agent } from "./agent.js";
|
|
2
|
-
import { TaskMessage } from "./message.js";
|
|
3
|
-
import { Task, type TaskStatus } from "./task.js";
|
|
4
|
-
type AgentTaskEvents = {
|
|
5
|
-
start: {
|
|
6
|
-
status: TaskStatus;
|
|
7
|
-
};
|
|
8
|
-
status: {
|
|
9
|
-
status: TaskStatus;
|
|
10
|
-
};
|
|
11
|
-
message: {
|
|
12
|
-
message: TaskMessage<"agent-message" | "user-message">;
|
|
13
|
-
};
|
|
14
|
-
update: {
|
|
15
|
-
message: TaskMessage<"tool-run">;
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
export type AgentTaskState = "idle" | "starting-up" | "running" | "pending-approval" | "waiting-for-capacity" | "cancelled" | "timed-out" | "escalated" | "unrecoverable" | "paused" | "completed" | "errored-pending-approval" | "queued-for-approval" | "queued-for-rerun";
|
|
19
|
-
/**
|
|
20
|
-
* AgentTask represents a conversation task with an AI agent. It extends the
|
|
21
|
-
* base Task class with agent-specific functionality for sending messages and
|
|
22
|
-
* retrieving conversation history.
|
|
23
|
-
*
|
|
24
|
-
* @see {@link Task} for the base task functionality.
|
|
25
|
-
* @see {@link Agent} for the agent this task is associated with.
|
|
26
|
-
*
|
|
27
|
-
* @class AgentTask
|
|
28
|
-
* @extends Task<Agent, AgentTaskEvents>
|
|
29
|
-
*/
|
|
30
|
-
export declare class AgentTask extends Task<Agent, AgentTaskEvents> {
|
|
31
|
-
/**
|
|
32
|
-
* Sends a message to the agent. This method triggers the agent with the
|
|
33
|
-
* message and updates the task ID if this is the first message.
|
|
34
|
-
*
|
|
35
|
-
* Note: This method is asynchronous but doesn't return a promise. Use event
|
|
36
|
-
* listeners to track the response.
|
|
37
|
-
*
|
|
38
|
-
* @param {string} message
|
|
39
|
-
*/
|
|
40
|
-
sendMessage(message: string): void;
|
|
41
|
-
/**
|
|
42
|
-
* Fetches the current status of the task from the API.
|
|
43
|
-
*
|
|
44
|
-
* @returns {Promise<TaskStatus>} The current task status.
|
|
45
|
-
* @throws {Error} if the agent or task ID is missing.
|
|
46
|
-
*/
|
|
47
|
-
fetchStatus(): Promise<TaskStatus>;
|
|
48
|
-
/**
|
|
49
|
-
* Fetches messages from the conversation.
|
|
50
|
-
*
|
|
51
|
-
* @param {Object} [options] Optional fetch options.
|
|
52
|
-
* @param {Date} [options.from] Fetch messages after this timestamp.
|
|
53
|
-
*
|
|
54
|
-
* @returns {Promise<TaskMessage[]>} Array of messages in ascending order.
|
|
55
|
-
* @throws {Error} if the agent or task ID is missing.
|
|
56
|
-
*/
|
|
57
|
-
fetchMessages({ from }?: {
|
|
58
|
-
from?: Date;
|
|
59
|
-
}): Promise<TaskMessage[]>;
|
|
60
|
-
}
|
|
61
|
-
export {};
|