Feature : RTK compress

This commit is contained in:
decolua 2026-04-22 15:36:51 +07:00
parent e1a219dba6
commit 8de9aae90c
26 changed files with 1612 additions and 0 deletions

View file

@ -25,6 +25,7 @@ export default function APIPageClient({ machineId }) {
const [requireLogin, setRequireLogin] = useState(true);
const [hasPassword, setHasPassword] = useState(true);
const [tunnelDashboardAccess, setTunnelDashboardAccess] = useState(false);
const [rtkEnabled, setRtkEnabledState] = useState(false);
// Cloudflare Tunnel state
const [tunnelChecking, setTunnelChecking] = useState(true);
@ -80,6 +81,7 @@ export default function APIPageClient({ machineId }) {
setRequireLogin(data.requireLogin !== false);
setHasPassword(data.hasPassword || false);
setTunnelDashboardAccess(data.tunnelDashboardAccess || false);
setRtkEnabledState(data.rtkEnabled || false);
}
if (statusRes.ok) {
const data = await statusRes.json();
@ -167,6 +169,19 @@ export default function APIPageClient({ machineId }) {
}
};
const handleRtkEnabled = async (value) => {
try {
const res = await fetch("/api/settings", {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ rtkEnabled: value }),
});
if (res.ok) setRtkEnabledState(value);
} catch (error) {
console.log("Error updating rtkEnabled:", error);
}
};
const fetchData = async () => {
try {
const keysRes = await fetch("/api/keys");
@ -798,6 +813,42 @@ export default function APIPageClient({ machineId }) {
)}
</Card>
{/* Token Saver (RTK) */}
<Card id="rtk">
<div className="flex items-center justify-between mb-2">
<div className="flex items-center gap-2">
<h2 className="text-lg font-semibold">Token Saver</h2>
<span className="px-2 py-0.5 text-xs font-medium rounded-full bg-amber-500/15 text-amber-600 dark:text-amber-400 border border-amber-500/30">
Experimental
</span>
</div>
</div>
<div className="flex items-center justify-between pt-2">
<div className="pr-4">
<p className="font-medium">Compress tool output</p>
<p className="text-sm text-text-muted">
Auto-compress git diff / status / grep / find / ls / tree / logs in <code>tool_result</code> before sending to LLM. Check server console for <code>[RTK] saved ...</code> log.
</p>
<p className="text-xs text-text-muted mt-1">
Inspired by{" "}
<a
href="https://github.com/rtk-ai/rtk"
target="_blank"
rel="noopener noreferrer"
className="underline hover:text-primary"
>
RTK (Rust Token Killer)
</a>
{" "} ported to JavaScript. This feature is still under testing; disable it if you notice unexpected results.
</p>
</div>
<Toggle
checked={rtkEnabled}
onChange={() => handleRtkEnabled(!rtkEnabled)}
/>
</div>
</Card>
{/* API Keys */}
<Card id="require-api-key">
<div className="flex items-center justify-between mb-4">

View file

@ -197,6 +197,21 @@ export async function POST(request) {
break;
}
case "opencode-go": {
const res = await fetch("https://opencode.ai/zen/go/v1/chat/completions", {
method: "POST",
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${apiKey}` },
body: JSON.stringify({
model: getDefaultModel("opencode-go"),
messages: [{ role: "user", content: "ping" }],
max_tokens: 1,
stream: false,
}),
});
isValid = res.status !== 401 && res.status !== 403;
break;
}
case "deepgram": {
const res = await fetch("https://api.deepgram.com/v1/projects", {
headers: { "Authorization": `Token ${apiKey}` },

View file

@ -1,6 +1,7 @@
import { NextResponse } from "next/server";
import { getSettings, updateSettings } from "@/lib/localDb";
import { applyOutboundProxyEnv } from "@/lib/network/outboundProxy";
import { setRtkEnabled } from "open-sse/rtk/flag.js";
import bcrypt from "bcryptjs";
export async function GET() {
@ -65,6 +66,11 @@ export async function PATCH(request) {
) {
applyOutboundProxyEnv(settings);
}
// Sync RTK toggle immediately (sync cache for request hot path)
if (Object.prototype.hasOwnProperty.call(body, "rtkEnabled")) {
setRtkEnabled(settings.rtkEnabled);
}
const { password, ...safeSettings } = settings;
return NextResponse.json(safeSettings);
} catch (error) {

View file

@ -3,6 +3,7 @@ import "./globals.css";
import { ThemeProvider } from "@/shared/components/ThemeProvider";
import "@/lib/initCloudSync"; // Auto-initialize cloud sync
import "@/lib/network/initOutboundProxy"; // Auto-initialize outbound proxy env
import "@/lib/rtk/initRtk"; // Auto-initialize RTK toggle from DB
import { initConsoleLogCapture } from "@/lib/consoleLogBuffer";
import { RuntimeI18nProvider } from "@/i18n/RuntimeI18nProvider";

View file

@ -36,6 +36,7 @@ const DEFAULT_SETTINGS = {
outboundProxyUrl: "",
outboundNoProxy: "",
mitmRouterBaseUrl: DEFAULT_MITM_ROUTER_BASE,
rtkEnabled: false,
};
function cloneDefaultData() {

20
src/lib/rtk/initRtk.js Normal file
View file

@ -0,0 +1,20 @@
import { getSettings } from "@/lib/localDb";
import { setRtkEnabled } from "open-sse/rtk/flag.js";
let initialized = false;
export async function ensureRtkInitialized() {
if (initialized) return true;
try {
const settings = await getSettings();
setRtkEnabled(settings.rtkEnabled === true);
initialized = true;
} catch (error) {
console.error("[ServerInit] Error initializing RTK flag:", error);
}
return initialized;
}
ensureRtkInitialized().catch(console.log);
export default ensureRtkInitialized;