📄 Tệp code
body/html toàn cục, position: fixed ở vỏ ngoài hay comment //.
.html của trang Studio về máy người dùng.Ctrl+U, Ctrl+S, F12, Ctrl+Shift+I) và khóa chuột phải ngoài vùng soạn thảo để bảo vệ bản quyền code của anh.
xkiro-proxy.khoathegioi.workers.dev) đã gắn 1 bộ nhớ KV tên là VAULT_KV (hoặc POPUP_VAULT), anh chỉ cần thêm nhánh /vault nhỏ gọn dưới đây vào đầu hàm fetch(request, env) là mọi thiết bị vào link sẽ tự động dùng chung Tủ mẫu:
export default {
async fetch(request, env, ctx) {
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
"Cache-Control": "no-store, no-cache, must-revalidate",
"Content-Type": "application/json"
};
if (request.method === "OPTIONS") {
return new Response(null, { status: 204, headers: corsHeaders });
}
const url = new URL(request.url);
// Tự động nhận diện cả D1 Database (popup-db) lẫn KV Namespace trong Bindings
let d1 = null;
let kv = null;
if (env) {
for (const k of Object.keys(env)) {
const b = env[k];
if (b && typeof b.prepare === "function") d1 = b;
else if (b && typeof b.get === "function" && typeof b.put === "function") kv = b;
}
}
// 1. NHÁNH TỦ LƯU TRỮ CHUNG (/vault hoặc /api/vault)
if (url.pathname.endsWith("/vault")) {
try {
if (d1) {
await d1.prepare("CREATE TABLE IF NOT EXISTS popup_vault (id TEXT PRIMARY KEY, data TEXT)").run();
if (request.method === "GET") {
const row = await d1.prepare("SELECT data FROM popup_vault WHERE id = 'shared_vault_v10'").first();
return new Response((row && row.data) ? row.data : "[]", { status: 200, headers: corsHeaders });
}
if (request.method === "POST") {
const body = await request.text();
await d1.prepare("INSERT OR REPLACE INTO popup_vault (id, data) VALUES ('shared_vault_v10', ?1)").bind(body).run();
return new Response(JSON.stringify({ ok: true, storage: "d1" }), { status: 200, headers: corsHeaders });
}
} else if (kv) {
if (request.method === "GET") {
const data = await kv.get("shared_vault_v10");
return new Response(data || "[]", { status: 200, headers: corsHeaders });
}
if (request.method === "POST") {
const body = await request.text();
await kv.put("shared_vault_v10", body);
return new Response(JSON.stringify({ ok: true, storage: "kv" }), { status: 200, headers: corsHeaders });
}
} else {
if (request.method === "GET") {
return new Response(globalThis.__MEM_VAULT || "[]", { status: 200, headers: corsHeaders });
}
if (request.method === "POST") {
globalThis.__MEM_VAULT = await request.text();
return new Response(JSON.stringify({ ok: true, storage: "memory_only" }), { status: 200, headers: corsHeaders });
}
}
} catch (dbErr) {
return new Response(JSON.stringify({ ok: false, error: dbErr.message }), { status: 500, headers: corsHeaders });
}
}
// 2. NHÁNH PROXY AI XKIRO (CHUYỂN TIẾP CHAT COMPLETIONS)
if (request.method !== "POST") {
return new Response(JSON.stringify({
status: "ok",
service: "AE888 xKiro Proxy & Shared Vault",
storage: d1 ? "d1_connected" : (kv ? "kv_connected" : "memory_only")
}), {
status: 200,
headers: corsHeaders
});
}
try {
const authHeader = request.headers.get("Authorization") || (env.XKIRO_API_KEY ? `Bearer ${env.XKIRO_API_KEY}` : "");
const bodyText = await request.text();
const upstreamRes = await fetch("https://api.xkiro.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": authHeader
},
body: bodyText
});
const resText = await upstreamRes.text();
return new Response(resText, {
status: upstreamRes.status,
headers: corsHeaders
});
} catch (err) {
return new Response(JSON.stringify({ error: { message: err.message || "Proxy Error" } }), {
status: 500,
headers: corsHeaders
});
}
}
};