@relevanceai/sdk 3.0.0-alpha.4 → 3.0.0-alpha.6
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 +240 -98
- package/esm/agent.d.ts +47 -8
- package/esm/agent.js +161 -14
- 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 +30 -12
- package/esm/message/tool.js +46 -10
- package/esm/message/user.d.ts +19 -2
- package/esm/message/user.js +21 -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 +3 -7
- 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/tool.d.ts +22 -0
- package/esm/tool.js +27 -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 +7 -1
- package/script/agent.d.ts +47 -8
- package/script/agent.js +162 -14
- 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 +30 -12
- package/script/message/tool.js +45 -9
- package/script/message/user.d.ts +19 -2
- package/script/message/user.js +20 -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 +3 -7
- 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/tool.d.ts +22 -0
- package/script/tool.js +31 -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/esm/tool.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export class Tool {
|
|
2
|
+
#config;
|
|
3
|
+
constructor(config) {
|
|
4
|
+
this.#config = config;
|
|
5
|
+
}
|
|
6
|
+
get id() {
|
|
7
|
+
return this.#config.studio_id;
|
|
8
|
+
}
|
|
9
|
+
get region() {
|
|
10
|
+
return this.#config.region;
|
|
11
|
+
}
|
|
12
|
+
get project() {
|
|
13
|
+
return this.#config.project;
|
|
14
|
+
}
|
|
15
|
+
get name() {
|
|
16
|
+
return this.#config.title;
|
|
17
|
+
}
|
|
18
|
+
get avatar() {
|
|
19
|
+
return this.#config.emoji;
|
|
20
|
+
}
|
|
21
|
+
get description() {
|
|
22
|
+
return this.#config.description;
|
|
23
|
+
}
|
|
24
|
+
get parametersSchema() {
|
|
25
|
+
return this.#config.params_schema;
|
|
26
|
+
}
|
|
27
|
+
}
|
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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@relevanceai/sdk",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.6",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "./script/mod.js",
|
|
6
6
|
"module": "./esm/mod.js",
|
|
@@ -11,5 +11,11 @@
|
|
|
11
11
|
}
|
|
12
12
|
},
|
|
13
13
|
"scripts": {},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"json-schema": "^0.4.0"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/json-schema": "^7.0.15"
|
|
19
|
+
},
|
|
14
20
|
"_generatedBy": "dnt@dev"
|
|
15
21
|
}
|
package/script/agent.d.ts
CHANGED
|
@@ -1,29 +1,68 @@
|
|
|
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
|
+
project: string;
|
|
7
8
|
name?: string;
|
|
8
9
|
description?: string;
|
|
9
10
|
emoji?: string;
|
|
10
11
|
insert_date_: string;
|
|
11
12
|
update_date_: string;
|
|
13
|
+
model: string;
|
|
12
14
|
}
|
|
15
|
+
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";
|
|
16
|
+
/**
|
|
17
|
+
* Converts an AgentTaskState to a simplified TaskStatus.
|
|
18
|
+
*
|
|
19
|
+
* @dev
|
|
20
|
+
* We want to simplify because our states are simplified in the UI and also
|
|
21
|
+
* some states have combined reasoning that the consumer does not need to care
|
|
22
|
+
* about. i.e. "queued-for-approval" "queued-for-rerun" should just be "queued".
|
|
23
|
+
*
|
|
24
|
+
* @param {AgentTaskState} state The agent task state to convert.
|
|
25
|
+
* @returns {TaskStatus} The simplified task status.
|
|
26
|
+
*/
|
|
27
|
+
export declare function stateToStatus(state: AgentTaskState): TaskStatus;
|
|
28
|
+
type SortDirection = "asc" | "desc";
|
|
29
|
+
type GetTaskOptionSort = {
|
|
30
|
+
createdAt: SortDirection;
|
|
31
|
+
} | {
|
|
32
|
+
updatedAt: SortDirection;
|
|
33
|
+
};
|
|
34
|
+
type GetTaskOptions = {
|
|
35
|
+
pageSize?: number;
|
|
36
|
+
page?: number;
|
|
37
|
+
sort?: GetTaskOptionSort;
|
|
38
|
+
search?: string;
|
|
39
|
+
filter?: {
|
|
40
|
+
status?: TaskStatus[];
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
type GetAllOptions = {
|
|
44
|
+
pageSize?: number;
|
|
45
|
+
page?: number;
|
|
46
|
+
};
|
|
13
47
|
export declare class Agent {
|
|
14
48
|
#private;
|
|
15
|
-
private readonly client;
|
|
16
49
|
static get(id: string, client?: Client): Promise<Agent>;
|
|
50
|
+
static getAll({ page, pageSize, }?: GetAllOptions, client?: Client): Promise<Agent[]>;
|
|
51
|
+
private readonly client;
|
|
17
52
|
constructor(config: AgentConfig, client: Client);
|
|
18
53
|
get id(): string;
|
|
54
|
+
get region(): Region;
|
|
55
|
+
get project(): string;
|
|
19
56
|
get name(): string | undefined;
|
|
20
57
|
get description(): string | undefined;
|
|
21
58
|
get avatar(): string | undefined;
|
|
22
59
|
get createdAt(): Date;
|
|
23
60
|
get updatedAt(): Date;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
sendMessage(message: string, task
|
|
61
|
+
getTask(taskId: string): Promise<Task<Agent>>;
|
|
62
|
+
getTasks({ sort, pageSize, page, search, filter, }?: GetTaskOptions): Promise<Task<Agent>[]>;
|
|
63
|
+
sendMessage(message: string): Promise<Task<Agent>>;
|
|
64
|
+
sendMessage(message: string, task: Task<Agent>): Promise<Task<Agent>>;
|
|
65
|
+
sendMessage(message: string, attachments: (Attachment | File)[]): Promise<Task<Agent>>;
|
|
66
|
+
sendMessage(message: string, attachments: (Attachment | File)[], task: Task<Agent>): Promise<Task<Agent>>;
|
|
28
67
|
}
|
|
29
68
|
export {};
|
package/script/agent.js
CHANGED
|
@@ -1,20 +1,117 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Agent = void 0;
|
|
4
|
+
exports.stateToStatus = stateToStatus;
|
|
4
5
|
const client_js_1 = require("./client.js");
|
|
5
|
-
const task_js_1 = require("./task.js");
|
|
6
6
|
const utils_js_1 = require("./utils.js");
|
|
7
|
+
const agent_strategy_js_1 = require("./task/agent-strategy.js");
|
|
8
|
+
const task_js_1 = require("./task/task.js");
|
|
9
|
+
/**
|
|
10
|
+
* Converts an AgentTaskState to a simplified TaskStatus.
|
|
11
|
+
*
|
|
12
|
+
* @dev
|
|
13
|
+
* We want to simplify because our states are simplified in the UI and also
|
|
14
|
+
* some states have combined reasoning that the consumer does not need to care
|
|
15
|
+
* about. i.e. "queued-for-approval" "queued-for-rerun" should just be "queued".
|
|
16
|
+
*
|
|
17
|
+
* @param {AgentTaskState} state The agent task state to convert.
|
|
18
|
+
* @returns {TaskStatus} The simplified task status.
|
|
19
|
+
*/
|
|
20
|
+
function stateToStatus(state) {
|
|
21
|
+
switch (state) {
|
|
22
|
+
case "paused":
|
|
23
|
+
return "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
|
+
return "cancelled";
|
|
40
|
+
case "completed":
|
|
41
|
+
return "completed";
|
|
42
|
+
case "unrecoverable":
|
|
43
|
+
case "errored-pending-approval":
|
|
44
|
+
return "error";
|
|
45
|
+
default:
|
|
46
|
+
throw new Error(`unhandled task state: ${state}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Reverses {@link stateToStatus} returning the group of task states a _simplified_
|
|
51
|
+
* status may relate to.
|
|
52
|
+
*
|
|
53
|
+
* @see {stateToStatus}
|
|
54
|
+
* @param {TaskStatus} status
|
|
55
|
+
* @returns {AgentTaskState[]}
|
|
56
|
+
*/
|
|
57
|
+
function statusToStates(status) {
|
|
58
|
+
switch (status) {
|
|
59
|
+
case "not-started":
|
|
60
|
+
// a special sdk-only status
|
|
61
|
+
return [];
|
|
62
|
+
case "idle":
|
|
63
|
+
return ["idle"];
|
|
64
|
+
case "paused":
|
|
65
|
+
return ["paused"];
|
|
66
|
+
case "queued":
|
|
67
|
+
return [
|
|
68
|
+
"starting-up",
|
|
69
|
+
"waiting-for-capacity",
|
|
70
|
+
"queued-for-approval",
|
|
71
|
+
"queued-for-rerun",
|
|
72
|
+
];
|
|
73
|
+
case "running":
|
|
74
|
+
return ["running"];
|
|
75
|
+
case "action":
|
|
76
|
+
return ["pending-approval", "escalated"];
|
|
77
|
+
case "completed":
|
|
78
|
+
return ["completed"];
|
|
79
|
+
case "cancelled":
|
|
80
|
+
return ["cancelled"];
|
|
81
|
+
case "error":
|
|
82
|
+
return ["errored-pending-approval", "timed-out", "unrecoverable"];
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function taskSortOptionsToParam(sort) {
|
|
86
|
+
if ("createdAt" in sort) {
|
|
87
|
+
return { insert_date: sort.createdAt };
|
|
88
|
+
}
|
|
89
|
+
else if ("updatedAt" in sort) {
|
|
90
|
+
return { update_date: sort.updatedAt };
|
|
91
|
+
}
|
|
92
|
+
throw new Error("invalid sort option");
|
|
93
|
+
}
|
|
7
94
|
const taskPrefixDelimiter = "_-_";
|
|
8
95
|
class Agent {
|
|
9
|
-
#config;
|
|
10
|
-
client;
|
|
11
96
|
static async get(id, client = client_js_1.Client.default()) {
|
|
12
97
|
const config = await Agent.#fetchConfig(id, client);
|
|
13
98
|
return new Agent(config, client);
|
|
14
99
|
}
|
|
100
|
+
static async getAll({ page = 1, pageSize = 20, } = {}, client = client_js_1.Client.default()) {
|
|
101
|
+
const { results } = await client.fetch("/agents/list", {
|
|
102
|
+
method: "POST",
|
|
103
|
+
body: JSON.stringify({
|
|
104
|
+
page_size: pageSize,
|
|
105
|
+
page,
|
|
106
|
+
}),
|
|
107
|
+
});
|
|
108
|
+
return results.map((config) => new Agent(config, client));
|
|
109
|
+
}
|
|
15
110
|
static #fetchConfig(agentId, client = client_js_1.Client.default()) {
|
|
16
111
|
return client.fetch(`/agents/${agentId}/get`).then(({ agent }) => agent);
|
|
17
112
|
}
|
|
113
|
+
#config;
|
|
114
|
+
client;
|
|
18
115
|
constructor(config, client) {
|
|
19
116
|
this.client = client;
|
|
20
117
|
this.#config = config;
|
|
@@ -22,6 +119,12 @@ class Agent {
|
|
|
22
119
|
get id() {
|
|
23
120
|
return this.#config.agent_id;
|
|
24
121
|
}
|
|
122
|
+
get region() {
|
|
123
|
+
return this.client.region;
|
|
124
|
+
}
|
|
125
|
+
get project() {
|
|
126
|
+
return this.#config.project;
|
|
127
|
+
}
|
|
25
128
|
get name() {
|
|
26
129
|
return this.#config.name;
|
|
27
130
|
}
|
|
@@ -37,16 +140,54 @@ class Agent {
|
|
|
37
140
|
get updatedAt() {
|
|
38
141
|
return new Date(this.#config.update_date_);
|
|
39
142
|
}
|
|
40
|
-
get region() {
|
|
41
|
-
return this.client.region;
|
|
42
|
-
}
|
|
43
|
-
get project() {
|
|
44
|
-
return this.client.project;
|
|
45
|
-
}
|
|
46
143
|
getTask(taskId) {
|
|
47
|
-
return
|
|
144
|
+
return agent_strategy_js_1.AgentStrategy.get(taskId, this, this.client);
|
|
48
145
|
}
|
|
49
|
-
async
|
|
146
|
+
async getTasks({ sort = { createdAt: "asc" }, pageSize = 100, page = 1, search, filter, } = {}) {
|
|
147
|
+
const filtersParam = [{
|
|
148
|
+
condition: "==",
|
|
149
|
+
condition_value: [this.id],
|
|
150
|
+
field: "conversation.agent_id",
|
|
151
|
+
filter_type: "exact_match",
|
|
152
|
+
}];
|
|
153
|
+
if (filter) {
|
|
154
|
+
for (const [field, value] of Object.entries(filter)) {
|
|
155
|
+
switch (field) {
|
|
156
|
+
case "status":
|
|
157
|
+
filtersParam.push({
|
|
158
|
+
condition: "==",
|
|
159
|
+
condition_value: value.flatMap((s) => statusToStates(s)),
|
|
160
|
+
field: "conversation.state",
|
|
161
|
+
filter_type: "exact_match",
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
const sortParam = taskSortOptionsToParam(sort);
|
|
167
|
+
const query = new URLSearchParams([
|
|
168
|
+
["filters", JSON.stringify(filtersParam)],
|
|
169
|
+
["sort", JSON.stringify([sortParam])],
|
|
170
|
+
["page_size", pageSize.toString()],
|
|
171
|
+
["page", page.toString()],
|
|
172
|
+
]);
|
|
173
|
+
if (search?.trim()) {
|
|
174
|
+
query.set("query", search.trim());
|
|
175
|
+
}
|
|
176
|
+
const { results } = await this.client.fetch(`/agents/conversations/list?${query.toString()}`);
|
|
177
|
+
return results.map(({ metadata }) => new task_js_1.Task({
|
|
178
|
+
id: metadata.knowledge_set,
|
|
179
|
+
region: this.client.region,
|
|
180
|
+
project: this.client.project,
|
|
181
|
+
name: metadata.conversation.title,
|
|
182
|
+
status: stateToStatus(metadata.conversation.state),
|
|
183
|
+
createdAt: new Date(metadata.insert_date),
|
|
184
|
+
updatedAt: new Date(metadata.update_date),
|
|
185
|
+
}, new agent_strategy_js_1.AgentStrategy(metadata.knowledge_set, this, this.client)));
|
|
186
|
+
}
|
|
187
|
+
async sendMessage(message, attachOrTask, maybeTask) {
|
|
188
|
+
const hasAttachments = Array.isArray(attachOrTask);
|
|
189
|
+
const attachFiles = hasAttachments ? attachOrTask : [];
|
|
190
|
+
const task = hasAttachments ? maybeTask : attachOrTask;
|
|
50
191
|
let taskId;
|
|
51
192
|
// embed keys require a task prefixing for new tasks
|
|
52
193
|
if (!task && this.client.isEmbedKey()) {
|
|
@@ -55,6 +196,10 @@ class Agent {
|
|
|
55
196
|
else if (task) {
|
|
56
197
|
taskId = task.id;
|
|
57
198
|
}
|
|
199
|
+
const attachments = [];
|
|
200
|
+
for (const item of attachFiles) {
|
|
201
|
+
attachments.push(item instanceof File ? await this.client.uploadTempFile(item) : item);
|
|
202
|
+
}
|
|
58
203
|
const res = await this.client.fetch("/agents/trigger", {
|
|
59
204
|
method: "POST",
|
|
60
205
|
body: JSON.stringify({
|
|
@@ -63,14 +208,17 @@ class Agent {
|
|
|
63
208
|
message: {
|
|
64
209
|
role: "user",
|
|
65
210
|
content: message,
|
|
66
|
-
attachments:
|
|
211
|
+
attachments: attachments.map(({ fileName, fileUrl }) => ({
|
|
212
|
+
file_name: fileName,
|
|
213
|
+
file_url: fileUrl,
|
|
214
|
+
})),
|
|
67
215
|
},
|
|
68
216
|
}),
|
|
69
217
|
});
|
|
70
218
|
if (task) {
|
|
71
|
-
task[task_js_1.
|
|
219
|
+
task[task_js_1.resetBackoffDuration]();
|
|
72
220
|
}
|
|
73
|
-
return task ?? this.getTask(res.conversation_id);
|
|
221
|
+
return task ?? await this.getTask(res.conversation_id);
|
|
74
222
|
}
|
|
75
223
|
}
|
|
76
224
|
exports.Agent = Agent;
|
package/script/client.d.ts
CHANGED
|
@@ -5,6 +5,10 @@ type CreateClientOptions = {
|
|
|
5
5
|
region: Region;
|
|
6
6
|
project: string;
|
|
7
7
|
};
|
|
8
|
+
export type Attachment = {
|
|
9
|
+
fileName: string;
|
|
10
|
+
fileUrl: string;
|
|
11
|
+
};
|
|
8
12
|
/**
|
|
9
13
|
* Creates and returns the _default_ client instance.
|
|
10
14
|
*
|
|
@@ -26,7 +30,8 @@ export declare class Client {
|
|
|
26
30
|
get region(): Region;
|
|
27
31
|
get project(): string;
|
|
28
32
|
isEmbedKey(): boolean;
|
|
29
|
-
fetch<T>(input: "/agents/trigger" | `/agents/${string}/get` | `/agents/${string}/tasks/${string}/metadata` | `/agents/${string}/tasks/${string}/view` | "/agents/conversations/list", init?: RequestInit): Promise<T>;
|
|
33
|
+
fetch<T>(input: "/agents/trigger" | `/agents/${string}/get` | `/agents/${string}/tasks/${string}/metadata` | `/agents/${string}/tasks/${string}/view` | "/agents/conversations/list" | "/services/get_temporary_file_upload_url" | `/workforce/items/${string}` | `/workforce/tasks/${string}/metadata` | `/workforce/items/${string}/tasks/${string}/messages` | "/workforce/trigger" | "/agents/list", init?: RequestInit): Promise<T>;
|
|
30
34
|
url(path: string): URL;
|
|
35
|
+
uploadTempFile(file: File): Promise<Attachment>;
|
|
31
36
|
}
|
|
32
37
|
export {};
|
package/script/client.js
CHANGED
|
@@ -54,11 +54,16 @@ class Client {
|
|
|
54
54
|
}
|
|
55
55
|
async fetch(input, init) {
|
|
56
56
|
const url = this.url(input);
|
|
57
|
-
const
|
|
58
|
-
|
|
57
|
+
const reqInit = Object.assign({}, init, {
|
|
58
|
+
headers: {
|
|
59
|
+
...this.key.fetchHeaders(),
|
|
60
|
+
...init?.headers,
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
const response = await fetch(url, reqInit);
|
|
59
64
|
if (!response.ok) {
|
|
60
65
|
const body = await response.text();
|
|
61
|
-
console.error(
|
|
66
|
+
console.error("client#fetch(): request failed.", url, body, reqInit);
|
|
62
67
|
throw new Error(response.statusText);
|
|
63
68
|
}
|
|
64
69
|
return response.json();
|
|
@@ -69,5 +74,34 @@ class Client {
|
|
|
69
74
|
}
|
|
70
75
|
return new URL((0, utils_js_1.cleanPath)(path), this.baseURL);
|
|
71
76
|
}
|
|
77
|
+
async uploadTempFile(file) {
|
|
78
|
+
const { upload_url: uploadUrl, download_url: downloadUrl } = await this
|
|
79
|
+
.fetch("/services/get_temporary_file_upload_url", {
|
|
80
|
+
method: "POST",
|
|
81
|
+
body: JSON.stringify({
|
|
82
|
+
filename: file.name,
|
|
83
|
+
extension: (0, utils_js_1.getFileExtension)(file.name),
|
|
84
|
+
}),
|
|
85
|
+
});
|
|
86
|
+
let content = file.type;
|
|
87
|
+
if (file.type.startsWith("text/")) {
|
|
88
|
+
content += "; charset=utf-8"; // force utf-8 for text files
|
|
89
|
+
}
|
|
90
|
+
const response = await fetch(uploadUrl, {
|
|
91
|
+
method: "PUT",
|
|
92
|
+
body: file,
|
|
93
|
+
headers: {
|
|
94
|
+
"content-type": content,
|
|
95
|
+
"x-amz-tagging": "Expire=true",
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
if (!response.ok) {
|
|
99
|
+
throw new Error(response.statusText);
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
fileName: file.name,
|
|
103
|
+
fileUrl: downloadUrl,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
72
106
|
}
|
|
73
107
|
exports.Client = Client;
|
package/script/event.d.ts
CHANGED
|
@@ -1,33 +1,14 @@
|
|
|
1
1
|
import type { AgentErrorMessage } from "./message/agent-error.js";
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export declare class TaskStartEvent extends CustomEvent<{
|
|
7
|
-
id: string;
|
|
8
|
-
status: TaskStatus;
|
|
9
|
-
}> {
|
|
10
|
-
readonly type = "start";
|
|
11
|
-
constructor(id: string, status: TaskStatus);
|
|
12
|
-
}
|
|
13
|
-
export declare class TaskStatusEvent extends CustomEvent<{
|
|
14
|
-
status: TaskStatus;
|
|
15
|
-
}> {
|
|
16
|
-
readonly type = "status";
|
|
17
|
-
constructor(status: TaskStatus);
|
|
2
|
+
import type { AnyTaskMessage } from "./message/task.js";
|
|
3
|
+
export declare class TaskUpdateEvent extends CustomEvent<void> {
|
|
4
|
+
readonly type = "update";
|
|
5
|
+
constructor();
|
|
18
6
|
}
|
|
19
7
|
export declare class TaskMessageEvent extends CustomEvent<{
|
|
20
|
-
message:
|
|
8
|
+
message: AnyTaskMessage;
|
|
21
9
|
}> {
|
|
22
10
|
readonly type = "message";
|
|
23
|
-
constructor(message:
|
|
24
|
-
isUserMessage(): boolean;
|
|
25
|
-
}
|
|
26
|
-
export declare class TaskUpdateEvent extends CustomEvent<{
|
|
27
|
-
message: ToolMessage;
|
|
28
|
-
}> {
|
|
29
|
-
readonly type = "update";
|
|
30
|
-
constructor(message: ToolMessage);
|
|
11
|
+
constructor(message: AnyTaskMessage);
|
|
31
12
|
}
|
|
32
13
|
export declare class TaskErrorEvent extends CustomEvent<{
|
|
33
14
|
message: AgentErrorMessage;
|
package/script/event.js
CHANGED
|
@@ -1,37 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TaskErrorEvent = exports.
|
|
4
|
-
class
|
|
5
|
-
type = "
|
|
6
|
-
constructor(
|
|
7
|
-
super("
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
exports.TaskStartEvent = TaskStartEvent;
|
|
11
|
-
class TaskStatusEvent extends CustomEvent {
|
|
12
|
-
type = "status";
|
|
13
|
-
constructor(status) {
|
|
14
|
-
super("status", { detail: { status } });
|
|
3
|
+
exports.TaskErrorEvent = exports.TaskMessageEvent = exports.TaskUpdateEvent = void 0;
|
|
4
|
+
class TaskUpdateEvent extends CustomEvent {
|
|
5
|
+
type = "update";
|
|
6
|
+
constructor() {
|
|
7
|
+
super("update");
|
|
15
8
|
}
|
|
16
9
|
}
|
|
17
|
-
exports.
|
|
10
|
+
exports.TaskUpdateEvent = TaskUpdateEvent;
|
|
18
11
|
class TaskMessageEvent extends CustomEvent {
|
|
19
12
|
type = "message";
|
|
20
13
|
constructor(message) {
|
|
21
14
|
super("message", { detail: { message } });
|
|
22
15
|
}
|
|
23
|
-
isUserMessage() {
|
|
24
|
-
return this.detail.message.type === "user-message";
|
|
25
|
-
}
|
|
26
16
|
}
|
|
27
17
|
exports.TaskMessageEvent = TaskMessageEvent;
|
|
28
|
-
class TaskUpdateEvent extends CustomEvent {
|
|
29
|
-
type = "update";
|
|
30
|
-
constructor(message) {
|
|
31
|
-
super("update", { detail: { message } });
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
exports.TaskUpdateEvent = TaskUpdateEvent;
|
|
35
18
|
class TaskErrorEvent extends CustomEvent {
|
|
36
19
|
type = "error";
|
|
37
20
|
constructor(message) {
|
package/script/key.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { GenericMessage } from "./task.js";
|
|
2
2
|
export interface AgentErrorMessageContent {
|
|
3
3
|
type: "agent-error";
|
|
4
|
+
errors: {
|
|
5
|
+
body: string;
|
|
6
|
+
}[];
|
|
4
7
|
}
|
|
5
|
-
export declare class AgentErrorMessage extends
|
|
8
|
+
export declare class AgentErrorMessage extends GenericMessage<AgentErrorMessageContent> {
|
|
9
|
+
get errors(): string[];
|
|
10
|
+
get lastError(): string;
|
|
6
11
|
}
|
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AgentErrorMessage = void 0;
|
|
4
4
|
const task_js_1 = require("./task.js");
|
|
5
|
-
class AgentErrorMessage extends task_js_1.
|
|
5
|
+
class AgentErrorMessage extends task_js_1.GenericMessage {
|
|
6
|
+
get errors() {
|
|
7
|
+
return this.message.content.errors.map((e) => e.body);
|
|
8
|
+
}
|
|
9
|
+
get lastError() {
|
|
10
|
+
return this.errors.at(-1);
|
|
11
|
+
}
|
|
6
12
|
}
|
|
7
13
|
exports.AgentErrorMessage = AgentErrorMessage;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { GenericMessage } from "./task.js";
|
|
2
2
|
export interface AgentMessageContent {
|
|
3
3
|
type: "agent-message";
|
|
4
4
|
text: string;
|
|
5
5
|
}
|
|
6
|
-
export declare class AgentMessage extends
|
|
6
|
+
export declare class AgentMessage extends GenericMessage<AgentMessageContent> {
|
|
7
7
|
get text(): string;
|
|
8
8
|
get agentId(): string;
|
|
9
9
|
}
|
package/script/message/agent.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AgentMessage = void 0;
|
|
4
4
|
const task_js_1 = require("./task.js");
|
|
5
|
-
class AgentMessage extends task_js_1.
|
|
5
|
+
class AgentMessage extends task_js_1.GenericMessage {
|
|
6
6
|
get text() {
|
|
7
7
|
return this.message.content.text;
|
|
8
8
|
}
|