Skip to content

Commit 8f23102

Browse files
feat(core): add tool call details to tool events (#102)
1 parent 0565bf1 commit 8f23102

File tree

5 files changed

+59
-15
lines changed

5 files changed

+59
-15
lines changed

packages/agents-core/src/lifecycle.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
EventEmitterEvents,
88
} from '@openai/agents-core/_shims';
99
import { TextOutput, UnknownContext } from './types';
10+
import * as protocol from './types/protocol';
1011

1112
export abstract class EventEmitterDelegate<
1213
EventTypes extends EventEmitterEvents = Record<string, any[]>,
@@ -64,7 +65,11 @@ export type AgentHookEvents<
6465
* @param agent - The agent that is starting a tool
6566
* @param tool - The tool that is starting
6667
*/
67-
agent_tool_start: [context: RunContext<TContext>, tool: Tool<any>];
68+
agent_tool_start: [
69+
context: RunContext<TContext>,
70+
tool: Tool<any>,
71+
details: { toolCall: protocol.ToolCallItem },
72+
];
6873
/**
6974
* @param context - The context of the run
7075
* @param agent - The agent that is ending a tool
@@ -75,6 +80,7 @@ export type AgentHookEvents<
7580
context: RunContext<TContext>,
7681
tool: Tool<any>,
7782
result: string,
83+
details: { toolCall: protocol.ToolCallItem },
7884
];
7985
};
8086

@@ -129,6 +135,7 @@ export type RunHookEvents<
129135
context: RunContext<TContext>,
130136
agent: Agent<TContext, TOutput>,
131137
tool: Tool,
138+
details: { toolCall: protocol.ToolCallItem },
132139
];
133140
/**
134141
* @param context - The context of the run
@@ -141,6 +148,7 @@ export type RunHookEvents<
141148
agent: Agent<TContext, TOutput>,
142149
tool: Tool,
143150
result: string,
151+
details: { toolCall: protocol.ToolCallItem },
144152
];
145153
};
146154

packages/agents-core/src/runImplementation.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -732,8 +732,12 @@ export async function executeFunctionToolCalls<TContext = UnknownContext>(
732732
}
733733

734734
try {
735-
runner.emit('agent_tool_start', state._context, agent, toolRun.tool);
736-
agent.emit('agent_tool_start', state._context, toolRun.tool);
735+
runner.emit('agent_tool_start', state._context, agent, toolRun.tool, {
736+
toolCall: toolRun.toolCall,
737+
});
738+
agent.emit('agent_tool_start', state._context, toolRun.tool, {
739+
toolCall: toolRun.toolCall,
740+
});
737741
const result = await toolRun.tool.invoke(
738742
state._context,
739743
toolRun.toolCall.arguments,
@@ -747,12 +751,14 @@ export async function executeFunctionToolCalls<TContext = UnknownContext>(
747751
agent,
748752
toolRun.tool,
749753
stringResult,
754+
{ toolCall: toolRun.toolCall },
750755
);
751756
agent.emit(
752757
'agent_tool_end',
753758
state._context,
754759
toolRun.tool,
755760
stringResult,
761+
{ toolCall: toolRun.toolCall },
756762
);
757763

758764
if (runner.config.traceIncludeSensitiveData) {
@@ -879,9 +885,11 @@ export async function executeComputerActions(
879885
const toolCall = action.toolCall;
880886

881887
// Hooks: on_tool_start (global + agent)
882-
runner.emit('agent_tool_start', runContext, agent, action.computer);
888+
runner.emit('agent_tool_start', runContext, agent, action.computer, {
889+
toolCall,
890+
});
883891
if (typeof agent.emit === 'function') {
884-
agent.emit('agent_tool_start', runContext, action.computer);
892+
agent.emit('agent_tool_start', runContext, action.computer, { toolCall });
885893
}
886894

887895
// Run the action and get screenshot
@@ -894,9 +902,13 @@ export async function executeComputerActions(
894902
}
895903

896904
// Hooks: on_tool_end (global + agent)
897-
runner.emit('agent_tool_end', runContext, agent, action.computer, output);
905+
runner.emit('agent_tool_end', runContext, agent, action.computer, output, {
906+
toolCall,
907+
});
898908
if (typeof agent.emit === 'function') {
899-
agent.emit('agent_tool_end', runContext, action.computer, output);
909+
agent.emit('agent_tool_end', runContext, action.computer, output, {
910+
toolCall,
911+
});
900912
}
901913

902914
// Always return a screenshot as a base64 data URL

packages/agents-core/test/runImplementation.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,16 @@ describe('executeFunctionToolCalls', () => {
537537
);
538538

539539
expect(res[0].type).toBe('function_output');
540-
expect(start).toHaveBeenCalled();
541-
expect(end).toHaveBeenCalled();
540+
expect(start).toHaveBeenCalledWith(state._context, state._currentAgent, t, {
541+
toolCall,
542+
});
543+
expect(end).toHaveBeenCalledWith(
544+
state._context,
545+
state._currentAgent,
546+
t,
547+
'ok',
548+
{ toolCall },
549+
);
542550
expect(res[0].runItem).toBeInstanceOf(ToolCallOutputItem);
543551
expect(invokeSpy).toHaveBeenCalled();
544552
});

packages/agents-realtime/src/realtimeSession.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,12 @@ export class RealtimeSession<
372372
callId: toolCall.callId,
373373
});
374374
if (approval === false) {
375-
this.emit('agent_tool_start', this.#context, this.#currentAgent, tool);
376-
this.#currentAgent.emit('agent_tool_start', this.#context, tool);
375+
this.emit('agent_tool_start', this.#context, this.#currentAgent, tool, {
376+
toolCall,
377+
});
378+
this.#currentAgent.emit('agent_tool_start', this.#context, tool, {
379+
toolCall,
380+
});
377381

378382
const result = 'Tool execution was not approved.';
379383
this.#transport.sendFunctionCallOutput(toolCall, result, true);
@@ -383,8 +387,11 @@ export class RealtimeSession<
383387
this.#currentAgent,
384388
tool,
385389
result,
390+
{ toolCall },
386391
);
387-
this.#currentAgent.emit('agent_tool_end', this.#context, tool, result);
392+
this.#currentAgent.emit('agent_tool_end', this.#context, tool, result, {
393+
toolCall,
394+
});
388395
return;
389396
} else if (typeof approval === 'undefined') {
390397
this.emit(
@@ -401,8 +408,12 @@ export class RealtimeSession<
401408
}
402409
}
403410

404-
this.emit('agent_tool_start', this.#context, this.#currentAgent, tool);
405-
this.#currentAgent.emit('agent_tool_start', this.#context, tool);
411+
this.emit('agent_tool_start', this.#context, this.#currentAgent, tool, {
412+
toolCall,
413+
});
414+
this.#currentAgent.emit('agent_tool_start', this.#context, tool, {
415+
toolCall,
416+
});
406417

407418
this.#context.context.history = JSON.parse(JSON.stringify(this.#history)); // deep copy of the history
408419
const result = await tool.invoke(this.#context, toolCall.arguments);
@@ -414,12 +425,14 @@ export class RealtimeSession<
414425
this.#currentAgent,
415426
tool,
416427
stringResult,
428+
{ toolCall },
417429
);
418430
this.#currentAgent.emit(
419431
'agent_tool_end',
420432
this.#context,
421433
tool,
422434
stringResult,
435+
{ toolCall },
423436
);
424437
}
425438

packages/agents-realtime/src/realtimeSessionEvents.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { RealtimeItem } from './items';
99
import { RealtimeAgent } from './realtimeAgent';
1010
import { TransportEvent, TransportLayerAudio } from './transportLayerEvents';
1111
import { RealtimeContextData } from './realtimeSession';
12+
import { protocol } from '@openai/agents-core';
1213

1314
type AgentWithOrWithoutHistory<TContext> =
1415
| RealtimeAgent<TContext>
@@ -59,6 +60,7 @@ export type RealtimeSessionEventTypes<TContext = unknown> = {
5960
context: RunContext<RealtimeContextData<TContext>>,
6061
agent: AgentWithOrWithoutHistory<TContext>,
6162
tool: FunctionTool<RealtimeContextData<TContext>>,
63+
details: { toolCall: protocol.ToolCallItem },
6264
];
6365

6466
/**
@@ -68,7 +70,8 @@ export type RealtimeSessionEventTypes<TContext = unknown> = {
6870
context: RunContext<RealtimeContextData<TContext>>,
6971
agent: AgentWithOrWithoutHistory<TContext>,
7072
tool: FunctionTool<RealtimeContextData<TContext>>,
71-
result?: string,
73+
result: string,
74+
details: { toolCall: protocol.ToolCallItem },
7275
];
7376

7477
/**

0 commit comments

Comments
 (0)