Limit tool calls recursion
| @@ -3409,9 +3409,9 @@ function removeLastMessage() { | ||
| 3409 | 3409 | * @param {GenerateOptions} options Generation options |
| 3410 | 3410 | * @param {boolean} dryRun Whether to actually generate a message or just assemble the prompt |
| 3411 | 3411 | * @returns {Promise<any>} Returns a promise that resolves when the text is done generating. |
| 3412 | 3412 | * @typedef {{automatic_trigger?: boolean, force_name2?: boolean, quiet_prompt?: string, quietToLoud?: boolean, skipWIAN?: boolean, force_chid?: number, signal?: AbortSignal, quietImage?: string, quietName?: string, depth?: number }} GenerateOptions |
| 3413 | 3413 | */ |
| 3414 | 3414 | export async function Generate(type, { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, quietName, depth = 0 } = {}, dryRun = false) { |
| 3415 | 3415 | console.log('Generate entered'); |
| 3416 | 3416 | setGenerationProgress(0); |
| 3417 | 3417 | generation_started = new Date(); |
| @@ -3631,7 +3631,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | ||
| 3631 | 3631 | |
| 3632 | 3632 | // Collect messages with usable content |
| 3633 | 3633 | const canUseTools = ToolManager.isToolCallingSupported(); |
| 3634 | 3634 | const canPerformToolCalls = !dryRun && ToolManager.canPerformToolCalls(type) && depth < ToolManager.RECURSE_LIMIT; |
| 3635 | 3635 | let coreChat = chat.filter(x => !x.is_system || (canUseTools && Array.isArray(x.extra?.tool_invocations))); |
| 3636 | 3636 | if (type === 'swipe') { |
| 3637 | 3637 | coreChat.pop(); |
| @@ -4485,8 +4485,9 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | ||
| 4485 | 4485 | } |
| 4486 | 4486 | |
| 4487 | 4487 | streamingProcessor = null; |
| 4488 | + depth = depth + 1; | |
| 4488 | 4489 | await ToolManager.saveFunctionToolInvocations(invocationResult.invocations); |
| 4489 | 4490 | return Generate('normal', { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, quietName, depth }, dryRun); |
| 4490 | 4491 | } |
| 4491 | 4492 | } |
| 4492 | 4493 | |
| @@ -4577,8 +4578,9 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | ||
| 4577 | 4578 | return; |
| 4578 | 4579 | } |
| 4579 | 4580 | |
| 4581 | + depth = depth + 1; | |
| 4580 | 4582 | await ToolManager.saveFunctionToolInvocations(invocationResult.invocations); |
| 4581 | 4583 | return Generate('normal', { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, quietName, depth }, dryRun); |
| 4582 | 4584 | } |
| 4583 | 4585 | } |
| 4584 | 4586 | |
| @@ -211,6 +211,12 @@ export class ToolManager { | ||
| 211 | 211 | static #INPUT_DELTA_KEY = '__input_json_delta'; |
| 212 | 212 | |
| 213 | 213 | /** |
| 214 | + * The maximum number of times to recurse when parsing tool calls. | |
| 215 | + * @type {number} | |
| 216 | + */ | |
| 217 | + static RECURSE_LIMIT = 5; | |
| 218 | + | |
| 219 | + /** | |
| 214 | 220 | * Returns an Array of all tools that have been registered. |
| 215 | 221 | * @type {ToolDefinition[]} |
| 216 | 222 | */ |