Add intelligent soft fuzzy search to corps search command

Support Chinese homophones, front/back nasal vowels, initials, typo/missing letter tolerance, and --strict exact mode fallback.
This commit is contained in:
gitea committed 2026-09-11 17:43:58 +08:00
1 parent 5a5c0c11a9
commit b6aa4efd09
8 files changed
+772 -211

No files matched your search

+32
View File
@@ -293,6 +293,38 @@ describe("corps get and search", () => {
const listWithQuery = await runCli(["corps", "list", "演练"]);
expect(listWithQuery.exitCode).toBe(0);
expect(JSON.parse(listWithQuery.stdout).length).toBe(1);
// Soft search by homophone (盛产 -> 生产正式企业)
const softHomophone = await runCli(["corps", "search", "盛产"]);
expect(softHomophone.exitCode).toBe(0);
const softHResults = JSON.parse(softHomophone.stdout);
expect(softHResults.length).toBe(1);
expect(softHResults[0].id).toBe("corp-prod");
// Soft search by initials (sc -> 生产)
const softInitials = await runCli(["corps", "search", "sc"]);
expect(softInitials.exitCode).toBe(0);
const softIResults = JSON.parse(softInitials.stdout);
expect(softIResults.length).toBe(1);
expect(softIResults[0].id).toBe("corp-prod");
// Soft search with missing letter / typo (zhangsa -> admin-zhangsan)
const softTypo = await runCli(["corps", "search", "zhangsa"]);
expect(softTypo.exitCode).toBe(0);
const softTResults = JSON.parse(softTypo.stdout);
expect(softTResults.length).toBe(1);
expect(softTResults[0].id).toBe("corp-prod");
// Strict mode rejects fuzzy matches
const strictFuzzy = await runCli(["corps", "search", "盛产", "--strict"]);
expect(strictFuzzy.exitCode).toBe(0);
expect(JSON.parse(strictFuzzy.stdout).length).toBe(0);
expect(strictFuzzy.stderr).toContain("智能软搜索");
// Strict mode accepts exact substring
const strictExact = await runCli(["corps", "search", "生产", "--strict"]);
expect(strictExact.exitCode).toBe(0);
expect(JSON.parse(strictExact.stdout).length).toBe(1);
});
});
+8 -33
View File
@@ -21,6 +21,7 @@ import {
saveGlobalConfig,
updateCorpConfig
} from "../../core/config/loader.js";
import { filterCorpsFuzzy } from "../../core/search/fuzzy.js";
import { SealCookieMapSchema, SealHeaderMapSchema } from "../../core/config/types.js";
import type { CorpConfig, CorpEnvironment, SealSource } from "../../core/config/types.js";
import * as api from "../../domains/seal/api.js";
@@ -109,14 +110,14 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
const rawQuery = (
maybeName ?? stringOption(args.options.query) ?? stringOption(args.options.q)
)?.trim();
const query = rawQuery?.toLowerCase();
const isStrict = booleanOption(args.options.strict) === true;
const envFilter = (
stringOption(args.options.env) ?? stringOption(args.options.environment)
)?.trim().toLowerCase();
if ((action === "search" || action === "find") && !query && !envFilter) {
throw usageError("Usage: seal-use corps search <query> [--env <production|test>]");
if ((action === "search" || action === "find") && !rawQuery && !envFilter) {
throw usageError("Usage: seal-use corps search <query> [--env <production|test>] [--strict]");
}
const corpId = stringOption(args.options.corp);
@@ -134,40 +135,14 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
filtered = filtered.filter((item) => (item.environment ?? "production").toLowerCase() === envFilter);
}
if (query) {
filtered = filtered.filter((item) => {
const id = item.id.toLowerCase();
const name = item.name.toLowerCase();
const env = (item.environment ?? "production").toLowerCase();
const sourceType = item.source.type.toLowerCase();
const provider = String((item.source as any).provider ?? item.source.type).toLowerCase();
const staffId = String(item.source.staffId ?? "").toLowerCase();
const domain = String((item.source as any).domain ?? "").toLowerCase();
const sourceCorpId = String((item.source as any).corpId ?? "").toLowerCase();
const tenantSlug = String(item.seal.tenantSlug ?? (item.source as any).tenantSlug ?? "").toLowerCase();
const tenantName = String((item.source as any).tenantName ?? "").toLowerCase();
const testCorpId = String(item.testCorpId ?? "").toLowerCase();
const prodCorpId = String(item.prodCorpId ?? "").toLowerCase();
return (
id.includes(query) ||
name.includes(query) ||
env.includes(query) ||
sourceType.includes(query) ||
provider.includes(query) ||
staffId.includes(query) ||
domain.includes(query) ||
sourceCorpId.includes(query) ||
tenantSlug.includes(query) ||
tenantName.includes(query) ||
testCorpId.includes(query) ||
prodCorpId.includes(query)
);
});
if (rawQuery) {
filtered = filterCorpsFuzzy(filtered, rawQuery, { strict: isStrict });
}
if (allCorps.length === 0) {
console.error("提示:当前未检测到企业配置。你可以使用 seal-use corps add-hose 或 seal-use corps add-seal 添加企业,或运行 seal-use help 查看说明。");
} else if (isStrict && filtered.length === 0 && (action === "search" || action === "find") && rawQuery) {
console.error(`提示:未在严格模式下匹配到企业 "${rawQuery}"。可以尝试省略 --strict 进行智能软搜索。`);
}
printJson(filtered.map((item) => ({