diff --git a/README.md b/README.md index 6e85750..088af64 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,7 @@ The following meta commands have special behaviors to manipulate the context or - `:branch `: Create a new branch in the conversation. - `:clear`: Clear all messages in the conversation. - `:continue`: Re-prompt the model without a user message. +- `:copy`: Copy the last agent response to clipboard. - `:cost`: Show token counts of calls to remote LLM APIs in this conversation. - `:dump`: Dump all context file contents to disk. - `:exit`: Exit the conversation. diff --git a/src/chat/commands.ts b/src/chat/commands.ts index 0408c90..b9cf515 100644 --- a/src/chat/commands.ts +++ b/src/chat/commands.ts @@ -4,6 +4,7 @@ import { CommandDescription } from './command' import { branchCommand } from './commands/branch' import { clearCommand } from './commands/clear' import { continueCommand } from './commands/continue' +import { copyCommand } from './commands/copy' import { costCommand } from './commands/cost' import { dumpCommand } from './commands/dump' import { exitCommand } from './commands/exit' @@ -34,6 +35,7 @@ export const commands: CommandDescription[] = [ branchCommand, clearCommand, continueCommand, + copyCommand, costCommand, dumpCommand, exitCommand, diff --git a/src/chat/commands/copy.ts b/src/chat/commands/copy.ts new file mode 100644 index 0000000..1bd3ead --- /dev/null +++ b/src/chat/commands/copy.ts @@ -0,0 +1,41 @@ +import chalk from 'chalk' +import * as ncp from 'copy-paste' +import { CommandDescription } from '../command' +import { ChatContext } from '../context' + +export const copyCommand: CommandDescription = { + prefix: ':copy', + description: 'Copy the last agent response to clipboard.', + handler: handleCopy, +} + +async function handleCopy(context: ChatContext, args: string) { + if (args.trim() !== '') { + console.log(chalk.red.bold('Unexpected arguments supplied to :copy.')) + console.log() + return + } + + const messages = context.provider.conversationManager.visibleMessages() + const lastUserMessageIndex = messages.findLastIndex(message => message.role === 'user') + const agentMessages = lastUserMessageIndex === -1 ? [] : messages.slice(lastUserMessageIndex + 1) + + if (agentMessages.length === 0) { + console.log('Nothing to copy.') + console.log() + return + } + + await new Promise((resolve, reject) => { + ncp.copy(JSON.stringify(agentMessages, null, 2), err => { + if (err) { + reject(err) + } else { + resolve() + } + }) + }) + + console.log('Last agent response copied to clipboard\n') + console.log() +}