invokeCodeInterpreter method

Future<InvokeCodeInterpreterResponse> invokeCodeInterpreter({
  1. required String codeInterpreterIdentifier,
  2. required ToolName name,
  3. ToolArguments? arguments,
  4. String? sessionId,
  5. String? traceId,
  6. String? traceParent,
})

Executes code within an active code interpreter session in Amazon Bedrock AgentCore. This operation processes the provided code, runs it in a secure environment, and returns the execution results including output, errors, and generated visualizations.

To execute code, you must specify the code interpreter identifier, session ID, and the code to run in the arguments parameter. The operation returns a stream containing the execution results, which can include text output, error messages, and data visualizations.

This operation is subject to request rate limiting based on your account's service quotas.

The following operations are related to InvokeCodeInterpreter:

May throw AccessDeniedException. May throw ConflictException. May throw InternalServerException. May throw ResourceNotFoundException. May throw ServiceQuotaExceededException. May throw ThrottlingException. May throw ValidationException.

Parameter codeInterpreterIdentifier : The unique identifier of the code interpreter associated with the session. This must match the identifier used when creating the session with StartCodeInterpreterSession.

Parameter name : The name of the code interpreter to invoke.

Parameter arguments : The arguments for the code interpreter. This includes the code to execute and any additional parameters such as the programming language, whether to clear the execution context, and other execution options. The structure of this parameter depends on the specific code interpreter being used.

Parameter sessionId : The unique identifier of the code interpreter session to use. This must be an active session created with StartCodeInterpreterSession. If the session has expired or been stopped, the request will fail.

Parameter traceId : The trace identifier for request tracking.

Parameter traceParent : The parent trace information for distributed tracing.

Implementation

Future<InvokeCodeInterpreterResponse> invokeCodeInterpreter({
  required String codeInterpreterIdentifier,
  required ToolName name,
  ToolArguments? arguments,
  String? sessionId,
  String? traceId,
  String? traceParent,
}) async {
  final headers = <String, String>{
    if (sessionId != null)
      'x-amzn-code-interpreter-session-id': sessionId.toString(),
    if (traceId != null) 'X-Amzn-Trace-Id': traceId.toString(),
    if (traceParent != null) 'traceparent': traceParent.toString(),
  };
  final $payload = <String, dynamic>{
    'name': name.value,
    if (arguments != null) 'arguments': arguments,
  };
  final response = await _protocol.sendRaw(
    payload: $payload,
    method: 'POST',
    requestUri:
        '/code-interpreters/${Uri.encodeComponent(codeInterpreterIdentifier)}/tools/invoke',
    headers: headers,
    exceptionFnMap: _exceptionFns,
  );
  final $json = await _s.jsonFromResponse(response);
  return InvokeCodeInterpreterResponse(
    stream: CodeInterpreterStreamOutput.fromJson($json),
    sessionId: _s.extractHeaderStringValue(
        response.headers, 'x-amzn-code-interpreter-session-id'),
  );
}