Skip to content

add support for BedrockAgents #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 5, 2024
Merged
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
50 changes: 50 additions & 0 deletions Sources/AWSLambdaEvents/BedrockAgent.swift
Original file line number Diff line number Diff line change
@@ -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]
}
}
}
90 changes: 90 additions & 0 deletions Tests/AWSLambdaEventsTests/BedrockAgentTests.swift
Original file line number Diff line number Diff line change
@@ -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 HTTPTypes
import XCTest

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")
}
}