Skip to content

Commit

Permalink
Update test
Browse files Browse the repository at this point in the history
  • Loading branch information
congminh1254 committed Jul 29, 2024
1 parent bb70555 commit b9ea367
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 72 deletions.
30 changes: 11 additions & 19 deletions src/commands/ai/ask.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const BoxCommand = require('../../box-command');
const { flags } = require('@oclif/command');
const utils = require('../../util');

class AiAskCommand extends BoxCommand {
async run() {
Expand Down Expand Up @@ -52,25 +53,16 @@ AiAskCommand.flags = {
id: '',
type: 'file'
};

for (const part of input.split(',')) {
const [
key,
value,
] = part.split('=');

switch (key) {
case 'id':
item.id = value;
break;
case 'type':
item.type = value;
break;
case 'content':
item.content = value;
break;
default:
throw new Error(`Invalid item key ${key}`);
const obj = utils.parseStringToObject(input);
for (const key in obj) {
if (key === 'id') {
item.id = obj[key];
} else if (key === 'type') {
item.type = obj[key];
} else if (key === 'content') {
item.content = obj[key];
} else {
throw new Error(`Invalid item key ${key}`);
}
}

Expand Down
67 changes: 26 additions & 41 deletions src/commands/ai/text-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

const BoxCommand = require('../../box-command');
const { flags } = require('@oclif/command');
const utils = require('../../util');

class AiTextGenCommand extends BoxCommand {
async run() {
const { flags, args } = this.parse(AiTextGenCommand);
let options = {};

if (flags.dialogue_history) {
options.dialogueHistory = flags.dialogue_history;
if (flags['dialogue-history']) {
options.dialogueHistory = flags['dialogue-history'];
}
options.prompt = flags.prompt;
options.items = flags.items;
Expand All @@ -24,36 +25,28 @@ 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" --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 = {
...BoxCommand.flags,
dialogue_history: flags.string({

'dialogue-history': flags.string({
required: false,
description: 'The history of prompts and answers previously passed to the LLM.',
multiple: true,
parse(input) {
const record = {};
for (const part of input.split(',')) {
const [
key,
value,
] = part.split('=');

switch (key) {
case 'prompt':
record.prompt = value;
break;
case 'answer':
record.answer = value;
break;
case 'created_at':
case 'createdAt':
record.created_at = value;
break;
default:
throw new Error(`Invalid record key: ${key}`);
const obj = utils.parseStringToObject(input);
for (const key in obj) {
if (key === 'prompt') {
record.prompt = obj[key];
} else if (key === 'answer') {
record.answer = obj[key];
} else if (key === 'created-at') {
record.created_at = BoxCommand.normalizeDateString(obj[key]);
} else {
throw new Error(`Invalid record key ${key}`);
}
}

Expand All @@ -69,24 +62,16 @@ AiTextGenCommand.flags = {
id: '',
type: 'file'
};
for (const part of input.split(',')) {
const [
key,
value,
] = part.split('=');

switch (key) {
case 'id':
item.id = value;
break;
case 'type':
item.type = value;
break;
case 'content':
item.content = value;
break;
default:
throw new Error(`Invalid item key ${key}`);
const obj = utils.parseStringToObject(input);
for (const key in obj) {
if (key === 'id') {
item.id = obj[key];
} else if (key === 'type') {
item.type = obj[key];
} else if (key === 'content') {
item.content = obj[key];
} else {
throw new Error(`Invalid item key ${key}`);
}
}
return item;
Expand Down
25 changes: 25 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,30 @@ function parseMetadataString(input) {
return op;
}

/**
* Parse a string into an JSON object
*
* @param {string} str The string to parse
* @returns {Object} The parsed object
*/
function parseStringToObject(str) {
const obj = {};
const regex = /([\w-]+)=((?:"[^"]*")|[^,]*)/g; // Regular expression to match key=value pairs, including keys with dashes and quoted values

Check failure on line 195 in src/util.js

View workflow job for this annotation

GitHub Actions / Node 14 on ubuntu-latest

Use the 'u' flag

Check failure on line 195 in src/util.js

View workflow job for this annotation

GitHub Actions / Node 16 on ubuntu-latest

Use the 'u' flag

Check failure on line 195 in src/util.js

View workflow job for this annotation

GitHub Actions / Node 18 on ubuntu-latest

Use the 'u' flag

Check failure on line 195 in src/util.js

View workflow job for this annotation

GitHub Actions / Node 14 on windows-latest

Use the 'u' flag

Check failure on line 195 in src/util.js

View workflow job for this annotation

GitHub Actions / Node 16 on windows-latest

Use the 'u' flag

Check failure on line 195 in src/util.js

View workflow job for this annotation

GitHub Actions / Node 18 on windows-latest

Use the 'u' flag

Check failure on line 195 in src/util.js

View workflow job for this annotation

GitHub Actions / Node 16 on macos-latest

Use the 'u' flag

Check failure on line 195 in src/util.js

View workflow job for this annotation

GitHub Actions / Node 18 on macos-latest

Use the 'u' flag
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;
}

/**
* Check if directory exists and creates it if shouldCreate flag was passed.
*
Expand Down Expand Up @@ -343,6 +367,7 @@ module.exports = {
parseMetadataOp(value) {
return parseMetadataString(value);
},
parseStringToObject,
checkDir,
readFileAsync,
writeFileAsync,
Expand Down
25 changes: 13 additions & 12 deletions test/commands/ai.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('AI', () => {
{
id: '12345',
type: 'file',
content: 'one,two,three',
},
],
mode: 'single_item_qa',
Expand All @@ -34,7 +35,7 @@ describe('AI', () => {
.stdout()
.command([
'ai:ask',
'--items=id=12345,type=file',
'--items=content="one,two,three",id=12345,type=file',
'--prompt',
'What is the status of this document?',
'--mode',
Expand All @@ -58,7 +59,7 @@ describe('AI', () => {
.stdout()
.command([
'ai:ask',
'--items=id=12345,type=file',
'--items=content="one,two,three",id=12345,type=file',
'--prompt',
'What is the status of this document?',
'--mode',
Expand All @@ -76,12 +77,12 @@ describe('AI', () => {
describe('ai:text-gen', () => {
const expectedRequestBody = {
prompt: 'What is the status of this document?',
items: [{ id: '12345', type: 'file' }],
items: [{ id: '12345', type: 'file', content: 'one,two,three' }],
dialogue_history: [
{
prompt: 'What is the status of this document?',
answer: 'It is in review',
created_at: '2024-07-09T11:29:46.835Z'
prompt: 'What is the status of this document, signatures?',
answer: 'It is in review, waiting for signatures.',
created_at: '2024-07-09T11:29:46+00:00'
},
],
};
Expand All @@ -102,9 +103,9 @@ describe('AI', () => {
.stdout()
.command([
'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',
'--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?',
'--json',
Expand All @@ -126,9 +127,9 @@ describe('AI', () => {
.stdout()
.command([
'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',
'--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?',
'--token=test',
Expand Down

0 comments on commit b9ea367

Please sign in to comment.