Skip to content

Commit

Permalink
Refactor index.ts by extracting parseArgs; fix typo in assistant word
Browse files Browse the repository at this point in the history
  • Loading branch information
synaptiko committed Apr 8, 2023
1 parent e93c012 commit 6c20490
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 89 deletions.
6 changes: 3 additions & 3 deletions src/ConversationPersistance.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const initialMessagesPath = '.cli-gpt.initial.md';
const conversationPath = '.cli-gpt.conversation.md';

export type Role = 'user' | 'assistent' | 'system';
export type Role = 'user' | 'assistant' | 'system';
export type Message = { role: Role; content: string };

function readFile(path: string): string | undefined {
Expand All @@ -25,8 +25,8 @@ function parseMessages(fileContent = ''): Message[] {
case '# system:':
newRole = 'system';
break;
case '# assistent:':
newRole = 'assistent';
case '# assistant:':
newRole = 'assistant';
break;
case '# user:':
newRole = 'user';
Expand Down
99 changes: 15 additions & 84 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { writeText as copyToClipboard } from 'copy_paste/mod.ts';
import { prompt } from './prompt.ts';
import { ChatCompletion } from './ChatCompletion.ts';
import { printHelp } from './printHelp.ts';
import { ConversationPersistance, Role } from './ConversationPersistance.ts';
import { ConversationPersistance } from './ConversationPersistance.ts';
import { parseArgs } from './parseArgs.ts';

// TODO: finish the README
// TODO: add ability to set the other model params with env vars
Expand All @@ -18,84 +19,14 @@ if (env.OPENAI_API_KEY === undefined) {
Deno.exit(1);
}

const args = [...Deno.args];
let argsRead = false;
let role: Role = 'user';
let multiline = false;
let readFiles;
let affectInitialMessages = false;
let oneShot = false;
let copyResponse = false;
let reset = false;
let help = false;

while (!argsRead) {
// TODO: add validations
switch (args[0]) {
case '--user':
case '-u':
args.shift();
role = 'user';
break;
case '--assistent':
case '-a':
args.shift();
role = 'assistent';
break;
case '--system':
case '-s':
args.shift();
role = 'system';
break;
case '--multiline':
case '-m':
args.shift();
multiline = true;
break;
case '--read':
case '-r':
args.shift();
readFiles = [...args];
args.splice(0, args.length);
break;
case '--initial':
case '-i':
args.shift();
affectInitialMessages = true;
break;
case '--one-shot':
case '-o':
args.shift();
oneShot = true;
break;
case '--copy':
case '-c':
args.shift();
copyResponse = true;
break;
case '--reset':
case '-e':
reset = true;
argsRead = true;
break;
case '--help':
case '-h':
help = true;
argsRead = true;
break;
}

if (!argsRead) {
argsRead = args.length === 0 || !args[0].startsWith('-');
}
}

const conversationPersistance = new ConversationPersistance();
const { flags, role, readFiles, prompt: promptFromArgs } = parseArgs();
const { affectInitialMessages } = flags;

if (help) {
if (flags.help) {
printHelp();
} else if (reset) {
conversationPersistance.reset(affectInitialMessages);
} else if (flags.reset) {
conversationPersistance.reset(flags.affectInitialMessages);
} else {
let content;

Expand All @@ -109,14 +40,14 @@ if (help) {
console.log(content);
}

if (multiline || args.length === 0) {
content = (content ?? '') + await prompt(multiline);
if (flags.multiline || promptFromArgs === undefined) {
content = (content ?? '') + await prompt(flags.multiline);
console.log('\nResponse:');
} else {
content = args.join(' ');
content = promptFromArgs;
}

if (!oneShot) {
if (!flags.oneShot) {
conversationPersistance.append({
role,
content,
Expand All @@ -130,7 +61,7 @@ if (help) {
const write = (chunk: string) => Deno.stdout.write(encoder.encode(chunk));
const responseContent = [];

if (oneShot) {
if (flags.oneShot) {
chatCompletion.setMessages([{ role: 'user', content }]);
} else {
chatCompletion.setMessages(conversationPersistance.getMessages());
Expand All @@ -143,13 +74,13 @@ if (help) {
}
write('\n');

if (copyResponse) {
if (flags.copyResponse) {
copyToClipboard(responseContent.join(''));
}

if (!oneShot) {
if (!flags.oneShot) {
conversationPersistance.append({
role: 'assistent',
role: 'assistant',
content: responseContent.join(''),
affectInitialMessages,
});
Expand Down
102 changes: 102 additions & 0 deletions src/parseArgs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { Role } from './ConversationPersistance.ts';

export type Params = {
flags: Flags;
role: Role;
readFiles: string[] | undefined;
prompt: string | undefined;
};

export type Flags = {
multiline: boolean;
affectInitialMessages: boolean;
oneShot: boolean;
copyResponse: boolean;
reset: boolean;
help: boolean;
};

export function parseArgs(): Params {
const args = [...Deno.args];
let argsRead = false;
const params: Params = {
role: 'user' as Role,
readFiles: undefined,
prompt: undefined,
flags: {
multiline: false,
affectInitialMessages: false,
oneShot: false,
copyResponse: false,
reset: false,
help: false
}
};

while (!argsRead) {
// TODO: add validations
switch (args[0]) {
case '--user':
case '-u':
args.shift();
params.role = 'user';
break;
case '--assistant':
case '-a':
args.shift();
params.role = 'assistant';
break;
case '--system':
case '-s':
args.shift();
params.role = 'system';
break;
case '--multiline':
case '-m':
args.shift();
params.flags.multiline = true;
break;
case '--read':
case '-r':
args.shift();
params.readFiles = [...args];
args.splice(0, args.length);
break;
case '--initial':
case '-i':
args.shift();
params.flags.affectInitialMessages = true;
break;
case '--one-shot':
case '-o':
args.shift();
params.flags.oneShot = true;
break;
case '--copy':
case '-c':
args.shift();
params.flags.copyResponse = true;
break;
case '--reset':
case '-e':
params.flags.reset = true;
argsRead = true;
break;
case '--help':
case '-h':
params.flags.help = true;
argsRead = true;
break;
}

if (!argsRead) {
argsRead = args.length === 0 || !args[0].startsWith('-');
}
}

if (args.length !== 0) {
params.prompt = args.join(' ');
}

return params;
}
4 changes: 2 additions & 2 deletions src/printHelp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Usage: ${binName} [options] [prompt]
Options:
--user, -u Set the role for the message as 'user'.
--assistent, -a Set the role for the message as 'assistent'.
--assistant, -a Set the role for the message as 'assistant'.
--system, -s Set the role for the message as 'system'.
--multiline, -m Enable multiline input.
--read, -r [file(s)] Read file(s) and use their content in the prompt.
Expand All @@ -28,7 +28,7 @@ Options:
Input:
prompt Text content to send to the API (in case or 'user' role)
or to add to the conversation
(in case of 'assistent' or 'system' roles).
(in case of 'assistant' or 'system' roles).
Tip: Wrap the prompt in quotes to avoid shell expansion.
When no prompt is provided, the user will be prompted to enter it over standard input.
Expand Down

0 comments on commit 6c20490

Please sign in to comment.