Skip to content

Commit 99a5d8b

Browse files
committed
merge BedrockService and BedrockTypes into a single library
1 parent 3d05a53 commit 99a5d8b

File tree

113 files changed

+286
-348
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+286
-348
lines changed

Package.swift

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ let package = Package(
88
platforms: [.macOS(.v15), .iOS(.v18), .tvOS(.v18)],
99
products: [
1010
.library(name: "BedrockService", targets: ["BedrockService"]),
11-
.library(name: "BedrockTypes", targets: ["BedrockTypes"]),
1211
],
1312
dependencies: [
1413
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.3.0"),
@@ -21,7 +20,6 @@ let package = Package(
2120
.target(
2221
name: "BedrockService",
2322
dependencies: [
24-
.target(name: "BedrockTypes"),
2523
.product(name: "AWSClientRuntime", package: "aws-sdk-swift"),
2624
.product(name: "AWSBedrock", package: "aws-sdk-swift"),
2725
.product(name: "AWSBedrockRuntime", package: "aws-sdk-swift"),
@@ -31,29 +29,12 @@ let package = Package(
3129
],
3230
path: "swift-bedrock-library/Sources/BedrockService"
3331
),
34-
.target(
35-
name: "BedrockTypes",
36-
dependencies: [
37-
.product(name: "AWSBedrockRuntime", package: "aws-sdk-swift"),
38-
.product(name: "AWSBedrock", package: "aws-sdk-swift"),
39-
.product(name: "Smithy", package: "smithy-swift"),
40-
],
41-
path: "swift-bedrock-library/Sources/BedrockTypes"
42-
),
4332
.testTarget(
4433
name: "BedrockServiceTests",
4534
dependencies: [
4635
.target(name: "BedrockService"),
47-
.target(name: "BedrockTypes"),
4836
],
4937
path: "swift-bedrock-library/Tests/BedrockServiceTests"
5038
),
51-
.testTarget(
52-
name: "BedrockTypesTests",
53-
dependencies: [
54-
.target(name: "BedrockTypes")
55-
],
56-
path: "swift-bedrock-library/Tests/BedrockTypesTests"
57-
),
5839
]
5940
)

swift-bedrock-library/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ let package = Package(
3939
)
4040
```
4141

42-
2. Import the BedrockService and BedrockTypes
42+
2. Import the BedrockService
4343

4444
```swift
4545
import BedrockService
46-
import BedrockTypes
4746
```
4847

4948
3. Initialize the BedrockService
@@ -873,10 +872,10 @@ struct LlamaText: TextModality {
873872
stopSequences: [String]?
874873
) throws -> BedrockBodyCodable {
875874
guard topK == nil else {
876-
throw BedrockServiceError.notSupported("TopK is not supported for Llama text completion")
875+
throw BedrockLibraryError.notSupported("TopK is not supported for Llama text completion")
877876
}
878877
guard stopSequences == nil else {
879-
throw BedrockServiceError.notSupported("stopSequences is not supported for Llama text completion")
878+
throw BedrockLibraryError.notSupported("stopSequences is not supported for Llama text completion")
880879
}
881880
return LlamaRequestBody(
882881
prompt: prompt,

swift-bedrock-library/Sources/BedrockService/BedrockAuthentication+JWT.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
//===----------------------------------------------------------------------===//
1515

1616
import AWSSDKIdentity
17-
import BedrockTypes
1817
import Logging
1918

2019
#if canImport(FoundationEssentials)
@@ -54,7 +53,7 @@ extension BedrockAuthentication {
5453
}
5554

5655
guard (try? tokenString.write(to: tokenFileURL, atomically: true, encoding: .utf8)) != nil else {
57-
throw BedrockServiceError.authenticationFailed("Failed to write token to file")
56+
throw BedrockLibraryError.authenticationFailed("Failed to write token to file")
5857
}
5958

6059
// Create an identity resolver that uses the JWT token received from an Identity Provider
@@ -83,7 +82,7 @@ extension BedrockAuthentication {
8382

8483
} catch {
8584
logger.error("Failed to assume role using web identity token: \(error)")
86-
throw BedrockServiceError.authenticationFailed(
85+
throw BedrockLibraryError.authenticationFailed(
8786
"Failed to assume role using web identity token: \(error.localizedDescription)"
8887
)
8988
}

swift-bedrock-library/Sources/BedrockService/BedrockAuthentication.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
//===----------------------------------------------------------------------===//
1515

1616
import AWSSDKIdentity
17-
import BedrockTypes
1817
import Logging
1918
import SmithyIdentity
2019

swift-bedrock-library/Sources/BedrockTypes/BedrockModel.swift renamed to swift-bedrock-library/Sources/BedrockService/BedrockModel.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public struct BedrockModel: Hashable, Sendable, Equatable, RawRepresentable {
120120
/// - Returns: TextModality if the model supports text modality
121121
public func getTextModality() throws -> any TextModality {
122122
guard let textModality = modality as? any TextModality else {
123-
throw BedrockServiceError.invalidModality(
123+
throw BedrockLibraryError.invalidModality(
124124
self,
125125
modality,
126126
"Model \(id) does not support text generation"
@@ -141,7 +141,7 @@ public struct BedrockModel: Hashable, Sendable, Equatable, RawRepresentable {
141141
/// - Returns: TextModality if the model supports image modality
142142
public func getImageModality() throws -> any ImageModality {
143143
guard let imageModality = modality as? any ImageModality else {
144-
throw BedrockServiceError.invalidModality(
144+
throw BedrockLibraryError.invalidModality(
145145
self,
146146
modality,
147147
"Model \(id) does not support image generation"
@@ -160,7 +160,7 @@ public struct BedrockModel: Hashable, Sendable, Equatable, RawRepresentable {
160160
/// - Returns: TextToImageModality if the model supports image modality
161161
public func getTextToImageModality() throws -> any TextToImageModality {
162162
guard let textToImageModality = modality as? any TextToImageModality else {
163-
throw BedrockServiceError.invalidModality(
163+
throw BedrockLibraryError.invalidModality(
164164
self,
165165
modality,
166166
"Model \(id) does not support text to image generation"
@@ -179,7 +179,7 @@ public struct BedrockModel: Hashable, Sendable, Equatable, RawRepresentable {
179179
/// - Returns: ImageVariationModality if the model supports image modality
180180
public func getImageVariationModality() throws -> any ImageVariationModality {
181181
guard let modality = modality as? any ImageVariationModality else {
182-
throw BedrockServiceError.invalidModality(
182+
throw BedrockLibraryError.invalidModality(
183183
self,
184184
modality,
185185
"Model \(id) does not support image variation"
@@ -224,7 +224,7 @@ public struct BedrockModel: Hashable, Sendable, Equatable, RawRepresentable {
224224
/// - Returns: ConverseModality if the model supports text modality
225225
public func getConverseModality() throws -> any ConverseModality {
226226
guard let modality = modality as? any ConverseModality else {
227-
throw BedrockServiceError.invalidModality(
227+
throw BedrockLibraryError.invalidModality(
228228
self,
229229
modality,
230230
"Model \(id) does not support text generation"

swift-bedrock-library/Sources/BedrockService/BedrockService.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
@preconcurrency import AWSBedrockRuntime
1818
import AWSClientRuntime
1919
import AwsCommonRuntimeKit
20-
import BedrockTypes
2120
import Foundation
2221
import Logging
2322

@@ -158,15 +157,15 @@ public struct BedrockService: Sendable {
158157
case .crtError(let crtError):
159158
switch crtError.code {
160159
case 6153:
161-
throw BedrockServiceError.authenticationFailed(
160+
throw BedrockLibraryError.authenticationFailed(
162161
"No valid credentials found: \(crtError.message)"
163162
)
164163
case 6170:
165-
throw BedrockServiceError.authenticationFailed(
164+
throw BedrockLibraryError.authenticationFailed(
166165
"AWS SSO token expired: \(crtError.message)"
167166
)
168167
default:
169-
throw BedrockServiceError.authenticationFailed(
168+
throw BedrockLibraryError.authenticationFailed(
170169
"Authentication failed: \(crtError.message)"
171170
)
172171
}
@@ -180,7 +179,7 @@ public struct BedrockService: Sendable {
180179
// MARK: Public Methods
181180

182181
/// Lists all available foundation models from Amazon Bedrock
183-
/// - Throws: BedrockServiceError.invalidResponse
182+
/// - Throws: BedrockLibraryError.invalidResponse
184183
/// - Returns: An array of ModelSummary objects containing details about each available model
185184
public func listModels() async throws -> [ModelSummary] {
186185
logger.trace("Fetching foundation models")
@@ -190,7 +189,7 @@ public struct BedrockService: Sendable {
190189
)
191190
guard let models = response.modelSummaries else {
192191
logger.trace("Failed to extract modelSummaries from response")
193-
throw BedrockServiceError.invalidSDKResponse(
192+
throw BedrockLibraryError.invalidSDKResponse(
194193
"Something went wrong while extracting the modelSummaries from the response."
195194
)
196195
}

swift-bedrock-library/Sources/BedrockTypes/BedrockServiceError.swift renamed to swift-bedrock-library/Sources/BedrockService/BedrockServiceError.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
import Foundation
1717

18-
public enum BedrockServiceError: Error {
18+
//TODO: split in two structs : BedrockModelError and BedrockLibraryError
19+
public enum BedrockLibraryError: Error {
1920
case invalidParameter(ParameterName, String)
2021
case invalidModality(BedrockModel, Modality, String)
2122
case invalidPrompt(String)

swift-bedrock-library/Sources/BedrockService/Converse/BedrockService+Converse.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
@preconcurrency import AWSBedrockRuntime
1717
import AwsCommonRuntimeKit
18-
import BedrockTypes
1918
import Foundation
2019

2120
extension BedrockService {
@@ -30,11 +29,11 @@ extension BedrockService {
3029
/// - stopSequences: Optional array of sequences where generation should stop
3130
/// - systemPrompts: Optional array of system prompts to guide the conversation
3231
/// - tools: Optional array of tools the model can use
33-
/// - Throws: BedrockServiceError.notSupported for parameters or functionalities that are not supported
34-
/// BedrockServiceError.invalidParameter for invalid parameters
35-
/// BedrockServiceError.invalidPrompt if the prompt is empty or too long
36-
/// BedrockServiceError.invalidModality for invalid modality from the selected model
37-
/// BedrockServiceError.invalidSDKResponse if the response body is missing
32+
/// - Throws: BedrockLibraryError.notSupported for parameters or functionalities that are not supported
33+
/// BedrockLibraryError.invalidParameter for invalid parameters
34+
/// BedrockLibraryError.invalidPrompt if the prompt is empty or too long
35+
/// BedrockLibraryError.invalidModality for invalid modality from the selected model
36+
/// BedrockLibraryError.invalidSDKResponse if the response body is missing
3837
/// - Returns: A Message containing the model's response
3938
public func converse(
4039
with model: BedrockModel,
@@ -106,7 +105,7 @@ extension BedrockService {
106105
/// Use Converse API with the ConverseRequestBuilder
107106
/// - Parameters:
108107
/// - builder: ConverseRequestBuilder object
109-
/// - Throws: BedrockServiceError.invalidSDKResponse if the response body is missing
108+
/// - Throws: BedrockLibraryError.invalidSDKResponse if the response body is missing
110109
/// - Returns: A ConverseReply object
111110
public func converse(with builder: ConverseRequestBuilder) async throws -> ConverseReply {
112111
logger.trace("Conversing")

swift-bedrock-library/Sources/BedrockService/Converse/BedrockService+ConverseStreaming.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
@preconcurrency import AWSBedrockRuntime
1717
import AwsCommonRuntimeKit
18-
import BedrockTypes
18+
1919
import Foundation
2020

2121
extension BedrockService {
@@ -30,11 +30,11 @@ extension BedrockService {
3030
/// - stopSequences: Optional array of sequences where generation should stop
3131
/// - systemPrompts: Optional array of system prompts to guide the conversation
3232
/// - tools: Optional array of tools the model can use
33-
/// - Throws: BedrockServiceError.notSupported for parameters or functionalities that are not supported
34-
/// BedrockServiceError.invalidParameter for invalid parameters
35-
/// BedrockServiceError.invalidPrompt if the prompt is empty or too long
36-
/// BedrockServiceError.invalidModality for invalid modality from the selected model
37-
/// BedrockServiceError.invalidSDKResponse if the response body is missing
33+
/// - Throws: BedrockLibraryError.notSupported for parameters or functionalities that are not supported
34+
/// BedrockLibraryError.invalidParameter for invalid parameters
35+
/// BedrockLibraryError.invalidPrompt if the prompt is empty or too long
36+
/// BedrockLibraryError.invalidModality for invalid modality from the selected model
37+
/// BedrockLibraryError.invalidSDKResponse if the response body is missing
3838
/// - Returns: A stream of ConverseResponseStreaming objects
3939
public func converseStream(
4040
with model: BedrockModel,
@@ -50,7 +50,7 @@ extension BedrockService {
5050
) async throws -> AsyncThrowingStream<ConverseStreamElement, any Error> {
5151
do {
5252
guard model.hasConverseStreamingModality() else {
53-
throw BedrockServiceError.invalidModality(
53+
throw BedrockLibraryError.invalidModality(
5454
model,
5555
try model.getConverseModality(),
5656
"This model does not support converse streaming."
@@ -106,7 +106,7 @@ extension BedrockService {
106106
logger.trace("Received response", metadata: ["response": "\(response)"])
107107

108108
guard let sdkStream = response.stream else {
109-
throw BedrockServiceError.invalidSDKResponse(
109+
throw BedrockLibraryError.invalidSDKResponse(
110110
"The response stream is missing. This error should never happen."
111111
)
112112
}
@@ -140,7 +140,7 @@ extension BedrockService {
140140
/// Use Converse Stream API with the ConverseBuilder
141141
/// - Parameters:
142142
/// - builder: ConverseBuilder object
143-
/// - Throws: BedrockServiceError.invalidSDKResponse if the response body is missing
143+
/// - Throws: BedrockLibraryError.invalidSDKResponse if the response body is missing
144144
/// - Returns: A stream of ConverseResponseStreaming objects
145145
public func converseStream(
146146
with builder: ConverseRequestBuilder

swift-bedrock-library/Sources/BedrockTypes/Converse/ContentBlocks/Content.swift renamed to swift-bedrock-library/Sources/BedrockService/Converse/ContentBlocks/Content.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ public enum Content: Codable, CustomStringConvertible, Sendable {
4949
case .redactedcontent(let data):
5050
self = .encryptedReasoning(EncryptedReasoning(data))
5151
default:
52-
throw BedrockServiceError.notImplemented(
52+
throw BedrockLibraryError.notImplemented(
5353
"ReasoningContentBlock \(sdkReasoningBlock) is not implemented by BedrockService or not implemented by BedrockRuntimeClientTypes in case of `sdkUnknown`"
5454
)
5555
}
5656
default:
57-
throw BedrockServiceError.notImplemented(
57+
throw BedrockLibraryError.notImplemented(
5858
"ContentBlock \(sdkContentBlock) is not implemented by BedrockService or not implemented by BedrockRuntimeClientTypes in case of `sdkUnknown`"
5959
)
6060
}

0 commit comments

Comments
 (0)