@chipi-stack/backend 14.15.0 → 14.15.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.mjs CHANGED
@@ -3310,9 +3310,13 @@ var ChipiAi = class {
3310
3310
  * session key: a USDC `transfer` to the offer's `payTo`, then the same call
3311
3311
  * again with `X-PAYMENT`. The price must be at most `maxAmount`, `model`
3312
3312
  * must be payable and the input within `maxPromptBytes`, all checked before
3313
- * paying. The session must whitelist `transfer` and cap USDC. A payment
3314
- * that was made but not served is reported in `error.details.xPayment`;
3315
- * pass it back as `options.xPayment` to retry without paying again.
3313
+ * paying. The wallet must be a wallet of the organization that owns the
3314
+ * `sk_`: the first request names it in `X-PAYER`, and when the API answers
3315
+ * that a payment from it would be refused, nothing is paid
3316
+ * (`X402_PAYER_NOT_IN_ORG`). The session must whitelist `transfer` and cap
3317
+ * USDC. A payment that was made but not served is reported in
3318
+ * `error.details.xPayment`; pass it back as `options.xPayment` to retry
3319
+ * without paying again.
3316
3320
  *
3317
3321
  * @example
3318
3322
  * ```typescript
@@ -3324,20 +3328,32 @@ var ChipiAi = class {
3324
3328
  * ```
3325
3329
  */
3326
3330
  async think(params, options = {}) {
3331
+ let payer;
3332
+ if (options.payWithSession && !options.xPayment) {
3333
+ try {
3334
+ payer = padFelt(options.payWithSession.wallet.publicKey);
3335
+ } catch {
3336
+ payer = void 0;
3337
+ }
3338
+ }
3327
3339
  try {
3328
- return await this.postThink(params, options.xPayment);
3340
+ return await this.postThink(params, options.xPayment, payer);
3329
3341
  } catch (err) {
3330
3342
  const offer = options.payWithSession ? sessionTransferOffer(err) : void 0;
3331
3343
  if (!offer || !options.payWithSession) throw err;
3332
3344
  return this.payAndThink(params, offer, options.payWithSession);
3333
3345
  }
3334
3346
  }
3335
- postThink(params, xPayment) {
3347
+ postThink(params, xPayment, payer) {
3348
+ const headers = {
3349
+ ...xPayment ? { "X-PAYMENT": xPayment } : {},
3350
+ ...payer ? { "X-PAYER": payer } : {}
3351
+ };
3336
3352
  return this.client.post({
3337
3353
  endpoint: `${AI_BASE}/think`,
3338
3354
  bearerToken: this.secret(),
3339
3355
  body: params,
3340
- ...xPayment ? { headers: { "X-PAYMENT": xPayment } } : {}
3356
+ ...Object.keys(headers).length > 0 ? { headers } : {}
3341
3357
  });
3342
3358
  }
3343
3359
  /**
@@ -3390,6 +3406,15 @@ var ChipiAi = class {
3390
3406
  { x402: { accepts: [offer] } }
3391
3407
  );
3392
3408
  }
3409
+ const payerCheck = offer.extra?.payer;
3410
+ if (payerCheck?.accepted === false && typeof payerCheck.address === "string" && sameFelt(payerCheck.address, pay.wallet.publicKey)) {
3411
+ throw new ChipiApiError(
3412
+ `Wallet ${pay.wallet.publicKey} is not a wallet of the organization that owns this API key, so the API would refuse its payment; nothing was paid. Pay from a wallet of this organization.`,
3413
+ "X402_PAYER_NOT_IN_ORG",
3414
+ 402,
3415
+ { x402: { accepts: [offer] } }
3416
+ );
3417
+ }
3393
3418
  const payer = pay.wallet.publicKey;
3394
3419
  const mask = (1n << 128n) - 1n;
3395
3420
  const transactionHash = await this.sessions.executeTransactionWithSession(