-
Notifications
You must be signed in to change notification settings - Fork 749
fix(chat): fix shell command execution output markdown block issue #6995
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
Merged
zixlin7
merged 2 commits into
aws:feature/agentic-chat
from
yueny2020:feature/agentic-chat
Apr 10, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -117,6 +117,14 @@ export interface CommandValidation { | |
| warning?: string | ||
| } | ||
|
|
||
| // Interface for timestamped output chunks | ||
| interface TimestampedChunk { | ||
| timestamp: number | ||
| isStdout: boolean | ||
| content: string | ||
| isFirst: boolean | ||
| } | ||
|
|
||
| export class ExecuteBash { | ||
| private readonly command: string | ||
| private readonly workingDirectory?: string | ||
|
|
@@ -207,22 +215,68 @@ export class ExecuteBash { | |
| const stdoutBuffer: string[] = [] | ||
| const stderrBuffer: string[] = [] | ||
|
|
||
| let firstChunk = true | ||
| let firstStderrChunk = true | ||
| // Use a closure boolean value firstChunk and a function to get and set its value | ||
| let isFirstChunk = true | ||
| const getAndSetFirstChunk = (newValue: boolean): boolean => { | ||
|
Contributor
Author
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. the function getAndSetFirstChunk can avoid duplicate code in line 260 and line 271. |
||
| const oldValue = isFirstChunk | ||
| isFirstChunk = newValue | ||
| return oldValue | ||
| } | ||
|
|
||
| // Use a queue to maintain chronological order of chunks | ||
| // This ensures that the output is processed in the exact order it was generated by the child process. | ||
| const outputQueue: TimestampedChunk[] = [] | ||
| let processingQueue = false | ||
|
|
||
| // Process the queue in order | ||
| const processQueue = () => { | ||
| if (processingQueue || outputQueue.length === 0) { | ||
| return | ||
| } | ||
|
|
||
| processingQueue = true | ||
|
|
||
| try { | ||
| // Sort by timestamp to ensure chronological order | ||
| outputQueue.sort((a, b) => a.timestamp - b.timestamp) | ||
|
|
||
| while (outputQueue.length > 0) { | ||
| const chunk = outputQueue.shift()! | ||
| ExecuteBash.handleTimestampedChunk(chunk, stdoutBuffer, stderrBuffer, updates) | ||
| } | ||
| } finally { | ||
| processingQueue = false | ||
| } | ||
| } | ||
|
|
||
| const childProcessOptions: ChildProcessOptions = { | ||
| spawnOptions: { | ||
| cwd: this.workingDirectory, | ||
| stdio: ['pipe', 'pipe', 'pipe'], | ||
| }, | ||
| collect: false, | ||
| waitForStreams: true, | ||
| onStdout: (chunk: string) => { | ||
| ExecuteBash.handleChunk(firstChunk ? '```console\n' + chunk : chunk, stdoutBuffer, updates) | ||
| firstChunk = false | ||
| onStdout: async (chunk: string) => { | ||
| const isFirst = getAndSetFirstChunk(false) | ||
| const timestamp = Date.now() | ||
| outputQueue.push({ | ||
| timestamp, | ||
| isStdout: true, | ||
| content: chunk, | ||
| isFirst, | ||
| }) | ||
| processQueue() | ||
| }, | ||
| onStderr: (chunk: string) => { | ||
| ExecuteBash.handleChunk(firstStderrChunk ? '```console\n' + chunk : chunk, stderrBuffer, updates) | ||
| firstStderrChunk = false | ||
| onStderr: async (chunk: string) => { | ||
| const isFirst = getAndSetFirstChunk(false) | ||
| const timestamp = Date.now() | ||
| outputQueue.push({ | ||
| timestamp, | ||
| isStdout: false, | ||
| content: chunk, | ||
| isFirst, | ||
| }) | ||
| processQueue() | ||
| }, | ||
| } | ||
|
|
||
|
|
@@ -261,6 +315,17 @@ export class ExecuteBash { | |
| }) | ||
| } | ||
|
|
||
| private static handleTimestampedChunk( | ||
| chunk: TimestampedChunk, | ||
| stdoutBuffer: string[], | ||
| stderrBuffer: string[], | ||
| updates?: Writable | ||
| ): void { | ||
| const buffer = chunk.isStdout ? stdoutBuffer : stderrBuffer | ||
| const content = chunk.isFirst ? '```console\n' + chunk.content : chunk.content | ||
| ExecuteBash.handleChunk(content, buffer, updates) | ||
| } | ||
|
|
||
| private static handleChunk(chunk: string, buffer: string[], updates?: Writable) { | ||
| try { | ||
| updates?.write(chunk) | ||
|
|
||
Oops, something went wrong.
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.
this would be a field that actually needs a brief comment that gives insight into its purpose.