Nicely format message for tool calls

This commit is contained in:
Cohee 2024-10-02 23:13:11 +03:00
parent 2dad86e076
commit 2b7c03f3b0
2 changed files with 32 additions and 3 deletions

View File

@ -108,6 +108,7 @@ export class ToolManager {
const definition = new ToolDefinition(name, description, parameters, action); const definition = new ToolDefinition(name, description, parameters, action);
this.#tools.set(name, definition); this.#tools.set(name, definition);
console.log('[ToolManager] Registered function tool:', definition);
} }
/** /**
@ -121,6 +122,7 @@ export class ToolManager {
} }
this.#tools.delete(name); this.#tools.delete(name);
console.log(`[ToolManager] Unregistered function tool: ${name}`);
} }
/** /**
@ -333,8 +335,9 @@ export class ToolManager {
const parameters = toolCall.function.arguments; const parameters = toolCall.function.arguments;
const name = toolCall.function.name; const name = toolCall.function.name;
toastr.info('Invoking function tool: ' + name); const toast = toastr.info(`Invoking function tool: ${name}`);
const result = await ToolManager.invokeFunctionTool(name, parameters); const result = await ToolManager.invokeFunctionTool(name, parameters);
toastr.clear(toast);
console.log('Function tool result:', result); console.log('Function tool result:', result);
// Save a successful invocation // Save a successful invocation
@ -374,18 +377,35 @@ export class ToolManager {
return invocations; return invocations;
} }
/**
* Formats a message with tool invocations.
* @param {ToolInvocation[]} invocations Tool invocations.
* @returns {string} Formatted message with tool invocations.
*/
static #formatMessage(invocations) {
const toolNames = invocations.map(i => i.name).join(', ');
const detailsElement = document.createElement('details');
const summaryElement = document.createElement('summary');
const preElement = document.createElement('pre');
const codeElement = document.createElement('code');
codeElement.textContent = JSON.stringify(invocations, null, 2);
summaryElement.textContent = `Performed tool calls: ${toolNames}`;
preElement.append(codeElement);
detailsElement.append(summaryElement, preElement);
return detailsElement.outerHTML;
}
/** /**
* Saves function tool invocations to the last user chat message extra metadata. * Saves function tool invocations to the last user chat message extra metadata.
* @param {ToolInvocation[]} invocations Successful tool invocations * @param {ToolInvocation[]} invocations Successful tool invocations
*/ */
static saveFunctionToolInvocations(invocations) { static saveFunctionToolInvocations(invocations) {
const toolNames = invocations.map(i => i.name).join(', ');
const message = { const message = {
name: systemUserName, name: systemUserName,
force_avatar: system_avatar, force_avatar: system_avatar,
is_system: true, is_system: true,
is_user: false, is_user: false,
mes: `Performed tool calls: ${toolNames}`, mes: ToolManager.#formatMessage(invocations),
extra: { extra: {
isSmallSys: true, isSmallSys: true,
tool_invocations: invocations, tool_invocations: invocations,

View File

@ -418,6 +418,15 @@ small {
text-align: center; text-align: center;
} }
.mes.smallSysMes pre {
text-align: initial;
word-break: break-all;
}
.mes.smallSysMes summary {
cursor: pointer;
}
.mes.smallSysMes .mes_text p:last-child { .mes.smallSysMes .mes_text p:last-child {
margin: 0; margin: 0;
} }