Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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
7 changes: 7 additions & 0 deletions .changeset/khaki-rivers-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@openai/agents': patch
'@openai/agents-core': patch
'@openai/agents-openai': patch
---

feat: #1097 support overrideArguments for approved tool calls
7 changes: 3 additions & 4 deletions examples/agent-patterns/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Agent Pattern Examples

This directory contains small scripts that demonstrate different agent patterns.
Run them with `pnpm` using the commands shown below.
This directory contains small scripts that demonstrate different agent patterns. Run them with `pnpm` using the commands shown below.

- `agents-as-tools.ts` – Orchestrate translator agents using them as tools.
```bash
Expand All @@ -23,11 +22,11 @@ Run them with `pnpm` using the commands shown below.
```bash
pnpm -F agent-patterns start:forcing-tool-use
```
- `human-in-the-loop.ts` – Manually approve certain tool calls.
- `human-in-the-loop.ts` – Manually approve certain tool calls and override approved arguments.
```bash
pnpm examples:human-in-the-loop
```
- `human-in-the-loop-stream.ts` – Streaming version of human approval.
- `human-in-the-loop-stream.ts` – Streaming version of human approval with approved-argument overrides.
```bash
pnpm examples:streamed:human-in-the-loop
```
Expand Down
30 changes: 23 additions & 7 deletions examples/agent-patterns/human-in-the-loop-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ async function confirm(question: string): Promise<boolean> {
}

async function main() {
const APPROVER_NAME = 'Kaz';
const temperatureParams = z.object({
city: z.string(),
approver: z.string().nullable().optional(),
});

// Define a tool that requires approval for certain inputs
const getWeatherTool = tool({
name: 'get_weather',
Expand All @@ -40,12 +46,11 @@ async function main() {
const getTemperatureTool = tool({
name: 'get_temperature',
description: 'Get the temperature for a given city',
parameters: z.object({
city: z.string(),
}),
parameters: temperatureParams,
needsApproval: async (_ctx, { city }) => city.includes('Oakland'),
execute: async ({ city }) => {
return `The temperature in ${city} is 20° Celsius`;
execute: async ({ city, approver }) => {
const approvedBy = approver ? ` Approved by ${approver}.` : '';
return `The temperature in ${city} is 20° Celsius.${approvedBy}`;
},
});

Expand All @@ -68,7 +73,7 @@ async function main() {

let stream = await run(
mainAgent,
'What is the weather and temperature in San Francisco and Oakland? Use available tools as needed.',
'Please check both San Francisco and Oakland, and do not consider the task complete until you have provided the weather and temperature for both cities.',
{ stream: true },
);
stream.toTextStream({ compatibleWithNodeStreams: true }).pipe(process.stdout);
Expand All @@ -89,7 +94,18 @@ async function main() {
`Agent ${interruption.agent.name} would like to use the tool ${interruption.rawItem.name} with "${interruption.rawItem.arguments}". Do you approve?`,
);
if (ok) {
state.approve(interruption);
if (interruption.name === 'get_temperature') {
const parsedArgs = temperatureParams.parse(
JSON.parse(interruption.rawItem.arguments),
);
const overrideArguments = { ...parsedArgs, approver: APPROVER_NAME };
console.log(
`Injecting approver="${APPROVER_NAME}" into the approved tool call.`,
);
state.approve(interruption, { overrideArguments });
} else {
state.approve(interruption);
}
} else {
state.reject(interruption);
}
Expand Down
34 changes: 27 additions & 7 deletions examples/agent-patterns/human-in-the-loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,21 @@ const weatherAgent = new Agent({
tools: [getWeatherTool],
});

const temperatureParams = z.object({
city: z.string(),
approver: z.string().nullable().optional(),
});

const APPROVER_NAME = 'Kaz';

const getTemperatureTool = tool({
name: 'get_temperature',
description: 'Get the temperature for a given city',
parameters: z.object({
city: z.string(),
}),
parameters: temperatureParams,
needsApproval: async (_ctx, { city }) => city.includes('Oakland'),
execute: async ({ city }) => {
return `The temperature in ${city} is 20° Celsius`;
execute: async ({ city, approver }) => {
const approvedBy = approver ? ` Approved by ${approver}.` : '';
return `The temperature in ${city} is 20° Celsius.${approvedBy}`;
},
});

Expand Down Expand Up @@ -73,7 +79,7 @@ async function confirm(question: string) {
async function main() {
let result: RunResult<unknown, Agent<unknown, any>> = await run(
agent,
'What is the weather and temperature in San Francisco and Oakland? Use available tools as needed.',
'Please check both San Francisco and Oakland, and do not consider the task complete until you have provided the weather and temperature for both cities.',
);
let hasInterruptions = result.interruptions?.length > 0;
while (hasInterruptions) {
Expand All @@ -96,7 +102,21 @@ async function main() {
);

if (confirmed) {
state.approve(interruption);
if (
interruption.rawItem.type === 'function_call' &&
interruption.name === 'get_temperature'
) {
const parsedArgs = temperatureParams.parse(
JSON.parse(interruption.rawItem.arguments),
);
const overrideArguments = { ...parsedArgs, approver: APPROVER_NAME };
console.log(
`Injecting approver="${APPROVER_NAME}" into the approved tool call.`,
);
state.approve(interruption, { overrideArguments });
} else {
state.approve(interruption);
}
} else {
state.reject(interruption);
}
Expand Down
Loading
Loading