55 * Generates Python code strings for action groups, KBs, multi-agent collaboration,
66 * code interpreter, user input, guardrails, prompts, and memory wiring.
77 */
8-
98import type {
109 ActionGroupInfo ,
1110 BedrockAgentConfig ,
@@ -76,18 +75,15 @@ export abstract class BaseBedrockTranslator {
7675 protected readonly collaboratorContext ?: { name : string ; instruction : string ; relayHistory : string }
7776 ) {
7877 this . agentInfo = config . agent ;
79- this . actionGroups = ( config . action_groups ?? [ ] ) . filter (
80- ag => ( ag . actionGroupState ?? 'ENABLED' ) === 'ENABLED'
81- ) ;
78+ this . actionGroups = ( config . action_groups ?? [ ] ) . filter ( ag => ( ag . actionGroupState ?? 'ENABLED' ) === 'ENABLED' ) ;
8279 this . customActionGroups = this . actionGroups . filter ( ag => ! ag . parentActionSignature ) ;
8380 this . knowledgeBases = config . knowledge_bases ?? [ ] ;
8481 this . collaborators = config . collaborators ?? [ ] ;
8582
8683 this . modelId = this . agentInfo . foundationModel ?? '' ;
8784 this . agentRegion = this . agentInfo . agentArn ?. split ( ':' ) [ 3 ] ?? 'us-east-1' ;
8885 this . instruction = this . agentInfo . instruction ?? '' ;
89- this . promptConfigs =
90- this . agentInfo . promptOverrideConfiguration ?. promptConfigurations ?? [ ] ;
86+ this . promptConfigs = this . agentInfo . promptOverrideConfiguration ?. promptConfigurations ?? [ ] ;
9187 this . enabledPrompts = [ ] ;
9288
9389 // Memory
@@ -111,7 +107,8 @@ export abstract class BaseBedrockTranslator {
111107 this . guardrailConfig = { } ;
112108 const gc = this . agentInfo . guardrailConfiguration ;
113109 if ( gc ) {
114- const gId = ( gc as Record < string , string > ) . guardrailId ?? ( gc as Record < string , string > ) . guardrailIdentifier ?? '' ;
110+ const gId =
111+ ( gc as Record < string , string > ) . guardrailId ?? ( gc as Record < string , string > ) . guardrailIdentifier ?? '' ;
115112 const gVersion = ( gc as Record < string , string > ) . version ?? ( gc as Record < string , string > ) . guardrailVersion ?? '' ;
116113 if ( gId ) {
117114 this . guardrailConfig = { guardrailIdentifier : gId , guardrailVersion : gVersion } ;
@@ -221,7 +218,16 @@ load_dotenv()
221218 const params = fn . parameters ?? { } ;
222219 const paramList = Object . entries ( params )
223220 . map ( ( [ name , info ] ) => {
224- const pyType = info . type === 'string' ? 'str' : info . type === 'integer' ? 'int' : info . type === 'number' ? 'float' : info . type === 'boolean' ? 'bool' : 'str' ;
221+ const pyType =
222+ info . type === 'string'
223+ ? 'str'
224+ : info . type === 'integer'
225+ ? 'int'
226+ : info . type === 'number'
227+ ? 'float'
228+ : info . type === 'boolean'
229+ ? 'bool'
230+ : 'str' ;
225231 return `${ name } : ${ pyType } ` ;
226232 } )
227233 . join ( ', ' ) ;
@@ -286,13 +292,9 @@ memory_id = os.environ.get("MEMORY_ID", "")
286292 platform === 'strands'
287293 ? 'tools_used.update(list(agent_result.metrics.tool_metrics.keys()))'
288294 : 'tools_used.update([msg.name for msg in agent_result if isinstance(msg, ToolMessage)])' ;
289- const responseContent =
290- platform === 'strands' ? 'str(agent_result)' : 'agent_result[-1].content' ;
295+ const responseContent = platform === 'strands' ? 'str(agent_result)' : 'agent_result[-1].content' ;
291296
292- const lines = [
293- 'def endpoint(payload, context):' ,
294- ' try:' ,
295- ] ;
297+ const lines = [ 'def endpoint(payload, context):' , ' try:' ] ;
296298 if ( this . agentcoreMemoryEnabled ) {
297299 lines . push ( ' global user_id' ) ;
298300 lines . push ( ' user_id = user_id or payload.get("userId", uuid.uuid4().hex[:8])' ) ;
@@ -303,30 +305,30 @@ memory_id = os.environ.get("MEMORY_ID", "")
303305 ' tools_used.clear()' ,
304306 ' agent_query = payload.get("prompt", "")' ,
305307 ' if not agent_query:' ,
306- ' return {\ 'error\ ': "No query provided, please provide a \ 'prompt\ ' field in the payload."}' ,
308+ " return {'error': \ "No query provided, please provide a 'prompt' field in the payload.\"}" ,
307309 '' ,
308310 ' agent_result = invoke_agent(agent_query)' ,
309311 '' ,
310312 ' ' + toolsUsedUpdate ,
311313 ' response_content = ' + responseContent ,
312314 '' ,
313315 ' sources = []' ,
314- ' urls = re.findall(r\ 'https?://[^\\s<>"{}|\\\\^`\\[\\]]+\ ', response_content)' ,
316+ " urls = re.findall(r'https?://[^\\s<>\ "{}|\\\\^`\\[\\]]+', response_content)" ,
315317 ' source_tags = re.findall(r"<source>(.*?)</source>", response_content)' ,
316318 ' sources.extend(urls)' ,
317319 ' sources.extend(source_tags)' ,
318320 ' sources = list(set(sources))' ,
319321 '' ,
320- ' formatted_messages = [(agent_query, "USER"), (response_content if response_content else "No Response.", "ASSISTANT")]' ,
322+ ' formatted_messages = [(agent_query, "USER"), (response_content if response_content else "No Response.", "ASSISTANT")]'
321323 ) ;
322324 if ( memoryEventCode ) {
323325 lines . push ( memoryEventCode ) ;
324326 }
325327 lines . push (
326328 '' ,
327- ' return {\ 'result\ ': {\ 'response\ ': response_content, \ 'sources\ ': sources, \ 'tools_used\ ': list(tools_used), \ 'sessionId\ ': session_id, \ 'messages\ ': formatted_messages}}' ,
329+ " return {'result': {'response': response_content, 'sources': sources, 'tools_used': list(tools_used), 'sessionId': session_id, 'messages': formatted_messages}}" ,
328330 ' except Exception as e:' ,
329- ' return {\ 'error\ ': str(e)}' ,
331+ " return {'error': str(e)}"
330332 ) ;
331333 code += lines . join ( '\n' ) ;
332334 return code ;
0 commit comments