From 17a31042a1fe2b00333543a6c312a843f9d53c16 Mon Sep 17 00:00:00 2001 From: yunyingk Date: Fri, 11 Sep 2026 01:37:10 +0800 Subject: [PATCH] Add read-only contacts directory APIs --- skills/seal-use/references/cli.md | 4 +++ src/cli/commands/contacts.ts | 42 ++++++++++++++++++++++++++ src/cli/printer.ts | 4 +++ src/cli/runner.ts | 7 +++++ src/domains/seal/api.test.ts | 47 +++++++++++++++++++++++++++++ src/domains/seal/api.ts | 49 +++++++++++++++++++++++++++++++ src/domains/seal/tools.test.ts | 8 +++++ src/domains/seal/tools.ts | 39 ++++++++++++++++++++++++ src/domains/seal/types.ts | 30 +++++++++++++++++++ 9 files changed, 230 insertions(+) create mode 100644 src/cli/commands/contacts.ts diff --git a/skills/seal-use/references/cli.md b/skills/seal-use/references/cli.md index b956b50..4c796be 100644 --- a/skills/seal-use/references/cli.md +++ b/skills/seal-use/references/cli.md @@ -62,6 +62,10 @@ seal-use users create --name --phone [--dept ] [--corp [--corp ] seal-use users get [--corp ] seal-use users delete [--corp ] +seal-use contacts departments [] [--corp ] +seal-use contacts children [--corp ] +seal-use contacts users [--corp ] +seal-use contacts sync-time [--corp ] seal-use llm config [--corp ] seal-use llm providers [--corp ] seal-use permissions available [--corp ] diff --git a/src/cli/commands/contacts.ts b/src/cli/commands/contacts.ts new file mode 100644 index 0000000..9cc4031 --- /dev/null +++ b/src/cli/commands/contacts.ts @@ -0,0 +1,42 @@ +import type { CorpConfig } from "../../core/config/types.js"; +import type { createSealClient } from "../../core/http/factory.js"; +import { runTool } from "../helpers.js"; +import { outputOptions } from "../printer.js"; +import type { ParsedArgs } from "../types.js"; + +export async function handleContactsCommand( + action: string | undefined, + maybeName: string | undefined, + client: Awaited>, + corp: CorpConfig, + args: ParsedArgs +) { + if (action === "sync-time" || action === "last-sync-time") { + await runTool("seal_contacts_last_sync_time_get", client, corp, {}, outputOptions(args.options)); + return; + } + + if (action === "departments" || action === "department") { + if (!maybeName) { + await runTool("seal_contacts_departments_root_list", client, corp, {}, outputOptions(args.options)); + return; + } + + await runTool("seal_contacts_department_get", client, corp, { departmentId: maybeName }, outputOptions(args.options)); + return; + } + + if (action === "children" || action === "department-children") { + if (!maybeName) throw new Error("Usage: seal-use contacts children "); + await runTool("seal_contacts_department_children_list", client, corp, { departmentId: maybeName }, outputOptions(args.options)); + return; + } + + if (action === "users" || action === "department-users") { + if (!maybeName) throw new Error("Usage: seal-use contacts users "); + await runTool("seal_contacts_department_users_list", client, corp, { departmentId: maybeName }, outputOptions(args.options)); + return; + } + + throw new Error(`Unknown contacts command: ${action ?? ""}. Run 'seal-use help' for available commands.`); +} diff --git a/src/cli/printer.ts b/src/cli/printer.ts index 2757168..04d34be 100644 --- a/src/cli/printer.ts +++ b/src/cli/printer.ts @@ -113,6 +113,10 @@ Usage: seal-use users reset-password [--corp ] seal-use users get [--corp ] seal-use users delete [--corp ] + seal-use contacts departments [] [--corp ] + seal-use contacts children [--corp ] + seal-use contacts users [--corp ] + seal-use contacts sync-time [--corp ] seal-use llm config [--corp ] seal-use llm providers [--corp ] seal-use permissions available [--corp ] diff --git a/src/cli/runner.ts b/src/cli/runner.ts index 9105dcf..45ac83c 100755 --- a/src/cli/runner.ts +++ b/src/cli/runner.ts @@ -9,6 +9,7 @@ import { handleApprovalRunsCommand } from "./commands/approval-runs.js"; import { handleAuthCommand } from "./commands/auth.js"; import { handleBillingCommand } from "./commands/billing.js"; import { handleConfigCommand } from "./commands/config.js"; +import { handleContactsCommand } from "./commands/contacts.js"; import { handleContextCommand } from "./commands/context.js"; import { handleCorpsCommand } from "./commands/corps.js"; import { handleDelegationCommand, handleDelegationFromHoseLink } from "./commands/delegation.js"; @@ -63,6 +64,7 @@ const KNOWN_AREAS = new Set([ "auth", "delegation", "users", + "contacts", "llm", "permissions", "billing", @@ -320,6 +322,11 @@ async function dispatch(argv: string[], runtime: CliRuntime) { return; } + if (area === "contacts") { + await handleContactsCommand(action, maybeName, client, corp, args); + return; + } + if (area === "llm") { await handleLlmCommand(action, client, corp, args); return; diff --git a/src/domains/seal/api.test.ts b/src/domains/seal/api.test.ts index c827bfc..d764936 100644 --- a/src/domains/seal/api.test.ts +++ b/src/domains/seal/api.test.ts @@ -9,6 +9,11 @@ import { previewAddWebhookIntegration, previewDeleteWebhookIntegration, previewUpdateWebhookIntegration, + getDepartment, + getContactsLastSyncTime, + listDepartmentChildren, + listDepartmentUsers, + listRootDepartments, searchUsers, updateWebhookIntegration } from "./api.js"; @@ -38,6 +43,48 @@ describe("Seal contact API", () => { expect(requestedParams).toEqual({ query: "欧阳楠楠" }); expect(result.users).toEqual([{ _id: "user-1", name: "欧阳楠楠", departmentIds: [], permissions: [] }]); }); + + test("reads the department tree, department users, and last sync time", async () => { + const requestedUrls: string[] = []; + const client = { + get: (url: string) => { + requestedUrls.push(url); + const data = url.endsWith("last-sync-time") + ? { lastSyncTime: "2026-06-23T08:14:40.000Z" } + : url.endsWith("/users") + ? { items: [{ _id: "user-1", name: "测试用户" }] } + : url.endsWith("/children") || url.endsWith("root/children") + ? { items: [{ _id: "department-2", name: "财务" }] } + : { _id: "department-1", name: "公司", parentId: null }; + return { json: async () => ({ data }) }; + } + } as unknown as KyInstance; + + await expect(listRootDepartments(client)).resolves.toMatchObject({ + items: [{ _id: "department-2", name: "财务" }] + }); + await expect(getDepartment(client, "department-1")).resolves.toMatchObject({ + _id: "department-1", + name: "公司" + }); + await expect(listDepartmentChildren(client, "department-1")).resolves.toMatchObject({ + items: [{ _id: "department-2" }] + }); + await expect(listDepartmentUsers(client, "department-1")).resolves.toMatchObject({ + items: [{ _id: "user-1", name: "测试用户" }] + }); + await expect(getContactsLastSyncTime(client)).resolves.toEqual({ + lastSyncTime: "2026-06-23T08:14:40.000Z" + }); + + expect(requestedUrls).toEqual([ + "api/v1/contacts/departments/root/children", + "api/v1/contacts/departments/department-1", + "api/v1/contacts/departments/department-1/children", + "api/v1/contacts/departments/department-1/users", + "api/v1/contacts/last-sync-time" + ]); + }); }); describe("Seal Webhook API", () => { diff --git a/src/domains/seal/api.ts b/src/domains/seal/api.ts index 3064db6..6976a7c 100644 --- a/src/domains/seal/api.ts +++ b/src/domains/seal/api.ts @@ -41,6 +41,14 @@ import { SealUserDetailSchema, SealContactSearchResult, SealContactSearchResultSchema, + SealDepartment, + SealDepartmentSchema, + SealDepartmentListData, + SealDepartmentListDataSchema, + SealDepartmentUsersData, + SealDepartmentUsersDataSchema, + SealContactsLastSyncTime, + SealContactsLastSyncTimeSchema, LlmConfig, LlmConfigSchema, LlmProvider, @@ -926,6 +934,47 @@ export async function searchUsers( ); } +export async function listRootDepartments(client: KyInstance): Promise { + return unwrap( + await client.get("api/v1/contacts/departments/root/children"), + SealDepartmentListDataSchema + ); +} + +export async function getDepartment( + client: KyInstance, + departmentId: string +): Promise { + return unwrap( + await client.get(`api/v1/contacts/departments/${encodeURIComponent(departmentId)}`), + SealDepartmentSchema + ); +} + +export async function listDepartmentChildren( + client: KyInstance, + departmentId: string +): Promise { + return unwrap( + await client.get(`api/v1/contacts/departments/${encodeURIComponent(departmentId)}/children`), + SealDepartmentListDataSchema + ); +} + +export async function listDepartmentUsers( + client: KyInstance, + departmentId: string +): Promise { + return unwrap( + await client.get(`api/v1/contacts/departments/${encodeURIComponent(departmentId)}/users`), + SealDepartmentUsersDataSchema + ); +} + +export async function getContactsLastSyncTime(client: KyInstance): Promise { + return unwrap(await client.get("api/v1/contacts/last-sync-time"), SealContactsLastSyncTimeSchema); +} + export async function deleteLocalUser( client: KyInstance, userId: string diff --git a/src/domains/seal/tools.test.ts b/src/domains/seal/tools.test.ts index fcfa8b7..ed727c4 100644 --- a/src/domains/seal/tools.test.ts +++ b/src/domains/seal/tools.test.ts @@ -84,6 +84,14 @@ describe("sealTools", () => { expect(userDelegationTool).toBeDefined(); }); + test("includes read-only contacts directory tools", () => { + expect(sealTools.find((item) => item.name === "seal_contacts_departments_root_list")).toBeDefined(); + expect(sealTools.find((item) => item.name === "seal_contacts_department_get")).toBeDefined(); + expect(sealTools.find((item) => item.name === "seal_contacts_department_children_list")).toBeDefined(); + expect(sealTools.find((item) => item.name === "seal_contacts_department_users_list")).toBeDefined(); + expect(sealTools.find((item) => item.name === "seal_contacts_last_sync_time_get")).toBeDefined(); + }); + test("includes system configuration tools", () => { expect(sealTools.find((item) => item.name === "seal_llm_config_get")).toBeDefined(); expect(sealTools.find((item) => item.name === "seal_llm_providers_list")).toBeDefined(); diff --git a/src/domains/seal/tools.ts b/src/domains/seal/tools.ts index d2c7ffa..38f4228 100644 --- a/src/domains/seal/tools.ts +++ b/src/domains/seal/tools.ts @@ -165,6 +165,45 @@ export const sealTools = [ context.corp ) }, + { + name: "seal_contacts_departments_root_list", + description: "列出当前 Seal 租户通讯录的顶级部门", + parameters: z.object({}), + handler: async (client: KyInstance) => api.listRootDepartments(client) + }, + { + name: "seal_contacts_department_get", + description: "获取指定通讯录部门的详细信息", + parameters: z.object({ + departmentId: z.string().describe("部门 ID") + }), + handler: async (client: KyInstance, params: { departmentId: string }) => + api.getDepartment(client, params.departmentId) + }, + { + name: "seal_contacts_department_children_list", + description: "列出指定通讯录部门的直接子部门", + parameters: z.object({ + departmentId: z.string().describe("部门 ID") + }), + handler: async (client: KyInstance, params: { departmentId: string }) => + api.listDepartmentChildren(client, params.departmentId) + }, + { + name: "seal_contacts_department_users_list", + description: "列出指定通讯录部门下的用户", + parameters: z.object({ + departmentId: z.string().describe("部门 ID") + }), + handler: async (client: KyInstance, params: { departmentId: string }) => + api.listDepartmentUsers(client, params.departmentId) + }, + { + name: "seal_contacts_last_sync_time_get", + description: "查询通讯录最后同步时间", + parameters: z.object({}), + handler: async (client: KyInstance) => api.getContactsLastSyncTime(client) + }, { name: "seal_llm_config_get", description: "查询当前 Seal 租户的大模型 (LLM) 配置", diff --git a/src/domains/seal/types.ts b/src/domains/seal/types.ts index dd47f7c..c611835 100644 --- a/src/domains/seal/types.ts +++ b/src/domains/seal/types.ts @@ -288,6 +288,32 @@ export const SealContactSearchResultSchema = z.object({ users: z.array(SealUserDetailSchema).default([]) }).passthrough(); +export const SealDepartmentSchema = z.object({ + _id: z.string(), + tenantId: z.string().optional(), + name: z.string().optional(), + parentId: z.string().nullable().optional(), + ancestors: z.array(z.string()).default([]), + provider: z.string().optional(), + permissions: z.array(z.string()).default([]), + isDeleted: z.boolean().optional(), + syncedAt: z.string().optional(), + createdAt: z.string().optional(), + updatedAt: z.string().optional() +}).passthrough(); + +export const SealDepartmentListDataSchema = z.object({ + items: z.array(SealDepartmentSchema).default([]) +}).passthrough(); + +export const SealDepartmentUsersDataSchema = z.object({ + items: z.array(SealUserDetailSchema).default([]) +}).passthrough(); + +export const SealContactsLastSyncTimeSchema = z.object({ + lastSyncTime: z.union([z.string(), z.number(), z.null()]) +}).passthrough(); + export const LlmConfigSchema = z.object({ provider: z.string(), config: z.record(z.string(), z.unknown()).default({}) @@ -539,6 +565,10 @@ export type SealUserCreateResult = z.infer; export type SealUserResetPasswordResult = z.infer; export type SealUserDetail = z.infer; export type SealContactSearchResult = z.infer; +export type SealDepartment = z.infer; +export type SealDepartmentListData = z.infer; +export type SealDepartmentUsersData = z.infer; +export type SealContactsLastSyncTime = z.infer; export type LlmConfig = z.infer; export type LlmProvider = z.infer; export type AvailablePermissionsData = z.infer;