Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ The following meta commands have special behaviors to manipulate the context or
- `:branch <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.
Expand Down
2 changes: 2 additions & 0 deletions src/chat/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -34,6 +35,7 @@ export const commands: CommandDescription[] = [
branchCommand,
clearCommand,
continueCommand,
copyCommand,
costCommand,
dumpCommand,
exitCommand,
Expand Down
41 changes: 41 additions & 0 deletions src/chat/commands/copy.ts
Original file line number Diff line number Diff line change
@@ -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<void>((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()
}