@silvana-one/agent 1.0.41 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/grpc.ts CHANGED
@@ -14,6 +14,8 @@ import {
14
14
  GetBlockRequestSchema,
15
15
  GetBlockSettlementRequestSchema,
16
16
  UpdateBlockSettlementRequestSchema,
17
+ GetSettlementProofRequestSchema,
18
+ SubmitSettlementProofRequestSchema,
17
19
  TerminateJobRequestSchema,
18
20
  ReadDataAvailabilityRequestSchema,
19
21
  SetKVRequestSchema,
@@ -44,6 +46,8 @@ import {
44
46
  type GetBlockResponse,
45
47
  type GetBlockSettlementResponse,
46
48
  type UpdateBlockSettlementResponse,
49
+ type GetSettlementProofResponse,
50
+ type SubmitSettlementProofResponse,
47
51
  type Block,
48
52
  type BlockSettlement,
49
53
  type Metadata,
@@ -78,7 +82,7 @@ let chain: string | null = null;
78
82
  let coordinatorId: string | null = null;
79
83
  let sessionPrivateKey: string | null = null;
80
84
  let developer: string | null = null;
81
- let agent: string | null = null;
85
+ let agentName: string | null = null;
82
86
  let agentMethod: string | null = null;
83
87
 
84
88
  /**
@@ -92,7 +96,7 @@ function getCoordinatorClient(): {
92
96
  coordinatorId: string;
93
97
  sessionPrivateKey: string;
94
98
  developer: string;
95
- agent: string;
99
+ agentName: string;
96
100
  agentMethod: string;
97
101
  } {
98
102
  if (coordinatorClient === null) {
@@ -102,7 +106,7 @@ function getCoordinatorClient(): {
102
106
  coordinatorId = process.env.COORDINATOR_ID || null;
103
107
  sessionPrivateKey = process.env.SESSION_PRIVATE_KEY || null;
104
108
  developer = process.env.DEVELOPER || null;
105
- agent = process.env.AGENT || null;
109
+ agentName = process.env.AGENT || null;
106
110
  agentMethod = process.env.AGENT_METHOD || null;
107
111
 
108
112
  // Check for required environment variables
@@ -146,7 +150,7 @@ function getCoordinatorClient(): {
146
150
  coordinatorId: coordinatorId as string,
147
151
  sessionPrivateKey: sessionPrivateKey as string,
148
152
  developer: developer as string,
149
- agent: agent as string,
153
+ agentName: agentName as string,
150
154
  agentMethod: agentMethod as string,
151
155
  };
152
156
  }
@@ -192,12 +196,12 @@ export async function getSecret(key: string): Promise<string | null> {
192
196
  * Gets a job from the coordinator
193
197
  */
194
198
  export async function getJob(): Promise<GetJobResponse> {
195
- const { client, sessionId, developer, agent, agentMethod } =
199
+ const { client, sessionId, developer, agentName, agentMethod } =
196
200
  getCoordinatorClient();
197
201
 
198
202
  const request = create(GetJobRequestSchema, {
199
203
  developer,
200
- agent,
204
+ agent: agentName,
201
205
  agentMethod,
202
206
  sessionId,
203
207
  });
@@ -291,14 +295,14 @@ export async function getSequenceStates(
291
295
  /**
292
296
  * Submits a proof
293
297
  */
294
- export async function submitProof(
295
- blockNumber: bigint,
296
- sequences: bigint[],
297
- proof: string,
298
- cpuTime: bigint,
299
- mergedSequences1?: bigint[],
300
- mergedSequences2?: bigint[]
301
- ): Promise<SubmitProofResponse> {
298
+ export async function submitProof(params: {
299
+ blockNumber: bigint;
300
+ sequences: bigint[];
301
+ proof: string;
302
+ cpuTime: bigint;
303
+ mergedSequences1?: bigint[];
304
+ mergedSequences2?: bigint[];
305
+ }): Promise<SubmitProofResponse> {
302
306
  if (!jobId) {
303
307
  throw new Error("Call getJob() first");
304
308
  }
@@ -307,12 +311,12 @@ export async function submitProof(
307
311
  const request = create(SubmitProofRequestSchema, {
308
312
  sessionId,
309
313
  jobId,
310
- blockNumber,
311
- sequences,
312
- proof,
313
- cpuTime,
314
- mergedSequences1: mergedSequences1 || [],
315
- mergedSequences2: mergedSequences2 || [],
314
+ blockNumber: params.blockNumber,
315
+ sequences: params.sequences,
316
+ proof: params.proof,
317
+ cpuTime: params.cpuTime,
318
+ mergedSequences1: params.mergedSequences1 || [],
319
+ mergedSequences2: params.mergedSequences2 || [],
316
320
  });
317
321
 
318
322
  return await client.submitProof(request);
@@ -321,11 +325,11 @@ export async function submitProof(
321
325
  /**
322
326
  * Submits state
323
327
  */
324
- export async function submitState(
325
- sequence: bigint,
326
- newStateData?: Uint8Array,
327
- serializedState?: string
328
- ): Promise<SubmitStateResponse> {
328
+ export async function submitState(params: {
329
+ sequence: bigint;
330
+ newStateData?: Uint8Array;
331
+ serializedState?: string;
332
+ }): Promise<SubmitStateResponse> {
329
333
  if (!jobId) {
330
334
  throw new Error("Call getJob() first");
331
335
  }
@@ -334,9 +338,9 @@ export async function submitState(
334
338
  const request = create(SubmitStateRequestSchema, {
335
339
  sessionId,
336
340
  jobId,
337
- sequence,
338
- newStateData,
339
- serializedState,
341
+ sequence: params.sequence,
342
+ newStateData: params.newStateData,
343
+ serializedState: params.serializedState,
340
344
  });
341
345
 
342
346
  return await client.submitState(request);
@@ -345,10 +349,10 @@ export async function submitState(
345
349
  /**
346
350
  * Gets a proof
347
351
  */
348
- export async function getProof(
349
- blockNumber: bigint,
350
- sequences: bigint[]
351
- ): Promise<GetProofResponse> {
352
+ export async function getProof(params: {
353
+ blockNumber: bigint;
354
+ sequences: bigint[];
355
+ }): Promise<GetProofResponse> {
352
356
  if (!jobId) {
353
357
  throw new Error("Call getJob() first");
354
358
  }
@@ -356,8 +360,8 @@ export async function getProof(
356
360
 
357
361
  const request = create(GetProofRequestSchema, {
358
362
  sessionId,
359
- blockNumber,
360
- sequences,
363
+ blockNumber: params.blockNumber,
364
+ sequences: params.sequences,
361
365
  jobId,
362
366
  });
363
367
 
@@ -406,10 +410,10 @@ export async function readDataAvailability(
406
410
  /**
407
411
  * Sets a key-value pair in the app instance KV store
408
412
  */
409
- export async function setKv(
410
- key: string,
411
- value: string
412
- ): Promise<SetKVResponse> {
413
+ export async function setKv(params: {
414
+ key: string;
415
+ value: string;
416
+ }): Promise<SetKVResponse> {
413
417
  if (!jobId) {
414
418
  throw new Error("Call getJob() first");
415
419
  }
@@ -418,8 +422,8 @@ export async function setKv(
418
422
  const request = create(SetKVRequestSchema, {
419
423
  sessionId,
420
424
  jobId,
421
- key,
422
- value,
425
+ key: params.key,
426
+ value: params.value,
423
427
  });
424
428
 
425
429
  return await client.setKV(request);
@@ -464,10 +468,10 @@ export async function deleteKv(key: string): Promise<DeleteKVResponse> {
464
468
  /**
465
469
  * Adds metadata to the app instance (write-once)
466
470
  */
467
- export async function addMetadata(
468
- key: string,
469
- value: string
470
- ): Promise<AddMetadataResponse> {
471
+ export async function addMetadata(params: {
472
+ key: string;
473
+ value: string;
474
+ }): Promise<AddMetadataResponse> {
471
475
  if (!jobId) {
472
476
  throw new Error("Call getJob() first");
473
477
  }
@@ -476,8 +480,8 @@ export async function addMetadata(
476
480
  const request = create(AddMetadataRequestSchema, {
477
481
  sessionId,
478
482
  jobId,
479
- key,
480
- value,
483
+ key: params.key,
484
+ value: params.value,
481
485
  });
482
486
 
483
487
  return await client.addMetadata(request);
@@ -530,10 +534,10 @@ export async function tryCreateBlock(): Promise<TryCreateBlockResponse> {
530
534
  /**
531
535
  * Updates block state data availability
532
536
  */
533
- export async function updateBlockStateDataAvailability(
534
- blockNumber: bigint,
535
- stateDataAvailability: string
536
- ): Promise<UpdateBlockStateDataAvailabilityResponse> {
537
+ export async function updateBlockStateDataAvailability(params: {
538
+ blockNumber: bigint;
539
+ stateDataAvailability: string;
540
+ }): Promise<UpdateBlockStateDataAvailabilityResponse> {
537
541
  if (!jobId) {
538
542
  throw new Error("Call getJob() first");
539
543
  }
@@ -542,8 +546,8 @@ export async function updateBlockStateDataAvailability(
542
546
  const request = create(UpdateBlockStateDataAvailabilityRequestSchema, {
543
547
  sessionId,
544
548
  jobId,
545
- blockNumber,
546
- stateDataAvailability,
549
+ blockNumber: params.blockNumber,
550
+ stateDataAvailability: params.stateDataAvailability,
547
551
  });
548
552
 
549
553
  return await client.updateBlockStateDataAvailability(request);
@@ -552,10 +556,10 @@ export async function updateBlockStateDataAvailability(
552
556
  /**
553
557
  * Updates block proof data availability
554
558
  */
555
- export async function updateBlockProofDataAvailability(
556
- blockNumber: bigint,
557
- proofDataAvailability: string
558
- ): Promise<UpdateBlockProofDataAvailabilityResponse> {
559
+ export async function updateBlockProofDataAvailability(params: {
560
+ blockNumber: bigint;
561
+ proofDataAvailability: string;
562
+ }): Promise<UpdateBlockProofDataAvailabilityResponse> {
559
563
  if (!jobId) {
560
564
  throw new Error("Call getJob() first");
561
565
  }
@@ -564,8 +568,8 @@ export async function updateBlockProofDataAvailability(
564
568
  const request = create(UpdateBlockProofDataAvailabilityRequestSchema, {
565
569
  sessionId,
566
570
  jobId,
567
- blockNumber,
568
- proofDataAvailability,
571
+ blockNumber: params.blockNumber,
572
+ proofDataAvailability: params.proofDataAvailability,
569
573
  });
570
574
 
571
575
  return await client.updateBlockProofDataAvailability(request);
@@ -574,11 +578,11 @@ export async function updateBlockProofDataAvailability(
574
578
  /**
575
579
  * Updates block settlement transaction hash
576
580
  */
577
- export async function updateBlockSettlementTxHash(
578
- blockNumber: bigint,
579
- settlementTxHash: string,
580
- settlementChain: string
581
- ): Promise<UpdateBlockSettlementTxHashResponse> {
581
+ export async function updateBlockSettlementTxHash(params: {
582
+ blockNumber: bigint;
583
+ settlementTxHash: string;
584
+ settlementChain: string;
585
+ }): Promise<UpdateBlockSettlementTxHashResponse> {
582
586
  if (!jobId) {
583
587
  throw new Error("Call getJob() first");
584
588
  }
@@ -587,9 +591,9 @@ export async function updateBlockSettlementTxHash(
587
591
  const request = create(UpdateBlockSettlementTxHashRequestSchema, {
588
592
  sessionId,
589
593
  jobId,
590
- blockNumber,
591
- settlementTxHash,
592
- chain: settlementChain,
594
+ blockNumber: params.blockNumber,
595
+ settlementTxHash: params.settlementTxHash,
596
+ chain: params.settlementChain,
593
597
  });
594
598
 
595
599
  return await client.updateBlockSettlementTxHash(request);
@@ -598,11 +602,11 @@ export async function updateBlockSettlementTxHash(
598
602
  /**
599
603
  * Updates block settlement transaction included in block
600
604
  */
601
- export async function updateBlockSettlementTxIncludedInBlock(
602
- blockNumber: bigint,
603
- settledAt: bigint,
604
- settlementChain: string
605
- ): Promise<UpdateBlockSettlementTxIncludedInBlockResponse> {
605
+ export async function updateBlockSettlementTxIncludedInBlock(params: {
606
+ blockNumber: bigint;
607
+ settledAt: bigint;
608
+ settlementChain: string;
609
+ }): Promise<UpdateBlockSettlementTxIncludedInBlockResponse> {
606
610
  if (!jobId) {
607
611
  throw new Error("Call getJob() first");
608
612
  }
@@ -611,9 +615,9 @@ export async function updateBlockSettlementTxIncludedInBlock(
611
615
  const request = create(UpdateBlockSettlementTxIncludedInBlockRequestSchema, {
612
616
  sessionId,
613
617
  jobId,
614
- blockNumber,
615
- settledAt,
616
- chain: settlementChain,
618
+ blockNumber: params.blockNumber,
619
+ settledAt: params.settledAt,
620
+ chain: params.settlementChain,
617
621
  });
618
622
 
619
623
  return await client.updateBlockSettlementTxIncludedInBlock(request);
@@ -680,10 +684,10 @@ export async function getBlock(blockNumber: bigint): Promise<GetBlockResponse> {
680
684
  /**
681
685
  * Rejects a proof for specific sequences
682
686
  */
683
- export async function rejectProof(
684
- blockNumber: bigint,
685
- sequences: bigint[]
686
- ): Promise<RejectProofResponse> {
687
+ export async function rejectProof(params: {
688
+ blockNumber: bigint;
689
+ sequences: bigint[];
690
+ }): Promise<RejectProofResponse> {
687
691
  if (!jobId) {
688
692
  throw new Error("Call getJob() first");
689
693
  }
@@ -691,8 +695,8 @@ export async function rejectProof(
691
695
 
692
696
  const request = create(RejectProofRequestSchema, {
693
697
  sessionId,
694
- blockNumber,
695
- sequences,
698
+ blockNumber: params.blockNumber,
699
+ sequences: params.sequences,
696
700
  jobId,
697
701
  });
698
702
 
@@ -702,10 +706,10 @@ export async function rejectProof(
702
706
  /**
703
707
  * Gets a block settlement for a specific chain
704
708
  */
705
- export async function getBlockSettlement(
706
- blockNumber: bigint,
707
- chain: string
708
- ): Promise<GetBlockSettlementResponse> {
709
+ export async function getBlockSettlement(params: {
710
+ blockNumber: bigint;
711
+ chain: string;
712
+ }): Promise<GetBlockSettlementResponse> {
709
713
  if (!jobId) {
710
714
  throw new Error("Call getJob() first");
711
715
  }
@@ -714,8 +718,8 @@ export async function getBlockSettlement(
714
718
  const request = create(GetBlockSettlementRequestSchema, {
715
719
  sessionId,
716
720
  jobId,
717
- blockNumber,
718
- chain,
721
+ blockNumber: params.blockNumber,
722
+ chain: params.chain,
719
723
  });
720
724
 
721
725
  return await client.getBlockSettlement(request);
@@ -724,16 +728,16 @@ export async function getBlockSettlement(
724
728
  /**
725
729
  * Updates a block settlement for a specific chain
726
730
  */
727
- export async function updateBlockSettlement(
728
- blockNumber: bigint,
729
- chain: string,
731
+ export async function updateBlockSettlement(params: {
732
+ blockNumber: bigint;
733
+ chain: string;
730
734
  settlementData: {
731
735
  settlementTxHash?: string;
732
736
  settlementTxIncludedInBlock?: boolean;
733
737
  sentToSettlementAt?: bigint;
734
738
  settledAt?: bigint;
735
- }
736
- ): Promise<UpdateBlockSettlementResponse> {
739
+ };
740
+ }): Promise<UpdateBlockSettlementResponse> {
737
741
  if (!jobId) {
738
742
  throw new Error("Call getJob() first");
739
743
  }
@@ -742,18 +746,64 @@ export async function updateBlockSettlement(
742
746
  const request = create(UpdateBlockSettlementRequestSchema, {
743
747
  sessionId,
744
748
  jobId,
745
- blockNumber,
746
- chain,
747
- settlementTxHash: settlementData.settlementTxHash,
749
+ blockNumber: params.blockNumber,
750
+ chain: params.chain,
751
+ settlementTxHash: params.settlementData.settlementTxHash,
748
752
  settlementTxIncludedInBlock:
749
- settlementData.settlementTxIncludedInBlock ?? false,
750
- sentToSettlementAt: settlementData.sentToSettlementAt,
751
- settledAt: settlementData.settledAt,
753
+ params.settlementData.settlementTxIncludedInBlock ?? false,
754
+ sentToSettlementAt: params.settlementData.sentToSettlementAt,
755
+ settledAt: params.settlementData.settledAt,
752
756
  });
753
757
 
754
758
  return await client.updateBlockSettlement(request);
755
759
  }
756
760
 
761
+ /**
762
+ * Gets a settlement proof for a specific block and chain
763
+ */
764
+ export async function getSettlementProof(params: {
765
+ blockNumber: bigint;
766
+ settlementChain: string;
767
+ }): Promise<GetSettlementProofResponse> {
768
+ if (!jobId) {
769
+ throw new Error("Call getJob() first");
770
+ }
771
+ const { client, sessionId } = getCoordinatorClient();
772
+
773
+ const request = create(GetSettlementProofRequestSchema, {
774
+ sessionId,
775
+ blockNumber: params.blockNumber,
776
+ jobId,
777
+ settlementChain: params.settlementChain,
778
+ });
779
+
780
+ return await client.getSettlementProof(request);
781
+ }
782
+
783
+ /**
784
+ * Submits a settlement proof
785
+ */
786
+ export async function submitSettlementProof(params: {
787
+ blockNumber: bigint;
788
+ proof: string;
789
+ cpuTime: bigint;
790
+ }): Promise<SubmitSettlementProofResponse> {
791
+ if (!jobId) {
792
+ throw new Error("Call getJob() first");
793
+ }
794
+ const { client, sessionId } = getCoordinatorClient();
795
+
796
+ const request = create(SubmitSettlementProofRequestSchema, {
797
+ sessionId,
798
+ blockNumber: params.blockNumber,
799
+ jobId,
800
+ proof: params.proof,
801
+ cpuTime: params.cpuTime,
802
+ });
803
+
804
+ return await client.submitSettlementProof(request);
805
+ }
806
+
757
807
  /**
758
808
  * Sends a proof event to the coordinator
759
809
  */
@@ -806,89 +856,105 @@ export async function agentMessage(
806
856
  }
807
857
 
808
858
  /**
809
- * Logs a debug message to the coordinator and console
810
- */
811
- export async function debug(...args: any[]): Promise<void> {
812
- const message = args
813
- .map((arg) => (typeof arg === "object" ? JSON.stringify(arg) : String(arg)))
814
- .join(" ");
815
-
816
- console.log(...args);
817
-
818
- try {
819
- await agentMessage(LogLevel.DEBUG, message);
820
- } catch (error) {
821
- console.error("Failed to send debug message to coordinator:", error);
822
- }
823
- }
824
-
825
- /**
826
- * Logs an info message to the coordinator and console
859
+ * Agent logging interface
860
+ * Provides methods to log messages to both the console and coordinator
827
861
  */
828
- export async function info(...args: any[]): Promise<void> {
829
- const message = args
830
- .map((arg) => (typeof arg === "object" ? JSON.stringify(arg) : String(arg)))
831
- .join(" ");
862
+ export const agent = {
863
+ /**
864
+ * Logs a debug message to the coordinator and console
865
+ */
866
+ debug: async (...args: any[]): Promise<void> => {
867
+ const message = args
868
+ .map((arg) =>
869
+ typeof arg === "object" ? JSON.stringify(arg) : String(arg)
870
+ )
871
+ .join(" ");
832
872
 
833
- console.log(...args);
873
+ console.log(...args);
834
874
 
835
- try {
836
- await agentMessage(LogLevel.INFO, message);
837
- } catch (error) {
838
- console.error("Failed to send info message to coordinator:", error);
839
- }
840
- }
841
-
842
- /**
843
- * Logs a warning message to the coordinator and console
844
- */
845
- export async function warn(...args: any[]): Promise<void> {
846
- const message = args
847
- .map((arg) => (typeof arg === "object" ? JSON.stringify(arg) : String(arg)))
848
- .join(" ");
849
-
850
- console.warn(...args);
851
-
852
- try {
853
- await agentMessage(LogLevel.WARN, message);
854
- } catch (error) {
855
- console.error("Failed to send warn message to coordinator:", error);
856
- }
857
- }
858
-
859
- /**
860
- * Logs an error message to the coordinator and console
861
- */
862
- export async function error(...args: any[]): Promise<void> {
863
- const message = args
864
- .map((arg) => (typeof arg === "object" ? JSON.stringify(arg) : String(arg)))
865
- .join(" ");
866
-
867
- console.error(...args);
868
-
869
- try {
870
- await agentMessage(LogLevel.ERROR, message);
871
- } catch (error) {
872
- console.error("Failed to send error message to coordinator:", error);
873
- }
874
- }
875
-
876
- /**
877
- * Logs a fatal message to the coordinator and console
878
- */
879
- export async function fatal(...args: any[]): Promise<void> {
880
- const message = args
881
- .map((arg) => (typeof arg === "object" ? JSON.stringify(arg) : String(arg)))
882
- .join(" ");
883
-
884
- console.error(...args);
885
-
886
- try {
887
- await agentMessage(LogLevel.FATAL, message);
888
- } catch (error) {
889
- console.error("Failed to send fatal message to coordinator:", error);
890
- }
891
- }
875
+ try {
876
+ await agentMessage(LogLevel.DEBUG, message);
877
+ } catch (error) {
878
+ console.error("Failed to send debug message to coordinator:", error);
879
+ }
880
+ },
881
+
882
+ /**
883
+ * Logs an info message to the coordinator and console
884
+ */
885
+ info: async (...args: any[]): Promise<void> => {
886
+ const message = args
887
+ .map((arg) =>
888
+ typeof arg === "object" ? JSON.stringify(arg) : String(arg)
889
+ )
890
+ .join(" ");
891
+
892
+ console.log(...args);
893
+
894
+ try {
895
+ await agentMessage(LogLevel.INFO, message);
896
+ } catch (error) {
897
+ console.error("Failed to send info message to coordinator:", error);
898
+ }
899
+ },
900
+
901
+ /**
902
+ * Logs a warning message to the coordinator and console
903
+ */
904
+ warn: async (...args: any[]): Promise<void> => {
905
+ const message = args
906
+ .map((arg) =>
907
+ typeof arg === "object" ? JSON.stringify(arg) : String(arg)
908
+ )
909
+ .join(" ");
910
+
911
+ console.warn(...args);
912
+
913
+ try {
914
+ await agentMessage(LogLevel.WARN, message);
915
+ } catch (error) {
916
+ console.error("Failed to send warn message to coordinator:", error);
917
+ }
918
+ },
919
+
920
+ /**
921
+ * Logs an error message to the coordinator and console
922
+ */
923
+ error: async (...args: any[]): Promise<void> => {
924
+ const message = args
925
+ .map((arg) =>
926
+ typeof arg === "object" ? JSON.stringify(arg) : String(arg)
927
+ )
928
+ .join(" ");
929
+
930
+ console.error(...args);
931
+
932
+ try {
933
+ await agentMessage(LogLevel.ERROR, message);
934
+ } catch (error) {
935
+ console.error("Failed to send error message to coordinator:", error);
936
+ }
937
+ },
938
+
939
+ /**
940
+ * Logs a fatal message to the coordinator and console
941
+ */
942
+ fatal: async (...args: any[]): Promise<void> => {
943
+ const message = args
944
+ .map((arg) =>
945
+ typeof arg === "object" ? JSON.stringify(arg) : String(arg)
946
+ )
947
+ .join(" ");
948
+
949
+ console.error(...args);
950
+
951
+ try {
952
+ await agentMessage(LogLevel.FATAL, message);
953
+ } catch (error) {
954
+ console.error("Failed to send fatal message to coordinator:", error);
955
+ }
956
+ },
957
+ };
892
958
 
893
959
  // Re-export types for users to access
894
960
  export type {
@@ -900,6 +966,8 @@ export type {
900
966
  GetBlockResponse,
901
967
  GetBlockSettlementResponse,
902
968
  UpdateBlockSettlementResponse,
969
+ GetSettlementProofResponse,
970
+ SubmitSettlementProofResponse,
903
971
  RejectProofResponse,
904
972
  ProofEventResponse,
905
973
  AgentMessageResponse,