Skip to content

Commit 44eca10

Browse files
authored
🤖 chore: Improve Assistants Run Logging (danny-avila#1801)
1 parent eb5ebd6 commit 44eca10

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

api/server/services/AssistantService.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,13 +470,14 @@ async function processMessages(openai, messages = []) {
470470
}
471471

472472
text += (content.text?.value ?? '') + ' ';
473+
logger.debug('[processMessages] Processing message:', { value: text });
473474

474475
// Process annotations if they exist
475-
if (!content.text?.annotations) {
476+
if (!content.text?.annotations?.length) {
476477
continue;
477478
}
478479

479-
logger.debug('Processing annotations:', content.text.annotations);
480+
logger.debug('[processMessages] Processing annotations:', content.text.annotations);
480481
for (const annotation of content.text.annotations) {
481482
logger.debug('Current annotation:', annotation);
482483
let file;

api/server/services/Runs/handle.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,35 +88,38 @@ async function waitForRun({
8888
const runInfo = `user: ${openai.req.user.id} | thread_id: ${thread_id} | ${runIdLog}`;
8989
const raceTimeoutMs = 3000;
9090
let maxRetries = 5;
91-
let attempt = 0;
9291
while (timeElapsed < timeout) {
9392
i++;
9493
logger.debug(`[heartbeat ${i}] ${runIdLog} | Retrieving run status...`);
9594
let updatedRun;
9695

97-
const startTime = Date.now();
96+
let attempt = 0;
97+
let startTime = Date.now();
9898
while (!updatedRun && attempt < maxRetries) {
9999
try {
100100
updatedRun = await withTimeout(
101101
retrieveRun({ thread_id, run_id, timeout: raceTimeoutMs, openai }),
102102
raceTimeoutMs,
103-
`[heartbeat ${i}] ${runIdLog} | Run retrieval timed out at ${timeElapsed} ms. Trying again (attempt ${
103+
`[heartbeat ${i}] ${runIdLog} | Run retrieval timed out after ${raceTimeoutMs} ms. Trying again (attempt ${
104104
attempt + 1
105105
} of ${maxRetries})...`,
106106
);
107-
attempt++;
107+
const endTime = Date.now();
108+
logger.debug(
109+
`[heartbeat ${i}] ${runIdLog} | Elapsed run retrieval time: ${endTime - startTime}`,
110+
);
108111
} catch (error) {
109-
logger.warn(`${runIdLog} | Error retrieving run status: ${error}`);
112+
attempt++;
113+
startTime = Date.now();
114+
logger.warn(`${runIdLog} | Error retrieving run status`, error);
110115
}
111116
}
112-
const endTime = Date.now();
113-
logger.debug(
114-
`[heartbeat ${i}] ${runIdLog} | Elapsed run retrieval time: ${endTime - startTime}`,
115-
);
117+
116118
if (!updatedRun) {
117119
const errorMessage = `[waitForRun] ${runIdLog} | Run retrieval failed after ${maxRetries} attempts`;
118120
throw new Error(errorMessage);
119121
}
122+
120123
run = updatedRun;
121124
attempt = 0;
122125
const runStatus = `${runInfo} | status: ${run.status}`;

api/server/services/Runs/methods.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ async function retrieveRun({ thread_id, run_id, timeout, openai }) {
4444
return response.data;
4545
} catch (error) {
4646
const logMessage = '[retrieveRun] Failed to retrieve run data:';
47-
if (error.response) {
47+
const timedOutMessage = 'Cannot read properties of undefined (reading \'status\')';
48+
if (error?.response && error?.response?.status) {
4849
logger.error(
4950
`${logMessage} The request was made and the server responded with a status code that falls out of the range of 2xx: ${
5051
error.message ? error.message : ''
@@ -64,9 +65,9 @@ async function retrieveRun({ thread_id, run_id, timeout, openai }) {
6465
request: error.request,
6566
},
6667
);
67-
} else {
68+
} else if (error?.message && !error?.message?.includes(timedOutMessage)) {
6869
logger.error(`${logMessage} Something happened in setting up the request`, {
69-
message: error.message ? error.message : '',
70+
message: error.message,
7071
});
7172
}
7273
throw error;

0 commit comments

Comments
 (0)