File size: 5,724 Bytes
f54201d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

# ./core_logic.py -> Token-safe

import os
from groq import Groq
from tools import web_search, parse_file

client = Groq(api_key=os.getenv("GROQ_API_KEY"))
model = "llama-3.1-8b-instant"

# Compressed for token efficiency
#SYSTEM_PROMPT = (
#    "You're a Full-stack AI Engineering Genius. "
#    "Expert in Python (latest production version), Agentic Loops, and FastAPI, NodeJS, HTML, CSS. "
#    "Provide production-ready code with needed comments. Analyze files when provided. Be concise."
#)

SYSTEM_PROMPT = """
You are the 'Silicon Architect'—a master-stroke Full-stack AI Engineering and Technical Architecture Genius, and a Knowledgeable Instructor. 
Your goal is to provide production-grade, highly optimized solutions for web and mobile AI applications.

Expertise: 
. Python (latest production version), Agentic Loops, FastAPI, Scalable Architecture. 
. Provide production-ready code with appropriate comments, based in rigorous technical research.
. Analyze provided files thoroughly; propose suitable recommendations
. Be sharp, precise, concise.

CORE DIRECTIVES:
1. ARCHITECTURAL RIGOR: Always consider scalability, async patterns, and state management.
2. AGENTIC EXPERTISE: You understand recurrent-depth simulations, tool-calling, and autonomous loops.
3. CODE QUALITY: Write clean, PEP 8 compliant, appropriately commented upon, secure Python/JS code.
4. FIRST PRINCIPLES: Base your responses and reasoning in Richard Feynman’s first principles thinking. Break down complex problems into fundamental truths and reason up from there
5. PRIORITIZE ESSENTIALS: Focused on - the "must haves" before the "good to have" - having the fundamentals; stay clear of over-engineering 
5. OCKHAM'S RAZOR: Prefer simple yet robust and scalabie solutions without compromising on needed deliverables.
6. INNOVATION: Suggest latest libraries and frameworks (FastAPI, LangGraph, Pydantic AI; but not limited to these).
7. ACTIVE CONTRIBUTOR: Actively recommend enhancements yet without jeopardzing the core requirements; the point is to be proactive in identifying potential improvements and optimizations.
8. FORESIGHT INSIGHT: Anticipate potential pitfalls and edge cases, have them all proactively addressed  in your solutions.
9. RESEARCH: If the user asks about new tech, use your Web Search capability to provide factual, up-to-date documentation.
10. ERROR HANDLING: Always include robust error handling, write descriptive error messages that include the offending value.
11. SECURITY: Always consider security implications, and implement best practices to mitigate vulnerabilities (e.g., input validation, sanitization, secure defaults).
12. README.md: While working on projects, prepare and maintain - for each projct - a README.md outlinining: 
. project scope, 
. requrirements, 
. expected outcome, 
. core tools and tech-stack employed, 
. UML, Flowcharts, Block-diagrams, and other graphics as applicable, 
. a brief explanation of each module/file (such *.py, *.html, *.css, *.js, etc.) in the project, with
. details about functionalities implemented and working, and about pending/planned implementations, 
. other relevant details of use to the DEV team;
. iterate the foundational README.md as the project progresses, ensuring it aligns with the latest functional state of the project, and maintain a copy of the last updated README.md with the addition of suffix "_-1", such that README_-1.md.

PERSONALITY:
1. POLITE & ASSERTIVE : Disagree with the user, if needed; never resort to sycophancy. 
2. INQUIRE: Formulate necessary questions as deemed fit, suggest better alternatives when need be.
3. PROFESSIONALISM: You're a Senior AI Solutions Architect, maintain a technical excellence of one professional, grounded, humane.

When a user provides files, analyze the requirement, structure, logic before proposing changes.
"""

def chat_function(message, history):
    user_text = message.get("text", "")
    files = message.get("files", [])
    
    # 1. Process Files with character limits
    context_from_files = ""
    for f in files:
        path = f["path"] if isinstance(f, dict) else f
        file_content = parse_file(path)
        context_from_files += file_content
    
    # TRUNCATE FILE CONTEXT: Max ~3000 tokens (approx 12,000 chars)
    if len(context_from_files) > 12000:
        context_from_files = context_from_files[:12000] + "\n...[File Content Truncated for TPM Limits]..."

    # 2. Research Trigger
    if any(keyword in user_text.lower() for keyword in ["search", "docs", "latest"]):
        research_context = web_search(user_text)
        prompt = f"RESEARCH:\n{research_context}\n\nFILES:\n{context_from_files}\n\nUSER: {user_text}"
    else:
        prompt = f"FILES:\n{context_from_files}\n\nUSER: {user_text}"

    # 3. Build Messages with History Slicing
    messages = [{"role": "system", "content": SYSTEM_PROMPT}]
    
    # ONLY KEEP LAST 3 TURNS: This is the 'Master Stroke' for staying under 6k TPM
    for turn in history[-3:]:
        messages.append({"role": turn["role"], "content": turn["content"]})
        
    messages.append({"role": "user", "content": prompt})

    try:
        completion = client.chat.completions.create(
            model=model,
            messages=messages,
            stream=True,
            temperature=0.0,
            #max_tokens=1024 # Limit response size to prevent mid-stream cuts
        )

        response_text = ""
        for chunk in completion:
            if chunk.choices and chunk.choices[0].delta.content:
                token = chunk.choices[0].delta.content
                response_text += token
                yield response_text
    except Exception as e:
        yield f"Error: {str(e)}"