refactor(agents): move calibration ratio capture to finally block

Reorganized the logic for capturing the calibration ratio in the AgentClient class to ensure it is executed in the finally block. This change guarantees that the ratio is captured even if the run is aborted, enhancing the reliability of the response message persistence. Removed redundant code and improved clarity in the handling of context metadata.
This commit is contained in:
Danny Avila 2026-03-11 22:21:08 -04:00
parent 688296e13b
commit 4ca86fae3d
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956

View file

@ -873,20 +873,6 @@ class AgentClient extends BaseClient {
const hideSequentialOutputs = config.configurable.hide_sequential_outputs;
await runAgents(initialMessages);
/** Capture calibration ratio from the completed run for persistence on the response message */
if (this.run) {
const ratio = this.run.getCalibrationRatio();
if (ratio > 0 && ratio !== 1) {
this.contextMeta = {
calibrationRatio: Math.round(ratio * 1000) / 1000,
encoding: this.getEncoding(),
};
logger.debug(
`[AgentClient] contextMeta to persist: ratio=${this.contextMeta.calibrationRatio}, encoding=${this.contextMeta.encoding}`,
);
}
}
/** @deprecated Agent Chain */
if (hideSequentialOutputs) {
this.contentParts = this.contentParts.filter((part, index) => {
@ -917,6 +903,18 @@ class AgentClient extends BaseClient {
});
}
} finally {
/** Capture calibration ratio from the run for persistence on the response message.
* Runs in finally so the ratio is captured even on abort. */
if (this.run) {
const ratio = this.run.getCalibrationRatio();
if (ratio > 0 && ratio !== 1) {
this.contextMeta = {
calibrationRatio: Math.round(ratio * 1000) / 1000,
encoding: this.getEncoding(),
};
}
}
try {
const attachments = await this.awaitMemoryWithTimeout(memoryPromise);
if (attachments && attachments.length > 0) {