Zain437 commited on
Commit
8b2178b
·
verified ·
1 Parent(s): 4db27eb

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +95 -72
index.html CHANGED
@@ -1,89 +1,112 @@
1
  <!DOCTYPE html>
2
  <html lang="en">
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>AI Chat App with Puter Auth</title>
7
- <link rel="stylesheet" href="style.css" />
8
- <script src="https://js.puter.com/v2/"></script>
9
- </head>
10
 
11
- <body>
12
- <!-- Authentication Section -->
13
- <div id="auth-container">
14
- <h2>Welcome to AI Chat</h2>
15
- <p>Please sign in with your Puter account to continue.</p>
16
- <button id="sign-in">🔐 Sign in with Puter</button>
17
- <pre id="auth-status"></pre>
18
- </div>
19
 
20
- <!-- Chat Section (hidden until authenticated) -->
21
- <div id="chat-container" style="display:none;">
22
- <div id="messages"></div>
23
- <div id="chat-input">
24
- <input type="text" id="input-message" placeholder="Type a message..." />
25
- <button id="send-btn">Send</button>
 
 
 
 
 
 
 
 
 
 
 
 
26
  </div>
27
- </div>
28
 
29
- <p class="footer-text">Created using <strong>Puter.JS</strong></p>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- <script>
32
- const authContainer = document.getElementById("auth-container");
33
- const chatContainer = document.getElementById("chat-container");
34
- const authStatus = document.getElementById("auth-status");
35
- const messagesDiv = document.getElementById("messages");
36
- const input = document.getElementById("input-message");
37
- const sendBtn = document.getElementById("send-btn");
38
- const messages = [];
 
 
 
 
39
 
40
- // --- Sign-in handler ---
41
- document.getElementById("sign-in").addEventListener("click", async () => {
42
- try {
43
- const user = await puter.auth.signIn();
44
- authStatus.textContent = "✅ Signed in:\n" + JSON.stringify(user, null, 2);
45
- authContainer.style.display = "none";
46
- chatContainer.style.display = "flex";
47
- addMessage(`Welcome ${user.user.username || "User"}! You’re now authenticated.`, "ai");
48
- } catch (error) {
49
- authStatus.textContent = "❌ Error:\n" + JSON.stringify(error, null, 2);
50
  }
51
- });
52
 
53
- // --- Chat logic ---
54
- function addMessage(text, sender) {
55
- const msg = document.createElement("div");
56
- msg.classList.add("message", sender);
57
- msg.textContent = text;
58
- messagesDiv.appendChild(msg);
59
- messagesDiv.scrollTop = messagesDiv.scrollHeight;
60
- }
61
 
62
- async function sendMessage() {
63
- const userText = input.value.trim();
64
- if (!userText) return;
 
65
 
66
- addMessage(userText, "user");
67
- messages.push({ role: "user", content: userText });
68
- input.value = "";
69
 
70
- addMessage("Thinking...", "ai");
71
 
72
- try {
73
- const response = await puter.ai.chat(messages);
74
- const reply = response.message.content || "No response from AI.";
75
- messages.push(response.message);
76
- messagesDiv.lastChild.textContent = reply;
77
- } catch (err) {
78
- console.error(err);
79
- messagesDiv.lastChild.textContent = "⚠️ Error: Failed to get response.";
 
80
  }
81
- }
82
 
83
- sendBtn.addEventListener("click", sendMessage);
84
- input.addEventListener("keypress", e => {
85
- if (e.key === "Enter") sendMessage();
86
- });
87
- </script>
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>