From 755a59180270b2e949a98ffa375146a4bcd70beb Mon Sep 17 00:00:00 2001 From: Minh Nguyen Cong Date: Wed, 31 Jul 2024 12:09:03 +0200 Subject: [PATCH] Update command --- docs/ai.md | 7 +++--- src/commands/ai/ask.js | 2 +- src/commands/ai/text-gen.js | 6 ++--- src/util.js | 50 ++++++++++++++++++++++++------------- test/commands/ai.test.js | 12 ++++----- 5 files changed, 46 insertions(+), 31 deletions(-) diff --git a/docs/ai.md b/docs/ai.md index d3229328..f46e13db 100644 --- a/docs/ai.md +++ b/docs/ai.md @@ -61,7 +61,7 @@ OPTIONS --as-user=as-user Provide an ID for a user --bulk-file-path=bulk-file-path File path to bulk .csv or .json objects --csv Output formatted CSV - --dialogue_history=dialogue_history The history of prompts and answers previously passed to the LLM. + --dialogue-history=dialogue-history The history of prompts and answers previously passed to the LLM. --fields=fields Comma separated list of fields to show --items=items (required) The items to be processed by the LLM, often files. The array can @@ -76,8 +76,9 @@ OPTIONS --save-to-file-path=save-to-file-path Override default file path to save report EXAMPLE - box ai:text-gen --dialogue_history=prompt="What is the status of this document?",answer="It is in review" - --items=id=12345,type=file --prompt="What is the status of this document?" + box ai:text-gen --dialogue_history=prompt="What is the status of this document?",answer="It is in + review",created-at="2024-07-09T11:29:46.835Z" --items=id=12345,type=file --prompt="What is the status of this + document?" ``` _See code: [src/commands/ai/text-gen.js](https://github.com/box/boxcli/blob/v3.14.1/src/commands/ai/text-gen.js)_ diff --git a/src/commands/ai/ask.js b/src/commands/ai/ask.js index a327b61b..cd3bf199 100644 --- a/src/commands/ai/ask.js +++ b/src/commands/ai/ask.js @@ -53,7 +53,7 @@ AiAskCommand.flags = { id: '', type: 'file' }; - const obj = utils.parseStringToObject(input); + const obj = utils.parseStringToObject(input, ['id', 'type', 'content']); for (const key in obj) { if (key === 'id') { item.id = obj[key]; diff --git a/src/commands/ai/text-gen.js b/src/commands/ai/text-gen.js index 2673c2bb..c8e97ede 100644 --- a/src/commands/ai/text-gen.js +++ b/src/commands/ai/text-gen.js @@ -25,7 +25,7 @@ class AiTextGenCommand extends BoxCommand { } AiTextGenCommand.description = 'Sends an AI request to supported LLMs and returns an answer specifically focused on the creation of new text.'; -AiTextGenCommand.examples = ['box ai:text-gen --dialogue_history=prompt="What is the status of this document?",answer="It is in review",created-at="2024-07-09T11:29:46.835Z" --items=id=12345,type=file --prompt="What is the status of this document?"']; +AiTextGenCommand.examples = ['box ai:text-gen --dialogue-history=prompt="What is the status of this document?",answer="It is in review",created-at="2024-07-09T11:29:46.835Z" --items=id=12345,type=file --prompt="What is the status of this document?"']; AiTextGenCommand._endpoint = 'post_ai_text_gen'; AiTextGenCommand.flags = { @@ -37,7 +37,7 @@ AiTextGenCommand.flags = { multiple: true, parse(input) { const record = {}; - const obj = utils.parseStringToObject(input); + const obj = utils.parseStringToObject(input, ['prompt', 'answer', 'created-at']); for (const key in obj) { if (key === 'prompt') { record.prompt = obj[key]; @@ -62,7 +62,7 @@ AiTextGenCommand.flags = { id: '', type: 'file' }; - const obj = utils.parseStringToObject(input); + const obj = utils.parseStringToObject(input, ['id', 'type', 'content']); for (const key in obj) { if (key === 'id') { item.id = obj[key]; diff --git a/src/util.js b/src/util.js index c556fc50..80e59dc1 100644 --- a/src/util.js +++ b/src/util.js @@ -185,27 +185,41 @@ function parseMetadataString(input) { } /** - * Parse a string into an JSON object + * Parse a string into a JSON object * - * @param {string} str The string to parse + * @param {string} inputString The string to parse + * @param {string[]} keys The keys to parse from the string * @returns {Object} The parsed object */ -function parseStringToObject(str) { - const obj = {}; - const regex = /([\w-]+)=((?:"[^"]*")|[^,]*)/gu; // Regular expression to match key=value pairs, including keys with dashes and quoted values - let match; - - while ((match = regex.exec(str)) !== null) { - const key = match[1].trim(); - let value = match[2].trim(); - // Remove surrounding quotes if present - if (value.startsWith('"') && value.endsWith('"')) { - value = value.slice(1, -1); - } - obj[key] = value; - } - - return obj; +function parseStringToObject(inputString, keys) { + const result = {}; + + while (inputString.length > 0) { + inputString = inputString.trim(); + let parsedKey = inputString.split('=')[0]; + inputString = inputString.substring(inputString.indexOf('=') + 1); + + // Find the next key or the end of the string + let nextKeyIndex = inputString.length; + for (let key of keys) { + let keyIndex = inputString.indexOf(key); + if (keyIndex !== -1 && keyIndex < nextKeyIndex) { + nextKeyIndex = keyIndex; + } + } + + let parsedValue = inputString.substring(0, nextKeyIndex).trim(); + if (parsedValue.endsWith(',') && nextKeyIndex !== inputString.length) { + parsedValue = parsedValue.substring(0, parsedValue.length - 1); + } + if (parsedValue.startsWith('"') && parsedValue.endsWith('"')) { + parsedValue = parsedValue.substring(1, parsedValue.length - 1); + } + + result[parsedKey] = parsedValue; + inputString = inputString.substring(nextKeyIndex); + } + return result; } /** diff --git a/test/commands/ai.test.js b/test/commands/ai.test.js index 1f2b07cf..2f0708ed 100644 --- a/test/commands/ai.test.js +++ b/test/commands/ai.test.js @@ -35,7 +35,7 @@ describe('AI', () => { .stdout() .command([ 'ai:ask', - '--items=content="one,two,three",id=12345,type=file', + '--items=content=one,two,three,id=12345,type=file', '--prompt', 'What is the status of this document?', '--mode', @@ -59,7 +59,7 @@ describe('AI', () => { .stdout() .command([ 'ai:ask', - '--items=content="one,two,three",id=12345,type=file', + '--items=content=one,two,three,id=12345,type=file', '--prompt', 'What is the status of this document?', '--mode', @@ -104,8 +104,8 @@ describe('AI', () => { .command([ 'ai:text-gen', '--dialogue-history', - 'prompt="What is the status of this document, signatures?",answer="It is in review, waiting for signatures.",created-at=2024-07-09T11:29:46.835Z', - '--items=content="one,two,three",id=12345,type=file', + 'prompt=What is the status of this document, signatures?,answer=It is in review, waiting for signatures.,created-at=2024-07-09T11:29:46.835Z', + '--items=content=one,two,three,id=12345,type=file', '--prompt', 'What is the status of this document?', '--json', @@ -128,8 +128,8 @@ describe('AI', () => { .command([ 'ai:text-gen', '--dialogue-history', - 'prompt="What is the status of this document, signatures?",answer="It is in review, waiting for signatures.",created-at=2024-07-09T11:29:46.835Z', - '--items=content="one,two,three",id=12345,type=file', + 'prompt=What is the status of this document, signatures?,answer=It is in review, waiting for signatures.,created-at=2024-07-09T11:29:46.835Z', + '--items=content=one,two,three,id=12345,type=file', '--prompt', 'What is the status of this document?', '--token=test',