Add compact approval rule listing
This commit is contained in:
1 parent
4353ee65d1
commit
0515a24e8a
5 files changed
+210
-7
No files matched your search
@@ -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:
|
||||
|
||||
@@ -59,6 +59,8 @@ Identity and session:
|
||||
|
||||
Approval rule maintenance:
|
||||
|
||||
- `seal-home rules count [--corp <corpId>]`
|
||||
- `seal-home rules list --summary [--corp <corpId>]`
|
||||
- `seal_approval_rules_list`
|
||||
- `seal_approval_rule_create`
|
||||
- `seal_approval_rule_update`
|
||||
|
||||
+62
-2
@@ -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<ReturnType<typeof createSealClient>>,
|
||||
corp: CorpConfig,
|
||||
params: Record<string, unknown>
|
||||
) {
|
||||
printJson(await runToolResult(name, client, corp, params));
|
||||
}
|
||||
|
||||
async function runToolResult(
|
||||
name: string,
|
||||
client: Awaited<ReturnType<typeof createSealClient>>,
|
||||
corp: CorpConfig,
|
||||
params: Record<string, unknown>
|
||||
) {
|
||||
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 <corpId>]
|
||||
seal-home source config [--corp <corpId>]
|
||||
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 versions [--corp <corpId>]
|
||||
seal-home rules version <versionId|versionNumber|latest> [--corp <corpId>]
|
||||
seal-home rules search <keyword> [--version <versionId|versionNumber|latest>] [--maxResults 20]
|
||||
|
||||
@@ -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<Record<string, unknown>> };
|
||||
|
||||
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: {
|
||||
|
||||
@@ -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<ApprovalRule> = {};
|
||||
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);
|
||||
|
||||
Reference in new issue
Block a user