levix-bot 4.2.0 → 4.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,6 +14,18 @@
14
14
  // Nothing here touches the disk. The bytes go up as a Blob, so there is no temp
15
15
  // file written next to the code (which a global install, a packaged binary or
16
16
  // the Android build may not even allow) and nothing to clean up afterwards.
17
+ //
18
+ // And not every Gemini endpoint has a Files API. A gateway or reverse proxy set
19
+ // as `gemini_base_url` often forwards generateContent and nothing else — and
20
+ // even one that forwards everything breaks if it lives under a path: the SDK
21
+ // takes the upload URL Google hands back and swaps in the base URL's host only,
22
+ // so `https://proxy.example/gemini` uploads to `https://proxy.example/upload/…`
23
+ // and gets the proxy's own 404. Text and voice notes worked (they ride inside
24
+ // the request); every photo failed. geminiMediaPart() tries the upload, and
25
+ // when the endpoint isn't there sends the bytes inline instead — and remembers,
26
+ // so the next photo doesn't pay for the failed upload again.
27
+
28
+ const logger = require("./logger.cjs");
17
29
 
18
30
  // Used only when WhatsApp sent no MIME type at all.
19
31
  const FALLBACK_MIME = {
@@ -23,6 +35,10 @@ const FALLBACK_MIME = {
23
35
  document: "application/octet-stream",
24
36
  };
25
37
 
38
+ // Gemini caps a whole inline request at 20 MB; media past this goes through the
39
+ // Files API or not at all.
40
+ const INLINE_MEDIA_LIMIT = 15 * 1024 * 1024;
41
+
26
42
  // A video is usable only once Google has processed it; images and audio are
27
43
  // normally ACTIVE straight away.
28
44
  const PROCESSING_POLL_MS = 1500;
@@ -96,4 +112,57 @@ async function uploadToGemini(
96
112
  return file;
97
113
  }
98
114
 
99
- module.exports = { mediaKind, baseMimeType, downloadMedia, uploadToGemini };
115
+ /**
116
+ * True when an upload failed because the endpoint has no Files API — not
117
+ * because of the key, the quota or the file. A 404/405/501, an HTML page where
118
+ * JSON belongs, or the SDK's own complaints about the upload handshake.
119
+ */
120
+ function isMissingUploadEndpoint(error) {
121
+ const status = Number(error?.status || error?.code || error?.response?.status);
122
+ if ([404, 405, 501].includes(status)) return true;
123
+ const message = String(error?.message || "");
124
+ return /<html|upload url|upload status is not finalized/i.test(message);
125
+ }
126
+
127
+ // Base URLs whose Files API turned out to be missing. In memory on purpose: a
128
+ // restart (or a changed base URL) tries the upload again.
129
+ const inlineOnly = new Set();
130
+
131
+ /**
132
+ * The part that puts `buffer` in front of Gemini: a `fileData` reference when
133
+ * the Files API is there, else the bytes inline.
134
+ *
135
+ * Inline media rides along in the stored conversation, the way the
136
+ * OpenAI-compatible and Anthropic providers already keep their images, so a
137
+ * follow-up question can still see the photo.
138
+ */
139
+ async function geminiMediaPart(genAI, buffer, mimeType, { baseUrl = "", ...uploadOptions } = {}) {
140
+ const key = baseUrl || "(default)";
141
+ const inline = () => ({ inlineData: { mimeType, data: buffer.toString("base64") } });
142
+ const fitsInline = buffer.length <= INLINE_MEDIA_LIMIT;
143
+
144
+ if (inlineOnly.has(key) && fitsInline) return inline();
145
+
146
+ try {
147
+ const file = await uploadToGemini(genAI, buffer, mimeType, uploadOptions);
148
+ return { fileData: { fileUri: file.uri, mimeType: file.mimeType || mimeType } };
149
+ } catch (error) {
150
+ if (!fitsInline || !isMissingUploadEndpoint(error)) throw error;
151
+ inlineOnly.add(key);
152
+ logger.warn(
153
+ { status: error?.status, baseUrl: baseUrl || null },
154
+ "[geminiMedia] this Gemini endpoint has no Files API — sending media inline from now on",
155
+ );
156
+ return inline();
157
+ }
158
+ }
159
+
160
+ module.exports = {
161
+ INLINE_MEDIA_LIMIT,
162
+ mediaKind,
163
+ baseMimeType,
164
+ downloadMedia,
165
+ uploadToGemini,
166
+ geminiMediaPart,
167
+ isMissingUploadEndpoint,
168
+ };
package/views/login.ejs CHANGED
@@ -32,7 +32,7 @@
32
32
 
33
33
  <section class="gateway-panel">
34
34
  <div class="gateway-panel-inner">
35
- <form class="card auth-card" action="/login" method="post">
35
+ <form class="card auth-card" id="login-card" action="/login" method="post">
36
36
  <div class="auth-head">
37
37
  <span class="eyebrow">Secure access</span>
38
38
  <h1>Welcome back</h1>
@@ -41,14 +41,20 @@
41
41
 
42
42
  <label class="field">
43
43
  <span>Control panel password</span>
44
- <input
45
- type="password"
46
- name="password"
47
- autocomplete="current-password"
48
- placeholder="Enter your password"
49
- autofocus
50
- required
51
- />
44
+ <div class="input-wrap">
45
+ <input
46
+ type="password"
47
+ name="password"
48
+ autocomplete="current-password"
49
+ placeholder="Enter your password"
50
+ autofocus
51
+ required
52
+ />
53
+ <button type="button" class="eye-btn" aria-label="Show password">
54
+ <svg class="eye-on" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7-10-7-10-7Z"/><circle cx="12" cy="12" r="3"/></svg>
55
+ <svg class="eye-off" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" style="display:none"><path d="M9.88 9.88a3 3 0 1 0 4.24 4.24"/><path d="M10.73 5.08A10.43 10.43 0 0 1 12 5c6.5 0 10 7 10 7a13.16 13.16 0 0 1-1.67 2.68"/><path d="M6.61 6.61A13.526 13.526 0 0 0 2 12s3.5 7 10 7a9.74 9.74 0 0 0 5.39-1.61"/><line x1="2" x2="22" y1="2" y2="22"/></svg>
56
+ </button>
57
+ </div>
52
58
  </label>
53
59
 
54
60
  <button type="submit" class="btn btn-primary auth-submit">
@@ -59,11 +65,205 @@
59
65
  <% if (error) { %>
60
66
  <div class="error" role="alert"><%= error %></div>
61
67
  <% } %>
68
+
69
+ <div class="auth-alt">
70
+ <button type="button" class="link-btn" id="forgot-link">Forgot password?</button>
71
+ </div>
62
72
  </form>
63
73
 
74
+ <div class="card auth-card" id="reset-card" hidden>
75
+ <div class="auth-head">
76
+ <span class="eyebrow">Password reset</span>
77
+ <h1>Reset your password</h1>
78
+ <p class="sub">
79
+ Levix sends a one-time code to your WhatsApp — the linked account's own chat.
80
+ The code works for 10 minutes.
81
+ </p>
82
+ </div>
83
+
84
+ <button type="button" class="btn btn-primary auth-submit" id="reset-send">
85
+ Send code to WhatsApp
86
+ <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M5 12h14M13 6l6 6-6 6"/></svg>
87
+ </button>
88
+
89
+ <form id="reset-form" hidden>
90
+ <label class="field">
91
+ <span>Reset code</span>
92
+ <input
93
+ type="text"
94
+ id="reset-code"
95
+ inputmode="numeric"
96
+ autocomplete="one-time-code"
97
+ maxlength="6"
98
+ placeholder="6 digits"
99
+ required
100
+ />
101
+ </label>
102
+
103
+ <label class="field">
104
+ <span>New password</span>
105
+ <div class="input-wrap">
106
+ <input
107
+ type="password"
108
+ id="reset-pass"
109
+ autocomplete="new-password"
110
+ placeholder="At least <%= minLength %> characters"
111
+ minlength="<%= minLength %>"
112
+ required
113
+ />
114
+ <button type="button" class="eye-btn" aria-label="Show password">
115
+ <svg class="eye-on" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7-10-7-10-7Z"/><circle cx="12" cy="12" r="3"/></svg>
116
+ <svg class="eye-off" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" style="display:none"><path d="M9.88 9.88a3 3 0 1 0 4.24 4.24"/><path d="M10.73 5.08A10.43 10.43 0 0 1 12 5c6.5 0 10 7 10 7a13.16 13.16 0 0 1-1.67 2.68"/><path d="M6.61 6.61A13.526 13.526 0 0 0 2 12s3.5 7 10 7a9.74 9.74 0 0 0 5.39-1.61"/><line x1="2" x2="22" y1="2" y2="22"/></svg>
117
+ </button>
118
+ </div>
119
+ </label>
120
+
121
+ <label class="field">
122
+ <span>Repeat password</span>
123
+ <div class="input-wrap">
124
+ <input
125
+ type="password"
126
+ id="reset-confirm"
127
+ autocomplete="new-password"
128
+ placeholder="Type it again"
129
+ minlength="<%= minLength %>"
130
+ required
131
+ />
132
+ <button type="button" class="eye-btn" aria-label="Show password">
133
+ <svg class="eye-on" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7-10-7-10-7Z"/><circle cx="12" cy="12" r="3"/></svg>
134
+ <svg class="eye-off" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" style="display:none"><path d="M9.88 9.88a3 3 0 1 0 4.24 4.24"/><path d="M10.73 5.08A10.43 10.43 0 0 1 12 5c6.5 0 10 7 10 7a13.16 13.16 0 0 1-1.67 2.68"/><path d="M6.61 6.61A13.526 13.526 0 0 0 2 12s3.5 7 10 7a9.74 9.74 0 0 0 5.39-1.61"/><line x1="2" x2="22" y1="2" y2="22"/></svg>
135
+ </button>
136
+ </div>
137
+ </label>
138
+
139
+ <button type="submit" class="btn btn-primary auth-submit">
140
+ Set new password
141
+ <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M5 12h14M13 6l6 6-6 6"/></svg>
142
+ </button>
143
+ </form>
144
+
145
+ <div class="error" id="reset-error" role="alert" hidden></div>
146
+ <div class="auth-alt">
147
+ <button type="button" class="link-btn" id="reset-back">Back to sign in</button>
148
+ </div>
149
+ </div>
150
+
64
151
  <div class="foot"><%= brand.name %> by <%= brand.developer %> · <%= brand.studio %></div>
65
152
  </div>
66
153
  </section>
67
154
  </main>
155
+
156
+ <script src="/gateway.js"></script>
157
+ <script>
158
+ (function () {
159
+ var loginCard = document.getElementById("login-card");
160
+ var resetCard = document.getElementById("reset-card");
161
+ var sendBtn = document.getElementById("reset-send");
162
+ var form = document.getElementById("reset-form");
163
+ var errorBox = document.getElementById("reset-error");
164
+
165
+ function showReset() {
166
+ loginCard.hidden = true;
167
+ resetCard.hidden = false;
168
+ }
169
+ function showLogin() {
170
+ resetCard.hidden = true;
171
+ loginCard.hidden = false;
172
+ document.getElementById("forgot-link").focus();
173
+ }
174
+ function showError(text) {
175
+ errorBox.textContent = text;
176
+ errorBox.hidden = false;
177
+ }
178
+ function hideError() {
179
+ errorBox.hidden = true;
180
+ }
181
+ async function postJson(url, body) {
182
+ const response = await fetch(url, {
183
+ method: "POST",
184
+ headers: { "Content-Type": "application/json" },
185
+ body: JSON.stringify(body || {}),
186
+ });
187
+ let data = {};
188
+ try {
189
+ data = await response.json();
190
+ } catch {}
191
+ return data;
192
+ }
193
+
194
+ document.getElementById("forgot-link").addEventListener("click", showReset);
195
+ document.getElementById("reset-back").addEventListener("click", showLogin);
196
+
197
+ sendBtn.addEventListener("click", async function () {
198
+ sendBtn.disabled = true;
199
+ hideError();
200
+ try {
201
+ const result = await postJson("/reset/request");
202
+ if (result.ok) {
203
+ sendBtn.hidden = true;
204
+ form.hidden = false;
205
+ document.getElementById("reset-code").focus();
206
+ } else if (result.reason === "throttled") {
207
+ showError("Too many requests. Try again in " + (result.retryAfter || 60) + " seconds.");
208
+ } else if (result.reason === "no_session") {
209
+ showError(
210
+ "WhatsApp isn't connected, so the code can't be sent. " +
211
+ "Sign in on the server and run `levix reset-password`, or start the WhatsApp session first."
212
+ );
213
+ } else if (result.reason === "send_failed") {
214
+ showError("Couldn't send the WhatsApp message. Try again in a minute.");
215
+ } else {
216
+ showError("Couldn't start the reset. Try again.");
217
+ }
218
+ } catch {
219
+ showError("Couldn't reach the server. Try again.");
220
+ }
221
+ sendBtn.disabled = false;
222
+ });
223
+
224
+ form.addEventListener("submit", async function (event) {
225
+ event.preventDefault();
226
+ hideError();
227
+ try {
228
+ const result = await postJson("/reset/confirm", {
229
+ code: document.getElementById("reset-code").value,
230
+ password: document.getElementById("reset-pass").value,
231
+ confirm: document.getElementById("reset-confirm").value,
232
+ });
233
+ if (result.ok) {
234
+ form.hidden = true;
235
+ sendBtn.hidden = true;
236
+ showError("Password updated. Taking you to sign in…");
237
+ setTimeout(function () {
238
+ window.location.href = "/";
239
+ }, 900);
240
+ return;
241
+ }
242
+ if (result.reason === "wrong_code") {
243
+ showError(
244
+ "Wrong code." +
245
+ (result.attemptsLeft ? " " + result.attemptsLeft + " attempts left." : "")
246
+ );
247
+ } else if (
248
+ result.reason === "expired" ||
249
+ result.reason === "no_pending" ||
250
+ result.reason === "too_many_attempts"
251
+ ) {
252
+ showError("That code is no longer valid. Send a new one.");
253
+ form.hidden = true;
254
+ sendBtn.hidden = false;
255
+ } else if (result.reason === "mismatch") {
256
+ showError("The two passwords don't match.");
257
+ } else if (result.reason === "invalid_password") {
258
+ showError(result.message || "That password isn't allowed.");
259
+ } else {
260
+ showError("Couldn't reset the password. Try again.");
261
+ }
262
+ } catch {
263
+ showError("Couldn't reach the server. Try again.");
264
+ }
265
+ });
266
+ })();
267
+ </script>
68
268
  </body>
69
269
  </html>
package/views/setup.ejs CHANGED
@@ -50,15 +50,27 @@
50
50
 
51
51
  <label class="field">
52
52
  <span>Password</span>
53
- <input type="password" name="password" autocomplete="new-password"
54
- placeholder="At least <%= minLength %> characters"
55
- minlength="<%= minLength %>" <%= needsCode ? "" : "autofocus" %> required />
53
+ <div class="input-wrap">
54
+ <input type="password" name="password" autocomplete="new-password"
55
+ placeholder="At least <%= minLength %> characters"
56
+ minlength="<%= minLength %>" <%= needsCode ? "" : "autofocus" %> required />
57
+ <button type="button" class="eye-btn" aria-label="Show password">
58
+ <svg class="eye-on" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7-10-7-10-7Z"/><circle cx="12" cy="12" r="3"/></svg>
59
+ <svg class="eye-off" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" style="display:none"><path d="M9.88 9.88a3 3 0 1 0 4.24 4.24"/><path d="M10.73 5.08A10.43 10.43 0 0 1 12 5c6.5 0 10 7 10 7a13.16 13.16 0 0 1-1.67 2.68"/><path d="M6.61 6.61A13.526 13.526 0 0 0 2 12s3.5 7 10 7a9.74 9.74 0 0 0 5.39-1.61"/><line x1="2" x2="22" y1="2" y2="22"/></svg>
60
+ </button>
61
+ </div>
56
62
  </label>
57
63
 
58
64
  <label class="field">
59
65
  <span>Repeat password</span>
60
- <input type="password" name="confirm" autocomplete="new-password"
61
- placeholder="Type it again" minlength="<%= minLength %>" required />
66
+ <div class="input-wrap">
67
+ <input type="password" name="confirm" autocomplete="new-password"
68
+ placeholder="Type it again" minlength="<%= minLength %>" required />
69
+ <button type="button" class="eye-btn" aria-label="Show password">
70
+ <svg class="eye-on" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7-10-7-10-7Z"/><circle cx="12" cy="12" r="3"/></svg>
71
+ <svg class="eye-off" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" style="display:none"><path d="M9.88 9.88a3 3 0 1 0 4.24 4.24"/><path d="M10.73 5.08A10.43 10.43 0 0 1 12 5c6.5 0 10 7 10 7a13.16 13.16 0 0 1-1.67 2.68"/><path d="M6.61 6.61A13.526 13.526 0 0 0 2 12s3.5 7 10 7a9.74 9.74 0 0 0 5.39-1.61"/><line x1="2" x2="22" y1="2" y2="22"/></svg>
72
+ </button>
73
+ </div>
62
74
  </label>
63
75
 
64
76
  <button type="submit" class="btn btn-primary auth-submit">
@@ -75,5 +87,7 @@
75
87
  </div>
76
88
  </section>
77
89
  </main>
90
+
91
+ <script src="/gateway.js"></script>
78
92
  </body>
79
93
  </html>