|
| 1 | +/** |
| 2 | + * MIT No Attribution |
| 3 | + * |
| 4 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 | + * the Software without restriction, including without limitation the rights to |
| 8 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 9 | + * the Software, and to permit persons to whom the Software is furnished to do so. |
| 10 | + * |
| 11 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 12 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 14 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 15 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 16 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 17 | + * SOFTWARE. |
| 18 | + */ |
| 19 | + |
| 20 | +import { randomUUID } from 'crypto'; |
| 21 | +import { BedrockAgentCoreClient, InvokeAgentRuntimeCommand, StopRuntimeSessionCommand } from '@aws-sdk/client-bedrock-agentcore'; |
| 22 | +import type { ComputeStrategy, SessionHandle, SessionStatus } from '../compute-strategy'; |
| 23 | +import { logger } from '../logger'; |
| 24 | +import type { BlueprintConfig } from '../repo-config'; |
| 25 | + |
| 26 | +export class AgentCoreComputeStrategy implements ComputeStrategy { |
| 27 | + readonly type = 'agentcore'; |
| 28 | + private readonly client: BedrockAgentCoreClient; |
| 29 | + private readonly runtimeArn: string; |
| 30 | + |
| 31 | + constructor(options: { runtimeArn: string }) { |
| 32 | + this.runtimeArn = options.runtimeArn; |
| 33 | + this.client = new BedrockAgentCoreClient({}); |
| 34 | + } |
| 35 | + |
| 36 | + async startSession(input: { |
| 37 | + taskId: string; |
| 38 | + payload: Record<string, unknown>; |
| 39 | + blueprintConfig: BlueprintConfig; |
| 40 | + }): Promise<SessionHandle> { |
| 41 | + // AgentCore requires runtimeSessionId >= 33 chars; UUID v4 is 36 chars. |
| 42 | + const sessionId = randomUUID(); |
| 43 | + const runtimeArn = input.blueprintConfig.runtime_arn ?? this.runtimeArn; |
| 44 | + |
| 45 | + const command = new InvokeAgentRuntimeCommand({ |
| 46 | + agentRuntimeArn: runtimeArn, |
| 47 | + runtimeSessionId: sessionId, |
| 48 | + contentType: 'application/json', |
| 49 | + accept: 'application/json', |
| 50 | + payload: new TextEncoder().encode(JSON.stringify({ input: input.payload })), |
| 51 | + }); |
| 52 | + |
| 53 | + await this.client.send(command); |
| 54 | + |
| 55 | + logger.info('AgentCore session invoked', { task_id: input.taskId, session_id: sessionId, runtime_arn: runtimeArn }); |
| 56 | + |
| 57 | + return { |
| 58 | + sessionId, |
| 59 | + strategyType: this.type, |
| 60 | + metadata: { runtimeArn }, |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + async pollSession(_handle: SessionHandle): Promise<SessionStatus> { |
| 65 | + // Polling is currently done at the orchestrator level via DDB reads. |
| 66 | + // This method exists for PR 2 where different strategies may poll differently. |
| 67 | + return { status: 'running' }; |
| 68 | + } |
| 69 | + |
| 70 | + async stopSession(handle: SessionHandle): Promise<void> { |
| 71 | + const runtimeArn = handle.metadata.runtimeArn as string | undefined; |
| 72 | + if (!runtimeArn) { |
| 73 | + logger.warn('No runtimeArn in session handle, cannot stop session', { session_id: handle.sessionId }); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + try { |
| 78 | + await this.client.send(new StopRuntimeSessionCommand({ |
| 79 | + agentRuntimeArn: runtimeArn, |
| 80 | + runtimeSessionId: handle.sessionId, |
| 81 | + })); |
| 82 | + logger.info('AgentCore session stopped', { session_id: handle.sessionId }); |
| 83 | + } catch (err) { |
| 84 | + logger.warn('Failed to stop AgentCore session (best-effort)', { |
| 85 | + session_id: handle.sessionId, |
| 86 | + error: err instanceof Error ? err.message : String(err), |
| 87 | + }); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments