UNPKG

askexperts

Version:

AskExperts SDK: build and use AI experts - ask them questions and pay with bitcoin on an open protocol

92 lines (91 loc) 3.6 kB
/** * Expert implementation for NIP-174 with payment handling * Extends the base server with payment functionality */ import { SimplePool } from "nostr-tools"; import { AskExpertsServerBase } from "./AskExpertsServerBase.js"; import { ExpertPaymentManager } from "../payments/ExpertPaymentManager.js"; import { OnPromptPriceCallback, OnPromptPaidCallback } from "../common/types.js"; import { StreamFactory } from "../stream/index.js"; /** * Expert server with payment handling * Extends the base server with payment functionality */ import { AskExpertsServerInterface } from "./AskExpertsServerInterface.js"; export declare class AskExpertsServer extends AskExpertsServerBase implements AskExpertsServerInterface { #private; /** * Payment manager for handling expert payments */ private paymentManager; /** * Default expiry time for invoices in seconds */ private readonly DEFAULT_EXPIRY_SEC; /** * Creates a new AskExpertsServer instance * * @param options - Configuration options * @param options.privkey - Expert's private key (required) * @param options.paymentManager - Payment manager for handling expert payments (required) * @param options.discoveryRelays - Relays for discovery phase * @param options.promptRelays - Relays for prompt phase * @param options.hashtags - Hashtags the expert is interested in * @param options.formats - Formats supported by the expert * @param options.streamFactory - StreamFactory for creating stream readers and writers * @param options.paymentMethods - Payment methods supported by the expert * @param options.onAsk - Callback for handling asks * @param options.onPromptPrice - Callback for determining prompt prices * @param options.onPromptPaid - Callback for handling paid prompts * @param options.pool - SimplePool instance for relay operations * @param options.streamFactory - Custom StreamFactory implementation */ constructor(options: { privkey: Uint8Array; paymentManager: ExpertPaymentManager; discoveryRelays?: string[]; promptRelays?: string[]; hashtags?: string[]; formats?: string[]; onAsk?: (ask: any) => Promise<any>; onPromptPrice?: OnPromptPriceCallback; onPromptPaid?: OnPromptPaidCallback; paymentMethods?: string[]; pool: SimplePool; streamFactory?: StreamFactory; nickname?: string; description?: string; }); /** * Gets the callback for determining prompt prices */ get onPromptPrice(): OnPromptPriceCallback | undefined; /** * Sets the callback for determining prompt prices */ set onPromptPrice(value: OnPromptPriceCallback | undefined); /** * Gets the callback for handling paid prompts */ get onPromptPaid(): OnPromptPaidCallback | undefined; /** * Sets the callback for handling paid prompts */ set onPromptPaid(value: OnPromptPaidCallback | undefined); /** * Custom prompt handler that determines price and creates invoices * * @param prompt - The prompt to handle * @returns Promise resolving to an ExpertQuote */ private handlePrompt; /** * Custom proof handler that verifies payment and processes the prompt * * @param prompt - The prompt to handle * @param quote - The expert quote containing invoices * @param proof - The payment proof * @returns Promise resolving to ExpertReplies or ExpertReply */ private handleProof; }