pangolin-agent 0.1.0b1__tar.gz
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.
- pangolin_agent-0.1.0b1/.gitignore +4 -0
- pangolin_agent-0.1.0b1/LICENSE +85 -0
- pangolin_agent-0.1.0b1/PKG-INFO +36 -0
- pangolin_agent-0.1.0b1/pangolin_agent/__init__.py +32 -0
- pangolin_agent-0.1.0b1/pangolin_agent/agent_runtimes/__init__.py +7 -0
- pangolin_agent-0.1.0b1/pangolin_agent/agent_runtimes/pangolin_runtime.py +299 -0
- pangolin_agent-0.1.0b1/pangolin_agent/api_providers/__init__.py +2 -0
- pangolin_agent-0.1.0b1/pangolin_agent/api_providers/anthropic_provider.py +294 -0
- pangolin_agent-0.1.0b1/pangolin_agent/api_providers/gemini_provider.py +180 -0
- pangolin_agent-0.1.0b1/pangolin_agent/api_providers/open_ai_provider.py +193 -0
- pangolin_agent-0.1.0b1/pangolin_agent/api_providers/smoke_test_client.py +91 -0
- pangolin_agent-0.1.0b1/pangolin_agent/cli.py +62 -0
- pangolin_agent-0.1.0b1/pangolin_agent/core/__init__.py +0 -0
- pangolin_agent-0.1.0b1/pangolin_agent/core/exceptions/__init__.py +11 -0
- pangolin_agent-0.1.0b1/pangolin_agent/core/exceptions/exceptions.py +18 -0
- pangolin_agent-0.1.0b1/pangolin_agent/core/interfaces/__init__.py +28 -0
- pangolin_agent-0.1.0b1/pangolin_agent/core/interfaces/agent_runtime.py +24 -0
- pangolin_agent-0.1.0b1/pangolin_agent/core/interfaces/client.py +12 -0
- pangolin_agent-0.1.0b1/pangolin_agent/core/interfaces/guardrail.py +9 -0
- pangolin_agent-0.1.0b1/pangolin_agent/core/interfaces/memory_manager.py +18 -0
- pangolin_agent-0.1.0b1/pangolin_agent/core/interfaces/provider.py +13 -0
- pangolin_agent-0.1.0b1/pangolin_agent/core/interfaces/rag_component.py +9 -0
- pangolin_agent-0.1.0b1/pangolin_agent/core/interfaces/response_composer.py +14 -0
- pangolin_agent-0.1.0b1/pangolin_agent/core/interfaces/skill_manager.py +25 -0
- pangolin_agent-0.1.0b1/pangolin_agent/core/interfaces/toolbox.py +23 -0
- pangolin_agent-0.1.0b1/pangolin_agent/core/types/__init__.py +10 -0
- pangolin_agent-0.1.0b1/pangolin_agent/core/types/chat_summary.py +10 -0
- pangolin_agent-0.1.0b1/pangolin_agent/core/types/guardrail_events.py +25 -0
- pangolin_agent-0.1.0b1/pangolin_agent/guardrails/__init__.py +7 -0
- pangolin_agent-0.1.0b1/pangolin_agent/guardrails/default_guardrail.py +37 -0
- pangolin_agent-0.1.0b1/pangolin_agent/memory_managers/__init__.py +9 -0
- pangolin_agent-0.1.0b1/pangolin_agent/memory_managers/simple_memory_manager.py +48 -0
- pangolin_agent-0.1.0b1/pangolin_agent/memory_managers/sqlite_memory_manager.py +229 -0
- pangolin_agent-0.1.0b1/pangolin_agent/messages/__init__.py +19 -0
- pangolin_agent-0.1.0b1/pangolin_agent/messages/agent_message.py +16 -0
- pangolin_agent-0.1.0b1/pangolin_agent/messages/api_event.py +15 -0
- pangolin_agent-0.1.0b1/pangolin_agent/messages/chat_record.py +43 -0
- pangolin_agent-0.1.0b1/pangolin_agent/messages/tool_call.py +13 -0
- pangolin_agent-0.1.0b1/pangolin_agent/messages/tool_call_result.py +11 -0
- pangolin_agent-0.1.0b1/pangolin_agent/py.typed +0 -0
- pangolin_agent-0.1.0b1/pangolin_agent/rag_routers/__init__.py +7 -0
- pangolin_agent-0.1.0b1/pangolin_agent/rag_routers/rag_router.py +32 -0
- pangolin_agent-0.1.0b1/pangolin_agent/response_composers/__init__.py +7 -0
- pangolin_agent-0.1.0b1/pangolin_agent/response_composers/pangolin_composer.py +47 -0
- pangolin_agent-0.1.0b1/pangolin_agent/skill_managers/__init__.py +11 -0
- pangolin_agent-0.1.0b1/pangolin_agent/skill_managers/advanced_skill_manager.py +108 -0
- pangolin_agent-0.1.0b1/pangolin_agent/skill_managers/rag_skill_manager.py +221 -0
- pangolin_agent-0.1.0b1/pangolin_agent/skill_managers/simple_skill_manager.py +38 -0
- pangolin_agent-0.1.0b1/pangolin_agent/skill_managers/skills/__init__.py +0 -0
- pangolin_agent-0.1.0b1/pangolin_agent/skill_managers/skills/pangolin_architecture.md +95 -0
- pangolin_agent-0.1.0b1/pangolin_agent/skill_managers/skills/pangolin_guardrail.md +80 -0
- pangolin_agent-0.1.0b1/pangolin_agent/skill_managers/skills/pangolin_memory.md +82 -0
- pangolin_agent-0.1.0b1/pangolin_agent/skill_managers/skills/pangolin_provider.md +86 -0
- pangolin_agent-0.1.0b1/pangolin_agent/skill_managers/skills/pangolin_skill_manager.md +89 -0
- pangolin_agent-0.1.0b1/pangolin_agent/skill_managers/skills/pangolin_toolbox.md +121 -0
- pangolin_agent-0.1.0b1/pangolin_agent/skill_managers/skills/pangolin_use.md +147 -0
- pangolin_agent-0.1.0b1/pangolin_agent/toolboxes/__init__.py +9 -0
- pangolin_agent-0.1.0b1/pangolin_agent/toolboxes/basic_toolbox.py +90 -0
- pangolin_agent-0.1.0b1/pangolin_agent/toolboxes/pangolin_toolbox.py +106 -0
- pangolin_agent-0.1.0b1/pangolin_agent/toolboxes/power_tools/create_tool.py +20 -0
- pangolin_agent-0.1.0b1/pangolin_agent/toolboxes/power_tools/delete_file.py +51 -0
- pangolin_agent-0.1.0b1/pangolin_agent/toolboxes/power_tools/run_sh.py +81 -0
- pangolin_agent-0.1.0b1/pangolin_agent/toolboxes/power_tools/write_file.py +51 -0
- pangolin_agent-0.1.0b1/pangolin_agent/toolboxes/tools/__init__.py +0 -0
- pangolin_agent-0.1.0b1/pangolin_agent/toolboxes/tools/browse_files.py +77 -0
- pangolin_agent-0.1.0b1/pangolin_agent/toolboxes/tools/create_skill.py +28 -0
- pangolin_agent-0.1.0b1/pangolin_agent/toolboxes/tools/fetch_page.py +26 -0
- pangolin_agent-0.1.0b1/pangolin_agent/toolboxes/tools/get_utc_time.py +16 -0
- pangolin_agent-0.1.0b1/pangolin_agent/toolboxes/tools/load_skill.py +26 -0
- pangolin_agent-0.1.0b1/pangolin_agent/toolboxes/tools/read_file.py +40 -0
- pangolin_agent-0.1.0b1/pangolin_agent/toolboxes/tools/unload_skill.py +26 -0
- pangolin_agent-0.1.0b1/pangolin_agent/toolboxes/tools/update_chat_summary.py +29 -0
- pangolin_agent-0.1.0b1/pangolin_agent/toolboxes/tools/web_search.py +24 -0
- pangolin_agent-0.1.0b1/pangolin_agent/toolboxes/tools/web_search_playwright.py +116 -0
- pangolin_agent-0.1.0b1/pangolin_agent/utils/__init__.py +16 -0
- pangolin_agent-0.1.0b1/pangolin_agent/utils/copy_built_ins.py +41 -0
- pangolin_agent-0.1.0b1/pangolin_agent/utils/ensure_async.py +19 -0
- pangolin_agent-0.1.0b1/pangolin_agent/utils/token_calculator.py +37 -0
- pangolin_agent-0.1.0b1/pyproject.toml +41 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
Copyright (c) 2026 Gene B. Denney. All Rights Reserved.
|
|
2
|
+
|
|
3
|
+
This software is released under the PolyForm Noncommercial License 1.0.0.
|
|
4
|
+
Non-commercial use is permitted per the license terms below.
|
|
5
|
+
|
|
6
|
+
Commercial use, or any use beyond the strict terms of this license, requires
|
|
7
|
+
explicit written approval from the copyright holder. This may be granted
|
|
8
|
+
for free or for a fee depending on the proposed use. Contact me to request
|
|
9
|
+
a license: gbdenney84@gmail.com.
|
|
10
|
+
|
|
11
|
+
Unapproved commercial use terminates rights under this license.
|
|
12
|
+
|
|
13
|
+
# PolyForm Noncommercial License 1.0.0
|
|
14
|
+
|
|
15
|
+
<https://polyformproject.org/licenses/noncommercial/1.0.0>
|
|
16
|
+
|
|
17
|
+
## Acceptance
|
|
18
|
+
|
|
19
|
+
In order to get any license under these terms, you must agree to them as both strict obligations and conditions to all your licenses.
|
|
20
|
+
|
|
21
|
+
## Copyright License
|
|
22
|
+
|
|
23
|
+
The licensor grants you a copyright license for the software to do everything you might do with the software that would otherwise infringe the licensor's copyright in it for any permitted purpose. However, you may only distribute the software according to [Distribution License](#distribution-license) and make changes or new works based on the software according to [Changes and New Works License](#changes-and-new-works-license).
|
|
24
|
+
|
|
25
|
+
## Distribution License
|
|
26
|
+
|
|
27
|
+
The licensor grants you an additional copyright license to distribute copies of the software. Your license to distribute covers distributing the software with changes and new works permitted by [Changes and New Works License](#changes-and-new-works-license).
|
|
28
|
+
|
|
29
|
+
## Notices
|
|
30
|
+
|
|
31
|
+
You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms or the URL for them above, as well as copies of any plain-text lines beginning with `Required Notice:` that the licensor provided with the software. For example:
|
|
32
|
+
|
|
33
|
+
> Required Notice: Copyright Gene B. Denney (2026)
|
|
34
|
+
|
|
35
|
+
## Changes and New Works License
|
|
36
|
+
|
|
37
|
+
The licensor grants you an additional copyright license to make changes and new works based on the software for any permitted purpose.
|
|
38
|
+
|
|
39
|
+
## Patent License
|
|
40
|
+
|
|
41
|
+
The licensor grants you a patent license for the software that covers patent claims the licensor can license, or becomes able to license, that you would infringe by using the software.
|
|
42
|
+
|
|
43
|
+
## Noncommercial Purposes
|
|
44
|
+
|
|
45
|
+
Any noncommercial purpose is a permitted purpose.
|
|
46
|
+
|
|
47
|
+
## Personal Uses
|
|
48
|
+
|
|
49
|
+
Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, is use for a permitted purpose.
|
|
50
|
+
|
|
51
|
+
## Noncommercial Organizations
|
|
52
|
+
|
|
53
|
+
Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution is use for a permitted purpose regardless of the source of funding or obligations resulting from the funding.
|
|
54
|
+
|
|
55
|
+
## Fair Use
|
|
56
|
+
|
|
57
|
+
You may have "fair use" rights for the software under the law. These terms do not limit them.
|
|
58
|
+
|
|
59
|
+
## No Other Rights
|
|
60
|
+
|
|
61
|
+
These terms do not allow you to sublicense or transfer any of your licenses to anyone else, or prevent the licensor from granting licenses to anyone else. These terms do not imply any other licenses.
|
|
62
|
+
|
|
63
|
+
## Patent Defense
|
|
64
|
+
|
|
65
|
+
If you make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
|
|
66
|
+
|
|
67
|
+
## Violations
|
|
68
|
+
|
|
69
|
+
The first time you are notified in writing that you have violated any of these terms, or done anything with the software not covered by your licenses, your licenses can nonetheless continue if you come into full compliance with these terms, and take practical steps to correct past violations, within 32 days of receiving notice. Otherwise, all your licenses end immediately.
|
|
70
|
+
|
|
71
|
+
## No Liability
|
|
72
|
+
|
|
73
|
+
***As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.***
|
|
74
|
+
|
|
75
|
+
## Definitions
|
|
76
|
+
|
|
77
|
+
The **licensor** is the individual or entity offering these terms, and the **software** is the software the licensor makes available under these terms.
|
|
78
|
+
|
|
79
|
+
**You** refers to the individual or entity agreeing to these terms.
|
|
80
|
+
|
|
81
|
+
**Your company** is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. **Control** means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
|
|
82
|
+
|
|
83
|
+
**Your licenses** are all the licenses granted to you for the software under these terms.
|
|
84
|
+
|
|
85
|
+
**Use** means anything you do with the software requiring one of your licenses.
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: pangolin-agent
|
|
3
|
+
Version: 0.1.0b1
|
|
4
|
+
Dynamic: Description
|
|
5
|
+
Dynamic: Description-Content-Type
|
|
6
|
+
Summary: A lightweight, modular, interface based AI Agentic library.
|
|
7
|
+
Project-URL: Homepage, https://github.com/NerdCult/pangolin
|
|
8
|
+
License: PolyForm Noncommercial 1.0.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: License :: Other/Proprietary License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Requires-Python: >=3.14
|
|
14
|
+
Requires-Dist: dataclasses-json>=0.6.0
|
|
15
|
+
Provides-Extra: all
|
|
16
|
+
Requires-Dist: anthropic>=0.30.0; extra == 'all'
|
|
17
|
+
Requires-Dist: beautifulsoup4>=4.12.0; extra == 'all'
|
|
18
|
+
Requires-Dist: chromadb; extra == 'all'
|
|
19
|
+
Requires-Dist: ddgs>=9.0.0; extra == 'all'
|
|
20
|
+
Requires-Dist: fastembed; extra == 'all'
|
|
21
|
+
Requires-Dist: google-genai>=3.6.0; extra == 'all'
|
|
22
|
+
Requires-Dist: openai>=1.0.0; extra == 'all'
|
|
23
|
+
Requires-Dist: requests>=2.32.0; extra == 'all'
|
|
24
|
+
Provides-Extra: anthropic
|
|
25
|
+
Requires-Dist: anthropic>=0.30.0; extra == 'anthropic'
|
|
26
|
+
Provides-Extra: gemini
|
|
27
|
+
Requires-Dist: google-genai>=3.6.0; extra == 'gemini'
|
|
28
|
+
Provides-Extra: openai
|
|
29
|
+
Requires-Dist: openai>=1.0.0; extra == 'openai'
|
|
30
|
+
Provides-Extra: rag
|
|
31
|
+
Requires-Dist: chromadb; extra == 'rag'
|
|
32
|
+
Requires-Dist: fastembed; extra == 'rag'
|
|
33
|
+
Provides-Extra: tools
|
|
34
|
+
Requires-Dist: beautifulsoup4>=4.12.0; extra == 'tools'
|
|
35
|
+
Requires-Dist: ddgs>=9.0.0; extra == 'tools'
|
|
36
|
+
Requires-Dist: requests>=2.32.0; extra == 'tools'
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
#Copyright (c) 2026 Gene B. Denney. All Rights Reserved.
|
|
2
|
+
|
|
3
|
+
from .cli import cli
|
|
4
|
+
from .agent_runtimes import PangolinRuntime
|
|
5
|
+
from .toolboxes import BasicToolbox, PangolinToolbox
|
|
6
|
+
from .skill_managers import AdvancedSkillManager, SimpleSkillManager, RAGSkillManager
|
|
7
|
+
from .rag_routers import RAGRouter
|
|
8
|
+
from .guardrails import DefaultGuardrail
|
|
9
|
+
from.response_composers import PangolinComposer
|
|
10
|
+
from .utils import copy_built_in_skills, copy_built_in_tools, copy_built_in_power_tools, ensure_async, ensure_async_gen
|
|
11
|
+
from .memory_managers import SQLiteMemoryManager, SimpleMemoryManager
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
'cli',
|
|
16
|
+
'PangolinRuntime',
|
|
17
|
+
'PangolinComposer',
|
|
18
|
+
'DefaultGuardrail',
|
|
19
|
+
'BasicToolbox',
|
|
20
|
+
'PangolinToolbox',
|
|
21
|
+
'copy_built_in_tools',
|
|
22
|
+
'copy_built_in_power_tools',
|
|
23
|
+
'copy_built_in_skills',
|
|
24
|
+
'ensure_async',
|
|
25
|
+
'ensure_async_gen',
|
|
26
|
+
'AdvancedSkillManager',
|
|
27
|
+
'SimpleSkillManager',
|
|
28
|
+
'RAGSkillManager',
|
|
29
|
+
'RAGRouter',
|
|
30
|
+
'SQLiteMemoryManager',
|
|
31
|
+
'SimpleMemoryManager'
|
|
32
|
+
]
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
#Copyright (c) 2026 Gene B. Denney. All Rights Reserved.
|
|
2
|
+
|
|
3
|
+
from typing import Protocol, AsyncGenerator
|
|
4
|
+
import logging, asyncio, threading
|
|
5
|
+
from ..core.interfaces import AgentRuntime, AgentRuntimeToQueue, MemoryManager,SkillManager, Toolbox, ToolboxAdvanced, ResponseComposer, Guardrail, RAGComponent
|
|
6
|
+
from ..core.exceptions import PangolinTypeError, PangolinGuardRailDenial
|
|
7
|
+
from ..core.types import GuardrailEvent
|
|
8
|
+
from ..messages import ChatRecord, Part, AgentMessage, ToolCall
|
|
9
|
+
from ..utils import count_tokens, ensure_async, ensure_async_gen
|
|
10
|
+
from ..memory_managers import SimpleMemoryManager
|
|
11
|
+
|
|
12
|
+
LOGGER = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class PangolinRuntime(AgentRuntime, AgentRuntimeToQueue, Protocol):
|
|
16
|
+
'''
|
|
17
|
+
default implementation of AgentRuntime.
|
|
18
|
+
the only required parameter is composer: ResponseComposer (recommend the PangolinComposer)
|
|
19
|
+
memory_manager will default to SimpleMemoryManager which only tracks the current session in memory.
|
|
20
|
+
'''
|
|
21
|
+
def __init__(self,
|
|
22
|
+
composer: ResponseComposer,
|
|
23
|
+
system_prompt: str = '',
|
|
24
|
+
memory_manager: MemoryManager | None = None,
|
|
25
|
+
skill_manager: SkillManager | None = None,
|
|
26
|
+
toolbox: Toolbox | None = None,
|
|
27
|
+
guardrail: Guardrail | None = None,
|
|
28
|
+
rag_connection: RAGComponent | None = None,
|
|
29
|
+
max_tokens: int = 32_000,
|
|
30
|
+
max_rounds: int = 10):
|
|
31
|
+
'''
|
|
32
|
+
aggregator: initilialized with an api_provider.Client instance.
|
|
33
|
+
system_prompt: system prompt that will be injected at the start of each turn, appended with any skills provided by the skill manager
|
|
34
|
+
memory_manager: maintains the chat history for the session. If None the SimpleMemoryManager will be used, which only stores the current session history in memory with old messages trimmed to keep system prompt, skills, chat history, and tools list under max_token size
|
|
35
|
+
skill_manager: provides skills to the session
|
|
36
|
+
toolbox: provides the available tools and executes them
|
|
37
|
+
rag_component: can be the same as skill_manager or toolbox if RAGComponent | RAGComponentAsync is implemented on them or use the RAGRouter to connect the hook to both
|
|
38
|
+
max_tokens: sets the ceiling for the max context sent to the model (system prompt, skills, chat history, and tools list)
|
|
39
|
+
max_rounds: int = 10 sets the maxiumum rounds the agent can make for a single user message (one round can have multiple tool calls)
|
|
40
|
+
'''
|
|
41
|
+
|
|
42
|
+
LOGGER.info(f'__init__: \n{composer}\n{system_prompt}\n{memory_manager}\n{skill_manager}\n{toolbox}\n{max_tokens}\n{max_rounds} ')
|
|
43
|
+
|
|
44
|
+
if not isinstance(composer, ResponseComposer):
|
|
45
|
+
raise PangolinTypeError('composer parameter does not implement ResponseComposer')
|
|
46
|
+
if memory_manager and not isinstance(memory_manager, MemoryManager):
|
|
47
|
+
raise PangolinTypeError('memory_manager parameter does not implement MemoryManger')
|
|
48
|
+
if skill_manager and not isinstance(skill_manager, SkillManager):
|
|
49
|
+
raise PangolinTypeError('skill_manager parameter does not implement SkillManager')
|
|
50
|
+
if toolbox and not isinstance(toolbox, Toolbox):
|
|
51
|
+
raise PangolinTypeError('toolbox parameter does not implement Toolbox')
|
|
52
|
+
if guardrail and not isinstance(guardrail, Guardrail):
|
|
53
|
+
raise PangolinTypeError('guardrail parameter does not implement Guardrail')
|
|
54
|
+
if rag_connection and not isinstance(rag_connection, RAGComponent):
|
|
55
|
+
raise PangolinTypeError('rag_component parameter does not implement RAGComponent')
|
|
56
|
+
|
|
57
|
+
self.composer: ResponseComposer = composer
|
|
58
|
+
self.system_prompt: str = system_prompt
|
|
59
|
+
self.memory_manager = memory_manager or SimpleMemoryManager()
|
|
60
|
+
self.skill_manager: SkillManager = skill_manager
|
|
61
|
+
self.toolbox: Toolbox = toolbox
|
|
62
|
+
if self.toolbox:
|
|
63
|
+
LOGGER.debug('toolbox found')
|
|
64
|
+
# Advanced toolbox can enable 'session aware tools' giving them access to talk with the skills manager and memory manager but only if the session is provided
|
|
65
|
+
if isinstance(self.toolbox, ToolboxAdvanced):
|
|
66
|
+
LOGGER.debug('toolbox is ToolboxAdvanced')
|
|
67
|
+
self.toolbox.set_session(self)
|
|
68
|
+
# Initaliaze the toolbox
|
|
69
|
+
self.toolbox.load()
|
|
70
|
+
LOGGER.info('toolbox loaded')
|
|
71
|
+
self.guardrail = guardrail
|
|
72
|
+
self.rag_connection = rag_connection
|
|
73
|
+
self.max_tokens = max_tokens
|
|
74
|
+
self.max_rounds = max_rounds
|
|
75
|
+
|
|
76
|
+
async def _add_context(self, record):
|
|
77
|
+
'''
|
|
78
|
+
adds a ChatRecord to the Memory Manger
|
|
79
|
+
'''
|
|
80
|
+
await ensure_async(self.memory_manager.add_record, record)
|
|
81
|
+
while True:
|
|
82
|
+
context = await self._get_context()
|
|
83
|
+
tools = await self._tools()
|
|
84
|
+
if count_tokens(context, tools) > self.max_tokens:
|
|
85
|
+
LOGGER.debug('trimming context')
|
|
86
|
+
await ensure_async(self.memory_manager.trim_history)
|
|
87
|
+
continue
|
|
88
|
+
break
|
|
89
|
+
|
|
90
|
+
async def _get_context(self):
|
|
91
|
+
'''
|
|
92
|
+
Assembles the current record set, without the tools list, as a list[TARecord]
|
|
93
|
+
'''
|
|
94
|
+
LOGGER.debug('_get_context')
|
|
95
|
+
skills = await ensure_async(self.skill_manager.skills) if self.skill_manager else ''
|
|
96
|
+
system = f'{self.system_prompt}\n{skills}'
|
|
97
|
+
system_prompt_record = ChatRecord(role='system', content=[Part(type='text', data={'text':system})])
|
|
98
|
+
history = await ensure_async(self.memory_manager.get_history)
|
|
99
|
+
return [system_prompt_record] + history
|
|
100
|
+
|
|
101
|
+
async def _tools(self) -> str | None:
|
|
102
|
+
''' Get the list of tool schemas '''
|
|
103
|
+
return await ensure_async(self.toolbox.schemas) if self.toolbox else None
|
|
104
|
+
|
|
105
|
+
async def _handle_agent_message(self, agent_message: AgentMessage, tool_calls: list, blocked_tool_call_ids: list, reason: list):
|
|
106
|
+
# if the ApiAggregator yields a message event add it to the context and check it for tool_calls
|
|
107
|
+
if agent_message.behavior == 'message':
|
|
108
|
+
LOGGER.info(f'assistant message recieved: {agent_message.chat_record}')
|
|
109
|
+
if agent_message.chat_record and self.guardrail:
|
|
110
|
+
ger = await ensure_async(self.guardrail.handle_guardrail_event,
|
|
111
|
+
GuardrailEvent(
|
|
112
|
+
event_type='assistant_output',
|
|
113
|
+
chat_record=agent_message.chat_record
|
|
114
|
+
)
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
if ger.event_type == 'deny':
|
|
118
|
+
raise PangolinGuardRailDenial(f'AgentMessage triggered a Guardrail denial {ger.reason}')
|
|
119
|
+
if ger.event_type == 'modify':
|
|
120
|
+
agent_message.chat_record = ger.chat_record
|
|
121
|
+
reason['reason'] = ger.reason
|
|
122
|
+
for rtci in ger.rejected_tool_call_ids or []:
|
|
123
|
+
blocked_tool_call_ids.append(rtci)
|
|
124
|
+
|
|
125
|
+
if agent_message.chat_record.tool_calls:
|
|
126
|
+
for tc in agent_message.chat_record.tool_calls:
|
|
127
|
+
tool_calls.append(tc)
|
|
128
|
+
|
|
129
|
+
await self._add_context(agent_message.chat_record)
|
|
130
|
+
|
|
131
|
+
async def _handle_agent_message(self, agent_message: AgentMessage, tool_calls: list, blocked_tool_call_ids: list, reason: list):
|
|
132
|
+
# if the ApiAggregator yields a message event add it to the context and check it for tool_calls
|
|
133
|
+
if agent_message.behavior == 'message':
|
|
134
|
+
LOGGER.info(f'assistant message recieved: {agent_message.chat_record}')
|
|
135
|
+
if agent_message.chat_record and self.guardrail:
|
|
136
|
+
ger = await self.guardrail.handle_guardrail_event(
|
|
137
|
+
GuardrailEvent(
|
|
138
|
+
event_type='assistant_output',
|
|
139
|
+
chat_record=agent_message.chat_record
|
|
140
|
+
)
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
if ger.event_type == 'deny':
|
|
144
|
+
raise PangolinGuardRailDenial(f'AgentMessage triggered a Guardrail denial {ger.reason}')
|
|
145
|
+
if ger.event_type == 'modify':
|
|
146
|
+
agent_message.chat_record = ger.chat_record
|
|
147
|
+
reason['reason'] = ger.reason
|
|
148
|
+
for rtci in ger.rejected_tool_call_ids or []:
|
|
149
|
+
blocked_tool_call_ids.append(rtci)
|
|
150
|
+
|
|
151
|
+
if agent_message.chat_record.tool_calls:
|
|
152
|
+
for tc in agent_message.chat_record.tool_calls:
|
|
153
|
+
tool_calls.append(tc)
|
|
154
|
+
|
|
155
|
+
await self._add_context(agent_message.chat_record)
|
|
156
|
+
|
|
157
|
+
async def _handle_tool_calls(self, tool_calls: list[ToolCall], blocked_tool_call_ids: list, reason: str) -> None:
|
|
158
|
+
LOGGER.info('making tool calls')
|
|
159
|
+
calls_to_run = []
|
|
160
|
+
for tc in tool_calls:
|
|
161
|
+
if tc.id in blocked_tool_call_ids:
|
|
162
|
+
await self._add_context(ChatRecord(
|
|
163
|
+
role='tool',
|
|
164
|
+
content=[Part(type='text', data={'text': reason})],
|
|
165
|
+
tool_call_id=tc.id))
|
|
166
|
+
else:
|
|
167
|
+
calls_to_run.append(tc)
|
|
168
|
+
|
|
169
|
+
async for result in ensure_async_gen(self.toolbox.handle_tool_calls, calls_to_run):
|
|
170
|
+
chat_record = ChatRecord(role='tool', content=[Part(type='text', data={'text': result.result})], tool_call_id=result.id)
|
|
171
|
+
if self.guardrail:
|
|
172
|
+
ger = await ensure_async(self.guardrail.handle_guardrail_event,
|
|
173
|
+
GuardrailEvent(
|
|
174
|
+
event_type='tool_call_result',
|
|
175
|
+
chat_record=chat_record
|
|
176
|
+
)
|
|
177
|
+
)
|
|
178
|
+
if ger.event_type == 'modify':
|
|
179
|
+
chat_record = ger.chat_record
|
|
180
|
+
|
|
181
|
+
LOGGER.debug(f'TOOL CALL RESULT {chat_record.to_json()}')
|
|
182
|
+
await self._add_context(chat_record)
|
|
183
|
+
|
|
184
|
+
async def _user_message_guardrail_check(self, record: ChatRecord):
|
|
185
|
+
if self.guardrail:
|
|
186
|
+
ger = await ensure_async(self.guardrail.handle_guardrail_event,
|
|
187
|
+
GuardrailEvent(event_type='user_input',
|
|
188
|
+
chat_record=record
|
|
189
|
+
)
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
if ger.event_type == 'deny':
|
|
193
|
+
raise PangolinGuardRailDenial(f'User Message triggered a Guardrail denial {ger.reason}')
|
|
194
|
+
if ger.event_type == 'modify':
|
|
195
|
+
await self._add_context(ger.chat_record)
|
|
196
|
+
return
|
|
197
|
+
|
|
198
|
+
await self._add_context(record)
|
|
199
|
+
|
|
200
|
+
async def handle_user_message(self, record: ChatRecord | str, stop: threading.Event = threading.Event()) -> AsyncGenerator[AgentMessage]:
|
|
201
|
+
''' Main entry point to start the agentic loop. '''
|
|
202
|
+
|
|
203
|
+
if isinstance(record, str):
|
|
204
|
+
record = ChatRecord(role='user', content=[Part(type='text', data={'text': record})])
|
|
205
|
+
elif not isinstance(record, ChatRecord):
|
|
206
|
+
raise PangolinTypeError('message does not implement ChatRecord')
|
|
207
|
+
|
|
208
|
+
LOGGER.info(f'user message recieved: {record.to_json()}')
|
|
209
|
+
try:
|
|
210
|
+
await self._user_message_guardrail_check(record)
|
|
211
|
+
|
|
212
|
+
if self.rag_connection:
|
|
213
|
+
history = await ensure_async(self.memory_manager.get_history)
|
|
214
|
+
await ensure_async(self.rag_connection.retrieve_with_chat_history, history)
|
|
215
|
+
|
|
216
|
+
LOGGER.debug('entering outer loop')
|
|
217
|
+
for _ in range(self.max_rounds):
|
|
218
|
+
if stop.is_set(): break
|
|
219
|
+
|
|
220
|
+
context = await self._get_context() # get the context to send to the api
|
|
221
|
+
tools = await self._tools()
|
|
222
|
+
tool_calls = [] # initialzed to store tool calls
|
|
223
|
+
blocked_tool_call_ids = []
|
|
224
|
+
reason = {'reason': ''}
|
|
225
|
+
|
|
226
|
+
LOGGER.debug('entering inner loop')
|
|
227
|
+
async for agent_message in ensure_async_gen(self.composer.process_context,context, tools):
|
|
228
|
+
if stop.is_set(): break
|
|
229
|
+
LOGGER.debug(f'agent message recieved: {agent_message}')
|
|
230
|
+
await self._handle_agent_message(agent_message, tool_calls, blocked_tool_call_ids, reason)
|
|
231
|
+
|
|
232
|
+
yield agent_message
|
|
233
|
+
|
|
234
|
+
# if the stream ends with no tool calls the loop exits
|
|
235
|
+
if tool_calls and self.toolbox:
|
|
236
|
+
LOGGER.debug(f'runtime got {len(tool_calls)} tool calls')
|
|
237
|
+
await self._handle_tool_calls(tool_calls, blocked_tool_call_ids, reason['reason'])
|
|
238
|
+
else:
|
|
239
|
+
break
|
|
240
|
+
except PangolinGuardRailDenial as grd:
|
|
241
|
+
yield AgentMessage(behavior='guardrail', content=grd)
|
|
242
|
+
except BaseException as e:
|
|
243
|
+
yield AgentMessage(behavior='error', content=e)
|
|
244
|
+
|
|
245
|
+
async def handle_user_message_to_queue(self, record: ChatRecord | str,
|
|
246
|
+
queue: asyncio.Queue[AgentMessage | None],
|
|
247
|
+
stop: threading.Event = threading.Event(),
|
|
248
|
+
done: threading.Event = threading.Event()):
|
|
249
|
+
''' To ansycio.Queue entry point to start the agentic loop. '''
|
|
250
|
+
|
|
251
|
+
if isinstance(record, str):
|
|
252
|
+
record = ChatRecord(role='user', content=[Part(type='text', data={'text': record})])
|
|
253
|
+
elif not isinstance(record, ChatRecord):
|
|
254
|
+
raise PangolinTypeError('message does not implement ChatRecord')
|
|
255
|
+
|
|
256
|
+
LOGGER.info(f'user message recieved: {record.to_json()}')
|
|
257
|
+
|
|
258
|
+
async def task_done():
|
|
259
|
+
await queue.put(None)
|
|
260
|
+
done.set()
|
|
261
|
+
|
|
262
|
+
try:
|
|
263
|
+
await self._user_message_guardrail_check(record)
|
|
264
|
+
|
|
265
|
+
if self.rag_connection:
|
|
266
|
+
history = await ensure_async(self.memory_manager.get_history)
|
|
267
|
+
await ensure_async(self.rag_connection.retrieve_with_chat_history, history)
|
|
268
|
+
|
|
269
|
+
for _ in range(self.max_rounds):
|
|
270
|
+
if stop.is_set(): break
|
|
271
|
+
|
|
272
|
+
context = await self._get_context() # get the context to send to the api
|
|
273
|
+
tools = await self._tools()
|
|
274
|
+
tool_calls = [] # initialzed to store tool calls
|
|
275
|
+
blocked_tool_call_ids = []
|
|
276
|
+
reason = {'reason': ''}
|
|
277
|
+
|
|
278
|
+
async for agent_message in ensure_async_gen(self.composer.process_context, context,tools):
|
|
279
|
+
await self._handle_agent_message(agent_message, tool_calls, blocked_tool_call_ids, reason)
|
|
280
|
+
await queue.put(agent_message)
|
|
281
|
+
|
|
282
|
+
if stop.is_set():
|
|
283
|
+
break
|
|
284
|
+
|
|
285
|
+
# if the stream ends with no tool calls the loop exits
|
|
286
|
+
if tool_calls and self.toolbox:
|
|
287
|
+
LOGGER.debug(f'runtime got {len(tool_calls)} tool calls')
|
|
288
|
+
await self._handle_tool_calls(tool_calls, blocked_tool_call_ids, reason['reason'])
|
|
289
|
+
else:
|
|
290
|
+
await task_done()
|
|
291
|
+
break
|
|
292
|
+
|
|
293
|
+
if stop.is_set():
|
|
294
|
+
await task_done()
|
|
295
|
+
break
|
|
296
|
+
except PangolinGuardRailDenial as grd:
|
|
297
|
+
await queue.put(AgentMessage(behavior='guardrail', content=grd))
|
|
298
|
+
except BaseException as e:
|
|
299
|
+
await queue.put(AgentMessage(behavior='error', content=e))
|