Skip to content

Commit becabb9

Browse files
authored
fix: #247 logging for a sub-agent w/ stopAtToolNames (#531)
1 parent 0fd8b6e commit becabb9

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

.changeset/cuddly-states-begin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openai/agents-core': patch
3+
---
4+
5+
fix: #247 logging for a sub-agent w/ stopAtToolNames

packages/agents-core/src/agent.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,20 @@ export class Agent<
556556
context,
557557
...(runOptions ?? {}),
558558
});
559+
560+
const usesStopAtToolNames =
561+
typeof this.toolUseBehavior === 'object' &&
562+
this.toolUseBehavior !== null &&
563+
'stopAtToolNames' in this.toolUseBehavior;
564+
565+
if (
566+
typeof customOutputExtractor !== 'function' &&
567+
usesStopAtToolNames
568+
) {
569+
logger.debug(
570+
`You're passing the agent (name: ${this.name}) with toolUseBehavior.stopAtToolNames configured as a tool to a different agent; this may not work as you expect. You may want to have a wrapper function tool to consistently return the final output.`,
571+
);
572+
}
559573
const outputText =
560574
typeof customOutputExtractor === 'function'
561575
? await customOutputExtractor(result as any)

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { z } from 'zod';
77
import { JsonSchemaDefinition, setDefaultModelProvider } from '../src';
88
import { FakeModelProvider } from './stubs';
99
import { Runner, RunConfig } from '../src/run';
10+
import logger from '../src/logger';
1011

1112
describe('Agent', () => {
1213
afterEach(() => {
@@ -186,6 +187,30 @@ describe('Agent', () => {
186187
expect(result2).toBe('Hello World');
187188
});
188189

190+
it('warns when using asTool with stopAtToolNames behavior without custom extractor', async () => {
191+
const warnSpy = vi.spyOn(logger, 'debug').mockImplementation(() => {});
192+
const runSpy = vi.spyOn(Runner.prototype, 'run').mockResolvedValue({
193+
rawResponses: [{ output: [] }],
194+
} as any);
195+
196+
const agent = new Agent({
197+
name: 'Stopper Agent',
198+
instructions: 'Stop instructions.',
199+
toolUseBehavior: { stopAtToolNames: ['report'] },
200+
});
201+
202+
const tool = agent.asTool({
203+
toolDescription: 'desc',
204+
});
205+
206+
await tool.invoke(new RunContext(), '{"input":"value"}');
207+
208+
expect(runSpy).toHaveBeenCalledTimes(1);
209+
expect(warnSpy).toHaveBeenCalledWith(
210+
`You're passing the agent (name: Stopper Agent) with toolUseBehavior.stopAtToolNames configured as a tool to a different agent; this may not work as you expect. You may want to have a wrapper function tool to consistently return the final output.`,
211+
);
212+
});
213+
189214
it('allows configuring needsApproval when using an agent as a tool', async () => {
190215
const approval = vi.fn().mockResolvedValue(true);
191216
const agent = new Agent({

0 commit comments

Comments
 (0)