Skip to content

Commit 863401b

Browse files
authored
🔧 fix: Assistants API SDK calls to match Updated Arguments (#8818)
* chore: remove comments in agents endpoint error handler * chore: improve openai sdk typing * chore: improve typing for azure asst init * 🔧 fix: Assistants API SDK calls to match Updated Arguments
1 parent 33c8b87 commit 863401b

File tree

15 files changed

+41
-40
lines changed

15 files changed

+41
-40
lines changed

api/server/controllers/agents/errors.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ const createErrorHandler = ({ req, res, getContext, originPath = '/assistants/ch
105105
return res.end();
106106
}
107107
await cache.delete(cacheKey);
108-
// const cancelledRun = await openai.beta.threads.runs.cancel(thread_id, run_id);
109-
// logger.debug(`[${originPath}] Cancelled run:`, cancelledRun);
110108
} catch (error) {
111109
logger.error(`[${originPath}] Error cancelling run`, error);
112110
}
@@ -115,7 +113,6 @@ const createErrorHandler = ({ req, res, getContext, originPath = '/assistants/ch
115113

116114
let run;
117115
try {
118-
// run = await openai.beta.threads.runs.retrieve(thread_id, run_id);
119116
await recordUsage({
120117
...run.usage,
121118
model: run.model,
@@ -128,18 +125,9 @@ const createErrorHandler = ({ req, res, getContext, originPath = '/assistants/ch
128125

129126
let finalEvent;
130127
try {
131-
// const errorContentPart = {
132-
// text: {
133-
// value:
134-
// error?.message ?? 'There was an error processing your request. Please try again later.',
135-
// },
136-
// type: ContentTypes.ERROR,
137-
// };
138-
139128
finalEvent = {
140129
final: true,
141130
conversation: await getConvo(req.user.id, conversationId),
142-
// runMessages,
143131
};
144132
} catch (error) {
145133
logger.error(`[${originPath}] Error finalizing error process`, error);

api/server/controllers/assistants/chatV1.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ const chatV1 = async (req, res) => {
152152
return res.end();
153153
}
154154
await cache.delete(cacheKey);
155-
const cancelledRun = await openai.beta.threads.runs.cancel(thread_id, run_id);
155+
const cancelledRun = await openai.beta.threads.runs.cancel(run_id, { thread_id });
156156
logger.debug('[/assistants/chat/] Cancelled run:', cancelledRun);
157157
} catch (error) {
158158
logger.error('[/assistants/chat/] Error cancelling run', error);
@@ -162,7 +162,7 @@ const chatV1 = async (req, res) => {
162162

163163
let run;
164164
try {
165-
run = await openai.beta.threads.runs.retrieve(thread_id, run_id);
165+
run = await openai.beta.threads.runs.retrieve(run_id, { thread_id });
166166
await recordUsage({
167167
...run.usage,
168168
model: run.model,
@@ -623,7 +623,7 @@ const chatV1 = async (req, res) => {
623623

624624
if (!response.run.usage) {
625625
await sleep(3000);
626-
completedRun = await openai.beta.threads.runs.retrieve(thread_id, response.run.id);
626+
completedRun = await openai.beta.threads.runs.retrieve(response.run.id, { thread_id });
627627
if (completedRun.usage) {
628628
await recordUsage({
629629
...completedRun.usage,

api/server/controllers/assistants/chatV2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ const chatV2 = async (req, res) => {
467467

468468
if (!response.run.usage) {
469469
await sleep(3000);
470-
completedRun = await openai.beta.threads.runs.retrieve(thread_id, response.run.id);
470+
completedRun = await openai.beta.threads.runs.retrieve(response.run.id, { thread_id });
471471
if (completedRun.usage) {
472472
await recordUsage({
473473
...completedRun.usage,

api/server/controllers/assistants/errors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const createErrorHandler = ({ req, res, getContext, originPath = '/assistants/ch
108108
return res.end();
109109
}
110110
await cache.delete(cacheKey);
111-
const cancelledRun = await openai.beta.threads.runs.cancel(thread_id, run_id);
111+
const cancelledRun = await openai.beta.threads.runs.cancel(run_id, { thread_id });
112112
logger.debug(`[${originPath}] Cancelled run:`, cancelledRun);
113113
} catch (error) {
114114
logger.error(`[${originPath}] Error cancelling run`, error);
@@ -118,7 +118,7 @@ const createErrorHandler = ({ req, res, getContext, originPath = '/assistants/ch
118118

119119
let run;
120120
try {
121-
run = await openai.beta.threads.runs.retrieve(thread_id, run_id);
121+
run = await openai.beta.threads.runs.retrieve(run_id, { thread_id });
122122
await recordUsage({
123123
...run.usage,
124124
model: run.model,

api/server/controllers/assistants/helpers.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@ const listAssistantsForAzure = async ({ req, res, version, azureConfig = {}, que
173173
};
174174
};
175175

176+
/**
177+
* Initializes the OpenAI client.
178+
* @param {object} params - The parameters object.
179+
* @param {ServerRequest} params.req - The request object.
180+
* @param {ServerResponse} params.res - The response object.
181+
* @param {TEndpointOption} params.endpointOption - The endpoint options.
182+
* @param {boolean} params.initAppClient - Whether to initialize the app client.
183+
* @param {string} params.overrideEndpoint - The endpoint to override.
184+
* @returns {Promise<{ openai: OpenAIClient, openAIApiKey: string; client: import('~/app/clients/OpenAIClient') }>} - The initialized OpenAI client.
185+
*/
176186
async function getOpenAIClient({ req, res, endpointOption, initAppClient, overrideEndpoint }) {
177187
let endpoint = overrideEndpoint ?? req.body.endpoint ?? req.query.endpoint;
178188
const version = await getCurrentVersion(req, endpoint);

api/server/controllers/assistants/v1.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ const deleteAssistant = async (req, res) => {
197197
await validateAuthor({ req, openai });
198198

199199
const assistant_id = req.params.id;
200-
const deletionStatus = await openai.beta.assistants.del(assistant_id);
200+
const deletionStatus = await openai.beta.assistants.delete(assistant_id);
201201
if (deletionStatus?.deleted) {
202202
await deleteAssistantActions({ req, assistant_id });
203203
}
@@ -365,7 +365,7 @@ const uploadAssistantAvatar = async (req, res) => {
365365
try {
366366
await fs.unlink(req.file.path);
367367
logger.debug('[/:agent_id/avatar] Temp. image upload file deleted');
368-
} catch (error) {
368+
} catch {
369369
logger.debug('[/:agent_id/avatar] Temp. image upload file already deleted');
370370
}
371371
}

api/server/middleware/abortRun.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ async function abortRun(req, res) {
4747

4848
try {
4949
await cache.set(cacheKey, 'cancelled', three_minutes);
50-
const cancelledRun = await openai.beta.threads.runs.cancel(thread_id, run_id);
50+
const cancelledRun = await openai.beta.threads.runs.cancel(run_id, { thread_id });
5151
logger.debug('[abortRun] Cancelled run:', cancelledRun);
5252
} catch (error) {
5353
logger.error('[abortRun] Error cancelling run', error);
@@ -60,7 +60,7 @@ async function abortRun(req, res) {
6060
}
6161

6262
try {
63-
const run = await openai.beta.threads.runs.retrieve(thread_id, run_id);
63+
const run = await openai.beta.threads.runs.retrieve(run_id, { thread_id });
6464
await recordUsage({
6565
...run.usage,
6666
model: run.model,

api/server/routes/convos.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ router.delete('/', async (req, res) => {
111111
/** @type {{ openai: OpenAI }} */
112112
const { openai } = await assistantClients[endpoint].initializeClient({ req, res });
113113
try {
114-
const response = await openai.beta.threads.del(thread_id);
114+
const response = await openai.beta.threads.delete(thread_id);
115115
logger.debug('Deleted OpenAI thread:', response);
116116
} catch (error) {
117117
logger.error('Error deleting OpenAI thread:', error);

api/server/services/AssistantService.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ function createInProgressHandler(openai, thread_id, messages) {
281281

282282
openai.seenCompletedMessages.add(message_id);
283283

284-
const message = await openai.beta.threads.messages.retrieve(thread_id, message_id);
284+
const message = await openai.beta.threads.messages.retrieve(message_id, { thread_id });
285285
if (!message?.content?.length) {
286286
return;
287287
}
@@ -435,9 +435,11 @@ async function runAssistant({
435435
};
436436
});
437437

438-
const outputs = await processRequiredActions(openai, actions);
439-
440-
const toolRun = await openai.beta.threads.runs.submitToolOutputs(run.thread_id, run.id, outputs);
438+
const tool_outputs = await processRequiredActions(openai, actions);
439+
const toolRun = await openai.beta.threads.runs.submitToolOutputs(run.id, {
440+
thread_id: run.thread_id,
441+
tool_outputs,
442+
});
441443

442444
// Recursive call with accumulated steps and messages
443445
return await runAssistant({

api/server/services/Endpoints/assistants/initalize.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const {
66
getUserKeyExpiry,
77
checkUserKeyExpiry,
88
} = require('~/server/services/UserService');
9-
const OpenAIClient = require('~/app/clients/OpenAIClient');
9+
const OAIClient = require('~/app/clients/OpenAIClient');
1010
const { isUserProvided } = require('~/server/utils');
1111

1212
const initializeClient = async ({ req, res, endpointOption, version, initAppClient = false }) => {
@@ -79,7 +79,7 @@ const initializeClient = async ({ req, res, endpointOption, version, initAppClie
7979
openai.res = res;
8080

8181
if (endpointOption && initAppClient) {
82-
const client = new OpenAIClient(apiKey, clientOptions);
82+
const client = new OAIClient(apiKey, clientOptions);
8383
return {
8484
client,
8585
openai,

0 commit comments

Comments
 (0)