
    ɢi,                     p   d dl Z d dlZd dlZd dlmZ d dlmZmZ d dlm	Z	  ej                  e      Zg dZdddd	d
ddddd
ddddd
dddd d
gZdddd	d
ddddd
ddddd
dddd d
gZddd d	d
ddd!dd
d"d#d$d%d
dd&d'd d
gZd(efd)Zd4d*ed+ed,ed-ed(ef
d.Zd4d*ed+ed/ed,ed-ed(efd0Zd1ed2ed,ed(efd3Zy)5    N)Optional)assemble_contextget_full_context_for_domain)call_llm_text)capturedanalyzedplannedcombinedstagedimplementedparkedinitzPreparing analysisz#Loading concept and project context   )keylabeldescriptionest_secondscontextzAssembling contextz0Gathering roadmap state, tasks, and project data   ai_analysiszAI analyzing conceptz=Classifying NEW vs LEGACY, identifying dependencies and scope   completezAnalysis completez8Concept analyzed with classification and recommendationszPreparing planz*Loading concept analysis and project statez+Gathering full project context for planningai_planzAI generating planz@Creating staged implementation plan with tasks, risks, and tests   zPlan completez/Implementation plan generated with staged taskszPreparing batchzLoading all selected conceptsz*Merging concept analyses and project stateai_batchzAI synthesizing batch planzECreating parallel tracks, dependency graph, and staged execution plan(   zBatch plan completez;Unified implementation plan with parallel processing tracksreturnc                    ddl m}m}m}m} | j                  |      j                         }g g g d}|D ]  }|j                  |j                  |j                  xs d|j                  xs dd}|j                  dk(  r|d   j                  |       ^|j                  dk(  r|d   j                  |       |j                  dk(  s|j                  r|j                  j                         nd |d	<   |d   j                  |        | j                  |      j                         }	i }
|	D ]c  }|j                  |
vrg |
|j                  <   |
|j                     j                  |j                  |j                  |j                  xs dd
       e | j                  |      j                         }g }|D ]r  }| j                  |      j!                  |j"                  |j                  k(        j%                         }|j                  |j&                  |j(                  d u|d       t ||
|t*        j*                  j-                         j                         dz   dS )Nr   )RoadmapItem	BuildTaskPackageDomain)r	   in_progressdone )idtitler   categoryr	   r#   r$   completed_at)r'   statusr(   )domainr   packagesZ)roadmaptasksdomains	timestamp)
app.modelsr   r    r!   r"   queryallr&   r'   r   r(   r*   appendr)   	isoformat	list_namefilter	domain_idcountr+   analyzed_atdatetimeutcnow)
db_sessionr   r    r!   r"   roadmap_itemsroadmap_stateitementryr/   
task_statetr0   domain_stated	pkg_counts                   2/sites/domain-asset-master/app/services/advisor.pybuild_advisor_contextrI   #   s1   BB$$[1557MM
  0wwDDTDTDZXZhlhuhuh{y{|;;)#)$++E2[[M)-(//6[[F"EIEVEVD$5$5$?$?$A\`E.!&!((/0 Y'++-EJ ;;j(&(Jq{{#1;;&&WWhh

(b(
 	 v&**,GL $$W-44W5F5F!$$5NOUUW	hhT1!
 	 !&&--/99;cA	     concept_titleconcept_inputadvisor_contextproject_contextc                 :   dt        |j                  di       j                  dg              dt        j                  |j                  di       j                  dg       d        dt        |j                  di       j                  dg              dt        j                  |j                  di       j                  dg       d        dt        |j                  di       j                  d	g              dt        j                  |j                  di       j                  d	g       d        d
t        j                  |j                  dg       d        dt        j                  |j                  di       d        d|rdt	        d       | nd d|  d| d}|S )Nuv  You are Aura Advisor, an AI architectural analyst for the Aura platform — an AI-powered domain-to-business generator.

--- CONTEXT DOCTRINE ---
You must use ALL provided context to inform your analysis. Context is the active ingredient, not background. The most expensive analysis is the one that ignores available data.

--- CURRENT PROJECT STATE ---
ROADMAP:
- Planned (r.   r	   z	 items): indentz
- In Progress (r#   z	
- Done (r$   z

ACTIVE DOMAINS: r0   

TASK LISTS: r/   

--- DOMAIN-SPECIFIC CONTEXT ---
   r%   z$

--- CONCEPT TO ANALYZE ---
Title: 
Description: a  

--- YOUR TASK ---
Analyze this concept and produce a structured JSON response with these fields:
{
    "classification": "NEW" or "LEGACY" (NEW = entirely new behavior, LEGACY = changes to existing behavior),
    "classification_reasoning": "Why this is NEW or LEGACY",
    "scope": "small" | "medium" | "large" | "epic",
    "scope_estimate": "Human-readable estimate of effort",
    "dependencies": ["List of existing systems/features this depends on or affects"],
    "risks": ["Potential risks or things that could go wrong"],
    "prerequisites": ["Things that must exist or be done first"],
    "related_roadmap_items": ["Titles of related roadmap items already tracked"],
    "suggested_approach": "High-level approach recommendation",
    "key_questions": ["Questions that should be answered before implementation"],
    "impact_areas": ["Parts of the codebase or user experience this would affect"]
}

Respond with ONLY valid JSON.)lengetjsondumpschr)rK   rL   rM   rN   prompts        rH   build_analysis_promptr]   S   s    ##Ir266y"EFGyQUQ[Q[\k\o\opy{}\~  ]C  ]C  DM  OQ  ]R  [_  R`  Qa aO''	26::="MNOyY]YcYcdsdwdw  yB  DF  eG  eK  eK  LY  []  e^  gk  Zl  Ym m		_  B/33FB?	@A4::VeViVijsuwVxV|V|  ~D  FH  WI  RV  LW  KX XO//	2>tLM NZZ++GR8FG HAP"3r7)O+<=VX Y Z 	 _ '%!FL MrJ   analysisc                 8   dt        j                  |j                  di       j                  dg       d        dt        j                  |j                  di       j                  dg       d        dt        j                  |j                  di       j                  dg       d        d	t        j                  |j                  d
i       d        d|rdt        d       | nd d|  d| dt        j                  |d       d|j                  dd       d}|S )Nu  You are Aura Advisor, an AI implementation planner for the Aura platform — an AI-powered domain-to-business generator.

--- CONTEXT DOCTRINE ---
You must use ALL provided context. Full structured context in every prompt. No assumptions — use what's provided.

--- DEVELOPMENT PROTOCOL ---
- For NEW behavior: strict TDD — write failing test first, then minimal code to pass, then refactor.
- For LEGACY behavior: characterize first — add tests capturing current behavior, then evolve.
- Always assume this code is serving production content.
- Prefer small, composable functions over large rewrites.

--- CURRENT PROJECT STATE ---
ROADMAP:
- Planned: r.   r	   rP   z
- In Progress: r#   z	
- Done: r$   rR   r/   rS   rT   rU   r%   z

--- CONCEPT ---
Title: rV   z

--- PRIOR ANALYSIS ---
r   z

--- YOUR TASK ---
Generate a detailed implementation plan as structured JSON:
{
    "plan_title": "Descriptive title for this implementation plan",
    "classification": "classificationNEWa  ",
    "phases": [
        {
            "phase_number": 1,
            "title": "Phase title",
            "description": "What this phase accomplishes",
            "tasks": [
                {
                    "title": "Task title",
                    "description": "What to do",
                    "type": "test" | "implementation" | "refactor" | "config",
                    "files_affected": ["list of files"],
                    "estimated_effort": "small" | "medium" | "large"
                }
            ]
        }
    ],
    "test_requirements": [
        {
            "test_name": "Name of test",
            "test_type": "unit" | "integration" | "e2e",
            "what_it_validates": "Description of what this test proves",
            "is_characterization": false
        }
    ],
    "risks_and_mitigations": [
        {
            "risk": "What could go wrong",
            "severity": "low" | "medium" | "high",
            "mitigation": "How to prevent or handle it"
        }
    ],
    "success_criteria": ["List of things that must be true when this is done"],
    "estimated_total_effort": "Human-readable total estimate"
}

Respond with ONLY valid JSON.)rY   rZ   rX   r[   )rK   rL   r^   rM   rN   r\   s         rH   build_plan_promptrb   }   s=    JJ**9b9==iLUYZ[ \

?..y"=AA-QST]abc d		O''	26::62Ft	TU VZZ++GR8FG HAP"3r7)O+<=VX Y Z 	 _  HQ   ! !%5u=> $?AD!FJ MrJ   conceptsanalysesc                    g }| D ]k  }|j                  |d   i       }|j                  |d   |d   |d   d d |j                  dd      |j                  dd      |j                  dg       d	       m d
t        j                  |j                  di       d        dt        j                  |j                  di       d        dt        j                  |d       d}|S )Nr&   r'   	raw_inputi  r`   unknownscopedependencies)r&   r'   r   r`   rh   ri   zYou are Aura Advisor, an AI batch planner for the Aura platform.

--- CONTEXT DOCTRINE ---
Use ALL provided context. Identify shared infrastructure, parallel tracks, and sequential dependencies across concepts.

--- CURRENT PROJECT STATE ---
ROADMAP: r.   rP   z
TASKS: r/   z

--- CONCEPTS TO BATCH ---
r   as  

--- YOUR TASK ---
Synthesize these concepts into a single staged implementation plan with parallel processing where possible. Return structured JSON:
{
    "batch_title": "Descriptive title for this batch plan",
    "parallel_tracks": [
        {
            "track_name": "Name of parallel track",
            "description": "What this track accomplishes",
            "concept_ids": [1, 2],
            "can_run_parallel_with": ["other track names"],
            "phases": [
                {
                    "phase_number": 1,
                    "title": "Phase title",
                    "tasks": [
                        {
                            "title": "Task title",
                            "concept_id": 1,
                            "estimated_effort": "small" | "medium" | "large"
                        }
                    ]
                }
            ]
        }
    ],
    "dependency_graph": {
        "sequential_dependencies": [
            {"before": "track/concept name", "after": "track/concept name", "reason": "why"}
        ],
        "shared_infrastructure": ["things multiple concepts need that should be built once"]
    },
    "execution_order": ["Ordered list of what to build first, second, etc."],
    "total_estimated_effort": "Human-readable estimate",
    "risk_summary": ["Top risks across all concepts"]
}

Respond with ONLY valid JSON.)rX   r5   rY   rZ   )rc   rd   rM   concept_summariescr^   r\   s          rH   build_batch_promptrl      s     	<<$,  D'wZ[>$3/&ll+;YG\\'95$LL<"
 		
 **_((B7
E	F G

?&&w3DAB C a( ) &*0!Fb MrJ   )r%   )rY   loggingr<   typingr   app.services.context_enginer   r   app.services.llmr   	getLogger__name__loggerCONCEPT_STATUSESADVISOR_ANALYZE_STEPSADVISOR_PLAN_STEPSADVISOR_BATCH_STEPSdictrI   strr]   rb   listrl    rJ   rH   <module>r|      s       U *			8	$e  1Bgxyz 4Ew  IJ  K$:  LK  \^  _!4E  QR  S	  ->j{|} 4Er  DE  F 4  FH  Y[  \Ar  DE  F	  .?^opq 4Eq  CD  E!=  OV  gi  j!6  HE  VW  X	 - -`' 'S 'SW 'jm 'wz 'TFS F F F_c Fvy F  DG FR> > > >QT >rJ   