mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-31 14:16:39 +01:00
fix(server): wake chat milestones on question claim transitions
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
1e72290f03
commit
2d971ceb5a
4 files changed
+172
-37
No files matched your search
@@ -455,6 +455,9 @@ export function questionResponseDeliveryService(
|
||||
current.lastAttemptAt.getTime() > claimAt.getTime() - claimStaleMs
|
||||
)
|
||||
return null;
|
||||
if (current.status !== "delivering") {
|
||||
await notifyChatRunMilestones(tx, { companyId: current.companyId, issueId: current.issueId });
|
||||
}
|
||||
return tx
|
||||
.update(issueQuestionResponseDeliveries)
|
||||
.set({
|
||||
@@ -593,25 +596,28 @@ export function questionResponseDeliveryService(
|
||||
const nextErrorCount = delivery.errorCount + (options.bounded ? 1 : 0);
|
||||
const exhausted =
|
||||
options.bounded && nextErrorCount >= MAX_DELIVERY_ATTEMPTS;
|
||||
await db
|
||||
.update(issueQuestionResponseDeliveries)
|
||||
.set({
|
||||
// Keep an exhausted claim owned until recordTerminal commits its outcome.
|
||||
status: exhausted ? "delivering" : "pending",
|
||||
...(options.bounded ? { errorCount: nextErrorCount } : {}),
|
||||
lastErrorCode: errorCode,
|
||||
updatedAt: at,
|
||||
})
|
||||
.where(
|
||||
and(
|
||||
eq(issueQuestionResponseDeliveries.id, delivery.id),
|
||||
eq(issueQuestionResponseDeliveries.status, "delivering"),
|
||||
eq(
|
||||
issueQuestionResponseDeliveries.attemptCount,
|
||||
delivery.attemptCount,
|
||||
),
|
||||
),
|
||||
await db.transaction(async (tx) => {
|
||||
const ownedClaim = and(
|
||||
eq(issueQuestionResponseDeliveries.id, delivery.id),
|
||||
eq(issueQuestionResponseDeliveries.status, "delivering"),
|
||||
eq(issueQuestionResponseDeliveries.attemptCount, delivery.attemptCount),
|
||||
);
|
||||
const [current] = await tx.select({ id: issueQuestionResponseDeliveries.id })
|
||||
.from(issueQuestionResponseDeliveries).where(ownedClaim).for("update");
|
||||
if (!current) return;
|
||||
if (!exhausted) {
|
||||
await notifyChatRunMilestones(tx, { companyId: delivery.companyId, issueId: delivery.issueId });
|
||||
}
|
||||
await tx.update(issueQuestionResponseDeliveries)
|
||||
.set({
|
||||
// Keep an exhausted claim owned until recordTerminal commits its outcome.
|
||||
status: exhausted ? "delivering" : "pending",
|
||||
...(options.bounded ? { errorCount: nextErrorCount } : {}),
|
||||
lastErrorCode: errorCode,
|
||||
updatedAt: at,
|
||||
})
|
||||
.where(ownedClaim);
|
||||
});
|
||||
return exhausted;
|
||||
}
|
||||
|
||||
@@ -1303,21 +1309,32 @@ export function questionResponseDeliveryService(
|
||||
async function sweepPending(limit = 50) {
|
||||
const sweepAt = now();
|
||||
const staleAt = new Date(sweepAt.getTime() - claimStaleMs);
|
||||
await db
|
||||
.update(issueQuestionResponseDeliveries)
|
||||
.set({
|
||||
status: "pending",
|
||||
updatedAt: sweepAt,
|
||||
})
|
||||
.where(
|
||||
and(
|
||||
eq(issueQuestionResponseDeliveries.status, "delivering"),
|
||||
or(
|
||||
isNull(issueQuestionResponseDeliveries.lastAttemptAt),
|
||||
lte(issueQuestionResponseDeliveries.lastAttemptAt, staleAt),
|
||||
),
|
||||
await db.transaction(async (tx) => {
|
||||
const staleClaims = await tx.select({
|
||||
id: issueQuestionResponseDeliveries.id,
|
||||
companyId: issueQuestionResponseDeliveries.companyId,
|
||||
issueId: issueQuestionResponseDeliveries.issueId,
|
||||
}).from(issueQuestionResponseDeliveries).where(and(
|
||||
eq(issueQuestionResponseDeliveries.status, "delivering"),
|
||||
or(
|
||||
isNull(issueQuestionResponseDeliveries.lastAttemptAt),
|
||||
lte(issueQuestionResponseDeliveries.lastAttemptAt, staleAt),
|
||||
),
|
||||
);
|
||||
)).orderBy(asc(issueQuestionResponseDeliveries.id)).for("update");
|
||||
if (staleClaims.length === 0) return;
|
||||
const notified = new Set<string>();
|
||||
for (const claim of staleClaims) {
|
||||
const scope = `${claim.companyId}:${claim.issueId}`;
|
||||
if (notified.has(scope)) continue;
|
||||
await notifyChatRunMilestones(tx, claim);
|
||||
notified.add(scope);
|
||||
}
|
||||
for (let offset = 0; offset < staleClaims.length; offset += 500) {
|
||||
await tx.update(issueQuestionResponseDeliveries)
|
||||
.set({ status: "pending", updatedAt: sweepAt })
|
||||
.where(inArray(issueQuestionResponseDeliveries.id, staleClaims.slice(offset, offset + 500).map(claim => claim.id)));
|
||||
}
|
||||
});
|
||||
const ids = await db
|
||||
.select({ interactionId: issueQuestionResponseDeliveries.interactionId })
|
||||
.from(issueQuestionResponseDeliveries)
|
||||
|
||||
Reference in new issue
Block a user