112 lines
3.7 KiB
Python
112 lines
3.7 KiB
Python
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from estimator.agent import EstimationPipeline, PromptStore
|
|
from estimator.agent.schemas import (
|
|
Adjustment,
|
|
ClarifyOutput,
|
|
CriticOutput,
|
|
DecomposeOutput,
|
|
EstimateOutput,
|
|
MissingTask,
|
|
Question,
|
|
TaskHours,
|
|
WbsTask,
|
|
)
|
|
from estimator.config.sections import AgentCfg
|
|
from estimator.domain.enums import Role, Segment
|
|
from estimator.domain.errors import LlmTransientError
|
|
from estimator.domain.llm import LlmResult, Usage
|
|
|
|
ROOT = Path(__file__).resolve().parents[3]
|
|
|
|
|
|
class FakeGateway:
|
|
"""Scripted LlmGateway: a queue of outputs (or exceptions) per step, calls recorded."""
|
|
|
|
def __init__(self, **script):
|
|
self.script = {k: list(v) for k, v in script.items()}
|
|
self.calls: list[dict] = []
|
|
|
|
async def structured(self, step, *, system, user, schema):
|
|
self.calls.append({"step": step, "system": system, "user": user, "schema": schema})
|
|
item = self.script[step].pop(0)
|
|
if isinstance(item, Exception):
|
|
raise item
|
|
assert isinstance(item, schema), f"{step}: scripted {type(item)} but pipeline asked for {schema}"
|
|
return LlmResult(item, Usage(100, 50), "fake", f"fake-{step}")
|
|
|
|
def steps(self):
|
|
return [c["step"] for c in self.calls]
|
|
|
|
|
|
def clarify_out(questions=(), **kw):
|
|
base = {
|
|
"summary": "Бот в Telegram, MAX и VK с ИИ и интеграцией amoCRM.",
|
|
"segment": Segment.MULTICHANNEL_AI,
|
|
"channels": ["telegram", "max", "vk"],
|
|
"has_ai": True,
|
|
"integrations": ["amocrm"],
|
|
"questions": list(questions),
|
|
}
|
|
base.update(kw)
|
|
return ClarifyOutput(**base)
|
|
|
|
|
|
def q(id, text="Вопрос?", blocking=False):
|
|
return Question(id=id, text=text, why="влияет на объём", blocking=blocking)
|
|
|
|
|
|
# The worked example from docs/bot-estimation-market-data.md, section 7
|
|
DOC_WBS = [
|
|
("Аналитика, ТЗ", Role.ANALYST, 8, 12, 20),
|
|
("Ядро бота (Telegram)", Role.BACKEND, 16, 24, 40),
|
|
("RAG и база знаний", Role.BACKEND, 20, 32, 56),
|
|
("Интеграция amoCRM", Role.BACKEND, 8, 16, 32),
|
|
("Адаптер MAX", Role.BACKEND, 8, 12, 20),
|
|
("Адаптер ВКонтакте", Role.BACKEND, 8, 12, 20),
|
|
("Тестирование", Role.QA, 12, 20, 36),
|
|
("Деплой и мониторинг", Role.DEVOPS, 4, 8, 16),
|
|
]
|
|
|
|
|
|
def decompose_out(rows=DOC_WBS):
|
|
return DecomposeOutput(tasks=[WbsTask(title=t, description=f"{t}: описание", role=r) for t, r, *_ in rows])
|
|
|
|
|
|
def estimate_out(rows=DOC_WBS, ids=None):
|
|
ids = ids or [f"t{i}" for i in range(1, len(rows) + 1)]
|
|
return EstimateOutput(
|
|
tasks=[
|
|
TaskHours(task_id=i, optimistic=o, likely=m, pessimistic=p, assumptions=["a"], risks=[f"риск {i}"])
|
|
for i, (_, _, o, m, p) in zip(ids, rows, strict=True)
|
|
]
|
|
)
|
|
|
|
|
|
def critic_out(missing=(), adjustments=(), concerns=()):
|
|
return CriticOutput(missing_tasks=list(missing), adjustments=list(adjustments), concerns=list(concerns))
|
|
|
|
|
|
def missing(title="Документация", role=Role.ANALYST, o=4, m=6, p=10):
|
|
return MissingTask(title=title, description="d", role=role, optimistic=o, likely=m, pessimistic=p, rationale="нужна")
|
|
|
|
|
|
def adjust(task_id, o, m, p, reason="занижено"):
|
|
return Adjustment(task_id=task_id, optimistic=o, likely=m, pessimistic=p, reason=reason)
|
|
|
|
|
|
@pytest.fixture
|
|
def prompts() -> PromptStore:
|
|
return PromptStore(ROOT / "prompts")
|
|
|
|
|
|
def make_pipeline(gateway, prompts, retriever=None, reference_k=3, **cfg):
|
|
return EstimationPipeline(
|
|
gateway, AgentCfg(**cfg), prompts, retriever=retriever, reference_k=reference_k, semantic_retries=1
|
|
)
|
|
|
|
|
|
TRANSIENT = LlmTransientError("down", provider="fake")
|