LibreChat/api/server/services/Schedules/index.js
lia-by-librechat[bot] 0eadbc45b1
Some checks are pending
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Codegraph E2E Vote Gate / Gate (push) Waiting to run
Codegraph E2E Votes / vote (shard 1/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 10/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 2/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 3/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 4/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 5/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 6/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 7/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 8/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 9/10) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
Frontend Unit Tests / Client build recovery regression (push) Waiting to run
Frontend Unit Tests / Codegraph select (push) Waiting to run
Frontend Unit Tests / Build packages (push) Waiting to run
Frontend Unit Tests / TypeScript type checks (client) (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/chat (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/client (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 1/2) (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 2/2) (push) Blocked by required conditions
Frontend Unit Tests / Vite build verification (push) Blocked by required conditions
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Waiting to run
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Blocked by required conditions
🪸 refactor: Separate Shared-Link and Schedule Persistence (#18031)
Co-authored-by: Lia <lia@librechat.ai>
2026-11-10 15:44:13 -05:00

102 lines
4.2 KiB
JavaScript

let service;
/**
* Build the schedule service on first use. Controllers import this facade in many
* focused test/runtime paths that never use scheduling; eagerly loading Config and
* the trigger worker made those paths initialize two unrelated service graphs just
* by requiring a controller.
*/
function getService() {
if (service != null) {
return service;
}
const { createSchedulesService } = require('@librechat/api');
const { getAppConfig } = require('~/server/services/Config/app');
const {
enqueueAgentTrigger,
getAgentTriggerDelivery,
} = require('~/server/services/Agents/triggers');
const { resolveAgentFireAccess } = require('./access');
const methods = require('~/models');
const isUserDeleting = async (userId) => !(await methods.isAgentTriggerPrincipalActive(userId));
service = createSchedulesService({
preflightMCP: require('./mcp'),
methods,
getAppConfig,
findUserById: (userId) => methods.findScheduleOwner(userId.toString()),
findBalance: (userId, options) =>
methods.findBalanceByUser(userId, { includeReservedCredits: true, ...options }),
upsertBalance: (userId, { set, setOnInsert }) =>
methods.upsertBalanceFields(userId, set ?? {}, setOnInsert ?? {}),
initializeNullBalance: methods.initializeNullBalance,
enqueueAgentTrigger,
// Reconciliation reads the durable delivery to tell a still-live admission (a
// deferred Retry-After) or a dead-letter apart from a genuinely orphaned run.
getTriggerDelivery: getAgentTriggerDelivery,
resolveAgentFireAccess,
// Chat projects are user-owned, so this scoped read is both the existence and the
// authorization check the fire-time destination precheck needs.
getChatProject: (userId, projectId) => methods.getChatProject(userId, projectId),
// Reuse the merged trigger/deletion admission fence. A schedule fire and every
// generic trigger now fail closed on the same durable principal state.
isUserDeleting,
});
return service;
}
const invoke =
(method) =>
(...args) =>
getService()[method](...args);
/** Host adapter for the generic generation runtime's approval-expiry seam. The
* callback is intentionally idempotent: a replica relay or schedule reconciliation
* may re-drive the same retained terminal evidence after a transient failure. */
async function recordExpiredScheduleApproval(streamId, job) {
if (!job?.scheduleId || !job?.scheduledFor) {
return;
}
const recorded = await getService().recordScheduleOutcome({
scheduleId: job.scheduleId,
scheduledFor: job.scheduledFor,
streamId,
jobCreatedAt: job.createdAt,
status: 'interrupted',
conversationId: job.conversationId ?? streamId,
error: 'Approval expired before a decision was made',
});
if (!recorded) {
throw new Error(`Failed to settle expired scheduled approval ${job.scheduleId}`);
}
}
module.exports = {
getLimits: invoke('getLimits'),
fireScheduleNow: invoke('fireScheduleNow'),
recordScheduleOutcome: invoke('recordScheduleOutcome'),
registerMCPSettlement: invoke('registerMCPSettlement'),
recordMCPToolAuthFailure: (input) =>
require('@librechat/api').recordScheduledMCPToolAuthFailure(
input,
() => getService().recordMCPToolAuthFailure,
() => getService().getMCPReceiptRetryPolicy(input),
),
beginScheduledStop: invoke('beginScheduledStop'),
acknowledgeScheduledStopPersistence: invoke('acknowledgeScheduledStopPersistence'),
claimScheduleResume: invoke('claimScheduleResume'),
releaseScheduleResumeClaim: invoke('releaseScheduleResumeClaim'),
finalizeScheduleResumeClaim: invoke('finalizeScheduleResumeClaim'),
releaseScheduleResumeFence: invoke('releaseScheduleResumeFence'),
isScheduleLive: invoke('isScheduleLive'),
deleteScheduleForOwner: invoke('deleteScheduleForOwner'),
quiesceUserSchedules: invoke('quiesceUserSchedules'),
restoreUserSchedulesFromDeletion: invoke('restoreUserSchedulesFromDeletion'),
initializeScheduleEngine: invoke('initializeScheduleEngine'),
initializeScheduleErasureSweep: invoke('initializeScheduleErasureSweep'),
recordExpiredScheduleApproval,
isUserDeleting: async (userId) => {
const methods = require('~/models');
return !(await methods.isAgentTriggerPrincipalActive(userId));
},
};