387 lines
16 KiB
TypeScript
387 lines
16 KiB
TypeScript
/**
|
||
* 外部系统跳转登录时的「任务深链」:登录 URL 带 ``taskId`` + ``goPath`` 时,
|
||
* 拉取任务详情并据 goPath 把用户直接送到对应的智能体问答 / 多智能体会商页。
|
||
*
|
||
* 流程:
|
||
* 1. 调 consumer 接口 ``GET {VITE_AIPI_CONSUMER_URL}/taskAnalyseSearch/cop-task-detail?taskId=``,
|
||
* 请求头 ``Authorization: Bearer admin``(由 service 拦截器统一注入)。
|
||
* 2. 把返回的 data(id/overview/taskDirection/taskName/taskContent)整包存到 sessionStorage。
|
||
* 3. 按 goPath 解析出业务码 + 模式:
|
||
* - ``3qfx`` / ``rwfx`` / ``xdfx`` → 单智能体问答(用「配置管理」里该业务配置的智能体)
|
||
* - ``3qfx-A`` / ``rwfx-A`` / ``xdfx-A`` → 多智能体会商(用该业务配置的业务链条)
|
||
* 携带 taskName / taskContent 作为开场信息。
|
||
*
|
||
* 业务码映射与「配置管理」(`/api/business-mapping`) 一致:
|
||
* 3Q=三情分析(3QFX)、6BF=任务分析(任务FX)、7BF=行动分析(XDFX)。
|
||
*/
|
||
|
||
import { requestCrudService } from "@/service/request-axios-service";
|
||
import { clearTaskContext, persistTaskContext } from "@/core/auth/task-purpose";
|
||
import { fetchTaskDeeplinkConfig } from "@/core/auth/task-deeplink-config";
|
||
import { readRuntimeEnv, readRuntimeFlag } from "@/env";
|
||
import { listBusinessMappings } from "@/strategy-components/api/business-mapping";
|
||
|
||
/** xdfx 业务码:深链传入的是「行动id」,需先调 action 接口解析出真正的「任务id」。 */
|
||
const XDFX_BUSINESS_CODE = "7BF";
|
||
|
||
/** consumer 接口返回的任务详情(data 字段)。 */
|
||
export interface CopTaskDetail {
|
||
id: string | number | null;
|
||
overview: string;
|
||
taskDirection: string;
|
||
taskName: string;
|
||
taskContent: string;
|
||
}
|
||
|
||
/** 任务详情整包暂存的 sessionStorage key(目标页/后续可读取)。 */
|
||
export const COP_TASK_DETAIL_STORAGE_KEY = "login.copTaskDetail";
|
||
|
||
/** 行动详情整包暂存的 sessionStorage key(xdfx 行动分析:会商/单智能体读取行动名称/叙事)。 */
|
||
export const ACTION_DETAIL_STORAGE_KEY = "login.actionDetail";
|
||
|
||
/** goPath 基础码 → 业务码(配置管理 businessCode)。 */
|
||
const GOPATH_BUSINESS_CODE: Record<string, string> = {
|
||
"3qfx": "3Q", // 三情分析
|
||
rwfx: "6BF", // 任务分析(任务FX)
|
||
xdfx: "7BF", // 行动分析(XDFX)
|
||
};
|
||
|
||
/**
|
||
* 业务码 → 「任务管理」布局下的路由前缀(与 sidebar-menu.ts 的 COH 中任务菜单路径一致)。
|
||
* 会商 / 智能体问答都挂在此前缀下,保证侧边栏停留在 任务管理 → COH中任务 → 对应业务 高亮。
|
||
*/
|
||
const BUSINESS_TASK_BASE: Record<string, string> = {
|
||
"3Q": "/page/strategy/task-management/plan-task-sentiment",
|
||
"6BF": "/page/strategy/task-management/plan-task/plan-task-fx",
|
||
"7BF": "/page/strategy/task-management/plan-task/plan-task-action",
|
||
};
|
||
|
||
export interface ParsedGoPath {
|
||
businessCode: string;
|
||
/** true = 多智能体会商(``-A`` 后缀);false = 单智能体问答。 */
|
||
roundtable: boolean;
|
||
}
|
||
|
||
/** 解析 goPath(大小写不敏感);``-A`` 后缀表示多智能体会商。无法识别返回 null。 */
|
||
export function parseGoPath(goPath: string): ParsedGoPath | null {
|
||
const raw = goPath.trim().toLowerCase();
|
||
if (!raw) return null;
|
||
const roundtable = raw.endsWith("-a");
|
||
const base = roundtable ? raw.slice(0, -2) : raw;
|
||
const businessCode = GOPATH_BUSINESS_CODE[base];
|
||
if (!businessCode) return null;
|
||
return { businessCode, roundtable };
|
||
}
|
||
|
||
function consumerBaseUrl(): string {
|
||
const value = readRuntimeEnv("VITE_AIPI_CONSUMER_URL");
|
||
return typeof value === "string" ? value.replace(/\/+$/, "") : "";
|
||
}
|
||
|
||
/** cop-task-detail 是否走本地假数据:开关 `VITE_MOCK_COP_TASK_DETAIL=true`(真实接口未就绪时调试用)。 */
|
||
function copTaskDetailMockEnabled(): boolean {
|
||
return readRuntimeFlag("VITE_MOCK_COP_TASK_DETAIL");
|
||
}
|
||
|
||
/** cop-task-detail 本地假数据(与真实 data 字段同构;mock 开关开启时返回)。 */
|
||
function mockCopTaskDetail(taskId: string): CopTaskDetail {
|
||
return {
|
||
id: taskId || "2055",
|
||
overview: "暑期文旅消费高峰期间的舆情态势综合研判与引导任务(本地假数据)。",
|
||
taskDirection: "正向引导 + 风险预警",
|
||
taskName: "任务01:暑期文旅舆情综合研判",
|
||
taskContent:
|
||
"围绕暑期文旅消费高峰,对重点城市、重点景区的公众情绪与舆情态势做综合研判,识别情绪拐点与潜在风险,提出正向引导与处置建议。",
|
||
};
|
||
}
|
||
|
||
/** 详情接口不可用时仍能继续深链跳转:至少带上任务 id,标题/简介可空。 */
|
||
export function stubCopTaskDetail(taskId: string): CopTaskDetail {
|
||
return {
|
||
id: taskId,
|
||
overview: "",
|
||
taskDirection: "",
|
||
taskName: "",
|
||
taskContent: "",
|
||
};
|
||
}
|
||
|
||
/** 拉取任务详情;``VITE_MOCK_COP_TASK_DETAIL=true`` 走假数据;缺少 consumer 地址 / 返回不合法 / 网络失败时返回 null。 */
|
||
export async function fetchCopTaskDetail(taskId: string): Promise<CopTaskDetail | null> {
|
||
if (copTaskDetailMockEnabled()) {
|
||
return mockCopTaskDetail(taskId);
|
||
}
|
||
const base = consumerBaseUrl();
|
||
if (!base || !taskId) return null;
|
||
try {
|
||
// service 拦截器已统一注入 `Authorization: Bearer admin` 并返回 response.data(整包 body)。
|
||
const body = await requestCrudService.get(`${base}/taskAnalyseSearch/cop-task-detail`, { taskId });
|
||
const data = body && typeof body === "object" ? (body as { data?: unknown }).data : null;
|
||
if (!data || typeof data !== "object") return null;
|
||
const d = data as Record<string, unknown>;
|
||
return {
|
||
id: (d.id as string | number | null) ?? null,
|
||
overview: typeof d.overview === "string" ? d.overview : "",
|
||
taskDirection: typeof d.taskDirection === "string" ? d.taskDirection : "",
|
||
taskName: typeof d.taskName === "string" ? d.taskName : "",
|
||
taskContent: typeof d.taskContent === "string" ? d.taskContent : "",
|
||
};
|
||
} catch (error) {
|
||
console.warn("[task-deeplink] cop-task-detail failed", { taskId, error });
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/** 把任务详情整包暂存到 sessionStorage(隐私模式/quota 异常时静默失败)。 */
|
||
export function persistCopTaskDetail(detail: CopTaskDetail): void {
|
||
try {
|
||
sessionStorage.setItem(COP_TASK_DETAIL_STORAGE_KEY, JSON.stringify(detail));
|
||
} catch {
|
||
/* noop */
|
||
}
|
||
}
|
||
|
||
/** 读取暂存的任务详情。 */
|
||
export function readCopTaskDetail(): CopTaskDetail | null {
|
||
try {
|
||
const raw = sessionStorage.getItem(COP_TASK_DETAIL_STORAGE_KEY);
|
||
return raw ? (JSON.parse(raw) as CopTaskDetail) : null;
|
||
} catch {
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/** 把行动详情整包暂存到 sessionStorage(xdfx 行动分析用;隐私模式/quota 异常时静默失败)。 */
|
||
export function persistActionDetail(detail: ActionDetail): void {
|
||
try {
|
||
sessionStorage.setItem(ACTION_DETAIL_STORAGE_KEY, JSON.stringify(detail));
|
||
} catch {
|
||
/* noop */
|
||
}
|
||
}
|
||
|
||
/** 读取暂存的行动详情(会商左上角「当前行动」、单智能体行动详情卡都可读)。 */
|
||
export function readActionDetail(): ActionDetail | null {
|
||
try {
|
||
const raw = sessionStorage.getItem(ACTION_DETAIL_STORAGE_KEY);
|
||
return raw ? (JSON.parse(raw) as ActionDetail) : null;
|
||
} catch {
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/** 行动详情接口 ``{action_detail_url}/{actionId}`` 返回的 ``data`` 字段(xdfx 行动分析用)。 */
|
||
export interface ActionDetail {
|
||
/** 行动id(深链 ?taskId= 传入的就是它)。 */
|
||
id: string;
|
||
/** 该行动归属的真正「任务id」。 */
|
||
taskId: string;
|
||
/** 所属目的id。 */
|
||
purposeId: string;
|
||
/** 行动名称。 */
|
||
actionName: string;
|
||
/** 行动状态码。 */
|
||
actionStatus: number | string;
|
||
/** 行动叙事 / 描述。 */
|
||
narration: string;
|
||
}
|
||
|
||
/**
|
||
* xdfx 深链:用「行动id」调行动详情接口 ``{action_detail_url}/{actionId}``,取出整包行动详情。
|
||
* ``action_test=true`` 时直接用后端配置里的假数据(接口未通时用)。
|
||
* 缺地址 / 返回不合法 → 返回 null。
|
||
*/
|
||
export async function fetchActionDetail(actionId: string): Promise<ActionDetail | null> {
|
||
if (!actionId) return null;
|
||
const cfg = await fetchTaskDeeplinkConfig();
|
||
let body: unknown;
|
||
if (cfg.actionTest) {
|
||
body = cfg.mock.actionDetail;
|
||
} else if (cfg.actionDetailUrl) {
|
||
const base = cfg.actionDetailUrl.replace(/\/+$/, "");
|
||
// service 拦截器已统一注入 Authorization: Bearer admin 并返回整包 body;行动id 走路径段。
|
||
body = await requestCrudService.get(`${base}/${encodeURIComponent(actionId)}`);
|
||
} else {
|
||
return null;
|
||
}
|
||
const data = body && typeof body === "object" ? (body as { data?: unknown }).data : null;
|
||
if (!data || typeof data !== "object") return null;
|
||
const d = data as Record<string, unknown>;
|
||
const detail: ActionDetail = {
|
||
id: d.id != null ? String(d.id) : "",
|
||
taskId: d.taskId != null ? String(d.taskId) : "",
|
||
purposeId: d.purposeId != null ? String(d.purposeId) : "",
|
||
actionName: typeof d.actionName === "string" ? d.actionName : "",
|
||
actionStatus:
|
||
typeof d.actionStatus === "number" || typeof d.actionStatus === "string" ? d.actionStatus : "",
|
||
narration: typeof d.narration === "string" ? d.narration : "",
|
||
};
|
||
// 取到即整包暂存:登录解析任务id 时(resolveDeeplinkTaskId→fetchTaskIdFromAction)就会缓存它,
|
||
// 之后会商左上角「当前行动」、单智能体行动详情卡都能直接 readActionDetail(),避免重复请求。
|
||
persistActionDetail(detail);
|
||
return detail;
|
||
}
|
||
|
||
/**
|
||
* xdfx 深链:用「行动id」解析出真正的「任务id」(取行动详情的 ``data.taskId``)。
|
||
* 缺地址 / 返回不合法 → 返回 ""(调用方据此回退)。
|
||
*/
|
||
export async function fetchTaskIdFromAction(actionId: string): Promise<string> {
|
||
return (await fetchActionDetail(actionId))?.taskId ?? "";
|
||
}
|
||
|
||
/**
|
||
* 据 goPath 把深链 URL 里的原始 id 解析成真正的「任务id」:
|
||
* - xdfx(行动分析):URL 传入的是「行动id」,调 action 接口换成 ``data.taskId``;
|
||
* 解析失败返回 ""(让调用方回退默认首页,避免拿行动id当任务id污染后续查询/绑定)。
|
||
* - 其它业务:URL 传入的本就是任务id,原样返回。
|
||
*/
|
||
export async function resolveDeeplinkTaskId(goPath: string, rawId: string): Promise<string> {
|
||
const parsed = parseGoPath(goPath);
|
||
if (parsed?.businessCode === XDFX_BUSINESS_CODE) {
|
||
try {
|
||
return (await fetchTaskIdFromAction(rawId)) || "";
|
||
} catch (error) {
|
||
console.warn("[task-deeplink] action→taskId resolve failed", { rawId, error });
|
||
return "";
|
||
}
|
||
}
|
||
return rawId;
|
||
}
|
||
|
||
export interface TaskDeeplinkTarget {
|
||
path: string;
|
||
/** react-router navigate 的 state(多智能体会商用 roundtablePrefill)。 */
|
||
state?: Record<string, unknown>;
|
||
}
|
||
|
||
/** xdfx(行动分析)开场信息里要追加的行动信息:行动标题 + 行动内容 + 行动id。 */
|
||
export interface ActionOpeningInfo {
|
||
/** 行动id(action_id)。 */
|
||
actionId?: string;
|
||
/** 行动标题(行动名称 actionName)。 */
|
||
actionName?: string;
|
||
/** 行动内容(行动叙事 narration)。 */
|
||
narration?: string;
|
||
}
|
||
|
||
/**
|
||
* 用 taskName / taskContent 拼出开场信息文本;末尾附上任务 id(task_id),供后端识别任务。
|
||
* xdfx(行动分析)额外附上「行动标题 / 行动内容 / 行动id(action_id)」——传入 action 时追加这几行。
|
||
*/
|
||
function buildOpeningText(detail: CopTaskDetail, taskId: string, action?: ActionOpeningInfo): string {
|
||
return [
|
||
detail.taskName ? `任务名称:${detail.taskName}` : "",
|
||
detail.taskContent ? `任务内容:${detail.taskContent}` : "",
|
||
taskId ? `任务id(task_id)为${taskId}` : "",
|
||
action?.actionName ? `行动标题:${action.actionName}` : "",
|
||
action?.narration ? `行动内容:${action.narration}` : "",
|
||
action?.actionId ? `行动id(action_id)为${action.actionId}` : "",
|
||
]
|
||
.filter(Boolean)
|
||
.join("\n");
|
||
}
|
||
|
||
/**
|
||
* 3qfx/rwfx 这类“按任务id 直接问答”的任务工作区,在 ``TaskContext`` 丢失但
|
||
* ``login.copTaskDetail`` 仍存在时,可用它重新拼出开场词做前端自恢复。
|
||
*/
|
||
export function buildTaskOpeningText(
|
||
detail: CopTaskDetail,
|
||
taskId: string,
|
||
action?: ActionOpeningInfo,
|
||
): string {
|
||
return buildOpeningText(detail, taskId, action);
|
||
}
|
||
|
||
/**
|
||
* 据 goPath + 任务详情解析跳转目标。
|
||
* - 多智能体会商:返回带 ``roundtablePrefill``(businessCode 让会商页自动选用配置的业务链条;
|
||
* taskId 让会商页按任务隔离存取草稿/聊天记录)的 state。
|
||
* - 单智能体问答:解析「配置管理」里该业务配置的智能体;把开场信息写入
|
||
* ``sessionStorage.pendingAgentMessage``(AgentChatPage 自动发出)。
|
||
* 无法识别 goPath / 未配置智能体时返回 null(调用方回退到默认首页)。
|
||
*
|
||
* @param taskId 真实任务 id(xdfx 已由 action 接口解析;其它业务即深链原始 ?taskId=)。
|
||
* 用于按任务隔离草稿/对话;与 consumer 返回的 detail.id 区分。
|
||
* @param rawId 深链 URL 的原始 ``?taskId=``(xdfx 为「行动id」)。供需追加行动id 的按钮用;
|
||
* 省略时回退 taskId。
|
||
*/
|
||
export async function resolveTaskDeeplinkTarget(
|
||
goPath: string,
|
||
detail: CopTaskDetail,
|
||
taskId: string,
|
||
rawId?: string,
|
||
): Promise<TaskDeeplinkTarget | null> {
|
||
const parsed = parseGoPath(goPath);
|
||
if (!parsed) return null;
|
||
const taskBase = BUSINESS_TASK_BASE[parsed.businessCode];
|
||
if (!taskBase) return null;
|
||
|
||
// 清掉上一次深链可能残留的任务上下文(单智能体分支会用本次任务重新写入)。
|
||
clearTaskContext();
|
||
|
||
if (parsed.roundtable) {
|
||
// 多智能体会商:嵌进对应业务的「任务管理」布局;businessCode 让会商页自动选用配置的业务链条;
|
||
// taskId 让会商页按任务隔离存取草稿(一个 taskId 多条记录),并触发第三步自动结构化输出。
|
||
// xdfx(行动分析):根是「行动」——把行动名称随首条消息交给任务理解,左上角展示「当前行动」。
|
||
// 行动详情登录解析任务id 时已缓存(fetchActionDetail 内 persistActionDetail);这里优先读缓存。
|
||
let actionName = "";
|
||
let actionId = "";
|
||
if (parsed.businessCode === XDFX_BUSINESS_CODE && rawId) {
|
||
actionId = rawId;
|
||
const ad = readActionDetail() ?? (await fetchActionDetail(rawId));
|
||
actionName = ad?.actionName ?? "";
|
||
}
|
||
return {
|
||
path: `${taskBase}/roundtable`,
|
||
state: {
|
||
roundtablePrefill: {
|
||
taskTitle: detail.taskName,
|
||
taskGoal: detail.taskContent,
|
||
businessCode: parsed.businessCode,
|
||
taskId: taskId || undefined,
|
||
...(actionId ? { actionId } : {}),
|
||
...(actionName ? { actionName } : {}),
|
||
autoStart: true,
|
||
},
|
||
},
|
||
};
|
||
}
|
||
|
||
let agentId: string | null = null;
|
||
try {
|
||
const mappings = await listBusinessMappings();
|
||
agentId = mappings.find((m) => m.businessCode === parsed.businessCode)?.agentId ?? null;
|
||
} catch (error) {
|
||
console.warn("[task-deeplink] business mapping failed", error);
|
||
agentId = null;
|
||
}
|
||
if (!agentId) {
|
||
console.warn("[task-deeplink] no agent mapped for", parsed.businessCode);
|
||
return null;
|
||
}
|
||
|
||
// 单智能体问答统一走「任务工作区」(任务↔对话 1:N,按 taskId + agentId 隔离不同业务的对话列表):
|
||
// 暂存任务上下文(taskId + agentId + 开场信息),跳到 chats 索引,由工作区按 taskId 决定
|
||
// 开最新历史对话 / 新建。rwfx 在工作区里额外过「目的树闸门」;3qfx/xdfx 仅 1:N 列表,
|
||
// 新建对话直接带开场信息自动发出(见 task-purpose / AgentChatPage 的 taskGate / taskBind)。
|
||
// xdfx(行动分析):聊天记录按「行动id」划分(partitionByAction),且开场信息追加
|
||
// 行动标题 / 行动内容 / action_id(行动详情登录解析任务id 时已缓存,优先读缓存避免重复请求)。
|
||
const isXdfx = parsed.businessCode === XDFX_BUSINESS_CODE;
|
||
const actionId = rawId || taskId;
|
||
let actionInfo: ActionOpeningInfo | undefined;
|
||
if (isXdfx) {
|
||
const ad = rawId ? (readActionDetail() ?? (await fetchActionDetail(rawId))) : null;
|
||
actionInfo = { actionId, actionName: ad?.actionName, narration: ad?.narration };
|
||
}
|
||
persistTaskContext({
|
||
taskId,
|
||
agentId,
|
||
openingText: buildOpeningText(detail, taskId, actionInfo),
|
||
actionId,
|
||
partitionByAction: isXdfx,
|
||
});
|
||
return { path: `${taskBase}/agent/${agentId}/chats` };
|
||
}
|