feat: 增加 SealAI 官方规则 CSV 导出
This commit is contained in:
1 parent
382fc1a7ae
commit
f454d62be1
14 files changed
+143
-9
No files matched your search
@@ -327,6 +327,19 @@ export async function handleRulesCommand(
|
||||
if (action === "pull") {
|
||||
const version = resolveVersionOption(args, maybeName);
|
||||
const out = stringOption(args.options.out) ?? stringOption(args.options.file) ?? stringOption(args.options.f);
|
||||
if (booleanOption(args.options.official) === true) {
|
||||
if (version) {
|
||||
throw usageError("rules pull --official uses SealAI's official current-rules CSV export and cannot be combined with --version.");
|
||||
}
|
||||
await runCapability(
|
||||
"sealApprovalRulesExportCsv",
|
||||
client!,
|
||||
corp,
|
||||
{ out },
|
||||
outputOptions(args.options)
|
||||
);
|
||||
return;
|
||||
}
|
||||
await runCapability(
|
||||
"sealApprovalRulesPull",
|
||||
client!,
|
||||
|
||||
+7
-2
@@ -340,8 +340,13 @@ export const HELP_REGISTRY: AreaHelp[] = [
|
||||
},
|
||||
{
|
||||
name: "pull",
|
||||
summary: "Export rules to local JSON file",
|
||||
usage: ["seal-use rules pull [--version <number|latest>] [--out <file>] [--corp <corpId>]"]
|
||||
summary: "Pull rules as local JSON, or use --official for the server-generated CSV",
|
||||
usage: ["seal-use rules pull [--official] [--version <number|latest>] [--out <file>] [--corp <corpId>]"],
|
||||
options: [
|
||||
{ flag: "--official", description: "Call SealAI's official CSV export endpoint and save its returned CSV unchanged; cannot be combined with --version" },
|
||||
{ flag: "--version <number|latest>", description: "Pull a published version as JSON; unavailable with --official" },
|
||||
{ flag: "--out <file>", description: "Output path; defaults to ./rules.local.json for JSON or Downloads for official CSV" }
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "push",
|
||||
|
||||
@@ -27,6 +27,10 @@ describe("parseArgs flag handling", () => {
|
||||
expect(parseArgs(["corps", "list", "--all"]).options.all).toBe(true);
|
||||
});
|
||||
|
||||
test("rules pull official is parsed as a boolean flag", () => {
|
||||
expect(parseArgs(["rules", "pull", "--official"]).options.official).toBe(true);
|
||||
});
|
||||
|
||||
test("value flags consume exactly one token", () => {
|
||||
const parsed = parseArgs(["approval-runs", "list", "--limit", "25", "--status", "completed"]);
|
||||
|
||||
|
||||
+1
-1
@@ -31,7 +31,7 @@ export const BOOLEAN_FLAGS = new Set([
|
||||
"exclude-deleted", "extract", "extract-expenses", "extractExpenses", "fail-on-flips", "flow", "force", "full", "glossary", "has-input", "has-output", "help",
|
||||
"has-extra-risk", "hasExtraRisk", "hose", "hose-outbound", "hose-sso", "hoseOutbound", "hoseSso", "include-deleted", "include-io-preview", "include-simulation", "include-unchanged",
|
||||
"include-bridge", "includeBridge", "includeSimulation", "includeUnchanged", "local", "no-attachments", "no-flow", "no-glossary",
|
||||
"no-wait", "noWait", "outbound", "overwrite", "plain", "publish", "quiet",
|
||||
"no-wait", "noWait", "official", "outbound", "overwrite", "plain", "publish", "quiet",
|
||||
"r", "raw-json", "rawJson", "recode", "refresh", "refresh-if-missing", "reveal", "rotate-secret", "rotateSecret",
|
||||
"save", "silent", "silent-save", "simulation", "skip-expense-extraction", "skip-extract", "skip-invoice-generation",
|
||||
"skipExpenseExtraction", "skipExtract", "skipInvoiceGeneration", "snippet-only", "snippetOnly", "sso", "strict",
|
||||
|
||||
@@ -25,8 +25,9 @@ import file21 from "../../skills/seal-use/references/langfuse-trace.md" with { t
|
||||
import file22 from "../../skills/seal-use/references/report-templates.md" with { type: "text" };
|
||||
import file23 from "../../skills/seal-use/references/rule-lightweight.md" with { type: "text" };
|
||||
import file24 from "../../skills/seal-use/references/rule-lint.md" with { type: "text" };
|
||||
import file25 from "../../skills/seal-use/references/staging-simulation-workflow.md" with { type: "text" };
|
||||
import file26 from "../../skills/seal-use/references/webhook-resource-pack.md" with { type: "text" };
|
||||
import file25 from "../../skills/seal-use/references/sealai-browser-rum.md" with { type: "text" };
|
||||
import file26 from "../../skills/seal-use/references/staging-simulation-workflow.md" with { type: "text" };
|
||||
import file27 from "../../skills/seal-use/references/webhook-resource-pack.md" with { type: "text" };
|
||||
|
||||
export const EMBEDDED_SKILL_TEXT_FILES: Record<string, string> = {
|
||||
"SKILL.md": file0,
|
||||
@@ -48,8 +49,9 @@ export const EMBEDDED_SKILL_TEXT_FILES: Record<string, string> = {
|
||||
"references/report-templates.md": file22,
|
||||
"references/rule-lightweight.md": file23,
|
||||
"references/rule-lint.md": file24,
|
||||
"references/staging-simulation-workflow.md": file25,
|
||||
"references/webhook-resource-pack.md": file26,
|
||||
"references/sealai-browser-rum.md": file25,
|
||||
"references/staging-simulation-workflow.md": file26,
|
||||
"references/webhook-resource-pack.md": file27,
|
||||
};
|
||||
|
||||
export const EMBEDDED_SKILL_ASSET_PATHS: Record<string, string> = {
|
||||
@@ -87,6 +89,7 @@ export const EMBEDDED_SKILL_ALL_PATHS = [
|
||||
"references/report-templates.md",
|
||||
"references/rule-lightweight.md",
|
||||
"references/rule-lint.md",
|
||||
"references/sealai-browser-rum.md",
|
||||
"references/staging-simulation-workflow.md",
|
||||
"references/webhook-resource-pack.md",
|
||||
];
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
getInsightRisksByTag,
|
||||
getInsightPerformance,
|
||||
getApprovalRunRawJson,
|
||||
exportApprovalRulesCsv,
|
||||
analyzeInsightRecord,
|
||||
listExpenses,
|
||||
listInvoices,
|
||||
@@ -41,6 +42,21 @@ describe("native approval JSON", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("official approval rules CSV export", () => {
|
||||
test("reads the server-generated CSV from the data envelope", async () => {
|
||||
let requestedUrl = "";
|
||||
const client = {
|
||||
get: (url: string) => {
|
||||
requestedUrl = url;
|
||||
return { json: async () => ({ data: { csv: "code,description\r\n001,测试" } }) };
|
||||
}
|
||||
} as unknown as KyInstance;
|
||||
|
||||
await expect(exportApprovalRulesCsv(client)).resolves.toBe("code,description\r\n001,测试");
|
||||
expect(requestedUrl).toBe("api/v1/rules/export/csv");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Seal contact API", () => {
|
||||
test("sends the contact search term as query", async () => {
|
||||
let requestedUrl = "";
|
||||
|
||||
@@ -121,6 +121,12 @@ export async function listApprovalRules(client: KyInstance): Promise<ApprovalRul
|
||||
return unwrap(await client.get("api/v1/rules"), ApprovalRulesDataSchema);
|
||||
}
|
||||
|
||||
export async function exportApprovalRulesCsv(client: KyInstance): Promise<string> {
|
||||
const response = await client.get("api/v1/rules/export/csv");
|
||||
const result = await unwrap(response, z.object({ csv: z.string() }));
|
||||
return result.csv;
|
||||
}
|
||||
|
||||
export async function createApprovalRule(
|
||||
client: KyInstance,
|
||||
params: { description: string; scope: string; strictness: string }
|
||||
|
||||
@@ -60,6 +60,7 @@ export const SEAL_READ_ONLY_CAPABILITIES: ReadonlySet<string> = new Set([
|
||||
"sealApprovalRuleVersionsList",
|
||||
"sealApprovalRulesLint",
|
||||
"sealApprovalRulesList",
|
||||
"sealApprovalRulesExportCsv",
|
||||
"sealApprovalRulesPull",
|
||||
"sealApprovalRunAttachmentsGet",
|
||||
"sealApprovalRunCitedRulesGet",
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { KyInstance } from "ky";
|
||||
import * as api from "../api.js";
|
||||
import { createDocument, deleteDocument, documentCreateSchema, documentDeleteSchema, documentListSchema, documentUpdateSchema, listDocuments, updateDocument } from "../documents/service.js";
|
||||
import { recodeRules, type RecodeOptions } from "../rules/recode.js";
|
||||
import { exportOfficialRulesCsv } from "../rules/export-csv.js";
|
||||
import { executeRulePush } from "../rules/push.js";
|
||||
import { formatBeijingIso } from "../../../core/config/timestamps.js";
|
||||
import { loadGlobalConfig } from "../../../core/config/loader.js";
|
||||
@@ -719,6 +720,18 @@ export const rulesCapabilities = [
|
||||
};
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "sealApprovalRulesExportCsv",
|
||||
description: "Call the official SealAI rules CSV export endpoint and save its CSV payload unchanged",
|
||||
parameters: z.object({
|
||||
out: z.string().optional().describe("Output file path; defaults to the Downloads directory")
|
||||
}),
|
||||
handler: async (
|
||||
client: KyInstance,
|
||||
params: { out?: string },
|
||||
context: CapabilityContext
|
||||
) => exportOfficialRulesCsv(client, context.corp, params.out)
|
||||
},
|
||||
{
|
||||
name: "sealApprovalRulesPush",
|
||||
description: "将本地规则文件增量同步到远端草稿;支持持久化步骤恢复记录、状态核对(--status)与直接发布正式版本",
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { homedir } from "node:os";
|
||||
import { join, resolve } from "node:path";
|
||||
import type { KyInstance } from "ky";
|
||||
import { formatBeijingIso } from "../../../core/config/timestamps.js";
|
||||
import type { CorpConfig } from "../../../core/config/types.js";
|
||||
import { writeSecureFile } from "../../../core/secure-file.js";
|
||||
import * as api from "../api.js";
|
||||
import { sanitizeFileName } from "../approval-runs/attachments.js";
|
||||
|
||||
export interface OfficialRulesCsvExportResult {
|
||||
filePath: string;
|
||||
corpName: string;
|
||||
byteLength: number;
|
||||
exportedAt: string;
|
||||
source: "sealai-official-csv-endpoint";
|
||||
}
|
||||
|
||||
export async function exportOfficialRulesCsv(
|
||||
client: KyInstance,
|
||||
corp: CorpConfig,
|
||||
outputFile?: string
|
||||
): Promise<OfficialRulesCsvExportResult> {
|
||||
const exportedAt = formatBeijingIso();
|
||||
const stamp = exportedAt.slice(0, 19).replace(/[-:T]/g, "");
|
||||
const safeCorpName = sanitizeFileName(corp.name || corp.id);
|
||||
const filePath = resolve(
|
||||
outputFile ?? join(homedir(), "Downloads", `${safeCorpName}_审核规则官方导出_${stamp}.csv`)
|
||||
);
|
||||
const csv = await api.exportApprovalRulesCsv(client);
|
||||
|
||||
// The API response is a JSON envelope containing the server-generated CSV string.
|
||||
// Persist that string as UTF-8 without parsing, reordering, normalizing, or rewriting CSV rows.
|
||||
writeSecureFile(filePath, csv);
|
||||
|
||||
return {
|
||||
filePath,
|
||||
corpName: corp.name || corp.id,
|
||||
byteLength: Buffer.byteLength(csv, "utf8"),
|
||||
exportedAt,
|
||||
source: "sealai-official-csv-endpoint"
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user