flop-sdk 0.2.0__py3-none-any.whl

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.
flop_sdk/__init__.py ADDED
@@ -0,0 +1,299 @@
1
+ """
2
+ FLOP Network Python SDK
3
+
4
+ Async Python library for interacting with the FLOP Network.
5
+ """
6
+
7
+ from .agent_wallet import (
8
+ AgentWalletConfig,
9
+ OnChainSessionInfo,
10
+ OnChainWalletInfo,
11
+ WalletGuard,
12
+ generate_session_keypair,
13
+ get_owner_wallet_count,
14
+ get_session_info,
15
+ get_total_wallets,
16
+ get_wallet_info,
17
+ )
18
+ from .agent_wallet import (
19
+ agent_transfer as agent_transfer_onchain,
20
+ )
21
+ from .agent_wallet import (
22
+ create_agent_wallet as create_agent_wallet_onchain,
23
+ )
24
+ from .agent_wallet import (
25
+ create_session as create_session_onchain,
26
+ )
27
+ from .agent_wallet import (
28
+ deactivate_wallet as deactivate_wallet_onchain,
29
+ )
30
+ from .agent_wallet import (
31
+ execute_via_session as execute_via_session_onchain,
32
+ )
33
+ from .agent_wallet import (
34
+ fund_agent_wallet as fund_agent_wallet_onchain,
35
+ )
36
+ from .agent_wallet import (
37
+ owner_withdraw as owner_withdraw_onchain,
38
+ )
39
+ from .agent_wallet import (
40
+ reactivate_wallet as reactivate_wallet_onchain,
41
+ )
42
+ from .agent_wallet import (
43
+ revoke_session as revoke_session_onchain,
44
+ )
45
+ from .client import (
46
+ SUPPORTED_SPEC_VERSION_RANGE,
47
+ SUPPORTED_SPEC_VERSIONS,
48
+ FlopClient,
49
+ FLOPError,
50
+ )
51
+ from .compute_channel import (
52
+ TranscriptLeafVersion,
53
+ TurnRecord,
54
+ channel_id_v1,
55
+ compact_u32,
56
+ decode_compact_u32,
57
+ decode_transcript_blob,
58
+ encode_transcript_blob,
59
+ encode_verified_turn,
60
+ h_ids,
61
+ leaf_preimage,
62
+ merkle_path,
63
+ merkle_root,
64
+ receipt_message_v1,
65
+ root_from_path,
66
+ task_hash_v1,
67
+ transcript_leaf,
68
+ )
69
+ from .da_client import (
70
+ PROVIDER_SOVEREIGN_VALIDATORS,
71
+ lease_deposit,
72
+ leased_data_ref,
73
+ reactivate_blob,
74
+ register_blob,
75
+ set_model_weights_ref,
76
+ top_up_lease,
77
+ upload_model_weights,
78
+ withdraw_lease,
79
+ )
80
+ from .hardware import (
81
+ NvidiaGpuInfo,
82
+ detect_nvidia_gpus,
83
+ get_gpu_info,
84
+ has_cuda_support,
85
+ is_nvidia_available,
86
+ )
87
+ from .staking_client import (
88
+ claim_unbonded,
89
+ delegate_stake,
90
+ deregister_miner,
91
+ get_block_rewards_info,
92
+ get_delegator_stake,
93
+ get_halving_info,
94
+ get_miner_contribution,
95
+ get_miner_info,
96
+ get_total_miners,
97
+ get_total_stake,
98
+ get_unbonding_requests,
99
+ register_miner,
100
+ set_commission,
101
+ set_miner_status,
102
+ unbond,
103
+ )
104
+ from .staking_types import (
105
+ BlockRewardsInfo,
106
+ DelegatorStake,
107
+ HalvingInfo,
108
+ MinerContribution,
109
+ MinerInfo,
110
+ StakingConstants,
111
+ UnbondingRequest,
112
+ perbill_to_percent,
113
+ percent_to_perbill,
114
+ )
115
+ from .stream import (
116
+ FlopStream,
117
+ StreamEvent,
118
+ StreamHandle,
119
+ StreamOptions,
120
+ )
121
+ from .substrate import (
122
+ SessionInfo,
123
+ SubstrateClient,
124
+ SubstrateOptions,
125
+ WalletInfo,
126
+ agent_transfer,
127
+ create_agent_wallet,
128
+ create_session,
129
+ execute_via_session,
130
+ fund_agent_wallet,
131
+ get_agent_wallet,
132
+ owner_withdraw,
133
+ revoke_session,
134
+ )
135
+ from .types import (
136
+ AccountInfo,
137
+ BuyInferenceParams,
138
+ ComputeTask,
139
+ CreateHtlcResult,
140
+ FlopClientOptions,
141
+ HealthResponse,
142
+ HtlcInfo,
143
+ HtlcStatus,
144
+ InferenceReceipt,
145
+ PoUISubmission,
146
+ TeeQuote,
147
+ )
148
+ from .utils import (
149
+ flop_to_raw,
150
+ format_address,
151
+ format_flop,
152
+ generate_agent_id,
153
+ generate_htlc_secrets,
154
+ raw_to_flop,
155
+ sign_message,
156
+ validate_address,
157
+ )
158
+ from .x402 import (
159
+ PaymentInstructions,
160
+ X402FetchOptions,
161
+ build_payment_headers,
162
+ generate_payment_secret,
163
+ parse_payment_headers,
164
+ x402_fetch,
165
+ )
166
+
167
+ __version__ = "0.2.0"
168
+ __all__ = [
169
+ # Client
170
+ "FlopClient",
171
+ "SubstrateClient",
172
+ "SubstrateOptions",
173
+ "FLOPError",
174
+ "SUPPORTED_SPEC_VERSION_RANGE",
175
+ "SUPPORTED_SPEC_VERSIONS",
176
+ # Types
177
+ "FlopClientOptions",
178
+ "HtlcStatus",
179
+ "HtlcInfo",
180
+ "ComputeTask",
181
+ "TeeQuote",
182
+ "CreateHtlcResult",
183
+ "AccountInfo",
184
+ "HealthResponse",
185
+ "PoUISubmission",
186
+ "WalletInfo",
187
+ "SessionInfo",
188
+ "NvidiaGpuInfo",
189
+ # Wallet
190
+ "create_agent_wallet",
191
+ "fund_agent_wallet",
192
+ "agent_transfer",
193
+ "owner_withdraw",
194
+ "create_session",
195
+ "revoke_session",
196
+ "execute_via_session",
197
+ "get_agent_wallet",
198
+ # Utils
199
+ "generate_htlc_secrets",
200
+ "sign_message",
201
+ "format_flop",
202
+ "flop_to_raw",
203
+ "raw_to_flop",
204
+ "format_address",
205
+ "validate_address",
206
+ "generate_agent_id",
207
+ # Hardware
208
+ "detect_nvidia_gpus",
209
+ "get_gpu_info",
210
+ "is_nvidia_available",
211
+ "has_cuda_support",
212
+ # x402
213
+ "PaymentInstructions",
214
+ "X402FetchOptions",
215
+ "build_payment_headers",
216
+ "generate_payment_secret",
217
+ "parse_payment_headers",
218
+ "x402_fetch",
219
+ # Agent Wallet (on-chain)
220
+ "AgentWalletConfig",
221
+ "OnChainWalletInfo",
222
+ "OnChainSessionInfo",
223
+ "WalletGuard",
224
+ "generate_session_keypair",
225
+ "create_agent_wallet_onchain",
226
+ "fund_agent_wallet_onchain",
227
+ "agent_transfer_onchain",
228
+ "owner_withdraw_onchain",
229
+ "deactivate_wallet_onchain",
230
+ "reactivate_wallet_onchain",
231
+ "get_wallet_info",
232
+ "get_total_wallets",
233
+ "get_owner_wallet_count",
234
+ "create_session_onchain",
235
+ "revoke_session_onchain",
236
+ "execute_via_session_onchain",
237
+ "get_session_info",
238
+ # Streaming
239
+ "FlopStream",
240
+ "StreamHandle",
241
+ "StreamOptions",
242
+ "StreamEvent",
243
+ # New Types
244
+ "InferenceReceipt",
245
+ "BuyInferenceParams",
246
+ # Staking & Block Rewards
247
+ "MinerInfo",
248
+ "DelegatorStake",
249
+ "UnbondingRequest",
250
+ "StakingConstants",
251
+ "BlockRewardsInfo",
252
+ "HalvingInfo",
253
+ "MinerContribution",
254
+ "perbill_to_percent",
255
+ "percent_to_perbill",
256
+ # Staking Client
257
+ "register_miner",
258
+ "deregister_miner",
259
+ "delegate_stake",
260
+ "unbond",
261
+ "claim_unbonded",
262
+ "set_commission",
263
+ "set_miner_status",
264
+ "get_miner_info",
265
+ "get_delegator_stake",
266
+ "get_unbonding_requests",
267
+ "get_total_stake",
268
+ "get_total_miners",
269
+ "get_block_rewards_info",
270
+ "get_halving_info",
271
+ "get_miner_contribution",
272
+ # DA Registry Client
273
+ "PROVIDER_SOVEREIGN_VALIDATORS",
274
+ "leased_data_ref",
275
+ "lease_deposit",
276
+ "register_blob",
277
+ "top_up_lease",
278
+ "withdraw_lease",
279
+ "reactivate_blob",
280
+ "set_model_weights_ref",
281
+ "upload_model_weights",
282
+ # Compute channel
283
+ "TranscriptLeafVersion",
284
+ "TurnRecord",
285
+ "channel_id_v1",
286
+ "compact_u32",
287
+ "decode_compact_u32",
288
+ "decode_transcript_blob",
289
+ "encode_transcript_blob",
290
+ "encode_verified_turn",
291
+ "h_ids",
292
+ "leaf_preimage",
293
+ "merkle_path",
294
+ "merkle_root",
295
+ "receipt_message_v1",
296
+ "root_from_path",
297
+ "task_hash_v1",
298
+ "transcript_leaf",
299
+ ]
@@ -0,0 +1,158 @@
1
+ """
2
+ Agent SDK - Error Codes and Error Classes
3
+
4
+ Machine-readable error handling for autonomous agents.
5
+ All errors include error codes for programmatic handling.
6
+ """
7
+
8
+ from dataclasses import dataclass, field
9
+ from enum import Enum
10
+ from typing import Any
11
+
12
+
13
+ class AgentErrorCode(Enum):
14
+ """Error code taxonomy for agent operations."""
15
+
16
+ # Session Errors (S001-S099)
17
+ SESSION_EXPIRED = "S001"
18
+ SPENDING_LIMIT_EXCEEDED = "S002"
19
+ INVALID_DELEGATE = "S003"
20
+ PALLET_NOT_ALLOWED = "S004"
21
+
22
+ # Retry Errors (R001-R099)
23
+ MAX_RETRIES_EXCEEDED = "R001"
24
+ BACKOFF_TIMEOUT = "R002"
25
+ NON_RETRYABLE_ERROR = "R003"
26
+
27
+ # Budget Errors (B001-B099)
28
+ INSUFFICIENT_FUNDS = "B001"
29
+ BUDGET_EXHAUSTED = "B002"
30
+ COST_ESTIMATE_MISMATCH = "B003"
31
+
32
+ # Batch Errors (K001-K099)
33
+ BATCH_PARTIAL_FAILURE = "K001"
34
+ BATCH_TIMEOUT = "K002"
35
+ INVALID_BATCH_OPERATION = "K003"
36
+
37
+ # Event Errors (E001-E099)
38
+ EVENT_STREAM_DISCONNECTED = "E001"
39
+ EVENT_TIMEOUT = "E002"
40
+ INVALID_EVENT_PAYLOAD = "E003"
41
+
42
+ # Coordination Errors (C001-C099)
43
+ AGENT_NOT_FOUND = "C001"
44
+ COORDINATION_TIMEOUT = "C002"
45
+ INVALID_COORDINATION_REQUEST = "C003"
46
+
47
+
48
+ @dataclass
49
+ class AgentError(Exception):
50
+ """Base agent error with machine-readable code."""
51
+
52
+ code: AgentErrorCode
53
+ message: str
54
+ retryable: bool = False
55
+ retry_after_ms: int | None = None
56
+ context: dict[str, Any] | None = field(default_factory=dict)
57
+
58
+ def __post_init__(self):
59
+ super().__init__(f"[{self.code.value}] {self.message}")
60
+
61
+
62
+ class SessionExpiredError(AgentError):
63
+ """Session expired - needs renewal."""
64
+
65
+ def __init__(self, message: str = "Session expired"):
66
+ super().__init__(
67
+ code=AgentErrorCode.SESSION_EXPIRED,
68
+ message=message,
69
+ retryable=True,
70
+ retry_after_ms=5000,
71
+ )
72
+
73
+
74
+ class SpendingLimitExceededError(AgentError):
75
+ """Spending limit exceeded."""
76
+
77
+ def __init__(self, available: float, required: float, shortfall: float):
78
+ super().__init__(
79
+ code=AgentErrorCode.SPENDING_LIMIT_EXCEEDED,
80
+ message=f"Available: {available} FLOP, Required: {required} FLOP, Shortfall: {shortfall} FLOP",
81
+ retryable=False,
82
+ context={"available": available, "required": required, "shortfall": shortfall},
83
+ )
84
+
85
+
86
+ class InsufficientFundsError(AgentError):
87
+ """Insufficient funds for operation."""
88
+
89
+ def __init__(self, available: float, required: float):
90
+ super().__init__(
91
+ code=AgentErrorCode.INSUFFICIENT_FUNDS,
92
+ message=f"Available: {available} FLOP, Required: {required} FLOP",
93
+ retryable=False,
94
+ context={
95
+ "available": available,
96
+ "required": required,
97
+ "shortfall": required - available,
98
+ },
99
+ )
100
+
101
+
102
+ class MaxRetriesExceededError(AgentError):
103
+ """Maximum retry attempts exceeded."""
104
+
105
+ def __init__(self, attempts: int, last_error: Exception | None = None):
106
+ super().__init__(
107
+ code=AgentErrorCode.MAX_RETRIES_EXCEEDED,
108
+ message=f"Max retries exceeded after {attempts} attempts",
109
+ retryable=False,
110
+ context={"attempts": attempts, "last_error": str(last_error) if last_error else None},
111
+ )
112
+
113
+
114
+ class BatchPartialFailureError(AgentError):
115
+ """Batch operation had partial failures."""
116
+
117
+ def __init__(
118
+ self,
119
+ total: int,
120
+ success_count: int,
121
+ failure_count: int,
122
+ failures: list[dict[str, Any]] | None = None,
123
+ ):
124
+ super().__init__(
125
+ code=AgentErrorCode.BATCH_PARTIAL_FAILURE,
126
+ message=f"Batch partial failure: {success_count}/{total} succeeded",
127
+ retryable=False,
128
+ context={
129
+ "total": total,
130
+ "success_count": success_count,
131
+ "failure_count": failure_count,
132
+ "failures": failures or [],
133
+ },
134
+ )
135
+
136
+
137
+ class EventStreamDisconnectedError(AgentError):
138
+ """Event stream disconnected."""
139
+
140
+ def __init__(self, message: str = "Event stream disconnected"):
141
+ super().__init__(
142
+ code=AgentErrorCode.EVENT_STREAM_DISCONNECTED,
143
+ message=message,
144
+ retryable=True,
145
+ retry_after_ms=10000,
146
+ )
147
+
148
+
149
+ __all__ = [
150
+ "AgentErrorCode",
151
+ "AgentError",
152
+ "SessionExpiredError",
153
+ "SpendingLimitExceededError",
154
+ "InsufficientFundsError",
155
+ "MaxRetriesExceededError",
156
+ "BatchPartialFailureError",
157
+ "EventStreamDisconnectedError",
158
+ ]