Add stateful and stateless enterprise selection
This commit is contained in:
1 parent
d383b3aad8
commit
0959cef1cc
22 files changed
+609
-187
No files matched your search
+132
-50
@@ -1,11 +1,19 @@
|
||||
#!/usr/bin/env bun
|
||||
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
||||
import { homedir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { getEnterprisesDirCandidates, loadCorpConfigs, resolveWritableEnterprisesDir } from "./core/config/loader.js";
|
||||
import {
|
||||
getEnterprisesDirCandidates,
|
||||
loadCorpConfigs,
|
||||
loadGlobalConfig,
|
||||
resolveGlobalConfigFile,
|
||||
resolveWritableEnterprisesDir,
|
||||
saveGlobalConfig
|
||||
} from "./core/config/loader.js";
|
||||
import { resolveCorpConfig } from "./core/config/manager.js";
|
||||
import { createSealClient } from "./core/http/factory.js";
|
||||
import { resolveLiveSealEnterpriseConfig } from "./domains/seal/source.js";
|
||||
import { getMe } from "./domains/seal/api.js";
|
||||
@@ -19,11 +27,31 @@ import { runServiceCommand, serviceStatus } from "./cli/service.js";
|
||||
|
||||
const APP_ROOT = dirname(dirname(fileURLToPath(import.meta.url)));
|
||||
const STATE_DIR = join(homedir(), ".config", "seal-home");
|
||||
const CURRENT_CORP_FILE = join(STATE_DIR, "current-corp");
|
||||
const RULE_LIST_FULL_COUNT_LIMIT = 100;
|
||||
const RULE_LIST_FULL_BYTES_LIMIT = 120_000;
|
||||
const RULE_SUMMARY_FIELDS = ["id", "status", "strictness", "scope", "createdAt", "updatedAt"];
|
||||
const DEFAULT_OUTPUT_BYTES_LIMIT = 200_000;
|
||||
const KNOWN_AREAS = new Set([
|
||||
"config",
|
||||
"service",
|
||||
"update",
|
||||
"tools",
|
||||
"corps",
|
||||
"tool",
|
||||
"source",
|
||||
"auth",
|
||||
"delegation",
|
||||
"users",
|
||||
"llm",
|
||||
"permissions",
|
||||
"billing",
|
||||
"webhook",
|
||||
"webhooks",
|
||||
"context",
|
||||
"rules",
|
||||
"approval-runs",
|
||||
"simulation"
|
||||
]);
|
||||
|
||||
type ParsedArgs = {
|
||||
command: string[];
|
||||
@@ -59,13 +87,50 @@ async function main() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!KNOWN_AREAS.has(area)) {
|
||||
throw new Error(`Unknown command: ${args.command.join(" ")}. Run 'seal-home help' for available commands.`);
|
||||
}
|
||||
|
||||
if (area === "config" && action === "paths") {
|
||||
printJson({
|
||||
candidates: getEnterprisesDirCandidates()
|
||||
candidates: getEnterprisesDirCandidates(),
|
||||
globalConfigFile: resolveGlobalConfigFile()
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (area === "config" && (action === "get" || action === "status")) {
|
||||
printJson(globalConfigSummary());
|
||||
return;
|
||||
}
|
||||
|
||||
if (area === "config" && action === "mode") {
|
||||
const mode = maybeName ?? stringOption(args.options.mode);
|
||||
if (!mode) {
|
||||
printJson(globalConfigSummary());
|
||||
return;
|
||||
}
|
||||
|
||||
const stateful = parseStateMode(mode);
|
||||
const requestedCorpId = stringOption(args.options.corp);
|
||||
const previousConfig = loadGlobalConfig();
|
||||
const defaultCorpId = stateful
|
||||
? requestedCorpId
|
||||
? resolveCorpConfig(
|
||||
loadCorpConfigs(),
|
||||
{ stateful: true, corpId: requestedCorpId },
|
||||
getEnterprisesDirCandidates()
|
||||
).id
|
||||
: previousConfig.defaultCorpId
|
||||
: undefined;
|
||||
const config = saveGlobalConfig({
|
||||
stateful,
|
||||
...(defaultCorpId ? { defaultCorpId } : {})
|
||||
});
|
||||
printJson(globalConfigSummary(config));
|
||||
return;
|
||||
}
|
||||
|
||||
if (area === "service") {
|
||||
await runServiceCommand(action, { appRoot: APP_ROOT, entryFile: fileURLToPath(import.meta.url), stateDir: STATE_DIR, version: VERSION });
|
||||
return;
|
||||
@@ -84,7 +149,12 @@ async function main() {
|
||||
if (area === "corps" && action === "list") {
|
||||
const corpId = stringOption(args.options.corp);
|
||||
const corps = loadCorpConfigs();
|
||||
const currentCorpId = corpId ?? readCurrentCorpId() ?? corps[0]?.id;
|
||||
const globalConfig = loadGlobalConfig();
|
||||
const currentCorpId = corpId ?? (
|
||||
globalConfig.stateful
|
||||
? globalConfig.defaultCorpId ?? corps[0]?.id
|
||||
: undefined
|
||||
);
|
||||
printJson(corps.map((item) => ({
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
@@ -96,7 +166,7 @@ async function main() {
|
||||
}
|
||||
|
||||
if (area === "corps" && action === "current") {
|
||||
const corp = selectCorp("");
|
||||
const corp = selectCorp(stringOption(args.options.corp));
|
||||
printJson({
|
||||
id: corp.id,
|
||||
name: corp.name,
|
||||
@@ -109,9 +179,15 @@ async function main() {
|
||||
if (area === "corps" && action === "switch") {
|
||||
const corpId = maybeName;
|
||||
if (!corpId) throw new Error("Usage: seal-home corps switch <corpId>");
|
||||
const globalConfig = loadGlobalConfig();
|
||||
if (!globalConfig.stateful) {
|
||||
throw new Error(
|
||||
"Cannot switch the default enterprise while stateless mode is enabled. " +
|
||||
"Use `seal-home config mode stateful` first."
|
||||
);
|
||||
}
|
||||
const corp = selectCorp(corpId);
|
||||
ensureStateDir();
|
||||
writeFileSync(CURRENT_CORP_FILE, `${corp.id}\n`, { mode: 0o600 });
|
||||
saveGlobalConfig({ ...globalConfig, stateful: true, defaultCorpId: corp.id });
|
||||
printJson({
|
||||
current: true,
|
||||
id: corp.id,
|
||||
@@ -130,7 +206,7 @@ async function main() {
|
||||
return;
|
||||
}
|
||||
|
||||
const corp = selectCorp(String(args.options.corp ?? ""));
|
||||
const corp = selectCorp(stringOption(args.options.corp));
|
||||
|
||||
if (area === "source" && action === "config") {
|
||||
printJson(await resolveLiveSealEnterpriseConfig(corp));
|
||||
@@ -666,28 +742,36 @@ function outputOptions(options: Record<string, string | boolean>): OutputOptions
|
||||
};
|
||||
}
|
||||
|
||||
function selectCorp(corpId: string): CorpConfig {
|
||||
function selectCorp(corpId?: string): CorpConfig {
|
||||
const corps = loadCorpConfigs();
|
||||
const selectedCorpId = corpId || readCurrentCorpId() || "";
|
||||
const corp = selectedCorpId
|
||||
? corps.find((item) => item.id === selectedCorpId)
|
||||
: corps[0];
|
||||
|
||||
if (!corp) {
|
||||
throw new Error(
|
||||
selectedCorpId
|
||||
? `No enterprise config found for ${selectedCorpId}`
|
||||
: `No enterprise config found. Add a non-example JSON file under one of: ${getEnterprisesDirCandidates().join(", ")}`
|
||||
);
|
||||
}
|
||||
|
||||
return corp;
|
||||
const globalConfig = loadGlobalConfig();
|
||||
return resolveCorpConfig(
|
||||
corps,
|
||||
{
|
||||
stateful: globalConfig.stateful,
|
||||
defaultCorpId: globalConfig.defaultCorpId,
|
||||
corpId
|
||||
},
|
||||
getEnterprisesDirCandidates()
|
||||
);
|
||||
}
|
||||
|
||||
function readCurrentCorpId(): string | undefined {
|
||||
if (!existsSync(CURRENT_CORP_FILE)) return undefined;
|
||||
const value = readFileSync(CURRENT_CORP_FILE, "utf-8").trim();
|
||||
return value || undefined;
|
||||
function globalConfigSummary(config = loadGlobalConfig()) {
|
||||
return {
|
||||
mode: config.stateful ? "stateful" : "stateless",
|
||||
stateful: config.stateful,
|
||||
...(config.stateful && config.defaultCorpId
|
||||
? { defaultCorpId: config.defaultCorpId }
|
||||
: {}),
|
||||
configFile: resolveGlobalConfigFile()
|
||||
};
|
||||
}
|
||||
|
||||
function parseStateMode(value: string): boolean {
|
||||
const normalized = value.trim().toLowerCase();
|
||||
if (["stateful", "state", "有状态"].includes(normalized)) return true;
|
||||
if (["stateless", "无状态"].includes(normalized)) return false;
|
||||
throw new Error("Invalid config mode. Use stateful or stateless.");
|
||||
}
|
||||
|
||||
async function addHoseCorp(input: Record<string, unknown>) {
|
||||
@@ -1321,10 +1405,6 @@ function booleanOption(value: string | boolean | undefined): boolean | undefined
|
||||
throw new Error(`Invalid boolean: ${value}`);
|
||||
}
|
||||
|
||||
function ensureStateDir() {
|
||||
mkdirSync(STATE_DIR, { recursive: true });
|
||||
}
|
||||
|
||||
function printJson(value: unknown, options: OutputOptions = {}) {
|
||||
protectLargeOutput(value, options);
|
||||
const json = `${JSON.stringify(value, null, 2)}\n`;
|
||||
@@ -1342,11 +1422,13 @@ function printHelp() {
|
||||
Usage:
|
||||
seal-home version
|
||||
seal-home config paths
|
||||
seal-home config get
|
||||
seal-home config mode <stateful|stateless> [--corp <corpId>]
|
||||
seal-home service <start|stop|restart|status>
|
||||
seal-home update
|
||||
seal-home tools list
|
||||
seal-home corps list [--corp <corpId>]
|
||||
seal-home corps current
|
||||
seal-home corps current [--corp <corpId>]
|
||||
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"}'
|
||||
@@ -1380,23 +1462,23 @@ Usage:
|
||||
seal-home rules clear [--corp <corpId>] [--dry-run|--yes]
|
||||
seal-home rules versions [--corp <corpId>]
|
||||
seal-home rules version <versionId|versionNumber|latest> [--corp <corpId>]
|
||||
seal-home rules get --version <number|latest> --code '#0038'
|
||||
seal-home rules get --record-id <recordId> --runtime-id rule-205
|
||||
seal-home rules search <keyword> [--version <versionId|versionNumber|latest>] [--maxResults 20]
|
||||
seal-home context [--ruleVersion <versionId|versionNumber|latest>] [--documentLimit 50]
|
||||
seal-home approval-runs summary [--date YYYY-MM-DD] [--timezone Asia/Shanghai]
|
||||
seal-home approval-runs search [--query text] [--humanResult 驳回] [--manualApprovalStatus TERMINATED] [--startDate ms] [--endDate ms] [--limit 20] [--includeBridge true]
|
||||
seal-home approval-runs pick --sn <documentSN> [--batch <batchId>] [--latest] [--fields recordId,status,aiDecision]
|
||||
seal-home approval-runs get <recordId> [--summary] [--fields metadata|document.fields,result.summary] [--full] [--output-file file.json]
|
||||
seal-home approval-runs cited-rules <recordId>
|
||||
seal-home approval-runs document-summary <recordId>
|
||||
seal-home approval-runs attachments <recordId> [--summary]
|
||||
seal-home approval-runs result <recordId> [--summary]
|
||||
seal-home approval-runs url
|
||||
seal-home approval-runs url <recordId>
|
||||
seal-home approval-runs url --sourceDocumentSN B26022501
|
||||
seal-home approval-runs bridge [--sourceDocumentSN B26001887]
|
||||
seal-home simulation batch-records <batchId>
|
||||
seal-home rules get --corp <corpId> --version <number|latest> --code '#0038'
|
||||
seal-home rules get --corp <corpId> --record-id <recordId> --runtime-id rule-205
|
||||
seal-home rules search <keyword> [--version <versionId|versionNumber|latest>] [--maxResults 20] [--corp <corpId>]
|
||||
seal-home context [--ruleVersion <versionId|versionNumber|latest>] [--documentLimit 50] [--corp <corpId>]
|
||||
seal-home approval-runs summary [--date YYYY-MM-DD] [--timezone Asia/Shanghai] [--corp <corpId>]
|
||||
seal-home approval-runs search [--query text] [--humanResult 驳回] [--manualApprovalStatus TERMINATED] [--startDate ms] [--endDate ms] [--limit 20] [--includeBridge true] [--corp <corpId>]
|
||||
seal-home approval-runs pick --sn <documentSN> [--batch <batchId>] [--latest] [--fields recordId,status,aiDecision] [--corp <corpId>]
|
||||
seal-home approval-runs get <recordId> [--summary] [--fields metadata|document.fields,result.summary] [--full] [--output-file file.json] [--corp <corpId>]
|
||||
seal-home approval-runs cited-rules <recordId> [--corp <corpId>]
|
||||
seal-home approval-runs document-summary <recordId> [--corp <corpId>]
|
||||
seal-home approval-runs attachments <recordId> [--summary] [--corp <corpId>]
|
||||
seal-home approval-runs result <recordId> [--summary] [--corp <corpId>]
|
||||
seal-home approval-runs url [--corp <corpId>]
|
||||
seal-home approval-runs url <recordId> [--corp <corpId>]
|
||||
seal-home approval-runs url --sourceDocumentSN B26022501 [--corp <corpId>]
|
||||
seal-home approval-runs bridge [--sourceDocumentSN B26001887] [--corp <corpId>]
|
||||
seal-home simulation batch-records <batchId> [--corp <corpId>]
|
||||
`);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user