@virtuals-protocol/acp-node 0.3.0-beta.22 → 0.3.0-beta.24

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/index.mjs CHANGED
@@ -8,7 +8,7 @@ var require_package = __commonJS({
8
8
  "package.json"(exports, module) {
9
9
  module.exports = {
10
10
  name: "@virtuals-protocol/acp-node",
11
- version: "0.3.0-beta.22",
11
+ version: "0.3.0-beta.24",
12
12
  main: "./dist/index.js",
13
13
  module: "./dist/index.mjs",
14
14
  types: "./dist/index.d.ts",
@@ -41,6 +41,8 @@ var require_package = __commonJS({
41
41
  "@account-kit/smart-contracts": "^4.73.0",
42
42
  "@virtuals-protocol/acp-node": "^0.3.0-beta.10",
43
43
  ajv: "^8.17.1",
44
+ axios: "^1.13.2",
45
+ "jwt-decode": "^4.0.0",
44
46
  "socket.io-client": "^4.8.1",
45
47
  tsup: "^8.5.0",
46
48
  viem: "^2.28.2"
@@ -1225,6 +1227,7 @@ var acpAbi_default = ACP_ABI;
1225
1227
  // src/acpClient.ts
1226
1228
  import { zeroAddress as zeroAddress3 } from "viem";
1227
1229
  import { io } from "socket.io-client";
1230
+ import { jwtDecode } from "jwt-decode";
1228
1231
 
1229
1232
  // src/contractClients/baseAcpContractClient.ts
1230
1233
  import {
@@ -3795,23 +3798,6 @@ var AcpOnlineStatus = /* @__PURE__ */ ((AcpOnlineStatus2) => {
3795
3798
  AcpOnlineStatus2["OFFLINE"] = "offline";
3796
3799
  return AcpOnlineStatus2;
3797
3800
  })(AcpOnlineStatus || {});
3798
- var PayloadType = /* @__PURE__ */ ((PayloadType2) => {
3799
- PayloadType2["FUND_RESPONSE"] = "fund_response";
3800
- PayloadType2["OPEN_POSITION"] = "open_position";
3801
- PayloadType2["SWAP_TOKEN"] = "swap_token";
3802
- PayloadType2["RESPONSE_SWAP_TOKEN"] = "response_swap_token";
3803
- PayloadType2["CLOSE_PARTIAL_POSITION"] = "close_partial_position";
3804
- PayloadType2["CLOSE_POSITION"] = "close_position";
3805
- PayloadType2["POSITION_FULFILLED"] = "position_fulfilled";
3806
- PayloadType2["CLOSE_JOB_AND_WITHDRAW"] = "close_job_and_withdraw";
3807
- PayloadType2["UNFULFILLED_POSITION"] = "unfulfilled_position";
3808
- return PayloadType2;
3809
- })(PayloadType || {});
3810
- var PositionDirection = /* @__PURE__ */ ((PositionDirection2) => {
3811
- PositionDirection2["LONG"] = "long";
3812
- PositionDirection2["SHORT"] = "short";
3813
- return PositionDirection2;
3814
- })(PositionDirection || {});
3815
3801
 
3816
3802
  // src/utils.ts
3817
3803
  function tryParseJson(content) {
@@ -3846,6 +3832,11 @@ var AcpJobOffering = class {
3846
3832
  this.ajv = new Ajv({ allErrors: true });
3847
3833
  }
3848
3834
  async initiateJob(serviceRequirement, evaluatorAddress, expiredAt = new Date(Date.now() + 1e3 * 60 * 60 * 24)) {
3835
+ if (this.providerAddress === this.acpClient.walletAddress) {
3836
+ throw new acpError_default(
3837
+ "Provider address cannot be the same as the client address"
3838
+ );
3839
+ }
3849
3840
  if (this.requirement && typeof this.requirement === "object") {
3850
3841
  const validator = this.ajv.compile(this.requirement);
3851
3842
  const valid = validator(serviceRequirement);
@@ -4159,8 +4150,8 @@ var AcpJob = class {
4159
4150
  return await this.acpContractClient.handleOperation(operations);
4160
4151
  }
4161
4152
  async deliver(deliverable) {
4162
- if (this.latestMemo?.nextPhase !== 3 /* EVALUATION */) {
4163
- throw new acpError_default("No transaction memo found");
4153
+ if (this.phase !== 2 /* TRANSACTION */) {
4154
+ throw new acpError_default("Job is not in transaction phase");
4164
4155
  }
4165
4156
  const operations = [];
4166
4157
  operations.push(
@@ -4190,6 +4181,7 @@ var AcpJob = class {
4190
4181
  operations.push(
4191
4182
  this.acpContractClient.createPayableMemo(
4192
4183
  this.id,
4184
+ // memoContent.url,
4193
4185
  preparePayload(deliverable),
4194
4186
  amount.amount,
4195
4187
  this.clientAddress,
@@ -4348,16 +4340,9 @@ var AcpMemo = class {
4348
4340
  this.payableDetails.amount = BigInt(this.payableDetails.amount);
4349
4341
  this.payableDetails.feeAmount = BigInt(this.payableDetails.feeAmount);
4350
4342
  }
4351
- this.structuredContent = tryParseJson(this.content) || void 0;
4352
- }
4353
- get payloadType() {
4354
- return this.structuredContent?.type;
4355
- }
4356
- getStructuredContent() {
4357
- return this.structuredContent;
4358
4343
  }
4359
4344
  async create(jobId, isSecured = true) {
4360
- return await this.contractClient.createMemo(
4345
+ return this.contractClient.createMemo(
4361
4346
  jobId,
4362
4347
  this.content,
4363
4348
  this.type,
@@ -4405,10 +4390,31 @@ var AcpAccount = class {
4405
4390
  }
4406
4391
  };
4407
4392
 
4393
+ // src/acpClient.ts
4394
+ import axios, { AxiosError } from "axios";
4395
+
4396
+ // src/acpAgent.ts
4397
+ var AcpAgent = class {
4398
+ constructor(args) {
4399
+ this.id = String(args.id);
4400
+ this.name = args.name;
4401
+ this.contractAddress = args.contractAddress;
4402
+ this.walletAddress = args.walletAddress;
4403
+ this.jobOfferings = Object.freeze([...args.jobOfferings]);
4404
+ this.description = args.description;
4405
+ this.twitterHandle = args.twitterHandle;
4406
+ this.metrics = args.metrics;
4407
+ this.resources = args.resources;
4408
+ }
4409
+ };
4410
+ var acpAgent_default = AcpAgent;
4411
+
4408
4412
  // src/acpClient.ts
4409
4413
  var { version } = require_package();
4410
4414
  var AcpClient = class {
4411
4415
  constructor(options) {
4416
+ this.accessToken = null;
4417
+ this.accessTokenInflight = null;
4412
4418
  this.contractClients = Array.isArray(options.acpContractClient) ? options.acpContractClient : [options.acpContractClient];
4413
4419
  if (this.contractClients.length === 0) {
4414
4420
  throw new acpError_default("ACP contract client is required");
@@ -4420,10 +4426,86 @@ var AcpClient = class {
4420
4426
  );
4421
4427
  }
4422
4428
  });
4429
+ this.acpClient = axios.create({
4430
+ baseURL: `${this.acpUrl}/api`,
4431
+ headers: {
4432
+ "wallet-address": this.walletAddress
4433
+ }
4434
+ });
4435
+ this.noAuthAcpClient = this.acpClient.create({
4436
+ baseURL: `${this.acpUrl}/api`
4437
+ });
4438
+ this.acpClient.interceptors.request.use(async (config) => {
4439
+ const accessToken = await this.getAccessToken();
4440
+ config.headers["authorization"] = `Bearer ${accessToken}`;
4441
+ return config;
4442
+ });
4423
4443
  this.onNewTask = options.onNewTask;
4424
4444
  this.onEvaluate = options.onEvaluate || this.defaultOnEvaluate;
4425
4445
  this.init(options.skipSocketConnection);
4426
4446
  }
4447
+ async getAccessToken() {
4448
+ if (this.accessTokenInflight) {
4449
+ return await this.accessTokenInflight;
4450
+ }
4451
+ let refreshToken = this.accessToken ? false : true;
4452
+ if (this.accessToken) {
4453
+ const decodedToken = jwtDecode(this.accessToken);
4454
+ if (decodedToken.exp && decodedToken.exp - 60 * 5 < Math.floor(Date.now() / 1e3)) {
4455
+ refreshToken = true;
4456
+ }
4457
+ }
4458
+ if (!refreshToken) {
4459
+ return this.accessToken;
4460
+ }
4461
+ this.accessTokenInflight = (async () => {
4462
+ this.accessToken = await this.refreshToken();
4463
+ return this.accessToken;
4464
+ })().finally(() => {
4465
+ this.accessTokenInflight = null;
4466
+ });
4467
+ return await this.accessTokenInflight;
4468
+ }
4469
+ async refreshToken() {
4470
+ const challenge = await this.getAuthChallenge();
4471
+ const signature = await this.acpContractClient.signTypedData(challenge);
4472
+ const verified = await this.verifyAuthChallenge(
4473
+ challenge.message["walletAddress"],
4474
+ challenge.message["nonce"],
4475
+ challenge.message["expiresAt"],
4476
+ signature
4477
+ );
4478
+ return verified.accessToken;
4479
+ }
4480
+ async getAuthChallenge() {
4481
+ try {
4482
+ const response = await this.noAuthAcpClient.get("/auth/challenge", {
4483
+ params: {
4484
+ walletAddress: this.walletAddress
4485
+ }
4486
+ });
4487
+ return response.data.data;
4488
+ } catch (err) {
4489
+ console.error(
4490
+ "Failed to get auth challenge",
4491
+ err.response?.data
4492
+ );
4493
+ throw new acpError_default("Failed to get auth challenge", err);
4494
+ }
4495
+ }
4496
+ async verifyAuthChallenge(walletAddress, nonce, expiresAt, signature) {
4497
+ try {
4498
+ const response = await this.noAuthAcpClient.post("/auth/verify-typed-signature", {
4499
+ walletAddress,
4500
+ nonce,
4501
+ expiresAt,
4502
+ signature
4503
+ });
4504
+ return response.data.data;
4505
+ } catch (err) {
4506
+ throw new acpError_default("Failed to verify auth challenge", err);
4507
+ }
4508
+ }
4427
4509
  contractClientByAddress(address) {
4428
4510
  if (!address) {
4429
4511
  return this.contractClients[0];
@@ -4452,9 +4534,13 @@ var AcpClient = class {
4452
4534
  if (skipSocketConnection) {
4453
4535
  return;
4454
4536
  }
4537
+ console.log("Initializing socket");
4455
4538
  const socket = io(this.acpUrl, {
4456
- auth: {
4457
- walletAddress: this.walletAddress
4539
+ auth: async (cb) => {
4540
+ cb({
4541
+ walletAddress: this.walletAddress,
4542
+ accessToken: await this.getAccessToken()
4543
+ });
4458
4544
  },
4459
4545
  extraHeaders: {
4460
4546
  "x-sdk-version": version,
@@ -4468,85 +4554,23 @@ var AcpClient = class {
4468
4554
  console.log("Joined ACP Room");
4469
4555
  callback(true);
4470
4556
  });
4471
- socket.on(
4472
- "onEvaluate" /* ON_EVALUATE */,
4473
- async (data, callback) => {
4474
- callback(true);
4475
- if (this.onEvaluate) {
4476
- const job = new acpJob_default(
4477
- this,
4478
- data.id,
4479
- data.clientAddress,
4480
- data.providerAddress,
4481
- data.evaluatorAddress,
4482
- data.price,
4483
- data.priceTokenAddress,
4484
- data.memos.map((memo) => {
4485
- return new acpMemo_default(
4486
- this.contractClientByAddress(data.contractAddress),
4487
- memo.id,
4488
- memo.memoType,
4489
- memo.content,
4490
- memo.nextPhase,
4491
- memo.status,
4492
- memo.senderAddress,
4493
- memo.signedReason,
4494
- memo.expiry ? new Date(parseInt(memo.expiry) * 1e3) : void 0,
4495
- memo.payableDetails,
4496
- memo.txHash,
4497
- memo.signedTxHash
4498
- );
4499
- }),
4500
- data.phase,
4501
- data.context,
4502
- data.contractAddress,
4503
- data.netPayableAmount
4504
- );
4505
- this.onEvaluate(job);
4506
- }
4557
+ socket.on("onEvaluate" /* ON_EVALUATE */, async (data, callback) => {
4558
+ callback(true);
4559
+ if (this.onEvaluate) {
4560
+ const job = this._hydrateJob(data);
4561
+ this.onEvaluate(job);
4507
4562
  }
4508
- );
4509
- socket.on(
4510
- "onNewTask" /* ON_NEW_TASK */,
4511
- async (data, callback) => {
4512
- callback(true);
4513
- if (this.onNewTask) {
4514
- const job = new acpJob_default(
4515
- this,
4516
- data.id,
4517
- data.clientAddress,
4518
- data.providerAddress,
4519
- data.evaluatorAddress,
4520
- data.price,
4521
- data.priceTokenAddress,
4522
- data.memos.map((memo) => {
4523
- return new acpMemo_default(
4524
- this.contractClientByAddress(data.contractAddress),
4525
- memo.id,
4526
- memo.memoType,
4527
- memo.content,
4528
- memo.nextPhase,
4529
- memo.status,
4530
- memo.senderAddress,
4531
- memo.signedReason,
4532
- memo.expiry ? new Date(parseInt(memo.expiry) * 1e3) : void 0,
4533
- memo.payableDetails,
4534
- memo.txHash,
4535
- memo.signedTxHash
4536
- );
4537
- }),
4538
- data.phase,
4539
- data.context,
4540
- data.contractAddress,
4541
- data.netPayableAmount
4542
- );
4543
- this.onNewTask(
4544
- job,
4545
- job.memos.find((m) => m.id == data.memoToSign)
4546
- );
4547
- }
4563
+ });
4564
+ socket.on("onNewTask" /* ON_NEW_TASK */, async (data, callback) => {
4565
+ callback(true);
4566
+ if (this.onNewTask) {
4567
+ const job = this._hydrateJob(data);
4568
+ this.onNewTask(
4569
+ job,
4570
+ job.memos.find((m) => m.id == data.memoToSign)
4571
+ );
4548
4572
  }
4549
- );
4573
+ });
4550
4574
  const cleanup = async () => {
4551
4575
  if (socket) {
4552
4576
  socket.disconnect();
@@ -4556,70 +4580,156 @@ var AcpClient = class {
4556
4580
  process.on("SIGINT", cleanup);
4557
4581
  process.on("SIGTERM", cleanup);
4558
4582
  }
4559
- async browseAgents(keyword, options) {
4560
- let { cluster, sort_by, top_k, graduationStatus, onlineStatus, showHiddenOfferings } = options;
4561
- top_k = top_k ?? 5;
4562
- let url = `${this.acpUrl}/api/agents/v4/search?search=${keyword}`;
4563
- if (sort_by && sort_by.length > 0) {
4564
- url += `&sortBy=${sort_by.map((s) => s).join(",")}`;
4583
+ async _fetch(url, method = "GET", params, data, errCallback) {
4584
+ try {
4585
+ const response = await this.acpClient.request({
4586
+ url,
4587
+ method,
4588
+ params,
4589
+ data
4590
+ });
4591
+ return response.data.data;
4592
+ } catch (err) {
4593
+ if (err instanceof AxiosError) {
4594
+ if (errCallback) {
4595
+ errCallback(err);
4596
+ } else if (err.response?.data.error?.message) {
4597
+ throw new acpError_default(err.response?.data.error.message);
4598
+ }
4599
+ } else {
4600
+ throw new acpError_default(
4601
+ `Failed to fetch ACP Endpoint: ${url} (network error)`,
4602
+ err
4603
+ );
4604
+ }
4605
+ }
4606
+ }
4607
+ _hydrateMemo(memo, contractClient) {
4608
+ try {
4609
+ return new acpMemo_default(
4610
+ contractClient,
4611
+ memo.id,
4612
+ memo.memoType,
4613
+ memo.content,
4614
+ memo.nextPhase,
4615
+ memo.status,
4616
+ memo.senderAddress,
4617
+ memo.signedReason,
4618
+ memo.expiry ? new Date(parseInt(memo.expiry) * 1e3) : void 0,
4619
+ memo.payableDetails,
4620
+ memo.txHash,
4621
+ memo.signedTxHash
4622
+ );
4623
+ } catch (err) {
4624
+ throw new acpError_default(`Failed to hydrate memo ${memo.id}`, err);
4625
+ }
4626
+ }
4627
+ _hydrateJob(job) {
4628
+ try {
4629
+ return new acpJob_default(
4630
+ this,
4631
+ job.id,
4632
+ job.clientAddress,
4633
+ job.providerAddress,
4634
+ job.evaluatorAddress,
4635
+ job.price,
4636
+ job.priceTokenAddress,
4637
+ job.memos.map(
4638
+ (memo) => this._hydrateMemo(
4639
+ memo,
4640
+ this.contractClientByAddress(job.contractAddress)
4641
+ )
4642
+ ),
4643
+ job.phase,
4644
+ job.context,
4645
+ job.contractAddress,
4646
+ job.netPayableAmount
4647
+ );
4648
+ } catch (err) {
4649
+ throw new acpError_default(`Failed to hydrate job ${job.id}`, err);
4565
4650
  }
4566
- if (top_k) {
4567
- url += `&top_k=${top_k}`;
4651
+ }
4652
+ _hydrateJobs(rawJobs, options) {
4653
+ const jobs = rawJobs.map((job) => {
4654
+ try {
4655
+ return this._hydrateJob(job);
4656
+ } catch (err) {
4657
+ console.warn(`${options?.logPrefix ?? "Skipped"}`, err);
4658
+ return null;
4659
+ }
4660
+ });
4661
+ return jobs.filter((job) => !!job);
4662
+ }
4663
+ _hydrateAgent(agent) {
4664
+ const acpContractClient = this.contractClients.find(
4665
+ (client) => client.contractAddress.toLowerCase() === agent.contractAddress.toLowerCase()
4666
+ );
4667
+ if (!acpContractClient) {
4668
+ throw new acpError_default("ACP contract client not found");
4568
4669
  }
4569
- if (this.walletAddress) {
4570
- url += `&walletAddressesToExclude=${this.walletAddress}`;
4670
+ return new acpAgent_default({
4671
+ id: agent.id,
4672
+ name: agent.name,
4673
+ description: agent.description,
4674
+ jobOfferings: agent.jobs.map((jobs) => {
4675
+ return new acpJobOffering_default(
4676
+ this,
4677
+ acpContractClient,
4678
+ agent.walletAddress,
4679
+ jobs.name,
4680
+ jobs.priceV2.value,
4681
+ jobs.priceV2.type,
4682
+ jobs.requirement
4683
+ );
4684
+ }),
4685
+ contractAddress: agent.contractAddress,
4686
+ twitterHandle: agent.twitterHandle,
4687
+ walletAddress: agent.walletAddress,
4688
+ metrics: agent.metrics,
4689
+ resources: agent.resources
4690
+ });
4691
+ }
4692
+ async browseAgents(keyword, options = {}) {
4693
+ const {
4694
+ cluster,
4695
+ sortBy,
4696
+ topK = 5,
4697
+ graduationStatus,
4698
+ onlineStatus,
4699
+ showHiddenOfferings
4700
+ } = options;
4701
+ const params = {
4702
+ search: keyword
4703
+ };
4704
+ params.top_k = topK;
4705
+ params.walletAddressesToExclude = this.walletAddress;
4706
+ if (sortBy && sortBy.length > 0) {
4707
+ params.sortBy = sortBy.join(",");
4571
4708
  }
4572
4709
  if (cluster) {
4573
- url += `&cluster=${cluster}`;
4710
+ params.cluster = cluster;
4574
4711
  }
4575
4712
  if (graduationStatus) {
4576
- url += `&graduationStatus=${graduationStatus}`;
4713
+ params.graduationStatus = graduationStatus;
4577
4714
  }
4578
4715
  if (onlineStatus) {
4579
- url += `&onlineStatus=${onlineStatus}`;
4716
+ params.onlineStatus = onlineStatus;
4580
4717
  }
4581
4718
  if (showHiddenOfferings) {
4582
- url += `&showHiddenOfferings=${showHiddenOfferings}`;
4719
+ params.showHiddenOfferings = true;
4583
4720
  }
4584
- const response = await fetch(url);
4585
- const data = await response.json();
4721
+ const agents = await this._fetch("/agents/v4/search", "GET", params) || [];
4586
4722
  const availableContractClientAddresses = this.contractClients.map(
4587
4723
  (client) => client.contractAddress.toLowerCase()
4588
4724
  );
4589
- return data.data.filter(
4725
+ return agents.filter(
4590
4726
  (agent) => agent.walletAddress.toLowerCase() !== this.walletAddress.toLowerCase()
4591
4727
  ).filter(
4592
4728
  (agent) => availableContractClientAddresses.includes(
4593
4729
  agent.contractAddress.toLowerCase()
4594
4730
  )
4595
4731
  ).map((agent) => {
4596
- const acpContractClient = this.contractClients.find(
4597
- (client) => client.contractAddress.toLowerCase() === agent.contractAddress.toLowerCase()
4598
- );
4599
- if (!acpContractClient) {
4600
- throw new acpError_default("ACP contract client not found");
4601
- }
4602
- return {
4603
- id: agent.id,
4604
- name: agent.name,
4605
- description: agent.description,
4606
- jobOfferings: agent.jobs.map((jobs) => {
4607
- return new acpJobOffering_default(
4608
- this,
4609
- acpContractClient,
4610
- agent.walletAddress,
4611
- jobs.name,
4612
- jobs.priceV2.value,
4613
- jobs.priceV2.type,
4614
- jobs.requirement
4615
- );
4616
- }),
4617
- contractAddress: agent.contractAddress,
4618
- twitterHandle: agent.twitterHandle,
4619
- walletAddress: agent.walletAddress,
4620
- metrics: agent.metrics,
4621
- resources: agent.resources
4622
- };
4732
+ return this._hydrateAgent(agent);
4623
4733
  });
4624
4734
  }
4625
4735
  async initiateJob(providerAddress, serviceRequirement, fareAmount, evaluatorAddress, expiredAt = new Date(Date.now() + 1e3 * 60 * 60 * 24)) {
@@ -4689,254 +4799,136 @@ var AcpClient = class {
4689
4799
  return jobId;
4690
4800
  }
4691
4801
  async getActiveJobs(page = 1, pageSize = 10) {
4692
- const url = `${this.acpUrl}/api/jobs/active?pagination[page]=${page}&pagination[pageSize]=${pageSize}`;
4693
- const rawJobs = await this._fetchJobList(url);
4694
- return this._hydrateJobs(rawJobs, { logPrefix: "Active jobs" });
4802
+ const rawJobs = await this._fetch("/jobs/active", "GET", {
4803
+ pagination: {
4804
+ page,
4805
+ pageSize
4806
+ }
4807
+ });
4808
+ return this._hydrateJobs(rawJobs ?? [], { logPrefix: "Active jobs" });
4695
4809
  }
4696
4810
  async getPendingMemoJobs(page = 1, pageSize = 10) {
4697
- const url = `${this.acpUrl}/api/jobs/pending-memos?pagination[page]=${page}&pagination[pageSize]=${pageSize}`;
4698
- const rawJobs = await this._fetchJobList(url);
4699
- return this._hydrateJobs(rawJobs, { logPrefix: "Pending memo jobs" });
4811
+ const rawJobs = await this._fetch("/jobs/pending-memos", "GET", {
4812
+ pagination: {
4813
+ page,
4814
+ pageSize
4815
+ }
4816
+ });
4817
+ return this._hydrateJobs(rawJobs ?? [], { logPrefix: "Pending memo jobs" });
4700
4818
  }
4701
4819
  async getCompletedJobs(page = 1, pageSize = 10) {
4702
- const url = `${this.acpUrl}/api/jobs/completed?pagination[page]=${page}&pagination[pageSize]=${pageSize}`;
4703
- const rawJobs = await this._fetchJobList(url);
4704
- return this._hydrateJobs(rawJobs, { logPrefix: "Completed jobs" });
4820
+ const rawJobs = await this._fetch("/jobs/completed", "GET", {
4821
+ pagination: {
4822
+ page,
4823
+ pageSize
4824
+ }
4825
+ });
4826
+ return this._hydrateJobs(rawJobs ?? [], { logPrefix: "Completed jobs" });
4705
4827
  }
4706
4828
  async getCancelledJobs(page = 1, pageSize = 10) {
4707
- const url = `${this.acpUrl}/api/jobs/cancelled?pagination[page]=${page}&pagination[pageSize]=${pageSize}`;
4708
- const rawJobs = await this._fetchJobList(url);
4709
- return this._hydrateJobs(rawJobs, { logPrefix: "Cancelled jobs" });
4710
- }
4711
- async _fetchJobList(url) {
4712
- let response;
4713
- try {
4714
- response = await fetch(url, {
4715
- headers: {
4716
- "wallet-address": this.walletAddress
4717
- }
4718
- });
4719
- } catch (err) {
4720
- throw new acpError_default("Failed to fetch ACP jobs (network error)", err);
4721
- }
4722
- let data;
4723
- try {
4724
- data = await response.json();
4725
- } catch (err) {
4726
- throw new acpError_default("Failed to parse ACP jobs response", err);
4727
- }
4728
- if (data.error) {
4729
- throw new acpError_default(data.error.message);
4730
- }
4731
- return data.data;
4732
- }
4733
- _hydrateJobs(rawJobs, options) {
4734
- const jobs = [];
4735
- const errors = [];
4736
- for (const job of rawJobs) {
4737
- try {
4738
- const memos = job.memos.map(
4739
- (memo) => new acpMemo_default(
4740
- this.contractClientByAddress(job.contractAddress),
4741
- memo.id,
4742
- memo.memoType,
4743
- memo.content,
4744
- memo.nextPhase,
4745
- memo.status,
4746
- memo.senderAddress,
4747
- memo.signedReason,
4748
- memo.expiry ? new Date(parseInt(memo.expiry) * 1e3) : void 0,
4749
- memo.payableDetails,
4750
- memo.txHash,
4751
- memo.signedTxHash
4752
- )
4753
- );
4754
- jobs.push(
4755
- new acpJob_default(
4756
- this,
4757
- job.id,
4758
- job.clientAddress,
4759
- job.providerAddress,
4760
- job.evaluatorAddress,
4761
- job.price,
4762
- job.priceTokenAddress,
4763
- memos,
4764
- job.phase,
4765
- job.context,
4766
- job.contractAddress,
4767
- job.netPayableAmount
4768
- )
4769
- );
4770
- } catch (err) {
4771
- errors.push({ jobId: job.id, error: err });
4829
+ const rawJobs = await this._fetch("/jobs/cancelled", "GET", {
4830
+ pagination: {
4831
+ page,
4832
+ pageSize
4772
4833
  }
4773
- }
4774
- if (errors.length > 0) {
4775
- console.warn(
4776
- `${options?.logPrefix ?? "Skipped"} ${errors.length} malformed job(s)
4777
- ` + JSON.stringify(
4778
- errors.map((e) => ({ jobId: e.jobId, message: e.error.message })),
4779
- null,
4780
- 2
4781
- )
4782
- );
4783
- }
4784
- return jobs;
4834
+ });
4835
+ return this._hydrateJobs(rawJobs ?? [], { logPrefix: "Cancelled jobs" });
4785
4836
  }
4786
4837
  async getJobById(jobId) {
4787
- const url = `${this.acpUrl}/api/jobs/${jobId}`;
4788
- let response;
4789
- try {
4790
- response = await fetch(url, {
4791
- headers: {
4792
- "wallet-address": this.acpContractClient.walletAddress
4793
- }
4794
- });
4795
- } catch (err) {
4796
- throw new acpError_default("Failed to fetch job by id (network error)", err);
4797
- }
4798
- let data;
4799
- try {
4800
- data = await response.json();
4801
- } catch (err) {
4802
- throw new acpError_default("Failed to parse job by id response", err);
4803
- }
4804
- if (data.error) {
4805
- throw new acpError_default(data.error.message);
4806
- }
4807
- const job = data.data;
4838
+ const job = await this._fetch(`/jobs/${jobId}`);
4808
4839
  if (!job) {
4809
- return void 0;
4810
- }
4811
- try {
4812
- const memos = job.memos.map(
4813
- (memo) => new acpMemo_default(
4814
- this.contractClientByAddress(job.contractAddress),
4815
- memo.id,
4816
- memo.memoType,
4817
- memo.content,
4818
- memo.nextPhase,
4819
- memo.status,
4820
- memo.senderAddress,
4821
- memo.signedReason,
4822
- memo.expiry ? new Date(parseInt(memo.expiry) * 1e3) : void 0,
4823
- memo.payableDetails,
4824
- memo.txHash,
4825
- memo.signedTxHash
4826
- )
4827
- );
4828
- return new acpJob_default(
4829
- this,
4830
- job.id,
4831
- job.clientAddress,
4832
- job.providerAddress,
4833
- job.evaluatorAddress,
4834
- job.price,
4835
- job.priceTokenAddress,
4836
- memos,
4837
- job.phase,
4838
- job.context,
4839
- job.contractAddress,
4840
- job.netPayableAmount
4841
- );
4842
- } catch (err) {
4843
- throw new acpError_default(`Failed to hydrate job ${jobId}`, err);
4840
+ return null;
4844
4841
  }
4842
+ return this._hydrateJob(job);
4845
4843
  }
4846
4844
  async getMemoById(jobId, memoId) {
4847
- const url = `${this.acpUrl}/api/jobs/${jobId}/memos/${memoId}`;
4848
- let response;
4849
- try {
4850
- response = await fetch(url, {
4851
- headers: {
4852
- "wallet-address": this.walletAddress
4853
- }
4854
- });
4855
- } catch (err) {
4856
- throw new acpError_default("Failed to fetch memo by id (network error)", err);
4857
- }
4858
- let data;
4859
- try {
4860
- data = await response.json();
4861
- } catch (err) {
4862
- throw new acpError_default("Failed to parse memo by id response", err);
4863
- }
4864
- if (data.error) {
4865
- throw new acpError_default(data.error.message);
4866
- }
4867
- const memo = data.data;
4845
+ const memo = await this._fetch(
4846
+ `/jobs/${jobId}/memos/${memoId}`
4847
+ );
4868
4848
  if (!memo) {
4869
- return void 0;
4870
- }
4871
- try {
4872
- return new acpMemo_default(
4873
- this.contractClientByAddress(memo.contractAddress),
4874
- memo.id,
4875
- memo.memoType,
4876
- memo.content,
4877
- memo.nextPhase,
4878
- memo.status,
4879
- memo.senderAddress,
4880
- memo.signedReason,
4881
- memo.expiry ? new Date(parseInt(memo.expiry) * 1e3) : void 0,
4882
- memo.payableDetails,
4883
- memo.txHash,
4884
- memo.signedTxHash
4885
- );
4886
- } catch (err) {
4887
- throw new acpError_default(
4888
- `Failed to hydrate memo ${memoId} for job ${jobId}`,
4889
- err
4890
- );
4849
+ return null;
4891
4850
  }
4851
+ return this._hydrateMemo(
4852
+ memo,
4853
+ this.contractClientByAddress(memo.contractAddress)
4854
+ );
4892
4855
  }
4893
- async getAgent(walletAddress) {
4894
- const url = `${this.acpUrl}/api/agents?filters[walletAddress]=${walletAddress}`;
4895
- const response = await fetch(url);
4896
- const data = await response.json();
4897
- const agents = data.data || [];
4898
- if (agents.length === 0) {
4899
- return;
4856
+ async getAgent(walletAddress, options = {}) {
4857
+ const params = {
4858
+ "filters[walletAddress]": walletAddress
4859
+ };
4860
+ const { showHiddenOfferings } = options;
4861
+ if (showHiddenOfferings) {
4862
+ params.showHiddenOfferings = true;
4863
+ }
4864
+ const agents = await this._fetch("/agents", "GET", params) || [];
4865
+ if (!agents) {
4866
+ return null;
4900
4867
  }
4901
- return agents[0];
4868
+ const agent = agents[0];
4869
+ return this._hydrateAgent(agent);
4902
4870
  }
4903
4871
  async getAccountByJobId(jobId, acpContractClient) {
4904
- try {
4905
- const url = `${this.acpUrl}/api/accounts/job/${jobId}`;
4906
- const response = await fetch(url);
4907
- const data = await response.json();
4908
- if (!data.data) {
4909
- return null;
4910
- }
4911
- return new AcpAccount(
4912
- acpContractClient || this.contractClients[0],
4913
- data.data.id,
4914
- data.data.clientAddress,
4915
- data.data.providerAddress,
4916
- data.data.metadata
4917
- );
4918
- } catch (error) {
4919
- throw new acpError_default("Failed to get account by job id", error);
4872
+ const account = await this._fetch(`/accounts/job/${jobId}`);
4873
+ if (!account) {
4874
+ return null;
4920
4875
  }
4876
+ return new AcpAccount(
4877
+ acpContractClient || this.contractClients[0],
4878
+ account.id,
4879
+ account.clientAddress,
4880
+ account.providerAddress,
4881
+ account.metadata
4882
+ );
4921
4883
  }
4922
4884
  async getByClientAndProvider(clientAddress, providerAddress, acpContractClient) {
4923
- try {
4924
- const url = `${this.acpUrl}/api/accounts/client/${clientAddress}/provider/${providerAddress}`;
4925
- const response = await fetch(url);
4926
- const data = await response.json();
4927
- if (!data.data) {
4928
- return null;
4885
+ const response = await this._fetch(
4886
+ `/accounts/client/${clientAddress}/provider/${providerAddress}`,
4887
+ "GET",
4888
+ {},
4889
+ {},
4890
+ (err) => {
4891
+ if (err.response?.status === 404) {
4892
+ console.warn("Account not found by client and provider");
4893
+ return;
4894
+ }
4895
+ throw new acpError_default("Failed to get account by client and provider", err);
4929
4896
  }
4930
- return new AcpAccount(
4931
- acpContractClient || this.contractClients[0],
4932
- data.data.id,
4933
- data.data.clientAddress,
4934
- data.data.providerAddress,
4935
- data.data.metadata
4936
- );
4937
- } catch (error) {
4938
- throw new acpError_default("Failed to get account by client and provider", error);
4897
+ );
4898
+ if (!response) {
4899
+ return null;
4900
+ }
4901
+ return new AcpAccount(
4902
+ acpContractClient || this.contractClients[0],
4903
+ response.id,
4904
+ response.clientAddress,
4905
+ response.providerAddress,
4906
+ response.metadata
4907
+ );
4908
+ }
4909
+ async createMemoContent(jobId, content) {
4910
+ const response = await this._fetch(
4911
+ `/memo-contents`,
4912
+ "POST",
4913
+ {},
4914
+ {
4915
+ data: {
4916
+ onChainJobId: jobId,
4917
+ content
4918
+ }
4919
+ }
4920
+ );
4921
+ if (!response) {
4922
+ throw new acpError_default("Failed to create memo content");
4939
4923
  }
4924
+ return response;
4925
+ }
4926
+ async getTokenBalances() {
4927
+ const response = await this._fetch(
4928
+ `/chains/token-balances`,
4929
+ "GET"
4930
+ );
4931
+ return response;
4940
4932
  }
4941
4933
  };
4942
4934
  var acpClient_default = AcpClient;
@@ -4947,7 +4939,10 @@ import { alchemy } from "@account-kit/infra";
4947
4939
  import {
4948
4940
  createModularAccountV2Client
4949
4941
  } from "@account-kit/smart-contracts";
4950
- import { decodeEventLog, encodeFunctionData as encodeFunctionData3 } from "viem";
4942
+ import {
4943
+ decodeEventLog,
4944
+ encodeFunctionData as encodeFunctionData3
4945
+ } from "viem";
4951
4946
 
4952
4947
  // src/acpX402.ts
4953
4948
  import {
@@ -5322,6 +5317,9 @@ var AcpContractClient = class _AcpContractClient extends baseAcpContractClient_d
5322
5317
  getAcpVersion() {
5323
5318
  return "1";
5324
5319
  }
5320
+ signTypedData(typedData) {
5321
+ return this.sessionKeyClient.signTypedData({ typedData });
5322
+ }
5325
5323
  };
5326
5324
  var acpContractClient_default = AcpContractClient;
5327
5325
 
@@ -5331,7 +5329,11 @@ import { alchemy as alchemy2 } from "@account-kit/infra";
5331
5329
  import {
5332
5330
  createModularAccountV2Client as createModularAccountV2Client2
5333
5331
  } from "@account-kit/smart-contracts";
5334
- import { createPublicClient as createPublicClient4, decodeEventLog as decodeEventLog2, http as http3 } from "viem";
5332
+ import {
5333
+ createPublicClient as createPublicClient4,
5334
+ decodeEventLog as decodeEventLog2,
5335
+ http as http3
5336
+ } from "viem";
5335
5337
 
5336
5338
  // src/abis/jobManagerAbi.ts
5337
5339
  var JOB_MANAGER_ABI = [
@@ -6249,6 +6251,9 @@ var AcpContractClientV2 = class _AcpContractClientV2 extends baseAcpContractClie
6249
6251
  getAcpVersion() {
6250
6252
  return "2";
6251
6253
  }
6254
+ async signTypedData(typedData) {
6255
+ return await this.sessionKeyClient.signTypedData({ typedData });
6256
+ }
6252
6257
  };
6253
6258
  var acpContractClientV2_default = AcpContractClientV2;
6254
6259
 
@@ -6256,6 +6261,7 @@ var acpContractClientV2_default = AcpContractClientV2;
6256
6261
  var index_default = acpClient_default;
6257
6262
  export {
6258
6263
  acpAbi_default as ACP_ABI,
6264
+ acpAgent_default as AcpAgent,
6259
6265
  AcpAgentSort,
6260
6266
  acpContractClient_default as AcpContractClient,
6261
6267
  acpContractClientV2_default as AcpContractClientV2,
@@ -6272,8 +6278,6 @@ export {
6272
6278
  FareAmount,
6273
6279
  FareBigInt,
6274
6280
  MemoType,
6275
- PayloadType,
6276
- PositionDirection,
6277
6281
  baseAcpConfig,
6278
6282
  baseAcpConfigV2,
6279
6283
  baseAcpX402Config,