Skip to content

Commit dc9370f

Browse files
committed
docs: update polling mode examples
1 parent feafbe2 commit dc9370f

3 files changed

Lines changed: 63 additions & 77 deletions

File tree

examples/acp-base/polling-mode/buyer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import AcpClient, {
44
AcpGraduationStatus,
55
AcpOnlineStatus,
66
AcpAgentSort,
7+
AcpError,
78
baseAcpX402ConfigV2,
89
} from "@virtuals-protocol/acp-node";
910
import {

examples/acp-base/polling-mode/evaluator.ts

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import AcpClient, {
22
AcpContractClientV2,
3+
AcpError,
34
AcpJobPhases,
45
} from "@virtuals-protocol/acp-node";
56
import {
@@ -30,62 +31,57 @@ async function evaluator() {
3031
console.log(`\nEvaluator: Polling for jobs assigned to ${EVALUATOR_AGENT_WALLET_ADDRESS} requiring evaluation...`);
3132
const activeJobsList = await acpClient.getActiveJobs();
3233

33-
if (!activeJobsList || activeJobsList.length === 0) {
34+
if (activeJobsList instanceof AcpError) {
35+
console.error(activeJobsList);
36+
break;
37+
}
38+
39+
if (activeJobsList.length === 0) {
3440
console.log("Evaluator: No active jobs found in this poll.");
3541
await sleep(POLL_INTERVAL_MS);
3642
continue;
3743
}
3844

3945
for (const job of activeJobsList) {
40-
const onchainJobId = job.id;
41-
try {
42-
const job = await acpClient.getJobById(onchainJobId);
43-
if (!job) {
44-
console.log(`Evaluator: Job ${onchainJobId} not found.`);
45-
continue;
46-
}
47-
const currentPhase = job.phase;
48-
const phaseName = AcpJobPhases[currentPhase];
46+
const currentPhase = job.phase;
47+
const phaseName = AcpJobPhases[currentPhase];
4948

50-
// Ensure this job is for the current evaluator
51-
if (job.evaluatorAddress !== EVALUATOR_AGENT_WALLET_ADDRESS) {
52-
continue;
53-
}
49+
// Ensure this job is for the current evaluator
50+
if (job.evaluatorAddress !== EVALUATOR_AGENT_WALLET_ADDRESS) {
51+
continue;
52+
}
5453

55-
if (currentPhase === AcpJobPhases.EVALUATION) {
56-
console.log(`Evaluator: Found Job ${onchainJobId} in EVALUATION phase.`);
54+
if (currentPhase === AcpJobPhases.EVALUATION) {
55+
console.log(`Evaluator: Found Job ${job.id} in EVALUATION phase.`);
5756

58-
// Simple evaluation logic: always accept
59-
const acceptTheDelivery = true;
60-
const evaluationReason = "Deliverable looks great, approved!";
57+
// Simple evaluation logic: always accept
58+
const acceptTheDelivery = true;
59+
const evaluationReason = "Deliverable looks great, approved!";
6160

62-
console.log(
63-
` Job ${onchainJobId}: Evaluating... Accepting: ${acceptTheDelivery}`
64-
);
65-
await job.evaluate(
66-
acceptTheDelivery,
67-
evaluationReason,
68-
);
69-
console.log(
70-
` Job ${onchainJobId}: Evaluation submitted.`
71-
);
72-
} else if (
73-
currentPhase === AcpJobPhases.REQUEST ||
74-
currentPhase === AcpJobPhases.NEGOTIATION
75-
) {
76-
console.log(
77-
`Evaluator: Job ${onchainJobId} is in ${phaseName} phase. Waiting for job to be delivered.`
78-
);
79-
} else if (
80-
currentPhase === AcpJobPhases.COMPLETED ||
81-
currentPhase === AcpJobPhases.REJECTED
82-
) {
83-
console.log(
84-
`Evaluator: Job ${onchainJobId} is already in ${phaseName}. No action.`
85-
);
86-
}
87-
} catch (e) {
88-
console.log(`Evaluator: Error processing job ${onchainJobId}: ${e}`);
61+
console.log(
62+
` Job ${job.id}: Evaluating... Accepting: ${acceptTheDelivery}`
63+
);
64+
await job.evaluate(
65+
acceptTheDelivery,
66+
evaluationReason,
67+
);
68+
console.log(
69+
` Job ${job.id}: Evaluation submitted.`
70+
);
71+
} else if (
72+
currentPhase === AcpJobPhases.REQUEST ||
73+
currentPhase === AcpJobPhases.NEGOTIATION
74+
) {
75+
console.log(
76+
`Evaluator: Job ${job.id} is in ${phaseName} phase. Waiting for job to be delivered.`
77+
);
78+
} else if (
79+
currentPhase === AcpJobPhases.COMPLETED ||
80+
currentPhase === AcpJobPhases.REJECTED
81+
) {
82+
console.log(
83+
`Evaluator: Job ${job.id} is already in ${phaseName}. No action.`
84+
);
8985
}
9086
}
9187
await sleep(POLL_INTERVAL_MS);

examples/acp-base/polling-mode/seller.ts

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import AcpClient, {
22
AcpContractClientV2,
3+
AcpError,
34
AcpJobPhases,
45
DeliverablePayload,
56
} from "@virtuals-protocol/acp-node";
@@ -28,85 +29,73 @@ async function seller() {
2829
),
2930
});
3031

31-
// job_id: { responded_to_request: boolean, delivered_work: boolean }
32-
const processedJobStages: Record<string, { responded_to_request?: boolean; delivered_work?: boolean }> = {};
33-
3432
while (true) {
3533
console.log(`\nSeller: Polling for active jobs for ${SELLER_AGENT_WALLET_ADDRESS}...`);
3634
const activeJobsList = await acpClient.getActiveJobs();
3735

38-
if (!activeJobsList || activeJobsList.length === 0) {
36+
if (activeJobsList instanceof AcpError) {
37+
console.error(activeJobsList);
38+
break;
39+
}
40+
41+
if (activeJobsList.length === 0) {
3942
console.log("Seller: No active jobs found in this poll.");
4043
await sleep(POLL_INTERVAL_MS);
4144
continue;
4245
}
4346

4447
for (const job of activeJobsList) {
45-
const onchainJobId = job.id;
4648
// Ensure this job is for the current seller
4749
if (job.providerAddress !== SELLER_AGENT_WALLET_ADDRESS) {
4850
continue;
4951
}
50-
const jobStages = processedJobStages[onchainJobId] || {};
5152
try {
52-
// Fetch full details to get current phase and memos
53-
const job = await acpClient.getJobById(onchainJobId);
54-
if (!job) {
55-
console.log(`Seller: Job ${onchainJobId} not found.`);
56-
continue;
57-
}
5853
const currentPhase = job.phase;
5954
const phaseName = AcpJobPhases[currentPhase];
60-
console.log(`Seller: Checking job ${onchainJobId}. Current Phase: ${phaseName}`);
55+
console.log(`Seller: Checking job ${job.id}. Current Phase: ${phaseName}`);
6156

62-
// 1. Respond to Job Request (if not already responded)
63-
if (currentPhase === AcpJobPhases.REQUEST && !jobStages.responded_to_request) {
57+
// 1. Respond to Job Request
58+
if (currentPhase === AcpJobPhases.REQUEST) {
6459
console.log(
65-
`Seller: Job ${onchainJobId} is in REQUEST. Responding to buyer's request with requirement: ${job.requirement}`
60+
`Seller: Job ${job.id} is in REQUEST. Responding to buyer's request with requirement: ${job.requirement}`
6661
);
6762
const response = true;
6863
if (response) {
6964
await job.accept("Job requirement matches agent capability");
70-
await job.createRequirement(`Job ${onchainJobId} accepted, please make payment to proceed`);
65+
await job.createRequirement(`Job ${job.id} accepted, please make payment to proceed`);
7166
} else {
7267
await job.reject("Job requirement does not meet agent capability");
7368
}
74-
console.log(`Job ${onchainJobId} ${response ? "accepted" : "rejected"}.`);
75-
jobStages.responded_to_request = true;
69+
console.log(`Job ${job.id} ${response ? "accepted" : "rejected"}.`);
7670
}
77-
// 2. Submit Deliverable (if job is paid and not yet delivered)
78-
else if (currentPhase === AcpJobPhases.TRANSACTION && !jobStages.delivered_work) {
71+
// 2. Submit Deliverable
72+
else if (currentPhase === AcpJobPhases.TRANSACTION) {
7973
// Buyer has paid, job is in TRANSACTION. Seller needs to deliver.
8074
// to cater cases where agent decide to reject job after payment has been made
8175
if (REJECT_JOB) { // conditional check for job rejection logic
8276
const reason = "Job requirement does not meet agent capability";
8377
console.log(`Rejecting job ${job.id} with reason: ${reason}`)
8478
await job.respond(false, reason);
85-
console.log(`Job ${onchainJobId} rejected`);
79+
console.log(`Job ${job.id} rejected`);
8680
return;
8781
}
8882

8983
const deliverable: DeliverablePayload = {
9084
type: "url",
9185
value: "https://example.com",
9286
}
93-
console.log(`Delivering job ${onchainJobId} with deliverable`, deliverable);
87+
console.log(`Delivering job ${job.id} with deliverable`, deliverable);
9488
await job.deliver(deliverable);
95-
console.log(`Job ${onchainJobId} delivered`);
96-
jobStages.delivered_work = true;
89+
console.log(`Job ${job.id} delivered`);
9790
} else if (
9891
currentPhase === AcpJobPhases.EVALUATION ||
9992
currentPhase === AcpJobPhases.COMPLETED ||
10093
currentPhase === AcpJobPhases.REJECTED
10194
) {
102-
console.log(`Seller: Job ${onchainJobId} is in ${phaseName}. No further action for seller.`);
103-
// Mark as fully handled for this script
104-
jobStages.responded_to_request = true;
105-
jobStages.delivered_work = true;
95+
console.log(`Seller: Job ${job.id} is in ${phaseName}. No further action for seller.`);
10696
}
107-
processedJobStages[onchainJobId] = jobStages;
10897
} catch (e) {
109-
console.log(`Seller: Error processing job ${onchainJobId}: ${e}`);
98+
console.log(`Seller: Error processing job ${job.id}: ${e}`);
11099
}
111100
}
112101
await sleep(POLL_INTERVAL_MS);

0 commit comments

Comments
 (0)