Update index.html
Browse files- index.html +95 -72
index.html
CHANGED
|
@@ -1,89 +1,112 @@
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html lang="en">
|
| 3 |
-
<head>
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
</head>
|
| 10 |
|
| 11 |
-
<body>
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
</div>
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
</div>
|
| 27 |
-
</div>
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
const
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
authStatus.textContent = "❌ Error:\n" + JSON.stringify(error, null, 2);
|
| 50 |
}
|
| 51 |
-
});
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
msg.textContent = text;
|
| 58 |
-
messagesDiv.appendChild(msg);
|
| 59 |
-
messagesDiv.scrollTop = messagesDiv.scrollHeight;
|
| 60 |
-
}
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
input.value = "";
|
| 69 |
|
| 70 |
-
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
| 80 |
}
|
| 81 |
-
}
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
</body>
|
| 89 |
</html>
|
|
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="utf-8" />
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
| 6 |
+
<title>Puter AI Chat</title>
|
| 7 |
+
<link rel="stylesheet" href="style.css" />
|
| 8 |
+
<script src="https://js.puter.com/v2/"></script>
|
| 9 |
+
</head>
|
| 10 |
|
| 11 |
+
<body>
|
| 12 |
+
<div id="app">
|
| 13 |
+
<header>
|
| 14 |
+
<h1>💬 Puter AI Chat</h1>
|
| 15 |
+
<button id="sign-in">🔐 Sign in</button>
|
| 16 |
+
<span id="status-text">Not signed in</span>
|
| 17 |
+
</header>
|
|
|
|
| 18 |
|
| 19 |
+
<main id="chat-container">
|
| 20 |
+
<div id="messages"></div>
|
| 21 |
+
<div id="chat-input">
|
| 22 |
+
<input
|
| 23 |
+
type="text"
|
| 24 |
+
id="input-message"
|
| 25 |
+
placeholder="Type a message..."
|
| 26 |
+
/>
|
| 27 |
+
<button id="send-btn">Send</button>
|
| 28 |
+
</div>
|
| 29 |
+
</main>
|
| 30 |
+
|
| 31 |
+
<footer>
|
| 32 |
+
<p>
|
| 33 |
+
Built with ❤️ using <a href="https://puter.com" target="_blank">Puter.js</a> |
|
| 34 |
+
<a href="https://huggingface.co/spaces/Zain437/puter" target="_blank">View Space</a>
|
| 35 |
+
</p>
|
| 36 |
+
</footer>
|
| 37 |
</div>
|
|
|
|
| 38 |
|
| 39 |
+
<script>
|
| 40 |
+
const messages = [];
|
| 41 |
+
const statusEl = document.getElementById("status-text");
|
| 42 |
+
|
| 43 |
+
// 🌐 Check sign-in status on load
|
| 44 |
+
async function checkSignInStatus() {
|
| 45 |
+
const signedIn = await puter.auth.isSignedIn();
|
| 46 |
+
if (signedIn) {
|
| 47 |
+
const user = await puter.auth.getUser();
|
| 48 |
+
statusEl.textContent = `Signed in as ${user.username}`;
|
| 49 |
+
} else {
|
| 50 |
+
statusEl.textContent = "Not signed in";
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
checkSignInStatus();
|
| 55 |
|
| 56 |
+
// 🔐 Sign in button
|
| 57 |
+
document.getElementById("sign-in").addEventListener("click", async () => {
|
| 58 |
+
try {
|
| 59 |
+
statusEl.textContent = "Signing in...";
|
| 60 |
+
const user = await puter.auth.signIn();
|
| 61 |
+
statusEl.textContent = `✅ Signed in as ${user.username}`;
|
| 62 |
+
console.log("User:", user);
|
| 63 |
+
} catch (error) {
|
| 64 |
+
statusEl.textContent = "❌ Sign-in failed";
|
| 65 |
+
console.error("Sign-in error:", error);
|
| 66 |
+
}
|
| 67 |
+
});
|
| 68 |
|
| 69 |
+
// 💬 Chat system
|
| 70 |
+
function addMessage(msg, isUser = false) {
|
| 71 |
+
const messagesDiv = document.getElementById("messages");
|
| 72 |
+
const messageDiv = document.createElement("div");
|
| 73 |
+
messageDiv.classList.add("message");
|
| 74 |
+
if (isUser) messageDiv.classList.add("user-message");
|
| 75 |
+
messageDiv.textContent = msg;
|
| 76 |
+
messagesDiv.appendChild(messageDiv);
|
| 77 |
+
messagesDiv.scrollTop = messagesDiv.scrollHeight;
|
|
|
|
| 78 |
}
|
|
|
|
| 79 |
|
| 80 |
+
document.getElementById("send-btn").addEventListener("click", sendMessage);
|
| 81 |
+
document.getElementById("input-message").addEventListener("keypress", (e) => {
|
| 82 |
+
if (e.key === "Enter") sendMessage();
|
| 83 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
async function sendMessage() {
|
| 86 |
+
const input = document.getElementById("input-message");
|
| 87 |
+
const message = input.value.trim();
|
| 88 |
+
if (!message) return;
|
| 89 |
|
| 90 |
+
addMessage(message, true);
|
| 91 |
+
input.value = "";
|
|
|
|
| 92 |
|
| 93 |
+
messages.push({ role: "user", content: message });
|
| 94 |
|
| 95 |
+
try {
|
| 96 |
+
const response = await puter.ai.chat(messages);
|
| 97 |
+
const reply = response.message?.content || JSON.stringify(response);
|
| 98 |
+
addMessage(reply);
|
| 99 |
+
messages.push({ role: "assistant", content: reply });
|
| 100 |
+
} catch (err) {
|
| 101 |
+
addMessage("⚠️ Error connecting to AI. Check console.");
|
| 102 |
+
console.error("AI chat error:", err);
|
| 103 |
+
}
|
| 104 |
}
|
|
|
|
| 105 |
|
| 106 |
+
// 📊 Optional: Show usage in console
|
| 107 |
+
puter.auth.getMonthlyUsage().then((usage) => {
|
| 108 |
+
console.log("Usage:", usage);
|
| 109 |
+
});
|
| 110 |
+
</script>
|
| 111 |
+
</body>
|
| 112 |
</html>
|