sync.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { produce } from "solid-js/store"
  2. import { createMemo } from "solid-js"
  3. import { Binary } from "@opencode-ai/util/binary"
  4. import { createSimpleContext } from "@opencode-ai/ui/context"
  5. import { useGlobalSync } from "./global-sync"
  6. import { useSDK } from "./sdk"
  7. export const { use: useSync, provider: SyncProvider } = createSimpleContext({
  8. name: "Sync",
  9. init: () => {
  10. const globalSync = useGlobalSync()
  11. const sdk = useSDK()
  12. const [store, setStore] = globalSync.child(sdk.directory)
  13. const load = {
  14. project: () => sdk.client.project.current().then((x) => setStore("project", x.data!)),
  15. provider: () => sdk.client.config.providers().then((x) => setStore("provider", x.data!.providers)),
  16. path: () => sdk.client.path.get().then((x) => setStore("path", x.data!)),
  17. agent: () => sdk.client.app.agents().then((x) => setStore("agent", x.data ?? [])),
  18. session: () =>
  19. sdk.client.session.list().then((x) => {
  20. const sessions = (x.data ?? [])
  21. .slice()
  22. .sort((a, b) => a.id.localeCompare(b.id))
  23. .slice(0, store.limit)
  24. setStore("session", sessions)
  25. }),
  26. status: () => sdk.client.session.status().then((x) => setStore("session_status", x.data!)),
  27. config: () => sdk.client.config.get().then((x) => setStore("config", x.data!)),
  28. changes: () => sdk.client.file.status().then((x) => setStore("changes", x.data!)),
  29. node: () => sdk.client.file.list({ query: { path: "/" } }).then((x) => setStore("node", x.data!)),
  30. }
  31. Promise.all(Object.values(load).map((p) => p())).then(() => setStore("ready", true))
  32. const absolute = (path: string) => (store.path.directory + "/" + path).replace("//", "/")
  33. return {
  34. data: store,
  35. set: setStore,
  36. get ready() {
  37. return store.ready
  38. },
  39. session: {
  40. get(sessionID: string) {
  41. const match = Binary.search(store.session, sessionID, (s) => s.id)
  42. if (match.found) return store.session[match.index]
  43. return undefined
  44. },
  45. async sync(sessionID: string, _isRetry = false) {
  46. const [session, messages, todo, diff] = await Promise.all([
  47. sdk.client.session.get({ path: { id: sessionID }, throwOnError: true }),
  48. sdk.client.session.messages({ path: { id: sessionID }, query: { limit: 100 } }),
  49. sdk.client.session.todo({ path: { id: sessionID } }),
  50. sdk.client.session.diff({ path: { id: sessionID } }),
  51. ])
  52. setStore(
  53. produce((draft) => {
  54. const match = Binary.search(draft.session, sessionID, (s) => s.id)
  55. if (match.found) draft.session[match.index] = session.data!
  56. if (!match.found) draft.session.splice(match.index, 0, session.data!)
  57. draft.todo[sessionID] = todo.data ?? []
  58. draft.message[sessionID] = messages
  59. .data!.map((x) => x.info)
  60. .slice()
  61. .sort((a, b) => a.id.localeCompare(b.id))
  62. for (const message of messages.data!) {
  63. draft.part[message.info.id] = message.parts.slice().sort((a, b) => a.id.localeCompare(b.id))
  64. }
  65. draft.session_diff[sessionID] = diff.data ?? []
  66. }),
  67. )
  68. },
  69. fetch: async (count = 10) => {
  70. setStore("limit", (x) => x + count)
  71. await load.session()
  72. },
  73. more: createMemo(() => store.session.length >= store.limit),
  74. },
  75. load,
  76. absolute,
  77. get directory() {
  78. return store.path.directory
  79. },
  80. }
  81. },
  82. })