diff --git a/server/src/services/chat-channels.ts b/server/src/services/chat-channels.ts index d8900fd6bb1..18f7263c073 100644 --- a/server/src/services/chat-channels.ts +++ b/server/src/services/chat-channels.ts @@ -1113,7 +1113,13 @@ export function chatChannelService(db: Db, options: ChatChannelServiceOptions) { enableDiscordGateway: discordOwnership !== null, callbacks: { onMessage: (event) => handleSdkMessage(event, context), - ...createPhotonRuntimeCallbacks(db, { endpointId: endpoint.id, connectionId: endpoint.connectionId, companyId: record.endpoint.companyId, assignedAgentId: record.endpoint.assignedAgentId, context, lineId: credentials.lineId, runtimeCallbackRecord, runtimeCallbackEndpoint, renewDiscordGatewayOwnershipForMessageAdmission }), + ...createPhotonRuntimeCallbacks(db, { endpointId: endpoint.id, connectionId: endpoint.connectionId, companyId: record.endpoint.companyId, assignedAgentId: record.endpoint.assignedAgentId, context, lineId: credentials.lineId, runtimeCallbackRecord, runtimeCallbackEndpoint, renewDiscordGatewayOwnershipForMessageAdmission, + failureOwners: { + retireClosedLocal: () => retiredLocalFailure(record.endpoint, context, photonConstructionAllocation === null), + recordFailure: (error) => handlePhotonFailure(endpoint.id, error, context), + retainCleanup: () => photonExternalRuntimeOwner.cleanupFailure(endpoint.id, context), + }, + }), onPhotonUsefulClientDebt: () => photonExternalRuntimeOwner.senderDebt(endpoint.id, context), onPhotonSenderRuntimeClosed: photonSenderAllocation ? () => photonExternalRuntimeOwner.senderRuntimeClosed(photonSenderAllocation, context) : undefined, @@ -1123,14 +1129,6 @@ export function chatChannelService(db: Db, options: ChatChannelServiceOptions) { return external ? { kind: "cloud", bindingId: external.binding.bindingId } : { kind: "local" }; }, onPhotonEvent: (event) => handlePhotonEvent(endpoint.id, event, context), - onPhotonFailure: async (error, cleanup) => { - if (cleanup?.localClosed) { - const retired = await retiredLocalFailure(record.endpoint, context, photonConstructionAllocation === null); - if (retired.kind === "retired-local") return retired; - } - const result = await handlePhotonFailure(endpoint.id, error, context); - return result?.kind === "recorded" ? result : photonExternalRuntimeOwner.cleanupFailure(endpoint.id, context); - }, onDiscordRootMentionAdmission: record.endpoint.provider === "discord" ? (event) => diff --git a/server/src/services/chat-channels/imessage-photon/runtime-callbacks.ts b/server/src/services/chat-channels/imessage-photon/runtime-callbacks.ts index 8a83a98ed94..89c9c39d5a2 100644 --- a/server/src/services/chat-channels/imessage-photon/runtime-callbacks.ts +++ b/server/src/services/chat-channels/imessage-photon/runtime-callbacks.ts @@ -6,6 +6,7 @@ import { PhotonState } from "../../photon/state.js"; import { and, eq, inArray, sql } from "drizzle-orm"; import { chatConversations, chatEndpoints, heartbeatRuns, toolConnections } from "@paperclipai/db"; import type { ChatSdkRuntimeCallbacks } from "../../chat-sdk-runtime.js"; +import type { PhotonFailureResponsibility } from "../../photon/receiver.js"; import type { Db } from "@paperclipai/db"; import type { RuntimeContext, EndpointRow, EndpointRecord, LiveInboundMessage } from "../runtime-types.js"; import type { createChatReceiverOwnership } from "../runtime/ownership.js"; @@ -15,10 +16,23 @@ type PhotonRuntimeOptions = { runtimeCallbackRecord: (endpointId: string, context: RuntimeContext, statuses: EndpointRow["status"][]) => Promise; runtimeCallbackEndpoint: Parameters[1]["runtimeCallbackEndpoint"]; renewDiscordGatewayOwnershipForMessageAdmission: ReturnType["renewDiscordGatewayOwnershipForMessageAdmission"]; + failureOwners: { + retireClosedLocal: () => Promise; + recordFailure: (error: unknown) => Promise; + retainCleanup: () => Promise; + }; }; -export function createPhotonRuntimeCallbacks(db: Db, options: PhotonRuntimeOptions): Pick { +export function createPhotonRuntimeCallbacks(db: Db, options: PhotonRuntimeOptions): Pick { const { endpointId, connectionId, companyId, assignedAgentId, context, lineId, runtimeCallbackRecord, runtimeCallbackEndpoint, renewDiscordGatewayOwnershipForMessageAdmission } = options; return { + onPhotonFailure: async (error, cleanup) => { + if (cleanup?.localClosed) { + const retired = await options.failureOwners.retireClosedLocal(); + if (retired.kind === "retired-local") return retired; + } + const result = await options.failureOwners.recordFailure(error); + return result?.kind === "recorded" ? result : options.failureOwners.retainCleanup(); + }, onPhotonAssertOwned: async (activeThreadId) => { if (!(await runtimeCallbackRecord(endpointId, context, ["verifying", "active", "attention"]))) throw new Error("Photon receiver ownership changed"); if (activeThreadId) {