Skip to content

Commit 4b2a6ea

Browse files
committed
fix: add duration tracking to tool output in prepareApiBasedTools function
1 parent 449bf15 commit 4b2a6ea

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

apiBasedTools.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,14 @@ function stringifyToolOutput(output: unknown): string {
635635
return YAML.stringify(output === undefined ? EMPTY_HANDLER_RESPONSE_ERROR : output);
636636
}
637637

638+
function attachDurationMs(output: unknown, durationMs: number): unknown {
639+
if (output !== null && typeof output === 'object' && !Array.isArray(output)) {
640+
return { ...output, durationMs };
641+
}
642+
643+
return { result: output, durationMs };
644+
}
645+
638646
export function prepareApiBasedTools(
639647
adminforth: IAdminForth,
640648
hiddenResourceIds: Iterable<string> = [],
@@ -666,11 +674,13 @@ export function prepareApiBasedTools(
666674
input_schema: schema.request_schema,
667675
agent: schema.agent,
668676
call: async ({ adminUser, adminuser, abortSignal, inputs, userTimeZone, acceptLanguage } = {}) => {
677+
const startedAt = Date.now();
678+
669679
if (isHiddenResourceCall(hiddenResourceIdSet, inputs)) {
670-
return stringifyToolOutput({
680+
return stringifyToolOutput(attachDurationMs({
671681
error: 'RESOURCE_NOT_AVAILABLE',
672682
message: 'This resource is not available to the agent.',
673-
});
683+
}, Date.now() - startedAt));
674684
}
675685

676686
const output = await callOpenApiSchema({
@@ -693,7 +703,7 @@ export function prepareApiBasedTools(
693703
userTimeZone,
694704
});
695705

696-
return stringifyToolOutput(processedOutput);
706+
return stringifyToolOutput(attachDurationMs(processedOutput, Date.now() - startedAt));
697707
},
698708
};
699709
}

0 commit comments

Comments
 (0)