tp-wy 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.
- tp_wy-0.1.0/LICENSE +21 -0
- tp_wy-0.1.0/PKG-INFO +29 -0
- tp_wy-0.1.0/README.md +1 -0
- tp_wy-0.1.0/pyproject.toml +17 -0
- tp_wy-0.1.0/setup.cfg +4 -0
- tp_wy-0.1.0/template/__init__.py +106 -0
- tp_wy-0.1.0/template/__main__.py +7 -0
- tp_wy-0.1.0/template/cli.py +193 -0
- tp_wy-0.1.0/template/converter.py +54 -0
- tp_wy-0.1.0/template/extractor.py +242 -0
- tp_wy-0.1.0/template/loader.py +94 -0
- tp_wy-0.1.0/template/models.py +64 -0
- tp_wy-0.1.0/template/store.py +221 -0
- tp_wy-0.1.0/tp_wy.egg-info/PKG-INFO +29 -0
- tp_wy-0.1.0/tp_wy.egg-info/SOURCES.txt +16 -0
- tp_wy-0.1.0/tp_wy.egg-info/dependency_links.txt +1 -0
- tp_wy-0.1.0/tp_wy.egg-info/entry_points.txt +2 -0
- tp_wy-0.1.0/tp_wy.egg-info/top_level.txt +1 -0
tp_wy-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
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.
|
tp_wy-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tp-wy
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: 从项目文件(PRD / 规格 / README 等)中提取任务拆解模板,供 tasker 多智能体编排器的 LLM 拆分阶段使用
|
|
5
|
+
License: MIT License
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2026
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
26
|
+
|
|
27
|
+
Requires-Python: >=3.9
|
|
28
|
+
License-File: LICENSE
|
|
29
|
+
Dynamic: license-file
|
tp_wy-0.1.0/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# tp
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "tp-wy"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
requires-python = ">=3.9"
|
|
9
|
+
description = "从项目文件(PRD / 规格 / README 等)中提取任务拆解模板,供 tasker 多智能体编排器的 LLM 拆分阶段使用"
|
|
10
|
+
license = { file = "LICENSE" }
|
|
11
|
+
dependencies = []
|
|
12
|
+
|
|
13
|
+
[project.scripts]
|
|
14
|
+
tp = "template.cli:main"
|
|
15
|
+
|
|
16
|
+
[tool.setuptools]
|
|
17
|
+
packages = ["template"]
|
tp_wy-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""tasker-template —— 从项目文件中提取任务拆解模板。
|
|
2
|
+
|
|
3
|
+
用法:
|
|
4
|
+
from template import extract_template
|
|
5
|
+
template = extract_template("path/to/readme.md")
|
|
6
|
+
print(template.to_planner_json())
|
|
7
|
+
|
|
8
|
+
from template import store_template, list_templates, catalog_for_llm
|
|
9
|
+
store_template("spec/payment.md", tags=["payment", "api"])
|
|
10
|
+
print(catalog_for_llm()) # 供 LLM 决策使用
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from .models import SuggestedTask, TaskTemplate
|
|
16
|
+
from .loader import load_file, load_files, load_directory
|
|
17
|
+
from .extractor import extract_tasks
|
|
18
|
+
from .converter import to_task_template, to_planner_json
|
|
19
|
+
from .store import (
|
|
20
|
+
Store,
|
|
21
|
+
catalog_for_llm,
|
|
22
|
+
get_template,
|
|
23
|
+
list_templates,
|
|
24
|
+
remove_template,
|
|
25
|
+
search_templates,
|
|
26
|
+
store_template,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
__version__ = "0.0.0"
|
|
30
|
+
try:
|
|
31
|
+
import re
|
|
32
|
+
from pathlib import Path
|
|
33
|
+
|
|
34
|
+
_text = (Path(__file__).resolve().parent.parent / "pyproject.toml").read_text(encoding="utf-8")
|
|
35
|
+
_m = re.search(r'^version\s*=\s*"([^"]+)"', _text, re.MULTILINE)
|
|
36
|
+
if _m:
|
|
37
|
+
__version__ = _m.group(1)
|
|
38
|
+
except Exception:
|
|
39
|
+
pass
|
|
40
|
+
__all__ = [
|
|
41
|
+
"extract_template",
|
|
42
|
+
"TaskTemplate",
|
|
43
|
+
"SuggestedTask",
|
|
44
|
+
"load_file",
|
|
45
|
+
"load_files",
|
|
46
|
+
"load_directory",
|
|
47
|
+
"extract_tasks",
|
|
48
|
+
"to_task_template",
|
|
49
|
+
"to_planner_json",
|
|
50
|
+
"Store",
|
|
51
|
+
"store_template",
|
|
52
|
+
"list_templates",
|
|
53
|
+
"search_templates",
|
|
54
|
+
"get_template",
|
|
55
|
+
"remove_template",
|
|
56
|
+
"catalog_for_llm",
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def extract_template(
|
|
61
|
+
path: str,
|
|
62
|
+
*,
|
|
63
|
+
template_name: str = "",
|
|
64
|
+
extra_context: str = "",
|
|
65
|
+
) -> TaskTemplate:
|
|
66
|
+
"""从文件或目录路径提取任务拆解模板。
|
|
67
|
+
|
|
68
|
+
一键函数:文件加载 → 任务提取 → 模板转换。
|
|
69
|
+
"""
|
|
70
|
+
import os
|
|
71
|
+
from pathlib import Path
|
|
72
|
+
|
|
73
|
+
p = Path(path).expanduser().resolve()
|
|
74
|
+
|
|
75
|
+
if p.is_dir():
|
|
76
|
+
files = load_directory(p)
|
|
77
|
+
if not files:
|
|
78
|
+
raise FileNotFoundError(f"目录 {p} 中没有可读取的文件")
|
|
79
|
+
combined_parts: list[str] = []
|
|
80
|
+
all_items: list[dict] = []
|
|
81
|
+
for fpath, content, file_type in files:
|
|
82
|
+
combined_parts.append(f"# 文件: {fpath}\n\n{content}")
|
|
83
|
+
all_items.extend(extract_tasks(content, file_type))
|
|
84
|
+
combined = "\n\n---\n\n".join(combined_parts)
|
|
85
|
+
items = extract_tasks(combined, "markdown")
|
|
86
|
+
if not items:
|
|
87
|
+
items = all_items
|
|
88
|
+
source = str(p)
|
|
89
|
+
else:
|
|
90
|
+
content, file_type = load_file(p)
|
|
91
|
+
items = extract_tasks(content, file_type)
|
|
92
|
+
source = str(p)
|
|
93
|
+
|
|
94
|
+
name = template_name or p.stem or "从文件提取的模板"
|
|
95
|
+
|
|
96
|
+
# 尝试读取 README 作为额外的上下文
|
|
97
|
+
if not extra_context:
|
|
98
|
+
try:
|
|
99
|
+
readme_dir = p.parent if p.is_file() else p
|
|
100
|
+
readme = readme_dir / "README.md"
|
|
101
|
+
if readme.exists():
|
|
102
|
+
extra_context = readme.read_text(encoding="utf-8", errors="replace")[:2000]
|
|
103
|
+
except Exception:
|
|
104
|
+
pass
|
|
105
|
+
|
|
106
|
+
return to_task_template(items, template_name=name, source_file=source, extra_context=extra_context)
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"""CLI 入口 —— 极简接口。
|
|
2
|
+
|
|
3
|
+
用法:
|
|
4
|
+
template <path> 解析文件/目录,提取任务模板,存入本地库,打印摘要
|
|
5
|
+
template <path> --json 输出完整 JSON(不存储)
|
|
6
|
+
template ls 列出已存储模板
|
|
7
|
+
template rm <name> 删除模板
|
|
8
|
+
template show <name> 查看模板完整 JSON
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import argparse
|
|
14
|
+
import json
|
|
15
|
+
import sys
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
|
|
18
|
+
from . import __version__
|
|
19
|
+
from .loader import load_directory, load_file
|
|
20
|
+
from .extractor import extract_tasks
|
|
21
|
+
from .converter import to_task_template
|
|
22
|
+
from .store import Store
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def main(argv: list[str] | None = None) -> int:
|
|
26
|
+
if argv is None:
|
|
27
|
+
argv = sys.argv[1:]
|
|
28
|
+
|
|
29
|
+
# ---- 无参数:显示帮助 ----
|
|
30
|
+
if not argv:
|
|
31
|
+
_print_help()
|
|
32
|
+
return 1
|
|
33
|
+
|
|
34
|
+
first = argv[0]
|
|
35
|
+
|
|
36
|
+
# ---- 子命令:ls / rm / show ----
|
|
37
|
+
if first in ("ls", "list"):
|
|
38
|
+
return cmd_list()
|
|
39
|
+
if first in ("rm", "remove"):
|
|
40
|
+
name = argv[1] if len(argv) > 1 else ""
|
|
41
|
+
return cmd_remove(name)
|
|
42
|
+
if first == "show":
|
|
43
|
+
name = argv[1] if len(argv) > 1 else ""
|
|
44
|
+
return cmd_show(name)
|
|
45
|
+
if first in ("-h", "--help", "help"):
|
|
46
|
+
_print_help()
|
|
47
|
+
return 0
|
|
48
|
+
if first in ("-V", "--version"):
|
|
49
|
+
print(f"template {__version__}")
|
|
50
|
+
return 0
|
|
51
|
+
|
|
52
|
+
# ---- 默认:<path> —— 解析 + 存储 ----
|
|
53
|
+
return cmd_default(argv)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _print_help() -> None:
|
|
57
|
+
print(
|
|
58
|
+
f"template {__version__} —— 从项目文件提取任务拆解模板\n"
|
|
59
|
+
"\n"
|
|
60
|
+
"用法:\n"
|
|
61
|
+
" template <path> 解析文件/目录,存储模板,打印摘要\n"
|
|
62
|
+
" template <path> --json 仅输出 JSON(不存储)\n"
|
|
63
|
+
" template <path> --tags tag1,tag2 --desc \"描述\"\n"
|
|
64
|
+
" template ls 列出已存储的模板\n"
|
|
65
|
+
" template rm <name> 删除模板\n"
|
|
66
|
+
" template show <name> 查看模板完整内容\n"
|
|
67
|
+
"\n"
|
|
68
|
+
f"模板存储目录: {Store().dir}\n"
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
# ================================================================
|
|
73
|
+
# 默认命令:template <path> [flags]
|
|
74
|
+
# ================================================================
|
|
75
|
+
def cmd_default(argv: list[str]) -> int:
|
|
76
|
+
path_str = argv[0]
|
|
77
|
+
tags_str = ""
|
|
78
|
+
desc = ""
|
|
79
|
+
json_only = False
|
|
80
|
+
compact = False
|
|
81
|
+
|
|
82
|
+
# 简陋但够用的 flag 解析
|
|
83
|
+
i = 1
|
|
84
|
+
while i < len(argv):
|
|
85
|
+
a = argv[i]
|
|
86
|
+
if a == "--tags" and i + 1 < len(argv):
|
|
87
|
+
tags_str = argv[i + 1]; i += 2
|
|
88
|
+
elif a == "--desc" and i + 1 < len(argv):
|
|
89
|
+
desc = argv[i + 1]; i += 2
|
|
90
|
+
elif a == "--json":
|
|
91
|
+
json_only = True; i += 1
|
|
92
|
+
elif a == "--compact":
|
|
93
|
+
compact = True; i += 1
|
|
94
|
+
elif a == "--planner-json":
|
|
95
|
+
json_only = True; i += 1
|
|
96
|
+
else:
|
|
97
|
+
i += 1
|
|
98
|
+
|
|
99
|
+
path = Path(path_str).expanduser().resolve()
|
|
100
|
+
tags = [t.strip() for t in tags_str.split(",") if t.strip()] if tags_str else []
|
|
101
|
+
|
|
102
|
+
# 提取
|
|
103
|
+
if path.is_dir():
|
|
104
|
+
items, source, name = _extract_dir(path)
|
|
105
|
+
else:
|
|
106
|
+
items, source, name = _extract_file(path)
|
|
107
|
+
|
|
108
|
+
template = to_task_template(items, template_name=name, source_file=source)
|
|
109
|
+
|
|
110
|
+
# --json:只输出不存储
|
|
111
|
+
if json_only:
|
|
112
|
+
output = template.to_planner_json() if "--planner-json" in argv else template.to_dict()
|
|
113
|
+
indent = None if compact else 2
|
|
114
|
+
print(json.dumps(output, ensure_ascii=False, indent=indent))
|
|
115
|
+
return 0
|
|
116
|
+
|
|
117
|
+
# 存储
|
|
118
|
+
store = Store()
|
|
119
|
+
store.add(template.to_dict(), name=name, source=source, tags=tags, description=desc)
|
|
120
|
+
print(f"已存储: {name}")
|
|
121
|
+
print(store.catalog_text())
|
|
122
|
+
return 0
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
# ================================================================
|
|
126
|
+
# 子命令
|
|
127
|
+
# ================================================================
|
|
128
|
+
def cmd_list() -> int:
|
|
129
|
+
store = Store()
|
|
130
|
+
items = store.list()
|
|
131
|
+
if not items:
|
|
132
|
+
print("(无已存储的模板)")
|
|
133
|
+
print(f"目录: {store.dir}")
|
|
134
|
+
return 0
|
|
135
|
+
print(store.catalog_text())
|
|
136
|
+
return 0
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def cmd_remove(name: str) -> int:
|
|
140
|
+
if not name:
|
|
141
|
+
print("用法: template rm <name>", file=sys.stderr)
|
|
142
|
+
return 1
|
|
143
|
+
store = Store()
|
|
144
|
+
if store.remove(name):
|
|
145
|
+
print(f"已删除: {name}")
|
|
146
|
+
else:
|
|
147
|
+
print(f"模板不存在: {name}", file=sys.stderr)
|
|
148
|
+
print(f"可用: {[m.get('name') for m in store.list()]}", file=sys.stderr)
|
|
149
|
+
return 1
|
|
150
|
+
return 0
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def cmd_show(name: str) -> int:
|
|
154
|
+
if not name:
|
|
155
|
+
print("用法: template show <name>", file=sys.stderr)
|
|
156
|
+
return 1
|
|
157
|
+
store = Store()
|
|
158
|
+
data = store.get(name)
|
|
159
|
+
if data is None:
|
|
160
|
+
print(f"模板不存在: {name}", file=sys.stderr)
|
|
161
|
+
return 1
|
|
162
|
+
print(json.dumps(data, ensure_ascii=False, indent=2))
|
|
163
|
+
return 0
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
# ================================================================
|
|
167
|
+
# 提取逻辑
|
|
168
|
+
# ================================================================
|
|
169
|
+
def _extract_file(path: Path) -> tuple[list[dict], str, str]:
|
|
170
|
+
content, file_type = load_file(path)
|
|
171
|
+
items = extract_tasks(content, file_type)
|
|
172
|
+
return items, str(path), path.stem or "unnamed"
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _extract_dir(path: Path) -> tuple[list[dict], str, str]:
|
|
176
|
+
files = load_directory(path)
|
|
177
|
+
if not files:
|
|
178
|
+
raise FileNotFoundError(f"目录 {path} 中没有可读取的文件")
|
|
179
|
+
|
|
180
|
+
combined_parts: list[str] = []
|
|
181
|
+
all_items: list[dict] = []
|
|
182
|
+
for fpath, content, file_type in files:
|
|
183
|
+
combined_parts.append(f"# 文件: {fpath}\n\n{content}")
|
|
184
|
+
all_items.extend(extract_tasks(content, file_type))
|
|
185
|
+
combined = "\n\n---\n\n".join(combined_parts)
|
|
186
|
+
items = extract_tasks(combined, "markdown")
|
|
187
|
+
if not items:
|
|
188
|
+
items = all_items
|
|
189
|
+
return items, str(path), path.name or "unnamed"
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
if __name__ == "__main__":
|
|
193
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""模板转换:将提取的任务条目转换为 tasker planner 兼容的模板。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .models import SuggestedTask, TaskTemplate
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def to_task_template(
|
|
9
|
+
items: list[dict],
|
|
10
|
+
template_name: str = "",
|
|
11
|
+
source_file: str = "",
|
|
12
|
+
*,
|
|
13
|
+
extra_context: str = "",
|
|
14
|
+
) -> TaskTemplate:
|
|
15
|
+
"""将 extractor 返回的条目列表组装为 TaskTemplate。"""
|
|
16
|
+
suggested: list[SuggestedTask] = []
|
|
17
|
+
for item in items:
|
|
18
|
+
suggested.append(
|
|
19
|
+
SuggestedTask(
|
|
20
|
+
title=item.get("title", ""),
|
|
21
|
+
description=item.get("description", ""),
|
|
22
|
+
executor=item.get("executor", "claude"),
|
|
23
|
+
depends_on=item.get("depends_on", []),
|
|
24
|
+
acceptance=item.get("acceptance", ""),
|
|
25
|
+
)
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
# 构造 system_prompt_extension:注入到 LLM 拆分 prompt 的附加上下文
|
|
29
|
+
ext_parts: list[str] = []
|
|
30
|
+
if extra_context:
|
|
31
|
+
ext_parts.append(extra_context)
|
|
32
|
+
if source_file:
|
|
33
|
+
ext_parts.append(f"以下模板提取自文件: {source_file}")
|
|
34
|
+
if suggested:
|
|
35
|
+
lines = ["参考以下建议的任务拆分作为起点(你可以调整、合并或增删):"]
|
|
36
|
+
for i, t in enumerate(suggested, start=1):
|
|
37
|
+
lines.append(f"{i}. [{t.executor}] {t.title}")
|
|
38
|
+
if t.description:
|
|
39
|
+
lines.append(f" {t.description[:200]}")
|
|
40
|
+
if t.depends_on:
|
|
41
|
+
lines.append(f" 依赖: {', '.join(t.depends_on)}")
|
|
42
|
+
ext_parts.append("\n".join(lines))
|
|
43
|
+
|
|
44
|
+
return TaskTemplate(
|
|
45
|
+
template_name=template_name or "从文件提取的模板",
|
|
46
|
+
source_file=source_file,
|
|
47
|
+
system_prompt_extension="\n\n".join(ext_parts),
|
|
48
|
+
suggested_tasks=suggested,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def to_planner_json(template: TaskTemplate) -> dict:
|
|
53
|
+
"""输出与 tasker planner SYSTEM_PROMPT 兼容的 JSON 结构。"""
|
|
54
|
+
return template.to_planner_json()
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
"""内容提取:根据文件类型,用启发式规则提取隐含的任务描述。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import re
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def extract_tasks(content: str, file_type: str) -> list[dict]:
|
|
10
|
+
"""从文本内容中提取任务条目。
|
|
11
|
+
|
|
12
|
+
返回 list[dict],每个 dict 含 title, description, executor, depends_on, acceptance。
|
|
13
|
+
"""
|
|
14
|
+
if file_type == "markdown":
|
|
15
|
+
return _extract_markdown(content)
|
|
16
|
+
if file_type in ("python", "javascript", "typescript", "go", "rust", "java", "c", "cpp"):
|
|
17
|
+
return _extract_code(content, file_type)
|
|
18
|
+
if file_type in ("json", "yaml", "toml"):
|
|
19
|
+
return _extract_structured(content, file_type)
|
|
20
|
+
return _extract_text(content)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
# ---- Markdown ----
|
|
24
|
+
_MD_HEADING = re.compile(r"^(#{1,6})\s+(.+)$", re.MULTILINE)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _extract_markdown(content: str) -> list[dict]:
|
|
28
|
+
"""按 Markdown 标题层级拆分:每个 ## 标题为一个潜在任务。
|
|
29
|
+
### 子标题作为子描述;# 标题作为模板名。"""
|
|
30
|
+
lines = content.splitlines()
|
|
31
|
+
sections: list[dict] = []
|
|
32
|
+
current: dict | None = None
|
|
33
|
+
current_lines: list[str] = []
|
|
34
|
+
template_name = ""
|
|
35
|
+
|
|
36
|
+
for line in lines:
|
|
37
|
+
m = _MD_HEADING.match(line)
|
|
38
|
+
if m:
|
|
39
|
+
level = len(m.group(1))
|
|
40
|
+
title = m.group(2).strip()
|
|
41
|
+
# 保存前一个 section(即使没有正文也保留标题)
|
|
42
|
+
if current is not None:
|
|
43
|
+
current["description"] = _clean_section_body(current_lines)
|
|
44
|
+
sections.append(current)
|
|
45
|
+
|
|
46
|
+
if level == 1:
|
|
47
|
+
template_name = template_name or title
|
|
48
|
+
current = None
|
|
49
|
+
current_lines = []
|
|
50
|
+
elif level == 2:
|
|
51
|
+
current = {"title": title, "executor": "claude", "depends_on": [], "acceptance": ""}
|
|
52
|
+
current_lines = []
|
|
53
|
+
elif level >= 3 and current is not None:
|
|
54
|
+
# 子标题成为 description 的一部分
|
|
55
|
+
current_lines.append(f"**{title}**")
|
|
56
|
+
else:
|
|
57
|
+
current = None
|
|
58
|
+
current_lines = []
|
|
59
|
+
else:
|
|
60
|
+
if current is not None and line.strip():
|
|
61
|
+
current_lines.append(line.strip())
|
|
62
|
+
|
|
63
|
+
# 最后一个 section(即使没有正文也保留标题)
|
|
64
|
+
if current is not None:
|
|
65
|
+
current["description"] = _clean_section_body(current_lines)
|
|
66
|
+
sections.append(current)
|
|
67
|
+
|
|
68
|
+
# 默认无模板名时从首行取
|
|
69
|
+
if not template_name:
|
|
70
|
+
template_name = (sections[0]["title"] if sections else "未命名模板")
|
|
71
|
+
|
|
72
|
+
# 推断依赖(按出现顺序)
|
|
73
|
+
for i, s in enumerate(sections):
|
|
74
|
+
if i > 0:
|
|
75
|
+
s.setdefault("depends_on", [f"t{i}"]) # 依赖前一个任务
|
|
76
|
+
|
|
77
|
+
return sections
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _clean_section_body(lines: list[str]) -> str:
|
|
81
|
+
text = "\n".join(lines).strip()
|
|
82
|
+
# 限制长度,避免 context 过大
|
|
83
|
+
return text[:2000]
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
# ---- 代码文件 ----
|
|
87
|
+
def _extract_code(content: str, file_type: str) -> list[dict]:
|
|
88
|
+
"""从代码文件中提取函数/类签名和 docstring 作为任务参考。"""
|
|
89
|
+
items: list[dict] = []
|
|
90
|
+
|
|
91
|
+
if file_type == "python":
|
|
92
|
+
items = _extract_python(content)
|
|
93
|
+
elif file_type in ("javascript", "typescript"):
|
|
94
|
+
items = _extract_js_ts(content)
|
|
95
|
+
elif file_type == "go":
|
|
96
|
+
items = _extract_go(content)
|
|
97
|
+
elif file_type == "rust":
|
|
98
|
+
items = _extract_rust(content)
|
|
99
|
+
else:
|
|
100
|
+
items = _extract_generic_code(content)
|
|
101
|
+
|
|
102
|
+
return items
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
_PY_DEF = re.compile(
|
|
106
|
+
r"^\s*(?:(?:async\s+)?def\s+(\w+)\s*\([^)]*\)|class\s+(\w+))",
|
|
107
|
+
re.MULTILINE,
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _extract_python(content: str) -> list[dict]:
|
|
112
|
+
items: list[dict] = []
|
|
113
|
+
for m in _PY_DEF.finditer(content):
|
|
114
|
+
name = m.group(1) or m.group(2)
|
|
115
|
+
kind = "函数" if m.group(1) else "类"
|
|
116
|
+
items.append({
|
|
117
|
+
"title": f"实现/修改 {kind} `{name}`",
|
|
118
|
+
"description": f"处理与 `{name}` 相关的代码逻辑。",
|
|
119
|
+
"executor": "claude",
|
|
120
|
+
"depends_on": [],
|
|
121
|
+
"acceptance": "",
|
|
122
|
+
})
|
|
123
|
+
return items[:30] # 截断,避免模板过大
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
_JS_TS_DEF = re.compile(
|
|
127
|
+
r"^\s*(?:(?:export\s+)?(?:async\s+)?function\s+(\w+)|(?:export\s+)?class\s+(\w+)|(?:const|let|var)\s+(\w+)\s*=\s*(?:async\s*)?\()",
|
|
128
|
+
re.MULTILINE,
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _extract_js_ts(content: str) -> list[dict]:
|
|
133
|
+
items: list[dict] = []
|
|
134
|
+
for m in _JS_TS_DEF.finditer(content):
|
|
135
|
+
name = m.group(1) or m.group(2) or m.group(3)
|
|
136
|
+
kind = "函数" if m.group(1) or m.group(3) else "类"
|
|
137
|
+
items.append({
|
|
138
|
+
"title": f"实现/修改 {kind} `{name}`",
|
|
139
|
+
"description": f"处理与 `{name}` 相关的代码逻辑。",
|
|
140
|
+
"executor": "claude",
|
|
141
|
+
"depends_on": [],
|
|
142
|
+
"acceptance": "",
|
|
143
|
+
})
|
|
144
|
+
return items[:30]
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
_GO_DEF = re.compile(r"^\s*func\s+(?:\(\s*\w+\s+\*?\w+\s*\)\s+)?(\w+)\s*\(", re.MULTILINE)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _extract_go(content: str) -> list[dict]:
|
|
151
|
+
items: list[dict] = []
|
|
152
|
+
for m in _GO_DEF.finditer(content):
|
|
153
|
+
name = m.group(1)
|
|
154
|
+
items.append({
|
|
155
|
+
"title": f"实现/修改函数 `{name}`",
|
|
156
|
+
"description": f"处理与 `{name}` 相关的代码逻辑。",
|
|
157
|
+
"executor": "claude",
|
|
158
|
+
"depends_on": [],
|
|
159
|
+
"acceptance": "",
|
|
160
|
+
})
|
|
161
|
+
return items[:30]
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
_RUST_DEF = re.compile(r"^\s*(?:pub\s+)?(?:async\s+)?fn\s+(\w+)\s*[<(]", re.MULTILINE)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _extract_rust(content: str) -> list[dict]:
|
|
168
|
+
items: list[dict] = []
|
|
169
|
+
for m in _RUST_DEF.finditer(content):
|
|
170
|
+
items.append({
|
|
171
|
+
"title": f"实现/修改函数 `{m.group(1)}`",
|
|
172
|
+
"description": f"处理与 `{m.group(1)}` 相关的代码逻辑。",
|
|
173
|
+
"executor": "claude",
|
|
174
|
+
"depends_on": [],
|
|
175
|
+
"acceptance": "",
|
|
176
|
+
})
|
|
177
|
+
return items[:30]
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def _extract_generic_code(content: str) -> list[dict]:
|
|
181
|
+
# 回退:按空行分段
|
|
182
|
+
return _extract_text(content)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
# ---- 结构化文件 ----
|
|
186
|
+
def _extract_structured(content: str, file_type: str) -> list[dict]:
|
|
187
|
+
"""从 JSON/YAML/TOML 中提取顶层 key 作为任务参考。"""
|
|
188
|
+
try:
|
|
189
|
+
if file_type == "json":
|
|
190
|
+
data = json.loads(content)
|
|
191
|
+
elif file_type == "toml":
|
|
192
|
+
import sys
|
|
193
|
+
if sys.version_info >= (3, 11):
|
|
194
|
+
import tomllib
|
|
195
|
+
data = tomllib.loads(content)
|
|
196
|
+
else:
|
|
197
|
+
return _extract_text(content)
|
|
198
|
+
else: # yaml
|
|
199
|
+
return _extract_text(content)
|
|
200
|
+
except Exception:
|
|
201
|
+
return _extract_text(content)
|
|
202
|
+
|
|
203
|
+
if not isinstance(data, dict):
|
|
204
|
+
return _extract_text(content)
|
|
205
|
+
|
|
206
|
+
items: list[dict] = []
|
|
207
|
+
for key in data:
|
|
208
|
+
items.append({
|
|
209
|
+
"title": f"处理配置项 `{key}`",
|
|
210
|
+
"description": f"根据 {key} 的配置内容进行相应处理。",
|
|
211
|
+
"executor": "claude",
|
|
212
|
+
"depends_on": [],
|
|
213
|
+
"acceptance": "",
|
|
214
|
+
})
|
|
215
|
+
return items[:30]
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
# ---- 纯文本 ----
|
|
219
|
+
_PARAGRAPH = re.compile(r"(.+?)(?:\n\n|$)", re.DOTALL)
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def _extract_text(content: str) -> list[dict]:
|
|
223
|
+
"""按空行分隔的段落作为任务组。"""
|
|
224
|
+
paragraphs: list[str] = []
|
|
225
|
+
for m in _PARAGRAPH.finditer(content):
|
|
226
|
+
text = m.group(1).strip()
|
|
227
|
+
if text and len(text) > 10: # 跳过太短的段落
|
|
228
|
+
paragraphs.append(text)
|
|
229
|
+
|
|
230
|
+
items: list[dict] = []
|
|
231
|
+
for i, para in enumerate(paragraphs[:20]): # 最多 20 个
|
|
232
|
+
# 取首句作为标题
|
|
233
|
+
first_sentence = para.split("。")[0].split(".")[0].strip()[:80]
|
|
234
|
+
items.append({
|
|
235
|
+
"title": first_sentence,
|
|
236
|
+
"description": para[:1500],
|
|
237
|
+
"executor": "claude",
|
|
238
|
+
"depends_on": [],
|
|
239
|
+
"acceptance": "",
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
return items
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"""文件加载:读取任意文本/代码文件,返回内容与文件类型。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
# 支持的文件扩展名 → 类型标记
|
|
9
|
+
EXT_MAP: dict[str, str] = {
|
|
10
|
+
".md": "markdown",
|
|
11
|
+
".markdown": "markdown",
|
|
12
|
+
".txt": "text",
|
|
13
|
+
".rst": "text",
|
|
14
|
+
".py": "python",
|
|
15
|
+
".js": "javascript",
|
|
16
|
+
".ts": "typescript",
|
|
17
|
+
".jsx": "javascript",
|
|
18
|
+
".tsx": "typescript",
|
|
19
|
+
".go": "go",
|
|
20
|
+
".rs": "rust",
|
|
21
|
+
".java": "java",
|
|
22
|
+
".c": "c",
|
|
23
|
+
".cpp": "cpp",
|
|
24
|
+
".h": "c",
|
|
25
|
+
".hpp": "cpp",
|
|
26
|
+
".json": "json",
|
|
27
|
+
".yaml": "yaml",
|
|
28
|
+
".yml": "yaml",
|
|
29
|
+
".toml": "toml",
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def load_file(path: str | Path) -> tuple[str, str]:
|
|
34
|
+
"""读取文件,返回 (content, file_type)。
|
|
35
|
+
|
|
36
|
+
file_type 为 ext_map 中对应的类型标记,未知扩展名返回 "text"。
|
|
37
|
+
"""
|
|
38
|
+
p = Path(path).expanduser().resolve()
|
|
39
|
+
if not p.exists():
|
|
40
|
+
raise FileNotFoundError(f"文件不存在: {p}")
|
|
41
|
+
if not p.is_file():
|
|
42
|
+
raise IsADirectoryError(f"路径是目录: {p}")
|
|
43
|
+
|
|
44
|
+
content = p.read_text(encoding="utf-8", errors="replace")
|
|
45
|
+
ext = p.suffix.lower()
|
|
46
|
+
file_type = EXT_MAP.get(ext, "text")
|
|
47
|
+
|
|
48
|
+
return content, file_type
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def load_files(paths: list[str | Path]) -> list[tuple[str, str, str]]:
|
|
52
|
+
"""批量读取多个文件,返回 [(file_path, content, file_type), ...]。
|
|
53
|
+
跳过目录和二进制文件。"""
|
|
54
|
+
results: list[tuple[str, str, str]] = []
|
|
55
|
+
for path in paths:
|
|
56
|
+
try:
|
|
57
|
+
content, ft = load_file(path)
|
|
58
|
+
results.append((str(path), content, ft))
|
|
59
|
+
except (FileNotFoundError, IsADirectoryError):
|
|
60
|
+
continue
|
|
61
|
+
return results
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def load_directory(dir_path: str | Path, *, glob_pattern: str = "**/*") -> list[tuple[str, str, str]]:
|
|
65
|
+
"""读取目录下所有匹配文件,返回 [(file_path, content, file_type), ...]。
|
|
66
|
+
默认读取所有文件;可通过 glob_pattern 筛选(如 "**/*.md")。"""
|
|
67
|
+
p = Path(dir_path).expanduser().resolve()
|
|
68
|
+
if not p.is_dir():
|
|
69
|
+
raise NotADirectoryError(f"路径不是目录: {p}")
|
|
70
|
+
|
|
71
|
+
results: list[tuple[str, str, str]] = []
|
|
72
|
+
for f in sorted(p.glob(glob_pattern)):
|
|
73
|
+
if not f.is_file():
|
|
74
|
+
continue
|
|
75
|
+
ext = f.suffix.lower()
|
|
76
|
+
if ext in _SKIP_EXTENSIONS:
|
|
77
|
+
continue
|
|
78
|
+
try:
|
|
79
|
+
content = f.read_text(encoding="utf-8", errors="replace")
|
|
80
|
+
file_type = EXT_MAP.get(ext, "text")
|
|
81
|
+
results.append((str(f), content, file_type))
|
|
82
|
+
except Exception:
|
|
83
|
+
continue
|
|
84
|
+
return results
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
_SKIP_EXTENSIONS: set[str] = {
|
|
88
|
+
".png", ".jpg", ".jpeg", ".gif", ".bmp", ".ico", ".svg",
|
|
89
|
+
".woff", ".woff2", ".ttf", ".eot",
|
|
90
|
+
".zip", ".tar", ".gz", ".bz2", ".7z",
|
|
91
|
+
".exe", ".dll", ".so", ".dylib",
|
|
92
|
+
".pyc", ".pyo", ".class",
|
|
93
|
+
".pdf", ".doc", ".docx", ".xls", ".xlsx",
|
|
94
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"""模板数据模型。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass
|
|
9
|
+
class SuggestedTask:
|
|
10
|
+
"""一个建议的子任务条目。"""
|
|
11
|
+
|
|
12
|
+
title: str
|
|
13
|
+
description: str
|
|
14
|
+
executor: str = "claude" # claude | codex
|
|
15
|
+
depends_on: list[str] = field(default_factory=list)
|
|
16
|
+
acceptance: str = ""
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class TaskTemplate:
|
|
21
|
+
"""从文件中提取出的任务拆解模板。
|
|
22
|
+
|
|
23
|
+
可直接注入 tasker planner 的 SYSTEM_PROMPT 作为拆解参考。
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
template_name: str = ""
|
|
27
|
+
source_file: str = ""
|
|
28
|
+
system_prompt_extension: str = "" # 追加到 planner SYSTEM_PROMPT 的上下文
|
|
29
|
+
suggested_tasks: list[SuggestedTask] = field(default_factory=list)
|
|
30
|
+
|
|
31
|
+
def to_dict(self) -> dict:
|
|
32
|
+
return {
|
|
33
|
+
"template_name": self.template_name,
|
|
34
|
+
"source_file": self.source_file,
|
|
35
|
+
"system_prompt_extension": self.system_prompt_extension,
|
|
36
|
+
"suggested_tasks": [
|
|
37
|
+
{
|
|
38
|
+
"title": t.title,
|
|
39
|
+
"description": t.description,
|
|
40
|
+
"executor": t.executor,
|
|
41
|
+
"depends_on": t.depends_on,
|
|
42
|
+
"acceptance": t.acceptance,
|
|
43
|
+
}
|
|
44
|
+
for t in self.suggested_tasks
|
|
45
|
+
],
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
def to_planner_json(self) -> dict:
|
|
49
|
+
"""输出与 tasker planner 兼容的 JSON 结构。"""
|
|
50
|
+
return {
|
|
51
|
+
"objective": self.template_name,
|
|
52
|
+
"rationale": self.system_prompt_extension,
|
|
53
|
+
"tasks": [
|
|
54
|
+
{
|
|
55
|
+
"id": f"t{i+1}",
|
|
56
|
+
"title": t.title,
|
|
57
|
+
"description": t.description,
|
|
58
|
+
"executor": t.executor,
|
|
59
|
+
"depends_on": t.depends_on,
|
|
60
|
+
"acceptance": t.acceptance,
|
|
61
|
+
}
|
|
62
|
+
for i, t in enumerate(self.suggested_tasks)
|
|
63
|
+
],
|
|
64
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"""模板本地存储。
|
|
2
|
+
|
|
3
|
+
模板文件存放在 ~/.tasker/templates/ 目录,跨平台兼容(macOS / Linux / Windows)。
|
|
4
|
+
|
|
5
|
+
文件格式:单 JSON 文件,_meta 作为第一个 key 携带元信息:
|
|
6
|
+
{
|
|
7
|
+
"_meta": {"v":1,"name":"...","source":"...","tags":[...],"desc":"...","stored_at":"..."},
|
|
8
|
+
"template_name": "...",
|
|
9
|
+
"system_prompt_extension": "...",
|
|
10
|
+
"suggested_tasks": [...]
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
LLM 通过读取 _meta 中的 tags/desc/name 来决定调用哪个模板。
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import json
|
|
19
|
+
import os
|
|
20
|
+
from datetime import datetime, timezone
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _store_dir() -> Path:
|
|
25
|
+
"""获取模板存储目录(创建若不存在)。"""
|
|
26
|
+
p = Path.home() / ".tasker" / "templates"
|
|
27
|
+
p.mkdir(parents=True, exist_ok=True)
|
|
28
|
+
return p
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _safe_filename(name: str) -> str:
|
|
32
|
+
"""将模板名转为安全的文件名(仅保留字母数字和连字符)。"""
|
|
33
|
+
import re
|
|
34
|
+
|
|
35
|
+
safe = re.sub(r"[^\w\-]", "-", name).strip("-").lower()
|
|
36
|
+
return safe or "template"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class Store:
|
|
40
|
+
"""模板本地存储。"""
|
|
41
|
+
|
|
42
|
+
def __init__(self, store_dir: str | Path | None = None):
|
|
43
|
+
self._dir = Path(store_dir) if store_dir else _store_dir()
|
|
44
|
+
self._dir.mkdir(parents=True, exist_ok=True)
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def dir(self) -> Path:
|
|
48
|
+
return self._dir
|
|
49
|
+
|
|
50
|
+
# ---- 写入 ----
|
|
51
|
+
def add(
|
|
52
|
+
self,
|
|
53
|
+
template: dict,
|
|
54
|
+
*,
|
|
55
|
+
name: str = "",
|
|
56
|
+
source: str = "",
|
|
57
|
+
tags: list[str] | None = None,
|
|
58
|
+
description: str = "",
|
|
59
|
+
) -> Path:
|
|
60
|
+
"""存储一个模板,返回文件路径。
|
|
61
|
+
|
|
62
|
+
template 是 to_dict() 输出的 dict。
|
|
63
|
+
name/source/tags/description 用于构造 _meta。
|
|
64
|
+
"""
|
|
65
|
+
name = name or template.get("template_name", "unnamed")
|
|
66
|
+
tpl = dict(template) # shallow copy
|
|
67
|
+
|
|
68
|
+
# 构造 _meta,始终放在最前面
|
|
69
|
+
meta: dict = {
|
|
70
|
+
"v": 1,
|
|
71
|
+
"name": name,
|
|
72
|
+
"source": source or template.get("source_file", ""),
|
|
73
|
+
"tags": sorted(set(tags or [])),
|
|
74
|
+
"desc": (description or "").strip()[:200],
|
|
75
|
+
"stored_at": datetime.now(timezone.utc).isoformat(),
|
|
76
|
+
}
|
|
77
|
+
# _meta 放在第一层 key 的最前面
|
|
78
|
+
out = {"_meta": meta}
|
|
79
|
+
out.update(tpl)
|
|
80
|
+
|
|
81
|
+
filename = _safe_filename(name) + ".json"
|
|
82
|
+
fpath = self._dir / filename
|
|
83
|
+
fpath.write_text(
|
|
84
|
+
json.dumps(out, ensure_ascii=False, indent=2), encoding="utf-8"
|
|
85
|
+
)
|
|
86
|
+
return fpath
|
|
87
|
+
|
|
88
|
+
def add_file(self, file_path: str | Path, *, tags: list[str] | None = None, description: str = "") -> Path:
|
|
89
|
+
"""直接从项目文件路径提取并存储模板。"""
|
|
90
|
+
from . import extract_template
|
|
91
|
+
|
|
92
|
+
tpl = extract_template(str(file_path))
|
|
93
|
+
name = tpl.template_name
|
|
94
|
+
source = str(Path(file_path).resolve())
|
|
95
|
+
# 优先用传入的 description,否则用任务数量作为摘要
|
|
96
|
+
desc = description or (f"{len(tpl.suggested_tasks)} 个子任务" if tpl.suggested_tasks else "")
|
|
97
|
+
return self.add(
|
|
98
|
+
tpl.to_dict(), name=name, source=source, tags=tags, description=desc,
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
# ---- 读取 ----
|
|
102
|
+
def list(self) -> list[dict]:
|
|
103
|
+
"""列出所有已存储模板的 _meta 信息(只读第一层元数据)。"""
|
|
104
|
+
result: list[dict] = []
|
|
105
|
+
for f in sorted(self._dir.glob("*.json")):
|
|
106
|
+
meta = self._read_meta(f)
|
|
107
|
+
if meta:
|
|
108
|
+
result.append(meta)
|
|
109
|
+
return result
|
|
110
|
+
|
|
111
|
+
def get(self, name: str) -> dict | None:
|
|
112
|
+
"""按名称读取完整模板。"""
|
|
113
|
+
fname = _safe_filename(name) + ".json"
|
|
114
|
+
fpath = self._dir / fname
|
|
115
|
+
if not fpath.exists():
|
|
116
|
+
return None
|
|
117
|
+
try:
|
|
118
|
+
return json.loads(fpath.read_text(encoding="utf-8"))
|
|
119
|
+
except (json.JSONDecodeError, OSError):
|
|
120
|
+
return None
|
|
121
|
+
|
|
122
|
+
def remove(self, name: str) -> bool:
|
|
123
|
+
"""删除一个模板。"""
|
|
124
|
+
fname = _safe_filename(name) + ".json"
|
|
125
|
+
fpath = self._dir / fname
|
|
126
|
+
if fpath.exists():
|
|
127
|
+
fpath.unlink()
|
|
128
|
+
return True
|
|
129
|
+
return False
|
|
130
|
+
|
|
131
|
+
def search(self, *, tags: list[str] | None = None, keyword: str = "") -> list[dict]:
|
|
132
|
+
"""按 tags 或关键词搜索模板,返回匹配的 _meta 列表。"""
|
|
133
|
+
all_meta = self.list()
|
|
134
|
+
if not tags and not keyword:
|
|
135
|
+
return all_meta
|
|
136
|
+
result: list[dict] = []
|
|
137
|
+
for meta in all_meta:
|
|
138
|
+
if tags and not any(t in (meta.get("tags") or []) for t in tags):
|
|
139
|
+
continue
|
|
140
|
+
if keyword:
|
|
141
|
+
kw = keyword.lower()
|
|
142
|
+
hay = json.dumps(meta, ensure_ascii=False).lower()
|
|
143
|
+
if kw not in hay:
|
|
144
|
+
continue
|
|
145
|
+
result.append(meta)
|
|
146
|
+
return result
|
|
147
|
+
|
|
148
|
+
def catalog_text(self) -> str:
|
|
149
|
+
"""生成供 LLM 使用的模板目录摘要文本。"""
|
|
150
|
+
items = self.list()
|
|
151
|
+
if not items:
|
|
152
|
+
return "(无已存储的模板。用 `template store <file>` 存入模板。)"
|
|
153
|
+
lines = [f"共 {len(items)} 个模板可用:"]
|
|
154
|
+
for m in items:
|
|
155
|
+
tags = ", ".join(m.get("tags") or [])
|
|
156
|
+
name = m.get("name", "?")
|
|
157
|
+
desc = (m.get("desc") or "")[:80]
|
|
158
|
+
line = f" - **{name}**"
|
|
159
|
+
if tags:
|
|
160
|
+
line += f" [{tags}]"
|
|
161
|
+
if desc:
|
|
162
|
+
line += f" {desc}"
|
|
163
|
+
lines.append(line)
|
|
164
|
+
return "\n".join(lines)
|
|
165
|
+
|
|
166
|
+
# ---- 内部 ----
|
|
167
|
+
@staticmethod
|
|
168
|
+
def _read_meta(fpath: Path) -> dict | None:
|
|
169
|
+
"""快速读取文件第一层 _meta(不解析完整 JSON 的情况下)。
|
|
170
|
+
|
|
171
|
+
策略:读前几 KB,用简单正则提取 _meta 对象。
|
|
172
|
+
如果文件很大,这个方法远快于完整 JSON 解析。
|
|
173
|
+
"""
|
|
174
|
+
try:
|
|
175
|
+
head = fpath.read_text(encoding="utf-8")[:4096]
|
|
176
|
+
# 尝试完整 JSON 解析(文件通常 < 100KB,开销可接受)
|
|
177
|
+
data = json.loads(fpath.read_text(encoding="utf-8"))
|
|
178
|
+
return data.get("_meta")
|
|
179
|
+
except (json.JSONDecodeError, OSError):
|
|
180
|
+
return None
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
# 模块级便捷函数
|
|
184
|
+
_default_store: Store | None = None
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _get_store() -> Store:
|
|
188
|
+
global _default_store
|
|
189
|
+
if _default_store is None:
|
|
190
|
+
_default_store = Store()
|
|
191
|
+
return _default_store
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def store_template(file_path: str, *, tags: list[str] | None = None) -> Path:
|
|
195
|
+
"""从文件提取并存储模板。"""
|
|
196
|
+
return _get_store().add_file(file_path, tags=tags)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def list_templates() -> list[dict]:
|
|
200
|
+
"""列出所有已存储模板的元信息。"""
|
|
201
|
+
return _get_store().list()
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def search_templates(tags: list[str] | None = None, keyword: str = "") -> list[dict]:
|
|
205
|
+
"""搜索模板。"""
|
|
206
|
+
return _get_store().search(tags=tags, keyword=keyword)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def get_template(name: str) -> dict | None:
|
|
210
|
+
"""获取模板完整内容。"""
|
|
211
|
+
return _get_store().get(name)
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def remove_template(name: str) -> bool:
|
|
215
|
+
"""删除模板。"""
|
|
216
|
+
return _get_store().remove(name)
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def catalog_for_llm() -> str:
|
|
220
|
+
"""生成供 LLM 决策的模板目录摘要。"""
|
|
221
|
+
return _get_store().catalog_text()
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tp-wy
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: 从项目文件(PRD / 规格 / README 等)中提取任务拆解模板,供 tasker 多智能体编排器的 LLM 拆分阶段使用
|
|
5
|
+
License: MIT License
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2026
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
26
|
+
|
|
27
|
+
Requires-Python: >=3.9
|
|
28
|
+
License-File: LICENSE
|
|
29
|
+
Dynamic: license-file
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
template/__init__.py
|
|
5
|
+
template/__main__.py
|
|
6
|
+
template/cli.py
|
|
7
|
+
template/converter.py
|
|
8
|
+
template/extractor.py
|
|
9
|
+
template/loader.py
|
|
10
|
+
template/models.py
|
|
11
|
+
template/store.py
|
|
12
|
+
tp_wy.egg-info/PKG-INFO
|
|
13
|
+
tp_wy.egg-info/SOURCES.txt
|
|
14
|
+
tp_wy.egg-info/dependency_links.txt
|
|
15
|
+
tp_wy.egg-info/entry_points.txt
|
|
16
|
+
tp_wy.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
template
|