@exulu/backend 5.5.1 → 5.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-ZWFL6YUH.js → chunk-TAXHJJ5R.js} +176 -11
- package/dist/{convert-exulu-tools-to-ai-sdk-tools-C5LYP4UN.js → convert-exulu-tools-to-ai-sdk-tools-BPZSN22N.js} +1 -1
- package/dist/index.cjs +2071 -587
- package/dist/index.d.cts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +1679 -390
- package/ee/queues/import-queue.ts +27 -0
- package/ee/queues/prune-job-results.test.ts +105 -1
- package/ee/queues/prune-job-results.ts +85 -9
- package/ee/queues/queues.ts +1 -0
- package/ee/tokenizer.test.ts +178 -0
- package/ee/tokenizer.ts +80 -6
- package/ee/workers.ts +173 -2
- package/package.json +1 -1
package/ee/workers.ts
CHANGED
|
@@ -42,6 +42,11 @@ import { createRunSession } from "@SRC/exulu/routines/run-session.ts";
|
|
|
42
42
|
import { casJobResultState, parseRunMetadata, upsertWorkflowRunStart } from "@SRC/exulu/routines/run-state.ts";
|
|
43
43
|
import { findLiteLLMModel } from "@SRC/exulu/litellm/catalog.ts";
|
|
44
44
|
import { computeRunCostUsd } from "@SRC/exulu/routines/run-cost.ts";
|
|
45
|
+
import { getS3ObjectStream } from "@SRC/uppy";
|
|
46
|
+
import { runImport, ImportAbort } from "@SRC/exulu/import/run-import.ts";
|
|
47
|
+
import { buildWorkerDeps, finalizeCancelled, parseMetadata } from "@SRC/exulu/import/worker-deps.ts";
|
|
48
|
+
import { importFieldsFor } from "@SRC/exulu/import/mapping.ts";
|
|
49
|
+
import type { ImportJobData } from "@EXULU_TYPES/queue-config.ts";
|
|
45
50
|
|
|
46
51
|
/**
|
|
47
52
|
* Session-backed runs persist messages at each step boundary, so retries must
|
|
@@ -209,9 +214,9 @@ export const createWorkers = async (
|
|
|
209
214
|
type: bullmqJob.data.type,
|
|
210
215
|
});
|
|
211
216
|
|
|
212
|
-
// For long-running processor jobs, set up progress heartbeat to prevent stalling
|
|
217
|
+
// For long-running processor/import jobs, set up progress heartbeat to prevent stalling
|
|
213
218
|
let progressInterval: NodeJS.Timeout | undefined;
|
|
214
|
-
if (bullmqJob.data.type === "processor") {
|
|
219
|
+
if (bullmqJob.data.type === "processor" || bullmqJob.data.type === "import") {
|
|
215
220
|
// Update progress every 25 seconds to keep the job alive
|
|
216
221
|
// This prevents BullMQ from marking the job as stalled during long-running operations
|
|
217
222
|
progressInterval = setInterval(async () => {
|
|
@@ -1109,6 +1114,172 @@ export const createWorkers = async (
|
|
|
1109
1114
|
};
|
|
1110
1115
|
}
|
|
1111
1116
|
|
|
1117
|
+
if (data.type === "import") {
|
|
1118
|
+
console.log("[EXULU] running a server-side bulk import job.", bullmqJob.name);
|
|
1119
|
+
|
|
1120
|
+
if (!data.jobResultId) {
|
|
1121
|
+
throw new Error(`No jobResultId set for import job ${bullmqJob.id}.`);
|
|
1122
|
+
}
|
|
1123
|
+
if (!data.context) {
|
|
1124
|
+
throw new Error(`No context id set for import job ${bullmqJob.id}.`);
|
|
1125
|
+
}
|
|
1126
|
+
// Narrowed, exact shape of this job type's data (types/queue-config.ts)
|
|
1127
|
+
// now that the two required fields above are confirmed present.
|
|
1128
|
+
const importData: ImportJobData = {
|
|
1129
|
+
type: "import",
|
|
1130
|
+
jobResultId: data.jobResultId,
|
|
1131
|
+
context: data.context,
|
|
1132
|
+
user: data.user,
|
|
1133
|
+
role: data.role,
|
|
1134
|
+
};
|
|
1135
|
+
const jobResultId = importData.jobResultId;
|
|
1136
|
+
|
|
1137
|
+
// The StartImport mutation (Task 5) inserts this row (state
|
|
1138
|
+
// "waiting", job_id = its own id) before enqueuing -- load it by
|
|
1139
|
+
// jobResultId (== job_id == bullmqJob.id) for its trigger_metadata
|
|
1140
|
+
// (s3Key/mapping/access/options/user) and current metadata.
|
|
1141
|
+
const row = await db
|
|
1142
|
+
.from("job_results")
|
|
1143
|
+
.where({ id: jobResultId })
|
|
1144
|
+
.first();
|
|
1145
|
+
if (!row) {
|
|
1146
|
+
throw new Error(
|
|
1147
|
+
`job_results row ${jobResultId} not found for import job ${bullmqJob.id}.`,
|
|
1148
|
+
);
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
// Stalled re-delivery guard: `attempts: 1` stops *failure*
|
|
1152
|
+
// retries, but BullMQ's stalled recovery (lockDuration 5 min,
|
|
1153
|
+
// stalledInterval 2 min, maxStalledCount 1 below) still moves
|
|
1154
|
+
// the job back to `wait` once after a worker crash, a long
|
|
1155
|
+
// event-loop block, or a Redis gap. Restarting the stream from
|
|
1156
|
+
// row 0 would re-create every key-less row a second time and
|
|
1157
|
+
// reset the counters, so a second pickup fails loudly instead:
|
|
1158
|
+
// the "failed" handler records this message on the row, which
|
|
1159
|
+
// also reopens the context's one-import gate (D6).
|
|
1160
|
+
const priorMetadata = parseMetadata(row.metadata);
|
|
1161
|
+
const priorProcessed = Number(priorMetadata.processed ?? 0);
|
|
1162
|
+
if (row.state === JOB_STATUS_ENUM.active || priorProcessed > 0) {
|
|
1163
|
+
throw new Error(
|
|
1164
|
+
`IMPORT_RESTARTED_AFTER_STALL: import ${jobResultId} was already running; ` +
|
|
1165
|
+
`partial rows are kept, start a new import`,
|
|
1166
|
+
);
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
// Moves the row to "active" (CAS-free: the row is keyed by
|
|
1170
|
+
// job_id, owned by this one worker pickup).
|
|
1171
|
+
await upsertJobStart(db, bullmqJob, row.label, "import");
|
|
1172
|
+
|
|
1173
|
+
const context = contexts.find((c) => c.id === importData.context);
|
|
1174
|
+
if (!context) {
|
|
1175
|
+
throw new Error(`Context ${importData.context} not found in the registry.`);
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
const triggerMetadata = parseRunMetadata(row.trigger_metadata) as {
|
|
1179
|
+
s3Key?: string;
|
|
1180
|
+
fileName?: string;
|
|
1181
|
+
mapping?: any;
|
|
1182
|
+
access?: any;
|
|
1183
|
+
options?: { skipEmbeddings?: boolean; delimiter?: string };
|
|
1184
|
+
user?: unknown;
|
|
1185
|
+
};
|
|
1186
|
+
if (!triggerMetadata.s3Key) {
|
|
1187
|
+
throw new Error(
|
|
1188
|
+
`No s3Key recorded on job_results row ${jobResultId} for import job.`,
|
|
1189
|
+
);
|
|
1190
|
+
}
|
|
1191
|
+
if (!triggerMetadata.access) {
|
|
1192
|
+
throw new Error(
|
|
1193
|
+
`No batch access recorded on job_results row ${jobResultId} for import job.`,
|
|
1194
|
+
);
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
// trigger_metadata.user is a snapshot of the acting user
|
|
1198
|
+
// ({ id, super_admin, role, team }) taken at StartImport time --
|
|
1199
|
+
// the top-level importData.user/importData.role on the BullMQ
|
|
1200
|
+
// job are plain ids/strings and carry neither super_admin nor
|
|
1201
|
+
// team, which canWrite needs. The snapshot may also just be a
|
|
1202
|
+
// bare id (number or numeric string); `snap ?? importData.user`
|
|
1203
|
+
// only reaches that branch when `snap` itself is not an object,
|
|
1204
|
+
// so a bare-number snapshot (not just a wholly absent one) also
|
|
1205
|
+
// falls back correctly -- `triggerMetadata.user ?? {...}` did
|
|
1206
|
+
// not (a bare number is not nullish, so it short-circuited the
|
|
1207
|
+
// fallback and `importUser` ended up as that raw number instead
|
|
1208
|
+
// of an object with an `.id`).
|
|
1209
|
+
const snap = triggerMetadata.user as
|
|
1210
|
+
| { id?: unknown; super_admin?: unknown; role?: unknown; team?: unknown }
|
|
1211
|
+
| number
|
|
1212
|
+
| string
|
|
1213
|
+
| undefined;
|
|
1214
|
+
const importUser =
|
|
1215
|
+
snap && typeof snap === "object"
|
|
1216
|
+
? {
|
|
1217
|
+
id: Number(snap.id),
|
|
1218
|
+
super_admin: snap.super_admin === true,
|
|
1219
|
+
role: snap.role,
|
|
1220
|
+
team: snap.team,
|
|
1221
|
+
}
|
|
1222
|
+
: { id: Number(snap ?? importData.user), role: importData.role };
|
|
1223
|
+
if (!importUser.id) {
|
|
1224
|
+
throw new Error(`No acting user recorded for import job ${bullmqJob.id}.`);
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
// trigger_metadata.s3Key arrives in the frontend's "bucket/key"
|
|
1228
|
+
// form (same as uploadFile's return value) -- getS3ObjectStream
|
|
1229
|
+
// wants the bare key.
|
|
1230
|
+
const bareS3Key = triggerMetadata.s3Key.slice(triggerMetadata.s3Key.indexOf("/") + 1);
|
|
1231
|
+
const stream = await getS3ObjectStream(bareS3Key, config);
|
|
1232
|
+
|
|
1233
|
+
try {
|
|
1234
|
+
const deps = buildWorkerDeps({
|
|
1235
|
+
db,
|
|
1236
|
+
config,
|
|
1237
|
+
context,
|
|
1238
|
+
contextId: importData.context,
|
|
1239
|
+
jobResultId,
|
|
1240
|
+
stream,
|
|
1241
|
+
fields: importFieldsFor(context.fields),
|
|
1242
|
+
mapping: triggerMetadata.mapping,
|
|
1243
|
+
access: triggerMetadata.access,
|
|
1244
|
+
options: triggerMetadata.options ?? {},
|
|
1245
|
+
user: importUser,
|
|
1246
|
+
log: (m: string) => console.log(`[EXULU] import ${jobResultId}: ${m}`),
|
|
1247
|
+
// The row count StartImport seeded into metadata.total
|
|
1248
|
+
// (from the caller's `totalRows`); buildWorkerDeps treats a
|
|
1249
|
+
// 0/absent value as "unknown" so the progress patch does
|
|
1250
|
+
// not overwrite it with null on the first tick.
|
|
1251
|
+
total: priorMetadata.total,
|
|
1252
|
+
});
|
|
1253
|
+
|
|
1254
|
+
const result = await runImport(deps);
|
|
1255
|
+
|
|
1256
|
+
if (result.cancelled) {
|
|
1257
|
+
// The completed handler's CAS only fires `WHERE state =
|
|
1258
|
+
// "active"` -- finalizeCancelled flips the row to
|
|
1259
|
+
// "cancelled" (so that CAS no longer matches and the state
|
|
1260
|
+
// stays "cancelled" instead of being overwritten back to
|
|
1261
|
+
// "completed") AND persists `result`/`errorReportKey` in
|
|
1262
|
+
// the same statement, since no handler will run for this
|
|
1263
|
+
// return value once the row is no longer "active".
|
|
1264
|
+
await finalizeCancelled(db, row.id, result);
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
return { result, metadata: result };
|
|
1268
|
+
} catch (error: unknown) {
|
|
1269
|
+
if (error instanceof ImportAbort) {
|
|
1270
|
+
console.error(`[EXULU] import ${jobResultId} aborted: ${error.code}`);
|
|
1271
|
+
}
|
|
1272
|
+
throw error;
|
|
1273
|
+
} finally {
|
|
1274
|
+
// The worker owns the S3 stream's lifecycle -- csv-stream.ts
|
|
1275
|
+
// only forwards a source error into the parser, it never
|
|
1276
|
+
// destroys the source itself. Safe to call unconditionally:
|
|
1277
|
+
// a stream already ended (full read) or already destroyed
|
|
1278
|
+
// (prior error) is a no-op.
|
|
1279
|
+
stream.destroy();
|
|
1280
|
+
}
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1112
1283
|
if (data.type === "email_intake") {
|
|
1113
1284
|
console.log("[EXULU] running a routine webhook intake job.", bullmqJob.name);
|
|
1114
1285
|
if (!data.inputs?.s3Key || !data.inputs?.triggerId) {
|