tiny-cli-agent 0.1.0__py3-none-any.whl
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.
- tiny_agent/__init__.py +3 -0
- tiny_agent/__main__.py +5 -0
- tiny_agent/cli.py +2343 -0
- tiny_agent/evals.py +300 -0
- tiny_cli_agent-0.1.0.dist-info/METADATA +214 -0
- tiny_cli_agent-0.1.0.dist-info/RECORD +10 -0
- tiny_cli_agent-0.1.0.dist-info/WHEEL +5 -0
- tiny_cli_agent-0.1.0.dist-info/entry_points.txt +3 -0
- tiny_cli_agent-0.1.0.dist-info/licenses/LICENSE +21 -0
- tiny_cli_agent-0.1.0.dist-info/top_level.txt +1 -0
tiny_agent/evals.py
ADDED
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
"""
|
|
2
|
+
tiny-agent-eval —— 给 Agent 做回归评测
|
|
3
|
+
|
|
4
|
+
用法:
|
|
5
|
+
tiny-agent-eval --list # 列出任务
|
|
6
|
+
tiny-agent-eval # 每个任务跑 1 次
|
|
7
|
+
tiny-agent-eval --repeat 3 --out base.json # 每个任务跑 3 次(LLM 有随机性,看通过率才可靠)
|
|
8
|
+
tiny-agent-eval --only fix_bug # 只跑某个任务
|
|
9
|
+
TINY_AGENT_MODEL=另一个模型 tiny-agent-eval --out other.json # 对比不同模型
|
|
10
|
+
tiny-agent-eval --compare base.json other.json # 对比两次结果
|
|
11
|
+
|
|
12
|
+
原理:每次在全新的临时目录里放好"夹具文件",用 --yes 无人值守地运行 Agent,
|
|
13
|
+
再用确定性的代码检查结果。检查的是"结果对不对",而不是"过程像不像"。
|
|
14
|
+
|
|
15
|
+
注意:--yes 会不经确认地执行 Agent 想执行的任何命令,所以本工具只在临时目录里运行它。
|
|
16
|
+
评测会真实调用 API,会产生费用。
|
|
17
|
+
"""
|
|
18
|
+
import argparse
|
|
19
|
+
import ast
|
|
20
|
+
import json
|
|
21
|
+
import os
|
|
22
|
+
import re
|
|
23
|
+
import shutil
|
|
24
|
+
import subprocess
|
|
25
|
+
import sys
|
|
26
|
+
import tempfile
|
|
27
|
+
import time
|
|
28
|
+
|
|
29
|
+
PY = sys.executable
|
|
30
|
+
USAGE_RE = re.compile(r"累计 (\d+) 次请求,输入 (\d+),输出 (\d+)")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# ---------- 辅助 ----------
|
|
34
|
+
def read(d, rel):
|
|
35
|
+
with open(os.path.join(d, rel), encoding="utf-8") as f:
|
|
36
|
+
return f.read()
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def run_py(d, *args):
|
|
40
|
+
return subprocess.run([PY, *args], cwd=d, capture_output=True, text=True, timeout=30)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def unchanged(d, files, names):
|
|
44
|
+
"""names 里的文件内容是否与夹具完全一致(防止 Agent 偷改测试或无关文件)。"""
|
|
45
|
+
for n in names:
|
|
46
|
+
if not os.path.exists(os.path.join(d, n)) or read(d, n) != files[n]:
|
|
47
|
+
return False, f"{n} 被改动了"
|
|
48
|
+
return True, ""
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def final_answer(out):
|
|
52
|
+
"""取最后一条 [模型] 之后、[用量] 之前的文字,即 Agent 的最终回复。"""
|
|
53
|
+
return out.split("[模型]")[-1].split("[用量]")[0]
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
# ---------- 各任务的检查函数:返回 (是否通过, 说明) ----------
|
|
57
|
+
def check_fix_bug(d, out, files):
|
|
58
|
+
ok, why = unchanged(d, files, ["test_calc.py"])
|
|
59
|
+
if not ok:
|
|
60
|
+
return False, why
|
|
61
|
+
r = run_py(d, "test_calc.py")
|
|
62
|
+
return r.returncode == 0, "" if r.returncode == 0 else "测试仍然失败"
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def check_add_function(d, out, files):
|
|
66
|
+
code = "import utils; assert utils.cube(3)==27 and utils.cube(-2)==-8 and utils.square(4)==16"
|
|
67
|
+
r = run_py(d, "-c", code)
|
|
68
|
+
return r.returncode == 0, "" if r.returncode == 0 else (r.stderr.strip().splitlines() or ["校验失败"])[-1]
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def check_find_answer(d, out, files):
|
|
72
|
+
ok, why = unchanged(d, files, list(files)) # 只读任务:不许改任何文件
|
|
73
|
+
if not ok:
|
|
74
|
+
return False, why
|
|
75
|
+
ans = final_answer(out)
|
|
76
|
+
if "config_loader.py" in ans and "legacy_config.py" not in ans:
|
|
77
|
+
return True, ""
|
|
78
|
+
return False, f"回答不对:{ans.strip()[:60]}"
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def check_rename(d, out, files):
|
|
82
|
+
for name in os.listdir(d):
|
|
83
|
+
if name.endswith(".py") and "calc_total" in read(d, name):
|
|
84
|
+
return False, f"{name} 里仍有 calc_total"
|
|
85
|
+
r = run_py(d, "main.py")
|
|
86
|
+
if r.returncode != 0 or r.stdout.strip() != "6 total=9":
|
|
87
|
+
return False, f"main.py 输出不对:{r.stdout.strip()[:40]}{r.stderr.strip()[-60:]}"
|
|
88
|
+
return True, ""
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def check_memory(d, out, files):
|
|
92
|
+
try:
|
|
93
|
+
tree = ast.parse(read(d, "greet.py"))
|
|
94
|
+
except SyntaxError:
|
|
95
|
+
return False, "greet.py 语法错误"
|
|
96
|
+
fn = next((n for n in ast.walk(tree) if isinstance(n, ast.FunctionDef) and n.name == "goodbye"), None)
|
|
97
|
+
if fn is None:
|
|
98
|
+
return False, "没有 goodbye 函数"
|
|
99
|
+
doc = ast.get_docstring(fn) or ""
|
|
100
|
+
if not re.search(r"[\u4e00-\u9fff]", doc):
|
|
101
|
+
return False, "goodbye 没有中文 docstring(没遵守 AGENT.md)"
|
|
102
|
+
r = run_py(d, "-c", "import greet; assert greet.goodbye('a')=='bye a'")
|
|
103
|
+
return r.returncode == 0, "" if r.returncode == 0 else "goodbye 行为不对"
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def check_minimal_diff(d, out, files):
|
|
107
|
+
ok, why = unchanged(d, files, ["lib.py", "README.md"])
|
|
108
|
+
if not ok:
|
|
109
|
+
return False, why + "(越界修改)"
|
|
110
|
+
r = run_py(d, "app.py")
|
|
111
|
+
return r.stdout.strip() == "Hello, world", "" if r.stdout.strip() == "Hello, world" else f"输出:{r.stdout.strip()}"
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
# ---------- 任务定义 ----------
|
|
115
|
+
TASKS = [
|
|
116
|
+
{
|
|
117
|
+
"name": "fix_bug",
|
|
118
|
+
"prompt": "运行 test_calc.py,如果失败就修复 calc.py 中的 bug,直到测试通过。不要修改测试文件。",
|
|
119
|
+
"files": {
|
|
120
|
+
"calc.py": "def add(a, b):\n return a - b\n\n\ndef mul(a, b):\n return a * b\n",
|
|
121
|
+
"test_calc.py": "from calc import add, mul\n\nassert add(1, 2) == 3\nassert add(-1, 1) == 0\nassert mul(2, 3) == 6\nprint('ok')\n",
|
|
122
|
+
},
|
|
123
|
+
"check": check_fix_bug,
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
"name": "add_function",
|
|
127
|
+
"prompt": "在 utils.py 里新增函数 cube(x),返回 x 的立方,保留已有函数不变。写完请运行验证一下。",
|
|
128
|
+
"files": {"utils.py": "def square(x):\n return x * x\n"},
|
|
129
|
+
"check": check_add_function,
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
"name": "find_answer",
|
|
133
|
+
"prompt": "这个项目里 parse_config 函数是在哪个文件里定义的?只回答文件名,不要修改任何文件。",
|
|
134
|
+
"files": {
|
|
135
|
+
"config_loader.py": "def parse_config(path):\n return {}\n",
|
|
136
|
+
"legacy_config.py": "def parse_config_legacy(path):\n return {}\n",
|
|
137
|
+
"main.py": "from config_loader import parse_config\nprint(parse_config('x'))\n",
|
|
138
|
+
"docs.md": "调用 parse_config 来读取配置。\n",
|
|
139
|
+
},
|
|
140
|
+
"check": check_find_answer,
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
"name": "rename_refactor",
|
|
144
|
+
"prompt": "把函数 calc_total 重命名为 compute_total,更新所有引用,改完运行 main.py 确认输出不变。",
|
|
145
|
+
"files": {
|
|
146
|
+
"a.py": "def calc_total(items):\n return sum(items)\n",
|
|
147
|
+
"b.py": "from a import calc_total\n\n\ndef report(items):\n return f'total={calc_total(items)}'\n",
|
|
148
|
+
"main.py": "from a import calc_total\nfrom b import report\n\nprint(calc_total([1, 2, 3]), report([4, 5]))\n",
|
|
149
|
+
},
|
|
150
|
+
"check": check_rename,
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
"name": "obey_memory",
|
|
154
|
+
"prompt": "在 greet.py 里新增函数 goodbye(name),返回 'bye ' 加上 name。",
|
|
155
|
+
"files": {
|
|
156
|
+
"AGENT.md": "# 项目约定\n- 所有新增的函数都必须带中文 docstring(三引号)。\n",
|
|
157
|
+
"greet.py": 'def hello(name):\n """打招呼。"""\n return f"hello {name}"\n',
|
|
158
|
+
},
|
|
159
|
+
"check": check_memory,
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
"name": "minimal_diff",
|
|
163
|
+
"prompt": "把 app.py 里的问候语从 Hi 改成 Hello,不要改动其他任何文件。",
|
|
164
|
+
"files": {
|
|
165
|
+
"app.py": "print('Hi, world')\n",
|
|
166
|
+
"lib.py": "def helper():\n return 'Hi'\n",
|
|
167
|
+
"README.md": "# demo\n问候语:Hi\n",
|
|
168
|
+
},
|
|
169
|
+
"check": check_minimal_diff,
|
|
170
|
+
},
|
|
171
|
+
]
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
# ---------- 运行器 ----------
|
|
175
|
+
def setup_dir(task):
|
|
176
|
+
d = tempfile.mkdtemp(prefix=f"eval_{task['name']}_")
|
|
177
|
+
for rel, content in task["files"].items():
|
|
178
|
+
with open(os.path.join(d, rel), "w", encoding="utf-8") as f:
|
|
179
|
+
f.write(content)
|
|
180
|
+
return d
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def run_once(task, agent_cmd, timeout, keep):
|
|
184
|
+
d = setup_dir(task)
|
|
185
|
+
env = dict(os.environ, PYTHONIOENCODING="utf-8", PYTHONUTF8="1")
|
|
186
|
+
t0 = time.time()
|
|
187
|
+
out, note = "", ""
|
|
188
|
+
try:
|
|
189
|
+
p = subprocess.run(
|
|
190
|
+
[*agent_cmd, "--yes", task["prompt"]],
|
|
191
|
+
cwd=d, capture_output=True, text=True, encoding="utf-8", errors="replace",
|
|
192
|
+
timeout=timeout, env=env,
|
|
193
|
+
)
|
|
194
|
+
out = p.stdout
|
|
195
|
+
if p.returncode != 0:
|
|
196
|
+
ok, note = False, "Agent 异常退出:" + (p.stderr.strip().splitlines() or ["?"])[-1][:80]
|
|
197
|
+
else:
|
|
198
|
+
try:
|
|
199
|
+
ok, note = task["check"](d, out, task["files"])
|
|
200
|
+
except Exception as e: # 检查函数自己出错也算失败,但要把原因留下
|
|
201
|
+
ok, note = False, f"检查出错 {type(e).__name__}: {e}"
|
|
202
|
+
except subprocess.TimeoutExpired:
|
|
203
|
+
ok, note = False, f"超时(>{timeout}s)"
|
|
204
|
+
secs = time.time() - t0
|
|
205
|
+
|
|
206
|
+
m = USAGE_RE.search(out)
|
|
207
|
+
reqs, pt, ct = (int(x) for x in m.groups()) if m else (0, 0, 0)
|
|
208
|
+
if keep:
|
|
209
|
+
note += f" [目录保留:{d}]"
|
|
210
|
+
else:
|
|
211
|
+
shutil.rmtree(d, ignore_errors=True)
|
|
212
|
+
return {"task": task["name"], "ok": ok, "secs": round(secs, 1), "requests": reqs,
|
|
213
|
+
"tokens": pt + ct, "note": note}
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def summarize(runs):
|
|
217
|
+
rows = {}
|
|
218
|
+
for r in runs:
|
|
219
|
+
rows.setdefault(r["task"], []).append(r)
|
|
220
|
+
print(f"\n{'任务':<18}{'通过':<8}{'平均耗时':<10}{'平均请求':<10}{'平均tokens':<12}失败原因")
|
|
221
|
+
print("-" * 78)
|
|
222
|
+
for name, rs in rows.items():
|
|
223
|
+
n_ok = sum(r["ok"] for r in rs)
|
|
224
|
+
avg = lambda k: sum(r[k] for r in rs) / len(rs)
|
|
225
|
+
why = next((r["note"] for r in rs if not r["ok"]), "")
|
|
226
|
+
print(f"{name:<18}{f'{n_ok}/{len(rs)}':<8}{avg('secs'):<10.1f}{avg('requests'):<10.1f}{avg('tokens'):<12.0f}{why[:60]}")
|
|
227
|
+
total_ok = sum(r["ok"] for r in runs)
|
|
228
|
+
print("-" * 78)
|
|
229
|
+
print(f"总计通过 {total_ok}/{len(runs)}({100 * total_ok / max(1, len(runs)):.0f}%),"
|
|
230
|
+
f"总 tokens {sum(r['tokens'] for r in runs)}")
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def rates(path):
|
|
234
|
+
with open(path, encoding="utf-8") as f:
|
|
235
|
+
runs = json.load(f)
|
|
236
|
+
out = {}
|
|
237
|
+
for r in runs:
|
|
238
|
+
a = out.setdefault(r["task"], [0, 0, 0])
|
|
239
|
+
a[0] += r["ok"]; a[1] += 1; a[2] += r["tokens"]
|
|
240
|
+
return out
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def compare(a_path, b_path):
|
|
244
|
+
a, b = rates(a_path), rates(b_path)
|
|
245
|
+
print(f"\n{'任务':<18}{os.path.basename(a_path):<22}{os.path.basename(b_path):<22}")
|
|
246
|
+
print("-" * 62)
|
|
247
|
+
for name in sorted(set(a) | set(b)):
|
|
248
|
+
fa = f"{a[name][0]}/{a[name][1]} ({a[name][2] // a[name][1]}tok)" if name in a else "-"
|
|
249
|
+
fb = f"{b[name][0]}/{b[name][1]} ({b[name][2] // b[name][1]}tok)" if name in b else "-"
|
|
250
|
+
print(f"{name:<18}{fa:<22}{fb:<22}")
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def main():
|
|
254
|
+
ap = argparse.ArgumentParser(description="Agent 评测")
|
|
255
|
+
ap.add_argument("--agent", default=None, help="被测脚本路径;默认测试已安装的 tiny-agent(python -m tiny_agent)")
|
|
256
|
+
ap.add_argument("--only", help="只跑指定任务名(逗号分隔)")
|
|
257
|
+
ap.add_argument("--repeat", type=int, default=1, help="每个任务重复次数")
|
|
258
|
+
ap.add_argument("--timeout", type=int, default=300, help="单次运行超时(秒)")
|
|
259
|
+
ap.add_argument("--keep", action="store_true", help="保留临时目录,便于排查失败原因")
|
|
260
|
+
ap.add_argument("--out", help="把每次运行的结果存成 JSON")
|
|
261
|
+
ap.add_argument("--compare", nargs=2, metavar=("A.json", "B.json"), help="对比两次结果并退出")
|
|
262
|
+
ap.add_argument("--list", action="store_true", help="列出所有任务并退出")
|
|
263
|
+
opts = ap.parse_args()
|
|
264
|
+
|
|
265
|
+
if opts.list:
|
|
266
|
+
for t in TASKS:
|
|
267
|
+
print(f"{t['name']:<18}{t['prompt']}")
|
|
268
|
+
return
|
|
269
|
+
if opts.compare:
|
|
270
|
+
compare(*opts.compare)
|
|
271
|
+
return
|
|
272
|
+
|
|
273
|
+
names = set(opts.only.split(",")) if opts.only else None
|
|
274
|
+
tasks = [t for t in TASKS if not names or t["name"] in names]
|
|
275
|
+
if not tasks:
|
|
276
|
+
sys.exit("没有匹配的任务,用 --list 查看")
|
|
277
|
+
from .cli import KEY_ENVS, resolve_setting
|
|
278
|
+
|
|
279
|
+
if not resolve_setting(KEY_ENVS, "api_key", "")[0]:
|
|
280
|
+
sys.exit("没有找到 API key。请先运行 tiny-agent --setup,或设置环境变量 DEEPSEEK_API_KEY。")
|
|
281
|
+
|
|
282
|
+
# 子进程会切换工作目录,所以指定脚本时必须用绝对路径
|
|
283
|
+
agent_cmd = [PY, os.path.abspath(opts.agent)] if opts.agent else [PY, "-m", "tiny_agent"]
|
|
284
|
+
model = os.environ.get("TINY_AGENT_MODEL") or os.environ.get("DEEPSEEK_MODEL") or "(配置文件或默认)"
|
|
285
|
+
print(f"Agent: {' '.join(agent_cmd)}\n模型: {model}\n")
|
|
286
|
+
runs = []
|
|
287
|
+
for t in tasks:
|
|
288
|
+
for i in range(opts.repeat):
|
|
289
|
+
r = run_once(t, agent_cmd, opts.timeout, opts.keep)
|
|
290
|
+
runs.append(r)
|
|
291
|
+
print(f"{'PASS' if r['ok'] else 'FAIL'} {t['name']:<18}#{i + 1} {r['secs']:>6.1f}s {r['note']}")
|
|
292
|
+
summarize(runs)
|
|
293
|
+
if opts.out:
|
|
294
|
+
with open(opts.out, "w", encoding="utf-8") as f:
|
|
295
|
+
json.dump(runs, f, ensure_ascii=False, indent=1)
|
|
296
|
+
print(f"\n结果已保存到 {opts.out}")
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
if __name__ == "__main__":
|
|
300
|
+
main()
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tiny-cli-agent
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A small terminal coding agent for DeepSeek / OpenAI-compatible APIs
|
|
5
|
+
Author: wangdejun
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Classifier: Development Status :: 3 - Alpha
|
|
8
|
+
Classifier: Environment :: Console
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Topic :: Software Development
|
|
15
|
+
Requires-Python: >=3.10
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: openai>=1.30
|
|
19
|
+
Requires-Dist: rich>=13
|
|
20
|
+
Requires-Dist: prompt_toolkit>=3.0.40
|
|
21
|
+
Requires-Dist: textual>=8.0
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
24
|
+
Requires-Dist: build; extra == "dev"
|
|
25
|
+
Requires-Dist: twine; extra == "dev"
|
|
26
|
+
Requires-Dist: pexpect>=4; sys_platform != "win32" and extra == "dev"
|
|
27
|
+
Requires-Dist: pyte>=0.8; sys_platform != "win32" and extra == "dev"
|
|
28
|
+
Dynamic: license-file
|
|
29
|
+
|
|
30
|
+
# tiny-agent
|
|
31
|
+
|
|
32
|
+
一个小型的终端编程 Agent:让大模型在循环里自己决定调用哪个工具(读写文件、搜索、执行命令、派生子 Agent),执行后把结果喂回模型,直到任务完成。思路与 Claude Code 一致,规模小到可以通读,适合学习和实验。默认对接 DeepSeek,任何 OpenAI 兼容的接口都可以使用。
|
|
33
|
+
|
|
34
|
+
> **状态:Alpha(0.1.0)。** 这是一个教学和实验性质的实现,不是生产级工具。使用前请先阅读下面的[安全须知](#安全须知)。
|
|
35
|
+
|
|
36
|
+
## 主要功能
|
|
37
|
+
|
|
38
|
+
- **工具**:读取(支持按行分页)、新建、精确编辑文件,按文件名和内容搜索,执行 shell 命令
|
|
39
|
+
- **权限**:写文件、改文件、执行命令前确认(改文件显示 diff,新建文件预览内容);文件工具限定在启动目录内
|
|
40
|
+
- **上下文管理**:超过阈值自动压缩(先精简旧的大块工具输出,再总结);重复调用检测;步数上限
|
|
41
|
+
- **子 Agent**:把调研类任务交给全新上下文的只读子 Agent,主对话只收到结论;多个子 Agent 并行
|
|
42
|
+
- **计划模式**:只读调研并提交计划,经你批准后才执行
|
|
43
|
+
- **记忆与会话**:`AGENT.md` 项目记忆;每轮自动存档,`--resume` 恢复
|
|
44
|
+
- **三种界面**:纯文本、终端(Markdown 渲染、补全、状态栏)、全屏(Textual)
|
|
45
|
+
- **可靠性**:API 限流、服务端错误、网络中断时自动退避重试;流式输出;用量统计
|
|
46
|
+
- **评测**:附带 `tiny-agent-eval`,用固定任务检验每次改动是变好还是变差
|
|
47
|
+
|
|
48
|
+
## 安装
|
|
49
|
+
|
|
50
|
+
需要 Python 3.10 或更高版本。
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
# 推荐:用 pipx 安装命令行工具(依赖隔离,不污染系统 Python)
|
|
54
|
+
pipx install . # 在项目根目录执行
|
|
55
|
+
pipx install ./tiny_cli_agent-0.1.0-py3-none-any.whl # 或者安装构建好的 wheel
|
|
56
|
+
|
|
57
|
+
# 也可以用 pip
|
|
58
|
+
pip install .
|
|
59
|
+
|
|
60
|
+
# 开发时用可编辑安装(改代码立即生效)
|
|
61
|
+
pip install -e ".[dev]"
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
目前还没有发布到 PyPI,所以暂时不能 `pip install tiny-cli-agent`。
|
|
65
|
+
|
|
66
|
+
## 快速开始
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
tiny-agent --setup # 配置 API key、接口地址、模型(保存到 ~/.tiny-agent/config.json)
|
|
70
|
+
cd 你的项目目录 # Agent 只能访问启动它时所在的目录
|
|
71
|
+
tiny-agent # 进入交互模式
|
|
72
|
+
tiny-agent '给 utils.py 里的函数补上类型注解' # 一次性任务,做完退出
|
|
73
|
+
tiny-agent --tui # 全屏界面
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
第一次使用建议在一个**空的测试目录**里试,不要直接在重要项目里运行。
|
|
77
|
+
|
|
78
|
+
## 命令行参数
|
|
79
|
+
|
|
80
|
+
| 参数 | 说明 |
|
|
81
|
+
| ---------------- | --------------------------------------------------------------------------- |
|
|
82
|
+
| `任务文字` | 一次性任务,执行完退出;不给则进入交互模式 |
|
|
83
|
+
| `--plan` | 以计划模式启动 |
|
|
84
|
+
| `--resume [ID]` | 恢复会话;不带 ID 恢复最近一次;若后面跟的不是已有的会话 ID,则当作任务文字 |
|
|
85
|
+
| `--tui` | 全屏界面(Textual) |
|
|
86
|
+
| `--max-steps N` | 主 Agent 每轮最多执行的步数,默认 30 |
|
|
87
|
+
| `--plain` | 纯文本输出(输出被重定向时会自动启用) |
|
|
88
|
+
| `--simple-input` | 不用 prompt_toolkit,改用普通输入 |
|
|
89
|
+
| `--yes` | **自动批准所有确认**,包括执行命令。危险,只用于一次性临时目录和评测 |
|
|
90
|
+
| `--setup` | 交互式配置 |
|
|
91
|
+
| `--show-config` | 显示当前生效的配置及其来源(key 会被遮蔽) |
|
|
92
|
+
| `--version` | 显示版本 |
|
|
93
|
+
|
|
94
|
+
## 交互模式
|
|
95
|
+
|
|
96
|
+
| 命令 | 说明 |
|
|
97
|
+
| ----------- | ----------------------------------------- |
|
|
98
|
+
| `/plan` | 在计划模式与执行模式之间切换 |
|
|
99
|
+
| `/sessions` | 列出最近的会话 |
|
|
100
|
+
| `/usage` | 查看累计用量 |
|
|
101
|
+
| `/compact` | 手动压缩上下文 |
|
|
102
|
+
| `/memory` | 查看已加载的记忆 |
|
|
103
|
+
| `/todo` | 查看任务清单 |
|
|
104
|
+
| `/expand` | 展开上一次工具输出的完整内容 |
|
|
105
|
+
| `/help` | 显示命令和快捷键 |
|
|
106
|
+
| `# 内容` | 追加一条记忆到项目的 `AGENT.md`,立即生效 |
|
|
107
|
+
| `exit` | 退出 |
|
|
108
|
+
|
|
109
|
+
终端界面:`Shift+Tab` 切换计划模式,`Alt+Enter` 或行尾反斜杠加回车换行,`/` 和 `@` 弹出补全,`Ctrl+C` 中断任务(空闲时连按两次退出)。
|
|
110
|
+
|
|
111
|
+
全屏界面(`--tui`):`Enter` 发送,`Ctrl+J` 换行,`Tab` 补全,`↑` `↓` 翻历史,`Shift+Tab` 切换计划模式,`Ctrl+C` 随时中断,`Ctrl+L` 清屏,`Ctrl+Q` 退出;另有 `/copy`(复制上一条回复)和 `/export`(导出整个对话)。
|
|
112
|
+
|
|
113
|
+
## 配置
|
|
114
|
+
|
|
115
|
+
取值顺序:**环境变量 > 配置文件 > 默认值**。可用 `tiny-agent --show-config` 查看每一项实际取自哪里。
|
|
116
|
+
|
|
117
|
+
| 项目 | 环境变量 | 配置文件字段 | 默认值 |
|
|
118
|
+
| -------- | ------------------------------------------ | ------------ | -------------------------- |
|
|
119
|
+
| API key | `TINY_AGENT_API_KEY` 或 `DEEPSEEK_API_KEY` | `api_key` | 无(必需) |
|
|
120
|
+
| 接口地址 | `TINY_AGENT_BASE_URL` | `base_url` | `https://api.deepseek.com` |
|
|
121
|
+
| 模型 | `TINY_AGENT_MODEL` 或 `DEEPSEEK_MODEL` | `model` | `deepseek-flash` |
|
|
122
|
+
|
|
123
|
+
其他环境变量:`TINY_AGENT_COMPACT_TOKENS`(触发压缩的阈值,默认 50000)、`TINY_AGENT_YES`(等同 `--yes`)、`TINY_AGENT_PLAIN`、`TINY_AGENT_SIMPLE_INPUT`。
|
|
124
|
+
|
|
125
|
+
**使用其他 OpenAI 兼容的服务**(包括本地的 Ollama、llama.cpp 等):把接口地址和模型改成对应的值即可。"关闭思考模式"是 DeepSeek 的扩展参数,只有当接口地址包含 `deepseek` 时才会发送。模型名更新很快,请以服务商的文档为准。
|
|
126
|
+
|
|
127
|
+
配置文件 `~/.tiny-agent/config.json` 里的 API key 是**明文**存放的,文件权限设为仅本人可读写(Windows 上权限模型不同,请自行留意)。不要把它提交到 git 或分享给别人。
|
|
128
|
+
|
|
129
|
+
## 记忆文件
|
|
130
|
+
|
|
131
|
+
启动时会读取两个文件并拼进系统提示:
|
|
132
|
+
|
|
133
|
+
- `~/.tiny-agent/AGENT.md`:用户级,对所有项目生效(个人偏好)
|
|
134
|
+
- 当前目录下的 `AGENT.md`:项目级(命令、代码风格、目录约定)
|
|
135
|
+
|
|
136
|
+
适合写模型从代码里猜不出来、但每次都需要知道的内容。写得短而具体。**不要在里面放任何密钥。**
|
|
137
|
+
|
|
138
|
+
## 工具与权限
|
|
139
|
+
|
|
140
|
+
| 工具 | 作用 | 需确认 |
|
|
141
|
+
| ---------------- | ------------------------------------------------------------------- | ------ |
|
|
142
|
+
| `read_file` | 按行读取,`offset` / `limit` 分页,输出带行号,被截断时提示续读位置 | 否 |
|
|
143
|
+
| `glob` | 按文件名查找 | 否 |
|
|
144
|
+
| `grep` | 正则搜索,`path` 可为目录或单个文件 | 否 |
|
|
145
|
+
| `write_file` | 新建或覆盖文件(确认前给预览或 diff) | 是 |
|
|
146
|
+
| `edit_file` | 精确字符串替换(要求匹配唯一) | 是 |
|
|
147
|
+
| `run_bash` | 执行 shell 命令,60 秒超时,不继承终端标准输入 | 是 |
|
|
148
|
+
| `task` | 派生只读子 Agent | 否 |
|
|
149
|
+
| `todo_write` | 更新任务清单 | 否 |
|
|
150
|
+
| `exit_plan_mode` | 提交计划待审批(仅计划模式) | 审批 |
|
|
151
|
+
|
|
152
|
+
确认时输入 `y` 本次允许,`a` 本会话内对该工具一直允许,`n` 拒绝。
|
|
153
|
+
|
|
154
|
+
## 安全须知
|
|
155
|
+
|
|
156
|
+
请在使用前了解这些限制,它们大多是有意保持简单的结果:
|
|
157
|
+
|
|
158
|
+
1. **`run_bash` 不受目录沙箱限制。** 沙箱只约束文件类工具,shell 命令能做你的账户能做的一切。每条命令请看清再批准;"本会话总是允许"要慎用;`--yes` 会跳过所有确认。
|
|
159
|
+
2. **只读工具能读到工作目录里的任何文本文件**,包括 `.env` 等存放密钥的文件,内容会被发送给模型接口。不要在含有机密文件的目录里启动。
|
|
160
|
+
3. **提示注入**:文件内容和命令输出都会进入模型的上下文,恶意内容可能诱导模型执行危险操作。权限确认是最后一道防线。
|
|
161
|
+
4. **会话文件是明文**,保存在启动目录的 `.tiny-agent/`(含文件内容和命令输出),已在 `.gitignore` 里排除,请保持这样。
|
|
162
|
+
5. 目前**没有**命令白名单与黑名单、Hooks、检查点与撤销。修改文件前建议先提交 git。
|
|
163
|
+
|
|
164
|
+
## 数据存放位置
|
|
165
|
+
|
|
166
|
+
| 位置 | 内容 |
|
|
167
|
+
| ------------------------------------- | ----------------------- |
|
|
168
|
+
| `~/.tiny-agent/config.json` | API key、接口地址、模型 |
|
|
169
|
+
| `~/.tiny-agent/AGENT.md` | 用户级记忆 |
|
|
170
|
+
| `<启动目录>/AGENT.md` | 项目级记忆 |
|
|
171
|
+
| `<启动目录>/.tiny-agent/sessions/` | 会话存档 |
|
|
172
|
+
| `<启动目录>/.tiny-agent/history` | 终端界面的输入历史 |
|
|
173
|
+
| `<启动目录>/.tiny-agent/transcripts/` | `/export` 导出的对话 |
|
|
174
|
+
|
|
175
|
+
## 评测
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
tiny-agent-eval --list # 列出内置的 6 个任务
|
|
179
|
+
tiny-agent-eval --repeat 3 --out base.json # 每个任务跑 3 次并保存结果
|
|
180
|
+
tiny-agent-eval --compare base.json new.json # 对比两次结果
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
每个任务在全新的临时目录里用 `--yes` 无人值守运行,再用确定性的代码检查结果(并专门防作弊,比如改测试、越界改文件)。**评测会真实调用 API,产生费用**;LLM 有随机性,请每个任务至少跑 3 次再看通过率。
|
|
184
|
+
|
|
185
|
+
## 开发
|
|
186
|
+
|
|
187
|
+
```
|
|
188
|
+
src/tiny_agent/
|
|
189
|
+
cli.py 核心:工具、权限、循环、压缩、会话、三种界面、入口(按注释分区组织)
|
|
190
|
+
evals.py 评测脚本
|
|
191
|
+
tests/ 检查脚本;用 python tests/run_all.py 或 pytest 运行
|
|
192
|
+
AGENT.md 给 Agent 自己看的开发约定和代码地图
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
```bash
|
|
196
|
+
pip install -e ".[dev]"
|
|
197
|
+
python tests/run_all.py # 运行全部检查(不会调用真实 API)
|
|
198
|
+
pytest # 或者用 pytest
|
|
199
|
+
python -m build # 构建 wheel 和 sdist 到 dist/
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
测试里有一套端到端检查:用真实的 `openai` SDK 对接本地模拟的 OpenAI 兼容服务器,验证流式解析、工具调用碎片拼接、限流重试和错误提示。
|
|
203
|
+
|
|
204
|
+
## 已知限制
|
|
205
|
+
|
|
206
|
+
- 单个 `cli.py` 文件较大(约 2300 行),后续计划拆成模块
|
|
207
|
+
- `run_bash` 超时固定 60 秒,没有后台长任务支持;Ctrl+C 不能立即终止正在执行的命令
|
|
208
|
+
- 上下文压缩的摘要质量没有专门评测过
|
|
209
|
+
- 只在 macOS/Linux 风格的环境里测试过,Windows 未实测
|
|
210
|
+
- 开发和测试主要使用的是 Python 3.12;依赖库只测过最新版本
|
|
211
|
+
|
|
212
|
+
## 许可证
|
|
213
|
+
|
|
214
|
+
本项目使用 [MIT 许可证](LICENSE)。
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
tiny_agent/__init__.py,sha256=y7ZzDAcEhdF5HTTE2dnQdnOu8AAq1UxvZ9En3VWZoxU,70
|
|
2
|
+
tiny_agent/__main__.py,sha256=S9mX0eKxt49LH1JpMoQwotNs2OJlZoPeryuHfFc16Po,98
|
|
3
|
+
tiny_agent/cli.py,sha256=9EuN4PWJNs-fFx1u6O585pTTi-JnlvVxmvy2OMhjjOQ,99212
|
|
4
|
+
tiny_agent/evals.py,sha256=B3ype2ZlHf01nvSH6eHLncp7AnHyLf3tFHW6_8ElMOA,12356
|
|
5
|
+
tiny_cli_agent-0.1.0.dist-info/licenses/LICENSE,sha256=FHZn3jIVnYUo5JxWiGM9zhuf-ta_6i1R3rKXBGMX0RY,1066
|
|
6
|
+
tiny_cli_agent-0.1.0.dist-info/METADATA,sha256=9bkygaqefGFlx-_UX5QeKL0GjnqcDUVDYQJpLrorMIg,12885
|
|
7
|
+
tiny_cli_agent-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
8
|
+
tiny_cli_agent-0.1.0.dist-info/entry_points.txt,sha256=pYQ2JjirzB_XhC5dJe4ybjqf4kpojNg6KYRwxjXiVtw,91
|
|
9
|
+
tiny_cli_agent-0.1.0.dist-info/top_level.txt,sha256=lWwIr9UPJOH8lYRuN8FCNKoZ6rzfcRGNBmxyH-XEd4o,11
|
|
10
|
+
tiny_cli_agent-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 wangdejun
|
|
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 @@
|
|
|
1
|
+
tiny_agent
|