File size: 8,902 Bytes
395651c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
213
214
215
216
217
218
219
220
221
222
223
224
import json
import logging
from typing import Any, Dict

from agents.geometry_agent import GeometryAgent
from agents.knowledge_agent import KnowledgeAgent
from agents.ocr_agent import OCRAgent
from agents.parser_agent import ParserAgent
from agents.solver_agent import SolverAgent
from app.logutil import log_step
from app.ocr_celery import ocr_from_image_url
from solver.dsl_parser import DSLParser
from solver.engine import GeometryEngine

logger = logging.getLogger(__name__)

_CLIP = 2000


def _clip(val: Any, n: int = _CLIP) -> str | None:
    if val is None:
        return None
    if isinstance(val, str):
        s = val
    else:
        s = json.dumps(val, ensure_ascii=False, default=str)
    return s if len(s) <= n else s[:n] + "…"


def _step_io(step: str, input_val: Any = None, output_val: Any = None) -> None:
    """Debug: chỉ input/output (đã cắt), tránh dump dài dòng không cần thiết."""
    log_step(step, input=_clip(input_val), output=_clip(output_val))


class Orchestrator:
    def __init__(self):
        self.parser_agent = ParserAgent()
        self.geometry_agent = GeometryAgent()
        self.ocr_agent = OCRAgent()
        self.knowledge_agent = KnowledgeAgent()
        self.solver_agent = SolverAgent()
        self.solver_engine = GeometryEngine()
        self.dsl_parser = DSLParser()

    def _generate_step_description(self, semantic_json: Dict[str, Any], engine_result: Dict[str, Any]) -> str:
        """Tạo mô tả từng bước vẽ dựa trên kết quả của engine."""
        analysis = semantic_json.get("analysis", "")
        if not analysis:
            analysis = f"Giải bài toán về {semantic_json.get('type', 'hình học')}."

        steps = ["\n\n**Các bước dựng hình:**"]
        drawing_phases = engine_result.get("drawing_phases", [])
        
        for phase in drawing_phases:
            label = phase.get("label", f"Giai đoạn {phase['phase']}")
            points = ", ".join(phase.get("points", []))
            segments = ", ".join([f"{s[0]}{s[1]}" for s in phase.get("segments", [])])
            
            step_text = f"- **{label}**:"
            if points:
                step_text += f" Xác định các điểm {points}."
            if segments:
                step_text += f" Vẽ các đoạn thẳng {segments}."
            steps.append(step_text)

        circles = engine_result.get("circles", [])
        for c in circles:
            steps.append(f"- **Đường tròn**: Vẽ đường tròn tâm {c['center']} bán kính {c['radius']}.")

        return analysis + "\n".join(steps)

    async def run(
        self,
        text: str,
        image_url: str = None,
        job_id: str = None,
        session_id: str = None,
        status_callback=None,
        history: list = None,
    ) -> Dict[str, Any]:
        """
        Run the full pipeline. Optional history allows context-aware solving.
        """
        _step_io(
            "orchestrate_start",
            input_val={
                "job_id": job_id,
                "text_len": len(text or ""),
                "image_url": image_url,
                "history_len": len(history or []),
            },
            output_val=None,
        )

        if status_callback:
            await status_callback("processing")

        # 1. Extract context from history (if any)
        previous_context = None
        if history:
            # Look for the last assistant message with geometry data
            for msg in reversed(history):
                if msg.get("role") == "assistant" and msg.get("metadata", {}).get("geometry_dsl"):
                    previous_context = {
                        "geometry_dsl": msg["metadata"]["geometry_dsl"],
                        "coordinates": msg["metadata"].get("coordinates", {}),
                        "analysis": msg.get("content", ""),
                    }
                    break
        
        if previous_context:
            _step_io("context_found", input_val=None, output_val={"dsl_len": len(previous_context["geometry_dsl"])})

        # 2. Gather input text (OCR or direct)
        input_text = text
        if image_url:
            input_text = await ocr_from_image_url(image_url, self.ocr_agent)
            _step_io("step1_ocr", input_val=image_url, output_val=input_text)
        else:
            _step_io("step1_ocr", input_val="(no image)", output_val=text)

        feedback = None
        MAX_RETRIES = 2

        for attempt in range(MAX_RETRIES + 1):
            _step_io(
                "attempt",
                input_val=f"{attempt + 1}/{MAX_RETRIES + 1}",
                output_val=None,
            )
            if status_callback:
                await status_callback("solving")

            # Parser with context
            _step_io("step2_parse", input_val=f"{input_text[:50]}...", output_val=None)
            semantic_json = await self.parser_agent.process(input_text, feedback=feedback, context=previous_context)
            semantic_json["input_text"] = input_text
            _step_io("step2_parse", input_val=None, output_val=semantic_json)

            # Knowledge augmentation
            _step_io("step3_knowledge", input_val=semantic_json, output_val=None)
            semantic_json = self.knowledge_agent.augment_semantic_data(semantic_json)
            _step_io("step3_knowledge", input_val=None, output_val=semantic_json)

            # Geometry DSL with context (passing previous DSL to guide generation)
            _step_io("step4_geometry_dsl", input_val=semantic_json, output_val=None)
            dsl_code = await self.geometry_agent.generate_dsl(
                semantic_json, 
                previous_dsl=previous_context["geometry_dsl"] if previous_context else None
            )
            _step_io("step4_geometry_dsl", input_val=None, output_val=dsl_code)

            _step_io("step5_dsl_parse", input_val=dsl_code, output_val=None)
            points, constraints, is_3d = self.dsl_parser.parse(dsl_code)
            _step_io(
                "step5_dsl_parse",
                input_val=None,
                output_val={
                    "points": len(points),
                    "constraints": len(constraints),
                    "is_3d": is_3d,
                },
            )

            _step_io("step6_solve", input_val=f"{len(points)} pts / {len(constraints)} cons (is_3d={is_3d})", output_val=None)
            import anyio
            engine_result = await anyio.to_thread.run_sync(self.solver_engine.solve, points, constraints, is_3d)

            if engine_result:
                coordinates = engine_result.get("coordinates")
                _step_io("step6_solve", input_val=None, output_val=coordinates)
                logger.info(
                    "[Orchestrator] geometry solved job_id=%s is_3d=%s n_coords=%d",
                    job_id,
                    is_3d,
                    len(coordinates) if isinstance(coordinates, dict) else 0,
                )
                break

            feedback = "Geometry solver failed to find a valid solution for the given constraints. Parallelism or lengths might be inconsistent."
            _step_io(
                "step6_solve",
                input_val=f"attempt {attempt + 1}",
                output_val=feedback,
            )
            if attempt == MAX_RETRIES:
                _step_io(
                    "orchestrate_abort",
                    input_val=None,
                    output_val="solver_exhausted_retries",
                )
                return {
                    "error": "Solver failed after multiple attempts.",
                    "last_dsl": dsl_code,
                }

        _step_io("orchestrate_done", input_val=job_id, output_val="success")

        # 8. Solution calculation (New in v5.1)
        solution = None
        if engine_result:
            _step_io("step8_solve_math", input_val=semantic_json.get("target_question"), output_val=None)
            solution = await self.solver_agent.solve(semantic_json, engine_result)
            _step_io("step8_solve_math", input_val=None, output_val=solution.get("answer"))

        final_analysis = self._generate_step_description(semantic_json, engine_result)

        status = "success"
        return {
            "status": status,
            "job_id": job_id,
            "geometry_dsl": dsl_code,
            "coordinates": coordinates,
            "polygon_order": engine_result.get("polygon_order", []),
            "circles": engine_result.get("circles", []),
            "lines": engine_result.get("lines", []),
            "rays": engine_result.get("rays", []),
            "drawing_phases": engine_result.get("drawing_phases", []),
            "semantic": semantic_json,
            "semantic_analysis": final_analysis,
            "solution": solution,
            "is_3d": is_3d,
        }