Skip to content

Commit e0669a2

Browse files
committed
docs: wire echoTool into mainAgent example and update tests to use RunContext for asTool.invoke
1 parent 76608ae commit e0669a2

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

examples/basic/tool-use-behavior.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { z } from 'zod';
2-
import { Agent, run, tool } from '@openai/agents';
2+
import { Agent, run, tool, RunContext, RunResult } from '@openai/agents';
33

44
const Weather = z.object({
55
city: z.string(),
@@ -46,6 +46,19 @@ const agent3 = new Agent({
4646
tools: [getWeatherTool, saySomethingTool],
4747
});
4848

49+
const agentWithRunResult = new Agent({
50+
name: 'Tool agent with RunResult',
51+
instructions,
52+
toolUseBehavior: { stopAtToolNames: ['get_weather'] },
53+
outputType: Weather,
54+
tools: [getWeatherTool, saySomethingTool],
55+
});
56+
57+
const weatherToolWithRunResult = agentWithRunResult.asTool({
58+
toolName: 'weather_summary',
59+
returnRunResult: true,
60+
});
61+
4962
async function main() {
5063
const input = 'What is the weather in San Francisco?';
5164
const result = await run(agent, input);
@@ -65,6 +78,12 @@ async function main() {
6578
const finalOutput3 = result3.finalOutput;
6679
// The weather in San Francisco is sunny. Thanks for asking!
6780
console.log(finalOutput3);
81+
82+
const result4 = (await weatherToolWithRunResult.invoke(
83+
new RunContext(),
84+
JSON.stringify({ input }),
85+
)) as RunResult<any, Agent<any, any>>;
86+
console.log('agentWithRunResult.newItems:', result4.newItems);
6887
}
6988

7089
main();

examples/docs/tools/agentsAsTools.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,17 @@ const summarizerTool = summarizer.asTool({
1010
toolDescription: 'Generate a concise summary of the supplied text.',
1111
});
1212

13+
const echoAgent = new Agent({
14+
name: 'Echo',
15+
instructions: 'Repeat whatever the user says.',
16+
});
17+
18+
const echoTool = echoAgent.asTool({
19+
toolName: 'echo_text',
20+
returnRunResult: true,
21+
});
22+
1323
const mainAgent = new Agent({
1424
name: 'Research assistant',
15-
tools: [summarizerTool],
25+
tools: [summarizerTool, echoTool],
1626
});

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,52 @@ describe('Agent', () => {
217217
);
218218
});
219219

220+
it('should expose finalOutput with stopAtToolNames using returnRunResult', async () => {
221+
setDefaultModelProvider(new FakeModelProvider());
222+
223+
const subAgent = new Agent({
224+
name: 'Echo',
225+
instructions: 'Repeat what the user says.',
226+
});
227+
228+
const queryTool = subAgent.asTool({
229+
toolName: 'query_action_logs',
230+
toolDescription: 'Echo tool that represents a long-running tool',
231+
});
232+
233+
const outerAgent = new Agent({
234+
name: 'Parent',
235+
instructions: 'Use the tool then stop.',
236+
tools: [queryTool],
237+
toolUseBehavior: { stopAtToolNames: ['query_action_logs'] },
238+
});
239+
240+
const outerToolDefault = outerAgent.asTool({
241+
toolName: 'get_action_logs',
242+
});
243+
244+
const outerToolWithRunResult = outerAgent.asTool({
245+
toolName: 'get_action_logs_rich',
246+
returnRunResult: true,
247+
});
248+
249+
const input = JSON.stringify({ input: 'hello' });
250+
251+
const resultDefault = await outerToolDefault.invoke(
252+
new RunContext({}),
253+
input,
254+
);
255+
expect(resultDefault).toBe('Hello World');
256+
257+
const resultWithRunResult = await outerToolWithRunResult.invoke(
258+
new RunContext({}),
259+
input,
260+
);
261+
expect(
262+
(resultWithRunResult as RunResult<any, Agent<any, any>>).finalOutput,
263+
).toBe('Hello World');
264+
});
265+
220266
it('should process final output (json schema)', async () => {
221267
const agent = new Agent({
222268
name: 'Test Agent',

0 commit comments

Comments
 (0)