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: 7 additions & 1 deletion Sources/Converse/ContentBlocks/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ public struct JSON: Codable, @unchecked Sendable { // FIXME: make Sendable
}

public init(from string: String) throws {
guard let data = string.data(using: .utf8) else {
var s: String!
if string.isEmpty {
s = "{}"
} else {
s = string
}
guard let data = s.data(using: .utf8) else {
throw BedrockLibraryError.encodingError("Could not encode String to Data")
}
try self.init(from: data)
Expand Down
7 changes: 3 additions & 4 deletions Sources/Converse/Streaming/ConverseReplyStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,9 @@ public struct ConverseReplyStream: Sendable {
// contentBlock[currentBlockId] = .reasoning(bufferReasoningData)
// bufferReasoningData = Data()
// }
if !state.bufferToolUse.isEmpty {
guard let toolUseStart = state.toolUseStart else {
throw BedrockLibraryError.invalidSDKType("Received a tool use delta without tool use start")
}

// when there is a toolUse, the bufferToolUse may be empty, when the model found out that there is not arguments to pass. Therefore, I just check the presence of the toolUseStart
if let toolUseStart = state.toolUseStart {
let json = try JSON(from: state.bufferToolUse)
state.lastContentBlock = (
state.currentBlockId, .toolUse(.init(id: toolUseStart.id, name: toolUseStart.name, input: json))
Expand Down
11 changes: 10 additions & 1 deletion Tests/Converse/JSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import Testing
@testable import BedrockService

// MARK: JSON
extension BedrockServiceTests {
@Suite("JSONTests")
struct JSONTests {

@Test("JSON getValue")
func jsonGetValue() async throws {
Expand Down Expand Up @@ -126,4 +127,12 @@ extension BedrockServiceTests {
let _ = try JSON(from: invalidJSONString)
}
}

@Test("Empty JSON")
func emptyJSON() async throws {
#expect(throws: Never.self) {
let json = try JSON(from: "")
#expect(json.getValue("nonExistentKey") == nil)
}
}
}