Add Seal-native enterprise support

This commit is contained in:
gitea committed 2026-07-08 15:22:31 +08:00
1 parent 2611ce4c85
commit 997ece006a
10 files changed
+176 -9

No files matched your search

+91
View File
@@ -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<string, unknown>) {
});
}
async function addSealCorp(input: Record<string, unknown>) {
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<string, string> = {}): 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<Record<string, unknown>> {
const body = await response.text();
if (!response.ok) {
@@ -931,6 +1016,11 @@ function optionalString(input: Record<string, unknown>, key: string): string | u
return typeof value === "string" && value.trim() ? value.trim() : undefined;
}
function optionalNumber(input: Record<string, unknown>, 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 <corpId>
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 <corpId>]
seal-home auth hose-link [--corp <corpId>] [--expire 7200]
seal-home source config [--corp <corpId>]
+7 -4
View File
@@ -2,23 +2,26 @@ import { CorpConfig } from "../config/types.js";
import { SealSession } from "./types.js";
export async function getDirectSealSession(corp: CorpConfig): Promise<SealSession> {
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
}
}
};
+1
View File
@@ -15,6 +15,7 @@ async function refreshSealSession(corp: CorpConfig): Promise<SealSession> {
case "hose":
return getHoseSealSession(corp);
case "direct":
case "seal":
return getDirectSealSession(corp);
}
}
+21
View File
@@ -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");
});
});
+16 -1
View File
@@ -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<typeof HoseSealSourceSchema>;
export type DirectSealSource = z.infer<typeof DirectSealSourceSchema>;
export type NativeSealSource = z.infer<typeof NativeSealSourceSchema>;
export type SealSource = z.infer<typeof SealSourceSchema>;
export type CorpAuthConfig = z.infer<typeof CorpAuthConfigSchema>;
export type CorpConfig = z.infer<typeof CorpConfigSchema>;