Skip to content

Commit 2502c35

Browse files
committed
feat: merged with main as of 21 may 2025
2 parents 57cd750 + ee89655 commit 2502c35

10 files changed

Lines changed: 240 additions & 75 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ const completedJobs = await acpClient.getCompletedJobs(page, pageSize);
121121
const cancelledJobs = await acpClient.getCancelledJobs(page, pageSize);
122122

123123
// Get specific job
124-
const job = await acpClient.getJobByOnChainJobId(onChainJobId);
124+
const job = await acpClient.getJobById(jobId);
125125

126126
// Get memo by ID
127-
const memo = await acpClient.getMemoById(onChainJobId, memoId);
127+
const memo = await acpClient.getMemoById(jobId, memoId);
128128
```
129129

130130
### Agent Discovery

examples/acp_base/external_evaluation/buyer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ async function buyer() {
3434
},
3535
});
3636

37+
const relevantAgents = await acpClient.browseAgent("Meme generator");
38+
console.log("Relevant seller agents: ", relevantAgents);
39+
3740
const jobId = await acpClient.initiateJob(
3841
SELLER_WALLET_ADDRESS,
3942
"Meme generator",

examples/acp_base/self_evaluation/buyer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ async function buyer() {
3838
},
3939
});
4040

41+
const relevantAgents = await acpClient.browseAgent("Meme generator");
42+
console.log("Relevant seller agents: ", relevantAgents);
43+
4144
const jobId = await acpClient.initiateJob(
4245
SELLER_WALLET_ADDRESS,
4346
"Meme generator",

interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { JSONSchemaType } from "ajv";
21
import { Address } from "viem";
32

43
export type AcpAgent = {
@@ -18,6 +17,7 @@ export type AcpAgent = {
1817
name: string;
1918
price: number;
2019
requirementSchema?: Object;
20+
deliverableSchema?: Object;
2121
}[];
2222
symbol: string | null;
2323
virtualAgentId: string | null;

src/acpClient.ts

Lines changed: 136 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -10,62 +10,18 @@ import AcpJob from "./acpJob";
1010
import AcpMemo from "./acpMemo";
1111
import AcpJobOffering from "./acpJobOffering";
1212
import AcpMessage from "./acpMessage";
13+
import {
14+
IAcpClientOptions,
15+
IAcpJob,
16+
IAcpJobResponse,
17+
IAcpMemo,
18+
} from "./interfaces";
1319

1420
export interface IDeliverable {
1521
type: string;
1622
value: string;
1723
}
1824

19-
interface IAcpMemoData {
20-
onChainJobId?: number;
21-
type: string;
22-
content: string;
23-
createdAt: string;
24-
memoId: number;
25-
memoType: MemoType;
26-
nextPhase: AcpJobPhases;
27-
}
28-
interface IAcpMemo {
29-
data: IAcpMemoData;
30-
error?: Error;
31-
}
32-
33-
interface IAcpJob {
34-
data: {
35-
onChainJobId: number;
36-
phase: AcpJobPhases;
37-
negoStatus: AcpNegoStatus;
38-
description: string;
39-
buyerAddress: `0x${string}`;
40-
sellerAddress: `0x${string}`;
41-
evaluatorAddress: `0x${string}`;
42-
price: number;
43-
deliverable: IDeliverable | null;
44-
memos: IAcpMemoData[];
45-
createdAt: string;
46-
};
47-
error?: Error;
48-
}
49-
interface IAcpJobResponse {
50-
data: IAcpJob["data"][];
51-
meta?: {
52-
pagination: {
53-
page: number;
54-
pageSize: number;
55-
pageCount: number;
56-
total: number;
57-
};
58-
};
59-
error?: Error;
60-
}
61-
62-
interface IAcpClientOptions {
63-
acpContractClient: AcpContractClient;
64-
onNewTask?: (job: AcpJob) => void;
65-
onEvaluate?: (job: AcpJob) => void;
66-
onNewMsg?: (msg: AcpMessage, job: AcpJob) => void;
67-
}
68-
6925
export enum SocketEvents {
7026
ROOM_JOINED = "roomJoined",
7127
ON_EVALUATE = "onEvaluate",
@@ -128,12 +84,14 @@ class AcpClient {
12884
if (this.onEvaluate) {
12985
const job = new AcpJob(
13086
this,
131-
data.onChainJobId,
132-
data.sellerAddress,
87+
data.id,
88+
data.clientAddress,
89+
data.providerAddress,
90+
data.evaluatorAddress,
13391
data.memos.map((memo) => {
13492
return new AcpMemo(
13593
this,
136-
memo.memoId,
94+
memo.id,
13795
memo.memoType,
13896
memo.content,
13997
memo.nextPhase
@@ -158,12 +116,14 @@ class AcpClient {
158116
if (this.onNewTask) {
159117
const job = new AcpJob(
160118
this,
161-
data.onChainJobId,
162-
data.sellerAddress,
119+
data.id,
120+
data.clientAddress,
121+
data.providerAddress,
122+
data.evaluatorAddress,
163123
data.memos.map((memo) => {
164124
return new AcpMemo(
165125
this,
166-
memo.memoId,
126+
memo.id,
167127
memo.memoType,
168128
memo.content,
169129
memo.nextPhase
@@ -327,7 +287,27 @@ class AcpClient {
327287
if (data.error) {
328288
throw new Error(data.error.message);
329289
}
330-
return data;
290+
291+
return data.data.map((job) => {
292+
return new AcpJob(
293+
this,
294+
job.id,
295+
job.clientAddress,
296+
job.providerAddress,
297+
job.evaluatorAddress,
298+
job.memos.map((memo) => {
299+
return new AcpMemo(
300+
this,
301+
memo.id,
302+
memo.memoType,
303+
memo.content,
304+
memo.nextPhase
305+
);
306+
}),
307+
job.phase,
308+
job.negoStatus
309+
);
310+
});
331311
} catch (error) {
332312
throw error;
333313
}
@@ -348,7 +328,27 @@ class AcpClient {
348328
if (data.error) {
349329
throw new Error(data.error.message);
350330
}
351-
return data;
331+
332+
return data.data.map((job) => {
333+
return new AcpJob(
334+
this,
335+
job.id,
336+
job.clientAddress,
337+
job.providerAddress,
338+
job.evaluatorAddress,
339+
job.memos.map((memo) => {
340+
return new AcpMemo(
341+
this,
342+
memo.id,
343+
memo.memoType,
344+
memo.content,
345+
memo.nextPhase
346+
);
347+
}),
348+
job.phase,
349+
job.negoStatus
350+
);
351+
});
352352
} catch (error) {
353353
throw error;
354354
}
@@ -369,14 +369,33 @@ class AcpClient {
369369
if (data.error) {
370370
throw new Error(data.error.message);
371371
}
372-
return data;
372+
return data.data.map((job) => {
373+
return new AcpJob(
374+
this,
375+
job.id,
376+
job.clientAddress,
377+
job.providerAddress,
378+
job.evaluatorAddress,
379+
job.memos.map((memo) => {
380+
return new AcpMemo(
381+
this,
382+
memo.id,
383+
memo.memoType,
384+
memo.content,
385+
memo.nextPhase
386+
);
387+
}),
388+
job.phase,
389+
job.negoStatus
390+
);
391+
});
373392
} catch (error) {
374393
throw error;
375394
}
376395
}
377396

378-
async getJobByOnChainJobId(onChainJobId: number) {
379-
let url = `${this.acpUrl}/api/jobs/${onChainJobId}`;
397+
async getJobById(jobId: number) {
398+
let url = `${this.acpUrl}/api/jobs/${jobId}`;
380399

381400
try {
382401
const response = await fetch(url, {
@@ -390,14 +409,37 @@ class AcpClient {
390409
if (data.error) {
391410
throw new Error(data.error.message);
392411
}
393-
return data;
412+
413+
const job = data.data;
414+
if (!job) {
415+
return;
416+
}
417+
418+
return new AcpJob(
419+
this,
420+
job.id,
421+
job.clientAddress,
422+
job.providerAddress,
423+
job.evaluatorAddress,
424+
job.memos.map((memo) => {
425+
return new AcpMemo(
426+
this,
427+
memo.id,
428+
memo.memoType,
429+
memo.content,
430+
memo.nextPhase
431+
);
432+
}),
433+
job.phase,
434+
job.negoStatus
435+
);
394436
} catch (error) {
395437
throw error;
396438
}
397439
}
398440

399-
async getMemoById(onChainJobId: number, memoId: number) {
400-
let url = `${this.acpUrl}/api/jobs/${onChainJobId}/memos/${memoId}`;
441+
async getMemoById(jobId: number, memoId: number) {
442+
let url = `${this.acpUrl}/api/jobs/${jobId}/memos/${memoId}`;
401443

402444
try {
403445
const response = await fetch(url, {
@@ -411,11 +453,40 @@ class AcpClient {
411453
if (data.error) {
412454
throw new Error(data.error.message);
413455
}
414-
return data;
456+
457+
const memo = data.data;
458+
if (!memo) {
459+
return;
460+
}
461+
462+
return new AcpMemo(
463+
this,
464+
memo.id,
465+
memo.memoType,
466+
memo.content,
467+
memo.nextPhase
468+
);
415469
} catch (error) {
416470
throw error;
417471
}
418472
}
473+
474+
async getAgent(walletAddress: Address) {
475+
const url = `${this.acpUrl}/api/agents?filters[walletAddress]=${walletAddress}`;
476+
477+
const response = await fetch(url);
478+
const data: {
479+
data: AcpAgent[];
480+
} = await response.json();
481+
482+
const agents = data.data || [];
483+
484+
if (agents.length === 0) {
485+
return;
486+
}
487+
488+
return agents[0];
489+
}
419490
}
420491

421492
export default AcpClient;

src/acpJob.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Address } from "viem";
12
import AcpClient from "./acpClient";
23
import { AcpJobPhases, AcpNegoStatus } from "./acpContractClient";
34
import AcpMemo from "./acpMemo";
@@ -6,12 +7,36 @@ class AcpJob {
67
constructor(
78
private acpClient: AcpClient,
89
public id: number,
9-
public providerAddress: string,
10+
public clientAddress: Address,
11+
public providerAddress: Address,
12+
public evaluatorAddress: Address,
1013
public memos: AcpMemo[],
1114
public phase: AcpJobPhases,
12-
public negoStatus: AcpNegoStatus
15+
public negoStatus?: AcpNegoStatus
1316
) {}
1417

18+
public get serviceRequirement() {
19+
return this.memos.find((m) => m.nextPhase === AcpJobPhases.NEGOTIATION)
20+
?.content;
21+
}
22+
23+
public get deliverable() {
24+
return this.memos.find((m) => m.nextPhase === AcpJobPhases.COMPLETED)
25+
?.content;
26+
}
27+
28+
public get providerAgent() {
29+
return this.acpClient.getAgent(this.providerAddress);
30+
}
31+
32+
public get clientAgent() {
33+
return this.acpClient.getAgent(this.clientAddress);
34+
}
35+
36+
public get evaluatorAgent() {
37+
return this.acpClient.getAgent(this.evaluatorAddress);
38+
}
39+
1540
async pay(amount: number, reason?: string) {
1641
const memo = this.memos.find(
1742
(m) => m.nextPhase === AcpJobPhases.TRANSACTION

src/acpJobOffering.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class AcpJobOffering {
1717

1818
async initiateJob(
1919
serviceRequirement: Object | string,
20+
evaluatorAddress?: Address,
2021
expiredAt: Date = new Date(Date.now() + 1000 * 60 * 60 * 24) // default: 1 day
2122
) {
2223
if (this.requirementSchema) {
@@ -31,7 +32,8 @@ class AcpJobOffering {
3132
return await this.acpClient.initiateJob(
3233
this.providerAddress,
3334
serviceRequirement,
34-
expiredAt
35+
expiredAt,
36+
evaluatorAddress
3537
);
3638
}
3739
}

0 commit comments

Comments
 (0)