Skip to content

Commit b4d315b

Browse files
authored
feat: Fix #412 add optional details data to function tool execution (#413)
1 parent a1c43dd commit b4d315b

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

.changeset/easy-rivers-build.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@openai/agents-realtime': patch
3+
'@openai/agents-core': patch
4+
---
5+
6+
feat: Fix #412 add optional details data to function tool execution

packages/agents-core/src/runImplementation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,7 @@ export async function executeFunctionToolCalls<TContext = UnknownContext>(
741741
const result = await toolRun.tool.invoke(
742742
state._context,
743743
toolRun.toolCall.arguments,
744+
{ toolCall: toolRun.toolCall },
744745
);
745746
// Use string data for tracing and event emitter
746747
const stringResult = toSmartString(result);

packages/agents-core/src/tool.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { getCurrentSpan } from './tracing';
1717
import { RunToolApprovalItem, RunToolCallOutputItem } from './items';
1818
import { toSmartString } from './utils/smartString';
1919
import * as ProviderData from './types/providerData';
20+
import * as protocol from './types/protocol';
2021

2122
/**
2223
* A function that determines if a tool call should be approved.
@@ -67,6 +68,7 @@ export type FunctionTool<
6768
invoke: (
6869
runContext: RunContext<Context>,
6970
input: string,
71+
details?: { toolCall: protocol.FunctionCallItem },
7072
) => Promise<string | Result>;
7173

7274
/**
@@ -411,6 +413,7 @@ type ToolExecuteFunction<
411413
> = (
412414
input: ToolExecuteArgument<TParameters>,
413415
context?: RunContext<Context>,
416+
details?: { toolCall: protocol.FunctionCallItem },
414417
) => Promise<unknown> | unknown;
415418

416419
/**
@@ -591,6 +594,7 @@ export function tool<
591594
async function _invoke(
592595
runContext: RunContext<Context>,
593596
input: string,
597+
details?: { toolCall: protocol.FunctionCallItem },
594598
): Promise<Result> {
595599
const [error, parsed] = await safeExecute(() => parser(input));
596600
if (error !== null) {
@@ -608,7 +612,7 @@ export function tool<
608612
logger.debug(`Invoking tool ${name} with input ${input}`);
609613
}
610614

611-
const result = await options.execute(parsed, runContext);
615+
const result = await options.execute(parsed, runContext, details);
612616
const stringResult = toSmartString(result);
613617

614618
if (logger.dontLogToolData) {
@@ -623,8 +627,9 @@ export function tool<
623627
async function invoke(
624628
runContext: RunContext<Context>,
625629
input: string,
630+
details?: { toolCall: protocol.FunctionCallItem },
626631
): Promise<string | Result> {
627-
return _invoke(runContext, input).catch<string>((error) => {
632+
return _invoke(runContext, input, details).catch<string>((error) => {
628633
if (toolErrorFunction) {
629634
const currentSpan = getCurrentSpan();
630635
currentSpan?.setError({

packages/agents-realtime/src/realtimeSession.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,9 @@ export class RealtimeSession<
486486
});
487487

488488
this.#context.context.history = JSON.parse(JSON.stringify(this.#history)); // deep copy of the history
489-
const result = await tool.invoke(this.#context, toolCall.arguments);
489+
const result = await tool.invoke(this.#context, toolCall.arguments, {
490+
toolCall,
491+
});
490492
let stringResult: string;
491493
if (isBackgroundResult(result)) {
492494
// Don't generate a new response, just send the result

0 commit comments

Comments
 (0)