Unmid commited on
Commit
6a1ae2a
·
verified ·
1 Parent(s): 23b97de

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +127 -50
index.html CHANGED
@@ -16,37 +16,123 @@
16
  }
17
  * { box-sizing: border-box; margin: 0; padding: 0; font-family: system-ui, sans-serif; }
18
  body { background-color: var(--bg-primary); color: var(--text-main); display: flex; flex-direction: column; height: 100vh; overflow: hidden; }
19
- header { background-color: var(--bg-secondary); border-bottom: 1px solid var(--border-color); padding: 12px 20px; display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px; }
20
- .config-group { display: flex; gap: 10px; align-items: center; font-size: 13px; }
21
- .config-group input { background: var(--bg-primary); border: 1px solid var(--border-color); color: var(--text-main); padding: 6px 10px; border-radius: 6px; font-size: 13px; outline: none; }
22
- #chat-window { flex: 1; padding: 20px; overflow-y: auto; display: flex; flex-direction: column; gap: 16px; }
23
- .message { max-width: 80%; line-height: 1.5; padding: 12px 16px; border-radius: 12px; word-break: break-word; white-space: pre-wrap; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  .user-msg { background-color: var(--bg-bubble-user); align-self: flex-end; border-bottom-right-radius: 2px; }
25
  .ai-msg { background-color: var(--bg-bubble-ai); align-self: flex-start; border-bottom-left-radius: 2px; border: 1px solid var(--border-color); }
26
  .error-msg { background-color: #7f1d1d; color: #fecaca; align-self: center; font-size: 13px; }
27
- #input-container { background-color: var(--bg-secondary); border-top: 1px solid var(--border-color); padding: 16px 20px; display: flex; gap: 12px; }
28
- textarea { flex: 1; background: var(--bg-primary); color: var(--text-main); border: 1px solid var(--border-color); border-radius: 8px; padding: 10px 14px; resize: none; height: 50px; outline: none; font-size: 14px; }
29
- button { background: var(--accent); color: #0f172a; border: none; font-weight: 600; padding: 0 20px; border-radius: 8px; cursor: pointer; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  button:disabled { opacity: 0.5; cursor: not-allowed; }
31
  </style>
32
  </head>
33
  <body>
 
34
  <header>
35
  <div><strong>OpenCode Zen Chat</strong></div>
 
36
  <div class="config-group">
 
 
 
 
 
 
 
 
 
 
 
 
37
  <label>Model:</label>
38
- <input type="text" id="model-input" value="deepseek-v4.1-flash-free" style="width: 220px;" />
39
- <label>API Key:</label>
40
- <input type="password" id="api-key" value="oc_sk_35b87025c3e1_wYnXQAjQFh5SahiJknEk7MRQdW3q7RaL" style="width: 170px;" />
 
41
  </div>
42
  </header>
43
 
44
  <div id="chat-window">
45
- <div class="message ai-msg">Connected to local proxy. Type a message to begin!</div>
46
  </div>
47
 
48
  <div id="input-container">
49
- <textarea id="prompt-input" placeholder="Type your message... (Shift + Enter for new line)"></textarea>
50
  <button id="send-btn" onclick="sendMessage()">Send</button>
51
  </div>
52
 
@@ -56,8 +142,12 @@
56
  const sendBtn = document.getElementById("send-btn");
57
  const apiKeyInput = document.getElementById("api-key");
58
  const modelInput = document.getElementById("model-input");
 
 
59
 
60
- let messages = [{ role: "system", content: "You are a helpful coding and AI assistant." }];
 
 
61
 
62
  promptInput.addEventListener("keydown", (e) => {
63
  if (e.key === "Enter" && !e.shiftKey) {
@@ -86,12 +176,18 @@
86
  messages.push({ role: "user", content: text });
87
  promptInput.value = "";
88
 
89
- const aiBubble = appendMessage("assistant", "...");
90
  sendBtn.disabled = true;
91
 
 
 
 
 
 
 
 
92
  try {
93
- // Points to our local server proxy endpoint
94
- const response = await fetch("/api/chat", {
95
  method: "POST",
96
  headers: {
97
  "Content-Type": "application/json",
@@ -100,52 +196,33 @@
100
  body: JSON.stringify({
101
  model: model,
102
  messages: messages,
103
- stream: true
104
  })
105
  });
106
 
107
  if (!response.ok) {
108
- const err = await response.json().catch(() => ({}));
109
- throw new Error(err.error?.message || `HTTP ${response.status}: ${response.statusText}`);
 
 
 
 
 
110
  }
111
 
112
- aiBubble.textContent = "";
113
- const reader = response.body.getReader();
114
- const decoder = new TextDecoder();
115
- let assistantReply = "";
116
- let buffer = "";
117
-
118
- while (true) {
119
- const { done, value } = await reader.read();
120
- if (done) break;
121
-
122
- buffer += decoder.decode(value, { stream: true });
123
- const lines = buffer.split("\n");
124
- buffer = lines.pop();
125
-
126
- for (const line of lines) {
127
- const trimmed = line.trim();
128
- if (!trimmed || trimmed.startsWith(":")) continue;
129
- if (trimmed === "data: [DONE]") break;
130
- if (trimmed.startsWith("data: ")) {
131
- try {
132
- const parsed = JSON.parse(trimmed.replace(/^data:\s*/, ""));
133
- const delta = parsed.choices?.[0]?.delta?.content || "";
134
- assistantReply += delta;
135
- aiBubble.textContent = assistantReply;
136
- chatWindow.scrollTop = chatWindow.scrollHeight;
137
- } catch (_) {}
138
- }
139
- }
140
- }
141
 
142
- messages.push({ role: "assistant", content: assistantReply });
143
  } catch (err) {
144
  aiBubble.className = "message error-msg";
145
  aiBubble.textContent = "Error: " + err.message;
146
  } finally {
147
  sendBtn.disabled = false;
148
  promptInput.focus();
 
149
  }
150
  }
151
  </script>
 
16
  }
17
  * { box-sizing: border-box; margin: 0; padding: 0; font-family: system-ui, sans-serif; }
18
  body { background-color: var(--bg-primary); color: var(--text-main); display: flex; flex-direction: column; height: 100vh; overflow: hidden; }
19
+
20
+ header {
21
+ background-color: var(--bg-secondary);
22
+ border-bottom: 1px solid var(--border-color);
23
+ padding: 12px 16px;
24
+ display: flex;
25
+ flex-wrap: wrap;
26
+ gap: 12px;
27
+ align-items: center;
28
+ justify-content: space-between;
29
+ }
30
+
31
+ .config-group {
32
+ display: flex;
33
+ flex-wrap: wrap;
34
+ gap: 10px;
35
+ align-items: center;
36
+ font-size: 13px;
37
+ }
38
+
39
+ .config-group input, .config-group select {
40
+ background: var(--bg-primary);
41
+ border: 1px solid var(--border-color);
42
+ color: var(--text-main);
43
+ padding: 6px 10px;
44
+ border-radius: 6px;
45
+ font-size: 13px;
46
+ outline: none;
47
+ }
48
+
49
+ #chat-window {
50
+ flex: 1;
51
+ padding: 20px;
52
+ overflow-y: auto;
53
+ display: flex;
54
+ flex-direction: column;
55
+ gap: 16px;
56
+ }
57
+
58
+ .message {
59
+ max-width: 85%;
60
+ line-height: 1.5;
61
+ padding: 12px 16px;
62
+ border-radius: 12px;
63
+ word-break: break-word;
64
+ white-space: pre-wrap;
65
+ }
66
+
67
  .user-msg { background-color: var(--bg-bubble-user); align-self: flex-end; border-bottom-right-radius: 2px; }
68
  .ai-msg { background-color: var(--bg-bubble-ai); align-self: flex-start; border-bottom-left-radius: 2px; border: 1px solid var(--border-color); }
69
  .error-msg { background-color: #7f1d1d; color: #fecaca; align-self: center; font-size: 13px; }
70
+
71
+ #input-container {
72
+ background-color: var(--bg-secondary);
73
+ border-top: 1px solid var(--border-color);
74
+ padding: 14px 20px;
75
+ display: flex;
76
+ gap: 10px;
77
+ }
78
+
79
+ textarea {
80
+ flex: 1;
81
+ background: var(--bg-primary);
82
+ color: var(--text-main);
83
+ border: 1px solid var(--border-color);
84
+ border-radius: 8px;
85
+ padding: 10px 14px;
86
+ resize: none;
87
+ height: 48px;
88
+ outline: none;
89
+ font-size: 14px;
90
+ }
91
+
92
+ button {
93
+ background: var(--accent);
94
+ color: #0f172a;
95
+ border: none;
96
+ font-weight: 600;
97
+ padding: 0 20px;
98
+ border-radius: 8px;
99
+ cursor: pointer;
100
+ }
101
  button:disabled { opacity: 0.5; cursor: not-allowed; }
102
  </style>
103
  </head>
104
  <body>
105
+
106
  <header>
107
  <div><strong>OpenCode Zen Chat</strong></div>
108
+
109
  <div class="config-group">
110
+ <label>Proxy:</label>
111
+ <select id="cors-mode">
112
+ <option value="proxy">CORS Proxy (For Web Hosts)</option>
113
+ <option value="direct">Direct (No Proxy)</option>
114
+ </select>
115
+
116
+ <label>Endpoint:</label>
117
+ <select id="endpoint-select">
118
+ <option value="https://opencode.ai/zen/v1/chat/completions">/zen/v1/chat/completions</option>
119
+ <option value="https://opencode.ai/inference/v1/chat/completions">/inference/v1/chat/completions</option>
120
+ </select>
121
+
122
  <label>Model:</label>
123
+ <input type="text" id="model-input" value="deepseek-v4.1-flash-free" style="width: 200px;" />
124
+
125
+ <label>Key:</label>
126
+ <input type="password" id="api-key" value="oc_sk_35b87025c3e1_wYnXQAjQFh5SahiJknEk7MRQdW3q7RaL" style="width: 140px;" />
127
  </div>
128
  </header>
129
 
130
  <div id="chat-window">
131
+ <div class="message ai-msg">Ready! Type your question below.</div>
132
  </div>
133
 
134
  <div id="input-container">
135
+ <textarea id="prompt-input" placeholder="Type a message... (Shift + Enter for new line)"></textarea>
136
  <button id="send-btn" onclick="sendMessage()">Send</button>
137
  </div>
138
 
 
142
  const sendBtn = document.getElementById("send-btn");
143
  const apiKeyInput = document.getElementById("api-key");
144
  const modelInput = document.getElementById("model-input");
145
+ const endpointSelect = document.getElementById("endpoint-select");
146
+ const corsMode = document.getElementById("cors-mode");
147
 
148
+ let messages = [
149
+ { role: "system", content: "You are a helpful coding and AI assistant." }
150
+ ];
151
 
152
  promptInput.addEventListener("keydown", (e) => {
153
  if (e.key === "Enter" && !e.shiftKey) {
 
176
  messages.push({ role: "user", content: text });
177
  promptInput.value = "";
178
 
179
+ const aiBubble = appendMessage("assistant", "Thinking...");
180
  sendBtn.disabled = true;
181
 
182
+ // Determine target URL
183
+ let targetUrl = endpointSelect.value;
184
+ if (corsMode.value === "proxy") {
185
+ // Routes through a CORS proxy so browser security doesn't block the static host
186
+ targetUrl = "https://corsproxy.io/?" + encodeURIComponent(targetUrl);
187
+ }
188
+
189
  try {
190
+ const response = await fetch(targetUrl, {
 
191
  method: "POST",
192
  headers: {
193
  "Content-Type": "application/json",
 
196
  body: JSON.stringify({
197
  model: model,
198
  messages: messages,
199
+ stream: false // Non-streamed for maximum proxy compatibility
200
  })
201
  });
202
 
203
  if (!response.ok) {
204
+ const errBody = await response.text();
205
+ let parsedMsg = errBody;
206
+ try {
207
+ const json = JSON.parse(errBody);
208
+ parsedMsg = json.error?.message || json.message || errBody;
209
+ } catch (_) {}
210
+ throw new Error(`HTTP ${response.status}: ${parsedMsg}`);
211
  }
212
 
213
+ const data = await response.json();
214
+ const reply = data.choices?.[0]?.message?.content || "(No response content returned)";
215
+
216
+ aiBubble.textContent = reply;
217
+ messages.push({ role: "assistant", content: reply });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
 
 
219
  } catch (err) {
220
  aiBubble.className = "message error-msg";
221
  aiBubble.textContent = "Error: " + err.message;
222
  } finally {
223
  sendBtn.disabled = false;
224
  promptInput.focus();
225
+ chatWindow.scrollTop = chatWindow.scrollHeight;
226
  }
227
  }
228
  </script>