intentkit 0.7.1.dev3__py3-none-any.whl → 0.7.2.dev1__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.

Potentially problematic release.


This version of intentkit might be problematic. Click here for more details.

intentkit/__init__.py CHANGED
@@ -3,7 +3,7 @@
3
3
  A powerful platform for building AI agents with blockchain and cryptocurrency capabilities.
4
4
  """
5
5
 
6
- __version__ = "0.7.1-dev3"
6
+ __version__ = "0.7.2-dev1"
7
7
  __author__ = "hyacinthus"
8
8
  __email__ = "hyacinthus@gmail.com"
9
9
 
intentkit/core/credit.py CHANGED
@@ -22,6 +22,7 @@ from intentkit.models.credit import (
22
22
  DEFAULT_PLATFORM_ACCOUNT_REFILL,
23
23
  DEFAULT_PLATFORM_ACCOUNT_REWARD,
24
24
  DEFAULT_PLATFORM_ACCOUNT_SKILL,
25
+ DEFAULT_PLATFORM_ACCOUNT_WITHDRAW,
25
26
  CreditAccount,
26
27
  CreditAccountTable,
27
28
  CreditDebit,
@@ -212,6 +213,166 @@ async def recharge(
212
213
  return user_account
213
214
 
214
215
 
216
+ async def withdraw(
217
+ session: AsyncSession,
218
+ agent_id: str,
219
+ amount: Decimal,
220
+ upstream_tx_id: str,
221
+ note: Optional[str] = None,
222
+ ) -> CreditAccount:
223
+ """
224
+ Withdraw credits from an agent account to platform account.
225
+
226
+ Args:
227
+ session: Async session to use for database operations
228
+ agent_id: ID of the agent to withdraw from
229
+ amount: Amount of credits to withdraw
230
+ upstream_tx_id: ID of the upstream transaction
231
+ note: Optional note for the transaction
232
+
233
+ Returns:
234
+ Updated agent credit account
235
+ """
236
+ # Check for idempotency - prevent duplicate transactions
237
+ await CreditEvent.check_upstream_tx_id_exists(
238
+ session, UpstreamType.API, upstream_tx_id
239
+ )
240
+
241
+ if amount <= Decimal("0"):
242
+ raise ValueError("Withdraw amount must be positive")
243
+
244
+ # Get agent to retrieve user_id from agent.owner
245
+ agent = await Agent.get(agent_id)
246
+ if not agent:
247
+ raise HTTPException(status_code=404, detail="Agent not found")
248
+
249
+ if not agent.owner:
250
+ raise HTTPException(status_code=400, detail="Agent has no owner")
251
+
252
+ # Get agent wallet address
253
+ agent_data = await AgentData.get(agent.id)
254
+ agent_wallet_address = agent_data.evm_wallet_address if agent_data else None
255
+
256
+ user_id = agent.owner
257
+
258
+ # Get agent account to check balance
259
+ agent_account = await CreditAccount.get_in_session(
260
+ session=session,
261
+ owner_type=OwnerType.AGENT,
262
+ owner_id=agent_id,
263
+ )
264
+
265
+ # Check if agent has sufficient permanent credits
266
+ if agent_account.credits < amount:
267
+ raise HTTPException(
268
+ status_code=400,
269
+ detail=f"Insufficient balance. Available: {agent_account.credits}, Required: {amount}",
270
+ )
271
+
272
+ # 1. Create credit event record first to get event_id
273
+ event_id = str(XID())
274
+
275
+ # 2. Update agent account - deduct credits
276
+ updated_agent_account = await CreditAccount.deduction_in_session(
277
+ session=session,
278
+ owner_type=OwnerType.AGENT,
279
+ owner_id=agent_id,
280
+ credit_type=CreditType.PERMANENT,
281
+ amount=amount,
282
+ event_id=event_id,
283
+ )
284
+
285
+ # 3. Update platform withdraw account - add credits
286
+ platform_account = await CreditAccount.income_in_session(
287
+ session=session,
288
+ owner_type=OwnerType.PLATFORM,
289
+ owner_id=DEFAULT_PLATFORM_ACCOUNT_WITHDRAW,
290
+ amount_details={
291
+ CreditType.PERMANENT: amount
292
+ }, # Withdraw adds to platform permanent credits
293
+ event_id=event_id,
294
+ )
295
+
296
+ # 4. Create credit event record
297
+ event = CreditEventTable(
298
+ id=event_id,
299
+ event_type=EventType.WITHDRAW,
300
+ user_id=user_id,
301
+ upstream_type=UpstreamType.API,
302
+ upstream_tx_id=upstream_tx_id,
303
+ direction=Direction.EXPENSE,
304
+ account_id=updated_agent_account.id,
305
+ total_amount=amount,
306
+ credit_type=CreditType.PERMANENT,
307
+ credit_types=[CreditType.PERMANENT],
308
+ balance_after=updated_agent_account.credits
309
+ + updated_agent_account.free_credits
310
+ + updated_agent_account.reward_credits,
311
+ base_amount=amount,
312
+ base_original_amount=amount,
313
+ base_free_amount=Decimal("0"), # No free credits involved in base amount
314
+ base_reward_amount=Decimal("0"), # No reward credits involved in base amount
315
+ base_permanent_amount=amount, # All base amount is permanent for withdraw
316
+ permanent_amount=amount, # Set permanent_amount since this is a permanent credit
317
+ free_amount=Decimal("0"), # No free credits involved
318
+ reward_amount=Decimal("0"), # No reward credits involved
319
+ agent_wallet_address=agent_wallet_address, # Include agent wallet address
320
+ note=note,
321
+ )
322
+ session.add(event)
323
+ await session.flush()
324
+
325
+ # 5. Create credit transaction records
326
+ # 5.1 Agent account transaction (debit)
327
+ agent_tx = CreditTransactionTable(
328
+ id=str(XID()),
329
+ account_id=updated_agent_account.id,
330
+ event_id=event_id,
331
+ tx_type=TransactionType.WITHDRAW,
332
+ credit_debit=CreditDebit.DEBIT,
333
+ change_amount=amount,
334
+ credit_type=CreditType.PERMANENT,
335
+ free_amount=Decimal("0"),
336
+ reward_amount=Decimal("0"),
337
+ permanent_amount=amount,
338
+ )
339
+ session.add(agent_tx)
340
+
341
+ # 5.2 Platform withdraw account transaction (credit)
342
+ platform_tx = CreditTransactionTable(
343
+ id=str(XID()),
344
+ account_id=platform_account.id,
345
+ event_id=event_id,
346
+ tx_type=TransactionType.WITHDRAW,
347
+ credit_debit=CreditDebit.CREDIT,
348
+ change_amount=amount,
349
+ credit_type=CreditType.PERMANENT,
350
+ free_amount=Decimal("0"),
351
+ reward_amount=Decimal("0"),
352
+ permanent_amount=amount,
353
+ )
354
+ session.add(platform_tx)
355
+
356
+ # Commit all changes
357
+ await session.commit()
358
+
359
+ # Send Slack notification for withdraw
360
+ try:
361
+ send_slack_message(
362
+ f"💸 **Credit Withdraw**\n"
363
+ f"• Agent ID: `{agent_id}`\n"
364
+ f"• User ID: `{user_id}`\n"
365
+ f"• Amount: `{amount}` credits\n"
366
+ f"• Transaction ID: `{upstream_tx_id}`\n"
367
+ f"• New Balance: `{updated_agent_account.credits}` credits\n"
368
+ f"• Note: {note or 'N/A'}"
369
+ )
370
+ except Exception as e:
371
+ logger.error(f"Failed to send Slack notification for withdraw: {str(e)}")
372
+
373
+ return updated_agent_account
374
+
375
+
215
376
  async def reward(
216
377
  session: AsyncSession,
217
378
  user_id: str,
@@ -60,6 +60,7 @@ DEFAULT_PLATFORM_ACCOUNT_VOICE = "platform_voice"
60
60
  DEFAULT_PLATFORM_ACCOUNT_KNOWLEDGE = "platform_knowledge"
61
61
  DEFAULT_PLATFORM_ACCOUNT_FEE = "platform_fee"
62
62
  DEFAULT_PLATFORM_ACCOUNT_DEV = "platform_dev"
63
+ DEFAULT_PLATFORM_ACCOUNT_WITHDRAW = "platform_withdraw"
63
64
 
64
65
 
65
66
  class CreditAccountTable(Base):
@@ -735,7 +736,7 @@ class CreditAccount(BaseModel):
735
736
  id=str(XID()),
736
737
  account_id=account.id,
737
738
  event_id=event_id,
738
- tx_type=TransactionType.RECHARGE,
739
+ tx_type=TransactionType.REFILL,
739
740
  credit_debit=CreditDebit.CREDIT,
740
741
  change_amount=free_quota,
741
742
  credit_type=CreditType.FREE,
@@ -865,6 +866,7 @@ class EventType(str, Enum):
865
866
  REFUND = "refund"
866
867
  ADJUSTMENT = "adjustment"
867
868
  REFILL = "refill"
869
+ WITHDRAW = "withdraw"
868
870
  # Sync with RewardType values
869
871
  REWARD = "reward"
870
872
  EVENT_REWARD = "event_reward"
@@ -1378,6 +1380,7 @@ class TransactionType(str, Enum):
1378
1380
  REFUND = "refund"
1379
1381
  ADJUSTMENT = "adjustment"
1380
1382
  REFILL = "refill"
1383
+ WITHDRAW = "withdraw"
1381
1384
  # Sync with RewardType values
1382
1385
  REWARD = "reward"
1383
1386
  EVENT_REWARD = "event_reward"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: intentkit
3
- Version: 0.7.1.dev3
3
+ Version: 0.7.2.dev1
4
4
  Summary: Intent-based AI Agent Platform - Core Package
5
5
  Project-URL: Homepage, https://github.com/crestalnetwork/intentkit
6
6
  Project-URL: Repository, https://github.com/crestalnetwork/intentkit
@@ -1,4 +1,4 @@
1
- intentkit/__init__.py,sha256=Vy_-mTwSa2SkcSN21QaZzVvTHhR_hPQEer29UVDb2fw,383
1
+ intentkit/__init__.py,sha256=6Irgq-hTcw_LXvAzBoQzdKrpqnaXVn1ju6AtdJ_Xtpk,383
2
2
  intentkit/abstracts/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
3
  intentkit/abstracts/agent.py,sha256=108gb5W8Q1Sy4G55F2_ZFv2-_CnY76qrBtpIr0Oxxqk,1489
4
4
  intentkit/abstracts/api.py,sha256=ZUc24vaQvQVbbjznx7bV0lbbQxdQPfEV8ZxM2R6wZWo,166
@@ -17,7 +17,7 @@ intentkit/core/agent.py,sha256=GIKDn1dTenIHWMRxe-ud7hd1cQaHzbTDdypy5IAgPfU,16658
17
17
  intentkit/core/api.py,sha256=WfoaHNquujYJIpNPuTR1dSaaxog0S3X2W4lG9Ehmkm4,3284
18
18
  intentkit/core/chat.py,sha256=6wdwaWfO5Tika6dXUhgeMVFUMP8Zpn3NeeAN4QF9_PA,1682
19
19
  intentkit/core/client.py,sha256=J5K7f08-ucszBKAbn9K3QNOFKIC__7amTbKYii1jFkI,3056
20
- intentkit/core/credit.py,sha256=Jbl7swi9d-4HyoxgC1iLeOIzfjc__x0-lJfIYWCIgK4,69817
20
+ intentkit/core/credit.py,sha256=vW3I5c_fKe_bmFvOc9_EaMsMuAbqw-XQ_NbJg_okdA8,75284
21
21
  intentkit/core/engine.py,sha256=H4ew1jhn2coMYJ3zR9isM1Y-XbnXNMg91SeDoeXdQ4U,36562
22
22
  intentkit/core/node.py,sha256=7h9zgDSd928bzUi3m3EZnKkhbwqlbRAQUr_uz7gKB5Y,8880
23
23
  intentkit/core/prompt.py,sha256=clZmH9Ryn7cUwi6lQADEH9-AupT4V4NLMaUCzOoFHS4,15899
@@ -29,7 +29,7 @@ intentkit/models/app_setting.py,sha256=iYbW63QD91bt4oEYV3wOXHuRFav2b4VXLwb_StgUQ
29
29
  intentkit/models/base.py,sha256=o-zRjVrak-f5Jokdvj8BjLm8gcC3yYiYMCTLegwT2lA,185
30
30
  intentkit/models/chat.py,sha256=cDccEHU8nd7Y5uhrHDCuZGwqrRwhqCaeztMiZcemiug,20469
31
31
  intentkit/models/conversation.py,sha256=nrbDIw-3GK5BYi_xkI15FLdx4a6SNrFK8wfAGLCsrqk,9032
32
- intentkit/models/credit.py,sha256=QQzc8oSbCTUfgRT-aLsMYxk6eTTktBCGM-nzD3glsFU,52169
32
+ intentkit/models/credit.py,sha256=JQ_ITxOM98XTkllxGDduKGf9-ZF4R-rYixN3OgNvl-Y,52275
33
33
  intentkit/models/db.py,sha256=nZjp6HlfLAWbMJeUkWrUcvlS4DAS8C_kpl5PzlO7eTc,5060
34
34
  intentkit/models/db_mig.py,sha256=vT6Tanm-BHC2T7dTztuB1UG494EFBAlHADKsNzR6xaQ,3577
35
35
  intentkit/models/generator.py,sha256=lyZu9U9rZUGkqd_QT5SAhay9DY358JJY8EhDSpN8I1M,10298
@@ -418,7 +418,7 @@ intentkit/utils/random.py,sha256=DymMxu9g0kuQLgJUqalvgksnIeLdS-v0aRk5nQU0mLI,452
418
418
  intentkit/utils/s3.py,sha256=9trQNkKQ5VgxWsewVsV8Y0q_pXzGRvsCYP8xauyUYkg,8549
419
419
  intentkit/utils/slack_alert.py,sha256=s7UpRgyzLW7Pbmt8cKzTJgMA9bm4EP-1rQ5KXayHu6E,2264
420
420
  intentkit/utils/tx.py,sha256=2yLLGuhvfBEY5n_GJ8wmIWLCzn0FsYKv5kRNzw_sLUI,1454
421
- intentkit-0.7.1.dev3.dist-info/METADATA,sha256=-6o8ouKvstC4DYOIKTEqkzC_YECsMygcIfhd-E_HR_g,6409
422
- intentkit-0.7.1.dev3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
423
- intentkit-0.7.1.dev3.dist-info/licenses/LICENSE,sha256=Bln6DhK-LtcO4aXy-PBcdZv2f24MlJFm_qn222biJtE,1071
424
- intentkit-0.7.1.dev3.dist-info/RECORD,,
421
+ intentkit-0.7.2.dev1.dist-info/METADATA,sha256=NFtTG-asUUpnQp9Sd4v_22mjhekVTQcL9jNHZQORiHI,6409
422
+ intentkit-0.7.2.dev1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
423
+ intentkit-0.7.2.dev1.dist-info/licenses/LICENSE,sha256=Bln6DhK-LtcO4aXy-PBcdZv2f24MlJFm_qn222biJtE,1071
424
+ intentkit-0.7.2.dev1.dist-info/RECORD,,