( returnedData: ExecuteCode2Item[] | null, conversationId: number, nLoops?: number, )
| 366 | } |
| 367 | |
| 368 | export function checkCodeExecutionOutput( |
| 369 | returnedData: ExecuteCode2Item[] | null, |
| 370 | conversationId: number, |
| 371 | nLoops?: number, |
| 372 | ): { isValid: boolean; retry: boolean } { |
| 373 | // If data field is null |
| 374 | if (returnedData === null || returnedData.length === 0) { |
| 375 | console.error( |
| 376 | `ERROR: Failed to write valid code for conversation ${conversationId}${ |
| 377 | nLoops ? `, attempt ${nLoops}/2` : "" |
| 378 | }`, |
| 379 | ); |
| 380 | return { isValid: false, retry: true }; |
| 381 | } |
| 382 | // If error messages in the data output |
| 383 | const errorMessages = returnedData.filter((m) => m.type === "error"); |
| 384 | if (errorMessages.length > 0) { |
| 385 | console.error( |
| 386 | `ERROR executing code for conversation ${conversationId}${ |
| 387 | nLoops ? `, attempt ${nLoops}/2` : "" |
| 388 | }:\n${errorMessages |
| 389 | // @ts-ignore |
| 390 | .map((m) => m.args.message) |
| 391 | .join("\n")}`, |
| 392 | ); |
| 393 | // Any 4XX status code is a permanent error |
| 394 | // @ts-ignore |
| 395 | if (errorMessages.some((m) => m.args.message.includes('"status": 4'))) { |
| 396 | return { isValid: false, retry: false }; |
| 397 | } |
| 398 | return { isValid: false, retry: true }; |
| 399 | } |
| 400 | // If there are log messages starting with the word Error or ERROR |
| 401 | const logMessages = returnedData.filter((m) => m.type === "log"); |
| 402 | if ( |
| 403 | logMessages.some( |
| 404 | (m) => "message" in m.args && m.args.message.match(/^E(rror|RROR)/), |
| 405 | ) |
| 406 | ) { |
| 407 | console.error( |
| 408 | `ERROR executing code for conversation ${conversationId}${ |
| 409 | nLoops ? `, attempt ${nLoops}/2` : "" |
| 410 | }:\n${logMessages |
| 411 | // @ts-ignore |
| 412 | .map((m) => m.args.message) |
| 413 | .join("\n")}`, |
| 414 | ); |
| 415 | return { isValid: false, retry: true }; |
| 416 | } |
| 417 | |
| 418 | // If 1 plot with missing data |
| 419 | const plotMessages = returnedData.filter((m) => m.type === "plot"); |
| 420 | if (plotMessages.length === 1) { |
| 421 | const plotArgs = plotMessages[0].args as BertieGraphData; |
| 422 | const isValue = plotArgs.data.length === 1; |
| 423 | const isTable = plotArgs.type === "table"; |
| 424 | if ( |
| 425 | // No data (exception is if it's a table or value) |
no test coverage detected