|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import types |
| 4 | +import typing |
| 5 | +from enum import Enum |
| 6 | +from typing import TYPE_CHECKING, Any, get_args, get_origin |
| 7 | + |
| 8 | +from pydantic import BaseModel |
| 9 | + |
| 10 | +if TYPE_CHECKING: |
| 11 | + from agentex.lib.sdk.state_machine.state import State |
| 12 | + |
| 13 | + |
| 14 | +class LifecycleState(BaseModel): |
| 15 | + name: str |
| 16 | + description: str = "" |
| 17 | + waits_for_input: bool = False |
| 18 | + accepts: list[str] = [] |
| 19 | + transitions: list[str] = [] |
| 20 | + |
| 21 | + |
| 22 | +class AgentLifecycle(BaseModel): |
| 23 | + states: list[LifecycleState] |
| 24 | + initial_state: str |
| 25 | + queries: list[str] = [] |
| 26 | + |
| 27 | + |
| 28 | +class AgentCard(BaseModel): |
| 29 | + protocol: str = "acp" |
| 30 | + lifecycle: AgentLifecycle | None = None |
| 31 | + data_events: list[str] = [] |
| 32 | + input_types: list[str] = [] |
| 33 | + output_schema: dict | None = None |
| 34 | + |
| 35 | + @classmethod |
| 36 | + def from_states( |
| 37 | + cls, |
| 38 | + initial_state: str | Enum, |
| 39 | + states: list[State], |
| 40 | + output_event_model: type[BaseModel] | None = None, |
| 41 | + extra_input_types: list[str] | None = None, |
| 42 | + queries: list[str] | None = None, |
| 43 | + ) -> AgentCard: |
| 44 | + """Build an AgentCard directly from a list[State] + initial_state. |
| 45 | +
|
| 46 | + Agents can share their `states` list between the StateMachine and acp.py |
| 47 | + without constructing a temporary StateMachine instance. |
| 48 | + """ |
| 49 | + lifecycle_states = [ |
| 50 | + LifecycleState( |
| 51 | + name=state.name, |
| 52 | + description=state.workflow.description, |
| 53 | + waits_for_input=state.workflow.waits_for_input, |
| 54 | + accepts=list(state.workflow.accepts), |
| 55 | + transitions=[ |
| 56 | + t.value if isinstance(t, Enum) else str(t) |
| 57 | + for t in state.workflow.transitions |
| 58 | + ], |
| 59 | + ) |
| 60 | + for state in states |
| 61 | + ] |
| 62 | + |
| 63 | + initial = initial_state.value if isinstance(initial_state, Enum) else initial_state |
| 64 | + |
| 65 | + data_events: list[str] = [] |
| 66 | + output_schema: dict | None = None |
| 67 | + if output_event_model: |
| 68 | + data_events = extract_literal_values(output_event_model, "type") |
| 69 | + output_schema = output_event_model.model_json_schema() |
| 70 | + |
| 71 | + derived_input_types: set[str] = set() |
| 72 | + for ls in lifecycle_states: |
| 73 | + derived_input_types.update(ls.accepts) |
| 74 | + |
| 75 | + return cls( |
| 76 | + lifecycle=AgentLifecycle( |
| 77 | + states=lifecycle_states, |
| 78 | + initial_state=initial, |
| 79 | + queries=queries or [], |
| 80 | + ), |
| 81 | + data_events=data_events, |
| 82 | + input_types=sorted(derived_input_types | set(extra_input_types or [])), |
| 83 | + output_schema=output_schema, |
| 84 | + ) |
| 85 | + |
| 86 | + @classmethod |
| 87 | + def from_state_machine( |
| 88 | + cls, |
| 89 | + state_machine: Any, |
| 90 | + output_event_model: type[BaseModel] | None = None, |
| 91 | + extra_input_types: list[str] | None = None, |
| 92 | + queries: list[str] | None = None, |
| 93 | + ) -> AgentCard: |
| 94 | + """Build an AgentCard from a StateMachine instance. Delegates to from_states().""" |
| 95 | + lifecycle = state_machine.get_lifecycle() |
| 96 | + states_data = lifecycle["states"] |
| 97 | + initial = lifecycle["initial_state"] |
| 98 | + |
| 99 | + # Reconstruct lightweight State-like objects from the lifecycle dict |
| 100 | + # so we can reuse from_states logic via the dict path |
| 101 | + data_events: list[str] = [] |
| 102 | + output_schema: dict | None = None |
| 103 | + if output_event_model: |
| 104 | + data_events = extract_literal_values(output_event_model, "type") |
| 105 | + output_schema = output_event_model.model_json_schema() |
| 106 | + |
| 107 | + derived_input_types: set[str] = set() |
| 108 | + lifecycle_states = [] |
| 109 | + for s in states_data: |
| 110 | + derived_input_types.update(s.get("accepts", [])) |
| 111 | + lifecycle_states.append(LifecycleState( |
| 112 | + name=s["name"], |
| 113 | + description=s.get("description", ""), |
| 114 | + waits_for_input=s.get("waits_for_input", False), |
| 115 | + accepts=s.get("accepts", []), |
| 116 | + transitions=s.get("transitions", []), |
| 117 | + )) |
| 118 | + |
| 119 | + return cls( |
| 120 | + lifecycle=AgentLifecycle( |
| 121 | + states=lifecycle_states, |
| 122 | + initial_state=initial, |
| 123 | + queries=queries or [], |
| 124 | + ), |
| 125 | + data_events=data_events, |
| 126 | + input_types=sorted(derived_input_types | set(extra_input_types or [])), |
| 127 | + output_schema=output_schema, |
| 128 | + ) |
| 129 | + |
| 130 | + |
| 131 | +def extract_literal_values(model: type[BaseModel], field: str) -> list[str]: |
| 132 | + """Extract allowed values from a Literal[...] type annotation on a Pydantic model field.""" |
| 133 | + field_info = model.model_fields.get(field) |
| 134 | + if field_info is None: |
| 135 | + return [] |
| 136 | + |
| 137 | + annotation = field_info.annotation |
| 138 | + if annotation is None: |
| 139 | + return [] |
| 140 | + |
| 141 | + # Unwrap Optional (Union[X, None] or PEP 604 X | None) to get the inner type |
| 142 | + if get_origin(annotation) is typing.Union or isinstance(annotation, types.UnionType): |
| 143 | + args = [a for a in get_args(annotation) if a is not type(None)] |
| 144 | + annotation = args[0] if len(args) == 1 else annotation |
| 145 | + |
| 146 | + if get_origin(annotation) is typing.Literal: |
| 147 | + return list(get_args(annotation)) |
| 148 | + |
| 149 | + return [] |
0 commit comments