tina-python 0.7.3__tar.gz → 0.7.5__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.
- {tina_python-0.7.3/tina_python.egg-info → tina_python-0.7.5}/PKG-INFO +21 -9
- {tina_python-0.7.3 → tina_python-0.7.5}/README.md +20 -8
- {tina_python-0.7.3 → tina_python-0.7.5}/pyproject.toml +16 -2
- {tina_python-0.7.3 → tina_python-0.7.5}/setup.py +1 -1
- tina_python-0.7.5/tina/__init__.py +49 -0
- tina_python-0.7.5/tina/agent/__init__.py +58 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/agent/agent.py +112 -33
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/agent/core/agent_response.py +0 -1
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/agent/core/agent_runtime.py +184 -47
- tina_python-0.7.5/tina/agent/core/context_manager.py +16 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/agent/core/events.py +0 -1
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/agent/core/executor.py +34 -3
- tina_python-0.7.5/tina/agent/core/message_view.py +23 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/agent/multimodal_agent.py +35 -36
- tina_python-0.7.5/tina/context/__init__.py +53 -0
- tina_python-0.7.5/tina/context/manager.py +494 -0
- tina_python-0.7.5/tina/context/message.py +641 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/core/error.py +1 -1
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/llm/base_api.py +9 -5
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/llm/base_multimodal_api.py +0 -4
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/mcp/client.py +0 -2
- tina_python-0.7.5/tina/utils/__init__.py +56 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/agent_worker.py +15 -4
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/coding_context.py +3 -17
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/coding_tools.py +16 -213
- tina_python-0.7.5/tina/utils/deepseek.py +32 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/multimodal_formatter.py +20 -1
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/output_parser.py +0 -1
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/run_agent_in_cli.py +2 -3
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/session_store.py +1 -1
- {tina_python-0.7.3 → tina_python-0.7.5/tina_python.egg-info}/PKG-INFO +21 -9
- {tina_python-0.7.3 → tina_python-0.7.5}/tina_python.egg-info/SOURCES.txt +4 -0
- tina_python-0.7.3/tina/__init__.py +0 -24
- tina_python-0.7.3/tina/agent/__init__.py +0 -25
- tina_python-0.7.3/tina/agent/core/context_manager.py +0 -511
- tina_python-0.7.3/tina/utils/__init__.py +0 -16
- tina_python-0.7.3/tina/utils/deepseek.py +0 -119
- {tina_python-0.7.3 → tina_python-0.7.5}/LICENSE +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/MANIFEST.in +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/setup.cfg +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/agent/core/__init__.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/agent/core/keyword_actions.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/agent/core/prompt.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/agent/core/state.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/agent/core/tools.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/core/__init__.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/core/logger.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/llm/__init__.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/llm/files_api.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/mcp/__init__.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/mcp/mcp_tools_executor.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/py.typed +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/doc_parser.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/env_reader.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/multi_agent/__init__.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/multi_agent/environment.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/multi_agent/message.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/multi_agent/message_bus.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/multi_agent/web/__init__.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/multi_agent/web/server.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/tui/__init__.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/tui/context_manager.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/tui/token.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/tui/tui.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/type_mapper.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/url.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina/utils/usage.py +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina_python.egg-info/dependency_links.txt +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina_python.egg-info/requires.txt +0 -0
- {tina_python-0.7.3 → tina_python-0.7.5}/tina_python.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tina-python
|
|
3
|
-
Version: 0.7.
|
|
3
|
+
Version: 0.7.5
|
|
4
4
|
Summary: tina is in your computer!
|
|
5
5
|
Home-page: https://gitee.com/wang-churi/tina
|
|
6
6
|
Author: 王出日
|
|
@@ -44,6 +44,9 @@ Dynamic: requires-python
|
|
|
44
44
|
|
|
45
45
|
一个轻量、模块化的 AI 智能体框架,基于 OpenAI API 格式构建。
|
|
46
46
|
|
|
47
|
+
> 刚上手先看 **[开发实战指南](docs/guide.md)**:从 20 行脚本到 code agent / 多 agent 团队,
|
|
48
|
+
> 全部用实际在跑的项目做例子。API 细节查 [docs/agent.md](docs/agent.md)。
|
|
49
|
+
|
|
47
50
|
## 安装
|
|
48
51
|
|
|
49
52
|
```bash
|
|
@@ -496,11 +499,13 @@ agent = Agent(
|
|
|
496
499
|
context_manager=None, # 可选:自定义 ContextManager
|
|
497
500
|
agent_runtime=None, # 可选:自定义 AgentRuntime
|
|
498
501
|
max_tool_loop=30, # 最大工具调用循环次数
|
|
499
|
-
max_context_length=100000,# 最大上下文长度(字符)
|
|
500
|
-
max_tool_result_length=6000, # 工具结果最大长度
|
|
501
502
|
)
|
|
502
503
|
```
|
|
503
504
|
|
|
505
|
+
> **框架不管上下文长度**:`ContextManager` 默认不做任何裁剪,工具结果也原样保留。
|
|
506
|
+
> 历史会一直增长 —— 裁剪/压缩策略自己决定(传自定义 `context_manager`,或覆写
|
|
507
|
+
> `limit_messages()`)。详见 [docs/agent.md](docs/agent.md#上下文管理器-contextmanager)。
|
|
508
|
+
|
|
504
509
|
### 3.2 使用 Agent 对话
|
|
505
510
|
|
|
506
511
|
```python
|
|
@@ -940,7 +945,9 @@ async def main():
|
|
|
940
945
|
asyncio.run(main())
|
|
941
946
|
```
|
|
942
947
|
|
|
943
|
-
`AgentWorker`
|
|
948
|
+
`AgentWorker` 是一个**示例实现**,演示「队列 + 串行处理」这个模式。默认**不会**把
|
|
949
|
+
`put` 注册成工具;需要多 Agent 互相派活时显式传 `register_put_tool=True`
|
|
950
|
+
(它会直接改 `agent.tools`,多个 Agent 共用同一个 `Tools` 包时会串)。
|
|
944
951
|
|
|
945
952
|
### 6.2 终端交互控制台 `run_agent_in_cli`
|
|
946
953
|
|
|
@@ -1098,9 +1105,9 @@ MultiAgentWeb(env, port=7077).run()
|
|
|
1098
1105
|
|
|
1099
1106
|
```python
|
|
1100
1107
|
from tina import Agent
|
|
1101
|
-
from tina.utils
|
|
1108
|
+
from tina.utils import CodingTools
|
|
1102
1109
|
|
|
1103
|
-
tools = CodingTools(root=".").get_tools() # code_read / code_glob / code_grep / code_write / code_edit /
|
|
1110
|
+
tools = CodingTools(root=".").get_tools() # code_read / code_glob / code_grep / code_write / code_edit / code_bash
|
|
1104
1111
|
agent = Agent(llm=llm, tools=tools)
|
|
1105
1112
|
```
|
|
1106
1113
|
|
|
@@ -1111,22 +1118,27 @@ agent = Agent(llm=llm, tools=tools)
|
|
|
1111
1118
|
| `code_grep` | 正则搜索(`include` / 上下文行 / 忽略大小写) | 否 |
|
|
1112
1119
|
| `code_write` | 整文件写入(`.py` 写前语法校验) | ✅ |
|
|
1113
1120
|
| `code_edit` | 精确文本替换(命中多次需 `replace_all`,返回 diff) | ✅ |
|
|
1114
|
-
| `code_patch` | 应用 unified diff 补丁(一次多处/跨行改动,行号可偏差、上下文对不上则整体中止) | ✅ |
|
|
1115
1121
|
| `code_bash` | 执行命令(首尾截断、带 exit code) | ✅ |
|
|
1116
1122
|
|
|
1123
|
+
> 需要一次改同一文件多处时,**并行发多个 `code_edit`** 就行 —— tina 会把同一条消息里的
|
|
1124
|
+
> 多个工具调用并发执行(同一文件的读-改-写有文件锁串行)。这比曾经的 `code_patch`
|
|
1125
|
+
> (unified diff)可靠得多:一个失败不影响其它,错误信息也精确到具体那一处。
|
|
1126
|
+
|
|
1117
1127
|
配套的项目感知上下文管理器(自动注入 `AGENTS.md` / `.tinacode.md` / `.tina.md`、工作目录与目录结构摘要,可用 `refresh_project_context()` 刷新):
|
|
1118
1128
|
|
|
1119
1129
|
既可在构造时传入,也可之后替换:
|
|
1120
1130
|
|
|
1121
1131
|
```python
|
|
1122
1132
|
from tina import Agent
|
|
1123
|
-
from tina.utils
|
|
1133
|
+
from tina.utils import CodingContextManager
|
|
1124
1134
|
|
|
1125
1135
|
agent = Agent(llm=llm, tools=tools, context_manager=CodingContextManager(root="."))
|
|
1126
1136
|
# 或:agent.set_context_manager(CodingContextManager(root="."))
|
|
1127
1137
|
```
|
|
1128
1138
|
|
|
1129
|
-
> 未显式传 `system_prompt` 时,Agent 会**保留** `context_manager`
|
|
1139
|
+
> 未显式传 `system_prompt` 时,Agent 会**保留** `context_manager` 自带的系统提示;
|
|
1140
|
+
> 两者都没有就**没有 system 消息**(框架不再注入任何默认人设)。
|
|
1141
|
+
> 想要现成的可以自己传 `tina.agent.core.prompt.Prompt("tina").prompt`。
|
|
1130
1142
|
|
|
1131
1143
|
---
|
|
1132
1144
|
|
|
@@ -6,6 +6,9 @@
|
|
|
6
6
|
|
|
7
7
|
一个轻量、模块化的 AI 智能体框架,基于 OpenAI API 格式构建。
|
|
8
8
|
|
|
9
|
+
> 刚上手先看 **[开发实战指南](docs/guide.md)**:从 20 行脚本到 code agent / 多 agent 团队,
|
|
10
|
+
> 全部用实际在跑的项目做例子。API 细节查 [docs/agent.md](docs/agent.md)。
|
|
11
|
+
|
|
9
12
|
## 安装
|
|
10
13
|
|
|
11
14
|
```bash
|
|
@@ -458,11 +461,13 @@ agent = Agent(
|
|
|
458
461
|
context_manager=None, # 可选:自定义 ContextManager
|
|
459
462
|
agent_runtime=None, # 可选:自定义 AgentRuntime
|
|
460
463
|
max_tool_loop=30, # 最大工具调用循环次数
|
|
461
|
-
max_context_length=100000,# 最大上下文长度(字符)
|
|
462
|
-
max_tool_result_length=6000, # 工具结果最大长度
|
|
463
464
|
)
|
|
464
465
|
```
|
|
465
466
|
|
|
467
|
+
> **框架不管上下文长度**:`ContextManager` 默认不做任何裁剪,工具结果也原样保留。
|
|
468
|
+
> 历史会一直增长 —— 裁剪/压缩策略自己决定(传自定义 `context_manager`,或覆写
|
|
469
|
+
> `limit_messages()`)。详见 [docs/agent.md](docs/agent.md#上下文管理器-contextmanager)。
|
|
470
|
+
|
|
466
471
|
### 3.2 使用 Agent 对话
|
|
467
472
|
|
|
468
473
|
```python
|
|
@@ -902,7 +907,9 @@ async def main():
|
|
|
902
907
|
asyncio.run(main())
|
|
903
908
|
```
|
|
904
909
|
|
|
905
|
-
`AgentWorker`
|
|
910
|
+
`AgentWorker` 是一个**示例实现**,演示「队列 + 串行处理」这个模式。默认**不会**把
|
|
911
|
+
`put` 注册成工具;需要多 Agent 互相派活时显式传 `register_put_tool=True`
|
|
912
|
+
(它会直接改 `agent.tools`,多个 Agent 共用同一个 `Tools` 包时会串)。
|
|
906
913
|
|
|
907
914
|
### 6.2 终端交互控制台 `run_agent_in_cli`
|
|
908
915
|
|
|
@@ -1060,9 +1067,9 @@ MultiAgentWeb(env, port=7077).run()
|
|
|
1060
1067
|
|
|
1061
1068
|
```python
|
|
1062
1069
|
from tina import Agent
|
|
1063
|
-
from tina.utils
|
|
1070
|
+
from tina.utils import CodingTools
|
|
1064
1071
|
|
|
1065
|
-
tools = CodingTools(root=".").get_tools() # code_read / code_glob / code_grep / code_write / code_edit /
|
|
1072
|
+
tools = CodingTools(root=".").get_tools() # code_read / code_glob / code_grep / code_write / code_edit / code_bash
|
|
1066
1073
|
agent = Agent(llm=llm, tools=tools)
|
|
1067
1074
|
```
|
|
1068
1075
|
|
|
@@ -1073,22 +1080,27 @@ agent = Agent(llm=llm, tools=tools)
|
|
|
1073
1080
|
| `code_grep` | 正则搜索(`include` / 上下文行 / 忽略大小写) | 否 |
|
|
1074
1081
|
| `code_write` | 整文件写入(`.py` 写前语法校验) | ✅ |
|
|
1075
1082
|
| `code_edit` | 精确文本替换(命中多次需 `replace_all`,返回 diff) | ✅ |
|
|
1076
|
-
| `code_patch` | 应用 unified diff 补丁(一次多处/跨行改动,行号可偏差、上下文对不上则整体中止) | ✅ |
|
|
1077
1083
|
| `code_bash` | 执行命令(首尾截断、带 exit code) | ✅ |
|
|
1078
1084
|
|
|
1085
|
+
> 需要一次改同一文件多处时,**并行发多个 `code_edit`** 就行 —— tina 会把同一条消息里的
|
|
1086
|
+
> 多个工具调用并发执行(同一文件的读-改-写有文件锁串行)。这比曾经的 `code_patch`
|
|
1087
|
+
> (unified diff)可靠得多:一个失败不影响其它,错误信息也精确到具体那一处。
|
|
1088
|
+
|
|
1079
1089
|
配套的项目感知上下文管理器(自动注入 `AGENTS.md` / `.tinacode.md` / `.tina.md`、工作目录与目录结构摘要,可用 `refresh_project_context()` 刷新):
|
|
1080
1090
|
|
|
1081
1091
|
既可在构造时传入,也可之后替换:
|
|
1082
1092
|
|
|
1083
1093
|
```python
|
|
1084
1094
|
from tina import Agent
|
|
1085
|
-
from tina.utils
|
|
1095
|
+
from tina.utils import CodingContextManager
|
|
1086
1096
|
|
|
1087
1097
|
agent = Agent(llm=llm, tools=tools, context_manager=CodingContextManager(root="."))
|
|
1088
1098
|
# 或:agent.set_context_manager(CodingContextManager(root="."))
|
|
1089
1099
|
```
|
|
1090
1100
|
|
|
1091
|
-
> 未显式传 `system_prompt` 时,Agent 会**保留** `context_manager`
|
|
1101
|
+
> 未显式传 `system_prompt` 时,Agent 会**保留** `context_manager` 自带的系统提示;
|
|
1102
|
+
> 两者都没有就**没有 system 消息**(框架不再注入任何默认人设)。
|
|
1103
|
+
> 想要现成的可以自己传 `tina.agent.core.prompt.Prompt("tina").prompt`。
|
|
1092
1104
|
|
|
1093
1105
|
---
|
|
1094
1106
|
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "tina-python"
|
|
7
|
-
version = "0.7.
|
|
7
|
+
version = "0.7.5"
|
|
8
8
|
description = "tina is in your computer!"
|
|
9
9
|
readme = { file = "README.md", content-type = "text/markdown" }
|
|
10
10
|
requires-python = ">=3.10"
|
|
@@ -64,4 +64,18 @@ where = ["."]
|
|
|
64
64
|
include = ["tina*"]
|
|
65
65
|
|
|
66
66
|
[tool.setuptools.package-data]
|
|
67
|
-
tina = ["py.typed"]
|
|
67
|
+
tina = ["py.typed"]
|
|
68
|
+
|
|
69
|
+
[tool.ruff]
|
|
70
|
+
line-length = 88
|
|
71
|
+
target-version = "py310"
|
|
72
|
+
|
|
73
|
+
[tool.ruff.lint]
|
|
74
|
+
# 只开能抓真问题的规则:
|
|
75
|
+
# F = pyflakes(未使用导入/变量、f-string 占位符)
|
|
76
|
+
# E9 = 语法/运行时错误
|
|
77
|
+
# B006 = 可变默认参数
|
|
78
|
+
# RUF012 = 可变类属性(同样会在实例之间共享)
|
|
79
|
+
# 类型注解现代化(UP*)、盲 except(BLE001)、try-except-pass(S110)暂时不开 ——
|
|
80
|
+
# 项目里大量是刻意的防御性写法,强制改只会制造噪音。
|
|
81
|
+
select = ["F", "E9", "B006", "RUF012"]
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
from tina.agent import (
|
|
2
|
+
Agent,
|
|
3
|
+
Tools,
|
|
4
|
+
KeywordActions,
|
|
5
|
+
ContextManager,
|
|
6
|
+
MultimodalContextManager,
|
|
7
|
+
ToolsExecutor,
|
|
8
|
+
MultimodalAgent,
|
|
9
|
+
AgentState,
|
|
10
|
+
AgentEvents,
|
|
11
|
+
)
|
|
12
|
+
from tina.context import (
|
|
13
|
+
AssistantMessage,
|
|
14
|
+
Message,
|
|
15
|
+
RawMessage,
|
|
16
|
+
SystemMessage,
|
|
17
|
+
ToolCalls,
|
|
18
|
+
ToolResultMessage,
|
|
19
|
+
UserMessage,
|
|
20
|
+
check_pairs,
|
|
21
|
+
dump_messages,
|
|
22
|
+
parse_messages,
|
|
23
|
+
)
|
|
24
|
+
from tina.llm.files_api import FilesAPI
|
|
25
|
+
|
|
26
|
+
__all__ = [
|
|
27
|
+
"Agent",
|
|
28
|
+
"Tools",
|
|
29
|
+
"KeywordActions",
|
|
30
|
+
"ContextManager",
|
|
31
|
+
"MultimodalContextManager",
|
|
32
|
+
"ToolsExecutor",
|
|
33
|
+
"MultimodalAgent",
|
|
34
|
+
"AgentState",
|
|
35
|
+
"AgentEvents",
|
|
36
|
+
"FilesAPI",
|
|
37
|
+
# 消息对象(上下文层)
|
|
38
|
+
"Message",
|
|
39
|
+
"SystemMessage",
|
|
40
|
+
"UserMessage",
|
|
41
|
+
"AssistantMessage",
|
|
42
|
+
"ToolCalls",
|
|
43
|
+
"ToolResultMessage",
|
|
44
|
+
"RawMessage",
|
|
45
|
+
"parse_messages",
|
|
46
|
+
"dump_messages",
|
|
47
|
+
"check_pairs",
|
|
48
|
+
]
|
|
49
|
+
__version__ = "0.7.5"
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
from .agent import Agent
|
|
2
|
+
from .multimodal_agent import MultimodalAgent
|
|
3
|
+
from .core.tools import Tools
|
|
4
|
+
from .core.keyword_actions import KeywordActions
|
|
5
|
+
from tina.context import (
|
|
6
|
+
AssistantMessage,
|
|
7
|
+
BaseContextManager,
|
|
8
|
+
ContextManager,
|
|
9
|
+
Message,
|
|
10
|
+
MultimodalContextManager,
|
|
11
|
+
RawMessage,
|
|
12
|
+
SystemMessage,
|
|
13
|
+
ToolCalls,
|
|
14
|
+
ToolResultMessage,
|
|
15
|
+
UserMessage,
|
|
16
|
+
check_pairs,
|
|
17
|
+
dump_messages,
|
|
18
|
+
parse_messages,
|
|
19
|
+
)
|
|
20
|
+
from .core.executor import ToolsExecutor
|
|
21
|
+
from .core.agent_runtime import (
|
|
22
|
+
BaseAgentRuntime,
|
|
23
|
+
ToolCallingAgentRuntime,
|
|
24
|
+
ToolCallingMutilemodalAgentRuntime,
|
|
25
|
+
ToolCallingMultimodalAgentRuntime,
|
|
26
|
+
)
|
|
27
|
+
from .core.state import AgentState
|
|
28
|
+
from .core.events import AgentEvents
|
|
29
|
+
from .core.agent_response import AgentResponse, ToolCall
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
"Agent",
|
|
33
|
+
"Tools",
|
|
34
|
+
"KeywordActions",
|
|
35
|
+
"BaseContextManager",
|
|
36
|
+
"ContextManager",
|
|
37
|
+
"MultimodalContextManager",
|
|
38
|
+
"ToolsExecutor",
|
|
39
|
+
"MultimodalAgent",
|
|
40
|
+
"BaseAgentRuntime",
|
|
41
|
+
"ToolCallingAgentRuntime",
|
|
42
|
+
"ToolCallingMutilemodalAgentRuntime",
|
|
43
|
+
"ToolCallingMultimodalAgentRuntime",
|
|
44
|
+
"AgentState",
|
|
45
|
+
"AgentEvents",
|
|
46
|
+
"AgentResponse",
|
|
47
|
+
"ToolCall",
|
|
48
|
+
"Message",
|
|
49
|
+
"SystemMessage",
|
|
50
|
+
"UserMessage",
|
|
51
|
+
"AssistantMessage",
|
|
52
|
+
"ToolCalls",
|
|
53
|
+
"ToolResultMessage",
|
|
54
|
+
"RawMessage",
|
|
55
|
+
"parse_messages",
|
|
56
|
+
"dump_messages",
|
|
57
|
+
"check_pairs",
|
|
58
|
+
]
|
|
@@ -29,8 +29,7 @@ from ..llm.base_api import BaseAPI
|
|
|
29
29
|
from .core.tools import Tools
|
|
30
30
|
from .core.keyword_actions import KeywordActions
|
|
31
31
|
from ..mcp.client import MCPClient, RemoteType, StdioType
|
|
32
|
-
from .
|
|
33
|
-
from .core.context_manager import ContextManager
|
|
32
|
+
from tina.context import ContextManager
|
|
34
33
|
from .core.agent_runtime import ToolCallingAgentRuntime, BaseAgentRuntime
|
|
35
34
|
from .core.events import AgentEvents
|
|
36
35
|
from ..core.error import TinaError
|
|
@@ -62,9 +61,7 @@ class Agent:
|
|
|
62
61
|
events: AgentEvents = None,
|
|
63
62
|
context_manager: ContextManager = None,
|
|
64
63
|
agent_runtime: BaseAgentRuntime = None,
|
|
65
|
-
max_tool_loop: int =
|
|
66
|
-
max_context_length: int = 100000,
|
|
67
|
-
max_tool_result_length: int = 6000,
|
|
64
|
+
max_tool_loop: int = 100,
|
|
68
65
|
name: str = None,
|
|
69
66
|
keyword_actions: KeywordActions = None,
|
|
70
67
|
):
|
|
@@ -76,8 +73,6 @@ class Agent:
|
|
|
76
73
|
tools: tina.Tools类型,工具集
|
|
77
74
|
sys_prompt: str 系统提示词
|
|
78
75
|
MCP: tina.MCPClient类型,MCP客户端对象,如果不传入,则不进行MCP调用。
|
|
79
|
-
context_length: int 最大上下文长度,超过该长度则删除旧消息,保留最近的消息。
|
|
80
|
-
context_limit: int 上下文限制,使用大模型来总结你的上下文,数字为0时不触发
|
|
81
76
|
max_tool_loop: int 最大工具调用次数,超过该次数则停止调用工具
|
|
82
77
|
name: str 智能体名字,用于多Agent区分
|
|
83
78
|
keyword_actions: KeywordActions,可选关键词动作(不进 tools schema);
|
|
@@ -95,24 +90,20 @@ class Agent:
|
|
|
95
90
|
if context_manager is None:
|
|
96
91
|
if agent_runtime is not None:
|
|
97
92
|
context_manager = agent_runtime.context_manager
|
|
98
|
-
|
|
99
|
-
context_manager = ContextManager(
|
|
100
|
-
max_length=max_context_length,
|
|
101
|
-
max_tool_result_length=max_tool_result_length,
|
|
102
|
-
)
|
|
93
|
+
if context_manager is None: # runtime 自己也没带,兜底新建一个
|
|
94
|
+
context_manager = ContextManager()
|
|
103
95
|
self.context_manager = context_manager
|
|
104
96
|
# 初始化MCP
|
|
105
97
|
self._mcp_to_tools(mcp)
|
|
106
98
|
self.events = AgentEvents() if events is None else events
|
|
107
99
|
if system_prompt is not None:
|
|
108
100
|
self.context_manager.set_system_message(system_prompt)
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
101
|
+
# 没给 system_prompt 就**不注入任何默认提示词**:
|
|
102
|
+
# 要么沿用 context_manager 自带的(如 CodingContextManager),要么就没有
|
|
103
|
+
# system 消息。角色设定是产品决策,框架不该替你选。
|
|
104
|
+
# 想要现成的可以用 `Prompt("tina").prompt` 自己传。
|
|
113
105
|
self._inject_keyword_actions_prompt()
|
|
114
106
|
|
|
115
|
-
self.messages = self.context_manager.get_messages()
|
|
116
107
|
if agent_runtime is None:
|
|
117
108
|
self.runtime = ToolCallingAgentRuntime(
|
|
118
109
|
self.llm,
|
|
@@ -124,15 +115,32 @@ class Agent:
|
|
|
124
115
|
keyword_actions=self.keyword_actions,
|
|
125
116
|
)
|
|
126
117
|
else:
|
|
127
|
-
self.
|
|
128
|
-
self.runtime.context_manager = self.context_manager
|
|
129
|
-
if (
|
|
130
|
-
self.keyword_actions is not None
|
|
131
|
-
and getattr(self.runtime, "keyword_actions", None) is None
|
|
132
|
-
):
|
|
133
|
-
self.runtime.keyword_actions = self.keyword_actions
|
|
118
|
+
self.set_runtime(agent_runtime)
|
|
134
119
|
self.other_agents = []
|
|
135
120
|
|
|
121
|
+
def set_runtime(self, runtime: BaseAgentRuntime) -> None:
|
|
122
|
+
"""替换运行时,并把它需要的引用同步过去
|
|
123
|
+
|
|
124
|
+
直接用 ``agent.runtime = xxx`` 也行,但那样得自己记得同步
|
|
125
|
+
``context_manager`` / ``keyword_actions``,漏一个就会两边各说各话
|
|
126
|
+
(运行时用的是旧上下文管理器 → 模型看到的和 Agent 维护的不是一份)。
|
|
127
|
+
|
|
128
|
+
自定义运行时如果想拿到 Agent 本身的引用(比如要在一轮推理前后回调
|
|
129
|
+
Agent),实现 ``attach(agent)`` 即可,这里会调用它。
|
|
130
|
+
"""
|
|
131
|
+
self.runtime = runtime
|
|
132
|
+
# 运行时必须跟着当前 Agent 的上下文管理器走
|
|
133
|
+
runtime.context_manager = self.context_manager
|
|
134
|
+
# keyword_actions 只在运行时自己没有时才补,别覆盖它自己的配置
|
|
135
|
+
if (
|
|
136
|
+
self.keyword_actions is not None
|
|
137
|
+
and getattr(runtime, "keyword_actions", None) is None
|
|
138
|
+
):
|
|
139
|
+
runtime.keyword_actions = self.keyword_actions
|
|
140
|
+
attach = getattr(runtime, "attach", None)
|
|
141
|
+
if callable(attach):
|
|
142
|
+
attach(self)
|
|
143
|
+
|
|
136
144
|
def set_keyword_actions(self, keyword_actions: KeywordActions | None) -> None:
|
|
137
145
|
"""挂载或替换 KeywordActions;会同步更新 system prompt 末尾的说明块。"""
|
|
138
146
|
self.keyword_actions = keyword_actions
|
|
@@ -150,18 +158,24 @@ class Agent:
|
|
|
150
158
|
将 KeywordActions 说明块追加到 system prompt 末尾:
|
|
151
159
|
<keyword_action>...</keyword_action>
|
|
152
160
|
"""
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
)
|
|
161
|
+
existing = self.context_manager.get_system_message() or ""
|
|
162
|
+
prompt = self._strip_keyword_action_block(existing)
|
|
156
163
|
block = ""
|
|
157
164
|
if self.keyword_actions is not None:
|
|
158
165
|
block = self.keyword_actions.build_prompt_block()
|
|
166
|
+
|
|
159
167
|
if block:
|
|
160
168
|
wrapped = f"<keyword_action>\n{block}\n</keyword_action>"
|
|
161
|
-
|
|
169
|
+
new_prompt = f"{prompt}\n\n{wrapped}" if prompt else wrapped
|
|
162
170
|
logger.warn("使用了关键词动作,会影响你的系统提示词")
|
|
163
171
|
else:
|
|
164
|
-
|
|
172
|
+
new_prompt = prompt
|
|
173
|
+
|
|
174
|
+
# 没变化就不动 —— 否则「没给 system_prompt 也没关键词」的情况下,
|
|
175
|
+
# 这里会凭空造出一条 content 为空的 system 消息。
|
|
176
|
+
if new_prompt == existing:
|
|
177
|
+
return
|
|
178
|
+
self.context_manager.set_system_message(new_prompt)
|
|
165
179
|
|
|
166
180
|
def build_keyword_actions_prompt_block(self) -> str:
|
|
167
181
|
"""生成可拼进 system prompt 的关键词动作说明块。"""
|
|
@@ -354,6 +368,24 @@ class Agent:
|
|
|
354
368
|
"""
|
|
355
369
|
self.events.add_before_llm_call_handler(func)
|
|
356
370
|
|
|
371
|
+
def on_usage(self):
|
|
372
|
+
"""
|
|
373
|
+
每次 LLM 请求返回 usage 时触发
|
|
374
|
+
|
|
375
|
+
需要事件处理函数接受下面的参数:
|
|
376
|
+
usage: tina.utils.Usage
|
|
377
|
+
"""
|
|
378
|
+
return self.events.on_usage()
|
|
379
|
+
|
|
380
|
+
def add_on_usage_handler(self, func: callable | list[callable]):
|
|
381
|
+
"""
|
|
382
|
+
每次 LLM 请求返回 usage 时触发
|
|
383
|
+
|
|
384
|
+
需要事件处理函数接受下面的参数:
|
|
385
|
+
usage: tina.utils.Usage
|
|
386
|
+
"""
|
|
387
|
+
self.events.add_on_usage_handler(func)
|
|
388
|
+
|
|
357
389
|
def _mcp_to_tools(self, MCP):
|
|
358
390
|
"""如果传入了MCP,则将MCP的工具集加入到当前的工具集中"""
|
|
359
391
|
try:
|
|
@@ -384,29 +416,76 @@ class Agent:
|
|
|
384
416
|
except Exception: # noqa: BLE001 工具集未命名等情况下静默跳过
|
|
385
417
|
pass
|
|
386
418
|
|
|
419
|
+
@property
|
|
420
|
+
def messages(self) -> list:
|
|
421
|
+
"""当前上下文的消息对象列表(活引用,永远和 context_manager 同步)
|
|
422
|
+
|
|
423
|
+
读它总是拿到最新的那一份(不会因为换过 context_manager 而拿到旧列表)。
|
|
424
|
+
赋值等价于 `context_manager.set_messages(...)`。
|
|
425
|
+
|
|
426
|
+
往里 append 原生 dict 会当场抛 TypeError —— 那是 0.7.3 的写法,
|
|
427
|
+
现在要加 OpenAI 格式的消息请用 `add_messages([...])` 或 `set_raw_messages([...])`。
|
|
428
|
+
"""
|
|
429
|
+
return self.context_manager.get_messages()
|
|
430
|
+
|
|
431
|
+
@messages.setter
|
|
432
|
+
def messages(self, value) -> None:
|
|
433
|
+
self.context_manager.set_messages(value)
|
|
434
|
+
|
|
387
435
|
def get_messages(self) -> list:
|
|
388
436
|
"""
|
|
389
437
|
获取当前Agent的完整消息列表(包含 system 系统提示词)
|
|
390
|
-
|
|
438
|
+
|
|
439
|
+
返回的是**消息对象**(`Message` / `ToolCalls`),改了立刻生效。
|
|
440
|
+
要 OpenAI wire format 的 `list[dict]`,用 `get_raw_messages()`。
|
|
391
441
|
"""
|
|
392
442
|
return self.messages
|
|
393
443
|
|
|
444
|
+
def get_raw_messages(self) -> list[dict]:
|
|
445
|
+
"""
|
|
446
|
+
获取 OpenAI wire format 的消息列表(`list[dict]`)
|
|
447
|
+
|
|
448
|
+
喂 LLM、存盘、或者交给不认 tina 对象的老代码时用这个。
|
|
449
|
+
"""
|
|
450
|
+
return self.context_manager.get_raw_messages()
|
|
451
|
+
|
|
452
|
+
def set_raw_messages(self, messages: list[dict]) -> None:
|
|
453
|
+
"""
|
|
454
|
+
用 OpenAI wire format 的 `list[dict]` 替换整个上下文
|
|
455
|
+
"""
|
|
456
|
+
self.context_manager.set_raw_messages(messages)
|
|
457
|
+
|
|
394
458
|
def get_conversation(self) -> list:
|
|
395
459
|
"""
|
|
396
460
|
获取当前Agent的对话消息列表(不包含 system 系统提示词)
|
|
461
|
+
|
|
462
|
+
返回的是**消息对象**;要 wire format 用 `get_raw_conversation()`。
|
|
397
463
|
"""
|
|
398
464
|
return self.context_manager.get_conversation()
|
|
399
465
|
|
|
466
|
+
def get_raw_conversation(self) -> list[dict]:
|
|
467
|
+
"""
|
|
468
|
+
获取不含 system 的 OpenAI wire format 对话列表(`list[dict]`)
|
|
469
|
+
|
|
470
|
+
存盘 / 导出 / 交给老代码时用这个。
|
|
471
|
+
"""
|
|
472
|
+
return self.context_manager.get_raw_conversation()
|
|
473
|
+
|
|
474
|
+
def copy_conversation(self) -> list:
|
|
475
|
+
"""获取对话消息对象的深拷贝(不含 system),改它不影响上下文"""
|
|
476
|
+
return self.context_manager.copy_conversation()
|
|
477
|
+
|
|
400
478
|
def set_context_manager(self, context_manager: ContextManager) -> None:
|
|
401
479
|
"""
|
|
402
|
-
|
|
480
|
+
替换上下文管理器,并同步给运行时
|
|
481
|
+
|
|
482
|
+
`messages` 是 property,会自动跟着新的 context_manager 走,不需要再手动同步。
|
|
403
483
|
|
|
404
484
|
Args:
|
|
405
485
|
context_manager: 新的上下文管理器
|
|
406
486
|
"""
|
|
407
487
|
self.context_manager = context_manager
|
|
408
488
|
self.runtime.context_manager = context_manager
|
|
409
|
-
self.messages = context_manager.get_messages()
|
|
410
489
|
|
|
411
490
|
def clear_messages(self) -> None:
|
|
412
491
|
"""
|
|
@@ -500,7 +579,7 @@ class Agent:
|
|
|
500
579
|
Args:
|
|
501
580
|
messages:消息列表,可以一次性添加多个消息,格式为[{"role": "user", "content": "你好,我是用户"}]
|
|
502
581
|
"""
|
|
503
|
-
self.
|
|
582
|
+
self.context_manager.add_messages(messages)
|
|
504
583
|
|
|
505
584
|
def get_tools_call_result(self) -> list:
|
|
506
585
|
"""
|