Skip to content

Commit 8bba4b4

Browse files
committed
feat: Fix #412 add optional details data to function tool execution
1 parent dba4bb6 commit 8bba4b4

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

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
/**
@@ -337,6 +339,7 @@ type ToolExecuteFunction<
337339
> = (
338340
input: ToolExecuteArgument<TParameters>,
339341
context?: RunContext<Context>,
342+
details?: { toolCall: protocol.FunctionCallItem },
340343
) => Promise<unknown> | unknown;
341344

342345
/**
@@ -517,6 +520,7 @@ export function tool<
517520
async function _invoke(
518521
runContext: RunContext<Context>,
519522
input: string,
523+
details: { toolCall: protocol.FunctionCallItem },
520524
): Promise<Result> {
521525
const [error, parsed] = await safeExecute(() => parser(input));
522526
if (error !== null) {
@@ -534,7 +538,7 @@ export function tool<
534538
logger.debug(`Invoking tool ${name} with input ${input}`);
535539
}
536540

537-
const result = await options.execute(parsed, runContext);
541+
const result = await options.execute(parsed, runContext, details);
538542
const stringResult = toSmartString(result);
539543

540544
if (logger.dontLogToolData) {
@@ -549,8 +553,9 @@ export function tool<
549553
async function invoke(
550554
runContext: RunContext<Context>,
551555
input: string,
556+
details: { toolCall: protocol.FunctionCallItem },
552557
): Promise<string | Result> {
553-
return _invoke(runContext, input).catch<string>((error) => {
558+
return _invoke(runContext, input, details).catch<string>((error) => {
554559
if (toolErrorFunction) {
555560
const currentSpan = getCurrentSpan();
556561
currentSpan?.setError({

packages/agents-realtime/src/realtimeSession.ts

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

448448
this.#context.context.history = JSON.parse(JSON.stringify(this.#history)); // deep copy of the history
449-
const result = await tool.invoke(this.#context, toolCall.arguments);
449+
const result = await tool.invoke(this.#context, toolCall.arguments, {
450+
toolCall,
451+
});
450452
const stringResult = toSmartString(result);
451453
this.#transport.sendFunctionCallOutput(toolCall, stringResult, true);
452454
this.emit(

0 commit comments

Comments
 (0)