54 lines
2.9 KiB
TypeScript
54 lines
2.9 KiB
TypeScript
import { z } from "zod";
|
|
import type { KyInstance } from "ky";
|
|
import { listDimensionCatalogs, listDimensionItems } from "./api.js";
|
|
import type { DimensionCatalog, DimensionItem, DimensionQuery } from "./types.js";
|
|
|
|
const cache = new Map<string, { timestamp: number; catalogs: DimensionCatalog[]; items: Map<string, DimensionItem[]> }>();
|
|
const CACHE_TTL_MS = 10 * 60 * 1000;
|
|
|
|
async function getCached(client: KyInstance, corpId: string): Promise<{ catalogs: DimensionCatalog[]; items: Map<string, DimensionItem[]> }> {
|
|
const existing = cache.get(corpId);
|
|
if (existing && Date.now() - existing.timestamp < CACHE_TTL_MS) return existing;
|
|
const catalogs = await listDimensionCatalogs(client, corpId);
|
|
const result = { timestamp: Date.now(), catalogs, items: new Map<string, DimensionItem[]>() };
|
|
cache.set(corpId, result);
|
|
return result;
|
|
}
|
|
|
|
export async function searchDimensions(client: KyInstance, corpId: string, query: DimensionQuery = {}): Promise<DimensionCatalog[]> {
|
|
const { catalogs } = await getCached(client, corpId);
|
|
const keyword = (query.name || query.code || query.id || "").trim().toLowerCase();
|
|
if (!keyword) return catalogs;
|
|
return catalogs.filter((item) => [item.id, item.name, item.code].some((value) => value.toLowerCase().includes(keyword)));
|
|
}
|
|
|
|
export async function getDimensionItems(client: KyInstance, corpId: string, dimension: DimensionCatalog, start = 0, count = 100): Promise<DimensionItem[]> {
|
|
const catalog = await getCached(client, corpId);
|
|
const cached = catalog.items.get(dimension.id);
|
|
if (cached && start === 0 && count >= cached.length) return cached;
|
|
const items = await listDimensionItems(client, corpId, dimension.id, start, count);
|
|
if (start === 0) catalog.items.set(dimension.id, items);
|
|
return items;
|
|
}
|
|
|
|
export async function resolveDimension(client: KyInstance, corpId: string, target: string): Promise<{ dimension: DimensionCatalog; items: DimensionItem[] } | undefined> {
|
|
const matches = await searchDimensions(client, corpId, { id: target, code: target, name: target });
|
|
const dimension = matches[0];
|
|
if (!dimension) return undefined;
|
|
return { dimension, items: await getDimensionItems(client, corpId, dimension) };
|
|
}
|
|
|
|
export const dimensionTools = [
|
|
{
|
|
name: "dimension_list",
|
|
description: "列出企业档案管理中的系统/自定义档案维度目录",
|
|
parameters: z.object({ keyword: z.string().optional(), count: z.number().optional() }),
|
|
handler: async ({ corpContext }: any, params: { keyword?: string; count?: number }) => {
|
|
const corp = corpContext.getCurrent();
|
|
if (!corp) throw new Error("未配置活跃企业上下文");
|
|
const items = await searchDimensions(await import("../../core/auth/ekb.js").then((m) => m.createEkbClient(corp)), corp.corpId || corp.id, { name: params.keyword });
|
|
return { count: items.length, dimensions: items.slice(0, params.count ?? 100) };
|
|
},
|
|
},
|
|
];
|