|
1 | 1 | import AcpClient, { |
2 | 2 | AcpContractClientV2, |
| 3 | + AcpError, |
3 | 4 | AcpJobPhases, |
4 | 5 | DeliverablePayload, |
5 | 6 | } from "@virtuals-protocol/acp-node"; |
@@ -28,85 +29,73 @@ async function seller() { |
28 | 29 | ), |
29 | 30 | }); |
30 | 31 |
|
31 | | - // job_id: { responded_to_request: boolean, delivered_work: boolean } |
32 | | - const processedJobStages: Record<string, { responded_to_request?: boolean; delivered_work?: boolean }> = {}; |
33 | | - |
34 | 32 | while (true) { |
35 | 33 | console.log(`\nSeller: Polling for active jobs for ${SELLER_AGENT_WALLET_ADDRESS}...`); |
36 | 34 | const activeJobsList = await acpClient.getActiveJobs(); |
37 | 35 |
|
38 | | - if (!activeJobsList || activeJobsList.length === 0) { |
| 36 | + if (activeJobsList instanceof AcpError) { |
| 37 | + console.error(activeJobsList); |
| 38 | + break; |
| 39 | + } |
| 40 | + |
| 41 | + if (activeJobsList.length === 0) { |
39 | 42 | console.log("Seller: No active jobs found in this poll."); |
40 | 43 | await sleep(POLL_INTERVAL_MS); |
41 | 44 | continue; |
42 | 45 | } |
43 | 46 |
|
44 | 47 | for (const job of activeJobsList) { |
45 | | - const onchainJobId = job.id; |
46 | 48 | // Ensure this job is for the current seller |
47 | 49 | if (job.providerAddress !== SELLER_AGENT_WALLET_ADDRESS) { |
48 | 50 | continue; |
49 | 51 | } |
50 | | - const jobStages = processedJobStages[onchainJobId] || {}; |
51 | 52 | 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 | | - } |
58 | 53 | const currentPhase = job.phase; |
59 | 54 | 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}`); |
61 | 56 |
|
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) { |
64 | 59 | 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}` |
66 | 61 | ); |
67 | 62 | const response = true; |
68 | 63 | if (response) { |
69 | 64 | 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`); |
71 | 66 | } else { |
72 | 67 | await job.reject("Job requirement does not meet agent capability"); |
73 | 68 | } |
74 | | - console.log(`Job ${onchainJobId} ${response ? "accepted" : "rejected"}.`); |
75 | | - jobStages.responded_to_request = true; |
| 69 | + console.log(`Job ${job.id} ${response ? "accepted" : "rejected"}.`); |
76 | 70 | } |
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) { |
79 | 73 | // Buyer has paid, job is in TRANSACTION. Seller needs to deliver. |
80 | 74 | // to cater cases where agent decide to reject job after payment has been made |
81 | 75 | if (REJECT_JOB) { // conditional check for job rejection logic |
82 | 76 | const reason = "Job requirement does not meet agent capability"; |
83 | 77 | console.log(`Rejecting job ${job.id} with reason: ${reason}`) |
84 | 78 | await job.respond(false, reason); |
85 | | - console.log(`Job ${onchainJobId} rejected`); |
| 79 | + console.log(`Job ${job.id} rejected`); |
86 | 80 | return; |
87 | 81 | } |
88 | 82 |
|
89 | 83 | const deliverable: DeliverablePayload = { |
90 | 84 | type: "url", |
91 | 85 | value: "https://example.com", |
92 | 86 | } |
93 | | - console.log(`Delivering job ${onchainJobId} with deliverable`, deliverable); |
| 87 | + console.log(`Delivering job ${job.id} with deliverable`, deliverable); |
94 | 88 | await job.deliver(deliverable); |
95 | | - console.log(`Job ${onchainJobId} delivered`); |
96 | | - jobStages.delivered_work = true; |
| 89 | + console.log(`Job ${job.id} delivered`); |
97 | 90 | } else if ( |
98 | 91 | currentPhase === AcpJobPhases.EVALUATION || |
99 | 92 | currentPhase === AcpJobPhases.COMPLETED || |
100 | 93 | currentPhase === AcpJobPhases.REJECTED |
101 | 94 | ) { |
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.`); |
106 | 96 | } |
107 | | - processedJobStages[onchainJobId] = jobStages; |
108 | 97 | } catch (e) { |
109 | | - console.log(`Seller: Error processing job ${onchainJobId}: ${e}`); |
| 98 | + console.log(`Seller: Error processing job ${job.id}: ${e}`); |
110 | 99 | } |
111 | 100 | } |
112 | 101 | await sleep(POLL_INTERVAL_MS); |
|
0 commit comments