Skip to content

Commit 90c8947

Browse files
committed
address comments
1 parent 977061c commit 90c8947

6 files changed

Lines changed: 36 additions & 10 deletions

File tree

apps/sim/app/api/copilot/chat/stop/route.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,14 @@ export async function POST(req: NextRequest) {
9393
const canAppendAssistant =
9494
userIdx >= 0 && userIdx === messages.length - 1 && !alreadyHasResponse
9595

96+
const updateWhere = and(
97+
eq(copilotChats.id, chatId),
98+
eq(copilotChats.userId, session.user.id),
99+
eq(copilotChats.conversationId, streamId)
100+
)
101+
96102
const setClause: Record<string, unknown> = {
97-
conversationId: sql`CASE WHEN ${copilotChats.conversationId} = ${streamId} THEN NULL ELSE ${copilotChats.conversationId} END`,
103+
conversationId: null,
98104
updatedAt: new Date(),
99105
}
100106

@@ -116,7 +122,7 @@ export async function POST(req: NextRequest) {
116122
const [updated] = await db
117123
.update(copilotChats)
118124
.set(setClause)
119-
.where(and(eq(copilotChats.id, chatId), eq(copilotChats.userId, session.user.id)))
125+
.where(updateWhere)
120126
.returning({ workspaceId: copilotChats.workspaceId })
121127

122128
if (updated?.workspaceId) {

apps/sim/lib/copilot/chat/terminal-state.test.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* @vitest-environment node
33
*/
44

5+
import { copilotChats } from '@sim/db/schema'
6+
import { and, eq } from 'drizzle-orm'
57
import { beforeEach, describe, expect, it, vi } from 'vitest'
68

79
const { selectLimit, selectWhere, selectFrom, select, updateWhere, updateSet, update } = vi.hoisted(
@@ -63,10 +65,13 @@ describe('finalizeAssistantTurn', () => {
6365
expect(updateSet).toHaveBeenCalledWith(
6466
expect.objectContaining({
6567
updatedAt: expect.any(Date),
66-
conversationId: expect.anything(),
68+
conversationId: null,
6769
messages: expect.anything(),
6870
})
6971
)
72+
expect(updateWhere).toHaveBeenCalledWith(
73+
and(eq(copilotChats.id, 'chat-1'), eq(copilotChats.conversationId, 'user-1'))
74+
)
7075
})
7176

7277
it('only clears the active stream marker when a response is already persisted', async () => {
@@ -90,13 +95,21 @@ describe('finalizeAssistantTurn', () => {
9095
},
9196
})
9297

93-
const updateArg = updateSet.mock.calls[0]?.[0] as Record<string, unknown>
98+
const updateCalls = updateSet.mock.calls as unknown as Array<[Record<string, unknown>]>
99+
const updateArg = updateCalls[0]?.[0]
100+
expect(updateArg).toBeDefined()
101+
if (!updateArg) {
102+
throw new Error('Expected updateSet to be called')
103+
}
94104
expect(updateArg).toEqual(
95105
expect.objectContaining({
96106
updatedAt: expect.any(Date),
97-
conversationId: expect.anything(),
107+
conversationId: null,
98108
})
99109
)
100110
expect(Object.hasOwn(updateArg, 'messages')).toBe(false)
111+
expect(updateWhere).toHaveBeenCalledWith(
112+
and(eq(copilotChats.id, 'chat-1'), eq(copilotChats.conversationId, 'user-1'))
113+
)
101114
})
102115
})

apps/sim/lib/copilot/chat/terminal-state.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { db } from '@sim/db'
22
import { copilotChats } from '@sim/db/schema'
3-
import { eq, sql } from 'drizzle-orm'
3+
import { and, eq, sql } from 'drizzle-orm'
44
import type { PersistedMessage } from '@/lib/copilot/chat/persisted-message'
55

66
interface FinalizeAssistantTurnParams {
@@ -32,9 +32,13 @@ export async function finalizeAssistantTurn({
3232
userIdx + 1 < messages.length &&
3333
(messages[userIdx + 1] as Record<string, unknown>)?.role === 'assistant'
3434
const canAppendAssistant = userIdx >= 0 && userIdx === messages.length - 1 && !alreadyHasResponse
35+
const updateWhere = and(
36+
eq(copilotChats.id, chatId),
37+
eq(copilotChats.conversationId, userMessageId)
38+
)
3539

3640
const baseUpdate = {
37-
conversationId: sql`CASE WHEN ${copilotChats.conversationId} = ${userMessageId} THEN NULL ELSE ${copilotChats.conversationId} END`,
41+
conversationId: null,
3842
updatedAt: new Date(),
3943
}
4044

@@ -45,9 +49,9 @@ export async function finalizeAssistantTurn({
4549
...baseUpdate,
4650
messages: sql`${copilotChats.messages} || ${JSON.stringify([assistantMessage])}::jsonb`,
4751
})
48-
.where(eq(copilotChats.id, chatId))
52+
.where(updateWhere)
4953
return
5054
}
5155

52-
await db.update(copilotChats).set(baseUpdate).where(eq(copilotChats.id, chatId))
56+
await db.update(copilotChats).set(baseUpdate).where(updateWhere)
5357
}

apps/sim/lib/copilot/request/session/contract.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import Ajv2020, { type ErrorObject, type ValidateFunction } from 'ajv/dist/2020'
1+
import type { ErrorObject, ValidateFunction } from 'ajv'
2+
import Ajv2020 from 'ajv/dist/2020.js'
23
import type {
34
MothershipStreamV1EventEnvelope,
45
MothershipStreamV1StreamRef,

apps/sim/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
"@trigger.dev/sdk": "4.4.3",
9898
"@types/react-window": "2.0.0",
9999
"@types/three": "0.177.0",
100+
"ajv": "8.18.0",
100101
"better-auth": "1.3.12",
101102
"better-auth-harmony": "1.3.1",
102103
"binary-extensions": "^2.0.0",

bun.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)