UNPKG

askexperts

Version:

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

208 lines (207 loc) 6.19 kB
/** * Expert implementation for NIP-174 * Server-side component that handles asks and prompts */ import { SimplePool } from "nostr-tools"; import { PromptFormat, PaymentMethod, OnAskCallback, OnPromptCallback, OnProofCallback } from "../common/types.js"; import { StreamFactory } from "../stream/index.js"; /** * Expert class for NIP-174 protocol * Handles asks and prompts from clients */ import { AskExpertsServerBaseInterface } from "./AskExpertsServerBaseInterface.js"; export declare class AskExpertsServerBase implements AskExpertsServerBaseInterface { #private; /** * Expert's public key */ readonly pubkey: string; /** * SimplePool instance for relay operations */ readonly pool: SimplePool; /** * Active ask subscription */ private askSub?; /** * Active prompt subscription */ private promptSub?; /** * Timer for periodic profile republishing */ private profileRepublishTimer; /** * Flag to indicate that profile needs to be republished */ private scheduledPublishProfile; /** * Flag to indicate that ask subscription needs to be updated */ private scheduledSubscribeToAsks; /** * Flag to signal that start was called */ private started; /** * Schedules the profile for republishing */ private schedulePublishProfile; /** * Schedules the ask subscription for updating */ private scheduleSubscribeToAsks; /** * Checks if ask subscription needs to be updated and does so if necessary */ private maybeSubscribeToAsks; /** * Creates a new Expert instance * * @param options - Configuration options * @param options.privkey - Expert's private key (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.paymentMethods - Payment methods supported by the expert * @param options.onAsk - Callback for handling asks * @param options.onPrompt - Callback for handling prompts * @param options.onProof - Callback for handling proofs and executing prompts * @param options.pool - SimplePool instance for relay operations */ constructor(options: { privkey: Uint8Array; discoveryRelays?: string[]; promptRelays?: string[]; hashtags?: string[]; formats?: PromptFormat[]; onAsk?: OnAskCallback; onPrompt?: OnPromptCallback; onProof?: OnProofCallback; paymentMethods?: PaymentMethod[]; pool: SimplePool; streamFactory?: StreamFactory; nickname?: string; description?: string; }); get nickname(): string; set nickname(value: string); get description(): string; set description(value: string); get discoveryRelays(): string[]; set discoveryRelays(value: string[]); get promptRelays(): string[]; set promptRelays(value: string[]); get hashtags(): string[]; set hashtags(value: string[]); get formats(): PromptFormat[]; set formats(value: PromptFormat[]); get paymentMethods(): PaymentMethod[]; set paymentMethods(value: PaymentMethod[]); get onAsk(): OnAskCallback | undefined; set onAsk(value: OnAskCallback | undefined); get onPrompt(): OnPromptCallback | undefined; set onPrompt(value: OnPromptCallback | undefined); get onProof(): OnProofCallback | undefined; set onProof(value: OnProofCallback | undefined); get streamFactory(): StreamFactory; set streamFactory(value: StreamFactory); /** * Starts the expert by subscribing to asks and prompts */ start(): Promise<void>; /** * Sets up periodic republishing of expert profile */ private setupProfileRepublishing; /** * Publishes the expert profile to discovery relays */ private publishExpertProfile; /** * Checks if profile needs to be republished and does so if necessary */ private maybeRepublishProfile; /** * Subscribes to ask events on discovery relays */ private subscribeToAsks; /** * Subscribes to prompt events on prompt relays */ private subscribeToPrompts; /** * Handles an ask event * * @param askEvent - The ask event */ private handleAskEvent; /** * Sends a bid in response to an ask * * @param ask - The ask * @param bid - The bid */ private sendBid; /** * Handles a prompt event * * @param promptEvent - The prompt event */ private handlePromptEvent; /** * Sends a quote in response to a prompt * * @param prompt - The prompt * @param quote - The quote */ private sendQuote; /** * Sends an error quote in response to a prompt * * @param prompt - The prompt * @param errorMessage - The error message */ private sendErrorQuote; /** * Handles a proof event * * @param proofEvent - The proof event * @param prompt - The prompt */ private handleProofEvent; /** * Sends an expert reply to a prompt * * @param prompt - The prompt * @param content - Content to send * @param error - Error to send */ private sendExpertReply; /** * Sends expert replies to a prompt * * @param prompt - The prompt * @param expertReplies - The expert replies */ /** * Helper function to calculate the size of content in bytes * * @param content - The content to measure * @returns Size in bytes */ private getContentSizeBytes; /** * Streams expert replies using a single reply event with stream metadata * * @param prompt - The prompt * @param expertReplies - The expert replies */ private streamExpertReplies; /** * Disposes of resources when the expert is no longer needed */ [Symbol.asyncDispose](): Promise<void>; }