feat: persist feature research and versioned alpha assessments

This commit is contained in:
yuxuanhui
2026-09-08 21:52:46 +08:00
parent f89ae211d2
commit eb4850a003
32 changed files with 2003 additions and 20 deletions
+16 -1
View File
@@ -12,6 +12,7 @@ import {
} from "@douyinfe/semi-ui-19";
import zhCN from "@douyinfe/semi-ui-19/lib/es/locale/source/zh_CN";
import { IconSetting, IconComment, IconHistory } from "@douyinfe/semi-icons";
import { FeaturesPage } from "./research/FeaturesPage";
import { ResearchWorkspace } from "./research/ResearchWorkspace";
import { OperatorsPage } from "./research/OperatorsPage";
import { AppSidebar } from "./components/AppSidebar";
@@ -49,7 +50,7 @@ export default function App() {
page: "templates",
});
useEffect(() => {
if (["operators", "templates", "variants"].includes(page))
if (["operators", "templates", "variants", "features"].includes(page))
setVisitedResearch((old) => (old.includes(page) ? old : [...old, page]));
}, [page]);
const [catalogModal, setCatalogModal] = useState(false);
@@ -370,6 +371,16 @@ export default function App() {
/>
)}
</div>
{visitedResearch.includes("features") && (
<div className="alpha-page-view" hidden={page !== "features"}>
<FeaturesPage
action={aiAction}
active={page === "features"}
onAction={handleAction}
onContext={setResearchContext}
/>
</div>
)}
{visitedResearch.includes("operators") && (
<div className="alpha-page-view" hidden={page !== "operators"}>
<OperatorsPage active={page === "operators"} />
@@ -437,6 +448,10 @@ export default function App() {
onClose={() => setChatOpen(false)}
context={
{
features:
researchContext.page === "features"
? researchContext
: { page: "features" as const },
operators: { page: "operators" as const },
templates:
researchContext.page === "templates"
+3 -1
View File
@@ -18,7 +18,8 @@ export type PageContext = {
| "backtests"
| "operators"
| "templates"
| "variants";
| "variants"
| "features";
research_asset_id?: string;
research_experiment_id?: string;
catalog_scope?: {
@@ -43,6 +44,7 @@ export type AlphaUIAction =
| { type: "open_alpha"; alpha_id: string; nonce: number }
| { type: "apply_filters"; filters: Record<string, unknown>; nonce: number };
export type UIAction =
| { type: "open_feature"; asset_id: string; version?: number; nonce: number }
| { type: "open_template"; asset_id: string; version?: number; nonce: number }
| { type: "open_experiment"; experiment_id: string; nonce: number }
| { type: "open_variant"; alpha_id: string; nonce: number }
+2
View File
@@ -7,6 +7,7 @@ const contextLabels: Record<WorkspacePage, (context: PageContext) => string> = {
operators: () => "上下文:算子库",
templates: (context) =>
`上下文:模板工坊${context.research_asset_id ? ` · ${context.research_asset_id}` : ""}${context.research_experiment_id ? ` · 实验 ${context.research_experiment_id}` : ""}`,
features: () => "上下文:特征工程",
variants: () => "上下文:Alpha 变体",
account: () => "上下文:个人信息页",
backtests: () => "上下文:回测研究",
@@ -31,6 +32,7 @@ type Destination = {
};
// Exhaustive action destinations prevent a new action silently falling into Alpha.
const destinations: Record<UIAction["type"], Destination> = {
open_feature: { page: "features", chat: "responsive" },
open_template: { page: "templates", chat: "responsive" },
open_experiment: { page: "templates", chat: "responsive" },
open_variant: { page: "variants", chat: "responsive" },
+12
View File
@@ -32,6 +32,8 @@ import type {
} from "../types";
import { PnlChart } from "./PnlChart";
import { SelfCorrelationPanel } from "./SelfCorrelationPanel";
import { EvaluationPanel } from "../research/EvaluationPanel";
import { LineagePanel } from "../research/LineagePanel";
import { ComparisonPanel } from "../research/ComparisonPanel";
import { AlphaSources } from "../research/AlphaSources";
import type { UIAction } from "../ai/types";
@@ -340,6 +342,16 @@ export function AlphaDetail({
<ComparisonPanel key={detail.id} baseline={detail.id} />
)}
</TabPane>
<TabPane tab="评估报告" itemKey="evaluations">
{tab === "evaluations" && id && (
<EvaluationPanel key={id} alphaId={id} />
)}
</TabPane>
<TabPane tab="变体关系" itemKey="lineage">
{tab === "lineage" && id && (
<LineagePanel key={id} alphaId={id} onAction={onAction} />
)}
</TabPane>
<TabPane tab="研究来源" itemKey="sources">
{tab === "sources" && id && (
<AlphaSources
+1
View File
@@ -24,6 +24,7 @@ const navigation = [
icon: IconGridView,
group: "数据与素材",
},
{ id: "features", label: "特征工程", icon: IconBeaker, group: "研究实验" },
{ id: "variants", label: "Alpha 变体", icon: IconBeaker, group: "研究实验" },
{ id: "backtests", label: "回测研究", icon: IconBeaker, group: "研究实验" },
{ id: "alphas", label: "Alpha 管理", icon: IconGridView, group: "研究成果" },
+40
View File
@@ -39,6 +39,7 @@ import { AlphaDetail } from "../components/AlphaDetail";
import { AlphaSyncDialog } from "../components/AlphaSyncDialog";
import type { PageContext, AlphaUIAction as UIAction } from "../ai/types";
import type { UIAction as WorkspaceAction } from "../ai/types";
import { SavedViews } from "../research/SavedViews";
import { sourceLabel } from "../research/SourceDetails";
const metricLabels = {
@@ -556,6 +557,45 @@ export function AlphaPage({
{ itemKey: "SUBMITTED", tab: "已提交" },
]}
/>
<SavedViews
filters={{ ...filters, submission, sort, direction, limit: pageSize }}
columns={visibleColumns}
onRestore={(view) => {
const {
submission: savedSubmission,
sort: savedSort,
direction: savedDirection,
limit,
offset: _offset,
...rest
} = view.filters;
const values = Object.fromEntries(
Object.entries(rest)
.filter(([, v]) => v !== null && v !== undefined)
.map(([k, v]) => [k, String(v)]),
);
setFilters(values);
setDraft(values);
setPage(1);
setSelected([]);
if (
savedSubmission === "SUBMITTED" ||
savedSubmission === "UNSUBMITTED"
)
setSubmission(savedSubmission);
setSort(String(savedSort || "date_created"));
setDirection(String(savedDirection || "desc"));
if (typeof limit === "number") setPageSize(limit);
const next = [
...new Set([
"name",
...view.columns.filter((c) => c in columnLabels),
]),
];
setVisibleColumns(next);
localStorage.setItem("alpha-columns", JSON.stringify(next));
}}
/>
<form
className="filter-panel"
onSubmit={(e) => {
+254
View File
@@ -0,0 +1,254 @@
import { useEffect, useState } from "react";
import {
Banner,
Button,
Empty,
InputNumber,
Tag,
Toast,
} from "@douyinfe/semi-ui-19";
import { api, formatNumber, formatTime, post } from "../api";
import { ResearchSelect } from "./ResearchSelect";
export type Evaluation = {
id: string;
created_at: string;
report: {
verdict: string;
rules: {
version: string;
sharpe_min: number;
fitness_min: number;
turnover_max: number;
};
records: {
alpha_id: string | null;
client_item_id: string | null;
verdict: string;
missing: string[];
failed: string[];
evidence: {
metric: string;
value: number | null;
bound: number;
direction: string;
status: string;
}[];
snapshot: unknown;
}[];
model_advice: {
summary: string;
risks: string[];
suggestions: string[];
} | null;
previous_evaluation_id?: string;
};
};
const labels: Record<string, string> = {
pass: "通过本地筛选",
review: "数据不足 / 待核实",
block: "未通过规则",
};
export function EvaluationPanel({
alphaId,
experimentId,
backtestRunId,
}: {
alphaId?: string;
experimentId?: string;
backtestRunId?: string;
}) {
const [items, setItems] = useState<Evaluation[]>([]);
const [selected, setSelected] = useState<Evaluation | null>(null);
const [rules, setRules] = useState({
version: "research-v1",
sharpe_min: 1,
fitness_min: 0.5,
turnover_max: 0.7,
});
const [busy, setBusy] = useState("");
const [page, setPage] = useState(1);
const [revision, setRevision] = useState(0);
useEffect(() => {
const controller = new AbortController();
api<{ items: Evaluation[] }>(
`/research/evaluations?${alphaId ? `alpha_id=${encodeURIComponent(alphaId)}` : `experiment_id=${encodeURIComponent(experimentId || "")}`}&offset=${(page - 1) * 25}`,
{ signal: controller.signal },
)
.then((r) => setItems(r.items))
.catch((e) => {
if (!controller.signal.aborted) Toast.error(e.message);
});
return () => controller.abort();
}, [alphaId, experimentId, page, revision]);
async function perform(name: string, work: () => Promise<Evaluation>) {
setBusy(name);
try {
setSelected(await work());
setPage(1);
setRevision((n) => n + 1);
} catch (e) {
Toast.error((e as Error).message);
} finally {
setBusy("");
}
}
return (
<div className="detail-section">
<h3>版本化研究评估</h3>
<p className="muted">
每份报告固定输入快照、规则与指标证据。模型建议单独保存,不改变规则判断或本地研究状态。
</p>
<div className="research-form-grid">
{(
[
["sharpe_min", "Sharpe 下限"],
["fitness_min", "Fitness 下限"],
["turnover_max", "Turnover 上限"],
] as const
).map(([key, label]) => (
<label key={key}>
{label}
<InputNumber
aria-label={label}
value={rules[key]}
step={0.1}
onChange={(v) => {
if (typeof v === "number") setRules({ ...rules, [key]: v });
}}
/>
</label>
))}
</div>
<div className="inline-actions">
<Button
disabled={Boolean(busy) || (!alphaId && !backtestRunId)}
loading={busy === "evaluate"}
onClick={() =>
void perform("evaluate", () =>
post("/research/evaluations", {
alpha_id: alphaId,
experiment_id: experimentId,
backtest_run_id: backtestRunId,
rules,
}),
)
}
>
保存规则评估
</Button>
<ResearchSelect
label="历史评估报告"
placeholder="查看历史报告"
value={selected?.id}
optionList={items.map((i) => ({
value: i.id,
label: `${formatTime(i.created_at)} · ${labels[i.report.verdict]}`,
}))}
onChange={(v) => setSelected(items.find((i) => i.id === v) || null)}
/>
<Button disabled={page <= 1} onClick={() => setPage((n) => n - 1)}>
上一页
</Button>
<Button
disabled={items.length < 25}
onClick={() => setPage((n) => n + 1)}
>
下一页
</Button>
</div>
{!selected ? (
<Empty description="选择历史报告,或按当前规则保存新评估" />
) : (
<>
<div className="section-toolbar">
<Tag
color={
selected.report.verdict === "pass"
? "green"
: selected.report.verdict === "block"
? "red"
: "orange"
}
>
{labels[selected.report.verdict]}
</Tag>
<span className="muted">
{formatTime(selected.created_at)} ·{" "}
{selected.report.rules.version}
</span>
</div>
<p>
规则:Sharpe ≥ {selected.report.rules.sharpe_min},Fitness ≥{" "}
{selected.report.rules.fitness_min},Turnover ≤{" "}
{selected.report.rules.turnover_max}
</p>
{selected.report.records.map((r, index) => (
<div className="research-card" key={index}>
<h4>
{r.alpha_id || r.client_item_id || "未返回 Alpha"} ·{" "}
{labels[r.verdict]}
</h4>
{r.evidence.map((e) => (
<div className="check-row" key={e.metric}>
<span>
{e.metric}: {formatNumber(e.value)}
</span>
<span>
{e.direction === "min" ? "≥" : "≤"} {e.bound}
</span>
<Tag>
{e.status === "unknown"
? "缺失"
: e.status === "pass"
? "通过"
: "失败"}
</Tag>
</div>
))}
{r.missing.length > 0 && (
<p>缺失或未决:{r.missing.join("、")}</p>
)}
{r.failed.length > 0 && <p>未通过:{r.failed.join("、")}</p>}
<details>
<summary>当时的输入快照</summary>
<pre className="code-block">
{JSON.stringify(r.snapshot, null, 2)}
</pre>
</details>
</div>
))}
<Banner
type="info"
description="这是本地研究筛选,不代表官方提交资格。已有平台检查缺失时保留为待核实。"
/>
{selected.report.model_advice && (
<div className="research-card">
<h4>模型建议</h4>
<p>{selected.report.model_advice.summary}</p>
<p>
风险:
{selected.report.model_advice.risks.join(";") || "未提供"}
</p>
<p>
建议:
{selected.report.model_advice.suggestions.join(";") ||
"未提供"}
</p>
</div>
)}
<Button
disabled={Boolean(busy)}
loading={busy === "advice"}
onClick={() =>
void perform("advice", () =>
post(`/research/evaluations/${selected.id}/advice`, {}),
)
}
>
生成建议并保存为新报告
</Button>
</>
)}
</div>
);
}
+31
View File
@@ -3,6 +3,9 @@ import { Banner, Button, Checkbox, Tag, Toast } from "@douyinfe/semi-ui-19";
import { post, formatTime } from "../api";
import type { UIAction } from "../ai/types";
import type { Experiment } from "./workspaceTypes";
import { EvaluationPanel } from "./EvaluationPanel";
import { LineagePanel } from "./LineagePanel";
import { ResearchSelect } from "./ResearchSelect";
import { validationLabel } from "./workspaceTypes";
export function ExperimentView({
@@ -13,6 +16,9 @@ export function ExperimentView({
onAction: (action: UIAction) => void;
}) {
const [selected, setSelected] = useState<string[]>([]);
const [assessment, setAssessment] = useState(false);
const [relations, setRelations] = useState(false);
const [runId, setRunId] = useState(experiment.backtest_run_ids[0]);
const [busy, setBusy] = useState(false);
const valid = experiment.candidates.filter(
(c) => c.validation.status === "valid",
@@ -208,6 +214,31 @@ export function ExperimentView({
</Button>
))}
</div>
<div className="inline-actions">
<Button onClick={() => setAssessment((v) => !v)}>评估研究结果</Button>
<Button onClick={() => setRelations((v) => !v)}>查看变体关系</Button>
</div>
{assessment && (
<>
<ResearchSelect
label="待评估的回测运行"
value={runId}
optionList={experiment.backtest_run_ids.map((id) => ({
value: id,
label: id,
}))}
onChange={(v) => setRunId(String(v))}
/>
<EvaluationPanel
key={experiment.id}
experimentId={experiment.id}
backtestRunId={runId}
/>
</>
)}
{relations && (
<LineagePanel experimentId={experiment.id} onAction={onAction} />
)}
</section>
);
}
+386
View File
@@ -0,0 +1,386 @@
import { useEffect, useRef, useState } from "react";
import {
Banner,
Button,
Input,
Pagination,
TextArea,
Toast,
} from "@douyinfe/semi-ui-19";
import { api, post } from "../api";
import type { PageContext, UIAction } from "../ai/types";
import { TemplateEditor } from "./TemplateEditor";
import { ResearchSelect } from "./ResearchSelect";
import { blankTemplate } from "./workspaceTypes";
import type { Asset, InputSnapshot, Template } from "./workspaceTypes";
import "./workspace.css";
type Feature = {
name: string;
hypothesis: string;
input_ids: string[];
steps: { name: string; rationale: string; expression: string }[];
template: Template | null;
};
type FeatureAsset = Omit<Asset, "content"> & { content: Feature };
const blank = (): Feature => ({
name: "新特征方案",
hypothesis: "",
input_ids: [],
steps: [],
template: blankTemplate(),
});
export function FeaturesPage({
active,
onAction,
onContext,
action,
}: {
active: boolean;
action?: UIAction | null;
onAction: (action: UIAction) => void;
onContext: (context: PageContext) => void;
}) {
const [draft, setDraft] = useState<Feature>(blank);
const [asset, setAsset] = useState<FeatureAsset | null>(null);
const [items, setItems] = useState<FeatureAsset[]>([]);
const [inputs, setInputs] = useState<InputSnapshot[]>([]);
const [versions, setVersions] = useState<{ version: number }[]>([]);
const [page, setPage] = useState(1);
const [total, setTotal] = useState(0);
const [q, setQ] = useState("");
const [busy, setBusy] = useState("");
const [error, setError] = useState("");
const [revision, setRevision] = useState(0);
useEffect(() => {
if (!active) return;
const controller = new AbortController();
Promise.all([
api<{ items: FeatureAsset[]; total: number }>(
`/research/assets?kind=feature&q=${encodeURIComponent(q)}&offset=${(page - 1) * 25}`,
{ signal: controller.signal },
),
api<{ items: InputSnapshot[] }>("/research/inputs", {
signal: controller.signal,
}),
])
.then(([list, fixed]) => {
setItems(list.items);
setTotal(list.total);
setInputs(fixed.items);
})
.catch((e) => {
if (!controller.signal.aborted) setError(e.message);
});
return () => controller.abort();
}, [active, page, q, revision]);
useEffect(() => {
if (active) onContext({ page: "features", research_asset_id: asset?.id });
}, [active, asset?.id, onContext]);
const handled = useRef(0);
useEffect(() => {
if (
!active ||
action?.type !== "open_feature" ||
handled.current === action.nonce
)
return;
handled.current = action.nonce;
void run("load", async () =>
load(
await api(
`/research/assets/${action.asset_id}${action.version ? `?version=${action.version}` : ""}`,
),
),
);
}, [active, action]);
async function load(item: FeatureAsset) {
setAsset(item);
setDraft(item.content);
setVersions(await api(`/research/assets/${item.id}/versions`));
}
async function run(name: string, work: () => Promise<void>) {
setBusy(name);
setError("");
try {
await work();
} catch (e) {
setError((e as Error).message);
} finally {
setBusy("");
}
}
async function save() {
const saved = await api<FeatureAsset>(
`/research/assets${asset ? `/${asset.id}` : ""}`,
{
method: asset ? "PUT" : "POST",
body: JSON.stringify({
kind: "feature",
version: asset?.version,
content: draft,
}),
},
);
await load(saved);
setRevision((n) => n + 1);
return saved;
}
return (
<section className="research-workspace">
<div className="section-toolbar">
<div>
<h2>特征工程</h2>
<p className="muted">
围绕固定字段记录处理步骤与经济假设,再进入模板、候选和回测闭环。
</p>
</div>
<Button
onClick={() => {
setAsset(null);
setDraft(blank());
setVersions([]);
}}
>
新建特征方案
</Button>
</div>
{error && <Banner type="danger" description={error} />}
<div className="research-layout">
<aside className="research-library">
<Input
aria-label="搜索特征方案"
placeholder="搜索方案"
value={q}
onChange={(v) => {
setQ(v);
setPage(1);
}}
/>
{items.map((item) => (
<Button
key={item.id}
block
theme={asset?.id === item.id ? "light" : "borderless"}
onClick={() => void run("load", () => load(item))}
>
{item.name} · v{item.version}
</Button>
))}
<Pagination
size="small"
currentPage={page}
pageSize={25}
total={total}
onPageChange={setPage}
/>
</aside>
<div className="research-main">
<div className="inline-actions">
{asset && (
<>
<span>版本</span>
<ResearchSelect
label="特征方案版本"
value={asset.version}
optionList={versions.map((v) => ({
value: v.version,
label: `v${v.version}`,
}))}
onChange={(v) =>
void run("load", async () => {
await load(
await api(`/research/assets/${asset.id}?version=${v}`),
);
})
}
/>
</>
)}
</div>
<label>
方案名称
<Input
aria-label="特征方案名称"
value={draft.name}
onChange={(name) => setDraft({ ...draft, name })}
/>
</label>
<label>
经济假设
<TextArea
aria-label="特征经济假设"
value={draft.hypothesis}
onChange={(hypothesis) => setDraft({ ...draft, hypothesis })}
/>
</label>
<label>
固定输入
<ResearchSelect
label="特征固定输入"
multiple
filter
value={draft.input_ids}
optionList={inputs.map((i) => ({
value: i.id,
label: `${i.dataset_id} · ${i.scope.region}/${i.scope.universe}/D${i.scope.delay} · ${i.field_ids.length} 字段 · ${i.id.slice(0, 8)}`,
}))}
onChange={(v) => setDraft({ ...draft, input_ids: v as string[] })}
/>
</label>
{inputs
.filter((i) => draft.input_ids.includes(i.id))
.map((i) => (
<details key={i.id}>
<summary>{i.dataset_id} 的固定字段</summary>
<p className="muted">{i.field_ids.join("、")}</p>
</details>
))}
<div className="section-toolbar">
<h3>处理与迭代步骤</h3>
<Button
onClick={() =>
setDraft({
...draft,
steps: [
...draft.steps,
{ name: "", rationale: "", expression: "" },
],
})
}
>
添加处理步骤
</Button>
</div>
{draft.steps.map((step, index) => (
<div className="research-card" key={index}>
<label>
步骤 {index + 1}
<Input
aria-label={`步骤 ${index + 1} 名称`}
value={step.name}
onChange={(name) =>
setDraft({
...draft,
steps: draft.steps.map((s, i) =>
i === index ? { ...s, name } : s,
),
})
}
/>
</label>
<label>
理由
<TextArea
aria-label={`步骤 ${index + 1} 理由`}
value={step.rationale}
onChange={(rationale) =>
setDraft({
...draft,
steps: draft.steps.map((s, i) =>
i === index ? { ...s, rationale } : s,
),
})
}
/>
</label>
<label>
候选表达式
<TextArea
aria-label={`步骤 ${index + 1} 表达式`}
value={step.expression}
onChange={(expression) =>
setDraft({
...draft,
steps: draft.steps.map((s, i) =>
i === index ? { ...s, expression } : s,
),
})
}
/>
</label>
<Button
type="danger"
theme="borderless"
onClick={() =>
setDraft({
...draft,
steps: draft.steps.filter((_, i) => i !== index),
})
}
>
删除此步骤
</Button>
</div>
))}
<h3>输出模板</h3>
<TemplateEditor
value={draft.template || blankTemplate()}
inputs={inputs.filter((i) => draft.input_ids.includes(i.id))}
onChange={(template) => setDraft({ ...draft, template })}
/>
<div className="inline-actions">
<Button
theme="solid"
loading={busy === "save"}
disabled={Boolean(busy)}
onClick={() =>
void run("save", async () => {
await save();
Toast.success("特征方案版本已保存");
})
}
>
保存特征方案
</Button>
<Button
disabled={Boolean(busy)}
loading={busy === "convert"}
onClick={() =>
void run("convert", async () => {
const fixed = await save();
const template = await post<Asset>(
`/research/features/${fixed.id}/template`,
{ version: fixed.version },
);
onAction({
type: "open_template",
asset_id: template.id,
version: template.version,
nonce: Date.now(),
});
})
}
>
保存并转为模板
</Button>
<Button
disabled={
Boolean(busy) || !draft.input_ids.length || !draft.hypothesis
}
loading={busy === "generate"}
onClick={() =>
void run("generate", async () => {
await load(
await post<FeatureAsset>("/research/generate", {
name: draft.name,
hypothesis: draft.hypothesis,
input_ids: draft.input_ids,
method: "feature",
}),
);
setRevision((n) => n + 1);
})
}
>
AI 生成新方案
</Button>
</div>
<p className="research-hint">
保存步骤不会计算原始时间序列。转换后的模板保留此方案的完整版本,回测前仍需展开校验并确认候选。
</p>
</div>
</div>
</section>
);
}
+106
View File
@@ -0,0 +1,106 @@
import { useEffect, useState } from "react";
import { Banner, Button, Empty, Toast } from "@douyinfe/semi-ui-19";
import { api } from "../api";
import type { UIAction } from "../ai/types";
import type { Experiment } from "./workspaceTypes";
type Graph = {
items: Experiment[];
edges: { child_id: string; parent_kind: string; parent_id: string }[];
truncated: boolean;
unresolved_experiment_ids: string[];
};
export function LineagePanel({
alphaId,
experimentId,
onAction,
}: {
alphaId?: string;
experimentId?: string;
onAction: (a: UIAction) => void;
}) {
const [graph, setGraph] = useState<Graph | null>(null);
const [focus, setFocus] = useState(experimentId);
useEffect(() => {
const c = new AbortController();
api<Graph>(
`/research/lineage?${focus ? `experiment_id=${encodeURIComponent(focus)}` : `alpha_id=${encodeURIComponent(alphaId || "")}`}`,
{ signal: c.signal },
)
.then(setGraph)
.catch((e) => {
if (!c.signal.aborted) Toast.error(e.message);
});
return () => c.abort();
}, [alphaId, focus]);
return (
<div className="detail-section">
<h3>变体关系</h3>
<p className="muted">
每个实验保留所有父来源;每次回测来源另见「研究来源」。选择实验可继续查看输入、设置差异、候选及回测。
</p>
{graph?.truncated && (
<Banner
type="info"
description="关系图按 100 个实验、8 层展示;可从边界实验继续展开。"
/>
)}
{graph?.items.length === 0 && <Empty description="尚无本地变体关系" />}
{graph?.items.map((e) => (
<div className="research-card" key={e.id}>
<h4>{e.name}</h4>
<p>{e.hypothesis}</p>
<p>
方法:{e.evidence.method || e.kind} · 模板{" "}
{e.evidence.template?.name || "—"}{" "}
{e.evidence.template?.version
? `v${e.evidence.template.version}`
: ""}
</p>
<div className="inline-actions">
{e.parents.map((p) => (
<Button
key={`${p.kind}:${p.id}`}
size="small"
onClick={() =>
p.kind === "alpha"
? onAction({
type: "open_alpha",
alpha_id: p.id,
nonce: Date.now(),
})
: setFocus(p.id)
}
>
父{p.kind === "alpha" ? " Alpha" : "实验"} {p.id.slice(0, 12)}
</Button>
))}
</div>
<p className="muted">
{e.candidates.length} 个候选 · {e.backtest_run_ids.length} 次回测
</p>
<div className="inline-actions">
<Button
onClick={() =>
onAction({
type: "open_experiment",
experiment_id: e.id,
nonce: Date.now(),
})
}
>
查看候选与来源
</Button>
<Button theme="borderless" onClick={() => setFocus(e.id)}>
以此实验展开关系
</Button>
</div>
</div>
))}
{graph?.unresolved_experiment_ids.map((id) => (
<Button key={id} onClick={() => setFocus(id)}>
继续展开 {id}
</Button>
))}
</div>
);
}
+5 -2
View File
@@ -8,13 +8,16 @@ export function ResearchToolCard({ call, onAction }: ToolCardProps) {
{typeof result.name === "string" ? result.name : "研究资料"}
{typeof result.version === "number" ? ` · v${result.version}` : ""}
</p>
{typeof result.id === "string" && (
{typeof result.id === "string" && !result.report && (
<Button
onClick={() =>
onAction(
result.content
? {
type: "open_template",
type:
result.kind === "feature"
? "open_feature"
: "open_template",
asset_id: result.id as string,
version: result.version as number,
nonce: Date.now(),
@@ -132,6 +132,24 @@ export function ResearchWorkspace({
);
setAsset(next);
setTemplate(next.content);
const feature = next.provenance?.feature;
if (feature) {
setInputIds(feature.content.input_ids);
setHypothesis(feature.content.hypothesis);
const fixed = feature.provenance?.inputs || [];
setInputs((old) => [
...old,
...fixed.filter((i) => !old.some((o) => o.id === i.id)),
]);
const first = fixed[0];
if (first)
setSettings((old) => ({
...old,
region: first.scope.region,
universe: first.scope.universe,
delay: first.scope.delay,
}));
}
});
if (action.type === "open_variant") {
setParent(action.alpha_id);
+113
View File
@@ -0,0 +1,113 @@
import { useEffect, useState } from "react";
import { Button, Input, Toast } from "@douyinfe/semi-ui-19";
import { api, post } from "../api";
import { ResearchSelect } from "./ResearchSelect";
export type ViewContent = {
name: string;
filters: Record<string, unknown>;
columns: string[];
};
type View = { id: string; version: number; content: ViewContent };
export function SavedViews({
filters,
columns,
onRestore,
}: {
filters: Record<string, unknown>;
columns: string[];
onRestore: (view: ViewContent) => void;
}) {
const [items, setItems] = useState<View[]>([]);
const [name, setName] = useState("");
const [selected, setSelected] = useState<string>();
const [busy, setBusy] = useState(false);
const [q, setQ] = useState("");
const [version, setVersion] = useState(0);
useEffect(() => {
const controller = new AbortController();
api<{ items: View[] }>(
`/research/assets?kind=view&q=${encodeURIComponent(q)}&limit=100`,
{ signal: controller.signal },
)
.then((r) => setItems(r.items))
.catch((e) => {
if (!controller.signal.aborted) Toast.error(e.message);
});
return () => controller.abort();
}, [q, version]);
async function save(replace: boolean) {
setBusy(true);
try {
const old = items.find((i) => i.id === selected);
if (replace && !old) return;
const content = {
name: name.trim() || old?.content.name || "研究队列",
filters: Object.fromEntries(
Object.entries(filters).filter(
([, v]) => v !== "" && v !== null && v !== undefined,
),
),
columns,
};
const value = replace
? await api<View>(`/research/assets/${old!.id}`, {
method: "PUT",
body: JSON.stringify({
kind: "view",
version: old!.version,
content,
}),
})
: await post<View>("/research/assets", { kind: "view", content });
setSelected(value.id);
setName(value.content.name);
setVersion((n) => n + 1);
Toast.success("筛选视图已保存");
} catch (e) {
Toast.error((e as Error).message);
} finally {
setBusy(false);
}
}
return (
<div className="research-saved-views inline-actions">
<ResearchSelect
label="保存的 Alpha 视图"
placeholder="搜索并恢复研究队列"
filter
remote
onSearch={setQ}
value={selected}
optionList={items.map((i) => ({
value: i.id,
label: `${i.content.name} · v${i.version}`,
}))}
onChange={(v) => {
const item = items.find((i) => i.id === v);
if (item) {
setSelected(item.id);
setName(item.content.name);
onRestore(item.content);
}
}}
/>
<Input
aria-label="视图名称"
placeholder="视图名称"
value={name}
onChange={setName}
style={{ width: 160 }}
/>
<Button size="small" disabled={busy} onClick={() => void save(false)}>
保存为新视图
</Button>
<Button
size="small"
disabled={busy || !selected}
onClick={() => void save(true)}
>
更新当前视图
</Button>
</div>
);
}
+9
View File
@@ -226,3 +226,12 @@
clip-path: inset(50%);
white-space: nowrap;
}
.research-saved-views {
padding: 8px 16px;
border-bottom: 1px solid var(--line);
flex-wrap: wrap;
}
.research-saved-views .semi-select {
min-width: 210px;
}
+6
View File
@@ -25,6 +25,12 @@ export type Asset = {
kind: string;
version: number;
content: Template;
provenance?: {
feature?: {
content: { input_ids: string[]; hypothesis: string };
provenance?: { inputs?: InputSnapshot[] };
};
};
created_at?: string;
};
export type InputSnapshot = {
+161
View File
@@ -0,0 +1,161 @@
import { expect, test } from "@playwright/test";
const headers = { "X-WQ-Request": "1" };
const scope = {
instrument_type: "EQUITY",
region: "USA",
universe: "TOP3000",
delay: 1,
};
test("feature conversion, saved views and immutable evaluations", async ({
page,
}) => {
const errors: string[] = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.goto("/#features");
await page.getByLabel("密码", { exact: true }).fill("browser-test-password");
await page.getByRole("button", { name: "进入工作空间" }).click();
await expect(
page.getByRole("heading", { name: "特征工程", exact: true }),
).toBeVisible();
await page.request.put("/api/v1/account/credentials", {
headers,
data: { email: "test@example.com", password: "synthetic-password" },
});
await page.request.post("/api/v1/account/connect", { headers });
await expect
.poll(
async () =>
(await (await page.request.get("/api/v1/account")).json())
.connection_status,
)
.toBe("connected");
for (const dataset_id of [null, "TEST_FIN"]) {
const result = await page.request.post("/api/v1/catalog/sync-jobs", {
headers,
data: { scope, dataset_id },
});
const job = await result.json();
await expect
.poll(
async () =>
(await (await page.request.get(`/api/v1/sync-jobs/${job.id}`)).json())
.status,
)
.toBe("completed");
}
const params = new URLSearchParams(
Object.entries(scope).map(([k, v]) => [k, String(v)]),
);
const fields = await (
await page.request.get(`/api/v1/catalog/datasets/TEST_FIN/fields?${params}`)
).json();
const fixed = await (
await page.request.post("/api/v1/catalog/inputs", {
headers,
data: {
scope,
dataset_id: "TEST_FIN",
collection_version: fields.collection_version,
selection: "all",
},
})
).json();
expect(fixed.id).toBeTruthy();
await page.reload();
await page.getByLabel("特征方案名称", { exact: true }).fill("浏览器特征验收");
await page
.getByLabel("特征经济假设", { exact: true })
.fill("排名可降低异常值影响");
await page.getByRole("combobox", { name: "特征固定输入" }).click();
await page
.getByRole("option")
.filter({ hasText: fixed.id.slice(0, 8) })
.click();
await page.getByRole("heading", { name: "特征工程", exact: true }).click();
await page.getByRole("button", { name: "添加处理步骤" }).click();
await page.getByLabel("步骤 1 名称").fill("横截面排名");
await page.getByLabel("步骤 1 理由").fill("减少尺度影响");
await page.getByLabel("步骤 1 表达式").fill("rank(TEST_FIN_001)");
await page
.getByRole("textbox", { name: "模板名称", exact: true })
.fill("特征转换模板");
await page.getByLabel("field 候选取值").fill("TEST_FIN_001");
await page.getByRole("button", { name: "保存特征方案", exact: true }).click();
await expect(
page.getByText("特征方案版本已保存", { exact: true }),
).toBeVisible();
await page.getByRole("button", { name: "保存并转为模板" }).click();
await expect(page).toHaveURL(/#templates$/);
await expect(
page.getByRole("textbox", { name: "模板名称", exact: true }),
).toHaveValue("特征转换模板");
const templates = await (
await page.request.get(
"/api/v1/research/assets?kind=template&q=特征转换模板",
)
).json();
expect(templates.items[0].provenance.feature.content.input_ids).toEqual([
fixed.id,
]);
await page.getByRole("button", { name: "Alpha 管理", exact: true }).click();
await page.getByLabel("视图名称", { exact: true }).fill("浏览器常用队列");
await page.getByRole("button", { name: "保存为新视图" }).click();
await expect(page.getByText("筛选视图已保存", { exact: true })).toBeVisible();
const views = await (
await page.request.get("/api/v1/research/assets?kind=view&q=浏览器常用队列")
).json();
expect(views.items[0].content.filters.submission).toBe("UNSUBMITTED");
expect(views.items[0].content.columns).toContain("name");
await page
.getByRole("button", { name: "导入 Alpha ID", exact: true })
.click();
await page.getByLabel("Alpha IDs", { exact: true }).fill("TEST0001");
const importing = page.waitForResponse(
(r) =>
r.url().endsWith("/api/v1/sync-jobs") && r.request().method() === "POST",
);
await page
.getByRole("dialog")
.getByRole("button", { name: "confirm", exact: true })
.click();
await expect
.poll(async () =>
(await page.request.get("/api/v1/alphas/TEST0001")).status(),
)
.toBe(200);
const importJob = await (await importing).json();
await expect
.poll(
async () =>
(
await (
await page.request.get(`/api/v1/sync-jobs/${importJob.id}`)
).json()
).status,
)
.toBe("completed");
await page.reload();
await page.getByRole("textbox", { name: "搜索 Alpha" }).fill("TEST0001");
await page.getByRole("button", { name: "查询", exact: true }).click();
await expect(page.locator(".alpha-link")).toHaveCount(1);
const alpha = await (
await page.request.get("/api/v1/alphas/TEST0001")
).json();
await page
.getByRole("button", { name: alpha.name || alpha.id, exact: true })
.click();
await page.getByRole("tab", { name: "评估报告", exact: true }).click();
await page.getByRole("button", { name: "保存规则评估" }).click();
await expect(
page.getByText("规则:Sharpe ≥", { exact: false }),
).toBeVisible();
const reports = await (
await page.request.get("/api/v1/research/evaluations?alpha_id=TEST0001")
).json();
expect(reports.items).toHaveLength(1);
await page.screenshot({
path: "/tmp/wq-stage-two-evaluation.png",
fullPage: true,
});
expect(errors).toEqual([]);
});
+2
View File
@@ -59,6 +59,7 @@ test("account → scoped sync → research → resync → PnL → filtered expor
await page.getByLabel("同步开始日期").fill("2025-01-01");
await page.getByLabel("同步结束日期").fill("2025-01-01");
await page.getByRole("button", { name: "开始同步", exact: true }).click();
await expect(page.locator(".job-panel")).toBeVisible();
await expect
.poll(
async () =>
@@ -72,6 +73,7 @@ test("account → scoped sync → research → resync → PnL → filtered expor
await page.keyboard.press("Escape");
await page.getByRole("tab", { name: "已提交", exact: true }).click();
await page.getByRole("button", { name: "全量同步已提交" }).click();
await expect(page.locator(".job-panel")).toBeVisible();
await expect
.poll(
async () =>