File size: 7,786 Bytes
cfbe38d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
ZODER TERMINAL AGENT WRAPPER (CROSS-PLATFORM)
Supports: Termux (Android), Linux, macOS, Windows (WSL/CMD)
Created by: Komandan Nasa - Bakso Bangi Pak Romdani
"""

import os
import sys
import re
import json
import platform
import subprocess

# Try to import requests, auto-install if missing
try:
    import requests
except ImportError:
    print("⏳ Installing required library 'requests'...")
    subprocess.check_call([sys.executable, "-m", "pip", "install", "requests"])
    import requests

# Try to import rich for UI, fallback to plain text if missing/fails
USE_RICH = False
try:
    from rich.console import Console
    from rich.panel import Panel
    from rich.prompt import Prompt
    USE_RICH = True
except ImportError:
    try:
        print("⏳ Installing optional UI library 'rich'...")
        subprocess.check_call([sys.executable, "-m", "pip", "install", "rich"])
        from rich.console import Console
        from rich.panel import Panel
        from rich.prompt import Prompt
        USE_RICH = True
    except:
        USE_RICH = False

# ============================================================
#  CONFIGURATION
# ============================================================
OLLAMA_HOST = os.environ.get("ZODER_OLLAMA_HOST", "http://localhost:11434")
MODEL_NAME = os.environ.get("ZODER_MODEL_NAME", "zoder")
HISTORY_FILE = os.path.expanduser("~/.zoder_history.json")

if USE_RICH:
    console = Console()


def print_ui(text, style="default"):
    """Cross-platform print function"""
    if USE_RICH:
        if style == "banner":
            console.print(Panel.fit(text, border_style="cyan"))
        elif style == "user":
            console.print(f"[bold cyan]πŸ‘€ You:[/bold cyan] {text}")
        elif style == "zoder":
            console.print(f"[bold magenta]πŸ€– Zoder:[/bold magenta] ", end="")
        elif style == "success":
            console.print(f"[bold green] βœ… {text}[/bold green]")
        elif style == "error":
            console.print(f"[bold red] ❌ {text}[/bold red]")
        elif style == "info":
            console.print(f"[dim]{text}[/dim]")
        else:
            console.print(text)
    else:
        # Plain text fallback for basic terminals
        if style == "banner":
            print("\n" + "="*50)
            print(text)
            print("="*50 + "\n")
        elif style == "user":
            print(f"\nπŸ‘€ You: {text}")
        elif style == "zoder":
            print(f"\nπŸ€– Zoder: ", end="")
        elif style == "success":
            print(f"βœ… {text}")
        elif style == "error":
            print(f"❌ {text}")
        else:
            print(text)


def banner():
    os_info = f"{platform.system()} {platform.release()}"
    if "ANDROID_ROOT" in os.environ or "TERMUX_VERSION" in os.environ:
        os_info = "Android Termux"
    
    banner_text = (
        "[bold cyan]πŸ€– ZODER OS AGENT v1.0[/bold cyan]\n"
        "[dim]Created by Komandan Nasa - Bakso Bangi Pak Romdani[/dim]\n"
        f"[dim]Model: Zoder1.0-1B | System: {os_info}[/dim]"
    ) if USE_RICH else (
        f"πŸ€– ZODER OS AGENT v1.0\n"
        f"Created by Komandan Nasa - Bakso Bangi Pak Romdani\n"
        f"Model: Zoder1.0-1B | System: {os_info}"
    )
    print_ui(banner_text, "banner")


def clean_markdown(text):
    """Remove all markdown and HTML formatting from AI response"""
    text = re.sub(r'\*\*(.*?)\*\*', r'\1', text)
    text = re.sub(r'\*(.*?)\*', r'\1', text)
    text = re.sub(r'__(.*?)__', r'\1', text)
    text = re.sub(r'_(.*?)_', r'\1', text)
    text = re.sub(r'^#{1,6}\s+', '', text, flags=re.MULTILINE)
    text = re.sub(r'```[\s\S]*?```', '', text)
    text = re.sub(r'`(.*?)`', r'\1', text)
    text = re.sub(r'<[^>]+>', '', text)
    return text.strip()


def extract_and_create_files(text):
    """Detect [CREATE_FILE] tags and write files to disk"""
    pattern = r'\[CREATE_FILE:\s*(.*?)\]\s*([\s\S]*?)\[/CREATE_FILE\]'
    matches = re.findall(pattern, text)
    
    clean_text = re.sub(pattern, '', text).strip()
    
    for filename, content in matches:
        filename = filename.strip()
        filepath = os.path.join(os.getcwd(), filename)
        
        try:
            dir_name = os.path.dirname(filepath)
            if dir_name:
                os.makedirs(dir_name, exist_ok=True)
            
            with open(filepath, 'w', encoding='utf-8') as f:
                f.write(content.strip())
            
            print_ui(f"File created: {filepath}", "success")
        except Exception as e:
            print_ui(f"Failed to create file {filename}: {e}", "error")
    
    return clean_text


def load_history():
    if os.path.exists(HISTORY_FILE):
        try:
            with open(HISTORY_FILE, 'r', encoding='utf-8') as f:
                return json.load(f)
        except:
            return []
    return []


def save_history(history):
    try:
        with open(HISTORY_FILE, 'w', encoding='utf-8') as f:
            json.dump(history[-20:], f, ensure_ascii=False, indent=2)
    except:
        pass


def chat_with_ollama(messages):
    try:
        response = requests.post(
            f"{OLLAMA_HOST}/api/chat",
            json={
                "model": MODEL_NAME,
                "messages": messages,
                "stream": True
            },
            stream=True,
            timeout=300
        )
        response.raise_for_status()
        return response
    except requests.exceptions.ConnectionError:
        print_ui(f"Cannot connect to Ollama server at {OLLAMA_HOST}!", "error")
        print_ui("Make sure Ollama is running: ollama serve", "info")
        sys.exit(1)


def run_agent():
    banner()
    history = load_history()
    print_ui("Type '/clear' to reset history, '/exit' to quit.", "info")

    while True:
        try:
            if USE_RICH:
                from rich.prompt import Prompt
                user_input = Prompt.ask("[bold cyan]πŸ‘€ You[/bold cyan]")
            else:
                user_input = input("\nπŸ‘€ You: ")
            
            if user_input.lower() == '/exit':
                print_ui("Sampai jumpa, Komandan Nasa!", "info")
                break
                
            if user_input.lower() == '/clear':
                history = []
                save_history(history)
                print_ui("History cleared.", "success")
                continue

            if not user_input.strip():
                continue

            history.append({"role": "user", "content": user_input})
            print_ui("", "zoder")
            
            full_response = ""
            response = chat_with_ollama(history)
            
            for line in response.iter_lines():
                if line:
                    try:
                        data = json.loads(line.decode('utf-8'))
                        if "message" in data and "content" in data["message"]:
                            token = data["message"]["content"]
                            full_response += token
                            sys.stdout.write(token)
                            sys.stdout.flush()
                    except json.JSONDecodeError:
                        continue
            
            print() 
            
            processed_response = clean_markdown(full_response)
            final_response = extract_and_create_files(processed_response)
            
            history.append({"role": "assistant", "content": final_response})
            save_history(history)
            print()

        except KeyboardInterrupt:
            print_ui("\nSampai jumpa, Komandan Nasa!", "info")
            break
        except Exception as e:
            print_ui(f"Agent Error: {e}", "error")


if __name__ == "__main__":
    run_agent()