diff --git a/Sources/BedrockService.swift b/Sources/BedrockService.swift index 11776752..614f1c52 100644 --- a/Sources/BedrockService.swift +++ b/Sources/BedrockService.swift @@ -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 diff --git a/Sources/BedrockServiceError.swift b/Sources/BedrockServiceError.swift index 4de6af63..fff36e84 100644 --- a/Sources/BedrockServiceError.swift +++ b/Sources/BedrockServiceError.swift @@ -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 { @@ -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)" } diff --git a/Sources/Converse/ConverseRequestBuilder.swift b/Sources/Converse/ConverseRequestBuilder.swift index 75e69175..91887056 100644 --- a/Sources/Converse/ConverseRequestBuilder.swift +++ b/Sources/Converse/ConverseRequestBuilder.swift @@ -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 diff --git a/Sources/Converse/History.swift b/Sources/Converse/History.swift index ba388bf7..704c3f24 100644 --- a/Sources/Converse/History.swift +++ b/Sources/Converse/History.swift @@ -30,6 +30,10 @@ public struct History: Codable, Sendable { self.messages = messages } + public init(_ slide: Slice) { + self.messages = Array(slide) + } + public mutating func append(_ message: Message) { messages.append(message) } diff --git a/Sources/Converse/Streaming/ResponseMetaData.swift b/Sources/Converse/Streaming/ResponseMetaData.swift index fcc32cc5..8c5253d2 100644 --- a/Sources/Converse/Streaming/ResponseMetaData.swift +++ b/Sources/Converse/Streaming/ResponseMetaData.swift @@ -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 } }