Files
worldquant-alpha-system/frontend/src/ai/workspace.ts
T

57 lines
2.8 KiB
TypeScript
Raw Normal View History

import type { PageContext, UIAction } from "./types";
export type WorkspacePage = PageContext["page"];
const contextLabels: Record<WorkspacePage, (context: PageContext) => string> = {
alphas: (context) =>
`上下文:${context.alpha_id ? `Alpha ${context.alpha_id}` : "Alpha 列表"}${context.selected_ids?.length ? ` · 已选 ${context.selected_ids.length} 条` : ""}`,
operators: () => "上下文:算子库",
templates: (context) =>
`上下文:模板工坊${context.research_asset_id ? ` · ${context.research_asset_id}` : ""}${context.research_experiment_id ? ` · 实验 ${context.research_experiment_id}` : ""}`,
quantflow: (context) =>
`上下文:QuantFlow${context.research_run_id ? ` · ${context.research_run_id}` : ""}`,
pipeline: (context) =>
`上下文:研究流水线${context.research_run_id ? ` · ${context.research_run_id}` : ""}`,
features: () => "上下文:特征工程",
variants: () => "上下文:Alpha 变体",
account: () => "上下文:个人信息页",
backtests: () => "上下文:回测研究",
datasets: (context) =>
`上下文:${context.dataset_id ?? "数据目录"}${context.catalog_scope ? ` · ${context.catalog_scope.region}/${context.catalog_scope.universe}/D${context.catalog_scope.delay}` : ""}${context.template_input_id ? " · 固定研究输入" : context.unsaved_field_selection ? " · 请先保存字段选择" : ""}(不发送未保存备注)`,
};
export function contextLabel(context: PageContext): string {
return contextLabels[context.page](context);
}
export function pageFromHash(hash: string): WorkspacePage {
const value = hash.replace(/^#/, "");
const page = value.startsWith("quantflow/") ? "quantflow" : value;
return Object.hasOwn(contextLabels, page)
? (page as WorkspacePage)
: "alphas";
}
type Destination = {
page?: WorkspacePage;
chat: "open" | "close" | "responsive";
};
// 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" },
open_conversation: { chat: "open" },
open_research_input: { page: "datasets", chat: "close" },
open_backtest: { page: "backtests", chat: "responsive" },
open_backtest_preview: { page: "backtests", chat: "responsive" },
open_alpha: { page: "alphas", chat: "responsive" },
apply_filters: { page: "alphas", chat: "responsive" },
};
export function actionDestination(action: UIAction): Destination | undefined {
return Object.hasOwn(destinations, action.type)
? destinations[action.type]
: undefined;
}