@flop-labs/sdk 0.0.0-stage → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +315 -0
- package/LICENSE +238 -0
- package/README.md +622 -2
- package/dist/a2a/agent-card.d.ts +56 -0
- package/dist/a2a/agent-card.d.ts.map +1 -0
- package/dist/a2a/agent-card.js +111 -0
- package/dist/a2a/agent-card.js.map +1 -0
- package/dist/a2a/index.d.ts +11 -0
- package/dist/a2a/index.d.ts.map +1 -0
- package/dist/a2a/index.js +11 -0
- package/dist/a2a/index.js.map +1 -0
- package/dist/a2a/task-lifecycle.d.ts +41 -0
- package/dist/a2a/task-lifecycle.d.ts.map +1 -0
- package/dist/a2a/task-lifecycle.js +60 -0
- package/dist/a2a/task-lifecycle.js.map +1 -0
- package/dist/a2a/types.d.ts +96 -0
- package/dist/a2a/types.d.ts.map +1 -0
- package/dist/a2a/types.js +23 -0
- package/dist/a2a/types.js.map +1 -0
- package/dist/a2a-cjs/agent-card.js +115 -0
- package/dist/a2a-cjs/agent-card.js.map +1 -0
- package/dist/a2a-cjs/index.js +21 -0
- package/dist/a2a-cjs/index.js.map +1 -0
- package/dist/a2a-cjs/package.json +1 -0
- package/dist/a2a-cjs/task-lifecycle.js +64 -0
- package/dist/a2a-cjs/task-lifecycle.js.map +1 -0
- package/dist/a2a-cjs/types.js +26 -0
- package/dist/a2a-cjs/types.js.map +1 -0
- package/dist/adaptor.d.ts +39 -0
- package/dist/adaptor.d.ts.map +1 -0
- package/dist/adaptor.js +130 -0
- package/dist/adaptor.js.map +1 -0
- package/dist/agent/errors.d.ts +95 -0
- package/dist/agent/errors.d.ts.map +1 -0
- package/dist/agent/errors.js +132 -0
- package/dist/agent/errors.js.map +1 -0
- package/dist/agent/task-delegation.d.ts +283 -0
- package/dist/agent/task-delegation.d.ts.map +1 -0
- package/dist/agent/task-delegation.js +444 -0
- package/dist/agent/task-delegation.js.map +1 -0
- package/dist/agent/types.d.ts +19 -0
- package/dist/agent/types.d.ts.map +1 -0
- package/dist/agent/types.js +7 -0
- package/dist/agent/types.js.map +1 -0
- package/dist/agent-wallet.d.ts +197 -0
- package/dist/agent-wallet.d.ts.map +1 -0
- package/dist/agent-wallet.js +408 -0
- package/dist/agent-wallet.js.map +1 -0
- package/dist/amhl.d.ts +61 -0
- package/dist/amhl.d.ts.map +1 -0
- package/dist/amhl.js +161 -0
- package/dist/amhl.js.map +1 -0
- package/dist/attestation.d.ts +126 -0
- package/dist/attestation.d.ts.map +1 -0
- package/dist/attestation.js +287 -0
- package/dist/attestation.js.map +1 -0
- package/dist/bip340-adaptor.d.ts +8 -0
- package/dist/bip340-adaptor.d.ts.map +1 -0
- package/dist/bip340-adaptor.js +129 -0
- package/dist/bip340-adaptor.js.map +1 -0
- package/dist/btc-relayer.d.ts +42 -0
- package/dist/btc-relayer.d.ts.map +1 -0
- package/dist/btc-relayer.js +40 -0
- package/dist/btc-relayer.js.map +1 -0
- package/dist/bundle/index.esm.js +2 -0
- package/dist/bundle/index.esm.js.map +1 -0
- package/dist/bundle/index.umd.cjs +2 -0
- package/dist/bundle/index.umd.cjs.map +1 -0
- package/dist/capability-claim-sink.d.ts +5 -0
- package/dist/capability-claim-sink.d.ts.map +1 -0
- package/dist/capability-claim-sink.js +6 -0
- package/dist/capability-claim-sink.js.map +1 -0
- package/dist/chain.d.ts +63 -0
- package/dist/chain.d.ts.map +1 -0
- package/dist/chain.js +100 -0
- package/dist/chain.js.map +1 -0
- package/dist/client.d.ts +356 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +863 -0
- package/dist/client.js.map +1 -0
- package/dist/compute-channel/adaptor-settlement.d.ts +19 -0
- package/dist/compute-channel/adaptor-settlement.d.ts.map +1 -0
- package/dist/compute-channel/adaptor-settlement.js +40 -0
- package/dist/compute-channel/adaptor-settlement.js.map +1 -0
- package/dist/compute-channel/agent-client.d.ts +141 -0
- package/dist/compute-channel/agent-client.d.ts.map +1 -0
- package/dist/compute-channel/agent-client.js +265 -0
- package/dist/compute-channel/agent-client.js.map +1 -0
- package/dist/compute-channel/agent-driver.d.ts +59 -0
- package/dist/compute-channel/agent-driver.d.ts.map +1 -0
- package/dist/compute-channel/agent-driver.js +364 -0
- package/dist/compute-channel/agent-driver.js.map +1 -0
- package/dist/compute-channel/chain.d.ts +245 -0
- package/dist/compute-channel/chain.d.ts.map +1 -0
- package/dist/compute-channel/chain.js +490 -0
- package/dist/compute-channel/chain.js.map +1 -0
- package/dist/compute-channel/chat-render.d.ts +21 -0
- package/dist/compute-channel/chat-render.d.ts.map +1 -0
- package/dist/compute-channel/chat-render.js +87 -0
- package/dist/compute-channel/chat-render.js.map +1 -0
- package/dist/compute-channel/crypto.d.ts +19 -0
- package/dist/compute-channel/crypto.d.ts.map +1 -0
- package/dist/compute-channel/crypto.js +52 -0
- package/dist/compute-channel/crypto.js.map +1 -0
- package/dist/compute-channel/da.d.ts +111 -0
- package/dist/compute-channel/da.d.ts.map +1 -0
- package/dist/compute-channel/da.js +320 -0
- package/dist/compute-channel/da.js.map +1 -0
- package/dist/compute-channel/deepseek-v41.d.ts +56 -0
- package/dist/compute-channel/deepseek-v41.d.ts.map +1 -0
- package/dist/compute-channel/deepseek-v41.js +267 -0
- package/dist/compute-channel/deepseek-v41.js.map +1 -0
- package/dist/compute-channel/direct-session-chain.d.ts +3 -0
- package/dist/compute-channel/direct-session-chain.d.ts.map +1 -0
- package/dist/compute-channel/direct-session-chain.js +245 -0
- package/dist/compute-channel/direct-session-chain.js.map +1 -0
- package/dist/compute-channel/direct-session.d.ts +78 -0
- package/dist/compute-channel/direct-session.d.ts.map +1 -0
- package/dist/compute-channel/direct-session.js +208 -0
- package/dist/compute-channel/direct-session.js.map +1 -0
- package/dist/compute-channel/dispute.d.ts +103 -0
- package/dist/compute-channel/dispute.d.ts.map +1 -0
- package/dist/compute-channel/dispute.js +314 -0
- package/dist/compute-channel/dispute.js.map +1 -0
- package/dist/compute-channel/flop-meter.d.ts +96 -0
- package/dist/compute-channel/flop-meter.d.ts.map +1 -0
- package/dist/compute-channel/flop-meter.js +210 -0
- package/dist/compute-channel/flop-meter.js.map +1 -0
- package/dist/compute-channel/flop-submitter.d.ts +40 -0
- package/dist/compute-channel/flop-submitter.d.ts.map +1 -0
- package/dist/compute-channel/flop-submitter.js +134 -0
- package/dist/compute-channel/flop-submitter.js.map +1 -0
- package/dist/compute-channel/hybrid-candidate.d.ts +22 -0
- package/dist/compute-channel/hybrid-candidate.d.ts.map +1 -0
- package/dist/compute-channel/hybrid-candidate.js +155 -0
- package/dist/compute-channel/hybrid-candidate.js.map +1 -0
- package/dist/compute-channel/hybrid-metering.d.ts +36 -0
- package/dist/compute-channel/hybrid-metering.d.ts.map +1 -0
- package/dist/compute-channel/hybrid-metering.js +69 -0
- package/dist/compute-channel/hybrid-metering.js.map +1 -0
- package/dist/compute-channel/hybrid.d.ts +16 -0
- package/dist/compute-channel/hybrid.d.ts.map +1 -0
- package/dist/compute-channel/hybrid.js +98 -0
- package/dist/compute-channel/hybrid.js.map +1 -0
- package/dist/compute-channel/index.d.ts +47 -0
- package/dist/compute-channel/index.d.ts.map +1 -0
- package/dist/compute-channel/index.js +48 -0
- package/dist/compute-channel/index.js.map +1 -0
- package/dist/compute-channel/lane-producer.d.ts +92 -0
- package/dist/compute-channel/lane-producer.d.ts.map +1 -0
- package/dist/compute-channel/lane-producer.js +265 -0
- package/dist/compute-channel/lane-producer.js.map +1 -0
- package/dist/compute-channel/layered.d.ts +77 -0
- package/dist/compute-channel/layered.d.ts.map +1 -0
- package/dist/compute-channel/layered.js +220 -0
- package/dist/compute-channel/layered.js.map +1 -0
- package/dist/compute-channel/lineage.d.ts +19 -0
- package/dist/compute-channel/lineage.d.ts.map +1 -0
- package/dist/compute-channel/lineage.js +28 -0
- package/dist/compute-channel/lineage.js.map +1 -0
- package/dist/compute-channel/miner-host.d.ts +329 -0
- package/dist/compute-channel/miner-host.d.ts.map +1 -0
- package/dist/compute-channel/miner-host.js +880 -0
- package/dist/compute-channel/miner-host.js.map +1 -0
- package/dist/compute-channel/model-config.d.ts +87 -0
- package/dist/compute-channel/model-config.d.ts.map +1 -0
- package/dist/compute-channel/model-config.js +697 -0
- package/dist/compute-channel/model-config.js.map +1 -0
- package/dist/compute-channel/offers.d.ts +57 -0
- package/dist/compute-channel/offers.d.ts.map +1 -0
- package/dist/compute-channel/offers.js +182 -0
- package/dist/compute-channel/offers.js.map +1 -0
- package/dist/compute-channel/point-claim-sink.d.ts +5 -0
- package/dist/compute-channel/point-claim-sink.d.ts.map +1 -0
- package/dist/compute-channel/point-claim-sink.js +6 -0
- package/dist/compute-channel/point-claim-sink.js.map +1 -0
- package/dist/compute-channel/protocol.d.ts +243 -0
- package/dist/compute-channel/protocol.d.ts.map +1 -0
- package/dist/compute-channel/protocol.js +249 -0
- package/dist/compute-channel/protocol.js.map +1 -0
- package/dist/compute-channel/runtime-storage.d.ts +6 -0
- package/dist/compute-channel/runtime-storage.d.ts.map +1 -0
- package/dist/compute-channel/runtime-storage.js +11 -0
- package/dist/compute-channel/runtime-storage.js.map +1 -0
- package/dist/compute-channel/sampled-audit.d.ts +24 -0
- package/dist/compute-channel/sampled-audit.d.ts.map +1 -0
- package/dist/compute-channel/sampled-audit.js +97 -0
- package/dist/compute-channel/sampled-audit.js.map +1 -0
- package/dist/compute-channel/session-auth.d.ts +6 -0
- package/dist/compute-channel/session-auth.d.ts.map +1 -0
- package/dist/compute-channel/session-auth.js +23 -0
- package/dist/compute-channel/session-auth.js.map +1 -0
- package/dist/compute-channel/session-estimate.d.ts +103 -0
- package/dist/compute-channel/session-estimate.d.ts.map +1 -0
- package/dist/compute-channel/session-estimate.js +140 -0
- package/dist/compute-channel/session-estimate.js.map +1 -0
- package/dist/compute-channel/session-prior-bank.d.ts +61 -0
- package/dist/compute-channel/session-prior-bank.d.ts.map +1 -0
- package/dist/compute-channel/session-prior-bank.js +74 -0
- package/dist/compute-channel/session-prior-bank.js.map +1 -0
- package/dist/compute-channel/session-prior.d.ts +145 -0
- package/dist/compute-channel/session-prior.d.ts.map +1 -0
- package/dist/compute-channel/session-prior.js +325 -0
- package/dist/compute-channel/session-prior.js.map +1 -0
- package/dist/compute-channel/session-store.d.ts +51 -0
- package/dist/compute-channel/session-store.d.ts.map +1 -0
- package/dist/compute-channel/session-store.js +186 -0
- package/dist/compute-channel/session-store.js.map +1 -0
- package/dist/compute-channel/session.d.ts +120 -0
- package/dist/compute-channel/session.d.ts.map +1 -0
- package/dist/compute-channel/session.js +232 -0
- package/dist/compute-channel/session.js.map +1 -0
- package/dist/compute-channel/toploc-checker.d.ts +384 -0
- package/dist/compute-channel/toploc-checker.d.ts.map +1 -0
- package/dist/compute-channel/toploc-checker.js +794 -0
- package/dist/compute-channel/toploc-checker.js.map +1 -0
- package/dist/compute-channel/toploc-completion-rpc.d.ts +26 -0
- package/dist/compute-channel/toploc-completion-rpc.d.ts.map +1 -0
- package/dist/compute-channel/toploc-completion-rpc.js +391 -0
- package/dist/compute-channel/toploc-completion-rpc.js.map +1 -0
- package/dist/compute-channel/toploc-completion.d.ts +109 -0
- package/dist/compute-channel/toploc-completion.d.ts.map +1 -0
- package/dist/compute-channel/toploc-completion.js +301 -0
- package/dist/compute-channel/toploc-completion.js.map +1 -0
- package/dist/compute-channel/toploc-evidence.d.ts +185 -0
- package/dist/compute-channel/toploc-evidence.d.ts.map +1 -0
- package/dist/compute-channel/toploc-evidence.js +299 -0
- package/dist/compute-channel/toploc-evidence.js.map +1 -0
- package/dist/compute-channel/toploc-gumbel.d.ts +149 -0
- package/dist/compute-channel/toploc-gumbel.d.ts.map +1 -0
- package/dist/compute-channel/toploc-gumbel.js +245 -0
- package/dist/compute-channel/toploc-gumbel.js.map +1 -0
- package/dist/compute-channel/toploc.d.ts +63 -0
- package/dist/compute-channel/toploc.d.ts.map +1 -0
- package/dist/compute-channel/toploc.js +197 -0
- package/dist/compute-channel/toploc.js.map +1 -0
- package/dist/compute-channel/transcript.d.ts +127 -0
- package/dist/compute-channel/transcript.d.ts.map +1 -0
- package/dist/compute-channel/transcript.js +279 -0
- package/dist/compute-channel/transcript.js.map +1 -0
- package/dist/compute-channel/transport-http.d.ts +217 -0
- package/dist/compute-channel/transport-http.d.ts.map +1 -0
- package/dist/compute-channel/transport-http.js +625 -0
- package/dist/compute-channel/transport-http.js.map +1 -0
- package/dist/coordination.d.ts +341 -0
- package/dist/coordination.d.ts.map +1 -0
- package/dist/coordination.js +481 -0
- package/dist/coordination.js.map +1 -0
- package/dist/da-client.d.ts +124 -0
- package/dist/da-client.d.ts.map +1 -0
- package/dist/da-client.js +127 -0
- package/dist/da-client.js.map +1 -0
- package/dist/discovery/capabilities.d.ts +129 -0
- package/dist/discovery/capabilities.d.ts.map +1 -0
- package/dist/discovery/capabilities.js +398 -0
- package/dist/discovery/capabilities.js.map +1 -0
- package/dist/discovery/discovery-service.d.ts +476 -0
- package/dist/discovery/discovery-service.d.ts.map +1 -0
- package/dist/discovery/discovery-service.js +1005 -0
- package/dist/discovery/discovery-service.js.map +1 -0
- package/dist/discovery/examples.d.ts +50 -0
- package/dist/discovery/examples.d.ts.map +1 -0
- package/dist/discovery/examples.js +163 -0
- package/dist/discovery/examples.js.map +1 -0
- package/dist/discovery/fee-estimation.d.ts +550 -0
- package/dist/discovery/fee-estimation.d.ts.map +1 -0
- package/dist/discovery/fee-estimation.js +921 -0
- package/dist/discovery/fee-estimation.js.map +1 -0
- package/dist/discovery/index.d.ts +38 -0
- package/dist/discovery/index.d.ts.map +1 -0
- package/dist/discovery/index.js +32 -0
- package/dist/discovery/index.js.map +1 -0
- package/dist/discovery/metered-pricing.d.ts +100 -0
- package/dist/discovery/metered-pricing.d.ts.map +1 -0
- package/dist/discovery/metered-pricing.js +150 -0
- package/dist/discovery/metered-pricing.js.map +1 -0
- package/dist/discovery/priority.d.ts +16 -0
- package/dist/discovery/priority.d.ts.map +1 -0
- package/dist/discovery/priority.js +17 -0
- package/dist/discovery/priority.js.map +1 -0
- package/dist/discovery/scoring.d.ts +231 -0
- package/dist/discovery/scoring.d.ts.map +1 -0
- package/dist/discovery/scoring.js +354 -0
- package/dist/discovery/scoring.js.map +1 -0
- package/dist/ecdsa-adaptor.d.ts +72 -0
- package/dist/ecdsa-adaptor.d.ts.map +1 -0
- package/dist/ecdsa-adaptor.js +351 -0
- package/dist/ecdsa-adaptor.js.map +1 -0
- package/dist/errors.d.ts +98 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +190 -0
- package/dist/errors.js.map +1 -0
- package/dist/escrow.d.ts +116 -0
- package/dist/escrow.d.ts.map +1 -0
- package/dist/escrow.js +172 -0
- package/dist/escrow.js.map +1 -0
- package/dist/evm-relayer.d.ts +12 -0
- package/dist/evm-relayer.d.ts.map +1 -0
- package/dist/evm-relayer.js +25 -0
- package/dist/evm-relayer.js.map +1 -0
- package/dist/experimental.d.ts +3 -0
- package/dist/experimental.d.ts.map +1 -0
- package/dist/experimental.js +3 -0
- package/dist/experimental.js.map +1 -0
- package/dist/faucet.d.ts +141 -0
- package/dist/faucet.d.ts.map +1 -0
- package/dist/faucet.js +312 -0
- package/dist/faucet.js.map +1 -0
- package/dist/finality-status.d.ts +21 -0
- package/dist/finality-status.d.ts.map +1 -0
- package/dist/finality-status.js +10 -0
- package/dist/finality-status.js.map +1 -0
- package/dist/generated-client/capabilities.d.ts +83 -0
- package/dist/generated-client/capabilities.d.ts.map +1 -0
- package/dist/generated-client/capabilities.js +87 -0
- package/dist/generated-client/capabilities.js.map +1 -0
- package/dist/generated-client/cli.d.ts +3 -0
- package/dist/generated-client/cli.d.ts.map +1 -0
- package/dist/generated-client/cli.js +33 -0
- package/dist/generated-client/cli.js.map +1 -0
- package/dist/generated-client/errors.d.ts +13 -0
- package/dist/generated-client/errors.d.ts.map +1 -0
- package/dist/generated-client/errors.js +23 -0
- package/dist/generated-client/errors.js.map +1 -0
- package/dist/generated-client/factory.d.ts +28 -0
- package/dist/generated-client/factory.d.ts.map +1 -0
- package/dist/generated-client/factory.js +182 -0
- package/dist/generated-client/factory.js.map +1 -0
- package/dist/generated-client/generator.d.ts +9 -0
- package/dist/generated-client/generator.d.ts.map +1 -0
- package/dist/generated-client/generator.js +80 -0
- package/dist/generated-client/generator.js.map +1 -0
- package/dist/generated-client/index.d.ts +11 -0
- package/dist/generated-client/index.d.ts.map +1 -0
- package/dist/generated-client/index.js +6 -0
- package/dist/generated-client/index.js.map +1 -0
- package/dist/generated-client/profile.d.ts +166 -0
- package/dist/generated-client/profile.d.ts.map +1 -0
- package/dist/generated-client/profile.js +158 -0
- package/dist/generated-client/profile.js.map +1 -0
- package/dist/generated-client/templates/agent-chat.d.ts +16 -0
- package/dist/generated-client/templates/agent-chat.d.ts.map +1 -0
- package/dist/generated-client/templates/agent-chat.js +12 -0
- package/dist/generated-client/templates/agent-chat.js.map +1 -0
- package/dist/generated-client/templates/index.d.ts +4 -0
- package/dist/generated-client/templates/index.d.ts.map +1 -0
- package/dist/generated-client/templates/index.js +4 -0
- package/dist/generated-client/templates/index.js.map +1 -0
- package/dist/generated-client/templates/miner-operator.d.ts +16 -0
- package/dist/generated-client/templates/miner-operator.d.ts.map +1 -0
- package/dist/generated-client/templates/miner-operator.js +12 -0
- package/dist/generated-client/templates/miner-operator.js.map +1 -0
- package/dist/generated-client/templates/model-publisher.d.ts +21 -0
- package/dist/generated-client/templates/model-publisher.d.ts.map +1 -0
- package/dist/generated-client/templates/model-publisher.js +13 -0
- package/dist/generated-client/templates/model-publisher.js.map +1 -0
- package/dist/hardware.d.ts +68 -0
- package/dist/hardware.d.ts.map +1 -0
- package/dist/hardware.js +222 -0
- package/dist/hardware.js.map +1 -0
- package/dist/index.d.ts +109 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +140 -0
- package/dist/index.js.map +1 -0
- package/dist/model-loader/canonical.d.ts +79 -0
- package/dist/model-loader/canonical.d.ts.map +1 -0
- package/dist/model-loader/canonical.js +116 -0
- package/dist/model-loader/canonical.js.map +1 -0
- package/dist/model-loader/dir.d.ts +26 -0
- package/dist/model-loader/dir.d.ts.map +1 -0
- package/dist/model-loader/dir.js +109 -0
- package/dist/model-loader/dir.js.map +1 -0
- package/dist/model-loader/dmverity.d.ts +78 -0
- package/dist/model-loader/dmverity.d.ts.map +1 -0
- package/dist/model-loader/dmverity.js +141 -0
- package/dist/model-loader/dmverity.js.map +1 -0
- package/dist/model-loader/download-state.d.ts +24 -0
- package/dist/model-loader/download-state.d.ts.map +1 -0
- package/dist/model-loader/download-state.js +99 -0
- package/dist/model-loader/download-state.js.map +1 -0
- package/dist/model-loader/fetch.d.ts +51 -0
- package/dist/model-loader/fetch.d.ts.map +1 -0
- package/dist/model-loader/fetch.js +410 -0
- package/dist/model-loader/fetch.js.map +1 -0
- package/dist/model-loader/file-table.d.ts +38 -0
- package/dist/model-loader/file-table.d.ts.map +1 -0
- package/dist/model-loader/file-table.js +106 -0
- package/dist/model-loader/file-table.js.map +1 -0
- package/dist/model-loader/hf-preflight.d.ts +65 -0
- package/dist/model-loader/hf-preflight.d.ts.map +1 -0
- package/dist/model-loader/hf-preflight.js +179 -0
- package/dist/model-loader/hf-preflight.js.map +1 -0
- package/dist/model-loader/index.d.ts +27 -0
- package/dist/model-loader/index.d.ts.map +1 -0
- package/dist/model-loader/index.js +21 -0
- package/dist/model-loader/index.js.map +1 -0
- package/dist/model-loader/registry.d.ts +24 -0
- package/dist/model-loader/registry.d.ts.map +1 -0
- package/dist/model-loader/registry.js +105 -0
- package/dist/model-loader/registry.js.map +1 -0
- package/dist/model-loader/types.d.ts +101 -0
- package/dist/model-loader/types.d.ts.map +1 -0
- package/dist/model-loader/types.js +13 -0
- package/dist/model-loader/types.js.map +1 -0
- package/dist/model-loader/verify.d.ts +83 -0
- package/dist/model-loader/verify.d.ts.map +1 -0
- package/dist/model-loader/verify.js +138 -0
- package/dist/model-loader/verify.js.map +1 -0
- package/dist/model-qualification.d.ts +79 -0
- package/dist/model-qualification.d.ts.map +1 -0
- package/dist/model-qualification.js +135 -0
- package/dist/model-qualification.js.map +1 -0
- package/dist/near-relayer.d.ts +30 -0
- package/dist/near-relayer.d.ts.map +1 -0
- package/dist/near-relayer.js +81 -0
- package/dist/near-relayer.js.map +1 -0
- package/dist/paid-storage/bytes.d.ts +21 -0
- package/dist/paid-storage/bytes.d.ts.map +1 -0
- package/dist/paid-storage/bytes.js +71 -0
- package/dist/paid-storage/bytes.js.map +1 -0
- package/dist/paid-storage/chain.d.ts +20 -0
- package/dist/paid-storage/chain.d.ts.map +1 -0
- package/dist/paid-storage/chain.js +182 -0
- package/dist/paid-storage/chain.js.map +1 -0
- package/dist/paid-storage/client.d.ts +161 -0
- package/dist/paid-storage/client.d.ts.map +1 -0
- package/dist/paid-storage/client.js +1038 -0
- package/dist/paid-storage/client.js.map +1 -0
- package/dist/paid-storage/cloudflare.d.ts +4 -0
- package/dist/paid-storage/cloudflare.d.ts.map +1 -0
- package/dist/paid-storage/cloudflare.js +9 -0
- package/dist/paid-storage/cloudflare.js.map +1 -0
- package/dist/paid-storage/codec.d.ts +8 -0
- package/dist/paid-storage/codec.d.ts.map +1 -0
- package/dist/paid-storage/codec.js +92 -0
- package/dist/paid-storage/codec.js.map +1 -0
- package/dist/paid-storage/crypto.d.ts +27 -0
- package/dist/paid-storage/crypto.d.ts.map +1 -0
- package/dist/paid-storage/crypto.js +91 -0
- package/dist/paid-storage/crypto.js.map +1 -0
- package/dist/paid-storage/heads.d.ts +26 -0
- package/dist/paid-storage/heads.d.ts.map +1 -0
- package/dist/paid-storage/heads.js +208 -0
- package/dist/paid-storage/heads.js.map +1 -0
- package/dist/paid-storage/index.d.ts +14 -0
- package/dist/paid-storage/index.d.ts.map +1 -0
- package/dist/paid-storage/index.js +7 -0
- package/dist/paid-storage/index.js.map +1 -0
- package/dist/paid-storage/memory-wire.d.ts +85 -0
- package/dist/paid-storage/memory-wire.d.ts.map +1 -0
- package/dist/paid-storage/memory-wire.js +147 -0
- package/dist/paid-storage/memory-wire.js.map +1 -0
- package/dist/paid-storage/node-store.d.ts +9 -0
- package/dist/paid-storage/node-store.d.ts.map +1 -0
- package/dist/paid-storage/node-store.js +34 -0
- package/dist/paid-storage/node-store.js.map +1 -0
- package/dist/paid-storage/profile.d.ts +21 -0
- package/dist/paid-storage/profile.d.ts.map +1 -0
- package/dist/paid-storage/profile.js +22 -0
- package/dist/paid-storage/profile.js.map +1 -0
- package/dist/paid-storage/store.d.ts +18 -0
- package/dist/paid-storage/store.d.ts.map +1 -0
- package/dist/paid-storage/store.js +32 -0
- package/dist/paid-storage/store.js.map +1 -0
- package/dist/paid-storage/transport.d.ts +70 -0
- package/dist/paid-storage/transport.d.ts.map +1 -0
- package/dist/paid-storage/transport.js +140 -0
- package/dist/paid-storage/transport.js.map +1 -0
- package/dist/paid-storage/vouchers.d.ts +75 -0
- package/dist/paid-storage/vouchers.d.ts.map +1 -0
- package/dist/paid-storage/vouchers.js +239 -0
- package/dist/paid-storage/vouchers.js.map +1 -0
- package/dist/paid-storage-wire.d.ts +11 -0
- package/dist/paid-storage-wire.d.ts.map +1 -0
- package/dist/paid-storage-wire.js +184 -0
- package/dist/paid-storage-wire.js.map +1 -0
- package/dist/point-claims.d.ts +40 -0
- package/dist/point-claims.d.ts.map +1 -0
- package/dist/point-claims.js +98 -0
- package/dist/point-claims.js.map +1 -0
- package/dist/quickstart.d.ts +307 -0
- package/dist/quickstart.d.ts.map +1 -0
- package/dist/quickstart.js +536 -0
- package/dist/quickstart.js.map +1 -0
- package/dist/react/context.d.ts +10 -0
- package/dist/react/context.d.ts.map +1 -0
- package/dist/react/context.js +16 -0
- package/dist/react/context.js.map +1 -0
- package/dist/react/hooks.d.ts +206 -0
- package/dist/react/hooks.d.ts.map +1 -0
- package/dist/react/hooks.js +560 -0
- package/dist/react/hooks.js.map +1 -0
- package/dist/react/index.d.ts +4 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +4 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/provider.d.ts +18 -0
- package/dist/react/provider.d.ts.map +1 -0
- package/dist/react/provider.js +17 -0
- package/dist/react/provider.js.map +1 -0
- package/dist/relayer.d.ts +122 -0
- package/dist/relayer.d.ts.map +1 -0
- package/dist/relayer.js +203 -0
- package/dist/relayer.js.map +1 -0
- package/dist/runtime-compatibility.d.ts +8 -0
- package/dist/runtime-compatibility.d.ts.map +1 -0
- package/dist/runtime-compatibility.js +20 -0
- package/dist/runtime-compatibility.js.map +1 -0
- package/dist/session-manager.d.ts +197 -0
- package/dist/session-manager.d.ts.map +1 -0
- package/dist/session-manager.js +429 -0
- package/dist/session-manager.js.map +1 -0
- package/dist/solana-ptlc.d.ts +64 -0
- package/dist/solana-ptlc.d.ts.map +1 -0
- package/dist/solana-ptlc.js +137 -0
- package/dist/solana-ptlc.js.map +1 -0
- package/dist/staking-client.d.ts +103 -0
- package/dist/staking-client.d.ts.map +1 -0
- package/dist/staking-client.js +308 -0
- package/dist/staking-client.js.map +1 -0
- package/dist/staking-types.d.ts +203 -0
- package/dist/staking-types.d.ts.map +1 -0
- package/dist/staking-types.js +16 -0
- package/dist/staking-types.js.map +1 -0
- package/dist/submit-tx.d.ts +14 -0
- package/dist/submit-tx.d.ts.map +1 -0
- package/dist/submit-tx.js +33 -0
- package/dist/submit-tx.js.map +1 -0
- package/dist/templates/index.d.ts +52 -0
- package/dist/templates/index.d.ts.map +1 -0
- package/dist/templates/index.js +123 -0
- package/dist/templates/index.js.map +1 -0
- package/dist/ton/index.d.ts +52 -0
- package/dist/ton/index.d.ts.map +1 -0
- package/dist/ton/index.js +163 -0
- package/dist/ton/index.js.map +1 -0
- package/dist/ton-relayer.d.ts +26 -0
- package/dist/ton-relayer.d.ts.map +1 -0
- package/dist/ton-relayer.js +22 -0
- package/dist/ton-relayer.js.map +1 -0
- package/dist/tracing.d.ts +48 -0
- package/dist/tracing.d.ts.map +1 -0
- package/dist/tracing.js +108 -0
- package/dist/tracing.js.map +1 -0
- package/dist/transaction-signer.d.ts +13 -0
- package/dist/transaction-signer.d.ts.map +1 -0
- package/dist/transaction-signer.js +13 -0
- package/dist/transaction-signer.js.map +1 -0
- package/dist/trust-root.d.ts +56 -0
- package/dist/trust-root.d.ts.map +1 -0
- package/dist/trust-root.js +28 -0
- package/dist/trust-root.js.map +1 -0
- package/dist/types.d.ts +201 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/validator-staking-client.d.ts +87 -0
- package/dist/validator-staking-client.d.ts.map +1 -0
- package/dist/validator-staking-client.js +226 -0
- package/dist/validator-staking-client.js.map +1 -0
- package/dist/virtuals-acp.d.ts +149 -0
- package/dist/virtuals-acp.d.ts.map +1 -0
- package/dist/virtuals-acp.js +418 -0
- package/dist/virtuals-acp.js.map +1 -0
- package/dist/wallet-manager.d.ts +199 -0
- package/dist/wallet-manager.d.ts.map +1 -0
- package/dist/wallet-manager.js +425 -0
- package/dist/wallet-manager.js.map +1 -0
- package/dist/wallets/burner.d.ts +45 -0
- package/dist/wallets/burner.d.ts.map +1 -0
- package/dist/wallets/burner.js +58 -0
- package/dist/wallets/burner.js.map +1 -0
- package/dist/wallets/index.d.ts +26 -0
- package/dist/wallets/index.d.ts.map +1 -0
- package/dist/wallets/index.js +20 -0
- package/dist/wallets/index.js.map +1 -0
- package/dist/wallets/injected.d.ts +22 -0
- package/dist/wallets/injected.d.ts.map +1 -0
- package/dist/wallets/injected.js +45 -0
- package/dist/wallets/injected.js.map +1 -0
- package/dist/wallets/ledger.d.ts +76 -0
- package/dist/wallets/ledger.d.ts.map +1 -0
- package/dist/wallets/ledger.js +106 -0
- package/dist/wallets/ledger.js.map +1 -0
- package/dist/wallets/near.d.ts +69 -0
- package/dist/wallets/near.d.ts.map +1 -0
- package/dist/wallets/near.js +101 -0
- package/dist/wallets/near.js.map +1 -0
- package/dist/wallets/types.d.ts +42 -0
- package/dist/wallets/types.d.ts.map +1 -0
- package/dist/wallets/types.js +31 -0
- package/dist/wallets/types.js.map +1 -0
- package/dist/wallets/walletconnect.d.ts +38 -0
- package/dist/wallets/walletconnect.d.ts.map +1 -0
- package/dist/wallets/walletconnect.js +94 -0
- package/dist/wallets/walletconnect.js.map +1 -0
- package/dist/weak-subjectivity.d.ts +213 -0
- package/dist/weak-subjectivity.d.ts.map +1 -0
- package/dist/weak-subjectivity.js +389 -0
- package/dist/weak-subjectivity.js.map +1 -0
- package/dist/x402-types/channel-types.d.ts +78 -0
- package/dist/x402-types/channel-types.d.ts.map +1 -0
- package/dist/x402-types/channel-types.js +5 -0
- package/dist/x402-types/channel-types.js.map +1 -0
- package/dist/x402-types/exact-flop.d.ts +29 -0
- package/dist/x402-types/exact-flop.d.ts.map +1 -0
- package/dist/x402-types/exact-flop.js +64 -0
- package/dist/x402-types/exact-flop.js.map +1 -0
- package/dist/x402-types/index.d.ts +65 -0
- package/dist/x402-types/index.d.ts.map +1 -0
- package/dist/x402-types/index.js +26 -0
- package/dist/x402-types/index.js.map +1 -0
- package/dist/x402.d.ts +111 -0
- package/dist/x402.d.ts.map +1 -0
- package/dist/x402.js +206 -0
- package/dist/x402.js.map +1 -0
- package/package.json +165 -3
|
@@ -0,0 +1,921 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fee Estimation Module for FLOP Network
|
|
3
|
+
*
|
|
4
|
+
* Provides pre-submission fee estimation for inference tasks, enabling
|
|
5
|
+
* agents to understand costs before submitting tasks to the network.
|
|
6
|
+
*
|
|
7
|
+
* This module implements:
|
|
8
|
+
* - Metered cost estimation: with `metering`, compute is priced exactly as compute-channel
|
|
9
|
+
* settlement charges it, via `computeChannel.estimateSession`
|
|
10
|
+
* - Token-based display estimates (without `metering`; never used for a budget decision)
|
|
11
|
+
* - Network congestion pricing
|
|
12
|
+
* - Priority-based fee multipliers
|
|
13
|
+
* - Task complexity analysis
|
|
14
|
+
* - Budget-aware model selection
|
|
15
|
+
* - Cost-quality tradeoff optimization
|
|
16
|
+
*
|
|
17
|
+
* @module discovery/fee-estimation
|
|
18
|
+
* @see docs/research/r0009-agent-onboarding-recommendations.md section 2.3
|
|
19
|
+
*/
|
|
20
|
+
import { estimateLatencyFromSize } from './scoring.js';
|
|
21
|
+
import { TaskPriority } from './priority.js';
|
|
22
|
+
import { estimateSession, } from '../compute-channel/session-estimate.js';
|
|
23
|
+
// ── Constants ───────────────────────────────────────────────────────────
|
|
24
|
+
/**
|
|
25
|
+
* Base cost per 1K tokens by model size.
|
|
26
|
+
*
|
|
27
|
+
* @deprecated Display-only legacy rates, about 480× below metered settlement cost for an 8B model
|
|
28
|
+
* at the runtime base price; pass `metering` to price from `G_n`.
|
|
29
|
+
*/
|
|
30
|
+
export const BASE_COST_PER_1K_TOKENS = {
|
|
31
|
+
small: 0.1, // ~7B params
|
|
32
|
+
medium: 0.3, // ~30B params
|
|
33
|
+
large: 0.8, // ~70B params
|
|
34
|
+
xlarge: 2.0, // ~100B+ params
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Priority multipliers for pricing modes.
|
|
38
|
+
*/
|
|
39
|
+
export const PRICING_MODE_MULTIPLIERS = {
|
|
40
|
+
standard: 1.0,
|
|
41
|
+
priority: 2.0,
|
|
42
|
+
urgent: 3.0,
|
|
43
|
+
economy: 0.7,
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Task priority multipliers.
|
|
47
|
+
*/
|
|
48
|
+
export const TASK_PRIORITY_MULTIPLIERS = {
|
|
49
|
+
[TaskPriority.LOW]: 0.8,
|
|
50
|
+
[TaskPriority.NORMAL]: 1.0,
|
|
51
|
+
[TaskPriority.HIGH]: 1.5,
|
|
52
|
+
[TaskPriority.CRITICAL]: 2.5,
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Mempool congestion thresholds.
|
|
56
|
+
*/
|
|
57
|
+
export const CONGESTION_THRESHOLDS = {
|
|
58
|
+
low: 100, // < 100 tasks in mempool
|
|
59
|
+
medium: 500, // 100-500 tasks
|
|
60
|
+
high: 1000, // 500-1000 tasks
|
|
61
|
+
critical: 2000, // > 1000 tasks
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Default network fee (transaction overhead).
|
|
65
|
+
*/
|
|
66
|
+
export const DEFAULT_NETWORK_FEE = 0.1; // FLOP
|
|
67
|
+
/**
|
|
68
|
+
* Minimum fee floor (prevents zero-cost tasks).
|
|
69
|
+
*/
|
|
70
|
+
export const MIN_FEE_FLOOR = 0.5; // FLOP
|
|
71
|
+
function isTokenCount(n) {
|
|
72
|
+
return n !== undefined && Number.isSafeInteger(n) && n >= 0;
|
|
73
|
+
}
|
|
74
|
+
/** Display projection of raw units (10⁻¹⁸ FLOP); budgets are compared in raw units instead. */
|
|
75
|
+
function rawToFlop(raw) {
|
|
76
|
+
return Number(raw) / 1e18;
|
|
77
|
+
}
|
|
78
|
+
/** The exact value of a FLOP budget in raw units, to 10⁻¹⁸ FLOP. */
|
|
79
|
+
function flopToRaw(budget) {
|
|
80
|
+
const fixed = Number.isFinite(budget) && budget >= 0 ? budget.toFixed(18) : '';
|
|
81
|
+
if (!/^\d+\.\d{18}$/.test(fixed)) {
|
|
82
|
+
throw new FeeEstimationError(`maxBudget ${budget} is not a FLOP amount in [0, 1e21)`, 'INVALID_BUDGET');
|
|
83
|
+
}
|
|
84
|
+
return BigInt(fixed.replace('.', ''));
|
|
85
|
+
}
|
|
86
|
+
// ── Fee Estimator Class ─────────────────────────────────────────────────
|
|
87
|
+
/**
|
|
88
|
+
* Fee Estimator for pre-submission cost analysis.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```ts
|
|
92
|
+
* const estimator = new FeeEstimator({ api });
|
|
93
|
+
*
|
|
94
|
+
* // Estimate fee before submission
|
|
95
|
+
* const estimate = await estimator.estimateFee({
|
|
96
|
+
* prompt: 'Write a Python function to sort a list',
|
|
97
|
+
* modelSize: 'large',
|
|
98
|
+
* pricingMode: 'standard',
|
|
99
|
+
* });
|
|
100
|
+
*
|
|
101
|
+
* console.log(`Estimated cost: ${estimate.estimatedFee} FLOP`);
|
|
102
|
+
* console.log(`Breakdown:`, estimate.breakdown);
|
|
103
|
+
*
|
|
104
|
+
* // Budget-aware selection prices each candidate from its own metering
|
|
105
|
+
* const priced = await discovery.meteringByModel(models);
|
|
106
|
+
* const selection = await estimator.selectWithinBudget({
|
|
107
|
+
* models: priced.models,
|
|
108
|
+
* maxBudget: 5.0,
|
|
109
|
+
* inputTokens: 1000,
|
|
110
|
+
* outputTokens: 500,
|
|
111
|
+
* meteringByModel: priced.meteringByModel,
|
|
112
|
+
* });
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
export class FeeEstimator {
|
|
116
|
+
api;
|
|
117
|
+
lastMempoolQuery;
|
|
118
|
+
cachedMempoolSize;
|
|
119
|
+
cacheTTL = 60_000; // 1 minute
|
|
120
|
+
constructor(options = {}) {
|
|
121
|
+
this.api = options.api;
|
|
122
|
+
this.cacheTTL = options.cacheTTL ?? 60_000;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Estimate fee for a task before submission.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```ts
|
|
129
|
+
* const estimate = await estimator.estimateFee({
|
|
130
|
+
* inputTokens: 1000,
|
|
131
|
+
* outputTokens: 500,
|
|
132
|
+
* maxBudget: 10,
|
|
133
|
+
* // verified config.json and hf_quant_config.json (or null), miner status, catalog price, runtime constants
|
|
134
|
+
* metering: meteredPricing({ modelHash, configJson, hfQuantConfigJson, miner, catalog, chain: readMeteringChainParams(api) }),
|
|
135
|
+
* });
|
|
136
|
+
*
|
|
137
|
+
* if (estimate.withinBudget) {
|
|
138
|
+
* console.log(`Safe to submit: ${estimate.estimatedFee} FLOP`);
|
|
139
|
+
* } else {
|
|
140
|
+
* console.log(`Exceeds budget: need ${estimate.estimatedFee} FLOP`);
|
|
141
|
+
* }
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
async estimateFee(options) {
|
|
145
|
+
const timestamp = new Date().toISOString();
|
|
146
|
+
// 0. Price from metered G_n; the legacy token rates never decide a budget
|
|
147
|
+
const metered = options.metering !== undefined ? this.meterRequest(options, options.metering) : undefined;
|
|
148
|
+
if (metered === undefined && options.maxBudget !== undefined) {
|
|
149
|
+
throw new FeeEstimationError('maxBudget needs metering: token-rate estimates are far below what compute-channel settlement charges', 'METERING_REQUIRED');
|
|
150
|
+
}
|
|
151
|
+
// 1. Determine token count
|
|
152
|
+
const tokens = metered !== undefined ? options.inputTokens + options.outputTokens : this.estimateTokens(options);
|
|
153
|
+
// 2. Determine model size
|
|
154
|
+
const modelSize = this.resolveModelSize(options);
|
|
155
|
+
// 3. Get network congestion info
|
|
156
|
+
const congestion = await this.getNetworkCongestion(options.mempoolSize);
|
|
157
|
+
// 4. Resolve pricing mode
|
|
158
|
+
const pricingMode = options.pricingMode ?? this.resolvePricingMode(options.priority);
|
|
159
|
+
// 4.5. Extract model's estimatedCost if ScoredModel is passed
|
|
160
|
+
const modelEstimatedCost = options.model !== undefined && typeof options.model !== 'string' && 'estimatedCost' in options.model
|
|
161
|
+
? options.model.estimatedCost
|
|
162
|
+
: undefined;
|
|
163
|
+
// 5. Calculate fee breakdown
|
|
164
|
+
const breakdown = metered !== undefined ? this.meteredBreakdown(metered, tokens) : this.calculateFeeBreakdown({
|
|
165
|
+
tokens,
|
|
166
|
+
modelSize,
|
|
167
|
+
pricingMode,
|
|
168
|
+
priority: options.priority,
|
|
169
|
+
congestionFactor: congestion.congestionFactor,
|
|
170
|
+
gflops: options.gflops,
|
|
171
|
+
// An unmetered estimate is display-only (no maxBudget reaches here), so it skips the floor
|
|
172
|
+
// to show model-size differences unless asked.
|
|
173
|
+
bypassFloor: options.bypassFloor ?? true,
|
|
174
|
+
bypassNetworkFee: options.bypassNetworkFee ?? false, // Default to false, set true for theoretical comparisons
|
|
175
|
+
modelEstimatedCost,
|
|
176
|
+
});
|
|
177
|
+
// 6. Calculate estimated latency
|
|
178
|
+
const baseLatency = estimateLatencyFromSize(modelSize);
|
|
179
|
+
const latencyMultiplier = pricingMode === 'economy' ? 2.0
|
|
180
|
+
: pricingMode === 'urgent' ? 0.5
|
|
181
|
+
: 1.0;
|
|
182
|
+
const estimatedLatency = Math.round(baseLatency * latencyMultiplier + congestion.estimatedWaitBlocks);
|
|
183
|
+
// 7. Check budget: raw units against the exact escrow, never the rounded display fee
|
|
184
|
+
let withinBudget = true;
|
|
185
|
+
let budgetRemaining;
|
|
186
|
+
if (metered !== undefined && options.maxBudget !== undefined) {
|
|
187
|
+
const budget = flopToRaw(options.maxBudget);
|
|
188
|
+
withinBudget = metered.escrowRaw <= budget;
|
|
189
|
+
budgetRemaining = withinBudget ? rawToFlop(budget - metered.escrowRaw) : 0;
|
|
190
|
+
}
|
|
191
|
+
// 8. Calculate confidence
|
|
192
|
+
const confidence = this.calculateConfidence(options, tokens, modelSize);
|
|
193
|
+
// 9. Determine model name
|
|
194
|
+
const modelName = this.resolveModelName(options);
|
|
195
|
+
// Build result
|
|
196
|
+
// Use consistent precision for all fee values
|
|
197
|
+
const precision = options.bypassFloor || options.bypassNetworkFee ? 10000 : 100;
|
|
198
|
+
const result = {
|
|
199
|
+
estimatedFee: breakdown.totalCost,
|
|
200
|
+
// Settlement pays exactly the escrow, so a metered fee has no spread.
|
|
201
|
+
minFee: metered !== undefined ? breakdown.totalCost
|
|
202
|
+
: Math.round((breakdown.computeCost + breakdown.networkFee) * precision) / precision,
|
|
203
|
+
maxFee: metered !== undefined ? breakdown.totalCost
|
|
204
|
+
: Math.round(breakdown.totalCost * 1.5 * precision) / precision, // Upper bound estimate
|
|
205
|
+
recommendedFee: metered !== undefined ? breakdown.totalCost
|
|
206
|
+
: Math.round(this.recommendFee(breakdown, congestion) * precision) / precision,
|
|
207
|
+
breakdown,
|
|
208
|
+
model: modelName,
|
|
209
|
+
modelSize,
|
|
210
|
+
estimatedLatency,
|
|
211
|
+
pricingMode,
|
|
212
|
+
withinBudget,
|
|
213
|
+
budgetRemaining,
|
|
214
|
+
confidence,
|
|
215
|
+
timestamp,
|
|
216
|
+
...(metered !== undefined ? { metered } : {}),
|
|
217
|
+
};
|
|
218
|
+
return result;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Quick fee estimate without full options.
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* ```ts
|
|
225
|
+
* const fee = await estimator.quickEstimate('Write Python code', 'large');
|
|
226
|
+
* console.log(`Estimated: ${fee} FLOP`);
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
async quickEstimate(prompt, modelSize = 'medium') {
|
|
230
|
+
const result = await this.estimateFee({
|
|
231
|
+
prompt,
|
|
232
|
+
modelSize,
|
|
233
|
+
pricingMode: 'standard',
|
|
234
|
+
bypassFloor: true, // Bypass floor to show actual model size differences
|
|
235
|
+
});
|
|
236
|
+
return result.estimatedFee;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Get current network congestion info.
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* ```ts
|
|
243
|
+
* const congestion = await estimator.getNetworkCongestion();
|
|
244
|
+
* console.log(`Congestion: ${congestion.congestionLevel}`);
|
|
245
|
+
* console.log(`Wait time: ${congestion.estimatedWaitBlocks} blocks`);
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
248
|
+
async getNetworkCongestion(mempoolOverride) {
|
|
249
|
+
// Use override if provided
|
|
250
|
+
let mempoolSize = mempoolOverride;
|
|
251
|
+
// Query mempool if API available and cache expired
|
|
252
|
+
if (mempoolSize === undefined && this.api) {
|
|
253
|
+
const now = Date.now();
|
|
254
|
+
if (!this.cachedMempoolSize || now - this.lastMempoolQuery > this.cacheTTL) {
|
|
255
|
+
try {
|
|
256
|
+
// Query mempool size from RPC
|
|
257
|
+
if (this.api.rpc.flopPoUI_mempoolSize) {
|
|
258
|
+
const result = await this.api.rpc.flopPoUI_mempoolSize();
|
|
259
|
+
mempoolSize = Number(result.toJSON?.() ?? result ?? 0);
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
mempoolSize = 0; // Default if RPC not available
|
|
263
|
+
}
|
|
264
|
+
this.cachedMempoolSize = mempoolSize;
|
|
265
|
+
this.lastMempoolQuery = now;
|
|
266
|
+
}
|
|
267
|
+
catch (error) {
|
|
268
|
+
console.warn('Failed to query mempool size:', error);
|
|
269
|
+
mempoolSize = this.cachedMempoolSize ?? 0;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
mempoolSize = this.cachedMempoolSize;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
// Default if no data available
|
|
277
|
+
if (mempoolSize === undefined) {
|
|
278
|
+
mempoolSize = 0;
|
|
279
|
+
}
|
|
280
|
+
// Determine congestion level and factor
|
|
281
|
+
const congestionLevel = this.classifyCongestion(mempoolSize);
|
|
282
|
+
const congestionFactor = this.calculateCongestionFactor(mempoolSize);
|
|
283
|
+
const estimatedWaitBlocks = this.calculateWaitBlocks(mempoolSize);
|
|
284
|
+
const recommendedPriority = this.recommendPriority(mempoolSize);
|
|
285
|
+
return {
|
|
286
|
+
mempoolSize,
|
|
287
|
+
congestionLevel,
|
|
288
|
+
congestionFactor,
|
|
289
|
+
estimatedWaitBlocks,
|
|
290
|
+
recommendedPriority,
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Select best model within budget with fee awareness.
|
|
295
|
+
*
|
|
296
|
+
* Every candidate is priced from its `meteringByModel` entry; one without an entry fails the call.
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* ```ts
|
|
300
|
+
* const selection = await estimator.selectWithinBudget({
|
|
301
|
+
* maxBudget: 5.0,
|
|
302
|
+
* discoveryResult, // from ModelDiscoveryService.discover()
|
|
303
|
+
* inputTokens: 1000,
|
|
304
|
+
* outputTokens: 500,
|
|
305
|
+
* meteringByModel,
|
|
306
|
+
* });
|
|
307
|
+
*
|
|
308
|
+
* console.log(`Selected: ${selection.selectedModel.name}`);
|
|
309
|
+
* console.log(`Fee: ${selection.estimatedFee.estimatedFee} FLOP`);
|
|
310
|
+
* console.log(`Alternatives: ${selection.alternatives.length}`);
|
|
311
|
+
* ```
|
|
312
|
+
*/
|
|
313
|
+
async selectWithinBudget(options) {
|
|
314
|
+
// Get models to evaluate
|
|
315
|
+
let models = options.models ?? options.discoveryResult?.models ?? [];
|
|
316
|
+
if (models.length === 0) {
|
|
317
|
+
throw new FeeEstimationError('No models provided for budget-aware selection', 'NO_MODELS');
|
|
318
|
+
}
|
|
319
|
+
// Estimate fees for all models
|
|
320
|
+
const feeEstimates = await Promise.all(models.map(async (model) => {
|
|
321
|
+
const fee = await this.estimateFee({
|
|
322
|
+
prompt: options.prompt,
|
|
323
|
+
model: model,
|
|
324
|
+
modelSize: model.size,
|
|
325
|
+
pricingMode: options.pricingMode ?? 'standard',
|
|
326
|
+
maxBudget: options.maxBudget,
|
|
327
|
+
inputTokens: options.inputTokens,
|
|
328
|
+
outputTokens: options.outputTokens,
|
|
329
|
+
metering: options.meteringByModel?.get(model.hash),
|
|
330
|
+
});
|
|
331
|
+
return { model, fee };
|
|
332
|
+
}));
|
|
333
|
+
// Filter by budget
|
|
334
|
+
let withinBudget = feeEstimates.filter(({ fee }) => fee.withinBudget);
|
|
335
|
+
const outOfBudget = feeEstimates.filter(({ fee }) => !fee.withinBudget);
|
|
336
|
+
// Filter by minimum quality if specified
|
|
337
|
+
if (options.minQuality !== undefined) {
|
|
338
|
+
const minQ = options.minQuality;
|
|
339
|
+
withinBudget = withinBudget.filter(({ model }) => model.qualityScore >= minQ);
|
|
340
|
+
}
|
|
341
|
+
if (withinBudget.length === 0) {
|
|
342
|
+
// Return best option even if over budget
|
|
343
|
+
const bestByQuality = feeEstimates.sort((a, b) => b.model.qualityScore - a.model.qualityScore)[0];
|
|
344
|
+
return {
|
|
345
|
+
selectedModel: bestByQuality.model,
|
|
346
|
+
estimatedFee: bestByQuality.fee,
|
|
347
|
+
alternatives: [],
|
|
348
|
+
outOfBudget: feeEstimates.map(({ model, fee }) => ({
|
|
349
|
+
model,
|
|
350
|
+
fee,
|
|
351
|
+
budgetGap: fee.estimatedFee - options.maxBudget,
|
|
352
|
+
})),
|
|
353
|
+
reason: 'All models exceed budget; selected highest quality',
|
|
354
|
+
tradeoffScore: 0,
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
// Calculate cost-quality tradeoffs
|
|
358
|
+
const tradeoffs = withinBudget.map(({ model, fee }) => this.calculateCostQualityTradeoff(model, fee.estimatedFee));
|
|
359
|
+
// Mark best value
|
|
360
|
+
const bestValue = tradeoffs.reduce((best, current) => current.qualityPerFlop > best.qualityPerFlop ? current : best);
|
|
361
|
+
bestValue.isBestValue = true;
|
|
362
|
+
// Select model based on preference
|
|
363
|
+
let selected;
|
|
364
|
+
if (options.preferBestValue) {
|
|
365
|
+
// Select best cost-quality tradeoff
|
|
366
|
+
const bestValueIdx = tradeoffs.findIndex(t => t.isBestValue);
|
|
367
|
+
selected = withinBudget[bestValueIdx];
|
|
368
|
+
}
|
|
369
|
+
else {
|
|
370
|
+
// Select highest quality within budget
|
|
371
|
+
selected = withinBudget.sort((a, b) => b.model.qualityScore - a.model.qualityScore)[0];
|
|
372
|
+
}
|
|
373
|
+
// Build alternatives list
|
|
374
|
+
const alternatives = withinBudget
|
|
375
|
+
.filter(({ model }) => model.hash !== selected.model.hash)
|
|
376
|
+
.map(({ model, fee }) => ({
|
|
377
|
+
model,
|
|
378
|
+
fee,
|
|
379
|
+
savingsVsSelected: selected.fee.estimatedFee - fee.estimatedFee,
|
|
380
|
+
qualityVsSelected: selected.model.qualityScore - model.qualityScore,
|
|
381
|
+
}))
|
|
382
|
+
.sort((a, b) => b.savingsVsSelected - a.savingsVsSelected);
|
|
383
|
+
// Build out-of-budget list
|
|
384
|
+
const outOfBudgetResult = outOfBudget.map(({ model, fee }) => ({
|
|
385
|
+
model,
|
|
386
|
+
fee,
|
|
387
|
+
budgetGap: fee.estimatedFee - options.maxBudget,
|
|
388
|
+
}));
|
|
389
|
+
// Generate selection reason
|
|
390
|
+
const reason = this.generateSelectionReason(selected, bestValue, options.preferBestValue ?? false);
|
|
391
|
+
// Calculate tradeoff score for selected
|
|
392
|
+
const selectedTradeoff = tradeoffs.find(t => t.modelHash === selected.model.hash);
|
|
393
|
+
const tradeoffScore = selectedTradeoff?.qualityPerFlop ?? 0;
|
|
394
|
+
return {
|
|
395
|
+
selectedModel: selected.model,
|
|
396
|
+
estimatedFee: selected.fee,
|
|
397
|
+
alternatives,
|
|
398
|
+
outOfBudget: outOfBudgetResult,
|
|
399
|
+
reason,
|
|
400
|
+
tradeoffScore,
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Analyze cost-quality tradeoffs for multiple models.
|
|
405
|
+
*
|
|
406
|
+
* @example
|
|
407
|
+
* ```ts
|
|
408
|
+
* const tradeoffs = await estimator.analyzeTradeoffs(models);
|
|
409
|
+
* tradeoffs.forEach(t => {
|
|
410
|
+
* console.log(`${t.modelName}: ${t.tradeoffRating}`);
|
|
411
|
+
* console.log(` Quality per FLOP: ${t.qualityPerFlop}`);
|
|
412
|
+
* });
|
|
413
|
+
* ```
|
|
414
|
+
*/
|
|
415
|
+
async analyzeTradeoffs(models, options) {
|
|
416
|
+
const estimates = await Promise.all(models.map(async (model) => {
|
|
417
|
+
const fee = await this.estimateFee({
|
|
418
|
+
...options,
|
|
419
|
+
model: model,
|
|
420
|
+
modelSize: model.size,
|
|
421
|
+
});
|
|
422
|
+
return this.calculateCostQualityTradeoff(model, fee.estimatedFee);
|
|
423
|
+
}));
|
|
424
|
+
// Mark best value
|
|
425
|
+
if (estimates.length > 0) {
|
|
426
|
+
const bestValue = estimates.reduce((best, current) => current.qualityPerFlop > best.qualityPerFlop ? current : best);
|
|
427
|
+
bestValue.isBestValue = true;
|
|
428
|
+
}
|
|
429
|
+
return estimates.sort((a, b) => b.qualityPerFlop - a.qualityPerFlop);
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Compare fees across pricing modes.
|
|
433
|
+
*
|
|
434
|
+
* @example
|
|
435
|
+
* ```ts
|
|
436
|
+
* const comparison = await estimator.comparePricingModes({
|
|
437
|
+
* prompt: 'Write Python code',
|
|
438
|
+
* modelSize: 'large',
|
|
439
|
+
* });
|
|
440
|
+
* console.log(`Standard: ${comparison.standard.estimatedFee} FLOP`);
|
|
441
|
+
* console.log(`Priority: ${comparison.priority.estimatedFee} FLOP`);
|
|
442
|
+
* console.log(`Urgent: ${comparison.urgent.estimatedFee} FLOP`);
|
|
443
|
+
* ```
|
|
444
|
+
*/
|
|
445
|
+
async comparePricingModes(options) {
|
|
446
|
+
const modes = ['economy', 'standard', 'priority', 'urgent'];
|
|
447
|
+
const results = {};
|
|
448
|
+
await Promise.all(modes.map(async (mode) => {
|
|
449
|
+
results[mode] = await this.estimateFee({
|
|
450
|
+
...options,
|
|
451
|
+
pricingMode: mode,
|
|
452
|
+
bypassFloor: true, // Bypass floor to show theoretical multiplier ratio
|
|
453
|
+
bypassNetworkFee: true, // Bypass network fee for theoretical multiplier comparisons
|
|
454
|
+
});
|
|
455
|
+
}));
|
|
456
|
+
return results;
|
|
457
|
+
}
|
|
458
|
+
// ── Private Helper Methods ─────────────────────────────────────────────
|
|
459
|
+
/**
|
|
460
|
+
* Estimate token count from prompt or options.
|
|
461
|
+
*/
|
|
462
|
+
estimateTokens(options) {
|
|
463
|
+
// Use explicit token counts if provided
|
|
464
|
+
if (options.totalTokens !== undefined) {
|
|
465
|
+
return options.totalTokens;
|
|
466
|
+
}
|
|
467
|
+
if (options.inputTokens !== undefined && options.outputTokens !== undefined) {
|
|
468
|
+
return options.inputTokens + options.outputTokens;
|
|
469
|
+
}
|
|
470
|
+
// Estimate from prompt text
|
|
471
|
+
if (options.prompt !== undefined) {
|
|
472
|
+
return this.countTokens(options.prompt);
|
|
473
|
+
}
|
|
474
|
+
// Default estimate for typical task
|
|
475
|
+
return 500; // Average task token count
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Count tokens in text (simple estimation).
|
|
479
|
+
*
|
|
480
|
+
* Note: This is a heuristic-based estimate. For accurate counting,
|
|
481
|
+
* use the model's tokenizer (tiktoken for OpenAI models, etc.)
|
|
482
|
+
*/
|
|
483
|
+
countTokens(text) {
|
|
484
|
+
// Simple heuristic: ~4 characters per token for English
|
|
485
|
+
// This is an approximation; actual token counts depend on the model
|
|
486
|
+
// Count words and adjust for punctuation/formatting
|
|
487
|
+
const words = text.split(/\s+/).filter(w => w.length > 0);
|
|
488
|
+
const punctuationCount = (text.match(/[.,!?;:]/g) ?? []).length;
|
|
489
|
+
const codeTokens = (text.match(/[{}()\[\]]/g) ?? []).length;
|
|
490
|
+
// Base estimate from words, with character fallback for few words
|
|
491
|
+
let baseTokens;
|
|
492
|
+
if (words.length <= 2 && text.length > 100) {
|
|
493
|
+
// Character-based estimation for continuous strings without spaces
|
|
494
|
+
baseTokens = Math.ceil(text.length / 4); // ~4 chars per token
|
|
495
|
+
}
|
|
496
|
+
else {
|
|
497
|
+
baseTokens = Math.ceil(words.length * 1.3);
|
|
498
|
+
}
|
|
499
|
+
// Add punctuation and special characters
|
|
500
|
+
const totalTokens = baseTokens + Math.ceil(punctuationCount * 0.5) + Math.ceil(codeTokens * 0.3);
|
|
501
|
+
// Estimate output tokens (typically 2-3x input for generation tasks)
|
|
502
|
+
const outputEstimate = Math.ceil(totalTokens * 2.5);
|
|
503
|
+
return totalTokens + outputEstimate;
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Resolve model size from options.
|
|
507
|
+
*/
|
|
508
|
+
resolveModelSize(options) {
|
|
509
|
+
if (options.modelSize !== undefined) {
|
|
510
|
+
return options.modelSize;
|
|
511
|
+
}
|
|
512
|
+
if (options.model !== undefined) {
|
|
513
|
+
if (typeof options.model === 'string') {
|
|
514
|
+
// Infer from model name
|
|
515
|
+
return this.inferSizeFromModelName(options.model);
|
|
516
|
+
}
|
|
517
|
+
else if ('size' in options.model) {
|
|
518
|
+
return options.model.size;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
// Default to medium
|
|
522
|
+
return 'medium';
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Infer model size from model name.
|
|
526
|
+
*/
|
|
527
|
+
inferSizeFromModelName(name) {
|
|
528
|
+
const lower = name.toLowerCase();
|
|
529
|
+
// Check for size indicators in name
|
|
530
|
+
if (lower.includes('7b') || lower.includes('8b') || lower.includes('small')) {
|
|
531
|
+
return 'small';
|
|
532
|
+
}
|
|
533
|
+
if (lower.includes('70b') || lower.includes('72b') || lower.includes('large') || lower.includes('llama-3-70b')) {
|
|
534
|
+
return 'large';
|
|
535
|
+
}
|
|
536
|
+
if (lower.includes('100b') || lower.includes('120b') || lower.includes('xlarge') || lower.includes('gpt-4')) {
|
|
537
|
+
return 'xlarge';
|
|
538
|
+
}
|
|
539
|
+
// Default medium for 30B-ish models
|
|
540
|
+
return 'medium';
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Resolve model name from options.
|
|
544
|
+
*/
|
|
545
|
+
resolveModelName(options) {
|
|
546
|
+
if (options.model !== undefined) {
|
|
547
|
+
if (typeof options.model === 'string') {
|
|
548
|
+
return options.model;
|
|
549
|
+
}
|
|
550
|
+
return options.model.name;
|
|
551
|
+
}
|
|
552
|
+
return `auto-${options.modelSize ?? 'medium'}`;
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* Resolve pricing mode from task priority.
|
|
556
|
+
*/
|
|
557
|
+
resolvePricingMode(priority) {
|
|
558
|
+
if (priority === undefined) {
|
|
559
|
+
return 'standard';
|
|
560
|
+
}
|
|
561
|
+
switch (priority) {
|
|
562
|
+
case TaskPriority.LOW:
|
|
563
|
+
return 'economy';
|
|
564
|
+
case TaskPriority.NORMAL:
|
|
565
|
+
return 'standard';
|
|
566
|
+
case TaskPriority.HIGH:
|
|
567
|
+
return 'priority';
|
|
568
|
+
case TaskPriority.CRITICAL:
|
|
569
|
+
return 'urgent';
|
|
570
|
+
default:
|
|
571
|
+
return 'standard';
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* Meter the request as one turn, exactly as settlement will. Refuses inputs it cannot price
|
|
576
|
+
* exactly instead of guessing a token split.
|
|
577
|
+
*/
|
|
578
|
+
meterRequest(options, metering) {
|
|
579
|
+
const { inputTokens, outputTokens } = options;
|
|
580
|
+
if (!isTokenCount(inputTokens) || !isTokenCount(outputTokens)) {
|
|
581
|
+
throw new FeeEstimationError('metering needs inputTokens and outputTokens as non-negative integers: G_n depends on the split', 'TOKEN_SPLIT_REQUIRED');
|
|
582
|
+
}
|
|
583
|
+
if (options.gflops !== undefined) {
|
|
584
|
+
throw new FeeEstimationError('gflops conflicts with metering, which meters G_n itself', 'INVALID_OPTIONS');
|
|
585
|
+
}
|
|
586
|
+
if (metering.expiresAtMs !== undefined && !(Date.now() < metering.expiresAtMs)) {
|
|
587
|
+
throw new FeeEstimationError(`metered pricing expired at ${new Date(metering.expiresAtMs).toISOString()}; rebuild metering from a live catalog price and miner status`, 'METERING_UNAVAILABLE');
|
|
588
|
+
}
|
|
589
|
+
return estimateSession({
|
|
590
|
+
...metering,
|
|
591
|
+
turns: [{ contextTokens: BigInt(inputTokens), generatedTokens: BigInt(outputTokens) }],
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
/** The escrow is the whole metered charge: no premium, floor or network fee enters it. */
|
|
595
|
+
meteredBreakdown(session, tokens) {
|
|
596
|
+
const cost = rawToFlop(session.escrowRaw);
|
|
597
|
+
return {
|
|
598
|
+
computeCost: cost,
|
|
599
|
+
networkFee: 0,
|
|
600
|
+
priorityPremium: 0,
|
|
601
|
+
congestionPremium: 0,
|
|
602
|
+
totalCost: cost,
|
|
603
|
+
costPer1kTokens: tokens === 0 ? 0 : cost / (tokens / 1000),
|
|
604
|
+
estimatedTokens: tokens,
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Calculate detailed fee breakdown.
|
|
609
|
+
*/
|
|
610
|
+
calculateFeeBreakdown(params) {
|
|
611
|
+
const { tokens, modelSize, pricingMode, priority, congestionFactor, gflops, bypassFloor, bypassNetworkFee, modelEstimatedCost } = params;
|
|
612
|
+
// Use model's estimated cost if provided (from ScoredModel)
|
|
613
|
+
// Otherwise calculate from tokens
|
|
614
|
+
let baseComputeCost;
|
|
615
|
+
if (modelEstimatedCost !== undefined) {
|
|
616
|
+
baseComputeCost = modelEstimatedCost;
|
|
617
|
+
}
|
|
618
|
+
else {
|
|
619
|
+
const costPer1kTokens = BASE_COST_PER_1K_TOKENS[modelSize];
|
|
620
|
+
baseComputeCost = (tokens / 1000) * costPer1kTokens;
|
|
621
|
+
}
|
|
622
|
+
// Add GFLOPs cost if specified (for compute-heavy tasks)
|
|
623
|
+
const gflopsCost = gflops !== undefined ? gflops * 0.01 : 0;
|
|
624
|
+
// Apply minimum fee floor unless bypassed for estimation purposes
|
|
625
|
+
// When bypassFloor is true, show actual costs without any floor
|
|
626
|
+
// When bypassFloor is false, apply the submission floor (MIN_FEE_FLOOR)
|
|
627
|
+
const computeCost = bypassFloor
|
|
628
|
+
? baseComputeCost + gflopsCost // Actual costs, no floor
|
|
629
|
+
: Math.max(MIN_FEE_FLOOR, baseComputeCost + gflopsCost);
|
|
630
|
+
// Network fee (fixed overhead) - can be bypassed for theoretical comparisons
|
|
631
|
+
const networkFee = bypassNetworkFee ? 0 : DEFAULT_NETWORK_FEE;
|
|
632
|
+
// Priority premium
|
|
633
|
+
const priorityMultiplier = priority !== undefined
|
|
634
|
+
? TASK_PRIORITY_MULTIPLIERS[priority]
|
|
635
|
+
: PRICING_MODE_MULTIPLIERS[pricingMode];
|
|
636
|
+
const priorityPremium = computeCost * (priorityMultiplier - 1);
|
|
637
|
+
// Congestion premium
|
|
638
|
+
const congestionPremium = computeCost * congestionFactor;
|
|
639
|
+
// Total cost
|
|
640
|
+
const totalCost = computeCost + networkFee + priorityPremium + congestionPremium;
|
|
641
|
+
// Use higher precision for estimation (bypass modes), lower for actual costs
|
|
642
|
+
const precision = bypassFloor || bypassNetworkFee ? 10000 : 100;
|
|
643
|
+
return {
|
|
644
|
+
computeCost: Math.round(computeCost * precision) / precision,
|
|
645
|
+
networkFee: Math.round(networkFee * precision) / precision,
|
|
646
|
+
priorityPremium: Math.round(priorityPremium * precision) / precision,
|
|
647
|
+
congestionPremium: Math.round(congestionPremium * precision) / precision,
|
|
648
|
+
totalCost: Math.round(totalCost * precision) / precision,
|
|
649
|
+
costPer1kTokens: modelEstimatedCost !== undefined
|
|
650
|
+
? modelEstimatedCost / (tokens / 1000) // Derive from estimated cost
|
|
651
|
+
: BASE_COST_PER_1K_TOKENS[modelSize],
|
|
652
|
+
estimatedTokens: tokens,
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* Classify congestion level from mempool size.
|
|
657
|
+
*/
|
|
658
|
+
classifyCongestion(mempoolSize) {
|
|
659
|
+
if (mempoolSize < CONGESTION_THRESHOLDS.low)
|
|
660
|
+
return 'low';
|
|
661
|
+
if (mempoolSize < CONGESTION_THRESHOLDS.medium)
|
|
662
|
+
return 'medium';
|
|
663
|
+
if (mempoolSize < CONGESTION_THRESHOLDS.high)
|
|
664
|
+
return 'high';
|
|
665
|
+
return 'critical';
|
|
666
|
+
}
|
|
667
|
+
/**
|
|
668
|
+
* Calculate congestion factor (0-2).
|
|
669
|
+
* For mempool sizes exceeding critical threshold, factor increases beyond 1.0.
|
|
670
|
+
*/
|
|
671
|
+
calculateCongestionFactor(mempoolSize) {
|
|
672
|
+
if (mempoolSize < CONGESTION_THRESHOLDS.low)
|
|
673
|
+
return 0;
|
|
674
|
+
if (mempoolSize < CONGESTION_THRESHOLDS.medium)
|
|
675
|
+
return 0.1;
|
|
676
|
+
if (mempoolSize < CONGESTION_THRESHOLDS.high)
|
|
677
|
+
return 0.3;
|
|
678
|
+
if (mempoolSize < CONGESTION_THRESHOLDS.critical)
|
|
679
|
+
return 0.5;
|
|
680
|
+
// For very high congestion (> critical threshold), factor scales up to 2.0
|
|
681
|
+
const excess = mempoolSize - CONGESTION_THRESHOLDS.critical;
|
|
682
|
+
const extraFactor = Math.min(1.0, excess / CONGESTION_THRESHOLDS.critical);
|
|
683
|
+
return 1.0 + extraFactor;
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Calculate estimated wait time in blocks.
|
|
687
|
+
*/
|
|
688
|
+
calculateWaitBlocks(mempoolSize) {
|
|
689
|
+
// Base wait: 5 blocks + scaling with congestion
|
|
690
|
+
const base = 5;
|
|
691
|
+
const scaling = Math.min(50, mempoolSize / 50);
|
|
692
|
+
return Math.round(base + scaling);
|
|
693
|
+
}
|
|
694
|
+
/**
|
|
695
|
+
* Recommend priority based on congestion.
|
|
696
|
+
*/
|
|
697
|
+
recommendPriority(mempoolSize) {
|
|
698
|
+
if (mempoolSize < CONGESTION_THRESHOLDS.low)
|
|
699
|
+
return TaskPriority.NORMAL;
|
|
700
|
+
if (mempoolSize < CONGESTION_THRESHOLDS.medium)
|
|
701
|
+
return TaskPriority.NORMAL;
|
|
702
|
+
if (mempoolSize < CONGESTION_THRESHOLDS.high)
|
|
703
|
+
return TaskPriority.HIGH;
|
|
704
|
+
return TaskPriority.CRITICAL;
|
|
705
|
+
}
|
|
706
|
+
/**
|
|
707
|
+
* Recommend fee based on breakdown and congestion.
|
|
708
|
+
*/
|
|
709
|
+
recommendFee(breakdown, congestion) {
|
|
710
|
+
// Recommend fee that ensures timely execution
|
|
711
|
+
if (congestion.congestionLevel === 'critical') {
|
|
712
|
+
// Add urgent premium for critical congestion
|
|
713
|
+
return Math.round(breakdown.totalCost * 1.5 * 100) / 100;
|
|
714
|
+
}
|
|
715
|
+
if (congestion.congestionLevel === 'high') {
|
|
716
|
+
// Add priority premium for high congestion
|
|
717
|
+
return Math.round(breakdown.totalCost * 1.2 * 100) / 100;
|
|
718
|
+
}
|
|
719
|
+
return breakdown.totalCost;
|
|
720
|
+
}
|
|
721
|
+
/**
|
|
722
|
+
* Calculate confidence level for estimate.
|
|
723
|
+
*/
|
|
724
|
+
calculateConfidence(options, tokens, modelSize) {
|
|
725
|
+
let confidence = 80; // Base confidence
|
|
726
|
+
// Higher confidence if tokens are explicitly provided
|
|
727
|
+
if (options.totalTokens !== undefined ||
|
|
728
|
+
(options.inputTokens !== undefined && options.outputTokens !== undefined)) {
|
|
729
|
+
confidence += 10;
|
|
730
|
+
}
|
|
731
|
+
// Lower confidence if estimating from prompt
|
|
732
|
+
if (options.prompt !== undefined && options.totalTokens === undefined) {
|
|
733
|
+
confidence -= 5;
|
|
734
|
+
}
|
|
735
|
+
// Lower confidence for larger models (more variability)
|
|
736
|
+
if (modelSize === 'xlarge') {
|
|
737
|
+
confidence -= 10;
|
|
738
|
+
}
|
|
739
|
+
// Lower confidence without congestion data
|
|
740
|
+
if (options.mempoolSize === undefined && !this.api) {
|
|
741
|
+
confidence -= 10;
|
|
742
|
+
}
|
|
743
|
+
// Higher confidence with known model
|
|
744
|
+
if (options.model !== undefined) {
|
|
745
|
+
confidence += 5;
|
|
746
|
+
}
|
|
747
|
+
return Math.min(100, Math.max(50, confidence));
|
|
748
|
+
}
|
|
749
|
+
/**
|
|
750
|
+
* Calculate cost-quality tradeoff for a model.
|
|
751
|
+
*/
|
|
752
|
+
calculateCostQualityTradeoff(model, cost) {
|
|
753
|
+
const costPerQualityPoint = cost / model.qualityScore;
|
|
754
|
+
const qualityPerFlop = model.qualityScore / cost;
|
|
755
|
+
// Determine tradeoff rating
|
|
756
|
+
let tradeoffRating;
|
|
757
|
+
if (qualityPerFlop > 20) {
|
|
758
|
+
tradeoffRating = 'excellent';
|
|
759
|
+
}
|
|
760
|
+
else if (qualityPerFlop > 10) {
|
|
761
|
+
tradeoffRating = 'good';
|
|
762
|
+
}
|
|
763
|
+
else if (qualityPerFlop > 5) {
|
|
764
|
+
tradeoffRating = 'fair';
|
|
765
|
+
}
|
|
766
|
+
else {
|
|
767
|
+
tradeoffRating = 'poor';
|
|
768
|
+
}
|
|
769
|
+
return {
|
|
770
|
+
modelHash: model.hash,
|
|
771
|
+
modelName: model.name,
|
|
772
|
+
qualityScore: model.qualityScore,
|
|
773
|
+
estimatedCost: cost,
|
|
774
|
+
costPerQualityPoint,
|
|
775
|
+
qualityPerFlop,
|
|
776
|
+
tradeoffRating,
|
|
777
|
+
};
|
|
778
|
+
}
|
|
779
|
+
/**
|
|
780
|
+
* Generate selection reason text.
|
|
781
|
+
*/
|
|
782
|
+
generateSelectionReason(selected, bestValue, preferBestValue) {
|
|
783
|
+
if (preferBestValue && bestValue.modelHash === selected.model.hash) {
|
|
784
|
+
return `Best cost-quality tradeoff: ${selected.model.name} delivers ${selected.model.qualityScore}% quality at ${selected.fee.estimatedFee} FLOP (${bestValue.qualityPerFlop.toFixed(1)} quality/FLOP)`;
|
|
785
|
+
}
|
|
786
|
+
return `Highest quality within budget: ${selected.model.name} at ${selected.model.qualityScore}% quality for ${selected.fee.estimatedFee} FLOP`;
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
// ── Errors ──────────────────────────────────────────────────────────────
|
|
790
|
+
/**
|
|
791
|
+
* Fee estimation error.
|
|
792
|
+
*/
|
|
793
|
+
export class FeeEstimationError extends Error {
|
|
794
|
+
code;
|
|
795
|
+
details;
|
|
796
|
+
constructor(message, code, detailsOrCause) {
|
|
797
|
+
super(message);
|
|
798
|
+
this.code = code;
|
|
799
|
+
this.name = 'FeeEstimationError';
|
|
800
|
+
// Handle overloaded parameter: if it's an Error, set cause; if object, set details
|
|
801
|
+
if (detailsOrCause instanceof Error) {
|
|
802
|
+
this.cause = detailsOrCause;
|
|
803
|
+
}
|
|
804
|
+
else if (detailsOrCause) {
|
|
805
|
+
this.details = detailsOrCause;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
// ── Convenience Functions ─────────────────────────────────────────────────
|
|
810
|
+
/**
|
|
811
|
+
* Create a fee estimator with default configuration.
|
|
812
|
+
*/
|
|
813
|
+
export function createFeeEstimator(api, options) {
|
|
814
|
+
return new FeeEstimator({ api, ...options });
|
|
815
|
+
}
|
|
816
|
+
/**
|
|
817
|
+
* Quick inference/session fee estimation helper.
|
|
818
|
+
*
|
|
819
|
+
* @param options - Object with prompt, modelSize, pricingMode, maxBudget, api
|
|
820
|
+
* @example
|
|
821
|
+
* ```ts
|
|
822
|
+
* const result = await estimateInferenceFee({ prompt: 'Test', modelSize: 'large' });
|
|
823
|
+
* ```
|
|
824
|
+
*/
|
|
825
|
+
export async function estimateInferenceFee(options) {
|
|
826
|
+
const estimator = createFeeEstimator(options.api);
|
|
827
|
+
return estimator.estimateFee({
|
|
828
|
+
prompt: options.prompt ?? '',
|
|
829
|
+
modelSize: options.modelSize ?? 'medium',
|
|
830
|
+
pricingMode: options.pricingMode ?? 'standard',
|
|
831
|
+
maxBudget: options.maxBudget,
|
|
832
|
+
inputTokens: options.inputTokens,
|
|
833
|
+
outputTokens: options.outputTokens,
|
|
834
|
+
metering: options.metering,
|
|
835
|
+
});
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
* Quick budget-aware selection helper.
|
|
839
|
+
*
|
|
840
|
+
* @param options - Object with models, maxBudget, prompt, pricingMode, preferBestValue, api
|
|
841
|
+
* @example
|
|
842
|
+
* ```ts
|
|
843
|
+
* const selection = await selectModelWithinBudget({ models, maxBudget: 5.0, inputTokens: 1000, outputTokens: 500, meteringByModel });
|
|
844
|
+
* ```
|
|
845
|
+
*/
|
|
846
|
+
export async function selectModelWithinBudget(options) {
|
|
847
|
+
const estimator = createFeeEstimator(options.api);
|
|
848
|
+
return estimator.selectWithinBudget({
|
|
849
|
+
models: options.models,
|
|
850
|
+
maxBudget: options.maxBudget,
|
|
851
|
+
prompt: options.prompt,
|
|
852
|
+
pricingMode: options.pricingMode,
|
|
853
|
+
preferBestValue: options.preferBestValue,
|
|
854
|
+
minQuality: options.minQuality,
|
|
855
|
+
inputTokens: options.inputTokens,
|
|
856
|
+
outputTokens: options.outputTokens,
|
|
857
|
+
meteringByModel: options.meteringByModel,
|
|
858
|
+
});
|
|
859
|
+
}
|
|
860
|
+
/**
|
|
861
|
+
* Legacy signature for backward compatibility.
|
|
862
|
+
* @deprecated Use object-based signature instead
|
|
863
|
+
*/
|
|
864
|
+
export async function selectModelWithinBudgetLegacy(models, maxBudget, options) {
|
|
865
|
+
return selectModelWithinBudget({ models, maxBudget, ...options });
|
|
866
|
+
}
|
|
867
|
+
/**
|
|
868
|
+
* Quick pricing mode comparison helper.
|
|
869
|
+
*
|
|
870
|
+
* @param options - Object with prompt, modelSize, api
|
|
871
|
+
* @example
|
|
872
|
+
* ```ts
|
|
873
|
+
* const comparison = await compareFeeModes({ prompt: 'Test', modelSize: 'large' });
|
|
874
|
+
* ```
|
|
875
|
+
*/
|
|
876
|
+
export async function compareFeeModes(options) {
|
|
877
|
+
const estimator = createFeeEstimator(options.api);
|
|
878
|
+
return estimator.comparePricingModes({
|
|
879
|
+
prompt: options.prompt ?? '',
|
|
880
|
+
modelSize: options.modelSize ?? 'medium',
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
/**
|
|
884
|
+
* Legacy signature for backward compatibility.
|
|
885
|
+
* @deprecated Use object-based signature instead
|
|
886
|
+
*/
|
|
887
|
+
export async function compareFeeModesLegacy(prompt, modelSize = 'medium', api) {
|
|
888
|
+
return compareFeeModes({ prompt, modelSize, api });
|
|
889
|
+
}
|
|
890
|
+
/**
|
|
891
|
+
* Get network congestion info helper.
|
|
892
|
+
*
|
|
893
|
+
* @param mempoolOverrideOrApi - Either a number for mempool override, or ApiPromise
|
|
894
|
+
* @param api - ApiPromise (if first param is mempool override)
|
|
895
|
+
* @example
|
|
896
|
+
* ```ts
|
|
897
|
+
* // With mempool override
|
|
898
|
+
* const info = await getNetworkCongestionInfo(100);
|
|
899
|
+
*
|
|
900
|
+
* // With API
|
|
901
|
+
* const info = await getNetworkCongestionInfo(api);
|
|
902
|
+
*
|
|
903
|
+
* // Both
|
|
904
|
+
* const info = await getNetworkCongestionInfo(100, api);
|
|
905
|
+
* ```
|
|
906
|
+
*/
|
|
907
|
+
export async function getNetworkCongestionInfo(mempoolOverrideOrApi, api) {
|
|
908
|
+
// Handle overloaded signature
|
|
909
|
+
let mempoolOverride;
|
|
910
|
+
let actualApi;
|
|
911
|
+
if (typeof mempoolOverrideOrApi === 'number') {
|
|
912
|
+
mempoolOverride = mempoolOverrideOrApi;
|
|
913
|
+
actualApi = api;
|
|
914
|
+
}
|
|
915
|
+
else {
|
|
916
|
+
actualApi = mempoolOverrideOrApi;
|
|
917
|
+
}
|
|
918
|
+
const estimator = createFeeEstimator(actualApi);
|
|
919
|
+
return estimator.getNetworkCongestion(mempoolOverride);
|
|
920
|
+
}
|
|
921
|
+
//# sourceMappingURL=fee-estimation.js.map
|