|
| 1 | +import type { StepResult, StopCondition, Tool } from './tool-types.js'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Stop condition that checks if step count equals or exceeds a specific number |
| 5 | + * @param stepCount - The number of steps to allow before stopping |
| 6 | + * @returns StopCondition that returns true when steps.length >= stepCount |
| 7 | + * |
| 8 | + * @example |
| 9 | + * ```typescript |
| 10 | + * stopWhen: stepCountIs(5) // Stop after 5 steps |
| 11 | + * ``` |
| 12 | + */ |
| 13 | +export function stepCountIs(stepCount: number): StopCondition { |
| 14 | + return ({ steps }: { readonly steps: ReadonlyArray<StepResult> }) => steps.length >= stepCount; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Stop condition that checks if any step contains a tool call with the given name |
| 19 | + * @param toolName - The name of the tool to check for |
| 20 | + * @returns StopCondition that returns true if the tool was called in any step |
| 21 | + * |
| 22 | + * @example |
| 23 | + * ```typescript |
| 24 | + * stopWhen: hasToolCall('search') // Stop when search tool is called |
| 25 | + * ``` |
| 26 | + */ |
| 27 | +export function hasToolCall(toolName: string): StopCondition { |
| 28 | + return ({ steps }: { readonly steps: ReadonlyArray<StepResult> }) => { |
| 29 | + return steps.some((step: StepResult) => |
| 30 | + step.toolCalls.some((call: { name: string }) => call.name === toolName), |
| 31 | + ); |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Evaluates an array of stop conditions |
| 37 | + * Returns true if ANY condition returns true (OR logic) |
| 38 | + * @param options - Object containing stopConditions and steps |
| 39 | + * @returns Promise<boolean> indicating if execution should stop |
| 40 | + * |
| 41 | + * @example |
| 42 | + * ```typescript |
| 43 | + * const shouldStop = await isStopConditionMet({ |
| 44 | + * stopConditions: [stepCountIs(5), hasToolCall('search')], |
| 45 | + * steps: allSteps |
| 46 | + * }); |
| 47 | + * ``` |
| 48 | + */ |
| 49 | +export async function isStopConditionMet<TOOLS extends readonly Tool[]>(options: { |
| 50 | + readonly stopConditions: ReadonlyArray<StopCondition<TOOLS>>; |
| 51 | + readonly steps: ReadonlyArray<StepResult<TOOLS>>; |
| 52 | +}): Promise<boolean> { |
| 53 | + const { stopConditions, steps } = options; |
| 54 | + |
| 55 | + // Evaluate all conditions in parallel |
| 56 | + const results = await Promise.all( |
| 57 | + stopConditions.map((condition: StopCondition<TOOLS>) => |
| 58 | + Promise.resolve( |
| 59 | + condition({ |
| 60 | + steps, |
| 61 | + }), |
| 62 | + ), |
| 63 | + ), |
| 64 | + ); |
| 65 | + |
| 66 | + // Return true if ANY condition is true (OR logic) |
| 67 | + return results.some((result: boolean | undefined) => result === true); |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Stop when total token usage exceeds a threshold |
| 72 | + * OpenRouter-specific helper using usage data |
| 73 | + * |
| 74 | + * @param maxTokens - Maximum total tokens to allow |
| 75 | + * @returns StopCondition that returns true when token usage exceeds threshold |
| 76 | + * |
| 77 | + * @example |
| 78 | + * ```typescript |
| 79 | + * stopWhen: maxTokensUsed(10000) // Stop when total tokens exceed 10,000 |
| 80 | + * ``` |
| 81 | + */ |
| 82 | +export function maxTokensUsed(maxTokens: number): StopCondition<any> { |
| 83 | + return ({ steps }: { readonly steps: ReadonlyArray<StepResult> }) => { |
| 84 | + const totalTokens = steps.reduce( |
| 85 | + (sum: number, step: StepResult) => sum + (step.usage?.totalTokens ?? 0), |
| 86 | + 0, |
| 87 | + ); |
| 88 | + return totalTokens >= maxTokens; |
| 89 | + }; |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Stop when total cost exceeds a threshold |
| 94 | + * OpenRouter-specific helper using cost data |
| 95 | + * |
| 96 | + * @param maxCostInDollars - Maximum cost in dollars to allow |
| 97 | + * @returns StopCondition that returns true when cost exceeds threshold |
| 98 | + * |
| 99 | + * @example |
| 100 | + * ```typescript |
| 101 | + * stopWhen: maxCost(0.50) // Stop when total cost exceeds $0.50 |
| 102 | + * ``` |
| 103 | + */ |
| 104 | +export function maxCost(maxCostInDollars: number): StopCondition { |
| 105 | + return ({ steps }: { readonly steps: ReadonlyArray<StepResult> }) => { |
| 106 | + const totalCost = steps.reduce( |
| 107 | + (sum: number, step: StepResult) => sum + (step.usage?.cost ?? 0), |
| 108 | + 0, |
| 109 | + ); |
| 110 | + return totalCost >= maxCostInDollars; |
| 111 | + }; |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Stop when a specific finish reason is encountered |
| 116 | + * |
| 117 | + * @param reason - The finish reason to check for |
| 118 | + * @returns StopCondition that returns true when finish reason matches |
| 119 | + * |
| 120 | + * @example |
| 121 | + * ```typescript |
| 122 | + * stopWhen: finishReasonIs('length') // Stop when context length limit is hit |
| 123 | + * ``` |
| 124 | + */ |
| 125 | +export function finishReasonIs(reason: string): StopCondition { |
| 126 | + return ({ steps }: { readonly steps: ReadonlyArray<StepResult> }) => { |
| 127 | + return steps.some((step: StepResult) => step.finishReason === reason); |
| 128 | + }; |
| 129 | +} |
0 commit comments