Files
vibemake-pcb-studio/backend/easyeda_backend/design_runtime/api_contract.py
T
2026-11-10 19:04:27 +08:00

32 lines
1.3 KiB
Python

"""Reject invented EDA methods before spending an editor transaction on them."""
import re
from functools import lru_cache
from pathlib import Path
@lru_cache(maxsize=1)
def signatures():
root = Path(__file__).resolve().parents[1] / 'skills/easyeda-api/references/classes'
result = {}
for path in root.glob('*.md'):
text = path.read_text(encoding='utf-8')
methods = {name: '' for name in re.findall(r'function\s+(\w+)\s*(?:<[^\n]*?>)?\s*\(', text)}
result[path.stem.lower()] = methods
return result
def validate_api(code):
catalog = signatures()
errors = []
for cls, method in re.findall(r'\beda\.(\w+)\.(\w+)\s*\(', code):
known = catalog.get(cls.lower())
if known is not None and method not in known:
alternatives = ', '.join(sorted(known)[:30])
errors.append(f'{cls}.{method} is not documented. Available: {alternatives}')
# These particular hallucinations repeatedly consumed whole design rounds.
for name in ('getState_Device', 'getState_MPN', 'createNetLabel'):
if re.search(r'\.' + name + r'\s*\(', code):
errors.append(f'{name} is unsupported; use netlist props for metadata and named wires for nets')
if errors:
raise ValueError('; '.join(dict.fromkeys(errors))[:1800])