UNPKG

askexperts

Version:

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

167 lines (166 loc) 5.61 kB
/** * AskExpertsClient implementation for NIP-174 * Works in both browser and Node.js environments */ import { SimplePool } from "nostr-tools"; import { FindExpertsParams, FetchExpertsParams, AskExpertParams, Bid, Expert, Quote, Replies, OnQuoteCallback, OnPayCallback } from "../common/types.js"; import { StreamFactory } from "../stream/index.js"; /** * AskExpertsClient class for NIP-174 protocol */ import { AskExpertsClientInterface } from "./AskExpertsClientInterface.js"; export declare class AskExpertsClient implements AskExpertsClientInterface { /** * Zod schema for quote payload */ private quotePayloadSchema; /** * Zod schema for reply payload */ /** * Zod schema for reply payload according to NIP-174 * A reply payload should have either an "error" field or a "content" field, but not both */ private replyPayloadSchema; /** * Default onQuote callback */ private defaultOnQuote?; /** * Default onPay callback */ private defaultOnPay?; /** * StreamFactory instance for creating stream readers and writers */ private streamFactory; /** * SimplePool instance for relay operations */ private pool; /** * Flag indicating whether the pool was created internally */ private poolCreatedInternally; /** * Array of discovery relay URLs to use as fallback */ private discoveryRelays?; /** * Creates a new AskExpertsClient instance * * @param options - Optional configuration * @param options.onQuote - Default callback for handling quotes * @param options.onPay - Default callback for handling payments * @param options.compression - Custom compression implementation * @param options.pool - SimplePool instance for relay operations * @param options.discoveryRelays - Array of discovery relay URLs to use as fallback */ constructor(options?: { onQuote?: OnQuoteCallback; onPay?: OnPayCallback; streamFactory?: StreamFactory; pool?: SimplePool; discoveryRelays?: string[]; }); /** * Disposes of resources when the client is no longer needed */ [Symbol.dispose](): void; /** * Finds experts by publishing an ask event and collecting bids * * @param params - Parameters for finding experts * @returns Promise resolving to array of Bid objects */ findExperts(params: FindExpertsParams): Promise<Bid[]>; /** * Fetches expert profiles from relays * * @param params - Parameters for fetching expert profiles * @returns Promise resolving to array of Expert objects */ fetchExperts(params: FetchExpertsParams): Promise<Expert[]>; /** * Helper function to calculate the size of content in bytes * * @param content - The content to measure * @returns Size in bytes */ private getContentSizeBytes; /** * Sends a prompt to an expert * * @param expertPubkey - Expert's public key * @param expertRelays - Expert's relays * @param content - Content of the prompt * @param format - Format of the prompt * @param compr - Compression method to use * @param promptPrivkey - Private key for the prompt * @returns Promise resolving to a Prompt object * @private */ private sendPrompt; /** * Fetches a quote from an expert * * @param promptId - Prompt event ID * @param expertPubkey - Expert's public key * @param promptPrivkey - Private key used for the prompt * @param publishedRelays - Relays where the prompt was published * @returns Promise resolving to Quote object * @private */ private fetchQuote; /** * Sends an error proof to an expert and throws an error * * @param errorMessage - Error message * @param expertPubkey - Expert's public key * @param promptId - Prompt event ID * @param promptPrivkey - Private key used for the prompt * @param publishedRelays - Relays where the prompt was published * @param error - Error to throw * @throws The provided error * @private */ private sendErrorProof; /** * Sends a proof to an expert * * @param proof - Proof object * @param expertPubkey - Expert's public key * @param promptId - Prompt event ID * @param promptPrivkey - Private key used for the prompt * @param publishedRelays - Relays where the prompt was published * @returns Promise resolving to published relays * @private */ private sendProof; /** * Creates a Replies object that handles reply events * * @param prompt - Prompt * @param expertPubkey - Expert's public key * @param promptPrivkey - Private key used for the prompt * @param publishedRelays - Relays where the prompt was published * @param compression - Compression instance * @returns Replies object * @private */ private createRepliesHandler; /** * Asks an expert a question and receives replies * * @param params - Parameters for asking an expert * @returns Promise resolving to Replies object */ askExpert(params: AskExpertParams): Promise<Replies>; /** * Validates a quote by checking that all lightning invoices have matching amounts * * @param quote - The quote to validate * @throws PaymentRejectedError if any invoice amount doesn't match the expected amount */ validateQuote(quote: Quote): void; }