Skip to content

Commit

Permalink
process initial message
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjastrzebski committed Dec 6, 2024
1 parent 4c96767 commit 2dee726
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 34 deletions.
72 changes: 47 additions & 25 deletions src/commands/chat/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { CommandModule } from 'yargs';
import { Message } from '@callstack/byorg-core';
import { parseConfigFile } from '../../config-file.js';
import {
clearCurrentLine,
output,
outputAssistant,
outputAssistantPartial,
outputError,
outputVerbose,

Check failure on line 10 in src/commands/chat/index.tsx

View workflow job for this annotation

GitHub Actions / Lint and Typecheck

'outputVerbose' is defined but never used
readUserInput,
setInterruptHandler,
setVerbose,
Expand Down Expand Up @@ -41,8 +43,12 @@ async function run(initialPrompt: string, options: PromptOptions) {
shouldExit = true;
});

const { app, messages, stream, verbose } = createState(options, configFile, initialPrompt);
setVerbose(verbose);
const { app, messages, stream } = createState(options, configFile, initialPrompt);

if (messages.length > 0) {
messages.forEach((m) => outputMessage(m));
await processMessages(messages, { app, stream });
}

while (!shouldExit) {
const userMessage = await readUserInput();
Expand All @@ -51,35 +57,51 @@ async function run(initialPrompt: string, options: PromptOptions) {
}

messages.push({ role: 'user', content: userMessage });
await processMessages(messages, { app, stream });
}
} catch (error) {
outputError(error);
process.exit(1);
}
}

lastMessage = '';
if (stream) {
outputAssistantPartial('ai: ');
}
type ProcessMessagesOptions = {
app: App;
stream: boolean;
};

const { response } = await app.processMessages(messages, {
onPartialResponse: stream ? onPartialUpdate : undefined,
});
async function processMessages(messages: Message[], { app, stream }: ProcessMessagesOptions) {
lastMessage = '';
if (stream) {
outputAssistantPartial('ai: ');
}

if (stream) {
outputAssistantPartial('\n');
}
const { response } = await app.processMessages(messages, {
onPartialResponse: stream ? onPartialUpdate : undefined,
});

// With streaming, assistant response is already printed
if (response.role === 'assistant') {
messages.push({ role: 'assistant', content: response.content });
if (stream) {
outputAssistantPartial('\n');
}

if (!stream) {
outputAssistant(response.content);
}
}
// With streaming, assistant response is already printed
if (response.role === 'assistant') {
messages.push({ role: 'assistant', content: response.content });

if (response.role === 'system') {
output(response.content);
}
if (!stream) {
outputAssistant(response.content);
}
} catch (error) {
outputError(error);
process.exit(1);
}

if (response.role === 'system') {
output(response.content);
}
}

function outputMessage(message: Message) {
if (message.role === 'user') {
output(`user: ${message.content}`);
} else {
outputAssistant(`ai: ${message.content}`);
}
}
4 changes: 3 additions & 1 deletion src/commands/chat/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { createApp, type Message } from '@callstack/byorg-core';
import { type ConfigFile } from '../../config-file.js';
import { DEFAULT_SYSTEM_PROMPT } from '../../default-config.js';
import type { ResponseStyle } from '../../engine/providers/config.js';
import { output, outputVerbose, outputWarning } from '../../output/index.js';
import type { PromptOptions } from './prompt-options.js';
import { getDefaultProvider, resolveProviderFromOption } from './providers.js';
import { filterOutApiKey, handleInputFile } from './utils.js';
import { output, outputVerbose, outputWarning } from '../../output/index.js';

export let messages: Message[] = [];

Expand Down Expand Up @@ -56,6 +56,8 @@ export function createState(options: PromptOptions, configFile: ConfigFile, init
}
}

output('');

if (initialPrompt) {
messages.push({ role: 'user', content: initialPrompt });
}
Expand Down
16 changes: 8 additions & 8 deletions src/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ export function outputError(error: unknown, ...args: unknown[]) {
console.error(chalk.red(`ERROR: ${message}`, ...args));
}

export function outputAssistant(text: string) {
console.log(chalk.hex(colors.assistant)(text));
export function outputAssistant(text: string, ...args: unknown[]) {
console.log(chalk.hex(colors.assistant)(text, ...args));
}

export function outputAssistantPartial(text: string) {
process.stdout.write(chalk.hex(colors.assistant)(text));
export function outputAssistantPartial(text: string, ...args: unknown[]) {
process.stdout.write(chalk.hex(colors.assistant)(text, ...args));
}

export function output(text: string) {
console.log(chalk.hex(colors.info)(text));
export function output(text: string, ...args: unknown[]) {
console.log(text, ...args);
}

export function outputVerbose(text: string) {
export function outputVerbose(message: string, ...args: unknown[]) {
if (showVerbose) {
console.log(chalk.dim(text));
console.log(chalk.dim(message, ...args));
}
}

Expand Down

0 comments on commit 2dee726

Please sign in to comment.