mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
section 3: Phase 3: Auto-accept engine, default stamping and notificati
This commit is contained in:
1 parent
66e6c20f8e
commit
f85531f2d3
6 files changed
+391
-27
No files matched your search
@@ -139,9 +139,12 @@ export function buildEventNotificationPayload(
|
||||
};
|
||||
}
|
||||
|
||||
type EventSuppressor = (event: SSEEvent, sessionId: string | undefined) => Promise<boolean>;
|
||||
|
||||
export class NotificationService {
|
||||
private vapidConfig: VapidConfig | null = null;
|
||||
private settingsService: SettingsService;
|
||||
private eventSuppressors: EventSuppressor[] = [];
|
||||
|
||||
constructor(private db: Database) {
|
||||
this.settingsService = new SettingsService(db);
|
||||
@@ -178,6 +181,10 @@ export class NotificationService {
|
||||
return this.vapidConfig?.publicKey ?? null;
|
||||
}
|
||||
|
||||
addEventSuppressor(suppressor: EventSuppressor): void {
|
||||
this.eventSuppressors.push(suppressor);
|
||||
}
|
||||
|
||||
isConfigured(): boolean {
|
||||
return this.vapidConfig !== null;
|
||||
}
|
||||
@@ -321,6 +328,10 @@ export class NotificationService {
|
||||
if (sessionId && sseAggregator.isSessionBeingViewed(sessionId)) return;
|
||||
if (sessionId && sseAggregator.isSubagentSession(sessionId)) return;
|
||||
|
||||
for (const suppressor of this.eventSuppressors) {
|
||||
if (await suppressor(event, sessionId)) return;
|
||||
}
|
||||
|
||||
if (!this.isConfigured()) return;
|
||||
|
||||
const userIds = this.getAllUserIds();
|
||||
|
||||
@@ -3,9 +3,17 @@ import type {
|
||||
SessionPermissionMode,
|
||||
SessionPermissionModeState,
|
||||
} from '@opencode-manager/shared/schemas'
|
||||
import { openCodeLocation, type PermissionRequest } from '@opencode-manager/shared/opencode'
|
||||
import { getScheduleRunBySessionId } from '../db/schedules'
|
||||
import { getSessionPermissionMode, setSessionPermissionMode } from '../db/session-permission-modes'
|
||||
import {
|
||||
deleteSessionPermissionMode,
|
||||
getSessionPermissionMode,
|
||||
insertSessionPermissionModeIfAbsent,
|
||||
setSessionPermissionMode,
|
||||
} from '../db/session-permission-modes'
|
||||
import { logger } from '../utils/logger'
|
||||
import type { OpenCodeClient } from './opencode/client'
|
||||
import type { SSEEvent } from './sse-aggregator'
|
||||
import type { SettingsService } from './settings'
|
||||
|
||||
const MAX_PARENT_HOPS = 10
|
||||
@@ -46,28 +54,36 @@ export class SessionPermissionModeService {
|
||||
return current
|
||||
}
|
||||
|
||||
async handleEvent(directory: string, event: SSEEvent): Promise<void> {
|
||||
switch (event.type) {
|
||||
case 'session.created': {
|
||||
const { sessionID, parentID } = event.data
|
||||
this.rememberParent(sessionID, parentID)
|
||||
if (!parentID && this.defaultMode() === 'auto') {
|
||||
insertSessionPermissionModeIfAbsent(this.db, sessionID, 'auto')
|
||||
}
|
||||
return
|
||||
}
|
||||
case 'permission.asked': {
|
||||
const { sessionID, id } = event.data
|
||||
await this.autoAcceptRequest(sessionID, id)
|
||||
return
|
||||
}
|
||||
case 'session.deleted': {
|
||||
const { sessionID } = event.data
|
||||
this.parentBySession.delete(sessionID)
|
||||
deleteSessionPermissionMode(this.db, sessionID)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async getEffectiveMode(sessionId: string): Promise<SessionPermissionModeState> {
|
||||
let rootSessionId: string
|
||||
|
||||
try {
|
||||
rootSessionId = await this.resolveRootSessionId(sessionId)
|
||||
} catch {
|
||||
return { sessionId, rootSessionId: sessionId, mode: 'ask', inherited: false }
|
||||
}
|
||||
|
||||
const inherited = rootSessionId !== sessionId
|
||||
|
||||
if (getScheduleRunBySessionId(this.db, rootSessionId)) {
|
||||
return { sessionId, rootSessionId, mode: 'ask', inherited }
|
||||
}
|
||||
|
||||
const stored = getSessionPermissionMode(this.db, rootSessionId)
|
||||
return { sessionId, rootSessionId, mode: stored ?? 'ask', inherited }
|
||||
const rootSessionId = await this.resolveRootSessionIdOrNull(sessionId)
|
||||
return this.effectiveModeForRoot(sessionId, rootSessionId)
|
||||
}
|
||||
|
||||
async setMode(sessionId: string, mode: SessionPermissionMode, directory: string): Promise<SessionPermissionModeState> {
|
||||
void directory
|
||||
|
||||
const rootSessionId = await this.resolveRootSessionId(sessionId)
|
||||
if (rootSessionId !== sessionId) {
|
||||
throw new SessionPermissionModeError(
|
||||
@@ -84,6 +100,9 @@ export class SessionPermissionModeService {
|
||||
}
|
||||
|
||||
setSessionPermissionMode(this.db, sessionId, mode)
|
||||
if (mode === 'auto') {
|
||||
await this.acceptPendingRequests(sessionId, directory)
|
||||
}
|
||||
return { sessionId, rootSessionId, mode, inherited: false }
|
||||
}
|
||||
|
||||
@@ -91,6 +110,77 @@ export class SessionPermissionModeService {
|
||||
return this.settingsService.getSettings().preferences.sessionDefaults?.permissionMode ?? 'ask'
|
||||
}
|
||||
|
||||
private effectiveModeForRoot(
|
||||
sessionId: string,
|
||||
rootSessionId: string | null,
|
||||
): SessionPermissionModeState {
|
||||
if (!rootSessionId) {
|
||||
return { sessionId, rootSessionId: sessionId, mode: 'ask', inherited: false }
|
||||
}
|
||||
|
||||
const inherited = rootSessionId !== sessionId
|
||||
|
||||
if (getScheduleRunBySessionId(this.db, rootSessionId)) {
|
||||
return { sessionId, rootSessionId, mode: 'ask', inherited }
|
||||
}
|
||||
|
||||
const stored = getSessionPermissionMode(this.db, rootSessionId)
|
||||
return { sessionId, rootSessionId, mode: stored ?? 'ask', inherited }
|
||||
}
|
||||
|
||||
private async acceptPendingRequests(rootSessionId: string, directory: string): Promise<void> {
|
||||
let requests: PermissionRequest[]
|
||||
try {
|
||||
const result = await this.openCodeClient.api.permission.request.list(openCodeLocation(directory))
|
||||
requests = result.data
|
||||
} catch (error) {
|
||||
logger.error(`Failed to list pending permission requests for session ${rootSessionId}:`, error)
|
||||
return
|
||||
}
|
||||
|
||||
for (const request of requests) {
|
||||
await this.autoAcceptRequest(request.sessionID, request.id, rootSessionId)
|
||||
}
|
||||
}
|
||||
|
||||
private async autoAcceptRequest(
|
||||
sessionID: string,
|
||||
requestID: string,
|
||||
expectedRootSessionId?: string,
|
||||
): Promise<void> {
|
||||
const rootSessionId = await this.resolveRootSessionIdOrNull(sessionID)
|
||||
if (!rootSessionId) {
|
||||
return
|
||||
}
|
||||
|
||||
if (expectedRootSessionId !== undefined && rootSessionId !== expectedRootSessionId) {
|
||||
return
|
||||
}
|
||||
|
||||
if (this.effectiveModeForRoot(sessionID, rootSessionId).mode !== 'auto') {
|
||||
return
|
||||
}
|
||||
|
||||
await this.replyOnce(sessionID, requestID)
|
||||
}
|
||||
|
||||
private async replyOnce(sessionID: string, requestID: string): Promise<void> {
|
||||
try {
|
||||
await this.openCodeClient.api.permission.reply({ sessionID, requestID, decision: 'once' })
|
||||
logger.info(`Auto-accepted permission request ${requestID} for session ${sessionID}`)
|
||||
} catch (error) {
|
||||
logger.error(`Failed to auto-accept permission request ${requestID} for session ${sessionID}:`, error)
|
||||
}
|
||||
}
|
||||
|
||||
private async resolveRootSessionIdOrNull(sessionId: string): Promise<string | null> {
|
||||
try {
|
||||
return await this.resolveRootSessionId(sessionId)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
private async getParentId(sessionId: string): Promise<string | null> {
|
||||
if (this.parentBySession.has(sessionId)) {
|
||||
return this.parentBySession.get(sessionId) ?? null
|
||||
|
||||
Reference in new issue
Block a user