Skip to content

Commit fe67046

Browse files
committed
Transform [Message] to History struct
1 parent cbd6b89 commit fe67046

File tree

11 files changed

+90
-35
lines changed

11 files changed

+90
-35
lines changed

Examples/ios-math-solver/Sources/MathSolverViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ final class MathSolverViewModel: ObservableObject, @unchecked Sendable {
113113

114114
// Make the streaming request
115115
Task {
116-
var messages: [Message] = []
116+
var messages: History = []
117117
do {
118118
// Process the stream
119119
let response = try await bedrockService.converseStream(with: promptBuilder)

Examples/text_chat/Sources/TextChat.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct TextChat {
4848
var request: ConverseRequestBuilder? = nil
4949

5050
// we keep track of the history of the conversation
51-
var history: [Message] = []
51+
var history: History = []
5252

5353
// while the user doesn't type "exit" or "quit"
5454
while true {

Examples/web-playground/backend/Sources/PlaygroundAPI/Types/Chat.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extension Message: @retroactive ResponseCodable {}
2121

2222
struct ChatInput: Codable {
2323
let prompt: String?
24-
let history: [Message]?
24+
let history: History?
2525
let imageFormat: ImageBlock.Format?
2626
let imageBytes: String?
2727
let documentName: String?

Sources/Converse/BedrockService+Converse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ extension BedrockService {
4242
/// - Returns: A Message containing the model's response
4343
public func converse(
4444
with model: BedrockModel,
45-
conversation: [Message],
45+
conversation: History,
4646
maxTokens: Int? = nil,
4747
temperature: Double? = nil,
4848
topP: Double? = nil,

Sources/Converse/BedrockService+ConverseStreaming.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ extension BedrockService {
4343
/// or the low-level stream provided by the AWS SDK.
4444
public func converseStream(
4545
with model: BedrockModel,
46-
conversation: [Message],
46+
conversation: History,
4747
maxTokens: Int? = nil,
4848
temperature: Double? = nil,
4949
topP: Double? = nil,

Sources/Converse/ContentBlocks/History.swift

Lines changed: 0 additions & 26 deletions
This file was deleted.

Sources/Converse/ConverseRequest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ import Foundation
2424

2525
public struct ConverseRequest {
2626
let model: BedrockModel
27-
let messages: [Message]
27+
let messages: History
2828
let inferenceConfig: InferenceConfig?
2929
let toolConfig: ToolConfig?
3030
let systemPrompts: [String]?
3131
let maxReasoningTokens: Int?
3232

3333
init(
3434
model: BedrockModel,
35-
messages: [Message] = [],
35+
messages: History,
3636
maxTokens: Int?,
3737
temperature: Double?,
3838
topP: Double?,

Sources/Converse/ConverseRequestBuilder.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public struct ConverseRequestBuilder: Sendable {
2424
public private(set) var model: BedrockModel
2525
private var parameters: ConverseParameters
2626

27-
public private(set) var history: [Message]
27+
public private(set) var history: History
2828
public private(set) var tools: [Tool]?
2929

3030
public private(set) var prompt: String?
@@ -88,8 +88,11 @@ public struct ConverseRequestBuilder: Sendable {
8888
// MARK - builder methods
8989

9090
// MARK - builder methods - history
91-
9291
public func withHistory(_ history: [Message]) throws -> ConverseRequestBuilder {
92+
try withHistory(.init(history))
93+
}
94+
95+
public func withHistory(_ history: History) throws -> ConverseRequestBuilder {
9396
if let lastMessage = history.last {
9497
guard lastMessage.role == .assistant else {
9598
throw BedrockLibraryError.ConverseRequestBuilder("Last message in history must be from assistant.")

Sources/Converse/History.swift

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Bedrock Library open source project
4+
//
5+
// Copyright (c) 2025 Amazon.com, Inc. or its affiliates
6+
// and the Swift Bedrock Library project authors
7+
// Licensed under Apache License v2.0
8+
//
9+
// See LICENSE.txt for license information
10+
// See CONTRIBUTORS.txt for the list of Swift Bedrock Library project authors
11+
//
12+
// SPDX-License-Identifier: Apache-2.0
13+
//
14+
//===----------------------------------------------------------------------===//
15+
16+
public struct History: Codable, Sendable {
17+
private var messages: [Message] = []
18+
19+
public init() {}
20+
21+
public init(_ message: Message) {
22+
messages = [message]
23+
}
24+
25+
public init(_ messages: [Message]) {
26+
self.messages = messages
27+
}
28+
29+
public init(_ messages: Message...) {
30+
self.messages = messages
31+
}
32+
33+
public mutating func append(_ message: Message) {
34+
messages.append(message)
35+
}
36+
37+
/// Essentials functions from Array that History needs
38+
public var count: Int { messages.count }
39+
40+
public subscript(index: Int) -> Message {
41+
messages[index]
42+
}
43+
44+
public var last : Message? {
45+
messages.last
46+
}
47+
48+
public static func + (lhs: History, rhs: [Message]) -> History {
49+
var result = lhs
50+
result.messages.append(contentsOf: rhs)
51+
return result
52+
}
53+
}
54+
55+
/// Collection
56+
extension History: Collection {
57+
public var startIndex: Int { messages.startIndex }
58+
public var endIndex: Int { messages.endIndex }
59+
public func index(after i: Int) -> Int { messages.index(after: i) }
60+
}
61+
62+
/// CustomString Convertible
63+
extension History: CustomStringConvertible {
64+
public var description: String {
65+
var result = "\(self.count) turns:\n"
66+
for message in self {
67+
result += "\(message)\n"
68+
}
69+
return result
70+
}
71+
}
72+
73+
/// ExpressibleByArrayLiteral
74+
extension History: ExpressibleByArrayLiteral {
75+
public init(arrayLiteral elements: Message...) {
76+
self.messages = elements
77+
}
78+
}
File renamed without changes.

0 commit comments

Comments
 (0)