txcode-sdk 0.1.0__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.
Files changed (88) hide show
  1. txcode_sdk-0.1.0/.gitignore +27 -0
  2. txcode_sdk-0.1.0/LICENSE +21 -0
  3. txcode_sdk-0.1.0/PKG-INFO +58 -0
  4. txcode_sdk-0.1.0/README.md +37 -0
  5. txcode_sdk-0.1.0/docs/api/agent.html +84 -0
  6. txcode_sdk-0.1.0/docs/api/client.html +147 -0
  7. txcode_sdk-0.1.0/docs/api/session.html +126 -0
  8. txcode_sdk-0.1.0/docs/api/tools.html +139 -0
  9. txcode_sdk-0.1.0/docs/assets/style.css +198 -0
  10. txcode_sdk-0.1.0/docs/example/hello-world.html +104 -0
  11. txcode_sdk-0.1.0/docs/example/tool-calling.html +158 -0
  12. txcode_sdk-0.1.0/docs/guide/configuration.html +118 -0
  13. txcode_sdk-0.1.0/docs/guide/installation.html +98 -0
  14. txcode_sdk-0.1.0/docs/guide/quickstart.html +99 -0
  15. txcode_sdk-0.1.0/docs/index.html +92 -0
  16. txcode_sdk-0.1.0/pyproject.toml +35 -0
  17. txcode_sdk-0.1.0/src/example/README.md +27 -0
  18. txcode_sdk-0.1.0/src/example/__init__.py +1 -0
  19. txcode_sdk-0.1.0/src/example/code_example.py +96 -0
  20. txcode_sdk-0.1.0/src/example/code_session.py +80 -0
  21. txcode_sdk-0.1.0/src/example/code_tool.py +110 -0
  22. txcode_sdk-0.1.0/src/example/constants.py +6 -0
  23. txcode_sdk-0.1.0/src/example/query_session.py +68 -0
  24. txcode_sdk-0.1.0/src/txcode_sdk/__init__.py +49 -0
  25. txcode_sdk-0.1.0/src/txcode_sdk/agent/__init__.py +17 -0
  26. txcode_sdk-0.1.0/src/txcode_sdk/agent/base_agent.py +475 -0
  27. txcode_sdk-0.1.0/src/txcode_sdk/agent/chat/__init__.py +4 -0
  28. txcode_sdk-0.1.0/src/txcode_sdk/agent/chat/agent_tool.py +1 -0
  29. txcode_sdk-0.1.0/src/txcode_sdk/agent/chat/chat_agent.py +26 -0
  30. txcode_sdk-0.1.0/src/txcode_sdk/agent/chat/prompts/role.txt +31 -0
  31. txcode_sdk-0.1.0/src/txcode_sdk/agent/code/__init__.py +4 -0
  32. txcode_sdk-0.1.0/src/txcode_sdk/agent/code/agent_tool.py +1 -0
  33. txcode_sdk-0.1.0/src/txcode_sdk/agent/code/code_agent.py +26 -0
  34. txcode_sdk-0.1.0/src/txcode_sdk/agent/code/prompts/role.txt +51 -0
  35. txcode_sdk-0.1.0/src/txcode_sdk/agent/common/__init__.py +3 -0
  36. txcode_sdk-0.1.0/src/txcode_sdk/agent/common/common_agent.py +23 -0
  37. txcode_sdk-0.1.0/src/txcode_sdk/agent/common/prompt/role.txt +51 -0
  38. txcode_sdk-0.1.0/src/txcode_sdk/agent/mem/__init__.py +3 -0
  39. txcode_sdk-0.1.0/src/txcode_sdk/agent/mem/agent_tool.py +1 -0
  40. txcode_sdk-0.1.0/src/txcode_sdk/agent/mem/mem_agent.py +139 -0
  41. txcode_sdk-0.1.0/src/txcode_sdk/agent/mem/prompts/role.txt +36 -0
  42. txcode_sdk-0.1.0/src/txcode_sdk/agent/summarizer/__init__.py +3 -0
  43. txcode_sdk-0.1.0/src/txcode_sdk/agent/summarizer/prompts/role.txt +31 -0
  44. txcode_sdk-0.1.0/src/txcode_sdk/agent/summarizer/summarizer_agent.py +101 -0
  45. txcode_sdk-0.1.0/src/txcode_sdk/agent/task/__init__.py +4 -0
  46. txcode_sdk-0.1.0/src/txcode_sdk/agent/task/agent_tool.py +1 -0
  47. txcode_sdk-0.1.0/src/txcode_sdk/agent/task/prompts/role.txt +51 -0
  48. txcode_sdk-0.1.0/src/txcode_sdk/agent/task/task_agent.py +26 -0
  49. txcode_sdk-0.1.0/src/txcode_sdk/agent/tool_registry.py +119 -0
  50. txcode_sdk-0.1.0/src/txcode_sdk/client.py +215 -0
  51. txcode_sdk-0.1.0/src/txcode_sdk/compact/__init__.py +3 -0
  52. txcode_sdk-0.1.0/src/txcode_sdk/compact/compressor.py +74 -0
  53. txcode_sdk-0.1.0/src/txcode_sdk/hooks/__init__.py +19 -0
  54. txcode_sdk-0.1.0/src/txcode_sdk/hooks/handlers/__init__.py +4 -0
  55. txcode_sdk-0.1.0/src/txcode_sdk/hooks/handlers/base.py +19 -0
  56. txcode_sdk-0.1.0/src/txcode_sdk/hooks/handlers/memory.py +30 -0
  57. txcode_sdk-0.1.0/src/txcode_sdk/hooks/processor.py +19 -0
  58. txcode_sdk-0.1.0/src/txcode_sdk/hooks/queue.py +29 -0
  59. txcode_sdk-0.1.0/src/txcode_sdk/hooks/types.py +10 -0
  60. txcode_sdk-0.1.0/src/txcode_sdk/provider/__init__.py +6 -0
  61. txcode_sdk-0.1.0/src/txcode_sdk/provider/base.py +34 -0
  62. txcode_sdk-0.1.0/src/txcode_sdk/provider/deepseek_provider.py +60 -0
  63. txcode_sdk-0.1.0/src/txcode_sdk/provider/openai_provider.py +209 -0
  64. txcode_sdk-0.1.0/src/txcode_sdk/provider/router.py +13 -0
  65. txcode_sdk-0.1.0/src/txcode_sdk/session/__init__.py +3 -0
  66. txcode_sdk-0.1.0/src/txcode_sdk/session/manager.py +316 -0
  67. txcode_sdk-0.1.0/src/txcode_sdk/skills/__init__.py +43 -0
  68. txcode_sdk-0.1.0/src/txcode_sdk/specs/__init__.py +132 -0
  69. txcode_sdk-0.1.0/src/txcode_sdk/tools/__init__.py +3 -0
  70. txcode_sdk-0.1.0/src/txcode_sdk/tools/bash.txt +47 -0
  71. txcode_sdk-0.1.0/src/txcode_sdk/tools/bash_tool.py +79 -0
  72. txcode_sdk-0.1.0/src/txcode_sdk/tools/edit_file.py +141 -0
  73. txcode_sdk-0.1.0/src/txcode_sdk/tools/edit_file.txt +13 -0
  74. txcode_sdk-0.1.0/src/txcode_sdk/tools/glob.txt +12 -0
  75. txcode_sdk-0.1.0/src/txcode_sdk/tools/glob_tool.py +67 -0
  76. txcode_sdk-0.1.0/src/txcode_sdk/tools/grep.txt +14 -0
  77. txcode_sdk-0.1.0/src/txcode_sdk/tools/grep_tool.py +76 -0
  78. txcode_sdk-0.1.0/src/txcode_sdk/tools/memory_tool.py +78 -0
  79. txcode_sdk-0.1.0/src/txcode_sdk/tools/read_file.py +75 -0
  80. txcode_sdk-0.1.0/src/txcode_sdk/tools/read_file.txt +17 -0
  81. txcode_sdk-0.1.0/src/txcode_sdk/tools/write_file.py +43 -0
  82. txcode_sdk-0.1.0/src/txcode_sdk/tools/write_file.txt +11 -0
  83. txcode_sdk-0.1.0/src/txcode_sdk/types.py +158 -0
  84. txcode_sdk-0.1.0/tests/__init__.py +1 -0
  85. txcode_sdk-0.1.0/tests/test_client.py +37 -0
  86. txcode_sdk-0.1.0/tests/test_session.py +52 -0
  87. txcode_sdk-0.1.0/tests/test_session_query.py +358 -0
  88. txcode_sdk-0.1.0/tests/test_tools.py +322 -0
@@ -0,0 +1,27 @@
1
+ # txcode runtime
2
+ .txcode/
3
+
4
+ # Python
5
+ __pycache__/
6
+ *.py[cod]
7
+ *.egg-info/
8
+ dist/
9
+ build/
10
+
11
+ # Virtual environment
12
+ .venv/
13
+ venv/
14
+
15
+ # Tests
16
+ .pytest_cache/
17
+
18
+ # Environment
19
+ .env
20
+
21
+ # IDE
22
+ .idea/
23
+ .vscode/
24
+
25
+ # OS
26
+ Thumbs.db
27
+ .DS_Store
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 homecommunity
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,58 @@
1
+ Metadata-Version: 2.4
2
+ Name: txcode-sdk
3
+ Version: 0.1.0
4
+ Summary: AI Agent SDK with built-in tools for Python projects
5
+ Project-URL: Repository, https://github.com/homecommunity/txcode-sdk
6
+ Author: homecommunity
7
+ License: MIT
8
+ License-File: LICENSE
9
+ Keywords: agent,ai,openai,react,tools
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Requires-Python: >=3.10
18
+ Requires-Dist: openai>=1.0.0
19
+ Requires-Dist: tiktoken>=0.5.0
20
+ Description-Content-Type: text/markdown
21
+
22
+ # txcode-sdk
23
+
24
+ AI Agent SDK with built-in tools for Python projects.
25
+
26
+ ## Installation
27
+
28
+ ```bash
29
+ pip install txcode-sdk
30
+ ```
31
+
32
+ ## Quick Start
33
+
34
+ ```python
35
+ import asyncio
36
+ from txcode_sdk import TxCodeClient
37
+
38
+ async def main():
39
+ client = TxCodeClient(
40
+ api_key="sk-xxx",
41
+ base_url="https://api.openai.com/v1",
42
+ model="gpt-4",
43
+ )
44
+ result = await client.chat("Hello!")
45
+ print(result.answer)
46
+
47
+ asyncio.run(main())
48
+ ```
49
+
50
+ ## Features
51
+
52
+ - ReAct Agent loop with tool calling
53
+ - Multiple agent types: code, chat, common, task
54
+ - 6 built-in tools: read_file, write_file, edit_file, glob, grep, bash
55
+ - Session management with JSON file persistence
56
+ - Context compression for long conversations
57
+ - Multimodal input support (images)
58
+ - Extensible custom tools
@@ -0,0 +1,37 @@
1
+ # txcode-sdk
2
+
3
+ AI Agent SDK with built-in tools for Python projects.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install txcode-sdk
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```python
14
+ import asyncio
15
+ from txcode_sdk import TxCodeClient
16
+
17
+ async def main():
18
+ client = TxCodeClient(
19
+ api_key="sk-xxx",
20
+ base_url="https://api.openai.com/v1",
21
+ model="gpt-4",
22
+ )
23
+ result = await client.chat("Hello!")
24
+ print(result.answer)
25
+
26
+ asyncio.run(main())
27
+ ```
28
+
29
+ ## Features
30
+
31
+ - ReAct Agent loop with tool calling
32
+ - Multiple agent types: code, chat, common, task
33
+ - 6 built-in tools: read_file, write_file, edit_file, glob, grep, bash
34
+ - Session management with JSON file persistence
35
+ - Context compression for long conversations
36
+ - Multimodal input support (images)
37
+ - Extensible custom tools
@@ -0,0 +1,84 @@
1
+ <!DOCTYPE html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Agent 类型 - txcode-sdk 文档</title>
7
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
8
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
9
+ <link rel="stylesheet" href="../assets/style.css">
10
+ </head>
11
+ <body>
12
+ <button class="menu-toggle" onclick="toggleSidebar()">&#9776;</button>
13
+ <nav id="sidebar">
14
+ <div class="sidebar-header">
15
+ <h1><a href="../index.html">txcode-sdk</a></h1>
16
+ <span class="version">v0.1.0</span>
17
+ </div>
18
+ <div class="search-box">
19
+ <input type="text" id="search" placeholder="搜索文档..." oninput="onSearch(this.value)">
20
+ </div>
21
+ <div class="menu-group">
22
+ <div class="menu-group-header">指南</div>
23
+ <div class="menu-group-children">
24
+ <a href="../guide/quickstart.html">快速开始</a>
25
+ <a href="../guide/installation.html">安装说明</a>
26
+ <a href="../guide/configuration.html">配置指南</a>
27
+ </div>
28
+ </div>
29
+ <div class="menu-group">
30
+ <div class="menu-group-header">API 参考</div>
31
+ <div class="menu-group-children">
32
+ <a href="../api/client.html">TxCodeClient</a>
33
+ <a href="../api/agent.html" class="active">Agent 类型</a>
34
+ <a href="../api/tools.html">内置工具</a>
35
+ <a href="../api/session.html">会话管理</a>
36
+ </div>
37
+ </div>
38
+ <div class="menu-group">
39
+ <div class="menu-group-header">示例</div>
40
+ <div class="menu-group-children">
41
+ <a href="../example/hello-world.html">Hello World</a>
42
+ <a href="../example/tool-calling.html">工具调用</a>
43
+ </div>
44
+ </div>
45
+ </nav>
46
+ <main id="content">
47
+ <h1>Agent 类型</h1>
48
+ <p>txcode-sdk 提供 4 种 Agent 类型,每种有不同的角色定位和工具集。</p>
49
+ <h2>概述</h2>
50
+ <table>
51
+ <tr><th>Agent</th><th>类型名</th><th>角色定位</th><th>适用场景</th></tr>
52
+ <tr><td>CodeAgent</td><td><code>"code"</code></td><td>代码生成与修改专家</td><td>软件开发任务</td></tr>
53
+ <tr><td>ChatAgent</td><td><code>"chat"</code></td><td>对话交流助手</td><td>问答、咨询</td></tr>
54
+ <tr><td>CommonAgent</td><td><code>"common"</code></td><td>通用任务执行者</td><td>均衡的代码+对话</td></tr>
55
+ <tr><td>TaskAgent</td><td><code>"task"</code></td><td>任务规划与分解</td><td>复杂多步骤任务</td></tr>
56
+ </table>
57
+ <h2>CodeAgent (<code>"code"</code>)</h2>
58
+ <p><strong>默认 Agent</strong>,专注于软件工程任务。工具集:read_file, write_file, edit_file, glob, grep, bash, memory_tool</p>
59
+ <pre><code class="language-python">client = TxCodeClient(api_key="sk-xxx", agent_type="code")</code></pre>
60
+ <h2>ChatAgent (<code>"chat"</code>)</h2>
61
+ <p>轻量级对话 Agent。工具集:read_file, write_file, glob, grep(精简集)</p>
62
+ <pre><code class="language-python">result = await client.chat("Python 中的 asyncio 怎么用?", agent_type="chat")</code></pre>
63
+ <h2>CommonAgent (<code>"common"</code>)</h2>
64
+ <p>通用 Agent,平衡对话与工具使用。</p>
65
+ <pre><code class="language-python">result = await client.chat("分析项目结构并给出建议", agent_type="common")</code></pre>
66
+ <h2>TaskAgent (<code>"task"</code>)</h2>
67
+ <p>任务规划型 Agent,适合复杂多步骤任务。</p>
68
+ <pre><code class="language-python">result = await client.chat("重构整个项目的错误处理逻辑", agent_type="task")</code></pre>
69
+ <h2>Agent 类型选择指南</h2>
70
+ <pre><code>需要写代码 / 修改文件?
71
+ ├── 是 → 需要复杂规划?
72
+ │ ├── 是 → task
73
+ │ └── 否 → code
74
+ └── 否 → 只是聊天问答?
75
+ ├── 是 → chat
76
+ └── 否(通用任务)→ common</code></pre>
77
+ </main>
78
+ <script>
79
+ function toggleSidebar(){document.getElementById('sidebar').classList.toggle('open')}
80
+ function onSearch(v){var q=v.toLowerCase().trim();document.querySelectorAll('#sidebar .menu-group-children a').forEach(function(a){a.style.display=(!q||a.textContent.toLowerCase().indexOf(q)>=0)?'':'none'})}
81
+ hljs.highlightAll();
82
+ </script>
83
+ </body>
84
+ </html>
@@ -0,0 +1,147 @@
1
+ <!DOCTYPE html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>TxCodeClient API - txcode-sdk 文档</title>
7
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
8
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
9
+ <link rel="stylesheet" href="../assets/style.css">
10
+ </head>
11
+ <body>
12
+ <button class="menu-toggle" onclick="toggleSidebar()">&#9776;</button>
13
+ <nav id="sidebar">
14
+ <div class="sidebar-header">
15
+ <h1><a href="../index.html">txcode-sdk</a></h1>
16
+ <span class="version">v0.1.0</span>
17
+ </div>
18
+ <div class="search-box">
19
+ <input type="text" id="search" placeholder="搜索文档..." oninput="onSearch(this.value)">
20
+ </div>
21
+ <div class="menu-group">
22
+ <div class="menu-group-header">指南</div>
23
+ <div class="menu-group-children">
24
+ <a href="../guide/quickstart.html">快速开始</a>
25
+ <a href="../guide/installation.html">安装说明</a>
26
+ <a href="../guide/configuration.html">配置指南</a>
27
+ </div>
28
+ </div>
29
+ <div class="menu-group">
30
+ <div class="menu-group-header">API 参考</div>
31
+ <div class="menu-group-children">
32
+ <a href="../api/client.html" class="active">TxCodeClient</a>
33
+ <a href="../api/agent.html">Agent 类型</a>
34
+ <a href="../api/tools.html">内置工具</a>
35
+ <a href="../api/session.html">会话管理</a>
36
+ </div>
37
+ </div>
38
+ <div class="menu-group">
39
+ <div class="menu-group-header">示例</div>
40
+ <div class="menu-group-children">
41
+ <a href="../example/hello-world.html">Hello World</a>
42
+ <a href="../example/tool-calling.html">工具调用</a>
43
+ </div>
44
+ </div>
45
+ </nav>
46
+ <main id="content">
47
+ <h1>TxCodeClient API</h1>
48
+ <p>TxCodeClient 是 txcode-sdk 的唯一入口类,封装了 Agent 管理、会话管理和上下文压缩。</p>
49
+ <h2>导入</h2>
50
+ <pre><code class="language-python">from txcode_sdk import TxCodeClient</code></pre>
51
+ <h2>构造函数</h2>
52
+ <pre><code class="language-python">TxCodeClient(
53
+ api_key: str,
54
+ base_url: str = "https://api.openai.com/v1",
55
+ model: str = "gpt-4",
56
+ agent_type: AgentType = "code",
57
+ tools: list[Tool] | None = None,
58
+ max_context_tokens: int = 150_000,
59
+ max_iterations: int = 50,
60
+ system_prompt: str | None = None,
61
+ work_dir: str | None = None,
62
+ log_enabled: bool = False,
63
+ )</code></pre>
64
+ <p>详见 <a href="../guide/configuration.html">配置指南</a>。</p>
65
+ <h2>chat()</h2>
66
+ <p>发送消息给 Agent,执行 ReAct 循环。</p>
67
+ <pre><code class="language-python">async def chat(
68
+ self,
69
+ message: str,
70
+ session_id: str | None = None,
71
+ agent_type: AgentType | None = None,
72
+ history: list[dict] | None = None,
73
+ media_files: list[MediaFile] | None = None,
74
+ on_step: Callable[[StepInfo], None] | None = None,
75
+ on_compact: Callable[[dict], None] | None = None,
76
+ abort_signal: Any = None,
77
+ ) -&gt; ChatResult:</code></pre>
78
+ <p><strong>返回值 ChatResult</strong>:</p>
79
+ <table>
80
+ <tr><th>字段</th><th>类型</th><th>说明</th></tr>
81
+ <tr><td><code>answer</code></td><td><code>str</code></td><td>Agent 最终回答</td></tr>
82
+ <tr><td><code>steps</code></td><td><code>list[StepInfo]</code></td><td>所有工具调用步骤</td></tr>
83
+ <tr><td><code>session_id</code></td><td><code>str</code></td><td>会话 ID</td></tr>
84
+ <tr><td><code>iterations</code></td><td><code>int</code></td><td>实际迭代次数</td></tr>
85
+ <tr><td><code>success</code></td><td><code>bool</code></td><td>是否成功</td></tr>
86
+ <tr><td><code>error</code></td><td><code>str | None</code></td><td>错误信息</td></tr>
87
+ <tr><td><code>usage</code></td><td><code>TokenUsage | None</code></td><td>Token 用量</td></tr>
88
+ </table>
89
+ <h3>续接会话</h3>
90
+ <pre><code class="language-python">result1 = await client.chat("我叫小明")
91
+ sid = result1.session_id
92
+ result2 = await client.chat("我叫什么名字?", session_id=sid)
93
+ print(result2.answer) # "小明"</code></pre>
94
+ <h3>on_step 回调</h3>
95
+ <pre><code class="language-python">from txcode_sdk.types import StepInfo
96
+
97
+ def on_step(step: StepInfo):
98
+ print(f"第 {step.iteration} 步")
99
+ for i, tc in enumerate(step.tool_calls):
100
+ res = step.results[i] if i &lt; len(step.results) else None
101
+ status = "&#x2713;" if res and res.success else "&#x2717;"
102
+ print(f" [{status}] {tc.name}: {tc.arguments}")
103
+
104
+ result = await client.chat("列出所有 .py 文件", on_step=on_step)</code></pre>
105
+ <h3>abort_signal</h3>
106
+ <pre><code class="language-python">class AbortSignal:
107
+ aborted = False
108
+ signal = AbortSignal()
109
+ task = asyncio.create_task(client.chat("复杂任务", abort_signal=signal))</code></pre>
110
+ <h2>会话管理</h2>
111
+ <h3>list_sessions()</h3>
112
+ <pre><code class="language-python">for s in client.list_sessions():
113
+ print(s["print_summary"])</code></pre>
114
+ <h3>get_session()</h3>
115
+ <pre><code class="language-python">session = client.get_session(session_id)</code></pre>
116
+ <h3>get_session_messages()</h3>
117
+ <pre><code class="language-python">data = client.get_session_messages(session_id)
118
+ for msg in data["messages"]:
119
+ print(msg.get("printMsg") or msg["content"][:200])</code></pre>
120
+ <h3>delete_session()</h3>
121
+ <pre><code class="language-python">client.delete_session(session_id)</code></pre>
122
+ <h2>完整示例</h2>
123
+ <pre><code class="language-python">import asyncio
124
+ from txcode_sdk import TxCodeClient
125
+ from txcode_sdk.types import StepInfo
126
+
127
+ async def main():
128
+ client = TxCodeClient(
129
+ api_key="sk-xxx",
130
+ base_url="https://api.deepseek.com/v1",
131
+ model="deepseek-chat",
132
+ max_iterations=10,
133
+ )
134
+ def on_step(step: StepInfo):
135
+ if step.printMsg: print(step.printMsg)
136
+ result = await client.chat("在 src/ 下找到所有 Python 文件", on_step=on_step)
137
+ print(result.answer)
138
+
139
+ asyncio.run(main())</code></pre>
140
+ </main>
141
+ <script>
142
+ function toggleSidebar(){document.getElementById('sidebar').classList.toggle('open')}
143
+ function onSearch(v){var q=v.toLowerCase().trim();document.querySelectorAll('#sidebar .menu-group-children a').forEach(function(a){a.style.display=(!q||a.textContent.toLowerCase().indexOf(q)>=0)?'':'none'})}
144
+ hljs.highlightAll();
145
+ </script>
146
+ </body>
147
+ </html>
@@ -0,0 +1,126 @@
1
+ <!DOCTYPE html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>会话管理 - txcode-sdk 文档</title>
7
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
8
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
9
+ <link rel="stylesheet" href="../assets/style.css">
10
+ </head>
11
+ <body>
12
+ <button class="menu-toggle" onclick="toggleSidebar()">&#9776;</button>
13
+ <nav id="sidebar">
14
+ <div class="sidebar-header">
15
+ <h1><a href="../index.html">txcode-sdk</a></h1>
16
+ <span class="version">v0.1.0</span>
17
+ </div>
18
+ <div class="search-box">
19
+ <input type="text" id="search" placeholder="搜索文档..." oninput="onSearch(this.value)">
20
+ </div>
21
+ <div class="menu-group">
22
+ <div class="menu-group-header">指南</div>
23
+ <div class="menu-group-children">
24
+ <a href="../guide/quickstart.html">快速开始</a>
25
+ <a href="../guide/installation.html">安装说明</a>
26
+ <a href="../guide/configuration.html">配置指南</a>
27
+ </div>
28
+ </div>
29
+ <div class="menu-group">
30
+ <div class="menu-group-header">API 参考</div>
31
+ <div class="menu-group-children">
32
+ <a href="../api/client.html">TxCodeClient</a>
33
+ <a href="../api/agent.html">Agent 类型</a>
34
+ <a href="../api/tools.html">内置工具</a>
35
+ <a href="../api/session.html" class="active">会话管理</a>
36
+ </div>
37
+ </div>
38
+ <div class="menu-group">
39
+ <div class="menu-group-header">示例</div>
40
+ <div class="menu-group-children">
41
+ <a href="../example/hello-world.html">Hello World</a>
42
+ <a href="../example/tool-calling.html">工具调用</a>
43
+ </div>
44
+ </div>
45
+ </nav>
46
+ <main id="content">
47
+ <h1>会话管理</h1>
48
+ <p>txcode-sdk 自动将对话历史持久化为 JSON 文件,存储在 <code>{work_dir}/.txcode/session/</code> 目录下。</p>
49
+ <h2>会话存储结构</h2>
50
+ <pre><code>.txcode/session/
51
+ └── session_{session_id}.json</code></pre>
52
+ <p>每个会话文件的结构:</p>
53
+ <pre><code class="language-json">{
54
+ "id": "a3ced874e170...",
55
+ "title": "用户第一条消息的前50字符",
56
+ "created_at": "2026-06-15T10:00:00+00:00",
57
+ "updated_at": "2026-06-15T10:05:00+00:00",
58
+ "prompt_tokens": 1500,
59
+ "completion_tokens": 800,
60
+ "summary_message_index": null,
61
+ "messages": [
62
+ { "index": 0, "role": "user", "content": "帮我创建 hello.py", "keep_context": true, "is_original": true },
63
+ { "index": 1, "role": "assistant", "content": "{\"type\":\"assistant_with_tools\", ...}", "keep_context": true },
64
+ { "index": 2, "role": "tool", "content": "文件已创建", "tool_call_id": "call_xxx", "keep_context": false }
65
+ ]
66
+ }</code></pre>
67
+ <h2>API 参考</h2>
68
+ <h3>list_sessions()</h3>
69
+ <pre><code class="language-python">sessions = client.list_sessions()
70
+ for s in sessions:
71
+ print(s["print_summary"])
72
+ # 输出: [2026-06-15] 帮我创建 hello.py (12 messages)</code></pre>
73
+ <p>扫描 <code>.txcode/session/session_*.json</code>,提取第一条 user 消息作为标题。</p>
74
+ <h3>get_session()</h3>
75
+ <pre><code class="language-python">session = client.get_session("a3ced874e170")
76
+ if session:
77
+ for msg in session["messages"]:
78
+ print(f" [{msg['role']}] {msg['content'][:80]}...")</code></pre>
79
+ <h3>get_session_messages()</h3>
80
+ <p>获取带 <code>printMsg</code> 字段的消息,用于友好展示。</p>
81
+ <pre><code class="language-python">data = client.get_session_messages(session_id)
82
+ for msg in data["messages"]:
83
+ pm = msg.get("printMsg")
84
+ if pm:
85
+ print(pm)</code></pre>
86
+ <p><strong>printMsg 生成逻辑</strong>:</p>
87
+ <ul>
88
+ <li>两遍扫描:先收集 tool_call_id→is_success 映射,再为 assistant 标记 &#x2713;/&#x2717;/?</li>
89
+ <li>user 消息:截断 200 字符,前缀 "User: "</li>
90
+ <li>assistant 含工具调用:格式 <code>{tool:'&#x2713; tool_name args', reason:'推理'}</code></li>
91
+ <li>tool 消息:不生成 printMsg</li>
92
+ <li>判据:content 不以 "Error:" 开头为成功</li>
93
+ </ul>
94
+ <h3>delete_session()</h3>
95
+ <pre><code class="language-python">client.delete_session(session_id)</code></pre>
96
+ <h3>chat() 中的会话续接</h3>
97
+ <pre><code class="language-python">result1 = await client.chat("我叫小明")
98
+ sid = result1.session_id
99
+ result2 = await client.chat("我叫什么?", session_id=sid)
100
+ print(result2.answer) # "你叫小明"</code></pre>
101
+ <h2>相关类型</h2>
102
+ <h3>StepInfo</h3>
103
+ <pre><code class="language-python">@dataclass
104
+ class StepInfo:
105
+ reasoning: str | None # AI 推理内容
106
+ tool_calls: list[ToolCallInfo] # 工具调用列表
107
+ results: list[ToolResultInfo] # 工具执行结果
108
+ iteration: int # 当前迭代编号
109
+ usage: TokenUsage | None # Token 用量
110
+ printMsg: str | None # 可打印消息
111
+
112
+ @dataclass
113
+ class ToolCallInfo:
114
+ id: str; name: str; arguments: dict
115
+
116
+ @dataclass
117
+ class ToolResultInfo:
118
+ name: str; success: bool; output: str | None; error: str | None</code></pre>
119
+ </main>
120
+ <script>
121
+ function toggleSidebar(){document.getElementById('sidebar').classList.toggle('open')}
122
+ function onSearch(v){var q=v.toLowerCase().trim();document.querySelectorAll('#sidebar .menu-group-children a').forEach(function(a){a.style.display=(!q||a.textContent.toLowerCase().indexOf(q)>=0)?'':'none'})}
123
+ hljs.highlightAll();
124
+ </script>
125
+ </body>
126
+ </html>
@@ -0,0 +1,139 @@
1
+ <!DOCTYPE html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>内置工具 - txcode-sdk 文档</title>
7
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
8
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
9
+ <link rel="stylesheet" href="../assets/style.css">
10
+ </head>
11
+ <body>
12
+ <button class="menu-toggle" onclick="toggleSidebar()">&#9776;</button>
13
+ <nav id="sidebar">
14
+ <div class="sidebar-header">
15
+ <h1><a href="../index.html">txcode-sdk</a></h1>
16
+ <span class="version">v0.1.0</span>
17
+ </div>
18
+ <div class="search-box">
19
+ <input type="text" id="search" placeholder="搜索文档..." oninput="onSearch(this.value)">
20
+ </div>
21
+ <div class="menu-group">
22
+ <div class="menu-group-header">指南</div>
23
+ <div class="menu-group-children">
24
+ <a href="../guide/quickstart.html">快速开始</a>
25
+ <a href="../guide/installation.html">安装说明</a>
26
+ <a href="../guide/configuration.html">配置指南</a>
27
+ </div>
28
+ </div>
29
+ <div class="menu-group">
30
+ <div class="menu-group-header">API 参考</div>
31
+ <div class="menu-group-children">
32
+ <a href="../api/client.html">TxCodeClient</a>
33
+ <a href="../api/agent.html">Agent 类型</a>
34
+ <a href="../api/tools.html" class="active">内置工具</a>
35
+ <a href="../api/session.html">会话管理</a>
36
+ </div>
37
+ </div>
38
+ <div class="menu-group">
39
+ <div class="menu-group-header">示例</div>
40
+ <div class="menu-group-children">
41
+ <a href="../example/hello-world.html">Hello World</a>
42
+ <a href="../example/tool-calling.html">工具调用</a>
43
+ </div>
44
+ </div>
45
+ </nav>
46
+ <main id="content">
47
+ <h1>内置工具</h1>
48
+ <p>txcode-sdk 提供 7 个内置工具,覆盖文件操作、搜索和命令执行。</p>
49
+ <h2>工具列表</h2>
50
+ <table>
51
+ <tr><th>工具</th><th>用途</th><th>分类</th></tr>
52
+ <tr><td>read_file</td><td>读取文件或目录内容</td><td>文件操作</td></tr>
53
+ <tr><td>write_file</td><td>写入新文件</td><td>文件操作</td></tr>
54
+ <tr><td>edit_file</td><td>精确字符串替换编辑</td><td>文件操作</td></tr>
55
+ <tr><td>glob</td><td>文件名模式匹配</td><td>搜索</td></tr>
56
+ <tr><td>grep</td><td>正则表达式内容搜索</td><td>搜索</td></tr>
57
+ <tr><td>bash</td><td>执行 shell 命令</td><td>命令执行</td></tr>
58
+ <tr><td>memory_tool</td><td>记忆管理(内部使用)</td><td>内部</td></tr>
59
+ </table>
60
+ <hr>
61
+ <h2>read_file</h2>
62
+ <table>
63
+ <tr><th>参数</th><th>类型</th><th>必填</th><th>说明</th></tr>
64
+ <tr><td><code>file_path</code></td><td><code>str</code></td><td>是</td><td>文件或目录绝对路径</td></tr>
65
+ <tr><td><code>offset</code></td><td><code>int</code></td><td>否</td><td>起始行号,默认 1</td></tr>
66
+ <tr><td><code>limit</code></td><td><code>int</code></td><td>否</td><td>最大读取行数,默认 2000</td></tr>
67
+ </table>
68
+ <p>返回内容每行带行号前缀,支持读取图片和 PDF 作为附件。</p>
69
+ <hr>
70
+ <h2>write_file</h2>
71
+ <table>
72
+ <tr><th>参数</th><th>类型</th><th>必填</th><th>说明</th></tr>
73
+ <tr><td><code>file_path</code></td><td><code>str</code></td><td>是</td><td>文件绝对路径</td></tr>
74
+ <tr><td><code>content</code></td><td><code>str</code></td><td>是</td><td>要写入的内容</td></tr>
75
+ </table>
76
+ <p>已存在的文件必须用 edit_file 修改。</p>
77
+ <hr>
78
+ <h2>edit_file</h2>
79
+ <table>
80
+ <tr><th>参数</th><th>类型</th><th>必填</th><th>说明</th></tr>
81
+ <tr><td><code>file_path</code></td><td><code>str</code></td><td>是</td><td>文件绝对路径</td></tr>
82
+ <tr><td><code>old_string</code></td><td><code>str</code></td><td>是</td><td>要被替换的内容</td></tr>
83
+ <tr><td><code>new_string</code></td><td><code>str</code></td><td>是</td><td>替换后的新内容</td></tr>
84
+ <tr><td><code>replace_all</code></td><td><code>bool</code></td><td>否</td><td>替换所有匹配项,默认 false</td></tr>
85
+ </table>
86
+ <p>编辑前必须先 read_file。old_string 找不到或出现多次会失败。</p>
87
+ <hr>
88
+ <h2>glob</h2>
89
+ <table>
90
+ <tr><th>参数</th><th>类型</th><th>必填</th><th>说明</th></tr>
91
+ <tr><td><code>pattern</code></td><td><code>str</code></td><td>是</td><td>glob 模式,如 <code>**/*.py</code></td></tr>
92
+ <tr><td><code>directory</code></td><td><code>str</code></td><td>否</td><td>搜索目录</td></tr>
93
+ </table>
94
+ <hr>
95
+ <h2>grep</h2>
96
+ <table>
97
+ <tr><th>参数</th><th>类型</th><th>必填</th><th>说明</th></tr>
98
+ <tr><td><code>pattern</code></td><td><code>str</code></td><td>是</td><td>正则表达式</td></tr>
99
+ <tr><td><code>directory</code></td><td><code>str</code></td><td>否</td><td>搜索目录</td></tr>
100
+ <tr><td><code>include</code></td><td><code>str</code></td><td>否</td><td>文件过滤</td></tr>
101
+ </table>
102
+ <hr>
103
+ <h2>bash</h2>
104
+ <table>
105
+ <tr><th>参数</th><th>类型</th><th>必填</th><th>说明</th></tr>
106
+ <tr><td><code>command</code></td><td><code>str</code></td><td>是</td><td>要执行的命令</td></tr>
107
+ <tr><td><code>workdir</code></td><td><code>str</code></td><td>否</td><td>工作目录</td></tr>
108
+ <tr><td><code>timeout</code></td><td><code>int</code></td><td>否</td><td>超时毫秒,默认 120000</td></tr>
109
+ </table>
110
+ <p>仅用于终端操作,禁止用于文件操作。</p>
111
+ <hr>
112
+ <h2>创建自定义工具</h2>
113
+ <pre><code class="language-python">from txcode_sdk import Tool
114
+ from txcode_sdk.types import ToolContext, ToolResult
115
+
116
+ def my_execute(params: dict, ctx: ToolContext) -&gt; ToolResult:
117
+ name = params.get("name", "World")
118
+ return ToolResult(success=True, output=f"Hello, {name}!")
119
+
120
+ my_tool = Tool(
121
+ name="hello",
122
+ description="Print a greeting message",
123
+ parameters={
124
+ "type": "object",
125
+ "properties": {"name": {"type": "string", "description": "Name to greet"}},
126
+ "required": ["name"]
127
+ },
128
+ execute=my_execute,
129
+ )
130
+
131
+ client = TxCodeClient(api_key="sk-xxx", tools=[my_tool])</code></pre>
132
+ </main>
133
+ <script>
134
+ function toggleSidebar(){document.getElementById('sidebar').classList.toggle('open')}
135
+ function onSearch(v){var q=v.toLowerCase().trim();document.querySelectorAll('#sidebar .menu-group-children a').forEach(function(a){a.style.display=(!q||a.textContent.toLowerCase().indexOf(q)>=0)?'':'none'})}
136
+ hljs.highlightAll();
137
+ </script>
138
+ </body>
139
+ </html>