Skip to content
2 changes: 1 addition & 1 deletion docs/users/features/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ Commands for obtaining information and performing system settings.
| `/insight` | Generate programming insights from chat history | `/insight` |
| `/setup-github` | Set up GitHub Actions | `/setup-github` |
| `/bug` | Submit issue about Qwen Code | `/bug Button click unresponsive` |
| `/copy` | Copy to clipboard: reply (Nth-last), code (by lang), LaTeX, or Mermaid | `/copy`, `/copy 2`, `/copy python`, `/copy latex`, `/copy mermaid` |
| `/copy` | Copy to clipboard: reply (Nth-last with text), code (by lang), LaTeX, or Mermaid | `/copy`, `/copy 2`, `/copy python`, `/copy latex`, `/copy mermaid` |
| `/quit` | Exit Qwen Code immediately | `/quit` or `/exit` |

> [!warning]
Expand Down
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
4 changes: 2 additions & 2 deletions packages/cli/src/i18n/locales/ca.js
Original file line number Diff line number Diff line change
Expand Up @@ -2574,8 +2574,8 @@ export default {
'Mou aquesta sessió a un directori de treball nou',
'Fast context compression without AI. Strips old tool outputs and thinking parts.':
"Compressió ràpida del context sense IA. Elimina sortides d'eines antigues i parts de pensament.",
'Copy to clipboard: reply, code (by lang), LaTeX, or Mermaid. N = Nth-latest message, index = block number':
'Copia al porta-retalls: resposta, codi (per llenguatge), LaTeX o Mermaid. N = N-èsim missatge més recent, índex = número de bloc',
'Copy to clipboard: reply, code (by lang), LaTeX, or Mermaid. N = Nth-latest AI message with text, index = block number':
'Copia al porta-retalls: resposta, codi (per llenguatge), LaTeX o Mermaid. N = N-èsim missatge de la IA amb text més recent, índex = número de bloc',
'Show working-tree change stats versus HEAD':
"Mostra les estadístiques de canvis de l'arbre de treball respecte a HEAD",
'Could not determine current working directory.':
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/i18n/locales/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,8 @@ export default {
'open full Qwen Code documentation in your browser',
'Configuration not available.': 'Configuration not available.',
'Connect an LLM provider': 'Connect an LLM provider',
'Copy to clipboard: reply, code (by lang), LaTeX, or Mermaid. N = Nth-latest message, index = block number':
'Copy to clipboard: reply, code (by lang), LaTeX, or Mermaid. N = Nth-latest message, index = block number',
'Copy to clipboard: reply, code (by lang), LaTeX, or Mermaid. N = Nth-latest AI message with text, index = block number':
Comment thread
burrows99 marked this conversation as resolved.
Resolved
'Copy to clipboard: reply, code (by lang), LaTeX, or Mermaid. N = Nth-latest AI message with text, index = block number',
'Show working-tree change stats versus HEAD':
'Show working-tree change stats versus HEAD',
'Could not determine current working directory.':
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/i18n/locales/ru.js
Original file line number Diff line number Diff line change
Expand Up @@ -2693,8 +2693,8 @@ export default {
'Дождитесь завершения фоновых агентов или остановите их в /tasks, прежде чем менять каталог.',
'Fast context compression without AI. Strips old tool outputs and thinking parts.':
'Быстрое сжатие контекста без ИИ. Удаляет старые выводы инструментов и фрагменты рассуждений.',
'Copy to clipboard: reply, code (by lang), LaTeX, or Mermaid. N = Nth-latest message, index = block number':
'Копировать в буфер: ответ, код (по языку), LaTeX или Mermaid. N — N-е с конца сообщение, index — номер блока',
'Copy to clipboard: reply, code (by lang), LaTeX, or Mermaid. N = Nth-latest AI message with text, index = block number':
'Копировать в буфер: ответ, код (по языку), LaTeX или Mermaid. N — N-е с конца сообщение ИИ с текстом, index — номер блока',
'Show working-tree change stats versus HEAD':
'Показать статистику изменений рабочего дерева относительно HEAD',
'Could not determine current working directory.':
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/i18n/locales/zh-TW.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,8 @@ export default {
'在瀏覽器中打開完整的 Qwen Code 文檔',
'Configuration not available.': '配置不可用',
'Connect an LLM provider': '連接 LLM 提供商',
'Copy to clipboard: reply, code (by lang), LaTeX, or Mermaid. N = Nth-latest message, index = block number':
'複製到剪貼簿:AI 回應、程式碼區塊(可依語言篩選)、LaTeX 或 Mermaid。N 為倒數第 N 則訊息,index 為程式碼區塊序號',
'Copy to clipboard: reply, code (by lang), LaTeX, or Mermaid. N = Nth-latest AI message with text, index = block number':
'複製到剪貼簿:AI 回應、程式碼區塊(可依語言篩選)、LaTeX 或 Mermaid。N 為倒數第 N 則含文字的 AI 訊息,index 為程式碼區塊序號',
'Show working-tree change stats versus HEAD':
'顯示工作區相對 HEAD 的變更統計',
'Could not determine current working directory.': '無法確定當前工作目錄。',
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/i18n/locales/zh.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,8 @@ export default {
'在浏览器中打开完整的 Qwen Code 文档',
'Configuration not available.': '配置不可用',
'Connect an LLM provider': '连接 LLM 提供商',
'Copy to clipboard: reply, code (by lang), LaTeX, or Mermaid. N = Nth-latest message, index = block number':
'复制到剪贴板:AI 回复、代码块(可按语言筛选)、LaTeX 或 Mermaid。N 为倒数第 N 条消息,index 为代码块序号',
'Copy to clipboard: reply, code (by lang), LaTeX, or Mermaid. N = Nth-latest AI message with text, index = block number':
'复制到剪贴板:AI 回复、代码块(可按语言筛选)、LaTeX 或 Mermaid。N 为倒数第 N 条含文本的 AI 消息,index 为代码块序号',
'Show working-tree change stats versus HEAD':
'显示工作区相对 HEAD 的变更统计',
'Could not determine current working directory.': '无法确定当前工作目录。',
Expand Down
213 changes: 209 additions & 4 deletions packages/cli/src/ui/commands/copyCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,92 @@ 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 with text in this session.',
});
// `/copy 1` is documented as equivalent to `/copy`.
for (const args of ['', '1']) {
expect(await copyCommand.action(mockContext, args)).toEqual({
type: 'message',
messageType: 'info',
content: 'Last AI output contains no text to copy.',
});
}
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 with text 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 @@ -1078,7 +1164,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 with text 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 @@ -1094,11 +1230,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 with text 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 with text 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 Expand Up @@ -1284,7 +1489,7 @@ math then literal: $x^2\$$`,
expect(result).toEqual({
type: 'message',
messageType: 'info',
content: 'Only 1 AI message in this session.',
content: 'Only 1 AI message with text in this session.',
});
expect(mockCopyToClipboard).not.toHaveBeenCalled();
});
Expand All @@ -1303,7 +1508,7 @@ math then literal: $x^2\$$`,
expect(result).toEqual({
type: 'message',
messageType: 'info',
content: 'Only 3 AI messages in this session.',
content: 'Only 3 AI messages with text in this session.',
});
expect(mockCopyToClipboard).not.toHaveBeenCalled();
});
Expand Down
Loading