release: Евномия, новая версия, линейка юристов
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
"""Заглушка `claude -p` для тестов Фемиды Next: без сети и без живого claude.
|
||||
|
||||
Журнал вызовов — $FAKE_CLAUDE_LOG, по строке JSON на вызов: argv, stdin, cwd, role.
|
||||
Исходы по ролям — $FAKE_CLAUDE_PLAN: {роль: [исход, ...]}, остаток хранится в том же файле.
|
||||
Роль — первая строка --system-prompt вида «РОЛЬ: <роль>».
|
||||
|
||||
Исход:
|
||||
{"structured_output": {...}} успех, JSON как у настоящего claude -p --output-format json
|
||||
{"sleep": N, ...} пауза N секунд перед исходом (для таймаута)
|
||||
{"code": N, "stdout": "..."} произвольный вывод и код выхода
|
||||
"529" API Error: 529 overloaded, код 1
|
||||
"garbage" не JSON, код 0
|
||||
Блокировки flock на плане и журнале сохраняют порядок при вызовах из потоков.
|
||||
"""
|
||||
import fcntl
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
|
||||
def take(role: str) -> object:
|
||||
"""Достает следующий исход роли и сохраняет остаток плана."""
|
||||
with open(os.environ["FAKE_CLAUDE_PLAN"], "r+", encoding="utf-8") as file:
|
||||
fcntl.flock(file, fcntl.LOCK_EX)
|
||||
plan = json.load(file)
|
||||
queue = plan.get(role) or []
|
||||
outcome = queue.pop(0) if queue else None
|
||||
file.seek(0)
|
||||
file.truncate()
|
||||
json.dump(plan, file, ensure_ascii=False)
|
||||
return outcome
|
||||
|
||||
|
||||
def main() -> int:
|
||||
argv = sys.argv[1:]
|
||||
stdin = sys.stdin.buffer.read().decode("utf-8")
|
||||
role = argv[argv.index("--system-prompt") + 1].partition("\n")[0].removeprefix("РОЛЬ: ")
|
||||
with open(os.environ["FAKE_CLAUDE_LOG"], "a", encoding="utf-8") as file:
|
||||
fcntl.flock(file, fcntl.LOCK_EX)
|
||||
file.write(json.dumps({"argv": argv, "stdin": stdin, "cwd": os.getcwd(), "role": role}, ensure_ascii=False) + "\n")
|
||||
outcome = take(role)
|
||||
if outcome is None:
|
||||
print(f"заглушка: в плане нет исхода для роли {role}", file=sys.stderr)
|
||||
return 3
|
||||
if outcome == "529":
|
||||
print("API Error: 529 overloaded")
|
||||
return 1
|
||||
if outcome == "garbage":
|
||||
print("это не JSON")
|
||||
return 0
|
||||
time.sleep(outcome.get("sleep", 0))
|
||||
if "code" in outcome:
|
||||
print(outcome["stdout"])
|
||||
return outcome["code"]
|
||||
reply = {"type": "result", "is_error": False, "structured_output": outcome["structured_output"],
|
||||
"usage": outcome.get("usage", {"input_tokens": 1, "output_tokens": 1})}
|
||||
print(json.dumps(reply))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user