@relevanceai/sdk 3.0.0-alpha.4 → 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
package/script/message/user.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.UserMessage = void 0;
|
|
4
4
|
const task_js_1 = require("./task.js");
|
|
5
|
-
class UserMessage extends task_js_1.
|
|
5
|
+
class UserMessage extends task_js_1.GenericMessage {
|
|
6
6
|
/**
|
|
7
7
|
* The message as text sent.
|
|
8
8
|
*
|
|
@@ -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,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WorkforceAgentHandoverMessage = void 0;
|
|
4
|
+
const task_js_1 = require("./task.js");
|
|
5
|
+
class WorkforceAgentHandoverMessage extends task_js_1.GenericMessage {
|
|
6
|
+
}
|
|
7
|
+
exports.WorkforceAgentHandoverMessage = WorkforceAgentHandoverMessage;
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WorkforceAgentMessage = void 0;
|
|
4
|
+
const task_js_1 = require("./task.js");
|
|
5
|
+
class WorkforceAgentMessage extends task_js_1.GenericMessage {
|
|
6
|
+
}
|
|
7
|
+
exports.WorkforceAgentMessage = WorkforceAgentMessage;
|
package/script/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/script/mod.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.Workforce = exports.REGION_US = exports.REGION_EU = exports.REGION_AU = exports.Key = exports.createClient = exports.Client = exports.Agent = void 0;
|
|
4
4
|
var agent_js_1 = require("./agent.js");
|
|
5
5
|
Object.defineProperty(exports, "Agent", { enumerable: true, get: function () { return agent_js_1.Agent; } });
|
|
6
6
|
var client_js_1 = require("./client.js");
|
|
@@ -12,5 +12,5 @@ var region_js_1 = require("./region.js");
|
|
|
12
12
|
Object.defineProperty(exports, "REGION_AU", { enumerable: true, get: function () { return region_js_1.REGION_AU; } });
|
|
13
13
|
Object.defineProperty(exports, "REGION_EU", { enumerable: true, get: function () { return region_js_1.REGION_EU; } });
|
|
14
14
|
Object.defineProperty(exports, "REGION_US", { enumerable: true, get: function () { return region_js_1.REGION_US; } });
|
|
15
|
-
var
|
|
16
|
-
Object.defineProperty(exports, "
|
|
15
|
+
var workforce_js_1 = require("./workforce.js");
|
|
16
|
+
Object.defineProperty(exports, "Workforce", { enumerable: true, get: function () { return workforce_js_1.Workforce; } });
|
|
@@ -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,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AgentStrategy = void 0;
|
|
4
|
+
const agent_js_1 = require("../agent.js");
|
|
5
|
+
const client_js_1 = require("../client.js");
|
|
6
|
+
const agent_error_js_1 = require("../message/agent-error.js");
|
|
7
|
+
const agent_js_2 = require("../message/agent.js");
|
|
8
|
+
const tool_js_1 = require("../message/tool.js");
|
|
9
|
+
const user_js_1 = require("../message/user.js");
|
|
10
|
+
const task_js_1 = require("./task.js");
|
|
11
|
+
class AgentStrategy {
|
|
12
|
+
static async get(id, agent, client = client_js_1.Client.default()) {
|
|
13
|
+
const subject = new this(id, agent, client);
|
|
14
|
+
return new task_js_1.Task(await subject.getMetadata(), subject);
|
|
15
|
+
}
|
|
16
|
+
id;
|
|
17
|
+
agent;
|
|
18
|
+
client;
|
|
19
|
+
constructor(id, agent, client) {
|
|
20
|
+
this.id = id;
|
|
21
|
+
this.agent = agent;
|
|
22
|
+
this.client = client;
|
|
23
|
+
}
|
|
24
|
+
get subject() {
|
|
25
|
+
return this.agent;
|
|
26
|
+
}
|
|
27
|
+
async getMetadata() {
|
|
28
|
+
const { metadata } = await this.client.fetch(`/agents/${this.agent.id}/tasks/${this.id}/metadata`);
|
|
29
|
+
return {
|
|
30
|
+
id: this.id,
|
|
31
|
+
region: this.client.region,
|
|
32
|
+
project: this.client.project,
|
|
33
|
+
name: metadata.conversation.title,
|
|
34
|
+
status: (0, agent_js_1.stateToStatus)(metadata.conversation.state),
|
|
35
|
+
createdAt: new Date(metadata.insert_date),
|
|
36
|
+
updatedAt: new Date(metadata.update_date),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
async getMessages({ after = new Date(0) } = {}) {
|
|
40
|
+
const url = `/agents/${this.agent.id}/tasks/${this.id}/view`;
|
|
41
|
+
const res = await this.client.fetch(url, {
|
|
42
|
+
method: "POST",
|
|
43
|
+
body: JSON.stringify({
|
|
44
|
+
page_size: 1_000, // @todo: pagination
|
|
45
|
+
cursor: {
|
|
46
|
+
after: after.toISOString(),
|
|
47
|
+
},
|
|
48
|
+
}),
|
|
49
|
+
});
|
|
50
|
+
// message should be in ascending order
|
|
51
|
+
return res.results.reverse().map((data) => {
|
|
52
|
+
switch (data.content.type) {
|
|
53
|
+
case "agent-error":
|
|
54
|
+
return new agent_error_js_1.AgentErrorMessage(data);
|
|
55
|
+
case "agent-message":
|
|
56
|
+
return new agent_js_2.AgentMessage(data);
|
|
57
|
+
case "tool-run":
|
|
58
|
+
return new tool_js_1.ToolMessage(data);
|
|
59
|
+
case "user-message":
|
|
60
|
+
return new user_js_1.UserMessage(data);
|
|
61
|
+
default:
|
|
62
|
+
throw new Error("unknown message response");
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.AgentStrategy = AgentStrategy;
|
|
@@ -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 {};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Task = exports.resetBackoffDuration = void 0;
|
|
4
|
+
const emitter_js_1 = require("../emitter.js");
|
|
5
|
+
const utils_js_1 = require("../utils.js");
|
|
6
|
+
const event_js_1 = require("../event.js");
|
|
7
|
+
exports.resetBackoffDuration = Symbol("resetBackoffDuration");
|
|
8
|
+
const backoffStartingDuration = 1_000;
|
|
9
|
+
const backoffMaxDuration = 60_000;
|
|
10
|
+
class Task extends emitter_js_1.Emitter {
|
|
11
|
+
subscribed = null;
|
|
12
|
+
backoff = null;
|
|
13
|
+
backoffDuration = 0;
|
|
14
|
+
strategy;
|
|
15
|
+
#metadata;
|
|
16
|
+
constructor(metadata, strategy) {
|
|
17
|
+
super();
|
|
18
|
+
this.strategy = strategy;
|
|
19
|
+
this.#metadata = metadata;
|
|
20
|
+
}
|
|
21
|
+
get subject() {
|
|
22
|
+
return this.strategy.subject;
|
|
23
|
+
}
|
|
24
|
+
get id() {
|
|
25
|
+
return this.#metadata.id;
|
|
26
|
+
}
|
|
27
|
+
get region() {
|
|
28
|
+
return this.#metadata.region;
|
|
29
|
+
}
|
|
30
|
+
get project() {
|
|
31
|
+
return this.#metadata.project;
|
|
32
|
+
}
|
|
33
|
+
get name() {
|
|
34
|
+
return this.#metadata.name;
|
|
35
|
+
}
|
|
36
|
+
get status() {
|
|
37
|
+
return this.#metadata.status;
|
|
38
|
+
}
|
|
39
|
+
get createdAt() {
|
|
40
|
+
return this.#metadata.createdAt;
|
|
41
|
+
}
|
|
42
|
+
get updatedAt() {
|
|
43
|
+
return this.#metadata.updatedAt;
|
|
44
|
+
}
|
|
45
|
+
getMessages(options) {
|
|
46
|
+
return this.strategy.getMessages(options);
|
|
47
|
+
}
|
|
48
|
+
isRunning() {
|
|
49
|
+
switch (this.status) {
|
|
50
|
+
case "queued":
|
|
51
|
+
case "running":
|
|
52
|
+
return true;
|
|
53
|
+
default:
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
#subscribe() {
|
|
58
|
+
if (this.subscribed) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const subscribed = new AbortController();
|
|
62
|
+
this.subscribed = subscribed; // subscribed ref
|
|
63
|
+
const isSubscribed = () => !subscribed.signal.aborted;
|
|
64
|
+
this.backoffDuration = backoffStartingDuration;
|
|
65
|
+
const cursor = new Date(0);
|
|
66
|
+
const emitted = new Set();
|
|
67
|
+
const pending = new Map();
|
|
68
|
+
void (async () => {
|
|
69
|
+
while (isSubscribed()) {
|
|
70
|
+
try {
|
|
71
|
+
const [metadata, messages] = await Promise.all([
|
|
72
|
+
this.strategy.getMetadata(),
|
|
73
|
+
this.strategy.getMessages({ after: cursor }),
|
|
74
|
+
]);
|
|
75
|
+
if (!isSubscribed()) {
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
let hasChanges = false;
|
|
79
|
+
if (metadata.updatedAt > this.updatedAt) {
|
|
80
|
+
this.dispatchEvent(new event_js_1.TaskUpdatedEvent());
|
|
81
|
+
hasChanges = true;
|
|
82
|
+
}
|
|
83
|
+
this.#metadata = metadata;
|
|
84
|
+
if (messages.length) {
|
|
85
|
+
for (const message of messages) {
|
|
86
|
+
if (emitted.has(message.id)) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
switch (message.type) {
|
|
90
|
+
case "agent-error":
|
|
91
|
+
emitted.add(message.id);
|
|
92
|
+
this.dispatchEvent(new event_js_1.TaskErrorEvent(message));
|
|
93
|
+
break;
|
|
94
|
+
case "tool-run": {
|
|
95
|
+
const { status } = message;
|
|
96
|
+
if (pending.get(message.id)?.status == status) {
|
|
97
|
+
// no change to the tool status
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (["pending", "running"].includes(status)) {
|
|
101
|
+
pending.set(message.id, message);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
emitted.add(message.id);
|
|
105
|
+
pending.delete(message.id);
|
|
106
|
+
}
|
|
107
|
+
this.dispatchEvent(new event_js_1.TaskMessageEvent(message));
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
case "agent-message":
|
|
111
|
+
case "user-message":
|
|
112
|
+
hasChanges = true;
|
|
113
|
+
emitted.add(message.id);
|
|
114
|
+
this.dispatchEvent(new event_js_1.TaskMessageEvent(message));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
let latest = messages.at(-1).createdAt;
|
|
118
|
+
for (const message of pending.values()) {
|
|
119
|
+
if (latest > message.createdAt) {
|
|
120
|
+
latest = message.createdAt;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
cursor.setTime(latest.getTime());
|
|
124
|
+
}
|
|
125
|
+
if (hasChanges) {
|
|
126
|
+
this.backoffDuration = backoffStartingDuration;
|
|
127
|
+
}
|
|
128
|
+
else if (!this.isRunning()) {
|
|
129
|
+
this.backoffDuration = Math.min(this.backoffDuration * 2, backoffMaxDuration);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
if (isSubscribed()) {
|
|
134
|
+
const backoff = new AbortController();
|
|
135
|
+
this.backoff = backoff;
|
|
136
|
+
await Promise.race([
|
|
137
|
+
(0, utils_js_1.delay)(this.backoffDuration),
|
|
138
|
+
(0, utils_js_1.abortPromise)(AbortSignal.any([subscribed.signal, backoff.signal])),
|
|
139
|
+
]);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
})();
|
|
144
|
+
}
|
|
145
|
+
unsubscribe() {
|
|
146
|
+
this.subscribed?.abort();
|
|
147
|
+
this.subscribed = null;
|
|
148
|
+
this.backoff?.abort();
|
|
149
|
+
this.backoff = null;
|
|
150
|
+
this.backoffDuration = 0;
|
|
151
|
+
}
|
|
152
|
+
[exports.resetBackoffDuration]() {
|
|
153
|
+
this.backoffDuration = backoffStartingDuration;
|
|
154
|
+
}
|
|
155
|
+
addEventListener(type, listener, options) {
|
|
156
|
+
this.#subscribe();
|
|
157
|
+
const signal = AbortSignal.any([
|
|
158
|
+
...(options && typeof options === "object" && options.signal
|
|
159
|
+
? [options.signal]
|
|
160
|
+
: []),
|
|
161
|
+
...(this.subscribed ? [this.subscribed.signal] : []),
|
|
162
|
+
]);
|
|
163
|
+
const capture = typeof options === "boolean"
|
|
164
|
+
? options
|
|
165
|
+
: Boolean(options?.capture);
|
|
166
|
+
const addOptions = Object.assign({}, options, { signal, capture });
|
|
167
|
+
super.addEventListener(type, listener, addOptions);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
exports.Task = Task;
|
|
@@ -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,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WorkforceStrategy = void 0;
|
|
4
|
+
const client_js_1 = require("../client.js");
|
|
5
|
+
const task_js_1 = require("./task.js");
|
|
6
|
+
const agent_error_js_1 = require("../message/agent-error.js");
|
|
7
|
+
const agent_js_1 = require("../message/agent.js");
|
|
8
|
+
const tool_js_1 = require("../message/tool.js");
|
|
9
|
+
const user_js_1 = require("../message/user.js");
|
|
10
|
+
const workforce_agent_js_1 = require("../message/workforce-agent.js");
|
|
11
|
+
const workforce_agent_handover_js_1 = require("../message/workforce-agent-handover.js");
|
|
12
|
+
class WorkforceStrategy {
|
|
13
|
+
static async get(id, workforce, client = client_js_1.Client.default()) {
|
|
14
|
+
const subject = new this(id, workforce, client);
|
|
15
|
+
const metadata = await subject.getMetadata();
|
|
16
|
+
return new task_js_1.Task(metadata, subject);
|
|
17
|
+
}
|
|
18
|
+
static convertStatus() {
|
|
19
|
+
return "not-implemented";
|
|
20
|
+
}
|
|
21
|
+
id;
|
|
22
|
+
workforce;
|
|
23
|
+
client;
|
|
24
|
+
constructor(id, workforce, client) {
|
|
25
|
+
this.id = id;
|
|
26
|
+
this.workforce = workforce;
|
|
27
|
+
this.client = client;
|
|
28
|
+
}
|
|
29
|
+
get subject() {
|
|
30
|
+
return this.workforce;
|
|
31
|
+
}
|
|
32
|
+
async getMessages({ after: _after }) {
|
|
33
|
+
const { results } = await this.client.fetch(`/workforce/items/${this.workforce.id}/tasks/${this.id}/messages`, {
|
|
34
|
+
method: "POST",
|
|
35
|
+
body: JSON.stringify({
|
|
36
|
+
page_size: 1_000, // @todo: pagination
|
|
37
|
+
// @todo: cursor "before" :S
|
|
38
|
+
}),
|
|
39
|
+
});
|
|
40
|
+
return results.map((data) => {
|
|
41
|
+
switch (data.content.type) {
|
|
42
|
+
case "agent-error":
|
|
43
|
+
return new agent_error_js_1.AgentErrorMessage(data);
|
|
44
|
+
case "agent-message":
|
|
45
|
+
return new agent_js_1.AgentMessage(data);
|
|
46
|
+
case "tool-run":
|
|
47
|
+
return new tool_js_1.ToolMessage(data);
|
|
48
|
+
case "user-message":
|
|
49
|
+
return new user_js_1.UserMessage(data);
|
|
50
|
+
case "workforce-agent-run":
|
|
51
|
+
return new workforce_agent_js_1.WorkforceAgentMessage(data);
|
|
52
|
+
case "workforce-agent-handover":
|
|
53
|
+
return new workforce_agent_handover_js_1.WorkforceAgentHandoverMessage(data);
|
|
54
|
+
default:
|
|
55
|
+
throw new Error("unknown message response");
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
async getMetadata() {
|
|
60
|
+
const { metadata } = await this.client.fetch(`/workforce/tasks/${this.id}/metadata`);
|
|
61
|
+
return {
|
|
62
|
+
id: this.id,
|
|
63
|
+
region: this.client.region,
|
|
64
|
+
project: this.client.project,
|
|
65
|
+
name: metadata.title,
|
|
66
|
+
status: WorkforceStrategy.convertStatus(), // @todo: not implemented
|
|
67
|
+
createdAt: new Date(metadata.insert_date),
|
|
68
|
+
updatedAt: new Date(metadata.update_date),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.WorkforceStrategy = WorkforceStrategy;
|
package/script/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/script/utils.js
CHANGED
|
@@ -37,6 +37,7 @@ exports.abortPromise = abortPromise;
|
|
|
37
37
|
exports.delay = delay;
|
|
38
38
|
exports.cleanPath = cleanPath;
|
|
39
39
|
exports.randomUUID = randomUUID;
|
|
40
|
+
exports.getFileExtension = getFileExtension;
|
|
40
41
|
function abortPromise(signal, reject) {
|
|
41
42
|
return new Promise((res, rej) => signal.addEventListener("abort", () => reject ? rej() : res()));
|
|
42
43
|
}
|
|
@@ -54,3 +55,7 @@ async function randomUUID() {
|
|
|
54
55
|
const cryptoModule = await Promise.resolve().then(() => __importStar(require("node:crypto")));
|
|
55
56
|
return cryptoModule.randomUUID();
|
|
56
57
|
}
|
|
58
|
+
function getFileExtension(filename) {
|
|
59
|
+
const dot = filename.lastIndexOf(".");
|
|
60
|
+
return dot >= 0 ? filename.slice(dot + 1) : "";
|
|
61
|
+
}
|
|
@@ -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 {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Workforce = void 0;
|
|
4
|
+
const client_js_1 = require("./client.js");
|
|
5
|
+
const task_js_1 = require("./task/task.js");
|
|
6
|
+
const workforce_strategy_js_1 = require("./task/workforce-strategy.js");
|
|
7
|
+
class Workforce {
|
|
8
|
+
static async get(id, client = client_js_1.Client.default()) {
|
|
9
|
+
const config = await client.fetch(`/workforce/items/${id}`);
|
|
10
|
+
return new Workforce(config, client);
|
|
11
|
+
}
|
|
12
|
+
client;
|
|
13
|
+
#config;
|
|
14
|
+
constructor(config, client) {
|
|
15
|
+
this.#config = config;
|
|
16
|
+
this.client = client;
|
|
17
|
+
}
|
|
18
|
+
get id() {
|
|
19
|
+
return this.#config.id;
|
|
20
|
+
}
|
|
21
|
+
get region() {
|
|
22
|
+
return this.client.region;
|
|
23
|
+
}
|
|
24
|
+
get project() {
|
|
25
|
+
return this.client.project;
|
|
26
|
+
}
|
|
27
|
+
get name() {
|
|
28
|
+
return this.#config.workforce_metadata.name;
|
|
29
|
+
}
|
|
30
|
+
async getTask(id) {
|
|
31
|
+
return await workforce_strategy_js_1.WorkforceStrategy.get(id, this, this.client);
|
|
32
|
+
}
|
|
33
|
+
async sendMessage(message, task) {
|
|
34
|
+
const { workforce_task_id: taskId } = await this.client.fetch("/workforce/trigger", {
|
|
35
|
+
method: "POST",
|
|
36
|
+
body: JSON.stringify({
|
|
37
|
+
workforce_id: this.id,
|
|
38
|
+
workforce_task_id: task?.id,
|
|
39
|
+
trigger: {
|
|
40
|
+
message: {
|
|
41
|
+
role: "user",
|
|
42
|
+
content: message,
|
|
43
|
+
attachments: [],
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
}),
|
|
47
|
+
});
|
|
48
|
+
if (task) {
|
|
49
|
+
task[task_js_1.resetBackoffDuration]();
|
|
50
|
+
}
|
|
51
|
+
return task ?? this.getTask(taskId);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.Workforce = Workforce;
|