Repository navigation
Expand file tree
/
Copy pathmaterial_workflow.py
More file actions
70 lines (62 loc) · 2.02 KB
/
Copy pathmaterial_workflow.py
File metadata and controls
70 lines (62 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from typing import Any, Dict, List, Tuple
from sqlalchemy.ext.asyncio import AsyncSession
from app.agent.planner import build_material_writing_plan
from app.core.config import settings
from app.models.entities import AgentRun, AgentStep, Conversation
async def start_material_run(
session: AsyncSession,
conversation: Conversation,
goal: str,
goal_spec: Dict[str, Any],
) -> Tuple[AgentRun, List[AgentStep]]:
plan = build_material_writing_plan(goal_spec)
run = AgentRun(
owner_id=settings.local_owner_id,
conversation_id=conversation.id,
skill_name="material-writing",
skill_version="0.1.0",
goal=goal,
plan=plan,
context_snapshot={"goal_spec": goal_spec},
status="running",
)
session.add(run)
await session.flush()
steps = []
for position, item in enumerate(plan):
step = AgentStep(
owner_id=settings.local_owner_id,
run_id=run.id,
position=position,
name=item["name"],
expected_output=item["expected_output"],
checkpoint={
"plan_id": item["id"],
"step_type": item["step_type"],
"acceptance_criteria": item["acceptance_criteria"],
},
)
session.add(step)
steps.append(step)
await session.flush()
return run, steps
def set_material_step(
steps: List[AgentStep],
step_type: str,
status: str,
result: Dict[str, Any],
) -> None:
step = next(
item
for item in steps
if (item.checkpoint or {}).get("step_type") == step_type
)
step.status = status
step.result = result
step.checkpoint = {**(step.checkpoint or {}), "last_status": status}
def sync_material_plan(run: AgentRun, steps: List[AgentStep]) -> None:
statuses = {item.position: item.status for item in steps}
run.plan = [
{**item, "status": statuses.get(index, item.get("status", "pending"))}
for index, item in enumerate(run.plan or [])
]