Add role and hidden arguments to /messages commands

This commit is contained in:
Cohee 2024-05-13 21:36:55 +03:00
parent 28da838bd1
commit 14aa70eea8
1 changed files with 42 additions and 2 deletions

View File

@ -847,6 +847,12 @@ SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'messages',
new SlashCommandNamedArgument( new SlashCommandNamedArgument(
'names', 'show message author names', [ARGUMENT_TYPE.BOOLEAN], false, false, 'off', ['off', 'on'], 'names', 'show message author names', [ARGUMENT_TYPE.BOOLEAN], false, false, 'off', ['off', 'on'],
), ),
new SlashCommandNamedArgument(
'hidden', 'include hidden messages', [ARGUMENT_TYPE.BOOLEAN], false, false, 'on', ['off', 'on'],
),
new SlashCommandNamedArgument(
'role', 'filter messages by role' , [ARGUMENT_TYPE.STRING], false, false, null, ['system', 'assistant', 'user'],
),
], ],
unnamedArgumentList: [ unnamedArgumentList: [
new SlashCommandArgument( new SlashCommandArgument(
@ -858,6 +864,12 @@ SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'messages',
<div> <div>
Returns the specified message or range of messages as a string. Returns the specified message or range of messages as a string.
</div> </div>
<div>
Use the <code>hidden=off</code> argument to exclude hidden messages.
</div>
<div>
Use the <code>role</code> argument to filter messages by role. Possible values are: system, assistant, user.
</div>
<div> <div>
<strong>Examples:</strong> <strong>Examples:</strong>
<ul> <ul>
@ -1310,13 +1322,35 @@ async function popupCallback(args, value) {
function getMessagesCallback(args, value) { function getMessagesCallback(args, value) {
const includeNames = !isFalseBoolean(args?.names); const includeNames = !isFalseBoolean(args?.names);
const includeHidden = isTrueBoolean(args?.hidden);
const role = args?.role;
const range = stringToRange(value, 0, chat.length - 1); const range = stringToRange(value, 0, chat.length - 1);
if (!range) { if (!range) {
console.warn(`WARN: Invalid range provided for /getmessages command: ${value}`); console.warn(`WARN: Invalid range provided for /messages command: ${value}`);
return ''; return '';
} }
const filterByRole = (mes) => {
if (!role) {
return true;
}
if (role === 'system') {
return mes.is_system || mes.extra?.type === system_message_types.NARRATOR;
}
if (role === 'assistant') {
return !mes.is_user;
}
if (role === 'user') {
return mes.is_user;
}
throw new Error(`Invalid role provided. Expected one of: system, assistant, user. Got: ${role}`);
};
const messages = []; const messages = [];
for (let messageId = range.start; messageId <= range.end; messageId++) { for (let messageId = range.start; messageId <= range.end; messageId++) {
@ -1326,7 +1360,13 @@ function getMessagesCallback(args, value) {
continue; continue;
} }
if (message.is_system) { if (role && !filterByRole(message)) {
console.debug(`/messages: Skipping message with ID ${messageId} due to role filter`);
continue;
}
if (!includeHidden && message.is_system) {
console.debug(`/messages: Skipping hidden message with ID ${messageId}`);
continue; continue;
} }