@agent-fuel/sdk 0.1.0 → 0.1.1

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/dist/index.cjs CHANGED
@@ -187,6 +187,28 @@ var NotWhitelistedError = class extends SpendPolicyError {
187
187
  this.service = service;
188
188
  }
189
189
  };
190
+ var RecordPaymentError = class extends AgentFuelError {
191
+ constructor(message) {
192
+ super(message);
193
+ this.name = "RecordPaymentError";
194
+ }
195
+ };
196
+ var ReceiptAlreadyRecordedError = class extends RecordPaymentError {
197
+ receiptHash;
198
+ constructor(receiptHash) {
199
+ super("payment receipt was already recorded on chain");
200
+ this.name = "ReceiptAlreadyRecordedError";
201
+ this.receiptHash = receiptHash;
202
+ }
203
+ };
204
+ var ServiceInactiveError = class extends RecordPaymentError {
205
+ service;
206
+ constructor(service) {
207
+ super(`service ${service} is paused; record_payment rejected`);
208
+ this.name = "ServiceInactiveError";
209
+ this.service = service;
210
+ }
211
+ };
190
212
 
191
213
  // src/guardrails.ts
192
214
  function guardSpend(args) {
@@ -294,6 +316,12 @@ function wsUrl(apiBase, path) {
294
316
  if (apiBase.startsWith("http://")) return apiBase.replace(/^http/, "ws") + path;
295
317
  return apiBase + path;
296
318
  }
319
+ function subscribeService(apiBase, servicePubkey, opts) {
320
+ return subscribe(wsUrl(apiBase, `/ws/services/${servicePubkey}`), opts);
321
+ }
322
+ function subscribeVault(apiBase, vaultPubkey, opts) {
323
+ return subscribe(wsUrl(apiBase, `/ws/vaults/${vaultPubkey}`), opts);
324
+ }
297
325
 
298
326
  // src/idl/reputation.json
299
327
  var reputation_default = {
@@ -849,10 +877,14 @@ var reputation_default = {
849
877
  ],
850
878
  accounts: [
851
879
  {
852
- name: "service",
880
+ name: "sponsor",
853
881
  writable: true,
854
882
  signer: true
855
883
  },
884
+ {
885
+ name: "service",
886
+ signer: true
887
+ },
856
888
  {
857
889
  name: "service_registry",
858
890
  writable: true,
@@ -899,6 +931,15 @@ var reputation_default = {
899
931
  name: "ServiceCategory"
900
932
  }
901
933
  }
934
+ },
935
+ {
936
+ name: "service_uri",
937
+ type: {
938
+ array: [
939
+ "u8",
940
+ 128
941
+ ]
942
+ }
902
943
  }
903
944
  ]
904
945
  },
@@ -1014,6 +1055,61 @@ var reputation_default = {
1014
1055
  }
1015
1056
  }
1016
1057
  ]
1058
+ },
1059
+ {
1060
+ name: "set_service_active",
1061
+ discriminator: [
1062
+ 221,
1063
+ 172,
1064
+ 149,
1065
+ 49,
1066
+ 212,
1067
+ 184,
1068
+ 77,
1069
+ 101
1070
+ ],
1071
+ accounts: [
1072
+ {
1073
+ name: "service",
1074
+ signer: true
1075
+ },
1076
+ {
1077
+ name: "service_registry",
1078
+ writable: true,
1079
+ pda: {
1080
+ seeds: [
1081
+ {
1082
+ kind: "const",
1083
+ value: [
1084
+ 115,
1085
+ 101,
1086
+ 114,
1087
+ 118,
1088
+ 105,
1089
+ 99,
1090
+ 101
1091
+ ]
1092
+ },
1093
+ {
1094
+ kind: "account",
1095
+ path: "service"
1096
+ }
1097
+ ]
1098
+ }
1099
+ },
1100
+ {
1101
+ name: "authority",
1102
+ relations: [
1103
+ "service_registry"
1104
+ ]
1105
+ }
1106
+ ],
1107
+ args: [
1108
+ {
1109
+ name: "active",
1110
+ type: "bool"
1111
+ }
1112
+ ]
1017
1113
  }
1018
1114
  ],
1019
1115
  accounts: [
@@ -1162,6 +1258,19 @@ var reputation_default = {
1162
1258
  109
1163
1259
  ]
1164
1260
  },
1261
+ {
1262
+ name: "ServiceActiveSet",
1263
+ discriminator: [
1264
+ 151,
1265
+ 122,
1266
+ 7,
1267
+ 40,
1268
+ 191,
1269
+ 139,
1270
+ 237,
1271
+ 52
1272
+ ]
1273
+ },
1165
1274
  {
1166
1275
  name: "ServiceRegistered",
1167
1276
  discriminator: [
@@ -1226,6 +1335,11 @@ var reputation_default = {
1226
1335
  code: 6009,
1227
1336
  name: "FeedbackRateLimited",
1228
1337
  msg: "Feedback rate limit not yet expired for this (service, agent) pair"
1338
+ },
1339
+ {
1340
+ code: 6010,
1341
+ name: "UnauthorizedService",
1342
+ msg: "Only the service authority may perform this action"
1229
1343
  }
1230
1344
  ],
1231
1345
  types: [
@@ -1666,6 +1780,26 @@ var reputation_default = {
1666
1780
  ]
1667
1781
  }
1668
1782
  },
1783
+ {
1784
+ name: "ServiceActiveSet",
1785
+ type: {
1786
+ kind: "struct",
1787
+ fields: [
1788
+ {
1789
+ name: "service",
1790
+ type: "pubkey"
1791
+ },
1792
+ {
1793
+ name: "active",
1794
+ type: "bool"
1795
+ },
1796
+ {
1797
+ name: "slot",
1798
+ type: "u64"
1799
+ }
1800
+ ]
1801
+ }
1802
+ },
1669
1803
  {
1670
1804
  name: "ServiceCategory",
1671
1805
  type: {
@@ -1698,6 +1832,14 @@ var reputation_default = {
1698
1832
  name: "service",
1699
1833
  type: "pubkey"
1700
1834
  },
1835
+ {
1836
+ name: "sponsor",
1837
+ docs: [
1838
+ 'Wallet that paid rent. Lets us answer "who registered this service?"',
1839
+ "without an extra column on the on-chain account."
1840
+ ],
1841
+ type: "pubkey"
1842
+ },
1701
1843
  {
1702
1844
  name: "name",
1703
1845
  type: {
@@ -1740,6 +1882,19 @@ var reputation_default = {
1740
1882
  ]
1741
1883
  }
1742
1884
  },
1885
+ {
1886
+ name: "service_uri",
1887
+ docs: [
1888
+ "Off-chain metadata URI (pricing, docs, endpoint, logo). Padded to 128 bytes;",
1889
+ "trailing NULs are trimmed when read. Mirrors `AgentProfile::agent_uri`."
1890
+ ],
1891
+ type: {
1892
+ array: [
1893
+ "u8",
1894
+ 128
1895
+ ]
1896
+ }
1897
+ },
1743
1898
  {
1744
1899
  name: "category",
1745
1900
  type: {
@@ -3096,6 +3251,34 @@ function serviceRegistryPda(serviceAuthority) {
3096
3251
  );
3097
3252
  return pda;
3098
3253
  }
3254
+ function agentProfilePda(agent) {
3255
+ const [pda] = web3_js.PublicKey.findProgramAddressSync(
3256
+ [Buffer.from("agent"), toPubkey(agent).toBuffer()],
3257
+ PROGRAM_IDS.reputation
3258
+ );
3259
+ return pda;
3260
+ }
3261
+ function agentServiceLinkPda(agentProfile, serviceRegistry) {
3262
+ const [pda] = web3_js.PublicKey.findProgramAddressSync(
3263
+ [
3264
+ Buffer.from("link"),
3265
+ toPubkey(agentProfile).toBuffer(),
3266
+ toPubkey(serviceRegistry).toBuffer()
3267
+ ],
3268
+ PROGRAM_IDS.reputation
3269
+ );
3270
+ return pda;
3271
+ }
3272
+ function receiptUsedPda(receiptHash) {
3273
+ if (receiptHash.length !== 32) {
3274
+ throw new Error(`receiptUsedPda expects a 32-byte hash, got ${receiptHash.length}`);
3275
+ }
3276
+ const [pda] = web3_js.PublicKey.findProgramAddressSync(
3277
+ [Buffer.from("receipt"), Buffer.from(receiptHash)],
3278
+ PROGRAM_IDS.reputation
3279
+ );
3280
+ return pda;
3281
+ }
3099
3282
  function buildProvider(connection, keypair) {
3100
3283
  return new anchor.AnchorProvider(connection, new anchor.Wallet(keypair), anchor.AnchorProvider.defaultOptions());
3101
3284
  }
@@ -3204,6 +3387,21 @@ var AgentFuel = class {
3204
3387
  throw mapSpendError(err, { service, amountUsdc, vault, policy });
3205
3388
  }
3206
3389
  }
3390
+ /// Triggers an on-chain recomputation of the agent's reputation score.
3391
+ /// Permissionless — any signer can call it; we use the agent itself since
3392
+ /// the SDK already has its keypair available and it already pays tx fees
3393
+ /// for spends. The instruction reads the agent's already-public counters
3394
+ /// (volume, diversity, streak, tenure, feedback), recalculates the score
3395
+ /// to a single u16 ≤ 1000, and emits a `ScoreComputed` event. The webhook
3396
+ /// picks that up; the next `getScore()` returns the fresh value.
3397
+ async computeScore() {
3398
+ const profile = agentProfilePda(this.agentPubkey);
3399
+ const signature = await this.reputation.methods.computeScore().accounts({
3400
+ caller: this.agentPubkey,
3401
+ agentProfile: profile
3402
+ }).signers([this.agent]).rpc();
3403
+ return { signature };
3404
+ }
3207
3405
  onEvent(callback, options) {
3208
3406
  const target = options?.agent ? toPubkey(options.agent) : this.agentPubkey;
3209
3407
  const url = wsUrl(this.apiBase, `/ws/agents/${target.toBase58()}`);
@@ -3248,6 +3446,56 @@ async function safeText(res) {
3248
3446
  return void 0;
3249
3447
  }
3250
3448
  }
3449
+ async function recordPayment(args) {
3450
+ const { service, connection, amountUsdc } = args;
3451
+ const agentPubkey = toPubkey(args.agent);
3452
+ if (amountUsdc <= 0) {
3453
+ throw new Error("recordPayment: amountUsdc must be > 0");
3454
+ }
3455
+ if (args.receiptHash.length !== 32) {
3456
+ throw new Error(
3457
+ `recordPayment: receiptHash must be 32 bytes, got ${args.receiptHash.length}`
3458
+ );
3459
+ }
3460
+ const provider = buildProvider(connection, service);
3461
+ const program = reputationProgram(provider);
3462
+ const agentProfile = agentProfilePda(agentPubkey);
3463
+ const serviceRegistry = serviceRegistryPda(service.publicKey);
3464
+ const link = agentServiceLinkPda(agentProfile, serviceRegistry);
3465
+ const receipt = receiptUsedPda(args.receiptHash);
3466
+ try {
3467
+ const signature = await program.methods.recordPayment(new anchor.BN(amountUsdc), args.receiptHash).accounts({
3468
+ service: service.publicKey,
3469
+ agentProfile,
3470
+ serviceRegistry,
3471
+ agentServiceLink: link,
3472
+ receiptUsed: receipt,
3473
+ systemProgram: web3_js.SystemProgram.programId
3474
+ }).signers([service]).rpc();
3475
+ return { signature };
3476
+ } catch (err) {
3477
+ throw mapRecordPaymentError(err, args.receiptHash);
3478
+ }
3479
+ }
3480
+ function mapRecordPaymentError(err, receiptHash) {
3481
+ const message = err instanceof Error ? err.message : String(err);
3482
+ if (/already in use/i.test(message)) {
3483
+ return new ReceiptAlreadyRecordedError(receiptHash);
3484
+ }
3485
+ if (!(err instanceof anchor.AnchorError)) return err;
3486
+ switch (err.error.errorCode.number) {
3487
+ case 6e3:
3488
+ return new RecordPaymentError("counter overflow on chain");
3489
+ case 6001:
3490
+ return new ServiceInactiveError(
3491
+ err.error.origin?.toString() ?? "<unknown service>"
3492
+ );
3493
+ case 6002:
3494
+ return new ZeroAmountError();
3495
+ default:
3496
+ return err;
3497
+ }
3498
+ }
3251
3499
 
3252
3500
  // src/x402.ts
3253
3501
  var HEADER = "X-Payment-Required";
@@ -3346,16 +3594,25 @@ exports.OwnerNotConfiguredError = OwnerNotConfiguredError;
3346
3594
  exports.PROGRAM_IDS = PROGRAM_IDS;
3347
3595
  exports.PaymentParseError = PaymentParseError;
3348
3596
  exports.PerTxLimitExceededError = PerTxLimitExceededError;
3597
+ exports.ReceiptAlreadyRecordedError = ReceiptAlreadyRecordedError;
3598
+ exports.RecordPaymentError = RecordPaymentError;
3349
3599
  exports.SLOTS_PER_HOUR = SLOTS_PER_HOUR;
3600
+ exports.ServiceInactiveError = ServiceInactiveError;
3350
3601
  exports.SpendPolicyError = SpendPolicyError;
3351
3602
  exports.TOKEN_PROGRAM_ID = TOKEN_PROGRAM_ID;
3352
3603
  exports.VaultFrozenError = VaultFrozenError;
3353
3604
  exports.ZeroAmountError = ZeroAmountError;
3605
+ exports.agentProfilePda = agentProfilePda;
3606
+ exports.agentServiceLinkPda = agentServiceLinkPda;
3354
3607
  exports.createAssociatedTokenAccountIdempotentInstruction = createAssociatedTokenAccountIdempotentInstruction;
3355
3608
  exports.getAssociatedTokenAddress = getAssociatedTokenAddress;
3356
3609
  exports.paymentRequired = paymentRequired;
3357
3610
  exports.policyPda = policyPda;
3611
+ exports.receiptUsedPda = receiptUsedPda;
3612
+ exports.recordPayment = recordPayment;
3358
3613
  exports.serviceRegistryPda = serviceRegistryPda;
3614
+ exports.subscribeService = subscribeService;
3615
+ exports.subscribeVault = subscribeVault;
3359
3616
  exports.vaultPda = vaultPda;
3360
3617
  //# sourceMappingURL=index.cjs.map
3361
3618
  //# sourceMappingURL=index.cjs.map