Agora402 Shared Types and Utility Functions Reference
Reference for Agora402 shared TypeScript types: ServiceListing, Quote, Receipt, PricingModel, and utility functions priceFor, generateUaid, formatAmount.
The @agora402/shared package is the single source of truth for every data structure and utility function that crosses the boundary between buyers, sellers, and the registry. All types come with runtime validation and full TypeScript inference from the same definition. The utility functions handle pricing arithmetic, amount formatting, UAID generation, and token estimation — all without floating-point arithmetic touching money.
The record a seller publishes to the HCS registry topic. One listing describes an entire service: who receives payments, where the service lives, and which endpoints it sells.
The seller’s HCS-14 Universal Agent Identifier, always starting with 'uaid:'. Generated with generateUaid(). Used as the primary key across the registry and receipt ledger.
Optional HCS topic ID where the seller writes Receipt messages after each settlement. Provide this so buyers can audit the billing trail independently.
Stable identifier for this endpoint within the service, e.g. 'infer' or 'hbar-rate'. Buyers use this to discover the service via findEndpoint() and discover().
The buyer’s work estimate. Fields: inputTokens (integer), maxOutputTokens (integer), units (integer). All optional; the seller uses whichever fields are relevant to its pricing model. Defaults to {}.
Optional ceiling price the buyer is willing to pay, in atomic units as a decimal string. The seller may accept this counter, reject it (HTTP 409), or quote between the counter and list price.
Unique identifier for this quote (minimum 8 characters). The buyer includes this in the subsequent paid request so the seller can match the agreed price.
Hex-encoded DER public key corresponding to signature. When verifyQuoteSigner is enabled on the buyer, this key is verified against the seller’s payTo account on the mirror node.
Written to the receipts HCS topic by the seller after every successful payment settlement. See the ReceiptLedger reference for how to read and audit receipts.
Computes the price of a request in atomic units using the given pricing model and work estimate. All arithmetic uses BigInt — tinybars never pass through a float. Token costs are rounded up per 1,000-token block.
Both buyers and sellers call priceFor() with the same pricing model and the same estimate. This reproducibility is what makes the quote handshake trustworthy: neither side can unilaterally inflate the price.
Generates a deterministic HCS-14 Universal Agent Identifier from the agent’s identity fields. The core aid component is Base58(SHA-384(canonical JSON)), so the same identity always produces the same UAID and the identifier is collision-resistant.
parseAmount() silently truncates fractional digits beyond decimals. For example, parseAmount('0.123456789', 8) returns 12_345_678n, discarding the ninth decimal digit. Always validate user-supplied amounts before passing them to this function.
estimateChatInput(body: unknown): { inputTokens: number; maxOutputTokens: number }
Produces a deterministic work estimate from an OpenAI-style chat completion request body. Uses a simple ceil(characters / 4) heuristic (~4 characters per token for English text). Both buyers and sellers call this function so the estimate is consistent across both sides of the price negotiation.
An OpenAI-style request body. The function reads body.messages[].content (string or JSON-serialisable) and body.prompt (string fallback). body.max_tokens sets maxOutputTokens; defaults to 256 if absent or non-positive.
Pass the same body object to both estimateChatInput() and the paid call() to ensure the estimate used for quoting matches the actual request, giving you predictable pricing.
⌘I
Assistant
Responses are generated using AI and may contain mistakes.