-
Notifications
You must be signed in to change notification settings - Fork 3
Implementation of a persistent shell #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
seabo
wants to merge
1
commit into
main
Choose a base branch
from
persistent-shell
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,97 @@ | ||
| import { spawn } from "child_process"; | ||
| import { ChildProcessWithoutNullStreams, spawn } from "node:child_process"; | ||
| import { ShellCommand, ShellCommandOutput } from "./llm"; | ||
| import { randomString } from "./utils/rand"; | ||
|
|
||
| export async function runCommand( | ||
| cmd: ShellCommand | ||
| ): Promise<ShellCommandOutput> { | ||
| return new Promise((resolve, reject) => { | ||
| const command = spawn(`${cmd.command} ${cmd.args.join(" ")}`, { | ||
| shell: true, | ||
| export class PersistentShell { | ||
| process: ChildProcessWithoutNullStreams; | ||
| stdout: string; | ||
| stderr: string; | ||
| listeners: Map<string, () => void>; | ||
|
|
||
| constructor(shell?: string) { | ||
| this.process = this.createShellProcess(shell); | ||
| this.stdout = ""; | ||
| this.stderr = ""; | ||
| this.listeners = new Map(); | ||
|
|
||
| this.process.stdout.on("data", (data) => { | ||
| data = data.toString(); | ||
| this.stdout += data; | ||
| this.listeners.forEach((listener) => listener()); | ||
| }); | ||
| this.process.stderr.on("data", (data) => { | ||
| data = data.toString(); | ||
| this.stderr += data; | ||
| this.listeners.forEach((listener) => listener()); | ||
| }); | ||
|
|
||
| const commandOutput: ShellCommandOutput = { | ||
| stdout: "", | ||
| stderr: "", | ||
| exitCode: null, | ||
| }; | ||
| this.process.on("exit", (exitCode) => { | ||
| console.log(`shell exit code ${exitCode}`); | ||
| }); | ||
|
|
||
| command.stdout.on("data", (output) => { | ||
| commandOutput.stdout += output.toString(); | ||
| this.process.on("error", (err) => { | ||
| console.error(`shell error: ${err}`); | ||
| }); | ||
| command.stderr.on("data", (output) => { | ||
| commandOutput.stderr += output.toString(); | ||
| this.process.on("message", (msg) => { | ||
| console.error(`shell message: ${msg}`); | ||
| }); | ||
| command.on("close", (code) => { | ||
| commandOutput.exitCode = code; | ||
| resolve(commandOutput); | ||
| this.process.on("disconnect", () => { | ||
| console.log("shell disconnected"); | ||
| }); | ||
| } | ||
|
|
||
| createShellProcess(shell?: string) { | ||
| let shellFile = "/bin/sh"; | ||
| if (process.platform === "win32") { | ||
| shellFile = process.env.comspec || "cmd.exe"; | ||
| } else if (process.platform === "android") { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what |
||
| shellFile = "/system/bin/sh"; | ||
| } | ||
|
|
||
| shellFile = shell || shellFile; | ||
|
|
||
| return spawn(shellFile, { | ||
| shell: false, | ||
| }); | ||
| } | ||
|
|
||
| executeCommand(cmd: ShellCommand): Promise<ShellCommandOutput> { | ||
| const uniqueCommandId = `end_command_${randomString(16)}`; | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| // This listener runs on every chunk received to either stdout or stderr | ||
| const onChunk = () => { | ||
| // Check the entire stdout for the unique end command id | ||
| if (this.stdout.includes(uniqueCommandId)) { | ||
| const splitStdout = this.stdout.split("\n"); | ||
| while (splitStdout.pop() !== uniqueCommandId) {} | ||
| const exitCode = parseInt(splitStdout.pop() || "0"); | ||
| const commandStdout = splitStdout.join("\n"); | ||
|
|
||
| // Remove the end command id from the stdout | ||
| this.stdout = this.stdout.replace(uniqueCommandId, ""); | ||
|
|
||
| // Remove the listener for this command | ||
| if (!this.listeners.delete(uniqueCommandId)) { | ||
| throw new Error( | ||
| `removing listener for command ${uniqueCommandId} failed` | ||
| ); | ||
| } | ||
|
|
||
| resolve({ | ||
| stdout: commandStdout, | ||
| stderr: this.stderr, | ||
| exitCode, | ||
| }); | ||
| } | ||
| }; | ||
| this.listeners.set(uniqueCommandId, onChunk); | ||
|
|
||
| // Write the main command, and the exit code retrieval, and the unique end command id | ||
| // to stdin on the shell. | ||
| this.process.stdin.write(`${cmd.command} ${cmd.args.join(" ")}\n`); | ||
| this.process.stdin.write(`echo $?\n`); | ||
| this.process.stdin.write(`echo ${uniqueCommandId}\n`); | ||
| }); | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import crypto from "crypto"; | ||
|
|
||
| export function randomString(length: number) { | ||
| return crypto.randomBytes(length).toString("hex").slice(0, length); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shellhere isn't usedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh wait it's used further down. think the code could be slightly clearer (and not go through the process.platform checks if that work's going to be ignored anyway)