notification.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { createStore } from "solid-js/store"
  2. import { createSimpleContext } from "@opencode-ai/ui/context"
  3. import { useGlobalSDK } from "./global-sdk"
  4. import { useGlobalSync } from "./global-sync"
  5. import { usePlatform } from "@/context/platform"
  6. import { Binary } from "@opencode-ai/util/binary"
  7. import { base64Encode } from "@opencode-ai/util/encode"
  8. import { EventSessionError } from "@opencode-ai/sdk/v2"
  9. import { makeAudioPlayer } from "@solid-primitives/audio"
  10. import idleSound from "@opencode-ai/ui/audio/staplebops-01.aac"
  11. import errorSound from "@opencode-ai/ui/audio/nope-03.aac"
  12. import { persisted } from "@/utils/persist"
  13. type NotificationBase = {
  14. directory?: string
  15. session?: string
  16. metadata?: any
  17. time: number
  18. viewed: boolean
  19. }
  20. type TurnCompleteNotification = NotificationBase & {
  21. type: "turn-complete"
  22. }
  23. type ErrorNotification = NotificationBase & {
  24. type: "error"
  25. error: EventSessionError["properties"]["error"]
  26. }
  27. export type Notification = TurnCompleteNotification | ErrorNotification
  28. export const { use: useNotification, provider: NotificationProvider } = createSimpleContext({
  29. name: "Notification",
  30. init: () => {
  31. let idlePlayer: ReturnType<typeof makeAudioPlayer> | undefined
  32. let errorPlayer: ReturnType<typeof makeAudioPlayer> | undefined
  33. try {
  34. idlePlayer = makeAudioPlayer(idleSound)
  35. errorPlayer = makeAudioPlayer(errorSound)
  36. } catch (err) {
  37. console.log("Failed to load audio", err)
  38. }
  39. const globalSDK = useGlobalSDK()
  40. const globalSync = useGlobalSync()
  41. const platform = usePlatform()
  42. const [store, setStore, _, ready] = persisted(
  43. "notification.v1",
  44. createStore({
  45. list: [] as Notification[],
  46. }),
  47. )
  48. globalSDK.event.listen((e) => {
  49. const directory = e.name
  50. const event = e.details
  51. const base = {
  52. directory,
  53. time: Date.now(),
  54. viewed: false,
  55. }
  56. switch (event.type) {
  57. case "session.idle": {
  58. const sessionID = event.properties.sessionID
  59. const [syncStore] = globalSync.child(directory)
  60. const match = Binary.search(syncStore.session, sessionID, (s) => s.id)
  61. const session = match.found ? syncStore.session[match.index] : undefined
  62. if (session?.parentID) break
  63. try {
  64. idlePlayer?.play()
  65. } catch {}
  66. setStore("list", store.list.length, {
  67. ...base,
  68. type: "turn-complete",
  69. session: sessionID,
  70. })
  71. const href = `/${base64Encode(directory)}/session/${sessionID}`
  72. void platform.notify("Response ready", session?.title ?? sessionID, href)
  73. break
  74. }
  75. case "session.error": {
  76. const sessionID = event.properties.sessionID
  77. const [syncStore] = globalSync.child(directory)
  78. const match = sessionID ? Binary.search(syncStore.session, sessionID, (s) => s.id) : undefined
  79. const session = sessionID && match?.found ? syncStore.session[match.index] : undefined
  80. if (session?.parentID) break
  81. try {
  82. errorPlayer?.play()
  83. } catch {}
  84. const error = "error" in event.properties ? event.properties.error : undefined
  85. setStore("list", store.list.length, {
  86. ...base,
  87. type: "error",
  88. session: sessionID ?? "global",
  89. error,
  90. })
  91. const description = session?.title ?? (typeof error === "string" ? error : "An error occurred")
  92. const href = sessionID ? `/${base64Encode(directory)}/session/${sessionID}` : `/${base64Encode(directory)}`
  93. void platform.notify("Session error", description, href)
  94. break
  95. }
  96. }
  97. })
  98. return {
  99. ready,
  100. session: {
  101. all(session: string) {
  102. return store.list.filter((n) => n.session === session)
  103. },
  104. unseen(session: string) {
  105. return store.list.filter((n) => n.session === session && !n.viewed)
  106. },
  107. markViewed(session: string) {
  108. setStore("list", (n) => n.session === session, "viewed", true)
  109. },
  110. },
  111. project: {
  112. all(directory: string) {
  113. return store.list.filter((n) => n.directory === directory)
  114. },
  115. unseen(directory: string) {
  116. return store.list.filter((n) => n.directory === directory && !n.viewed)
  117. },
  118. markViewed(directory: string) {
  119. setStore("list", (n) => n.directory === directory, "viewed", true)
  120. },
  121. },
  122. }
  123. },
  124. })