Prevent stale session overwrite on new tasks
This commit is contained in:
@@ -102,7 +102,7 @@ export async function POST(req: Request) {
|
||||
if (!account) return new Response("请先登录账号", { status: 401 });
|
||||
|
||||
const { id, messages, resumeSessionId }: ChatRequestBody = await req.json();
|
||||
const resumeId = resumeSessionId ?? getLastSessionId(messages);
|
||||
const resumeId = resumeSessionId;
|
||||
const sessionId = safeSessionId(resumeId ?? id ?? crypto.randomUUID());
|
||||
await ensureSessionDirectories(account.id, sessionId);
|
||||
const userPrompt = await getLastUserText(messages, account.id, sessionId);
|
||||
@@ -362,18 +362,6 @@ function renderSessionRuntimeContext(accountId: string, sessionId: string) {
|
||||
].join("\n\n");
|
||||
}
|
||||
|
||||
function getLastSessionId(messages: UIMessage[]) {
|
||||
for (const message of [...messages].reverse()) {
|
||||
if (message.role !== "assistant") continue;
|
||||
const metadata = message.metadata;
|
||||
if (metadata && typeof metadata === "object" && "sessionId" in metadata) {
|
||||
const sessionId = (metadata as { sessionId?: unknown }).sessionId;
|
||||
if (typeof sessionId === "string" && sessionId) return sessionId;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
async function saveFilePart(
|
||||
part: Extract<UIMessage["parts"][number], { type: "file" }>,
|
||||
accountId: string,
|
||||
|
||||
@@ -30,6 +30,7 @@ import {
|
||||
import {
|
||||
ACTIVE_SESSION_CHANGED_EVENT,
|
||||
consumeFreshLocalId,
|
||||
createLocalSessionId,
|
||||
readActiveSessionId,
|
||||
readPendingWorkspaceSessionId,
|
||||
writeActiveSessionId,
|
||||
@@ -60,11 +61,7 @@ export const Assistant = ({ initialSessionId }: AssistantProps = {}) => {
|
||||
const body = options.body as Record<string, unknown>;
|
||||
const messages = options.messages as UIMessage[];
|
||||
const selectedSessionId =
|
||||
readSessionIdFromUrl() ??
|
||||
readActiveSessionId() ??
|
||||
initialSessionId?.trim() ??
|
||||
null;
|
||||
const lastSessionId = getLastSessionId(messages);
|
||||
readSessionIdFromUrl() ?? readActiveSessionId() ?? null;
|
||||
const pendingWorkspaceSessionId =
|
||||
!selectedSessionId && messages.length <= 1
|
||||
? readPendingWorkspaceSessionId()
|
||||
@@ -73,8 +70,7 @@ export const Assistant = ({ initialSessionId }: AssistantProps = {}) => {
|
||||
const outgoingSessionId =
|
||||
selectedResumeSessionId ??
|
||||
pendingWorkspaceSessionId ??
|
||||
lastSessionId ??
|
||||
options.id;
|
||||
createLocalSessionId();
|
||||
const lastUserMessage = [...messages]
|
||||
.reverse()
|
||||
.find((message) => message.role === "user");
|
||||
@@ -86,13 +82,12 @@ export const Assistant = ({ initialSessionId }: AssistantProps = {}) => {
|
||||
...body,
|
||||
id: outgoingSessionId,
|
||||
messages: lastUserMessage ? [lastUserMessage] : [],
|
||||
resumeSessionId:
|
||||
selectedResumeSessionId ?? lastSessionId ?? undefined,
|
||||
resumeSessionId: selectedResumeSessionId ?? undefined,
|
||||
},
|
||||
};
|
||||
},
|
||||
}),
|
||||
[initialSessionId],
|
||||
[],
|
||||
);
|
||||
const runtime = useChatRuntime({
|
||||
transport,
|
||||
@@ -410,15 +405,3 @@ function ActivityDrawerTrigger() {
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
|
||||
function getLastSessionId(messages: UIMessage[]) {
|
||||
for (const message of [...messages].reverse()) {
|
||||
if (message.role !== "assistant") continue;
|
||||
const metadata = message.metadata;
|
||||
if (metadata && typeof metadata === "object" && "sessionId" in metadata) {
|
||||
const sessionId = (metadata as { sessionId?: unknown }).sessionId;
|
||||
if (typeof sessionId === "string" && sessionId) return sessionId;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@@ -50,6 +50,13 @@ export function clearActiveSessionId() {
|
||||
);
|
||||
}
|
||||
|
||||
export function createLocalSessionId() {
|
||||
if (typeof crypto !== "undefined" && "randomUUID" in crypto) {
|
||||
return `__LOCALID_${crypto.randomUUID().replace(/-/g, "").slice(0, 8)}`;
|
||||
}
|
||||
return `__LOCALID_${Math.random().toString(36).slice(2, 10)}`;
|
||||
}
|
||||
|
||||
export function writePendingWorkspaceSessionId(
|
||||
sessionId: string | null | undefined,
|
||||
) {
|
||||
|
||||
Reference in New Issue
Block a user