mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-05 06:03:51 +01:00
Keep Photon failure ownership inside provider callbacks
This commit is contained in:
1 parent
54ee8e3a34
commit
c010ebe310
2 files changed
+22
-10
No files matched your search
@@ -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) =>
|
||||
|
||||
@@ -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<EndpointRecord | null>;
|
||||
runtimeCallbackEndpoint: Parameters<typeof createChatMessageLifecycle>[1]["runtimeCallbackEndpoint"];
|
||||
renewDiscordGatewayOwnershipForMessageAdmission: ReturnType<typeof createChatReceiverOwnership>["renewDiscordGatewayOwnershipForMessageAdmission"];
|
||||
failureOwners: {
|
||||
retireClosedLocal: () => Promise<PhotonFailureResponsibility>;
|
||||
recordFailure: (error: unknown) => Promise<void | PhotonFailureResponsibility>;
|
||||
retainCleanup: () => Promise<PhotonFailureResponsibility>;
|
||||
};
|
||||
};
|
||||
export function createPhotonRuntimeCallbacks(db: Db, options: PhotonRuntimeOptions): Pick<ChatSdkRuntimeCallbacks, "onPhotonAssertOwned" | "onPhotonCheckpoint"> {
|
||||
export function createPhotonRuntimeCallbacks(db: Db, options: PhotonRuntimeOptions): Pick<ChatSdkRuntimeCallbacks, "onPhotonAssertOwned" | "onPhotonCheckpoint" | "onPhotonFailure"> {
|
||||
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) {
|
||||
|
||||
Reference in new issue
Block a user