fix: forked session query condition (#7568)
This commit is contained in:
@@ -194,6 +194,12 @@ export class ChatSessionService {
|
|||||||
|
|
||||||
// find existing session if session is chat session
|
// find existing session if session is chat session
|
||||||
if (!state.prompt.action) {
|
if (!state.prompt.action) {
|
||||||
|
const extraCondition: Record<string, any> = {};
|
||||||
|
if (state.parentSessionId) {
|
||||||
|
// also check session id if provided session is forked session
|
||||||
|
extraCondition.id = state.sessionId;
|
||||||
|
extraCondition.parentSessionId = state.parentSessionId;
|
||||||
|
}
|
||||||
const { id, deletedAt } =
|
const { id, deletedAt } =
|
||||||
(await tx.aiSession.findFirst({
|
(await tx.aiSession.findFirst({
|
||||||
where: {
|
where: {
|
||||||
@@ -201,7 +207,8 @@ export class ChatSessionService {
|
|||||||
workspaceId: state.workspaceId,
|
workspaceId: state.workspaceId,
|
||||||
docId: state.docId,
|
docId: state.docId,
|
||||||
prompt: { action: { equals: null } },
|
prompt: { action: { equals: null } },
|
||||||
parentSessionId: state.parentSessionId,
|
parentSessionId: null,
|
||||||
|
...extraCondition,
|
||||||
},
|
},
|
||||||
select: { id: true, deletedAt: true },
|
select: { id: true, deletedAt: true },
|
||||||
})) || {};
|
})) || {};
|
||||||
|
|||||||
@@ -290,17 +290,46 @@ test('should be able to fork chat session', async t => {
|
|||||||
const s1 = (await session.get(sessionId))!;
|
const s1 = (await session.get(sessionId))!;
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
const latestMessageId = s1.finish({}).find(m => m.role === 'assistant')!.id;
|
const latestMessageId = s1.finish({}).find(m => m.role === 'assistant')!.id;
|
||||||
const forkedSessionId = await session.fork({
|
const forkedSessionId1 = await session.fork({
|
||||||
userId,
|
userId,
|
||||||
sessionId,
|
sessionId,
|
||||||
latestMessageId,
|
latestMessageId,
|
||||||
...commonParams,
|
...commonParams,
|
||||||
});
|
});
|
||||||
t.not(sessionId, forkedSessionId, 'should fork a new session');
|
t.not(sessionId, forkedSessionId1, 'should fork a new session');
|
||||||
|
const forkedSessionId2 = await session.fork({
|
||||||
|
userId,
|
||||||
|
sessionId,
|
||||||
|
latestMessageId,
|
||||||
|
...commonParams,
|
||||||
|
});
|
||||||
|
t.not(
|
||||||
|
forkedSessionId1,
|
||||||
|
forkedSessionId2,
|
||||||
|
'should fork new session with same params'
|
||||||
|
);
|
||||||
|
|
||||||
// check forked session messages
|
// check forked session messages
|
||||||
{
|
{
|
||||||
const s2 = (await session.get(forkedSessionId))!;
|
const s2 = (await session.get(forkedSessionId1))!;
|
||||||
|
|
||||||
|
const finalMessages = s2
|
||||||
|
.finish(params) // @ts-expect-error
|
||||||
|
.map(({ id: _, createdAt: __, ...m }) => m);
|
||||||
|
t.deepEqual(
|
||||||
|
finalMessages,
|
||||||
|
[
|
||||||
|
{ role: 'system', content: 'hello world', params },
|
||||||
|
{ role: 'user', content: 'hello' },
|
||||||
|
{ role: 'assistant', content: 'world' },
|
||||||
|
],
|
||||||
|
'should generate the final message'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// check second times forked session messages
|
||||||
|
{
|
||||||
|
const s2 = (await session.get(forkedSessionId2))!;
|
||||||
|
|
||||||
const finalMessages = s2
|
const finalMessages = s2
|
||||||
.finish(params) // @ts-expect-error
|
.finish(params) // @ts-expect-error
|
||||||
|
|||||||
Reference in New Issue
Block a user