mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
feat(servers): link remote OpenCode servers with QR pairing
This commit is contained in:
1 parent
7f5c242fe0
commit
c591c15743
115 files changed
+7630
-566
No files matched your search
@@ -1,9 +1,12 @@
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { sessionIDFromEvent, type V2Event } from '@opencode-manager/shared/opencode'
|
||||
import { buildServerDirectoryKey, LOCAL_OPENCODE_SERVER_ID } from '@opencode-manager/shared/schemas'
|
||||
import { listSessionMessages, readSessionSnapshot } from '@/api/opencode'
|
||||
import { openCodeEventStream } from '@/lib/opencode-event-stream'
|
||||
import { sessionTranscriptQueryKey } from '@/lib/queryInvalidation'
|
||||
import { serverScopedKey } from '@/lib/serverScope'
|
||||
import { useOpenCodeServerId } from '@/contexts/OpenCodeServerContext'
|
||||
import {
|
||||
applySessionEvents,
|
||||
emptySessionTranscript,
|
||||
@@ -21,8 +24,12 @@ interface NewestPageRead {
|
||||
const TRANSCRIPT_FALLBACK_POLL_INTERVAL_MS = 5000
|
||||
|
||||
export function useSessionTranscript(sessionID: string, directory: string) {
|
||||
const serverId = useOpenCodeServerId()
|
||||
const queryClient = useQueryClient()
|
||||
const queryKey = useMemo(() => sessionTranscriptQueryKey(sessionID), [sessionID])
|
||||
const queryKey = useMemo(
|
||||
() => serverScopedKey(sessionTranscriptQueryKey(sessionID), serverId),
|
||||
[sessionID, serverId],
|
||||
)
|
||||
const queuedEventsRef = useRef<V2Event[]>([])
|
||||
const frameRef = useRef<number | null>(null)
|
||||
const connectedRef = useRef<boolean | null>(null)
|
||||
@@ -42,7 +49,7 @@ export function useSessionTranscript(sessionID: string, directory: string) {
|
||||
newestReadRef.current = read
|
||||
let snapshot: SessionSnapshot
|
||||
try {
|
||||
snapshot = await readSessionSnapshot(sessionID)
|
||||
snapshot = await readSessionSnapshot(sessionID, serverId)
|
||||
} finally {
|
||||
if (newestReadRef.current === read) newestReadRef.current = null
|
||||
}
|
||||
@@ -69,7 +76,7 @@ export function useSessionTranscript(sessionID: string, directory: string) {
|
||||
if (next.nextCursor !== current?.nextCursor) cursorGenerationRef.current += 1
|
||||
queryClient.setQueryData<TranscriptCache>(queryKey, next)
|
||||
return next
|
||||
}, [queryClient, queryKey, sessionID])
|
||||
}, [queryClient, queryKey, sessionID, serverId])
|
||||
|
||||
const readNewestPage = useCallback((): Promise<TranscriptCache> => {
|
||||
const read = performNewestPageRead()
|
||||
@@ -92,7 +99,7 @@ export function useSessionTranscript(sessionID: string, directory: string) {
|
||||
fetchingOlderRef.current = true
|
||||
const generation = cursorGenerationRef.current
|
||||
try {
|
||||
const page = await listSessionMessages(sessionID, { cursor })
|
||||
const page = await listSessionMessages(sessionID, { cursor }, serverId)
|
||||
if (cursorGenerationRef.current !== generation) return
|
||||
queryClient.setQueryData<TranscriptCache>(queryKey, (current) => {
|
||||
if (!current) return current
|
||||
@@ -109,7 +116,7 @@ export function useSessionTranscript(sessionID: string, directory: string) {
|
||||
} finally {
|
||||
fetchingOlderRef.current = false
|
||||
}
|
||||
}, [queryClient, queryKey, sessionID])
|
||||
}, [queryClient, queryKey, sessionID, serverId])
|
||||
|
||||
useEffect(() => {
|
||||
if (!sessionID || !directory) return
|
||||
@@ -134,9 +141,10 @@ export function useSessionTranscript(sessionID: string, directory: string) {
|
||||
}
|
||||
|
||||
const subscription = openCodeEventStream.subscribeGlobalMonitor({
|
||||
directories: [directory],
|
||||
directories: [buildServerDirectoryKey(serverId, directory)],
|
||||
onEvent: (data) => {
|
||||
const event = data as V2Event
|
||||
const event = data as V2Event & { serverId?: string }
|
||||
if ((event.serverId ?? LOCAL_OPENCODE_SERVER_ID) !== serverId) return
|
||||
if (sessionIDFromEvent(event) !== sessionID) return
|
||||
newestReadRef.current?.buffered.push(event)
|
||||
queuedEventsRef.current.push(event)
|
||||
@@ -170,7 +178,7 @@ export function useSessionTranscript(sessionID: string, directory: string) {
|
||||
newestReadRef.current = null
|
||||
subscription.dispose()
|
||||
}
|
||||
}, [directory, queryClient, queryKey, readNewestPage, sessionID])
|
||||
}, [directory, queryClient, queryKey, readNewestPage, sessionID, serverId])
|
||||
|
||||
return {
|
||||
messages: query.data?.transcript.messages ?? [],
|
||||
|
||||
Reference in new issue
Block a user