Skip to content

Commit 3d05a53

Browse files
committed
minor API changes
1 parent d1ada32 commit 3d05a53

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ extension BedrockService {
124124
let reply = ConverseReplyStream(sdkStream)
125125

126126
// this time, a different stream is created from the previous one, this one has the following elements
127-
// - content segment: this contains a ContentSegment, an enum which can currently only be a .text(Int, String),
127+
// - content segment: this contains a ContentSegment, an enum which can be a .text(Int, String),
128128
// the integer is the id for the content block that the content segment is a part of,
129129
// the String is the part of text that is send from the model.
130130
// - content block complete: this includes the id of the completed content block and the complete content block itself

swift-bedrock-library/Sources/BedrockTypes/Converse/ConverseReply.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public struct ConverseReply: Codable, CustomStringConvertible {
3232
}
3333

3434
// MARK: Initializers
35-
35+
public init(withMessage msg: Message) throws {
36+
try self.init([msg])
37+
}
3638
public init(_ history: History) throws {
3739
guard let lastMessage = history.last else {
3840
throw BedrockServiceError.invalidConverseReply("The provided history is not allowed to be empty.")

swift-bedrock-library/Sources/BedrockTypes/Converse/Message.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,36 @@ public struct Message: Codable, CustomStringConvertible, Sendable {
9797

9898
// MARK - public functions
9999

100+
public func hasToolUse() -> Bool {
101+
return content.contains { $0.isToolUse() }
102+
}
103+
public func getToolUse() -> ToolUseBlock? {
104+
let content = content.first(where: { $0.isToolUse() })
105+
if case .toolUse(let block) = content {
106+
return block
107+
} else {
108+
return nil
109+
}
110+
}
111+
public func hasTextContent() -> Bool {
112+
return content.contains { $0.isText() }
113+
}
114+
public func hasImageContent() -> Bool {
115+
return content.contains { $0.isImage() }
116+
}
117+
public func hasVideoContent() -> Bool {
118+
return content.contains { $0.isVideo() }
119+
}
120+
public func hasReasoningContent() -> Bool {
121+
return content.contains { $0.isReasoning() }
122+
}
123+
public func hasEncryptedReasoningContent() -> Bool {
124+
return content.contains { $0.isEncryptedReasoning() }
125+
}
126+
public func hasToolResult() -> Bool {
127+
return content.contains { $0.isToolResult() }
128+
}
129+
100130
public func getSDKMessage() throws -> BedrockRuntimeClientTypes.Message {
101131
let contentBlocks: [BedrockRuntimeClientTypes.ContentBlock] = try content.map {
102132
content -> BedrockRuntimeClientTypes.ContentBlock in

swift-bedrock-library/Sources/BedrockTypes/Converse/Streaming/ConverseReplyStream.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ package struct ConverseReplyStream {
3030
do {
3131
for try await output in inputStream {
3232
switch output {
33+
case .messagestart(_):
34+
continuation.yield(.messageStart)
3335
case .contentblockstart(let event):
3436
guard let index = event.contentBlockIndex else {
3537
throw BedrockServiceError.invalidSDKType(
@@ -51,7 +53,9 @@ package struct ConverseReplyStream {
5153
)
5254
}
5355
if !indexes.contains(index) {
56+
//some models do not send ContentBlockStart before ContentBlockDelta
5457
indexes.append(index)
58+
// continuation.yield(.messageStart)
5559
}
5660
guard let delta = event.delta else {
5761
throw BedrockServiceError.invalidSDKType("No delta found in ContentBlockDelta")

swift-bedrock-library/Sources/BedrockTypes/Converse/Streaming/ConverseStreamElement.swift

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

1616
public enum ConverseStreamElement: Sendable {
17+
case messageStart
1718
case contentSegment(ContentSegment)
1819
case contentBlockComplete(Int, Content)
1920
case messageComplete(Message)

0 commit comments

Comments
 (0)