feat: add smart auto-albums with visual workflow editor
- Visual node-based workflow editor using React Flow - Asset triggers: new asset, asset updated, all assets (with lookback buffer) - Logic nodes: if/switch with combinable conditions (person, location, date, camera, EXIF, favorites, album membership) - Action nodes: create album, add/remove from album, favorite, unfavorite, archive, tag - Workflow-level triggers: manual run, cron schedule, webhook - Debug mode: dry run with step-by-step logs and asset previews - Run history with per-node asset tracking - Run detail page with annotated read-only graph - Processed asset tracking to prevent duplicate processing - Multi-person picker with face thumbnails - Album picker dropdown for actions - JSON import/export for workflow sharing - Workflow list page with create, delete, enable/disable
This commit is contained in:
1 parent
e814793190
commit
c7a7ea3023
41 files changed
+4756
-2
No files matched your search
@@ -0,0 +1,168 @@
|
||||
import { ENV } from "@/config/environment";
|
||||
import { db } from "@/config/db";
|
||||
import { exif } from "@/schema";
|
||||
import { assets } from "@/schema/assets.schema";
|
||||
import { person } from "@/schema/person.schema";
|
||||
import { assetFaces } from "@/schema/assetFaces.schema";
|
||||
import { eq, inArray, sql, desc } from "drizzle-orm";
|
||||
import { IUser } from "@/types/user";
|
||||
import { getUserHeaders } from "@/helpers/user.helper";
|
||||
|
||||
interface ActionResult {
|
||||
action: string;
|
||||
assetsProcessed: number;
|
||||
albumId?: string;
|
||||
albumName?: string;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
async function immichFetch(path: string, method: string, body: any, user: IUser): Promise<any> {
|
||||
const res = await fetch(`${ENV.IMMICH_URL}/api${path}`, {
|
||||
method,
|
||||
headers: {
|
||||
...getUserHeaders(user),
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
});
|
||||
if (!res.ok) {
|
||||
const text = await res.text();
|
||||
throw new Error(`Immich API error ${res.status}: ${text}`);
|
||||
}
|
||||
const contentType = res.headers.get("content-type");
|
||||
if (contentType?.includes("application/json")) {
|
||||
return res.json();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async function resolveTemplate(template: string, assetIds: string[]): Promise<string> {
|
||||
if (!template || assetIds.length === 0) return template;
|
||||
|
||||
let result = template;
|
||||
|
||||
if (result.includes("{city}") || result.includes("{state}") || result.includes("{country}") || result.includes("{camera}")) {
|
||||
// Get most common EXIF values
|
||||
const exifRows = await db
|
||||
.select({
|
||||
city: exif.city,
|
||||
state: exif.state,
|
||||
country: exif.country,
|
||||
make: exif.make,
|
||||
model: exif.model,
|
||||
})
|
||||
.from(exif)
|
||||
.where(inArray(exif.assetId, assetIds))
|
||||
.limit(100);
|
||||
|
||||
const mostCommon = (values: (string | null)[]) => {
|
||||
const counts = new Map<string, number>();
|
||||
for (const v of values) {
|
||||
if (v) counts.set(v, (counts.get(v) || 0) + 1);
|
||||
}
|
||||
let best = "";
|
||||
let bestCount = 0;
|
||||
for (const [v, c] of counts) {
|
||||
if (c > bestCount) { best = v; bestCount = c; }
|
||||
}
|
||||
return best || "Unknown";
|
||||
};
|
||||
|
||||
result = result.replace("{city}", mostCommon(exifRows.map((r) => r.city)));
|
||||
result = result.replace("{state}", mostCommon(exifRows.map((r) => r.state)));
|
||||
result = result.replace("{country}", mostCommon(exifRows.map((r) => r.country)));
|
||||
result = result.replace("{camera}", mostCommon(exifRows.map((r) => [r.make, r.model].filter(Boolean).join(" "))));
|
||||
}
|
||||
|
||||
if (result.includes("{date}")) {
|
||||
const today = new Date();
|
||||
result = result.replace("{date}", today.toISOString().split("T")[0]);
|
||||
}
|
||||
|
||||
if (result.includes("{person}")) {
|
||||
const faces = await db
|
||||
.select({ name: person.name })
|
||||
.from(assetFaces)
|
||||
.innerJoin(person, eq(assetFaces.personId, person.id))
|
||||
.where(inArray(assetFaces.assetId, assetIds))
|
||||
.limit(100);
|
||||
|
||||
const names = faces.map((f) => f.name).filter(Boolean);
|
||||
const mostCommon = (values: string[]) => {
|
||||
const counts = new Map<string, number>();
|
||||
for (const v of values) counts.set(v, (counts.get(v) || 0) + 1);
|
||||
let best = "";
|
||||
let bestCount = 0;
|
||||
for (const [v, c] of counts) {
|
||||
if (c > bestCount) { best = v; bestCount = c; }
|
||||
}
|
||||
return best || "Unknown";
|
||||
};
|
||||
result = result.replace("{person}", mostCommon(names));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function executeAction(
|
||||
subType: string,
|
||||
config: any,
|
||||
assetIds: string[],
|
||||
user: IUser
|
||||
): Promise<ActionResult> {
|
||||
if (assetIds.length === 0) {
|
||||
return { action: subType, assetsProcessed: 0 };
|
||||
}
|
||||
|
||||
switch (subType) {
|
||||
case "create_album": {
|
||||
const albumName = await resolveTemplate(config.nameTemplate || "Auto Album", assetIds);
|
||||
const album = await immichFetch("/albums", "POST", { albumName, assetIds }, user);
|
||||
return { action: "create_album", assetsProcessed: assetIds.length, albumId: album.id, albumName };
|
||||
}
|
||||
|
||||
case "add_to_album": {
|
||||
if (!config.albumId) throw new Error("Album ID is required for add_to_album");
|
||||
await immichFetch(`/albums/${config.albumId}/assets`, "PUT", { ids: assetIds }, user);
|
||||
return { action: "add_to_album", assetsProcessed: assetIds.length, albumId: config.albumId };
|
||||
}
|
||||
|
||||
case "remove_from_album": {
|
||||
if (!config.albumId) throw new Error("Album ID is required for remove_from_album");
|
||||
await immichFetch(`/albums/${config.albumId}/assets`, "DELETE", { ids: assetIds }, user);
|
||||
return { action: "remove_from_album", assetsProcessed: assetIds.length, albumId: config.albumId };
|
||||
}
|
||||
|
||||
case "favorite": {
|
||||
await immichFetch("/assets", "PUT", { ids: assetIds, isFavorite: true }, user);
|
||||
return { action: "favorite", assetsProcessed: assetIds.length };
|
||||
}
|
||||
|
||||
case "unfavorite": {
|
||||
await immichFetch("/assets", "PUT", { ids: assetIds, isFavorite: false }, user);
|
||||
return { action: "unfavorite", assetsProcessed: assetIds.length };
|
||||
}
|
||||
|
||||
case "archive": {
|
||||
await immichFetch("/assets", "PUT", { ids: assetIds, isArchived: true }, user);
|
||||
return { action: "archive", assetsProcessed: assetIds.length };
|
||||
}
|
||||
|
||||
case "tag": {
|
||||
if (!config.tagName) throw new Error("Tag name is required");
|
||||
// Tags API may vary by Immich version — try the standard approach
|
||||
try {
|
||||
// Create or get tag
|
||||
const tag = await immichFetch("/tags", "POST", { name: config.tagName, type: "OBJECT" }, user);
|
||||
// Tag assets
|
||||
await immichFetch(`/tags/${tag.id}/assets`, "PUT", { ids: assetIds }, user);
|
||||
return { action: "tag", assetsProcessed: assetIds.length };
|
||||
} catch (e: any) {
|
||||
return { action: "tag", assetsProcessed: 0, error: e.message };
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
return { action: subType, assetsProcessed: 0, error: `Unknown action: ${subType}` };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
import { SQL, and, eq, ne, gte, lte, isNull, sql } from "drizzle-orm";
|
||||
import { assets } from "@/schema/assets.schema";
|
||||
import { exif } from "@/schema";
|
||||
import { ICondition } from "@/types/workflow";
|
||||
import { subDays } from "date-fns";
|
||||
|
||||
export function buildConditions(conditions: ICondition[], ownerId: string): SQL[] {
|
||||
const clauses: SQL[] = [
|
||||
eq(assets.ownerId, ownerId),
|
||||
eq(assets.visibility, "timeline"),
|
||||
eq(assets.status, "active"),
|
||||
isNull(assets.deletedAt),
|
||||
];
|
||||
|
||||
for (const c of conditions) {
|
||||
const clause = buildSingleCondition(c);
|
||||
if (clause) clauses.push(clause);
|
||||
}
|
||||
|
||||
return clauses;
|
||||
}
|
||||
|
||||
function buildSingleCondition(c: ICondition): SQL | undefined {
|
||||
switch (c.type) {
|
||||
case "city":
|
||||
return c.match === "not_equals"
|
||||
? ne(exif.city, c.city)
|
||||
: eq(exif.city, c.city);
|
||||
|
||||
case "state":
|
||||
return c.match === "not_equals"
|
||||
? ne(exif.state, c.state)
|
||||
: eq(exif.state, c.state);
|
||||
|
||||
case "country":
|
||||
return c.match === "not_equals"
|
||||
? ne(exif.country, c.country)
|
||||
: eq(exif.country, c.country);
|
||||
|
||||
case "camera_make":
|
||||
return eq(exif.make, c.make);
|
||||
|
||||
case "camera_model":
|
||||
return eq(exif.model, c.model);
|
||||
|
||||
case "lens":
|
||||
return eq(exif.lensModel, c.lensModel);
|
||||
|
||||
case "asset_type":
|
||||
return eq(assets.type, c.assetType || "IMAGE");
|
||||
|
||||
case "is_favorited":
|
||||
return eq(assets.isFavorite, c.value !== false);
|
||||
|
||||
case "date_range": {
|
||||
const parts: SQL[] = [];
|
||||
if (c.after) parts.push(gte(exif.dateTimeOriginal, new Date(c.after)));
|
||||
if (c.before) parts.push(lte(exif.dateTimeOriginal, new Date(c.before)));
|
||||
return parts.length > 0 ? and(...parts)! : undefined;
|
||||
}
|
||||
|
||||
case "date_relative":
|
||||
if (c.lastDays) {
|
||||
return gte(exif.dateTimeOriginal, subDays(new Date(), c.lastDays));
|
||||
}
|
||||
return undefined;
|
||||
|
||||
case "day_of_week":
|
||||
if (c.days && c.days.length > 0) {
|
||||
return sql`EXTRACT(DOW FROM ${exif.dateTimeOriginal}) IN (${sql.raw(c.days.join(","))})`;
|
||||
}
|
||||
return undefined;
|
||||
|
||||
case "iso_range": {
|
||||
const parts: SQL[] = [];
|
||||
if (c.min !== undefined) parts.push(gte(exif.iso, c.min));
|
||||
if (c.max !== undefined) parts.push(lte(exif.iso, c.max));
|
||||
return parts.length > 0 ? and(...parts)! : undefined;
|
||||
}
|
||||
|
||||
case "focal_length": {
|
||||
const parts: SQL[] = [];
|
||||
if (c.min !== undefined) parts.push(gte(exif.focalLength, c.min));
|
||||
if (c.max !== undefined) parts.push(lte(exif.focalLength, c.max));
|
||||
return parts.length > 0 ? and(...parts)! : undefined;
|
||||
}
|
||||
|
||||
case "rating": {
|
||||
const parts: SQL[] = [];
|
||||
if (c.min !== undefined) parts.push(gte(exif.rating, c.min));
|
||||
if (c.max !== undefined) parts.push(lte(exif.rating, c.max));
|
||||
return parts.length > 0 ? and(...parts)! : undefined;
|
||||
}
|
||||
|
||||
case "person": {
|
||||
const ids: string[] = c.personIds || (c.personId ? [c.personId] : []);
|
||||
if (ids.length === 0) return undefined;
|
||||
|
||||
if (c.match === "not_contains") {
|
||||
// Asset must not contain ANY of these people
|
||||
const checks = ids.map((pid: string) =>
|
||||
sql`NOT EXISTS (SELECT 1 FROM "asset_face" af WHERE af."assetId" = ${assets.id} AND af."personId" = ${pid})`
|
||||
);
|
||||
return and(...checks)!;
|
||||
}
|
||||
|
||||
if (c.match === "contains_all") {
|
||||
// Asset must contain ALL of these people
|
||||
const checks = ids.map((pid: string) =>
|
||||
sql`EXISTS (SELECT 1 FROM "asset_face" af WHERE af."assetId" = ${assets.id} AND af."personId" = ${pid})`
|
||||
);
|
||||
return and(...checks)!;
|
||||
}
|
||||
|
||||
// contains_any (default) — asset contains at least one of these people
|
||||
const idList = ids.map((id: string) => `'${id}'`).join(",");
|
||||
return sql`EXISTS (SELECT 1 FROM "asset_face" af WHERE af."assetId" = ${assets.id} AND af."personId" IN (${sql.raw(idList)}))`;
|
||||
}
|
||||
|
||||
case "person_unnamed":
|
||||
if (c.match === "no_unnamed") {
|
||||
return sql`NOT EXISTS (SELECT 1 FROM "asset_face" af JOIN "person" p ON af."personId" = p.id WHERE af."assetId" = ${assets.id} AND (p.name = '' OR p.name IS NULL))`;
|
||||
}
|
||||
return sql`EXISTS (SELECT 1 FROM "asset_face" af JOIN "person" p ON af."personId" = p.id WHERE af."assetId" = ${assets.id} AND (p.name = '' OR p.name IS NULL))`;
|
||||
|
||||
case "not_in_album":
|
||||
return sql`NOT EXISTS (SELECT 1 FROM "album_asset" aa WHERE aa."assetId" = ${assets.id})`;
|
||||
|
||||
case "not_in_specific_album":
|
||||
return sql`NOT EXISTS (SELECT 1 FROM "album_asset" aa WHERE aa."assetId" = ${assets.id} AND aa."albumId" = ${c.albumId})`;
|
||||
|
||||
case "geo_radius":
|
||||
if (c.lat !== undefined && c.lng !== undefined && c.radiusKm) {
|
||||
// Haversine approximation: 1 degree ≈ 111km
|
||||
const latDelta = c.radiusKm / 111;
|
||||
const lngDelta = c.radiusKm / (111 * Math.cos((c.lat * Math.PI) / 180));
|
||||
return and(
|
||||
gte(exif.latitude, c.lat - latDelta),
|
||||
lte(exif.latitude, c.lat + latDelta),
|
||||
gte(exif.longitude, c.lng - lngDelta),
|
||||
lte(exif.longitude, c.lng + lngDelta),
|
||||
)!;
|
||||
}
|
||||
return undefined;
|
||||
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,357 @@
|
||||
import { appDb } from "@/db";
|
||||
import { db } from "@/config/db";
|
||||
import { workflows, workflowNodes, workflowEdges, workflowRuns, workflowProcessedAssets } from "@/db/schema/workflows.schema";
|
||||
import { assets } from "@/schema/assets.schema";
|
||||
import { exif } from "@/schema";
|
||||
import { eq, and, desc, gte, isNull, inArray, sql, ne } from "drizzle-orm";
|
||||
import { buildConditions } from "./conditionBuilder";
|
||||
import { executeAction } from "./actionExecutor";
|
||||
import { IUser } from "@/types/user";
|
||||
import { randomUUID } from "crypto";
|
||||
|
||||
async function resolveAssetTrigger(
|
||||
subType: string,
|
||||
workflowId: string,
|
||||
userId: string,
|
||||
lookbackMinutes: number = 0,
|
||||
): Promise<string[]> {
|
||||
// Get last successful non-debug run time
|
||||
const [lastRun] = await appDb
|
||||
.select({ completedAt: workflowRuns.completedAt })
|
||||
.from(workflowRuns)
|
||||
.where(and(
|
||||
eq(workflowRuns.workflowId, workflowId),
|
||||
eq(workflowRuns.status, "completed"),
|
||||
ne(workflowRuns.trigger, "debug"),
|
||||
))
|
||||
.orderBy(desc(workflowRuns.completedAt))
|
||||
.limit(1);
|
||||
|
||||
// Fall back to workflow creation time if no previous run
|
||||
let sinceDate = lastRun?.completedAt;
|
||||
if (!sinceDate) {
|
||||
const [wf] = await appDb
|
||||
.select({ createdAt: workflows.createdAt })
|
||||
.from(workflows)
|
||||
.where(eq(workflows.id, workflowId))
|
||||
.limit(1);
|
||||
sinceDate = wf?.createdAt || new Date();
|
||||
}
|
||||
|
||||
// Apply lookback buffer
|
||||
if (lookbackMinutes > 0) {
|
||||
sinceDate = new Date(sinceDate.getTime() - lookbackMinutes * 60 * 1000);
|
||||
}
|
||||
|
||||
const baseConditions = [
|
||||
eq(assets.ownerId, userId),
|
||||
eq(assets.visibility, "timeline"),
|
||||
eq(assets.status, "active"),
|
||||
isNull(assets.deletedAt),
|
||||
];
|
||||
|
||||
// Get already-processed assets for this workflow
|
||||
let processedMap: Map<string, Date> | null = null;
|
||||
if (subType === "new_asset" || subType === "asset_updated") {
|
||||
const processed = await appDb
|
||||
.select({ assetId: workflowProcessedAssets.assetId, processedAt: workflowProcessedAssets.processedAt })
|
||||
.from(workflowProcessedAssets)
|
||||
.where(eq(workflowProcessedAssets.workflowId, workflowId));
|
||||
processedMap = new Map(processed.map((r) => [r.assetId, r.processedAt || new Date(0)]));
|
||||
}
|
||||
|
||||
let candidateIds: string[];
|
||||
|
||||
if (subType === "new_asset") {
|
||||
const rows = await db
|
||||
.selectDistinctOn([assets.id], { id: assets.id })
|
||||
.from(assets)
|
||||
.where(and(...baseConditions, gte(assets.createdAt, sinceDate)))
|
||||
.limit(10000);
|
||||
// For new_asset: skip any asset we've ever processed
|
||||
candidateIds = rows.map((r) => r.id).filter((id) => !processedMap?.has(id));
|
||||
} else if (subType === "asset_updated") {
|
||||
const rows = await db
|
||||
.selectDistinctOn([assets.id], { id: assets.id, updatedAt: assets.updatedAt })
|
||||
.from(assets)
|
||||
.where(and(...baseConditions, gte(assets.updatedAt, sinceDate)))
|
||||
.limit(10000);
|
||||
// For asset_updated: only skip if we processed it AFTER its current updatedAt
|
||||
candidateIds = rows
|
||||
.filter((r) => {
|
||||
const lastProcessed = processedMap?.get(r.id);
|
||||
if (!lastProcessed) return true; // never processed
|
||||
return r.updatedAt > lastProcessed; // updated since last processing
|
||||
})
|
||||
.map((r) => r.id);
|
||||
} else {
|
||||
// all_assets — no dedup
|
||||
const rows = await db
|
||||
.selectDistinctOn([assets.id], { id: assets.id })
|
||||
.from(assets)
|
||||
.where(and(...baseConditions))
|
||||
.limit(10000);
|
||||
return rows.map((r) => r.id);
|
||||
}
|
||||
|
||||
return candidateIds;
|
||||
}
|
||||
|
||||
interface DebugStep {
|
||||
nodeId: string;
|
||||
nodeType: string;
|
||||
subType: string;
|
||||
label: string;
|
||||
inputAssets: number;
|
||||
outputAssets: Record<string, number>;
|
||||
assetIds?: string[];
|
||||
detail?: string;
|
||||
}
|
||||
|
||||
interface RunResult {
|
||||
matchedAssets: number;
|
||||
assetIds: string[];
|
||||
actions: any[];
|
||||
debug?: DebugStep[];
|
||||
}
|
||||
|
||||
export async function executeWorkflow(
|
||||
workflowId: string,
|
||||
trigger: "manual" | "schedule" | "webhook" | "debug",
|
||||
user: IUser
|
||||
): Promise<string> {
|
||||
const isDebug = trigger === "debug";
|
||||
|
||||
// Create run record
|
||||
const runId = randomUUID();
|
||||
await appDb.insert(workflowRuns).values({
|
||||
id: runId,
|
||||
workflowId,
|
||||
trigger,
|
||||
status: "running",
|
||||
});
|
||||
|
||||
try {
|
||||
// Load graph
|
||||
const nodes = await appDb.select().from(workflowNodes).where(eq(workflowNodes.workflowId, workflowId));
|
||||
const edges = await appDb.select().from(workflowEdges).where(eq(workflowEdges.workflowId, workflowId));
|
||||
|
||||
// Build adjacency map: nodeId -> { handle -> targetNodeId[] }
|
||||
const adjacency = new Map<string, Map<string | null, string[]>>();
|
||||
for (const edge of edges) {
|
||||
if (!adjacency.has(edge.sourceNodeId)) adjacency.set(edge.sourceNodeId, new Map());
|
||||
const handles = adjacency.get(edge.sourceNodeId)!;
|
||||
const handle = edge.sourceHandle || null;
|
||||
if (!handles.has(handle)) handles.set(handle, []);
|
||||
handles.get(handle)!.push(edge.targetNodeId);
|
||||
}
|
||||
|
||||
const nodeMap = new Map(nodes.map((n) => [n.id, n]));
|
||||
|
||||
// Find asset trigger nodes
|
||||
const triggerNodes = nodes.filter((n) => n.type === "trigger");
|
||||
if (triggerNodes.length === 0) {
|
||||
throw new Error("No asset trigger nodes found in the workflow");
|
||||
}
|
||||
|
||||
const result: RunResult = { matchedAssets: 0, assetIds: [], actions: [] };
|
||||
const debugSteps: DebugStep[] = [];
|
||||
const allProcessedAssetIds = new Set<string>();
|
||||
|
||||
// BFS: process nodes with their incoming asset sets
|
||||
const queue: { nodeId: string; assetIds: string[] | null }[] = [];
|
||||
|
||||
// Resolve each asset trigger to get the initial asset set
|
||||
for (const triggerNode of triggerNodes) {
|
||||
const triggerConfig = JSON.parse(triggerNode.data || "{}");
|
||||
const lookback = triggerConfig.lookbackMinutes || 0;
|
||||
const assetIds = await resolveAssetTrigger(triggerNode.subType, workflowId, user.id, lookback);
|
||||
result.matchedAssets += assetIds.length;
|
||||
|
||||
debugSteps.push({
|
||||
nodeId: triggerNode.id,
|
||||
nodeType: "trigger",
|
||||
subType: triggerNode.subType,
|
||||
label: `Asset Trigger: ${triggerNode.subType}`,
|
||||
inputAssets: 0,
|
||||
outputAssets: { out: assetIds.length },
|
||||
assetIds: assetIds.slice(0, 100),
|
||||
detail: `Lookback: ${lookback}m, Found ${assetIds.length} assets`,
|
||||
});
|
||||
|
||||
const targets = adjacency.get(triggerNode.id)?.get(null) || [];
|
||||
for (const targetId of targets) {
|
||||
queue.push({ nodeId: targetId, assetIds });
|
||||
}
|
||||
}
|
||||
|
||||
// Process queue
|
||||
while (queue.length > 0) {
|
||||
const { nodeId, assetIds } = queue.shift()!;
|
||||
const node = nodeMap.get(nodeId);
|
||||
if (!node) continue;
|
||||
|
||||
const config = JSON.parse(node.data || "{}");
|
||||
|
||||
if (node.type === "logic") {
|
||||
if (node.subType === "if") {
|
||||
// Query assets matching conditions, scoped to incoming set
|
||||
const conditions = config.conditions || [];
|
||||
const whereClauses = buildConditions(conditions, user.id);
|
||||
|
||||
// Scope to incoming assets if available
|
||||
if (assetIds !== null && assetIds.length > 0) {
|
||||
whereClauses.push(inArray(assets.id, assetIds));
|
||||
}
|
||||
|
||||
const matchedRows = await db
|
||||
.selectDistinctOn([assets.id], { id: assets.id })
|
||||
.from(assets)
|
||||
.leftJoin(exif, eq(assets.id, exif.assetId))
|
||||
.where(and(...whereClauses))
|
||||
.limit(10000);
|
||||
|
||||
const matchedIds = matchedRows.map((r) => r.id);
|
||||
const matchedSet = new Set(matchedIds);
|
||||
|
||||
let trueIds = matchedIds;
|
||||
let falseIds: string[] = [];
|
||||
|
||||
if (assetIds !== null) {
|
||||
trueIds = assetIds.filter((id) => matchedSet.has(id));
|
||||
falseIds = assetIds.filter((id) => !matchedSet.has(id));
|
||||
}
|
||||
|
||||
debugSteps.push({
|
||||
nodeId: node.id,
|
||||
nodeType: "logic",
|
||||
subType: "if",
|
||||
label: `IF (${conditions.length} conditions)`,
|
||||
inputAssets: assetIds?.length || 0,
|
||||
outputAssets: { true: trueIds.length, false: falseIds.length },
|
||||
assetIds: trueIds.slice(0, 100),
|
||||
detail: `Matched: ${trueIds.length} true, ${falseIds.length} false`,
|
||||
});
|
||||
|
||||
// Route to true/false branches
|
||||
const trueTargets = adjacency.get(nodeId)?.get("true") || [];
|
||||
const falseTargets = adjacency.get(nodeId)?.get("false") || [];
|
||||
for (const t of trueTargets) queue.push({ nodeId: t, assetIds: trueIds });
|
||||
for (const t of falseTargets) queue.push({ nodeId: t, assetIds: falseIds });
|
||||
|
||||
} else if (node.subType === "switch") {
|
||||
const cases = config.cases || [];
|
||||
let remaining = assetIds;
|
||||
|
||||
for (const c of cases) {
|
||||
const whereClauses = buildConditions(c.conditions || [], user.id);
|
||||
|
||||
// Scope to remaining assets if available
|
||||
if (remaining !== null && remaining.length > 0) {
|
||||
whereClauses.push(inArray(assets.id, remaining));
|
||||
}
|
||||
|
||||
const matchedRows = await db
|
||||
.selectDistinctOn([assets.id], { id: assets.id })
|
||||
.from(assets)
|
||||
.leftJoin(exif, eq(assets.id, exif.assetId))
|
||||
.where(and(...whereClauses))
|
||||
.limit(10000);
|
||||
|
||||
const matchedIds = matchedRows.map((r) => r.id);
|
||||
const matchedSet = new Set(matchedIds);
|
||||
let caseIds: string[];
|
||||
|
||||
if (remaining !== null) {
|
||||
caseIds = remaining.filter((id) => matchedSet.has(id));
|
||||
remaining = remaining.filter((id) => !matchedSet.has(id));
|
||||
} else {
|
||||
caseIds = matchedIds;
|
||||
remaining = [];
|
||||
}
|
||||
|
||||
result.matchedAssets = Math.max(result.matchedAssets, caseIds.length);
|
||||
|
||||
const caseTargets = adjacency.get(nodeId)?.get(c.handle) || [];
|
||||
for (const t of caseTargets) queue.push({ nodeId: t, assetIds: caseIds });
|
||||
}
|
||||
|
||||
const caseOutput: Record<string, number> = {};
|
||||
cases.forEach((c: any, i: number) => { caseOutput[c.label || `case_${i}`] = 0; });
|
||||
// Already routed above, just log
|
||||
debugSteps.push({
|
||||
nodeId: node.id,
|
||||
nodeType: "logic",
|
||||
subType: "switch",
|
||||
label: `SWITCH (${cases.length} cases)`,
|
||||
inputAssets: assetIds?.length || 0,
|
||||
outputAssets: { ...caseOutput, default: (remaining || []).length },
|
||||
});
|
||||
|
||||
// Default branch gets remaining
|
||||
const defaultTargets = adjacency.get(nodeId)?.get("default") || [];
|
||||
for (const t of defaultTargets) queue.push({ nodeId: t, assetIds: remaining || [] });
|
||||
}
|
||||
|
||||
} else if (node.type === "action") {
|
||||
// Execute action on the asset set
|
||||
const actionAssetIds = assetIds || [];
|
||||
actionAssetIds.forEach((id) => allProcessedAssetIds.add(id));
|
||||
|
||||
debugSteps.push({
|
||||
nodeId: node.id,
|
||||
nodeType: "action",
|
||||
subType: node.subType,
|
||||
label: `Action: ${node.subType}`,
|
||||
inputAssets: actionAssetIds.length,
|
||||
outputAssets: {},
|
||||
assetIds: actionAssetIds.slice(0, 100),
|
||||
detail: isDebug ? `DRY RUN — would process ${actionAssetIds.length} assets` : `Processing ${actionAssetIds.length} assets`,
|
||||
});
|
||||
|
||||
if (!isDebug && actionAssetIds.length > 0) {
|
||||
const actionResult = await executeAction(node.subType, config, actionAssetIds, user);
|
||||
result.actions.push({ ...actionResult, assetIds: actionAssetIds });
|
||||
|
||||
// Record processed assets to prevent reprocessing
|
||||
if (actionAssetIds.length > 0) {
|
||||
const batchSize = 100;
|
||||
for (let i = 0; i < actionAssetIds.length; i += batchSize) {
|
||||
const batch = actionAssetIds.slice(i, i + batchSize);
|
||||
await appDb.insert(workflowProcessedAssets).values(
|
||||
batch.map((assetId) => ({
|
||||
workflowId,
|
||||
assetId,
|
||||
runId,
|
||||
}))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Finalize result
|
||||
result.assetIds = Array.from(allProcessedAssetIds);
|
||||
result.matchedAssets = result.assetIds.length;
|
||||
result.debug = debugSteps;
|
||||
|
||||
// Update run record
|
||||
await appDb.update(workflowRuns).set({
|
||||
status: "completed",
|
||||
result: JSON.stringify(result),
|
||||
completedAt: new Date(),
|
||||
}).where(eq(workflowRuns.id, runId));
|
||||
|
||||
return runId;
|
||||
|
||||
} catch (error: any) {
|
||||
await appDb.update(workflowRuns).set({
|
||||
status: "failed",
|
||||
error: error.message || "Unknown error",
|
||||
completedAt: new Date(),
|
||||
}).where(eq(workflowRuns.id, runId));
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import cron, { ScheduledTask } from "node-cron";
|
||||
import { appDb } from "@/db";
|
||||
import { db } from "@/config/db";
|
||||
import { workflows } from "@/db/schema/workflows.schema";
|
||||
import { users } from "@/schema/users.schema";
|
||||
import { eq, and, isNotNull } from "drizzle-orm";
|
||||
import { executeWorkflow } from "./engine";
|
||||
|
||||
const scheduledTasks = new Map<string, ScheduledTask>();
|
||||
|
||||
export async function registerWorkflow(workflowId: string, cronSchedule: string, ownerId: string) {
|
||||
// Unregister if already exists
|
||||
unregisterWorkflow(workflowId);
|
||||
|
||||
if (!cron.validate(cronSchedule)) {
|
||||
console.error(`Invalid cron expression for workflow ${workflowId}: ${cronSchedule}`);
|
||||
return;
|
||||
}
|
||||
|
||||
const task = cron.schedule(cronSchedule, async () => {
|
||||
try {
|
||||
const [user] = await db.select().from(users).where(eq(users.id, ownerId));
|
||||
if (!user) {
|
||||
console.error(`Owner ${ownerId} not found for workflow ${workflowId}`);
|
||||
return;
|
||||
}
|
||||
await executeWorkflow(workflowId, "schedule", user as any);
|
||||
console.log(`Scheduled workflow ${workflowId} executed successfully`);
|
||||
} catch (error: any) {
|
||||
console.error(`Scheduled workflow ${workflowId} failed:`, error.message);
|
||||
}
|
||||
});
|
||||
|
||||
scheduledTasks.set(workflowId, task);
|
||||
}
|
||||
|
||||
export function unregisterWorkflow(workflowId: string) {
|
||||
const existing = scheduledTasks.get(workflowId);
|
||||
if (existing) {
|
||||
existing.stop();
|
||||
scheduledTasks.delete(workflowId);
|
||||
}
|
||||
}
|
||||
|
||||
export async function loadAllScheduledWorkflows() {
|
||||
const enabledWorkflows = await appDb
|
||||
.select()
|
||||
.from(workflows)
|
||||
.where(and(eq(workflows.enabled, true), isNotNull(workflows.cronSchedule)));
|
||||
|
||||
for (const w of enabledWorkflows) {
|
||||
if (w.cronSchedule) {
|
||||
await registerWorkflow(w.id, w.cronSchedule, w.ownerId);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Loaded ${enabledWorkflows.length} scheduled workflows`);
|
||||
}
|
||||
Reference in new issue
Block a user