Add approval run rule version workflow
This commit is contained in:
1 parent
db08b9d898
commit
6825c4d421
4 files changed
+419
-30
No files matched your search
+131
-5
@@ -16,6 +16,7 @@ const APP_ROOT = dirname(dirname(fileURLToPath(import.meta.url)));
|
||||
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");
|
||||
|
||||
type ParsedArgs = {
|
||||
command: string[];
|
||||
@@ -65,7 +66,7 @@ async function main() {
|
||||
if (area === "corps" && action === "list") {
|
||||
const corpId = stringOption(args.options.corp);
|
||||
const corps = loadCorpConfigs();
|
||||
const currentCorpId = corpId ?? corps[0]?.id;
|
||||
const currentCorpId = corpId ?? readCurrentCorpId() ?? corps[0]?.id;
|
||||
printJson(corps.map((item) => ({
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
@@ -75,6 +76,30 @@ async function main() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (area === "corps" && action === "current") {
|
||||
const corp = selectCorp("");
|
||||
printJson({
|
||||
id: corp.id,
|
||||
name: corp.name,
|
||||
sourceType: corp.source.type
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (area === "corps" && action === "switch") {
|
||||
const corpId = maybeName;
|
||||
if (!corpId) throw new Error("Usage: seal-home corps switch <corpId>");
|
||||
const corp = selectCorp(corpId);
|
||||
ensureStateDir();
|
||||
writeFileSync(CURRENT_CORP_FILE, `${corp.id}\n`, { mode: 0o600 });
|
||||
printJson({
|
||||
current: true,
|
||||
id: corp.id,
|
||||
name: corp.name
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (area === "corps" && action === "add-hose") {
|
||||
addHoseCorp(jsonObject(args.json));
|
||||
return;
|
||||
@@ -89,6 +114,40 @@ async function main() {
|
||||
|
||||
const client = await createSealClient(corp);
|
||||
|
||||
if (area === "context") {
|
||||
await runTool("seal_approval_context_get", client, corp, {
|
||||
documentLimit: numberOption(args.options.documentLimit),
|
||||
...ruleVersionContextOptions(stringOption(args.options.ruleVersion))
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (area === "rules" && action === "versions") {
|
||||
await runTool("seal_approval_rule_versions_list", client, corp, {});
|
||||
return;
|
||||
}
|
||||
|
||||
if (area === "rules" && action === "version") {
|
||||
const version = maybeName ?? stringOption(args.options.version);
|
||||
if (!version) throw new Error("Usage: seal-home rules version <versionId|versionNumber|latest>");
|
||||
await runTool("seal_approval_rule_version_get", client, corp, parseRuleVersionSelector(version));
|
||||
return;
|
||||
}
|
||||
|
||||
if (area === "rules" && action === "search") {
|
||||
const keyword = maybeName ?? stringOption(args.options.query);
|
||||
if (!keyword) throw new Error("Usage: seal-home rules search <keyword> [--version <versionId|versionNumber|latest>]");
|
||||
await runTool("seal_approval_search", client, corp, {
|
||||
keywords: [keyword],
|
||||
areas: ["rules"],
|
||||
maxResults: numberOption(args.options.maxResults),
|
||||
contextLines: numberOption(args.options.contextLines),
|
||||
refresh: booleanOption(args.options.refresh),
|
||||
...ruleVersionSearchOptions(stringOption(args.options.version))
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (area === "tool") {
|
||||
if (!action) throw new Error("Usage: seal-home tool <toolName> [--json '{...}']");
|
||||
await runTool(action, client, corp, jsonObject(args.json));
|
||||
@@ -142,6 +201,17 @@ async function main() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (area === "approval-runs" && action === "pick") {
|
||||
const query = maybeName ?? stringOption(args.options.query);
|
||||
if (!query) throw new Error("Usage: seal-home approval-runs pick <documentSN-or-query>");
|
||||
await runTool("seal_approval_run_pick", client, corp, {
|
||||
query,
|
||||
limit: numberOption(args.options.limit),
|
||||
includeBridge: booleanOption(args.options.includeBridge)
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (area === "approval-runs" && action === "get") {
|
||||
const recordId = args.command[2];
|
||||
if (!recordId) throw new Error("Missing approval run recordId");
|
||||
@@ -315,14 +385,15 @@ async function runTool(
|
||||
|
||||
function selectCorp(corpId: string): CorpConfig {
|
||||
const corps = loadCorpConfigs();
|
||||
const corp = corpId
|
||||
? corps.find((item) => item.id === corpId)
|
||||
const selectedCorpId = corpId || readCurrentCorpId() || "";
|
||||
const corp = selectedCorpId
|
||||
? corps.find((item) => item.id === selectedCorpId)
|
||||
: corps[0];
|
||||
|
||||
if (!corp) {
|
||||
throw new Error(
|
||||
corpId
|
||||
? `No enterprise config found for ${corpId}`
|
||||
selectedCorpId
|
||||
? `No enterprise config found for ${selectedCorpId}`
|
||||
: `No enterprise config found. Add a non-example JSON file under one of: ${getEnterprisesDirCandidates().join(", ")}`
|
||||
);
|
||||
}
|
||||
@@ -330,6 +401,12 @@ function selectCorp(corpId: string): CorpConfig {
|
||||
return corp;
|
||||
}
|
||||
|
||||
function readCurrentCorpId(): string | undefined {
|
||||
if (!existsSync(CURRENT_CORP_FILE)) return undefined;
|
||||
const value = readFileSync(CURRENT_CORP_FILE, "utf-8").trim();
|
||||
return value || undefined;
|
||||
}
|
||||
|
||||
function addHoseCorp(input: Record<string, unknown>) {
|
||||
const id = requiredString(input, "id");
|
||||
const name = requiredString(input, "name");
|
||||
@@ -391,6 +468,48 @@ function safeFileName(value: string): string {
|
||||
return value.replace(/[^a-zA-Z0-9._-]/g, "_");
|
||||
}
|
||||
|
||||
function parseRuleVersionSelector(value: string) {
|
||||
if (value === "latest") return { latest: true };
|
||||
const versionNumber = Number(value);
|
||||
if (Number.isInteger(versionNumber) && versionNumber > 0) {
|
||||
return { versionNumber };
|
||||
}
|
||||
return { versionId: value };
|
||||
}
|
||||
|
||||
function ruleVersionSearchOptions(value?: string) {
|
||||
if (!value) return {};
|
||||
const selector = parseRuleVersionSelector(value);
|
||||
if ("latest" in selector) {
|
||||
return {
|
||||
ruleVersionScope: "version",
|
||||
latestRuleVersion: true
|
||||
};
|
||||
}
|
||||
if ("versionNumber" in selector) {
|
||||
return {
|
||||
ruleVersionScope: "version",
|
||||
ruleVersionNumber: selector.versionNumber
|
||||
};
|
||||
}
|
||||
return {
|
||||
ruleVersionScope: "version",
|
||||
ruleVersionId: selector.versionId
|
||||
};
|
||||
}
|
||||
|
||||
function ruleVersionContextOptions(value?: string) {
|
||||
if (!value) return {};
|
||||
const selector = parseRuleVersionSelector(value);
|
||||
if ("latest" in selector) {
|
||||
return { latestRuleVersion: true };
|
||||
}
|
||||
if ("versionNumber" in selector) {
|
||||
return { ruleVersionNumber: selector.versionNumber };
|
||||
}
|
||||
return { ruleVersionId: selector.versionId };
|
||||
}
|
||||
|
||||
function parseArgs(argv: string[]): ParsedArgs {
|
||||
const command: string[] = [];
|
||||
const options: Record<string, string | boolean> = {};
|
||||
@@ -491,11 +610,18 @@ Usage:
|
||||
seal-home update
|
||||
seal-home tools list
|
||||
seal-home corps list [--corp <corpId>]
|
||||
seal-home corps current
|
||||
seal-home corps switch <corpId>
|
||||
seal-home corps add-hose --json '{"id":"corp-id","name":"企业名称","appKey":"...","appSecurity":"...","corpId":"...","staffId":"..."}'
|
||||
seal-home source config [--corp <corpId>]
|
||||
seal-home tool <toolName> [--corp <corpId>] [--json '{"key":"value"}']
|
||||
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]
|
||||
seal-home context [--ruleVersion <versionId|versionNumber|latest>] [--documentLimit 50]
|
||||
seal-home approval-runs summary [--date YYYY-MM-DD] [--timezone Asia/Shanghai]
|
||||
seal-home approval-runs search [--query text] [--humanResult 驳回] [--manualApprovalStatus TERMINATED] [--startDate ms] [--endDate ms] [--limit 20] [--includeBridge true]
|
||||
seal-home approval-runs pick <documentSN-or-query> [--limit 20]
|
||||
seal-home approval-runs get <recordId>
|
||||
seal-home approval-runs bridge [--sourceDocumentSN B26001887]
|
||||
seal-home simulation batch-records <batchId>
|
||||
|
||||
+46
-12
@@ -29,6 +29,8 @@ export type ApprovalSearchParams = {
|
||||
refresh?: boolean;
|
||||
ruleVersionScope?: RuleVersionScope;
|
||||
ruleVersionId?: string;
|
||||
ruleVersionNumber?: number;
|
||||
latestRuleVersion?: boolean;
|
||||
};
|
||||
|
||||
export type ApprovalSearchResult = {
|
||||
@@ -288,13 +290,15 @@ function preferenceEntries(preferences: ApprovalStylePreferences): SearchEntry[]
|
||||
async function loadSearchEntries(
|
||||
client: KyInstance,
|
||||
corp: CorpConfig,
|
||||
params: Pick<ApprovalSearchParams, "documentLimit" | "refresh" | "ruleVersionScope" | "ruleVersionId">
|
||||
params: Pick<ApprovalSearchParams, "documentLimit" | "refresh" | "ruleVersionScope" | "ruleVersionId" | "ruleVersionNumber" | "latestRuleVersion">
|
||||
): Promise<SearchCacheEntry> {
|
||||
const ruleVersionScope = params.ruleVersionScope ?? "current";
|
||||
const cacheKey = [
|
||||
corp.id,
|
||||
`ruleVersionScope=${ruleVersionScope}`,
|
||||
`ruleVersionId=${params.ruleVersionId ?? ""}`,
|
||||
`ruleVersionNumber=${params.ruleVersionNumber ?? ""}`,
|
||||
`latestRuleVersion=${params.latestRuleVersion ?? false}`,
|
||||
`documentLimit=${params.documentLimit ?? 100}`
|
||||
].join("|");
|
||||
const cached = searchCache.get(cacheKey);
|
||||
@@ -303,7 +307,12 @@ async function loadSearchEntries(
|
||||
}
|
||||
|
||||
const [rules, documents, preferences] = await Promise.all([
|
||||
loadRuleEntries(client, ruleVersionScope, params.ruleVersionId),
|
||||
loadRuleEntries(client, {
|
||||
scope: ruleVersionScope,
|
||||
versionId: params.ruleVersionId,
|
||||
versionNumber: params.ruleVersionNumber,
|
||||
latest: params.latestRuleVersion
|
||||
}),
|
||||
listApprovalDocuments(client, { limit: params.documentLimit ?? 100 }),
|
||||
getApprovalStylePreferences(
|
||||
client,
|
||||
@@ -328,19 +337,20 @@ async function loadSearchEntries(
|
||||
|
||||
async function loadRuleEntries(
|
||||
client: KyInstance,
|
||||
scope: RuleVersionScope,
|
||||
versionId?: string
|
||||
params: {
|
||||
scope: RuleVersionScope;
|
||||
versionId?: string;
|
||||
versionNumber?: number;
|
||||
latest?: boolean;
|
||||
}
|
||||
): Promise<SearchEntry[]> {
|
||||
if (scope === "current") {
|
||||
if (params.scope === "current") {
|
||||
const rules = await listApprovalRules(client);
|
||||
return rules.rules.flatMap(ruleEntries);
|
||||
}
|
||||
|
||||
if (scope === "version") {
|
||||
if (!versionId) {
|
||||
throw new Error("ruleVersionId is required when ruleVersionScope is version");
|
||||
}
|
||||
const version = await getApprovalRuleVersion(client, versionId);
|
||||
if (params.scope === "version") {
|
||||
const version = await resolveRuleVersion(client, params);
|
||||
return ruleVersionEntries(version);
|
||||
}
|
||||
|
||||
@@ -348,6 +358,26 @@ async function loadRuleEntries(
|
||||
return versions.flatMap(ruleVersionEntries);
|
||||
}
|
||||
|
||||
async function resolveRuleVersion(
|
||||
client: KyInstance,
|
||||
params: { versionId?: string; versionNumber?: number; latest?: boolean }
|
||||
) {
|
||||
if (params.versionId) return getApprovalRuleVersion(client, params.versionId);
|
||||
|
||||
const versions = await listApprovalRuleVersions(client);
|
||||
const version = params.versionNumber
|
||||
? versions.find((item) => item.versionNumber === params.versionNumber)
|
||||
: versions[0];
|
||||
|
||||
if (!version) {
|
||||
throw new Error(params.versionNumber
|
||||
? `Rule version not found: ${params.versionNumber}`
|
||||
: "No rule versions found");
|
||||
}
|
||||
|
||||
return version;
|
||||
}
|
||||
|
||||
export async function searchApprovalContent(
|
||||
client: KyInstance,
|
||||
corp: CorpConfig,
|
||||
@@ -369,7 +399,9 @@ export async function searchApprovalContent(
|
||||
documentLimit: params.documentLimit,
|
||||
refresh: params.refresh,
|
||||
ruleVersionScope,
|
||||
ruleVersionId: params.ruleVersionId
|
||||
ruleVersionId: params.ruleVersionId,
|
||||
ruleVersionNumber: params.ruleVersionNumber,
|
||||
latestRuleVersion: params.latestRuleVersion
|
||||
});
|
||||
|
||||
const results = cached.entries
|
||||
@@ -387,7 +419,9 @@ export async function searchApprovalContent(
|
||||
contextLines,
|
||||
refresh: params.refresh ?? false,
|
||||
ruleVersionScope,
|
||||
ruleVersionId: params.ruleVersionId
|
||||
ruleVersionId: params.ruleVersionId,
|
||||
ruleVersionNumber: params.ruleVersionNumber,
|
||||
latestRuleVersion: params.latestRuleVersion ?? false
|
||||
},
|
||||
cache: {
|
||||
fetchedAt: new Date(cached.fetchedAt).toISOString(),
|
||||
|
||||
+239
-13
@@ -3,7 +3,7 @@ import { KyInstance } from "ky";
|
||||
import * as api from "./api.js";
|
||||
import { CorpConfig } from "../../core/config/types.js";
|
||||
import { searchApprovalContent } from "./search.js";
|
||||
import type { ApprovalRun } from "./types.js";
|
||||
import type { ApprovalRun, RuleSetVersion } from "./types.js";
|
||||
|
||||
type ToolContext = {
|
||||
corp: CorpConfig;
|
||||
@@ -78,9 +78,44 @@ export const sealTools = [
|
||||
},
|
||||
{
|
||||
name: "seal_approval_rule_versions_list",
|
||||
description: "列出 Seal 已发布审批规则版本",
|
||||
description: "列出 Seal 已发布审批规则版本摘要,不展开每个版本的规则快照",
|
||||
parameters: z.object({}),
|
||||
handler: async (client: KyInstance) => api.listApprovalRuleVersions(client)
|
||||
handler: async (client: KyInstance) => {
|
||||
const versions = await api.listApprovalRuleVersions(client);
|
||||
return {
|
||||
count: versions.length,
|
||||
latest: versions[0] ? summarizeRuleVersion(versions[0]) : undefined,
|
||||
versions: versions.map(summarizeRuleVersion)
|
||||
};
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "seal_approval_rule_version_get",
|
||||
description: "获取指定 Seal 已发布审批规则版本详情,包含该版本的规则快照",
|
||||
parameters: z.object({
|
||||
versionId: z.string().optional().describe("规则版本 ID"),
|
||||
versionNumber: z.number().int().positive().optional().describe("规则版本号"),
|
||||
latest: z.boolean().optional().describe("为 true 时获取最新版本")
|
||||
}),
|
||||
handler: async (
|
||||
client: KyInstance,
|
||||
params: { versionId?: string; versionNumber?: number; latest?: boolean }
|
||||
) => {
|
||||
if (params.versionId) return api.getApprovalRuleVersion(client, params.versionId);
|
||||
|
||||
const versions = await api.listApprovalRuleVersions(client);
|
||||
const version = params.versionNumber
|
||||
? versions.find((item) => item.versionNumber === params.versionNumber)
|
||||
: versions[0];
|
||||
|
||||
if (!version) {
|
||||
throw new Error(params.versionNumber
|
||||
? `Rule version not found: ${params.versionNumber}`
|
||||
: "No rule versions found");
|
||||
}
|
||||
|
||||
return version;
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "seal_approval_rule_version_publish",
|
||||
@@ -183,7 +218,9 @@ export const sealTools = [
|
||||
maxResults: z.number().int().positive().optional().describe("最多返回命中数,默认 50"),
|
||||
refresh: z.boolean().optional().describe("是否跳过 5 分钟内存缓存并强制刷新"),
|
||||
ruleVersionScope: z.enum(["current", "all", "version"]).optional().describe("规则检索范围,默认 current;all 会检索所有已发布历史版本"),
|
||||
ruleVersionId: z.string().optional().describe("ruleVersionScope=version 时指定版本 ID")
|
||||
ruleVersionId: z.string().optional().describe("ruleVersionScope=version 时指定版本 ID"),
|
||||
ruleVersionNumber: z.number().int().positive().optional().describe("ruleVersionScope=version 时指定版本号"),
|
||||
latestRuleVersion: z.boolean().optional().describe("ruleVersionScope=version 时取最新已发布版本")
|
||||
}),
|
||||
handler: async (
|
||||
client: KyInstance,
|
||||
@@ -198,6 +235,8 @@ export const sealTools = [
|
||||
refresh?: boolean;
|
||||
ruleVersionScope?: "current" | "all" | "version";
|
||||
ruleVersionId?: string;
|
||||
ruleVersionNumber?: number;
|
||||
latestRuleVersion?: boolean;
|
||||
},
|
||||
context: ToolContext
|
||||
) => searchApprovalContent(client, context.corp, params)
|
||||
@@ -206,17 +245,39 @@ export const sealTools = [
|
||||
name: "seal_approval_context_get",
|
||||
description: "聚合获取 Seal 现有审批规则、审批文档和审批风格偏好",
|
||||
parameters: z.object({
|
||||
documentLimit: z.number().int().positive().optional().describe("最多返回的审批文档数量")
|
||||
documentLimit: z.number().int().positive().optional().describe("最多返回的审批文档数量"),
|
||||
ruleVersionId: z.string().optional().describe("指定已发布规则版本 ID;指定后 rules 返回该版本快照"),
|
||||
ruleVersionNumber: z.number().int().positive().optional().describe("指定已发布规则版本号;指定后 rules 返回该版本快照"),
|
||||
latestRuleVersion: z.boolean().optional().describe("为 true 时 rules 返回最新已发布版本快照")
|
||||
}),
|
||||
handler: async (
|
||||
client: KyInstance,
|
||||
params: { documentLimit?: number },
|
||||
params: {
|
||||
documentLimit?: number;
|
||||
ruleVersionId?: string;
|
||||
ruleVersionNumber?: number;
|
||||
latestRuleVersion?: boolean;
|
||||
},
|
||||
context: ToolContext
|
||||
) =>
|
||||
api.getApprovalContext(client, {
|
||||
) => {
|
||||
const currentContext = await api.getApprovalContext(client, {
|
||||
documentLimit: params.documentLimit,
|
||||
stylePreferencesEndpoint: context.corp.seal.endpoints.approvalStylePreferences
|
||||
})
|
||||
});
|
||||
|
||||
if (!params.ruleVersionId && !params.ruleVersionNumber && !params.latestRuleVersion) {
|
||||
return currentContext;
|
||||
}
|
||||
|
||||
const version = await resolveRuleVersion(client, params);
|
||||
return {
|
||||
...currentContext,
|
||||
rules: {
|
||||
version: summarizeRuleVersion(version),
|
||||
rules: version.rules
|
||||
}
|
||||
};
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "seal_approval_style_preferences_get",
|
||||
@@ -387,6 +448,60 @@ export const sealTools = [
|
||||
};
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "seal_approval_run_pick",
|
||||
description: "按合思单号或关键字查询运行记录,返回候选记录及其当时使用的规则版本,便于再取详情或查版本规则",
|
||||
parameters: z.object({
|
||||
query: z.string().describe("合思单号、源单据 ID、运行记录 ID 或 trace 关键字"),
|
||||
limit: z.number().int().positive().max(100).optional(),
|
||||
maxScan: z.number().int().positive().max(5000).optional().describe("最多扫描多少条运行记录用于本地匹配,默认 1000"),
|
||||
includeBridge: z.boolean().optional()
|
||||
}),
|
||||
handler: async (
|
||||
client: KyInstance,
|
||||
params: { query: string; limit?: number; maxScan?: number; includeBridge?: boolean }
|
||||
) => {
|
||||
const picked = await pickApprovalRuns(client, {
|
||||
query: params.query,
|
||||
limit: params.limit ?? 20,
|
||||
maxScan: params.maxScan ?? 1000
|
||||
});
|
||||
const records = await Promise.all(
|
||||
picked.records.map((record) => api.getApprovalRun(client, record.id))
|
||||
);
|
||||
|
||||
return {
|
||||
query: params.query,
|
||||
total: picked.total,
|
||||
scanned: picked.scanned,
|
||||
matched: records.length,
|
||||
records: records.map((record) => {
|
||||
const summary = summarizeApprovalRun(record);
|
||||
return {
|
||||
recordId: summary.id,
|
||||
sourceDocumentSN: summary.sourceDocumentSN,
|
||||
sourceDocumentId: summary.sourceDocumentId,
|
||||
status: summary.status,
|
||||
taskMode: summary.taskMode,
|
||||
finalExecutionMode: summary.finalExecutionMode,
|
||||
createdAt: summary.createdAt,
|
||||
ruleSetVersionNumber: summary.ruleSetVersionNumber,
|
||||
ruleSetPublishedAt: summary.ruleSetPublishedAt,
|
||||
ruleSetPublishedByName: summary.ruleSetPublishedByName,
|
||||
langfuseTraceId: summary.langfuseTraceId,
|
||||
langfuseSessionFallback: summary.langfuseSessionFallback
|
||||
};
|
||||
}),
|
||||
next: {
|
||||
getDetail: "seal-home approval-runs get <recordId>",
|
||||
getRuleVersion: "seal-home rules version <ruleSetVersionNumber>",
|
||||
searchRuleVersion: "seal-home tool seal_approval_search --json '{\"keywords\":[\"关键词\"],\"areas\":[\"rules\"],\"ruleVersionScope\":\"version\",\"ruleVersionNumber\":14}'",
|
||||
getVersionContext: "seal-home tool seal_approval_context_get --json '{\"ruleVersionNumber\":14}'"
|
||||
},
|
||||
...(params.includeBridge ? { bridge: summarizeRunBridge(records) } : {})
|
||||
};
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "seal_simulation_batch_records_get",
|
||||
description: "获取某一次 Seal 模拟批次的运行记录,并整理对应 Langfuse trace/session 桥接字段",
|
||||
@@ -475,6 +590,7 @@ const sealActionNames = [
|
||||
"seal_approval_rule_update",
|
||||
"seal_approval_rule_delete",
|
||||
"seal_approval_rule_versions_list",
|
||||
"seal_approval_rule_version_get",
|
||||
"seal_approval_rule_version_publish",
|
||||
"seal_approval_documents_list",
|
||||
"seal_approval_document_get",
|
||||
@@ -484,6 +600,7 @@ const sealActionNames = [
|
||||
"seal_approval_style_preferences_update",
|
||||
"seal_approval_runs_summary",
|
||||
"seal_approval_run_get",
|
||||
"seal_approval_run_pick",
|
||||
"seal_simulation_batch_records_get",
|
||||
"seal_approval_run_langfuse_bridge_get"
|
||||
] as const;
|
||||
@@ -495,6 +612,7 @@ const sealActionAliases: Record<string, (typeof sealActionNames)[number]> = {
|
||||
"rule.update": "seal_approval_rule_update",
|
||||
"rule.delete": "seal_approval_rule_delete",
|
||||
"rule.versions.list": "seal_approval_rule_versions_list",
|
||||
"rule.version.get": "seal_approval_rule_version_get",
|
||||
"rule.publish": "seal_approval_rule_version_publish",
|
||||
"doc.list": "seal_approval_documents_list",
|
||||
"doc.get": "seal_approval_document_get",
|
||||
@@ -504,6 +622,7 @@ const sealActionAliases: Record<string, (typeof sealActionNames)[number]> = {
|
||||
"style.update": "seal_approval_style_preferences_update",
|
||||
"runs.summary": "seal_approval_runs_summary",
|
||||
"runs.get": "seal_approval_run_get",
|
||||
"runs.pick": "seal_approval_run_pick",
|
||||
"batch.records.get": "seal_simulation_batch_records_get",
|
||||
"langfuse.bridge.get": "seal_approval_run_langfuse_bridge_get"
|
||||
};
|
||||
@@ -608,6 +727,9 @@ type ApprovalRunSummary = {
|
||||
simulationBatchId?: string;
|
||||
langfuseTraceId?: string;
|
||||
langfuseSessionFallback?: string;
|
||||
ruleSetVersionNumber?: number;
|
||||
ruleSetPublishedAt?: number;
|
||||
ruleSetPublishedByName?: string;
|
||||
humanResult?: string;
|
||||
humanResultPath?: string;
|
||||
createdAt?: string | number;
|
||||
@@ -639,6 +761,9 @@ function summarizeApprovalRun(record: ApprovalRun): ApprovalRunSummary {
|
||||
simulationBatchId: record.sourceExtendData?.simulation_batch_id,
|
||||
langfuseTraceId: record.sourceExtendData?._langfuseTraceId,
|
||||
langfuseSessionFallback: sourceDocumentSN ? `hosecloud-${sourceDocumentSN}` : undefined,
|
||||
ruleSetVersionNumber: record.ruleSetVersionNumber,
|
||||
ruleSetPublishedAt: record.ruleSetPublishedAt,
|
||||
ruleSetPublishedByName: record.ruleSetPublishedByName,
|
||||
humanResult: humanResult?.value,
|
||||
humanResultPath: humanResult?.path,
|
||||
createdAt: record.createdAt,
|
||||
@@ -667,6 +792,9 @@ function summarizeApprovalRuns(records: ApprovalRun[], options: ApprovalRunsSumm
|
||||
simulationBatchId: summary.simulationBatchId,
|
||||
langfuseTraceId: summary.langfuseTraceId,
|
||||
langfuseSessionFallback: summary.langfuseSessionFallback,
|
||||
ruleSetVersionNumber: summary.ruleSetVersionNumber,
|
||||
ruleSetPublishedAt: formatLocalDateTime(summary.ruleSetPublishedAt, options.timezone),
|
||||
ruleSetPublishedByName: summary.ruleSetPublishedByName,
|
||||
humanResult: summary.humanResult,
|
||||
humanResultPath: summary.humanResultPath,
|
||||
recordId: summary.id
|
||||
@@ -687,6 +815,9 @@ function summarizeRunBridge(records: ApprovalRun[]) {
|
||||
simulationBatchId: summary.simulationBatchId,
|
||||
langfuseTraceId: summary.langfuseTraceId,
|
||||
langfuseSessionFallback: summary.langfuseSessionFallback,
|
||||
ruleSetVersionNumber: summary.ruleSetVersionNumber,
|
||||
ruleSetPublishedAt: summary.ruleSetPublishedAt,
|
||||
ruleSetPublishedByName: summary.ruleSetPublishedByName,
|
||||
resolution:
|
||||
summary.langfuseTraceId
|
||||
? "trace"
|
||||
@@ -697,19 +828,114 @@ function summarizeRunBridge(records: ApprovalRun[]) {
|
||||
});
|
||||
}
|
||||
|
||||
function summarizeRuleVersion(version: RuleSetVersion) {
|
||||
return {
|
||||
id: version.id,
|
||||
tenantId: version.tenantId,
|
||||
versionNumber: version.versionNumber,
|
||||
versionName: version.versionName,
|
||||
publishedBy: version.publishedBy,
|
||||
publishedByName: version.publishedByName,
|
||||
rulesCount: version.rules.length,
|
||||
publishedAt: version.publishedAt,
|
||||
createdAt: version.createdAt
|
||||
};
|
||||
}
|
||||
|
||||
async function pickApprovalRuns(
|
||||
client: KyInstance,
|
||||
params: { query: string; limit: number; maxScan: number }
|
||||
) {
|
||||
const pageSize = Math.min(100, Math.max(params.limit, 20));
|
||||
const records: ApprovalRun[] = [];
|
||||
let total: number | undefined;
|
||||
let scanned = 0;
|
||||
|
||||
while (scanned < params.maxScan && records.length < params.limit) {
|
||||
const data = await api.listApprovalRuns(client, {
|
||||
offset: scanned,
|
||||
limit: Math.min(pageSize, params.maxScan - scanned)
|
||||
});
|
||||
|
||||
total = data.total;
|
||||
scanned += data.records.length;
|
||||
records.push(...filterRuns(data.records, params.query));
|
||||
|
||||
if (data.records.length === 0 || (total !== undefined && scanned >= total)) break;
|
||||
|
||||
}
|
||||
|
||||
return {
|
||||
total,
|
||||
scanned,
|
||||
records: records.slice(0, params.limit)
|
||||
};
|
||||
}
|
||||
|
||||
async function resolveRuleVersion(
|
||||
client: KyInstance,
|
||||
params: { versionId?: string; versionNumber?: number; latestRuleVersion?: boolean; latest?: boolean }
|
||||
) {
|
||||
if (params.versionId) return api.getApprovalRuleVersion(client, params.versionId);
|
||||
|
||||
const versions = await api.listApprovalRuleVersions(client);
|
||||
const version = params.versionNumber
|
||||
? versions.find((item) => item.versionNumber === params.versionNumber)
|
||||
: versions[0];
|
||||
|
||||
if (!version) {
|
||||
throw new Error(params.versionNumber
|
||||
? `Rule version not found: ${params.versionNumber}`
|
||||
: "No rule versions found");
|
||||
}
|
||||
|
||||
return version;
|
||||
}
|
||||
|
||||
function filterRuns(records: ApprovalRun[], query?: string): ApprovalRun[] {
|
||||
const normalized = query?.trim().toLowerCase();
|
||||
if (!normalized) return records;
|
||||
|
||||
return records.filter((record) =>
|
||||
[
|
||||
return records.filter((record) => {
|
||||
const directValues = [
|
||||
record.id,
|
||||
record.sourceDocumentSN,
|
||||
record.sourceDocumentId,
|
||||
record.documentId,
|
||||
record.sourceExtendData?._langfuseTraceId,
|
||||
record.sourceExtendData?.simulation_batch_id
|
||||
].some((value) => value?.toLowerCase().includes(normalized))
|
||||
record.sourceExtendData?.simulation_batch_id,
|
||||
record.status,
|
||||
record.taskMode,
|
||||
record.finalExecutionMode,
|
||||
record.sourceSystem,
|
||||
record.ruleSetVersionNumber,
|
||||
record.ruleSetPublishedByName
|
||||
];
|
||||
|
||||
return directValues.some((value) => scalarIncludes(value, normalized)) ||
|
||||
objectContainsScalar(record, normalized);
|
||||
});
|
||||
}
|
||||
|
||||
function scalarIncludes(value: unknown, normalized: string): boolean {
|
||||
if (value === null || value === undefined) return false;
|
||||
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
||||
return String(value).toLowerCase().includes(normalized);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function objectContainsScalar(value: unknown, normalized: string, depth = 0): boolean {
|
||||
if (depth > 6) return false;
|
||||
if (scalarIncludes(value, normalized)) return true;
|
||||
if (!value || typeof value !== "object") return false;
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
return value.some((item) => objectContainsScalar(item, normalized, depth + 1));
|
||||
}
|
||||
|
||||
return Object.entries(value as Record<string, unknown>).some(([key, item]) =>
|
||||
scalarIncludes(key, normalized) || objectContainsScalar(item, normalized, depth + 1)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -203,6 +203,9 @@ export const ApprovalRunSchema = z.object({
|
||||
result: z.unknown().optional(),
|
||||
pipelineData: z.unknown().optional(),
|
||||
sourceExtendData: ApprovalRunSourceExtendDataSchema.optional(),
|
||||
ruleSetVersionNumber: z.number().optional(),
|
||||
ruleSetPublishedAt: z.number().optional(),
|
||||
ruleSetPublishedByName: z.string().optional(),
|
||||
createdAt: z.union([z.string(), z.number()]).optional(),
|
||||
updatedAt: z.union([z.string(), z.number()]).optional()
|
||||
}).passthrough();
|
||||
|
||||
Reference in new issue
Block a user