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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
"""
Easy API β€” Build self-improving AI agent teams with zero technical expertise.

This module is the ONLY thing a non-technical user needs to touch.
Everything else is auto-configured.

    import purpose_agent as pa

    # One line. That's it.
    team = pa.purpose("Build a research assistant that finds and summarizes papers")
    result = team.run("Find recent papers on climate change solutions")
    print(result)

Three levels of usage:
    Level 1 (Beginner):  pa.purpose("description") β†’ working team
    Level 2 (Intermediate): pa.Team.build(agents=[...]) β†’ custom team
    Level 3 (Advanced):  pa.Agent(), pa.Graph(), pa.Conversation() β†’ full control
"""

from __future__ import annotations

import logging
import os
import re
from typing import Any

from purpose_agent.unified import Agent, Graph, Conversation, parallel, KnowledgeStore, START, END
from purpose_agent.tools import (
    Tool, FunctionTool, CalculatorTool, PythonExecTool,
    ReadFileTool, WriteFileTool, ToolRegistry,
)
from purpose_agent.llm_backend import LLMBackend, MockLLMBackend, ChatMessage
from purpose_agent.types import State
from purpose_agent.orchestrator import TaskResult

logger = logging.getLogger(__name__)


# ═══════════════════════════════════════════════════════════════════════════
# LEVEL 1 β€” The purpose() function. ONE call does everything.
# ═══════════════════════════════════════════════════════════════════════════

def purpose(
    description: str,
    model: str | LLMBackend | None = None,
    local: bool = True,
    knowledge: list[str] | str | None = None,
    tools: list[Tool] | None = None,
    interactive: bool = False,
) -> "Team":
    """
    Build a complete self-improving agent team from a plain English description.

    This is the entry point for everyone. No technical knowledge required.

    Args:
        description: What you want the team to do, in plain English.
                     e.g. "Research and summarize scientific papers"
                     e.g. "Write Python code and test it"
                     e.g. "Analyze CSV files and create reports"

        model: (optional) Which AI model to use.
               - None β†’ auto-detects (Ollama if installed, else mock for testing)
               - "qwen3:1.7b" β†’ local model via Ollama (free, private)
               - "gpt-4o" β†’ OpenAI (needs OPENAI_API_KEY)
               - "Qwen/Qwen3-32B" β†’ HuggingFace cloud (needs HF_TOKEN)

        local: If True (default), prefer local models. Zero cost, full privacy.

        knowledge: (optional) Give your agents knowledge.
                   - List of strings: ["fact 1", "fact 2", ...]
                   - File path: "./docs" or "./data.txt"

        tools: (optional) Extra tools for the agents to use.

        interactive: If True, agents ask for your approval before acting.

    Returns:
        A Team you can run tasks on. It gets smarter with every task.

    Examples:
        # Simplest possible usage
        team = purpose("Help me with coding tasks")
        result = team.run("Write a function to sort a list")
        print(result)

        # With local SLM (free, private)
        team = purpose("Research assistant", model="qwen3:1.7b")
        result = team.run("What are the latest trends in AI?")

        # With knowledge base
        team = purpose("Answer questions about my docs", knowledge="./my_docs/")
        result = team.run("What is our refund policy?")

        # Interactive mode (approve each action)
        team = purpose("File organizer", interactive=True)
        result = team.run("Organize my downloads folder")
    """
    # Auto-detect the best model
    resolved_model = _auto_detect_model(model, local)

    # Analyze the purpose description to pick the right team template
    template = _analyze_purpose(description)

    # Build tools list
    all_tools = list(tools or [])

    # Add template-specific tools
    for tool_cls in template["tools"]:
        all_tools.append(tool_cls())

    # Add knowledge store if provided
    kb = None
    if knowledge:
        kb = _build_knowledge_store(knowledge)
        all_tools.append(kb.as_tool())

    # Build the team
    team = Team(
        purpose=description,
        agents=template["agents"],
        model=resolved_model,
        tools=all_tools,
        interactive=interactive,
        knowledge=kb,
    )

    logger.info(f"βœ… Team created: {template['name']} ({len(template['agents'])} agents)")
    return team


# ═══════════════════════════════════════════════════════════════════════════
# LEVEL 2 β€” Team class. Customizable but still simple.
# ═══════════════════════════════════════════════════════════════════════════

class Team:
    """
    A self-improving team of AI agents.

    Created automatically by purpose(), or build your own:

        team = Team.build(
            purpose="code review assistant",
            agents=["researcher", "coder", "reviewer"],
            model="qwen3:1.7b",
        )
        result = team.run("Review this pull request: ...")
    """

    def __init__(
        self,
        purpose: str,
        agents: list[dict[str, str]],
        model: str | LLMBackend | None = None,
        tools: list[Tool] | None = None,
        interactive: bool = False,
        knowledge: KnowledgeStore | None = None,
    ):
        self.purpose = purpose
        self.interactive = interactive
        self.knowledge = knowledge
        self._history: list[dict] = []

        # Create Agent instances
        self._agents: list[Agent] = []
        first_agent = None
        for spec in agents:
            agent = Agent(
                name=spec["name"],
                instructions=spec.get("role", ""),
                model=model,
                tools=tools,
                handoff_from=first_agent,  # Shared learning!
            )
            self._agents.append(agent)
            if first_agent is None:
                first_agent = agent

        # Create conversation for multi-agent collaboration
        if len(self._agents) > 1:
            self._conversation = Conversation(self._agents)
        else:
            self._conversation = None

    def run(self, task: str, verbose: bool = True) -> str:
        """
        Run a task. Returns a human-readable result string.

        The team gets smarter with every task you give it.

        Args:
            task: What you want done, in plain English.
            verbose: If True, print progress as it happens.

        Returns:
            The result as a simple string.
        """
        if verbose:
            print(f"\nπŸš€ Working on: {task}")
            print(f"   Team: {', '.join(a.name for a in self._agents)}")
            print(f"   Purpose: {self.purpose}")
            print()

        # Single agent β†’ direct run
        if len(self._agents) == 1:
            result = self._agents[0].run(task)
            output = self._format_result(result)
        else:
            # Multi-agent β†’ conversation
            conv_result = self._conversation.run(
                topic=task,
                rounds=2,
                initial_context=f"Team purpose: {self.purpose}",
            )
            output = conv_result.summary or str(conv_result.data.get("final_summary", ""))

        self._history.append({"task": task, "result": output[:500]})

        if verbose:
            print(f"\nβœ… Done!")
            print(f"   Tasks completed: {len(self._history)}")
            print(f"   (The team learns from each task β€” it gets better over time)")

        return output

    def ask(self, question: str) -> str:
        """Shorthand for run() β€” more natural for Q&A use cases."""
        return self.run(question, verbose=False)

    def teach(self, lesson: str) -> None:
        """
        Teach the team something. This goes directly into their memory.

        Example:
            team.teach("Always cite your sources")
            team.teach("When writing code, add docstrings to every function")
        """
        for agent in self._agents:
            from purpose_agent.types import Heuristic, MemoryTier
            h = Heuristic(
                pattern="Always",
                strategy=lesson,
                steps=[],
                tier=MemoryTier.STRATEGIC,
                q_value=1.0,
            )
            agent.orch.optimizer.heuristic_library.append(h)
            agent.orch.sync_memory()
        print(f"πŸ“ Taught all {len(self._agents)} agents: \"{lesson}\"")

    def status(self) -> str:
        """Show what the team has learned."""
        lines = [f"🧠 Team Status: {self.purpose}", ""]

        # Agents
        for agent in self._agents:
            n_heuristics = len(agent.orch.optimizer.heuristic_library)
            n_experiences = agent.orch.experience_replay.size
            lines.append(f"  πŸ€– {agent.name}: {n_heuristics} lessons learned, {n_experiences} experiences")

        # History
        lines.append(f"\n  πŸ“‹ Tasks completed: {len(self._history)}")
        for i, h in enumerate(self._history[-5:], 1):
            lines.append(f"     {i}. {h['task'][:60]}")

        return "\n".join(lines)

    @staticmethod
    def _format_result(result: TaskResult) -> str:
        """Convert a TaskResult into a readable string."""
        data = result.final_state.data
        # Try to get the most useful output
        for key in ["_last_result", "_result", "result", "output", "answer"]:
            if key in data and data[key]:
                return str(data[key])
        if result.final_state.summary:
            return result.final_state.summary
        return str(data)

    @classmethod
    def build(
        cls,
        purpose: str,
        agents: list[str] | list[dict],
        model: str | LLMBackend | None = None,
        tools: list[Tool] | None = None,
    ) -> "Team":
        """
        Build a custom team with named agents.

        Args:
            purpose: What the team does.
            agents: List of agent names or {"name": ..., "role": ...} dicts.
            model: AI model to use.
            tools: Tools available to all agents.

        Example:
            team = Team.build(
                purpose="Content creation",
                agents=["writer", "editor", "fact_checker"],
                model="qwen3:1.7b",
            )
        """
        agent_specs = []
        for a in agents:
            if isinstance(a, str):
                agent_specs.append({"name": a, "role": f"You are the {a}."})
            else:
                agent_specs.append(a)
        return cls(purpose=purpose, agents=agent_specs, model=model, tools=tools)


# ═══════════════════════════════════════════════════════════════════════════
# Auto-Detection & Templates
# ═══════════════════════════════════════════════════════════════════════════

# Pre-built team templates matched by keywords in the purpose description
TEAM_TEMPLATES = {
    "research": {
        "name": "Research Team",
        "keywords": ["research", "find", "search", "discover", "learn", "papers", "study", "investigate", "summarize", "analyze information"],
        "agents": [
            {"name": "researcher", "role": "Find and gather relevant information. Be thorough and cite sources."},
            {"name": "analyst", "role": "Analyze the gathered information. Identify patterns, draw conclusions, and summarize findings clearly."},
        ],
        "tools": [CalculatorTool],
    },
    "coding": {
        "name": "Coding Team",
        "keywords": ["code", "program", "develop", "build", "software", "python", "javascript", "debug", "fix bug", "function", "api", "script"],
        "agents": [
            {"name": "architect", "role": "Design the solution. Break the problem into clear steps before coding."},
            {"name": "coder", "role": "Write clean, well-documented code. Include error handling and comments."},
            {"name": "tester", "role": "Review the code for bugs, edge cases, and improvements. Suggest fixes."},
        ],
        "tools": [PythonExecTool, CalculatorTool],
    },
    "writing": {
        "name": "Writing Team",
        "keywords": ["write", "blog", "article", "essay", "content", "copy", "draft", "edit", "proofread", "report", "documentation"],
        "agents": [
            {"name": "writer", "role": "Write clear, engaging content. Focus on the reader's needs."},
            {"name": "editor", "role": "Review and improve the writing. Fix grammar, clarity, and flow. Be constructive."},
        ],
        "tools": [],
    },
    "data": {
        "name": "Data Team",
        "keywords": ["data", "csv", "excel", "spreadsheet", "database", "sql", "chart", "graph", "statistics", "analytics", "dashboard"],
        "agents": [
            {"name": "analyst", "role": "Analyze data, find patterns, and compute statistics."},
            {"name": "reporter", "role": "Present findings in clear, non-technical language with key takeaways."},
        ],
        "tools": [PythonExecTool, CalculatorTool, ReadFileTool],
    },
    "assistant": {
        "name": "General Assistant",
        "keywords": ["help", "assist", "answer", "question", "explain", "general", "task", "do"],
        "agents": [
            {"name": "assistant", "role": "Help the user with their request. Be helpful, clear, and thorough."},
        ],
        "tools": [CalculatorTool],
    },
}


def _analyze_purpose(description: str) -> dict:
    """Match a purpose description to the best team template."""
    desc_lower = description.lower()
    best_template = None
    best_score = 0

    for template_key, template in TEAM_TEMPLATES.items():
        score = 0
        for keyword in template["keywords"]:
            if keyword in desc_lower:
                score += 1
                # Bonus for exact match
                if f" {keyword} " in f" {desc_lower} ":
                    score += 0.5
        if score > best_score:
            best_score = score
            best_template = template

    # Default to general assistant
    if best_template is None or best_score < 0.5:
        best_template = TEAM_TEMPLATES["assistant"]

    return best_template


def _auto_detect_model(model: str | LLMBackend | None, prefer_local: bool) -> str | LLMBackend:
    """Auto-detect the best available model."""
    if model is not None:
        return model

    # Check for Ollama
    if prefer_local:
        try:
            import urllib.request
            urllib.request.urlopen("http://localhost:11434/api/tags", timeout=2)
            logger.info("🟒 Ollama detected β€” using local models (free, private)")
            return "qwen3:1.7b"
        except Exception:
            pass

    # Check for API keys
    if os.environ.get("OPENAI_API_KEY"):
        logger.info("πŸ”‘ OpenAI API key found β€” using gpt-4o-mini")
        return "gpt-4o-mini"

    # Fallback: mock for testing
    logger.info(
        "πŸ’‘ No local model detected. Using mock backend for testing.\n"
        "   To use a real model:\n"
        "   β€’ Install Ollama: https://ollama.ai (free, local, private)\n"
        "   β€’ Or set OPENAI_API_KEY for OpenAI\n"
        "   β€’ Or set HF_TOKEN for HuggingFace"
    )
    return MockLLMBackend()


def _build_knowledge_store(knowledge: list[str] | str) -> KnowledgeStore:
    """Build a KnowledgeStore from various input types."""
    if isinstance(knowledge, list):
        return KnowledgeStore.from_texts(knowledge)
    elif os.path.isdir(knowledge):
        return KnowledgeStore.from_directory(knowledge, glob="*.*")
    elif os.path.isfile(knowledge):
        kb = KnowledgeStore()
        kb.add_file(knowledge)
        return kb
    else:
        # Treat as a single text
        return KnowledgeStore.from_texts([knowledge])


# ═══════════════════════════════════════════════════════════════════════════
# CLI Quickstart Wizard
# ═══════════════════════════════════════════════════════════════════════════

def quickstart():
    """
    Interactive wizard for creating an agent team. Run from command line:

        python -m purpose_agent

    Walks the user through setup step by step.
    """
    print()
    print("╔══════════════════════════════════════════════════════════╗")
    print("β•‘        🧠 Purpose Agent β€” Quickstart Wizard            β•‘")
    print("β•‘  Build a self-improving AI team in under 60 seconds.   β•‘")
    print("β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•")
    print()

    # Step 1: What's your purpose?
    print("Step 1: What do you want your AI team to do?")
    print("  Examples: 'research assistant', 'code helper', 'content writer'")
    print()
    user_purpose = input("  Your purpose: ").strip()
    if not user_purpose:
        user_purpose = "general assistant"
    print()

    # Step 2: Model selection
    print("Step 2: Which AI model? (press Enter for auto-detect)")
    print("  β€’ Enter     β†’ auto-detect (recommended)")
    print("  β€’ 'local'   β†’ use Ollama (free, private)")
    print("  β€’ 'cloud'   β†’ use HuggingFace cloud")
    print("  β€’ 'openai'  β†’ use OpenAI")
    print()
    model_choice = input("  Model: ").strip().lower()

    if model_choice == "local":
        model = "qwen3:1.7b"
    elif model_choice == "cloud":
        model = "Qwen/Qwen3-32B"
    elif model_choice == "openai":
        model = "gpt-4o-mini"
    elif model_choice:
        model = model_choice
    else:
        model = None  # auto-detect
    print()

    # Step 3: Knowledge?
    print("Step 3: Do you have documents for your team to learn from?")
    print("  β€’ Enter         β†’ no documents")
    print("  β€’ folder path   β†’ load all files from folder")
    print("  β€’ file path     β†’ load a specific file")
    print()
    knowledge_input = input("  Documents: ").strip()
    knowledge = knowledge_input if knowledge_input else None
    print()

    # Build!
    print("━" * 50)
    print("Building your team...")
    print()

    team = purpose(user_purpose, model=model, knowledge=knowledge)

    print()
    print("βœ… Your team is ready!")
    print()
    print("Try it now β€” type a task (or 'quit' to exit):")
    print()

    while True:
        try:
            task = input("πŸ“ Task: ").strip()
        except (EOFError, KeyboardInterrupt):
            print("\nπŸ‘‹ Goodbye!")
            break

        if not task or task.lower() in ("quit", "exit", "q"):
            print("\nπŸ‘‹ Goodbye!")
            break

        if task.lower() == "status":
            print(team.status())
            continue

        if task.lower().startswith("teach:"):
            lesson = task[6:].strip()
            team.teach(lesson)
            continue

        result = team.run(task)
        print(f"\nπŸ’‘ Result:\n{result}\n")