event-reducer.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. import { Binary } from "@opencode-ai/util/binary"
  2. import { produce, reconcile, type SetStoreFunction, type Store } from "solid-js/store"
  3. import type {
  4. FileDiff,
  5. Message,
  6. Part,
  7. PermissionRequest,
  8. Project,
  9. QuestionRequest,
  10. Session,
  11. SessionStatus,
  12. Todo,
  13. } from "@opencode-ai/sdk/v2/client"
  14. import type { State, VcsCache } from "./types"
  15. import { trimSessions } from "./session-trim"
  16. export function applyGlobalEvent(input: {
  17. event: { type: string; properties?: unknown }
  18. project: Project[]
  19. setGlobalProject: (next: Project[] | ((draft: Project[]) => void)) => void
  20. refresh: () => void
  21. }) {
  22. if (input.event.type === "global.disposed" || input.event.type === "server.connected") {
  23. input.refresh()
  24. return
  25. }
  26. if (input.event.type !== "project.updated") return
  27. const properties = input.event.properties as Project
  28. const result = Binary.search(input.project, properties.id, (s) => s.id)
  29. if (result.found) {
  30. input.setGlobalProject((draft) => {
  31. draft[result.index] = { ...draft[result.index], ...properties }
  32. })
  33. return
  34. }
  35. input.setGlobalProject((draft) => {
  36. draft.splice(result.index, 0, properties)
  37. })
  38. }
  39. function cleanupSessionCaches(store: Store<State>, setStore: SetStoreFunction<State>, sessionID: string) {
  40. if (!sessionID) return
  41. const hasAny =
  42. store.message[sessionID] !== undefined ||
  43. store.session_diff[sessionID] !== undefined ||
  44. store.todo[sessionID] !== undefined ||
  45. store.permission[sessionID] !== undefined ||
  46. store.question[sessionID] !== undefined ||
  47. store.session_status[sessionID] !== undefined
  48. if (!hasAny) return
  49. setStore(
  50. produce((draft) => {
  51. const messages = draft.message[sessionID]
  52. if (messages) {
  53. for (const message of messages) {
  54. const id = message?.id
  55. if (!id) continue
  56. delete draft.part[id]
  57. }
  58. }
  59. delete draft.message[sessionID]
  60. delete draft.session_diff[sessionID]
  61. delete draft.todo[sessionID]
  62. delete draft.permission[sessionID]
  63. delete draft.question[sessionID]
  64. delete draft.session_status[sessionID]
  65. }),
  66. )
  67. }
  68. export function applyDirectoryEvent(input: {
  69. event: { type: string; properties?: unknown }
  70. store: Store<State>
  71. setStore: SetStoreFunction<State>
  72. push: (directory: string) => void
  73. directory: string
  74. loadLsp: () => void
  75. vcsCache?: VcsCache
  76. }) {
  77. const event = input.event
  78. switch (event.type) {
  79. case "server.instance.disposed": {
  80. input.push(input.directory)
  81. return
  82. }
  83. case "session.created": {
  84. const info = (event.properties as { info: Session }).info
  85. const result = Binary.search(input.store.session, info.id, (s) => s.id)
  86. if (result.found) {
  87. input.setStore("session", result.index, reconcile(info))
  88. break
  89. }
  90. const next = input.store.session.slice()
  91. next.splice(result.index, 0, info)
  92. const trimmed = trimSessions(next, { limit: input.store.limit, permission: input.store.permission })
  93. input.setStore("session", reconcile(trimmed, { key: "id" }))
  94. if (!info.parentID) input.setStore("sessionTotal", (value) => value + 1)
  95. break
  96. }
  97. case "session.updated": {
  98. const info = (event.properties as { info: Session }).info
  99. const result = Binary.search(input.store.session, info.id, (s) => s.id)
  100. if (info.time.archived) {
  101. if (result.found) {
  102. input.setStore(
  103. "session",
  104. produce((draft) => {
  105. draft.splice(result.index, 1)
  106. }),
  107. )
  108. }
  109. cleanupSessionCaches(input.store, input.setStore, info.id)
  110. if (info.parentID) break
  111. input.setStore("sessionTotal", (value) => Math.max(0, value - 1))
  112. break
  113. }
  114. if (result.found) {
  115. input.setStore("session", result.index, reconcile(info))
  116. break
  117. }
  118. const next = input.store.session.slice()
  119. next.splice(result.index, 0, info)
  120. const trimmed = trimSessions(next, { limit: input.store.limit, permission: input.store.permission })
  121. input.setStore("session", reconcile(trimmed, { key: "id" }))
  122. break
  123. }
  124. case "session.deleted": {
  125. const info = (event.properties as { info: Session }).info
  126. const result = Binary.search(input.store.session, info.id, (s) => s.id)
  127. if (result.found) {
  128. input.setStore(
  129. "session",
  130. produce((draft) => {
  131. draft.splice(result.index, 1)
  132. }),
  133. )
  134. }
  135. cleanupSessionCaches(input.store, input.setStore, info.id)
  136. if (info.parentID) break
  137. input.setStore("sessionTotal", (value) => Math.max(0, value - 1))
  138. break
  139. }
  140. case "session.diff": {
  141. const props = event.properties as { sessionID: string; diff: FileDiff[] }
  142. input.setStore("session_diff", props.sessionID, reconcile(props.diff, { key: "file" }))
  143. break
  144. }
  145. case "todo.updated": {
  146. const props = event.properties as { sessionID: string; todos: Todo[] }
  147. input.setStore("todo", props.sessionID, reconcile(props.todos, { key: "id" }))
  148. break
  149. }
  150. case "session.status": {
  151. const props = event.properties as { sessionID: string; status: SessionStatus }
  152. input.setStore("session_status", props.sessionID, reconcile(props.status))
  153. break
  154. }
  155. case "message.updated": {
  156. const info = (event.properties as { info: Message }).info
  157. const messages = input.store.message[info.sessionID]
  158. if (!messages) {
  159. input.setStore("message", info.sessionID, [info])
  160. break
  161. }
  162. const result = Binary.search(messages, info.id, (m) => m.id)
  163. if (result.found) {
  164. input.setStore("message", info.sessionID, result.index, reconcile(info))
  165. break
  166. }
  167. input.setStore(
  168. "message",
  169. info.sessionID,
  170. produce((draft) => {
  171. draft.splice(result.index, 0, info)
  172. }),
  173. )
  174. break
  175. }
  176. case "message.removed": {
  177. const props = event.properties as { sessionID: string; messageID: string }
  178. input.setStore(
  179. produce((draft) => {
  180. const messages = draft.message[props.sessionID]
  181. if (messages) {
  182. const result = Binary.search(messages, props.messageID, (m) => m.id)
  183. if (result.found) messages.splice(result.index, 1)
  184. }
  185. delete draft.part[props.messageID]
  186. }),
  187. )
  188. break
  189. }
  190. case "message.part.updated": {
  191. const part = (event.properties as { part: Part }).part
  192. const parts = input.store.part[part.messageID]
  193. if (!parts) {
  194. input.setStore("part", part.messageID, [part])
  195. break
  196. }
  197. const result = Binary.search(parts, part.id, (p) => p.id)
  198. if (result.found) {
  199. input.setStore("part", part.messageID, result.index, reconcile(part))
  200. break
  201. }
  202. input.setStore(
  203. "part",
  204. part.messageID,
  205. produce((draft) => {
  206. draft.splice(result.index, 0, part)
  207. }),
  208. )
  209. break
  210. }
  211. case "message.part.removed": {
  212. const props = event.properties as { messageID: string; partID: string }
  213. const parts = input.store.part[props.messageID]
  214. if (!parts) break
  215. const result = Binary.search(parts, props.partID, (p) => p.id)
  216. if (result.found) {
  217. input.setStore(
  218. produce((draft) => {
  219. const list = draft.part[props.messageID]
  220. if (!list) return
  221. const next = Binary.search(list, props.partID, (p) => p.id)
  222. if (!next.found) return
  223. list.splice(next.index, 1)
  224. if (list.length === 0) delete draft.part[props.messageID]
  225. }),
  226. )
  227. }
  228. break
  229. }
  230. case "message.part.delta": {
  231. const props = event.properties as { messageID: string; partID: string; field: string; delta: string }
  232. const parts = input.store.part[props.messageID]
  233. if (!parts) break
  234. const result = Binary.search(parts, props.partID, (p) => p.id)
  235. if (!result.found) break
  236. input.setStore(
  237. "part",
  238. props.messageID,
  239. produce((draft) => {
  240. const part = draft[result.index]
  241. const field = props.field as keyof typeof part
  242. const existing = part[field] as string | undefined
  243. ;(part[field] as string) = (existing ?? "") + props.delta
  244. }),
  245. )
  246. break
  247. }
  248. case "vcs.branch.updated": {
  249. const props = event.properties as { branch: string }
  250. if (input.store.vcs?.branch === props.branch) break
  251. const next = { branch: props.branch }
  252. input.setStore("vcs", next)
  253. if (input.vcsCache) input.vcsCache.setStore("value", next)
  254. break
  255. }
  256. case "permission.asked": {
  257. const permission = event.properties as PermissionRequest
  258. const permissions = input.store.permission[permission.sessionID]
  259. if (!permissions) {
  260. input.setStore("permission", permission.sessionID, [permission])
  261. break
  262. }
  263. const result = Binary.search(permissions, permission.id, (p) => p.id)
  264. if (result.found) {
  265. input.setStore("permission", permission.sessionID, result.index, reconcile(permission))
  266. break
  267. }
  268. input.setStore(
  269. "permission",
  270. permission.sessionID,
  271. produce((draft) => {
  272. draft.splice(result.index, 0, permission)
  273. }),
  274. )
  275. break
  276. }
  277. case "permission.replied": {
  278. const props = event.properties as { sessionID: string; requestID: string }
  279. const permissions = input.store.permission[props.sessionID]
  280. if (!permissions) break
  281. const result = Binary.search(permissions, props.requestID, (p) => p.id)
  282. if (!result.found) break
  283. input.setStore(
  284. "permission",
  285. props.sessionID,
  286. produce((draft) => {
  287. draft.splice(result.index, 1)
  288. }),
  289. )
  290. break
  291. }
  292. case "question.asked": {
  293. const question = event.properties as QuestionRequest
  294. const questions = input.store.question[question.sessionID]
  295. if (!questions) {
  296. input.setStore("question", question.sessionID, [question])
  297. break
  298. }
  299. const result = Binary.search(questions, question.id, (q) => q.id)
  300. if (result.found) {
  301. input.setStore("question", question.sessionID, result.index, reconcile(question))
  302. break
  303. }
  304. input.setStore(
  305. "question",
  306. question.sessionID,
  307. produce((draft) => {
  308. draft.splice(result.index, 0, question)
  309. }),
  310. )
  311. break
  312. }
  313. case "question.replied":
  314. case "question.rejected": {
  315. const props = event.properties as { sessionID: string; requestID: string }
  316. const questions = input.store.question[props.sessionID]
  317. if (!questions) break
  318. const result = Binary.search(questions, props.requestID, (q) => q.id)
  319. if (!result.found) break
  320. input.setStore(
  321. "question",
  322. props.sessionID,
  323. produce((draft) => {
  324. draft.splice(result.index, 1)
  325. }),
  326. )
  327. break
  328. }
  329. case "lsp.updated": {
  330. input.loadLsp()
  331. break
  332. }
  333. }
  334. }