🤖 Open-source, local-first alternative to Grok Bot, OpenAI dots and Claude Duet. Your own AI bots on any model: GPT, Claude, Gemini, Grok, DeepSeek, Kimi, GLM and 70+ more through one Modelflare key. Reach them in the app, WeChat or Feishu; they remember you, work in the background, run commands and use your computer. Pay per use.
Python ai-agentai-assistantchatbotclaudecomputer-usedeepseekdesktop-appdshelectronfeishugeminigptgrokllmlocal-firstmodelflaremulti-modelopenai-compatiblepersonal-ai-agentwechat
/**
* 首次设置向导(终端版;界面里也能完成同样的设置):
* 1. 连接 Modelflare:登录账户(打印登录链接,在这台电脑的浏览器里完成;SSH 远程时在别的设备上登录,再把链接贴回来)
* 2. 飞书 App ID / App Secret → 写入本程序数据库
*/
import readline from "node:readline";
import { spawn } from "node:child_process";
import { stdin, stdout } from "node:process";
import { loadConfig } from "../config.js";
import { App } from "../app.js";
import { normalizeLine } from "../modelflare.js";
import { t as tr } from "../i18n/index.js";
/** 行队列:兼容交互终端与管道输入(管道时所有行会一次性到达) */
function lineReader() {
const tty = !!stdin.isTTY;
const rl = readline.createInterface({ input: stdin, output: stdout, terminal: tty });
const lines: string[] = [];
const waiters: Array<(l: string | undefined) => void> = [];
let closed = false;
let muted = false;
const r = rl as unknown as { _writeToOutput?: (s: string) => void };
const orig = r._writeToOutput?.bind(rl);
r._writeToOutput = (str: string) => {
if (!muted) return orig?.(str);
if (str.includes("\n") || str.includes("\r")) stdout.write("\n");
else if (!str.startsWith("\x1b")) stdout.write("*".repeat([...str].length));
};
rl.on("line", (l) => (waiters.length ? waiters.shift()!(l) : lines.push(l)));
rl.on("close", () => {
closed = true;
while (waiters.length) waiters.shift()!(undefined);
});
const next = () => (lines.length ? Promise.resolve(lines.shift()) : closed ? Promise.resolve(undefined) : new Promise<string | undefined>((res) => waiters.push(res)));
return {
async ask(q: string, secret = false): Promise<string> {
stdout.write(q);
muted = secret && tty;
const l = await next();
muted = false;
if (!tty) stdout.write(secret ? tr("cli.entered") + "\n" : `${l ?? ""}\n`);
return (l ?? "").trim();
},
close: () => rl.close(),
};
}
async function main(): Promise<void> {
const config = loadConfig();
const app = new App(config, { noWeb: true });
const rl = lineReader();
try {
await setup(app, rl, config.dataDir);
} finally {
rl.close();
await app.magpie?.stop();
app.store.close();
}
console.log("\n" + tr("cli.done"));
}
async function setup(app: App, rl: ReturnType<typeof lineReader>, dataDir: string): Promise<void> {
console.log(tr("cli.title", { dir: dataDir }) + "\n");
if (app.magpie) {
if (!app.magpie.binPath) {
throw new Error(tr("cli.noMagpie"));
}
await app.magpie.start();
const account = app.ensureAccount();
await app.retirePastedKey();
const status = app.modelflareStatus();
if (status.connected) {
console.log(tr("cli.alreadyConnected", { tail: status.keyTail, account: account.signedIn ? tr("cli.accountSuffix", { name: account.status().name || tr("cli.signedIn") }) : "" }));
} else {
const how = (await rl.ask(tr("cli.askHow"))).toLowerCase();
if (how !== "skip") {
const line = normalizeLine((await rl.ask(tr("cli.askLine"))).trim().toLowerCase());
await app.setModelflareLine(line);
await loginInBrowser(app, rl, how === "remote");
}
}
}
const appId = (await rl.ask(tr("cli.askFeishuId"))).trim();
if (appId) {
const secret = await rl.ask(tr("cli.askFeishuSecret"), true);
let bot = app.store.listBots()[0];
if (!bot) bot = app.store.createBot({ id: "default", name: tr("app.default_bot_name"), weixinEnabled: true });
app.store.updateBot(bot.id, { feishuAppId: appId, feishuAppSecret: secret });
console.log(tr("cli.savedToBot", { name: bot.name }));
}
}
/** 浏览器登录:回调落在本机 127.0.0.1。remote:浏览器在别的设备上(SSH 远程),回调到不了这里,把登录后的链接贴回来 */
async function loginInBrowser(app: App, rl: ReturnType<typeof lineReader>, remote: boolean): Promise<void> {
const account = app.ensureAccount();
const url = await account.startLogin();
console.log(`\n${tr("cli.openLink")}\n${url}\n`);
if (remote) account.completeLoginFromLink(await rl.ask(tr("cli.askLoginLink")));
else openInBrowser(url);
for (;;) {
await new Promise((r) => setTimeout(r, 500));
const s = account.status();
if (s.state === "signed_in") {
const models = await app.runtime.listModels(true).catch(() => []);
console.log(tr("cli.signedInConnected", { name: s.name || "Modelflare", count: models.length }));
return;
}
if (s.state !== "signing_in") throw new Error(s.error || tr("cli.loginIncomplete"));
}
}
/** 尽力打开系统浏览器;打不开也没关系,链接已经打印出来了。参数直接传给程序,不经过 shell */
function openInBrowser(url: string): void {
const [cmd, args] =
process.platform === "darwin" ? ["open", [url]] : process.platform === "win32" ? ["rundll32", ["url.dll,FileProtocolHandler", url]] : ["xdg-open", [url]];
try {
const child = spawn(cmd, args as string[], { stdio: "ignore", detached: true });
child.on("error", () => {});
child.unref();
} catch {
/* 没有可用的打开方式 */
}
}
main().catch((e) => {
console.error(tr("cli.setupFailed"), (e as Error).message);
process.exit(1);
});