UNPKG

askexperts

Version:

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

161 lines (160 loc) 5.95 kB
import { Doc, DocStore, DocStoreClient, Subscription } from './interfaces.js'; /** * Configuration options for DocStoreWebSocketClient */ export interface DocStoreWebSocketClientOptions { /** WebSocket URL to connect to */ url: string; /** Optional private key for authentication (as Uint8Array) */ privateKey?: Uint8Array; /** Optional token for bearer authentication */ token?: string; /** Optional WebSocket instance to use instead of creating a new one */ webSocket?: WebSocket; } /** * WebSocket client for DocStoreSQLiteServer * Implements the DocStoreClient interface */ export declare class DocStoreWebSocketClient implements DocStoreClient { private ws; private messageCallbacks; private subscriptionCallbacks; private docBuffers; private processingSubscriptions; private connected; private connectPromise; private connectResolve; private connectReject; private privateKey?; private token?; private url; private authenticated; private messageHandler; private openHandler; private closeHandler; private errorHandler; /** * Creates a new DocStoreWebSocketClient * @param options - Configuration options for the client */ constructor(options: DocStoreWebSocketClientOptions | string); /** * Send authentication message after connection * @returns Promise that resolves when authentication is successful */ private sendAuthMessage; /** * Wait for the connection to be established * @returns Promise that resolves when connected */ waitForConnection(): Promise<void>; /** * Handle incoming WebSocket messages * @param message - Parsed message */ private handleMessage; /** * Send a message to the server and wait for a response * @param type - Message type * @param method - Method name * @param params - Method parameters * @returns Promise that resolves with the response */ private sendAndWait; /** * Subscribe to documents in a docstore * @param options - Subscription options * @param onDoc - Callback function to handle each document * @returns Promise that resolves with a Subscription object to manage the subscription */ subscribe(options: { docstore_id: string; type?: string; since?: number; until?: number; }, onDoc: (doc?: Doc) => Promise<void>): Promise<Subscription>; /** * Prepare a document for serialization by converting Float32Array to regular arrays * @param doc - Document to prepare * @returns A serializable version of the document */ private prepareDocForSerialization; /** * Upsert a document in the store * @param doc - Document to upsert * @returns Promise that resolves when the operation is complete */ upsert(doc: Doc): Promise<void>; /** * Get a document by ID * @param docstore_id - ID of the docstore containing the document * @param doc_id - ID of the document to get * @returns Promise that resolves with the document if found, null otherwise */ get(docstore_id: string, doc_id: string): Promise<Doc | null>; /** * Delete a document from the store * @param docstore_id - ID of the docstore containing the document * @param doc_id - ID of the document to delete * @returns Promise that resolves with true if document existed and was deleted, false otherwise */ delete(docstore_id: string, doc_id: string): Promise<boolean>; /** * Create a new docstore if one with the given name doesn't exist * @param name - Name of the docstore to create * @param model - Name of the embeddings model * @param vector_size - Size of embedding vectors * @param options - Options for the model, defaults to empty string * @returns Promise that resolves with the ID of the created or existing docstore */ createDocstore(name: string, model?: string, vector_size?: number, options?: string): Promise<string>; /** * Get a docstore by ID * @param id - ID of the docstore to get * @returns Promise that resolves with the docstore if found, undefined otherwise */ getDocstore(id: string): Promise<DocStore | undefined>; /** * List all docstores * @returns Promise that resolves with an array of docstore objects */ listDocstores(): Promise<DocStore[]>; /** * List docstores by specific IDs * @param ids - Array of docstore IDs to retrieve * @returns Promise that resolves with an array of docstore objects */ listDocStoresByIds(ids: string[]): Promise<DocStore[]>; /** * List documents by specific IDs * @param docstore_id - ID of the docstore containing the documents * @param ids - Array of document IDs to retrieve * @returns Promise that resolves with an array of document objects */ listDocsByIds(docstore_id: string, ids: string[]): Promise<Doc[]>; /** * Delete a docstore and all its documents * @param id - ID of the docstore to delete * @returns Promise that resolves with true if docstore existed and was deleted, false otherwise */ deleteDocstore(id: string): Promise<boolean>; /** * Count documents in a docstore * @param docstore_id - ID of the docstore to count documents for * @returns Promise that resolves with the number of documents in the docstore */ countDocs(docstore_id: string): Promise<number>; /** * Enqueue a document for processing by a subscription * @param subId - Subscription ID * @param doc - Document to process, or undefined to signal end of feed */ private enqueueDoc; /** * Process documents for a subscription sequentially * @param subId - Subscription ID */ private processDocs; [Symbol.dispose](): void; }