Merge branch 'tool-calling' of https://github.com/SillyTavern/SillyTavern into tool-calling

This commit is contained in:
Cohee 2024-10-04 13:39:13 +03:00
commit ca1f6a56c3
2 changed files with 27 additions and 17 deletions

View File

@ -8180,6 +8180,8 @@ window['SillyTavern'].getContext = function () {
unregisterMacro: MacrosParser.unregisterMacro.bind(MacrosParser),
registerFunctionTool: ToolManager.registerFunctionTool.bind(ToolManager),
unregisterFunctionTool: ToolManager.unregisterFunctionTool.bind(ToolManager),
isToolCallingSupported: ToolManager.isToolCallingSupported.bind(ToolManager),
canPerformToolCalls: ToolManager.canPerformToolCalls.bind(ToolManager),
registerDebugFunction: registerDebugFunction,
/** @deprecated Use renderExtensionTemplateAsync instead. */
renderExtensionTemplate: renderExtensionTemplate,

View File

@ -18,6 +18,16 @@ import { Popup } from './popup.js';
* @property {Error[]} errors Errors that occurred during tool invocation
*/
/**
* @typedef {object} ToolRegistration
* @property {string} name - The name of the tool.
* @property {string} displayName - The display name of the tool.
* @property {string} description - A description of the tool.
* @property {object} parameters - The parameters for the tool.
* @property {function} action - The action to perform when the tool is invoked.
* @property {function} formatMessage - A function to format the tool call message.
*/
/**
* A class that represents a tool definition.
*/
@ -136,13 +146,7 @@ export class ToolManager {
/**
* Registers a new tool with the tool registry.
* @param {object} tool The tool to register.
* @param {string} tool.name The name of the tool.
* @param {string} tool.displayName A user-friendly display name for the tool.
* @param {string} tool.description A description of what the tool does.
* @param {object} tool.parameters A JSON schema for the parameters that the tool accepts.
* @param {function} tool.action A function that will be called when the tool is executed.
* @param {function} tool.formatMessage A function that will be called to format the tool call toast.
* @param {ToolRegistration} tool The tool to register.
*/
static registerFunctionTool({ name, displayName, description, parameters, action, formatMessage }) {
// Convert WIP arguments
@ -201,6 +205,12 @@ export class ToolManager {
}
}
/**
* Formats a message for a tool call by name.
* @param {string} name The name of the tool to format the message for.
* @param {object} parameters Function tool call parameters.
* @returns {string} The formatted message for the tool call.
*/
static formatToolCallMessage(name, parameters) {
if (!this.#tools.has(name)) {
return `Invoked unknown tool: ${name}`;
@ -312,9 +322,14 @@ export class ToolManager {
}
}
/**
* Apply a tool call delta to a target object.
* @param {object} target The target object to apply the delta to
* @param {object} delta The delta object to apply
*/
static #applyToolCallDelta(target, delta) {
for (const key in delta) {
if (!delta.hasOwnProperty(key)) continue;
if (!Object.prototype.hasOwnProperty.call(delta, key)) continue;
if (key === '__proto__' || key === 'constructor') continue;
const deltaValue = delta[key];
@ -426,13 +441,6 @@ export class ToolManager {
errors: [],
};
const toolCalls = ToolManager.#getToolCallsFromData(data);
const oaiCompatibleSources = [
chat_completion_sources.OPENAI,
chat_completion_sources.CUSTOM,
chat_completion_sources.MISTRALAI,
chat_completion_sources.OPENROUTER,
chat_completion_sources.GROQ,
];
if (!Array.isArray(toolCalls)) {
return result;
@ -480,7 +488,7 @@ export class ToolManager {
* @param {ToolInvocation[]} invocations Tool invocations.
* @returns {string} Formatted message with tool invocations.
*/
static #formatMessage(invocations) {
static #formatToolInvocationMessage(invocations) {
const tryParse = (x) => { try { return JSON.parse(x); } catch { return x; } };
const data = structuredClone(invocations);
const detailsElement = document.createElement('details');
@ -510,7 +518,7 @@ export class ToolManager {
force_avatar: system_avatar,
is_system: true,
is_user: false,
mes: ToolManager.#formatMessage(invocations),
mes: ToolManager.#formatToolInvocationMessage(invocations),
extra: {
isSmallSys: true,
tool_invocations: invocations,