Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(mcp): place a loose date_from back inside the query wrapper (#103504)

posthog[bot]620c742c1755
diff --git a/services/mcp/src/tools/exec.ts b/services/mcp/src/tools/exec.ts
index a3bd1346002b..6d027a30772e 100644
--- a/services/mcp/src/tools/exec.ts
+++ b/services/mcp/src/tools/exec.ts
@@ -597,6 +597,135 @@ const DEPRECATED_TOOL_REDIRECTS: Record<string, (allTools: Tool<ZodObjectAny>[])
'Tool "self-driving-inbox-get" was removed. Use "inbox-reports-list", which lists the same reports. For the old default, pass { "view": "actionable", "use_priority_preference": true, "sort": "priority", "limit": 10 }. The array filters became comma-separated strings: `priorities` is now `priority`, `source_products` is now `source_product`, and `scouts` is now `scout`. `view`, `scope`, `teammate_uuid`, `search`, and `offset` keep their names.',
}
+/** The form caller keys and field names are matched on, so `date_from` reaches a field
+ * named `dateFrom`. These schemas mix the two spellings — a query's own fields are
+ * camelCase while its window's fields are snake_case — and a caller cannot tell which
+ * one a field uses without reading the schema. */
+function matchableName(name: string): string {
+ return name.replace(/[_-]/g, '').toLowerCase()
+}
+
+/** Where a loose key belongs: the object the wrapper declares that holds a field of that name. */
+interface FieldPlacement {
+ object: string
+ field: string
+}
+
+/**
+ * Indexes values by the name a caller might send for them.
+ *
+ * A name that two values would answer to is dropped: acting on it would guess which one the
+ * caller meant, and a guess here runs a query the caller did not ask for.
+ */
+function byMatchableName<T>(entries: Iterable<readonly [string, T]>): Map<string, T> {
+ const found = new Map<string, T>()
+ const ambiguous = new Set<string>()
+ for (const [field, value] of entries) {
+ const name = matchableName(field)
+ if (found.has(name)) {
+ ambiguous.add(name)
+ continue
+ }
+ found.set(name, value)
+ }
+ for (const name of ambiguous) {
+ found.delete(name)
+ }
+ return found
+}
+
+/** Every name a caller might send for a field of an object the wrapper declares, such as
+ * `date_from` for `dateRange.date_from`. */
+function subfieldPlacements(
+ schema: ZodObjectAny,
+ key: string,
+ declared: ReadonlySet<string>
+): Map<string, FieldPlacement> {
+ return byMatchableName(
+ [...declared].flatMap((object) =>
+ [...wrapperFieldNames(schema, [key, object])].map(
+ (field) => [field, { object, field }] as readonly [string, FieldPlacement]
+ )
+ )
+ )
+}
+
+/**
+ * Whether a field of the rebuilt call is already filled, counting an object and its own
+ * fields as the same place.
+ *
+ * Two caller keys can name one field: `date_from` and `dateFrom` both answer to
+ * `dateRange.date_from`, and `dateRange` and `date_range` both answer to `dateRange`. The
+ * second one to arrive would overwrite the first, so it is left unplaced and the rebuild
+ * stands down rather than choosing a value for the caller.
+ */
+function isTaken(taken: ReadonlySet<string>, destination: string): boolean {
+ if (taken.has(destination)) {
+ return true
+ }
+ const separator = destination.indexOf('.')
+ if (separator > 0) {
+ return taken.has(destination.slice(0, separator))
+ }
+ return [...taken].some((filled) => filled.startsWith(`${destination}.`))
+}
+
+/**
+ * Sorts a flattened payload into the call the caller meant.
+ *
+ * Keys the outer schema declares beside the wrapper stay at the top level, and keys the
+ * wrapper declares move inside it. A caller that flattens the payload usually flattens its
+ * window one level further, so `date_from` arrives beside `limit` rather than inside
+ * `dateRange`; a key like that goes back into the object that declares it.
+ *
+ * `unplaced` holds every key that belongs nowhere, and a caller that sent an object and one
+ * of its fields loose puts that field there. Nothing is dropped to make a payload fit: a
+ * wrapper that defaults its own fields parses `{"dateRagne": ...}` into a full set of
+ * defaults, so dropping the key would run an unfiltered query and return plausible but wrong
+ * rows instead of reporting the typo.
+ */
+function splitFlattenedPayload(
+ input: Record<string, unknown>,
+ key: string,
+ schema: ZodObjectAny
+): { rebuilt: Record<string, unknown>; nested: Record<string, unknown>; unplaced: string[] } {
+ const siblings = topLevelFieldNames(schema)
+ const declared = wrapperFieldNames(schema, [key])
+ const fields = byMatchableName([...declared].map((field) => [field, field] as const))
+ const placements = subfieldPlacements(schema, key, declared)
+ // A name the wrapper declares itself is that field, never a field of one of its objects.
+ for (const name of fields.keys()) {
+ placements.delete(name)
+ }
+ const rebuilt: Record<string, unknown> = {}
+ const nested: Record<string, unknown> = {}
+ const objects = new Map<string, Record<string, unknown>>()
+ const taken = new Set<string>()
+ const unplaced: string[] = []
+ for (const [name, value] of Object.entries(input)) {
+ const field = fields.get(matchableName(name))
+ const placement = placements.get(matchableName(name))
+ if (name !== key && siblings.has(name)) {
+ rebuilt[name] = value
+ } else if (field !== undefined && !isTaken(taken, field)) {
+ taken.add(field)
+ nested[field] = value
+ } else if (placement !== undefined && !isTaken(taken, `${placement.object}.${placement.field}`)) {
+ taken.add(`${placement.object}.${placement.field}`)
+ const object = objects.get(placement.object) ?? {}
+ object[placement.field] = value
+ objects.set(placement.object, object)
+ } else {
+ unplaced.push(name)
+ }
+ }
+ for (const [name, value] of objects) {
+ nested[name] = value
+ }
+ rebuilt[key] = nested
+ return { rebuilt, nested, unplaced }
+}
+
/**
* Detects the caller sending a required object parameter's *contents* in place of
* the parameter itself — `{dateRange, limit}` where `{query: {dateRange, limit}}`
@@ -605,8 +734,8 @@ const DEPRECATED_TOOL_REDIRECTS: Record<string, (allTools: Tool<ZodObjectAny>[])
* has no way to tell which mistake it made.
*
* Decided by re-parsing rather than by reading the schema, so it holds for any
- * wrapper shape: nest the input under the missing key and see whether the schema
- * accepts it. Confident when the wrapped value either parses to something
+ * wrapper shape: rebuild the call with `splitFlattenedPayload` and see whether the
+ * schema accepts it. Confident when the rebuild either parses to something
* non-empty, or fails only on paths *inside* the wrapper — both mean the nested
* schema recognized the content. A payload of undeclared keys parses to `{}` and
* is correctly rejected, as is a caller that sent nothing at all.
@@ -620,18 +749,22 @@ function looksLikeUnwrappedPayload(
return false
}
const key = String(issuePath[0])
- if (typeof input !== 'object' || input === null || Array.isArray(input)) {
+ if (!isRecord(input)) {
return false
}
const keys = Object.keys(input)
if (keys.length === 0 || keys.includes(key)) {
return false
}
+ const { rebuilt, nested } = splitFlattenedPayload(input, key, schema)
+ // A wrapper that declares no fields of its own, such as a loose or union shape,
+ // places nothing, so read the raw payload under the key instead.
+ const candidate = Object.keys(nested).length > 0 ? rebuilt : { [key]: input }
- const wrapped = schema.safeParse({ [key]: input })
+ const wrapped = schema.safeParse(candidate)
if (wrapped.success) {
- const nested = (wrapped.data as Record<string, unknown>)[key]
- return typeof nested === 'object' && nested !== null && Object.keys(nested).length > 0
+ const parsed = (wrapped.data as Record<string, unknown>)[key]
+ return isRecord(parsed) && Object.keys(parsed).length > 0
}
// Every remaining complaint sits under the wrapper: the nested schema read the
// content and rejected specific fields, so the nesting itself was the mistake.
@@ -696,16 +829,9 @@ function acceptedTopLevelShape(nested: unknown, schema: ZodObjectAny | undefined
* Naming the mistake in the rejection still costs a round trip, and the flattened shape is the most
* common rejection on the tools built this way.
*
- * Keys the outer schema declares beside the wrapper stay at the top level. Folding a sibling such as
- * `baselineDateRange` into `query` would have the nested schema strip it, and the caller would get a
- * different query than it asked for without being told.
- *
- * Every key that moves inside must be one the wrapper declares. A wrapper that defaults its own
- * fields parses `{"dateRagne": ...}` into a full set of defaults, so accepting that rebuild would run
- * an unfiltered query and return plausible but wrong rows instead of reporting the typo.
- *
- * Returns undefined unless the rebuilt payload parses, so a payload malformed for some other reason
- * keeps its own rejection.
+ * `splitFlattenedPayload` decides where each key belongs. Returns undefined when a key belongs
+ * nowhere, or when the rebuilt payload does not parse, so a payload malformed for some other
+ * reason keeps its own rejection instead of being guessed at.
*/
export function rewrapFlattenedArguments(
error: z.ZodError,
@@ -723,23 +849,10 @@ export function rewrapFlattenedArguments(
return undefined
}
- const key = String(issue.path[0])
- const siblings = topLevelFieldNames(schema)
- const rebuilt: Record<string, unknown> = {}
- const nested: Record<string, unknown> = {}
- for (const [name, value] of Object.entries(input)) {
- if (name !== key && siblings.has(name)) {
- rebuilt[name] = value
- } else {
- nested[name] = value
- }
- }
- const declared = wrapperFieldNames(schema, key)
- const nestedNames = Object.keys(nested)
- if (nestedNames.length === 0 || !nestedNames.every((name) => declared.has(name))) {
+ const { rebuilt, nested, unplaced } = splitFlattenedPayload(input, String(issue.path[0]), schema)
+ if (Object.keys(nested).length === 0 || unplaced.length > 0) {
return undefined
}
- rebuilt[key] = nested
return schema.safeParse(rebuilt).success ? rebuilt : undefined
}
@@ -751,21 +864,24 @@ function topLevelFieldNames(schema: ZodObjectAny): ReadonlySet<string> {
}
/**
- * The field names a wrapper parameter declares directly, including the fields of
- * each variant when the wrapper is a union (`read-data-schema` keys its shape off
- * a `kind` discriminator). Composition keywords are walked one level; nothing
- * deeper is collected, because only the caller's own top-level keys are matched
- * against this set.
+ * The field names the parameter at `path` declares directly, including the fields of
+ * each variant when it is a union (`read-data-schema` keys its shape off a `kind`
+ * discriminator). Composition keywords are walked one level at each step; nothing
+ * deeper is collected, because only the caller's own keys are matched against this set.
*/
-function wrapperFieldNames(schema: ZodObjectAny, key: string): ReadonlySet<string> {
+function wrapperFieldNames(schema: ZodObjectAny, path: readonly string[]): ReadonlySet<string> {
const root = inputJsonSchema(schema)
- const properties = isRecord(root) ? root['properties'] : undefined
- const wrapper = isRecord(properties) ? properties[key] : undefined
- const names = new Set<string>()
- if (!isRecord(wrapper)) {
- return names
+ let nodes = isRecord(root) ? [root] : []
+ for (const key of path) {
+ nodes = nodes
+ .flatMap((node) => [node, ...variantsOf(node)])
+ .map((node) =>
+ isRecord(node['properties']) ? (node['properties'] as Record<string, unknown>)[key] : undefined
+ )
+ .filter(isRecord)
}
- for (const node of [wrapper, ...variantsOf(wrapper)]) {
+ const names = new Set<string>()
+ for (const node of nodes.flatMap((node) => [node, ...variantsOf(node)])) {
const fields = node['properties']
if (isRecord(fields)) {
for (const name of Object.keys(fields)) {
@@ -779,8 +895,8 @@ function wrapperFieldNames(schema: ZodObjectAny, key: string): ReadonlySet<strin
/** An object shape written from field names alone, capped, with every value
* elided, so the message carries the tool's vocabulary and no caller input. */
function renderFieldShape(names: readonly string[]): string {
- const shown = names.slice(0, MAX_WRAPPER_KEYS_NAMED).map((name) => `"${name}": ...`)
- if (names.length > MAX_WRAPPER_KEYS_NAMED) {
+ const shown = names.slice(0, MAX_KEYS_NAMED).map((name) => `"${name}": ...`)
+ if (names.length > MAX_KEYS_NAMED) {
shown.push('...')
}
return `{${shown.join(', ')}}`
@@ -805,17 +921,28 @@ function variantsOf(node: Record<string, unknown>): Record<string, unknown>[] {
* tool's own vocabulary rather than the caller's, and values are always elided.
* The message is returned to the caller and recorded as the analytics error
* message, so it must carry no input. Falls back to `{...}` when no key matches.
+ * `unplaced` names the keys that fit nowhere, which the shape cannot show.
*/
-function acceptedWrapperShape(key: string, input: unknown, schema: ZodObjectAny | undefined): string {
+function acceptedWrapperShape(
+ key: string,
+ input: unknown,
+ schema: ZodObjectAny | undefined
+): { shape: string; unplaced: string[] } {
if (!schema || !isRecord(input)) {
- return `{"${key}": {...}}`
+ return { shape: `{"${key}": {...}}`, unplaced: [] }
}
- const declared = wrapperFieldNames(schema, key)
- const named = Object.keys(input).filter((name) => declared.has(name))
- if (named.length === 0) {
- return `{"${key}": {...}}`
- }
- return `{"${key}": ${renderFieldShape(named)}}`
+ const { rebuilt, nested, unplaced } = splitFlattenedPayload(input, key, schema)
+ const named = Object.keys(nested)
+ // Siblings ride along: a caller that copies the shape without them loses the `name` or
+ // the baseline window it sent, and its retry asks a different question.
+ const fields = [
+ `"${key}": ${named.length > 0 ? renderFieldShape(named) : '{...}'}`,
+ ...Object.keys(rebuilt)
+ .filter((sibling) => sibling !== key)
+ .slice(0, MAX_KEYS_NAMED)
+ .map((sibling) => `"${sibling}": ...`),
+ ]
+ return { shape: `{${fields.join(', ')}}`, unplaced }
}
/**
@@ -843,13 +970,19 @@ function undeclaredKeys(input: unknown, schema: ZodObjectAny | undefined): strin
return Object.keys(input).filter((key) => !declared.has(key))
}
-/** Bound on how many dropped keys the message names, so a caller sending a large
- * undeclared payload cannot inflate the analytics error message. */
-const MAX_DROPPED_KEYS_NAMED = 5
+/** Bound on how many keys a message names, so a caller sending a large payload
+ * cannot inflate the analytics error message. Enough keys to recognize the
+ * payload, not enough for a large one to bloat what we record. */
+const MAX_KEYS_NAMED = 5
-/** Same bound for the rendered wrapper shape: enough keys to recognize the
- * payload, not enough for a large one to inflate the message. */
-const MAX_WRAPPER_KEYS_NAMED = 5
+/** Key names only, never values: the message is returned to the caller and
+ * recorded as the analytics error message. */
+function namedKeys(keys: readonly string[]): string {
+ return keys
+ .slice(0, MAX_KEYS_NAMED)
+ .map((key) => `"${key}"`)
+ .join(', ')
+}
/** Bound on the parameter description echoed back with a missing-parameter
* rejection, so a tool with a long field description cannot inflate the
@@ -1106,8 +1239,11 @@ export function formatInputValidationError(
if ('input' in issue && issue.input === undefined) {
const hint = missingParameterHint(issue.path, schema)
if (looksLikeUnwrappedPayload(issue.path, input, schema)) {
- const shape = acceptedWrapperShape(path, input, schema)
- return `missing required parameter: ${path}${hint}; the fields you sent belong inside it, so resend them as ${shape}`
+ const { shape, unplaced } = acceptedWrapperShape(path, input, schema)
+ const rejected = unplaced.length
+ ? `; these keys are not fields of ${path}, so remove or rename them: ${namedKeys(unplaced)}`
+ : ''
+ return `missing required parameter: ${path}${hint}; the fields you sent belong inside it, so resend them as ${shape}${rejected}`
}
const overWrapped = overWrappedKey(issue.path)
if (overWrapped !== undefined) {
@@ -1116,11 +1252,7 @@ export function formatInputValidationError(
}
const dropped = keysWereRejected ? [] : undeclaredKeys(input, schema)
if (dropped.length) {
- const named = dropped
- .slice(0, MAX_DROPPED_KEYS_NAMED)
- .map((key) => `"${key}"`)
- .join(', ')
- return `missing required parameter: ${path}${hint}; this tool ignored these keys it does not accept: ${named}`
+ return `missing required parameter: ${path}${hint}; this tool ignored these keys it does not accept: ${namedKeys(dropped)}`
}
return `missing required parameter: ${path}${hint}`
}
diff --git a/services/mcp/tests/unit/exec.test.ts b/services/mcp/tests/unit/exec.test.ts
index 74a4960bd281..1b263463bd0f 100644
--- a/services/mcp/tests/unit/exec.test.ts
+++ b/services/mcp/tests/unit/exec.test.ts
@@ -2287,14 +2287,14 @@ describe('exec tool', () => {
expect(message).toContain('resend them as {"query": {"orderBy": ..., "limit": ...}}')
})
- it('names only wrapper fields, so an undeclared key cannot reach the message', () => {
- // The rendered shape is returned to the caller and recorded as the
- // analytics error message, so every key in it has to come from the
- // tool's own schema rather than the caller's payload.
+ it('builds the shape from wrapper fields, and names the key that fits none of them', () => {
+ // The rendered shape is the tool's own vocabulary, so a key the schema
+ // never declared cannot appear inside it. Naming it alongside is what
+ // stops the caller resending the same unaccepted key.
const message = formatFor({ limit: 10, sneaky_key: 'x' })
- expect(message).toContain('{"query": {"limit": ...}}')
- expect(message).not.toContain('sneaky_key')
+ expect(message).toContain('resend them as {"query": {"limit": ...}}')
+ expect(message).toContain('not fields of query, so remove or rename them: "sneaky_key"')
})
it('caps the fields it names so a large payload cannot inflate the message', () => {
@@ -2429,6 +2429,22 @@ describe('exec tool', () => {
)
})
+ it('keeps a top-level sibling in the shape it tells the caller to resend', () => {
+ // A caller that copies a shape without `baselineDateRange` diffs against the
+ // default baseline, so the retry answers a different question.
+ const tool = GENERATED_TOOL_MAP['logs-patterns-diff']!()
+ const input = {
+ serviceNames: ['api'],
+ dateRange: { date_from: '-1d' },
+ baselineDateRange: { date_from: '-2d', date_to: '-1d' },
+ }
+ const result = tool.schema.safeParse(input, { reportInput: true })
+
+ expect(formatInputValidationError('logs-patterns-diff', result.error!, input, tool.schema)).toContain(
+ '"baselineDateRange": ...'
+ )
+ })
+
describe('rewrapping it into the call the caller meant', () => {
const rewrapFor = (schema: ZodObjectAny, input: unknown): Record<string, unknown> | undefined => {
const result = schema.safeParse(input, { reportInput: true })
@@ -2482,6 +2498,83 @@ describe('exec tool', () => {
expect(rewrapFor(tool.schema, input)).toBeUndefined()
})
+ // Flattening the payload usually flattens the window one level further, and
+ // `{date_from, limit}` is the single most common rejected shape on these tools.
+ it.each([
+ ['a window sent as loose snake_case keys', { date_from: '-1h', limit: 10 }],
+ ['the camelCase spelling of the same keys', { dateFrom: '-1h', limit: 10 }],
+ ])("folds %s into the wrapper's own dateRange", (_label, input) => {
+ const tool = GENERATED_TOOL_MAP['query-logs']!()
+
+ expect(rewrapFor(tool.schema, input)).toMatchObject({
+ query: { dateRange: { date_from: '-1h' }, limit: 10 },
+ })
+ })
+
+ it('carries both ends of the window across', () => {
+ const tool = GENERATED_TOOL_MAP['query-apm-spans']!()
+
+ expect(rewrapFor(tool.schema, { date_from: '-1d', date_to: '-1h' })).toMatchObject({
+ query: { dateRange: { date_from: '-1d', date_to: '-1h' } },
+ })
+ })
+
+ it('places a loose field in whichever object declares it', () => {
+ // Nothing about this is specific to a window: `compare` belongs to
+ // `compareFilter` the same way `date_from` belongs to `dateRange`.
+ const tool = GENERATED_TOOL_MAP['apm-spans-aggregate']!()
+ const input = { serviceNames: ['api'], compare: true, date_from: '-1d' }
+
+ expect(rewrapFor(tool.schema, input)).toMatchObject({
+ query: {
+ serviceNames: ['api'],
+ compareFilter: { compare: true },
+ dateRange: { date_from: '-1d' },
+ },
+ })
+ })
+
+ it('reads a field the caller spelled in the other case convention', () => {
+ // These schemas mix the two: a query's own fields are camelCase while a
+ // window's fields are snake_case, and `query-metrics` names its own window
+ // `dateFrom`. A caller cannot tell which without reading the schema.
+ const tool = GENERATED_TOOL_MAP['query-metrics']!()
+ const input = { metricName: 'http_requests', date_from: '2026-09-01T00:00:00Z' }
+
+ expect(rewrapFor(tool.schema, input)).toMatchObject({
+ query: { metricName: 'http_requests', dateFrom: '2026-09-01T00:00:00Z' },
+ })
+ })
+
+ it.each([
+ ['two spellings of one field', { date_from: '-1h', dateFrom: '-7d' }],
+ [
+ 'two spellings of one object',
+ { dateRange: { date_from: '-1h' }, date_range: { date_from: '-7d' } },
+ ],
+ ])('leaves a payload alone when it carries %s', (_label, input) => {
+ // Both keys answer to the same place, so placing them would keep one value
+ // and drop the other without telling the caller which.
+ const tool = GENERATED_TOOL_MAP['query-logs']!()
+
+ expect(rewrapFor(tool.schema, input)).toBeUndefined()
+ })
+
+ it('leaves a loose date key alone when the caller also sent a dateRange', () => {
+ // Two windows in one call is a caller that means something we cannot read,
+ // and picking either one runs a query over a range it did not ask for.
+ const tool = GENERATED_TOOL_MAP['query-logs']!()
+ const input = { dateRange: { date_from: '-1h' }, date_from: '-7d' }
+
+ expect(rewrapFor(tool.schema, input)).toBeUndefined()
+ })
+
+ it('leaves a loose date key alone when the wrapper holds no window', () => {
+ expect(
+ rewrapFor(z.object({ query: z.object({ limit: z.number() }) }), { date_from: '-1h', limit: 10 })
+ ).toBeUndefined()
+ })
+
it('leaves a rejection that names more than the missing wrapper alone', () => {
// Two complaints mean the payload is wrong in a way nesting cannot fix,
// so guessing at one of them would hide the other.
diff --git a/services/mcp/tests/unit/query-wrapper-contract.test.ts b/services/mcp/tests/unit/query-wrapper-contract.test.ts
index 507f7ae6c59a..f7e98029dc49 100644
--- a/services/mcp/tests/unit/query-wrapper-contract.test.ts
+++ b/services/mcp/tests/unit/query-wrapper-contract.test.ts
@@ -43,6 +43,25 @@ function leadExample(
}
}
+/**
+ * The lead example with its window flattened one level further: `date_from` beside the
+ * other fields rather than inside `dateRange`. This is the shape callers send most, and
+ * undoing only the outer wrapper leaves it rejected.
+ */
+function flattenedWindowCall(example: Record<string, unknown>): Record<string, unknown> | undefined {
+ const query = example['query'] as Record<string, unknown>
+ const range = query['dateRange']
+ if (typeof range !== 'object' || range === null || Array.isArray(range)) {
+ return undefined
+ }
+ const { date_from: dateFrom, ...otherBounds } = range as Record<string, unknown>
+ if (typeof dateFrom !== 'string' || Object.keys(otherBounds).length > 0) {
+ return undefined
+ }
+ const { dateRange: _window, ...fields } = query
+ return { ...fields, date_from: dateFrom }
+}
+
/** An example below this offset sits under a workflow section, which a caller reaches after it composed the call. */
const LEAD_EXAMPLE_MAX_OFFSET = 700
@@ -56,6 +75,14 @@ describe('tools whose payload sits under a required `query` object', () => {
expect(tools.length).toBeGreaterThan(10)
})
+ it('covers the flattened window on most of them', () => {
+ const withWindow = tools.filter(([name]) =>
+ flattenedWindowCall(leadExample(getToolDefinition(name).description)!.json)
+ )
+
+ expect(withWindow.length).toBeGreaterThan(10)
+ })
+
describe.each(tools)('%s', (name, tool) => {
const description = getToolDefinition(name).description
const example = leadExample(description)
@@ -85,13 +112,17 @@ describe('tools whose payload sits under a required `query` object', () => {
expect(message).toContain('resend them as {"query": {')
})
- it('rewraps that flattened example back into the call the caller meant', () => {
- const flattened = example!.json['query'] as Record<string, unknown>
+ it.each([
+ ['as the caller sent it', (json: Record<string, unknown>) => json['query'] as Record<string, unknown>],
+ ['with its window flattened too', flattenedWindowCall],
+ ])('rewraps the example flattened %s back into the call the caller meant', (_label, flatten) => {
+ const flattened = flatten(example!.json)
+ if (!flattened) {
+ return
+ }
const rejected = tool.schema.safeParse(flattened, { reportInput: true })
- const rewrapped = rewrapFlattenedArguments(rejected.error!, flattened, tool.schema)
-
- expect(rewrapped).toEqual(example!.json)
+ expect(rewrapFlattenedArguments(rejected.error!, flattened, tool.schema)).toEqual(example!.json)
})
})
})