@superblocksteam/sdk 1.5.0 → 1.5.2
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/dist/client.js +117 -71
- package/dist/flag.js +1 -2
- package/dist/socket/handlers.d.ts +3 -2
- package/dist/socket/handlers.js +17 -5
- package/dist/socket/index.d.ts +2 -2
- package/dist/socket/index.js +2 -2
- package/dist/socket/signing.d.ts +4 -4
- package/dist/socket/signing.js +47 -58
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +37 -7
- package/package.json +2 -2
- package/src/client.ts +117 -68
- package/src/socket/handlers.ts +27 -6
- package/src/socket/index.ts +3 -3
- package/src/socket/signing.ts +48 -57
- package/src/utils.ts +39 -3
- package/tsconfig.json +1 -1
package/src/socket/signing.ts
CHANGED
|
@@ -11,12 +11,12 @@ export async function signResource({
|
|
|
11
11
|
token,
|
|
12
12
|
branchName,
|
|
13
13
|
resource,
|
|
14
|
-
|
|
14
|
+
agentUrl,
|
|
15
15
|
}: {
|
|
16
16
|
token: string;
|
|
17
17
|
branchName: string;
|
|
18
18
|
resource: ApiResource | GenericResource;
|
|
19
|
-
|
|
19
|
+
agentUrl: string;
|
|
20
20
|
}): Promise<Signature> {
|
|
21
21
|
const requestResource: Record<string, any> = {
|
|
22
22
|
branchName: branchName ?? "main",
|
|
@@ -28,22 +28,18 @@ export async function signResource({
|
|
|
28
28
|
requestResource.literal = (resource as GenericResource).literal;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return resp.signature;
|
|
40
|
-
} catch (e) {
|
|
41
|
-
throw new Error("No agents available to sign the resource");
|
|
42
|
-
}
|
|
31
|
+
const resp = await callAgent<SignatureResponse>({
|
|
32
|
+
baseUrl: agentUrl,
|
|
33
|
+
path: "v1/signature/sign",
|
|
34
|
+
method: "post",
|
|
35
|
+
token: token,
|
|
36
|
+
data: { resource: requestResource },
|
|
37
|
+
});
|
|
38
|
+
return resp.signature;
|
|
43
39
|
}
|
|
44
40
|
|
|
45
41
|
export async function verifyResources({
|
|
46
|
-
|
|
42
|
+
agentUrl,
|
|
47
43
|
token,
|
|
48
44
|
branchName,
|
|
49
45
|
resources,
|
|
@@ -51,63 +47,58 @@ export async function verifyResources({
|
|
|
51
47
|
resources: Array<GenericResource | ApiResource>;
|
|
52
48
|
token: string;
|
|
53
49
|
branchName: string;
|
|
54
|
-
|
|
50
|
+
agentUrl: string;
|
|
55
51
|
}): Promise<void> {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if ((res as ApiResource).api) {
|
|
65
|
-
return {
|
|
66
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
67
|
-
api: getSanitizedApi((res as ApiResource).api),
|
|
68
|
-
branchName: branchName ?? "main",
|
|
69
|
-
};
|
|
70
|
-
}
|
|
52
|
+
await callAgent<{ keyId: string }>({
|
|
53
|
+
baseUrl: agentUrl,
|
|
54
|
+
path: "v1/signature/verify",
|
|
55
|
+
method: "post",
|
|
56
|
+
token: token,
|
|
57
|
+
data: {
|
|
58
|
+
resources: resources.map((res) => {
|
|
59
|
+
if ((res as ApiResource).api) {
|
|
71
60
|
return {
|
|
72
|
-
|
|
61
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
62
|
+
api: getSanitizedApi((res as ApiResource).api),
|
|
73
63
|
branchName: branchName ?? "main",
|
|
74
64
|
};
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
...res,
|
|
68
|
+
branchName: branchName ?? "main",
|
|
69
|
+
};
|
|
70
|
+
}),
|
|
71
|
+
},
|
|
72
|
+
});
|
|
81
73
|
}
|
|
82
74
|
|
|
83
|
-
async function
|
|
84
|
-
|
|
75
|
+
async function callAgent<T>({
|
|
76
|
+
baseUrl,
|
|
85
77
|
path,
|
|
86
78
|
method,
|
|
87
79
|
token,
|
|
88
80
|
data,
|
|
89
81
|
}: {
|
|
90
|
-
|
|
82
|
+
baseUrl: string;
|
|
91
83
|
path: string;
|
|
92
84
|
method: Method;
|
|
93
85
|
token: string;
|
|
94
86
|
data: any;
|
|
95
87
|
}): Promise<T> {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
88
|
+
try {
|
|
89
|
+
const url = new URL(path, baseUrl);
|
|
90
|
+
const config: AxiosRequestConfig = {
|
|
91
|
+
url: url.toString(),
|
|
92
|
+
method: method,
|
|
93
|
+
headers: {
|
|
94
|
+
Authorization: "Bearer " + token,
|
|
95
|
+
},
|
|
96
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
97
|
+
data: data,
|
|
98
|
+
};
|
|
99
|
+
const resp = await axios<T>(config);
|
|
100
|
+
return resp.data;
|
|
101
|
+
} catch (error) {
|
|
102
|
+
throw new Error(`Failed to request the agent ${baseUrl}. Error: ${error}`);
|
|
111
103
|
}
|
|
112
|
-
throw new Error("Failed to request ");
|
|
113
104
|
}
|
package/src/utils.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import axios, { AxiosRequestConfig } from "axios";
|
|
1
2
|
import {
|
|
2
3
|
Agent,
|
|
3
4
|
AgentStatus,
|
|
@@ -7,11 +8,46 @@ import {
|
|
|
7
8
|
transcribeAudioToTextTranslateToEnglishTruthyValues,
|
|
8
9
|
} from "./types";
|
|
9
10
|
|
|
10
|
-
export function
|
|
11
|
-
|
|
11
|
+
export async function getAgentUrl(
|
|
12
|
+
agents: Agent[],
|
|
13
|
+
agentType: AgentType,
|
|
14
|
+
profile?: string,
|
|
15
|
+
healthCheck = true
|
|
16
|
+
): Promise<string> {
|
|
17
|
+
let filtered = agents.filter(
|
|
12
18
|
(a) => a.type === agentType && a.status === AgentStatus.ACTIVE
|
|
13
19
|
);
|
|
14
|
-
|
|
20
|
+
if (profile) {
|
|
21
|
+
filtered = agents.filter(
|
|
22
|
+
(a) =>
|
|
23
|
+
a.tags.profile &&
|
|
24
|
+
(a.tags.profile.includes("*") || a.tags.profile.includes(profile))
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
if (healthCheck) {
|
|
28
|
+
const coroutines: Promise<string>[] = [];
|
|
29
|
+
for (const agent of filtered) {
|
|
30
|
+
const p: Promise<string> = (async () => {
|
|
31
|
+
const url = new URL("health", agent.url);
|
|
32
|
+
const config: AxiosRequestConfig = {
|
|
33
|
+
url: url.toString(),
|
|
34
|
+
method: "get",
|
|
35
|
+
headers: {},
|
|
36
|
+
};
|
|
37
|
+
await axios(config);
|
|
38
|
+
return agent.url;
|
|
39
|
+
})();
|
|
40
|
+
coroutines.push(p);
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
return await Promise.any(coroutines);
|
|
44
|
+
} catch (e) {
|
|
45
|
+
throw new Error("No available agents");
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
const randomIndex = Math.floor(Math.random() * filtered.length);
|
|
49
|
+
return filtered[randomIndex].url;
|
|
50
|
+
}
|
|
15
51
|
}
|
|
16
52
|
|
|
17
53
|
export const sanitizeV2RequestBody = (
|