-
Notifications
You must be signed in to change notification settings - Fork 653
/
Copy pathengine.py
976 lines (907 loc) · 36.3 KB
/
engine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
"""AI Agent Management Module.
This module provides functionality for initializing and executing AI agents. It handles:
- Agent initialization with LangChain
- Tool and skill management
- Agent execution and response handling
- Memory management with PostgreSQL
- Integration with CDP and Twitter
The module uses a global cache to store initialized agents for better performance.
"""
import importlib
import logging
import textwrap
import time
from datetime import datetime
from decimal import Decimal
import sqlalchemy
from coinbase_agentkit import (
AgentKit,
AgentKitConfig,
CdpWalletProvider,
CdpWalletProviderConfig,
basename_action_provider,
cdp_api_action_provider,
cdp_wallet_action_provider,
erc20_action_provider,
morpho_action_provider,
pyth_action_provider,
superfluid_action_provider,
wallet_action_provider,
weth_action_provider,
wow_action_provider,
)
from coinbase_agentkit.action_providers.erc721 import erc721_action_provider
from coinbase_agentkit_langchain import get_langchain_tools
from epyxid import XID
from fastapi import HTTPException
from langchain_core.messages import (
BaseMessage,
HumanMessage,
)
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnableConfig
from langchain_core.tools import BaseTool
from langchain_openai import ChatOpenAI
from langchain_xai import ChatXAI
from langgraph.checkpoint.postgres.aio import AsyncPostgresSaver
from langgraph.graph.graph import CompiledGraph
from sqlalchemy import func, update
from sqlalchemy.exc import SQLAlchemyError
from abstracts.graph import AgentState
from app.config.config import config
from app.core.agent import AgentStore
from app.core.credit import expense_message, expense_skill
from app.core.graph import create_agent
from app.core.prompt import agent_prompt
from app.core.skill import skill_store
from models.agent import Agent, AgentData, AgentQuota, AgentTable
from models.chat import AuthorType, ChatMessage, ChatMessageCreate, ChatMessageSkillCall
from models.credit import CreditAccount, OwnerType
from models.db import get_pool, get_session
from models.skill import AgentSkillData, ThreadSkillData
from skills.acolyt import get_acolyt_skill
from skills.allora import get_allora_skill
from skills.cdp.get_balance import GetBalance
from skills.elfa import get_elfa_skill
from skills.enso import get_enso_skill
from skills.goat import (
create_smart_wallets_if_not_exist,
get_goat_skill,
init_smart_wallets,
)
from skills.twitter import get_twitter_skill
logger = logging.getLogger(__name__)
# Global variable to cache all agent executors
_agents: dict[str, CompiledGraph] = {}
_private_agents: dict[str, CompiledGraph] = {}
# Global dictionaries to cache agent update times
_agents_updated: dict[str, datetime] = {}
_private_agents_updated: dict[str, datetime] = {}
async def initialize_agent(aid, is_private=False):
"""Initialize an AI agent with specified configuration and tools.
This function:
1. Loads agent configuration from database
2. Initializes LLM with specified model
3. Loads and configures requested tools
4. Sets up PostgreSQL-based memory
5. Creates and caches the agent
Args:
aid (str): Agent ID to initialize
is_private (bool, optional): Flag indicating whether the agent is private. Defaults to False.
Returns:
Agent: Initialized LangChain agent
Raises:
HTTPException: If agent not found (404) or database error (500)
"""
"""Initialize the agent with CDP Agentkit."""
# init agent store
agent_store = AgentStore(aid)
# get the agent from the database
agent: Agent = await Agent.get(aid)
if not agent:
raise HTTPException(status_code=404, detail="Agent not found")
agent_data: AgentData = await AgentData.get(aid)
# ==== Initialize LLM.
input_token_limit = config.input_token_limit
# TODO: model name whitelist
if agent.model.startswith("deepseek"):
llm = ChatOpenAI(
model_name=agent.model,
openai_api_key=config.deepseek_api_key,
openai_api_base="https://api.deepseek.com",
frequency_penalty=agent.frequency_penalty,
presence_penalty=agent.presence_penalty,
temperature=agent.temperature,
timeout=300,
)
if input_token_limit > 60000:
input_token_limit = 60000
elif agent.model.startswith("grok"):
llm = ChatXAI(
model_name=agent.model,
api_key=config.xai_api_key,
frequency_penalty=agent.frequency_penalty,
presence_penalty=agent.presence_penalty,
temperature=agent.temperature,
timeout=180,
)
if input_token_limit > 120000:
input_token_limit = 120000
elif agent.model == "eternalai":
agent.model = "unsloth/Llama-3.3-70B-Instruct-bnb-4bit"
llm = ChatOpenAI(
model_name=agent.model,
openai_api_key=config.eternal_api_key,
openai_api_base="https://api.eternalai.org/v1",
frequency_penalty=agent.frequency_penalty,
presence_penalty=agent.presence_penalty,
temperature=agent.temperature,
timeout=300,
)
if input_token_limit > 60000:
input_token_limit = 60000
else:
llm = ChatOpenAI(
model_name=agent.model,
openai_api_key=config.openai_api_key,
frequency_penalty=agent.frequency_penalty,
presence_penalty=agent.presence_penalty,
temperature=agent.temperature,
timeout=180,
)
if input_token_limit > 120000:
input_token_limit = 120000
# ==== Store buffered conversation history in memory.
memory = AsyncPostgresSaver(get_pool())
# ==== Load skills
tools: list[BaseTool] = []
if agent.skills:
for k, v in agent.skills.items():
if not v.get("enabled", False):
continue
try:
skill_module = importlib.import_module(f"skills.{k}")
if hasattr(skill_module, "get_skills"):
skill_tools = await skill_module.get_skills(
v, is_private, skill_store, agent_id=aid
)
if skill_tools and len(skill_tools) > 0:
tools.extend(skill_tools)
else:
logger.error(f"Skill {k} does not have get_skills function")
except ImportError as e:
logger.error(f"Could not import skill module: {k} ({e})")
# Configure CDP Agentkit Langchain Extension.
# Deprecated
cdp_wallet_provider = None
if (
agent.cdp_enabled
and agent_data
and agent_data.cdp_wallet_data
and agent.cdp_skills
and ("cdp" not in agent.skills if agent.skills else True)
):
cdp_wallet_provider_config = CdpWalletProviderConfig(
api_key_name=config.cdp_api_key_name,
api_key_private_key=config.cdp_api_key_private_key,
network_id=agent.cdp_network_id,
wallet_data=agent_data.cdp_wallet_data,
)
cdp_wallet_provider = CdpWalletProvider(cdp_wallet_provider_config)
agent_kit = AgentKit(
AgentKitConfig(
wallet_provider=cdp_wallet_provider,
action_providers=[
wallet_action_provider(),
cdp_api_action_provider(cdp_wallet_provider_config),
cdp_wallet_action_provider(cdp_wallet_provider_config),
pyth_action_provider(),
basename_action_provider(),
erc20_action_provider(),
erc721_action_provider(),
weth_action_provider(),
morpho_action_provider(),
superfluid_action_provider(),
wow_action_provider(),
],
)
)
cdp_tools = get_langchain_tools(agent_kit)
for skill in agent.cdp_skills:
if skill == "get_balance":
tools.append(
GetBalance(
wallet=cdp_wallet_provider._wallet,
agent_id=aid,
skill_store=skill_store,
)
)
continue
for tool in cdp_tools:
if tool.name.endswith(skill):
tools.append(tool)
if (
agent.goat_enabled
and agent.crossmint_config
and ("goat" not in agent.skills if agent.skills else True)
):
if (
hasattr(config, "chain_provider")
and config.crossmint_api_key
and config.crossmint_api_base_url
):
crossmint_networks = agent.crossmint_config.get("networks")
if crossmint_networks and len(crossmint_networks) > 0:
crossmint_wallet_data = (
agent_data.crossmint_wallet_data
if agent_data.crossmint_wallet_data
else {}
)
try:
smart_wallet_data = create_smart_wallets_if_not_exist(
config.crossmint_api_base_url,
config.crossmint_api_key,
crossmint_wallet_data.get("smart"),
)
# save the wallet after first create
if (
not crossmint_wallet_data
or not crossmint_wallet_data.get("smart")
or not crossmint_wallet_data.get("smart").get("evm")
or not crossmint_wallet_data.get("smart")
.get("evm")
.get("address")
):
await agent_store.set_data(
{
"crossmint_wallet_data": {"smart": smart_wallet_data},
}
)
# give rpc some time to prevent error #429
time.sleep(1)
evm_crossmint_wallets = init_smart_wallets(
config.crossmint_api_key,
config.chain_provider,
crossmint_networks,
smart_wallet_data["evm"],
)
for wallet in evm_crossmint_wallets:
try:
s = get_goat_skill(
wallet,
agent.goat_skills,
skill_store,
agent_store,
aid,
)
tools.extend(s)
except Exception as e:
logger.warning(e)
except Exception as e:
logger.warning(e)
# Enso skills
if (
agent.enso_skills
and len(agent.enso_skills) > 0
and agent.enso_config
and ("enso" not in agent.skills if agent.skills else True)
):
for skill in agent.enso_skills:
try:
s = get_enso_skill(
skill,
skill_store,
)
tools.append(s)
except Exception as e:
logger.warning(e)
# Acoalyt skills
if (
agent.acolyt_skills
and len(agent.acolyt_skills) > 0
and ("acolyt" not in agent.skills if agent.skills else True)
):
for skill in agent.acolyt_skills:
try:
s = get_acolyt_skill(
skill,
skill_store,
)
tools.append(s)
except Exception as e:
logger.warning(e)
# Allora skills
if (
agent.allora_skills
and len(agent.allora_skills) > 0
and ("allora" not in agent.skills if agent.skills else True)
):
for skill in agent.allora_skills:
try:
s = get_allora_skill(
skill,
skill_store,
)
tools.append(s)
except Exception as e:
logger.warning(e)
# Elfa skills
if (
agent.elfa_skills
and len(agent.elfa_skills) > 0
and ("elfa" not in agent.skills if agent.skills else True)
):
for skill in agent.elfa_skills:
try:
s = get_elfa_skill(
skill,
skill_store,
)
tools.append(s)
except Exception as e:
logger.warning(e)
# Twitter skills
if (
agent.twitter_skills
and len(agent.twitter_skills) > 0
and ("twitter" not in agent.skills if agent.skills else True)
):
for skill in agent.twitter_skills:
s = get_twitter_skill(
skill,
skill_store,
)
tools.append(s)
# filter the duplicate tools
tools = list({tool.name: tool for tool in tools}.values())
# finally, set up the system prompt
prompt = agent_prompt(agent, agent_data)
# Escape curly braces in the prompt
escaped_prompt = prompt.replace("{", "{{").replace("}", "}}")
prompt_array = [
("system", escaped_prompt),
("placeholder", "{entrypoint_prompt}"),
("placeholder", "{messages}"),
]
if agent.prompt_append:
# Escape any curly braces in prompt_append
escaped_append = agent.prompt_append.replace("{", "{{").replace("}", "}}")
if agent.model.startswith("deepseek"):
prompt_array.insert(1, ("system", escaped_append))
else:
prompt_array.append(("system", escaped_append))
prompt_temp = ChatPromptTemplate.from_messages(prompt_array)
def formatted_prompt(
state: AgentState, config: RunnableConfig
) -> list[BaseMessage]:
# logger.debug(f"[{aid}] formatted prompt: {state}")
entrypoint_prompt = []
if config.get("configurable") and config["configurable"].get(
"entrypoint_prompt"
):
entrypoint_prompt = [
("system", config["configurable"]["entrypoint_prompt"])
]
return prompt_temp.invoke(
{"messages": state["messages"], "entrypoint_prompt": entrypoint_prompt},
config,
)
# hack for deepseek r1, it doesn't support tools
if agent.model in [
"deepseek-reasoner",
]:
tools = []
# log all tools
for tool in tools:
logger.info(
f"[{aid}{'-private' if is_private else ''}] loaded tool: {tool.name}"
)
logger.debug(
f"[{aid}{'-private' if is_private else ''}] init prompt: {escaped_prompt}"
)
# Create ReAct Agent using the LLM and CDP Agentkit tools.
executor = create_agent(
aid,
llm,
tools=tools,
checkpointer=memory,
state_modifier=formatted_prompt,
debug=config.debug_checkpoint,
input_token_limit=input_token_limit,
)
if is_private:
_private_agents[aid] = executor
_private_agents_updated[aid] = agent.updated_at
else:
_agents[aid] = executor
_agents_updated[aid] = agent.updated_at
async def agent_executor(agent_id: str, is_private: bool) -> (CompiledGraph, float):
start = time.perf_counter()
agent = await Agent.get(agent_id)
if not agent:
raise HTTPException(status_code=404, detail="Agent not found")
agents = _private_agents if is_private else _agents
agents_updated = _private_agents_updated if is_private else _agents_updated
# Check if agent needs reinitialization due to updates
needs_reinit = False
if agent_id in agents:
if (
agent_id not in agents_updated
or agent.updated_at != agents_updated[agent_id]
):
needs_reinit = True
logger.info(
f"Reinitializing agent {agent_id} due to updates, private mode: {is_private}"
)
# cold start or needs reinitialization
cold_start_cost = 0.0
if (agent_id not in agents) or needs_reinit:
await initialize_agent(agent_id, is_private)
cold_start_cost = time.perf_counter() - start
return agents[agent_id], cold_start_cost
async def execute_agent(
message: ChatMessageCreate, debug: bool = False
) -> list[ChatMessage]:
"""
Execute an agent with the given prompt and return response lines.
This function:
1. Configures execution context with thread ID
2. Initializes agent if not in cache
3. Streams agent execution results
4. Formats and times the execution steps
Args:
message (ChatMessageCreate): The chat message containing agent_id, chat_id, and message content
debug (bool): Enable debug mode, will save the skill results
Returns:
list[ChatMessage]: Formatted response lines including timing information
"""
quota = await AgentQuota.get(message.agent_id)
if quota and not quota.has_message_quota():
raise HTTPException(status_code=429, detail="Agent Daily Quota exceeded")
resp = []
start = time.perf_counter()
# make sure reply_to is set
message.reply_to = message.id
input = await message.save()
agent = await Agent.get(input.agent_id)
# hack for temporary disable models
if config.env == "testnet-prod" and agent.model not in [
"gpt-4o-mini",
"gpt-4.1-nano",
]:
error_message_create = ChatMessageCreate(
id=str(XID()),
agent_id=input.agent_id,
chat_id=input.chat_id,
user_id=input.user_id,
author_id=input.agent_id,
author_type=AuthorType.SYSTEM,
thread_type=input.author_type,
reply_to=input.id,
message="This model is currently unavailable. Please switch to a different supported model or wait for further updates from Nation App.",
time_cost=time.perf_counter() - start,
)
error_message = await error_message_create.save()
resp.append(error_message)
return resp
# check user balance
if is_payment_required(input, agent):
payer = input.user_id
if (
input.author_type == AuthorType.TELEGRAM
or input.author_type == AuthorType.TWITTER
):
payer = agent.owner
user_account = await CreditAccount.get_or_create(OwnerType.USER, payer)
if not user_account.has_sufficient_credits(1):
error_message_create = ChatMessageCreate(
id=str(XID()),
agent_id=input.agent_id,
chat_id=input.chat_id,
user_id=input.user_id,
author_id=input.agent_id,
author_type=AuthorType.SYSTEM,
thread_type=input.author_type,
reply_to=input.id,
message="Insufficient balance.",
time_cost=time.perf_counter() - start,
)
error_message = await error_message_create.save()
resp.append(error_message)
return resp
# once the input saved, reduce message quota
await quota.add_message()
is_private = False
if input.user_id == agent.owner:
is_private = True
executor, cold_start_cost = await agent_executor(input.agent_id, is_private)
last = start + cold_start_cost
# Extract images from attachments
image_urls = []
if input.attachments:
image_urls = [
att["url"]
for att in input.attachments
if "type" in att and att["type"] == "image" and "url" in att
]
# message
# if the model doesn't natively support image parsing, add the image URLs to the message
if agent.has_image_parser_skill() and image_urls:
input.message += f"\n\nImages:\n{'\n'.join(image_urls)}"
content = [
{"type": "text", "text": input.message},
]
if not agent.has_image_parser_skill() and image_urls:
# anyway, pass it directly to LLM
content.extend(
[
{"type": "image_url", "image_url": {"url": image_url}}
for image_url in image_urls
]
)
messages = [
HumanMessage(content=content),
]
entrypoint_prompt = None
if (
agent.twitter_entrypoint_enabled
and agent.twitter_entrypoint_prompt
and input.author_type == AuthorType.TWITTER
):
entrypoint_prompt = agent.twitter_entrypoint_prompt
logger.debug("twitter entrypoint prompt added")
elif (
agent.telegram_entrypoint_enabled
and agent.telegram_entrypoint_prompt
and input.author_type == AuthorType.TELEGRAM
):
entrypoint_prompt = agent.telegram_entrypoint_prompt
logger.debug("telegram entrypoint prompt added")
# stream config
thread_id = f"{input.agent_id}-{input.chat_id}"
stream_config = {
"configurable": {
"agent": agent,
"thread_id": thread_id,
"user_id": input.user_id,
"entrypoint": input.author_type,
"entrypoint_prompt": entrypoint_prompt,
}
}
# run
cached_tool_step = None
async for chunk in executor.astream({"messages": messages}, stream_config):
try:
this_time = time.perf_counter()
# logger.debug(f"stream chunk: {chunk}", extra={"thread_id": thread_id})
if "agent" in chunk and "messages" in chunk["agent"]:
if len(chunk["agent"]["messages"]) != 1:
logger.error(
"unexpected agent message: " + str(chunk["agent"]["messages"]),
extra={"thread_id": thread_id},
)
msg = chunk["agent"]["messages"][0]
if hasattr(msg, "tool_calls") and msg.tool_calls:
# tool calls, save for later use
cached_tool_step = msg
elif hasattr(msg, "content") and msg.content:
# agent message
chat_message_create = ChatMessageCreate(
id=str(XID()),
agent_id=input.agent_id,
chat_id=input.chat_id,
user_id=input.user_id,
author_id=input.agent_id,
author_type=AuthorType.AGENT,
thread_type=input.author_type,
reply_to=input.id,
message=msg.content,
input_tokens=(
msg.usage_metadata.get("input_tokens", 0)
if hasattr(msg, "usage_metadata") and msg.usage_metadata
else 0
),
output_tokens=(
msg.usage_metadata.get("output_tokens", 0)
if hasattr(msg, "usage_metadata") and msg.usage_metadata
else 0
),
time_cost=this_time - last,
)
last = this_time
if cold_start_cost > 0:
chat_message_create.cold_start_cost = cold_start_cost
cold_start_cost = 0
# handle message and payment in one transaction
async with get_session() as session:
# payment
if is_payment_required(input, agent):
amount = (
Decimal("200")
* (
Decimal(str(chat_message_create.input_tokens))
* Decimal("0.3")
+ Decimal(str(chat_message_create.output_tokens))
* Decimal("1.2")
)
/ Decimal("1000000")
)
credit_event = await expense_message(
session,
input.agent_id,
payer,
chat_message_create.id,
input.id,
amount,
agent.fee_percentage
if agent.fee_percentage
else Decimal("0"),
agent.owner,
)
logger.info(f"[{input.agent_id}] expense message: {amount}")
chat_message_create.credit_event_id = credit_event.id
chat_message_create.credit_cost = credit_event.total_amount
chat_message = await chat_message_create.save_in_session(
session
)
await session.commit()
resp.append(chat_message)
else:
logger.error(
"unexpected agent message: " + str(msg),
extra={"thread_id": thread_id},
)
elif "tools" in chunk and "messages" in chunk["tools"]:
if not cached_tool_step:
logger.error(
"unexpected tools message: " + str(chunk["tools"]),
extra={"thread_id": thread_id},
)
continue
skill_calls = []
for msg in chunk["tools"]["messages"]:
if not hasattr(msg, "tool_call_id"):
logger.error(
"unexpected tools message: " + str(chunk["tools"]),
extra={"thread_id": thread_id},
)
continue
for call in cached_tool_step.tool_calls:
if call["id"] == msg.tool_call_id:
skill_call: ChatMessageSkillCall = {
"id": msg.tool_call_id,
"name": call["name"],
"parameters": call["args"],
"success": True,
}
if msg.status == "error":
skill_call["success"] = False
skill_call["error_message"] = msg.content
else:
if debug:
skill_call["response"] = msg.content
else:
skill_call["response"] = textwrap.shorten(
msg.content, width=100, placeholder="..."
)
skill_calls.append(skill_call)
break
skill_message_create = ChatMessageCreate(
id=str(XID()),
agent_id=input.agent_id,
chat_id=input.chat_id,
user_id=input.user_id,
author_id=input.agent_id,
author_type=AuthorType.SKILL,
thread_type=input.author_type,
reply_to=input.id,
message="",
skill_calls=skill_calls,
input_tokens=(
cached_tool_step.usage_metadata.get("input_tokens", 0)
if hasattr(cached_tool_step, "usage_metadata")
and cached_tool_step.usage_metadata
else 0
),
output_tokens=(
cached_tool_step.usage_metadata.get("output_tokens", 0)
if hasattr(cached_tool_step, "usage_metadata")
and cached_tool_step.usage_metadata
else 0
),
time_cost=this_time - last,
)
last = this_time
if cold_start_cost > 0:
skill_message_create.cold_start_cost = cold_start_cost
cold_start_cost = 0
cached_tool_step = None
# save message and credit in one transaction
async with get_session() as session:
if is_payment_required(input, agent):
# message payment
message_amount = (
Decimal("200")
* (
Decimal(str(skill_message_create.input_tokens))
* Decimal("0.3")
+ Decimal(str(skill_message_create.output_tokens))
* Decimal("1.2")
)
/ Decimal("1000000")
)
message_payment_event = await expense_message(
session,
input.agent_id,
payer,
skill_message_create.id,
input.id,
message_amount,
agent.fee_percentage
if agent.fee_percentage
else Decimal("0"),
agent.owner,
)
skill_message_create.credit_event_id = message_payment_event.id
skill_message_create.credit_cost = (
message_payment_event.total_amount
)
# skill payment
for skill_call in skill_calls:
payment_event = await expense_skill(
session,
input.agent_id,
payer,
skill_message_create.id,
input.id,
skill_call["id"],
skill_call["name"],
agent.fee_percentage
if agent.fee_percentage
else Decimal("0"),
agent.owner,
)
skill_call["credit_event_id"] = payment_event.id
skill_call["credit_cost"] = payment_event.total_amount
logger.info(
f"[{input.agent_id}] skill payment: {skill_call}"
)
skill_message_create.skill_calls = skill_calls
skill_message = await skill_message_create.save_in_session(session)
await session.commit()
resp.append(skill_message)
elif "memory_manager" in chunk:
pass
else:
logger.error(
"unexpected message type: " + str(chunk),
extra={"thread_id": thread_id},
)
except SQLAlchemyError as e:
logger.error(
f"db error when execute agent: {str(e)}", extra={"thread_id": thread_id}
)
error_message_create = ChatMessageCreate(
id=str(XID()),
agent_id=input.agent_id,
chat_id=input.chat_id,
user_id=input.user_id,
author_id=input.agent_id,
author_type=AuthorType.SYSTEM,
thread_type=input.author_type,
reply_to=input.id,
message="IntentKit internal error",
time_cost=time.perf_counter() - start,
)
error_message = await error_message_create.save()
resp.append(error_message)
return resp
except Exception as e:
logger.error(
f"failed to execute agent: {str(e)}", extra={"thread_id": thread_id}
)
error_message_create = ChatMessageCreate(
id=str(XID()),
agent_id=input.agent_id,
chat_id=input.chat_id,
user_id=input.user_id,
author_id=input.agent_id,
author_type=AuthorType.SYSTEM,
thread_type=input.author_type,
reply_to=input.id,
message=f"Error in agent:\n {str(e)}",
time_cost=time.perf_counter() - start,
)
error_message = await error_message_create.save()
resp.append(error_message)
return resp
return resp
async def clean_agent_memory(
agent_id: str,
chat_id: str = "",
clean_agent: bool = False,
clean_skill: bool = False,
) -> str:
"""
Clean an agent's memory with the given prompt and return response.
This function:
1. Cleans the agents skills data.
2. Cleans the thread skills data.
3. Cleans the graph checkpoint data.
4. Cleans the graph checkpoint_writes data.
5. Cleans the graph checkpoint_blobs data.
Args:
agent_id (str): Agent ID
chat_id (str): Thread ID for the agent memory cleanup
clean_agent (bool): Whether to clean agent's memory data
clean_skill (bool): Whether to clean skills memory data
Returns:
str: Successful response message.
"""
# get the agent from the database
try:
if not clean_skill and not clean_agent:
raise HTTPException(
status_code=400,
detail="at least one of skills data or agent memory should be true.",
)
if clean_skill:
await AgentSkillData.clean_data(agent_id)
await ThreadSkillData.clean_data(agent_id, chat_id)
async with get_session() as db:
if clean_agent:
chat_id = chat_id.strip()
q_suffix = "%"
if chat_id and chat_id != "":
q_suffix = chat_id
deletion_param = {"value": agent_id + "-" + q_suffix}
await db.execute(
sqlalchemy.text(
"DELETE FROM checkpoints WHERE thread_id like :value",
),
deletion_param,
)
await db.execute(
sqlalchemy.text(
"DELETE FROM checkpoint_writes WHERE thread_id like :value",
),
deletion_param,
)
await db.execute(
sqlalchemy.text(
"DELETE FROM checkpoint_blobs WHERE thread_id like :value",
),
deletion_param,
)
# update the updated_at field so that the agent instance will all reload
await db.execute(
update(AgentTable)
.where(AgentTable.id == agent_id)
.values(updated_at=func.now())
)
await db.commit()
logger.info(f"Agent [{agent_id}] data cleaned up successfully.")
return "Agent data cleaned up successfully."
except SQLAlchemyError as e:
# Handle other SQLAlchemy-related errors
logger.error(e)
raise HTTPException(status_code=500, detail=str(e))
except Exception as e:
logger.error("failed to cleanup the agent memory: " + str(e))
raise e
async def thread_stats(agent_id: str, chat_id: str) -> list[BaseMessage]:
thread_id = f"{agent_id}-{chat_id}"
stream_config = {"configurable": {"thread_id": thread_id}}
is_private = False
if chat_id.startswith("owner") or chat_id.startswith("autonomous"):
is_private = True
executor, _ = await agent_executor(agent_id, is_private)
snap = await executor.aget_state(stream_config)
if snap.values and "messages" in snap.values:
return snap.values["messages"]
else:
return []
def is_payment_required(input: ChatMessageCreate, agent: Agent) -> bool:
if config.payment_enabled and input.user_id and agent.owner:
return True
return False