Spaces:
Running
Running
Deploy ff9cd3b từ GitHub Actions
Browse filesFiles changed (3)
3 unmodified lines4567891026 unmodified lines3738394041424344454647484950515253545556573 unmodified lines*/import { randomUUID } from 'node:crypto';import { db } from './firebase.ts';import { clearCooldown, keyStatus, setUiKeys, testKey } from './gemini.ts';interface Stored { id: string; label: string; key: string; enabled: boolean }interface Doc { keys: Stored[]; updatedAt: number; updatedBy: string }26 unmodified linesawait ensure();// cho dán nhiều key một lần (mỗi dòng / phân cách dấu phẩy)const raw = typeof body.keys === 'string' ? body.keys : '';const keys = [...new Set(raw.split(/[\s,;]+/).map((k) => k.trim()).filter(Boolean))];if (!keys.length) throw httpError('Chưa nhập key');if (keys.length > 50) throw httpError('Tối đa 50 key mỗi lần');const label = typeof body.label === 'string' ? body.label.trim().slice(0, 60) : '';const bad = keys.filter((k) => !/^[\w-]{20,200}$/.test(k));if (bad.length) throw httpError(`Key không đúng định dạng: ${bad.map((k) => k.slice(0, 6) + '…').join(', ')}`);const have = new Set(list.map((k) => k.key));let added = 0;for (const key of keys) {if (have.has(key)) continue;list.push({ id: randomUUID().slice(0, 8), label: label ? (keys.length > 1 ? `${label} #${added + 1}` : label) : `Key ${list.length + 1}`, key, enabled: true });added++;}await persist(by);return { added, skipped: keys.length - added };}export async function updateGeminiKey(id: string, patch: { label?: unknown; enabled?: unknown }, by: string) {3 unmodified lines4567891026 unmodified lines3738394041424344454647484950515253545556575859603 unmodified lines*/import { randomUUID } from 'node:crypto';import { db } from './firebase.ts';import { clearCooldown, keyHint, keyStatus, setUiKeys, testKey } from './gemini.ts';interface Stored { id: string; label: string; key: string; enabled: boolean }interface Doc { keys: Stored[]; updatedAt: number; updatedBy: string }26 unmodified linesawait ensure();// cho dán nhiều key một lần (mỗi dòng / phân cách dấu phẩy)const raw = typeof body.keys === 'string' ? body.keys : '';const keys = [...new Set(raw.split(/[\s,;]+/).map((k) => k.trim().replace(/^["']+|["']+$/g, '')).filter(Boolean))];if (!keys.length) throw httpError('Chưa nhập key');if (keys.length > 50) throw httpError('Tối đa 50 key mỗi lần');const label = typeof body.label === 'string' ? body.label.trim().slice(0, 60) : '';const have = new Set(list.map((k) => k.key));// chỉ thêm key gọi thử Gemini được; không kiểm tra định dạngconst fresh = keys.filter((k) => !have.has(k));const results = await Promise.all(fresh.map(async (key) => ({ key, r: await testKey(key) })));const failed = results.filter((x) => !x.r.ok);let added = 0;for (const { key, r } of results) {if (!r.ok) continue;list.push({ id: randomUUID().slice(0, 8), label: label ? (keys.length > 1 ? `${label} #${added + 1}` : label) : `Key ${list.length + 1}`, key, enabled: true });added++;}if (!added && failed.length) throw httpError(`Key không dùng được: ${failed[0].r.error}`);await persist(by);return { added, skipped: keys.length - fresh.length, failed: failed.map((x) => ({ hint: keyHint(x.key), error: x.r.error })) };}export async function updateGeminiKey(id: string, patch: { label?: unknown; enabled?: unknown }, by: string) {
62 unmodified lines63646566676869707162 unmodified linessetKeys('');setLabel('');return r;}, (r) => `Đã thêm ${r.added} key${r.skipped ? `, bỏ qua ${r.skipped} key trùng` : ''}`);}}><label className="stack"><span>API key (dán nhiều key: mỗi dòng một key hoặc cách nhau dấu phẩy)</span><textarea required rows={4} spellCheck={false} autoComplete="off" placeholder={'AIza…\nAIza…'} value={keys} onChange={(e) => setKeys(e.target.value)} style={{ fontFamily: 'monospace' }} /></label><div className="row" style={{ alignItems: 'flex-end', flexWrap: 'wrap' }}>62 unmodified lines63646566676869707162 unmodified linessetKeys('');setLabel('');return r;}, (r) => `Đã thêm ${r.added} key${r.skipped ? `, bỏ qua ${r.skipped} key trùng` : ''}${r.failed.length ? `, ${r.failed.length} key không dùng được (${r.failed.map((f: { hint: string }) => f.hint).join(', ')})` : ''}`);}}><label className="stack"><span>API key (hệ thống sẽ gọi thử, dùng được mới thêm; dán nhiều key: mỗi dòng một key hoặc cách nhau dấu phẩy)</span><textarea required rows={4} spellCheck={false} autoComplete="off" placeholder={'AIza…\nAIza…'} value={keys} onChange={(e) => setKeys(e.target.value)} style={{ fontFamily: 'monospace' }} /></label><div className="row" style={{ alignItems: 'flex-end', flexWrap: 'wrap' }}>
90 unmodified lines9192939495969790 unmodified lines/** Hoá đơn tháng: chi phí cố định (admin nhập) + khoản nào đã thanh toán; chỉ admin sửa được */export interface GeminiKeyRow { id: string; label: string; hint: string; enabled: boolean; calls: number; limited: number; lastUsedAt?: number; lastError?: string; lastErrorAt?: number; cooldownUntil?: number }export const listGeminiKeys = () => call<{ keys: GeminiKeyRow[] }>('/api/gemini-keys');export const addGeminiKeys = (keys: string, label: string) => call<{ added: number; skipped: number }>('/api/gemini-keys', jsonBody('POST', { keys, label }));export const updateGeminiKey = (id: string, patch: { label?: string; enabled?: boolean }) => call(`/api/gemini-keys/${id}`, jsonBody('PATCH', patch));export const deleteGeminiKey = (id: string) => call(`/api/gemini-keys/${id}`, { method: 'DELETE' });export const testGeminiKey = (body: { id?: string; key?: string }) => call<{ ok: boolean; error?: string }>('/api/gemini-keys/test', jsonBody('POST', body));90 unmodified lines9192939495969790 unmodified lines/** Hoá đơn tháng: chi phí cố định (admin nhập) + khoản nào đã thanh toán; chỉ admin sửa được */export interface GeminiKeyRow { id: string; label: string; hint: string; enabled: boolean; calls: number; limited: number; lastUsedAt?: number; lastError?: string; lastErrorAt?: number; cooldownUntil?: number }export const listGeminiKeys = () => call<{ keys: GeminiKeyRow[] }>('/api/gemini-keys');export const addGeminiKeys = (keys: string, label: string) => call<{ added: number; skipped: number; failed: { hint: string; error?: string }[] }>('/api/gemini-keys', jsonBody('POST', { keys, label }));export const updateGeminiKey = (id: string, patch: { label?: string; enabled?: boolean }) => call(`/api/gemini-keys/${id}`, jsonBody('PATCH', patch));export const deleteGeminiKey = (id: string) => call(`/api/gemini-keys/${id}`, { method: 'DELETE' });export const testGeminiKey = (body: { id?: string; key?: string }) => call<{ ok: boolean; error?: string }>('/api/gemini-keys/test', jsonBody('POST', body));
passport-check/apps/server/src/gemini-keys.ts+10-7
passport-check/apps/web/src/components/GeminiKeys.tsx+2-2
passport-check/apps/web/src/lib/api.ts+1-1