Skip to content

Commit eb88522

Browse files
authored
add support for BedrockAgents (#55)
* add support for BedrockAgents * fix soudness
1 parent 212c6dc commit eb88522

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2017-2022 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import HTTPTypes
16+
17+
// https://docs.aws.amazon.com/bedrock/latest/userguide/agents-lambda.html#agents-lambda-input
18+
public struct BedrockAgentRequest: Codable, Sendable {
19+
public let messageVersion: String
20+
public let agent: Agent?
21+
public let sessionId: String?
22+
public let sessionAttributes: [String: String]?
23+
public let promptSessionAttributes: [String: String]?
24+
public let inputText: String?
25+
public let apiPath: String?
26+
public let actionGroup: String?
27+
public let httpMethod: HTTPRequest.Method?
28+
public let parameters: [Parameter]?
29+
public let requestBody: RequestBody?
30+
31+
public struct Agent: Codable, Sendable {
32+
public let alias: String
33+
public let name: String
34+
public let version: String
35+
public let id: String
36+
}
37+
38+
public struct Parameter: Codable, Sendable {
39+
public let name: String
40+
public let type: String
41+
public let value: String
42+
}
43+
44+
public struct RequestBody: Codable, Sendable {
45+
public let content: [String: Content]
46+
public struct Content: Codable, Sendable {
47+
public let properties: [Parameter]
48+
}
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
@testable import AWSLambdaEvents
16+
import HTTPTypes
17+
import XCTest
18+
19+
class BedrockAgentTests: XCTestCase {
20+
static let eventBody =
21+
"""
22+
{
23+
"messageVersion": "1.0",
24+
"agent": {
25+
"alias": "AGENT_ID",
26+
"name": "StockQuoteAgent",
27+
"version": "DRAFT",
28+
"id": "PR3AHNYEAA"
29+
},
30+
"sessionId": "486652066693565",
31+
"sessionAttributes": {},
32+
"promptSessionAttributes": {},
33+
"inputText": "what the price of amazon stock ?",
34+
"apiPath": "/stocks/{symbol}",
35+
"actionGroup": "StockQuoteService",
36+
"httpMethod": "GET",
37+
"parameters": [
38+
{
39+
"name": "symbol",
40+
"type": "string",
41+
"value": "AMZN"
42+
}
43+
],
44+
"requestBody": {
45+
"content": {
46+
"application/text": {
47+
"properties": [
48+
{
49+
"name": "symbol",
50+
"type": "string",
51+
"value": "AMZN"
52+
}
53+
]
54+
}
55+
}
56+
}
57+
}
58+
"""
59+
60+
func testSimpleEventFromJSON() throws {
61+
let data = BedrockAgentTests.eventBody.data(using: .utf8)!
62+
var event: BedrockAgentRequest?
63+
XCTAssertNoThrow(event = try JSONDecoder().decode(BedrockAgentRequest.self, from: data))
64+
65+
XCTAssertEqual(event?.messageVersion, "1.0")
66+
67+
XCTAssertEqual(event?.agent?.alias, "AGENT_ID")
68+
XCTAssertEqual(event?.agent?.name, "StockQuoteAgent")
69+
XCTAssertEqual(event?.agent?.version, "DRAFT")
70+
XCTAssertEqual(event?.agent?.id, "PR3AHNYEAA")
71+
72+
XCTAssertEqual(event?.sessionId, "486652066693565")
73+
XCTAssertEqual(event?.inputText, "what the price of amazon stock ?")
74+
XCTAssertEqual(event?.apiPath, "/stocks/{symbol}")
75+
XCTAssertEqual(event?.actionGroup, "StockQuoteService")
76+
XCTAssertEqual(event?.httpMethod, .get)
77+
78+
XCTAssertTrue(event?.parameters?.count == 1)
79+
XCTAssertEqual(event?.parameters?[0].name, "symbol")
80+
XCTAssertEqual(event?.parameters?[0].type, "string")
81+
XCTAssertEqual(event?.parameters?[0].value, "AMZN")
82+
83+
let body = try XCTUnwrap(event?.requestBody?.content)
84+
let content = try XCTUnwrap(body["application/text"])
85+
XCTAssertTrue(content.properties.count == 1)
86+
XCTAssertEqual(content.properties[0].name, "symbol")
87+
XCTAssertEqual(content.properties[0].type, "string")
88+
XCTAssertEqual(content.properties[0].value, "AMZN")
89+
}
90+
}

0 commit comments

Comments
 (0)