@nevermined-io/payments 1.0.0-rc0 → 1.0.0-rc2

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.
Files changed (71) hide show
  1. package/dist/a2a/agent-card.d.ts +85 -0
  2. package/dist/a2a/agent-card.d.ts.map +1 -0
  3. package/dist/a2a/agent-card.js +79 -0
  4. package/dist/a2a/agent-card.js.map +1 -0
  5. package/dist/a2a/index.d.ts +23 -0
  6. package/dist/a2a/index.d.ts.map +1 -0
  7. package/dist/a2a/index.js +42 -0
  8. package/dist/a2a/index.js.map +1 -0
  9. package/dist/a2a/paymentsRequestHandler.d.ts +107 -0
  10. package/dist/a2a/paymentsRequestHandler.d.ts.map +1 -0
  11. package/dist/a2a/paymentsRequestHandler.js +333 -0
  12. package/dist/a2a/paymentsRequestHandler.js.map +1 -0
  13. package/dist/a2a/server.d.ts +126 -0
  14. package/dist/a2a/server.d.ts.map +1 -0
  15. package/dist/a2a/server.js +189 -0
  16. package/dist/a2a/server.js.map +1 -0
  17. package/dist/a2a/types.d.ts +91 -0
  18. package/dist/a2a/types.d.ts.map +1 -0
  19. package/dist/a2a/types.js +16 -0
  20. package/dist/a2a/types.js.map +1 -0
  21. package/dist/api/agents-api.d.ts +196 -0
  22. package/dist/api/agents-api.d.ts.map +1 -0
  23. package/dist/api/agents-api.js +281 -0
  24. package/dist/api/agents-api.js.map +1 -0
  25. package/dist/api/base-payments.d.ts +35 -0
  26. package/dist/api/base-payments.d.ts.map +1 -0
  27. package/dist/api/base-payments.js +62 -0
  28. package/dist/api/base-payments.js.map +1 -0
  29. package/dist/api/nvm-api.d.ts +12 -12
  30. package/dist/api/nvm-api.d.ts.map +1 -1
  31. package/dist/api/nvm-api.js +57 -59
  32. package/dist/api/nvm-api.js.map +1 -1
  33. package/dist/api/plans-api.d.ts +349 -0
  34. package/dist/api/plans-api.d.ts.map +1 -0
  35. package/dist/api/plans-api.js +465 -0
  36. package/dist/api/plans-api.js.map +1 -0
  37. package/dist/api/query-api.d.ts +12 -41
  38. package/dist/api/query-api.d.ts.map +1 -1
  39. package/dist/api/query-api.js +17 -46
  40. package/dist/api/query-api.js.map +1 -1
  41. package/dist/api/requests-api.d.ts +108 -0
  42. package/dist/api/requests-api.d.ts.map +1 -0
  43. package/dist/api/requests-api.js +171 -0
  44. package/dist/api/requests-api.js.map +1 -0
  45. package/dist/common/helper.js.map +1 -1
  46. package/dist/common/payments.error.d.ts +31 -1
  47. package/dist/common/payments.error.d.ts.map +1 -1
  48. package/dist/common/payments.error.js +39 -1
  49. package/dist/common/payments.error.js.map +1 -1
  50. package/dist/common/types.d.ts +57 -3
  51. package/dist/common/types.d.ts.map +1 -1
  52. package/dist/common/types.js +10 -1
  53. package/dist/common/types.js.map +1 -1
  54. package/dist/environments.d.ts +1 -1
  55. package/dist/environments.d.ts.map +1 -1
  56. package/dist/environments.js +19 -28
  57. package/dist/environments.js.map +1 -1
  58. package/dist/index.d.ts +2 -0
  59. package/dist/index.d.ts.map +1 -1
  60. package/dist/index.js +14 -0
  61. package/dist/index.js.map +1 -1
  62. package/dist/payments.d.ts +35 -408
  63. package/dist/payments.d.ts.map +1 -1
  64. package/dist/payments.js +71 -618
  65. package/dist/payments.js.map +1 -1
  66. package/dist/plans.js.map +1 -1
  67. package/dist/utils.d.ts +30 -0
  68. package/dist/utils.d.ts.map +1 -1
  69. package/dist/utils.js +74 -1
  70. package/dist/utils.js.map +1 -1
  71. package/package.json +13 -6
@@ -0,0 +1,281 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AgentsAPI = void 0;
4
+ const helper_1 = require("../common/helper");
5
+ const payments_error_1 = require("../common/payments.error");
6
+ const types_1 = require("../common/types");
7
+ const base_payments_1 = require("./base-payments");
8
+ const nvm_api_1 = require("./nvm-api");
9
+ /**
10
+ * The AgentsAPI class provides methods to register and interact with AI Agents on Nevermined.
11
+ */
12
+ class AgentsAPI extends base_payments_1.BasePaymentsAPI {
13
+ /**
14
+ * This method is used to create a singleton instance of the AgentsAPI class.
15
+ *
16
+ * @param options - The options to initialize the payments class.
17
+ * @returns The instance of the AgentsAPI class.
18
+ */
19
+ static getInstance(options) {
20
+ return new AgentsAPI(options);
21
+ }
22
+ /**
23
+ *
24
+ * It registers a new AI Agent on Nevermined.
25
+ * The agent must be associated to one or multiple Payment Plans. Users that are subscribers of a payment plan can query the agent.
26
+ * Depending on the Payment Plan and the configuration of the agent, the usage of the agent/service will consume credits.
27
+ * When the plan expires (because the time is over or the credits are consumed), the user needs to renew the plan to continue using the agent.
28
+ *
29
+ * @remarks This method is oriented to AI Builders
30
+ * @remarks To call this method, the NVM API Key must have publication permissions
31
+ *
32
+ * @see https://docs.nevermined.app/docs/tutorials/builders/register-agent
33
+ *
34
+ * @param agentMetadata - @see {@link AgentMetadata}
35
+ * @param agentApi - @see {@link AgentAPIAttributes}
36
+ * @param paymentPlans - the list of payment plans giving access to the agent.
37
+ *
38
+ * @example
39
+ * ```
40
+ * const agentMetadata = { name: 'My AI Payments Agent', tags: ['test'] }
41
+ * const agentApi = { endpoints: [{ 'POST': 'https://example.com/api/v1/agents/:agentId/tasks' }] }
42
+ * const paymentPlans = [planId]
43
+ *
44
+ * const { agentId } = await payments.agents.registerAgent(agentMetadata, agentApi, paymentPlans)
45
+ * ```
46
+ *
47
+ * @returns The unique identifier of the newly created agent (Agent Id).
48
+ */
49
+ async registerAgent(agentMetadata, agentApi, paymentPlans) {
50
+ const body = {
51
+ metadataAttributes: agentMetadata,
52
+ agentApiAttributes: agentApi,
53
+ plans: paymentPlans,
54
+ };
55
+ const options = this.getBackendHTTPOptions('POST', body);
56
+ const url = new URL(nvm_api_1.API_URL_REGISTER_AGENT, this.environment.backend);
57
+ const response = await fetch(url, options);
58
+ if (!response.ok) {
59
+ throw new payments_error_1.PaymentsError(`Unable to register agent. ${response.statusText} - ${await response.text()}`);
60
+ }
61
+ const agentData = await response.json();
62
+ return { agentId: agentData.agentId };
63
+ }
64
+ /**
65
+ *
66
+ * It registers a new AI Agent and a Payment Plan associated to this new agent.
67
+ * Depending on the Payment Plan and the configuration of the agent, the usage of the agent/service will consume credits.
68
+ * When the plan expires (because the time is over or the credits are consumed), the user needs to renew the plan to continue using the agent.
69
+ *
70
+ * @remarks This method is oriented to AI Builders
71
+ * @remarks To call this method, the NVM API Key must have publication permissions
72
+ *
73
+ * @see https://docs.nevermined.app/docs/tutorials/builders/register-agent
74
+ *
75
+ * @param agentMetadata - @see {@link AgentMetadata}
76
+ * @param agentApi - @see {@link AgentAPIAttributes}
77
+ * @param planMetadata - @see {@link PlanMetadata}
78
+ * @param priceConfig - @see {@link PlanPriceConfig}
79
+ * @param creditsConfig - @see {@link PlanCreditsConfig}
80
+ *
81
+ * @example
82
+ * ```
83
+ * const agentMetadata = { name: 'My AI Payments Agent', tags: ['test'] }
84
+ * const agentApi { endpoints: [{ 'POST': 'https://example.com/api/v1/agents/:agentId/tasks' }] }
85
+ * const cryptoPriceConfig = getNativeTokenPriceConfig(100n, builderAddress)
86
+ * const 1dayDurationPlan = getExpirableDurationConfig(ONE_DAY_DURATION)
87
+ * const { agentId, planId } = await payments.agents.registerAgentAndPlan(
88
+ * agentMetadata,
89
+ * agentApi,
90
+ * cryptoPriceConfig,
91
+ * 1dayDurationPlan
92
+ * )
93
+ * ```
94
+ *
95
+ * @returns The unique identifier of the newly created agent (agentId).
96
+ * @returns The unique identifier of the newly created plan (planId).
97
+ */
98
+ async registerAgentAndPlan(agentMetadata, agentApi, planMetadata, priceConfig, creditsConfig) {
99
+ const body = {
100
+ plan: {
101
+ metadataAttributes: planMetadata,
102
+ priceConfig: priceConfig,
103
+ creditsConfig: creditsConfig,
104
+ },
105
+ agent: {
106
+ metadataAttributes: agentMetadata,
107
+ agentApiAttributes: agentApi,
108
+ },
109
+ };
110
+ console.log('Registering agent and plan with body:', JSON.stringify(body, helper_1.jsonReplacer));
111
+ const options = this.getBackendHTTPOptions('POST', body);
112
+ const url = new URL(nvm_api_1.API_URL_REGISTER_AGENTS_AND_PLAN, this.environment.backend);
113
+ const response = await fetch(url, options);
114
+ if (!response.ok) {
115
+ throw new payments_error_1.PaymentsError(`Unable to register agent & plan. ${response.statusText} - ${await response.text()}`);
116
+ }
117
+ const result = await response.json();
118
+ return {
119
+ agentId: result.data.agentId,
120
+ planId: result.data.planId,
121
+ txHash: result.txHash,
122
+ };
123
+ }
124
+ /**
125
+ * Gets the metadata for a given Agent identifier.
126
+ *
127
+ * @param agentId - The unique identifier of the agent.
128
+ * @returns A promise that resolves to the agent's metadata.
129
+ * @throws PaymentsError if the agent is not found.
130
+ *
131
+ * @example
132
+ * ```
133
+ * const plan = payments.agents.getAgent(agentId)
134
+ * ```
135
+ */
136
+ async getAgent(agentId) {
137
+ const url = new URL(nvm_api_1.API_URL_GET_AGENT.replace(':agentId', agentId), this.environment.backend);
138
+ const response = await fetch(url);
139
+ if (!response.ok) {
140
+ throw new payments_error_1.PaymentsError(`Agent not found. ${response.statusText} - ${await response.text()}`);
141
+ }
142
+ return response.json();
143
+ }
144
+ /**
145
+ * Gets the list of plans that can be ordered to get access to an agent.
146
+ *
147
+ * @param agentId - The unique identifier of the agent.
148
+ * @param pagination - Optional pagination options to control the number of results returned.p
149
+ * @returns A promise that resolves to the list of all different plans giving access to the agent.
150
+ * @throws PaymentsError if the agent is not found.
151
+ *
152
+ * @example
153
+ * ```
154
+ * const result = payments.agents.getAgentPlans(planId)
155
+ * // {
156
+ * // total: 10,
157
+ * // page: 1,
158
+ * // offset: 5,
159
+ * // plans: [ ..]
160
+ * // }
161
+ * ```
162
+ */
163
+ async getAgentPlans(agentId, pagination = new types_1.PaginationOptions()) {
164
+ const query = nvm_api_1.API_URL_GET_AGENT_PLANS.replace(':agentId', agentId) + '?' + pagination.asQueryParams();
165
+ const url = new URL(query, this.environment.backend);
166
+ console.log(`Fetching plans for agent ${agentId} from ${url.toString()}`);
167
+ const response = await fetch(url);
168
+ if (!response.ok) {
169
+ throw new payments_error_1.PaymentsError(`Agent not found. ${response.statusText} - ${await response.text()}`);
170
+ }
171
+ return response.json();
172
+ }
173
+ /**
174
+ * Adds an existing Payment Plan to an AI Agent.
175
+ * After this operation, users with access to the Payment Plan will be able to access the AI Agent.
176
+ *
177
+ * @remarks
178
+ * Only the owner of the Payment Plan can call this method.
179
+ *
180
+ * @param planId - The unique identifier of the Payment Plan.
181
+ * @param agentId - The unique identifier of the AI Agent.
182
+ * @returns @see {@link NvmAPIResult} A promise that resolves indicating if the operation was successful.
183
+ * @throws PaymentsError if unable to add the plan to the agent.
184
+ *
185
+ * @example
186
+ * ```
187
+ * const result = await payments.agents.addPlanToAgent(planId, agentId)
188
+ * // {
189
+ * // txHash: '0x8d29d5769e832a35e53f80cd4e8890d941c50a09c33dbd975533debc894f2535',
190
+ * // success: true
191
+ * // }
192
+ * ```
193
+ */
194
+ async addPlanToAgent(planId, agentId) {
195
+ const options = this.getBackendHTTPOptions('POST');
196
+ const endpoint = nvm_api_1.API_URL_ADD_PLAN_AGENT.replace(':planId', planId).replace(':agentId', agentId);
197
+ const url = new URL(endpoint, this.environment.backend);
198
+ const response = await fetch(url, options);
199
+ if (!response.ok) {
200
+ throw new payments_error_1.PaymentsError(`Unable to add plan to agent. ${response.statusText} - ${await response.text()}`);
201
+ }
202
+ return response.json();
203
+ }
204
+ /**
205
+ * Removes a Payment Plan from an AI Agent.
206
+ * After this operation, users with access to the Payment Plan will no longer be able to access the AI Agent.
207
+ *
208
+ * @remarks
209
+ * Only the owner of the Payment Plan can call this method.
210
+ *
211
+ * @param planId - The unique identifier of the Payment Plan.
212
+ * @param agentId - The unique identifier of the AI Agent.
213
+ * @returns @see {@link NvmAPIResult} A promise that resolves indicating if the operation was successful.
214
+ * @throws PaymentsError if unable to remove the plan from the agent.
215
+ *
216
+ * @example
217
+ * ```
218
+ * const result = await payments.agents.removePlanFromAgent(planId, agentId)
219
+ * // {
220
+ * // txHash: '0x8d29d5769e832a35e53f80cd4e8890d941c50a09c33dbd975533debc894f2535',
221
+ * // success: true
222
+ * // }
223
+ * ```
224
+ */
225
+ async removePlanFromAgent(planId, agentId) {
226
+ const options = this.getBackendHTTPOptions('DELETE');
227
+ const endpoint = nvm_api_1.API_URL_REMOVE_PLAN_AGENT.replace(':planId', planId).replace(':agentId', agentId);
228
+ const url = new URL(endpoint, this.environment.backend);
229
+ const response = await fetch(url, options);
230
+ if (!response.ok) {
231
+ throw new payments_error_1.PaymentsError(`Unable to remove plan from agent. ${response.statusText} - ${await response.text()}`);
232
+ }
233
+ return response.json();
234
+ }
235
+ /**
236
+ * When the user calling this method is a valid subscriber, it generates an access token related to the Payment Plan and the AI Agent.
237
+ * The access token can be used to query the AI Agent's API endpoints. The access token is unique for the subscriber, payment plan and agent.
238
+ *
239
+ * @remarks
240
+ * Only a valid subscriber of the Payment Plan can generate a valid access token.
241
+ * @remarks
242
+ * The access token generated can be send by HTTP Authorization header to query the AI Agent's API endpoints.
243
+ *
244
+ * @param planId - The unique identifier of the Payment Plan.
245
+ * @param agentId - The unique identifier of the AI Agent.
246
+ * @returns @see {@link AgentAccessCredentials} The access token and the proxy URL to query the agent.
247
+ * @throws PaymentsError if unable to remove the plan from the agent.
248
+ *
249
+ * @example
250
+ * ```
251
+ * const credentials = await payments.agents.getAgentAccessToken(planId, agentId)
252
+ * // {
253
+ * // accessToken: 'eyJhbGciOiJFUzI1NksifQ.eyJpc3MiOiIweD ...',
254
+ * // proxies: ['https://proxy.nevermined.network/']
255
+ * // }
256
+ *
257
+ * const agentHTTPOptions = {
258
+ * method: 'POST',
259
+ * headers: {
260
+ * Accept: 'application/json',
261
+ * 'Content-Type': 'application/json',
262
+ * Authorization: `Bearer ${credentials.accessToken}`,
263
+ * }
264
+ * }
265
+ *
266
+ * const response = await fetch(new URL(agentEndpoint), agentHTTPOptions)
267
+ * ```
268
+ */
269
+ async getAgentAccessToken(planId, agentId) {
270
+ const accessTokenUrl = nvm_api_1.API_URL_GET_AGENT_ACCESS_TOKEN.replace(':planId', planId).replace(':agentId', agentId);
271
+ const options = this.getBackendHTTPOptions('GET');
272
+ const url = new URL(accessTokenUrl, this.environment.backend);
273
+ const response = await fetch(url, options);
274
+ if (!response.ok) {
275
+ throw new payments_error_1.PaymentsError(`Unable to get agent access token. ${response.statusText} - ${await response.text()}`);
276
+ }
277
+ return response.json();
278
+ }
279
+ }
280
+ exports.AgentsAPI = AgentsAPI;
281
+ //# sourceMappingURL=agents-api.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agents-api.js","sourceRoot":"","sources":["../../src/api/agents-api.ts"],"names":[],"mappings":";;;AAAA,6CAA+C;AAC/C,6DAAwD;AACxD,2CAUwB;AACxB,mDAAiD;AACjD,uCAQkB;AAElB;;GAEG;AACH,MAAa,SAAU,SAAQ,+BAAe;IAC5C;;;;;OAKG;IACH,MAAM,CAAC,WAAW,CAAC,OAAuB;QACxC,OAAO,IAAI,SAAS,CAAC,OAAO,CAAC,CAAA;IAC/B,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACI,KAAK,CAAC,aAAa,CACxB,aAA4B,EAC5B,QAA4B,EAC5B,YAAsB;QAEtB,MAAM,IAAI,GAAG;YACX,kBAAkB,EAAE,aAAa;YACjC,kBAAkB,EAAE,QAAQ;YAC5B,KAAK,EAAE,YAAY;SACpB,CAAA;QAED,MAAM,OAAO,GAAG,IAAI,CAAC,qBAAqB,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;QACxD,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,gCAAsB,EAAE,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAA;QAErE,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;QAC1C,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YACjB,MAAM,IAAI,8BAAa,CACrB,6BAA6B,QAAQ,CAAC,UAAU,MAAM,MAAM,QAAQ,CAAC,IAAI,EAAE,EAAE,CAC9E,CAAA;QACH,CAA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AAU,MAAM,MAAM,QAAQ,CAAC,IAAI,EAAE,EAAE,CACtF,CAAA;QACH,CAAC;QAED,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAA;IACxB,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACI,KAAK,CAAC,mBAAmB,CAC9B,MAAc,EACd,OAAe;QAEf,MAAM,cAAc,GAAG,wCAA8B,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,OAAO,CACtF,UAAU,EACV,OAAQ,CACT,CAAA;QACD,MAAM,OAAO,GAAG,IAAI,CAAC,qBAAqB,CAAC,KAAK,CAAC,CAAA;QAEjD,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,cAAc,EAAE,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAA;QAC7D,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;QAC1C,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YACjB,MAAM,IAAI,8BAAa,CACrB,qCAAqC,QAAQ,CAAC,UAAU,MAAM,MAAM,QAAQ,CAAC,IAAI,EAAE,EAAE,CACtF,CAAA;QACH,CAAC;QAED,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAA;IACxB,CAAC;CACF;AA3TD,8BA2TC","sourcesContent":["import { jsonReplacer } from '../common/helper'\nimport { PaymentsError } from '../common/payments.error'\nimport {\n AgentAccessCredentials,\n AgentAPIAttributes,\n AgentMetadata,\n NvmAPIResult,\n PaginationOptions,\n PaymentOptions,\n PlanCreditsConfig,\n PlanMetadata,\n PlanPriceConfig,\n} from '../common/types'\nimport { BasePaymentsAPI } from './base-payments'\nimport {\n API_URL_ADD_PLAN_AGENT,\n API_URL_GET_AGENT,\n API_URL_GET_AGENT_ACCESS_TOKEN,\n API_URL_GET_AGENT_PLANS,\n API_URL_REGISTER_AGENT,\n API_URL_REGISTER_AGENTS_AND_PLAN,\n API_URL_REMOVE_PLAN_AGENT,\n} from './nvm-api'\n\n/**\n * The AgentsAPI class provides methods to register and interact with AI Agents on Nevermined.\n */\nexport class AgentsAPI extends BasePaymentsAPI {\n /**\n * This method is used to create a singleton instance of the AgentsAPI class.\n *\n * @param options - The options to initialize the payments class.\n * @returns The instance of the AgentsAPI class.\n */\n static getInstance(options: PaymentOptions): AgentsAPI {\n return new AgentsAPI(options)\n }\n\n /**\n *\n * It registers a new AI Agent on Nevermined.\n * The agent must be associated to one or multiple Payment Plans. Users that are subscribers of a payment plan can query the agent.\n * Depending on the Payment Plan and the configuration of the agent, the usage of the agent/service will consume credits.\n * When the plan expires (because the time is over or the credits are consumed), the user needs to renew the plan to continue using the agent.\n *\n * @remarks This method is oriented to AI Builders\n * @remarks To call this method, the NVM API Key must have publication permissions\n *\n * @see https://docs.nevermined.app/docs/tutorials/builders/register-agent\n *\n * @param agentMetadata - @see {@link AgentMetadata}\n * @param agentApi - @see {@link AgentAPIAttributes}\n * @param paymentPlans - the list of payment plans giving access to the agent.\n *\n * @example\n * ```\n * const agentMetadata = { name: 'My AI Payments Agent', tags: ['test'] }\n * const agentApi = { endpoints: [{ 'POST': 'https://example.com/api/v1/agents/:agentId/tasks' }] }\n * const paymentPlans = [planId]\n *\n * const { agentId } = await payments.agents.registerAgent(agentMetadata, agentApi, paymentPlans)\n * ```\n *\n * @returns The unique identifier of the newly created agent (Agent Id).\n */\n public async registerAgent(\n agentMetadata: AgentMetadata,\n agentApi: AgentAPIAttributes,\n paymentPlans: string[],\n ): Promise<{ agentId: string }> {\n const body = {\n metadataAttributes: agentMetadata,\n agentApiAttributes: agentApi,\n plans: paymentPlans,\n }\n\n const options = this.getBackendHTTPOptions('POST', body)\n const url = new URL(API_URL_REGISTER_AGENT, this.environment.backend)\n\n const response = await fetch(url, options)\n if (!response.ok) {\n throw new PaymentsError(\n `Unable to register agent. ${response.statusText} - ${await response.text()}`,\n )\n }\n const agentData = await response.json()\n return { agentId: agentData.agentId }\n }\n\n /**\n *\n * It registers a new AI Agent and a Payment Plan associated to this new agent.\n * Depending on the Payment Plan and the configuration of the agent, the usage of the agent/service will consume credits.\n * When the plan expires (because the time is over or the credits are consumed), the user needs to renew the plan to continue using the agent.\n *\n * @remarks This method is oriented to AI Builders\n * @remarks To call this method, the NVM API Key must have publication permissions\n *\n * @see https://docs.nevermined.app/docs/tutorials/builders/register-agent\n *\n * @param agentMetadata - @see {@link AgentMetadata}\n * @param agentApi - @see {@link AgentAPIAttributes}\n * @param planMetadata - @see {@link PlanMetadata}\n * @param priceConfig - @see {@link PlanPriceConfig}\n * @param creditsConfig - @see {@link PlanCreditsConfig}\n *\n * @example\n * ```\n * const agentMetadata = { name: 'My AI Payments Agent', tags: ['test'] }\n * const agentApi { endpoints: [{ 'POST': 'https://example.com/api/v1/agents/:agentId/tasks' }] }\n * const cryptoPriceConfig = getNativeTokenPriceConfig(100n, builderAddress)\n * const 1dayDurationPlan = getExpirableDurationConfig(ONE_DAY_DURATION)\n * const { agentId, planId } = await payments.agents.registerAgentAndPlan(\n * agentMetadata,\n * agentApi,\n * cryptoPriceConfig,\n * 1dayDurationPlan\n * )\n * ```\n *\n * @returns The unique identifier of the newly created agent (agentId).\n * @returns The unique identifier of the newly created plan (planId).\n */\n public async registerAgentAndPlan(\n agentMetadata: AgentMetadata,\n agentApi: AgentAPIAttributes,\n planMetadata: PlanMetadata,\n priceConfig: PlanPriceConfig,\n creditsConfig: PlanCreditsConfig,\n ): Promise<{\n agentId: string\n planId: string\n txHash: string\n }> {\n const body = {\n plan: {\n metadataAttributes: planMetadata,\n priceConfig: priceConfig,\n creditsConfig: creditsConfig,\n },\n agent: {\n metadataAttributes: agentMetadata,\n agentApiAttributes: agentApi,\n },\n }\n console.log('Registering agent and plan with body:', JSON.stringify(body, jsonReplacer))\n const options = this.getBackendHTTPOptions('POST', body)\n const url = new URL(API_URL_REGISTER_AGENTS_AND_PLAN, this.environment.backend)\n\n const response = await fetch(url, options)\n if (!response.ok) {\n throw new PaymentsError(\n `Unable to register agent & plan. ${response.statusText} - ${await response.text()}`,\n )\n }\n const result = await response.json()\n return {\n agentId: result.data.agentId,\n planId: result.data.planId,\n txHash: result.txHash,\n }\n }\n\n /**\n * Gets the metadata for a given Agent identifier.\n *\n * @param agentId - The unique identifier of the agent.\n * @returns A promise that resolves to the agent's metadata.\n * @throws PaymentsError if the agent is not found.\n *\n * @example\n * ```\n * const plan = payments.agents.getAgent(agentId)\n * ```\n */\n public async getAgent(agentId: string) {\n const url = new URL(API_URL_GET_AGENT.replace(':agentId', agentId), this.environment.backend)\n const response = await fetch(url)\n if (!response.ok) {\n throw new PaymentsError(`Agent not found. ${response.statusText} - ${await response.text()}`)\n }\n return response.json()\n }\n\n /**\n * Gets the list of plans that can be ordered to get access to an agent.\n *\n * @param agentId - The unique identifier of the agent.\n * @param pagination - Optional pagination options to control the number of results returned.p\n * @returns A promise that resolves to the list of all different plans giving access to the agent.\n * @throws PaymentsError if the agent is not found.\n *\n * @example\n * ```\n * const result = payments.agents.getAgentPlans(planId)\n * // {\n * // total: 10,\n * // page: 1,\n * // offset: 5,\n * // plans: [ ..]\n * // }\n * ```\n */\n public async getAgentPlans(agentId: string, pagination = new PaginationOptions()) {\n const query =\n API_URL_GET_AGENT_PLANS.replace(':agentId', agentId) + '?' + pagination.asQueryParams()\n const url = new URL(query, this.environment.backend)\n console.log(`Fetching plans for agent ${agentId} from ${url.toString()}`)\n const response = await fetch(url)\n if (!response.ok) {\n throw new PaymentsError(`Agent not found. ${response.statusText} - ${await response.text()}`)\n }\n return response.json()\n }\n\n /**\n * Adds an existing Payment Plan to an AI Agent.\n * After this operation, users with access to the Payment Plan will be able to access the AI Agent.\n *\n * @remarks\n * Only the owner of the Payment Plan can call this method.\n *\n * @param planId - The unique identifier of the Payment Plan.\n * @param agentId - The unique identifier of the AI Agent.\n * @returns @see {@link NvmAPIResult} A promise that resolves indicating if the operation was successful.\n * @throws PaymentsError if unable to add the plan to the agent.\n *\n * @example\n * ```\n * const result = await payments.agents.addPlanToAgent(planId, agentId)\n * // {\n * // txHash: '0x8d29d5769e832a35e53f80cd4e8890d941c50a09c33dbd975533debc894f2535',\n * // success: true\n * // }\n * ```\n */\n public async addPlanToAgent(planId: string, agentId: string): Promise<NvmAPIResult> {\n const options = this.getBackendHTTPOptions('POST')\n const endpoint = API_URL_ADD_PLAN_AGENT.replace(':planId', planId).replace(':agentId', agentId)\n const url = new URL(endpoint, this.environment.backend)\n const response = await fetch(url, options)\n if (!response.ok) {\n throw new PaymentsError(\n `Unable to add plan to agent. ${response.statusText} - ${await response.text()}`,\n )\n }\n\n return response.json()\n }\n\n /**\n * Removes a Payment Plan from an AI Agent.\n * After this operation, users with access to the Payment Plan will no longer be able to access the AI Agent.\n *\n * @remarks\n * Only the owner of the Payment Plan can call this method.\n *\n * @param planId - The unique identifier of the Payment Plan.\n * @param agentId - The unique identifier of the AI Agent.\n * @returns @see {@link NvmAPIResult} A promise that resolves indicating if the operation was successful.\n * @throws PaymentsError if unable to remove the plan from the agent.\n *\n * @example\n * ```\n * const result = await payments.agents.removePlanFromAgent(planId, agentId)\n * // {\n * // txHash: '0x8d29d5769e832a35e53f80cd4e8890d941c50a09c33dbd975533debc894f2535',\n * // success: true\n * // }\n * ```\n */\n public async removePlanFromAgent(planId: string, agentId: string): Promise<NvmAPIResult> {\n const options = this.getBackendHTTPOptions('DELETE')\n const endpoint = API_URL_REMOVE_PLAN_AGENT.replace(':planId', planId).replace(\n ':agentId',\n agentId,\n )\n const url = new URL(endpoint, this.environment.backend)\n const response = await fetch(url, options)\n if (!response.ok) {\n throw new PaymentsError(\n `Unable to remove plan from agent. ${response.statusText} - ${await response.text()}`,\n )\n }\n\n return response.json()\n }\n\n /**\n * When the user calling this method is a valid subscriber, it generates an access token related to the Payment Plan and the AI Agent.\n * The access token can be used to query the AI Agent's API endpoints. The access token is unique for the subscriber, payment plan and agent.\n *\n * @remarks\n * Only a valid subscriber of the Payment Plan can generate a valid access token.\n * @remarks\n * The access token generated can be send by HTTP Authorization header to query the AI Agent's API endpoints.\n *\n * @param planId - The unique identifier of the Payment Plan.\n * @param agentId - The unique identifier of the AI Agent.\n * @returns @see {@link AgentAccessCredentials} The access token and the proxy URL to query the agent.\n * @throws PaymentsError if unable to remove the plan from the agent.\n *\n * @example\n * ```\n * const credentials = await payments.agents.getAgentAccessToken(planId, agentId)\n * // {\n * // accessToken: 'eyJhbGciOiJFUzI1NksifQ.eyJpc3MiOiIweD ...',\n * // proxies: ['https://proxy.nevermined.network/']\n * // }\n *\n * const agentHTTPOptions = {\n * method: 'POST',\n * headers: {\n * Accept: 'application/json',\n * 'Content-Type': 'application/json',\n * Authorization: `Bearer ${credentials.accessToken}`,\n * }\n * }\n *\n * const response = await fetch(new URL(agentEndpoint), agentHTTPOptions)\n * ```\n */\n public async getAgentAccessToken(\n planId: string,\n agentId: string,\n ): Promise<AgentAccessCredentials> {\n const accessTokenUrl = API_URL_GET_AGENT_ACCESS_TOKEN.replace(':planId', planId).replace(\n ':agentId',\n agentId!,\n )\n const options = this.getBackendHTTPOptions('GET')\n\n const url = new URL(accessTokenUrl, this.environment.backend)\n const response = await fetch(url, options)\n if (!response.ok) {\n throw new PaymentsError(\n `Unable to get agent access token. ${response.statusText} - ${await response.text()}`,\n )\n }\n\n return response.json()\n }\n}\n"]}
@@ -0,0 +1,35 @@
1
+ import { EnvironmentInfo } from '../environments';
2
+ import { PaymentOptions } from '../common/types';
3
+ /**
4
+ * Base class extended by all Payments API classes.
5
+ * It provides common functionality such as parsing the NVM API Key and getting the account address.
6
+ */
7
+ export declare abstract class BasePaymentsAPI {
8
+ protected nvmApiKey: string;
9
+ protected environment: EnvironmentInfo;
10
+ protected returnUrl: string;
11
+ protected appId?: string;
12
+ protected version?: string;
13
+ protected accountAddress?: string;
14
+ isBrowserInstance: boolean;
15
+ constructor(options: PaymentOptions);
16
+ /**
17
+ * Parses the NVM API Key to extract the account address.
18
+ * @throws PaymentsError if the API key is invalid.
19
+ */
20
+ protected parseNvmApiKey(): void;
21
+ /**
22
+ * It returns the account address associated with the NVM API Key used to initialize the Payments Library instance.
23
+ * @returns The account address extracted from the NVM API Key
24
+ */
25
+ getAccountAddress(): string | undefined;
26
+ /**
27
+ * Returns the HTTP options required to query the backend.
28
+ * @param method - HTTP method.
29
+ * @param body - Optional request body.
30
+ * @returns HTTP options object.
31
+ * @internal
32
+ */
33
+ protected getBackendHTTPOptions(method: string, body?: any): any;
34
+ }
35
+ //# sourceMappingURL=base-payments.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base-payments.d.ts","sourceRoot":"","sources":["../../src/api/base-payments.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAgB,MAAM,iBAAiB,CAAA;AAC/D,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAIhD;;;GAGG;AACH,8BAAsB,eAAe;IACnC,SAAS,CAAC,SAAS,EAAE,MAAM,CAAA;IAC3B,SAAS,CAAC,WAAW,EAAE,eAAe,CAAA;IACtC,SAAS,CAAC,SAAS,EAAE,MAAM,CAAA;IAC3B,SAAS,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;IACxB,SAAS,CAAC,OAAO,CAAC,EAAE,MAAM,CAAA;IAC1B,SAAS,CAAC,cAAc,CAAC,EAAE,MAAM,CAAA;IAC1B,iBAAiB,UAAO;gBAEnB,OAAO,EAAE,cAAc;IASnC;;;OAGG;IACH,SAAS,CAAC,cAAc;IASxB;;;OAGG;IACI,iBAAiB,IAAI,MAAM,GAAG,SAAS;IAI9C;;;;;;OAMG;IACH,SAAS,CAAC,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG;CAW3D"}
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BasePaymentsAPI = void 0;
4
+ const helper_1 = require("../common/helper");
5
+ const environments_1 = require("../environments");
6
+ const jose_1 = require("jose");
7
+ const payments_error_1 = require("../common/payments.error");
8
+ /**
9
+ * Base class extended by all Payments API classes.
10
+ * It provides common functionality such as parsing the NVM API Key and getting the account address.
11
+ */
12
+ class BasePaymentsAPI {
13
+ constructor(options) {
14
+ this.isBrowserInstance = true;
15
+ this.nvmApiKey = options.nvmApiKey;
16
+ this.returnUrl = options.returnUrl || '';
17
+ this.environment = environments_1.Environments[options.environment];
18
+ this.appId = options.appId;
19
+ this.version = options.version;
20
+ this.parseNvmApiKey();
21
+ }
22
+ /**
23
+ * Parses the NVM API Key to extract the account address.
24
+ * @throws PaymentsError if the API key is invalid.
25
+ */
26
+ parseNvmApiKey() {
27
+ try {
28
+ const jwt = (0, jose_1.decodeJwt)(this.nvmApiKey);
29
+ this.accountAddress = jwt.sub;
30
+ }
31
+ catch (error) {
32
+ throw new payments_error_1.PaymentsError('Invalid NVM API Key');
33
+ }
34
+ }
35
+ /**
36
+ * It returns the account address associated with the NVM API Key used to initialize the Payments Library instance.
37
+ * @returns The account address extracted from the NVM API Key
38
+ */
39
+ getAccountAddress() {
40
+ return this.accountAddress;
41
+ }
42
+ /**
43
+ * Returns the HTTP options required to query the backend.
44
+ * @param method - HTTP method.
45
+ * @param body - Optional request body.
46
+ * @returns HTTP options object.
47
+ * @internal
48
+ */
49
+ getBackendHTTPOptions(method, body) {
50
+ return {
51
+ method,
52
+ headers: {
53
+ Accept: 'application/json',
54
+ 'Content-Type': 'application/json',
55
+ Authorization: `Bearer ${this.nvmApiKey}`,
56
+ },
57
+ ...(body && { body: JSON.stringify(body, helper_1.jsonReplacer) }),
58
+ };
59
+ }
60
+ }
61
+ exports.BasePaymentsAPI = BasePaymentsAPI;
62
+ //# sourceMappingURL=base-payments.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base-payments.js","sourceRoot":"","sources":["../../src/api/base-payments.ts"],"names":[],"mappings":";;;AAAA,6CAA+C;AAC/C,kDAA+D;AAE/D,+BAAgC;AAChC,6DAAwD;AAExD;;;GAGG;AACH,MAAsB,eAAe;IASnC,YAAY,OAAuB;QAF5B,sBAAiB,GAAG,IAAI,CAAA;QAG7B,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAA;QAClC,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,EAAE,CAAA;QACxC,IAAI,CAAC,WAAW,GAAG,2BAAY,CAAC,OAAO,CAAC,WAAW,CAAC,CAAA;QACpD,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAA;QAC1B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAA;QAC9B,IAAI,CAAC,cAAc,EAAE,CAAA;IACvB,CAAC;IAED;;;OAGG;IACO,cAAc;QACtB,IAAI,CAAC;YACH,MAAM,GAAG,GAAG,IAAA,gBAAS,EAAC,IAAI,CAAC,SAAU,CAAC,CAAA;YACtC,IAAI,CAAC,cAAc,GAAG,GAAG,CAAC,GAAG,CAAA;QAC/B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,8BAAa,CAAC,qBAAqB,CAAC,CAAA;QAChD,CAAC;IACH,CAAC;IAED;;;OAGG;IACI,iBAAiB;QACtB,OAAO,IAAI,CAAC,cAAc,CAAA;IAC5B,CAAC;IAED;;;;;;OAMG;IACO,qBAAqB,CAAC,MAAc,EAAE,IAAU;QACxD,OAAO;YACL,MAAM;YACN,OAAO,EAAE;gBACP,MAAM,EAAE,kBAAkB;gBAC1B,cAAc,EAAE,kBAAkB;gBAClC,aAAa,EAAE,UAAU,IAAI,CAAC,SAAS,EAAE;aAC1C;YACD,GAAG,CAAC,IAAI,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,qBAAY,CAAC,EAAE,CAAC;SAC1D,CAAA;IACH,CAAC;CACF;AAzDD,0CAyDC","sourcesContent":["import { jsonReplacer } from '../common/helper'\nimport { EnvironmentInfo, Environments } from '../environments'\nimport { PaymentOptions } from '../common/types'\nimport { decodeJwt } from 'jose'\nimport { PaymentsError } from '../common/payments.error'\n\n/**\n * Base class extended by all Payments API classes.\n * It provides common functionality such as parsing the NVM API Key and getting the account address.\n */\nexport abstract class BasePaymentsAPI {\n protected nvmApiKey: string\n protected environment: EnvironmentInfo\n protected returnUrl: string\n protected appId?: string\n protected version?: string\n protected accountAddress?: string\n public isBrowserInstance = true\n\n constructor(options: PaymentOptions) {\n this.nvmApiKey = options.nvmApiKey\n this.returnUrl = options.returnUrl || ''\n this.environment = Environments[options.environment]\n this.appId = options.appId\n this.version = options.version\n this.parseNvmApiKey()\n }\n\n /**\n * Parses the NVM API Key to extract the account address.\n * @throws PaymentsError if the API key is invalid.\n */\n protected parseNvmApiKey() {\n try {\n const jwt = decodeJwt(this.nvmApiKey!)\n this.accountAddress = jwt.sub\n } catch (error) {\n throw new PaymentsError('Invalid NVM API Key')\n }\n }\n\n /**\n * It returns the account address associated with the NVM API Key used to initialize the Payments Library instance.\n * @returns The account address extracted from the NVM API Key\n */\n public getAccountAddress(): string | undefined {\n return this.accountAddress\n }\n\n /**\n * Returns the HTTP options required to query the backend.\n * @param method - HTTP method.\n * @param body - Optional request body.\n * @returns HTTP options object.\n * @internal\n */\n protected getBackendHTTPOptions(method: string, body?: any) {\n return {\n method,\n headers: {\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${this.nvmApiKey}`,\n },\n ...(body && { body: JSON.stringify(body, jsonReplacer) }),\n }\n }\n}\n"]}
@@ -1,5 +1,6 @@
1
1
  export declare const API_URL_REGISTER_PLAN = "/api/v1/protocol/plans";
2
2
  export declare const API_URL_REGISTER_AGENT = "/api/v1/protocol/agents";
3
+ export declare const API_URL_REGISTER_AGENTS_AND_PLAN = "/api/v1/protocol/agents/plans";
3
4
  export declare const API_URL_GET_AGENT = "/api/v1/protocol/agents/:agentId";
4
5
  export declare const API_URL_GET_AGENT_PLANS = "/api/v1/protocol/agents/:agentId/plans";
5
6
  export declare const API_URL_GET_PLAN = "/api/v1/protocol/plans/:planId";
@@ -9,10 +10,12 @@ export declare const API_URL_MINT_PLAN = "/api/v1/protocol/plans/mint";
9
10
  export declare const API_URL_MINT_EXPIRABLE_PLAN = "/api/v1/protocol/plans/mintExpirable";
10
11
  export declare const API_URL_ADD_PLAN_AGENT = "/api/v1/protocol/agents/:agentId/plan/:planId";
11
12
  export declare const API_URL_REMOVE_PLAN_AGENT = "/api/v1/protocol/agents/:agentId/plan/:planId";
12
- export declare const API_URL_BURN_PLAN = "/api/v1/protocol/plans/burn";
13
+ export declare const API_URL_REDEEM_PLAN = "/api/v1/protocol/plans/redeem";
13
14
  export declare const API_URL_PLAN_BALANCE = "/api/v1/protocol/plans/:planId/balance/:holderAddress";
14
15
  export declare const API_URL_GET_AGENT_ACCESS_TOKEN = "/api/v1/protocol/token/:planId/:agentId";
15
- export declare const API_URL_VALIDATE_AGENT_ACCESS_TOKEN = "/api/v1/protocol/token/validate/:agentId";
16
+ export declare const API_URL_INITIALIZE_AGENT = "/api/v1/protocol/agents/initialize/:agentId";
17
+ export declare const API_URL_TRACK_AGENT_SUB_TASK = "/api/v1/protocol/agent-sub-tasks";
18
+ export declare const API_URL_STRIPE_CHECKOUT = "/api/v1/stripe/checkout";
16
19
  export interface BackendApiOptions {
17
20
  /**
18
21
  * The host of the backend server
@@ -36,24 +39,21 @@ export interface BackendApiOptions {
36
39
  };
37
40
  }
38
41
  export declare class HTTPRequestOptions {
42
+ accessToken?: string;
39
43
  sendThroughProxy: boolean;
40
44
  proxyHost?: string;
41
45
  headers?: {
42
46
  [key: string]: string;
43
47
  };
44
48
  }
45
- export declare class NVMBackendApi {
46
- protected opts: BackendApiOptions;
47
- private hasKey;
48
- private agentId;
49
- constructor(opts: BackendApiOptions);
50
- parseUrl(uri: string, reqOptions: HTTPRequestOptions): string;
51
- parseHeaders(additionalHeaders: {
49
+ export declare abstract class AbstractHTTPClient {
50
+ constructor();
51
+ parseUrl(urlRequested: string, reqOptions: HTTPRequestOptions): string;
52
+ parseHeaders(requestHeaders: {
52
53
  [key: string]: string;
53
- }): {
54
- [x: string]: string;
54
+ }, accessToken?: string): {
55
+ Authorization?: string | undefined;
55
56
  };
56
- setBearerToken(token: string): void;
57
57
  request(method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH', url: string, data?: any, reqOptions?: HTTPRequestOptions): Promise<import("axios").AxiosResponse<any, any>>;
58
58
  get(url: string, reqOptions?: HTTPRequestOptions): Promise<import("axios").AxiosResponse<any, any>>;
59
59
  post(url: string, data: any, reqOptions: HTTPRequestOptions): Promise<import("axios").AxiosResponse<any, any>>;
@@ -1 +1 @@
1
- {"version":3,"file":"nvm-api.d.ts","sourceRoot":"","sources":["../../src/api/nvm-api.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,qBAAqB,2BAA2B,CAAA;AAC7D,eAAO,MAAM,sBAAsB,4BAA4B,CAAA;AAC/D,eAAO,MAAM,iBAAiB,qCAAqC,CAAA;AACnE,eAAO,MAAM,uBAAuB,2CAA2C,CAAA;AAC/E,eAAO,MAAM,gBAAgB,mCAAmC,CAAA;AAChE,eAAO,MAAM,uBAAuB,0CAA0C,CAAA;AAC9E,eAAO,MAAM,kBAAkB,yCAAyC,CAAA;AACxE,eAAO,MAAM,iBAAiB,gCAAgC,CAAA;AAC9D,eAAO,MAAM,2BAA2B,yCAAyC,CAAA;AACjF,eAAO,MAAM,sBAAsB,kDAAkD,CAAA;AACrF,eAAO,MAAM,yBAAyB,kDAAkD,CAAA;AAExF,eAAO,MAAM,iBAAiB,gCAAgC,CAAA;AAC9D,eAAO,MAAM,oBAAoB,0DAA0D,CAAA;AAC3F,eAAO,MAAM,8BAA8B,4CAA4C,CAAA;AACvF,eAAO,MAAM,mCAAmC,6CAA6C,CAAA;AAE7F,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,WAAW,EAAE,MAAM,CAAA;IAEnB;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IAEf;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA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