Skip to content
8 changes: 6 additions & 2 deletions docs/users/features/markdown-rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,12 @@ substantive output is one or two turns back.
| `/copy 2 code python` | Copies the last `python` code block from the 2nd-last. |
| `/copy 3 latex` | Copies the last LaTeX block from the 3rd-last message. |

`/copy 1` is equivalent to `/copy`. If `N` exceeds the number of AI messages
in the session, `/copy` reports the actual count instead of copying anything.
`/copy 1` is equivalent to `/copy`. Only AI messages with text are counted: a
Comment thread
burrows99 marked this conversation as resolved.
Resolved
turn that only calls a tool, only thinks, or carries only whitespace is
skipped. A turn that writes text before calling a tool still counts, so
`/copy N` can land on that short text. If `N` exceeds the number of
AI messages in the session, `/copy` reports the actual count instead of
copying anything.
Without a leading integer, sub-selectors such as `/copy code python 2` keep
their existing meaning (the 2nd `python` block in the last message).

Expand Down
201 changes: 199 additions & 2 deletions packages/cli/src/ui/commands/copyCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,84 @@ math then literal: $x^2\$$`,
expect(mockCopyToClipboard).not.toHaveBeenCalled();
});

it('should report no text to copy when the only AI turn calls a tool', async () => {
if (!copyCommand.action) throw new Error('Command has no action');

mockGetHistoryShallow.mockReturnValue([
{ role: 'user', parts: [{ text: 'question' }] },
{
role: 'model',
parts: [{ functionCall: { name: 'glob', args: { pattern: '*' } } }],
},
]);

expect(await copyCommand.action(mockContext, '')).toEqual({
type: 'message',
messageType: 'info',
content: 'Last AI output contains no text to copy.',
});
expect(mockCopyToClipboard).not.toHaveBeenCalled();
});

it('should validate N before the no-text check when no AI turn has text', async () => {
if (!copyCommand.action) throw new Error('Command has no action');

mockGetHistoryShallow.mockReturnValue([
{ role: 'user', parts: [{ text: 'question' }] },
{
role: 'model',
parts: [{ functionCall: { name: 'glob', args: {} } }],
},
]);

expect(await copyCommand.action(mockContext, '0')).toEqual({
type: 'message',
messageType: 'info',
content:
'Message index must be a positive integer (1 = last AI message).',
});
expect(await copyCommand.action(mockContext, '3')).toEqual({
type: 'message',
messageType: 'info',
content: 'Only 0 AI messages in this session.',
});
expect(mockCopyToClipboard).not.toHaveBeenCalled();
});

it('should not count a whitespace-only model turn as an AI message', async () => {
if (!copyCommand.action) throw new Error('Command has no action');

mockGetHistoryShallow.mockReturnValue([
{ role: 'model', parts: [{ text: 'earlier reply' }] },
{ role: 'user', parts: [{ text: 'question' }] },
{
role: 'model',
parts: [
{ text: '\n' },
{ functionCall: { name: 'glob', args: { pattern: '*' } } },
],
},
{
role: 'user',
parts: [{ functionResponse: { name: 'glob', response: {} } }],
},
]);
mockCopyToClipboard.mockResolvedValue(undefined);

expect(await copyCommand.action(mockContext, '')).toEqual({
type: 'message',
messageType: 'info',
content: 'Last output copied to the clipboard',
});
expect(mockCopyToClipboard).toHaveBeenLastCalledWith('earlier reply');

expect(await copyCommand.action(mockContext, '2')).toEqual({
type: 'message',
messageType: 'info',
content: 'Only 1 AI message in this session.',
});
});

it('should copy the Nth-last AI message with /copy N', async () => {
if (!copyCommand.action) throw new Error('Command has no action');

Expand All @@ -940,7 +1018,57 @@ math then literal: $x^2\$$`,
});
});

it('should label the error with AI message N when /copy N has no text', async () => {
it('should not count tool-call-only model turns toward /copy N', async () => {
Comment thread
burrows99 marked this conversation as resolved.
Resolved
if (!copyCommand.action) throw new Error('Command has no action');

const history = [
{ role: 'user', parts: [{ text: 'first question' }] },
{
role: 'model',
parts: [{ text: 'previous reply\n\n```ts\nconst one = 1;\n```' }],
},
{ role: 'user', parts: [{ text: 'second question' }] },
{
role: 'model',
parts: [
{ text: 'planning', thought: true },
{ functionCall: { name: 'glob', args: { pattern: '*' } } },
],
},
{
role: 'user',
parts: [{ functionResponse: { name: 'glob', response: {} } }],
},
{ role: 'model', parts: [{ text: 'latest reply' }] },
];

mockGetHistoryShallow.mockReturnValue(history);
mockCopyToClipboard.mockResolvedValue(undefined);

expect(await copyCommand.action(mockContext, '2')).toEqual({
type: 'message',
messageType: 'info',
content: 'AI message 2 copied to the clipboard',
});
expect(mockCopyToClipboard).toHaveBeenLastCalledWith(
'previous reply\n\n```ts\nconst one = 1;\n```',
);

expect(await copyCommand.action(mockContext, '2 code')).toEqual({
type: 'message',
messageType: 'info',
content: 'Code block 1 copied to the clipboard',
});
expect(mockCopyToClipboard).toHaveBeenLastCalledWith('const one = 1;');

expect(await copyCommand.action(mockContext, '3')).toEqual({
type: 'message',
messageType: 'info',
content: 'Only 2 AI messages in this session.',
});
});

it('should skip AI messages without text when counting /copy N', async () => {
if (!copyCommand.action) throw new Error('Command has no action');

const history = [
Expand All @@ -956,11 +1084,80 @@ math then literal: $x^2\$$`,
expect(result).toEqual({
type: 'message',
messageType: 'info',
content: 'AI message 2 contains no text to copy.',
content: 'Only 1 AI message in this session.',
});
expect(mockCopyToClipboard).not.toHaveBeenCalled();
});

it('should count a model turn with preamble text and a tool call', async () => {
if (!copyCommand.action) throw new Error('Command has no action');

const history = [
{ role: 'model', parts: [{ text: 'older reply' }] },
{ role: 'user', parts: [{ text: 'question' }] },
{
role: 'model',
parts: [
{ text: 'Let me check.' },
{ functionCall: { name: 'glob', args: { pattern: '*' } } },
],
},
{
role: 'user',
parts: [{ functionResponse: { name: 'glob', response: {} } }],
},
{ role: 'model', parts: [{ text: 'final reply' }] },
];

mockGetHistoryShallow.mockReturnValue(history);
mockCopyToClipboard.mockResolvedValue(undefined);

expect(await copyCommand.action(mockContext, '2')).toEqual({
type: 'message',
messageType: 'info',
content: 'AI message 2 copied to the clipboard',
});
expect(mockCopyToClipboard).toHaveBeenLastCalledWith('Let me check.');

expect(await copyCommand.action(mockContext, '3')).toEqual({
type: 'message',
messageType: 'info',
content: 'AI message 3 copied to the clipboard',
});
expect(mockCopyToClipboard).toHaveBeenLastCalledWith('older reply');

expect(await copyCommand.action(mockContext, '4')).toEqual({
type: 'message',
messageType: 'info',
content: 'Only 3 AI messages in this session.',
});
});

it('should copy the last reply with text when the newest turn only called a tool', async () => {
if (!copyCommand.action) throw new Error('Command has no action');

// e.g. the turn was interrupted after the tool call, before any reply.
const history = [
{ role: 'user', parts: [{ text: 'question' }] },
{ role: 'model', parts: [{ text: 'earlier reply' }] },
{ role: 'user', parts: [{ text: 'follow-up' }] },
{
role: 'model',
parts: [{ functionCall: { name: 'glob', args: { pattern: '*' } } }],
},
];

mockGetHistoryShallow.mockReturnValue(history);
mockCopyToClipboard.mockResolvedValue(undefined);

expect(await copyCommand.action(mockContext, '')).toEqual({
type: 'message',
messageType: 'info',
content: 'Last output copied to the clipboard',
});
expect(mockCopyToClipboard).toHaveBeenLastCalledWith('earlier reply');
});

it('should label the error with AI message N when /copy N <selector> misses', async () => {
if (!copyCommand.action) throw new Error('Command has no action');

Expand Down
Loading
Loading