mirror of
https://github.com/QwenLM/qwen-code.git
synced 2026-01-03 23:49:14 +00:00
68 lines
1.8 KiB
TypeScript
68 lines
1.8 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2025 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
import { copyToClipboard } from '../utils/commandUtils.js';
|
|
import type { SlashCommand, SlashCommandActionReturn } from './types.js';
|
|
import { CommandKind } from './types.js';
|
|
import { t } from '../../i18n/index.js';
|
|
|
|
export const copyCommand: SlashCommand = {
|
|
name: 'copy',
|
|
get description() {
|
|
return t('Copy the last result or code snippet to clipboard');
|
|
},
|
|
kind: CommandKind.BUILT_IN,
|
|
action: async (context, _args): Promise<SlashCommandActionReturn | void> => {
|
|
const chat = await context.services.config?.getGeminiClient()?.getChat();
|
|
const history = chat?.getHistory();
|
|
|
|
// Get the last message from the AI (model role)
|
|
const lastAiMessage = history
|
|
? history.filter((item) => item.role === 'model').pop()
|
|
: undefined;
|
|
|
|
if (!lastAiMessage) {
|
|
return {
|
|
type: 'message',
|
|
messageType: 'info',
|
|
content: 'No output in history',
|
|
};
|
|
}
|
|
// Extract text from the parts
|
|
const lastAiOutput = lastAiMessage.parts
|
|
?.filter((part) => part.text)
|
|
.map((part) => part.text)
|
|
.join('');
|
|
|
|
if (lastAiOutput) {
|
|
try {
|
|
await copyToClipboard(lastAiOutput);
|
|
|
|
return {
|
|
type: 'message',
|
|
messageType: 'info',
|
|
content: 'Last output copied to the clipboard',
|
|
};
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : String(error);
|
|
console.debug(message);
|
|
|
|
return {
|
|
type: 'message',
|
|
messageType: 'error',
|
|
content: `Failed to copy to the clipboard. ${message}`,
|
|
};
|
|
}
|
|
} else {
|
|
return {
|
|
type: 'message',
|
|
messageType: 'info',
|
|
content: 'Last AI output contains no text to copy.',
|
|
};
|
|
}
|
|
},
|
|
};
|