Extract message runtime handoff helpers
This commit is contained in:
@@ -141,10 +141,6 @@
|
|||||||
"maxFunctionLines": 239,
|
"maxFunctionLines": 239,
|
||||||
"owner": "message executor hotspot"
|
"owner": "message executor hotspot"
|
||||||
},
|
},
|
||||||
"src/message-runtime-handoffs.ts": {
|
|
||||||
"maxFunctionLines": 207,
|
|
||||||
"owner": "message runtime handoffs hotspot"
|
|
||||||
},
|
|
||||||
"src/message-runtime-queue.test.ts": {
|
"src/message-runtime-queue.test.ts": {
|
||||||
"maxFunctionLines": 366,
|
"maxFunctionLines": 366,
|
||||||
"owner": "message runtime queue test hotspot"
|
"owner": "message runtime queue test hotspot"
|
||||||
|
|||||||
@@ -19,7 +19,12 @@ import {
|
|||||||
import { buildPairedTurnIdentity } from './paired-turn-identity.js';
|
import { buildPairedTurnIdentity } from './paired-turn-identity.js';
|
||||||
import type { ScheduledPairedFollowUpIntentKind } from './paired-follow-up-scheduler.js';
|
import type { ScheduledPairedFollowUpIntentKind } from './paired-follow-up-scheduler.js';
|
||||||
import type { ExecuteTurnFn } from './message-runtime-types.js';
|
import type { ExecuteTurnFn } from './message-runtime-types.js';
|
||||||
import type { Channel, PairedTask, RegisteredGroup } from './types.js';
|
import type {
|
||||||
|
Channel,
|
||||||
|
PairedRoomRole,
|
||||||
|
PairedTask,
|
||||||
|
RegisteredGroup,
|
||||||
|
} from './types.js';
|
||||||
|
|
||||||
export function enqueuePendingHandoffs(args: {
|
export function enqueuePendingHandoffs(args: {
|
||||||
enqueueTask: (
|
enqueueTask: (
|
||||||
@@ -158,6 +163,61 @@ function failClaimedHandoff(args: {
|
|||||||
requeueFailedClaimedPairedTurn(args);
|
requeueFailedClaimedPairedTurn(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function resolveHandoffDeliveryChannel(args: {
|
||||||
|
handoff: ServiceHandoff;
|
||||||
|
handoffRole: PairedRoomRole;
|
||||||
|
fallbackChannel: Channel;
|
||||||
|
channels: Channel[];
|
||||||
|
getPairedTaskById?:
|
||||||
|
| ((
|
||||||
|
id: string,
|
||||||
|
) =>
|
||||||
|
| Pick<PairedTask, 'id' | 'status' | 'round_trip_count' | 'updated_at'>
|
||||||
|
| undefined)
|
||||||
|
| undefined;
|
||||||
|
enqueueMessageCheck?: ((chatJid: string) => void) | undefined;
|
||||||
|
}): Channel | undefined {
|
||||||
|
const { handoff, handoffRole } = args;
|
||||||
|
if (handoffRole === 'owner') {
|
||||||
|
return args.fallbackChannel;
|
||||||
|
}
|
||||||
|
|
||||||
|
const roleChannel = findChannelByName(
|
||||||
|
args.channels,
|
||||||
|
getFixedRoleChannelName(handoffRole),
|
||||||
|
);
|
||||||
|
if (!roleChannel) {
|
||||||
|
failClaimedHandoff({
|
||||||
|
handoff,
|
||||||
|
error: getMissingRoleChannelMessage(handoffRole),
|
||||||
|
getPairedTaskById: args.getPairedTaskById,
|
||||||
|
enqueueMessageCheck: args.enqueueMessageCheck,
|
||||||
|
});
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return roleChannel;
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildHandoffPairedTurnIdentity(
|
||||||
|
handoff: ServiceHandoff,
|
||||||
|
handoffRole: PairedRoomRole,
|
||||||
|
) {
|
||||||
|
if (
|
||||||
|
!handoff.paired_task_id ||
|
||||||
|
!handoff.paired_task_updated_at ||
|
||||||
|
!handoff.turn_intent_kind
|
||||||
|
) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return buildPairedTurnIdentity({
|
||||||
|
taskId: handoff.paired_task_id,
|
||||||
|
taskUpdatedAt: handoff.paired_task_updated_at,
|
||||||
|
intentKind: handoff.turn_intent_kind,
|
||||||
|
role: handoff.turn_role ?? handoffRole,
|
||||||
|
turnId: handoff.turn_id,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export async function processClaimedHandoff(args: {
|
export async function processClaimedHandoff(args: {
|
||||||
handoff: ServiceHandoff;
|
handoff: ServiceHandoff;
|
||||||
getRoomBindings: () => Record<string, RegisteredGroup>;
|
getRoomBindings: () => Record<string, RegisteredGroup>;
|
||||||
@@ -239,52 +299,23 @@ export async function processClaimedHandoff(args: {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let handoffChannel = channel;
|
const handoffChannel = resolveHandoffDeliveryChannel({
|
||||||
if (handoffRole === 'reviewer') {
|
handoff,
|
||||||
const reviewerChannel = findChannelByName(
|
handoffRole,
|
||||||
args.channels,
|
fallbackChannel: channel,
|
||||||
getFixedRoleChannelName('reviewer'),
|
channels: args.channels,
|
||||||
);
|
getPairedTaskById: args.getPairedTaskById,
|
||||||
if (!reviewerChannel) {
|
enqueueMessageCheck: args.enqueueMessageCheck,
|
||||||
failClaimedHandoff({
|
});
|
||||||
handoff,
|
if (!handoffChannel) {
|
||||||
error: getMissingRoleChannelMessage('reviewer'),
|
return;
|
||||||
getPairedTaskById: args.getPairedTaskById,
|
|
||||||
enqueueMessageCheck: args.enqueueMessageCheck,
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
handoffChannel = reviewerChannel;
|
|
||||||
} else if (handoffRole === 'arbiter') {
|
|
||||||
const arbiterChannel = findChannelByName(
|
|
||||||
args.channels,
|
|
||||||
getFixedRoleChannelName('arbiter'),
|
|
||||||
);
|
|
||||||
if (!arbiterChannel) {
|
|
||||||
failClaimedHandoff({
|
|
||||||
handoff,
|
|
||||||
error: getMissingRoleChannelMessage('arbiter'),
|
|
||||||
getPairedTaskById: args.getPairedTaskById,
|
|
||||||
enqueueMessageCheck: args.enqueueMessageCheck,
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
handoffChannel = arbiterChannel;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const runId = `handoff-${handoff.id}`;
|
const runId = `handoff-${handoff.id}`;
|
||||||
const pairedTurnIdentity =
|
const pairedTurnIdentity = buildHandoffPairedTurnIdentity(
|
||||||
handoff.paired_task_id &&
|
handoff,
|
||||||
handoff.paired_task_updated_at &&
|
handoffRole,
|
||||||
handoff.turn_intent_kind
|
);
|
||||||
? buildPairedTurnIdentity({
|
|
||||||
taskId: handoff.paired_task_id,
|
|
||||||
taskUpdatedAt: handoff.paired_task_updated_at,
|
|
||||||
intentKind: handoff.turn_intent_kind,
|
|
||||||
role: handoff.turn_role ?? handoffRole,
|
|
||||||
turnId: handoff.turn_id,
|
|
||||||
})
|
|
||||||
: undefined;
|
|
||||||
try {
|
try {
|
||||||
logger.info(
|
logger.info(
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user