@@ -10,62 +10,18 @@ import AcpJob from "./acpJob";
1010import AcpMemo from "./acpMemo" ;
1111import AcpJobOffering from "./acpJobOffering" ;
1212import AcpMessage from "./acpMessage" ;
13+ import {
14+ IAcpClientOptions ,
15+ IAcpJob ,
16+ IAcpJobResponse ,
17+ IAcpMemo ,
18+ } from "./interfaces" ;
1319
1420export 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-
6925export 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
421492export default AcpClient ;
0 commit comments