Spaces:
Sleeping
Sleeping
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> | |
| <title>🇮🇳 TIRANGA - Advanced AI</title> | |
| <link rel="preconnect" href="https://fonts.googleapis.com"> | |
| <link href="https://fonts.googleapis.com/css2?family=Instrument+Serif:ital@0;1&family=Inter:wght@300;400;500;600;700&family=Geist+Mono:wght@400;500&display=swap" rel="stylesheet"> | |
| <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github.min.css"> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/marked/12.0.1/marked.min.js"></script> | |
| <style> | |
| /* --- BASE RESET & VARIABLES --- */ | |
| *{margin:0;padding:0;box-sizing:border-box;} | |
| :root{ | |
| --bg-color: #ffffff; --bg-secondary: #f9f9fb; --text-main: #1d1d1f; --text-muted: #86868b; | |
| --accent: #FF9933; --accent-green: #138808; --accent-hover: #e68a2e; | |
| --border-light: rgba(0, 0, 0, 0.08); --border-focus: rgba(255, 153, 51, 0.3); | |
| --glass-bg: rgba(255, 255, 255, 0.85); --glass-border: rgba(255, 255, 255, 0.4); | |
| --shadow-sm: 0 2px 8px rgba(0,0,0,0.04); --shadow-md: 0 8px 32px rgba(0,0,0,0.08); | |
| --shadow-lg: 0 16px 48px rgba(0,0,0,0.12); | |
| --font-serif: 'Instrument Serif', serif; --font-sans: 'Inter', -apple-system, sans-serif; | |
| --font-mono: 'Geist Mono', monospace; | |
| --radius-lg: 24px; --radius-md: 16px; --radius-sm: 8px; | |
| } | |
| html,body{height:100%;overflow:hidden;background:var(--bg-color);color:var(--text-main);font-family:var(--font-sans);-webkit-font-smoothing:antialiased;} | |
| /* --- TIRANGA BACKGROUND --- */ | |
| .bg{position:fixed;inset:0;z-index:0;overflow:hidden;pointer-events:none;background: linear-gradient(135deg, rgba(255,153,51,0.05) 0%, rgba(255,255,255,1) 50%, rgba(19,136,8,0.05) 100%);} | |
| .orb{position:absolute;border-radius:50%;filter:blur(100px);opacity:0.5;animation:drift 25s ease-in-out infinite alternate;} | |
| .orb1{width:600px;height:600px;background:radial-gradient(circle,rgba(255,153,51,0.2),transparent 70%);top:-150px;left:-100px;} | |
| .orb2{width:500px;height:500px;background:radial-gradient(circle,rgba(19,136,8,0.18),transparent 70%);bottom:-100px;right:-100px;animation-delay:-10s;} | |
| @keyframes drift{0%{transform:translate(0,0) scale(1);}100%{transform:translate(60px,40px) scale(1.05);}} | |
| /* --- MAIN LAYOUT (CHAT + WORKSPACE) --- */ | |
| .shell{position:relative;z-index:1;display:flex;height:100vh;width:100vw;overflow:hidden;} | |
| .sidebar{width:280px;display:flex;flex-direction:column;background:var(--bg-secondary);border-right:1px solid var(--border-light);flex-shrink:0;} | |
| .main-wrapper{flex:1;display:flex;position:relative;overflow:hidden;} | |
| .chat-main{flex:1;display:flex;flex-direction:column;background:transparent;position:relative;min-width:300px;transition:all 0.3s cubic-bezier(0.4, 0, 0.2, 1);} | |
| .workspace{width:0;background:#fafafa;border-left:1px solid var(--border-light);display:flex;flex-direction:column;transition:all 0.3s cubic-bezier(0.4, 0, 0.2, 1);overflow:hidden;box-shadow:-10px 0 30px rgba(0,0,0,0.02);} | |
| .workspace.open{width:50%;} | |
| /* --- SIDEBAR & BRANDING --- */ | |
| .logo-area{padding:24px 20px;border-bottom:1px solid var(--border-light);} | |
| .logo-row{display:flex;align-items:center;gap:12px;} | |
| .logo-img{width:44px;height:44px;border-radius:12px;object-fit:cover;box-shadow:var(--shadow-sm);border:1px solid var(--border-light);background:#fff;} | |
| .logo-text{line-height:1.2;} | |
| .logo-name{font-family:var(--font-sans);font-weight:800;font-size:18px;color:var(--text-main);letter-spacing:-0.5px;} | |
| .logo-sub{font-size:10px;font-weight:600;letter-spacing:1px;text-transform:uppercase;color:var(--text-muted);} | |
| .settings{padding:20px;flex:1;overflow-y:auto;display:flex;flex-direction:column;} | |
| .settings::-webkit-scrollbar{width:4px;} | |
| .settings::-webkit-scrollbar-thumb{background:rgba(0,0,0,0.1);border-radius:10px;} | |
| .brand-box {background: #ffffff;border: 1px solid var(--border-light);border-radius: var(--radius-md);padding: 24px 16px;text-align: center;box-shadow: var(--shadow-sm);margin-top:20px;} | |
| .brand-box img {width: 70px; height: 70px; border-radius: 16px; object-fit: cover; margin-bottom: 12px; box-shadow: var(--shadow-sm); border: 1px solid var(--border-light);} | |
| .brand-box h3 {font-family: var(--font-sans); font-size: 15px; font-weight: 700; color: var(--text-main); margin-bottom: 6px;} | |
| .brand-box p {font-size: 10px; font-weight: 600; color: var(--accent-green); line-height: 1.4; letter-spacing: 0.5px;} | |
| .clear-btn{width:100%;padding:12px;border-radius:var(--radius-sm);background:#ffffff;border:1px solid var(--border-light);color:var(--text-main);font-size:13px;font-weight:600;cursor:pointer;transition:all 0.2s;margin-top:auto;} | |
| .clear-btn:hover{background:#fff0f0;color:#ff3b30;border-color:#ff3b30;} | |
| /* --- CHAT AREA --- */ | |
| .chat-hdr{padding:14px 24px;border-bottom:1px solid var(--border-light);background:var(--glass-bg);backdrop-filter:blur(20px);-webkit-backdrop-filter:blur(20px);display:flex;align-items:center;justify-content:space-between;z-index:10;} | |
| .model-badge{font-size:13px;font-weight:600;display:flex;align-items:center;gap:8px;} | |
| .status-dot{width:8px;height:8px;border-radius:50%;background:#34c759;box-shadow:0 0 0 2px rgba(52,199,89,0.2);} | |
| .messages{flex:1;overflow-y:auto;padding:30px 40px;display:flex;flex-direction:column;gap:24px;scroll-behavior:smooth;position:relative;} | |
| .messages::-webkit-scrollbar{width:6px;} | |
| .messages::-webkit-scrollbar-thumb{background:rgba(0,0,0,0.1);border-radius:10px;} | |
| .welcome{display:flex;flex-direction:column;align-items:center;justify-content:center;text-align:center;padding:40px;margin:auto;animation:fadeIn 0.5s ease-out;width:100%;max-width:600px;} | |
| @keyframes fadeIn{from{opacity:0;transform:translateY(10px);}to{opacity:1;transform:none;}} | |
| .welcome-logo{width:80px;height:80px;border-radius:24px;object-fit:cover;box-shadow:var(--shadow-md);margin-bottom:24px;border:1px solid var(--border-light);} | |
| .welcome-title{font-size:32px;font-weight:800;letter-spacing:-1px;margin-bottom:12px;} | |
| .welcome-sub{font-size:15px;color:var(--text-muted);line-height:1.6;margin-bottom:32px;} | |
| .msg{display:flex;gap:16px;animation:msgIn 0.3s cubic-bezier(0.16, 1, 0.3, 1) both;max-width:85%;} | |
| @keyframes msgIn{from{opacity:0;transform:translateY(10px);}to{opacity:1;transform:none;}} | |
| .msg.user{flex-direction:row-reverse;align-self:flex-end;} | |
| .msg.bot{align-self:flex-start;} | |
| .avatar{width:36px;height:36px;border-radius:50%;flex-shrink:0;display:flex;align-items:center;justify-content:center;font-size:14px;font-weight:600;overflow:hidden;} | |
| .msg.user .avatar{background:#f0f0f0;color:var(--text-main);border:1px solid var(--border-light);} | |
| .msg.bot .avatar{border:1px solid var(--border-light);background:#fff;padding:2px;} | |
| .msg.bot .avatar img{width:100%;height:100%;border-radius:50%;object-fit:cover;} | |
| .body{display:flex;flex-direction:column;gap:6px;max-width:100%;} | |
| .msg.user .body{align-items:flex-end;} | |
| .bubble{padding:16px 20px;font-size:15px;line-height:1.6;color:var(--text-main);position:relative;} | |
| .msg.user .bubble{background:var(--text-main);color:#ffffff;border-radius:20px 20px 4px 20px;box-shadow:var(--shadow-sm);} | |
| .msg.bot .bubble{background:#ffffff;border:1px solid var(--border-light);border-radius:4px 20px 20px 20px;box-shadow:var(--shadow-sm);width:100%;} | |
| /* BOT ACTIONS (Copy Button below text) */ | |
| .msg-actions{display:flex;gap:8px;margin-top:4px;padding-left:12px;opacity:0;transition:opacity 0.2s;} | |
| .msg.bot:hover .msg-actions{opacity:1;} | |
| .action-btn{background:transparent;border:none;color:var(--text-muted);font-size:12px;cursor:pointer;display:flex;align-items:center;gap:4px;padding:4px 8px;border-radius:6px;transition:all 0.2s;} | |
| .action-btn:hover{background:var(--bg-secondary);color:var(--text-main);} | |
| /* Markdown inside bubble */ | |
| .bubble p{margin-bottom:12px;} | |
| .bubble p:last-child{margin-bottom:0;} | |
| .bubble code{font-family:var(--font-mono);background:rgba(0,0,0,0.05);padding:3px 6px;border-radius:6px;font-size:0.85em;color:#e83e8c;} | |
| .msg.user .bubble code{background:rgba(255,255,255,0.2);color:#fff;} | |
| .bubble img{max-width:100%;border-radius:12px;margin:10px 0;box-shadow:var(--shadow-sm);border:1px solid var(--border-light);} | |
| /* Generated Image Loader */ | |
| .img-loader {display:flex;align-items:center;gap:12px;padding:12px;background:rgba(255,153,51,0.1);border:1px solid rgba(255,153,51,0.3);border-radius:12px;color:var(--accent);font-weight:600;font-size:14px;margin-bottom:10px;} | |
| .spinner {width:20px;height:20px;border:3px solid rgba(255,153,51,0.3);border-top-color:var(--accent);border-radius:50%;animation:spin 1s linear infinite;} | |
| @keyframes spin{to{transform:rotate(360deg);}} | |
| /* Workspace Trigger Card in Chat */ | |
| .ws-trigger-card {background: var(--bg-secondary);border: 1px solid var(--border-light);border-radius: 12px;padding: 16px;margin: 12px 0;display: flex;align-items: center;justify-content: space-between;} | |
| .ws-trigger-info {display: flex;align-items: center;gap: 12px;} | |
| .ws-trigger-icon {width: 40px;height: 40px;border-radius: 10px;background: #fff;display: flex;align-items: center;justify-content: center;font-size: 20px;box-shadow: var(--shadow-sm);border: 1px solid var(--border-light);} | |
| .ws-trigger-text h4 {font-size: 14px;font-weight: 600;margin-bottom: 2px;} | |
| .ws-trigger-text p {font-size: 12px;color: var(--text-muted);font-family: var(--font-mono);} | |
| .ws-btn {padding: 8px 16px;background: var(--text-main);color: #fff;border: none;border-radius: 8px;font-size: 13px;font-weight: 500;cursor: pointer;transition: transform 0.2s;} | |
| .ws-btn:hover {transform: scale(1.05);} | |
| /* Typing indicator */ | |
| .typing{display:flex;align-items:center;gap:6px;padding:4px;} | |
| .typing span{width:6px;height:6px;border-radius:50%;background:var(--text-muted);opacity:0.4;animation:bounce 1.4s infinite ease-in-out both;} | |
| .typing span:nth-child(1){animation-delay:-0.32s;}.typing span:nth-child(2){animation-delay:-0.16s;} | |
| @keyframes bounce{0%,80%,100%{transform:scale(0);}40%{transform:scale(1);}} | |
| /* --- SCROLL TO BOTTOM BUTTON --- */ | |
| .scroll-bottom-btn {position:absolute;bottom:100px;right:40px;width:40px;height:40px;border-radius:50%;background:var(--glass-bg);backdrop-filter:blur(10px);border:1px solid var(--border-light);box-shadow:var(--shadow-md);display:flex;align-items:center;justify-content:center;cursor:pointer;font-size:18px;color:var(--text-main);opacity:0;pointer-events:none;transform:translateY(20px);transition:all 0.3s cubic-bezier(0.4, 0, 0.2, 1);z-index:20;} | |
| .scroll-bottom-btn.show {opacity:1;pointer-events:auto;transform:translateY(0);} | |
| .scroll-bottom-btn:hover {background:#fff;transform:translateY(-3px);} | |
| /* --- INPUT AREA (WITH INTEGRATED ATTACHMENT) --- */ | |
| .input-container{padding:0 40px 30px;background:linear-gradient(to top, var(--bg-color) 70%, transparent);position:relative;z-index:20;} | |
| .input-wrap{display:flex;flex-direction:column;background:var(--glass-bg);backdrop-filter:blur(24px);-webkit-backdrop-filter:blur(24px);border:1px solid var(--border-light);border-radius:24px;box-shadow:var(--shadow-md);transition:box-shadow 0.3s, border-color 0.3s;} | |
| .input-wrap:focus-within{border-color:var(--border-focus);box-shadow:0 12px 40px rgba(0,0,0,0.1), 0 0 0 3px rgba(255,153,51,0.1);} | |
| /* Image Preview Inside Input */ | |
| .img-prev-inline{display:none;padding:12px 20px 0;align-items:center;gap:12px;} | |
| .img-prev-inline.show{display:flex;animation:fadeIn 0.3s;} | |
| .inline-th{width:40px;height:40px;object-fit:cover;border-radius:8px;border:1px solid var(--border-light);} | |
| .inline-info{flex:1;} | |
| .inline-nm{font-size:12px;font-weight:600;color:var(--text-main);white-space:nowrap;overflow:hidden;text-overflow:ellipsis;max-width:150px;} | |
| .inline-rm{background:rgba(0,0,0,0.05);border:none;width:24px;height:24px;border-radius:50%;cursor:pointer;font-size:10px;display:flex;align-items:center;justify-content:center;transition:background 0.2s;} | |
| .inline-rm:hover{background:#ff3b30;color:#fff;} | |
| .input-row{display:flex;align-items:flex-end;gap:12px;padding:10px 14px 10px 20px;} | |
| .chat-ta{flex:1;border:none;outline:none;background:transparent;font-family:var(--font-sans);font-size:15px;color:var(--text-main);line-height:1.5;resize:none;max-height:200px;min-height:24px;padding:10px 0;} | |
| .chat-ta::placeholder{color:var(--text-muted);} | |
| .input-acts{display:flex;align-items:center;gap:8px;padding-bottom:6px;} | |
| /* Modern Attachment Button */ | |
| .attach-btn{width:36px;height:36px;border-radius:12px;background:transparent;border:1px solid transparent;display:flex;align-items:center;justify-content:center;cursor:pointer;color:var(--text-muted);transition:all 0.2s;position:relative;overflow:hidden;} | |
| .attach-btn:hover{background:rgba(0,0,0,0.04);color:var(--text-main);} | |
| .attach-btn input{position:absolute;inset:0;opacity:0;cursor:pointer;} | |
| .attach-btn svg{width:20px;height:20px;} | |
| /* Tiranga Gradient Send Button */ | |
| .send-btn{width:40px;height:40px;border-radius:14px;border:none;background:linear-gradient(135deg, #FF9933 0%, #ffffff 50%, #138808 100%);display:flex;align-items:center;justify-content:center;cursor:pointer;transition:all 0.3s;box-shadow:0 4px 12px rgba(0,0,0,0.1);position:relative;} | |
| .send-btn::before{content:'';position:absolute;inset:1px;background:#1d1d1f;border-radius:13px;transition:opacity 0.3s;} /* Dark inner to make icon pop, or keep gradient */ | |
| /* Let's make the arrow black on the gradient, no dark inner needed */ | |
| .send-btn::before{display:none;} | |
| .send-btn svg{width:18px;height:18px;fill:#1d1d1f;z-index:1;transition:transform 0.2s;} | |
| .send-btn:hover{box-shadow:0 6px 16px rgba(255,153,51,0.3);} | |
| .send-btn:hover svg{transform:translate(2px, -2px);} | |
| .send-btn:disabled{opacity:0.4;cursor:not-allowed;filter:grayscale(100%);} | |
| .input-hint{text-align:center;font-size:11px;color:var(--text-muted);margin-top:12px;font-weight:500;} | |
| /* --- WORKSPACE PANEL --- */ | |
| .ws-hdr{padding:14px 20px;border-bottom:1px solid var(--border-light);background:#fff;display:flex;align-items:center;justify-content:space-between;flex-shrink:0;} | |
| .ws-title{font-size:14px;font-weight:600;display:flex;align-items:center;gap:8px;} | |
| .ws-actions{display:flex;gap:8px;} | |
| .ws-icon-btn{width:32px;height:32px;border-radius:8px;background:#f5f5f7;border:none;cursor:pointer;display:flex;align-items:center;justify-content:center;color:var(--text-main);transition:all 0.2s;font-size:13px;} | |
| .ws-icon-btn:hover{background:#e5e5ea;} | |
| .ws-close{background:transparent;color:var(--text-muted);} | |
| .ws-close:hover{color:#ff3b30;} | |
| .ws-content{flex:1;overflow-y:auto;background:#fafafa;padding:20px;} | |
| .ws-content pre{margin:0;padding:20px;background:#fff;border:1px solid var(--border-light);border-radius:12px;font-family:var(--font-mono);font-size:14px;line-height:1.6;overflow-x:auto;box-shadow:var(--shadow-sm);} | |
| /* --- RESPONSIVE MOBILE --- */ | |
| @media(max-width:850px){ | |
| .sidebar{position:absolute;left:-280px;z-index:100;height:100%;transition:left 0.3s;} | |
| .sidebar.show{left:0;box-shadow:var(--shadow-lg);} | |
| .workspace.open{position:absolute;top:0;left:0;width:100%;height:100%;z-index:50;} | |
| .messages{padding:20px;} | |
| .input-container{padding:0 20px 20px;} | |
| .msg{max-width:95%;} | |
| .scroll-bottom-btn{right:20px;bottom:90px;} | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="bg"> | |
| <div class="orb orb1"></div> | |
| <div class="orb orb2"></div> | |
| </div> | |
| <div class="shell"> | |
| <!-- Sidebar --> | |
| <aside class="sidebar" id="sidebar"> | |
| <div class="logo-area"> | |
| <div class="logo-row"> | |
| <img src="https://i.ibb.co/x8m7DbMk/TIRANGA-20260523-101648-0000.png" alt="Tiranga Logo" class="logo-img"> | |
| <div class="logo-text"> | |
| <div class="logo-name">TIRANGA</div> | |
| <div class="logo-sub">Workspace Edition</div> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="settings"> | |
| <div class="brand-box"> | |
| <img src="https://i.ibb.co/x8m7DbMk/TIRANGA-20260523-101648-0000.png" alt="Tiranga Identity"> | |
| <h3>TIRANGA</h3> | |
| <p>ALWAYS BE FORWARD NEVER BE BACKWARD!!</p> | |
| </div> | |
| <button class="clear-btn" onclick="clearMemory()">🗑️ Clear Chat Memory</button> | |
| </div> | |
| </aside> | |
| <!-- Main Chat Area --> | |
| <div class="main-wrapper"> | |
| <main class="chat-main" id="chatMain"> | |
| <header class="chat-hdr"> | |
| <div class="model-badge"> | |
| <div class="status-dot"></div> | |
| TIRANGA Intelligence | |
| </div> | |
| </header> | |
| <div class="messages" id="msgs" onscroll="checkScroll()"> | |
| <!-- Welcome Screen --> | |
| <div class="welcome" id="welcome"> | |
| <img src="https://i.ibb.co/x8m7DbMk/TIRANGA-20260523-101648-0000.png" alt="Tiranga Logo" class="welcome-logo"> | |
| <div class="welcome-title">Hello There</div> | |
| <div class="welcome-sub"> I am TIRANGA. For your convenience, I have added new features like memory, workspace, and image generation. Tell me, what shall we create today?"</div> | |
| </div> | |
| </div> | |
| <!-- Scroll to bottom button --> | |
| <button class="scroll-bottom-btn" id="scrollBtn" onclick="forceScrollDown()"> | |
| <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 5v14M19 12l-7 7-7-7"/></svg> | |
| </button> | |
| <!-- Input Area --> | |
| <div class="input-container"> | |
| <div class="input-wrap"> | |
| <!-- Inline Image Preview --> | |
| <div class="img-prev-inline" id="imgPrev"> | |
| <img class="inline-th" id="imgThumb" src=""> | |
| <div class="inline-info"> | |
| <div class="inline-nm" id="imgName">image.png</div> | |
| </div> | |
| <button class="inline-rm" onclick="removeImg()">✕</button> | |
| </div> | |
| <div class="input-row"> | |
| <button class="attach-btn" title="Upload Image"> | |
| <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21.44 11.05l-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48"></path></svg> | |
| <input type="file" accept="image/*" onchange="handleFile(this)" id="fileInput"> | |
| </button> | |
| <textarea class="chat-ta" id="chatInput" placeholder="Message TIRANGA..." rows="1" onkeydown="handleKey(event)" oninput="autoGrow(this)"></textarea> | |
| <button class="send-btn" id="sendBtn" onclick="sendMsg()"> | |
| <svg viewBox="0 0 24 24"><path d="M2.01 21L23 12 2.01 3 2 10l15 2-15 2z"/></svg> | |
| </button> | |
| </div> | |
| </div> | |
| <div class="input-hint">TIRANGA is engineered for extreme performance. Verified by Divy Bhai.</div> | |
| </div> | |
| </main> | |
| <!-- Workspace Panel --> | |
| <aside class="workspace" id="workspace"> | |
| <div class="ws-hdr"> | |
| <div class="ws-title"> | |
| <span id="wsLangIcon">💻</span> | |
| <span id="wsTitleText">Generated Code</span> | |
| </div> | |
| <div class="ws-actions"> | |
| <button class="ws-icon-btn" onclick="copyWorkspaceCode()" title="Copy Code">📋</button> | |
| <button class="ws-icon-btn ws-close" onclick="closeWorkspace()" title="Close">✕</button> | |
| </div> | |
| </div> | |
| <div class="ws-content"> | |
| <pre><code id="wsCodeBlock" class="hljs"></code></pre> | |
| </div> | |
| </aside> | |
| </div> | |
| </div> | |
| <script> | |
| // --- STATE MANAGEMENT & MEMORY --- | |
| const S = { | |
| history: [], | |
| pending: null, | |
| busy: false, | |
| isUserScrolling: false, | |
| currentCode: "" // Holds the code currently in workspace | |
| }; | |
| // Load Memory on Boot | |
| window.onload = () => { | |
| const saved = localStorage.getItem('tiranga_chat_memory'); | |
| if(saved) { | |
| try { | |
| S.history = JSON.parse(saved); | |
| if(S.history.length > 0) { | |
| document.getElementById('welcome').style.display = 'none'; | |
| S.history.forEach(msg => { | |
| if(msg.role === 'user') appendUserDOM(msg.content, msg.badge); | |
| else appendBotDOM(msg.content); | |
| }); | |
| } | |
| } catch(e) { console.error("Memory load failed", e); } | |
| } | |
| }; | |
| function saveMemory() { | |
| // Keep last 20 messages to prevent local storage overflow | |
| const memToSave = S.history.slice(-20); | |
| localStorage.setItem('tiranga_chat_memory', JSON.stringify(memToSave)); | |
| } | |
| function clearMemory() { | |
| localStorage.removeItem('tiranga_chat_memory'); | |
| S.history = []; | |
| document.getElementById('msgs').innerHTML = ''; | |
| location.reload(); | |
| } | |
| // --- FILE HANDLING --- | |
| function handleFile(input){ | |
| const f=input.files[0];if(!f)return; | |
| const r=new FileReader(); | |
| r.onload=e=>{ | |
| const base64Data = e.target.result.split(',')[1]; | |
| S.pending={type: 'image', data: base64Data, name: f.name}; | |
| document.getElementById('imgThumb').src = e.target.result; | |
| document.getElementById('imgName').textContent=f.name; | |
| document.getElementById('imgPrev').classList.add('show'); | |
| }; | |
| r.readAsDataURL(f); | |
| } | |
| function removeImg(){S.pending=null;document.getElementById('imgPrev').classList.remove('show');document.getElementById('fileInput').value='';} | |
| // --- UI INTERACTIONS --- | |
| function handleKey(e){if(e.key==='Enter'&&!e.shiftKey){e.preventDefault();sendMsg();}} | |
| function autoGrow(el){el.style.height='auto';el.style.height=Math.min(el.scrollHeight,200)+'px';} | |
| // Scroll Handling | |
| function checkScroll() { | |
| const m = document.getElementById('msgs'); | |
| // If user scrolls up more than 50px from bottom, show button | |
| const isAtBottom = Math.abs((m.scrollHeight - m.scrollTop) - m.clientHeight) < 50; | |
| S.isUserScrolling = !isAtBottom; | |
| const btn = document.getElementById('scrollBtn'); | |
| if(S.isUserScrolling) btn.classList.add('show'); | |
| else btn.classList.remove('show'); | |
| } | |
| function forceScrollDown(){ | |
| const m=document.getElementById('msgs'); | |
| m.scrollTo({top: m.scrollHeight, behavior: 'smooth'}); | |
| S.isUserScrolling = false; | |
| } | |
| function autoScroll() { | |
| if(!S.isUserScrolling) { | |
| const m=document.getElementById('msgs'); | |
| m.scrollTop = m.scrollHeight; | |
| } | |
| } | |
| // --- COPY FUNCTIONALITY --- | |
| function copyText(btn, text) { | |
| navigator.clipboard.writeText(text); | |
| const orig = btn.innerHTML; | |
| btn.innerHTML = "✓ Copied!"; | |
| setTimeout(() => btn.innerHTML = orig, 2000); | |
| } | |
| // --- WORKSPACE LOGIC --- | |
| function openWorkspace(encodedCode, lang) { | |
| const code = decodeURIComponent(encodedCode); | |
| S.currentCode = code; | |
| document.getElementById('wsTitleText').textContent = lang ? `Generated ${lang.toUpperCase()}` : "Generated Code"; | |
| const codeBlock = document.getElementById('wsCodeBlock'); | |
| codeBlock.className = `hljs language-${lang || 'plaintext'}`; | |
| codeBlock.textContent = code; | |
| hljs.highlightElement(codeBlock); | |
| document.getElementById('workspace').classList.add('open'); | |
| // On mobile, close sidebar if open | |
| if(window.innerWidth <= 850) { | |
| document.getElementById('sidebar').classList.remove('show'); | |
| } | |
| } | |
| function closeWorkspace() { | |
| document.getElementById('workspace').classList.remove('open'); | |
| } | |
| function copyWorkspaceCode() { | |
| navigator.clipboard.writeText(S.currentCode); | |
| alert("Code Copied!"); // Native toast substitute | |
| } | |
| // --- IMAGE GENERATION INTERCEPTOR --- | |
| function isImageRequest(text) { | |
| const t = text.toLowerCase(); | |
| const keywords = ['image', 'photo', 'picture', 'draw', 'generate', 'create', 'तस्वीर', 'चित्र', 'फोटो', 'बनाओ', 'बना']; | |
| // Very basic intent check | |
| if((t.includes('create') || t.includes('generate') || t.includes('draw') || t.includes('बनाओ') || t.includes('बना')) && | |
| (t.includes('image') || t.includes('photo') || t.includes('picture') || t.includes('तस्वीर') || t.includes('चित्र') || t.includes('फोटो'))) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| async function handleImageGeneration(msg) { | |
| // Add User msg | |
| appendUserDOM(msg); | |
| S.history.push({role: 'user', content: msg}); | |
| // Create Bot loader | |
| const botDiv = document.createElement('div');botDiv.className='msg bot'; | |
| botDiv.innerHTML=`<div class="avatar"><img src="https://i.ibb.co/x8m7DbMk/TIRANGA-20260523-101648-0000.png"></div><div class="body"><div class="bubble"><div class="img-loader"><div class="spinner"></div> 🇮🇳 TIRANGA is creating your image...</div></div></div>`; | |
| document.getElementById('msgs').appendChild(botDiv); | |
| forceScrollDown(); | |
| // Parse logic for Pollinations | |
| const models = ['flux', 'flux-realism', 'flux-anime', 'flux-3d']; | |
| let selectedModel = models[0]; // default | |
| const tl = msg.toLowerCase(); | |
| if(tl.includes('real')) selectedModel = 'flux-realism'; | |
| if(tl.includes('anime')) selectedModel = 'flux-anime'; | |
| if(tl.includes('3d')) selectedModel = 'flux-3d'; | |
| const seed = Math.floor(Math.random() * 100000); | |
| const prompt = encodeURIComponent(msg); | |
| const url = `https://image.pollinations.ai/prompt/${prompt}?seed=${seed}&model=${selectedModel}&width=1024&height=1024&nologo=true`; | |
| // Create Image element | |
| const img = new Image(); | |
| img.src = url; | |
| img.onload = () => { | |
| // Replace loader with image | |
| botDiv.querySelector('.bubble').innerHTML = `<img src="${url}" alt="Generated Image"><p>Here is your image! (Model: ${selectedModel})</p>`; | |
| // Add to history | |
| const finalHTML = botDiv.querySelector('.bubble').innerHTML; | |
| S.history.push({role: 'bot', content: finalHTML, isRawHTML: true}); | |
| saveMemory(); | |
| forceScrollDown(); | |
| }; | |
| img.onerror = () => { | |
| botDiv.querySelector('.bubble').innerHTML = `<p>Sorry, I couldn't generate the image right now.</p>`; | |
| forceScrollDown(); | |
| }; | |
| } | |
| // --- MAIN CHAT LOGIC --- | |
| async function sendMsg(){ | |
| if(S.busy)return; | |
| const inp=document.getElementById('chatInput'); | |
| const msg=inp.value.trim();if(!msg && !S.pending)return; | |
| inp.value='';inp.style.height='auto'; | |
| // Image Generation Intercept | |
| if(!S.pending && isImageRequest(msg)) { | |
| handleImageGeneration(msg); | |
| return; | |
| } | |
| document.getElementById('sendBtn').disabled=true; | |
| S.busy=true; | |
| const w=document.getElementById('welcome'); | |
| if(w)w.style.display='none'; | |
| let attachments = []; | |
| let badge = null; | |
| if(S.pending) { | |
| attachments.push(S.pending); | |
| badge = '📎 '+S.pending.name; | |
| appendUserDOM(msg || '[Image Attached]', badge); | |
| S.history.push({role: 'user', content: msg || '[Image Attached]', badge: badge}); | |
| } else { | |
| appendUserDOM(msg); | |
| S.history.push({role: 'user', content: msg}); | |
| } | |
| removeImg(); | |
| saveMemory(); | |
| const botDiv = document.createElement('div');botDiv.className='msg bot'; | |
| botDiv.innerHTML=`<div class="avatar"><img src="https://i.ibb.co/x8m7DbMk/TIRANGA-20260523-101648-0000.png"></div><div class="body"><div class="bubble"><div class="typing"><span></span><span></span><span></span></div></div></div>`; | |
| document.getElementById('msgs').appendChild(botDiv); | |
| forceScrollDown(); | |
| const sysPrompt = "You are TIRANGA, a state-of-the-art AI assistant developed by Divy Patel at Vedika AI. your default language is English. Be extremely respectful. Always show immense respect to India. Answer comprehensively."; | |
| const payload = { | |
| message: msg, | |
| attachments: attachments, | |
| system_prompt: sysPrompt, | |
| max_tokens: 4096, | |
| temperature: 0.6 | |
| }; | |
| try{ | |
| const res = await fetch('/api/chat', { | |
| method: 'POST', | |
| headers: {'Content-Type': 'application/json'}, | |
| body: JSON.stringify(payload) | |
| }); | |
| const reader = res.body.getReader(); | |
| const decoder = new TextDecoder(); | |
| let accumulatedText = ""; | |
| while (true) { | |
| const { done, value } = await reader.read(); | |
| if (done) break; | |
| const chunk = decoder.decode(value, {stream: true}); | |
| const lines = chunk.split('\n'); | |
| for (let line of lines) { | |
| if (line.startsWith('data: ')) { | |
| const dataStr = line.substring(6); | |
| if (dataStr.trim() === '[DONE]') continue; | |
| try { | |
| const dataObj = JSON.parse(dataStr); | |
| if (dataObj.choices && dataObj.choices[0].delta.content) { | |
| accumulatedText += dataObj.choices[0].delta.content; | |
| renderBotText(botDiv, accumulatedText); | |
| autoScroll(); | |
| } | |
| } catch (e) { } | |
| } | |
| } | |
| } | |
| // Save to history after complete | |
| S.history.push({role: 'bot', content: accumulatedText}); | |
| saveMemory(); | |
| }catch(err){ | |
| renderBotText(botDiv,`**Error:** सर्वर से संपर्क नहीं हो पाया। कृपया दोबारा प्रयास करें।`); | |
| } | |
| S.busy=false; | |
| document.getElementById('sendBtn').disabled=false; | |
| autoScroll(); | |
| } | |
| // --- DOM INJECTION (For rendering history and live messages) --- | |
| function appendUserDOM(text, badgeText){ | |
| let bHtml = badgeText ? `<div style="font-size:11px;color:var(--accent);margin-bottom:4px;font-weight:600;">${esc(badgeText)}</div>` : ''; | |
| const el=document.createElement('div');el.className='msg user'; | |
| el.innerHTML=`<div class="avatar">U</div><div class="body">${bHtml}<div class="bubble">${esc(text)}</div></div>`; | |
| document.getElementById('msgs').appendChild(el); | |
| } | |
| function appendBotDOM(content) { | |
| const el=document.createElement('div');el.className='msg bot'; | |
| // Check if content is raw HTML (from image generator) | |
| if(content.includes('<img src="https://image.pollinations')) { | |
| el.innerHTML=`<div class="avatar"><img src="https://i.ibb.co/x8m7DbMk/TIRANGA-20260523-101648-0000.png"></div><div class="body"><div class="bubble">${content}</div></div>`; | |
| } else { | |
| el.innerHTML=`<div class="avatar"><img src="https://i.ibb.co/x8m7DbMk/TIRANGA-20260523-101648-0000.png"></div><div class="body"><div class="bubble"></div></div>`; | |
| renderBotText(el, content); | |
| } | |
| document.getElementById('msgs').appendChild(el); | |
| } | |
| // Custom Markdown rendering to extract code for Workspace | |
| function renderBotText(el, raw) { | |
| const b = el.querySelector('.bubble'); | |
| // Custom parsing for code blocks to convert to Workspace Triggers | |
| let processText = raw; | |
| const codeBlockRegex = /```(\w*)\n([\s\S]*?)```/g; | |
| processText = processText.replace(codeBlockRegex, (match, lang, code) => { | |
| const encoded = encodeURIComponent(code).replace(/'/g, "%27"); | |
| return ` | |
| <div class="ws-trigger-card"> | |
| <div class="ws-trigger-info"> | |
| <div class="ws-trigger-icon">💻</div> | |
| <div class="ws-trigger-text"> | |
| <h4>Code Generated</h4> | |
| <p>${lang || 'snippet'}</p> | |
| </div> | |
| </div> | |
| <button class="ws-btn" onclick="openWorkspace('${encoded}', '${lang}')">View in Workspace</button> | |
| </div>`; | |
| }); | |
| const html = marked.parse(processText); | |
| // Adding the copy button below the bubble text | |
| const cleanRaw = raw.replace(/"/g, '"').replace(/'/g, "\\'"); | |
| const actionsHTML = ` | |
| <div class="msg-actions"> | |
| <button class="action-btn" onclick="copyText(this, \`${cleanRaw}\`)"> | |
| <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg> | |
| Copy | |
| </button> | |
| </div> | |
| `; | |
| b.innerHTML = html; | |
| // Append actions to the body (outside bubble) | |
| let actionsDiv = el.querySelector('.msg-actions'); | |
| if(!actionsDiv) { | |
| const body = el.querySelector('.body'); | |
| body.insertAdjacentHTML('beforeend', actionsHTML); | |
| } | |
| } | |
| function esc(t){return t.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');} | |
| </script> | |
| </body> | |
| </html> |