(client: OpencodeClient, options?: { retry?: typeof retry })
| 130 | } |
| 131 | |
| 132 | export function createServerSession(client: OpencodeClient, options?: { retry?: typeof retry }) { |
| 133 | const [data, setData] = createStore({ |
| 134 | info: {} as Record<string, Session | undefined>, |
| 135 | session_status: {} as Record<string, SessionStatus>, |
| 136 | session_diff: {} as Record<string, SnapshotFileDiff[]>, |
| 137 | todo: {} as Record<string, Todo[]>, |
| 138 | permission: {} as Record<string, PermissionRequest[]>, |
| 139 | question: {} as Record<string, QuestionRequest[]>, |
| 140 | message: {} as Record<string, Message[]>, |
| 141 | part: {} as Record<string, Part[]>, |
| 142 | part_text_accum_delta: {} as Record<string, string>, |
| 143 | session_working(id: string) { |
| 144 | return (this.session_status[id]?.type ?? "idle") !== "idle" |
| 145 | }, |
| 146 | }) |
| 147 | const requests = new Map<string, Promise<Session>>() |
| 148 | const inflight = new Map<string, Promise<void>>() |
| 149 | const inflightDiff = new Map<string, Promise<void>>() |
| 150 | const inflightTodo = new Map<string, Promise<void>>() |
| 151 | const optimistic = new Map<string, Map<string, OptimisticItem>>() |
| 152 | const messageLoads = new Map<string, MessageLoadState>() |
| 153 | const pendingParts = new Map<string, Map<string, Set<string>>>() |
| 154 | const orphanParts = new Map<string, Set<string>>() |
| 155 | const removedMessages = new Map<string, Set<string>>() |
| 156 | const deltaBases = new Map<string, { base: string; sessionID: string }>() |
| 157 | const deleteMessageParts = ( |
| 158 | cache: { part: Record<string, Part[] | undefined>; part_text_accum_delta: Record<string, string | undefined> }, |
| 159 | messageID: string, |
| 160 | ) => { |
| 161 | for (const part of cache.part[messageID] ?? []) { |
| 162 | delete cache.part_text_accum_delta[part.id] |
| 163 | deltaBases.delete(part.id) |
| 164 | } |
| 165 | delete cache.part[messageID] |
| 166 | } |
| 167 | const seen = new Set<string>() |
| 168 | const infoSeen = new Set<string>() |
| 169 | const pinned = new Map<string, number>() |
| 170 | const generations = new Map<string, object>() |
| 171 | const generation = (sessionID: string) => { |
| 172 | const current = generations.get(sessionID) |
| 173 | if (current) return current |
| 174 | const created = {} |
| 175 | generations.set(sessionID, created) |
| 176 | return created |
| 177 | } |
| 178 | const [meta, setMeta] = createStore({ |
| 179 | limit: {} as Record<string, number | undefined>, |
| 180 | cursor: {} as Record<string, string | undefined>, |
| 181 | complete: {} as Record<string, boolean | undefined>, |
| 182 | loading: {} as Record<string, boolean | undefined>, |
| 183 | at: {} as Record<string, number | undefined>, |
| 184 | }) |
| 185 | |
| 186 | const remember = (session: Session) => { |
| 187 | setData("info", session.id, reconcile(session)) |
| 188 | infoSeen.delete(session.id) |
| 189 | infoSeen.add(session.id) |
no outgoing calls