@relevanceai/sdk 2.0.2 → 3.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +304 -105
- package/esm/agent-task.d.ts +61 -0
- package/esm/agent-task.js +112 -0
- package/esm/agent.d.ts +17 -0
- package/esm/agent.js +52 -0
- package/esm/client.d.ts +36 -0
- package/esm/client.js +69 -0
- package/esm/events.d.ts +40 -0
- package/esm/events.js +39 -0
- package/esm/key.d.ts +86 -0
- package/esm/key.js +121 -0
- package/esm/message.d.ts +18 -0
- package/esm/message.js +18 -0
- package/esm/mod.d.ts +7 -0
- package/esm/mod.js +4 -0
- package/esm/package.json +3 -0
- package/esm/region.d.ts +5 -0
- package/esm/region.js +6 -0
- package/esm/task.d.ts +25 -0
- package/esm/task.js +96 -0
- package/esm/utils.d.ts +8 -0
- package/esm/utils.js +29 -0
- package/package.json +11 -36
- package/dist-cjs/generated/VecDBApi.js +0 -1682
- package/dist-cjs/generated/_VecDBApiSchemaTypes.js +0 -3
- package/dist-cjs/generated/index.js +0 -17
- package/dist-cjs/index.js +0 -18
- package/dist-cjs/services/discovery/Dataset.js +0 -126
- package/dist-cjs/services/discovery/index.js +0 -159
- package/dist-cjs/services/index.js +0 -18
- package/dist-cjs/services/vecdb/Dataset.js +0 -137
- package/dist-cjs/services/vecdb/index.js +0 -137
- package/dist-cjs/shared/BaseClient.js +0 -35
- package/dist-cjs/shared/generate.js +0 -90
- package/dist-cjs/shared/serviceConfigs.js +0 -10
- package/dist-es/generated/VecDBApi.js +0 -2579
- package/dist-es/generated/_VecDBApiSchemaTypes.js +0 -2
- package/dist-es/generated/index.js +0 -1
- package/dist-es/index.js +0 -2
- package/dist-es/services/discovery/Dataset.js +0 -126
- package/dist-es/services/discovery/index.js +0 -159
- package/dist-es/services/index.js +0 -2
- package/dist-es/services/vecdb/Dataset.js +0 -356
- package/dist-es/services/vecdb/index.js +0 -184
- package/dist-es/shared/BaseClient.js +0 -82
- package/dist-es/shared/generate.js +0 -224
- package/dist-es/shared/serviceConfigs.js +0 -7
- package/dist-types/generated/VecDBApi.d.ts +0 -632
- package/dist-types/generated/_VecDBApiSchemaTypes.d.ts +0 -22299
- package/dist-types/generated/index.d.ts +0 -1
- package/dist-types/index.d.ts +0 -2
- package/dist-types/services/discovery/Dataset.d.ts +0 -0
- package/dist-types/services/discovery/index.d.ts +0 -0
- package/dist-types/services/index.d.ts +0 -1
- package/dist-types/services/vecdb/Dataset.d.ts +0 -52
- package/dist-types/services/vecdb/index.d.ts +0 -44
- package/dist-types/shared/BaseClient.d.ts +0 -28
- package/dist-types/shared/generate.d.ts +0 -1
- package/dist-types/shared/serviceConfigs.d.ts +0 -8
package/esm/client.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Agent } from "./agent.js";
|
|
2
|
+
import { type Region } from "./region.js";
|
|
3
|
+
import { AgentTask } from "./agent-task.js";
|
|
4
|
+
import { Key } from "./key.js";
|
|
5
|
+
type CreateClientOptions = {
|
|
6
|
+
apiKey: string;
|
|
7
|
+
region: Region;
|
|
8
|
+
project: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Creates and returns the _default_ client instance.
|
|
12
|
+
*
|
|
13
|
+
* @throws {Error} if a default client already exists.
|
|
14
|
+
* @see {Client.default}
|
|
15
|
+
*/
|
|
16
|
+
export declare function createClient(keyOrOptions: Key | CreateClientOptions): Client;
|
|
17
|
+
export declare class Client {
|
|
18
|
+
/**
|
|
19
|
+
* Returns the _default_ client instance.
|
|
20
|
+
*
|
|
21
|
+
* @throws {Error} if there is no default client.
|
|
22
|
+
* @see {createClient}
|
|
23
|
+
*/
|
|
24
|
+
static default(): Client;
|
|
25
|
+
readonly key: Key;
|
|
26
|
+
private readonly baseURL;
|
|
27
|
+
constructor(key: Key);
|
|
28
|
+
get region(): Region;
|
|
29
|
+
get project(): string;
|
|
30
|
+
isEmbedKey(): boolean;
|
|
31
|
+
createTask({ agent }: {
|
|
32
|
+
agent: string | Agent;
|
|
33
|
+
}): Promise<AgentTask>;
|
|
34
|
+
fetch<T>(input: `/agents/trigger` | `/agents/${string}/get` | `/agents/${string}/tasks/${string}/metadata` | `/agents/${string}/tasks/${string}/view`, init?: RequestInit): Promise<T>;
|
|
35
|
+
}
|
|
36
|
+
export {};
|
package/esm/client.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Agent } from "./agent.js";
|
|
2
|
+
import { regionBaseURL } from "./region.js";
|
|
3
|
+
import { AgentTask } from "./agent-task.js";
|
|
4
|
+
import { cleanPath } from "./utils.js";
|
|
5
|
+
import { Key } from "./key.js";
|
|
6
|
+
let defaultClient;
|
|
7
|
+
/**
|
|
8
|
+
* Creates and returns the _default_ client instance.
|
|
9
|
+
*
|
|
10
|
+
* @throws {Error} if a default client already exists.
|
|
11
|
+
* @see {Client.default}
|
|
12
|
+
*/
|
|
13
|
+
export function createClient(keyOrOptions) {
|
|
14
|
+
if (defaultClient) {
|
|
15
|
+
throw new Error("default client already exists");
|
|
16
|
+
}
|
|
17
|
+
const key = keyOrOptions instanceof Key ? keyOrOptions : new Key({
|
|
18
|
+
key: keyOrOptions.apiKey,
|
|
19
|
+
region: keyOrOptions.region,
|
|
20
|
+
project: keyOrOptions.project,
|
|
21
|
+
});
|
|
22
|
+
defaultClient = new Client(key);
|
|
23
|
+
return defaultClient;
|
|
24
|
+
}
|
|
25
|
+
export class Client {
|
|
26
|
+
/**
|
|
27
|
+
* Returns the _default_ client instance.
|
|
28
|
+
*
|
|
29
|
+
* @throws {Error} if there is no default client.
|
|
30
|
+
* @see {createClient}
|
|
31
|
+
*/
|
|
32
|
+
static default() {
|
|
33
|
+
if (!defaultClient) {
|
|
34
|
+
throw new Error("no default client");
|
|
35
|
+
}
|
|
36
|
+
return defaultClient;
|
|
37
|
+
}
|
|
38
|
+
key;
|
|
39
|
+
baseURL;
|
|
40
|
+
constructor(key) {
|
|
41
|
+
this.key = key;
|
|
42
|
+
this.baseURL = regionBaseURL(this.key.region);
|
|
43
|
+
}
|
|
44
|
+
get region() {
|
|
45
|
+
return this.key.region;
|
|
46
|
+
}
|
|
47
|
+
get project() {
|
|
48
|
+
return this.key.project;
|
|
49
|
+
}
|
|
50
|
+
isEmbedKey() {
|
|
51
|
+
return this.key.isEmbed();
|
|
52
|
+
}
|
|
53
|
+
async createTask({ agent }) {
|
|
54
|
+
if (agent) {
|
|
55
|
+
return new AgentTask(typeof agent === "string" ? await Agent.fetch(agent) : agent, undefined, this);
|
|
56
|
+
}
|
|
57
|
+
throw new Error("task not implemented");
|
|
58
|
+
}
|
|
59
|
+
async fetch(input, init) {
|
|
60
|
+
const url = new URL(cleanPath(input), this.baseURL);
|
|
61
|
+
const headers = new Headers(this.key.fetchHeaders());
|
|
62
|
+
const response = await fetch(url, Object.assign({ headers }, init));
|
|
63
|
+
if (!response.ok) {
|
|
64
|
+
console.error(url, init, headers);
|
|
65
|
+
throw new Error(response.statusText);
|
|
66
|
+
}
|
|
67
|
+
return response.json();
|
|
68
|
+
}
|
|
69
|
+
}
|
package/esm/events.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { TaskMessage } from "./message.js";
|
|
2
|
+
import type { TaskStatus } from "./task.js";
|
|
3
|
+
export declare class TaskStartEvent extends CustomEvent<{
|
|
4
|
+
id: string;
|
|
5
|
+
status: TaskStatus;
|
|
6
|
+
}> {
|
|
7
|
+
readonly type = "start";
|
|
8
|
+
constructor(id: string, status: TaskStatus);
|
|
9
|
+
}
|
|
10
|
+
export declare class TaskStatusEvent extends CustomEvent<{
|
|
11
|
+
status: TaskStatus;
|
|
12
|
+
}> {
|
|
13
|
+
readonly type = "status";
|
|
14
|
+
constructor(status: TaskStatus);
|
|
15
|
+
}
|
|
16
|
+
export declare class TaskMessageEvent extends CustomEvent<{
|
|
17
|
+
message: TaskMessage;
|
|
18
|
+
}> {
|
|
19
|
+
readonly type = "message";
|
|
20
|
+
constructor(message: TaskMessage);
|
|
21
|
+
isUserMessage(): boolean;
|
|
22
|
+
}
|
|
23
|
+
export declare class TaskUpdateEvent extends CustomEvent<{
|
|
24
|
+
message: TaskMessage;
|
|
25
|
+
}> {
|
|
26
|
+
readonly type = "update";
|
|
27
|
+
constructor(message: TaskMessage);
|
|
28
|
+
}
|
|
29
|
+
export declare class TaskActionEvent extends CustomEvent<{
|
|
30
|
+
message: TaskMessage;
|
|
31
|
+
}> {
|
|
32
|
+
readonly type = "action";
|
|
33
|
+
constructor(message: TaskMessage);
|
|
34
|
+
}
|
|
35
|
+
export declare class TaskErrorEvent extends CustomEvent<{
|
|
36
|
+
message: TaskMessage;
|
|
37
|
+
}> {
|
|
38
|
+
readonly type = "error";
|
|
39
|
+
constructor(message: TaskMessage);
|
|
40
|
+
}
|
package/esm/events.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export class TaskStartEvent extends CustomEvent {
|
|
2
|
+
type = "start";
|
|
3
|
+
constructor(id, status) {
|
|
4
|
+
super("start", { detail: { id, status } });
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export class TaskStatusEvent extends CustomEvent {
|
|
8
|
+
type = "status";
|
|
9
|
+
constructor(status) {
|
|
10
|
+
super("status", { detail: { status } });
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export class TaskMessageEvent extends CustomEvent {
|
|
14
|
+
type = "message";
|
|
15
|
+
constructor(message) {
|
|
16
|
+
super("message", { detail: { message } });
|
|
17
|
+
}
|
|
18
|
+
isUserMessage() {
|
|
19
|
+
return this.detail.message.type === "user-message";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export class TaskUpdateEvent extends CustomEvent {
|
|
23
|
+
type = "update";
|
|
24
|
+
constructor(message) {
|
|
25
|
+
super("update", { detail: { message } });
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export class TaskActionEvent extends CustomEvent {
|
|
29
|
+
type = "action";
|
|
30
|
+
constructor(message) {
|
|
31
|
+
super("action", { detail: { message } });
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export class TaskErrorEvent extends CustomEvent {
|
|
35
|
+
type = "error";
|
|
36
|
+
constructor(message) {
|
|
37
|
+
super("error", { detail: { message } });
|
|
38
|
+
}
|
|
39
|
+
}
|
package/esm/key.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { Region } from "./mod.js";
|
|
2
|
+
type CreateKeyOptions = {
|
|
3
|
+
key: string;
|
|
4
|
+
region: Region;
|
|
5
|
+
project: string;
|
|
6
|
+
agentId?: string;
|
|
7
|
+
taskPrefix?: string;
|
|
8
|
+
};
|
|
9
|
+
type GenerateEmbedKeyOptions = Omit<CreateKeyOptions, "key" | "taskPrefix">;
|
|
10
|
+
/**
|
|
11
|
+
* Key is used to authenticate requests for the {@link Client}. A Key can be
|
|
12
|
+
* either a _full_ key or an _embed_ key.
|
|
13
|
+
*
|
|
14
|
+
* A full key has access to SDK features. An embed key is scoped to a specific
|
|
15
|
+
* agent and is only used to create and interact with tasks for that agent.
|
|
16
|
+
*
|
|
17
|
+
* @see {@link Key.generateEmbedKey} to generate an embed key for a specific agent.
|
|
18
|
+
*
|
|
19
|
+
* @class Key
|
|
20
|
+
*/
|
|
21
|
+
export declare class Key {
|
|
22
|
+
#private;
|
|
23
|
+
/**
|
|
24
|
+
* Generates an embed key for the specified agent. The embed key can then be
|
|
25
|
+
* used to create a {@link Client} instance that can create and interact with
|
|
26
|
+
* tasks for that agent.
|
|
27
|
+
*
|
|
28
|
+
* @throws {Error} if the request to generate an embed key fails.
|
|
29
|
+
*
|
|
30
|
+
* @param {GenerateEmbedKeyOptions} options The generation options.
|
|
31
|
+
*
|
|
32
|
+
* @returns {Promise<Key>}
|
|
33
|
+
*/
|
|
34
|
+
static generateEmbedKey({ region, project, agentId, }: GenerateEmbedKeyOptions): Promise<Key>;
|
|
35
|
+
/**
|
|
36
|
+
* The region the key is scoped to.
|
|
37
|
+
*
|
|
38
|
+
* @property {string} region
|
|
39
|
+
*/
|
|
40
|
+
readonly region: Region;
|
|
41
|
+
/**
|
|
42
|
+
* The project the key is scoped to.
|
|
43
|
+
*
|
|
44
|
+
* @property {string} project
|
|
45
|
+
*/
|
|
46
|
+
readonly project: string;
|
|
47
|
+
/**
|
|
48
|
+
* The agent ID the embed key is scoped to. This is `undefined` for full
|
|
49
|
+
* keys.
|
|
50
|
+
*
|
|
51
|
+
* @property {string | undefined} agentId
|
|
52
|
+
*/
|
|
53
|
+
readonly agentId: string | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* The task prefix used to namespace tasks created with the embed key. This
|
|
56
|
+
* is `undefined` for full keys.
|
|
57
|
+
*
|
|
58
|
+
* @property {string | undefined} taskPrefix
|
|
59
|
+
*/
|
|
60
|
+
readonly taskPrefix: string | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* Creates a new {@link Key} instance with the provided options.
|
|
63
|
+
*
|
|
64
|
+
* @param {CreateKeyOptions} options
|
|
65
|
+
*/
|
|
66
|
+
constructor({ key, region, project, agentId, taskPrefix }: CreateKeyOptions);
|
|
67
|
+
/**
|
|
68
|
+
* Returns whether the key is an embed key.
|
|
69
|
+
*
|
|
70
|
+
* @returns {boolean}
|
|
71
|
+
*/
|
|
72
|
+
isEmbed(): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Returns the headers required for authenticating requests with this key.
|
|
75
|
+
*
|
|
76
|
+
* @returns {HeadersInit}
|
|
77
|
+
*/
|
|
78
|
+
fetchHeaders(): HeadersInit;
|
|
79
|
+
/**
|
|
80
|
+
* Returns a JSON representation of the key.
|
|
81
|
+
*
|
|
82
|
+
* @returns {CreateKeyOptions}
|
|
83
|
+
*/
|
|
84
|
+
toJSON(): CreateKeyOptions;
|
|
85
|
+
}
|
|
86
|
+
export {};
|
package/esm/key.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { regionBaseURL } from "./region.js";
|
|
2
|
+
import { cleanPath } from "./utils.js";
|
|
3
|
+
/**
|
|
4
|
+
* Key is used to authenticate requests for the {@link Client}. A Key can be
|
|
5
|
+
* either a _full_ key or an _embed_ key.
|
|
6
|
+
*
|
|
7
|
+
* A full key has access to SDK features. An embed key is scoped to a specific
|
|
8
|
+
* agent and is only used to create and interact with tasks for that agent.
|
|
9
|
+
*
|
|
10
|
+
* @see {@link Key.generateEmbedKey} to generate an embed key for a specific agent.
|
|
11
|
+
*
|
|
12
|
+
* @class Key
|
|
13
|
+
*/
|
|
14
|
+
export class Key {
|
|
15
|
+
/**
|
|
16
|
+
* Generates an embed key for the specified agent. The embed key can then be
|
|
17
|
+
* used to create a {@link Client} instance that can create and interact with
|
|
18
|
+
* tasks for that agent.
|
|
19
|
+
*
|
|
20
|
+
* @throws {Error} if the request to generate an embed key fails.
|
|
21
|
+
*
|
|
22
|
+
* @param {GenerateEmbedKeyOptions} options The generation options.
|
|
23
|
+
*
|
|
24
|
+
* @returns {Promise<Key>}
|
|
25
|
+
*/
|
|
26
|
+
static async generateEmbedKey({ region, project, agentId, }) {
|
|
27
|
+
const embedKeyURL = new URL(cleanPath("/agents/get_embed_key"), regionBaseURL(region));
|
|
28
|
+
const response = await fetch(embedKeyURL, {
|
|
29
|
+
method: "POST",
|
|
30
|
+
body: JSON.stringify({ agent_id: agentId, project }),
|
|
31
|
+
});
|
|
32
|
+
if (!response.ok) {
|
|
33
|
+
throw new Error("failed to fetch embed key", { cause: response });
|
|
34
|
+
}
|
|
35
|
+
const { embed_key: embedKey, conversation_prefix: taskPrefix } = await response.json();
|
|
36
|
+
return new Key({
|
|
37
|
+
key: embedKey,
|
|
38
|
+
region,
|
|
39
|
+
project,
|
|
40
|
+
agentId,
|
|
41
|
+
taskPrefix,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The region the key is scoped to.
|
|
46
|
+
*
|
|
47
|
+
* @property {string} region
|
|
48
|
+
*/
|
|
49
|
+
region;
|
|
50
|
+
/**
|
|
51
|
+
* The project the key is scoped to.
|
|
52
|
+
*
|
|
53
|
+
* @property {string} project
|
|
54
|
+
*/
|
|
55
|
+
project;
|
|
56
|
+
/**
|
|
57
|
+
* The API key used for authentication.
|
|
58
|
+
*
|
|
59
|
+
* @private
|
|
60
|
+
* @property {string} key
|
|
61
|
+
*/
|
|
62
|
+
#key;
|
|
63
|
+
/**
|
|
64
|
+
* The agent ID the embed key is scoped to. This is `undefined` for full
|
|
65
|
+
* keys.
|
|
66
|
+
*
|
|
67
|
+
* @property {string | undefined} agentId
|
|
68
|
+
*/
|
|
69
|
+
agentId;
|
|
70
|
+
/**
|
|
71
|
+
* The task prefix used to namespace tasks created with the embed key. This
|
|
72
|
+
* is `undefined` for full keys.
|
|
73
|
+
*
|
|
74
|
+
* @property {string | undefined} taskPrefix
|
|
75
|
+
*/
|
|
76
|
+
taskPrefix;
|
|
77
|
+
/**
|
|
78
|
+
* Creates a new {@link Key} instance with the provided options.
|
|
79
|
+
*
|
|
80
|
+
* @param {CreateKeyOptions} options
|
|
81
|
+
*/
|
|
82
|
+
constructor({ key, region, project, agentId, taskPrefix }) {
|
|
83
|
+
this.#key = key;
|
|
84
|
+
this.region = region;
|
|
85
|
+
this.project = project;
|
|
86
|
+
this.agentId = agentId;
|
|
87
|
+
this.taskPrefix = taskPrefix;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Returns whether the key is an embed key.
|
|
91
|
+
*
|
|
92
|
+
* @returns {boolean}
|
|
93
|
+
*/
|
|
94
|
+
isEmbed() {
|
|
95
|
+
return (this.agentId !== undefined && this.taskPrefix !== undefined);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Returns the headers required for authenticating requests with this key.
|
|
99
|
+
*
|
|
100
|
+
* @returns {HeadersInit}
|
|
101
|
+
*/
|
|
102
|
+
fetchHeaders() {
|
|
103
|
+
return {
|
|
104
|
+
Authorization: `${this.project}:${this.#key}`,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Returns a JSON representation of the key.
|
|
109
|
+
*
|
|
110
|
+
* @returns {CreateKeyOptions}
|
|
111
|
+
*/
|
|
112
|
+
toJSON() {
|
|
113
|
+
return {
|
|
114
|
+
key: this.#key,
|
|
115
|
+
region: this.region,
|
|
116
|
+
project: this.project,
|
|
117
|
+
agentId: this.agentId,
|
|
118
|
+
taskPrefix: this.taskPrefix,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
}
|
package/esm/message.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
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/esm/mod.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { Agent } from "./agent.js";
|
|
2
|
+
export { Client, createClient } from "./client.js";
|
|
3
|
+
export { Key } from "./key.js";
|
|
4
|
+
export { type Region, REGION_AU, REGION_EU, REGION_US } from "./region.js";
|
|
5
|
+
export type { AgentTask } from "./agent-task.js";
|
|
6
|
+
export type { TaskMessage } from "./message.js";
|
|
7
|
+
export type { Task, TaskStatus } from "./task.js";
|
package/esm/mod.js
ADDED
package/esm/package.json
ADDED
package/esm/region.d.ts
ADDED
package/esm/region.js
ADDED
package/esm/task.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Client } from "./client.js";
|
|
2
|
+
import type { TaskMessage } from "./message.js";
|
|
3
|
+
export type TaskStatus = "not-started" | "idle" | "queued" | "running" | "action" | "complete" | "error";
|
|
4
|
+
export declare abstract class Task<S, E extends Record<string, unknown>> extends EventTarget {
|
|
5
|
+
#private;
|
|
6
|
+
readonly subject: S;
|
|
7
|
+
protected readonly client: Client;
|
|
8
|
+
private listenController;
|
|
9
|
+
abstract fetchMessages(fetchOptions?: {
|
|
10
|
+
from?: Date;
|
|
11
|
+
}): Promise<TaskMessage[]>;
|
|
12
|
+
abstract fetchStatus(): Promise<TaskStatus>;
|
|
13
|
+
constructor(subject: S, id?: string | undefined, client?: Client);
|
|
14
|
+
get id(): string | undefined;
|
|
15
|
+
protected setId(id: string, status?: TaskStatus): void;
|
|
16
|
+
listen(): void;
|
|
17
|
+
isListening(): boolean;
|
|
18
|
+
stopListening(): void;
|
|
19
|
+
addEventListener<K extends keyof E>(type: Extract<K, string>, listener: ((event: CustomEvent<E[K]>) => void) | {
|
|
20
|
+
handleEvent: (event: CustomEvent<E[K]>) => void;
|
|
21
|
+
} | null, options?: boolean | AddEventListenerOptions): void;
|
|
22
|
+
removeEventListener<K extends keyof E>(type: Extract<K, string>, listener: ((event: CustomEvent<E[K]>) => void) | {
|
|
23
|
+
handleEvent: (event: CustomEvent<E[K]>) => void;
|
|
24
|
+
} | null, options?: boolean | AddEventListenerOptions): void;
|
|
25
|
+
}
|
package/esm/task.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { Client } from "./client.js";
|
|
2
|
+
import { TaskErrorEvent, TaskMessageEvent, TaskStartEvent, TaskStatusEvent, TaskUpdateEvent, } from "./events.js";
|
|
3
|
+
import { runInterval } from "./utils.js";
|
|
4
|
+
export class Task extends EventTarget {
|
|
5
|
+
subject;
|
|
6
|
+
client;
|
|
7
|
+
#id;
|
|
8
|
+
listenController;
|
|
9
|
+
constructor(subject, id = undefined, client = Client.default()) {
|
|
10
|
+
super();
|
|
11
|
+
this.subject = subject;
|
|
12
|
+
this.client = client;
|
|
13
|
+
this.#id = id;
|
|
14
|
+
}
|
|
15
|
+
get id() {
|
|
16
|
+
return this.#id;
|
|
17
|
+
}
|
|
18
|
+
setId(id, status = "not-started") {
|
|
19
|
+
if (this.#id) {
|
|
20
|
+
throw new Error("task id is already set");
|
|
21
|
+
}
|
|
22
|
+
// @ts-ignore: allow assignment to readonly in this special case
|
|
23
|
+
this.#id = id;
|
|
24
|
+
this.dispatchEvent(new TaskStartEvent(id, status));
|
|
25
|
+
}
|
|
26
|
+
listen() {
|
|
27
|
+
if (this.isListening()) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
this.listenController = new AbortController();
|
|
31
|
+
const signal = this.listenController.signal;
|
|
32
|
+
let currentStatus = null;
|
|
33
|
+
const messagesCursor = new Date(0);
|
|
34
|
+
void runInterval(async () => {
|
|
35
|
+
// no task, yet
|
|
36
|
+
if (!this.id) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const [status, messages] = await Promise.all([
|
|
40
|
+
this.fetchStatus(),
|
|
41
|
+
this.fetchMessages({
|
|
42
|
+
from: messagesCursor,
|
|
43
|
+
}),
|
|
44
|
+
]);
|
|
45
|
+
if (!this.isListening()) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (status !== currentStatus) {
|
|
49
|
+
currentStatus = status;
|
|
50
|
+
this.dispatchEvent(new TaskStatusEvent(status));
|
|
51
|
+
}
|
|
52
|
+
if (messages.length) {
|
|
53
|
+
for (const message of messages) {
|
|
54
|
+
switch (message.type) {
|
|
55
|
+
case "agent-error":
|
|
56
|
+
this.dispatchEvent(new TaskErrorEvent(message));
|
|
57
|
+
break;
|
|
58
|
+
case "tool-run":
|
|
59
|
+
this.dispatchEvent(new TaskUpdateEvent(message));
|
|
60
|
+
break;
|
|
61
|
+
case "agent-message":
|
|
62
|
+
case "user-message":
|
|
63
|
+
this.dispatchEvent(new TaskMessageEvent(message));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
messagesCursor.setTime(
|
|
67
|
+
// +1 the api treats after inclusively
|
|
68
|
+
messages.at(-1).createdAt.getTime() + 1);
|
|
69
|
+
}
|
|
70
|
+
}, 15_000, { signal });
|
|
71
|
+
}
|
|
72
|
+
isListening() {
|
|
73
|
+
return this.listenController !== undefined;
|
|
74
|
+
}
|
|
75
|
+
stopListening() {
|
|
76
|
+
this.listenController?.abort();
|
|
77
|
+
this.listenController = undefined;
|
|
78
|
+
}
|
|
79
|
+
addEventListener(type, listener, options) {
|
|
80
|
+
this.listen();
|
|
81
|
+
const signal = AbortSignal.any([
|
|
82
|
+
...(options && typeof options === "object" && options.signal
|
|
83
|
+
? [options.signal]
|
|
84
|
+
: []),
|
|
85
|
+
this.listenController.signal,
|
|
86
|
+
]);
|
|
87
|
+
const capture = typeof options === "boolean"
|
|
88
|
+
? options
|
|
89
|
+
: Boolean(options?.capture);
|
|
90
|
+
const addOptions = Object.assign({}, options, { signal, capture });
|
|
91
|
+
super.addEventListener(type, listener, addOptions);
|
|
92
|
+
}
|
|
93
|
+
removeEventListener(type, listener, options) {
|
|
94
|
+
super.removeEventListener(type, listener, options);
|
|
95
|
+
}
|
|
96
|
+
}
|
package/esm/utils.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
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>;
|
|
7
|
+
export declare function cleanPath(path: string, version?: string): string;
|
|
8
|
+
export declare function randomUUID(): Promise<any>;
|
package/esm/utils.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export function abortPromise(signal, reject) {
|
|
2
|
+
return new Promise((res, rej) => signal.addEventListener("abort", () => reject ? rej() : res()));
|
|
3
|
+
}
|
|
4
|
+
export function delay(timeout) {
|
|
5
|
+
return new Promise((done) => setTimeout(done, timeout));
|
|
6
|
+
}
|
|
7
|
+
export async function runInterval(runner, interval, { signal, } = {}) {
|
|
8
|
+
while (true) {
|
|
9
|
+
if (signal?.aborted) {
|
|
10
|
+
break;
|
|
11
|
+
}
|
|
12
|
+
await runner();
|
|
13
|
+
await Promise.race([
|
|
14
|
+
delay(interval),
|
|
15
|
+
signal ? abortPromise(signal) : new Promise(() => { }),
|
|
16
|
+
]);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function cleanPath(path, version = "latest") {
|
|
20
|
+
return `/${version}/${path.trim().replace(/^\/+/, "")}`;
|
|
21
|
+
}
|
|
22
|
+
export async function randomUUID() {
|
|
23
|
+
if (typeof crypto !== "undefined") {
|
|
24
|
+
return crypto.randomUUID();
|
|
25
|
+
}
|
|
26
|
+
// @ts-ignore allow this import for node builds
|
|
27
|
+
const cryptoModule = await import("node:crypto");
|
|
28
|
+
return cryptoModule.randomUUID();
|
|
29
|
+
}
|
package/package.json
CHANGED
|
@@ -1,38 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@relevanceai/sdk",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
"scripts": {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"test": "jest",
|
|
15
|
-
"prepare": "npm run build",
|
|
16
|
-
"sample": "ts-node ./test/sample.test.mjs"
|
|
17
|
-
},
|
|
18
|
-
"repository": {
|
|
19
|
-
"type": "git",
|
|
20
|
-
"url": "git+https://github.com/RelevanceAI/relevance-js-sdk.git"
|
|
21
|
-
},
|
|
22
|
-
"author": "",
|
|
23
|
-
"license": "ISC",
|
|
24
|
-
"bugs": {
|
|
25
|
-
"url": "https://github.com/RelevanceAI/relevance-js-sdk/issues"
|
|
26
|
-
},
|
|
27
|
-
"homepage": "https://github.com/RelevanceAI/relevance-js-sdk#readme",
|
|
28
|
-
"devDependencies": {
|
|
29
|
-
"@types/jest": "^27.0.2",
|
|
30
|
-
"openapi-typescript": "^4.4.0",
|
|
31
|
-
"ts-jest": "^27.0.7",
|
|
32
|
-
"ts-node": "^10.4.0",
|
|
33
|
-
"typescript": "^4.4.4"
|
|
34
|
-
},
|
|
35
|
-
"dependencies": {
|
|
36
|
-
"cross-fetch": "^3.1.4"
|
|
37
|
-
}
|
|
38
|
-
}
|
|
3
|
+
"version": "3.0.0-alpha.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"module": "./esm/mod.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./esm/mod.js"
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"scripts": {},
|
|
12
|
+
"_generatedBy": "dnt@dev"
|
|
13
|
+
}
|