use pending history item for shell mode, update as output is received (#471)
This commit is contained in:
parent
01dbc61d1c
commit
00ab1905e0
|
@ -15,6 +15,7 @@ import type { exec as ExecType } from 'child_process'; // For typing the injecte
|
||||||
|
|
||||||
// Mocks
|
// Mocks
|
||||||
const mockAddItemToHistory = vi.fn();
|
const mockAddItemToHistory = vi.fn();
|
||||||
|
const mockSetPendingHistoryItem = vi.fn();
|
||||||
const mockOnExec = vi.fn(async (promise) => await promise);
|
const mockOnExec = vi.fn(async (promise) => await promise);
|
||||||
const mockOnDebugMessage = vi.fn();
|
const mockOnDebugMessage = vi.fn();
|
||||||
const mockGetTargetDir = vi.fn();
|
const mockGetTargetDir = vi.fn();
|
||||||
|
@ -94,6 +95,7 @@ describe('useShellCommandProcessor', () => {
|
||||||
renderHook(() =>
|
renderHook(() =>
|
||||||
useShellCommandProcessor(
|
useShellCommandProcessor(
|
||||||
mockAddItemToHistory,
|
mockAddItemToHistory,
|
||||||
|
mockSetPendingHistoryItem,
|
||||||
mockOnExec,
|
mockOnExec,
|
||||||
mockOnDebugMessage,
|
mockOnDebugMessage,
|
||||||
mockConfig,
|
mockConfig,
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { spawn } from 'child_process';
|
import { spawn } from 'child_process';
|
||||||
|
import type { HistoryItemWithoutId } from '../types.js';
|
||||||
import type { exec as ExecType } from 'child_process';
|
import type { exec as ExecType } from 'child_process';
|
||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
import { Config } from '@gemini-code/server';
|
import { Config } from '@gemini-code/server';
|
||||||
|
@ -21,6 +22,9 @@ import fs from 'fs';
|
||||||
*/
|
*/
|
||||||
export const useShellCommandProcessor = (
|
export const useShellCommandProcessor = (
|
||||||
addItemToHistory: UseHistoryManagerReturn['addItem'],
|
addItemToHistory: UseHistoryManagerReturn['addItem'],
|
||||||
|
setPendingHistoryItem: React.Dispatch<
|
||||||
|
React.SetStateAction<HistoryItemWithoutId | null>
|
||||||
|
>,
|
||||||
onExec: (command: Promise<void>) => void,
|
onExec: (command: Promise<void>) => void,
|
||||||
onDebugMessage: (message: string) => void,
|
onDebugMessage: (message: string) => void,
|
||||||
config: Config,
|
config: Config,
|
||||||
|
@ -115,18 +119,25 @@ export const useShellCommandProcessor = (
|
||||||
cwd: targetDir,
|
cwd: targetDir,
|
||||||
stdio: ['ignore', 'pipe', 'pipe'],
|
stdio: ['ignore', 'pipe', 'pipe'],
|
||||||
});
|
});
|
||||||
|
|
||||||
let output = '';
|
let output = '';
|
||||||
child.stdout.on('data', (data) => {
|
const handleOutput = (data: string) => {
|
||||||
output += data;
|
output += data;
|
||||||
});
|
setPendingHistoryItem({
|
||||||
child.stderr.on('data', (data) => {
|
type: 'info',
|
||||||
output += data;
|
text: output,
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
child.stdout.on('data', handleOutput);
|
||||||
|
child.stderr.on('data', handleOutput);
|
||||||
|
|
||||||
let error: Error | null = null;
|
let error: Error | null = null;
|
||||||
child.on('error', (err: Error) => {
|
child.on('error', (err: Error) => {
|
||||||
error = err;
|
error = err;
|
||||||
});
|
});
|
||||||
|
|
||||||
child.on('close', (code, signal) => {
|
child.on('close', (code, signal) => {
|
||||||
|
setPendingHistoryItem(null);
|
||||||
output = output.trim() || '(Command produced no output)';
|
output = output.trim() || '(Command produced no output)';
|
||||||
if (error) {
|
if (error) {
|
||||||
const text = `${error.message.replace(commandToExecute, rawQuery)}\n${output}`;
|
const text = `${error.message.replace(commandToExecute, rawQuery)}\n${output}`;
|
||||||
|
@ -169,7 +180,14 @@ export const useShellCommandProcessor = (
|
||||||
|
|
||||||
return true; // Command was initiated
|
return true; // Command was initiated
|
||||||
},
|
},
|
||||||
[config, onDebugMessage, addItemToHistory, onExec, executeCommand],
|
[
|
||||||
|
config,
|
||||||
|
onDebugMessage,
|
||||||
|
addItemToHistory,
|
||||||
|
setPendingHistoryItem,
|
||||||
|
onExec,
|
||||||
|
executeCommand,
|
||||||
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
return { handleShellCommand };
|
return { handleShellCommand };
|
||||||
|
|
|
@ -82,6 +82,7 @@ export const useGeminiStream = (
|
||||||
}, []);
|
}, []);
|
||||||
const { handleShellCommand } = useShellCommandProcessor(
|
const { handleShellCommand } = useShellCommandProcessor(
|
||||||
addItem,
|
addItem,
|
||||||
|
setPendingHistoryItem,
|
||||||
onExec,
|
onExec,
|
||||||
onDebugMessage,
|
onDebugMessage,
|
||||||
config,
|
config,
|
||||||
|
|
Loading…
Reference in New Issue