@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.
@@ -11,12 +11,12 @@ export async function signResource({
11
11
  token,
12
12
  branchName,
13
13
  resource,
14
- agentUrls,
14
+ agentUrl,
15
15
  }: {
16
16
  token: string;
17
17
  branchName: string;
18
18
  resource: ApiResource | GenericResource;
19
- agentUrls: string[];
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
- try {
32
- const resp = await callAgentFallBack<SignatureResponse>({
33
- baseUrls: agentUrls,
34
- path: "v1/signature/sign",
35
- method: "post",
36
- token: token,
37
- data: { resource: requestResource },
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
- agentUrls,
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
- agentUrls: string[];
50
+ agentUrl: string;
55
51
  }): Promise<void> {
56
- try {
57
- await callAgentFallBack<{ keyId: string }>({
58
- baseUrls: agentUrls,
59
- path: "v1/signature/verify",
60
- method: "post",
61
- token: token,
62
- data: {
63
- resources: resources.map((res) => {
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
- ...res,
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
- } catch (e) {
79
- throw new Error("No agents available to verify the resource");
80
- }
65
+ }
66
+ return {
67
+ ...res,
68
+ branchName: branchName ?? "main",
69
+ };
70
+ }),
71
+ },
72
+ });
81
73
  }
82
74
 
83
- async function callAgentFallBack<T>({
84
- baseUrls,
75
+ async function callAgent<T>({
76
+ baseUrl,
85
77
  path,
86
78
  method,
87
79
  token,
88
80
  data,
89
81
  }: {
90
- baseUrls: string[];
82
+ baseUrl: string;
91
83
  path: string;
92
84
  method: Method;
93
85
  token: string;
94
86
  data: any;
95
87
  }): Promise<T> {
96
- for (const baseUrl of baseUrls) {
97
- try {
98
- const url = new URL(path, baseUrl);
99
- const config: AxiosRequestConfig = {
100
- url: url.toString(),
101
- method: method,
102
- headers: {
103
- Authorization: "Bearer " + token,
104
- },
105
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
106
- data: data,
107
- };
108
- const resp = await axios<T>(config);
109
- return resp.data;
110
- } catch (e) {}
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 getAgentUrls(agents: Agent[], agentType: AgentType): string[] {
11
- const filtered = agents.filter(
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
- return filtered.map((f) => f.url);
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 = (
package/tsconfig.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "outDir": "dist",
7
7
  "rootDir": "src",
8
8
  "strict": true,
9
- "target": "es2019",
9
+ "target": "es2021",
10
10
  "esModuleInterop": true,
11
11
  "skipLibCheck": true
12
12
  },