diff --git a/README.md b/README.md index 65d22e3..a6c4f8c 100644 --- a/README.md +++ b/README.md @@ -183,7 +183,7 @@ These tools remain available to the CLI: - `seal_source_config`: resolve current Seal enterprise config from the configured source. - `seal_approval_runs_search`: search approval run history and optional bridge rows. - `seal_session_get`: current Seal Bearer session and expiration. -- `seal_approval_rules_list`: current draft approval rules. +- `seal_approval_rules_list`: current draft approval rules. Use `{"countOnly":true}` or `{"fields":["id"],"limit":0}` to return only `{"count":...}`; use `fields` for lightweight projections when rule descriptions are not needed. - `seal_approval_rule_create`, `seal_approval_rule_update`, `seal_approval_rule_delete`: maintain approval rules. - `seal_approval_rule_versions_list`, `seal_approval_rule_version_publish`: read and publish rule versions. - `seal_approval_documents_list`, `seal_approval_document_get`, `seal_approval_document_create`, `seal_approval_document_update`: maintain approval documents. @@ -193,6 +193,8 @@ These tools remain available to the CLI: - `seal_simulation_batch_records_get`: read records from one simulation batch via `api/v1/simulation/batch/{batchId}/records`. - `seal_approval_run_langfuse_bridge_get`: resolve approval run records to Langfuse lookup hints. It prefers `sourceExtendData._langfuseTraceId`; if that is missing, it returns `hosecloud-{sourceDocumentSN}` as the session fallback. +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. + ## Approval Run To Langfuse Bridge Seal approval runs and simulation batch records are treated as two views over the same run data: diff --git a/skills/seal-home/references/cli.md b/skills/seal-home/references/cli.md index c0b1a92..d224d45 100644 --- a/skills/seal-home/references/cli.md +++ b/skills/seal-home/references/cli.md @@ -59,6 +59,8 @@ Identity and session: Approval rule maintenance: +- `seal-home rules count [--corp ]` +- `seal-home rules list --summary [--corp ]` - `seal_approval_rules_list` - `seal_approval_rule_create` - `seal_approval_rule_update` diff --git a/src/cli.ts b/src/cli.ts index 43cc072..13acb9a 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -19,6 +19,9 @@ const STATE_DIR = join(homedir(), ".config", "seal-home"); const SERVICE_PID_FILE = join(STATE_DIR, "service.pid"); const SERVICE_LOG_FILE = join(STATE_DIR, "service.log"); const CURRENT_CORP_FILE = join(STATE_DIR, "current-corp"); +const RULE_LIST_FULL_COUNT_LIMIT = 100; +const RULE_LIST_FULL_BYTES_LIMIT = 120_000; +const RULE_SUMMARY_FIELDS = ["id", "status", "strictness", "scope", "createdAt", "updatedAt"]; type ParsedArgs = { command: string[]; @@ -129,6 +132,34 @@ async function main() { return; } + if (area === "rules" && action === "count") { + await runTool("seal_approval_rules_list", client, corp, { countOnly: true }); + return; + } + + if (area === "rules" && action === "list") { + if (booleanOption(args.options.count)) { + await runTool("seal_approval_rules_list", client, corp, { countOnly: true }); + return; + } + + const limit = numberOption(args.options.limit); + const offset = numberOption(args.options.offset); + if (booleanOption(args.options.summary)) { + await runTool("seal_approval_rules_list", client, corp, { + fields: RULE_SUMMARY_FIELDS, + limit, + offset + }); + return; + } + + const result = await runToolResult("seal_approval_rules_list", client, corp, { limit, offset }); + protectLargeRuleListOutput(result, booleanOption(args.options.full) === true); + printJson(result); + return; + } + if (area === "rules" && action === "versions") { await runTool("seal_approval_rule_versions_list", client, corp, {}); return; @@ -387,6 +418,15 @@ async function runTool( client: Awaited>, corp: CorpConfig, params: Record +) { + printJson(await runToolResult(name, client, corp, params)); +} + +async function runToolResult( + name: string, + client: Awaited>, + corp: CorpConfig, + params: Record ) { const tool = sealTools.find((item) => item.name === name); if (!tool) throw new Error(`Unknown Seal tool: ${name}`); @@ -395,8 +435,26 @@ async function runTool( Object.entries(params).filter(([, value]) => value !== undefined) ); const handler = tool.handler as SealTool["handler"]; - const result = await handler(client, compactParams as never, { corp }); - printJson(result); + return handler(client, compactParams as never, { corp }); +} + +function protectLargeRuleListOutput(result: unknown, allowFull: boolean) { + if (allowFull) return; + + const data = result && typeof result === "object" ? result as { + count?: unknown; + rules?: unknown; + } : {}; + const count = typeof data.count === "number" ? data.count : undefined; + const rulesCount = Array.isArray(data.rules) ? data.rules.length : undefined; + const bodyBytes = Buffer.byteLength(JSON.stringify(result), "utf-8"); + const effectiveCount = count ?? rulesCount ?? 0; + + if (effectiveCount > RULE_LIST_FULL_COUNT_LIMIT || bodyBytes > RULE_LIST_FULL_BYTES_LIMIT) { + throw new Error( + `Rule list is large (${effectiveCount} rules, ${bodyBytes} bytes). Use "seal-home rules list --summary" or "seal-home rules count"; pass --full only when you really need full descriptions.` + ); + } } function selectCorp(corpId: string): CorpConfig { @@ -958,6 +1016,8 @@ Usage: seal-home auth diagnose [--corp ] seal-home source config [--corp ] 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 versions [--corp ] seal-home rules version [--corp ] seal-home rules search [--version ] [--maxResults 20] diff --git a/src/domains/seal/tools.test.ts b/src/domains/seal/tools.test.ts index d55605b..a28acbe 100644 --- a/src/domains/seal/tools.test.ts +++ b/src/domains/seal/tools.test.ts @@ -61,6 +61,49 @@ describe("sealTools", () => { expect(tool).toBeDefined(); }); + test("approval rules list can return count only", async () => { + const tool = sealTools.find((item) => item.name === "seal_approval_rules_list"); + expect(tool).toBeDefined(); + + const handler = tool!.handler as ToolHandler; + const result = await handler(fakeRulesClient(), { countOnly: true }, fakeContext()); + + expect(result).toEqual({ count: 2 }); + }); + + test("approval rules list treats limit zero as count only", async () => { + const tool = sealTools.find((item) => item.name === "seal_approval_rules_list"); + expect(tool).toBeDefined(); + + const handler = tool!.handler as ToolHandler; + const result = await handler(fakeRulesClient(), { fields: ["id"], limit: 0 }, fakeContext()); + + expect(result).toEqual({ count: 2 }); + }); + + test("approval rules list can project lightweight fields", async () => { + const tool = sealTools.find((item) => item.name === "seal_approval_rules_list"); + expect(tool).toBeDefined(); + + const handler = tool!.handler as ToolHandler; + const result = await handler( + fakeRulesClient(), + { fields: ["id", "status", "strictness", "scope"], limit: 1 }, + fakeContext() + ) as { count: number; rules: Array> }; + + expect(result.count).toBe(2); + expect(result.rules).toEqual([ + { + id: "rule-1", + status: "active", + strictness: "MUST_FOLLOW", + scope: "travel" + } + ]); + expect(result.rules[0]).not.toHaveProperty("description"); + }); + 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(); @@ -385,6 +428,40 @@ function fakeApprovalRunsClient(requests: string[] = []): KyInstance { } as unknown as KyInstance; } +function fakeRulesClient(): KyInstance { + return { + get: () => ({ + json: async () => ({ + data: { + hasPendingDeletes: false, + rules: [ + { + id: "rule-1", + tenantId: "tenant-1", + description: "Must include full invoice detail", + scope: "travel", + strictness: "MUST_FOLLOW", + status: "active", + createdAt: 1780898164536, + updatedAt: 1780898164536 + }, + { + id: "rule-2", + tenantId: "tenant-1", + description: "Prefer matching attendee names", + scope: "meal", + strictness: "SHOULD_FOLLOW", + status: "draft", + createdAt: 1780898164537, + updatedAt: 1780898164537 + } + ] + } + }) + }) + } as unknown as KyInstance; +} + function fakeContext(): { corp: CorpConfig } { return { corp: { diff --git a/src/domains/seal/tools.ts b/src/domains/seal/tools.ts index 67ac648..72ee033 100644 --- a/src/domains/seal/tools.ts +++ b/src/domains/seal/tools.ts @@ -4,7 +4,7 @@ import * as api from "./api.js"; import { getHoseEnterpriseUrl } from "../../core/auth/hose.js"; import { CorpConfig } from "../../core/config/types.js"; import { searchApprovalContent } from "./search.js"; -import type { ApprovalRun, RuleSetVersion } from "./types.js"; +import type { ApprovalRule, ApprovalRun, RuleSetVersion } from "./types.js"; type ToolContext = { corp: CorpConfig; @@ -32,9 +32,34 @@ export const sealTools = [ }, { name: "seal_approval_rules_list", - description: "列出 Seal 当前草稿审批规则", - parameters: z.object({}), - handler: async (client: KyInstance) => api.listApprovalRules(client) + description: "列出 Seal 当前草稿审批规则;支持 countOnly、fields、limit 以避免返回规则正文", + parameters: z.object({ + countOnly: z.boolean().optional().describe("只返回规则数量,不返回规则列表"), + fields: z.array(z.enum([ + "id", + "tenantId", + "status", + "strictness", + "scope", + "createdAt", + "updatedAt", + "description" + ])).optional().describe("只返回指定字段;不传则返回完整规则"), + limit: z.number().int().nonnegative().optional().describe("最多返回多少条规则;0 可配合 fields 或 countOnly 只取数量"), + offset: z.number().int().nonnegative().optional().describe("跳过前多少条规则") + }), + handler: async ( + client: KyInstance, + params: { + countOnly?: boolean; + fields?: RuleField[]; + limit?: number; + offset?: number; + } + ) => { + const data = await api.listApprovalRules(client); + return projectApprovalRules(data, params); + } }, { name: "seal_approval_rule_create", @@ -785,6 +810,43 @@ type ApprovalRunsSummaryOptions = { total?: number; }; +type RuleField = keyof ApprovalRule; + +function projectApprovalRules( + data: { rules: ApprovalRule[]; hasPendingDeletes: boolean }, + params: { + countOnly?: boolean; + fields?: RuleField[]; + limit?: number; + offset?: number; + } +) { + const count = data.rules.length; + if (params.countOnly || params.limit === 0) { + return { count }; + } + + const offset = params.offset ?? 0; + const rules = data.rules.slice(offset, params.limit === undefined ? undefined : offset + params.limit); + const fields = params.fields; + + return { + count, + hasPendingDeletes: data.hasPendingDeletes, + rules: fields ? rules.map((rule) => pickRuleFields(rule, fields)) : rules + }; +} + +function pickRuleFields(rule: ApprovalRule, fields: readonly RuleField[]) { + const output: Partial = {}; + for (const field of fields) { + if (rule[field] !== undefined) { + output[field] = rule[field] as never; + } + } + return output; +} + function summarizeApprovalRun(record: ApprovalRun): ApprovalRunSummary { const sourceDocumentSN = record.sourceDocumentSN; const humanResult = extractHumanResult(record);