Files
FlowPilot/shared/flow-registry.js
T
2026-05-18 20:34:55 +08:00

536 lines
17 KiB
JavaScript

(function attachMultiPageFlowRegistry(root, factory) {
root.MultiPageFlowRegistry = factory();
})(typeof self !== 'undefined' ? self : globalThis, function createFlowRegistryModule() {
const DEFAULT_FLOW_ID = 'openai';
const DEFAULT_OPENAI_INTEGRATION_TARGET_ID = 'cpa';
const DEFAULT_KIRO_INTEGRATION_TARGET_ID = 'kiro-rs';
const DEFAULT_KIRO_PUBLICATION_TARGET_ID = 'kiro-rs';
const DEFAULT_KIRO_RS_URL = 'https://kiro.leftcode.xyz/admin';
const OPENAI_INTEGRATION_TARGET_IDS = Object.freeze(['cpa', 'sub2api', 'codex2api']);
const SHARED_SERVICE_IDS = Object.freeze(['account', 'email', 'proxy']);
const DEFAULT_FLOW_CAPABILITIES = Object.freeze({
supportsEmailSignup: true,
supportsPhoneSignup: false,
supportsPhoneVerificationSettings: false,
supportsPlusMode: false,
supportsContributionMode: false,
supportedIntegrationTargets: [],
supportsLuckmail: false,
supportsOauthTimeoutBudget: false,
canSwitchFlow: true,
stepDefinitionMode: 'default',
sourceSelectorLabel: '来源',
});
function freezeDeep(value) {
if (!value || typeof value !== 'object' || Object.isFrozen(value)) {
return value;
}
Object.getOwnPropertyNames(value).forEach((key) => {
freezeDeep(value[key]);
});
return Object.freeze(value);
}
const FLOW_DEFINITIONS = freezeDeep({
openai: {
id: 'openai',
label: 'Codex / OpenAI',
services: ['account', 'email', 'proxy'],
capabilities: {
...DEFAULT_FLOW_CAPABILITIES,
supportsPhoneSignup: true,
supportsPhoneVerificationSettings: true,
supportsPlusMode: true,
supportsContributionMode: true,
supportedIntegrationTargets: [...OPENAI_INTEGRATION_TARGET_IDS],
supportsLuckmail: true,
supportsOauthTimeoutBudget: true,
stepDefinitionMode: 'openai-dynamic',
},
baseGroups: [
'openai-plus',
'openai-phone',
'openai-oauth',
'openai-step6',
],
integrationTargets: {
cpa: {
id: 'cpa',
label: 'CPA 面板',
groups: ['openai-target-cpa'],
},
sub2api: {
id: 'sub2api',
label: 'SUB2API',
groups: ['openai-target-sub2api'],
},
codex2api: {
id: 'codex2api',
label: 'Codex2API',
groups: ['openai-target-codex2api'],
},
},
runtimeSources: {
'openai-auth': {
flowId: 'openai',
kind: 'flow-page',
label: '认证页',
readyPolicy: 'allow-child-frame',
family: 'openai-auth-family',
driverId: 'content/signup-page',
cleanupScopes: ['oauth-localhost-callback'],
},
chatgpt: {
flowId: 'openai',
kind: 'flow-entry',
label: 'ChatGPT 首页',
readyPolicy: 'allow-child-frame',
family: 'chatgpt-entry-family',
driverId: null,
cleanupScopes: [],
},
'vps-panel': {
flowId: 'openai',
kind: 'panel-page',
label: 'CPA 面板',
readyPolicy: 'allow-child-frame',
family: 'vps-panel-family',
driverId: 'content/vps-panel',
cleanupScopes: [],
},
'platform-panel': {
flowId: 'openai',
kind: 'virtual-page',
label: '平台回调面板',
readyPolicy: 'disabled',
family: 'platform-panel-family',
driverId: 'content/platform-panel',
cleanupScopes: [],
},
'sub2api-panel': {
flowId: 'openai',
kind: 'panel-page',
label: 'SUB2API 后台',
readyPolicy: 'allow-child-frame',
family: 'sub2api-panel-family',
driverId: 'content/sub2api-panel',
cleanupScopes: [],
},
'codex2api-panel': {
flowId: 'openai',
kind: 'panel-page',
label: 'Codex2API 后台',
readyPolicy: 'allow-child-frame',
family: 'codex2api-panel-family',
driverId: 'content/sub2api-panel',
cleanupScopes: [],
},
'plus-checkout': {
flowId: 'openai',
kind: 'flow-page',
label: 'Plus Checkout',
readyPolicy: 'top-frame-only',
family: 'plus-checkout-family',
driverId: 'content/plus-checkout',
cleanupScopes: [],
},
'paypal-flow': {
flowId: 'openai',
kind: 'flow-page',
label: 'PayPal 授权页',
readyPolicy: 'allow-child-frame',
family: 'paypal-flow-family',
driverId: 'content/paypal-flow',
cleanupScopes: [],
},
'gopay-flow': {
flowId: 'openai',
kind: 'flow-page',
label: 'GoPay 授权页',
readyPolicy: 'allow-child-frame',
family: 'gopay-flow-family',
driverId: 'content/gopay-flow',
cleanupScopes: [],
},
},
driverDefinitions: {
'content/signup-page': {
sourceId: 'openai-auth',
commands: [
'submit-signup-email',
'fill-password',
'fill-profile',
'oauth-login',
'submit-verification-code',
'post-login-phone-verification',
'bind-email',
'fetch-bind-email-code',
'confirm-oauth',
'detect-auth-state',
],
},
'content/sub2api-panel': {
sourceId: 'sub2api-panel',
commands: ['open-panel', 'fetch-oauth-url', 'platform-verify'],
},
'content/vps-panel': {
sourceId: 'vps-panel',
commands: ['open-panel', 'fetch-oauth-url', 'platform-verify'],
},
'content/platform-panel': {
sourceId: 'platform-panel',
commands: ['platform-verify', 'fetch-oauth-url'],
},
'content/plus-checkout': {
sourceId: 'plus-checkout',
commands: ['plus-checkout-create', 'plus-checkout-billing', 'plus-checkout-return'],
},
'content/paypal-flow': {
sourceId: 'paypal-flow',
commands: ['paypal-approve'],
},
'content/gopay-flow': {
sourceId: 'gopay-flow',
commands: ['gopay-subscription-confirm'],
},
},
},
kiro: {
id: 'kiro',
label: 'kiro(暂时无法使用,会立刻封号)',
services: ['account', 'email', 'proxy'],
capabilities: {
...DEFAULT_FLOW_CAPABILITIES,
supportedIntegrationTargets: [DEFAULT_KIRO_INTEGRATION_TARGET_ID],
stepDefinitionMode: 'kiro-device-auth',
},
baseGroups: [
'kiro-runtime-status',
],
integrationTargets: {
'kiro-rs': {
id: 'kiro-rs',
label: 'kiro.rs',
groups: ['kiro-target-kiro-rs'],
},
},
publicationTargets: {
'kiro-rs': {
id: 'kiro-rs',
label: 'kiro.rs',
},
},
runtimeSources: {
'kiro-device-auth': {
flowId: 'kiro',
kind: 'flow-page',
label: 'Kiro 授权页',
readyPolicy: 'top-frame-only',
family: 'kiro-device-auth-family',
driverId: 'content/kiro-device-auth-page',
cleanupScopes: [],
},
'kiro-rs-admin': {
flowId: 'kiro',
kind: 'virtual-page',
label: 'kiro.rs Admin',
readyPolicy: 'disabled',
family: 'kiro-rs-admin-family',
driverId: null,
cleanupScopes: [],
},
},
driverDefinitions: {
'content/kiro-device-auth-page': {
sourceId: 'kiro-device-auth',
commands: [
'kiro-submit-email',
'kiro-submit-name',
'kiro-submit-verification-code',
'kiro-fill-password',
'kiro-confirm-access',
],
},
'background/kiro-device-auth': {
sourceId: 'kiro-device-auth',
commands: [
'kiro-start-device-login',
'kiro-submit-email',
'kiro-submit-name',
'kiro-submit-verification-code',
'kiro-fill-password',
'kiro-confirm-access',
'kiro-upload-credential',
],
},
},
},
});
const SETTINGS_GROUP_DEFINITIONS = freezeDeep({
'service-account': {
id: 'service-account',
label: '账户',
rowIds: ['row-custom-password'],
},
'service-email': {
id: 'service-email',
label: '邮箱服务',
},
'service-proxy': {
id: 'service-proxy',
label: 'IP 代理',
sectionIds: ['ip-proxy-section'],
},
'openai-target-cpa': {
id: 'openai-target-cpa',
label: 'CPA 来源',
rowIds: ['row-vps-url', 'row-vps-password', 'row-local-cpa-step9-mode'],
},
'openai-target-sub2api': {
id: 'openai-target-sub2api',
label: 'SUB2API 来源',
rowIds: [
'row-sub2api-url',
'row-sub2api-email',
'row-sub2api-password',
'row-sub2api-group',
'row-sub2api-account-priority',
'row-sub2api-default-proxy',
],
},
'openai-target-codex2api': {
id: 'openai-target-codex2api',
label: 'Codex2API 来源',
rowIds: ['row-codex2api-url', 'row-codex2api-admin-key'],
},
'openai-plus': {
id: 'openai-plus',
label: 'Plus',
rowIds: ['row-plus-mode'],
},
'openai-phone': {
id: 'openai-phone',
label: '接码设置',
sectionIds: ['phone-verification-section'],
rowIds: [],
},
'openai-oauth': {
id: 'openai-oauth',
label: 'OAuth',
rowIds: ['row-oauth-flow-timeout', 'row-oauth-display'],
},
'openai-step6': {
id: 'openai-step6',
label: '第六步',
rowIds: ['row-step6-cookie-settings'],
},
'kiro-target-kiro-rs': {
id: 'kiro-target-kiro-rs',
label: 'kiro.rs 配置',
rowIds: ['row-kiro-rs-url', 'row-kiro-rs-key'],
},
'kiro-runtime-status': {
id: 'kiro-runtime-status',
label: 'Kiro 运行态',
rowIds: ['row-kiro-device-code', 'row-kiro-login-url', 'row-kiro-upload-status'],
},
});
function normalizeFlowId(value = '', fallback = DEFAULT_FLOW_ID) {
const normalized = String(value || '').trim().toLowerCase();
if (normalized && Object.prototype.hasOwnProperty.call(FLOW_DEFINITIONS, normalized)) {
return normalized;
}
const fallbackValue = String(fallback || '').trim().toLowerCase();
return Object.prototype.hasOwnProperty.call(FLOW_DEFINITIONS, fallbackValue)
? fallbackValue
: DEFAULT_FLOW_ID;
}
function getRegisteredFlowIds() {
return Object.keys(FLOW_DEFINITIONS);
}
function getFlowDefinition(flowId) {
return FLOW_DEFINITIONS[normalizeFlowId(flowId)] || FLOW_DEFINITIONS[DEFAULT_FLOW_ID];
}
function getFlowLabel(flowId) {
return getFlowDefinition(flowId)?.label || normalizeFlowId(flowId);
}
function getDefaultIntegrationTargetId(flowId) {
return normalizeFlowId(flowId) === 'kiro'
? DEFAULT_KIRO_INTEGRATION_TARGET_ID
: DEFAULT_OPENAI_INTEGRATION_TARGET_ID;
}
function normalizeOpenAiIntegrationTargetId(value = '', fallback = DEFAULT_OPENAI_INTEGRATION_TARGET_ID) {
const normalized = String(value || '').trim().toLowerCase();
if (OPENAI_INTEGRATION_TARGET_IDS.includes(normalized)) {
return normalized;
}
const fallbackValue = String(fallback || '').trim().toLowerCase();
return OPENAI_INTEGRATION_TARGET_IDS.includes(fallbackValue)
? fallbackValue
: DEFAULT_OPENAI_INTEGRATION_TARGET_ID;
}
function normalizeKiroIntegrationTargetId(value = '', fallback = DEFAULT_KIRO_INTEGRATION_TARGET_ID) {
const normalized = String(value || '').trim().toLowerCase();
if (normalized === DEFAULT_KIRO_INTEGRATION_TARGET_ID) {
return normalized;
}
const fallbackValue = String(fallback || '').trim().toLowerCase();
return fallbackValue === DEFAULT_KIRO_INTEGRATION_TARGET_ID
? fallbackValue
: DEFAULT_KIRO_INTEGRATION_TARGET_ID;
}
function normalizeIntegrationTargetId(flowId, integrationTargetId = '', fallback = undefined) {
const normalizedFlowId = normalizeFlowId(flowId);
if (normalizedFlowId === 'kiro') {
return normalizeKiroIntegrationTargetId(
integrationTargetId,
fallback || DEFAULT_KIRO_INTEGRATION_TARGET_ID
);
}
return normalizeOpenAiIntegrationTargetId(
integrationTargetId,
fallback || DEFAULT_OPENAI_INTEGRATION_TARGET_ID
);
}
function getIntegrationTargetDefinitions(flowId) {
return getFlowDefinition(flowId)?.integrationTargets || {};
}
function getIntegrationTargetDefinition(flowId, integrationTargetId) {
const normalizedFlowId = normalizeFlowId(flowId);
const normalizedIntegrationTargetId = normalizeIntegrationTargetId(
normalizedFlowId,
integrationTargetId,
getDefaultIntegrationTargetId(normalizedFlowId)
);
return getIntegrationTargetDefinitions(normalizedFlowId)[normalizedIntegrationTargetId] || null;
}
function getIntegrationTargetOptions(flowId) {
return Object.values(getIntegrationTargetDefinitions(flowId));
}
function getIntegrationTargetLabel(flowId, integrationTargetId) {
return getIntegrationTargetDefinition(flowId, integrationTargetId)?.label
|| normalizeIntegrationTargetId(flowId, integrationTargetId);
}
function getPublicationTargetDefinitions(flowId) {
return getFlowDefinition(flowId)?.publicationTargets || {};
}
function getPublicationTargetDefinition(flowId, publicationTargetId) {
const normalizedFlowId = normalizeFlowId(flowId);
const normalizedPublicationTargetId = String(
publicationTargetId || (
normalizedFlowId === 'kiro'
? DEFAULT_KIRO_PUBLICATION_TARGET_ID
: ''
)
).trim().toLowerCase();
return getPublicationTargetDefinitions(normalizedFlowId)[normalizedPublicationTargetId] || null;
}
function getFlowCapabilities(flowId) {
return {
...DEFAULT_FLOW_CAPABILITIES,
...(getFlowDefinition(flowId)?.capabilities || {}),
};
}
function getVisibleGroupIds(flowId, integrationTargetId, options = {}) {
const normalizedFlowId = normalizeFlowId(flowId);
const flowDefinition = getFlowDefinition(normalizedFlowId);
const normalizedIntegrationTargetId = normalizeIntegrationTargetId(
normalizedFlowId,
integrationTargetId,
getDefaultIntegrationTargetId(normalizedFlowId)
);
const integrationTargetDefinition = getIntegrationTargetDefinition(
normalizedFlowId,
normalizedIntegrationTargetId
);
const includeSharedServices = options?.includeSharedServices !== false;
const serviceGroups = includeSharedServices
? (Array.isArray(flowDefinition?.services)
? flowDefinition.services.map((serviceId) => `service-${serviceId}`)
: [])
: [];
return Array.from(new Set([
...(Array.isArray(flowDefinition?.baseGroups) ? flowDefinition.baseGroups : []),
...(Array.isArray(integrationTargetDefinition?.groups) ? integrationTargetDefinition.groups : []),
...serviceGroups,
]));
}
function getSettingsGroupDefinition(groupId) {
const normalizedGroupId = String(groupId || '').trim();
return SETTINGS_GROUP_DEFINITIONS[normalizedGroupId] || null;
}
function getSettingsGroupDefinitions() {
return SETTINGS_GROUP_DEFINITIONS;
}
function getRuntimeSourceDefinitions() {
const next = {};
Object.values(FLOW_DEFINITIONS).forEach((flowDefinition) => {
Object.assign(next, flowDefinition.runtimeSources || {});
});
return next;
}
function getDriverDefinitions() {
const next = {};
Object.values(FLOW_DEFINITIONS).forEach((flowDefinition) => {
Object.assign(next, flowDefinition.driverDefinitions || {});
});
return next;
}
return {
DEFAULT_FLOW_CAPABILITIES,
DEFAULT_FLOW_ID,
DEFAULT_KIRO_INTEGRATION_TARGET_ID,
DEFAULT_KIRO_PUBLICATION_TARGET_ID,
DEFAULT_KIRO_RS_URL,
DEFAULT_OPENAI_INTEGRATION_TARGET_ID,
FLOW_DEFINITIONS,
OPENAI_INTEGRATION_TARGET_IDS,
SETTINGS_GROUP_DEFINITIONS,
SHARED_SERVICE_IDS,
getDefaultIntegrationTargetId,
getDriverDefinitions,
getFlowCapabilities,
getFlowDefinition,
getFlowLabel,
getIntegrationTargetDefinition,
getIntegrationTargetDefinitions,
getIntegrationTargetLabel,
getIntegrationTargetOptions,
getPublicationTargetDefinition,
getPublicationTargetDefinitions,
getRegisteredFlowIds,
getRuntimeSourceDefinitions,
getSettingsGroupDefinition,
getSettingsGroupDefinitions,
getVisibleGroupIds,
normalizeFlowId,
normalizeIntegrationTargetId,
normalizeKiroIntegrationTargetId,
normalizeOpenAiIntegrationTargetId,
};
});