Skip to content

Commit ca12918

Browse files
committed
assign only defined new preferences
1 parent 0548b18 commit ca12918

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

src/client/components/Admin/Testing.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import { Box } from '@mui/material'
22
import { OutlineButtonBlack, OutlineButtonBlue } from '../ChatV2/general/Buttons'
33
import * as Sentry from '@sentry/react'
44
import useCurrentUser from '../../hooks/useCurrentUser'
5+
import apiClient from '../../util/apiClient'
6+
7+
const testResponsesApi = async () => {
8+
await apiClient.post('/test/responses-api')
9+
}
10+
11+
const testCompletionsApi = async () => {
12+
await apiClient.post('/test/completions-api')
13+
}
514

615
export default function Testing() {
716
const { user } = useCurrentUser()
@@ -26,6 +35,19 @@ export default function Testing() {
2635
>
2736
Sentry message testing button. Pressing should cause a sentry message being emitted and a message sent to toska slack.
2837
</OutlineButtonBlue>
38+
<OutlineButtonBlue
39+
onClick={testResponsesApi}
40+
// eslint-disable-next-line i18next/no-literal-string
41+
>
42+
Responses API test, see logs. Do not press for fun.
43+
</OutlineButtonBlue>
44+
45+
<OutlineButtonBlue
46+
onClick={testCompletionsApi}
47+
// eslint-disable-next-line i18next/no-literal-string
48+
>
49+
Completions API test, Do not press for fun.
50+
</OutlineButtonBlue>
2951
</Box>
3052
)
3153
}

src/server/routes/testUtils.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { getTestUserHeaders } from '../../shared/testData'
66
import logger from '../util/logger'
77
import { TEST_COURSES } from '../../shared/testData'
88
import { headersToUser } from '../middleware/user'
9+
import { ResponsesClient } from '../util/azure/ResponsesAPI'
10+
import { RequestWithUser } from '../types'
11+
import getEncoding from '../util/tiktoken'
12+
import { getCompletionEvents } from '../util/azure/client'
913

1014
const router = Router()
1115

@@ -83,4 +87,58 @@ router.post('/reset-test-data', async (req, res) => {
8387
res.status(200).json({ message: 'Test data reset successfully' })
8488
})
8589

90+
router.post('/responses-api', async (req, res) => {
91+
const { user } = req as RequestWithUser
92+
93+
const encoding = getEncoding('gpt-4o-mini')
94+
95+
const responsesClient = new ResponsesClient({
96+
model: 'gpt-4o-mini',
97+
ragIndex: undefined,
98+
instructions: '',
99+
temperature: 0.9,
100+
user,
101+
})
102+
103+
console.log('Starting Responses API stream')
104+
105+
const stream = await responsesClient.createResponse({
106+
input: {
107+
role: 'user',
108+
content: 'Hello!, please explain the concept of artificial intelligence.',
109+
},
110+
})
111+
112+
console.log('Stream Responses API started')
113+
114+
const result = await responsesClient.handleResponse({ stream, encoding, res })
115+
116+
console.log('Stream Responses API ended', result)
117+
})
118+
119+
router.post('/completions-api', async (req, res) => {
120+
console.log('Starting Completions API')
121+
122+
const result = await getCompletionEvents({
123+
messages: [
124+
{
125+
role: 'user',
126+
// @ts-expect-error whatever
127+
content: 'Hello!, please explain the concept of artificial intelligence.',
128+
},
129+
],
130+
model: 'gpt-4o-mini',
131+
options: {
132+
temperature: 0.9,
133+
},
134+
})
135+
136+
// @ts-expect-error whatever
137+
for await (const chunk of result) {
138+
console.log('Completions API chunk:', chunk)
139+
}
140+
141+
console.log('Completions API result:', result)
142+
})
143+
86144
export default router

src/server/routes/user.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,13 @@ userRouter.put('/preferences', async (req, res) => {
126126

127127
const newPreferences = {
128128
...(user.preferences ?? {}),
129-
...preferences,
129+
}
130+
131+
// Assign only defined values
132+
for (const key in preferences) {
133+
if (preferences[key] !== undefined) {
134+
newPreferences[key] = preferences[key]
135+
}
130136
}
131137

132138
await User.update({ preferences: newPreferences }, { where: { id } })

0 commit comments

Comments
 (0)