From f007e4a5a0d6c222cc3388d2b8d134ef7d32a707 Mon Sep 17 00:00:00 2001 From: Sebastien Stormacq Date: Wed, 29 May 2024 17:49:39 +0200 Subject: [PATCH 1/2] add support for BedrockAgents --- Sources/AWSLambdaEvents/BedrockAgent.swift | 50 +++++++++++ .../BedrockAgentTests.swift | 90 +++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 Sources/AWSLambdaEvents/BedrockAgent.swift create mode 100644 Tests/AWSLambdaEventsTests/BedrockAgentTests.swift diff --git a/Sources/AWSLambdaEvents/BedrockAgent.swift b/Sources/AWSLambdaEvents/BedrockAgent.swift new file mode 100644 index 0000000..68e9559 --- /dev/null +++ b/Sources/AWSLambdaEvents/BedrockAgent.swift @@ -0,0 +1,50 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftAWSLambdaRuntime open source project +// +// Copyright (c) 2017-2022 Apple Inc. and the SwiftAWSLambdaRuntime project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import HTTPTypes + +// https://docs.aws.amazon.com/bedrock/latest/userguide/agents-lambda.html#agents-lambda-input +public struct BedrockAgentRequest: Codable, Sendable { + public let messageVersion: String + public let agent: Agent? + public let sessionId: String? + public let sessionAttributes: [String: String]? + public let promptSessionAttributes: [String: String]? + public let inputText: String? + public let apiPath: String? + public let actionGroup: String? + public let httpMethod: HTTPRequest.Method? + public let parameters: [Parameter]? + public let requestBody: RequestBody? + + public struct Agent: Codable, Sendable { + public let alias: String + public let name: String + public let version: String + public let id: String + } + + public struct Parameter: Codable, Sendable { + public let name: String + public let type: String + public let value: String + } + + public struct RequestBody: Codable, Sendable { + public let content: [String: Content] + public struct Content: Codable, Sendable { + public let properties: [Parameter] + } + } +} diff --git a/Tests/AWSLambdaEventsTests/BedrockAgentTests.swift b/Tests/AWSLambdaEventsTests/BedrockAgentTests.swift new file mode 100644 index 0000000..278e02e --- /dev/null +++ b/Tests/AWSLambdaEventsTests/BedrockAgentTests.swift @@ -0,0 +1,90 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftAWSLambdaRuntime open source project +// +// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +@testable import AWSLambdaEvents +import XCTest +import HTTPTypes + +class BedrockAgentTests: XCTestCase { + static let eventBody = + """ + { + "messageVersion": "1.0", + "agent": { + "alias": "AGENT_ID", + "name": "StockQuoteAgent", + "version": "DRAFT", + "id": "PR3AHNYEAA" + }, + "sessionId": "486652066693565", + "sessionAttributes": {}, + "promptSessionAttributes": {}, + "inputText": "what the price of amazon stock ?", + "apiPath": "/stocks/{symbol}", + "actionGroup": "StockQuoteService", + "httpMethod": "GET", + "parameters": [ + { + "name": "symbol", + "type": "string", + "value": "AMZN" + } + ], + "requestBody": { + "content": { + "application/text": { + "properties": [ + { + "name": "symbol", + "type": "string", + "value": "AMZN" + } + ] + } + } + } + } + """ + + func testSimpleEventFromJSON() throws { + let data = BedrockAgentTests.eventBody.data(using: .utf8)! + var event: BedrockAgentRequest? + XCTAssertNoThrow(event = try JSONDecoder().decode(BedrockAgentRequest.self, from: data)) + + XCTAssertEqual(event?.messageVersion, "1.0") + + XCTAssertEqual(event?.agent?.alias, "AGENT_ID") + XCTAssertEqual(event?.agent?.name, "StockQuoteAgent") + XCTAssertEqual(event?.agent?.version, "DRAFT") + XCTAssertEqual(event?.agent?.id, "PR3AHNYEAA") + + XCTAssertEqual(event?.sessionId, "486652066693565") + XCTAssertEqual(event?.inputText, "what the price of amazon stock ?") + XCTAssertEqual(event?.apiPath, "/stocks/{symbol}") + XCTAssertEqual(event?.actionGroup, "StockQuoteService") + XCTAssertEqual(event?.httpMethod, .get) + + XCTAssertTrue(event?.parameters?.count == 1) + XCTAssertEqual(event?.parameters?[0].name, "symbol") + XCTAssertEqual(event?.parameters?[0].type, "string") + XCTAssertEqual(event?.parameters?[0].value, "AMZN") + + let body = try XCTUnwrap(event?.requestBody?.content) + let content = try XCTUnwrap(body["application/text"]) + XCTAssertTrue(content.properties.count == 1) + XCTAssertEqual(content.properties[0].name, "symbol") + XCTAssertEqual(content.properties[0].type, "string") + XCTAssertEqual(content.properties[0].value, "AMZN") + } +} From 208d7803d72fc036b448f3960d8fb3bc30118cb7 Mon Sep 17 00:00:00 2001 From: Sebastien Stormacq Date: Wed, 29 May 2024 17:51:41 +0200 Subject: [PATCH 2/2] fix soudness --- Tests/AWSLambdaEventsTests/BedrockAgentTests.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/AWSLambdaEventsTests/BedrockAgentTests.swift b/Tests/AWSLambdaEventsTests/BedrockAgentTests.swift index 278e02e..ef675f5 100644 --- a/Tests/AWSLambdaEventsTests/BedrockAgentTests.swift +++ b/Tests/AWSLambdaEventsTests/BedrockAgentTests.swift @@ -13,8 +13,8 @@ //===----------------------------------------------------------------------===// @testable import AWSLambdaEvents -import XCTest import HTTPTypes +import XCTest class BedrockAgentTests: XCTestCase { static let eventBody = @@ -63,7 +63,7 @@ class BedrockAgentTests: XCTestCase { XCTAssertNoThrow(event = try JSONDecoder().decode(BedrockAgentRequest.self, from: data)) XCTAssertEqual(event?.messageVersion, "1.0") - + XCTAssertEqual(event?.agent?.alias, "AGENT_ID") XCTAssertEqual(event?.agent?.name, "StockQuoteAgent") XCTAssertEqual(event?.agent?.version, "DRAFT")