File size: 1,833 Bytes
7a1ad33 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type { GitService } from '../services/gitService.js';
import type { CommandActionReturn } from './types.js';
import { type ToolCallData } from '../utils/checkpointUtils.js';
export async function* performRestore<
HistoryType = unknown,
ArgsType = unknown,
>(
toolCallData: ToolCallData<HistoryType, ArgsType>,
gitService: GitService | undefined,
): AsyncGenerator<CommandActionReturn<HistoryType>> {
if (toolCallData.history && toolCallData.clientHistory) {
yield {
type: 'load_history',
history: toolCallData.history,
clientHistory: toolCallData.clientHistory,
};
}
if (toolCallData.commitHash) {
if (!gitService) {
yield {
type: 'message',
messageType: 'error',
content:
'Git service is not available, cannot restore checkpoint. Please ensure you are in a git repository.',
};
return;
}
try {
await gitService.restoreProjectFromSnapshot(toolCallData.commitHash);
yield {
type: 'message',
messageType: 'info',
content: 'Restored project to the state before the tool call.',
};
} catch (e) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
const error = e as Error;
if (error.message.includes('unable to read tree')) {
yield {
type: 'message',
messageType: 'error',
content: `The commit hash '${toolCallData.commitHash}' associated with this checkpoint could not be found in your Git repository. This can happen if the repository has been re-cloned, reset, or if old commits have been garbage collected. This checkpoint cannot be restored.`,
};
return;
}
throw e;
}
}
}
|