fix(chat): avoid implicit stream reuse
This commit is contained in:
+20
-6
@@ -193,6 +193,21 @@ const pipeUiChunk = (
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const resolveRequestedStreamSessionId = (input: {
|
||||||
|
sessionId?: string;
|
||||||
|
promptText: string;
|
||||||
|
createClientSessionId: () => string;
|
||||||
|
}) => {
|
||||||
|
const sessionId = input.sessionId?.trim();
|
||||||
|
if (sessionId) {
|
||||||
|
return sessionId;
|
||||||
|
}
|
||||||
|
if (isPressureTrendDemoPrompt(input.promptText)) {
|
||||||
|
return input.createClientSessionId();
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
export const buildChatRouter = (
|
export const buildChatRouter = (
|
||||||
sessionBridge: ChatSessionBridge,
|
sessionBridge: ChatSessionBridge,
|
||||||
runtime: OpencodeRuntimeAdapter,
|
runtime: OpencodeRuntimeAdapter,
|
||||||
@@ -617,12 +632,11 @@ export const buildChatRouter = (
|
|||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const requestedSessionId =
|
const requestedSessionId = resolveRequestedStreamSessionId({
|
||||||
parsed.data.session_id?.trim() ||
|
sessionId: parsed.data.session_id,
|
||||||
parsed.data.id?.trim() ||
|
promptText,
|
||||||
(isPressureTrendDemoPrompt(promptText)
|
createClientSessionId: () => sessionBridge.createClientSessionId(),
|
||||||
? sessionBridge.createClientSessionId()
|
});
|
||||||
: undefined);
|
|
||||||
const existingSessionRecord = requestedSessionId
|
const existingSessionRecord = requestedSessionId
|
||||||
? await sessionMetadataStore.get(
|
? await sessionMetadataStore.get(
|
||||||
{ actorKey, projectId, projectKey, userId },
|
{ actorKey, projectId, projectKey, userId },
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { describe, expect, it } from "bun:test";
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
buildForkedSessionUiState,
|
buildForkedSessionUiState,
|
||||||
|
resolveRequestedStreamSessionId,
|
||||||
} from "../../src/routes/chat.js";
|
} from "../../src/routes/chat.js";
|
||||||
import {
|
import {
|
||||||
buildPromptWithLearningContext,
|
buildPromptWithLearningContext,
|
||||||
@@ -14,6 +15,38 @@ import { type SessionTurnRecord } from "../../src/sessions/transcriptStore.js";
|
|||||||
import { type MemoryStore } from "../../src/memory/store.js";
|
import { type MemoryStore } from "../../src/memory/store.js";
|
||||||
import { type OpencodeRuntimeAdapter } from "../../src/runtime/opencode.js";
|
import { type OpencodeRuntimeAdapter } from "../../src/runtime/opencode.js";
|
||||||
|
|
||||||
|
describe("resolveRequestedStreamSessionId", () => {
|
||||||
|
it("uses only explicit session_id for normal chat reuse", () => {
|
||||||
|
const createClientSessionId = () => {
|
||||||
|
throw new Error("should not create a demo session id");
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(
|
||||||
|
resolveRequestedStreamSessionId({
|
||||||
|
sessionId: " existing-session ",
|
||||||
|
promptText: "检查压力异常",
|
||||||
|
createClientSessionId,
|
||||||
|
}),
|
||||||
|
).toBe("existing-session");
|
||||||
|
|
||||||
|
expect(
|
||||||
|
resolveRequestedStreamSessionId({
|
||||||
|
promptText: "检查压力异常",
|
||||||
|
createClientSessionId,
|
||||||
|
}),
|
||||||
|
).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps the pressure trend demo on its generated client session id", () => {
|
||||||
|
expect(
|
||||||
|
resolveRequestedStreamSessionId({
|
||||||
|
promptText: "展示最近压力趋势",
|
||||||
|
createClientSessionId: () => "demo-session",
|
||||||
|
}),
|
||||||
|
).toBe("demo-session");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("shouldGenerateSessionTitle", () => {
|
describe("shouldGenerateSessionTitle", () => {
|
||||||
it("allows auto-title generation for the first turn when the title was not edited", () => {
|
it("allows auto-title generation for the first turn when the title was not edited", () => {
|
||||||
expect(
|
expect(
|
||||||
|
|||||||
Reference in New Issue
Block a user