Skip to content

Commit

Permalink
Update command
Browse files Browse the repository at this point in the history
  • Loading branch information
congminh1254 committed Jul 31, 2024
1 parent 7febb24 commit 755a591
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 31 deletions.
7 changes: 4 additions & 3 deletions docs/ai.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)_
2 changes: 1 addition & 1 deletion src/commands/ai/ask.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
6 changes: 3 additions & 3 deletions src/commands/ai/text-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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];
Expand All @@ -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];
Expand Down
50 changes: 32 additions & 18 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions test/commands/ai.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down

0 comments on commit 755a591

Please sign in to comment.