forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add top-level exec and shell commands #52
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
Draft
Hweinstock
wants to merge
8
commits into
refactor
Choose a base branch
from
feat/top-level-exec-shell
base: refactor
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.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8113ec3
feat: add top-level exec and shell commands
Hweinstock dc85a8e
fix: address exec and shell review feedback
Hweinstock e6bb43c
fix: simplify exec and shell routing
Hweinstock 74c4ef7
fix: support runtime exec routing
Hweinstock 9ecc9dc
fix: use explicit exec route parameters
Hweinstock f733fb2
fix: add dedicated runtime exec screen
Hweinstock 01b8d2e
fix: preserve resource exec and shell routes
Hweinstock 5eb1928
test: update resource command menus
Hweinstock 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
Some comments aren't visible on the classic Files Changed page.
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
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
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,114 @@ | ||
| import z from "zod"; | ||
| import { ResourceNotFoundError, InputValidationError } from "../../errors"; | ||
| import { createHandler, flag } from "../../router"; | ||
| import { JsonKey } from "../keys"; | ||
| import type { AppIO } from "../../io"; | ||
| import type { Core } from "../types"; | ||
| import { | ||
| assertMutuallyExclusiveFlags, | ||
| contextForResource, | ||
| coreOptsFromCtx, | ||
| toResourceArn, | ||
| } from "../utils"; | ||
| import { JsonRendererKey, renderTuiAt } from "../../tui"; | ||
| import { regionFromArn, serviceIdFromArn } from "../../core/arn"; | ||
| import { RegionKey } from "../keys"; | ||
| import { invokeExecCommand } from "./operation"; | ||
|
|
||
| export const createExecHandler = (core: Core, io: AppIO) => | ||
| createHandler({ | ||
| name: "exec", | ||
| description: "run a shell command in a Runtime or harness", | ||
| flags: [ | ||
| flag( | ||
| "runtime", | ||
| "the name a Runtime in the project, or ID of a Runtime in the account", | ||
| z.string().min(1).optional(), | ||
| ), | ||
| flag( | ||
| "harness", | ||
| "the name of a harness in the project, or ID of a harness in the account", | ||
| z.string().min(1).optional(), | ||
| ), | ||
| flag("command", "the shell command to run", z.string().optional()), | ||
| flag( | ||
| "session-id", | ||
| "the session ID to run in (33-100 characters)", | ||
| z.string().min(33).max(100).optional(), | ||
| ), | ||
| flag( | ||
| "qualifier", | ||
| "the endpoint qualifier to run in (default DEFAULT)", | ||
| z.string().optional(), | ||
| ), | ||
| flag( | ||
| "timeout", | ||
| "seconds to wait for the command (1-3600)", | ||
| z.number().int().min(1).max(3600).optional(), | ||
| ), | ||
| ], | ||
| handle: async (ctx, flags) => { | ||
| assertMutuallyExclusiveFlags(flags, ["runtime", "harness"]); | ||
|
|
||
| const resourceType = | ||
| flags.runtime !== undefined | ||
| ? "runtime" | ||
| : flags.harness !== undefined | ||
| ? "harness" | ||
| : undefined; | ||
| const identifier = flags.runtime ?? flags.harness; | ||
| if (resourceType === undefined || identifier === undefined) { | ||
| throw new InputValidationError("specify one of --runtime or --harness"); | ||
| } | ||
|
|
||
| const resourceCtx = await contextForResource({ | ||
| core, | ||
| context: ctx, | ||
| resourceType, | ||
| identifier, | ||
| }); | ||
| const resourceArn = await toResourceArn({ | ||
| core, | ||
| context: resourceCtx, | ||
| resourceType, | ||
| identifier, | ||
| }); | ||
| if (resourceArn === undefined) { | ||
| throw new ResourceNotFoundError( | ||
| `${resourceType === "runtime" ? "Runtime" : "Harness"} '${identifier}' was not found`, | ||
| ); | ||
| } | ||
|
|
||
| const resourceRegion = regionFromArn(resourceArn); | ||
| const resolvedCtx = resourceRegion | ||
| ? resourceCtx.withValue(RegionKey, resourceRegion) | ||
| : resourceCtx; | ||
| const resourceId = serviceIdFromArn(resourceArn); | ||
| if (flags.command === undefined) { | ||
| if (ctx.require(JsonKey)) { | ||
| throw new InputValidationError("required option '--command <command>' not specified"); | ||
| } | ||
| let path = `/agentcore/${resourceType === "runtime" ? "runtime/exec" : "harness/exec"}/${encodeURIComponent(resourceId)}`; | ||
| if (flags["session-id"]) path += `/${encodeURIComponent(flags["session-id"])}`; | ||
| const params = new URLSearchParams(); | ||
| if (flags.qualifier) params.set("qualifier", flags.qualifier); | ||
| if (flags.timeout !== undefined) params.set("timeout", String(flags.timeout)); | ||
| if (params.size > 0) path += `?${params}`; | ||
| await renderTuiAt(path, resolvedCtx, core, io); | ||
| return; | ||
| } | ||
|
|
||
| const result = await invokeExecCommand({ | ||
|
Owner
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. i want to be able to exec in runtime. see previous comment. |
||
| core, | ||
| input: { | ||
| resourceArn, | ||
| command: flags.command, | ||
| runtimeSessionId: flags["session-id"], | ||
| qualifier: flags.qualifier ?? "DEFAULT", | ||
| timeout: flags.timeout, | ||
| }, | ||
| options: coreOptsFromCtx(resolvedCtx), | ||
| }); | ||
| ctx.require(JsonRendererKey).renderJson(result); | ||
| }, | ||
| }); | ||
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,50 @@ | ||
| import type { InvokeAgentRuntimeCommandRequest } from "@aws-sdk/client-bedrock-agentcore"; | ||
| import type { CoreOptions } from "../../core/types"; | ||
| import type { Core } from "../types"; | ||
| import { applyExecEvent, finishExec, newExecItem } from "../harness/invoke/transcript"; | ||
|
|
||
| export type ExecInput = { | ||
| resourceArn: string; | ||
| command: string; | ||
| runtimeSessionId?: string; | ||
| qualifier: string; | ||
| timeout?: number; | ||
| }; | ||
|
|
||
| export type ExecResult = { | ||
| sessionId?: string; | ||
| command: string; | ||
| exitCode?: number; | ||
| status: "running" | "success" | "error"; | ||
| output: string; | ||
| }; | ||
|
|
||
| export async function invokeExecCommand({ | ||
| core, | ||
| input, | ||
| options, | ||
| signal, | ||
| }: { | ||
| core: Core; | ||
| input: ExecInput; | ||
| options: CoreOptions; | ||
| signal?: AbortSignal; | ||
| }): Promise<ExecResult> { | ||
| const request: InvokeAgentRuntimeCommandRequest = { | ||
| agentRuntimeArn: input.resourceArn, | ||
| qualifier: input.qualifier, | ||
| runtimeSessionId: input.runtimeSessionId, | ||
| body: { command: input.command, timeout: input.timeout }, | ||
| }; | ||
| const response = await core.harness.invokeAgentRuntimeCommand(request, options, signal); | ||
| const item = newExecItem(input.command); | ||
| for await (const event of response.stream ?? []) applyExecEvent(item, event); | ||
| finishExec(item); | ||
| return { | ||
| sessionId: input.runtimeSessionId ?? response.runtimeSessionId, | ||
| command: item.command, | ||
| exitCode: item.exitCode, | ||
| status: item.status, | ||
| output: item.output, | ||
| }; | ||
| } |
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
Oops, something went wrong.
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.
i feel like we could simplify this here with an or to avoid the same error twice.