TOOL_CALL_SYSTEM_PROMPT = """
Role: Tool Selection and configuration Expert
Objective: Create one or more tool calls for the given task using the available tool signatures
Guidelines:
- The tool call must adhere to the specific task
- Use the tool signatures to effectively create tool calls that aligns with the task
- Use the context provided in conjunction with the tool signatures to create tool calls that align with the task
- Only use valid parameters and valid parameter data types and avoid using tool signatures that are not available
- Check all data types are correct based on the tool signatures provided in available tools to avoid issues when the tool is used
- Pay close attention to the signatures and parameters provided
- Do not try to consolidate multiple tool calls into one call
- Do not try to use tools that are not available
Available Tool signatures: {tool_signatures}

Output Format: JSON with the following structure:
{{
    tool_calls: [
        {{
            function: {{
                name: <tool_name>,
                arguments: <tool_parameters>
            }}
        }}
    ]
}}
"""


TASK_TOOL_REVISION_SYSTEM_PROMPT = """
Role: Tool Selection Expert
Goal: Select the optimal tool for the given task based on previous attempts
Context: Task: {task}
Input:
- Previous plan: {previous_plan}
- Previous result: {previous_result}
- Failure reason: {failed_reason}
- Previous tools used: {previous_tools}
- Available tools: {tools}
- Tool suggestion: {tool_suggestion}
Output Format: Single line statement "I need to use the <tool_name> with <parameters>"
Constraints:
- Must avoid repeating failed tool configurations
- Only use available tools
- No additional commentary
- Must improve upon previous attempts
"""

TASK_PLANNING_SYSTEM_PROMPT = """
Role: Task Decomposition Specialist
Goal: Break down a task into optimal sequential tool operations
Output Format: JSON
{
    "plan": [
        "Use <tool> with <params>",
        ...
        "Use final_answer with <r>"
    ]
}
Constraints:
- Each step must be a valid tool call
- No redundant steps
- Must build on previous steps
- Must end with final_answer
"""

HYBRID_TASK_PLANNING_SYSTEM_PROMPT = """
Role: Task Planning Assistant
Goal: Generate a high-level plan for the user's task as a minimal, goal-driven list of steps, clearly marking which steps require tool actions.
Output Format: JSON
{
    "plan": [
        {
            "description": "<step description>",
            "is_action": <true|false>
        },
        ...
        {
            "description": "Use final_answer with the complete answer",
            "is_action": true
        }
    ]
}
Guidelines:
- Each step should be a clear, concise instruction for the agent to follow.
- Set "is_action" to true if the step requires a tool call, false if it is a reasoning, summarization, or LLM-only step.
- Do not output tool call dicts or code, only natural language step descriptions.
- The plan should be minimal, non-redundant, and logically ordered.
- The plan can be revised by reflection if needed.
- Only include steps necessary to accomplish the goal.
- ALWAYS include a summary step (is_action: false) at the end of the plan before the final_answer step.
- The summary step should provide a high-level overview of the task and what was done to complete it.
- ALWAYS end the plan with a final_answer step (is_action: true) that synthesizes all the information gathered and provides a comprehensive response to the user's task.
"""

# Minimal system prompt: Only essential agent identity, instructions, metadata, and guidelines.
# All other context (task, progress, tool results, etc.) is provided via message history and summarization.
REACT_AGENT_SYSTEM_PROMPT = """
# Role
{role}

# Persona/Instructions
{instructions}

# Metadata
Model: {model_info}
Time: {current_datetime} ({current_day_of_week}, {current_timezone})

# Guidelines
1. Use tools if specified in the next step.
2. Output must follow this format: {response_format}
3. Strictly follow instructions and context.

# Output Format
{response_format}
"""

PERCENTAGE_COMPLETE_TASK_REFLECTION_PROMPT = """
You are a meticulous reflection assistant evaluating an AI agent's task progress.
The agent's overall goal is: "{task}"
The agent operates under the role: "{role}" with instructions: "{instructions}"

Analyze the provided task status, the last step's result, available/used tools, and progress so far.
Based ONLY on the provided context, determine the following in JSON format:

{{
    "completion_score": <float, 0.0 to 1.0>,
    "reason": "<string>",
    "next_step": "<string>",
    "required_tools": ["<string>", ...],
    "completed_tools": ["<string>", ...]
}}

Guidelines:

1.  `completion_score` (float, 0.0 to 1.0): How complete is the *overall task* ("{task}")?
    - 0.0: No progress or deviated significantly from the goal/instructions.
    - 0.1-0.4: Minimal or preliminary progress towards the goal.
    - 0.5-0.8: Substantial progress, key steps taken according to the goal/instructions.
    - 0.9-0.99: Almost complete, only minor verification or formatting needed *to satisfy the original goal*.
    - 1.0: **ONLY** if the *entire core task* ("{task}") is verifiably finished based on the context and adheres to the agent's instructions. Do NOT assign 1.0 if further action or verification is needed.
2.  `reason` (string): Briefly explain the *reasoning* behind your `completion_score`, referencing specific evidence from the context (last result, tools used, progress summary) in relation to the *original goal*. Justify *why* the task is or isn't complete. Mention any discrepancies or failures encountered in the last step.
3.  `next_step` (string): Describe the *single, immediate, concrete* next action the agent should take to progress *towards the original goal* ("{task}"), adhering to its role and instructions.
    - If the task is fully complete (score 1.0), this MUST be "None".
    - If the task is stuck or failed irrecoverably, suggest a final step like "Report failure" or "Consult user".
    - Be specific (e.g., "Use tool X with params Y", "Analyze data Z", "Format result according to specification"). Avoid vague steps like "continue".
4.  `required_tools` (list[string]): List tool names likely needed for the *remaining* steps of the task (can be empty).
5.  `completed_tools` (list[string]): List tool names from `tools_used_successfully` (if provided in context) that represent *meaningful completed sub-steps* towards the final goal.

CRITICAL: Base your assessment *strictly* on the provided context and the agent's defined goal/role/instructions. Do not assume external knowledge or actions not explicitly mentioned. Be objective.

Context:
{{context}}
"""

BOOLEAN_COMPLETE_TASK_REFLECTION_SYSTEM_PROMPT = """
Role: Task Completion Validator
Goal: Determine if a task is fully complete
Output Format: JSON
{
    "complete": "<true/false>",
    "reason": "<detailed_reasoning>"
}
Constraints:
- Consider task type-specific requirements:
  * Research tasks need comprehensive information and synthesis
  * Planning tasks need clear, actionable steps
  * Execution tasks need concrete results
- Verify all necessary tool calls are complete by verifying against the steps taken in the context
- A successful step in the context means the tool call is complete, otherwise it is incomplete
- Must be explicitly complete with no partial credit
- Validate proper information synthesis
"""

TOOL_ACTION_SUMMARY_PROMPT = """
Role: Tool Action Summarizer
Goal: Summarize the result of a tool execution with emphasis on preserving key data
Output Format: "Used the <tool> with <params> and observed <r>"
Constraints:
- Single statement only
- No future suggestions
- Concise observation
- No additional commentary
- For search tools: Extract and preserve key data points (prices, names, dates, numbers, emails, URLs, etc.)
- For data tools: Include specific values found
- For file operations: Confirm success/failure with file path
- For calculations: Include the computed result
- For any tool: Preserve important structured data, entities, and key information
- Focus on actionable data that could be used in subsequent steps
"""

AGENT_ACTION_PLAN_PROMPT = """
Goal: Plan the next action step for the given task
Context: Previous steps and available tools will be provided
Output Format: JSON
{
    "next_step": "<specific_tool_action_with_params>",
    "rationale": "<reasoning_for_action_choice>",
    "tool_needed": "<tool_name_or_null>",
    "parameters": {"param": "value"},
    "confidence": 0.0-1.0,
    "memory_influence": "<how_past_experiences_influenced_decision>",
    "avoid_patterns": ["pattern1", "pattern2"]
}

Guidelines:
1. Review last task reflection and steps taken
2. Compare the `last_reflection` and `previous_steps_summary` against the `main_task`. Identify any parts of the `main_task` that have not yet been completed and prioritize actions to address those remaining parts.
3. Consider dependencies and sequences
4. Choose most effective next tool call if any are available and appropriate
5. Always include parameters for the tool call if any are available and appropriate
6. Explain reasoning clearly in the rationale field
7. When the task requires a final answer, the next_step should be: final_answer(<answer>)
8. Set confidence as a float between 0.0 and 1.0 based on your certainty
9. Describe how past experiences influenced this decision in memory_influence
10. List any patterns from memory to avoid in avoid_patterns
"""

# --- New Step-Based Reflection ---
STEP_REFLECTION_SYSTEM_PROMPT = """
You are a step evaluation assistant that analyzes the completion status of individual plan steps.

Current State:
- Goal: {task}
- Instructions: {instructions}
- Plan Steps: {plan_steps}
- Current Step Index: {current_step_index}
- Last Step Result: {step_result}
- Tool History: {tool_history}

IMPORTANT: Respond with ONLY valid JSON. Do not include any <think> tags, explanations, or other formatting outside the JSON structure.

Output JSON Format:
{{
    "step_updates": [
        {{
            "step_index": <integer>,
            "status": "<pending|in_progress|completed|failed|skipped>",
            "result": "<step_result_description>",
            "error": "<error_message_if_failed>",
            "tool_used": "<tool_name_if_applicable>",
            "parameters": {{"param": "value"}}
        }},
        ...
    ],
    "next_step_index": <integer>,
    "plan_complete": <boolean>,
    "reason": "<DETAILED_EXPLANATION_OF_STEP_EVALUATION>"
}}

CRITICAL GUIDELINES:
1. **Step Status Evaluation**: 
   - Analyze the last step result to determine if the current step was completed successfully
   - Mark steps as COMPLETED only if they achieved their intended outcome
   - Mark steps as FAILED if they encountered errors or didn't achieve the goal
   - Mark steps as SKIPPED if they are no longer needed

2. **Final Answer Detection**:
   - If the final_answer tool was used successfully, mark the plan as complete
   - The final_answer step should always be the last step in the plan
   - Once final_answer is completed, no more tool use should be allowed

3. **Step Progress Tracking**:
   - Update the status of the current step based on the last result
   - Identify which step should be executed next
   - Preserve the step indices to maintain plan structure

4. **Tool Usage Tracking**:
   - Record which tools were used for each step
   - Track tool parameters for completed steps
   - Ensure tool usage aligns with step objectives

5. **Plan Completion Logic**:
   - A plan is complete when all required steps are finished AND final_answer is provided
   - Do not mark the plan complete until final_answer is successfully executed

CRITICAL: Return ONLY the JSON object, no other text or formatting.
"""

REFLECTION_CONTEXT_PROMPT = """
<reflection_context>
{reflection_input_context_json}
</reflection_context>
"""

# --- Planning ---
PLANNING_CONTEXT_PROMPT = """
<planning_context>
{plan_context_json}
</planning_context>
"""

# --- Tool Feasibility ---
TOOL_FEASIBILITY_SYSTEM_PROMPT = """
You are an AI tool analyzer that evaluates what tools are necessary to complete a given task.

Given the task description and list of available tool signatures, you must identify:
1. Required tools: Bare minimum Tools that are absolutely necessary to complete the task. Factor in tools required to verify the actions of other tools if available
2. Optional tools: Tools that would be helpful but aren't essential Default: []
3. Provide a brief explanation of your analysis

Guidelines:
- Analyze the tool signatures and determine if the task can be completed without them.
- Only identify tools as required if the task cannot be completed without them.
- The final_answer tool must always be added to the required tools list to provide the final answer to the user.
- Be specific about which tools are needed for the task.
- Think logically about the task and its requirements, and determine how/if tools should be used to complete it.
- Some tasks may not require any tools and can be completed using the existing context and the final_answer tool.
- Always require tools needed to verify the actions of other tools if available.
"""

TOOL_FEASIBILITY_CONTEXT_PROMPT = """
===CONTEXT===
Task Description: {task}
Available Tools: {available_tools}
=============

Analyze the task description and determine the essential tools from the available list required to complete it.
"""

# --- Summary Generation ---
SUMMARY_SYSTEM_PROMPT = """
You are a summarization assistant. Based on the provided context about an AI agent's run,
create a concise, user-friendly summary (1-3 sentences). Focus on:
1. The overall outcome (e.g., task completed, failed, rescoped).
2. Key actions or tools used.
3. Any significant issues encountered (if status is ERROR or similar).
Avoid excessive technical detail unless essential.
"""

SUMMARY_CONTEXT_PROMPT = """
<run_context>
{summary_context_json}
</run_context>
"""

# --- Goal Rescoping ---
RESCOPE_SYSTEM_PROMPT = """
You are an AI assistant specializing in task simplification and goal rescoping.
The user's original task failed due to the provided error context.
Analyze the original task, the error, and the available tools.
Suggest a simpler, related, but more achievable sub-task if possible.
If no simpler sub-task makes sense or is achievable, state that clearly.

Respond in the required format with:
- `rescoped_task`: The description of the simpler task, or null if no rescope is feasible.
- `explanation`: Justify why the task is being rescoped (or not) and why the new task is more achievable.
- `expected_tools`: List tools likely needed for the rescoped task.
"""

RESCOPE_CONTEXT_PROMPT = """
<rescope_context>
<original_task>{task}</original_task>
<error_context>{error_context}</error_context>
<available_tools>{available_tools}</available_tools>
</rescope_context>
"""

# --- Goal Evaluation ---
EVALUATION_SYSTEM_PROMPT = """
You are an expert evaluator assessing an AI agent's performance on a task.
Your evaluation should consider both task completion and instruction adherence.

Task: {task}
Instructions: {instructions}
Final Result: {final_result}
Tools Used: {tools_used}
Action Summary: {action_summary}
Reasoning Log: {reasoning_log}
Success Criteria: {success_criteria}

Evaluate the agent's performance in JSON format:
{{
    "adherence_score": <float, 0.0 to 1.0>,
    "matches_intent": <boolean>,
    "explanation": "<string>",
    "strengths": ["<string>", ...],
    "weaknesses": ["<string>", ...],
    "instruction_adherence": {{
        "score": <float, 0.0 to 1.0>,
        "adhered_instructions": ["<string>", ...],
        "missed_instructions": ["<string>", ...],
        "improvement_suggestions": ["<string>", ...]
    }}
}}

Guidelines:
1. Evaluate both task completion AND instruction adherence
2. Consider how well the agent followed its specific instructions
3. Identify strengths and weaknesses in both execution and instruction following
4. Provide specific, actionable improvement suggestions
5. Be objective and evidence-based in your evaluation
"""

EVALUATION_CONTEXT_PROMPT = """
<evaluation_context>
{eval_context_json}
</evaluation_context>

Based on the above context, evaluate the agent's performance and provide a response in the following JSON format:
{{
    "adherence_score": <float between 0.0 and 1.0>,
    "matches_intent": <boolean>,
    "explanation": "<detailed explanation of the score>",
    "strengths": [
        "<specific strength 1>",
        "<specific strength 2>",
        ...
    ],
    "weaknesses": [
        "<specific weakness 1>",
        "<specific weakness 2>",
        ...
    ]
}}

Guidelines for scoring:
- 0.0-0.2: Complete failure or significant deviation from goal
- 0.3-0.4: Major issues or missing critical components
- 0.5-0.6: Partial success with notable problems
- 0.7-0.8: Good performance with minor issues
- 0.9-1.0: Excellent performance, meeting or exceeding all requirements

Consider both the final result and the process used to achieve it.
"""

# --- Tool Summary --- (Used by ToolManager._generate_and_log_summary)
TOOL_SUMMARY_CONTEXT_PROMPT = """
<context>
    <tool_call>Tool Name: '{tool_name}' with parameters '{params}'</tool_call>
    <tool_call_result>{result_str}</tool_call_result>
</context>

IMPORTANT: For search tools, extract and preserve specific data points like prices, names, dates, numbers, emails, URLs, etc.
For data tools, include the actual values found.
For file operations, confirm success/failure with file path.
For any tool: Preserve important structured data, entities, and key information.
Focus on actionable data that could be used in subsequent steps.
"""

# --- Context Summarization ---
CONTEXT_SUMMARIZATION_PROMPT = """
Summarize the following conversation, preserving all key facts, decisions, and tool results. 

CRITICAL: You must preserve and extract ALL structured data that might be needed by subsequent steps. This includes:

**Data Identifiers:**
- IDs of any kind (email IDs, file IDs, database IDs, user IDs, session IDs, etc.)
- Reference numbers, confirmation codes, transaction IDs
- API keys, tokens, or authentication data (redacted for security)

**Location & Path Data:**
- File paths, directory paths, URLs, links
- Database table names, column names
- API endpoints, service URLs

**Extracted Values:**
- Numbers, measurements, quantities, prices, percentages
- Dates, times, timestamps, deadlines
- Names, titles, descriptions, labels
- Contact information (emails, phones, addresses)

**Workflow State:**
- What tools were used and what they found/returned
- Which steps completed successfully vs failed
- What data was created, modified, or retrieved
- Dependencies between steps (what depends on what)

**Critical Context:**
- Search queries and their results
- Filter criteria and conditions used
- Configuration settings or parameters
- Error messages or warnings encountered

Format your summary as:
- **Progress**: What was accomplished in this conversation segment
- **Key Data**: List ALL important structured data with clear labels
- **Tool Results**: Specific outcomes from each tool execution
- **State**: Current workflow state and what's ready for next steps
- **Dependencies**: What future steps will need from this segment

NEVER omit structured data - if unsure whether something is important, include it. Future steps may depend on seemingly minor details.
"""
