Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 52 additions & 7 deletions web/strategy-switch-console/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ <h2 data-i18n="summary">切换摘要</h2>
strategy: "策略",
mode: "模式",
live: "实盘",
paper: "模拟",
paper: "Dry run",
summary: "切换摘要",
copySummary: "复制摘要",
loginToRun: "登录后切换",
Expand Down Expand Up @@ -718,7 +718,7 @@ <h2 data-i18n="summary">切换摘要</h2>
strategy: "Strategy",
mode: "Mode",
live: "Live",
paper: "Paper",
paper: "Dry run",
summary: "Switch Summary",
copySummary: "Copy summary",
loginToRun: "Sign in to switch",
Expand Down Expand Up @@ -827,18 +827,57 @@ <h2 data-i18n="summary">切换摘要</h2>
return strategyLabels[profile] || profile;
}

function currentStrategyForAccount(platform, account) {
function modeLabel(mode) {
return mode === "paper" ? t("paper") : t("live");
}

function currentEntryForAccount(platform, account) {
const byPlatform = state.currentStrategies[platform] || {};
const keys = [account?.key, account?.target_name, account?.label]
.filter(Boolean)
.map((value) => String(value));
for (const key of keys) {
const profile = cleanStrategyProfile(byPlatform[key]?.strategy_profile);
if (profile) return profile;
const entry = byPlatform[key];
const profile = cleanStrategyProfile(entry?.strategy_profile);
if (profile) return entry;
}
return null;
}

function currentStrategyForAccount(platform, account) {
const entry = currentEntryForAccount(platform, account);
const profile = cleanStrategyProfile(entry?.strategy_profile);
return profile || "";
}

function normalizeExecutionMode(value, dryRunOnly) {
const mode = String(value || "").trim().toLowerCase();
if (mode === "live" || mode === "paper") return mode;
if (dryRunOnly === true || dryRunOnly === "true" || dryRunOnly === "1" || dryRunOnly === 1) return "paper";
if (dryRunOnly === false || dryRunOnly === "false" || dryRunOnly === "0" || dryRunOnly === 0) return "live";
return "";
}

function defaultExecutionModeForAccount(platform, account, fallback = "live") {
const currentMode = normalizeExecutionMode(
currentEntryForAccount(platform, account)?.execution_mode,
currentEntryForAccount(platform, account)?.dry_run_only,
);
if (currentMode) return currentMode;
const hint = [
account?.key,
account?.label,
account?.target_name,
account?.deployment_selector,
account?.account_scope,
account?.service_name,
].join(" ").toLowerCase();
if (hint.split(/\s+/).includes("paper") || hint.includes("-paper") || hint.includes("_paper")) {
return "paper";
}
return fallback;
}

function defaultStrategyForAccount(platform, account, fallback = "tqqq_growth_income") {
const currentProfile = currentStrategyForAccount(platform, account);
if (currentProfile) return currentProfile;
Expand All @@ -862,6 +901,10 @@ <h2 data-i18n="summary">切换摘要</h2>
const account = selectedAccount(platform);
if (!account) return;
state.forms[platform].strategy = defaultStrategyForAccount(platform, account, state.forms[platform].strategy);
state.forms[platform].executionMode = defaultExecutionModeForAccount(
platform,
account,
);
}

function ensureAccountSelection(platform) {
Expand Down Expand Up @@ -923,7 +966,7 @@ <h2 data-i18n="summary">切换摘要</h2>
[t("repository"), repositories[state.selected]],
[t("selectedAccount"), account.label],
[t("selectedStrategy"), strategyLabel(inputs.strategy_profile)],
[t("selectedMode"), inputs.execution_mode],
[t("selectedMode"), modeLabel(inputs.execution_mode)],
[t("target"), inputs.target_name],
[t("accountSelector"), inputs.account_selector || "auto"],
[t("service"), inputs.service_name || derivedService(state.selected, inputs.target_name)],
Expand Down Expand Up @@ -1001,7 +1044,7 @@ <h2 data-i18n="summary">切换摘要</h2>
</div>
`).join("");

el("mode-pill").textContent = inputs.execution_mode;
el("mode-pill").textContent = modeLabel(inputs.execution_mode);
}

function renderAuth() {
Expand Down Expand Up @@ -1125,6 +1168,8 @@ <h2 data-i18n="summary">切换摘要</h2>
if (!profile) continue;
normalized[platform][String(key)] = {
strategy_profile: profile,
execution_mode: normalizeExecutionMode(entry?.execution_mode, entry?.dry_run_only),
dry_run_only: entry?.dry_run_only === true || entry?.dry_run_only === "true" || entry?.dry_run_only === "1",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Don't coerce missing dry_run_only to live

When /api/config returns a current strategy entry that has no execution_mode and no dry_run_only (for example a legacy STRATEGY_PROFILE fallback for an explicitly paper account), this normalization stores dry_run_only: false. defaultExecutionModeForAccount() then treats that synthetic false as an explicit live mode and returns before checking the account's paper hint, so those paper accounts render and dispatch as Live instead of Dry run. Preserve the unknown state here instead of converting every missing value to false.

Useful? React with 👍 / 👎.

source: entry?.source ? String(entry.source) : "",
};
}
Expand Down
2 changes: 1 addition & 1 deletion web/strategy-switch-console/page_asset.js

Large diffs are not rendered by default.

43 changes: 38 additions & 5 deletions web/strategy-switch-console/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,12 @@ async function loadCurrentStrategies(accountOptions, env) {

async function resolveCurrentStrategyForAccount({ platform, option, optionsCount, repository, readVariable }) {
const serviceTargetsValue = await readVariable(repository, "repository", "", "CLOUD_RUN_SERVICE_TARGETS_JSON");
const serviceTargetProfile = strategyFromServiceTargets(serviceTargetsValue, platform, option);
const serviceTarget = runtimeTargetFromServiceTargets(serviceTargetsValue, platform, option);
const serviceTargetProfile = cleanCurrentStrategy(serviceTarget?.strategy_profile);
if (serviceTargetProfile) {
return {
strategy_profile: serviceTargetProfile,
...runtimeModePayload(serviceTarget),
source: "CLOUD_RUN_SERVICE_TARGETS_JSON",
variable_scope: "repository",
};
Expand All @@ -498,6 +500,7 @@ async function resolveCurrentStrategyForAccount({ platform, option, optionsCount
if (runtimeTargetProfile) {
return {
strategy_profile: runtimeTargetProfile,
...runtimeModePayload(runtimeTarget),
source: "RUNTIME_TARGET_JSON",
variable_scope: variableScope,
github_environment: githubEnvironment || "",
Expand All @@ -508,12 +511,16 @@ async function resolveCurrentStrategyForAccount({ platform, option, optionsCount
const profileValue = await readVariable(repository, variableScope, githubEnvironment, "STRATEGY_PROFILE");
const profile = cleanCurrentStrategy(profileValue);
if (profile) {
return {
const current = {
strategy_profile: profile,
source: "STRATEGY_PROFILE",
variable_scope: variableScope,
github_environment: githubEnvironment || "",
};
if (variableScope === "environment" && normalizeMatchValue(option?.target_name) === "paper") {
current.execution_mode = "paper";
}
return current;
}
}

Expand Down Expand Up @@ -834,7 +841,7 @@ function resolveGithubEnvironment(platform, option, variableScope) {
return targetName;
}

function strategyFromServiceTargets(rawValue, platform, option) {
function runtimeTargetFromServiceTargets(rawValue, platform, option) {
const payload = parseJsonObject(rawValue);
const targets = Array.isArray(payload?.targets) ? payload.targets : [];
for (const entry of targets) {
Expand All @@ -843,12 +850,38 @@ function strategyFromServiceTargets(rawValue, platform, option) {
? entry.runtime_target
: {};
if (!runtimeTargetMatchesAccount(runtimeTarget, platform, option, entry)) continue;
const profile = cleanCurrentStrategy(runtimeTarget.strategy_profile || entry.strategy_profile);
if (profile) return profile;
return {
...runtimeTarget,
strategy_profile: runtimeTarget.strategy_profile || entry.strategy_profile,
};
}
return null;
}

function runtimeModePayload(runtimeTarget) {
const executionMode = normalizeRuntimeExecutionMode(runtimeTarget?.execution_mode, runtimeTarget?.dry_run_only);
const payload = {};
if (executionMode) payload.execution_mode = executionMode;
const dryRunOnly = cleanOptionalBoolean(runtimeTarget?.dry_run_only);
if (dryRunOnly !== null) payload.dry_run_only = dryRunOnly;
return payload;
}

function normalizeRuntimeExecutionMode(value, dryRunOnly) {
const mode = String(value || "").trim().toLowerCase();
if (mode === "live" || mode === "paper") return mode;
const dryRun = cleanOptionalBoolean(dryRunOnly);
if (dryRun === true) return "paper";
if (dryRun === false) return "live";
return "";
}

function cleanOptionalBoolean(value) {
if (value === true || value === "true" || value === "1" || value === 1) return true;
if (value === false || value === "false" || value === "0" || value === 0) return false;
return null;
}

function runtimeTargetMatchesAccount(runtimeTarget, platform, option, entry = {}) {
const runtimePlatform = String(runtimeTarget?.platform_id || "").trim().toLowerCase();
if (runtimePlatform && runtimePlatform !== platform) return false;
Expand Down