replaced returnChunks with return arg to use existing helper functions
Signed| @@ -30,6 +30,8 @@ import { textgen_types, textgenerationwebui_settings } from '../../textgen-setti | ||
| 30 | 30 | import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js'; |
| 31 | 31 | import { SlashCommand } from '../../slash-commands/SlashCommand.js'; |
| 32 | 32 | import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from '../../slash-commands/SlashCommandArgument.js'; |
| 33 | +import { SlashCommandEnumValue, enumTypes } from '../../slash-commands/SlashCommandEnumValue.js'; | |
| 34 | +import { slashCommandReturnHelper } from '../../slash-commands/SlashCommandReturnHelper.js'; | |
| 33 | 35 | import { callGenericPopup, POPUP_RESULT, POPUP_TYPE } from '../../popup.js'; |
| 34 | 36 | import { generateWebLlmChatPrompt, isWebLlmSupported } from '../shared.js'; |
| 35 | 37 | |
| @@ -1619,25 +1621,31 @@ jQuery(async () => { | ||
| 1619 | 1621 | const attachments = source ? getDataBankAttachmentsForSource(source, false) : getDataBankAttachments(false); |
| 1620 | 1622 | const collectionIds = await ingestDataBankAttachments(String(source)); |
| 1621 | 1623 | const queryResults = await queryMultipleCollections(collectionIds, String(query), resultSize, threshold); |
| 1622 | - const returnChunks = String(args?.returnChunks).toLowerCase() === 'true' | |
| 1623 | 1624 | |
| 1624 | - if (returnChunks) { | |
| 1625 | + // Get URLs | |
| 1626 | + const urls = Object | |
| 1627 | + .keys(queryResults) | |
| 1628 | + .map(x => attachments.find(y => getFileCollectionId(y.url) === x)) | |
| 1629 | + .filter(x => x) | |
| 1630 | + .map(x => x.url); | |
| 1631 | + | |
| 1632 | + // Gets the actual text content of chunks | |
| 1633 | + const getChunksText = () => { | |
| 1625 | 1634 | let textResult = ''; |
| 1626 | 1635 | for (const collectionId in queryResults) { |
| 1627 | 1636 | const metadata = queryResults[collectionId].metadata?.filter(x => x.text)?.sort((a, b) => a.index - b.index)?.map(x => x.text)?.filter(onlyUnique) || []; |
| 1628 | 1637 | textResult += metadata.join('\n') + '\n\n'; |
| 1629 | 1638 | } |
| 1630 | 1639 | return textResult; |
| 1631 | - } else { | |
| 1640 | + }; | |
| 1632 | - // Map collection IDs to file URLs | |
| 1633 | - const urls = Object | |
| 1634 | - .keys(queryResults) | |
| 1635 | - .map(x => attachments.find(y => getFileCollectionId(y.url) === x)) | |
| 1636 | - .filter(x => x) | |
| 1637 | - .map(x => x.url); | |
| 1638 | 1641 | |
| 1639 | - return JSON.stringify(urls); | |
| 1642 | + if (args.return === 'chunks') { | |
| 1643 | + return getChunksText(); | |
| 1640 | 1644 | } |
| 1645 | + | |
| 1646 | + // @ts-ignore | |
| 1647 | + return slashCommandReturnHelper.doReturn(args.return ?? 'object', urls, { objectToStringFunc: list => list.join('\n') }); | |
| 1648 | + | |
| 1641 | 1649 | }, |
| 1642 | 1650 | aliases: ['databank-search', 'data-bank-search'], |
| 1643 | 1651 | helpString: 'Search the Data Bank for a specific query using vector similarity. Returns a list of file URLs with the most relevant content.', |
| @@ -1645,7 +1653,17 @@ jQuery(async () => { | ||
| 1645 | 1653 | new SlashCommandNamedArgument('threshold', 'Threshold for the similarity score in the [0, 1] range. Uses the global config value if not set.', ARGUMENT_TYPE.NUMBER, false, false, ''), |
| 1646 | 1654 | new SlashCommandNamedArgument('resultSize', 'Maximum number of query results to return.', ARGUMENT_TYPE.NUMBER, false, false, ''), |
| 1647 | 1655 | new SlashCommandNamedArgument('source', 'Optional filter for the attachments by source.', ARGUMENT_TYPE.STRING, false, false, '', ['global', 'character', 'chat']), |
| 1648 | - new SlashCommandNamedArgument('returnChunks', 'If true, returns the actual content chunks instead of URLs.', ARGUMENT_TYPE.STRING, false, false, '', ['true', 'false']), | |
| 1656 | + SlashCommandNamedArgument.fromProps({ | |
| 1657 | + name: 'return', | |
| 1658 | + description: 'How you want the return value to be provided', | |
| 1659 | + typeList: [ARGUMENT_TYPE.STRING], | |
| 1660 | + defaultValue: 'object', | |
| 1661 | + enumList: [ | |
| 1662 | + new SlashCommandEnumValue('chunks', 'Return the actual content chunks', enumTypes.enum, '{}'), | |
| 1663 | + ...slashCommandReturnHelper.enumList({ allowObject: true }) | |
| 1664 | + ], | |
| 1665 | + forceEnum: true, | |
| 1666 | + }) | |
| 1649 | 1667 | ], |
| 1650 | 1668 | unnamedArgumentList: [ |
| 1651 | 1669 | new SlashCommandArgument('Query to search by.', ARGUMENT_TYPE.STRING, true, false), |