diff --git a/.agents/skills/seal-home/SKILL.md b/.agents/skills/seal-home/SKILL.md index 464f098..2b202fb 100644 --- a/.agents/skills/seal-home/SKILL.md +++ b/.agents/skills/seal-home/SKILL.md @@ -1,13 +1,13 @@ --- name: seal-home -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." +version: 0.4.1 +description: "v0.4.1: 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.4.0 - short-description: Seal approval automation tools (v0.4.0) + version: 0.4.1 + short-description: Seal approval automation tools (v0.4.1) --- -# Seal Home (v0.4.0) +# Seal Home (v0.4.1) 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. @@ -25,12 +25,15 @@ seal-home tools list - Before write operations on rules, documents, style preferences, or publishing, fetch current state and confirm the intended mutation with the user. For repeat test-environment rule resets, prefer `rules clear --dry-run` followed by `rules clear --yes`. - For date questions, use explicit dates and `--timezone Asia/Shanghai`. - If output may be large, use `--summary`, `--fields`, `--count`, or `--output-file`; use `--full` only when the user really needs full payloads. -- When returning authorization/login links (e.g. 合思授权链接), ALWAYS provide BOTH a code block (` ```text ... ``` `) for easy copying without accidental default browser redirection, AND a clickable hyperlink. +- When returning ANY authorization, login, or delegation links (e.g. 合思授权链接, Seal授权链接/委托链接), ALWAYS provide BOTH a code block (` ```text ... ``` `) for easy copying without accidental default browser redirection, AND a clickable hyperlink. +- For delegation links, always prefer `seal-home delegation create` over `users delegation` for link generation to ensure persistent token tracking. ## Route By Intent -- Delegation links (授权链接): `seal-home delegation list`, `create [--note ...]`, `revoke `. -- User & Contacts Management (用户搜索、激活与一键代生成授权): `seal-home users search [keyword]`, `users delegation `, `users activate `, `users create --name ... --phone ...`, `reset-password `, `get `, `delete `. +- User-facing Seal authorization / delegation link (Seal 授权链接/委托链接): `seal-home delegation create [--note ...] [--corp ]`. Always provide BOTH a code block (` ```text ... ``` `) AND a hyperlink. +- User-facing 合思 authorization link (合思授权链接): `seal-home auth hose-link --corp --expire 7200`; read `references/hose-enterprise-auth.md`. Always provide BOTH a code block (` ```text ... ``` `) AND a hyperlink. +- Delegation links management: `seal-home delegation list`, `create [--note ...]`, `revoke `. +- User & Contacts Management (用户搜索、激活与一键代生成授权): `seal-home users search [keyword]`, `users delegation ` (仅适用于 Seal 本地创建的账号 `provider: local`;对合思同步账号系统拒绝重置密码,需改用 `auth hose-link`), `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 4711910..99f2d8d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "seal-home", - "version": "0.4.0", + "version": "0.4.2", "type": "module", "module": "src/server.ts", "bin": { diff --git a/src/core/auth/hose.ts b/src/core/auth/hose.ts index 75113c7..a0f4a6c 100644 --- a/src/core/auth/hose.ts +++ b/src/core/auth/hose.ts @@ -40,7 +40,7 @@ type SealTokenResponse = { }; }; -function normalizeBaseUrl(url: string): string { +export function normalizeBaseUrl(url: string): string { return url.replace(/\/+$/, ""); } @@ -48,7 +48,7 @@ function isFresh(entry: TokenEntry | undefined, refreshTtl = 60): entry is Token return Boolean(entry && Date.now() < entry.expiresAt - refreshTtl * 1000); } -function defaultSealUrl(corp: CorpConfig, source: HoseSealSource): string { +export function defaultSealUrl(corp: CorpConfig, source: HoseSealSource): string { if (corp.seal.url) return corp.seal.url; if (source.sealUrl) return source.sealUrl; return `https://${source.corpId.toLowerCase()}.sealai.cc`; @@ -108,7 +108,7 @@ async function fetchHoseOpenapiToken(source: HoseSealSource): Promise { +export async function ensureHoseOpenapiToken(corp: CorpConfig): Promise { if (corp.source.type !== "hose") { throw new Error(`Unsupported Seal source: ${corp.source.type}`); } @@ -126,7 +126,7 @@ async function ensureHoseOpenapiToken(corp: CorpConfig): Promise { return openapi.token; } -async function fetchHoseCloseApiToken( +export async function fetchHoseCloseApiToken( source: HoseSealSource, openapiToken: string, expiresIn = 7200 diff --git a/src/domains/seal/api.ts b/src/domains/seal/api.ts index a10e622..d15ca88 100644 --- a/src/domains/seal/api.ts +++ b/src/domains/seal/api.ts @@ -1,5 +1,7 @@ -import { KyInstance } from "ky"; +import ky, { KyInstance } from "ky"; import { z } from "zod"; +import { CorpConfig } from "../../core/config/types.js"; +import { ensureHoseOpenapiToken, fetchHoseCloseApiToken, defaultSealUrl, normalizeBaseUrl } from "../../core/auth/hose.js"; import { ApprovalDocumentListData, ApprovalDocumentListDataSchema, @@ -445,7 +447,8 @@ export async function loginWithPhonePassword( export async function createDelegationForUser( client: KyInstance, params: { userIdOrPhone: string; note?: string }, - createClientForToken: (token: string) => Promise + createClientForToken: (token: string) => Promise, + corp?: CorpConfig ): Promise<{ targetUser: SealUserDetail; delegation: SealDelegationCreateResult; @@ -464,7 +467,9 @@ export async function createDelegationForUser( } else { user = await getUserDetail(client, params.userIdOrPhone).catch(async () => { const search = await searchUsers(client, params.userIdOrPhone); - const found = search.users[0]; + // 优先精确匹配姓名,避免返回错误用户 + const found = + search.users.find((u) => u.name === params.userIdOrPhone) ?? search.users[0]; if (!found) throw new Error(`User not found: ${params.userIdOrPhone}`); return found; }); @@ -475,7 +480,75 @@ export async function createDelegationForUser( user = await getUserDetail(client, user._id); } - const resetRes = await resetUserPassword(client, user._id); + const userAny = user as any; + const isSynced = + (Array.isArray(userAny.identities) && + (userAny.identities as Array<{ provider?: string }>).some( + (i) => i.provider && i.provider !== "local" + )) || + !!userAny.apps?.hosecloud; + + if (isSynced && corp && corp.source.type === "hose") { + const rawPhone = + user.mobile || + (userAny.identities as Array<{ providerId?: string }>)?.[0]?.providerId || + ""; + const cleanPhone = rawPhone.replace(/^\+86/, ""); + const hoseIdentity = (userAny.identities as Array<{ provider?: string; providerId?: string }>)?.find( + (i) => i.provider === "hosecloud" + )?.providerId; + const staffId = + (userAny.apps?.hosecloud as { boundId?: string })?.boundId || + hoseIdentity || + (cleanPhone ? `${corp.source.corpId}:${cleanPhone}` : undefined); + + if (staffId) { + const openapiToken = await ensureHoseOpenapiToken(corp); + const provisional = await fetchHoseCloseApiToken({ ...corp.source, staffId }, openapiToken); + const closeapiToken = provisional.token; + + const sealUrl = defaultSealUrl(corp, corp.source); + const sealResponse = await ky + .get(`${normalizeBaseUrl(sealUrl)}/api/auth/oauth2/session/oem-hosecloud`, { + searchParams: { + token: closeapiToken, + returnToken: "1" + }, + timeout: 30000 + }) + .json<{ token?: string; accessToken?: string; data?: { token?: string } }>(); + + const targetUserSealToken = + sealResponse.token ?? sealResponse.accessToken ?? sealResponse.data?.token; + + if (targetUserSealToken) { + const userClient = await createClientForToken(targetUserSealToken); + const delegation = await createDelegation(userClient, { + note: params.note ?? `Delegation generated by admin for ${user.name}` + }); + + return { + targetUser: user, + delegation, + temporaryPassword: "N/A (Hose SSO)" + }; + } + } + } + + let resetRes: SealUserResetPasswordResult; + try { + resetRes = await resetUserPassword(client, user._id); + } catch (err: any) { + if (isSynced) { + throw new Error( + `用户 "${user.name}" (${user.mobile || user._id}) 为合思同步账号 (provider: hosecloud),无 Seal 本地密码。` + + `Seal 系统暂不支持对同步账号直接重置本地密码。` + ); + } + throw err; + } + const rawPhone = user.mobile || (user as unknown as { identities?: Array<{ providerId?: string }> }).identities?.[0]?.providerId || ""; const cleanPhone = rawPhone.replace(/^\+86/, ""); if (!cleanPhone) { diff --git a/src/domains/seal/tools.ts b/src/domains/seal/tools.ts index 263df09..36cc2b8 100644 --- a/src/domains/seal/tools.ts +++ b/src/domains/seal/tools.ts @@ -126,8 +126,11 @@ export const sealTools = [ params: { userIdOrPhone: string; note?: string }, context: ToolContext ) => - api.createDelegationForUser(client, params, (token) => - createSealClientForToken(context.corp, token) + api.createDelegationForUser( + client, + params, + (token) => createSealClientForToken(context.corp, token), + context.corp ) }, {