Add rules clear command

This commit is contained in:
gitea committed 2026-07-08 15:22:04 +08:00
1 parent eeba2a4ea4
commit 2611ce4c85
8 files changed
+234 -1

No files matched your search

+22
View File
@@ -173,6 +173,27 @@ async function main() {
return;
}
if (area === "rules" && action === "clear") {
const confirmed = booleanOption(args.options.yes) === true;
const dryRun = booleanOption(args.options.dryRun) ?? booleanOption(args.options["dry-run"]) ?? !confirmed;
if (!confirmed && !dryRun) {
throw new Error("Refusing to clear rules without --yes. Use --dry-run to preview.");
}
const result = await runToolResult("seal_approval_rules_clear", client, corp, {
dryRun,
recode: booleanOption(args.options.recode)
});
if (!confirmed && dryRun && result && typeof result === "object") {
printJson({
...result,
nextCommand: `seal-home rules clear --corp ${corp.id} --yes`
}, outputOptions(args.options));
return;
}
printJson(result, outputOptions(args.options));
return;
}
if (area === "rules" && action === "versions") {
await runTool("seal_approval_rule_versions_list", client, corp, {}, outputOptions(args.options));
return;
@@ -1130,6 +1151,7 @@ Usage:
seal-home tool <toolName> [--corp <corpId>] [--json '{"key":"value"}']
seal-home rules count [--corp <corpId>]
seal-home rules list [--summary] [--count] [--limit 20] [--offset 0] [--corp <corpId>]
seal-home rules clear [--corp <corpId>] [--dry-run|--yes]
seal-home rules versions [--corp <corpId>]
seal-home rules version <versionId|versionNumber|latest> [--corp <corpId>]
seal-home rules get --version <number|latest> --code '#0038'
+10
View File
@@ -59,6 +59,16 @@ export async function deleteApprovalRule(client: KyInstance, ruleId: string): Pr
await client.delete(`api/v1/rules/${ruleId}`);
}
export async function recodeApprovalRules(client: KyInstance): Promise<unknown> {
const response = await client.post("api/v1/rules/batch-recode", { throwHttpErrors: false });
const body = await response.json().catch(() => ({}));
return {
ok: response.ok,
status: response.status,
body
};
}
export async function listApprovalRuleVersions(client: KyInstance): Promise<RuleSetVersion[]> {
return unwrap(await client.get("api/v1/rule-set-versions"), z.array(RuleSetVersionSchema));
}
+96
View File
@@ -104,6 +104,52 @@ describe("sealTools", () => {
expect(result.rules[0]).not.toHaveProperty("description");
});
test("approval rules clear can dry run without deleting", async () => {
const tool = sealTools.find((item) => item.name === "seal_approval_rules_clear");
expect(tool).toBeDefined();
const client = fakeClearRulesClient();
const handler = tool!.handler as ToolHandler;
const result = await handler(client, { dryRun: true }, fakeContext()) as {
dryRun: boolean;
beforeCount: number;
deleted: number;
afterCount: number;
};
expect(result).toMatchObject({
dryRun: true,
beforeCount: 2,
deleted: 0,
afterCount: 2
});
expect(await countFakeRules(client)).toBe(2);
});
test("approval rules clear deletes all draft rules", async () => {
const tool = sealTools.find((item) => item.name === "seal_approval_rules_clear");
expect(tool).toBeDefined();
const client = fakeClearRulesClient();
const handler = tool!.handler as ToolHandler;
const result = await handler(client, {}, fakeContext()) as {
dryRun: boolean;
beforeCount: number;
deleted: number;
afterCount: number;
failed: unknown[];
};
expect(result).toMatchObject({
dryRun: false,
beforeCount: 2,
deleted: 2,
afterCount: 0,
failed: []
});
expect(await countFakeRules(client)).toBe(0);
});
test("includes approval run Langfuse bridge tools", () => {
expect(sealTools.find((item) => item.name === "seal_approval_runs_search")).toBeDefined();
expect(sealTools.find((item) => item.name === "seal_approval_runs_summary")).toBeDefined();
@@ -712,6 +758,56 @@ function fakeRulesClient(): KyInstance {
} as unknown as KyInstance;
}
function fakeClearRulesClient(): KyInstance {
const rules = [
{
id: "rule-1",
tenantId: "tenant-1",
description: "Must include full invoice detail",
scope: "travel",
strictness: "MUST_FOLLOW",
status: "active"
},
{
id: "rule-2",
tenantId: "tenant-1",
description: "Prefer matching attendee names",
scope: "meal",
strictness: "SHOULD_FOLLOW",
status: "draft"
}
];
return {
get: () => ({
json: async () => ({
data: {
hasPendingDeletes: false,
rules: [...rules]
}
})
}),
delete: (url: string) => {
const ruleId = url.split("/").pop();
const index = rules.findIndex((rule) => rule.id === ruleId);
if (index >= 0) rules.splice(index, 1);
return Promise.resolve({});
},
post: () => Promise.resolve({
ok: true,
status: 200,
json: async () => ({ activeRules: rules.length, pendingDeleteRules: 0, nextCode: 1 })
})
} as unknown as KyInstance;
}
async function countFakeRules(client: KyInstance): Promise<number> {
const response = await client.get("api/v1/rules").json() as {
data: { rules: unknown[] };
};
return response.data.rules.length;
}
function fakeContext(): { corp: CorpConfig } {
return {
corp: {
+70
View File
@@ -61,6 +61,74 @@ export const sealTools = [
return projectApprovalRules(data, params);
}
},
{
name: "seal_approval_rules_clear",
description: "清空 Seal 当前草稿审批规则;用于测试环境导入规则前重置草稿规则集",
parameters: z.object({
dryRun: z.boolean().optional().describe("只预览将删除的规则数量,不执行删除"),
recode: z.boolean().optional().describe("删除后尝试重新编排规则编号;失败会作为 warning 返回")
}),
handler: async (
client: KyInstance,
params: { dryRun?: boolean; recode?: boolean },
context: ToolContext
) => {
const before = await api.listApprovalRules(client);
if (params.dryRun) {
return {
corpId: context.corp.id,
corpName: context.corp.name,
dryRun: true,
beforeCount: before.rules.length,
deleted: 0,
afterCount: before.rules.length,
hasPendingDeletes: before.hasPendingDeletes
};
}
const failed: Array<{ ruleId: string; error: string }> = [];
let deleted = 0;
for (const rule of before.rules) {
try {
await api.deleteApprovalRule(client, rule.id);
deleted += 1;
} catch (error) {
failed.push({
ruleId: rule.id,
error: error instanceof Error ? error.message : String(error)
});
}
}
const warnings: string[] = [];
let recode: unknown;
if (deleted > 0 && params.recode !== false) {
recode = await api.recodeApprovalRules(client);
if (
recode &&
typeof recode === "object" &&
"ok" in recode &&
recode.ok !== true
) {
warnings.push("batch-recode did not complete; current rules were still deleted");
}
}
const after = await api.listApprovalRules(client);
return {
corpId: context.corp.id,
corpName: context.corp.name,
dryRun: false,
beforeCount: before.rules.length,
deleted,
failed,
afterCount: after.rules.length,
hasPendingDeletes: after.hasPendingDeletes,
warnings,
recode
};
}
},
{
name: "seal_approval_rule_create",
description: "新建 Seal 草稿审批规则",
@@ -764,6 +832,7 @@ const mcpDirectToolNames = [
const sealActionNames = [
"seal_session_get",
"seal_approval_rules_list",
"seal_approval_rules_clear",
"seal_approval_rule_create",
"seal_approval_rule_update",
"seal_approval_rule_delete",
@@ -792,6 +861,7 @@ const sealActionNames = [
const sealActionAliases: Record<string, (typeof sealActionNames)[number]> = {
"session.get": "seal_session_get",
"rule.list": "seal_approval_rules_list",
"rule.clear": "seal_approval_rules_clear",
"rule.create": "seal_approval_rule_create",
"rule.update": "seal_approval_rule_update",
"rule.delete": "seal_approval_rule_delete",