diff --git a/README.md b/README.md index 7e97f55..8651b8e 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Bun + TypeScript CLI and optional local service for pulling Seal enterprise appr The first provider is Hose/合思: `key`, `password`, `corpId`, `staffId`, and an optional Hose domain. The provider exchanges those credentials for a Seal enterprise session, then the Seal domain tools can read and maintain approval rules, approval documents, and approval style preferences. -`direct` is also available for later sources that already provide a Seal Bearer token. +Seal-native enterprise pages can be added with a Seal Bearer token. The older `direct` source remains available for existing configs that already provide a Seal Bearer token. ## Setup @@ -16,7 +16,7 @@ bun run probe bun run start ``` -For an already-authenticated Seal source, copy `enterprises/example.direct.json` instead. +For an already-authenticated Seal-native source, use `seal-home corps add-seal` or copy `enterprises/example.seal.json`. Existing `direct` configs remain supported via `enterprises/example.direct.json`. ## Public Usage @@ -43,6 +43,15 @@ seal-home tools list `corps add-hose` verifies by default before writing the config. It uses the Hose OpenAPI credential response to derive the corporation ID, then checks provisional auth, Seal SSO, and Seal `whoami`. Pass `"force":true` or `"verify":false` only when you intentionally want to write a config that may fail later. +Create a user-level Seal-native enterprise config from an already signed-in Seal Home page: + +```bash +seal-home corps add-seal --json '{"sealUrl":"https://tenant.staging.sealai.cc","token":"seal-bearer-token"}' +seal-home auth diagnose --corp tenant +``` + +`corps add-seal` verifies `/api/v1/auth/me` by default and writes the tenant slug, tenant ID, tenant name, and provider user ID into local config metadata. Pass `"force":true` or `"verify":false` only when writing an offline config intentionally. + For custom config locations, set `SEAL_HOME_ENTERPRISES_DIR` to a directory containing non-example `*.json` enterprise config files. Update an existing checkout: diff --git a/enterprises/example.seal.json b/enterprises/example.seal.json new file mode 100644 index 0000000..ed496f7 --- /dev/null +++ b/enterprises/example.seal.json @@ -0,0 +1,26 @@ +{ + "id": "tenant-slug", + "name": "Seal 原生企业", + "seal": { + "url": "https://tenant.sealai.cc", + "tenantSlug": "tenant-slug", + "endpoints": { + "approvalStylePreferences": "api/v1/agent/ai-approval/config" + } + }, + "source": { + "type": "seal", + "token": "seal-bearer-token", + "sealUrl": "https://tenant.sealai.cc", + "expiresIn": 2592000, + "staffId": "seal-user-id", + "corpId": "tenant-slug", + "tenantId": "seal-tenant-id", + "tenantSlug": "tenant-slug", + "tenantName": "Seal 原生企业", + "providerUserId": "provider-user-id" + }, + "auth": { + "refreshTtl": 300 + } +} diff --git a/package.json b/package.json index f0cb0b2..19851c2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "seal-home", - "version": "0.3.3", + "version": "0.3.5", "type": "module", "module": "src/server.ts", "bin": { diff --git a/skills/seal-home/SKILL.md b/skills/seal-home/SKILL.md index 092bfe1..55f6f15 100644 --- a/skills/seal-home/SKILL.md +++ b/skills/seal-home/SKILL.md @@ -27,7 +27,7 @@ seal-home tools list ## Route By Intent - Current tenant or identity: `seal-home tool seal_whoami`. -- Enterprise list/current/switch/add: `seal-home corps ...`; for Hose setup read `references/hose-enterprise-auth.md`. +- Enterprise list/current/switch/add: `seal-home corps ...`; use `corps add-seal` for Seal-native tenants and `corps add-hose` for Hose setup; for Hose setup read `references/hose-enterprise-auth.md`. - Hose login/SSO/auth failures: `seal-home auth diagnose --corp `. - User-facing 合思 authorization link: `seal-home auth hose-link --corp --expire 7200`; read `references/hose-enterprise-auth.md`. - One approval document by 单号/SN: use `seal-home approval-runs pick --sn <单号> --latest`; read `references/approval-run-lightweight.md`. diff --git a/skills/seal-home/references/cli.md b/skills/seal-home/references/cli.md index 9dafc4f..26e06fd 100644 --- a/skills/seal-home/references/cli.md +++ b/skills/seal-home/references/cli.md @@ -24,6 +24,7 @@ seal-home corps list [--corp ] seal-home corps current seal-home corps switch seal-home corps add-hose --json '{"name":"企业名称","domain":"https://app.ekuaibao.com","appKey":"...","appSecurity":"...","proxyStaffBizId":"corpId:staffId"}' +seal-home corps add-seal --json '{"sealUrl":"https://tenant.sealai.cc","token":"seal-bearer-token"}' seal-home auth diagnose [--corp ] seal-home auth hose-link [--corp ] [--expire 7200] seal-home source config [--corp ] diff --git a/src/cli.ts b/src/cli.ts index cbd27f5..7b0684a 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -117,6 +117,11 @@ async function main() { return; } + if (area === "corps" && action === "add-seal") { + await addSealCorp(jsonObject(args.json)); + return; + } + const corp = selectCorp(String(args.options.corp ?? "")); if (area === "source" && action === "config") { @@ -655,6 +660,75 @@ async function addHoseCorp(input: Record) { }); } +async function addSealCorp(input: Record) { + const token = requiredString(input, "token"); + const sealUrl = (optionalString(input, "sealUrl") ?? optionalString(input, "url") ?? "").replace(/\/+$/, ""); + if (!sealUrl) throw new Error("Missing required JSON field: sealUrl"); + + const expiresIn = optionalNumber(input, "expiresIn") ?? 30 * 24 * 60 * 60; + const verify = input.force === true || input.verify === false ? false : true; + const remote = verify ? await resolveSealWhoami(sealUrl, token) : undefined; + const remoteUser = remote?.user; + const remoteTenant = remote?.tenant; + const tenantSlug = optionalString(input, "tenantSlug") ?? stringValue(remoteTenant?.tenantSlug); + const tenantId = optionalString(input, "tenantId") ?? stringValue(remoteTenant?.id); + const tenantName = optionalString(input, "tenantName") ?? + optionalString(input, "name") ?? + stringValue(remoteTenant?.name); + const id = optionalString(input, "id") ?? tenantSlug ?? tenantId; + if (!id) { + throw new Error("Missing required JSON field: id. Seal whoami did not return tenantSlug or tenantId."); + } + const staffId = optionalString(input, "staffId") ?? + stringValue(remoteUser?.providerUserId) ?? + stringValue(remoteUser?.id) ?? + "seal"; + + const config: CorpConfig = { + id, + name: tenantName ?? id, + seal: { + url: sealUrl, + tenantSlug, + endpoints: { + approvalStylePreferences: "api/v1/agent/ai-approval/config" + } + }, + source: { + type: "seal", + token, + sealUrl, + expiresIn, + staffId, + corpId: tenantSlug ?? tenantId ?? id, + tenantId, + tenantSlug, + tenantName, + providerUserId: stringValue(remoteUser?.providerUserId) + }, + auth: { + refreshTtl: 300 + } + }; + + const enterprisesDir = resolveWritableEnterprisesDir(); + mkdirSync(enterprisesDir, { recursive: true, mode: 0o700 }); + const file = join(enterprisesDir, `${safeFileName(id)}.json`); + if (existsSync(file) && input.overwrite !== true) { + throw new Error(`Enterprise config already exists: ${file}. Pass "overwrite": true to replace it.`); + } + + writeFileSync(file, `${JSON.stringify(config, null, 2)}\n`, { mode: 0o600 }); + printJson({ + id, + name: config.name, + tenantId, + tenantSlug, + verified: verify, + path: file + }); +} + async function diagnoseAuth(corp: CorpConfig) { if (corp.source.type !== "hose") { const client = await createSealClient(corp); @@ -871,6 +945,17 @@ async function getJson(url: string, headers: Record = {}): Promi return parseJsonResponse(response); } +async function resolveSealWhoami(sealUrl: string, token: string) { + const json = await getJson(`${sealUrl}/api/v1/auth/me`, { + authorization: `Bearer ${token}` + }); + const data = objectValue(json.data); + return { + user: objectValue(data.user ?? json.user), + tenant: objectValue(data.tenant ?? json.tenant) + }; +} + async function parseJsonResponse(response: Response): Promise> { const body = await response.text(); if (!response.ok) { @@ -931,6 +1016,11 @@ function optionalString(input: Record, key: string): string | u return typeof value === "string" && value.trim() ? value.trim() : undefined; } +function optionalNumber(input: Record, key: string): number | undefined { + const value = input[key]; + return typeof value === "number" && Number.isFinite(value) ? value : undefined; +} + function inferCorpIdFromStaffId(staffId: string): string | undefined { const [corpId] = staffId.split(":", 1); return corpId || undefined; @@ -1145,6 +1235,7 @@ Usage: seal-home corps current seal-home corps switch seal-home corps add-hose --json '{"name":"企业名称","domain":"https://app.ekuaibao.com","appKey":"...","appSecurity":"...","proxyStaffBizId":"corpId:staffId"}' + seal-home corps add-seal --json '{"sealUrl":"https://tenant.sealai.cc","token":"seal-bearer-token"}' seal-home auth diagnose [--corp ] seal-home auth hose-link [--corp ] [--expire 7200] seal-home source config [--corp ] diff --git a/src/core/auth/direct.ts b/src/core/auth/direct.ts index 3a9c4b4..2719cc1 100644 --- a/src/core/auth/direct.ts +++ b/src/core/auth/direct.ts @@ -2,23 +2,26 @@ import { CorpConfig } from "../config/types.js"; import { SealSession } from "./types.js"; export async function getDirectSealSession(corp: CorpConfig): Promise { - if (corp.source.type !== "direct") { + if (corp.source.type !== "direct" && corp.source.type !== "seal") { throw new Error(`Unsupported Seal source: ${corp.source.type}`); } const sealUrl = corp.seal.url ?? corp.source.sealUrl; + const nativeSource = corp.source.type === "seal" ? corp.source : undefined; return { token: corp.source.token, expiresAt: Date.now() + corp.source.expiresIn * 1000, enterprise: { - provider: "direct", + provider: nativeSource ? "seal" : "direct", corpId: corp.source.corpId ?? corp.id, staffId: corp.source.staffId, sealUrl, - tenantSlug: corp.seal.tenantSlug, + tenantId: nativeSource?.tenantId, + tenantSlug: corp.seal.tenantSlug ?? nativeSource?.tenantSlug, + tenantName: nativeSource?.tenantName, raw: { - sourceType: "direct" + sourceType: corp.source.type } } }; diff --git a/src/core/auth/seal.ts b/src/core/auth/seal.ts index 1de2d7b..36040db 100644 --- a/src/core/auth/seal.ts +++ b/src/core/auth/seal.ts @@ -15,6 +15,7 @@ async function refreshSealSession(corp: CorpConfig): Promise { case "hose": return getHoseSealSession(corp); case "direct": + case "seal": return getDirectSealSession(corp); } } diff --git a/src/core/config/types.test.ts b/src/core/config/types.test.ts index 9879248..e75f05b 100644 --- a/src/core/config/types.test.ts +++ b/src/core/config/types.test.ts @@ -57,4 +57,25 @@ describe("CorpConfigSchema", () => { } expect(config.source.expiresIn).toBe(3600); }); + + test("accepts native Seal bearer source", () => { + const config = CorpConfigSchema.parse({ + id: "xiezihao", + name: "谢子豪的测试环境", + source: { + type: "seal", + token: "seal-token", + sealUrl: "https://xiezihao.staging.sealai.cc", + tenantSlug: "xiezihao", + tenantId: "tenant-id" + } + }); + + expect(config.source.type).toBe("seal"); + if (config.source.type !== "seal") { + throw new Error("expected seal source"); + } + expect(config.source.staffId).toBe("seal"); + expect(config.source.tenantSlug).toBe("xiezihao"); + }); }); diff --git a/src/core/config/types.ts b/src/core/config/types.ts index b38c384..8cd2c4e 100644 --- a/src/core/config/types.ts +++ b/src/core/config/types.ts @@ -37,9 +37,23 @@ export const DirectSealSourceSchema = z.object({ corpId: z.string().optional() }); +export const NativeSealSourceSchema = z.object({ + type: z.literal("seal"), + token: z.string(), + sealUrl: z.string().url(), + expiresIn: z.number().int().positive().default(3600), + staffId: z.string().default("seal"), + corpId: z.string().optional(), + tenantId: z.string().optional(), + tenantSlug: z.string().optional(), + tenantName: z.string().optional(), + providerUserId: z.string().optional() +}); + export const SealSourceSchema = z.discriminatedUnion("type", [ HoseSealSourceSchema, - DirectSealSourceSchema + DirectSealSourceSchema, + NativeSealSourceSchema ]); export const SealEndpointSchema = z.object({ @@ -70,6 +84,7 @@ export const CorpConfigSchema = z.object({ export type HoseSealSource = z.infer; export type DirectSealSource = z.infer; +export type NativeSealSource = z.infer; export type SealSource = z.infer; export type CorpAuthConfig = z.infer; export type CorpConfig = z.infer;