fix: remote stdin and restore diff display

This commit is contained in:
Nate 2025-08-15 12:55:03 -07:00
parent 985db54769
commit 09d3805e03
8 changed files with 126 additions and 12 deletions

View file

@ -6,6 +6,7 @@ import { telemetryService } from "../telemetry/telemetryService.js";
import { startRemoteTUIChat } from "../ui/index.js";
import { getRepoUrl } from "../util/git.js";
import { logger } from "../util/logger.js";
import { readStdinSync } from "../util/stdin.js";
export async function remote(
prompt: string | undefined,
@ -16,6 +17,22 @@ export async function remote(
branch?: string;
} = {},
) {
// Check if prompt should come from stdin instead of parameter
let actualPrompt = prompt;
if (prompt) {
// If prompt is provided, still check for stdin and combine them
const stdinInput = readStdinSync();
if (stdinInput) {
// Combine stdin and prompt argument - stdin comes first in XML block
actualPrompt = `<stdin>\n${stdinInput}\n</stdin>\n\n${prompt}`;
}
} else {
// Try to read from stdin (for piped input like: echo "hello" | cn remote -s)
const stdinInput = readStdinSync();
if (stdinInput) {
actualPrompt = stdinInput;
}
}
// If --url is provided, connect directly to that URL
if (options.url) {
if (options.start) {
@ -41,7 +58,7 @@ export async function remote(
try {
// Start the TUI in remote mode
await startRemoteTUIChat(options.url, prompt);
await startRemoteTUIChat(options.url, actualPrompt);
} finally {
telemetryService.stopActiveTime();
}
@ -64,7 +81,7 @@ export async function remote(
const requestBody: any = {
repoUrl: getRepoUrl(),
name: `devbox-${Date.now()}`,
prompt: prompt,
prompt: actualPrompt,
idempotencyKey: options.idempotencyKey,
};
@ -123,7 +140,7 @@ export async function remote(
try {
// Start the TUI in remote mode (prompt is optional)
await startRemoteTUIChat(remoteUrl, prompt);
await startRemoteTUIChat(remoteUrl, actualPrompt);
} finally {
telemetryService.stopActiveTime();
}

View file

@ -0,0 +1,83 @@
import { createTestContext, cleanupTestContext, runCLI } from "../test-helpers/cli-helpers.js";
describe("E2E: Remote Command - Stdin Support", () => {
let context: any;
beforeEach(async () => {
context = await createTestContext();
});
afterEach(async () => {
await cleanupTestContext(context);
});
it("should work correctly when prompt is provided via stdin with remote -s flag", async () => {
const result = await runCLI(context, {
args: ["remote", "-s", "--url", "http://example.com"],
input: "Hello from stdin",
expectError: false,
timeout: 5000,
});
// Should succeed with direct URL connection
expect(result.exitCode).toBe(0);
// Should output JSON response for --start mode
const output = result.stdout;
expect(output).toContain('"status":"success"');
expect(output).toContain('"url":"http://example.com"');
expect(output).toContain('"mode":"direct_url"');
});
it("should combine stdin and command line prompt when both are provided", async () => {
const result = await runCLI(context, {
args: ["remote", "-s", "--url", "http://example.com", "Command line prompt"],
input: "Stdin input",
expectError: false,
timeout: 5000,
});
// Should succeed with direct URL connection
expect(result.exitCode).toBe(0);
// Should output JSON response for --start mode
const output = result.stdout;
expect(output).toContain('"status":"success"');
expect(output).toContain('"url":"http://example.com"');
expect(output).toContain('"mode":"direct_url"');
});
it("should work with just command line prompt (no stdin)", async () => {
const result = await runCLI(context, {
args: ["remote", "-s", "--url", "http://example.com", "Just command line"],
expectError: false,
timeout: 5000,
});
// Should succeed with direct URL connection
expect(result.exitCode).toBe(0);
// Should output JSON response for --start mode
const output = result.stdout;
expect(output).toContain('"status":"success"');
expect(output).toContain('"url":"http://example.com"');
expect(output).toContain('"mode":"direct_url"');
});
it("should work with no prompt at all", async () => {
const result = await runCLI(context, {
args: ["remote", "-s", "--url", "http://example.com"],
expectError: false,
timeout: 5000,
});
// Should succeed with direct URL connection
expect(result.exitCode).toBe(0);
// Should output JSON response for --start mode
const output = result.stdout;
expect(output).toContain('"status":"success"');
expect(output).toContain('"url":"http://example.com"');
expect(output).toContain('"mode":"direct_url"');
});
});

View file

@ -170,7 +170,7 @@ describe("editTool", () => {
const result = await editTool.run(args);
expect(result).toBe(`Successfully edited ${testFilePath}`);
expect(result).toBe(`Successfully edited ${testFilePath}\nDiff:\nmocked diff`);
expect(fs.writeFileSync).toHaveBeenCalledWith(testFilePath, newContent, "utf-8");
});

View file

@ -178,7 +178,10 @@ parameter is useful if you want to rename a variable for instance.`,
);
}
return `Successfully edited ${args.file_path}`;
// Generate diff for result display
const diff = generateDiff(args.oldContent, args.newContent, args.file_path);
return `Successfully edited ${args.file_path}\nDiff:\n${diff}`;
} catch (error) {
throw new Error(
`Error: failed to edit ${args.file_path}: ${

View file

@ -327,7 +327,7 @@ describe("multiEditTool", () => {
const result = await multiEditTool.run(args);
expect(result).toBe(`Successfully edited ${testFilePath} with 1 edit`);
expect(result).toBe(`Successfully edited ${testFilePath} with 1 edit\nDiff:\nmocked diff`);
expect(fs.writeFileSync).toHaveBeenCalledWith(testFilePath, newContent, "utf-8");
});
@ -342,7 +342,7 @@ describe("multiEditTool", () => {
const result = await multiEditTool.run(args);
expect(result).toBe(`Successfully edited ${testFilePath} with 3 edits`);
expect(result).toBe(`Successfully edited ${testFilePath} with 3 edits\nDiff:\nmocked diff`);
});
it("should return correct message for new file creation", async () => {
@ -356,7 +356,7 @@ describe("multiEditTool", () => {
const result = await multiEditTool.run(args);
expect(result).toBe(`Successfully created /tmp/new-file.txt with 1 edit`);
expect(result).toBe(`Successfully created /tmp/new-file.txt with 1 edit\nDiff:\nmocked diff`);
});
it("should throw error if file write fails", async () => {

View file

@ -309,8 +309,11 @@ If you want to create a new file, use:
);
}
// Generate diff for result display
const diff = generateDiff(args.originalContent, args.newContent, args.file_path);
const action = args.isCreatingNewFile ? "created" : "edited";
return `Successfully ${action} ${args.file_path} with ${args.editCount} edit${args.editCount === 1 ? "" : "s"}`;
return `Successfully ${action} ${args.file_path} with ${args.editCount} edit${args.editCount === 1 ? "" : "s"}\nDiff:\n${diff}`;
} catch (error) {
throw new Error(
`Error: failed to edit ${args.file_path}: ${

View file

@ -139,7 +139,10 @@ export const writeFileTool: Tool = {
);
}
return `Successfully wrote to file: ${args.filepath}`;
// Generate diff for result display
const diff = generateDiff(oldContent, args.content, args.filepath);
return `Successfully wrote to file: ${args.filepath}\nDiff:\n${diff}`;
} else {
// New file creation - count all lines as added
const lineCount = args.content.split("\n").length;

View file

@ -43,9 +43,9 @@ const ToolResultSummary: React.FC<ToolResultSummaryProps> = ({
);
}
// Handle Write/Edit with diff specially
// Handle Write/Edit/MultiEdit with diff specially
if (
(toolName === "Write" || toolName === "Edit") &&
(toolName === "Write" || toolName === "Edit" || toolName === "MultiEdit") &&
content.includes("Diff:\n")
) {
const diffSection = content.split("Diff:\n")[1];
@ -56,6 +56,8 @@ const ToolResultSummary: React.FC<ToolResultSummaryProps> = ({
<Text color="gray">⎿ </Text>
<Text color="green">
{toolName === "Edit"
? " File edited successfully"
: toolName === "MultiEdit"
? " File edited successfully"
: " File written successfully"}
</Text>
@ -147,6 +149,9 @@ const ToolResultSummary: React.FC<ToolResultSummaryProps> = ({
case "Edit":
return "File edited successfully";
case "MultiEdit":
return "File edited successfully";
case "List":
return `Listed ${lines} ${lines === 1 ? "item" : "items"}`;