mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 13:49:19 +08:00
fix: 草稿首次落库前先改绑发布助手会话,避免路由重挂新建空会话
This commit is contained in:
@@ -115,7 +115,13 @@ export async function submitAgentClientToolResultApi(
|
||||
|
||||
export async function patchAgentSessionApi(
|
||||
threadId: string,
|
||||
body: { title?: string; llmProviderId?: number; llmModel?: string },
|
||||
body: {
|
||||
title?: string;
|
||||
llmProviderId?: number;
|
||||
llmModel?: string;
|
||||
/** 未落库会话(local-*)改绑到系统草稿 ID */
|
||||
sceneRef?: string;
|
||||
},
|
||||
) {
|
||||
return requestClient.patch<AgentVO.Session>(
|
||||
`/agent/sessions/${threadId}`,
|
||||
|
||||
+72
-2
@@ -50,6 +50,8 @@ const emit = defineEmits<{
|
||||
}>();
|
||||
|
||||
const SCENE = 'ecom_goods_publish' as const;
|
||||
/** 跨路由重挂:记住未落库会话 threadId,保存草稿后改绑用 */
|
||||
const PENDING_LOCAL_THREAD_KEY = 'pdd-publish-agent-pending-thread';
|
||||
|
||||
/** 未落库时用本地 sceneRef,落库后切到草稿 ID */
|
||||
const localSceneKey = `local-${
|
||||
@@ -64,6 +66,30 @@ const sceneRef = computed(() =>
|
||||
: localSceneKey,
|
||||
);
|
||||
|
||||
function rememberPendingLocalThread(threadId: string) {
|
||||
try {
|
||||
sessionStorage.setItem(PENDING_LOCAL_THREAD_KEY, threadId);
|
||||
} catch {
|
||||
/* ignore quota / private mode */
|
||||
}
|
||||
}
|
||||
|
||||
function peekPendingLocalThread(): string | null {
|
||||
try {
|
||||
return sessionStorage.getItem(PENDING_LOCAL_THREAD_KEY);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function clearPendingLocalThread() {
|
||||
try {
|
||||
sessionStorage.removeItem(PENDING_LOCAL_THREAD_KEY);
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
const sessionTitle = computed(() =>
|
||||
props.draftId != null && props.draftId > 0
|
||||
? `发布草稿 #${props.draftId}`
|
||||
@@ -243,16 +269,21 @@ async function loadEnabledModels() {
|
||||
async function ensureSession(): Promise<string | null> {
|
||||
if (session.value?.threadId) return session.value.threadId;
|
||||
const parsed = parseModelKey(selectedModelKey.value);
|
||||
const ref = sceneRef.value;
|
||||
const s = await createAgentSessionApi({
|
||||
title: sessionTitle.value,
|
||||
llmProviderId: parsed?.providerId,
|
||||
llmModel: parsed?.model,
|
||||
scene: SCENE,
|
||||
sceneRef: sceneRef.value,
|
||||
sceneRef: ref,
|
||||
reuseBySceneRef: true,
|
||||
});
|
||||
session.value = s;
|
||||
syncSelectedFromSession();
|
||||
// 未落库会话写入 pending,供 new→真实 ID 路由重挂后改绑
|
||||
if (ref.startsWith('local-') && s.threadId) {
|
||||
rememberPendingLocalThread(s.threadId);
|
||||
}
|
||||
return s.threadId;
|
||||
}
|
||||
|
||||
@@ -278,6 +309,36 @@ async function loadMessages(threadId: string) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 草稿首次落库:把 local-* 会话改绑到草稿 ID。
|
||||
* 须在路由 replace 之前调用:new→id 常会整页重挂,内存会话会丢。
|
||||
*/
|
||||
async function rebindSessionToDraft(draftId: number): Promise<boolean> {
|
||||
let threadId = session.value?.threadId || peekPendingLocalThread();
|
||||
if (!threadId) {
|
||||
// 尚未 ensure 时先建临时会话再改绑
|
||||
if (props.draftId == null) {
|
||||
threadId = (await ensureSession()) ?? null;
|
||||
}
|
||||
}
|
||||
if (!threadId) return false;
|
||||
try {
|
||||
const s = await patchAgentSessionApi(threadId, {
|
||||
sceneRef: String(draftId),
|
||||
title: `发布草稿 #${draftId}`,
|
||||
});
|
||||
session.value = session.value
|
||||
? { ...session.value, ...s }
|
||||
: s;
|
||||
clearPendingLocalThread();
|
||||
return true;
|
||||
} catch (e: any) {
|
||||
// 该草稿已有别的会话等冲突:不打断当前编辑
|
||||
console.warn(e?.message || '发布助手会话改绑失败');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function bootstrap() {
|
||||
loadingSession.value = true;
|
||||
messages.value = [];
|
||||
@@ -286,6 +347,10 @@ async function bootstrap() {
|
||||
citations.value = [];
|
||||
try {
|
||||
await loadEnabledModels();
|
||||
// 路由重挂后 draftId 已是真实 ID:先尝试把 pending 的 local 会话改绑过来
|
||||
if (props.draftId != null && props.draftId > 0 && peekPendingLocalThread()) {
|
||||
await rebindSessionToDraft(props.draftId);
|
||||
}
|
||||
const threadId = await ensureSession();
|
||||
if (threadId) await loadMessages(threadId);
|
||||
} catch (e: any) {
|
||||
@@ -298,8 +363,9 @@ async function bootstrap() {
|
||||
watch(
|
||||
() => props.draftId,
|
||||
(id, prev) => {
|
||||
// 新建落库后仅 URL 从 new→id:沿用当前会话,避免打断
|
||||
// 同页内 new→id 且会话仍在:改绑后不重建(父级也会在 replace 前主动改绑)
|
||||
if (prev == null && id != null && id > 0 && session.value?.threadId) {
|
||||
void rebindSessionToDraft(id);
|
||||
return;
|
||||
}
|
||||
abort?.abort();
|
||||
@@ -308,6 +374,10 @@ watch(
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
defineExpose({
|
||||
rebindSessionToDraft,
|
||||
});
|
||||
|
||||
async function onModelChange(key: string) {
|
||||
if (!key || !key.includes('::')) return;
|
||||
selectedModelKey.value = key;
|
||||
|
||||
@@ -37,6 +37,9 @@ const presetPddShopId = computed(() => {
|
||||
});
|
||||
|
||||
const editorRef = ref<PublishEditorAgentApi | null>(null);
|
||||
const agentPanelRef = ref<{
|
||||
rebindSessionToDraft: (draftId: number) => Promise<boolean>;
|
||||
} | null>(null);
|
||||
/** 显式取 .value,避免把组件 ref 本体传给子组件导致偶发取不到命令面 */
|
||||
const editorApi = computed(() => editorRef.value);
|
||||
const { width: agentWidthPct, collapsed: agentCollapsed } =
|
||||
@@ -59,10 +62,15 @@ function goBack() {
|
||||
void router.push('/pdd/goods/publish');
|
||||
}
|
||||
|
||||
/** 首次落库后把 URL 换成真实草稿 ID,便于刷新续编与 Agent 绑定 */
|
||||
function onDraftSaved(id: number) {
|
||||
/** 首次落库:先把发布助手 local 会话改绑到草稿 ID,再改 URL(避免路由重挂丢掉临时会话) */
|
||||
async function onDraftSaved(id: number) {
|
||||
if (!id || draftId.value === id) return;
|
||||
void router.replace({
|
||||
try {
|
||||
await agentPanelRef.value?.rebindSessionToDraft(id);
|
||||
} catch {
|
||||
/* 改绑失败仍替换 URL,bootstrap 会再试 pending */
|
||||
}
|
||||
await router.replace({
|
||||
path: `/pdd/goods/publish/editor/${id}`,
|
||||
query: {},
|
||||
});
|
||||
@@ -103,6 +111,7 @@ function onDraftSaved(id: number) {
|
||||
:style="agentPaneStyle"
|
||||
>
|
||||
<PddPublishAgentPanel
|
||||
ref="agentPanelRef"
|
||||
class="h-full min-h-0"
|
||||
:draft-id="draftId"
|
||||
:collapsed="agentCollapsed"
|
||||
|
||||
Reference in New Issue
Block a user