File size: 11,912 Bytes
734b5b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path
from typing import Any

from scripts.orchestrator import PDFEditorOrchestrator


@dataclass(frozen=True)
class TeamMember:
    id: str
    name: str
    title: str
    agent_key: str
    icon: str
    color: str
    system_prompt: str


TEAM_MEMBERS: tuple[TeamMember, ...] = (
    TeamMember(
        id="lead",
        name="Mike",
        title="Command Lead",
        agent_key="layout_analysis",
        icon="🧭",
        color="#5b7cfa",
        system_prompt=(
            "You are a command lead for a PDF AI team. Break the user's goal into a "
            "clear execution direction for the rest of the team. Be concise."
        ),
    ),
    TeamMember(
        id="text",
        name="Iris",
        title="Text Extraction Specialist",
        agent_key="text_extraction",
        icon="📝",
        color="#2ec9b7",
        system_prompt=(
            "You decide how text should be extracted from PDFs. Focus on OCR, scanned "
            "documents, text quality, and extraction risks."
        ),
    ),
    TeamMember(
        id="tables",
        name="David",
        title="Table Analyst",
        agent_key="table_detection",
        icon="📊",
        color="#f59e0b",
        system_prompt=(
            "You analyze structured content in PDFs. Focus on tables, statements, rows, "
            "columns, and downstream data extraction needs."
        ),
    ),
    TeamMember(
        id="image",
        name="Alex",
        title="Image Quality Engineer",
        agent_key="image_processing",
        icon="🖼️",
        color="#a78bfa",
        system_prompt=(
            "You assess PDF page image quality. Focus on scan quality, blur, contrast, "
            "and whether OCR or cleanup is needed first."
        ),
    ),
    TeamMember(
        id="format",
        name="Emma",
        title="Formatting Editor",
        agent_key="format_correction",
        icon="✏️",
        color="#3ecf8e",
        system_prompt=(
            "You refine extracted PDF content. Focus on edits, formatting corrections, "
            "text insertion needs, cleanup, and output clarity."
        ),
    ),
    TeamMember(
        id="language",
        name="Sarah",
        title="Language Reviewer",
        agent_key="language_detection",
        icon="🌐",
        color="#ec4899",
        system_prompt=(
            "You detect PDF language and review multilingual requirements. Focus on OCR "
            "language choice and language-sensitive processing."
        ),
    ),
    TeamMember(
        id="quality",
        name="Bob",
        title="Quality Validator",
        agent_key="content_validation",
        icon="✅",
        color="#f87171",
        system_prompt=(
            "You validate whether the PDF workflow will produce reliable results. Focus "
            "on risks, verification, and failure prevention."
        ),
    ),
    TeamMember(
        id="export",
        name="Nora",
        title="Export Coordinator",
        agent_key="export_rendering",
        icon="📦",
        color="#06b6d4",
        system_prompt=(
            "You prepare the final delivery plan for a PDF workflow. Focus on output, "
            "artifacts, export expectations, and handoff."
        ),
    ),
)


class CommandRouter:
    """Atoms-like AI team planner for PDF workflows."""

    def __init__(self, project_root: Path | None = None, provider: str = "lmstudio"):
        self.project_root = Path(project_root) if project_root else Path.cwd()
        self.orchestrator = PDFEditorOrchestrator(provider=provider)

    def describe_team(self) -> list[dict[str, Any]]:
        mapping = self.orchestrator.describe_agent_models()
        members = []
        for member in TEAM_MEMBERS:
            llm = mapping.get(member.agent_key, {})
            members.append(
                {
                    "id": member.id,
                    "name": member.name,
                    "title": member.title,
                    "agent_key": member.agent_key,
                    "icon": member.icon,
                    "color": member.color,
                    "requested_provider": llm.get("requested_provider", ""),
                    "provider": llm.get("provider", ""),
                    "model": llm.get("model", ""),
                    "base_url": llm.get("base_url", ""),
                }
            )
        return members

    def plan_command(self, payload: dict[str, Any]) -> dict[str, Any]:
        brief = (payload.get("brief") or "").strip()
        if not brief:
            return {"success": False, "error": "Command brief is required."}

        selected_file = str(payload.get("input_pdf") or "").strip()
        output_dir = str(payload.get("output_dir") or "").strip()
        recommendation = self._build_recommendation(brief)
        team = self.describe_team()
        insights = [self._member_plan(member, brief, selected_file) for member in TEAM_MEMBERS]

        return {
            "success": True,
            "brief": brief,
            "input_pdf": selected_file,
            "output_dir": output_dir,
            "team": team,
            "insights": insights,
            "recommendation": recommendation,
            "available_pipelines": self.orchestrator.list_available_pipelines(),
        }

    def _member_plan(

        self, member: TeamMember, brief: str, selected_file: str

    ) -> dict[str, Any]:
        llm = self.orchestrator.agent_llms[member.agent_key]
        prompt = (
            f"User command:\n{brief}\n\n"
            f"Input PDF: {selected_file or 'not specified'}\n\n"
            "Respond in exactly 3 short bullet points:\n"
            "- Focus: your main responsibility\n"
            "- Action: what you would do next\n"
            "- Handoff: what the next role should receive"
        )
        raw = llm.complete(
            prompt,
            system=member.system_prompt,
            max_tokens=220,
            temperature=0.4,
        )
        focus, action, handoff = self._parse_member_response(raw, member)
        return {
            "member_id": member.id,
            "name": member.name,
            "title": member.title,
            "agent_key": member.agent_key,
            "focus": focus,
            "action": action,
            "handoff": handoff,
            "raw": raw,
        }

    def _parse_member_response(

        self, raw: str, member: TeamMember

    ) -> tuple[str, str, str]:
        if not raw or raw.startswith("Error:"):
            return self._fallback_member_response(member)

        lines = []
        for line in raw.splitlines():
            cleaned = line.strip().lstrip("-").strip()
            if cleaned:
                lines.append(cleaned)

        if len(lines) < 3:
            return self._fallback_member_response(member)

        return lines[0], lines[1], lines[2]

    def _fallback_member_response(self, member: TeamMember) -> tuple[str, str, str]:
        fallback = {
            "layout_analysis": (
                "Define the overall PDF processing direction.",
                "Decide whether the command needs OCR, editing, annotation, or validation first.",
                "Pass a prioritized flow to the rest of the team.",
            ),
            "text_extraction": (
                "Check whether text must be extracted from scanned pages.",
                "Recommend OCR when the command depends on searchable text.",
                "Pass extracted-text requirements to editing and validation.",
            ),
            "table_detection": (
                "Check whether the command targets statement or table structure.",
                "Highlight whether row/column preservation matters.",
                "Pass structured-data risks to the editor and validator.",
            ),
            "image_processing": (
                "Check scan quality and OCR readiness.",
                "Recommend cleanup when blur, noise, or low contrast will block OCR.",
                "Pass image-quality constraints to OCR and validation.",
            ),
            "format_correction": (
                "Plan text cleanup and edit operations.",
                "Translate the user command into concrete text or region edits.",
                "Pass cleaned content requirements to export.",
            ),
            "language_detection": (
                "Choose the right language handling path.",
                "Recommend OCR language and multilingual handling.",
                "Pass language settings to OCR and quality review.",
            ),
            "content_validation": (
                "Define what must be checked before delivery.",
                "Flag accuracy, truncation, and corruption risks.",
                "Pass final QA criteria to export.",
            ),
            "export_rendering": (
                "Decide final output expectations.",
                "Prepare final PDF, manifest, and delivery artifacts.",
                "Return the deliverables to the user.",
            ),
        }
        return fallback.get(
            member.agent_key,
            (
                "Own this part of the command.",
                "Produce the next useful step.",
                "Hand it to the next role.",
            ),
        )

    def _build_recommendation(self, brief: str) -> dict[str, Any]:
        text = brief.lower()
        wants_ocr = any(
            token in text
            for token in ["ocr", "scan", "scanned", "searchable", "extract text", "text layer"]
        )
        wants_edit = any(
            token in text
            for token in [
                "edit",
                "replace",
                "insert",
                "delete page",
                "reorder",
                "rotate",
                "fix text",
                "cleanup",
            ]
        )
        wants_annotation = any(
            token in text
            for token in ["highlight", "comment", "annotate", "redact", "mark", "rectangle"]
        )

        recommended_agents = ["inspector"]
        if wants_ocr:
            recommended_agents.append("ocr")
        if wants_edit:
            recommended_agents.append("editor")
        if wants_annotation:
            recommended_agents.append("annotator")
        recommended_agents.extend(["validator", "exporter"])

        deduped_agents = []
        for agent in recommended_agents:
            if agent not in deduped_agents:
                deduped_agents.append(agent)

        if wants_edit and wants_annotation:
            pipeline_name = "full_document"
        elif wants_edit:
            pipeline_name = "text_edit"
        elif wants_annotation:
            pipeline_name = "annotation_review"
        elif wants_ocr:
            pipeline_name = "ocr_preflight"
        else:
            pipeline_name = "full_document"

        reasons = []
        if wants_ocr:
            reasons.append("Command viitab OCR-ile või skännitud PDF-ile.")
        if wants_edit:
            reasons.append("Command sisaldab muutmist või sisu parandamist.")
        if wants_annotation:
            reasons.append("Command sisaldab märkimist, kommentaare või redaktsiooni.")
        if not reasons:
            reasons.append("Vaikimisi soovitan täielikku ülevaate + valideerimise töövoogu.")

        return {
            "pipeline_name": pipeline_name,
            "recommended_agents": deduped_agents,
            "reasons": reasons,
        }