|
| 1 | +// TODO: Point the imports to acp-node after publishing |
| 2 | + |
| 3 | +import AcpClient from "../../../src/acpClient"; |
| 4 | +import AcpContractClient, { AcpJobPhases, AcpNegoStatus } from "../../../src/acpContractClient"; |
| 5 | +import AcpJob from "../../../src/acpJob"; |
| 6 | +import AcpMessage from "../../../src/acpMessage"; |
| 7 | +import { baseSepoliaAcpConfig } from "../../../src"; |
| 8 | +import { SimpleNegotiationManager } from "./negotiationManager"; |
| 9 | +import dotenv from 'dotenv'; |
| 10 | + |
| 11 | +dotenv.config(); |
| 12 | + |
| 13 | +const BUYER_WALLET_ADDRESS = process.env.BUYER_WALLET_ADDRESS!; |
| 14 | +const SELLER_WALLET_ADDRESS = process.env.SELLER_WALLET_ADDRESS!; |
| 15 | +const WHITELISTED_WALLET_ENTITY_ID = process.env.WHITELISTED_WALLET_ENTITY_ID!; |
| 16 | +const WHITELISTED_WALLET_PRIVATE_KEY = process.env.WHITELISTED_WALLET_PRIVATE_KEY!; |
| 17 | + |
| 18 | +async function buyer() { |
| 19 | + console.log("Starting AI Buyer..."); |
| 20 | + |
| 21 | + const acpClient = new AcpClient({ |
| 22 | + acpContractClient: await AcpContractClient.build( |
| 23 | + WHITELISTED_WALLET_PRIVATE_KEY as `0x${string}`, |
| 24 | + Number(WHITELISTED_WALLET_ENTITY_ID), |
| 25 | + BUYER_WALLET_ADDRESS as `0x${string}`, |
| 26 | + baseSepoliaAcpConfig |
| 27 | + ), |
| 28 | + onNewTask: async (job: AcpJob) => { |
| 29 | + console.log(`BUYER received task: Job ${job.id}, Phase: ${job.phase}, NegoStatus: ${job.negoStatus}`); |
| 30 | + |
| 31 | + if (job.phase === AcpJobPhases.NEGOTIATION ) { |
| 32 | + console.log("Starting negotiation with REAL job object..."); |
| 33 | + // Ensure negoStatus is PENDING before starting negotiation |
| 34 | + job.negoStatus = AcpNegoStatus.PENDING; |
| 35 | + console.log(`Set job ${job.id} negoStatus to PENDING`); |
| 36 | + |
| 37 | + await SimpleNegotiationManager.negotiateChatWithoutSocket( |
| 38 | + BUYER_WALLET_ADDRESS, |
| 39 | + SELLER_WALLET_ADDRESS, |
| 40 | + 'Meme generator service', |
| 41 | + 1, |
| 42 | + 2 |
| 43 | + ); |
| 44 | + } |
| 45 | + }, |
| 46 | + onNewMsg: async (msg: AcpMessage, job: AcpJob) => { |
| 47 | + // Handle messages during negotiation |
| 48 | + if (msg.messages && msg.messages.length > 0) { |
| 49 | + const latestMessage = msg.messages[msg.messages.length - 1]; |
| 50 | + |
| 51 | + if (latestMessage.sender !== BUYER_WALLET_ADDRESS) { |
| 52 | + const isDone = await SimpleNegotiationManager.handleMessage( |
| 53 | + BUYER_WALLET_ADDRESS, |
| 54 | + latestMessage.content, |
| 55 | + msg, |
| 56 | + job |
| 57 | + ); |
| 58 | + |
| 59 | + if (isDone) { |
| 60 | + console.log("Negotiation complete - paying..."); |
| 61 | + await job.pay(1000); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + }, |
| 66 | + onEvaluate: async (job: AcpJob) => { |
| 67 | + await job.evaluate(true, "AI buyer approved"); |
| 68 | + }, |
| 69 | + }); |
| 70 | + |
| 71 | + console.log("Starting job..."); |
| 72 | + const jobId = await acpClient.initiateJob(SELLER_WALLET_ADDRESS as `0x${string}`, "Meme generator", undefined); |
| 73 | + console.log(`Job ${jobId} initiated - waiting for seller response...`); |
| 74 | +} |
| 75 | + |
| 76 | +buyer(); |
0 commit comments