11import { Address , parseEther } from "viem" ;
22import { io } from "socket.io-client" ;
3- import AcpContractClient , { AcpJobPhases , MemoType } from "./acpContractClient" ;
3+ import AcpContractClient , {
4+ AcpJobPhases ,
5+ AcpNegoStatus ,
6+ MemoType ,
7+ } from "./acpContractClient" ;
48import { AcpAgent } from "../interfaces" ;
59import AcpJob from "./acpJob" ;
610import AcpMemo from "./acpMemo" ;
711import AcpJobOffering from "./acpJobOffering" ;
12+ import AcpMessage from "./acpMessage" ;
813
914export interface IDeliverable {
1015 type : string ;
@@ -29,6 +34,7 @@ interface IAcpJob {
2934 data : {
3035 onChainJobId : number ;
3136 phase : AcpJobPhases ;
37+ negoStatus : AcpNegoStatus ;
3238 description : string ;
3339 buyerAddress : `0x${string } `;
3440 sellerAddress : `0x${string } `;
@@ -57,12 +63,15 @@ interface IAcpClientOptions {
5763 acpContractClient : AcpContractClient ;
5864 onNewTask ?: ( job : AcpJob ) => void ;
5965 onEvaluate ?: ( job : AcpJob ) => void ;
66+ onNewMsg ?: ( msg : AcpMessage , job : AcpJob ) => void ;
6067}
6168
62- enum SocketEvents {
69+ export enum SocketEvents {
6370 ROOM_JOINED = "roomJoined" ,
6471 ON_EVALUATE = "onEvaluate" ,
6572 ON_NEW_TASK = "onNewTask" ,
73+ ON_NEW_MSG = "onNewMsg" ,
74+ ON_CREATE_MSG = "onCreateMsg" ,
6675}
6776export class EvaluateResult {
6877 isApproved : boolean ;
@@ -76,15 +85,16 @@ export class EvaluateResult {
7685
7786class AcpClient {
7887 private acpUrl ;
88+ private acpJob : AcpJob | null = null ;
7989 public acpContractClient : AcpContractClient ;
8090 private onNewTask ?: ( job : AcpJob ) => void ;
8191 private onEvaluate ?: ( job : AcpJob ) => void ;
82-
92+ private onNewMsg ?: ( msg : AcpMessage , job : AcpJob ) => void ;
8393 constructor ( options : IAcpClientOptions ) {
8494 this . acpContractClient = options . acpContractClient ;
8595 this . onNewTask = options . onNewTask ;
8696 this . onEvaluate = options . onEvaluate || this . defaultOnEvaluate ;
87-
97+ this . onNewMsg = options . onNewMsg ;
8898 this . acpUrl = this . acpContractClient . config . acpUrl ;
8999 this . init ( ) ;
90100 }
@@ -129,9 +139,12 @@ class AcpClient {
129139 memo . nextPhase
130140 ) ;
131141 } ) ,
132- data . phase
142+ data . phase ,
143+ data . negoStatus
133144 ) ;
134145
146+ this . acpJob = job ;
147+
135148 this . onEvaluate ( job ) ;
136149 }
137150 }
@@ -156,14 +169,35 @@ class AcpClient {
156169 memo . nextPhase
157170 ) ;
158171 } ) ,
159- data . phase
172+ data . phase ,
173+ data . negoStatus
160174 ) ;
161175
176+ this . acpJob = job ;
177+
162178 this . onNewTask ( job ) ;
163179 }
164180 }
165181 ) ;
166182
183+ socket . on ( SocketEvents . ON_NEW_MSG , ( data , callback ) => {
184+ callback ( true ) ;
185+
186+ if ( this . onNewMsg && this . acpJob ) {
187+ this . acpJob . negoStatus = AcpNegoStatus . PENDING ;
188+
189+ const msg = new AcpMessage (
190+ data . id ,
191+ data . messages ?? [ ] ,
192+ socket ,
193+ this . acpJob ,
194+ this . acpContractClient . walletAddress
195+ ) ;
196+
197+ this . onNewMsg ( msg , this . acpJob ) ;
198+ }
199+ } ) ;
200+
167201 const cleanup = async ( ) => {
168202 if ( socket ) {
169203 socket . disconnect ( ) ;
0 commit comments