Repository navigation
Add shell input to the composer #163
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bdfda67
Add shell input to the composer (issue #161)
soorya-u b9e7f39
Polish shell input: terminal-green accent, disappearing !, quieter row
soorya-u b232647
Simplify shell execution row: tooltip on dot, toast on failure
soorya-u 98eec54
Address PR review feedback: a11y, agent-gate coupling, tree-stability…
soorya-u 3a656c0
Address second round of review feedback: proper error types, folder p…
soorya-u 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
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 |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { appendConversation } from "@cyrus/database/repositories/conversations"; | ||
| import { resolveThreadGitCwd } from "@cyrus/database/repositories/git"; | ||
| import { orpcOk, throwOrpc } from "@cyrus/errors/orpc"; | ||
| import { randomId } from "@cyrus/utils/identity"; | ||
| import { log } from "evlog"; | ||
| import { cancelActiveShellExecution, runShellExecution } from "@/shell/run"; | ||
| import type { ControllerDeps } from "./deps"; | ||
|
|
||
| export function shellHandlers({ os }: ControllerDeps) { | ||
| return { | ||
| executeShellInput: os.executeShellInput.handler( | ||
| async ({ input, context }) => { | ||
| const { threadId, command } = input; | ||
| const cwd = orpcOk(await resolveThreadGitCwd(threadId)); | ||
| const shellExecutionId = randomId(); | ||
|
|
||
| const started = await appendConversation(threadId, { | ||
| threadId, | ||
| shellExecutionId, | ||
| event: { type: "shell_execution_start", command }, | ||
| }); | ||
| if (started.isErr()) throwOrpc(started.error); | ||
| context.eventBus.publish(started.value.chunk); | ||
|
|
||
| runShellExecution({ | ||
| command, | ||
| context, | ||
| cwd, | ||
| shellExecutionId, | ||
| threadId, | ||
| }).catch((error) => { | ||
| log.error({ | ||
| kind: "shell_execution_failed", | ||
| error, | ||
| shellExecutionId, | ||
| threadId, | ||
| }); | ||
| }); | ||
|
|
||
| return { shellExecutionId }; | ||
| } | ||
| ), | ||
|
|
||
| cancelShellExecution: os.cancelShellExecution.handler(({ input }) => { | ||
| cancelActiveShellExecution(input.threadId, input.shellExecutionId); | ||
| return {}; | ||
| }), | ||
| }; | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import type { ShellExecutionLine } from "@cyrus/schemas/rtc/chat"; | ||
| import { pumpLines } from "./run"; | ||
|
|
||
| function streamFromChunks(chunks: Uint8Array[]): ReadableStream<Uint8Array> { | ||
| return new ReadableStream({ | ||
| start(controller) { | ||
| for (const chunk of chunks) controller.enqueue(chunk); | ||
| controller.close(); | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| function encode(text: string): Uint8Array { | ||
| return new TextEncoder().encode(text); | ||
| } | ||
|
|
||
| async function collectLines( | ||
| stream: ReadableStream<Uint8Array> | null | ||
| ): Promise<ShellExecutionLine[]> { | ||
| const lines: ShellExecutionLine[] = []; | ||
| await pumpLines(stream, "stdout", (line) => lines.push(line)); | ||
| return lines; | ||
| } | ||
|
|
||
| describe("pumpLines", () => { | ||
| test("splits a single chunk into its newline-terminated lines", async () => { | ||
| const lines = await collectLines(streamFromChunks([encode("a\nb\nc\n")])); | ||
| expect(lines).toEqual([ | ||
| { stream: "stdout", text: "a" }, | ||
| { stream: "stdout", text: "b" }, | ||
| { stream: "stdout", text: "c" }, | ||
| ]); | ||
| }); | ||
|
|
||
| test("joins a line split across two read chunks", async () => { | ||
| const lines = await collectLines( | ||
| streamFromChunks([encode("hello wor"), encode("ld\n")]) | ||
| ); | ||
| expect(lines).toEqual([{ stream: "stdout", text: "hello world" }]); | ||
| }); | ||
|
|
||
| test("emits trailing text with no final newline as its own line", async () => { | ||
| const lines = await collectLines(streamFromChunks([encode("no newline")])); | ||
| expect(lines).toEqual([{ stream: "stdout", text: "no newline" }]); | ||
| }); | ||
|
|
||
| test("decodes a multi-byte UTF-8 character split across chunk boundaries", async () => { | ||
| // "🎉" is 4 bytes in UTF-8 — split it in the middle of the codepoint. | ||
| const bytes = encode("party 🎉\n"); | ||
| const lines = await collectLines( | ||
| streamFromChunks([bytes.slice(0, 7), bytes.slice(7)]) | ||
| ); | ||
| expect(lines).toEqual([{ stream: "stdout", text: "party 🎉" }]); | ||
| }); | ||
|
|
||
| test("emits nothing for an empty stream", async () => { | ||
| const lines = await collectLines(streamFromChunks([])); | ||
| expect(lines).toEqual([]); | ||
| }); | ||
|
|
||
| test("resolves immediately for a null stream (stdout/stderr absent)", async () => { | ||
| const lines = await collectLines(null); | ||
| expect(lines).toEqual([]); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.