Limit tool calls recursion

7db85e7ed869023dacdb5127ec713ecbe38d5b65

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +13 -5Showing whitespace changes
public/script.js+7 -5
@@ -3409,9 +3409,9 @@ function removeLastMessage() {
34093409 * @param {GenerateOptions} options Generation options
34103410 * @param {boolean} dryRun Whether to actually generate a message or just assemble the prompt
34113411 * @returns {Promise<any>} Returns a promise that resolves when the text is done generating.
34123412 * @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
34133413 */
34143414export async function Generate(type, { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, quietName, depth = 0 } = {}, dryRun = false) {
34153415 console.log('Generate entered');
34163416 setGenerationProgress(0);
34173417 generation_started = new Date();
@@ -3631,7 +3631,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
36313631
36323632 // Collect messages with usable content
36333633 const canUseTools = ToolManager.isToolCallingSupported();
36343634 const canPerformToolCalls = !dryRun && ToolManager.canPerformToolCalls(type) && depth < ToolManager.RECURSE_LIMIT;
36353635 let coreChat = chat.filter(x => !x.is_system || (canUseTools && Array.isArray(x.extra?.tool_invocations)));
36363636 if (type === 'swipe') {
36373637 coreChat.pop();
@@ -4485,8 +4485,9 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
44854485 }
44864486
44874487 streamingProcessor = null;
4488+ depth = depth + 1;
44884489 await ToolManager.saveFunctionToolInvocations(invocationResult.invocations);
44894490 return Generate('normal', { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, quietName, depth }, dryRun);
44904491 }
44914492 }
44924493
@@ -4577,8 +4578,9 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
45774578 return;
45784579 }
45794580
4581+ depth = depth + 1;
45804582 await ToolManager.saveFunctionToolInvocations(invocationResult.invocations);
45814583 return Generate('normal', { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, quietName, depth }, dryRun);
45824584 }
45834585 }
45844586
public/scripts/tool-calling.js+6 -0
@@ -211,6 +211,12 @@ export class ToolManager {
211211 static #INPUT_DELTA_KEY = '__input_json_delta';
212212
213213 /**
214+ * The maximum number of times to recurse when parsing tool calls.
215+ * @type {number}
216+ */
217+ static RECURSE_LIMIT = 5;
218+
219+ /**
214220 * Returns an Array of all tools that have been registered.
215221 * @type {ToolDefinition[]}
216222 */