refactor flows into canonical runtime architecture

This commit is contained in:
QLHazyCoder
2026-05-21 07:21:34 +08:00
parent e2485d2e64
commit a7b35ee11a
167 changed files with 9034 additions and 3032 deletions
@@ -52,12 +52,24 @@ test('importSettingsBundle normalizes unsupported capability flags before persis
const api = new Function(`
const SETTINGS_EXPORT_SCHEMA_VERSION = 1;
const DEFAULT_REGISTRATION_EMAIL_STATE = { emailHistory: [] };
const DEFAULT_ACTIVE_FLOW_ID = 'openai';
const self = {
MultiPageLegacySettingsImporter: {
createSettingsImporter() {
return {
importSettings(settings = {}) {
return { ...settings };
},
};
},
},
};
let persistedUpdates = null;
let stateUpdates = null;
let broadcastPayload = null;
let currentState = {
activeFlowId: 'site-a',
panelMode: 'sub2api',
targetId: 'sub2api',
signupMethod: 'phone',
plusModeEnabled: false,
phoneVerificationEnabled: false,
@@ -74,7 +86,7 @@ function validateModeSwitchState() {
ok: false,
errors: [{ code: 'panel_mode_unsupported', message: '当前 flow 不支持 SUB2API 面板模式。' }],
normalizedUpdates: {
panelMode: 'cpa',
targetId: 'cpa',
plusModeEnabled: false,
phoneVerificationEnabled: false,
signupMethod: 'email',
@@ -84,6 +96,9 @@ function validateModeSwitchState() {
function resolveSignupMethod(state = {}) {
return String(state?.signupMethod || '').trim().toLowerCase() === 'phone' ? 'phone' : 'email';
}
function getSettingsSchemaApi() {
return null;
}
async function setPersistentSettings(updates) {
persistedUpdates = { ...updates };
}
@@ -109,7 +124,7 @@ return {
const result = await api.importSettingsBundle({
schemaVersion: 1,
settings: {
panelMode: 'sub2api',
targetId: 'sub2api',
plusModeEnabled: true,
phoneVerificationEnabled: true,
signupMethod: 'phone',
@@ -117,16 +132,164 @@ return {
});
assert.deepEqual(api.getPersistedUpdates(), {
panelMode: 'cpa',
targetId: 'cpa',
plusModeEnabled: false,
phoneVerificationEnabled: false,
signupMethod: 'email',
});
assert.equal(api.getStateUpdates().panelMode, 'cpa');
assert.equal(api.getStateUpdates().targetId, 'cpa');
assert.equal(api.getStateUpdates().plusModeEnabled, false);
assert.equal(api.getStateUpdates().phoneVerificationEnabled, false);
assert.equal(api.getStateUpdates().signupMethod, 'email');
assert.equal(api.getBroadcastPayload().panelMode, 'cpa');
assert.equal(api.getBroadcastPayload().targetId, 'cpa');
assert.equal(api.getBroadcastPayload().signupMethod, 'email');
assert.equal(result.signupMethod, 'email');
});
test('importSettingsBundle routes legacy settings through the legacy importer before persisting', async () => {
const api = new Function(`
const SETTINGS_EXPORT_SCHEMA_VERSION = 1;
const DEFAULT_REGISTRATION_EMAIL_STATE = { emailHistory: [] };
const DEFAULT_ACTIVE_FLOW_ID = 'openai';
let importerInput = null;
let persistedUpdates = null;
let currentState = {
activeFlowId: 'openai',
nodeStatuses: {},
};
const self = {
MultiPageFlowRegistry: {
DEFAULT_FLOW_ID: 'openai',
},
MultiPageLegacySettingsImporter: {
createSettingsImporter() {
return {
importSettings(settings = {}) {
importerInput = JSON.parse(JSON.stringify(settings));
return {
settingsSchemaVersion: 5,
settingsState: {
schemaVersion: 5,
activeFlowId: 'kiro',
services: {
account: { customPassword: '' },
email: { provider: '163' },
proxy: { enabled: false, provider: '711proxy', mode: 'account' },
},
flows: {
openai: {
selectedTargetId: 'cpa',
targets: {
cpa: { vpsUrl: '', vpsPassword: '', localCpaStep9Mode: 'submit' },
sub2api: {
sub2apiUrl: '',
sub2apiEmail: '',
sub2apiPassword: '',
sub2apiGroupName: 'codex',
sub2apiGroupNames: ['codex', 'openai-plus'],
sub2apiAccountPriority: 1,
sub2apiDefaultProxyName: '',
},
codex2api: { codex2apiUrl: '', codex2apiAdminKey: '' },
},
signup: {
signupMethod: 'email',
phoneVerificationEnabled: false,
phoneSignupReloginAfterBindEmailEnabled: false,
},
plus: {
plusModeEnabled: false,
plusPaymentMethod: 'paypal',
plusAccountAccessStrategy: 'oauth',
},
autoRun: {
stepExecutionRange: { enabled: false, fromStep: 1, toStep: 11 },
},
},
kiro: {
selectedTargetId: 'kiro-rs',
targets: {
'kiro-rs': {
baseUrl: 'https://kiro.example.com/admin',
apiKey: 'imported-key',
},
},
autoRun: {
stepExecutionRange: { enabled: false, fromStep: 1, toStep: 9 },
},
},
},
},
};
},
};
},
},
};
async function ensureManualInteractionAllowed() {
return currentState;
}
function buildPersistentSettingsPayload(settings = {}) {
return {
activeFlowId: settings.settingsState.activeFlowId,
targetId: 'cpa',
signupMethod: 'email',
targetId: 'kiro-rs',
kiroRsUrl: settings.settingsState.flows.kiro.targets['kiro-rs'].baseUrl,
kiroRsKey: settings.settingsState.flows.kiro.targets['kiro-rs'].apiKey,
settingsSchemaVersion: settings.settingsSchemaVersion,
settingsState: settings.settingsState,
};
}
function validateModeSwitchState() {
return { normalizedUpdates: {} };
}
function resolveSignupMethod() {
return 'email';
}
function getSettingsSchemaApi() {
return null;
}
async function setPersistentSettings(updates) {
persistedUpdates = { ...updates };
return updates;
}
async function setState(updates) {
currentState = { ...currentState, ...updates };
}
function broadcastDataUpdate() {}
async function getState() {
return currentState;
}
${extractFunction('importSettingsBundle')}
return {
importSettingsBundle,
getImporterInput: () => importerInput,
getPersistedUpdates: () => persistedUpdates,
};
`)();
await api.importSettingsBundle({
schemaVersion: 1,
settings: {
targetId: 'sub2api',
kiroRuntime: {
upload: {
status: 'uploaded',
},
},
},
});
assert.deepEqual(api.getImporterInput(), {
targetId: 'sub2api',
kiroRuntime: {
upload: {
status: 'uploaded',
},
},
});
assert.equal(api.getPersistedUpdates().activeFlowId, 'kiro');
assert.equal(api.getPersistedUpdates().settingsSchemaVersion, 5);
assert.equal(api.getPersistedUpdates().settingsState.flows.kiro.targets['kiro-rs'].apiKey, 'imported-key');
});