112 lines
4.0 KiB
TypeScript
112 lines
4.0 KiB
TypeScript
import { z } from "zod";
|
|
import { getEasoClient } from "../../core/auth/easo.js";
|
|
import type { CorpConfig } from "../../core/config/types.js";
|
|
import { fetchStaffs } from "./api.js";
|
|
import type { ContactListResult, ContactSearchOptions, Staff } from "./types.js";
|
|
|
|
export const DEFAULT_STAFF_LIST_LIMIT = 20;
|
|
|
|
export async function listStaffsProtected(
|
|
corp: CorpConfig,
|
|
options: ContactSearchOptions = {}
|
|
): Promise<ContactListResult> {
|
|
const { client } = await getEasoClient(corp);
|
|
const allStaffs = await fetchStaffs(client, options.external ?? false);
|
|
|
|
const total = allStaffs.length;
|
|
const isFull = options.allowFull === true;
|
|
const limit = isFull ? total : options.limit ?? DEFAULT_STAFF_LIST_LIMIT;
|
|
const staffs = allStaffs.slice(0, limit);
|
|
const hasMore = total > staffs.length;
|
|
|
|
return {
|
|
total,
|
|
limit,
|
|
hasMore,
|
|
hint: hasMore
|
|
? `企业员工共 ${total} 人,当前仅展示前 ${staffs.length} 条以防止上下文溢出。可使用 'hose-use contact search <姓名|工号>' 精准检索,或附加 '--limit <数量>' / '-o <文件路径>' / '--full'`
|
|
: undefined,
|
|
staffs,
|
|
};
|
|
}
|
|
|
|
export async function searchStaffs(
|
|
corp: CorpConfig,
|
|
options: { keyword: string; limit?: number; external?: boolean }
|
|
): Promise<Staff[]> {
|
|
const { client } = await getEasoClient(corp);
|
|
const allStaffs = await fetchStaffs(client, options.external ?? false);
|
|
|
|
const kw = options.keyword.trim().toLowerCase();
|
|
if (!kw) {
|
|
return allStaffs.slice(0, options.limit ?? DEFAULT_STAFF_LIST_LIMIT);
|
|
}
|
|
|
|
const matched = allStaffs.filter((s) => {
|
|
const name = s.name?.toLowerCase() || "";
|
|
const email = s.email?.toLowerCase() || "";
|
|
const phone = s.cellphone || "";
|
|
const id = s.id?.toLowerCase() || "";
|
|
return name.includes(kw) || email.includes(kw) || phone.includes(kw) || id.includes(kw);
|
|
});
|
|
|
|
return matched.slice(0, options.limit ?? 50);
|
|
}
|
|
|
|
export async function getStaffById(
|
|
corp: CorpConfig,
|
|
staffId: string
|
|
): Promise<Staff | null> {
|
|
const { client } = await getEasoClient(corp);
|
|
const allStaffs = await fetchStaffs(client, false);
|
|
|
|
const cleanId = staffId.trim();
|
|
const found = allStaffs.find(
|
|
(s) => s.id === cleanId || s.id.endsWith(`:${cleanId}`) || s.id.split(":")[1] === cleanId
|
|
);
|
|
|
|
return found || null;
|
|
}
|
|
|
|
// ── MCP Tool Definitions ──────────────────────────────────────────────────────
|
|
|
|
export const contactTools = [
|
|
{
|
|
name: "contact_search",
|
|
description: "在当前企业通讯录中模糊检索员工(支持姓名、邮箱、手机号、工号)",
|
|
parameters: z.object({
|
|
keyword: z.string().describe("检索关键词,如姓名、邮箱前缀、手机号或工号"),
|
|
limit: z.number().optional().describe("最大返回条数,默认 20"),
|
|
}),
|
|
handler: async (corp: CorpConfig, params: { keyword: string; limit?: number }) => {
|
|
const results = await searchStaffs(corp, params);
|
|
return { count: results.length, staffs: results };
|
|
},
|
|
},
|
|
{
|
|
name: "contact_get",
|
|
description: "根据员工 staffId 或工号精准获取员工详细档案",
|
|
parameters: z.object({
|
|
staffId: z.string().describe("员工的完整 staffId 或 ID"),
|
|
}),
|
|
handler: async (corp: CorpConfig, params: { staffId: string }) => {
|
|
const staff = await getStaffById(corp, params.staffId);
|
|
if (!staff) {
|
|
throw new Error(`未找到员工: ${params.staffId}`);
|
|
}
|
|
return staff;
|
|
},
|
|
},
|
|
{
|
|
name: "contact_list",
|
|
description: "查看企业通讯录(带默认 20 条防爆截断与总数保护)",
|
|
parameters: z.object({
|
|
limit: z.number().optional().describe("截断返回条数,默认 20"),
|
|
allowFull: z.boolean().optional().describe("是否强制全量输出(大企业请谨慎使用)"),
|
|
}),
|
|
handler: async (corp: CorpConfig, params: { limit?: number; allowFull?: boolean }) => {
|
|
return await listStaffsProtected(corp, params);
|
|
},
|
|
},
|
|
];
|