| 1 | import { hasSessionGeneration, sessionIdentityStableKey } from "./sessionIdentity"; |
| 2 | import { interactionInstanceKey, sameInteractionIdentity, type InteractionKind, type InteractionTarget } from "./interactionTarget"; |
| 3 | import type { Meta, WireApproval, WireAsk, WireMCPInteraction } from "./types"; |
| 4 | |
| 5 | type Prompt = WireApproval | WireAsk | WireMCPInteraction; |
| 6 | |
| 7 | export interface InteractionState { |
| 8 | meta?: Meta; |
| 9 | activeTurnId?: string; |
| 10 | approval?: WireApproval; |
| 11 | ask?: WireAsk; |
| 12 | mcpInteraction?: WireMCPInteraction; |
| 13 | } |
| 14 | |
| 15 | export function promptForInteraction(state: InteractionState, target: Pick<InteractionTarget, "kind">): Prompt | undefined { |
| 16 | if (target.kind === "ask") return state.ask; |
| 17 | if (target.kind === "mcp") return state.mcpInteraction; |
| 18 | return state.approval; |
| 19 | } |
| 20 | |
| 21 | export function stateOwnsInteraction(state: InteractionState, target: InteractionTarget): boolean { |
| 22 | if (!target.sessionId || !hasSessionGeneration(target.sessionGeneration) || !state.meta?.session?.sessionId || !hasSessionGeneration(state.meta.sessionGeneration)) return false; |
| 23 | if (state.meta.session.sessionId !== target.sessionId || state.meta.sessionGeneration !== target.sessionGeneration) return false; |
| 24 | if (target.hostId && state.meta.session.hostId !== target.hostId) return false; |
| 25 | const currentSessionKey = sessionIdentityStableKey(state.meta); |
| 26 | if (currentSessionKey && target.sessionKey && currentSessionKey !== target.sessionKey) return false; |
| 27 | const prompt = promptForInteraction(state, target); |
| 28 | if (!sameInteractionIdentity(prompt, target)) return false; |
| 29 | if (target.requestGeneration !== undefined && prompt && "generation" in prompt && prompt.generation !== target.requestGeneration) return false; |
| 30 | if (target.permissionRevision !== undefined && prompt && "permissionRevision" in prompt && prompt.permissionRevision !== target.permissionRevision) return false; |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | export function promptInstanceKeyForState(state: InteractionState, prompt: Prompt, kind: InteractionKind): string { |
| 35 | return interactionInstanceKey({ |
| 36 | tabId: "", |
| 37 | sessionKey: sessionIdentityStableKey(state.meta), |
| 38 | hostId: state.meta?.session?.hostId, |
| 39 | sessionId: state.meta?.session?.sessionId, |
| 40 | sessionGeneration: state.meta?.sessionGeneration, |
| 41 | promptId: prompt.id, |
| 42 | turnId: prompt.turnId, |
| 43 | runtimeEpoch: prompt.runtimeEpoch, |
| 44 | kind, |
| 45 | requestGeneration: "generation" in prompt ? prompt.generation : undefined, |
| 46 | permissionRevision: "permissionRevision" in prompt ? prompt.permissionRevision : undefined, |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | export function interactionTargetFromState( |
| 51 | tabId: string, |
| 52 | state: InteractionState | undefined, |
| 53 | kind: InteractionKind, |
| 54 | promptId: string, |
| 55 | ): InteractionTarget { |
| 56 | const prompt = state ? promptForInteraction(state, { kind }) : undefined; |
| 57 | const base = { |
| 58 | tabId, |
| 59 | sessionKey: sessionIdentityStableKey(state?.meta) || tabId, |
| 60 | hostId: state?.meta?.session?.hostId, |
| 61 | sessionId: state?.meta?.session?.sessionId, |
| 62 | sessionGeneration: state?.meta?.sessionGeneration, |
| 63 | promptId, |
| 64 | turnId: prompt?.turnId ?? state?.activeTurnId, |
| 65 | runtimeEpoch: prompt?.runtimeEpoch ?? state?.meta?.runtime?.epoch, |
| 66 | kind, |
| 67 | requestGeneration: prompt && "generation" in prompt ? prompt.generation : undefined, |
| 68 | permissionRevision: prompt && "permissionRevision" in prompt ? prompt.permissionRevision : undefined, |
| 69 | }; |
| 70 | return { ...base, instanceKey: interactionInstanceKey(base) }; |
| 71 | } |
| 72 |