|
|
@@ -22,20 +22,38 @@ export class FFBQueryExecutorService {
|
|
|
return this.repo;
|
|
|
}
|
|
|
|
|
|
+ /** Convert any string dates in preFilter to Date objects */
|
|
|
+ private convertDates(preFilter?: Record<string, any>) {
|
|
|
+ if (!preFilter) return;
|
|
|
+ const pd = preFilter.productionDate;
|
|
|
+ if (pd) {
|
|
|
+ if (pd.$gte) pd.$gte = new Date(pd.$gte);
|
|
|
+ if (pd.$lte) pd.$lte = new Date(pd.$lte);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
async execute(plan: AgentQueryPlan) {
|
|
|
const repo = await this.getRepo();
|
|
|
|
|
|
+ // Convert string dates to Date objects
|
|
|
+ this.convertDates(plan.preFilter);
|
|
|
+
|
|
|
// Build $project if fields are specified
|
|
|
- let postPipeline = plan.postPipeline ?? [];
|
|
|
+ let postPipeline: Array<Record<string, any>> = plan.postPipeline ?? [];
|
|
|
if (plan.fields && plan.fields.length > 0) {
|
|
|
- postPipeline = [{ $project: plan.fields.reduce((acc, f) => ({ ...acc, [f]: 1 }), { _id: 0 }) }, ...postPipeline];
|
|
|
+ postPipeline = [
|
|
|
+ { $project: plan.fields.reduce((acc, f) => ({ ...acc, [f]: 1 }), { _id: 0 }) },
|
|
|
+ ...postPipeline,
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
- // CASE 1: Vector search
|
|
|
+ // Construct the aggregation pipeline
|
|
|
+ let pipeline: Array<Record<string, any>> = [];
|
|
|
+
|
|
|
if (plan.vectorQuery) {
|
|
|
const vector = await this.vectorService['embedText'](plan.vectorQuery);
|
|
|
|
|
|
- return repo.aggregate([
|
|
|
+ pipeline = [
|
|
|
{
|
|
|
$vectorSearch: {
|
|
|
index: 'vector_index',
|
|
|
@@ -47,13 +65,20 @@ export class FFBQueryExecutorService {
|
|
|
},
|
|
|
},
|
|
|
...postPipeline,
|
|
|
- ]);
|
|
|
+ ];
|
|
|
+ } else {
|
|
|
+ // Pure aggregation
|
|
|
+ pipeline = [
|
|
|
+ ...(plan.preFilter ? [{ $match: plan.preFilter }] : []),
|
|
|
+ ...postPipeline,
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
- // CASE 2: Pure aggregation (no vectors)
|
|
|
- return repo.aggregate([
|
|
|
- ...(plan.preFilter ? [{ $match: plan.preFilter }] : []),
|
|
|
- ...postPipeline,
|
|
|
- ]);
|
|
|
+ console.log('--- Aggregation pipeline ---\n', JSON.stringify(pipeline, null, 2));
|
|
|
+
|
|
|
+ const results = await repo.aggregate(pipeline);
|
|
|
+ console.log('--- Raw results ---\n', results);
|
|
|
+
|
|
|
+ return results;
|
|
|
}
|
|
|
}
|