Skip to content
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
8 changes: 8 additions & 0 deletions Sources/BedrockService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ public struct BedrockService: Sendable {
)
}
}
} else if let validationError = error as? AWSBedrockRuntime.ValidationException {
logger.trace("ValidationException while \(context)", metadata: ["error": "\(error)"])
let message = validationError.properties.message ?? "Validation error occurred"
if message.contains("Input is too long") {
throw BedrockLibraryError.inputTooLong(message)
} else {
throw BedrockLibraryError.invalid(message)
}
} else {
logger.trace("Error while \(context)", metadata: ["error": "\(error)"])
throw error
Expand Down
3 changes: 3 additions & 0 deletions Sources/BedrockServiceError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public enum BedrockLibraryError: Error {
case notSupported(String)
case notFound(String)
case authenticationFailed(String)
case inputTooLong(String)
case unknownError(String)

public var message: String {
Expand Down Expand Up @@ -86,6 +87,8 @@ public enum BedrockLibraryError: Error {
return "Not found: \(message)"
case .authenticationFailed(let message):
return "Authentication failed: \(message)"
case .inputTooLong(let message):
return "Input too long: \(message)"
case .unknownError(let message):
return "Unknown error: \(message)"
}
Expand Down
13 changes: 10 additions & 3 deletions Sources/Converse/ConverseRequestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,21 @@ public struct ConverseRequestBuilder: Sendable {
try withHistory(History(history))
}
public func withHistory(_ history: History) throws -> ConverseRequestBuilder {
if let lastMessage = history.last {

if history.count > 0,
let lastMessage = history.last
{
guard lastMessage.role == .assistant else {
throw BedrockLibraryError.ConverseRequestBuilder("Last message in history must be from assistant.")
}
}
if toolResult != nil {
if history.count > 0,
toolResult != nil
{
guard case .toolUse(_) = history.last?.content.last else {
throw BedrockLibraryError.invalidPrompt("Tool result is defined but last message is not tool use.")
throw BedrockLibraryError.ConverseRequestBuilder(
"Tool result is defined but last message is not tool use."
)
}
}
var copy = self
Expand Down
4 changes: 4 additions & 0 deletions Sources/Converse/History.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public struct History: Codable, Sendable {
self.messages = messages
}

public init(_ slide: Slice<History>) {
self.messages = Array(slide)
}

public mutating func append(_ message: Message) {
messages.append(message)
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/Converse/Streaming/ResponseMetaData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ public struct ResponseMetadata: Codable, Sendable {
self.totalTokens = sdkUsage.totalTokens ?? 0
}

let inputTokens: Int
let outputTokens: Int
let totalTokens: Int
public let inputTokens: Int
public let outputTokens: Int
public let totalTokens: Int
}

public struct Metrics: Codable, Sendable {
package init(from sdkMetrics: BedrockRuntimeClientTypes.ConverseStreamMetrics) throws {
self.latencyMs = Int(sdkMetrics.latencyMs ?? 0)
}
let latencyMs: Int
public let latencyMs: Int
}
}