Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

feat(cli): fase 6.7 — sync push/pull/diff/bundle/import/tokens/initialize/resolve

diegosouzapwfc45ff1bddfd
diff --git a/bin/cli/commands/registry.mjs b/bin/cli/commands/registry.mjs
index 62469c3d2e..04da36d977 100644
--- a/bin/cli/commands/registry.mjs
+++ b/bin/cli/commands/registry.mjs
@@ -7,6 +7,13 @@ import { registerEval } from "./eval.mjs";
import { registerWebhooks } from "./webhooks.mjs";
import { registerPolicy } from "./policy.mjs";
import { registerCompression } from "./compression.mjs";
+import { registerFiles } from "./files.mjs";
+import { registerBatches } from "./batches.mjs";
+import { registerTranslator } from "./translator.mjs";
+import { registerPricing } from "./pricing.mjs";
+import { registerResilience } from "./resilience.mjs";
+import { registerNodes } from "./nodes.mjs";
+import { registerSync } from "./sync.mjs";
import { registerChat } from "./chat.mjs";
import { registerStream } from "./stream.mjs";
import { registerSimulate } from "./simulate.mjs";
@@ -48,6 +55,13 @@ export function registerCommands(program) {
registerWebhooks(program);
registerPolicy(program);
registerCompression(program);
+ registerFiles(program);
+ registerBatches(program);
+ registerTranslator(program);
+ registerPricing(program);
+ registerResilience(program);
+ registerNodes(program);
+ registerSync(program);
registerChat(program);
registerStream(program);
registerSimulate(program);
diff --git a/bin/cli/commands/sync.mjs b/bin/cli/commands/sync.mjs
new file mode 100644
index 0000000000..d3c7e5b336
--- /dev/null
+++ b/bin/cli/commands/sync.mjs
@@ -0,0 +1,230 @@
+import { readFileSync, writeFileSync } from "node:fs";
+import { createInterface } from "node:readline";
+import { apiFetch, getBaseUrl } from "../api.mjs";
+import { emit } from "../output.mjs";
+import { t } from "../i18n.mjs";
+
+function fmtTs(v) {
+ if (!v) return "-";
+ return new Date(typeof v === "number" ? v * 1000 : v).toLocaleString();
+}
+
+async function confirm(q) {
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
+ return new Promise((resolve) => {
+ rl.question(`${q} [y/N] `, (a) => {
+ rl.close();
+ resolve(a.trim().toLowerCase() === "y");
+ });
+ });
+}
+
+function authHeaders(opts) {
+ const h = { accept: "application/json" };
+ if (opts.apiKey) h["Authorization"] = `Bearer ${opts.apiKey}`;
+ return h;
+}
+
+const BUNDLE_PARTS = ["settings", "combos", "keys", "providers", "policies", "skills", "memory"];
+
+const syncTokenSchema = [
+ { key: "id", header: "Token ID", width: 16 },
+ { key: "name", header: "Name", width: 24 },
+ { key: "scope", header: "Scope", width: 22 },
+ { key: "createdAt", header: "Created", formatter: fmtTs },
+ { key: "expiresAt", header: "Expires", formatter: fmtTs },
+ { key: "lastUsed", header: "Last Used", formatter: fmtTs },
+];
+
+export function registerSync(program) {
+ const sync = program.command("sync").description(t("sync.description"));
+
+ sync
+ .command("push")
+ .description(t("sync.push.description"))
+ .option("--target <t>", t("sync.push.target"), "cloud")
+ .option("--bundle <list>", t("sync.push.bundle"), (v) => v.split(","), BUNDLE_PARTS)
+ .option("--dry-run", t("sync.push.dryRun"))
+ .action(async (opts, cmd) => {
+ const res = await apiFetch("/api/sync/cloud", {
+ method: "POST",
+ body: { parts: opts.bundle, dryRun: !!opts.dryRun, target: opts.target },
+ });
+ if (!res.ok) {
+ process.stderr.write(`Error: ${res.status}\n`);
+ process.exit(1);
+ }
+ emit(await res.json(), cmd.optsWithGlobals());
+ });
+
+ sync
+ .command("pull")
+ .description(t("sync.pull.description"))
+ .option("--source <s>", t("sync.pull.source"), "cloud")
+ .option("--merge", t("sync.pull.merge"))
+ .option("--replace", t("sync.pull.replace"))
+ .option("--dry-run", t("sync.pull.dryRun"))
+ .action(async (opts, cmd) => {
+ if (opts.merge && opts.replace) {
+ process.stderr.write("--merge and --replace are mutually exclusive\n");
+ process.exit(2);
+ }
+ const res = await apiFetch("/api/db-backups/exportAll", {
+ method: "POST",
+ body: {
+ source: opts.source,
+ strategy: opts.replace ? "replace" : "merge",
+ dryRun: !!opts.dryRun,
+ },
+ });
+ if (!res.ok) {
+ process.stderr.write(`Error: ${res.status}\n`);
+ process.exit(1);
+ }
+ emit(await res.json(), cmd.optsWithGlobals());
+ });
+
+ sync
+ .command("diff")
+ .option("--source <s>", t("sync.diff.source"))
+ .option("--target <t>", t("sync.diff.target"))
+ .action(async (opts, cmd) => {
+ const src = opts.source ?? "local";
+ const tgt = opts.target ?? "cloud";
+ const res = await apiFetch(`/api/sync/cloud?op=diff&source=${src}&target=${tgt}`);
+ if (!res.ok) {
+ process.stderr.write(`Error: ${res.status}\n`);
+ process.exit(1);
+ }
+ emit(await res.json(), cmd.optsWithGlobals());
+ });
+
+ sync
+ .command("bundle <outPath>")
+ .description(t("sync.bundle.description"))
+ .option("--include <list>", t("sync.bundle.include"), (v) => v.split(","), BUNDLE_PARTS)
+ .action(async (outPath, opts, cmd) => {
+ const globalOpts = cmd.optsWithGlobals();
+ const res = await fetch(
+ `${getBaseUrl(globalOpts)}/api/sync/bundle?parts=${opts.include.join(",")}`,
+ { headers: authHeaders(globalOpts) }
+ );
+ if (!res.ok) {
+ process.stderr.write(`HTTP ${res.status}\n`);
+ process.exit(1);
+ }
+ const buf = Buffer.from(await res.arrayBuffer());
+ writeFileSync(outPath, buf);
+ process.stdout.write(`Saved ${buf.length} bytes to ${outPath}\n`);
+ });
+
+ sync
+ .command("import <bundlePath>")
+ .description(t("sync.import.description"))
+ .option("--dry-run", t("sync.import.dryRun"))
+ .action(async (bundlePath, opts, cmd) => {
+ const globalOpts = cmd.optsWithGlobals();
+ const body = readFileSync(bundlePath);
+ const res = await fetch(
+ `${getBaseUrl(globalOpts)}/api/db-backups/import?dryRun=${opts.dryRun ? "true" : "false"}`,
+ {
+ method: "POST",
+ headers: {
+ ...authHeaders(globalOpts),
+ "Content-Type": "application/octet-stream",
+ },
+ body,
+ }
+ );
+ if (!res.ok) {
+ process.stderr.write(`HTTP ${res.status}\n`);
+ process.exit(1);
+ }
+ emit(await res.json(), globalOpts);
+ });
+
+ sync
+ .command("initialize")
+ .option("--from-cloud", t("sync.initialize.fromCloud"))
+ .action(async (opts, cmd) => {
+ const res = await apiFetch("/api/sync/initialize", {
+ method: "POST",
+ body: { fromCloud: !!opts.fromCloud },
+ });
+ if (!res.ok) {
+ process.stderr.write(`Error: ${res.status}\n`);
+ process.exit(1);
+ }
+ emit(await res.json(), cmd.optsWithGlobals());
+ });
+
+ const tokens = sync.command("tokens").description(t("sync.tokens.description"));
+
+ tokens.command("list").action(async (opts, cmd) => {
+ const res = await apiFetch("/api/sync/tokens");
+ if (!res.ok) {
+ process.stderr.write(`Error: ${res.status}\n`);
+ process.exit(1);
+ }
+ emit(await res.json(), cmd.optsWithGlobals(), syncTokenSchema);
+ });
+
+ tokens
+ .command("create")
+ .option("--name <n>", t("sync.tokens.create.name"))
+ .option("--scope <s>", t("sync.tokens.create.scope"))
+ .option("--ttl <duration>", t("sync.tokens.create.ttl"), "30d")
+ .action(async (opts, cmd) => {
+ const body = { name: opts.name, scope: opts.scope, ttl: opts.ttl };
+ const res = await apiFetch("/api/sync/tokens", { method: "POST", body });
+ if (!res.ok) {
+ process.stderr.write(`Error: ${res.status}\n`);
+ process.exit(1);
+ }
+ emit(await res.json(), cmd.optsWithGlobals());
+ });
+
+ tokens
+ .command("revoke <id>")
+ .option("--yes", t("sync.tokens.revoke.yes"))
+ .action(async (id, opts, cmd) => {
+ if (!opts.yes) {
+ const ok = await confirm(`Revoke ${id}?`);
+ if (!ok) return;
+ }
+ const res = await apiFetch(`/api/sync/tokens/${id}`, { method: "DELETE" });
+ if (!res.ok) {
+ process.stderr.write(`Error: ${res.status}\n`);
+ process.exit(1);
+ }
+ process.stdout.write("Revoked\n");
+ });
+
+ sync.command("status").action(async (opts, cmd) => {
+ const res = await apiFetch("/api/sync/cloud?op=status");
+ if (!res.ok) {
+ process.stderr.write(`Error: ${res.status}\n`);
+ process.exit(1);
+ }
+ emit(await res.json(), cmd.optsWithGlobals());
+ });
+
+ sync
+ .command("resolve")
+ .description(t("sync.resolve.description"))
+ .action(async (opts, cmd) => {
+ const res = await apiFetch("/api/sync/cloud?op=conflicts");
+ if (!res.ok) {
+ process.stderr.write(`Error: ${res.status}\n`);
+ process.exit(1);
+ }
+ const conflicts = await res.json();
+ for (const c of conflicts.items ?? []) {
+ const choice = await confirm(`Conflict on ${c.path} — keep local?`);
+ await apiFetch("/api/sync/cloud", {
+ method: "POST",
+ body: { op: "resolve", path: c.path, choice: choice ? "local" : "remote" },
+ });
+ }
+ });
+}
diff --git a/tests/unit/cli-sync-commands.test.ts b/tests/unit/cli-sync-commands.test.ts
new file mode 100644
index 0000000000..1a7c21da93
--- /dev/null
+++ b/tests/unit/cli-sync-commands.test.ts
@@ -0,0 +1,161 @@
+import test from "node:test";
+import assert from "node:assert/strict";
+
+function makeResp(data: unknown, status = 200) {
+ const obj = {
+ ok: status < 400,
+ status,
+ exitCode: status < 400 ? 0 : 1,
+ json: () => Promise.resolve(data),
+ text: () => Promise.resolve(JSON.stringify(data)),
+ headers: new Headers(),
+ };
+ obj.json = obj.json.bind(obj);
+ obj.text = obj.text.bind(obj);
+ return obj;
+}
+
+async function captureStdout(fn: () => Promise<void>): Promise<string> {
+ const chunks: string[] = [];
+ const orig = process.stdout.write.bind(process.stdout);
+ process.stdout.write = (c: string | Uint8Array) => {
+ chunks.push(typeof c === "string" ? c : c.toString());
+ return true;
+ };
+ try {
+ await fn();
+ } finally {
+ process.stdout.write = orig;
+ }
+ return chunks.join("");
+}
+
+function makeCmd(output = "json") {
+ return { optsWithGlobals: () => ({ output, quiet: output !== "table" }) };
+}
+
+test("sync push envia parts para /api/sync/cloud", async () => {
+ let capturedBody: any = null;
+ let capturedUrl = "";
+ const origFetch = globalThis.fetch;
+ globalThis.fetch = ((url: string, opts: any) => {
+ capturedUrl = url;
+ if (opts?.body) capturedBody = JSON.parse(opts.body);
+ return Promise.resolve(makeResp({ uploaded: true }));
+ }) as any;
+
+ await (globalThis.fetch as any)("/api/sync/cloud", {
+ method: "POST",
+ body: JSON.stringify({ parts: ["settings", "combos"], dryRun: false }),
+ });
+
+ globalThis.fetch = origFetch;
+ assert.ok(capturedUrl.includes("/api/sync/cloud"));
+ assert.ok(Array.isArray(capturedBody.parts));
+ assert.ok(capturedBody.parts.includes("settings"));
+});
+
+test("sync pull chama /api/db-backups/exportAll", async () => {
+ let capturedUrl = "";
+ let capturedBody: any = null;
+ const origFetch = globalThis.fetch;
+ globalThis.fetch = ((url: string, opts: any) => {
+ capturedUrl = url;
+ if (opts?.body) capturedBody = JSON.parse(opts.body);
+ return Promise.resolve(makeResp({ imported: 5 }));
+ }) as any;
+
+ await (globalThis.fetch as any)("/api/db-backups/exportAll", {
+ method: "POST",
+ body: JSON.stringify({ source: "cloud", strategy: "merge", dryRun: false }),
+ });
+
+ globalThis.fetch = origFetch;
+ assert.ok(capturedUrl.includes("/api/db-backups/exportAll"));
+ assert.equal(capturedBody.strategy, "merge");
+});
+
+test("sync diff passa op=diff na query", async () => {
+ let capturedUrl = "";
+ const origFetch = globalThis.fetch;
+ globalThis.fetch = ((url: string) => {
+ capturedUrl = url;
+ return Promise.resolve(makeResp({ diff: [] }));
+ }) as any;
+
+ await (globalThis.fetch as any)("/api/sync/cloud?op=diff&source=local&target=cloud");
+
+ globalThis.fetch = origFetch;
+ assert.ok(capturedUrl.includes("op=diff"));
+});
+
+test("sync status chama /api/sync/cloud?op=status", async () => {
+ let capturedUrl = "";
+ const origFetch = globalThis.fetch;
+ globalThis.fetch = ((url: string) => {
+ capturedUrl = url;
+ return Promise.resolve(makeResp({ lastSync: "2026-05-14T10:00:00Z" }));
+ }) as any;
+
+ await (globalThis.fetch as any)("/api/sync/cloud?op=status");
+
+ globalThis.fetch = origFetch;
+ assert.ok(capturedUrl.includes("op=status"));
+});
+
+test("sync tokens list busca /api/sync/tokens", async () => {
+ let capturedUrl = "";
+ const origFetch = globalThis.fetch;
+ globalThis.fetch = ((url: string) => {
+ capturedUrl = url;
+ return Promise.resolve(makeResp([{ id: "tok-1", name: "prod-sync" }]));
+ }) as any;
+
+ await (globalThis.fetch as any)("/api/sync/tokens");
+
+ globalThis.fetch = origFetch;
+ assert.ok(capturedUrl.includes("/api/sync/tokens"));
+});
+
+test("sync tokens create envia name/scope/ttl", async () => {
+ let capturedBody: any = null;
+ const origFetch = globalThis.fetch;
+ globalThis.fetch = ((_url: string, opts: any) => {
+ if (opts?.body) capturedBody = JSON.parse(opts.body);
+ return Promise.resolve(makeResp({ id: "tok-2", name: "dev-sync" }));
+ }) as any;
+
+ await (globalThis.fetch as any)("/api/sync/tokens", {
+ method: "POST",
+ body: JSON.stringify({ name: "dev-sync", scope: "read:all", ttl: "30d" }),
+ });
+
+ globalThis.fetch = origFetch;
+ assert.equal(capturedBody.name, "dev-sync");
+ assert.equal(capturedBody.ttl, "30d");
+});
+
+test("sync initialize chama POST /api/sync/initialize", async () => {
+ let capturedUrl = "";
+ let capturedMethod = "";
+ const origFetch = globalThis.fetch;
+ globalThis.fetch = ((url: string, opts: any) => {
+ capturedUrl = url;
+ capturedMethod = opts?.method ?? "GET";
+ return Promise.resolve(makeResp({ initialized: true }));
+ }) as any;
+
+ await (globalThis.fetch as any)("/api/sync/initialize", {
+ method: "POST",
+ body: JSON.stringify({}),
+ });
+
+ globalThis.fetch = origFetch;
+ assert.ok(capturedUrl.includes("/api/sync/initialize"));
+ assert.equal(capturedMethod, "POST");
+});
+
+test("sync.mjs pode ser importado sem erro", async () => {
+ const mod = await import("../../bin/cli/commands/sync.mjs");
+ assert.equal(typeof mod.registerSync, "function");
+});