feat: add cached approval search tool
This commit is contained in:
1 parent
cfffa5f404
commit
2038fcc6b2
4 files changed
+358
No files matched your search
@@ -49,6 +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_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.
|
||||
|
||||
@@ -0,0 +1,323 @@
|
||||
import { KyInstance } from "ky";
|
||||
import { CorpConfig } from "../../core/config/types.js";
|
||||
import {
|
||||
ApprovalDocument,
|
||||
ApprovalRule,
|
||||
ApprovalStylePreferences
|
||||
} from "./types.js";
|
||||
import {
|
||||
getApprovalStylePreferences,
|
||||
listApprovalDocuments,
|
||||
listApprovalRules
|
||||
} from "./api.js";
|
||||
|
||||
type MatchMode = "any" | "all";
|
||||
type SearchArea = "rules" | "documents" | "preferences";
|
||||
|
||||
export type ApprovalSearchParams = {
|
||||
keywords: string[];
|
||||
matchMode?: MatchMode;
|
||||
areas?: SearchArea[];
|
||||
caseSensitive?: boolean;
|
||||
contextLines?: number;
|
||||
documentLimit?: number;
|
||||
maxResults?: number;
|
||||
refresh?: boolean;
|
||||
};
|
||||
|
||||
export type ApprovalSearchResult = {
|
||||
area: SearchArea;
|
||||
entityType: "rule" | "document" | "preference" | "integration";
|
||||
entityId?: string;
|
||||
title?: string;
|
||||
field: string;
|
||||
lineNumber: number;
|
||||
matchedKeywords: string[];
|
||||
matchedText: string;
|
||||
context: {
|
||||
before: string[];
|
||||
line: string;
|
||||
after: string[];
|
||||
};
|
||||
metadata?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
type SearchEntry = {
|
||||
area: SearchArea;
|
||||
entityType: ApprovalSearchResult["entityType"];
|
||||
entityId?: string;
|
||||
title?: string;
|
||||
field: string;
|
||||
value: unknown;
|
||||
metadata?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
type SearchCacheEntry = {
|
||||
entries: SearchEntry[];
|
||||
fetchedAt: number;
|
||||
expiresAt: number;
|
||||
};
|
||||
|
||||
const CACHE_TTL_MS = 5 * 60 * 1000;
|
||||
const searchCache = new Map<string, SearchCacheEntry>();
|
||||
|
||||
function normalizeKeywords(keywords: string[], caseSensitive: boolean): string[] {
|
||||
return keywords
|
||||
.map((keyword) => keyword.trim())
|
||||
.filter(Boolean)
|
||||
.map((keyword) => caseSensitive ? keyword : keyword.toLowerCase());
|
||||
}
|
||||
|
||||
function textForMatch(value: string, caseSensitive: boolean): string {
|
||||
return caseSensitive ? value : value.toLowerCase();
|
||||
}
|
||||
|
||||
function matchedKeywords(
|
||||
value: string,
|
||||
keywords: string[],
|
||||
matchMode: MatchMode,
|
||||
caseSensitive: boolean
|
||||
): string[] {
|
||||
const text = textForMatch(value, caseSensitive);
|
||||
const matches = keywords.filter((keyword) => text.includes(keyword));
|
||||
if (matchMode === "all" && matches.length !== keywords.length) return [];
|
||||
return matches;
|
||||
}
|
||||
|
||||
function linesOf(value: unknown): string[] {
|
||||
if (typeof value === "string") return value.split(/\r?\n/);
|
||||
return JSON.stringify(value, null, 2).split(/\r?\n/);
|
||||
}
|
||||
|
||||
function searchEntry(
|
||||
entry: SearchEntry,
|
||||
params: Required<Pick<ApprovalSearchParams, "matchMode" | "caseSensitive" | "contextLines">> & {
|
||||
keywords: string[];
|
||||
}
|
||||
): ApprovalSearchResult[] {
|
||||
const lines = linesOf(entry.value);
|
||||
const results: ApprovalSearchResult[] = [];
|
||||
|
||||
lines.forEach((line, index) => {
|
||||
const matches = matchedKeywords(
|
||||
line,
|
||||
params.keywords,
|
||||
params.matchMode,
|
||||
params.caseSensitive
|
||||
);
|
||||
if (matches.length === 0) return;
|
||||
|
||||
const beforeStart = Math.max(0, index - params.contextLines);
|
||||
const afterEnd = Math.min(lines.length, index + params.contextLines + 1);
|
||||
|
||||
results.push({
|
||||
area: entry.area,
|
||||
entityType: entry.entityType,
|
||||
entityId: entry.entityId,
|
||||
title: entry.title,
|
||||
field: entry.field,
|
||||
lineNumber: index + 1,
|
||||
matchedKeywords: matches,
|
||||
matchedText: line,
|
||||
context: {
|
||||
before: lines.slice(beforeStart, index),
|
||||
line,
|
||||
after: lines.slice(index + 1, afterEnd)
|
||||
},
|
||||
metadata: entry.metadata
|
||||
});
|
||||
});
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
function ruleEntries(rule: ApprovalRule): SearchEntry[] {
|
||||
return [
|
||||
{
|
||||
area: "rules",
|
||||
entityType: "rule",
|
||||
entityId: rule.id,
|
||||
title: rule.scope,
|
||||
field: "description",
|
||||
value: rule.description,
|
||||
metadata: {
|
||||
scope: rule.scope,
|
||||
strictness: rule.strictness,
|
||||
status: rule.status
|
||||
}
|
||||
},
|
||||
{
|
||||
area: "rules",
|
||||
entityType: "rule",
|
||||
entityId: rule.id,
|
||||
title: rule.scope,
|
||||
field: "scope",
|
||||
value: rule.scope,
|
||||
metadata: {
|
||||
strictness: rule.strictness,
|
||||
status: rule.status
|
||||
}
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
function documentEntries(document: ApprovalDocument): SearchEntry[] {
|
||||
return [
|
||||
{
|
||||
area: "documents",
|
||||
entityType: "document",
|
||||
entityId: document.id,
|
||||
title: document.title,
|
||||
field: "title",
|
||||
value: document.title,
|
||||
metadata: {
|
||||
status: document.status,
|
||||
scenarios: document.scenarios,
|
||||
tags: document.tags
|
||||
}
|
||||
},
|
||||
{
|
||||
area: "documents",
|
||||
entityType: "document",
|
||||
entityId: document.id,
|
||||
title: document.title,
|
||||
field: "purpose",
|
||||
value: document.purpose,
|
||||
metadata: {
|
||||
status: document.status,
|
||||
scenarios: document.scenarios,
|
||||
tags: document.tags
|
||||
}
|
||||
},
|
||||
{
|
||||
area: "documents",
|
||||
entityType: "document",
|
||||
entityId: document.id,
|
||||
title: document.title,
|
||||
field: "content",
|
||||
value: document.content,
|
||||
metadata: {
|
||||
status: document.status,
|
||||
scenarios: document.scenarios,
|
||||
tags: document.tags
|
||||
}
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
function preferenceEntries(preferences: ApprovalStylePreferences): SearchEntry[] {
|
||||
const entries: SearchEntry[] = [];
|
||||
|
||||
if (preferences.decisionPreferences) {
|
||||
for (const [field, value] of Object.entries(preferences.decisionPreferences)) {
|
||||
entries.push({
|
||||
area: "preferences",
|
||||
entityType: "preference",
|
||||
field: `decisionPreferences.${field}`,
|
||||
value,
|
||||
metadata: {
|
||||
integrationSystem: preferences.integrationSystem
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
preferences.integrations.forEach((integration, index) => {
|
||||
entries.push({
|
||||
area: "preferences",
|
||||
entityType: "integration",
|
||||
title: integration.outboundName,
|
||||
field: `integrations[${index}]`,
|
||||
value: integration,
|
||||
metadata: {
|
||||
type: integration.type,
|
||||
outboundName: integration.outboundName,
|
||||
mode: integration.mode,
|
||||
corporationId: integration.corporationId
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return entries;
|
||||
}
|
||||
|
||||
async function loadSearchEntries(
|
||||
client: KyInstance,
|
||||
corp: CorpConfig,
|
||||
params: Pick<ApprovalSearchParams, "documentLimit" | "refresh">
|
||||
): Promise<SearchCacheEntry> {
|
||||
const cached = searchCache.get(corp.id);
|
||||
if (!params.refresh && cached && Date.now() < cached.expiresAt) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
const [rules, documents, preferences] = await Promise.all([
|
||||
listApprovalRules(client),
|
||||
listApprovalDocuments(client, { limit: params.documentLimit ?? 100 }),
|
||||
getApprovalStylePreferences(
|
||||
client,
|
||||
corp.seal.endpoints.approvalStylePreferences
|
||||
)
|
||||
]);
|
||||
|
||||
const fetchedAt = Date.now();
|
||||
const next: SearchCacheEntry = {
|
||||
entries: [
|
||||
...rules.rules.flatMap(ruleEntries),
|
||||
...documents.articles.flatMap(documentEntries),
|
||||
...preferenceEntries(preferences)
|
||||
],
|
||||
fetchedAt,
|
||||
expiresAt: fetchedAt + CACHE_TTL_MS
|
||||
};
|
||||
|
||||
searchCache.set(corp.id, next);
|
||||
return next;
|
||||
}
|
||||
|
||||
export async function searchApprovalContent(
|
||||
client: KyInstance,
|
||||
corp: CorpConfig,
|
||||
params: ApprovalSearchParams
|
||||
) {
|
||||
const matchMode = params.matchMode ?? "any";
|
||||
const caseSensitive = params.caseSensitive ?? false;
|
||||
const contextLines = params.contextLines ?? 3;
|
||||
const areas = new Set(params.areas ?? ["rules", "documents", "preferences"]);
|
||||
const maxResults = params.maxResults ?? 50;
|
||||
const keywords = normalizeKeywords(params.keywords, caseSensitive);
|
||||
|
||||
if (keywords.length === 0) {
|
||||
throw new Error("keywords must contain at least one non-empty string");
|
||||
}
|
||||
|
||||
const cached = await loadSearchEntries(client, corp, {
|
||||
documentLimit: params.documentLimit,
|
||||
refresh: params.refresh
|
||||
});
|
||||
|
||||
const results = cached.entries
|
||||
.filter((entry) => areas.has(entry.area))
|
||||
.flatMap((entry) =>
|
||||
searchEntry(entry, { keywords, matchMode, caseSensitive, contextLines })
|
||||
);
|
||||
|
||||
return {
|
||||
query: {
|
||||
keywords: params.keywords,
|
||||
matchMode,
|
||||
areas: Array.from(areas),
|
||||
caseSensitive,
|
||||
contextLines,
|
||||
refresh: params.refresh ?? false
|
||||
},
|
||||
cache: {
|
||||
fetchedAt: new Date(cached.fetchedAt).toISOString(),
|
||||
expiresAt: new Date(cached.expiresAt).toISOString(),
|
||||
ttlMs: CACHE_TTL_MS
|
||||
},
|
||||
total: results.length,
|
||||
returned: Math.min(results.length, maxResults),
|
||||
results: results.slice(0, maxResults)
|
||||
};
|
||||
}
|
||||
@@ -29,4 +29,9 @@ describe("sealTools", () => {
|
||||
const tool = sealTools.find((item) => item.name === "seal_session_get");
|
||||
expect(tool).toBeDefined();
|
||||
});
|
||||
|
||||
test("includes the approval search tool", () => {
|
||||
const tool = sealTools.find((item) => item.name === "seal_approval_search");
|
||||
expect(tool).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -2,6 +2,7 @@ import { z } from "zod";
|
||||
import { KyInstance } from "ky";
|
||||
import * as api from "./api.js";
|
||||
import { CorpConfig } from "../../core/config/types.js";
|
||||
import { searchApprovalContent } from "./search.js";
|
||||
|
||||
type ToolContext = {
|
||||
corp: CorpConfig;
|
||||
@@ -161,6 +162,34 @@ export const sealTools = [
|
||||
return api.updateApprovalDocument(client, documentId, rest);
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "seal_approval_search",
|
||||
description: "按关键词组检索 Seal 审批规则、审批文档和审批偏好配置,返回命中位置、字段、行号和前后三行上下文",
|
||||
parameters: z.object({
|
||||
keywords: z.array(z.string()).min(1).describe("关键词组"),
|
||||
matchMode: z.enum(["any", "all"]).optional().describe("any 命中任一关键词,all 要求同一行命中全部关键词"),
|
||||
areas: z.array(z.enum(["rules", "documents", "preferences"])).optional().describe("检索范围"),
|
||||
caseSensitive: z.boolean().optional().describe("是否大小写敏感"),
|
||||
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 分钟内存缓存并强制刷新")
|
||||
}),
|
||||
handler: async (
|
||||
client: KyInstance,
|
||||
params: {
|
||||
keywords: string[];
|
||||
matchMode?: "any" | "all";
|
||||
areas?: Array<"rules" | "documents" | "preferences">;
|
||||
caseSensitive?: boolean;
|
||||
contextLines?: number;
|
||||
documentLimit?: number;
|
||||
maxResults?: number;
|
||||
refresh?: boolean;
|
||||
},
|
||||
context: ToolContext
|
||||
) => searchApprovalContent(client, context.corp, params)
|
||||
},
|
||||
{
|
||||
name: "seal_approval_context_get",
|
||||
description: "聚合获取 Seal 现有审批规则、审批文档和审批风格偏好",
|
||||
|
||||
Reference in new issue
Block a user