Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/core/dev/localInvoke.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { randomUUID } from "node:crypto";
import { InputValidationError, InvalidEnvironmentError } from "../../errors";
import { abortable } from "../abortable";
import type { RuntimeInvokeResponse } from "../invokeRuntime";

export type LocalRuntimeInvokeRequest = {
port: number;
payload: Uint8Array;
contentType?: string;
accept?: string;
runtimeSessionId?: string;
runtimeUserId?: string;
applicationHeaders?: [string, string][];
traceId?: string;
traceParent?: string;
traceState?: string;
baggage?: string;
};

async function* emptyBody(): AsyncGenerator<Uint8Array> {}

export async function invokeLocalRuntime(
request: LocalRuntimeInvokeRequest,
signal?: AbortSignal,
): Promise<RuntimeInvokeResponse> {
const runtimeSessionId = request.runtimeSessionId ?? randomUUID();
let headers: Headers;
try {
headers = new Headers(request.applicationHeaders);
for (const [name, value] of [
["Content-Type", request.contentType ?? "application/json"],
["Accept", request.accept ?? "text/event-stream"],
["X-Amzn-Bedrock-AgentCore-Runtime-Session-Id", runtimeSessionId],
["X-Amzn-Bedrock-AgentCore-Runtime-User-Id", request.runtimeUserId ?? "default"],
["X-Amzn-Trace-Id", request.traceId],
["traceparent", request.traceParent],
["tracestate", request.traceState],
["baggage", request.baggage],
] as const) {
if (value !== undefined) headers.set(name, value);
}
} catch {
throw new InputValidationError("Invalid local Runtime request header");
}

let response: Response;
try {
response = await fetch(`http://127.0.0.1:${request.port}/invocations`, {
method: "POST",
redirect: "manual",
headers,
body: request.payload as RequestInit["body"],
signal,
});
} catch (error) {
if (signal?.aborted) throw signal.reason ?? error;
const detail = error instanceof Error ? error.message : String(error);
throw new InvalidEnvironmentError(
Comment thread
aidandaly24 marked this conversation as resolved.
`Could not reach local dev server on port ${request.port} (${detail}). Start it with: ` +
`agentcore project dev --mode headless --agent <name> --port ${request.port}`,
{ cause: error },
);
}

const body = (response.body as AsyncIterable<Uint8Array> | null) ?? emptyBody();
return {
statusCode: response.status,
contentType: response.headers.get("content-type") ?? "",
runtimeSessionId:
response.headers.get("x-amzn-bedrock-agentcore-runtime-session-id") ?? runtimeSessionId,
mcpSessionId: response.headers.get("mcp-session-id") ?? undefined,
mcpProtocolVersion: response.headers.get("mcp-protocol-version") ?? undefined,
traceId: response.headers.get("x-amzn-trace-id") ?? undefined,
traceParent: response.headers.get("traceparent") ?? undefined,
traceState: response.headers.get("tracestate") ?? undefined,
baggage: response.headers.get("baggage") ?? undefined,
body: signal ? abortable(body, signal) : body,
};
}
Loading
Loading