mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-19 09:33:53 +00:00
Add /compress command to force a compression of the context (#986)
Related to https://b.corp.google.com/issues/423605555 - I figured this might be a simpler solution to start with, while still also being useful on its own even if we do implement that.
This commit is contained in:
@@ -9,6 +9,7 @@ import { type PartListUnion } from '@google/genai';
|
||||
import open from 'open';
|
||||
import process from 'node:process';
|
||||
import { UseHistoryManagerReturn } from './useHistoryManager.js';
|
||||
import { useStateAndRef } from './useStateAndRef.js';
|
||||
import {
|
||||
Config,
|
||||
GitService,
|
||||
@@ -80,6 +81,13 @@ export const useSlashCommandProcessor = (
|
||||
return new GitService(config.getProjectRoot());
|
||||
}, [config]);
|
||||
|
||||
const pendingHistoryItems: HistoryItemWithoutId[] = [];
|
||||
const [pendingCompressionItemRef, setPendingCompressionItem] =
|
||||
useStateAndRef<HistoryItemWithoutId | null>(null);
|
||||
if (pendingCompressionItemRef.current != null) {
|
||||
pendingHistoryItems.push(pendingCompressionItemRef.current);
|
||||
}
|
||||
|
||||
const addMessage = useCallback(
|
||||
(message: Message) => {
|
||||
// Convert Message to HistoryItemWithoutId
|
||||
@@ -105,6 +113,11 @@ export const useSlashCommandProcessor = (
|
||||
stats: message.stats,
|
||||
duration: message.duration,
|
||||
};
|
||||
} else if (message.type === MessageType.COMPRESSION) {
|
||||
historyItemContent = {
|
||||
type: 'compression',
|
||||
compression: message.compression,
|
||||
};
|
||||
} else {
|
||||
historyItemContent = {
|
||||
type: message.type as
|
||||
@@ -641,6 +654,57 @@ Add any other context about the problem here.
|
||||
}, 100);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'compress',
|
||||
altName: 'summarize',
|
||||
description: 'Compresses the context by replacing it with a summary.',
|
||||
action: async (_mainCommand, _subCommand, _args) => {
|
||||
if (pendingCompressionItemRef.current !== null) {
|
||||
addMessage({
|
||||
type: MessageType.ERROR,
|
||||
content:
|
||||
'Already compressing, wait for previous request to complete',
|
||||
timestamp: new Date(),
|
||||
});
|
||||
return;
|
||||
}
|
||||
setPendingCompressionItem({
|
||||
type: MessageType.COMPRESSION,
|
||||
compression: {
|
||||
isPending: true,
|
||||
},
|
||||
});
|
||||
try {
|
||||
const compressed = await config!
|
||||
.getGeminiClient()!
|
||||
.tryCompressChat(true);
|
||||
if (compressed) {
|
||||
addMessage({
|
||||
type: MessageType.COMPRESSION,
|
||||
compression: {
|
||||
isPending: false,
|
||||
originalTokenCount: compressed.originalTokenCount,
|
||||
newTokenCount: compressed.newTokenCount,
|
||||
},
|
||||
timestamp: new Date(),
|
||||
});
|
||||
} else {
|
||||
addMessage({
|
||||
type: MessageType.ERROR,
|
||||
content: 'Failed to compress chat history.',
|
||||
timestamp: new Date(),
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
addMessage({
|
||||
type: MessageType.ERROR,
|
||||
content: `Failed to compress chat history: ${e instanceof Error ? e.message : String(e)}`,
|
||||
timestamp: new Date(),
|
||||
});
|
||||
}
|
||||
setPendingCompressionItem(null);
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
if (config?.getCheckpointEnabled()) {
|
||||
@@ -767,6 +831,8 @@ Add any other context about the problem here.
|
||||
loadHistory,
|
||||
addItem,
|
||||
setQuittingMessages,
|
||||
pendingCompressionItemRef,
|
||||
setPendingCompressionItem,
|
||||
]);
|
||||
|
||||
const handleSlashCommand = useCallback(
|
||||
@@ -830,5 +896,5 @@ Add any other context about the problem here.
|
||||
[addItem, slashCommands, addMessage],
|
||||
);
|
||||
|
||||
return { handleSlashCommand, slashCommands };
|
||||
return { handleSlashCommand, slashCommands, pendingHistoryItems };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user