feat: lay groundwork for multi-flow registries

This commit is contained in:
QLHazyCoder
2026-05-13 04:46:56 +08:00
parent 04f89620be
commit a85ed0ce89
28 changed files with 2775 additions and 92 deletions
+242
View File
@@ -0,0 +1,242 @@
(function attachMultiPageFlowCapabilities(root, factory) {
root.MultiPageFlowCapabilities = factory();
})(typeof self !== 'undefined' ? self : globalThis, function createFlowCapabilitiesModule() {
const DEFAULT_FLOW_ID = 'openai';
const DEFAULT_PANEL_MODE = 'cpa';
const SIGNUP_METHOD_EMAIL = 'email';
const SIGNUP_METHOD_PHONE = 'phone';
const VALID_PANEL_MODES = Object.freeze(['cpa', 'sub2api', 'codex2api']);
const DEFAULT_FLOW_CAPABILITIES = Object.freeze({
supportsEmailSignup: true,
supportsPhoneSignup: false,
supportsPhoneVerificationSettings: false,
supportsPlusMode: false,
supportsContributionMode: false,
supportsPlatformBinding: [],
supportsLuckmail: false,
supportsOauthTimeoutBudget: false,
canSwitchFlow: false,
stepDefinitionMode: 'default',
});
const FLOW_CAPABILITIES = Object.freeze({
openai: Object.freeze({
...DEFAULT_FLOW_CAPABILITIES,
supportsPhoneSignup: true,
supportsPhoneVerificationSettings: true,
supportsPlusMode: true,
supportsContributionMode: true,
supportsPlatformBinding: ['cpa', 'sub2api', 'codex2api'],
supportsLuckmail: true,
supportsOauthTimeoutBudget: true,
stepDefinitionMode: 'openai-dynamic',
}),
});
const DEFAULT_PANEL_CAPABILITIES = Object.freeze({
supportsPhoneSignup: true,
requiresPhoneSignupWarning: false,
});
const PANEL_CAPABILITIES = Object.freeze({
cpa: Object.freeze({
supportsPhoneSignup: true,
requiresPhoneSignupWarning: true,
}),
sub2api: Object.freeze({
supportsPhoneSignup: true,
requiresPhoneSignupWarning: false,
}),
codex2api: Object.freeze({
supportsPhoneSignup: true,
requiresPhoneSignupWarning: false,
}),
});
function normalizeFlowId(value = '', fallback = DEFAULT_FLOW_ID) {
const normalized = String(value || '').trim().toLowerCase();
if (normalized) {
return normalized;
}
const fallbackValue = String(fallback || '').trim().toLowerCase();
return fallbackValue || DEFAULT_FLOW_ID;
}
function normalizePanelMode(value = '', fallback = DEFAULT_PANEL_MODE) {
const normalized = String(value || '').trim().toLowerCase();
if (VALID_PANEL_MODES.includes(normalized)) {
return normalized;
}
const fallbackValue = String(fallback || '').trim().toLowerCase();
return VALID_PANEL_MODES.includes(fallbackValue) ? fallbackValue : DEFAULT_PANEL_MODE;
}
function normalizeSignupMethod(value = '') {
return String(value || '').trim().toLowerCase() === SIGNUP_METHOD_PHONE
? SIGNUP_METHOD_PHONE
: SIGNUP_METHOD_EMAIL;
}
function normalizePanelModeList(values = []) {
if (!Array.isArray(values)) {
return [];
}
const seen = new Set();
const normalized = [];
values.forEach((value) => {
const mode = normalizePanelMode(value, '');
if (!mode || seen.has(mode)) {
return;
}
seen.add(mode);
normalized.push(mode);
});
return normalized;
}
function createFlowCapabilityRegistry(deps = {}) {
const {
defaultFlowCapabilities = DEFAULT_FLOW_CAPABILITIES,
defaultFlowId = DEFAULT_FLOW_ID,
defaultPanelCapabilities = DEFAULT_PANEL_CAPABILITIES,
flowCapabilities = FLOW_CAPABILITIES,
panelCapabilities = PANEL_CAPABILITIES,
} = deps;
function getFlowCapabilities(flowId) {
const normalizedFlowId = normalizeFlowId(flowId, defaultFlowId);
const entry = flowCapabilities[normalizedFlowId] || null;
return {
...defaultFlowCapabilities,
...(entry || {}),
supportsPlatformBinding: normalizePanelModeList(entry?.supportsPlatformBinding || defaultFlowCapabilities.supportsPlatformBinding),
};
}
function getPanelCapabilities(panelMode) {
const normalizedPanelMode = normalizePanelMode(panelMode);
return {
...defaultPanelCapabilities,
...(panelCapabilities[normalizedPanelMode] || {}),
};
}
function resolveSidepanelCapabilities(options = {}) {
const state = options?.state || {};
const activeFlowId = normalizeFlowId(
options?.activeFlowId ?? state?.activeFlowId,
defaultFlowId
);
const flowState = getFlowCapabilities(activeFlowId);
const requestedPanelMode = normalizePanelMode(
options?.panelMode ?? state?.panelMode,
DEFAULT_PANEL_MODE
);
const supportedPanelModes = normalizePanelModeList(flowState.supportsPlatformBinding);
const panelModeSupported = supportedPanelModes.length === 0
? true
: supportedPanelModes.includes(requestedPanelMode);
const effectivePanelMode = panelModeSupported
? requestedPanelMode
: supportedPanelModes[0];
const panelState = getPanelCapabilities(effectivePanelMode);
const runtimeLocks = {
autoRunLocked: Boolean(options?.autoRunLocked ?? state?.autoRunLocked),
contributionMode: flowState.supportsContributionMode && Boolean(state?.contributionMode),
phoneVerificationEnabled: flowState.supportsPhoneVerificationSettings && Boolean(state?.phoneVerificationEnabled),
plusModeEnabled: flowState.supportsPlusMode && Boolean(state?.plusModeEnabled),
settingsMenuLocked: Boolean(options?.settingsMenuLocked ?? state?.settingsMenuLocked),
};
const effectiveSignupMethods = [];
if (flowState.supportsEmailSignup !== false) {
effectiveSignupMethods.push(SIGNUP_METHOD_EMAIL);
}
const canSelectPhoneSignup = Boolean(flowState.supportsPhoneSignup)
&& Boolean(panelState.supportsPhoneSignup)
&& runtimeLocks.phoneVerificationEnabled
&& !runtimeLocks.plusModeEnabled
&& !runtimeLocks.contributionMode;
if (canSelectPhoneSignup) {
effectiveSignupMethods.push(SIGNUP_METHOD_PHONE);
}
if (!effectiveSignupMethods.length) {
effectiveSignupMethods.push(SIGNUP_METHOD_EMAIL);
}
const requestedSignupMethod = normalizeSignupMethod(
options?.signupMethod ?? state?.signupMethod
);
const effectiveSignupMethod = requestedSignupMethod === SIGNUP_METHOD_PHONE && canSelectPhoneSignup
? SIGNUP_METHOD_PHONE
: (effectiveSignupMethods.includes(SIGNUP_METHOD_EMAIL)
? SIGNUP_METHOD_EMAIL
: effectiveSignupMethods[0]);
return {
activeFlowId,
canShowContributionMode: Boolean(flowState.supportsContributionMode),
canShowLuckmail: Boolean(flowState.supportsLuckmail),
canShowPhoneSettings: Boolean(flowState.supportsPhoneVerificationSettings),
canShowPlusSettings: Boolean(flowState.supportsPlusMode),
canSwitchFlow: Boolean(flowState.canSwitchFlow),
canUsePhoneSignup: canSelectPhoneSignup,
canUseSelectedPanelMode: panelModeSupported,
effectivePanelMode,
effectiveSignupMethod,
effectiveSignupMethods,
flowCapabilities: flowState,
panelCapabilities: panelState,
panelMode: effectivePanelMode,
requestedPanelMode,
requestedSignupMethod,
runtimeLocks,
shouldWarnCpaPhoneSignup: effectiveSignupMethod === SIGNUP_METHOD_PHONE
&& Boolean(panelState.requiresPhoneSignupWarning),
stepDefinitionOptions: {
activeFlowId,
panelMode: effectivePanelMode,
plusModeEnabled: runtimeLocks.plusModeEnabled,
signupMethod: effectiveSignupMethod,
},
supportedPanelModes,
};
}
function canUsePhoneSignup(state = {}) {
return resolveSidepanelCapabilities({ state }).canUsePhoneSignup;
}
function resolveSignupMethod(state = {}, signupMethod = undefined) {
return resolveSidepanelCapabilities({
signupMethod,
state,
}).effectiveSignupMethod;
}
return {
canUsePhoneSignup,
getFlowCapabilities,
getPanelCapabilities,
normalizeFlowId,
normalizePanelMode,
normalizeSignupMethod,
resolveSidepanelCapabilities,
resolveSignupMethod,
};
}
return {
createFlowCapabilityRegistry,
DEFAULT_FLOW_CAPABILITIES,
DEFAULT_FLOW_ID,
DEFAULT_PANEL_CAPABILITIES,
DEFAULT_PANEL_MODE,
FLOW_CAPABILITIES,
PANEL_CAPABILITIES,
SIGNUP_METHOD_EMAIL,
SIGNUP_METHOD_PHONE,
normalizeFlowId,
normalizePanelMode,
normalizeSignupMethod,
};
});
+433
View File
@@ -0,0 +1,433 @@
(function attachMultiPageSourceRegistry(root, factory) {
root.MultiPageSourceRegistry = factory();
})(typeof self !== 'undefined' ? self : globalThis, function createSourceRegistryModule() {
const SOURCE_ALIASES = Object.freeze({
'signup-page': 'openai-auth',
});
const SOURCE_DEFINITIONS = Object.freeze({
'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: [],
},
'qq-mail': {
flowId: null,
kind: 'mail-provider',
label: 'QQ 邮箱',
readyPolicy: 'top-frame-only',
family: 'qq-mail-family',
driverId: 'content/qq-mail',
cleanupScopes: [],
},
'mail-163': {
flowId: null,
kind: 'mail-provider',
label: '163 邮箱',
readyPolicy: 'top-frame-only',
family: 'mail-163-family',
driverId: 'content/mail-163',
cleanupScopes: [],
},
'gmail-mail': {
flowId: null,
kind: 'mail-provider',
label: 'Gmail 邮箱',
readyPolicy: 'top-frame-only',
family: 'gmail-mail-family',
driverId: 'content/gmail-mail',
cleanupScopes: [],
},
'icloud-mail': {
flowId: null,
kind: 'mail-provider',
label: 'iCloud 邮箱',
readyPolicy: 'allow-child-frame',
family: 'icloud-mail-family',
driverId: 'content/icloud-mail',
cleanupScopes: [],
},
'inbucket-mail': {
flowId: null,
kind: 'mail-provider',
label: 'Inbucket 邮箱',
readyPolicy: 'top-frame-only',
family: 'inbucket-mail-family',
driverId: 'content/inbucket-mail',
cleanupScopes: [],
},
'mail-2925': {
flowId: null,
kind: 'mail-provider',
label: '2925 邮箱',
readyPolicy: 'top-frame-only',
family: 'mail-2925-family',
driverId: 'content/mail-2925',
cleanupScopes: [],
},
'duck-mail': {
flowId: null,
kind: 'mail-provider',
label: 'Duck 邮箱',
readyPolicy: 'allow-child-frame',
family: 'duck-mail-family',
driverId: 'content/duck-mail',
cleanupScopes: [],
},
'vps-panel': {
flowId: 'openai',
kind: 'panel-page',
label: 'CPA 面板',
readyPolicy: 'allow-child-frame',
family: 'vps-panel-family',
driverId: 'content/vps-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: [],
},
'unknown-source': {
flowId: null,
kind: 'unknown',
label: '未知来源',
readyPolicy: 'disabled',
family: 'unknown-family',
driverId: null,
cleanupScopes: [],
},
});
const DRIVER_DEFINITIONS = Object.freeze({
'content/signup-page': {
sourceId: 'openai-auth',
commands: [
'OPEN_SIGNUP',
'SUBMIT_SIGNUP_IDENTIFIER',
'SUBMIT_PASSWORD',
'SUBMIT_PROFILE',
'SUBMIT_LOGIN_CODE',
'SUBMIT_PHONE_CODE',
'DETECT_AUTH_STATE',
],
},
'content/qq-mail': {
sourceId: 'qq-mail',
commands: ['POLL_EMAIL'],
},
'content/mail-163': {
sourceId: 'mail-163',
commands: ['POLL_EMAIL'],
},
'content/gmail-mail': {
sourceId: 'gmail-mail',
commands: ['POLL_EMAIL'],
},
'content/icloud-mail': {
sourceId: 'icloud-mail',
commands: ['POLL_EMAIL'],
},
'content/mail-2925': {
sourceId: 'mail-2925',
commands: ['POLL_EMAIL'],
},
'content/duck-mail': {
sourceId: 'duck-mail',
commands: ['FETCH_ALIAS_EMAIL'],
},
'content/sub2api-panel': {
sourceId: 'sub2api-panel',
commands: ['OPEN_PANEL', 'FETCH_OAUTH_URL', 'VERIFY_PLATFORM'],
},
'content/vps-panel': {
sourceId: 'vps-panel',
commands: ['OPEN_PANEL', 'FETCH_OAUTH_URL'],
},
'content/plus-checkout': {
sourceId: 'plus-checkout',
commands: ['CREATE_CHECKOUT', 'FILL_CHECKOUT'],
},
'content/paypal-flow': {
sourceId: 'paypal-flow',
commands: ['APPROVE_PAYPAL'],
},
'content/gopay-flow': {
sourceId: 'gopay-flow',
commands: ['APPROVE_GOPAY'],
},
});
const CLEANUP_SCOPE_OWNERS = Object.freeze({
'oauth-localhost-callback': 'openai-auth',
});
const AUTH_PAGE_HOSTS = new Set(['auth0.openai.com', 'auth.openai.com', 'accounts.openai.com']);
const ENTRY_PAGE_HOSTS = new Set(['chatgpt.com', 'www.chatgpt.com', 'chat.openai.com']);
const CHILD_FRAME_BLOCKED_SOURCES = new Set([
'qq-mail',
'mail-163',
'gmail-mail',
'mail-2925',
'inbucket-mail',
'plus-checkout',
]);
function createSourceRegistry() {
function parseUrlSafely(rawUrl) {
if (!rawUrl) return null;
try {
return new URL(rawUrl);
} catch {
return null;
}
}
function normalizeSourceId(source) {
return String(source || '').trim();
}
function resolveCanonicalSource(source) {
const normalized = normalizeSourceId(source);
if (!normalized) return '';
return SOURCE_ALIASES[normalized] || normalized;
}
function getAliasKeysForCanonicalSource(source) {
const canonical = resolveCanonicalSource(source);
return Object.keys(SOURCE_ALIASES).filter((alias) => SOURCE_ALIASES[alias] === canonical);
}
function getSourceKeys(source) {
const normalized = normalizeSourceId(source);
const canonical = resolveCanonicalSource(normalized);
return Array.from(new Set([
canonical,
...getAliasKeysForCanonicalSource(canonical),
normalized,
].filter(Boolean)));
}
function getSourceMeta(source) {
const canonical = resolveCanonicalSource(source);
const definition = SOURCE_DEFINITIONS[canonical];
if (!definition) {
return null;
}
return {
id: canonical,
aliases: getAliasKeysForCanonicalSource(canonical),
...definition,
};
}
function getSourceLabel(source) {
return getSourceMeta(source)?.label || normalizeSourceId(source) || '未知来源';
}
function getDriverIdForSource(source) {
return getSourceMeta(source)?.driverId || null;
}
function getDriverMeta(sourceOrDriverId) {
const directDriverId = normalizeSourceId(sourceOrDriverId);
const driverId = Object.prototype.hasOwnProperty.call(DRIVER_DEFINITIONS, directDriverId)
? directDriverId
: getDriverIdForSource(sourceOrDriverId);
if (!driverId || !Object.prototype.hasOwnProperty.call(DRIVER_DEFINITIONS, driverId)) {
return null;
}
return {
id: driverId,
...DRIVER_DEFINITIONS[driverId],
};
}
function isSignupPageHost(hostname = '') {
return AUTH_PAGE_HOSTS.has(String(hostname || '').toLowerCase());
}
function isSignupEntryHost(hostname = '') {
return ENTRY_PAGE_HOSTS.has(String(hostname || '').toLowerCase());
}
function is163MailHost(hostname = '') {
const normalized = String(hostname || '').toLowerCase();
return normalized === 'mail.163.com'
|| normalized.endsWith('.mail.163.com')
|| normalized === 'mail.126.com'
|| normalized.endsWith('.mail.126.com')
|| normalized === 'webmail.vip.163.com';
}
function matchesSourceUrlFamily(source, candidateUrl, referenceUrl) {
const candidate = parseUrlSafely(candidateUrl);
if (!candidate) return false;
const canonical = resolveCanonicalSource(source);
const reference = parseUrlSafely(referenceUrl);
switch (canonical) {
case 'openai-auth':
return isSignupPageHost(candidate.hostname) || isSignupEntryHost(candidate.hostname);
case 'chatgpt':
return isSignupEntryHost(candidate.hostname);
case 'duck-mail':
return candidate.hostname === 'duckduckgo.com' && candidate.pathname.startsWith('/email/');
case 'qq-mail':
return candidate.hostname === 'mail.qq.com' || candidate.hostname === 'wx.mail.qq.com';
case 'mail-163':
return is163MailHost(candidate.hostname);
case 'gmail-mail':
return candidate.hostname === 'mail.google.com';
case 'icloud-mail':
return candidate.hostname === 'www.icloud.com'
|| candidate.hostname === 'www.icloud.com.cn';
case 'inbucket-mail':
return Boolean(reference)
&& candidate.origin === reference.origin
&& candidate.pathname.startsWith('/m/');
case 'mail-2925':
return candidate.hostname === '2925.com' || candidate.hostname === 'www.2925.com';
case 'vps-panel':
return Boolean(reference)
&& candidate.origin === reference.origin
&& candidate.pathname === reference.pathname;
case 'sub2api-panel':
return Boolean(reference)
&& candidate.origin === reference.origin
&& (
candidate.pathname.startsWith('/admin/accounts')
|| candidate.pathname.startsWith('/login')
|| candidate.pathname === '/'
);
case 'codex2api-panel':
return Boolean(reference)
&& candidate.origin === reference.origin
&& (
candidate.pathname.startsWith('/admin/accounts')
|| candidate.pathname === '/admin'
|| candidate.pathname === '/'
);
case 'plus-checkout':
return candidate.hostname === 'chatgpt.com'
&& candidate.pathname.startsWith('/checkout/');
case 'paypal-flow':
return candidate.hostname.endsWith('paypal.com');
case 'gopay-flow':
return /gopay|gojek/i.test(candidate.hostname);
default:
return false;
}
}
function detectSourceFromLocation({
injectedSource,
url = '',
hostname = '',
} = {}) {
if (injectedSource) return resolveCanonicalSource(injectedSource);
const normalizedHostname = String(hostname || '').toLowerCase();
const normalizedUrl = String(url || '');
if (isSignupPageHost(normalizedHostname)) return 'openai-auth';
if (normalizedHostname === 'mail.qq.com' || normalizedHostname === 'wx.mail.qq.com') return 'qq-mail';
if (is163MailHost(normalizedHostname)) return 'mail-163';
if (normalizedHostname === 'mail.google.com') return 'gmail-mail';
if (normalizedHostname === 'www.icloud.com' || normalizedHostname === 'www.icloud.com.cn') return 'icloud-mail';
if (normalizedUrl.includes('duckduckgo.com/email/settings/autofill')) return 'duck-mail';
if (normalizedUrl.includes('2925.com')) return 'mail-2925';
if (isSignupEntryHost(normalizedHostname)) return 'chatgpt';
return 'unknown-source';
}
function shouldReportReadyForFrame(source, isChildFrame) {
const canonical = resolveCanonicalSource(source);
const readyPolicy = getSourceMeta(canonical)?.readyPolicy || 'allow-child-frame';
if (readyPolicy === 'disabled') return false;
if (!isChildFrame) return true;
if (readyPolicy === 'top-frame-only') return false;
if (CHILD_FRAME_BLOCKED_SOURCES.has(canonical)) return false;
return true;
}
function getCleanupOwnerSource(cleanupScope) {
return resolveCanonicalSource(CLEANUP_SCOPE_OWNERS[String(cleanupScope || '').trim()] || '');
}
return {
detectSourceFromLocation,
getCleanupOwnerSource,
getDriverIdForSource,
getDriverMeta,
getSourceKeys,
getSourceLabel,
getSourceMeta,
is163MailHost,
isSignupEntryHost,
isSignupPageHost,
matchesSourceUrlFamily,
parseUrlSafely,
resolveCanonicalSource,
shouldReportReadyForFrame,
};
}
return {
createSourceRegistry,
};
});