sync.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { batch, createMemo } from "solid-js"
  2. import { produce, reconcile } from "solid-js/store"
  3. import { Binary } from "@opencode-ai/util/binary"
  4. import { retry } from "@opencode-ai/util/retry"
  5. import { createSimpleContext } from "@opencode-ai/ui/context"
  6. import { useGlobalSync } from "./global-sync"
  7. import { useSDK } from "./sdk"
  8. import type { Message, Part } from "@opencode-ai/sdk/v2/client"
  9. export const { use: useSync, provider: SyncProvider } = createSimpleContext({
  10. name: "Sync",
  11. init: () => {
  12. const globalSync = useGlobalSync()
  13. const sdk = useSDK()
  14. const [store, setStore] = globalSync.child(sdk.directory)
  15. const absolute = (path: string) => (store.path.directory + "/" + path).replace("//", "/")
  16. return {
  17. data: store,
  18. set: setStore,
  19. get ready() {
  20. return store.ready
  21. },
  22. get project() {
  23. const match = Binary.search(globalSync.data.project, store.project, (p) => p.id)
  24. if (match.found) return globalSync.data.project[match.index]
  25. return undefined
  26. },
  27. session: {
  28. get(sessionID: string) {
  29. const match = Binary.search(store.session, sessionID, (s) => s.id)
  30. if (match.found) return store.session[match.index]
  31. return undefined
  32. },
  33. addOptimisticMessage(input: {
  34. sessionID: string
  35. messageID: string
  36. parts: Part[]
  37. agent: string
  38. model: { providerID: string; modelID: string }
  39. }) {
  40. const message: Message = {
  41. id: input.messageID,
  42. sessionID: input.sessionID,
  43. role: "user",
  44. time: { created: Date.now() },
  45. agent: input.agent,
  46. model: input.model,
  47. }
  48. setStore(
  49. produce((draft) => {
  50. const messages = draft.message[input.sessionID]
  51. if (!messages) {
  52. draft.message[input.sessionID] = [message]
  53. } else {
  54. const result = Binary.search(messages, input.messageID, (m) => m.id)
  55. messages.splice(result.index, 0, message)
  56. }
  57. draft.part[input.messageID] = input.parts.slice().sort((a, b) => a.id.localeCompare(b.id))
  58. }),
  59. )
  60. },
  61. async sync(sessionID: string, _isRetry = false) {
  62. const [session, messages, todo, diff] = await Promise.all([
  63. retry(() => sdk.client.session.get({ sessionID })),
  64. retry(() => sdk.client.session.messages({ sessionID, limit: 100 })),
  65. retry(() => sdk.client.session.todo({ sessionID })),
  66. retry(() => sdk.client.session.diff({ sessionID })),
  67. ])
  68. batch(() => {
  69. setStore(
  70. "session",
  71. produce((draft) => {
  72. const match = Binary.search(draft, sessionID, (s) => s.id)
  73. if (match.found) {
  74. draft[match.index] = session.data!
  75. return
  76. }
  77. draft.splice(match.index, 0, session.data!)
  78. }),
  79. )
  80. setStore("todo", sessionID, reconcile(todo.data ?? [], { key: "id" }))
  81. setStore(
  82. "message",
  83. sessionID,
  84. reconcile(
  85. (messages.data ?? [])
  86. .map((x) => x.info)
  87. .slice()
  88. .sort((a, b) => a.id.localeCompare(b.id)),
  89. { key: "id" },
  90. ),
  91. )
  92. for (const message of messages.data ?? []) {
  93. setStore(
  94. "part",
  95. message.info.id,
  96. reconcile(
  97. message.parts.slice().sort((a, b) => a.id.localeCompare(b.id)),
  98. { key: "id" },
  99. ),
  100. )
  101. }
  102. setStore("session_diff", sessionID, reconcile(diff.data ?? [], { key: "file" }))
  103. })
  104. },
  105. fetch: async (count = 10) => {
  106. setStore("limit", (x) => x + count)
  107. await sdk.client.session.list().then((x) => {
  108. const sessions = (x.data ?? [])
  109. .slice()
  110. .sort((a, b) => a.id.localeCompare(b.id))
  111. .slice(0, store.limit)
  112. setStore("session", reconcile(sessions, { key: "id" }))
  113. })
  114. },
  115. more: createMemo(() => store.session.length >= store.limit),
  116. archive: async (sessionID: string) => {
  117. await sdk.client.session.update({ sessionID, time: { archived: Date.now() } })
  118. setStore(
  119. produce((draft) => {
  120. const match = Binary.search(draft.session, sessionID, (s) => s.id)
  121. if (match.found) draft.session.splice(match.index, 1)
  122. }),
  123. )
  124. },
  125. },
  126. absolute,
  127. get directory() {
  128. return store.path.directory
  129. },
  130. }
  131. },
  132. })