import type { SessionEvent } from './event-bus' /** * Convert an internal session event into the SDK/control message shape that * bridge workers consume on both the legacy WS path and the v2 worker SSE path. */ export function toClientPayload(event: SessionEvent): Record { const payload = event.payload as Record | null const messageUuid = typeof payload?.uuid === 'string' && payload.uuid ? payload.uuid : event.id if (event.type === 'user' || event.type === 'user_message') { // Prefer payload.message.content (preserves image/content-block arrays from // the web UI) over payload.content (flattened text from normalizePayload). // Without this, image blocks sent by the web UI are stripped before // reaching the CLI bridge. Falls back to payload.content (string) and // finally payload.message (string) for legacy callers. const msgContent = (payload?.message as { content?: unknown } | undefined) ?.content const content = msgContent ?? payload?.content ?? (typeof payload?.message === 'string' ? payload.message : '') return { type: 'user', uuid: messageUuid, session_id: event.sessionId, ...(payload?.isSynthetic === true ? { isSynthetic: true } : {}), // SA-mode artifact escape hatch marker (web gallery AI dispatch) — // read by bridge workers to grant the artifact tool pre-turn. ...(payload?.artifact_dispatch === true ? { artifact_dispatch: true } : {}), message: { role: 'user', content, }, } } if ( event.type === 'permission_response' || event.type === 'control_response' ) { const approved = !!payload?.approved const existingResponse = payload?.response as | Record | undefined if (existingResponse) { return { type: 'control_response', response: existingResponse } } const updatedInput = payload?.updated_input as | Record | undefined const updatedPermissions = payload?.updated_permissions as | Record[] | undefined const feedbackMessage = payload?.message as string | undefined return { type: 'control_response', response: { subtype: approved ? 'success' : 'error', request_id: payload?.request_id ?? '', ...(approved ? { response: { behavior: 'allow' as const, // updatedInput is REQUIRED by the worker-side permission // schema (PermissionPromptToolResultSchema.ts). Web approvals // without input edits must send {} — the worker treats an // empty object as "use the original input" (same pattern as // mobile push-notification responses). updatedInput: updatedInput ?? {}, ...(updatedPermissions ? { updatedPermissions } : {}), }, } : { error: 'Permission denied by user', // The worker's deny schema (PermissionPromptToolResultSchema) // requires message — default it so plain Reject parses cleanly // (otherwise the worker falls back to a generic parse-error // deny and loses the user's feedback text). response: { behavior: 'deny' as const, message: feedbackMessage ?? 'Permission denied by user', }, ...(feedbackMessage ? { message: feedbackMessage } : {}), }), }, } } if (event.type === 'interrupt') { return { type: 'control_request', request_id: event.id, request: { subtype: 'interrupt' }, } } if (event.type === 'control_request') { return { type: 'control_request', request_id: payload?.request_id ?? event.id, request: payload?.request ?? payload, } } return { type: event.type, uuid: messageUuid, session_id: event.sessionId, message: payload, } }