File size: 11,391 Bytes
5b12974
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import { task, logger, queue } from "@trigger.dev/sdk";
import { runQuery } from "~/lib/neo4j.server";
import { getEmbedding } from "~/lib/model.server";
import type { EntityNode } from "@core/types";

interface EntityUpdateResult {
  uuid: string;
  name: string;
  type: string;
  success: boolean;
  error?: string;
}

interface BatchResult {
  batchId: string;
  entities: number;
  successful: number;
  failed: number;
  results: EntityUpdateResult[];
}

export const entity = queue({
  name: "entity-queue",
  concurrencyLimit: 5,
});

/**
 * Main orchestrator task that fans out batches of 100 entities
 */
export const updateAllEntityEmbeddings = task({
  id: "update-all-entity-embeddings",
  machine: "large-1x",

  run: async (
    payload: {
      userId?: string;
      batchSize?: number;
      forceUpdate?: boolean;
    } = {},
  ) => {
    const { userId, batchSize = 50, forceUpdate = false } = payload;

    logger.info("Starting entity embeddings update with fan-out approach", {
      userId,
      batchSize,
      forceUpdate,
      targetScope: userId ? `user ${userId}` : "all users",
    });

    try {
      // Step 1: Fetch entities (either all or only those needing updates)
      const entities = forceUpdate
        ? await getAllEntitiesForceRefresh(userId)
        : await getAllEntities(userId);
      logger.info(`Found ${entities.length} entities to update`, {
        strategy: forceUpdate ? "force-refresh-all" : "missing-embeddings-only",
      });

      if (entities.length === 0) {
        return {
          success: true,
          totalEntities: 0,
          totalBatches: 0,
          updated: 0,
          failed: 0,
          batchResults: [],
        };
      }

      // Step 2: Split entities into batches and fan out
      const batches: EntityNode[][] = [];
      for (let i = 0; i < entities.length; i += batchSize) {
        batches.push(entities.slice(i, i + batchSize));
      }

      logger.info(
        `Fanning out ${batches.length} batches of ~${batchSize} entities each`,
      );

      // Step 3: Fan out batch processing tasks in parallel
      const batchPromises = batches.map((batch, index) =>
        updateEntityBatch.trigger({
          entities: batch,
          batchId: `batch-${index + 1}`,
          batchNumber: index + 1,
          totalBatches: batches.length,
        }),
      );

      // Wait for all batch tasks to complete
      const batchRuns = await Promise.all(batchPromises);

      // Step 4: Collect results from all batches
      const batchResults: BatchResult[] = [];
      let totalUpdated = 0;
      let totalFailed = 0;

      for (const run of batchRuns) {
        try {
          // Note: In a real implementation, you'd need to wait for the run to complete
          // and fetch its result. This is a simplified version.
          logger.info(`Batch run ${run.id} started successfully`);
        } catch (error) {
          logger.error(`Failed to start batch run:`, { error });
        }
      }

      logger.info("All batches have been dispatched", {
        totalBatches: batches.length,
        totalEntities: entities.length,
        batchRunIds: batchRuns.map((r) => r.id),
      });

      return {
        success: true,
        totalEntities: entities.length,
        totalBatches: batches.length,
        batchRunIds: batchRuns.map((r) => r.id),
        message:
          "All batches dispatched successfully. Check individual batch runs for detailed results.",
      };
    } catch (error) {
      logger.error(
        "Fatal error during entity embeddings update orchestration:",
        { error },
      );
      throw error;
    }
  },
});

/**
 * Worker task that processes a single batch of entities
 */
export const updateEntityBatch = task({
  id: "update-entity-batch",
  queue: entity,
  run: async (payload: {
    entities: EntityNode[];
    batchId: string;
    batchNumber: number;
    totalBatches: number;
  }) => {
    const { entities, batchId, batchNumber, totalBatches } = payload;

    logger.info(`Processing ${batchId} (${batchNumber}/${totalBatches})`, {
      entityCount: entities.length,
    });

    const results: EntityUpdateResult[] = [];

    try {
      // Process all entities in this batch in parallel
      const entityPromises = entities.map((entity) =>
        updateEntityEmbeddings(entity),
      );
      const entityResults = await Promise.allSettled(entityPromises);

      // Collect results
      entityResults.forEach((result, index) => {
        const entity = entities[index];
        if (result.status === "fulfilled") {
          results.push(result.value);
        } else {
          logger.error(
            `Failed to update entity ${entity.uuid} in ${batchId}:`,
            { error: result.reason },
          );
          results.push({
            uuid: entity.uuid,
            name: entity.name,
            type: entity.type,
            success: false,
            error: result.reason?.message || "Unknown error",
          });
        }
      });

      const successful = results.filter((r) => r.success).length;
      const failed = results.filter((r) => !r.success).length;

      logger.info(`Completed ${batchId}`, {
        total: entities.length,
        successful,
        failed,
        successRate: `${((successful / entities.length) * 100).toFixed(2)}%`,
      });

      return {
        batchId,
        batchNumber,
        totalBatches,
        entities: entities.length,
        successful,
        failed,
        results,
      };
    } catch (error) {
      logger.error(`Fatal error in ${batchId}:`, { error });
      throw error;
    }
  },
});

/**
 * Fetch all entities from Neo4j database that need embedding updates
 */
async function getAllEntities(userId?: string): Promise<EntityNode[]> {
  try {
    // Only fetch entities that either:
    // 1. Have null/empty embeddings, OR
    // 2. Have embeddings but might need updates (optional: add timestamp check)
    const query = userId
      ? `MATCH (entity:Entity {userId: $userId}) 
         WHERE entity.nameEmbedding IS NULL 
            OR entity.typeEmbedding IS NULL 
            OR size(entity.nameEmbedding) = 0 
            OR size(entity.typeEmbedding) = 0
         RETURN entity ORDER BY entity.createdAt`
      : `MATCH (entity:Entity) 
         WHERE entity.nameEmbedding IS NULL 
            OR entity.typeEmbedding IS NULL 
            OR size(entity.nameEmbedding) = 0 
            OR size(entity.typeEmbedding) = 0
         RETURN entity ORDER BY entity.createdAt`;

    const params = userId ? { userId } : {};
    const records = await runQuery(query, params);

    return records.map((record) => {
      const entityProps = record.get("entity").properties;
      return {
        uuid: entityProps.uuid,
        name: entityProps.name,
        type: entityProps.type,
        attributes: JSON.parse(entityProps.attributes || "{}"),
        nameEmbedding: entityProps.nameEmbedding || [],
        typeEmbedding: entityProps.typeEmbedding || [],
        createdAt: new Date(entityProps.createdAt),
        userId: entityProps.userId,
        space: entityProps.space,
      };
    });
  } catch (error) {
    logger.error("Error fetching entities:", { error });
    throw new Error(`Failed to fetch entities: ${error}`);
  }
}

/**
 * Fetch ALL entities from Neo4j database (for force refresh)
 */
async function getAllEntitiesForceRefresh(
  userId?: string,
): Promise<EntityNode[]> {
  try {
    const query = userId
      ? `MATCH (entity:Entity {userId: $userId}) RETURN entity ORDER BY entity.createdAt`
      : `MATCH (entity:Entity) RETURN entity ORDER BY entity.createdAt`;

    const params = userId ? { userId } : {};
    const records = await runQuery(query, params);

    return records.map((record) => {
      const entityProps = record.get("entity").properties;
      return {
        uuid: entityProps.uuid,
        name: entityProps.name,
        type: entityProps.type,
        attributes: JSON.parse(entityProps.attributes || "{}"),
        nameEmbedding: entityProps.nameEmbedding || [],
        typeEmbedding: entityProps.typeEmbedding || [],
        createdAt: new Date(entityProps.createdAt),
        userId: entityProps.userId,
        space: entityProps.space,
      };
    });
  } catch (error) {
    logger.error("Error fetching entities:", { error });
    throw new Error(`Failed to fetch entities: ${error}`);
  }
}

/**
 * Update embeddings for a single entity
 */
async function updateEntityEmbeddings(
  entity: EntityNode,
): Promise<EntityUpdateResult> {
  try {
    logger.info(
      `Updating embeddings for entity: ${entity.name} (${entity.type})`,
    );

    // Generate new embeddings
    const [nameEmbedding, typeEmbedding] = await Promise.all([
      getEmbedding(entity.name),
      getEmbedding(entity.type),
    ]);

    // Update entity in Neo4j
    const updateQuery = `
      MATCH (entity:Entity {uuid: $uuid})
      SET 
        entity.nameEmbedding = $nameEmbedding,
        entity.typeEmbedding = $typeEmbedding,
        entity.updatedAt = $updatedAt
      RETURN entity.uuid as uuid
    `;

    const updateParams = {
      uuid: entity.uuid,
      nameEmbedding,
      typeEmbedding,
      updatedAt: new Date().toISOString(),
    };

    const result = await runQuery(updateQuery, updateParams);

    if (result.length === 0) {
      throw new Error(`Entity ${entity.uuid} not found during update`);
    }

    return {
      uuid: entity.uuid,
      name: entity.name,
      type: entity.type,
      success: true,
    };
  } catch (error) {
    return {
      uuid: entity.uuid,
      name: entity.name,
      type: entity.type,
      success: false,
      error: error instanceof Error ? error.message : "Unknown error", // TODO: fix this
    };
  }
}

/**
 * Helper function to trigger the entity embeddings update
 */
export async function triggerEntityEmbeddingsUpdate(
  options: {
    userId?: string;
    batchSize?: number;
    forceUpdate?: boolean;
  } = {},
) {
  try {
    const result = await updateAllEntityEmbeddings.trigger(options);
    logger.info(`Triggered entity embeddings update with run ID: ${result.id}`);
    return result;
  } catch (error) {
    logger.error("Failed to trigger entity embeddings update:", { error });
    throw error;
  }
}

/**
 * Update a single entity's embeddings (useful for individual updates)
 */
export async function updateSingleEntityEmbeddings(
  entityUuid: string,
): Promise<EntityUpdateResult> {
  try {
    const query = `MATCH (entity:Entity {uuid: $uuid}) RETURN entity`;
    const records = await runQuery(query, { uuid: entityUuid });

    if (records.length === 0) {
      throw new Error(`Entity with UUID ${entityUuid} not found`);
    }

    const entityProps = records[0].get("entity").properties;
    const entity: EntityNode = {
      uuid: entityProps.uuid,
      name: entityProps.name,
      type: entityProps.type,
      attributes: JSON.parse(entityProps.attributes || "{}"),
      nameEmbedding: entityProps.nameEmbedding || [],
      typeEmbedding: entityProps.typeEmbedding || [],
      createdAt: new Date(entityProps.createdAt),
      userId: entityProps.userId,
      space: entityProps.space,
    };

    return await updateEntityEmbeddings(entity);
  } catch (error) {
    logger.error(`Error updating single entity ${entityUuid}:`, { error });
    throw error;
  }
}