Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/easy-rivers-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@openai/agents-realtime': patch
'@openai/agents-core': patch
---

feat: Fix #412 add optional details data to function tool execution
1 change: 1 addition & 0 deletions packages/agents-core/src/runImplementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ export async function executeFunctionToolCalls<TContext = UnknownContext>(
const result = await toolRun.tool.invoke(
state._context,
toolRun.toolCall.arguments,
{ toolCall: toolRun.toolCall },
);
// Use string data for tracing and event emitter
const stringResult = toSmartString(result);
Expand Down
9 changes: 7 additions & 2 deletions packages/agents-core/src/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { getCurrentSpan } from './tracing';
import { RunToolApprovalItem, RunToolCallOutputItem } from './items';
import { toSmartString } from './utils/smartString';
import * as ProviderData from './types/providerData';
import * as protocol from './types/protocol';

/**
* A function that determines if a tool call should be approved.
Expand Down Expand Up @@ -67,6 +68,7 @@ export type FunctionTool<
invoke: (
runContext: RunContext<Context>,
input: string,
details?: { toolCall: protocol.FunctionCallItem },
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we don't want to expose all the tool call data, I can filter this to have only callId

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having FunctionCallItem is actually great 👍

) => Promise<string | Result>;

/**
Expand Down Expand Up @@ -411,6 +413,7 @@ type ToolExecuteFunction<
> = (
input: ToolExecuteArgument<TParameters>,
context?: RunContext<Context>,
details?: { toolCall: protocol.FunctionCallItem },
) => Promise<unknown> | unknown;

/**
Expand Down Expand Up @@ -591,6 +594,7 @@ export function tool<
async function _invoke(
runContext: RunContext<Context>,
input: string,
details?: { toolCall: protocol.FunctionCallItem },
): Promise<Result> {
const [error, parsed] = await safeExecute(() => parser(input));
if (error !== null) {
Expand All @@ -608,7 +612,7 @@ export function tool<
logger.debug(`Invoking tool ${name} with input ${input}`);
}

const result = await options.execute(parsed, runContext);
const result = await options.execute(parsed, runContext, details);
const stringResult = toSmartString(result);

if (logger.dontLogToolData) {
Expand All @@ -623,8 +627,9 @@ export function tool<
async function invoke(
runContext: RunContext<Context>,
input: string,
details?: { toolCall: protocol.FunctionCallItem },
): Promise<string | Result> {
return _invoke(runContext, input).catch<string>((error) => {
return _invoke(runContext, input, details).catch<string>((error) => {
if (toolErrorFunction) {
const currentSpan = getCurrentSpan();
currentSpan?.setError({
Expand Down
4 changes: 3 additions & 1 deletion packages/agents-realtime/src/realtimeSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,9 @@ export class RealtimeSession<
});

this.#context.context.history = JSON.parse(JSON.stringify(this.#history)); // deep copy of the history
const result = await tool.invoke(this.#context, toolCall.arguments);
const result = await tool.invoke(this.#context, toolCall.arguments, {
toolCall,
});
let stringResult: string;
if (isBackgroundResult(result)) {
// Don't generate a new response, just send the result
Expand Down