AI Assistant: fix json schema building#33588
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the Grid AI Assistant’s response JSON Schema generation to be compatible with structured-output APIs (notably OpenAI Structured Outputs), by normalizing unsupported schema constructs and ensuring $ref resolution works at the schema root.
Changes:
- Switch JSON Schema generation to
zod-to-json-schema’sopenAitarget and remove reliance on draft-07$schema. - Add schema post-processing utilities to (1) expand array-style
typetoanyOfand (2) hoist/rewrite$reftargets into root-level$defs. - Refactor
filterValuecommand schema to a discriminated-union object format (instead of tuple arrays) and update/add tests for schema + execution + integration behavior.
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/devextreme/js/__internal/grids/grid_core/ai_assistant/utils.ts | Adds JSON Schema transformation utilities (expandTypeArraysToAnyOf, $ref hoisting/rewriting helpers). |
| packages/devextreme/js/__internal/grids/grid_core/ai_assistant/types.ts | Simplifies JsonSchema typing and introduces ResponseSchemaBranch shape used by schema builder. |
| packages/devextreme/js/__internal/grids/grid_core/ai_assistant/grid_commands.ts | Builds OpenAI-targeted response schema, expands type unions, and hoists $defs to schema root. |
| packages/devextreme/js/__internal/grids/grid_core/ai_assistant/commands/index.ts | Removes obsolete TODO comment. |
| packages/devextreme/js/__internal/grids/grid_core/ai_assistant/commands/filtering.ts | Changes filter expression input to object-based discriminated union; converts to DataGrid’s expected array filter format at execution. |
| packages/devextreme/js/__internal/grids/grid_core/ai_assistant/commands/tests/filtering.test.ts | Updates filter command schema/execute tests for the new object expression format. |
| packages/devextreme/js/__internal/grids/grid_core/ai_assistant/ai_assistant_integration_controller.ts | Refactors context building into overridable buildContext / buildColumnContext and caches controllers during init(). |
| packages/devextreme/js/__internal/grids/grid_core/ai_assistant/tests/utils.test.ts | Adds unit tests for the new schema utilities (expandTypeArraysToAnyOf, hoistSchemaRefs). |
| packages/devextreme/js/__internal/grids/grid_core/ai_assistant/tests/grid_commands.test.ts | Updates schema tests for OpenAI target and adds coverage for $defs hoisting / inline $ref rewriting. |
| packages/devextreme/js/__internal/grids/grid_core/ai_assistant/tests/ai_assistant_integration_controller.integration.test.ts | Minor typing cleanup in test setup (aligns with new controller behavior). |
| packages/devextreme/js/__internal/grids/grid_core/ai_assistant/tests/ai_assistant_controller.test.ts | Adds coverage for additional failure paths, request concurrency rejection, header title formatting, and dispose behavior. |
| packages/devextreme/js/__internal/grids/data_grid/ai_assistant/commands/index.ts | Removes obsolete TODO comment. |
| packages/devextreme/js/__internal/grids/data_grid/ai_assistant/ai_assistant_integration_controller.ts | Updates overrides to buildContext / buildColumnContext to enrich context with summary info. |
| packages/devextreme/js/__internal/grids/data_grid/ai_assistant/tests/ai_assistant_integration_controller.integration.test.ts | New integration tests validating DataGrid-specific context enrichment and lifecycle behaviors. |
| packages/devextreme/js/__internal/grids/data_grid/ai_assistant/tests/ai_assistant_controller.test.ts | New controller tests ensuring DataGrid controller inherits core behavior and wiring. |
7fbd87c to
02e3f1f
Compare
| : 'Apply a filter.'; | ||
|
|
||
| try { | ||
| const filterValue = args.expression === null |
There was a problem hiding this comment.
Minor: convertFilterExprToArray could convert null to undefined too.
| value: z.union([z.string(), z.number()]), | ||
| }).strict(); | ||
|
|
||
| export const compositeKeyToObject = ( |
There was a problem hiding this comment.
Question: do we need export this method?
| const commands = [...this.commands.values()]; | ||
|
|
||
| return commands.map((command) => { | ||
| const argsSchema = zodToJsonSchema(command.schema, { target: 'openAi' }) as JsonSchema; |
There was a problem hiding this comment.
Question: Do we still need change target to openAi? I saw jsonSchema7 worked also.
|
|
||
| /** | ||
| * Recursively converts JSON Schema array-style `type` | ||
| * (e.g. `{"type": ["string", "number"]}`) to the equivalent `anyOf` form |
There was a problem hiding this comment.
We could write unit tests instead of comments to make the code more clear.
No description provided.