Spaces:
Paused
Paused
File size: 35,785 Bytes
21be64e 317ffd2 3fcf251 317ffd2 9112b0b 4b2cd36 945268c 0ff51c3 945268c 1e2ffc3 04f5d40 7f9e34a d2dd5ca 4b2cd36 321abe1 c65967d 321abe1 a1220fe 1e2ffc3 20bc5ae 1e2ffc3 ab1dce9 6ff9f06 de76de9 321abe1 42be9dd 945268c ded0cb5 945268c 1e2ffc3 15a2751 42be9dd 1e2ffc3 afd821e 42be9dd c43e306 5b177ea c43e306 423b76e 945268c 423b76e 949d7e2 e06dee8 7f9e34a 42be9dd 7f9e34a 21be64e 7f9e34a 5b177ea 1e2ffc3 5b177ea 7f9e34a 1e2ffc3 5b177ea ef24a72 945268c 1e2ffc3 42be9dd 945268c 949d7e2 e06dee8 c43e306 7f9e34a 1e2ffc3 42be9dd 1e2ffc3 7f9e34a 1e2ffc3 42be9dd 1e2ffc3 3fcf251 70e5b6f 1e2ffc3 21be64e afd821e 321abe1 afd821e 1e2ffc3 42be9dd 1e2ffc3 ef24a72 1e2ffc3 949d7e2 7f9e34a e06dee8 949d7e2 6ff9f06 3fcf251 6ff9f06 945268c 3fcf251 945268c e06dee8 6ff9f06 70e5b6f 3fcf251 70e5b6f 1ec5621 d199ab9 ef24a72 d199ab9 1e2ffc3 42be9dd 1e2ffc3 d199ab9 db72e72 ef24a72 31790e5 db72e72 9112b0b 42be9dd db72e72 1e2ffc3 ef24a72 1e2ffc3 42be9dd 9112b0b 1e2ffc3 9112b0b 1e2ffc3 ef24a72 c43e306 ef24a72 1e2ffc3 ef24a72 42be9dd c43e306 9112b0b c43e306 c65967d c43e306 c65967d 9112b0b c43e306 215dbf1 92f4e8d c43e306 215dbf1 0bf420b c43e306 42be9dd 8bd8e3d 04f5d40 e9f4817 42be9dd 144dad3 2fb1c39 de76de9 e9f4817 ab1dce9 d43f5a7 e9f4817 9112b0b e9f4817 b7ce560 e9f4817 e5902c3 42be9dd 9112b0b 4058185 1ec5621 9112b0b 80b971d 92f4e8d 80b971d 92f4e8d 9112b0b 92f4e8d 3fcf251 6f575da 92f4e8d 80b971d 1e2ffc3 42be9dd 1e2ffc3 e5902c3 42be9dd 0029409 1e2ffc3 4b2cd36 92f4e8d 42be9dd e5902c3 1e2ffc3 18874c0 1e2ffc3 15a2751 4b2cd36 a730828 e5902c3 321abe1 92f4e8d 9112b0b 321abe1 020ea82 8e6b633 92f4e8d 0029409 92f4e8d 0029409 92f4e8d 0029409 8bd8e3d 0029409 92f4e8d 3fcf251 0029409 5042df1 3fcf251 5042df1 0029409 9112b0b 04f5d40 0029409 a730828 0029409 f3f8ca5 f9c7644 f3f8ca5 321abe1 f3f8ca5 04f5d40 f9c7644 04f5d40 f3f8ca5 321abe1 f3f8ca5 a730828 f3f8ca5 0029409 321abe1 0029409 92f4e8d 0029409 92f4e8d c65967d 92f4e8d 0029409 c65967d 0029409 42be9dd 0029409 92f4e8d c65967d 92f4e8d 0029409 04f5d40 321abe1 04f5d40 0029409 5b177ea 92f4e8d 5b177ea 0029409 5b177ea 92f4e8d 0029409 f3f8ca5 760ba6e d2dd5ca 9112b0b d2dd5ca 9112b0b f3f8ca5 321abe1 3fcf251 9112b0b c65967d 4058185 9112b0b f3f8ca5 c65967d f3f8ca5 c65967d f3f8ca5 9112b0b 0029409 1dcf28d 0029409 321abe1 0029409 1dcf28d 321abe1 0029409 db9d435 9112b0b 321abe1 9112b0b 21be64e 92f4e8d de76de9 92f4e8d de76de9 21be64e 92f4e8d 21be64e 92f4e8d 9112b0b 92f4e8d 537c89a 21be64e 0029409 de76de9 0029409 de76de9 21be64e a11f4ba 21be64e 80b971d 25921da 80b971d 42be9dd db9d435 42be9dd db9d435 2d2ee7a c65967d 9112b0b 945268c 321abe1 21be64e 321abe1 c65967d 92f4e8d 9112b0b 92f4e8d 9112b0b 3fcf251 9112b0b e5902c3 9112b0b 3fcf251 9112b0b 215dbf1 9112b0b 92f4e8d c65967d 92f4e8d 9112b0b 92f4e8d c65967d 9112b0b 92f4e8d 21be64e e5902c3 92f4e8d 15a2751 ef24a72 f3f8ca5 ef24a72 96f0c2a 144dad3 ef24a72 f3f8ca5 ef24a72 96f0c2a 144dad3 96f0c2a 144dad3 ef24a72 f3f8ca5 ef24a72 96f0c2a 144dad3 ef24a72 04f5d40 42be9dd ef24a72 04f5d40 0ff51c3 f3f8ca5 0ff51c3 f3f8ca5 0ff51c3 7f9e34a 0ff51c3 f3f8ca5 ab1dce9 04f5d40 f3f8ca5 b622aac 04f5d40 ef24a72 04f5d40 f3f8ca5 b622aac f3f8ca5 bab921d f3f8ca5 42be9dd b622aac 321abe1 04f5d40 b622aac 04f5d40 42be9dd 9112b0b 42be9dd 9112b0b 6f575da 42be9dd c65967d 92f4e8d c65967d 1e2ffc3 e5902c3 5881d27 1e2ffc3 e5902c3 5266ca3 4b2cd36 317ffd2 4b2cd36 3fcf251 317ffd2 6ff9f06 317ffd2 3fcf251 423b76e 6ff9f06 317ffd2 3fcf251 317ffd2 3fcf251 317ffd2 3fcf251 42be9dd 317ffd2 3fcf251 0970947 3fcf251 317ffd2 6f575da 317ffd2 3fcf251 317ffd2 4b2cd36 317ffd2 42be9dd 3fcf251 42be9dd 9504283 f3f8ca5 f9c7644 f3f8ca5 3fcf251 f9c7644 42be9dd 3fcf251 42be9dd e5902c3 3fcf251 0970947 3fcf251 42be9dd 4b2cd36 e5902c3 de76de9 321abe1 de76de9 321abe1 de76de9 321abe1 de76de9 42be9dd 3fcf251 0970947 3fcf251 15a2751 de76de9 | 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 | import asyncio, random, string
import nest_asyncio
nest_asyncio.apply()
from collections import OrderedDict
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import Any, Awaitable, Coroutine, Dict, Literal
from enum import Enum
import uuid
import models
from python.helpers import extract_tools, files, errors, history, tokens, context as context_helper
from python.helpers import dirty_json
from python.helpers.print_style import PrintStyle
from langchain_core.prompts import (
ChatPromptTemplate,
)
from langchain_core.messages import SystemMessage, BaseMessage
import python.helpers.log as Log
from python.helpers.dirty_json import DirtyJson
from python.helpers.defer import DeferredTask
from typing import Callable
from python.helpers.localization import Localization
from python.helpers.extension import call_extensions
from python.helpers.errors import RepairableException
class AgentContextType(Enum):
USER = "user"
TASK = "task"
BACKGROUND = "background"
class AgentContext:
_contexts: dict[str, "AgentContext"] = {}
_counter: int = 0
_notification_manager = None
def __init__(
self,
config: "AgentConfig",
id: str | None = None,
name: str | None = None,
agent0: "Agent|None" = None,
log: Log.Log | None = None,
paused: bool = False,
streaming_agent: "Agent|None" = None,
created_at: datetime | None = None,
type: AgentContextType = AgentContextType.USER,
last_message: datetime | None = None,
data: dict | None = None,
output_data: dict | None = None,
set_current: bool = False,
):
# initialize context
self.id = id or AgentContext.generate_id()
existing = self._contexts.get(self.id, None)
if existing:
AgentContext.remove(self.id)
self._contexts[self.id] = self
if set_current:
AgentContext.set_current(self.id)
# initialize state
self.name = name
self.config = config
self.log = log or Log.Log()
self.log.context = self
self.agent0 = agent0 or Agent(0, self.config, self)
self.paused = paused
self.streaming_agent = streaming_agent
self.task: DeferredTask | None = None
self.created_at = created_at or datetime.now(timezone.utc)
self.type = type
AgentContext._counter += 1
self.no = AgentContext._counter
self.last_message = last_message or datetime.now(timezone.utc)
self.data = data or {}
self.output_data = output_data or {}
@staticmethod
def get(id: str):
return AgentContext._contexts.get(id, None)
@staticmethod
def use(id: str):
context = AgentContext.get(id)
if context:
AgentContext.set_current(id)
else:
AgentContext.set_current("")
return context
@staticmethod
def current():
ctxid = context_helper.get_context_data("agent_context_id","")
if not ctxid:
return None
return AgentContext.get(ctxid)
@staticmethod
def set_current(ctxid: str):
context_helper.set_context_data("agent_context_id", ctxid)
@staticmethod
def first():
if not AgentContext._contexts:
return None
return list(AgentContext._contexts.values())[0]
@staticmethod
def all():
return list(AgentContext._contexts.values())
@staticmethod
def generate_id():
def generate_short_id():
return ''.join(random.choices(string.ascii_letters + string.digits, k=8))
while True:
short_id = generate_short_id()
if short_id not in AgentContext._contexts:
return short_id
@classmethod
def get_notification_manager(cls):
if cls._notification_manager is None:
from python.helpers.notification import NotificationManager # type: ignore
cls._notification_manager = NotificationManager()
return cls._notification_manager
@staticmethod
def remove(id: str):
context = AgentContext._contexts.pop(id, None)
if context and context.task:
context.task.kill()
return context
def get_data(self, key: str, recursive: bool = True):
# recursive is not used now, prepared for context hierarchy
return self.data.get(key, None)
def set_data(self, key: str, value: Any, recursive: bool = True):
# recursive is not used now, prepared for context hierarchy
self.data[key] = value
def get_output_data(self, key: str, recursive: bool = True):
# recursive is not used now, prepared for context hierarchy
return self.output_data.get(key, None)
def set_output_data(self, key: str, value: Any, recursive: bool = True):
# recursive is not used now, prepared for context hierarchy
self.output_data[key] = value
def output(self):
return {
"id": self.id,
"name": self.name,
"created_at": (
Localization.get().serialize_datetime(self.created_at)
if self.created_at
else Localization.get().serialize_datetime(datetime.fromtimestamp(0))
),
"no": self.no,
"log_guid": self.log.guid,
"log_version": len(self.log.updates),
"log_length": len(self.log.logs),
"paused": self.paused,
"last_message": (
Localization.get().serialize_datetime(self.last_message)
if self.last_message
else Localization.get().serialize_datetime(datetime.fromtimestamp(0))
),
"type": self.type.value,
**self.output_data,
}
@staticmethod
def log_to_all(
type: Log.Type,
heading: str | None = None,
content: str | None = None,
kvps: dict | None = None,
temp: bool | None = None,
update_progress: Log.ProgressUpdate | None = None,
id: str | None = None, # Add id parameter
**kwargs,
) -> list[Log.LogItem]:
items: list[Log.LogItem] = []
for context in AgentContext.all():
items.append(
context.log.log(
type, heading, content, kvps, temp, update_progress, id, **kwargs
)
)
return items
def kill_process(self):
if self.task:
self.task.kill()
def reset(self):
self.kill_process()
self.log.reset()
self.agent0 = Agent(0, self.config, self)
self.streaming_agent = None
self.paused = False
def nudge(self):
self.kill_process()
self.paused = False
self.task = self.run_task(self.get_agent().monologue)
return self.task
def get_agent(self):
return self.streaming_agent or self.agent0
def communicate(self, msg: "UserMessage", broadcast_level: int = 1):
self.paused = False # unpause if paused
current_agent = self.get_agent()
if self.task and self.task.is_alive():
# set intervention messages to agent(s):
intervention_agent = current_agent
while intervention_agent and broadcast_level != 0:
intervention_agent.intervention = msg
broadcast_level -= 1
intervention_agent = intervention_agent.data.get(
Agent.DATA_NAME_SUPERIOR, None
)
else:
self.task = self.run_task(self._process_chain, current_agent, msg)
return self.task
def run_task(
self, func: Callable[..., Coroutine[Any, Any, Any]], *args: Any, **kwargs: Any
):
if not self.task:
self.task = DeferredTask(
thread_name=self.__class__.__name__,
)
self.task.start_task(func, *args, **kwargs)
return self.task
# this wrapper ensures that superior agents are called back if the chat was loaded from file and original callstack is gone
async def _process_chain(self, agent: "Agent", msg: "UserMessage|str", user=True):
try:
msg_template = (
agent.hist_add_user_message(msg) # type: ignore
if user
else agent.hist_add_tool_result(
tool_name="call_subordinate", tool_result=msg # type: ignore
)
)
response = await agent.monologue() # type: ignore
superior = agent.data.get(Agent.DATA_NAME_SUPERIOR, None)
if superior:
response = await self._process_chain(superior, response, False) # type: ignore
return response
except Exception as e:
agent.handle_critical_exception(e)
@dataclass
class AgentConfig:
chat_model: models.ModelConfig
utility_model: models.ModelConfig
embeddings_model: models.ModelConfig
browser_model: models.ModelConfig
mcp_servers: str
profile: str = ""
memory_subdir: str = ""
knowledge_subdirs: list[str] = field(default_factory=lambda: ["default", "custom"])
browser_http_headers: dict[str, str] = field(default_factory=dict) # Custom HTTP headers for browser requests
code_exec_ssh_enabled: bool = True
code_exec_ssh_addr: str = "localhost"
code_exec_ssh_port: int = 55022
code_exec_ssh_user: str = "root"
code_exec_ssh_pass: str = ""
additional: Dict[str, Any] = field(default_factory=dict)
@dataclass
class UserMessage:
message: str
attachments: list[str] = field(default_factory=list[str])
system_message: list[str] = field(default_factory=list[str])
class LoopData:
def __init__(self, **kwargs):
self.iteration = -1
self.system = []
self.user_message: history.Message | None = None
self.history_output: list[history.OutputMessage] = []
self.extras_temporary: OrderedDict[str, history.MessageContent] = OrderedDict()
self.extras_persistent: OrderedDict[str, history.MessageContent] = OrderedDict()
self.last_response = ""
self.params_temporary: dict = {}
self.params_persistent: dict = {}
self.current_tool = None
# override values with kwargs
for key, value in kwargs.items():
setattr(self, key, value)
# intervention exception class - skips rest of message loop iteration
class InterventionException(Exception):
pass
# killer exception class - not forwarded to LLM, cannot be fixed on its own, ends message loop
class HandledException(Exception):
pass
class Agent:
DATA_NAME_SUPERIOR = "_superior"
DATA_NAME_SUBORDINATE = "_subordinate"
DATA_NAME_CTX_WINDOW = "ctx_window"
def __init__(
self, number: int, config: AgentConfig, context: AgentContext | None = None
):
# agent config
self.config = config
# agent context
self.context = context or AgentContext(config=config, agent0=self)
# non-config vars
self.number = number
self.agent_name = f"A{self.number}"
self.history = history.History(self) # type: ignore[abstract]
self.last_user_message: history.Message | None = None
self.intervention: UserMessage | None = None
self.data: dict[str, Any] = {} # free data object all the tools can use
asyncio.run(self.call_extensions("agent_init"))
async def monologue(self):
while True:
try:
# loop data dictionary to pass to extensions
self.loop_data = LoopData(user_message=self.last_user_message)
# call monologue_start extensions
await self.call_extensions("monologue_start", loop_data=self.loop_data)
printer = PrintStyle(italic=True, font_color="#b3ffd9", padding=False)
# let the agent run message loop until he stops it with a response tool
while True:
self.context.streaming_agent = self # mark self as current streamer
self.loop_data.iteration += 1
self.loop_data.params_temporary = {} # clear temporary params
# call message_loop_start extensions
await self.call_extensions(
"message_loop_start", loop_data=self.loop_data
)
try:
# prepare LLM chain (model, system, history)
prompt = await self.prepare_prompt(loop_data=self.loop_data)
# call before_main_llm_call extensions
await self.call_extensions("before_main_llm_call", loop_data=self.loop_data)
async def reasoning_callback(chunk: str, full: str):
await self.handle_intervention()
if chunk == full:
printer.print("Reasoning: ") # start of reasoning
# Pass chunk and full data to extensions for processing
stream_data = {"chunk": chunk, "full": full}
await self.call_extensions(
"reasoning_stream_chunk", loop_data=self.loop_data, stream_data=stream_data
)
# Stream masked chunk after extensions processed it
if stream_data.get("chunk"):
printer.stream(stream_data["chunk"])
# Use the potentially modified full text for downstream processing
await self.handle_reasoning_stream(stream_data["full"])
async def stream_callback(chunk: str, full: str):
await self.handle_intervention()
# output the agent response stream
if chunk == full:
printer.print("Response: ") # start of response
# Pass chunk and full data to extensions for processing
stream_data = {"chunk": chunk, "full": full}
await self.call_extensions(
"response_stream_chunk", loop_data=self.loop_data, stream_data=stream_data
)
# Stream masked chunk after extensions processed it
if stream_data.get("chunk"):
printer.stream(stream_data["chunk"])
# Use the potentially modified full text for downstream processing
await self.handle_response_stream(stream_data["full"])
# call main LLM
agent_response, _reasoning = await self.call_chat_model(
messages=prompt,
response_callback=stream_callback,
reasoning_callback=reasoning_callback,
)
# Notify extensions to finalize their stream filters
await self.call_extensions(
"reasoning_stream_end", loop_data=self.loop_data
)
await self.call_extensions(
"response_stream_end", loop_data=self.loop_data
)
await self.handle_intervention(agent_response)
if (
self.loop_data.last_response == agent_response
): # if assistant_response is the same as last message in history, let him know
# Append the assistant's response to the history
self.hist_add_ai_response(agent_response)
# Append warning message to the history
warning_msg = self.read_prompt("fw.msg_repeat.md")
self.hist_add_warning(message=warning_msg)
PrintStyle(font_color="orange", padding=True).print(
warning_msg
)
self.context.log.log(type="warning", content=warning_msg)
else: # otherwise proceed with tool
# Append the assistant's response to the history
self.hist_add_ai_response(agent_response)
# process tools requested in agent message
tools_result = await self.process_tools(agent_response)
if tools_result: # final response of message loop available
return tools_result # break the execution if the task is done
# exceptions inside message loop:
except InterventionException as e:
pass # intervention message has been handled in handle_intervention(), proceed with conversation loop
except RepairableException as e:
# Forward repairable errors to the LLM, maybe it can fix them
msg = {"message": errors.format_error(e)}
await self.call_extensions("error_format", msg=msg)
self.hist_add_warning(msg["message"])
PrintStyle(font_color="red", padding=True).print(msg["message"])
self.context.log.log(type="error", content=msg["message"])
except Exception as e:
# Other exception kill the loop
self.handle_critical_exception(e)
finally:
# call message_loop_end extensions
await self.call_extensions(
"message_loop_end", loop_data=self.loop_data
)
# exceptions outside message loop:
except InterventionException as e:
pass # just start over
except Exception as e:
self.handle_critical_exception(e)
finally:
self.context.streaming_agent = None # unset current streamer
# call monologue_end extensions
await self.call_extensions("monologue_end", loop_data=self.loop_data) # type: ignore
async def prepare_prompt(self, loop_data: LoopData) -> list[BaseMessage]:
self.context.log.set_progress("Building prompt")
# call extensions before setting prompts
await self.call_extensions("message_loop_prompts_before", loop_data=loop_data)
# set system prompt and message history
loop_data.system = await self.get_system_prompt(self.loop_data)
loop_data.history_output = self.history.output()
# and allow extensions to edit them
await self.call_extensions("message_loop_prompts_after", loop_data=loop_data)
# concatenate system prompt
system_text = "\n\n".join(loop_data.system)
# join extras
extras = history.Message( # type: ignore[abstract]
False,
content=self.read_prompt(
"agent.context.extras.md",
extras=dirty_json.stringify(
{**loop_data.extras_persistent, **loop_data.extras_temporary}
),
),
).output()
loop_data.extras_temporary.clear()
# convert history + extras to LLM format
history_langchain: list[BaseMessage] = history.output_langchain(
loop_data.history_output + extras
)
# build full prompt from system prompt, message history and extrS
full_prompt: list[BaseMessage] = [
SystemMessage(content=system_text),
*history_langchain,
]
full_text = ChatPromptTemplate.from_messages(full_prompt).format()
# store as last context window content
self.set_data(
Agent.DATA_NAME_CTX_WINDOW,
{
"text": full_text,
"tokens": tokens.approximate_tokens(full_text),
},
)
return full_prompt
def handle_critical_exception(self, exception: Exception):
if isinstance(exception, HandledException):
raise exception # Re-raise the exception to kill the loop
elif isinstance(exception, asyncio.CancelledError):
# Handling for asyncio.CancelledError
PrintStyle(font_color="white", background_color="red", padding=True).print(
f"Context {self.context.id} terminated during message loop"
)
raise HandledException(
exception
) # Re-raise the exception to cancel the loop
else:
# Handling for general exceptions
error_text = errors.error_text(exception)
error_message = errors.format_error(exception)
# Mask secrets in error messages
PrintStyle(font_color="red", padding=True).print(error_message)
self.context.log.log(
type="error",
heading="Error",
content=error_message,
kvps={"text": error_text},
)
PrintStyle(font_color="red", padding=True).print(
f"{self.agent_name}: {error_text}"
)
raise HandledException(exception) # Re-raise the exception to kill the loop
async def get_system_prompt(self, loop_data: LoopData) -> list[str]:
system_prompt: list[str] = []
await self.call_extensions(
"system_prompt", system_prompt=system_prompt, loop_data=loop_data
)
return system_prompt
def parse_prompt(self, _prompt_file: str, **kwargs):
dirs = [files.get_abs_path("prompts")]
if (
self.config.profile
): # if agent has custom folder, use it and use default as backup
prompt_dir = files.get_abs_path("agents", self.config.profile, "prompts")
dirs.insert(0, prompt_dir)
prompt = files.parse_file(
_prompt_file, _directories=dirs, **kwargs
)
return prompt
def read_prompt(self, file: str, **kwargs) -> str:
dirs = [files.get_abs_path("prompts")]
if (
self.config.profile
): # if agent has custom folder, use it and use default as backup
prompt_dir = files.get_abs_path("agents", self.config.profile, "prompts")
dirs.insert(0, prompt_dir)
prompt = files.read_prompt_file(
file, _directories=dirs, **kwargs
)
prompt = files.remove_code_fences(prompt)
return prompt
def get_data(self, field: str):
return self.data.get(field, None)
def set_data(self, field: str, value):
self.data[field] = value
def hist_add_message(
self, ai: bool, content: history.MessageContent, tokens: int = 0
):
self.last_message = datetime.now(timezone.utc)
# Allow extensions to process content before adding to history
content_data = {"content": content}
asyncio.run(self.call_extensions("hist_add_before", content_data=content_data, ai=ai))
return self.history.add_message(ai=ai, content=content_data["content"], tokens=tokens)
def hist_add_user_message(self, message: UserMessage, intervention: bool = False):
self.history.new_topic() # user message starts a new topic in history
# load message template based on intervention
if intervention:
content = self.parse_prompt(
"fw.intervention.md",
message=message.message,
attachments=message.attachments,
system_message=message.system_message,
)
else:
content = self.parse_prompt(
"fw.user_message.md",
message=message.message,
attachments=message.attachments,
system_message=message.system_message,
)
# remove empty parts from template
if isinstance(content, dict):
content = {k: v for k, v in content.items() if v}
# add to history
msg = self.hist_add_message(False, content=content) # type: ignore
self.last_user_message = msg
return msg
def hist_add_ai_response(self, message: str):
self.loop_data.last_response = message
content = self.parse_prompt("fw.ai_response.md", message=message)
return self.hist_add_message(True, content=content)
def hist_add_warning(self, message: history.MessageContent):
content = self.parse_prompt("fw.warning.md", message=message)
return self.hist_add_message(False, content=content)
def hist_add_tool_result(self, tool_name: str, tool_result: str, **kwargs):
data = {
"tool_name": tool_name,
"tool_result": tool_result,
**kwargs,
}
asyncio.run(self.call_extensions("hist_add_tool_result", data=data))
return self.hist_add_message(False, content=data)
def concat_messages(
self, messages
): # TODO add param for message range, topic, history
return self.history.output_text(human_label="user", ai_label="assistant")
def get_chat_model(self):
return models.get_chat_model(
self.config.chat_model.provider,
self.config.chat_model.name,
model_config=self.config.chat_model,
**self.config.chat_model.build_kwargs(),
)
def get_utility_model(self):
return models.get_chat_model(
self.config.utility_model.provider,
self.config.utility_model.name,
model_config=self.config.utility_model,
**self.config.utility_model.build_kwargs(),
)
def get_browser_model(self):
return models.get_browser_model(
self.config.browser_model.provider,
self.config.browser_model.name,
model_config=self.config.browser_model,
**self.config.browser_model.build_kwargs(),
)
def get_embedding_model(self):
return models.get_embedding_model(
self.config.embeddings_model.provider,
self.config.embeddings_model.name,
model_config=self.config.embeddings_model,
**self.config.embeddings_model.build_kwargs(),
)
async def call_utility_model(
self,
system: str,
message: str,
callback: Callable[[str], Awaitable[None]] | None = None,
background: bool = False,
):
model = self.get_utility_model()
# call extensions
call_data = {
"model": model,
"system": system,
"message": message,
"callback": callback,
"background": background,
}
await self.call_extensions("util_model_call_before", call_data=call_data)
# propagate stream to callback if set
async def stream_callback(chunk: str, total: str):
if call_data["callback"]:
await call_data["callback"](chunk)
response, _reasoning = await call_data["model"].unified_call(
system_message=call_data["system"],
user_message=call_data["message"],
response_callback=stream_callback if call_data["callback"] else None,
rate_limiter_callback=self.rate_limiter_callback if not call_data["background"] else None,
)
return response
async def call_chat_model(
self,
messages: list[BaseMessage],
response_callback: Callable[[str, str], Awaitable[None]] | None = None,
reasoning_callback: Callable[[str, str], Awaitable[None]] | None = None,
background: bool = False,
):
response = ""
# model class
model = self.get_chat_model()
# call model
response, reasoning = await model.unified_call(
messages=messages,
reasoning_callback=reasoning_callback,
response_callback=response_callback,
rate_limiter_callback=self.rate_limiter_callback if not background else None,
)
return response, reasoning
async def rate_limiter_callback(
self, message: str, key: str, total: int, limit: int
):
# show the rate limit waiting in a progress bar, no need to spam the chat history
self.context.log.set_progress(message, True)
return False
async def handle_intervention(self, progress: str = ""):
while self.context.paused:
await asyncio.sleep(0.1) # wait if paused
if (
self.intervention
): # if there is an intervention message, but not yet processed
msg = self.intervention
self.intervention = None # reset the intervention message
# If a tool was running, save its progress to history
last_tool = self.loop_data.current_tool
if last_tool:
tool_progress = last_tool.progress.strip()
if tool_progress:
self.hist_add_tool_result(last_tool.name, tool_progress)
last_tool.set_progress(None)
if progress.strip():
self.hist_add_ai_response(progress)
# append the intervention message
self.hist_add_user_message(msg, intervention=True)
raise InterventionException(msg)
async def wait_if_paused(self):
while self.context.paused:
await asyncio.sleep(0.1)
async def process_tools(self, msg: str):
# search for tool usage requests in agent message
tool_request = extract_tools.json_parse_dirty(msg)
if tool_request is not None:
raw_tool_name = tool_request.get("tool_name", "") # Get the raw tool name
tool_args = tool_request.get("tool_args", {})
tool_name = raw_tool_name # Initialize tool_name with raw_tool_name
tool_method = None # Initialize tool_method
# Split raw_tool_name into tool_name and tool_method if applicable
if ":" in raw_tool_name:
tool_name, tool_method = raw_tool_name.split(":", 1)
tool = None # Initialize tool to None
# Try getting tool from MCP first
try:
import python.helpers.mcp_handler as mcp_helper
mcp_tool_candidate = mcp_helper.MCPConfig.get_instance().get_tool(
self, tool_name
)
if mcp_tool_candidate:
tool = mcp_tool_candidate
except ImportError:
PrintStyle(
background_color="black", font_color="yellow", padding=True
).print("MCP helper module not found. Skipping MCP tool lookup.")
except Exception as e:
PrintStyle(
background_color="black", font_color="red", padding=True
).print(f"Failed to get MCP tool '{tool_name}': {e}")
# Fallback to local get_tool if MCP tool was not found or MCP lookup failed
if not tool:
tool = self.get_tool(
name=tool_name, method=tool_method, args=tool_args, message=msg, loop_data=self.loop_data
)
if tool:
self.loop_data.current_tool = tool # type: ignore
try:
await self.handle_intervention()
# Call tool hooks for compatibility
await tool.before_execution(**tool_args)
await self.handle_intervention()
# Allow extensions to preprocess tool arguments
await self.call_extensions("tool_execute_before", tool_args=tool_args or {}, tool_name=tool_name)
response = await tool.execute(**tool_args)
await self.handle_intervention()
# Allow extensions to postprocess tool response
await self.call_extensions("tool_execute_after", response=response, tool_name=tool_name)
await tool.after_execution(response)
await self.handle_intervention()
if response.break_loop:
return response.message
finally:
self.loop_data.current_tool = None
else:
error_detail = (
f"Tool '{raw_tool_name}' not found or could not be initialized."
)
self.hist_add_warning(error_detail)
PrintStyle(font_color="red", padding=True).print(error_detail)
self.context.log.log(
type="error", content=f"{self.agent_name}: {error_detail}"
)
else:
warning_msg_misformat = self.read_prompt("fw.msg_misformat.md")
self.hist_add_warning(warning_msg_misformat)
PrintStyle(font_color="red", padding=True).print(warning_msg_misformat)
self.context.log.log(
type="error",
content=f"{self.agent_name}: Message misformat, no valid tool request found.",
)
async def handle_reasoning_stream(self, stream: str):
await self.handle_intervention()
await self.call_extensions(
"reasoning_stream",
loop_data=self.loop_data,
text=stream,
)
async def handle_response_stream(self, stream: str):
await self.handle_intervention()
try:
if len(stream) < 25:
return # no reason to try
response = DirtyJson.parse_string(stream)
if isinstance(response, dict):
await self.call_extensions(
"response_stream",
loop_data=self.loop_data,
text=stream,
parsed=response,
)
except Exception as e:
pass
def get_tool(
self, name: str, method: str | None, args: dict, message: str, loop_data: LoopData | None, **kwargs
):
from python.tools.unknown import Unknown
from python.helpers.tool import Tool
classes = []
# try agent tools first
if self.config.profile:
try:
classes = extract_tools.load_classes_from_file(
"agents/" + self.config.profile + "/tools/" + name + ".py", Tool # type: ignore[arg-type]
)
except Exception:
pass
# try default tools
if not classes:
try:
classes = extract_tools.load_classes_from_file(
"python/tools/" + name + ".py", Tool # type: ignore[arg-type]
)
except Exception as e:
pass
tool_class = classes[0] if classes else Unknown
return tool_class(
agent=self, name=name, method=method, args=args, message=message, loop_data=loop_data, **kwargs
)
async def call_extensions(self, extension_point: str, **kwargs) -> Any:
return await call_extensions(extension_point=extension_point, agent=self, **kwargs)
|