Spaces:
Sleeping
Sleeping
File size: 7,980 Bytes
a1e4405 9e95393 a1e4405 9e95393 a1e4405 9e95393 a1e4405 9e95393 a1e4405 9e95393 a1e4405 9e95393 a1e4405 9e95393 a1e4405 9e95393 a1e4405 9e95393 a1e4405 9e95393 a1e4405 9e95393 a1e4405 9e95393 a1e4405 9e95393 a1e4405 | 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | from __future__ import annotations
import asyncio
import json
from collections.abc import Mapping, Sequence
from .contracts import CapabilityCapsule, TaskContract
from .hf_client import HFChatClient
from .schemas import RoleName, RoleResult, SharedRolePacket
_ROLE_RULES = {
RoleName.CONSTRUCTIVE: (
"Resolve every task. Return public_text, evidence_ids, conditions, exceptions "
"and required action_steps. Never invent Astera facts."
),
RoleName.ADVERSARIAL: (
"Inspect the constructive draft. Find missing needs, contradictions, false premises, "
"failure conditions and unclear wording. Do not rewrite the whole answer."
),
RoleName.EVIDENCE_BOUND: (
"Inspect the constructive draft claim-by-claim against supplied facts. Flag unsupported, "
"stale, conditional or conflicting claims. Preserve evidence conditions and exceptions."
),
}
_VALIDATOR_CAPS = {
RoleName.ADVERSARIAL: {"review", "quality", "technical", "clarity", "audience", "terminology"},
RoleName.EVIDENCE_BOUND: {"evidence", "knowledge", "claims", "terminology", "review", "quality"},
}
class ThreeRoleModelPool:
"""One Work, three role runtimes: Constructive 4B, Adversarial 4B, Evidence-bound 8B."""
def __init__(self, clients: Mapping[RoleName, HFChatClient]):
missing = set(RoleName) - set(clients)
if missing:
raise ValueError(f"missing_role_clients:{sorted(role.value for role in missing)}")
self.clients = dict(clients)
@staticmethod
def _compact_packet(packet: SharedRolePacket) -> dict[str, object]:
return {
"request_id": packet.request_id,
"need": packet.normalized_need,
"audience": packet.audience,
"tasks": [
{
"id": t.task_id,
"text": t.text,
"intent": t.intent,
"shape": t.response_shape,
"facts_required": bool(t.required_facts),
"actionable": t.actionability_required,
}
for t in packet.tasks
],
"conditions": packet.user_conditions,
"facts": [
{
"id": f.fact_id,
"value": f.value,
"authority": f.authority,
"source_ids": f.source_ids or [f.source_id],
"conditions": f.conditions,
"exceptions": f.exceptions,
"relations": f.relations,
"knowledge_key": f.knowledge_key,
"domain": f.domain,
"topic": f.topic,
}
for f in packet.facts
],
"repair_targets": packet.repair_targets,
}
@staticmethod
def _draft_view(draft: RoleResult | None) -> dict[str, object] | None:
if draft is None:
return None
return {
"claims": draft.claims,
"evidence_ids": draft.evidence_ids,
"task_resolutions": [
{
"task_id": r.task_id,
"public_text": r.public_text,
"evidence_ids": r.evidence_ids,
"conditions": r.conditions,
"exceptions": r.exceptions,
"action_steps": r.action_steps,
"unresolved_reason": r.unresolved_reason,
}
for r in draft.task_resolutions
],
}
@staticmethod
def _skills_for_role(role: RoleName, skills: Sequence[CapabilityCapsule]) -> list[str]:
if role == RoleName.CONSTRUCTIVE:
return [s.text for s in skills]
wanted = _VALIDATOR_CAPS[role]
return [s.text for s in skills if wanted.intersection(s.capabilities)]
async def _run(
self,
role: RoleName,
packet: SharedRolePacket,
skills: Sequence[CapabilityCapsule],
*,
draft: RoleResult | None = None,
) -> RoleResult:
prompt = {
"role": role.value,
"rule": _ROLE_RULES[role],
"packet": self._compact_packet(packet),
"constructive_draft": self._draft_view(draft),
"capabilities": self._skills_for_role(role, skills),
"output": (
"RoleResult JSON: role, claims, evidence_ids, risks, uncertainties, "
"missing_needs, contradictions, task_resolutions, completion_state"
),
}
result = await self.clients[role].chat_json(
[
{
"role": "system",
"content": (
"You are one isolated role inside Astera Customer AI. Return JSON only. "
"Do not expose chain-of-thought. Do not claim external actions were executed."
),
},
{
"role": "user",
"content": json.dumps(prompt, ensure_ascii=False, separators=(",", ":")),
},
]
)
result["role"] = role.value
parsed = RoleResult.model_validate(result)
if parsed.role != role:
raise ValueError("role_mismatch")
return parsed
async def run_all(
self,
packet: SharedRolePacket,
skills: Sequence[CapabilityCapsule],
) -> list[RoleResult]:
constructive = await self._run(RoleName.CONSTRUCTIVE, packet, skills)
adversarial, evidence = await asyncio.gather(
self._run(RoleName.ADVERSARIAL, packet, skills, draft=constructive),
self._run(RoleName.EVIDENCE_BOUND, packet, skills, draft=constructive),
)
return [constructive, adversarial, evidence]
async def validate_draft(
self,
packet: SharedRolePacket,
skills: Sequence[CapabilityCapsule],
draft: RoleResult,
) -> list[RoleResult]:
adversarial, evidence = await asyncio.gather(
self._run(RoleName.ADVERSARIAL, packet, skills, draft=draft),
self._run(RoleName.EVIDENCE_BOUND, packet, skills, draft=draft),
)
return [adversarial, evidence]
async def retry_role(
self,
role: RoleName,
packet: SharedRolePacket,
skills: Sequence[CapabilityCapsule],
) -> RoleResult:
if role != RoleName.CONSTRUCTIVE:
raise ValueError("targeted_repair_constructive_only")
return await self._run(role, packet, skills)
async def semantic_decompose(self, message: str, seed: TaskContract) -> TaskContract:
prompt = {
"request": message,
"seed": seed.model_dump(mode="json"),
"required_fields": [
"purpose",
"target",
"conditions",
"constraints",
"premises",
"missing_information",
"success_criteria",
"need_tasks",
"completion_conditions",
],
"rule": (
"Refine only the task structure. Keep every user need and constraint. "
"Do not invent product facts. need_tasks must use t1,t2... and valid response_shape values."
),
}
obj = await self.clients[RoleName.CONSTRUCTIVE].chat_json(
[
{"role": "system", "content": "Return only a compact TaskContract JSON object."},
{
"role": "user",
"content": json.dumps(prompt, ensure_ascii=False, separators=(",", ":")),
},
],
max_tokens=1200,
)
contract = TaskContract.model_validate(obj)
if not contract.need_tasks:
raise ValueError("semantic_decomposition_empty")
return contract
|