Skip to content

Commit d9bd870

Browse files
chore: feature flag the aggregate schema
1 parent 6c3142b commit d9bd870

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

src/tools/mongodb/metadata/explain.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { ToolArgs, OperationType } from "../../tool.js";
44
import { formatUntrustedData } from "../../tool.js";
55
import { z } from "zod";
66
import type { Document } from "mongodb";
7-
import { AggregateArgs } from "../read/aggregate.js";
7+
import { getAggregateArgs } from "../read/aggregate.js";
88
import { FindArgs } from "../read/find.js";
99
import { CountArgs } from "../read/count.js";
1010

@@ -20,7 +20,7 @@ export class ExplainTool extends MongoDBToolBase {
2020
z.discriminatedUnion("name", [
2121
z.object({
2222
name: z.literal("aggregate"),
23-
arguments: z.object(AggregateArgs),
23+
arguments: z.object(getAggregateArgs(this.isFeatureEnabled("vectorSearch"))),
2424
}),
2525
z.object({
2626
name: z.literal("find"),

src/tools/mongodb/mongodbSchemas.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export type EmbeddingParameters = {
4242
export const zSupportedEmbeddingParameters = zVoyageEmbeddingParameters.extend({ model: zVoyageModels });
4343
export type SupportedEmbeddingParameters = z.infer<typeof zSupportedEmbeddingParameters>;
4444

45-
export const AnyVectorSearchStage = zEJSON();
45+
export const AnyAggregateStage = zEJSON();
4646
export const VectorSearchStage = z.object({
4747
$vectorSearch: z
4848
.object({

src/tools/mongodb/read/aggregate.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,17 @@ import { collectCursorUntilMaxBytesLimit } from "../../../helpers/collectCursorU
1212
import { operationWithFallback } from "../../../helpers/operationWithFallback.js";
1313
import { AGG_COUNT_MAX_TIME_MS_CAP, ONE_MB, CURSOR_LIMITS_TO_LLM_TEXT } from "../../../helpers/constants.js";
1414
import { LogId } from "../../../common/logger.js";
15-
import { AnyVectorSearchStage, VectorSearchStage } from "../mongodbSchemas.js";
15+
import { AnyAggregateStage, VectorSearchStage } from "../mongodbSchemas.js";
1616
import {
1717
assertVectorSearchFilterFieldsAreIndexed,
1818
type VectorSearchIndex,
1919
} from "../../../helpers/assertVectorSearchFilterFieldsAreIndexed.js";
2020

21-
export const AggregateArgs = {
22-
pipeline: z.array(z.union([AnyVectorSearchStage, VectorSearchStage])).describe(
23-
`An array of aggregation stages to execute.
21+
const pipelineDescription = `\
22+
An array of aggregation stages to execute.
2423
\`$vectorSearch\` **MUST** be the first stage of the pipeline, or the first stage of a \`$unionWith\` subpipeline.
2524
### Usage Rules for \`$vectorSearch\`
26-
- **Unset embeddings:**
25+
- **Unset embeddings:**
2726
Unless the user explicitly requests the embeddings, add an \`$unset\` stage **at the end of the pipeline** to remove the embedding field and avoid context limits. **The $unset stage in this situation is mandatory**.
2827
- **Pre-filtering:**
2928
If the user requests additional filtering, include filters in \`$vectorSearch.filter\` only for pre-filter fields in the vector index.
@@ -32,20 +31,26 @@ If the user requests additional filtering, include filters in \`$vectorSearch.fi
3231
For all remaining filters, add a $match stage after $vectorSearch.
3332
### Note to LLM
3433
- If unsure which fields are filterable, use the collection-indexes tool to determine valid prefilter fields.
35-
- If no requested filters are valid prefilters, omit the filter key from $vectorSearch.`
36-
),
37-
responseBytesLimit: z.number().optional().default(ONE_MB).describe(`\
34+
- If no requested filters are valid prefilters, omit the filter key from $vectorSearch.\
35+
`;
36+
37+
export const getAggregateArgs = (vectorSearchEnabled: boolean) =>
38+
({
39+
pipeline: z
40+
.array(vectorSearchEnabled ? z.union([AnyAggregateStage, VectorSearchStage]) : AnyAggregateStage)
41+
.describe(pipelineDescription),
42+
responseBytesLimit: z.number().optional().default(ONE_MB).describe(`\
3843
The maximum number of bytes to return in the response. This value is capped by the server's configured maxBytesPerQuery and cannot be exceeded. \
3944
Note to LLM: If the entire aggregation result is required, use the "export" tool instead of increasing this limit.\
4045
`),
41-
};
46+
}) as const;
4247

4348
export class AggregateTool extends MongoDBToolBase {
4449
public name = "aggregate";
4550
protected description = "Run an aggregation against a MongoDB collection";
4651
protected argsShape = {
4752
...DbOperationArgs,
48-
...AggregateArgs,
53+
...getAggregateArgs(this.isFeatureEnabled("vectorSearch")),
4954
};
5055
public operationType: OperationType = "read";
5156

src/tools/mongodb/read/export.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { OperationType, ToolArgs } from "../../tool.js";
66
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
77
import { FindArgs } from "./find.js";
88
import { jsonExportFormat } from "../../../common/exportsManager.js";
9-
import { AggregateArgs } from "./aggregate.js";
9+
import { getAggregateArgs } from "./aggregate.js";
1010

1111
export class ExportTool extends MongoDBToolBase {
1212
public name = "export";
@@ -32,7 +32,9 @@ export class ExportTool extends MongoDBToolBase {
3232
name: z
3333
.literal("aggregate")
3434
.describe("The literal name 'aggregate' to represent an aggregation cursor as target."),
35-
arguments: z.object(AggregateArgs).describe("The arguments for 'aggregate' operation."),
35+
arguments: z
36+
.object(getAggregateArgs(this.isFeatureEnabled("vectorSearch")))
37+
.describe("The arguments for 'aggregate' operation."),
3638
}),
3739
])
3840
)

tests/integration/tools/mongodb/read/aggregate.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ describeWithMongoDB("aggregate tool", (integration) => {
2727
...databaseCollectionParameters,
2828
{
2929
name: "pipeline",
30-
description: `An array of aggregation stages to execute.
30+
description: `An array of aggregation stages to execute.
3131
\`$vectorSearch\` **MUST** be the first stage of the pipeline, or the first stage of a \`$unionWith\` subpipeline.
3232
### Usage Rules for \`$vectorSearch\`
33-
- **Unset embeddings:**
33+
- **Unset embeddings:**
3434
Unless the user explicitly requests the embeddings, add an \`$unset\` stage **at the end of the pipeline** to remove the embedding field and avoid context limits. **The $unset stage in this situation is mandatory**.
3535
- **Pre-filtering:**
3636
If the user requests additional filtering, include filters in \`$vectorSearch.filter\` only for pre-filter fields in the vector index.

0 commit comments

Comments
 (0)