@@ -49,6 +49,7 @@ public final class OpenClawChatViewModel {
4949 private( set) var timelineRevision : UInt64 = 0
5050 public private( set) var sessions : [ OpenClawChatSessionEntry ] = [ ]
5151 private let transport : any OpenClawChatTransport
52+ let haptics : OpenClawChatHaptics
5253 private var sessionDefaults : OpenClawChatSessionsDefaults ?
5354 private let prefersExplicitThinkingLevel : Bool
5455 private let onSessionChanged : ( @MainActor ( String ) -> Void ) ?
@@ -164,13 +165,15 @@ public final class OpenClawChatViewModel {
164165 public init (
165166 sessionKey : String ,
166167 transport : any OpenClawChatTransport ,
168+ haptics : OpenClawChatHaptics = OpenClawChatHaptics ( ) ,
167169 initialThinkingLevel : String ? = nil ,
168170 onSessionChanged : ( @MainActor ( String ) -> Void ) ? = nil ,
169171 onThinkingLevelChanged : ( @MainActor @Sendable ( String ) -> Void ) ? = nil ,
170172 diagnosticsLog : ( @MainActor @Sendable ( String ) -> Void ) ? = nil )
171173 {
172174 self . sessionKey = sessionKey
173175 self . transport = transport
176+ self . haptics = haptics
174177 let normalizedThinkingLevel = Self . normalizedThinkingLevel ( initialThinkingLevel )
175178 let initialResolvedThinkingLevel = normalizedThinkingLevel ?? " off "
176179 self . thinkingLevel = initialResolvedThinkingLevel
@@ -571,8 +574,8 @@ public final class OpenClawChatViewModel {
571574 await self . refreshHistoryAfterRun ( historyRequest : context )
572575 await self . pollHealthIfNeeded ( force : true , sessionSnapshot : context . session )
573576 guard self . isCurrentSession ( context . session ) else { return }
574- if self . hasAssistantMessageAfterLatestUser ( ) {
575- self . clearPendingRuns ( reason : nil )
577+ if let hapticEvent = self . assistantHapticEventAfterLatestUser ( ) {
578+ self . clearPendingRuns ( reason : nil , hapticEvent : hapticEvent )
576579 self . pendingToolCallsById = [ : ]
577580 self . updateStreamingAssistantText ( nil )
578581 }
@@ -1491,6 +1494,9 @@ public final class OpenClawChatViewModel {
14911494 self . logDiagnostic (
14921495 " chat.ui transport send accepted sessionKey= \( sessionKey ) "
14931496 + " localRunId= \( runId ) remoteRunId= \( response . runId ) " )
1497+ if response . status != " error " , response . status != " timeout " {
1498+ self . haptics . perform ( . messageSent )
1499+ }
14941500 var reusedRunAlreadyFinal = false
14951501 if response . runId != runId {
14961502 let pendingUserMessageID = self . pendingLocalUserEchoMessageIDsByRunID . removeValue ( forKey : runId )
@@ -1509,7 +1515,7 @@ public final class OpenClawChatViewModel {
15091515 self . rescopeProvisionalFinalMessages ( runId : response . runId , scope : remoteRunScope )
15101516 reusedRunAlreadyFinal = self . hasRecordedFinalMessage ( runId : response . runId )
15111517 if reusedRunAlreadyFinal {
1512- self . clearPendingRun ( response . runId )
1518+ self . clearPendingRun ( response . runId , hapticEvent : . runCompleted )
15131519 self . pendingToolCallsById = [ : ]
15141520 self . updateStreamingAssistantText ( nil )
15151521 } else {
@@ -1545,8 +1551,8 @@ public final class OpenClawChatViewModel {
15451551 guard self . isCurrentSession ( sessionSnapshot ) else { return }
15461552 self . removePendingLocalUserEcho ( for : runId )
15471553 self . runMessageScopesByRunID . removeValue ( forKey : runId )
1548- self . clearPendingRun ( runId )
15491554 self . errorText = error . localizedDescription
1555+ self . clearPendingRun ( runId , hapticEvent : . runFailed )
15501556 self . logDiagnostic (
15511557 " chat.ui send failed sessionKey= \( sessionKey ) "
15521558 + " localRunId= \( runId ) error= \( error . localizedDescription ) " )
@@ -2227,10 +2233,15 @@ public final class OpenClawChatViewModel {
22272233 if chat . state == " error " {
22282234 self . errorText = chat . errorMessage ?? " Chat failed "
22292235 }
2236+ let hapticEvent : OpenClawChatHaptics . Event ? = switch chat . state {
2237+ case " final " : . runCompleted
2238+ case " error " : . runFailed
2239+ default : nil
2240+ }
22302241 if let runId = chat . runId {
2231- self . clearPendingRun ( runId )
2242+ self . clearPendingRun ( runId , hapticEvent : hapticEvent )
22322243 } else if self . pendingRuns . count <= 1 {
2233- self . clearPendingRuns ( reason : nil )
2244+ self . clearPendingRuns ( reason : nil , hapticEvent : hapticEvent )
22342245 }
22352246 self . pendingToolCallsById = [ : ]
22362247 self . updateStreamingAssistantText ( nil )
@@ -2371,7 +2382,9 @@ public final class OpenClawChatViewModel {
23712382 self . errorText = Self . agentLifecycleErrorMessage ( evt , aborted : aborted )
23722383 }
23732384 if isPendingRun {
2374- self . clearPendingRun ( evt . runId )
2385+ self . clearPendingRun (
2386+ evt . runId ,
2387+ hapticEvent : isFailure || aborted ? . runFailed : . runCompleted )
23752388 }
23762389 self . pendingToolCallsById = [ : ]
23772390 self . updateStreamingAssistantText ( nil )
@@ -2411,7 +2424,7 @@ public final class OpenClawChatViewModel {
24112424 }
24122425
24132426 private func finishPendingRunAfterTerminalOkSendAck ( _ response : OpenClawChatSendResponse ) {
2414- self . clearPendingRun ( response . runId )
2427+ self . clearPendingRun ( response . runId , hapticEvent : . runCompleted )
24152428 self . pendingToolCallsById = [ : ]
24162429 self . updateStreamingAssistantText ( nil )
24172430 self . logDiagnostic (
@@ -2423,20 +2436,20 @@ public final class OpenClawChatViewModel {
24232436 switch response . status {
24242437 case " timeout " :
24252438 self . removePendingLocalUserEcho ( for : response . runId )
2426- self . clearPendingRun ( response . runId )
24272439 self . pendingToolCallsById = [ : ]
24282440 self . updateStreamingAssistantText ( nil )
24292441 self . errorText = " Chat failed before the run started; try again. "
2442+ self . clearPendingRun ( response . runId , hapticEvent : . runFailed )
24302443 self . logDiagnostic (
24312444 " chat.ui send terminal ack sessionKey= \( self . sessionKey ) "
24322445 + " runId= \( response . runId ) status=timeout " )
24332446 return true
24342447 case " error " :
24352448 self . removePendingLocalUserEcho ( for : response . runId )
2436- self . clearPendingRun ( response . runId )
24372449 self . pendingToolCallsById = [ : ]
24382450 self . updateStreamingAssistantText ( nil )
24392451 self . errorText = " Chat failed before the run started; try again. "
2452+ self . clearPendingRun ( response . runId , hapticEvent : . runFailed )
24402453 self . logDiagnostic (
24412454 " chat.ui send terminal ack sessionKey= \( self . sessionKey ) "
24422455 + " runId= \( response . runId ) status=error " )
@@ -2519,17 +2532,13 @@ public final class OpenClawChatViewModel {
25192532
25202533 @ discardableResult
25212534 private func clearPendingRunIfAssistantMessagePresent ( runId : String , after timestamp : Double ) -> Bool {
2522- guard self . hasAssistantMessage ( after : timestamp ) else { return false }
2523- self . clearPendingRun ( runId )
2535+ guard let hapticEvent = self . assistantHapticEvent ( after : timestamp ) else { return false }
2536+ self . clearPendingRun ( runId , hapticEvent : hapticEvent )
25242537 self . pendingToolCallsById = [ : ]
25252538 self . updateStreamingAssistantText ( nil )
25262539 return true
25272540 }
25282541
2529- private func hasAssistantMessageAfterLatestUser ( ) -> Bool {
2530- Self . hasAssistantMessageAfterLatestUser ( in : self . messages )
2531- }
2532-
25332542 private static func hasUnansweredLatestUser ( in messages : [ OpenClawChatMessage ] ) -> Bool {
25342543 self . latestUserTurn ( in : messages ) != nil && ! self . hasAssistantMessageAfterLatestUser ( in : messages )
25352544 }
@@ -2637,16 +2646,31 @@ public final class OpenClawChatViewModel {
26372646 }
26382647 }
26392648
2640- private func hasAssistantMessage ( after timestamp : Double ) -> Bool {
2641- self . messages . contains { message in
2642- guard message . role . trimmingCharacters ( in : . whitespacesAndNewlines ) . lowercased ( ) == " assistant " else {
2643- return false
2644- }
2645- guard ( message . timestamp ?? 0 ) >= timestamp else { return false }
2646- let text = message . content . compactMap ( \. text ) . joined ( separator : " \n " )
2647- . trimmingCharacters ( in : . whitespacesAndNewlines )
2648- return ! text . isEmpty || message . errorMessage != nil
2649+ private static func assistantHapticEvent (
2650+ for message : OpenClawChatMessage ) -> OpenClawChatHaptics . Event ?
2651+ {
2652+ guard message . role . trimmingCharacters ( in : . whitespacesAndNewlines ) . lowercased ( ) == " assistant " else {
2653+ return nil
26492654 }
2655+ let text = message . content . compactMap ( \. text ) . joined ( separator : " \n " )
2656+ . trimmingCharacters ( in : . whitespacesAndNewlines )
2657+ guard ! text . isEmpty || message . errorMessage != nil else { return nil }
2658+ let stopReason = message . stopReason ? . trimmingCharacters ( in : . whitespacesAndNewlines ) . lowercased ( )
2659+ return stopReason == " error " || stopReason == " aborted " ? . runFailed : . runCompleted
2660+ }
2661+
2662+ private func assistantHapticEventAfterLatestUser ( ) -> OpenClawChatHaptics . Event ? {
2663+ guard let userIndex = self . messages . lastIndex ( where : { $0 . role . lowercased ( ) == " user " } ) else { return nil }
2664+ let nextIndex = self . messages . index ( after : userIndex )
2665+ guard nextIndex < self . messages . endIndex else { return nil }
2666+ return self . messages [ nextIndex ... ] . reversed ( ) . lazy . compactMap ( Self . assistantHapticEvent ) . first
2667+ }
2668+
2669+ private func assistantHapticEvent ( after timestamp : Double ) -> OpenClawChatHaptics . Event ? {
2670+ self . messages . reversed ( ) . lazy . compactMap { message in
2671+ guard ( message . timestamp ?? 0 ) >= timestamp else { return nil }
2672+ return Self . assistantHapticEvent ( for : message )
2673+ } . first
26502674 }
26512675
26522676 @ discardableResult
@@ -2675,13 +2699,16 @@ public final class OpenClawChatViewModel {
26752699 self . logDiagnostic (
26762700 " chat.ui pending timeout sessionKey= \( self . sessionKey ) "
26772701 + " runId= \( runId ) " )
2678- self . clearPendingRun ( runId )
26792702 self . errorText = " Timed out waiting for a reply; try again or refresh. "
2703+ self . clearPendingRun ( runId , hapticEvent : . runFailed )
26802704 }
26812705 }
26822706 }
26832707
2684- private func clearPendingRun ( _ runId : String ) {
2708+ private func clearPendingRun (
2709+ _ runId : String ,
2710+ hapticEvent : OpenClawChatHaptics . Event ? = nil )
2711+ {
26852712 let wasPending = self . pendingRuns . contains ( runId )
26862713 self . pendingRuns . remove ( runId )
26872714 self . pendingLocalUserEchoMessageIDsByRunID [ runId ] = nil
@@ -2691,17 +2718,26 @@ public final class OpenClawChatViewModel {
26912718 self . logDiagnostic (
26922719 " chat.ui pending cleared sessionKey= \( self . sessionKey ) "
26932720 + " runId= \( runId ) " )
2721+ if self . pendingRuns . isEmpty , let hapticEvent {
2722+ self . haptics . perform ( hapticEvent )
2723+ }
26942724 }
26952725 }
26962726
2697- private func clearPendingRuns ( reason : String ? ) {
2727+ private func clearPendingRuns (
2728+ reason : String ? ,
2729+ hapticEvent : OpenClawChatHaptics . Event ? = nil )
2730+ {
26982731 let runIds = Array ( pendingRuns )
26992732 for runId in self . pendingRuns {
27002733 self . pendingRunTimeoutTasks [ runId ] ? . cancel ( )
27012734 }
27022735 self . pendingRunTimeoutTasks . removeAll ( )
27032736 self . pendingRuns . removeAll ( )
27042737 self . pendingLocalUserEchoMessageIDsByRunID . removeAll ( )
2738+ if ! runIds . isEmpty , let hapticEvent {
2739+ self . haptics . perform ( hapticEvent )
2740+ }
27052741 if let reason , ! reason . isEmpty {
27062742 self . errorText = reason
27072743 for runId in runIds {
0 commit comments