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