mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Enhance permission request handling for multi-session support
This commit is contained in:
1 parent
7cec19f7cc
commit
adbc632f0e
4 files changed
+103
-24
No files matched your search
@@ -14,8 +14,10 @@ import { cn } from '@/lib/utils'
|
||||
interface PermissionRequestDialogProps {
|
||||
permission: Permission | null
|
||||
pendingCount: number
|
||||
isFromDifferentSession?: boolean
|
||||
sessionTitle?: string
|
||||
onRespond: (permissionID: string, sessionID: string, response: PermissionResponse) => Promise<void>
|
||||
onDismiss: (permissionID: string) => void
|
||||
onDismiss: (permissionID: string, sessionID?: string) => void
|
||||
}
|
||||
|
||||
function getPermissionTypeLabel(type: string): string {
|
||||
@@ -107,6 +109,8 @@ function getPermissionDetails(permission: Permission): { primary: string; second
|
||||
export function PermissionRequestDialog({
|
||||
permission,
|
||||
pendingCount,
|
||||
isFromDifferentSession,
|
||||
sessionTitle,
|
||||
onRespond,
|
||||
onDismiss,
|
||||
}: PermissionRequestDialogProps) {
|
||||
@@ -122,7 +126,7 @@ export function PermissionRequestDialog({
|
||||
await onRespond(permission.id, permission.sessionID, response)
|
||||
} catch (error) {
|
||||
console.error('Failed to respond to permission:', error)
|
||||
onDismiss(permission.id)
|
||||
onDismiss(permission.id, permission.sessionID)
|
||||
} finally {
|
||||
setIsLoading(false)
|
||||
setLoadingAction(null)
|
||||
@@ -132,6 +136,7 @@ export function PermissionRequestDialog({
|
||||
const typeLabel = getPermissionTypeLabel(permission.type)
|
||||
const details = getPermissionDetails(permission)
|
||||
const hasMultiple = pendingCount > 1
|
||||
const displaySessionName = sessionTitle || `Session ${permission.sessionID.slice(0, 8)}...`
|
||||
|
||||
return (
|
||||
<Dialog open={true} onOpenChange={() => {}}>
|
||||
@@ -173,9 +178,15 @@ export function PermissionRequestDialog({
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="text-xs text-muted-foreground">
|
||||
Session: <span className="font-mono">{permission.sessionID.slice(0, 12)}...</span>
|
||||
</div>
|
||||
{isFromDifferentSession ? (
|
||||
<div className="text-xs bg-amber-500/10 text-amber-600 dark:text-amber-400 border border-amber-500/20 rounded-md px-2 py-1.5">
|
||||
From another session: <span className="font-medium">{displaySessionName}</span>
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-xs text-muted-foreground">
|
||||
Session: <span className="font-medium">{displaySessionName}</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<DialogFooter className="flex-row gap-2 sm:justify-between">
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useState, useEffect, useCallback } from 'react'
|
||||
import { useState, useEffect, useCallback, useMemo } from 'react'
|
||||
import type { Permission } from '@/api/types'
|
||||
|
||||
type PermissionEventType = 'add' | 'remove'
|
||||
@@ -6,6 +6,7 @@ type PermissionEventType = 'add' | 'remove'
|
||||
interface PermissionEvent {
|
||||
type: PermissionEventType
|
||||
permission?: Permission
|
||||
sessionID?: string
|
||||
permissionID?: string
|
||||
}
|
||||
|
||||
@@ -25,37 +26,98 @@ export const permissionEvents = {
|
||||
}
|
||||
}
|
||||
|
||||
export function usePermissionRequests() {
|
||||
const [permissions, setPermissions] = useState<Permission[]>([])
|
||||
type PermissionsBySession = Record<string, Permission[]>
|
||||
|
||||
export function usePermissionRequests(currentSessionID?: string) {
|
||||
const [permissionsBySession, setPermissionsBySession] = useState<PermissionsBySession>({})
|
||||
|
||||
useEffect(() => {
|
||||
const unsubscribe = permissionEvents.subscribe((event) => {
|
||||
if (event.type === 'add' && event.permission) {
|
||||
setPermissions(prev => {
|
||||
const exists = prev.some(p => p.id === event.permission!.id)
|
||||
if (exists) return prev
|
||||
return [...prev, event.permission!]
|
||||
const permission = event.permission
|
||||
const sessionID = permission.sessionID
|
||||
|
||||
setPermissionsBySession(prev => {
|
||||
const sessionPermissions = prev[sessionID] ?? []
|
||||
const existingIndex = sessionPermissions.findIndex(p => p.id === permission.id)
|
||||
|
||||
if (existingIndex >= 0) {
|
||||
const updated = [...sessionPermissions]
|
||||
updated[existingIndex] = permission
|
||||
return { ...prev, [sessionID]: updated }
|
||||
}
|
||||
|
||||
return { ...prev, [sessionID]: [...sessionPermissions, permission] }
|
||||
})
|
||||
} else if (event.type === 'remove' && event.permissionID && event.sessionID) {
|
||||
const { sessionID, permissionID } = event
|
||||
|
||||
setPermissionsBySession(prev => {
|
||||
const sessionPermissions = prev[sessionID]
|
||||
if (!sessionPermissions) return prev
|
||||
|
||||
const filtered = sessionPermissions.filter(p => p.id !== permissionID)
|
||||
if (filtered.length === 0) {
|
||||
const { [sessionID]: _, ...rest } = prev
|
||||
return rest
|
||||
}
|
||||
return { ...prev, [sessionID]: filtered }
|
||||
})
|
||||
} else if (event.type === 'remove' && event.permissionID) {
|
||||
setPermissions(prev => prev.filter(p => p.id !== event.permissionID))
|
||||
}
|
||||
})
|
||||
return unsubscribe
|
||||
}, [])
|
||||
|
||||
const currentPermission = permissions[0] || null
|
||||
const allPermissions = useMemo(() => {
|
||||
return Object.values(permissionsBySession).flat()
|
||||
}, [permissionsBySession])
|
||||
|
||||
const dismissPermission = useCallback((permissionID: string) => {
|
||||
setPermissions(prev => prev.filter(p => p.id !== permissionID))
|
||||
const currentPermission = useMemo(() => {
|
||||
if (currentSessionID) {
|
||||
const sessionPerms = permissionsBySession[currentSessionID] ?? []
|
||||
if (sessionPerms.length > 0) return sessionPerms[0]
|
||||
}
|
||||
return allPermissions[0] ?? null
|
||||
}, [permissionsBySession, currentSessionID, allPermissions])
|
||||
|
||||
const isFromDifferentSession = useMemo(() => {
|
||||
if (!currentPermission || !currentSessionID) return false
|
||||
return currentPermission.sessionID !== currentSessionID
|
||||
}, [currentPermission, currentSessionID])
|
||||
|
||||
const dismissPermission = useCallback((permissionID: string, sessionID?: string) => {
|
||||
setPermissionsBySession(prev => {
|
||||
if (sessionID) {
|
||||
const sessionPermissions = prev[sessionID]
|
||||
if (!sessionPermissions) return prev
|
||||
|
||||
const filtered = sessionPermissions.filter(p => p.id !== permissionID)
|
||||
if (filtered.length === 0) {
|
||||
const { [sessionID]: _, ...rest } = prev
|
||||
return rest
|
||||
}
|
||||
return { ...prev, [sessionID]: filtered }
|
||||
}
|
||||
|
||||
const newState: PermissionsBySession = {}
|
||||
for (const [sid, perms] of Object.entries(prev)) {
|
||||
const filtered = perms.filter(p => p.id !== permissionID)
|
||||
if (filtered.length > 0) {
|
||||
newState[sid] = filtered
|
||||
}
|
||||
}
|
||||
return newState
|
||||
})
|
||||
}, [])
|
||||
|
||||
const clearAllPermissions = useCallback(() => {
|
||||
setPermissions([])
|
||||
setPermissionsBySession({})
|
||||
}, [])
|
||||
|
||||
return {
|
||||
currentPermission,
|
||||
pendingCount: permissions.length,
|
||||
pendingCount: allPermissions.length,
|
||||
isFromDifferentSession,
|
||||
dismissPermission,
|
||||
clearAllPermissions
|
||||
}
|
||||
|
||||
@@ -293,8 +293,12 @@ export const useSSE = (opcodeUrl: string | null | undefined, directory?: string)
|
||||
break
|
||||
|
||||
case 'permission.replied':
|
||||
if ('permissionID' in event.properties) {
|
||||
permissionEvents.emit({ type: 'remove', permissionID: event.properties.permissionID })
|
||||
if ('permissionID' in event.properties && 'sessionID' in event.properties) {
|
||||
permissionEvents.emit({
|
||||
type: 'remove',
|
||||
sessionID: event.properties.sessionID,
|
||||
permissionID: event.properties.permissionID
|
||||
})
|
||||
}
|
||||
break
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import { SessionList } from "@/components/session/SessionList";
|
||||
import { PermissionRequestDialog } from "@/components/session/PermissionRequestDialog";
|
||||
import { FileBrowserSheet } from "@/components/file-browser/FileBrowserSheet";
|
||||
import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog";
|
||||
import { useSession, useAbortSession, useUpdateSession, useOpenCodeClient, useMessages } from "@/hooks/useOpenCode";
|
||||
import { useSession, useSessions, useAbortSession, useUpdateSession, useOpenCodeClient, useMessages } from "@/hooks/useOpenCode";
|
||||
import { OPENCODE_API_ENDPOINT } from "@/config";
|
||||
import { useSSE } from "@/hooks/useSSE";
|
||||
import { useSettings } from "@/hooks/useSettings";
|
||||
@@ -56,14 +56,13 @@ export function SessionDetail() {
|
||||
enabled: !!repoId,
|
||||
});
|
||||
|
||||
const { currentPermission, pendingCount, dismissPermission } = usePermissionRequests();
|
||||
|
||||
const opcodeUrl = OPENCODE_API_ENDPOINT;
|
||||
const openCodeClient = useOpenCodeClient(opcodeUrl, repo?.fullPath);
|
||||
|
||||
const repoDirectory = repo?.fullPath;
|
||||
|
||||
const { data: messages, isLoading: messagesLoading } = useMessages(opcodeUrl, sessionId, repoDirectory);
|
||||
const { data: sessions } = useSessions(opcodeUrl, repoDirectory);
|
||||
|
||||
const { scrollToBottom } = useAutoScroll({
|
||||
containerRef: messageContainerRef,
|
||||
@@ -78,6 +77,7 @@ export function SessionDetail() {
|
||||
repoDirectory,
|
||||
);
|
||||
const { isConnected, isReconnecting } = useSSE(opcodeUrl, repoDirectory);
|
||||
const { currentPermission, pendingCount, isFromDifferentSession, dismissPermission } = usePermissionRequests(sessionId);
|
||||
const abortSession = useAbortSession(opcodeUrl, repoDirectory, sessionId);
|
||||
const updateSession = useUpdateSession(opcodeUrl, repoDirectory);
|
||||
const { open: openSettings } = useSettingsDialog();
|
||||
@@ -291,6 +291,8 @@ export function SessionDetail() {
|
||||
<PermissionRequestDialog
|
||||
permission={currentPermission}
|
||||
pendingCount={pendingCount}
|
||||
isFromDifferentSession={isFromDifferentSession}
|
||||
sessionTitle={currentPermission ? sessions?.find(s => s.id === currentPermission.sessionID)?.title : undefined}
|
||||
onRespond={handlePermissionResponse}
|
||||
onDismiss={dismissPermission}
|
||||
/>
|
||||
|
||||
Reference in new issue
Block a user