From c0318df70b2f32ab24bec14730a0afefa1ead2f6 Mon Sep 17 00:00:00 2001 From: yunyingk Date: Wed, 29 Jul 2026 17:24:22 +0800 Subject: [PATCH] Add user search, activation, and automated delegation flow and bump version to 0.4.0 --- .agents/skills/seal-home/SKILL.md | 12 ++-- package.json | 2 +- src/cli.ts | 21 +++++++ src/core/http/factory.ts | 24 ++++++++ src/domains/seal/api.ts | 94 +++++++++++++++++++++++++++++++ src/domains/seal/tools.test.ts | 6 ++ src/domains/seal/tools.ts | 35 ++++++++++++ src/domains/seal/types.ts | 6 ++ 8 files changed, 193 insertions(+), 7 deletions(-) diff --git a/.agents/skills/seal-home/SKILL.md b/.agents/skills/seal-home/SKILL.md index b2a4c11..464f098 100644 --- a/.agents/skills/seal-home/SKILL.md +++ b/.agents/skills/seal-home/SKILL.md @@ -1,13 +1,13 @@ --- name: seal-home -version: 0.3.8 -description: "v0.3.8: Use when working with Seal Home approval automation: 合思/易快报企业认证, Hose authorization links, approval run lookup, LLM config, permissions, billing status, users & password reset, delegation links, approval rules, documents, search, and Langfuse hints." +version: 0.4.0 +description: "v0.4.0: Use when working with Seal Home approval automation: 合思/易快报企业认证, Hose authorization links, approval run lookup, LLM config, permissions, billing status, user search, user activation, admin delegation link generation, delegation links, approval rules, documents, search, and Langfuse hints." metadata: - version: 0.3.8 - short-description: Seal approval automation tools (v0.3.8) + version: 0.4.0 + short-description: Seal approval automation tools (v0.4.0) --- -# Seal Home (v0.3.8) +# Seal Home (v0.4.0) Use the global `seal-home` CLI. This skill is a routing layer for agents: keep this file short, choose the right command, and read a focused reference only when the task needs detail. @@ -30,7 +30,7 @@ seal-home tools list ## Route By Intent - Delegation links (授权链接): `seal-home delegation list`, `create [--note ...]`, `revoke `. -- User & Contacts Management (用户与重置默认密码): `seal-home users create --name ... --phone ...`, `reset-password `, `get `, `delete `. +- User & Contacts Management (用户搜索、激活与一键代生成授权): `seal-home users search [keyword]`, `users delegation `, `users activate `, `users create --name ... --phone ...`, `reset-password `, `get `, `delete `. - LLM Model Config & Providers: `seal-home llm config`, `seal-home llm providers`. - System Permissions Config: `seal-home permissions available`, `seal-home permissions configs`. - Billing & Feature Quotas: `seal-home billing status`, `seal-home billing plans`, `seal-home billing features`. diff --git a/package.json b/package.json index 8926681..4711910 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "seal-home", - "version": "0.3.8", + "version": "0.4.0", "type": "module", "module": "src/server.ts", "bin": { diff --git a/src/cli.ts b/src/cli.ts index d9c57a1..efb7ce3 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -177,6 +177,12 @@ async function main() { return; } + if (area === "users" && (action === "search" || action === "list")) { + const keyword = maybeName ?? stringOption(args.options.keyword) ?? stringOption(args.options.query) ?? ""; + await runTool("seal_user_search", client, corp, { keyword }, outputOptions(args.options)); + return; + } + if (area === "users" && action === "get") { const userId = maybeName ?? stringOption(args.options.userId); if (!userId) throw new Error("Usage: seal-home users get "); @@ -191,6 +197,21 @@ async function main() { return; } + if (area === "users" && action === "activate") { + const userId = maybeName ?? stringOption(args.options.userId); + if (!userId) throw new Error("Usage: seal-home users activate "); + await runTool("seal_user_activate", client, corp, { userId }, outputOptions(args.options)); + return; + } + + if (area === "users" && (action === "delegation" || action === "impersonate")) { + const userIdOrPhone = maybeName ?? stringOption(args.options.userId) ?? stringOption(args.options.phone); + if (!userIdOrPhone) throw new Error("Usage: seal-home users delegation [--note ]"); + const note = stringOption(args.options.note); + await runTool("seal_user_delegation_create", client, corp, { userIdOrPhone, note }, outputOptions(args.options)); + return; + } + if (area === "llm" && (action === "config" || action === "get")) { await runTool("seal_llm_config_get", client, corp, {}, outputOptions(args.options)); return; diff --git a/src/core/http/factory.ts b/src/core/http/factory.ts index f7f1602..147f758 100644 --- a/src/core/http/factory.ts +++ b/src/core/http/factory.ts @@ -28,3 +28,27 @@ export async function createSealClient(corp: CorpConfig): Promise { } }); } + +export async function createSealClientForToken(corp: CorpConfig, token: string): Promise { + const session = await getSealSessionForCorp(corp); + + return ky.create({ + prefix: session.enterprise.sealUrl, + timeout: 30000, + headers: { + Authorization: `Bearer ${token}` + }, + hooks: { + beforeError: [ + async ({ error }) => { + const body = + error instanceof HTTPError + ? await error.response.text().catch(() => "") + : ""; + error.message = `${error.message}${body ? `: ${body}` : ""}`; + return error; + } + ] + } + }); +} diff --git a/src/domains/seal/api.ts b/src/domains/seal/api.ts index 38e5536..a10e622 100644 --- a/src/domains/seal/api.ts +++ b/src/domains/seal/api.ts @@ -31,6 +31,8 @@ import { SealUserResetPasswordResultSchema, SealUserDetail, SealUserDetailSchema, + SealContactSearchResult, + SealContactSearchResultSchema, LlmConfig, LlmConfigSchema, LlmProvider, @@ -396,6 +398,16 @@ export async function getUserDetail( ); } +export async function searchUsers( + client: KyInstance, + keyword = "" +): Promise { + return unwrap( + await client.get("api/v1/contacts/search", { searchParams: { keyword } }), + SealContactSearchResultSchema + ); +} + export async function deleteLocalUser( client: KyInstance, userId: string @@ -405,6 +417,88 @@ export async function deleteLocalUser( return json.data?.success ?? response.ok; } +export async function activateLocalUser( + client: KyInstance, + userId: string +): Promise { + const response = await client.post(`api/v1/contacts/local/users/${userId}/activate`, { json: {} }); + const json = (await response.json()) as { data?: { success?: boolean } }; + return json.data?.success ?? response.ok; +} + +export async function loginWithPhonePassword( + client: KyInstance, + params: { phone: string; password: string } +): Promise<{ token: string; user: unknown }> { + const response = await client.post("api/auth/sms-otp/sign-in-password", { + json: params + }); + const setAuthToken = response.headers.get("set-auth-token"); + const json = (await response.json()) as { user?: unknown; session?: { token?: string } }; + const token = setAuthToken ?? json.session?.token; + if (!token) { + throw new Error("Failed to retrieve authentication token from login response"); + } + return { token, user: json.user }; +} + +export async function createDelegationForUser( + client: KyInstance, + params: { userIdOrPhone: string; note?: string }, + createClientForToken: (token: string) => Promise +): Promise<{ + targetUser: SealUserDetail; + delegation: SealDelegationCreateResult; + temporaryPassword: string; +}> { + let user: SealUserDetail; + if (params.userIdOrPhone.startsWith("+") || /^\d{11}$/.test(params.userIdOrPhone)) { + const search = await searchUsers(client, params.userIdOrPhone); + const found = search.users.find( + (u) => u.mobile?.includes(params.userIdOrPhone) || u._id === params.userIdOrPhone + ) ?? search.users[0]; + if (!found) { + throw new Error(`User not found with phone/ID: ${params.userIdOrPhone}`); + } + user = found; + } else { + user = await getUserDetail(client, params.userIdOrPhone).catch(async () => { + const search = await searchUsers(client, params.userIdOrPhone); + const found = search.users[0]; + if (!found) throw new Error(`User not found: ${params.userIdOrPhone}`); + return found; + }); + } + + if (user.status === "resigned") { + await activateLocalUser(client, user._id); + user = await getUserDetail(client, user._id); + } + + const resetRes = await resetUserPassword(client, user._id); + const rawPhone = user.mobile || (user as unknown as { identities?: Array<{ providerId?: string }> }).identities?.[0]?.providerId || ""; + const cleanPhone = rawPhone.replace(/^\+86/, ""); + if (!cleanPhone) { + throw new Error(`User ${user.name} has no associated phone number for password login`); + } + + const { token } = await loginWithPhonePassword(client, { + phone: cleanPhone, + password: resetRes.password + }); + + const userClient = await createClientForToken(token); + const delegation = await createDelegation(userClient, { + note: params.note ?? `Delegation generated by admin for ${user.name}` + }); + + return { + targetUser: user, + delegation, + temporaryPassword: resetRes.password + }; +} + export async function getLlmConfig(client: KyInstance): Promise { return unwrap(await client.get("api/v1/llm/config"), LlmConfigSchema); } diff --git a/src/domains/seal/tools.test.ts b/src/domains/seal/tools.test.ts index 4e53305..8e0d938 100644 --- a/src/domains/seal/tools.test.ts +++ b/src/domains/seal/tools.test.ts @@ -70,12 +70,18 @@ describe("sealTools", () => { const createTool = sealTools.find((item) => item.name === "seal_user_create"); const resetTool = sealTools.find((item) => item.name === "seal_user_reset_password"); const getTool = sealTools.find((item) => item.name === "seal_user_get"); + const searchTool = sealTools.find((item) => item.name === "seal_user_search"); const deleteTool = sealTools.find((item) => item.name === "seal_user_delete"); + const activateTool = sealTools.find((item) => item.name === "seal_user_activate"); + const userDelegationTool = sealTools.find((item) => item.name === "seal_user_delegation_create"); expect(createTool).toBeDefined(); expect(resetTool).toBeDefined(); expect(getTool).toBeDefined(); + expect(searchTool).toBeDefined(); expect(deleteTool).toBeDefined(); + expect(activateTool).toBeDefined(); + expect(userDelegationTool).toBeDefined(); }); test("includes system configuration tools", () => { diff --git a/src/domains/seal/tools.ts b/src/domains/seal/tools.ts index 2589631..263df09 100644 --- a/src/domains/seal/tools.ts +++ b/src/domains/seal/tools.ts @@ -3,6 +3,7 @@ import { KyInstance } from "ky"; import * as api from "./api.js"; import { getHoseEnterpriseUrl } from "../../core/auth/hose.js"; import { CorpConfig } from "../../core/config/types.js"; +import { createSealClientForToken } from "../../core/http/factory.js"; import { searchApprovalContent } from "./search.js"; import type { ApprovalRule, ApprovalRun, RuleSetVersion } from "./types.js"; @@ -86,6 +87,15 @@ export const sealTools = [ handler: async (client: KyInstance, params: { userId: string }) => api.getUserDetail(client, params.userId) }, + { + name: "seal_user_search", + description: "按姓名、手机号或邮箱搜索 Seal 成员用户列表", + parameters: z.object({ + keyword: z.string().optional().describe("搜索关键词(姓名、手机号、邮箱等),不传或传空字符串可列出租户成员") + }), + handler: async (client: KyInstance, params: { keyword?: string }) => + api.searchUsers(client, params.keyword ?? "") + }, { name: "seal_user_delete", description: "删除指定 Seal 本地用户", @@ -95,6 +105,31 @@ export const sealTools = [ handler: async (client: KyInstance, params: { userId: string }) => api.deleteLocalUser(client, params.userId) }, + { + name: "seal_user_activate", + description: "启用/激活已离职的 Seal 用户账号", + parameters: z.object({ + userId: z.string().describe("用户 ID (_id)") + }), + handler: async (client: KyInstance, params: { userId: string }) => + api.activateLocalUser(client, params.userId) + }, + { + name: "seal_user_delegation_create", + description: "为指定 Seal 用户(按用户ID、手机号或姓名)重置密码登录并生成其专属授权链接 (Delegation)", + parameters: z.object({ + userIdOrPhone: z.string().describe("目标用户 ID、手机号或姓名"), + note: z.string().optional().describe("授权链接备注信息") + }), + handler: async ( + client: KyInstance, + params: { userIdOrPhone: string; note?: string }, + context: ToolContext + ) => + api.createDelegationForUser(client, params, (token) => + createSealClientForToken(context.corp, token) + ) + }, { name: "seal_llm_config_get", description: "查询当前 Seal 租户的大模型 (LLM) 配置", diff --git a/src/domains/seal/types.ts b/src/domains/seal/types.ts index 41f9036..68f6f69 100644 --- a/src/domains/seal/types.ts +++ b/src/domains/seal/types.ts @@ -256,6 +256,11 @@ export const SealUserDetailSchema = z.object({ updatedAt: z.string().optional() }).passthrough(); +export const SealContactSearchResultSchema = z.object({ + departments: z.array(z.record(z.string(), z.unknown())).default([]), + users: z.array(SealUserDetailSchema).default([]) +}).passthrough(); + export const LlmConfigSchema = z.object({ provider: z.string(), config: z.record(z.string(), z.unknown()).default({}) @@ -303,6 +308,7 @@ export type SealDelegationCreateResult = z.infer; export type SealUserResetPasswordResult = z.infer; export type SealUserDetail = z.infer; +export type SealContactSearchResult = z.infer; export type LlmConfig = z.infer; export type LlmProvider = z.infer; export type AvailablePermissionsData = z.infer;