Skip to content

Commit f0a7ece

Browse files
committed
feat: enhance AgentLayout and ChatProvider for improved message handling
- Updated AgentLayout to integrate a new ChatProvider for better message management and interaction. - Refactored message handling logic to utilize the ChatProvider, allowing for streamlined message processing and state management. - Introduced Equatable conformance to Source enum for improved comparison in SwiftUI. - Enhanced the initialization of AgentLayout to support additional configuration options, including callbacks for message changes. - Added comprehensive tests to validate the new ChatProvider functionality and ensure robust message handling across various scenarios.
1 parent 5144817 commit f0a7ece

File tree

9 files changed

+2091
-1011
lines changed

9 files changed

+2091
-1011
lines changed

Sources/Agent/AgentClient.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public enum AgentClientError: LocalizedError {
2424
}
2525
}
2626

27-
public enum Source: Identifiable, Sendable {
27+
public enum Source: Identifiable, Sendable, Equatable {
2828
case openAI(client: OpenAIClient, models: [Model])
2929
case openRouter(client: OpenRouterClient, models: [Model])
3030

@@ -35,6 +35,11 @@ public enum Source: Identifiable, Sendable {
3535
}
3636
}
3737

38+
public static func == (lhs: Source, rhs: Source) -> Bool {
39+
// Compare by id and models for SwiftUI change detection
40+
lhs.id == rhs.id && lhs.models == rhs.models
41+
}
42+
3843
public var displayName: String {
3944
switch self {
4045
case .openAI: return "OpenAI"

Sources/Agent/chat/openaiClient.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ public actor OpenAIClient: ChatClient {
8383

8484
public static let defaultBaseURL = URL(string: "https://api.openai.com/v1")!
8585

86-
public init(apiKey: String, baseURL: URL? = nil) {
86+
public init(
87+
apiKey: String = ProcessInfo.processInfo.environment["OPENAI_API_KEY"] ?? "",
88+
baseURL: URL? = URL(
89+
string: ProcessInfo.processInfo.environment["OPENAI_API_BASE_URL"] ?? "")
90+
) {
8791
self.apiKey = apiKey
8892
self.baseURL = baseURL ?? Self.defaultBaseURL
8993
}
@@ -165,10 +169,11 @@ public actor OpenAIClient: ChatClient {
165169
if let json = try? JSONDecoder().decode(StreamChunk.self, from: data),
166170
let choice = json.choices.first
167171
{
168-
continuation.yield(StreamDelta(
169-
delta: choice.delta,
170-
finishReason: choice.finishReason
171-
))
172+
continuation.yield(
173+
StreamDelta(
174+
delta: choice.delta,
175+
finishReason: choice.finishReason
176+
))
172177
}
173178
}
174179
}

0 commit comments

Comments
 (0)