feat: support approval rule history search

This commit is contained in:
gitea committed 2026-06-05 16:34:03 +08:00
1 parent 2038fcc6b2
commit d6a33de3d7
4 files changed
+102 -10

No files matched your search

+1 -1
View File
@@ -49,7 +49,7 @@ For an already-authenticated Seal source, copy `enterprises/example.direct.json`
- `seal_source_config`: resolve current Seal enterprise config from the configured source.
- `seal_whoami`: current Seal user and tenant.
- `seal_session_get`: current Seal Bearer session and expiration.
- `seal_approval_search`: keyword search across approval rules, documents, and approval preferences. Results include area, entity, field, line number, and surrounding context. Search data is cached in memory for 5 minutes.
- `seal_approval_search`: keyword search across approval rules, documents, approval preferences, and optionally published rule history. Results include area, entity, field, line number, and surrounding context. Search data is cached in memory for 5 minutes.
- `seal_approval_context_get`: read current approval rules, approval documents, and approval style preferences in one call.
- `seal_approval_rules_list`: current draft approval rules.
- `seal_approval_rule_create`, `seal_approval_rule_update`, `seal_approval_rule_delete`: maintain approval rules.
+10
View File
@@ -59,6 +59,16 @@ export async function listApprovalRuleVersions(client: KyInstance): Promise<Rule
return unwrap(await client.get("api/v1/rule-set-versions"), z.array(RuleSetVersionSchema));
}
export async function getApprovalRuleVersion(
client: KyInstance,
versionId: string
): Promise<RuleSetVersion> {
return unwrap(
await client.get(`api/v1/rule-set-versions/${versionId}`),
RuleSetVersionSchema
);
}
export async function publishApprovalRuleVersion(
client: KyInstance,
versionName: string
+86 -8
View File
@@ -3,16 +3,20 @@ import { CorpConfig } from "../../core/config/types.js";
import {
ApprovalDocument,
ApprovalRule,
ApprovalStylePreferences
ApprovalStylePreferences,
RuleSetVersion
} from "./types.js";
import {
getApprovalRuleVersion,
getApprovalStylePreferences,
listApprovalDocuments,
listApprovalRuleVersions,
listApprovalRules
} from "./api.js";
type MatchMode = "any" | "all";
type SearchArea = "rules" | "documents" | "preferences";
type RuleVersionScope = "current" | "all" | "version";
export type ApprovalSearchParams = {
keywords: string[];
@@ -23,6 +27,8 @@ export type ApprovalSearchParams = {
documentLimit?: number;
maxResults?: number;
refresh?: boolean;
ruleVersionScope?: RuleVersionScope;
ruleVersionId?: string;
};
export type ApprovalSearchResult = {
@@ -161,6 +167,44 @@ function ruleEntries(rule: ApprovalRule): SearchEntry[] {
];
}
function ruleVersionEntries(version: RuleSetVersion): SearchEntry[] {
return version.rules.flatMap((rule, index) => {
const entityId = `${version.id}:${rule.description.slice(0, 32)}`;
const metadata = {
versionId: version.id,
versionNumber: version.versionNumber,
versionName: version.versionName,
publishedByName: version.publishedByName,
publishedAt: version.publishedAt,
scope: rule.scope,
strictness: rule.strictness,
code: "code" in rule ? (rule as { code?: string }).code : undefined,
index
};
return [
{
area: "rules" as const,
entityType: "rule" as const,
entityId,
title: `${version.versionName} / ${rule.scope}`,
field: "history.description",
value: rule.description,
metadata
},
{
area: "rules" as const,
entityType: "rule" as const,
entityId,
title: `${version.versionName} / ${rule.scope}`,
field: "history.scope",
value: rule.scope,
metadata
}
];
});
}
function documentEntries(document: ApprovalDocument): SearchEntry[] {
return [
{
@@ -244,15 +288,22 @@ function preferenceEntries(preferences: ApprovalStylePreferences): SearchEntry[]
async function loadSearchEntries(
client: KyInstance,
corp: CorpConfig,
params: Pick<ApprovalSearchParams, "documentLimit" | "refresh">
params: Pick<ApprovalSearchParams, "documentLimit" | "refresh" | "ruleVersionScope" | "ruleVersionId">
): Promise<SearchCacheEntry> {
const cached = searchCache.get(corp.id);
const ruleVersionScope = params.ruleVersionScope ?? "current";
const cacheKey = [
corp.id,
`ruleVersionScope=${ruleVersionScope}`,
`ruleVersionId=${params.ruleVersionId ?? ""}`,
`documentLimit=${params.documentLimit ?? 100}`
].join("|");
const cached = searchCache.get(cacheKey);
if (!params.refresh && cached && Date.now() < cached.expiresAt) {
return cached;
}
const [rules, documents, preferences] = await Promise.all([
listApprovalRules(client),
loadRuleEntries(client, ruleVersionScope, params.ruleVersionId),
listApprovalDocuments(client, { limit: params.documentLimit ?? 100 }),
getApprovalStylePreferences(
client,
@@ -263,7 +314,7 @@ async function loadSearchEntries(
const fetchedAt = Date.now();
const next: SearchCacheEntry = {
entries: [
...rules.rules.flatMap(ruleEntries),
...rules,
...documents.articles.flatMap(documentEntries),
...preferenceEntries(preferences)
],
@@ -271,10 +322,32 @@ async function loadSearchEntries(
expiresAt: fetchedAt + CACHE_TTL_MS
};
searchCache.set(corp.id, next);
searchCache.set(cacheKey, next);
return next;
}
async function loadRuleEntries(
client: KyInstance,
scope: RuleVersionScope,
versionId?: string
): Promise<SearchEntry[]> {
if (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);
return ruleVersionEntries(version);
}
const versions = await listApprovalRuleVersions(client);
return versions.flatMap(ruleVersionEntries);
}
export async function searchApprovalContent(
client: KyInstance,
corp: CorpConfig,
@@ -285,6 +358,7 @@ export async function searchApprovalContent(
const contextLines = params.contextLines ?? 3;
const areas = new Set(params.areas ?? ["rules", "documents", "preferences"]);
const maxResults = params.maxResults ?? 50;
const ruleVersionScope = params.ruleVersionScope ?? "current";
const keywords = normalizeKeywords(params.keywords, caseSensitive);
if (keywords.length === 0) {
@@ -293,7 +367,9 @@ export async function searchApprovalContent(
const cached = await loadSearchEntries(client, corp, {
documentLimit: params.documentLimit,
refresh: params.refresh
refresh: params.refresh,
ruleVersionScope,
ruleVersionId: params.ruleVersionId
});
const results = cached.entries
@@ -309,7 +385,9 @@ export async function searchApprovalContent(
areas: Array.from(areas),
caseSensitive,
contextLines,
refresh: params.refresh ?? false
refresh: params.refresh ?? false,
ruleVersionScope,
ruleVersionId: params.ruleVersionId
},
cache: {
fetchedAt: new Date(cached.fetchedAt).toISOString(),
+5 -1
View File
@@ -173,7 +173,9 @@ export const sealTools = [
contextLines: z.number().int().nonnegative().optional().describe("返回命中行前后几行,默认 3"),
documentLimit: z.number().int().positive().optional().describe("缓存刷新时最多拉取文档数,默认 100"),
maxResults: z.number().int().positive().optional().describe("最多返回命中数,默认 50"),
refresh: z.boolean().optional().describe("是否跳过 5 分钟内存缓存并强制刷新")
refresh: z.boolean().optional().describe("是否跳过 5 分钟内存缓存并强制刷新"),
ruleVersionScope: z.enum(["current", "all", "version"]).optional().describe("规则检索范围,默认 current;all 会检索所有已发布历史版本"),
ruleVersionId: z.string().optional().describe("ruleVersionScope=version 时指定版本 ID")
}),
handler: async (
client: KyInstance,
@@ -186,6 +188,8 @@ export const sealTools = [
documentLimit?: number;
maxResults?: number;
refresh?: boolean;
ruleVersionScope?: "current" | "all" | "version";
ruleVersionId?: string;
},
context: ToolContext
) => searchApprovalContent(client, context.corp, params)