Skip to content

Commit f610c88

Browse files
author
Atharva Vaidya
committed
fix: some cancer
1 parent 2fa7c00 commit f610c88

File tree

6 files changed

+39
-68
lines changed

6 files changed

+39
-68
lines changed

Sources/CompilerSwiftAI/Model Calling/ChatHistory.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,28 @@ actor ChatHistory {
2828
}
2929

3030
init(systemPrompt: String) {
31-
self._messages = [Message(role: .system, content: [.text(systemPrompt)])]
31+
self._messages = [Message(role: .system, content: systemPrompt)]
3232
}
3333

3434
func notifyMessageUpdate() {
3535
continuation?.yield(_messages)
3636
}
3737

3838
func addUserMessage(_ content: String) {
39-
_messages.append(Message(role: .user, content: [.text(content)]))
39+
_messages.append(Message(role: .user, content: content))
4040
notifyMessageUpdate()
4141
}
4242

4343
func addAssistantMessage(_ content: String) {
44-
_messages.append(Message(role: .assistant, content: [.text(content)]))
44+
_messages.append(Message(role: .assistant, content: content))
4545
notifyMessageUpdate()
4646
}
4747

4848
/// Start a new streaming response from the assistant
4949
@discardableResult
5050
func beginStreamingResponse() -> UUID {
5151
let id = UUID()
52-
let msg = Message(id: id, role: .assistant, content: [.text("")], state: .streaming(""))
52+
let msg = Message(id: id, role: .assistant, content: "", state: .streaming(""))
5353
_messages.append(msg)
5454
messageID = id
5555
notifyMessageUpdate()
@@ -66,7 +66,7 @@ actor ChatHistory {
6666
_messages[idx] = Message(
6767
id: old.id,
6868
role: old.role,
69-
content: [.text(partial)],
69+
content: partial,
7070
state: .streaming(partial)
7171
)
7272
notifyMessageUpdate()
@@ -81,7 +81,7 @@ actor ChatHistory {
8181
_messages[idx] = Message(
8282
id: id,
8383
role: .assistant,
84-
content: [.text(finalContent)],
84+
content: finalContent,
8585
state: .complete
8686
)
8787
messageID = nil

Sources/CompilerSwiftAI/Model Calling/CompilerClient+Streaming.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ extension CompilerClient {
3535
let lastUserMessageIndex = messages.lastIndex(where: { $0.role == .user }) {
3636
var modifiedMessages = messages
3737
let lastUserMessage = modifiedMessages[lastUserMessageIndex]
38-
let stateContent = "\(lastUserMessage.apiContent)\n\nThe current app state is: \(state)"
38+
let stateContent = "\(lastUserMessage.content)\n\nThe current app state is: \(state)"
3939
modifiedMessages[lastUserMessageIndex] = Message(
4040
id: lastUserMessage.id,
4141
role: .user,
42-
content: [.text(stateContent)]
42+
content: stateContent
4343
)
4444
finalMessages = modifiedMessages
4545
} else {
@@ -155,14 +155,14 @@ extension CompilerClient {
155155
state: state
156156
)
157157

158-
var streamingMessage = Message(role: .assistant, content: [.text("")])
158+
var streamingMessage = Message(role: .assistant, content: "")
159159
continuation.yield(streamingMessage)
160160

161161
for try await chunk in stream {
162162
streamingMessage = Message(
163163
id: streamingMessage.id,
164164
role: .assistant,
165-
content: [.text(streamingMessage.apiContent + chunk)]
165+
content: streamingMessage.content + chunk
166166
)
167167
continuation.yield(streamingMessage)
168168
}

Sources/CompilerSwiftAI/Model Calling/Message.swift

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,10 @@ import Combine
44
import SwiftUI
55

66
struct Message: Codable, Sendable, Identifiable, Equatable {
7-
let id: UUID
8-
let role: Role
9-
let content: [Content]
10-
var state: MessageState
11-
127
private enum CodingKeys: String, CodingKey {
138
case role, content
149
}
1510

16-
init(from decoder: Decoder) throws {
17-
let container = try decoder.container(keyedBy: CodingKeys.self)
18-
self.id = UUID() // Generate new UUID on decode since we don't send it
19-
self.role = try container.decode(Role.self, forKey: .role)
20-
self.content = try container.decode([Content].self, forKey: .content)
21-
self.state = .complete // Default to complete state when decoding
22-
}
23-
24-
func encode(to encoder: Encoder) throws {
25-
var container = encoder.container(keyedBy: CodingKeys.self)
26-
try container.encode(role, forKey: .role)
27-
try container.encode(content, forKey: .content)
28-
}
29-
3011
enum Role: String, Codable, Sendable {
3112
case system
3213
case user
@@ -98,26 +79,31 @@ struct Message: Codable, Sendable, Identifiable, Equatable {
9879
}
9980
}
10081

101-
init(role: Role, text: String) {
102-
self.id = UUID()
103-
self.role = role
104-
self.content = [Content(type: .text, content: .text(text))]
105-
self.state = .complete
106-
}
107-
108-
init(role: Role, content: [Content]) {
109-
self.id = UUID()
110-
self.role = role
111-
self.content = content
112-
self.state = .complete
113-
}
82+
let id: UUID
83+
let role: Role
84+
let content: String
85+
var state: MessageState
11486

115-
init(id: UUID = UUID(), role: Role, content: [Content], state: MessageState = .complete) {
87+
init(id: UUID = UUID(), role: Message.Role, content: String, state: Message.MessageState = .complete) {
11688
self.id = id
11789
self.role = role
11890
self.content = content
11991
self.state = state
12092
}
93+
94+
init(from decoder: Decoder) throws {
95+
let container = try decoder.container(keyedBy: CodingKeys.self)
96+
self.id = UUID() // Generate new UUID on decode since we don't send it
97+
self.role = try container.decode(Role.self, forKey: .role)
98+
self.content = try container.decode(String.self, forKey: .content)
99+
self.state = .complete // Default to complete state when decoding
100+
}
101+
102+
func encode(to encoder: Encoder) throws {
103+
var container = encoder.container(keyedBy: CodingKeys.self)
104+
try container.encode(role, forKey: .role)
105+
try container.encode(content, forKey: .content)
106+
}
121107
}
122108

123109
// Convenience extensions for creating messages

Sources/CompilerSwiftAI/Model Calling/ModelCall.swift

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,3 @@ struct CompletionResponse: Codable, Sendable {
8080
struct StreamChunk: Codable, Sendable {
8181
let content: String
8282
}
83-
84-
// Helper for encoding/decoding Message content arrays for the API
85-
extension Message {
86-
var apiContent: String {
87-
content.map { content in
88-
switch content.content {
89-
case .text(let text):
90-
return text
91-
case .image(let image):
92-
return """
93-
[Image: \(image.mimeType.rawValue),
94-
Base64: \(image.base64Data.prefix(20))...]
95-
"""
96-
}
97-
}.joined(separator: "\n")
98-
}
99-
}

Sources/CompilerSwiftAI/UI/Chat/ChatBubble.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct ChatBubble: View {
4949
case .streaming(let partial):
5050
return partial
5151
case .complete:
52-
return message.apiContent
52+
return message.content
5353
}
5454
}
5555

Sources/CompilerSwiftAI/UI/Chat/ChatView/ChatViewModel.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import SwiftUI
21
import Speech
2+
import SwiftUI
33
import Transcriber
44

55
import OSLog
66

77
// Example system prompt
88
let defaultSystemPrompt: String = """
9-
You are a helpful AI Assistant. Be direct, concise, and friendly.
10-
"""
9+
You are a helpful AI Assistant. Be direct, concise, and friendly. Always format your responses in valid Markdown.
10+
"""
1111

1212
@MainActor
1313
@Observable
1414
class ChatViewModel: Transcribable {
15-
1615
public var isRecording = false
1716
public var transcribedText = ""
1817
public var authStatus: SFSpeechRecognizerAuthorizationStatus = .notDetermined
@@ -33,8 +32,9 @@ class ChatViewModel: Transcribable {
3332
}
3433

3534
// MARK: - Properties
35+
3636
var errorMessage: String?
37-
private var _userInput = "" // Make private to control access
37+
private var _userInput = "" // Make private to control access
3838
var isStreaming = false
3939
var visibleMessageCount: Int = 15
4040

@@ -83,7 +83,7 @@ class ChatViewModel: Transcribable {
8383
self.chatHistory = ChatHistory(systemPrompt: systemPrompt)
8484

8585
// Start observing messages from chatHistory
86-
messageStreamTask = Task.detached { [weak self] in
86+
self.messageStreamTask = Task.detached { [weak self] in
8787
guard let self = self else { return }
8888
await self.observeMessageStream()
8989
}
@@ -125,6 +125,7 @@ class ChatViewModel: Transcribable {
125125
}
126126

127127
// MARK: - Observe ChatHistory
128+
128129
/// Continuously read `chatHistory.messagesStream` and publish changes to SwiftUI.
129130
private func observeMessageStream() async {
130131
let throttleInterval: TimeInterval = 0.15
@@ -200,7 +201,7 @@ class ChatViewModel: Transcribable {
200201
var chunkCount = 0
201202
for try await partialMessage in stream {
202203
chunkCount += 1
203-
accumulated = partialMessage.apiContent
204+
accumulated = partialMessage.content
204205

205206
// Log each chunk size
206207
self.logger.log("Chunk #\(chunkCount): partial content size=\(accumulated.count). Updating streaming message.")
@@ -224,6 +225,7 @@ class ChatViewModel: Transcribable {
224225
}
225226

226227
// MARK: - Clear Chat
228+
227229
func clearChat() {
228230
logger.log("clearChat called. Clearing chat history.")
229231
Task.detached {

0 commit comments

Comments
 (0)