diff --git a/README.md b/README.md index 2d90b9a..7e97f55 100644 --- a/README.md +++ b/README.md @@ -200,6 +200,8 @@ These tools remain available to the CLI: For rule list commands, prefer `seal-home rules count` or `seal-home rules list --summary` before requesting full rule descriptions. `seal-home rules list` protects large outputs and asks for `--summary`, `--count`, or explicit `--full` when the response would be too large. +For test tenants that need a clean slate before importing another rule set, use `seal-home rules clear --corp --dry-run` first, then `seal-home rules clear --corp --yes` to delete the current draft rules and verify the remaining count. + For approval run detail commands, prefer `seal-home approval-runs pick --sn <单号> --latest`, `seal-home approval-runs get --summary`, `seal-home approval-runs document-summary `, `seal-home approval-runs cited-rules `, `seal-home approval-runs attachments --summary`, or `seal-home approval-runs result --summary` before requesting the full record. CLI JSON output is protected by a 200KB default limit; use `--output-file` for large payloads or `--full` only when the full response is intentional. Hose provisional auth links are available with `seal-home auth hose-link --corp --expire 7200`, which returns the unredacted `getProvisionalAuth` URL. ## Approval Run To Langfuse Bridge diff --git a/skills/seal-home/SKILL.md b/skills/seal-home/SKILL.md index d85e865..092bfe1 100644 --- a/skills/seal-home/SKILL.md +++ b/skills/seal-home/SKILL.md @@ -20,7 +20,7 @@ seal-home tools list - Prefer compact commands first: `pick`, `--summary`, `--fields`, `attachments --summary`, `rules count`, and `rules list --summary`. - Parse JSON stdout. Do not scrape human text. - Do not hand-edit enterprise configs for normal setup. Use `seal-home corps ...`. -- Before write operations on rules, documents, style preferences, or publishing, fetch current state and confirm the intended mutation with the user. +- Before write operations on rules, documents, style preferences, or publishing, fetch current state and confirm the intended mutation with the user. For repeat test-environment rule resets, prefer `rules clear --dry-run` followed by `rules clear --yes`. - For date questions, use explicit dates and `--timezone Asia/Shanghai`. - If output may be large, use `--summary`, `--fields`, `--count`, or `--output-file`; use `--full` only when the user really needs full payloads. @@ -37,6 +37,7 @@ seal-home tools list - Attachments/invoices without full raw document: use `approval-runs attachments --summary`. - Customer disputes OCR, receipt, invoice, image, or attachment findings: read `references/approval-attachment-trace.md`. - Rules count/list/version lookup: use `rules count`, `rules list --summary`, or `rules get`; read `references/rule-lightweight.md`. +- Clear draft rules before importing another rule set: use `seal-home rules clear --corp --dry-run`, then `seal-home rules clear --corp --yes`; read `references/rule-lightweight.md`. - Rule/document/style maintenance: use fine-grained `seal-home tool --json ...`. - Approval knowledge search: use `seal_approval_search` with `snippetOnly`, `maxChars`, and `fields` when possible; read `references/rule-lightweight.md`. - Daily run summary: `seal-home approval-runs summary --date YYYY-MM-DD --timezone Asia/Shanghai --limit 100`. @@ -56,6 +57,8 @@ seal-home approval-runs attachments --summary seal-home approval-runs result --summary seal-home rules count --corp seal-home rules list --corp --summary +seal-home rules clear --corp --dry-run +seal-home rules clear --corp --yes seal-home rules get --version 16 --code '#0038' seal-home rules get --record-id --runtime-id rule-205 seal-home tool seal_approval_search --json '{"keywords":["差旅","发票"],"areas":["rules"],"snippetOnly":true,"maxChars":500,"fields":["id","title","snippet"]}' diff --git a/skills/seal-home/references/cli.md b/skills/seal-home/references/cli.md index b808f0c..9dafc4f 100644 --- a/skills/seal-home/references/cli.md +++ b/skills/seal-home/references/cli.md @@ -65,7 +65,9 @@ Approval rule maintenance: - `seal-home rules count [--corp ]` - `seal-home rules list --summary [--corp ]` +- `seal-home rules clear [--corp ] [--dry-run|--yes]` - `seal_approval_rules_list` +- `seal_approval_rules_clear` - `seal_approval_rule_create` - `seal_approval_rule_update` - `seal_approval_rule_delete` diff --git a/skills/seal-home/references/rule-lightweight.md b/skills/seal-home/references/rule-lightweight.md index 72b9aa8..7be030e 100644 --- a/skills/seal-home/references/rule-lightweight.md +++ b/skills/seal-home/references/rule-lightweight.md @@ -39,6 +39,34 @@ Tool-layer equivalent: seal-home tool seal_approval_rules_list --json '{"fields":["id","scope","strictness","status","updatedAt"]}' ``` +## Clear Draft Rules Before Import + +Use this for test tenants when the next step is importing another tenant's rule set. Always preview first unless the user has already confirmed the exact tenant. + +```bash +seal-home rules clear --corp --dry-run +seal-home rules clear --corp --yes +``` + +Expected shape: + +```json +{ + "corpId": "xiezihao", + "dryRun": false, + "beforeCount": 49, + "deleted": 49, + "afterCount": 0 +} +``` + +Tool-layer equivalent: + +```bash +seal-home tool seal_approval_rules_clear --json '{"dryRun":true}' +seal-home tool seal_approval_rules_clear --json '{"dryRun":false}' +``` + ## Get One Historical Rule Use this when a run used a historical rule version and current rules may differ. diff --git a/src/cli.ts b/src/cli.ts index a46e1e7..cbd27f5 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -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 [--corp ] [--json '{"key":"value"}'] seal-home rules count [--corp ] seal-home rules list [--summary] [--count] [--limit 20] [--offset 0] [--corp ] + seal-home rules clear [--corp ] [--dry-run|--yes] seal-home rules versions [--corp ] seal-home rules version [--corp ] seal-home rules get --version --code '#0038' diff --git a/src/domains/seal/api.ts b/src/domains/seal/api.ts index 7f47675..526c842 100644 --- a/src/domains/seal/api.ts +++ b/src/domains/seal/api.ts @@ -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 { + 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 { return unwrap(await client.get("api/v1/rule-set-versions"), z.array(RuleSetVersionSchema)); } diff --git a/src/domains/seal/tools.test.ts b/src/domains/seal/tools.test.ts index a7b5dd7..584ba54 100644 --- a/src/domains/seal/tools.test.ts +++ b/src/domains/seal/tools.test.ts @@ -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 { + const response = await client.get("api/v1/rules").json() as { + data: { rules: unknown[] }; + }; + return response.data.rules.length; +} + function fakeContext(): { corp: CorpConfig } { return { corp: { diff --git a/src/domains/seal/tools.ts b/src/domains/seal/tools.ts index 10ca01d..630935f 100644 --- a/src/domains/seal/tools.ts +++ b/src/domains/seal/tools.ts @@ -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 = { "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",