Initial commit of eigent-main

This commit is contained in:
puzhen 2025-08-12 01:16:39 +02:00
commit 723df5a03e
1144 changed files with 103478 additions and 0 deletions

View file

@ -0,0 +1,135 @@
// src/config/format.ts
import { StackAssertionError, throwErr } from "../utils/errors";
import { deleteKey, filterUndefined, get, hasAndNotUndefined, set } from "../utils/objects";
function isValidConfig(c) {
return getInvalidConfigReason(c) === void 0;
}
function getInvalidConfigReason(c, options = {}) {
const configName = options.configName ?? "config";
if (c === null || typeof c !== "object") return `${configName} must be a non-null object`;
for (const [key, value] of Object.entries(c)) {
if (value === void 0) continue;
if (typeof key !== "string") return `${configName} must have only string keys (found: ${typeof key})`;
if (!key.match(/^[a-zA-Z0-9_:$][a-zA-Z_:$0-9\-]*(?:\.[a-zA-Z0-9_:$][a-zA-Z_:$0-9\-]*)*$/)) return `All keys of ${configName} must consist of only alphanumeric characters, dots, underscores, colons, dollar signs, or hyphens and start with a character other than a hyphen (found: ${key})`;
const entryName = `${configName}.${key}`;
const reason = getInvalidConfigValueReason(value, { valueName: entryName });
if (reason) return reason;
}
return void 0;
}
function getInvalidConfigValueReason(value, options = {}) {
const valueName = options.valueName ?? "value";
switch (typeof value) {
case "string":
case "number":
case "boolean": {
break;
}
case "object": {
if (value === null) {
break;
} else if (Array.isArray(value)) {
for (const [index, v] of value.entries()) {
const reason = getInvalidConfigValueReason(v, { valueName: `${valueName}[${index}]` });
if (reason) return reason;
}
} else {
const reason = getInvalidConfigReason(value, { configName: valueName });
if (reason) return reason;
}
break;
}
default: {
return `${valueName} has an invalid value type ${typeof value} (value: ${value})`;
}
}
return void 0;
}
function assertValidConfig(c) {
const reason = getInvalidConfigReason(c);
if (reason) throw new StackAssertionError(`Invalid config: ${reason}`, { c });
}
function override(c1, ...configs) {
if (configs.length === 0) return c1;
if (configs.length > 1) return override(override(c1, configs[0]), ...configs.slice(1));
const c2 = configs[0];
assertValidConfig(c1);
assertValidConfig(c2);
let result = c1;
for (const key of Object.keys(filterUndefined(c2))) {
result = Object.fromEntries(
Object.entries(result).filter(([k]) => k !== key && !k.startsWith(key + "."))
);
}
return {
...result,
...filterUndefined(c2)
};
}
var NormalizationError = class extends Error {
constructor(...args) {
super(...args);
}
};
NormalizationError.prototype.name = "NormalizationError";
function normalize(c, options = {}) {
assertValidConfig(c);
const onDotIntoNull = options.onDotIntoNull ?? "empty";
const countDots = (s) => s.match(/\./g)?.length ?? 0;
const result = {};
const keysByDepth = Object.keys(c).sort((a, b) => countDots(a) - countDots(b));
outer: for (const key of keysByDepth) {
const keySegmentsWithoutLast = key.split(".");
const last = keySegmentsWithoutLast.pop() ?? throwErr("split returns empty array?");
const value = get(c, key);
if (value === void 0) continue;
let current = result;
for (const keySegment of keySegmentsWithoutLast) {
if (!hasAndNotUndefined(current, keySegment)) {
switch (onDotIntoNull) {
case "empty": {
set(current, keySegment, {});
break;
}
case "throw": {
throw new NormalizationError(`Tried to use dot notation to access ${JSON.stringify(key)}, but ${JSON.stringify(keySegment)} doesn't exist on the object (or is null). Maybe this config is not normalizable?`);
}
case "ignore": {
continue outer;
}
}
}
const value2 = get(current, keySegment);
if (typeof value2 !== "object") {
throw new NormalizationError(`Tried to use dot notation to access ${JSON.stringify(key)}, but ${JSON.stringify(keySegment)} is not an object. Maybe this config is not normalizable?`);
}
current = value2;
}
setNormalizedValue(current, last, value);
}
return result;
}
function normalizeValue(value) {
if (value === null) throw new NormalizationError("Tried to normalize a null value");
if (Array.isArray(value)) return value.map(normalizeValue);
if (typeof value === "object") return normalize(value);
return value;
}
function setNormalizedValue(result, key, value) {
if (value === null) {
if (hasAndNotUndefined(result, key)) {
deleteKey(result, key);
}
} else {
set(result, key, normalizeValue(value));
}
}
export {
NormalizationError,
assertValidConfig,
getInvalidConfigReason,
isValidConfig,
normalize,
override
};
//# sourceMappingURL=format.js.map

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,201 @@
// src/config/schema.ts
import * as schemaFields from "../schema-fields";
import { yupBoolean, yupObject, yupRecord, yupString } from "../schema-fields";
import { allProviders } from "../utils/oauth";
import { get, has, isObjectLike, mapValues, set } from "../utils/objects";
var configLevels = ["project", "branch", "environment", "organization"];
var permissionRegex = /^\$?[a-z0-9_:]+$/;
var customPermissionRegex = /^[a-z0-9_:]+$/;
var projectConfigSchema = yupObject({});
var branchRbacDefaultPermissions = yupRecord(
yupString().optional().matches(permissionRegex),
yupBoolean().isTrue().optional()
).optional();
var branchRbacSchema = yupObject({
permissions: yupRecord(
yupString().optional().matches(customPermissionRegex),
yupObject({
description: yupString().optional(),
scope: yupString().oneOf(["team", "project"]).optional(),
containedPermissionIds: yupRecord(
yupString().optional().matches(permissionRegex),
yupBoolean().isTrue().optional()
).optional()
}).optional()
).optional(),
defaultPermissions: yupObject({
teamCreator: branchRbacDefaultPermissions,
teamMember: branchRbacDefaultPermissions,
signUp: branchRbacDefaultPermissions
}).optional()
}).optional();
var branchApiKeysSchema = yupObject({
enabled: yupObject({
team: yupBoolean().optional(),
user: yupBoolean().optional()
}).optional()
}).optional();
var branchAuthSchema = yupObject({
allowSignUp: yupBoolean().optional(),
password: yupObject({
allowSignIn: yupBoolean().optional()
}).optional(),
otp: yupObject({
allowSignIn: yupBoolean().optional()
}).optional(),
passkey: yupObject({
allowSignIn: yupBoolean().optional()
}).optional(),
oauth: yupObject({
accountMergeStrategy: yupString().oneOf(["link_method", "raise_error", "allow_duplicates"]).optional(),
providers: yupRecord(
yupString().optional().matches(permissionRegex),
yupObject({
type: yupString().oneOf(allProviders).optional(),
allowSignIn: yupBoolean().optional(),
allowConnectedAccounts: yupBoolean().optional()
}).defined()
).optional()
}).optional()
}).optional();
var branchDomain = yupObject({
allowLocalhost: yupBoolean().optional()
}).optional();
var branchConfigSchema = projectConfigSchema.concat(yupObject({
rbac: branchRbacSchema,
teams: yupObject({
createPersonalTeamOnSignUp: yupBoolean().optional(),
allowClientTeamCreation: yupBoolean().optional()
}).optional(),
users: yupObject({
allowClientUserDeletion: yupBoolean().optional()
}).optional(),
apiKeys: branchApiKeysSchema,
domains: branchDomain,
auth: branchAuthSchema,
emails: yupObject({})
}));
var environmentConfigSchema = branchConfigSchema.concat(yupObject({
auth: branchConfigSchema.getNested("auth").concat(yupObject({
oauth: branchConfigSchema.getNested("auth").getNested("oauth").concat(yupObject({
providers: yupRecord(
yupString().optional().matches(permissionRegex),
yupObject({
type: yupString().oneOf(allProviders).optional(),
isShared: yupBoolean().optional(),
clientId: schemaFields.oauthClientIdSchema.optional(),
clientSecret: schemaFields.oauthClientSecretSchema.optional(),
facebookConfigId: schemaFields.oauthFacebookConfigIdSchema.optional(),
microsoftTenantId: schemaFields.oauthMicrosoftTenantIdSchema.optional(),
allowSignIn: yupBoolean().optional(),
allowConnectedAccounts: yupBoolean().optional()
})
).optional()
}).optional())
})),
emails: branchConfigSchema.getNested("emails").concat(yupObject({
server: yupObject({
isShared: yupBoolean().optional(),
host: schemaFields.emailHostSchema.optional().nonEmpty(),
port: schemaFields.emailPortSchema.optional(),
username: schemaFields.emailUsernameSchema.optional().nonEmpty(),
password: schemaFields.emailPasswordSchema.optional().nonEmpty(),
senderName: schemaFields.emailSenderNameSchema.optional().nonEmpty(),
senderEmail: schemaFields.emailSenderEmailSchema.optional().nonEmpty()
})
}).optional()),
domains: branchConfigSchema.getNested("domains").concat(yupObject({
trustedDomains: yupRecord(
yupString().uuid().optional(),
yupObject({
baseUrl: schemaFields.urlSchema.optional(),
handlerPath: schemaFields.handlerPathSchema.optional()
})
).optional()
}))
}));
var organizationConfigSchema = environmentConfigSchema.concat(yupObject({}));
var projectConfigDefaults = {};
var branchConfigDefaults = {};
var environmentConfigDefaults = {};
var organizationConfigDefaults = {
rbac: {
permissions: (key) => ({}),
defaultPermissions: {
teamCreator: {},
teamMember: {},
signUp: {}
}
},
apiKeys: {
enabled: {
team: false,
user: false
}
},
teams: {
createPersonalTeamOnSignUp: false,
allowClientTeamCreation: false
},
users: {
allowClientUserDeletion: false
},
domains: {
allowLocalhost: false,
trustedDomains: (key) => ({
handlerPath: "/handler"
})
},
auth: {
allowSignUp: true,
password: {
allowSignIn: false
},
otp: {
allowSignIn: false
},
passkey: {
allowSignIn: false
},
oauth: {
accountMergeStrategy: "link_method",
providers: (key) => ({
isShared: true,
allowSignIn: false,
allowConnectedAccounts: false
})
}
},
emails: {
server: {
isShared: true
}
}
};
function applyDefaults(defaults, config) {
const res = typeof defaults === "function" ? {} : mapValues(defaults, (v) => typeof v === "function" ? {} : typeof v === "object" ? applyDefaults(v, {}) : v);
for (const [key, mergeValue] of Object.entries(config)) {
const baseValue = typeof defaults === "function" ? defaults(key) : has(defaults, key) ? get(defaults, key) : void 0;
if (baseValue !== void 0) {
if (isObjectLike(baseValue) && isObjectLike(mergeValue)) {
set(res, key, applyDefaults(baseValue, mergeValue));
continue;
}
}
set(res, key, mergeValue);
}
return res;
}
export {
applyDefaults,
branchConfigDefaults,
branchConfigSchema,
configLevels,
environmentConfigDefaults,
environmentConfigSchema,
organizationConfigDefaults,
organizationConfigSchema,
projectConfigDefaults,
projectConfigSchema
};
//# sourceMappingURL=schema.js.map

File diff suppressed because one or more lines are too long