askexperts
Version:
AskExperts SDK: build and use AI experts - ask them questions and pay with bitcoin on an open protocol
159 lines • 7.29 kB
JavaScript
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _AskExpertsPayingClient_paymentManager, _AskExpertsPayingClient_maxAmountSats, _AskExpertsPayingClient_onPaid;
import { AskExpertsClient } from "./AskExpertsClient.js";
import { parseBolt11 } from "../common/bolt11.js";
import { debugError } from "../common/debug.js";
/**
* AskExpertsPayingClient class that extends AskExpertsClient with payment capabilities
* This class abstracts payment handling logic that was duplicated in AskExpertsSmartClient and AskExpertsChatClient
*/
export class AskExpertsPayingClient extends AskExpertsClient {
/**
* Creates a new AskExpertsPayingClient instance
*
* @param paymentManager - Payment manager for handling payments
* @param options - Optional configuration
* @param options.maxAmountSats - Maximum amount to pay in satoshis
* @param options.discoveryRelays - Array of discovery relay URLs to use as fallback
*/
constructor(paymentManager, options) {
// Create the client with callbacks for quotes and payments
super({
discoveryRelays: options?.discoveryRelays,
onQuote: (quote, prompt) => this.handleQuote(quote, prompt),
onPay: (quote, prompt) => this.handlePayment(quote, prompt),
});
_AskExpertsPayingClient_paymentManager.set(this, void 0);
_AskExpertsPayingClient_maxAmountSats.set(this, 100); // Default max amount
_AskExpertsPayingClient_onPaid.set(this, void 0);
if (!paymentManager) {
throw new Error("Payment manager is required");
}
__classPrivateFieldSet(this, _AskExpertsPayingClient_paymentManager, paymentManager, "f");
// Set max amount if provided
if (options?.maxAmountSats) {
__classPrivateFieldSet(this, _AskExpertsPayingClient_maxAmountSats, options.maxAmountSats, "f");
}
// Set onPaid callback if provided
if (options?.onPaid) {
__classPrivateFieldSet(this, _AskExpertsPayingClient_onPaid, options.onPaid, "f");
}
}
/**
* Sets the maximum amount to pay in satoshis
*
* @param maxAmountSats - Maximum amount to pay in satoshis
*/
/**
* Gets the maximum amount to pay in satoshis
*/
get maxAmountSats() {
return __classPrivateFieldGet(this, _AskExpertsPayingClient_maxAmountSats, "f");
}
/**
* Sets the maximum amount to pay in satoshis
*/
set maxAmountSats(value) {
if (value <= 0) {
throw new Error("Maximum amount must be greater than zero");
}
__classPrivateFieldSet(this, _AskExpertsPayingClient_maxAmountSats, value, "f");
}
/**
* Gets the current onPaid callback function
*/
get onPaid() {
return __classPrivateFieldGet(this, _AskExpertsPayingClient_onPaid, "f");
}
/**
* Sets the onPaid callback function
*/
set onPaid(callback) {
__classPrivateFieldSet(this, _AskExpertsPayingClient_onPaid, callback, "f");
}
/**
* Handles quote events from experts
*
* @param quote - Quote from expert
* @param prompt - Prompt sent to expert
* @returns Promise resolving to boolean indicating whether to proceed with payment
* @protected
*/
async handleQuote(quote, prompt) {
// Check if there's a lightning invoice
const lightningInvoice = quote.invoices.find((inv) => inv.method === "lightning");
if (!lightningInvoice || !lightningInvoice.invoice) {
debugError("No lightning invoice found in quote");
return false;
}
// Parse the invoice to get the amount
try {
const { amount_sats } = parseBolt11(lightningInvoice.invoice);
// Check if the amount is within the max amount
if (amount_sats <= __classPrivateFieldGet(this, _AskExpertsPayingClient_maxAmountSats, "f")) {
return true;
}
else {
debugError(`Invoice amount (${amount_sats}) exceeds max amount (${__classPrivateFieldGet(this, _AskExpertsPayingClient_maxAmountSats, "f")})`);
return false;
}
}
catch (error) {
debugError("Failed to parse invoice:", error);
return false;
}
}
/**
* Handles payment for quotes
*
* @param quote - Quote from expert
* @param prompt - Prompt sent to expert
* @returns Promise resolving to Proof object
* @protected
*/
async handlePayment(quote, prompt) {
// Find the lightning invoice
const lightningInvoice = quote.invoices.find((inv) => inv.method === "lightning");
if (!lightningInvoice || !lightningInvoice.invoice) {
throw new Error("No lightning invoice found in quote");
}
try {
// Pay the invoice using the payment manager
const preimage = await __classPrivateFieldGet(this, _AskExpertsPayingClient_paymentManager, "f").payInvoice(lightningInvoice.invoice);
// Create the proof object
const proof = {
method: "lightning",
preimage,
};
// Call the onPaid callback if it exists
if (__classPrivateFieldGet(this, _AskExpertsPayingClient_onPaid, "f")) {
await __classPrivateFieldGet(this, _AskExpertsPayingClient_onPaid, "f").call(this, prompt, quote, proof);
}
// Return the proof
return proof;
}
catch (error) {
throw new Error(`Payment failed: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Disposes of resources when the client is no longer needed
*/
[(_AskExpertsPayingClient_paymentManager = new WeakMap(), _AskExpertsPayingClient_maxAmountSats = new WeakMap(), _AskExpertsPayingClient_onPaid = new WeakMap(), Symbol.dispose)]() {
// Dispose of the base client
super[Symbol.dispose]();
// Dispose of the payment manager
__classPrivateFieldGet(this, _AskExpertsPayingClient_paymentManager, "f")[Symbol.dispose]();
}
}
//# sourceMappingURL=AskExpertsPayingClient.js.map