Skip to content

🤖 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.

fix(agent): 已授权账号的别名不能重复

dyk98ae23ec40df49
diff --git a/src/agent/accounts.ts b/src/agent/accounts.ts
index 039b914..ed86821 100644
--- a/src/agent/accounts.ts
+++ b/src/agent/accounts.ts
@@ -176,7 +176,7 @@ export class AccountStore {
const account: ConnectedAccount = {
id: crypto.randomBytes(8).toString("hex"),
service: input.service,
- label: input.label.trim().slice(0, 80) || SERVICE_NAME[input.service],
+ label: this.freeLabel(input.service, input.label),
identity: input.identity,
needsReauth: false,
createdAt: Date.now(),
@@ -196,13 +196,34 @@ export class AccountStore {
this.save();
}
- rename(id: string, label: string): boolean {
+ /**
+ * 改名。同一个服务里名字不能重复:用户说名字时要能选到唯一一个账号。
+ * 也不能全是数字:模型把数字当序号,「2」就分不清是第二个账号还是叫 2 的账号。
+ */
+ rename(id: string, label: string): "ok" | "invalid" | "taken" {
const a = this.get(id);
const name = label.trim().slice(0, 80);
- if (!a || !name) return false;
+ if (!a || !name || /^\d+$/.test(name)) return "invalid";
+ if (this.labelTaken(a.service, name, id)) return "taken";
a.label = name;
this.save();
- return true;
+ return "ok";
+ }
+
+ private labelTaken(service: AccountService, label: string, except?: string): boolean {
+ const lower = label.toLowerCase();
+ return this.list(service).some((a) => a.id !== except && a.label.toLowerCase() === lower);
+ }
+
+ /** 新账号的默认名字(工作区名、飞书用户名):同名或全是数字时加点东西,账号总能按名字选到 */
+ private freeLabel(service: AccountService, label: string): string {
+ const raw = label.trim().slice(0, 76);
+ const base = !raw ? SERVICE_NAME[service] : /^\d+$/.test(raw) ? `${SERVICE_NAME[service]} ${raw}` : raw;
+ if (!this.labelTaken(service, base)) return base;
+ for (let n = 2; ; n++) {
+ const next = `${base} ${n}`;
+ if (!this.labelTaken(service, next)) return next;
+ }
}
remove(id: string): boolean {
@@ -241,10 +262,10 @@ export class AccountStore {
const pick = /^\d+$/.test(raw) ? list[Number(raw) - 1] : undefined;
if (pick) return pick;
const lower = raw.toLowerCase();
- const exact = list.find((a) => a.label.toLowerCase() === lower);
- if (exact) return exact;
- const near = list.filter((a) => a.label.toLowerCase().includes(lower));
+ const exact = list.filter((a) => a.label.toLowerCase() === lower);
+ const near = exact.length ? exact : list.filter((a) => a.label.toLowerCase().includes(lower));
if (near.length === 1) return near[0];
+ if (near.length > 1) throw new ConnectionError(`Several ${name} accounts match "${raw.slice(0, 80)}". Say which one by number, or ask the user:\n${this.listText(service)}`);
throw new ConnectionError(`There is no ${name} account "${raw.slice(0, 80)}". Connected accounts:\n${this.listText(service)}`);
}
}
diff --git a/src/i18n/messages/web.ts b/src/i18n/messages/web.ts
index f553489..5396dfd 100644
--- a/src/i18n/messages/web.ts
+++ b/src/i18n/messages/web.ts
@@ -16,7 +16,8 @@ export const zh = {
badSearchUrl: "SearXNG 地址要以 http:// 或 https:// 开头",
notionTokenRejected: "Notion 拒绝了这个令牌,请检查后重新填写",
feishuAppIncomplete: "App ID 和 App Secret 要一起填",
- badAccountName: "账号名字不能为空",
+ badAccountName: "名字不能为空,也不能只有数字",
+ accountNameTaken: "这个名字已经有账号在用了,换一个吧",
oauthDone: "已连接",
oauthClose: "可以关闭这个页面,回到 AnyModelBot 了。",
oauthFailed: "没有连接成功",
@@ -64,7 +65,8 @@ export const en: Catalog<typeof zh> = {
badSearchUrl: "The SearXNG address must start with http:// or https://",
notionTokenRejected: "Notion rejected this token. Check it and try again.",
feishuAppIncomplete: "Enter both the App ID and the App Secret",
- badAccountName: "The account name can't be empty",
+ badAccountName: "The name can't be empty or only numbers",
+ accountNameTaken: "Another account already uses this name. Pick a different one.",
oauthDone: "Connected",
oauthClose: "You can close this page and go back to AnyModelBot.",
oauthFailed: "Couldn't connect",
diff --git a/src/web/server.ts b/src/web/server.ts
index d6e1376..9849f74 100644
--- a/src/web/server.ts
+++ b/src/web/server.ts
@@ -536,7 +536,8 @@ export async function startWebServer(app: App, opts: { port?: number; host?: str
}
if (seg.length === 2 && m === "PUT") {
const b = await readJson(req);
- if (!c.accounts.rename(seg[1], String(b.label ?? ""))) throw new HttpError(400, t("web.badAccountName"));
+ const renamed = c.accounts.rename(seg[1], String(b.label ?? ""));
+ if (renamed !== "ok") throw new HttpError(400, t(renamed === "taken" ? "web.accountNameTaken" : "web.badAccountName"));
return send(res, 200, { accounts: c.accounts.view() });
}
if (seg.length === 2 && m === "DELETE") {
diff --git a/test/connections.unit.test.ts b/test/connections.unit.test.ts
index 6c3681d..101ffad 100644
--- a/test/connections.unit.test.ts
+++ b/test/connections.unit.test.ts
@@ -152,6 +152,44 @@ describe("已授权账号:全局列表", () => {
expect(b.needsReauth).toBe(false);
});
+ it("别名:同一个服务里不能重复(不分大小写),不能为空或全是数字;不同服务可以同名;改回自己的名字没问题", () => {
+ const accounts = new AccountStore(memoryKv());
+ const a = add(accounts, "feishu", "Work", "a");
+ const b = add(accounts, "feishu", "Home", "b");
+ const n = add(accounts, "notion", "Acme", "n");
+ expect(accounts.rename(b.id, "work")).toBe("taken");
+ expect(accounts.get(b.id)!.label).toBe("Home");
+ expect(accounts.rename(b.id, " ")).toBe("invalid");
+ expect(accounts.rename(b.id, "2")).toBe("invalid");
+ expect(accounts.rename(n.id, "Work")).toBe("ok");
+ expect(accounts.rename(a.id, "WORK")).toBe("ok");
+ expect(accounts.rename(b.id, "个人飞书")).toBe("ok");
+ expect(accounts.resolve("feishu", "个人飞书").id).toBe(b.id);
+ });
+
+ it("默认名字撞车:自动加序号,账号总能按名字选到;全是数字时带上服务名", () => {
+ const accounts = new AccountStore(memoryKv());
+ add(accounts, "feishu", "张三", "a");
+ add(accounts, "feishu", "张三", "b");
+ add(accounts, "feishu", "张三", "c");
+ add(accounts, "feishu", "123", "d");
+ expect(accounts.view().map((v) => v.label)).toEqual(["张三", "张三 2", "张三 3", "Feishu 123"]);
+ expect(accounts.resolve("feishu", "张三 2").identity).toBe("b");
+ });
+
+ it("名字对不上唯一一个账号:不猜,报错并列出来", () => {
+ const kv = memoryKv();
+ const auth = { kind: "notion-token", token: "t" };
+ kv.setKV(ACCOUNTS_KV, JSON.stringify([{ id: "a", service: "feishu", identity: "a", label: "工作", auth }, { id: "b", service: "feishu", identity: "b", label: "工作", auth }]));
+ const accounts = new AccountStore(kv);
+ expect(() => accounts.resolve("feishu", "工作")).toThrow(/Several Feishu accounts match/);
+ const other = new AccountStore(memoryKv());
+ add(other, "feishu", "工作一组", "x");
+ add(other, "feishu", "工作二组", "y");
+ expect(() => other.resolve("feishu", "工作")).toThrow(/Several Feishu accounts match/);
+ expect(other.resolve("feishu", "二组").identity).toBe("y");
+ });
+
it("坏数据不会让程序起不来,只跳过坏的那条", () => {
const kv = memoryKv();
kv.setKV(ACCOUNTS_KV, JSON.stringify([{ id: "x", service: "notion", identity: "i", auth: { kind: "notion-token" } }, { id: "y", service: "notion", identity: "j", label: "Ok", auth: { kind: "notion-token", token: "t" } }, "junk"]));