Skip to content

Commit cd4dc71

Browse files
Matt Appersonclaude
andcommitted
Update maxToolRounds to accept TurnContext-based function
- Changed MaxToolRounds type to accept (context: TurnContext) => boolean - Removed old 3-parameter function signature for simplicity - Function receives full TurnContext with numberOfTurns, messageHistory, model/models - Returns true to allow another turn, false to stop execution - Updated examples to demonstrate the new function signature - Simplified implementation logic in response-wrapper.ts This provides more context to the maxToolRounds function and makes the API more consistent with tool execute functions. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent a4e2b15 commit cd4dc71

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

examples/tools-example.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
*
1111
* The API is simple: just call getResponse() with tools, and await the result.
1212
* Tools are executed transparently before getMessage() or getText() returns!
13+
*
14+
* maxToolRounds can be:
15+
* - A number: Maximum number of tool execution rounds (default: 5)
16+
* - A function: (context: TurnContext) => boolean
17+
* - Return true to allow another turn
18+
* - Return false to stop execution
19+
* - Context includes: numberOfTurns, messageHistory, model/models
1320
*/
1421

1522
import { OpenRouter } from "../src/index.js";
@@ -60,6 +67,11 @@ async function basicToolExample() {
6067
model: "openai/gpt-4o",
6168
input: "What's the weather like in San Francisco?",
6269
tools: [weatherTool],
70+
// Example: limit to 3 turns using a function
71+
maxToolRounds: (context) => {
72+
console.log(`Checking if we should continue (currently on turn ${context.numberOfTurns})`);
73+
return context.numberOfTurns < 3; // Allow up to 3 turns
74+
},
6375
});
6476

6577
// Tools are automatically executed! Just get the final message

src/funcs/getResponse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import { convertEnhancedToolsToAPIFormat } from "../lib/tool-executor.js";
5858
* }
5959
* }
6060
* }],
61-
* maxToolRounds: 5, // or function: (round, calls, responses) => boolean
61+
* maxToolRounds: 5, // or function: (context: TurnContext) => boolean
6262
* });
6363
* const message = await response.getMessage(); // Tools auto-executed!
6464
*

src/lib/response-wrapper.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,14 @@ export class ResponseWrapper {
179179
break;
180180
}
181181
} else if (typeof maxToolRounds === "function") {
182-
const shouldContinue = maxToolRounds(
183-
currentRound,
184-
currentToolCalls,
185-
this.allToolExecutionRounds.map((r) => r.response)
186-
);
182+
// Function signature: (context: TurnContext) => boolean
183+
const turnContext: TurnContext = {
184+
numberOfTurns: currentRound + 1,
185+
messageHistory: currentInput,
186+
...(this.options.request.model && { model: this.options.request.model }),
187+
...(this.options.request.models && { models: this.options.request.models }),
188+
};
189+
const shouldContinue = maxToolRounds(turnContext);
187190
if (!shouldContinue) {
188191
break;
189192
}

src/lib/tool-types.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,7 @@ export interface ToolExecutionResult {
159159
*/
160160
export type MaxToolRounds =
161161
| number
162-
| ((
163-
round: number,
164-
toolCalls: ParsedToolCall[],
165-
responses: any[] // OpenResponsesNonStreamingResponse[]
166-
) => boolean); // Return true to continue, false to stop
162+
| ((context: TurnContext) => boolean); // Return true to allow another turn, false to stop
167163

168164
/**
169165
* Result of executeTools operation

0 commit comments

Comments
 (0)