@@ -9,6 +9,7 @@ final class ChatViewModel {
99 var inputText = " "
1010 var streaming = false
1111 var streamingText = " "
12+ var error : String ?
1213
1314 private let store : Store
1415 private let phoropterTrail : [ String ]
@@ -24,12 +25,13 @@ final class ChatViewModel {
2425 }
2526
2627 /// Initiates the conversation with the phoropter trajectory as context.
28+ /// Called from ChatView.onAppear so it fires when the view is actually visible.
2729 func initiateIfNeeded( ) {
28- guard !hasInitiated, messages. isEmpty else {
29- hasInitiated = true
30- return
31- }
30+ guard !hasInitiated else { return }
3231 hasInitiated = true
32+ guard messages. isEmpty else { return }
33+
34+ print ( " ChatViewModel: Initiating with trail: \( phoropterTrail) " )
3335 streamResponse ( chatLog: LightwardAPI . buildTransitionChatLog ( trajectory: phoropterTrail) )
3436 }
3537
@@ -38,6 +40,7 @@ final class ChatViewModel {
3840 guard !text. isEmpty, !streaming else { return }
3941
4042 inputText = " "
43+ error = nil
4144
4245 let userMessage = ChatMessage ( role: . user, text: text)
4346 messages. append ( userMessage)
@@ -51,42 +54,46 @@ final class ChatViewModel {
5154 currentTask? . cancel ( )
5255 streaming = true
5356 streamingText = " "
57+ error = nil
5458
5559 // Add placeholder assistant message
5660 let placeholder = ChatMessage ( role: . assistant, text: " " )
5761 messages. append ( placeholder)
5862
59- currentTask = Task { @ MainActor in
63+ currentTask = Task {
6064 do {
65+ print ( " ChatViewModel: Starting stream request... " )
6166 for try await event in LightwardAPI . stream ( chatLog: chatLog) {
6267 switch event {
6368 case . text( let chunk) :
6469 streamingText += chunk
65- // Update the last message in place
6670 if let last = messages. indices. last {
6771 messages [ last] . text = streamingText
6872 }
6973
7074 case . started:
71- break
75+ print ( " ChatViewModel: Stream started " )
7276
7377 case . finished:
74- break
78+ print ( " ChatViewModel: Stream finished " )
7579 }
7680 }
7781
7882 streaming = false
7983 // Save the completed message
80- if let last = messages. last {
84+ if let last = messages. last, !last . text . isEmpty {
8185 store. appendMessage ( last)
8286 }
87+ print ( " ChatViewModel: Stream complete, message length: \( messages. last? . text. count ?? 0 ) " )
8388 } catch {
89+ print ( " ChatViewModel: Stream error: \( error) " )
8490 streaming = false
8591 if !Task. isCancelled {
86- // Remove placeholder on error
92+ // Remove empty placeholder on error
8793 if messages. last? . text. isEmpty == true {
8894 messages. removeLast ( )
8995 }
96+ self . error = error. localizedDescription
9097 }
9198 }
9299 }
0 commit comments