@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.d.mts CHANGED
@@ -649,9 +649,13 @@ declare class ChipiAi {
649
649
  * session key: a USDC `transfer` to the offer's `payTo`, then the same call
650
650
  * again with `X-PAYMENT`. The price must be at most `maxAmount`, `model`
651
651
  * must be payable and the input within `maxPromptBytes`, all checked before
652
- * paying. The session must whitelist `transfer` and cap USDC. A payment
653
- * that was made but not served is reported in `error.details.xPayment`;
654
- * pass it back as `options.xPayment` to retry without paying again.
652
+ * paying. The wallet must be a wallet of the organization that owns the
653
+ * `sk_`: the first request names it in `X-PAYER`, and when the API answers
654
+ * that a payment from it would be refused, nothing is paid
655
+ * (`X402_PAYER_NOT_IN_ORG`). The session must whitelist `transfer` and cap
656
+ * USDC. A payment that was made but not served is reported in
657
+ * `error.details.xPayment`; pass it back as `options.xPayment` to retry
658
+ * without paying again.
655
659
  *
656
660
  * @example
657
661
  * ```typescript
package/dist/index.d.ts CHANGED
@@ -649,9 +649,13 @@ declare class ChipiAi {
649
649
  * session key: a USDC `transfer` to the offer's `payTo`, then the same call
650
650
  * again with `X-PAYMENT`. The price must be at most `maxAmount`, `model`
651
651
  * must be payable and the input within `maxPromptBytes`, all checked before
652
- * paying. The session must whitelist `transfer` and cap USDC. A payment
653
- * that was made but not served is reported in `error.details.xPayment`;
654
- * pass it back as `options.xPayment` to retry without paying again.
652
+ * paying. The wallet must be a wallet of the organization that owns the
653
+ * `sk_`: the first request names it in `X-PAYER`, and when the API answers
654
+ * that a payment from it would be refused, nothing is paid
655
+ * (`X402_PAYER_NOT_IN_ORG`). The session must whitelist `transfer` and cap
656
+ * USDC. A payment that was made but not served is reported in
657
+ * `error.details.xPayment`; pass it back as `options.xPayment` to retry
658
+ * without paying again.
655
659
  *
656
660
  * @example
657
661
  * ```typescript
package/dist/index.js CHANGED
@@ -3315,9 +3315,13 @@ var ChipiAi = class {
3315
3315
  * session key: a USDC `transfer` to the offer's `payTo`, then the same call
3316
3316
  * again with `X-PAYMENT`. The price must be at most `maxAmount`, `model`
3317
3317
  * must be payable and the input within `maxPromptBytes`, all checked before
3318
- * paying. The session must whitelist `transfer` and cap USDC. A payment
3319
- * that was made but not served is reported in `error.details.xPayment`;
3320
- * pass it back as `options.xPayment` to retry without paying again.
3318
+ * paying. The wallet must be a wallet of the organization that owns the
3319
+ * `sk_`: the first request names it in `X-PAYER`, and when the API answers
3320
+ * that a payment from it would be refused, nothing is paid
3321
+ * (`X402_PAYER_NOT_IN_ORG`). The session must whitelist `transfer` and cap
3322
+ * USDC. A payment that was made but not served is reported in
3323
+ * `error.details.xPayment`; pass it back as `options.xPayment` to retry
3324
+ * without paying again.
3321
3325
  *
3322
3326
  * @example
3323
3327
  * ```typescript
@@ -3329,20 +3333,32 @@ var ChipiAi = class {
3329
3333
  * ```
3330
3334
  */
3331
3335
  async think(params, options = {}) {
3336
+ let payer;
3337
+ if (options.payWithSession && !options.xPayment) {
3338
+ try {
3339
+ payer = padFelt(options.payWithSession.wallet.publicKey);
3340
+ } catch {
3341
+ payer = void 0;
3342
+ }
3343
+ }
3332
3344
  try {
3333
- return await this.postThink(params, options.xPayment);
3345
+ return await this.postThink(params, options.xPayment, payer);
3334
3346
  } catch (err) {
3335
3347
  const offer = options.payWithSession ? sessionTransferOffer(err) : void 0;
3336
3348
  if (!offer || !options.payWithSession) throw err;
3337
3349
  return this.payAndThink(params, offer, options.payWithSession);
3338
3350
  }
3339
3351
  }
3340
- postThink(params, xPayment) {
3352
+ postThink(params, xPayment, payer) {
3353
+ const headers = {
3354
+ ...xPayment ? { "X-PAYMENT": xPayment } : {},
3355
+ ...payer ? { "X-PAYER": payer } : {}
3356
+ };
3341
3357
  return this.client.post({
3342
3358
  endpoint: `${AI_BASE}/think`,
3343
3359
  bearerToken: this.secret(),
3344
3360
  body: params,
3345
- ...xPayment ? { headers: { "X-PAYMENT": xPayment } } : {}
3361
+ ...Object.keys(headers).length > 0 ? { headers } : {}
3346
3362
  });
3347
3363
  }
3348
3364
  /**
@@ -3395,6 +3411,15 @@ var ChipiAi = class {
3395
3411
  { x402: { accepts: [offer] } }
3396
3412
  );
3397
3413
  }
3414
+ const payerCheck = offer.extra?.payer;
3415
+ if (payerCheck?.accepted === false && typeof payerCheck.address === "string" && sameFelt(payerCheck.address, pay.wallet.publicKey)) {
3416
+ throw new shared.ChipiApiError(
3417
+ `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.`,
3418
+ "X402_PAYER_NOT_IN_ORG",
3419
+ 402,
3420
+ { x402: { accepts: [offer] } }
3421
+ );
3422
+ }
3398
3423
  const payer = pay.wallet.publicKey;
3399
3424
  const mask = (1n << 128n) - 1n;
3400
3425
  const transactionHash = await this.sessions.executeTransactionWithSession(