agentix-cli 0.192.0 → 0.194.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/README.md +3 -3
  2. package/apps/mac-helper/Sources/AgentXHelper/Presence.swift +70 -6
  3. package/apps/mac-helper/Sources/AgentXHelper/PresenceTag.swift +42 -5
  4. package/apps/mac-helper/Sources/AgentXHelper/main.swift +2 -1
  5. package/apps/mac-voice/Sources/AgentXVoice/AgentClient.swift +107 -4
  6. package/apps/mac-voice/Sources/AgentXVoice/Config.swift +0 -9
  7. package/apps/mac-voice/Sources/AgentXVoice/HistoryWindow.swift +3 -2
  8. package/apps/mac-voice/Sources/AgentXVoice/OpenRequests.swift +276 -8
  9. package/apps/mac-voice/Sources/AgentXVoice/RequestsWindow.swift +357 -26
  10. package/apps/mac-voice/Sources/AgentXVoice/SettingsWindow.swift +27 -0
  11. package/apps/mac-voice/Sources/AgentXVoice/StatusMenu.swift +17 -2
  12. package/apps/mac-voice/Sources/AgentXVoice/VoiceSettings.swift +23 -1
  13. package/apps/mac-voice/Sources/AgentXVoice/main.swift +335 -30
  14. package/dist/{App-N6WWCFBO.js → App-HBM52GXI.js} +2 -2
  15. package/dist/{ChatApp-JMSINKZP.js → ChatApp-N4P7M2TD.js} +2 -2
  16. package/dist/{absorb-eval-4LJ4R7GN.js → absorb-eval-HRWWPKT3.js} +2 -2
  17. package/dist/{adapter-2Y3PQ5IZ.js → adapter-X6T3YBUB.js} +2 -2
  18. package/dist/{agent-RSOKWOZ2.js → agent-3B4RMEDI.js} +2 -2
  19. package/dist/{board-dashboard-M3HOVCLM.js → board-dashboard-YCRGMNAT.js} +3668 -7064
  20. package/dist/board-dashboard-YCRGMNAT.js.map +1 -0
  21. package/dist/{chunk-4IHQSNE3.js → chunk-2GCPSAXQ.js} +2 -2
  22. package/dist/{chunk-PNWXXDTE.js → chunk-2KYGPVTX.js} +2 -2
  23. package/dist/chunk-3FZ6WHKX.js +2 -0
  24. package/dist/chunk-3FZ6WHKX.js.map +1 -0
  25. package/dist/{chunk-RZS4BHCW.js → chunk-6NQ7KYJH.js} +2 -2
  26. package/dist/{chunk-BSMT7MK4.js → chunk-7L54Z4EH.js} +2 -2
  27. package/dist/{chunk-WWFMIAYZ.js → chunk-BOCLWYWL.js} +2 -2
  28. package/dist/chunk-F2FJXKDG.js +7 -0
  29. package/dist/{chunk-Y7HU4GHT.js.map → chunk-F2FJXKDG.js.map} +1 -1
  30. package/dist/{chunk-OPBPH7IB.js → chunk-F3CMNSON.js} +2 -2
  31. package/dist/{chunk-6FHKA62Y.js → chunk-FO762MEC.js} +2 -2
  32. package/dist/{chunk-5XGQE2SN.js → chunk-HNZEXUNM.js} +2 -2
  33. package/dist/{chunk-RZPQML3W.js → chunk-HOZZUN47.js} +2 -2
  34. package/dist/chunk-HPRLRD4P.js +6 -0
  35. package/dist/chunk-HPRLRD4P.js.map +1 -0
  36. package/dist/chunk-I45E5TLK.js +934 -0
  37. package/dist/chunk-I45E5TLK.js.map +1 -0
  38. package/dist/{chunk-VGQDLAX3.js → chunk-K2BEV3BD.js} +2 -2
  39. package/dist/chunk-KE6BF4ZJ.js +2 -0
  40. package/dist/chunk-KE6BF4ZJ.js.map +1 -0
  41. package/dist/{chunk-2HPVL6UM.js → chunk-KXJRXRSD.js} +2 -2
  42. package/dist/{chunk-HNTWVZMH.js → chunk-LQIF2OHS.js} +2 -2
  43. package/dist/chunk-M2E3CNFS.js +38 -0
  44. package/dist/chunk-M2E3CNFS.js.map +1 -0
  45. package/dist/chunk-MB5CBY2C.js +189 -0
  46. package/dist/chunk-MB5CBY2C.js.map +1 -0
  47. package/dist/{chunk-QQTSGXFB.js → chunk-MC3ZZA5G.js} +2 -2
  48. package/dist/{chunk-2FCYIEKT.js → chunk-OEG476JY.js} +94 -91
  49. package/dist/chunk-OEG476JY.js.map +1 -0
  50. package/dist/chunk-P6WSD2UR.js +116 -0
  51. package/dist/chunk-P6WSD2UR.js.map +1 -0
  52. package/dist/{chunk-7NXFW4LM.js → chunk-PMSL4A5Y.js} +3 -3
  53. package/dist/{chunk-KZPZ2O2F.js → chunk-PO2SORKY.js} +36 -36
  54. package/dist/chunk-PO2SORKY.js.map +1 -0
  55. package/dist/{chunk-6DGMUN2Q.js → chunk-PX7PWXBI.js} +2 -2
  56. package/dist/{chunk-UUQ2AHUL.js → chunk-RW5FKJRE.js} +2 -2
  57. package/dist/{chunk-BZ2HOV4M.js → chunk-S42KBXNO.js} +332 -317
  58. package/dist/chunk-S42KBXNO.js.map +1 -0
  59. package/dist/{chunk-54LKUDJZ.js → chunk-UOIWIVU3.js} +2 -2
  60. package/dist/{chunk-JW3ZHQ7U.js → chunk-VX4PSOKU.js} +2 -2
  61. package/dist/{chunk-VG3NUHJX.js → chunk-XH36J3Z2.js} +2 -2
  62. package/dist/{chunk-FFPF7SAD.js → chunk-ZALVAXQ4.js} +2 -2
  63. package/dist/cli.js +1 -1
  64. package/dist/{client-CFS5VY26.js → client-YN3T56NM.js} +2 -2
  65. package/dist/{config-4R6DCRCN.js → config-G7T3TMWE.js} +2 -2
  66. package/dist/{config-mutate-EG5PS7KW.js → config-mutate-6CRKZQVQ.js} +2 -2
  67. package/dist/config-mutator-7VS3J5BY.js +2 -0
  68. package/dist/{contributions-G4FZ2PKZ.js → contributions-6VZ4OXJ5.js} +2 -2
  69. package/dist/{doctor-4VQ5RFWH.js → doctor-GFZZTK7C.js} +2 -2
  70. package/dist/{follow-up-tool-JJSFOO5D.js → follow-up-tool-NVP56W7E.js} +2 -2
  71. package/dist/{hub-Z3KDKP7M.js → hub-B434CABZ.js} +2 -2
  72. package/dist/index.d.ts +119 -7
  73. package/dist/index.js +1 -1
  74. package/dist/index.js.map +1 -1
  75. package/dist/{program-6UF4K6CK.js → program-IDRN5TAM.js} +255 -255
  76. package/dist/program-IDRN5TAM.js.map +1 -0
  77. package/dist/{promote-WJ5HXGJG.js → promote-3P6PGJOW.js} +2 -2
  78. package/dist/{query-7JPB2UBE.js → query-E4V7TS5S.js} +2 -2
  79. package/dist/query-runs-AENEZOM6.js +2 -0
  80. package/dist/{query-settings-O53GJRCB.js → query-settings-C5UBKJMN.js} +2 -2
  81. package/dist/registry-IG324PR3.js +2 -0
  82. package/dist/{request-planner-25WOUCP7.js → request-planner-G6IAUMSE.js} +2 -2
  83. package/dist/{review-candidates-KU3B6T5E.js → review-candidates-ZL2SJXWC.js} +2 -2
  84. package/dist/{routing-SJQB4SJM.js → routing-2IBIZYQU.js} +2 -2
  85. package/dist/schedule-tool-QCTT2R3B.js +2 -0
  86. package/dist/{scheduler-W3TMCBUH.js → scheduler-ZGB7IKZQ.js} +2 -2
  87. package/dist/{seat-NDZLFK4N.js → seat-CFY7LMPS.js} +2 -2
  88. package/dist/{select-3D3GZOBS.js → select-MU52ACKG.js} +2 -2
  89. package/dist/self-ELWZIMVY.js +2 -0
  90. package/dist/{set-enabled-ZUTWWK5S.js → set-enabled-XRNSYEI3.js} +2 -2
  91. package/dist/{sprint-tool-CAARUDXM.js → sprint-tool-E4MABD2N.js} +2 -2
  92. package/dist/{tool-3UIRQJZF.js → tool-V5Z4KWK7.js} +2 -2
  93. package/dist/{tool-HK7VOPB3.js → tool-W6M7GYRZ.js} +2 -2
  94. package/dist/{tool-GKPYCNZ3.js → tool-ZYVT4VUU.js} +2 -2
  95. package/dist/{wiki-F4YT226F.js → wiki-VP6XRWIS.js} +2 -2
  96. package/dist/wiki-enrich-LUFXPUYY.js +2 -0
  97. package/dist/wiki-enrich-LUFXPUYY.js.map +1 -0
  98. package/package.json +1 -1
  99. package/dist/board-dashboard-M3HOVCLM.js.map +0 -1
  100. package/dist/chunk-2FCYIEKT.js.map +0 -1
  101. package/dist/chunk-BZ2HOV4M.js.map +0 -1
  102. package/dist/chunk-CXAKD7XW.js +0 -114
  103. package/dist/chunk-CXAKD7XW.js.map +0 -1
  104. package/dist/chunk-CZMVJP7O.js +0 -2
  105. package/dist/chunk-CZMVJP7O.js.map +0 -1
  106. package/dist/chunk-E3UQEMNV.js +0 -188
  107. package/dist/chunk-E3UQEMNV.js.map +0 -1
  108. package/dist/chunk-JNJVRSOL.js +0 -36
  109. package/dist/chunk-JNJVRSOL.js.map +0 -1
  110. package/dist/chunk-KZPZ2O2F.js.map +0 -1
  111. package/dist/chunk-ORBEL4I4.js +0 -888
  112. package/dist/chunk-ORBEL4I4.js.map +0 -1
  113. package/dist/chunk-Y7HU4GHT.js +0 -7
  114. package/dist/config-mutator-YDVTCBQZ.js +0 -2
  115. package/dist/program-6UF4K6CK.js.map +0 -1
  116. package/dist/query-runs-VXS572PQ.js +0 -6
  117. package/dist/query-runs-VXS572PQ.js.map +0 -1
  118. package/dist/registry-WNKSI445.js +0 -2
  119. package/dist/schedule-tool-T6FHG77E.js +0 -2
  120. package/dist/self-SO6GCVYD.js +0 -2
  121. package/dist/wiki-enrich-E6YYAI2T.js +0 -2
  122. /package/dist/{App-N6WWCFBO.js.map → App-HBM52GXI.js.map} +0 -0
  123. /package/dist/{ChatApp-JMSINKZP.js.map → ChatApp-N4P7M2TD.js.map} +0 -0
  124. /package/dist/{absorb-eval-4LJ4R7GN.js.map → absorb-eval-HRWWPKT3.js.map} +0 -0
  125. /package/dist/{adapter-2Y3PQ5IZ.js.map → adapter-X6T3YBUB.js.map} +0 -0
  126. /package/dist/{agent-RSOKWOZ2.js.map → agent-3B4RMEDI.js.map} +0 -0
  127. /package/dist/{chunk-4IHQSNE3.js.map → chunk-2GCPSAXQ.js.map} +0 -0
  128. /package/dist/{chunk-PNWXXDTE.js.map → chunk-2KYGPVTX.js.map} +0 -0
  129. /package/dist/{chunk-RZS4BHCW.js.map → chunk-6NQ7KYJH.js.map} +0 -0
  130. /package/dist/{chunk-BSMT7MK4.js.map → chunk-7L54Z4EH.js.map} +0 -0
  131. /package/dist/{chunk-WWFMIAYZ.js.map → chunk-BOCLWYWL.js.map} +0 -0
  132. /package/dist/{chunk-OPBPH7IB.js.map → chunk-F3CMNSON.js.map} +0 -0
  133. /package/dist/{chunk-6FHKA62Y.js.map → chunk-FO762MEC.js.map} +0 -0
  134. /package/dist/{chunk-5XGQE2SN.js.map → chunk-HNZEXUNM.js.map} +0 -0
  135. /package/dist/{chunk-RZPQML3W.js.map → chunk-HOZZUN47.js.map} +0 -0
  136. /package/dist/{chunk-VGQDLAX3.js.map → chunk-K2BEV3BD.js.map} +0 -0
  137. /package/dist/{chunk-2HPVL6UM.js.map → chunk-KXJRXRSD.js.map} +0 -0
  138. /package/dist/{chunk-HNTWVZMH.js.map → chunk-LQIF2OHS.js.map} +0 -0
  139. /package/dist/{chunk-QQTSGXFB.js.map → chunk-MC3ZZA5G.js.map} +0 -0
  140. /package/dist/{chunk-7NXFW4LM.js.map → chunk-PMSL4A5Y.js.map} +0 -0
  141. /package/dist/{chunk-6DGMUN2Q.js.map → chunk-PX7PWXBI.js.map} +0 -0
  142. /package/dist/{chunk-UUQ2AHUL.js.map → chunk-RW5FKJRE.js.map} +0 -0
  143. /package/dist/{chunk-54LKUDJZ.js.map → chunk-UOIWIVU3.js.map} +0 -0
  144. /package/dist/{chunk-JW3ZHQ7U.js.map → chunk-VX4PSOKU.js.map} +0 -0
  145. /package/dist/{chunk-VG3NUHJX.js.map → chunk-XH36J3Z2.js.map} +0 -0
  146. /package/dist/{chunk-FFPF7SAD.js.map → chunk-ZALVAXQ4.js.map} +0 -0
  147. /package/dist/{client-CFS5VY26.js.map → client-YN3T56NM.js.map} +0 -0
  148. /package/dist/{config-4R6DCRCN.js.map → config-G7T3TMWE.js.map} +0 -0
  149. /package/dist/{config-mutate-EG5PS7KW.js.map → config-mutate-6CRKZQVQ.js.map} +0 -0
  150. /package/dist/{config-mutator-YDVTCBQZ.js.map → config-mutator-7VS3J5BY.js.map} +0 -0
  151. /package/dist/{contributions-G4FZ2PKZ.js.map → contributions-6VZ4OXJ5.js.map} +0 -0
  152. /package/dist/{doctor-4VQ5RFWH.js.map → doctor-GFZZTK7C.js.map} +0 -0
  153. /package/dist/{follow-up-tool-JJSFOO5D.js.map → follow-up-tool-NVP56W7E.js.map} +0 -0
  154. /package/dist/{hub-Z3KDKP7M.js.map → hub-B434CABZ.js.map} +0 -0
  155. /package/dist/{promote-WJ5HXGJG.js.map → promote-3P6PGJOW.js.map} +0 -0
  156. /package/dist/{query-7JPB2UBE.js.map → query-E4V7TS5S.js.map} +0 -0
  157. /package/dist/{registry-WNKSI445.js.map → query-runs-AENEZOM6.js.map} +0 -0
  158. /package/dist/{query-settings-O53GJRCB.js.map → query-settings-C5UBKJMN.js.map} +0 -0
  159. /package/dist/{routing-SJQB4SJM.js.map → registry-IG324PR3.js.map} +0 -0
  160. /package/dist/{request-planner-25WOUCP7.js.map → request-planner-G6IAUMSE.js.map} +0 -0
  161. /package/dist/{review-candidates-KU3B6T5E.js.map → review-candidates-ZL2SJXWC.js.map} +0 -0
  162. /package/dist/{schedule-tool-T6FHG77E.js.map → routing-2IBIZYQU.js.map} +0 -0
  163. /package/dist/{scheduler-W3TMCBUH.js.map → schedule-tool-QCTT2R3B.js.map} +0 -0
  164. /package/dist/{seat-NDZLFK4N.js.map → scheduler-ZGB7IKZQ.js.map} +0 -0
  165. /package/dist/{self-SO6GCVYD.js.map → seat-CFY7LMPS.js.map} +0 -0
  166. /package/dist/{select-3D3GZOBS.js.map → select-MU52ACKG.js.map} +0 -0
  167. /package/dist/{wiki-F4YT226F.js.map → self-ELWZIMVY.js.map} +0 -0
  168. /package/dist/{set-enabled-ZUTWWK5S.js.map → set-enabled-XRNSYEI3.js.map} +0 -0
  169. /package/dist/{sprint-tool-CAARUDXM.js.map → sprint-tool-E4MABD2N.js.map} +0 -0
  170. /package/dist/{tool-3UIRQJZF.js.map → tool-V5Z4KWK7.js.map} +0 -0
  171. /package/dist/{tool-HK7VOPB3.js.map → tool-W6M7GYRZ.js.map} +0 -0
  172. /package/dist/{tool-GKPYCNZ3.js.map → tool-ZYVT4VUU.js.map} +0 -0
  173. /package/dist/{wiki-enrich-E6YYAI2T.js.map → wiki-VP6XRWIS.js.map} +0 -0
@@ -9,6 +9,11 @@ import Foundation
9
9
  /// agent that is still thinking waits behind the first and goes when it
10
10
  /// answers.
11
11
  ///
12
+ /// The daemon keeps the list too (GET /voice/requests): each row has its
13
+ /// daemon id (`key`), so it survives a restart of the app or the daemon,
14
+ /// and heard on one client is heard on all of them. `sync` merges what the
15
+ /// daemon says into what the app holds.
16
+ ///
12
17
  /// Kept free of AppKit and SwiftUI so the tests can check it without a
13
18
  /// window. The answers themselves live with the app; this holds what the
14
19
  /// list and the pill show.
@@ -28,14 +33,23 @@ struct OpenRequests {
28
33
  struct Request: Equatable, Identifiable {
29
34
  let id: Int
30
35
  let agentID: String
31
- /// The words as heard.
32
- let question: String
36
+ /// The words as heard: the last ones asked, after a follow-up.
37
+ var question: String
33
38
  let sentAt: Date
34
39
  var phase: Phase
35
40
  /// When it started thinking, or when its answer came back.
36
41
  var since: Date
37
42
  /// Played or opened: its answer, or why it failed, has been seen.
38
43
  var heard = false
44
+ /// Its id on the daemon; nil until the daemon has it, or when the
45
+ /// daemon could not be reached.
46
+ var key: String? = nil
47
+ /// Run by this app: its /ask is ours, so the daemon's word on its
48
+ /// state waits for that answer. A row found on the daemon is not.
49
+ var local = true
50
+ /// A follow-up spoken while it was thinking: asked as soon as the
51
+ /// answer is back.
52
+ var pending: String? = nil
39
53
 
40
54
  /// An answer or a failure the owner has not seen yet.
41
55
  var isNew: Bool {
@@ -53,8 +67,8 @@ struct OpenRequests {
53
67
  private(set) var requests: [Request] = []
54
68
  private var nextID = 1
55
69
 
56
- /// Heard requests kept in the list. Closing a row by hand comes with
57
- /// its own slice; until then the oldest heard ones leave by themselves.
70
+ /// Heard requests kept in the list; the oldest heard ones leave by
71
+ /// themselves, as they do on the daemon (voice-requests-api.ts).
58
72
  static let keptHeard = 8
59
73
 
60
74
  func request(_ id: Int) -> Request? { requests.first { $0.id == id } }
@@ -71,6 +85,57 @@ struct OpenRequests {
71
85
  return id
72
86
  }
73
87
 
88
+ // MARK: Following up
89
+
90
+ /// What a follow-up does to its row.
91
+ enum Follow: Equatable {
92
+ /// Ask again now, with the new words.
93
+ case ask
94
+ /// Wait behind another request for the same agent.
95
+ case waits
96
+ /// Go once the answer it is thinking about is back.
97
+ case after
98
+ }
99
+
100
+ /// A follow-up spoken with this row picked: it continues the row; no
101
+ /// new row is added. A row done (answered or failed) asks again with
102
+ /// these words, not heard, or waits when its agent has another request
103
+ /// out. A row waiting its turn takes the words into the question not
104
+ /// asked yet. A row thinking keeps them for when its answer is back,
105
+ /// and that answer is not played (as when the owner talks over a turn).
106
+ /// Nil when the row is gone.
107
+ mutating func follow(_ id: Int, with words: String, at now: Date) -> Follow? {
108
+ guard let i = requests.firstIndex(where: { $0.id == id }) else { return nil }
109
+ switch requests[i].phase {
110
+ case .waiting:
111
+ requests[i].question += " " + words
112
+ return .waits
113
+ case .thinking:
114
+ requests[i].pending = requests[i].pending.map { $0 + " " + words } ?? words
115
+ return .after
116
+ case .answered, .failed:
117
+ let agent = requests[i].agentID
118
+ let busy = requests.contains { $0.id != id && $0.agentID == agent && $0.isOpen }
119
+ requests[i].question = words
120
+ requests[i].phase = busy ? .waiting : .thinking
121
+ requests[i].since = now
122
+ requests[i].heard = false
123
+ requests[i].local = true
124
+ return busy ? .waits : .ask
125
+ }
126
+ }
127
+
128
+ /// The answer is back on a row with a follow-up kept: the follow-up is
129
+ /// its question now and it thinks again. False when none was kept.
130
+ mutating func takePending(_ id: Int, at now: Date) -> Bool {
131
+ guard let i = requests.firstIndex(where: { $0.id == id }), let words = requests[i].pending else { return false }
132
+ requests[i].question = words
133
+ requests[i].pending = nil
134
+ requests[i].phase = .thinking
135
+ requests[i].since = now
136
+ return true
137
+ }
138
+
74
139
  /// The answer came back.
75
140
  mutating func answered(_ id: Int, at now: Date) {
76
141
  update(id) { $0.phase = .answered; $0.since = now }
@@ -82,12 +147,13 @@ struct OpenRequests {
82
147
  }
83
148
 
84
149
  /// The oldest request waiting for `agentID`, now thinking, if there is
85
- /// one and nothing else for that agent is.
150
+ /// one and nothing else for that agent is. This app runs it.
86
151
  mutating func startNext(for agentID: String, at now: Date) -> Int? {
87
152
  guard !requests.contains(where: { $0.agentID == agentID && $0.phase == .thinking }),
88
153
  let i = requests.firstIndex(where: { $0.agentID == agentID && $0.phase == .waiting }) else { return nil }
89
154
  requests[i].phase = .thinking
90
155
  requests[i].since = now
156
+ requests[i].local = true
91
157
  return requests[i].id
92
158
  }
93
159
 
@@ -101,11 +167,125 @@ struct OpenRequests {
101
167
  }
102
168
  }
103
169
 
170
+ /// The daemon took the row's words for something else (the floor
171
+ /// holder's sound). A follow-up kept while it thought is the owner's own
172
+ /// words, so it is not lost: the row asks it now and stays. With none
173
+ /// kept, the row leaves. True when the row asks again.
174
+ mutating func dropped(_ id: Int, at now: Date) -> Bool {
175
+ if takePending(id, at: now) { return true }
176
+ remove(id)
177
+ return false
178
+ }
179
+
104
180
  /// Gone without a trace: the daemon took the words for something else.
105
181
  mutating func remove(_ id: Int) {
106
182
  requests.removeAll { $0.id == id }
107
183
  }
108
184
 
185
+ /// The daemon has the request under `key`.
186
+ mutating func setKey(_ id: Int, _ key: String) {
187
+ update(id) { $0.key = key }
188
+ }
189
+
190
+ func request(key: String) -> Request? { requests.first { $0.key == key } }
191
+
192
+ // MARK: Closing
193
+
194
+ /// Close a row: it leaves the list. Returned so Undo can put it back.
195
+ @discardableResult
196
+ mutating func close(_ id: Int) -> Request? {
197
+ guard let r = request(id) else { return nil }
198
+ requests.removeAll { $0.id == id }
199
+ return r
200
+ }
201
+
202
+ /// Undo a close: the row goes back to its place, by when it was sent.
203
+ mutating func restore(_ r: Request) {
204
+ guard request(r.id) == nil else { return }
205
+ let at = requests.firstIndex { $0.sentAt > r.sentAt } ?? requests.endIndex
206
+ requests.insert(r, at: at)
207
+ }
208
+
209
+ /// "Close heard": every heard answer and every failure leaves.
210
+ @discardableResult
211
+ mutating func closeHeard() -> [Request] {
212
+ let gone = requests.filter { r in
213
+ if case .failed = r.phase { return true }
214
+ return r.phase == .answered && r.heard
215
+ }
216
+ let ids = Set(gone.map(\.id))
217
+ requests.removeAll { ids.contains($0.id) }
218
+ return gone
219
+ }
220
+
221
+ /// What closing a row asks first: a row still with its agent asks
222
+ /// whether to let it finish or stop it; anything else closes at once.
223
+ static func closeNeedsConfirm(_ r: Request) -> Bool { r.phase == .thinking }
224
+
225
+ // MARK: The daemon's list
226
+
227
+ /// One row of GET /voice/requests.
228
+ struct Remote: Equatable {
229
+ let key: String
230
+ let agentID: String
231
+ let question: String
232
+ let sentAt: Date
233
+ let since: Date
234
+ /// "waiting", "thinking", "answered" or "failed".
235
+ let state: String
236
+ let heard: Bool
237
+ let error: String?
238
+
239
+ var phase: Phase {
240
+ switch state {
241
+ case "waiting": return .waiting
242
+ case "answered": return .answered
243
+ case "failed": return .failed(error ?? "Something went wrong.")
244
+ default: return .thinking
245
+ }
246
+ }
247
+ }
248
+
249
+ /// Merge the daemon's open rows. A row heard elsewhere is heard here,
250
+ /// and one continued elsewhere has its new words and is not heard; a
251
+ /// row the daemon no longer lists was closed elsewhere and leaves; a
252
+ /// row only the daemon has (after the app restarted) joins. The state
253
+ /// of a row this app is running is the app's until its /ask answers.
254
+ /// Rows the daemon doesn't have yet (no key) are left alone. Returns
255
+ /// the ids that joined.
256
+ @discardableResult
257
+ mutating func sync(_ remote: [Remote]) -> [Int] {
258
+ let byKey = Dictionary(remote.map { ($0.key, $0) }, uniquingKeysWith: { a, _ in a })
259
+ requests.removeAll { r in r.key.map { byKey[$0] == nil } ?? false }
260
+ for i in requests.indices {
261
+ guard let key = requests[i].key, let theirs = byKey[key] else { continue }
262
+ if theirs.heard { requests[i].heard = true }
263
+ guard !requests[i].local else { continue }
264
+ if requests[i].phase != theirs.phase {
265
+ requests[i].phase = theirs.phase
266
+ requests[i].since = theirs.since
267
+ }
268
+ // Continued on another client: its new words, not heard.
269
+ requests[i].question = theirs.question
270
+ requests[i].heard = theirs.heard
271
+ }
272
+ var joined: [Int] = []
273
+ for theirs in remote where request(key: theirs.key) == nil {
274
+ let id = nextID
275
+ nextID += 1
276
+ restore(Request(id: id, agentID: theirs.agentID, question: theirs.question, sentAt: theirs.sentAt,
277
+ phase: theirs.phase, since: theirs.since, heard: theirs.heard,
278
+ key: theirs.key, local: false))
279
+ joined.append(id)
280
+ }
281
+ return joined
282
+ }
283
+
284
+ /// This app runs a row it found on the daemon (one waiting its turn).
285
+ mutating func adopt(_ id: Int) {
286
+ update(id) { $0.local = true }
287
+ }
288
+
109
289
  private mutating func update(_ id: Int, _ change: (inout Request) -> Void) {
110
290
  guard let i = requests.firstIndex(where: { $0.id == id }) else { return }
111
291
  change(&requests[i])
@@ -138,9 +318,62 @@ struct OpenRequests {
138
318
  }
139
319
 
140
320
  /// The rows in the order drawn: by when each was sent, the newest next
141
- /// to the pill. Rows never move when a state changes.
142
- func rows(newestLast: Bool) -> [Request] {
143
- newestLast ? requests : requests.reversed()
321
+ /// to the pill. Rows never move when a state changes. With `agentID`,
322
+ /// that agent's rows only.
323
+ func rows(newestLast: Bool, agentID: String? = nil) -> [Request] {
324
+ let mine = agentID.map { a in requests.filter { $0.agentID == a } } ?? requests
325
+ return newestLast ? mine : mine.reversed()
326
+ }
327
+
328
+ // MARK: Grouped by agent
329
+
330
+ /// Up to this many rows the list is flat; past it, one row per agent.
331
+ static let flatUpTo = 5
332
+
333
+ var grouped: Bool { requests.count > Self.flatUpTo }
334
+
335
+ /// One agent's rows, as one row of the grouped list.
336
+ struct Group: Equatable {
337
+ let agentID: String
338
+ /// Oldest first.
339
+ let requests: [Request]
340
+
341
+ /// Stable while any of its rows stays, and never a request's id:
342
+ /// the negative of its oldest row's.
343
+ var id: Int { -(requests.map(\.id).min() ?? 0) }
344
+ var newCount: Int { requests.filter(\.isNew).count }
345
+ var latest: Request { requests.max { $0.sentAt < $1.sentAt }! }
346
+ }
347
+
348
+ /// One group per agent, ordered by its newest row, the newest next to
349
+ /// the pill.
350
+ func groups(newestLast: Bool) -> [Group] {
351
+ var order: [String] = []
352
+ var byAgent: [String: [Request]] = [:]
353
+ for r in requests {
354
+ if byAgent[r.agentID] == nil { order.append(r.agentID) }
355
+ byAgent[r.agentID, default: []].append(r)
356
+ }
357
+ let groups = order.map { Group(agentID: $0, requests: byAgent[$0]!) }
358
+ .sorted { $0.latest.sentAt < $1.latest.sentAt }
359
+ return newestLast ? groups : groups.reversed()
360
+ }
361
+
362
+ /// A group's state, after the agent's name: what most needs the owner
363
+ /// first. "2 new", "Thinking", "Waiting its turn", else how many.
364
+ static func groupState(_ g: Group) -> String {
365
+ if g.newCount > 0 { return newBadge(g.newCount) }
366
+ if g.requests.contains(where: { $0.phase == .thinking }) { return "Thinking" }
367
+ if g.requests.contains(where: { $0.phase == .waiting }) { return "Waiting its turn" }
368
+ return requestsCount(g.requests.count)
369
+ }
370
+
371
+ /// "1 request", "4 requests".
372
+ static func requestsCount(_ n: Int) -> String { "\(n) request\(n == 1 ? "" : "s")" }
373
+
374
+ /// What VoiceOver reads for a group row.
375
+ static func groupVoiceOver(name: String, _ g: Group) -> String {
376
+ "\(name). \(requestsCount(g.requests.count)). \(groupState(g)). Last asked: \(flatLower(g.latest.question))."
144
377
  }
145
378
 
146
379
  // MARK: Words
@@ -220,6 +453,41 @@ struct OpenRequests {
220
453
  /// How long those lines stay before the pill goes back to its own.
221
454
  static let noteSeconds: TimeInterval = 4
222
455
 
456
+ /// The pill's line after a follow-up: it went into the picked row.
457
+ static let followNote = "Added to the same request"
458
+
459
+ /// The pill's line once a row is picked: the next words go to its agent
460
+ /// and continue that conversation.
461
+ static func continueNote(name: String) -> String { "\(name) · Hold ⌥Space to continue it" }
462
+
463
+ /// The line after a close, with Undo beside it.
464
+ static func closedNote(name: String) -> String { "Closed \(name)’s request" }
465
+
466
+ /// How long Undo stays after a close.
467
+ static let undoSeconds: TimeInterval = 5
468
+
469
+ /// The confirm a thinking row turns into when closed.
470
+ static func stillThinking(name: String) -> String { "\(name) is still thinking" }
471
+ static let letItFinish = "Close, let it finish"
472
+ static func stopAgent(name: String) -> String { "Stop \(name)" }
473
+ static let keep = "Keep"
474
+
475
+ // MARK: Failed, loading, unreachable
476
+
477
+ /// Under a failed row once picked: the same words to the same agent.
478
+ static let askAgain = "Ask again"
479
+
480
+ /// The list while the daemon does not answer.
481
+ static let unreachableTitle = "Can’t reach AgentX on this Mac"
482
+ static let unreachableBody = "Your open requests are kept by AgentX and come back when it answers. Trying again every few seconds."
483
+
484
+ /// The pill's line when the daemon stops answering.
485
+ static let unreachableNote = "AgentX isn’t answering · Requests come back when it does"
486
+
487
+ /// The list shows placeholder rows only when the daemon has not
488
+ /// answered the first read after this long.
489
+ static let loadingAfter: TimeInterval = 0.5
490
+
223
491
  // MARK: Picking a row
224
492
 
225
493
  enum Pick: Equatable {