@relevanceai/sdk 3.0.0-alpha.3 → 3.0.0-alpha.5
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 +195 -100
- package/esm/agent.d.ts +41 -8
- package/esm/agent.js +150 -13
- package/esm/client.d.ts +6 -1
- package/esm/client.js +38 -4
- package/esm/event.d.ts +6 -25
- package/esm/event.js +4 -19
- package/esm/key.d.ts +1 -1
- package/esm/message/agent-error.d.ts +7 -2
- package/esm/message/agent-error.js +8 -2
- package/esm/message/agent.d.ts +2 -2
- package/esm/message/agent.js +2 -2
- package/esm/message/task.d.ts +25 -4
- package/esm/message/task.js +26 -2
- package/esm/message/tool.d.ts +16 -6
- package/esm/message/tool.js +32 -6
- package/esm/message/user.d.ts +2 -2
- package/esm/message/user.js +2 -2
- package/esm/message/workforce-agent-handover.d.ts +23 -0
- package/esm/message/workforce-agent-handover.js +3 -0
- package/esm/message/workforce-agent.d.ts +23 -0
- package/esm/message/workforce-agent.js +3 -0
- package/esm/mod.d.ts +1 -6
- package/esm/mod.js +1 -1
- package/esm/task/agent-strategy.d.ts +26 -0
- package/esm/task/agent-strategy.js +63 -0
- package/esm/task/task.d.ts +59 -0
- package/esm/task/task.js +166 -0
- package/esm/task/workforce-strategy.d.ts +17 -0
- package/esm/task/workforce-strategy.js +68 -0
- package/esm/utils.d.ts +1 -0
- package/esm/utils.js +4 -0
- package/esm/workforce.d.ts +22 -0
- package/esm/workforce.js +50 -0
- package/package.json +1 -1
- package/script/agent.d.ts +41 -8
- package/script/agent.js +151 -13
- package/script/client.d.ts +6 -1
- package/script/client.js +37 -3
- package/script/event.d.ts +6 -25
- package/script/event.js +6 -23
- package/script/key.d.ts +1 -1
- package/script/message/agent-error.d.ts +7 -2
- package/script/message/agent-error.js +7 -1
- package/script/message/agent.d.ts +2 -2
- package/script/message/agent.js +1 -1
- package/script/message/task.d.ts +25 -4
- package/script/message/task.js +28 -4
- package/script/message/tool.d.ts +16 -6
- package/script/message/tool.js +31 -5
- package/script/message/user.d.ts +2 -2
- package/script/message/user.js +1 -1
- package/script/message/workforce-agent-handover.d.ts +23 -0
- package/script/message/workforce-agent-handover.js +7 -0
- package/script/message/workforce-agent.d.ts +23 -0
- package/script/message/workforce-agent.js +7 -0
- package/script/mod.d.ts +1 -6
- package/script/mod.js +3 -3
- package/script/task/agent-strategy.d.ts +26 -0
- package/script/task/agent-strategy.js +67 -0
- package/script/task/task.d.ts +59 -0
- package/script/task/task.js +170 -0
- package/script/task/workforce-strategy.d.ts +17 -0
- package/script/task/workforce-strategy.js +72 -0
- package/script/utils.d.ts +1 -0
- package/script/utils.js +5 -0
- package/script/workforce.d.ts +22 -0
- package/script/workforce.js +54 -0
- package/esm/task.d.ts +0 -57
- package/esm/task.js +0 -259
- package/script/task.d.ts +0 -57
- package/script/task.js +0 -263
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { GenericMessage } from "./task.js";
|
|
2
|
+
export interface WorkforceAgentHandoverMessageContent {
|
|
3
|
+
type: "workforce-agent-handover";
|
|
4
|
+
task_details?: {
|
|
5
|
+
project: string;
|
|
6
|
+
region: string;
|
|
7
|
+
conversation_id: string;
|
|
8
|
+
};
|
|
9
|
+
agent_details: {
|
|
10
|
+
agent_id: string;
|
|
11
|
+
project: string;
|
|
12
|
+
region: string;
|
|
13
|
+
name?: string;
|
|
14
|
+
emoji?: string;
|
|
15
|
+
description?: string;
|
|
16
|
+
deleted_reason?: "entity_deleted";
|
|
17
|
+
};
|
|
18
|
+
trigger: {
|
|
19
|
+
message: string;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export declare class WorkforceAgentHandoverMessage extends GenericMessage<WorkforceAgentHandoverMessageContent> {
|
|
23
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { AgentTaskState } from "../agent.js";
|
|
2
|
+
import { GenericMessage } from "./task.js";
|
|
3
|
+
export interface WorkforceAgentMessageContent {
|
|
4
|
+
type: "workforce-agent-run";
|
|
5
|
+
task_details: {
|
|
6
|
+
project: string;
|
|
7
|
+
region: string;
|
|
8
|
+
conversation_id: string;
|
|
9
|
+
finished_state?: AgentTaskState;
|
|
10
|
+
current_state?: AgentTaskState;
|
|
11
|
+
};
|
|
12
|
+
agent_details?: {
|
|
13
|
+
agent_id: string;
|
|
14
|
+
project: string;
|
|
15
|
+
region: string;
|
|
16
|
+
name?: string;
|
|
17
|
+
emoji?: string;
|
|
18
|
+
description?: string;
|
|
19
|
+
deleted_reason?: "entity_deleted";
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export declare class WorkforceAgentMessage extends GenericMessage<WorkforceAgentMessageContent> {
|
|
23
|
+
}
|
package/esm/mod.d.ts
CHANGED
|
@@ -2,9 +2,4 @@ export { Agent } from "./agent.js";
|
|
|
2
2
|
export { Client, createClient } from "./client.js";
|
|
3
3
|
export { Key } from "./key.js";
|
|
4
4
|
export { type Region, REGION_AU, REGION_EU, REGION_US } from "./region.js";
|
|
5
|
-
export {
|
|
6
|
-
export type { AgentErrorMessage } from "./message/agent-error.js";
|
|
7
|
-
export type { AgentMessage } from "./message/agent.js";
|
|
8
|
-
export type { ToolMessage } from "./message/tool.js";
|
|
9
|
-
export type { UserMessage } from "./message/user.js";
|
|
10
|
-
export type { AnyTaskMessage } from "./message/task.js";
|
|
5
|
+
export { Workforce } from "./workforce.js";
|
package/esm/mod.js
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type Agent, type AgentTaskState } from "../agent.js";
|
|
2
|
+
import { Client } from "../client.js";
|
|
3
|
+
import type { AnyTaskMessage } from "../message/task.js";
|
|
4
|
+
import { Task, type TaskMetadata, type TaskStrategy } from "./task.js";
|
|
5
|
+
export type AgentTaskMetadata = {
|
|
6
|
+
knowledge_set: string;
|
|
7
|
+
insert_date: string;
|
|
8
|
+
update_date: string;
|
|
9
|
+
conversation: {
|
|
10
|
+
created_by_user_id: string;
|
|
11
|
+
state: AgentTaskState;
|
|
12
|
+
title: string;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
export declare class AgentStrategy implements TaskStrategy<Agent> {
|
|
16
|
+
static get(id: string, agent: Agent, client?: Client): Promise<Task<Agent>>;
|
|
17
|
+
private readonly id;
|
|
18
|
+
private readonly agent;
|
|
19
|
+
private readonly client;
|
|
20
|
+
constructor(id: string, agent: Agent, client: Client);
|
|
21
|
+
get subject(): Agent;
|
|
22
|
+
getMetadata(): Promise<TaskMetadata>;
|
|
23
|
+
getMessages({ after }?: {
|
|
24
|
+
after?: Date;
|
|
25
|
+
}): Promise<AnyTaskMessage[]>;
|
|
26
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { stateToStatus } from "../agent.js";
|
|
2
|
+
import { Client } from "../client.js";
|
|
3
|
+
import { AgentErrorMessage, } from "../message/agent-error.js";
|
|
4
|
+
import { AgentMessage } from "../message/agent.js";
|
|
5
|
+
import { ToolMessage } from "../message/tool.js";
|
|
6
|
+
import { UserMessage } from "../message/user.js";
|
|
7
|
+
import { Task } from "./task.js";
|
|
8
|
+
export class AgentStrategy {
|
|
9
|
+
static async get(id, agent, client = Client.default()) {
|
|
10
|
+
const subject = new this(id, agent, client);
|
|
11
|
+
return new Task(await subject.getMetadata(), subject);
|
|
12
|
+
}
|
|
13
|
+
id;
|
|
14
|
+
agent;
|
|
15
|
+
client;
|
|
16
|
+
constructor(id, agent, client) {
|
|
17
|
+
this.id = id;
|
|
18
|
+
this.agent = agent;
|
|
19
|
+
this.client = client;
|
|
20
|
+
}
|
|
21
|
+
get subject() {
|
|
22
|
+
return this.agent;
|
|
23
|
+
}
|
|
24
|
+
async getMetadata() {
|
|
25
|
+
const { metadata } = await this.client.fetch(`/agents/${this.agent.id}/tasks/${this.id}/metadata`);
|
|
26
|
+
return {
|
|
27
|
+
id: this.id,
|
|
28
|
+
region: this.client.region,
|
|
29
|
+
project: this.client.project,
|
|
30
|
+
name: metadata.conversation.title,
|
|
31
|
+
status: stateToStatus(metadata.conversation.state),
|
|
32
|
+
createdAt: new Date(metadata.insert_date),
|
|
33
|
+
updatedAt: new Date(metadata.update_date),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
async getMessages({ after = new Date(0) } = {}) {
|
|
37
|
+
const url = `/agents/${this.agent.id}/tasks/${this.id}/view`;
|
|
38
|
+
const res = await this.client.fetch(url, {
|
|
39
|
+
method: "POST",
|
|
40
|
+
body: JSON.stringify({
|
|
41
|
+
page_size: 1_000, // @todo: pagination
|
|
42
|
+
cursor: {
|
|
43
|
+
after: after.toISOString(),
|
|
44
|
+
},
|
|
45
|
+
}),
|
|
46
|
+
});
|
|
47
|
+
// message should be in ascending order
|
|
48
|
+
return res.results.reverse().map((data) => {
|
|
49
|
+
switch (data.content.type) {
|
|
50
|
+
case "agent-error":
|
|
51
|
+
return new AgentErrorMessage(data);
|
|
52
|
+
case "agent-message":
|
|
53
|
+
return new AgentMessage(data);
|
|
54
|
+
case "tool-run":
|
|
55
|
+
return new ToolMessage(data);
|
|
56
|
+
case "user-message":
|
|
57
|
+
return new UserMessage(data);
|
|
58
|
+
default:
|
|
59
|
+
throw new Error("unknown message response");
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Emitter } from "../emitter.js";
|
|
2
|
+
import type { AnyTaskMessage } from "../message/task.js";
|
|
3
|
+
import type { Region } from "../region.js";
|
|
4
|
+
import type { Workforce } from "../workforce.js";
|
|
5
|
+
import type { Agent } from "../agent.js";
|
|
6
|
+
import type { AgentErrorMessage } from "../message/agent-error.js";
|
|
7
|
+
export type TaskStatus = "not-started" | "idle" | "paused" | "queued" | "running" | "action" | "completed" | "cancelled" | "error";
|
|
8
|
+
export interface TaskMetadata {
|
|
9
|
+
id: string;
|
|
10
|
+
region: Region;
|
|
11
|
+
project: string;
|
|
12
|
+
status: TaskStatus;
|
|
13
|
+
name: string;
|
|
14
|
+
createdAt: Date;
|
|
15
|
+
updatedAt: Date;
|
|
16
|
+
}
|
|
17
|
+
type TaskEventMap = {
|
|
18
|
+
updated: undefined;
|
|
19
|
+
message: {
|
|
20
|
+
message: AnyTaskMessage;
|
|
21
|
+
};
|
|
22
|
+
error: {
|
|
23
|
+
message: AgentErrorMessage;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
export interface TaskStrategy<S> {
|
|
27
|
+
subject: S;
|
|
28
|
+
getMessages(options?: {
|
|
29
|
+
after?: Date;
|
|
30
|
+
}): Promise<AnyTaskMessage[]>;
|
|
31
|
+
getMetadata(): Promise<TaskMetadata>;
|
|
32
|
+
}
|
|
33
|
+
export declare const resetBackoffDuration: unique symbol;
|
|
34
|
+
export declare class Task<S extends Agent | Workforce, E extends TaskEventMap = TaskEventMap> extends Emitter<E> {
|
|
35
|
+
#private;
|
|
36
|
+
private subscribed;
|
|
37
|
+
private backoff;
|
|
38
|
+
private backoffDuration;
|
|
39
|
+
private readonly strategy;
|
|
40
|
+
constructor(metadata: TaskMetadata, strategy: TaskStrategy<S>);
|
|
41
|
+
get subject(): S;
|
|
42
|
+
get id(): string;
|
|
43
|
+
get region(): Region;
|
|
44
|
+
get project(): string;
|
|
45
|
+
get name(): string;
|
|
46
|
+
get status(): TaskStatus;
|
|
47
|
+
get createdAt(): Date;
|
|
48
|
+
get updatedAt(): Date;
|
|
49
|
+
getMessages(options?: {
|
|
50
|
+
after?: Date;
|
|
51
|
+
}): Promise<AnyTaskMessage[]>;
|
|
52
|
+
isRunning(): boolean;
|
|
53
|
+
unsubscribe(): void;
|
|
54
|
+
[resetBackoffDuration](): void;
|
|
55
|
+
addEventListener<K extends keyof E>(type: Extract<K, string>, listener: ((event: CustomEvent<E[K]>) => void) | {
|
|
56
|
+
handleEvent: (event: CustomEvent<E[K]>) => void;
|
|
57
|
+
} | null, options?: boolean | AddEventListenerOptions): void;
|
|
58
|
+
}
|
|
59
|
+
export {};
|
package/esm/task/task.js
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { Emitter } from "../emitter.js";
|
|
2
|
+
import { abortPromise, delay } from "../utils.js";
|
|
3
|
+
import { TaskErrorEvent, TaskMessageEvent, TaskUpdatedEvent, } from "../event.js";
|
|
4
|
+
export const resetBackoffDuration = Symbol("resetBackoffDuration");
|
|
5
|
+
const backoffStartingDuration = 1_000;
|
|
6
|
+
const backoffMaxDuration = 60_000;
|
|
7
|
+
export class Task extends Emitter {
|
|
8
|
+
subscribed = null;
|
|
9
|
+
backoff = null;
|
|
10
|
+
backoffDuration = 0;
|
|
11
|
+
strategy;
|
|
12
|
+
#metadata;
|
|
13
|
+
constructor(metadata, strategy) {
|
|
14
|
+
super();
|
|
15
|
+
this.strategy = strategy;
|
|
16
|
+
this.#metadata = metadata;
|
|
17
|
+
}
|
|
18
|
+
get subject() {
|
|
19
|
+
return this.strategy.subject;
|
|
20
|
+
}
|
|
21
|
+
get id() {
|
|
22
|
+
return this.#metadata.id;
|
|
23
|
+
}
|
|
24
|
+
get region() {
|
|
25
|
+
return this.#metadata.region;
|
|
26
|
+
}
|
|
27
|
+
get project() {
|
|
28
|
+
return this.#metadata.project;
|
|
29
|
+
}
|
|
30
|
+
get name() {
|
|
31
|
+
return this.#metadata.name;
|
|
32
|
+
}
|
|
33
|
+
get status() {
|
|
34
|
+
return this.#metadata.status;
|
|
35
|
+
}
|
|
36
|
+
get createdAt() {
|
|
37
|
+
return this.#metadata.createdAt;
|
|
38
|
+
}
|
|
39
|
+
get updatedAt() {
|
|
40
|
+
return this.#metadata.updatedAt;
|
|
41
|
+
}
|
|
42
|
+
getMessages(options) {
|
|
43
|
+
return this.strategy.getMessages(options);
|
|
44
|
+
}
|
|
45
|
+
isRunning() {
|
|
46
|
+
switch (this.status) {
|
|
47
|
+
case "queued":
|
|
48
|
+
case "running":
|
|
49
|
+
return true;
|
|
50
|
+
default:
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
#subscribe() {
|
|
55
|
+
if (this.subscribed) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const subscribed = new AbortController();
|
|
59
|
+
this.subscribed = subscribed; // subscribed ref
|
|
60
|
+
const isSubscribed = () => !subscribed.signal.aborted;
|
|
61
|
+
this.backoffDuration = backoffStartingDuration;
|
|
62
|
+
const cursor = new Date(0);
|
|
63
|
+
const emitted = new Set();
|
|
64
|
+
const pending = new Map();
|
|
65
|
+
void (async () => {
|
|
66
|
+
while (isSubscribed()) {
|
|
67
|
+
try {
|
|
68
|
+
const [metadata, messages] = await Promise.all([
|
|
69
|
+
this.strategy.getMetadata(),
|
|
70
|
+
this.strategy.getMessages({ after: cursor }),
|
|
71
|
+
]);
|
|
72
|
+
if (!isSubscribed()) {
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
let hasChanges = false;
|
|
76
|
+
if (metadata.updatedAt > this.updatedAt) {
|
|
77
|
+
this.dispatchEvent(new TaskUpdatedEvent());
|
|
78
|
+
hasChanges = true;
|
|
79
|
+
}
|
|
80
|
+
this.#metadata = metadata;
|
|
81
|
+
if (messages.length) {
|
|
82
|
+
for (const message of messages) {
|
|
83
|
+
if (emitted.has(message.id)) {
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
switch (message.type) {
|
|
87
|
+
case "agent-error":
|
|
88
|
+
emitted.add(message.id);
|
|
89
|
+
this.dispatchEvent(new TaskErrorEvent(message));
|
|
90
|
+
break;
|
|
91
|
+
case "tool-run": {
|
|
92
|
+
const { status } = message;
|
|
93
|
+
if (pending.get(message.id)?.status == status) {
|
|
94
|
+
// no change to the tool status
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (["pending", "running"].includes(status)) {
|
|
98
|
+
pending.set(message.id, message);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
emitted.add(message.id);
|
|
102
|
+
pending.delete(message.id);
|
|
103
|
+
}
|
|
104
|
+
this.dispatchEvent(new TaskMessageEvent(message));
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
case "agent-message":
|
|
108
|
+
case "user-message":
|
|
109
|
+
hasChanges = true;
|
|
110
|
+
emitted.add(message.id);
|
|
111
|
+
this.dispatchEvent(new TaskMessageEvent(message));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
let latest = messages.at(-1).createdAt;
|
|
115
|
+
for (const message of pending.values()) {
|
|
116
|
+
if (latest > message.createdAt) {
|
|
117
|
+
latest = message.createdAt;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
cursor.setTime(latest.getTime());
|
|
121
|
+
}
|
|
122
|
+
if (hasChanges) {
|
|
123
|
+
this.backoffDuration = backoffStartingDuration;
|
|
124
|
+
}
|
|
125
|
+
else if (!this.isRunning()) {
|
|
126
|
+
this.backoffDuration = Math.min(this.backoffDuration * 2, backoffMaxDuration);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
if (isSubscribed()) {
|
|
131
|
+
const backoff = new AbortController();
|
|
132
|
+
this.backoff = backoff;
|
|
133
|
+
await Promise.race([
|
|
134
|
+
delay(this.backoffDuration),
|
|
135
|
+
abortPromise(AbortSignal.any([subscribed.signal, backoff.signal])),
|
|
136
|
+
]);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
})();
|
|
141
|
+
}
|
|
142
|
+
unsubscribe() {
|
|
143
|
+
this.subscribed?.abort();
|
|
144
|
+
this.subscribed = null;
|
|
145
|
+
this.backoff?.abort();
|
|
146
|
+
this.backoff = null;
|
|
147
|
+
this.backoffDuration = 0;
|
|
148
|
+
}
|
|
149
|
+
[resetBackoffDuration]() {
|
|
150
|
+
this.backoffDuration = backoffStartingDuration;
|
|
151
|
+
}
|
|
152
|
+
addEventListener(type, listener, options) {
|
|
153
|
+
this.#subscribe();
|
|
154
|
+
const signal = AbortSignal.any([
|
|
155
|
+
...(options && typeof options === "object" && options.signal
|
|
156
|
+
? [options.signal]
|
|
157
|
+
: []),
|
|
158
|
+
...(this.subscribed ? [this.subscribed.signal] : []),
|
|
159
|
+
]);
|
|
160
|
+
const capture = typeof options === "boolean"
|
|
161
|
+
? options
|
|
162
|
+
: Boolean(options?.capture);
|
|
163
|
+
const addOptions = Object.assign({}, options, { signal, capture });
|
|
164
|
+
super.addEventListener(type, listener, addOptions);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { AnyTaskMessage } from "../message/task.js";
|
|
2
|
+
import { Client } from "../client.js";
|
|
3
|
+
import { Task, type TaskMetadata, type TaskStatus, type TaskStrategy } from "./task.js";
|
|
4
|
+
import type { Workforce } from "../workforce.js";
|
|
5
|
+
export declare class WorkforceStrategy implements TaskStrategy<Workforce> {
|
|
6
|
+
static get(id: string, workforce: Workforce, client?: Client): Promise<Task<Workforce>>;
|
|
7
|
+
static convertStatus(): TaskStatus;
|
|
8
|
+
private readonly id;
|
|
9
|
+
private readonly workforce;
|
|
10
|
+
private readonly client;
|
|
11
|
+
private constructor();
|
|
12
|
+
get subject(): Workforce;
|
|
13
|
+
getMessages({ after: _after }: {
|
|
14
|
+
after?: Date;
|
|
15
|
+
}): Promise<AnyTaskMessage[]>;
|
|
16
|
+
getMetadata(): Promise<TaskMetadata>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Client } from "../client.js";
|
|
2
|
+
import { Task, } from "./task.js";
|
|
3
|
+
import { AgentErrorMessage, } from "../message/agent-error.js";
|
|
4
|
+
import { AgentMessage } from "../message/agent.js";
|
|
5
|
+
import { ToolMessage } from "../message/tool.js";
|
|
6
|
+
import { UserMessage } from "../message/user.js";
|
|
7
|
+
import { WorkforceAgentMessage, } from "../message/workforce-agent.js";
|
|
8
|
+
import { WorkforceAgentHandoverMessage, } from "../message/workforce-agent-handover.js";
|
|
9
|
+
export class WorkforceStrategy {
|
|
10
|
+
static async get(id, workforce, client = Client.default()) {
|
|
11
|
+
const subject = new this(id, workforce, client);
|
|
12
|
+
const metadata = await subject.getMetadata();
|
|
13
|
+
return new Task(metadata, subject);
|
|
14
|
+
}
|
|
15
|
+
static convertStatus() {
|
|
16
|
+
return "not-implemented";
|
|
17
|
+
}
|
|
18
|
+
id;
|
|
19
|
+
workforce;
|
|
20
|
+
client;
|
|
21
|
+
constructor(id, workforce, client) {
|
|
22
|
+
this.id = id;
|
|
23
|
+
this.workforce = workforce;
|
|
24
|
+
this.client = client;
|
|
25
|
+
}
|
|
26
|
+
get subject() {
|
|
27
|
+
return this.workforce;
|
|
28
|
+
}
|
|
29
|
+
async getMessages({ after: _after }) {
|
|
30
|
+
const { results } = await this.client.fetch(`/workforce/items/${this.workforce.id}/tasks/${this.id}/messages`, {
|
|
31
|
+
method: "POST",
|
|
32
|
+
body: JSON.stringify({
|
|
33
|
+
page_size: 1_000, // @todo: pagination
|
|
34
|
+
// @todo: cursor "before" :S
|
|
35
|
+
}),
|
|
36
|
+
});
|
|
37
|
+
return results.map((data) => {
|
|
38
|
+
switch (data.content.type) {
|
|
39
|
+
case "agent-error":
|
|
40
|
+
return new AgentErrorMessage(data);
|
|
41
|
+
case "agent-message":
|
|
42
|
+
return new AgentMessage(data);
|
|
43
|
+
case "tool-run":
|
|
44
|
+
return new ToolMessage(data);
|
|
45
|
+
case "user-message":
|
|
46
|
+
return new UserMessage(data);
|
|
47
|
+
case "workforce-agent-run":
|
|
48
|
+
return new WorkforceAgentMessage(data);
|
|
49
|
+
case "workforce-agent-handover":
|
|
50
|
+
return new WorkforceAgentHandoverMessage(data);
|
|
51
|
+
default:
|
|
52
|
+
throw new Error("unknown message response");
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
async getMetadata() {
|
|
57
|
+
const { metadata } = await this.client.fetch(`/workforce/tasks/${this.id}/metadata`);
|
|
58
|
+
return {
|
|
59
|
+
id: this.id,
|
|
60
|
+
region: this.client.region,
|
|
61
|
+
project: this.client.project,
|
|
62
|
+
name: metadata.title,
|
|
63
|
+
status: WorkforceStrategy.convertStatus(), // @todo: not implemented
|
|
64
|
+
createdAt: new Date(metadata.insert_date),
|
|
65
|
+
updatedAt: new Date(metadata.update_date),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
package/esm/utils.d.ts
CHANGED
|
@@ -2,3 +2,4 @@ export declare function abortPromise(signal: AbortSignal, reject?: boolean): Pro
|
|
|
2
2
|
export declare function delay(timeout: number | (() => number)): Promise<void>;
|
|
3
3
|
export declare function cleanPath(path: string, version?: string): string;
|
|
4
4
|
export declare function randomUUID(): Promise<any>;
|
|
5
|
+
export declare function getFileExtension(filename: string): string;
|
package/esm/utils.js
CHANGED
|
@@ -15,3 +15,7 @@ export async function randomUUID() {
|
|
|
15
15
|
const cryptoModule = await import("node:crypto");
|
|
16
16
|
return cryptoModule.randomUUID();
|
|
17
17
|
}
|
|
18
|
+
export function getFileExtension(filename) {
|
|
19
|
+
const dot = filename.lastIndexOf(".");
|
|
20
|
+
return dot >= 0 ? filename.slice(dot + 1) : "";
|
|
21
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Client } from "./client.js";
|
|
2
|
+
import type { Region } from "./region.js";
|
|
3
|
+
import { type Task } from "./task/task.js";
|
|
4
|
+
type WorkforceConfig = {
|
|
5
|
+
id: string;
|
|
6
|
+
workforce_metadata: {
|
|
7
|
+
name: string;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
export declare class Workforce {
|
|
11
|
+
#private;
|
|
12
|
+
static get(id: string, client?: Client): Promise<Workforce>;
|
|
13
|
+
private readonly client;
|
|
14
|
+
constructor(config: WorkforceConfig, client: Client);
|
|
15
|
+
get id(): string;
|
|
16
|
+
get region(): Region;
|
|
17
|
+
get project(): string;
|
|
18
|
+
get name(): string;
|
|
19
|
+
getTask(id: string): Promise<Task<Workforce>>;
|
|
20
|
+
sendMessage(message: string, task?: Task<Workforce>): Promise<Task<Workforce>>;
|
|
21
|
+
}
|
|
22
|
+
export {};
|
package/esm/workforce.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Client } from "./client.js";
|
|
2
|
+
import { resetBackoffDuration } from "./task/task.js";
|
|
3
|
+
import { WorkforceStrategy } from "./task/workforce-strategy.js";
|
|
4
|
+
export class Workforce {
|
|
5
|
+
static async get(id, client = Client.default()) {
|
|
6
|
+
const config = await client.fetch(`/workforce/items/${id}`);
|
|
7
|
+
return new Workforce(config, client);
|
|
8
|
+
}
|
|
9
|
+
client;
|
|
10
|
+
#config;
|
|
11
|
+
constructor(config, client) {
|
|
12
|
+
this.#config = config;
|
|
13
|
+
this.client = client;
|
|
14
|
+
}
|
|
15
|
+
get id() {
|
|
16
|
+
return this.#config.id;
|
|
17
|
+
}
|
|
18
|
+
get region() {
|
|
19
|
+
return this.client.region;
|
|
20
|
+
}
|
|
21
|
+
get project() {
|
|
22
|
+
return this.client.project;
|
|
23
|
+
}
|
|
24
|
+
get name() {
|
|
25
|
+
return this.#config.workforce_metadata.name;
|
|
26
|
+
}
|
|
27
|
+
async getTask(id) {
|
|
28
|
+
return await WorkforceStrategy.get(id, this, this.client);
|
|
29
|
+
}
|
|
30
|
+
async sendMessage(message, task) {
|
|
31
|
+
const { workforce_task_id: taskId } = await this.client.fetch("/workforce/trigger", {
|
|
32
|
+
method: "POST",
|
|
33
|
+
body: JSON.stringify({
|
|
34
|
+
workforce_id: this.id,
|
|
35
|
+
workforce_task_id: task?.id,
|
|
36
|
+
trigger: {
|
|
37
|
+
message: {
|
|
38
|
+
role: "user",
|
|
39
|
+
content: message,
|
|
40
|
+
attachments: [],
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
}),
|
|
44
|
+
});
|
|
45
|
+
if (task) {
|
|
46
|
+
task[resetBackoffDuration]();
|
|
47
|
+
}
|
|
48
|
+
return task ?? this.getTask(taskId);
|
|
49
|
+
}
|
|
50
|
+
}
|
package/package.json
CHANGED
package/script/agent.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Client } from "./client.js";
|
|
1
|
+
import { type Attachment, Client } from "./client.js";
|
|
2
2
|
import type { Region } from "./region.js";
|
|
3
|
-
import { Task } from "./task.js";
|
|
4
|
-
interface AgentConfig {
|
|
3
|
+
import { Task, type TaskStatus } from "./task/task.js";
|
|
4
|
+
export interface AgentConfig {
|
|
5
5
|
agent_id: string;
|
|
6
6
|
public: boolean;
|
|
7
7
|
name?: string;
|
|
@@ -9,21 +9,54 @@ interface AgentConfig {
|
|
|
9
9
|
emoji?: string;
|
|
10
10
|
insert_date_: string;
|
|
11
11
|
update_date_: string;
|
|
12
|
+
model: string;
|
|
12
13
|
}
|
|
14
|
+
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";
|
|
15
|
+
/**
|
|
16
|
+
* Converts an AgentTaskState to a simplified TaskStatus.
|
|
17
|
+
*
|
|
18
|
+
* @dev
|
|
19
|
+
* We want to simplify because our states are simplified in the UI and also
|
|
20
|
+
* some states have combined reasoning that the consumer does not need to care
|
|
21
|
+
* about. i.e. "queued-for-approval" "queued-for-rerun" should just be "queued".
|
|
22
|
+
*
|
|
23
|
+
* @param {AgentTaskState} state The agent task state to convert.
|
|
24
|
+
* @returns {TaskStatus} The simplified task status.
|
|
25
|
+
*/
|
|
26
|
+
export declare function stateToStatus(state: AgentTaskState): TaskStatus;
|
|
27
|
+
type SortDirection = "asc" | "desc";
|
|
28
|
+
type GetTaskOptionSort = {
|
|
29
|
+
createdAt: SortDirection;
|
|
30
|
+
} | {
|
|
31
|
+
updatedAt: SortDirection;
|
|
32
|
+
};
|
|
33
|
+
type GetTaskOptions = {
|
|
34
|
+
pageSize?: number;
|
|
35
|
+
page?: number;
|
|
36
|
+
sort?: GetTaskOptionSort;
|
|
37
|
+
search?: string;
|
|
38
|
+
filter?: {
|
|
39
|
+
status?: TaskStatus[];
|
|
40
|
+
};
|
|
41
|
+
};
|
|
13
42
|
export declare class Agent {
|
|
14
43
|
#private;
|
|
15
|
-
private readonly client;
|
|
16
44
|
static get(id: string, client?: Client): Promise<Agent>;
|
|
45
|
+
private readonly client;
|
|
17
46
|
constructor(config: AgentConfig, client: Client);
|
|
18
47
|
get id(): string;
|
|
48
|
+
get region(): Region;
|
|
49
|
+
get project(): string;
|
|
19
50
|
get name(): string | undefined;
|
|
20
51
|
get description(): string | undefined;
|
|
21
52
|
get avatar(): string | undefined;
|
|
22
53
|
get createdAt(): Date;
|
|
23
54
|
get updatedAt(): Date;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
sendMessage(message: string, task
|
|
55
|
+
getTask(taskId: string): Promise<Task<Agent>>;
|
|
56
|
+
getTasks({ sort, pageSize, page, search, filter, }?: GetTaskOptions): Promise<Task<Agent>[]>;
|
|
57
|
+
sendMessage(message: string): Promise<Task<Agent>>;
|
|
58
|
+
sendMessage(message: string, task: Task<Agent>): Promise<Task<Agent>>;
|
|
59
|
+
sendMessage(message: string, attachments: (Attachment | File)[]): Promise<Task<Agent>>;
|
|
60
|
+
sendMessage(message: string, attachments: (Attachment | File)[], task: Task<Agent>): Promise<Task<Agent>>;
|
|
28
61
|
}
|
|
29
62
|
export {};
|