Files
2026-09-24 10:40:57 +08:00

138 lines
4.4 KiB
Python
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""CADmind 交互式终端入口。
启动后一直停在终端里,直接输入零件描述,自动调用大模型分析、生成、校验,
结果落在 output/<零件名>/ 下。不需要每次都手动敲脚本命令。
用法:
python3 cadmind.py
(或者用 ./start.sh,会自动激活虚拟环境再启动这个脚本)
交互命令:
<直接描述零件> 生成一个新零件
改 <零件名> <修改要求> 在已生成的零件基础上改参数
ls 列出 output/ 下已有的零件
help 显示帮助
exit / quit / 退出 退出
"""
import sys
from pathlib import Path
from dotenv import load_dotenv
BASE_DIR = Path(__file__).resolve().parent
load_dotenv(BASE_DIR / ".env")
sys.path.insert(0, str(BASE_DIR))
from engine.pipeline import PipelineError, generate_from_description_best, modify_existing_part # noqa: E402
from engine.result_status import print_report # noqa: E402
BANNER = """\
========================================
CADmind - 自然语言生成机械零件
========================================
直接描述你要的零件,描述越详细做出来越准。
特殊命令:
ls —— 列出已生成的零件
help —— 显示这条帮助
exit / quit / 退出 —— 退出
修改已有零件: 直接在话里提到零件名就行(不用固定格式),比如"把v_guide_block_01的孔改到四角"、
"v_guide_block_01 改一下孔径"都可以,零件名可以用 ls 命令查看。
----------------------------------------
"""
EXIT_WORDS = {"exit", "quit", "退出", "q"}
def _existing_part_names():
out_dir = BASE_DIR / "output"
if not out_dir.exists():
return []
return sorted(p.name for p in out_dir.iterdir() if p.is_dir() and (p / "report.json").exists())
def list_parts():
names = _existing_part_names()
if not names:
print("(还没有生成过任何零件)")
return
for n in names:
print(f" - {n}")
def _detect_modify_target(user_input: str):
"""判断这句话是不是在改一个已有零件——不靠死板的前缀匹配(比如非要写"改 "两个字开头),
而是看用户的话里有没有提到一个已存在的零件名。之前"修改一下xxx"因为没写成"修改 xxx"
这种严格格式,被误判成了全新生成请求,GPT根本不知道要改的是哪个零件,瞎编了一个不相关
的结果——这就是命令行解析太脆弱导致的,不能再靠猜用户会怎么措辞。
"""
matches = [name for name in _existing_part_names() if name in user_input]
if not matches:
return None
# 有的零件名会互相包含(比如 xxx 和 xxx_modified),取最长的那个,避免匹配到子串零件
return max(matches, key=len)
def _run_modify(part_name: str, change_request: str):
print(f"[修改] 零件: {part_name}")
print(f"[修改] 要求: {change_request}")
try:
report = modify_existing_part(part_name, change_request, BASE_DIR)
except PipelineError as e:
print(f"[失败] {e}")
return
print_report(report)
def handle(user_input: str):
# 显式格式("改 xxx 要求" / "修改 xxx 要求")优先识别,其余情况靠零件名匹配
if user_input.startswith("改 ") or user_input.startswith("修改 "):
rest = user_input.split(" ", 1)[1].strip()
parts = rest.split(" ", 1)
if len(parts) >= 2:
_run_modify(parts[0], parts[1])
return
target = _detect_modify_target(user_input)
if target:
_run_modify(target, user_input)
return
try:
report = generate_from_description_best(user_input, BASE_DIR)
except PipelineError as e:
print(f"[失败] {e}")
return
print_report(report)
def main():
print(BANNER)
while True:
try:
user_input = input("你: ").strip()
except (EOFError, KeyboardInterrupt):
print("\n再见")
break
if not user_input:
continue
if user_input.lower() in EXIT_WORDS:
print("再见")
break
if user_input.lower() == "help":
print(BANNER)
continue
if user_input.lower() == "ls":
list_parts()
print()
continue
handle(user_input)
print()
if __name__ == "__main__":
main()