Skip to content

feat: Add natural language to query feature#2859

Merged
languy merged 139 commits intomainfrom
dev/languye/cdb-chat-participant-with-copilot-query
Mar 30, 2026
Merged

feat: Add natural language to query feature#2859
languy merged 139 commits intomainfrom
dev/languye/cdb-chat-participant-with-copilot-query

Conversation

@languy
Copy link
Copy Markdown
Contributor

@languy languy commented Dec 3, 2025

Summary

Adds AI-powered natural language to Cosmos DB NoSQL query capabilities, integrating GitHub Copilot into the Azure Cosmos DB VS Code extension. Users can generate, explain, and edit queries using natural language — both from the Query Editor toolbar and via a @cosmosdb chat participant in VS Code Chat.

Key Features

@cosmosdb Chat Participant

  • Registers a new VS Code chat participant (@cosmosdb) with dedicated slash commands:
    • /generateQuery — Generate a new Cosmos DB NoSQL query from a natural language description
    • /explainQuery — Get an AI-powered explanation of the current query
    • /editQuery — Edit/optimize an existing query with AI suggestions
    • /question — Ask general Azure Cosmos DB questions
    • /help — Show available commands
  • Supports natural language intent detection (no slash command required) — the LLM classifies the user's intent and routes accordingly
  • Includes "Update Query" and "Open Side-by-Side" action buttons on generated query results

Query Editor AI Integration

  • Adds an AI dropdown button to the Query Editor toolbar with "Generate query" and "Explain query" options
  • Inline GenerateQueryInput component: a text area for entering natural language prompts directly in the query editor panel
  • Supports model selection (when multiple Copilot models are available), prompt history with arrow-key navigation, cancellation, and thumbs up/down feedback
  • "Explain query" opens VS Code Chat with @cosmosdb /explainQuery pre-filled

Schema Sampling Tool (cosmosdb_sampleContainerSchema)

  • Registers a VS Code Language Model Tool that samples documents from the active container to infer its schema
  • The LLM autonomously decides when to call this tool (agentic loop with up to 3 tool rounds)
  • Uses a lightweight query (SELECT TOP 10 * FROM c WHERE c._ts % 2 = 0 ORDER BY c._ts DESC) to minimize RU cost
  • Results are cached in an in-memory query history store so subsequent queries avoid re-sampling
  • Includes user confirmation before sampling to inform about RU consumption

Query Execution History

  • CosmosDbOperationsService maintains per-container in-memory query history (last 20 executions per container)
  • Each entry stores the query text, document count, RU cost, and inferred schema (structure only — no user data is sent to the LLM)
  • History is automatically recorded on every query execution and included as LLM context for better query generation

Architecture

Component Purpose
CosmosDbChatParticipant Main chat participant: routes requests, handles commands, manages intent detection
CosmosDbOperationsService Core execution engine: query generation/explanation via LLM, schema management, query history
systemPrompt.ts All system prompts with shared defense rules (prompt injection prevention, content safety)
userPayload.ts User content builders with clear delimiters separating data from instructions
queryOneShotExamples.ts Few-shot learning examples injected as User/Assistant message pairs
sampleDataTool.ts Language Model Tool registration and schema sampling logic
sanitization.ts Security utilities for escaping HTML/Markdown, sanitizing SQL comments, preventing XSS
copilotUtils.ts Copilot availability detection with retry logic for enabling/disabling scenarios
SchemaAnalyzer.ts Breadth-first schema inference from NoSQL documents (uses denque for efficient queue)

Security

  • All system prompts include mandatory defense rules against prompt injection, role manipulation, and content safety violations (test-enforced in systemPrompt.test.ts)
  • User content is wrapped with explicit delimiters (BEGIN_USER_DATA/END_USER_DATA) to prevent instruction injection
  • SQL comment sanitization prevents breaking out of -- comment context
  • HTML/Markdown escaping prevents XSS in chat responses and webview rendering
  • Command URI validation prevents malicious command: URI injection
  • No user document data is sent to the LLM — only inferred schema structure (property names and types)

Other Changes

  • New dependency: denque (efficient double-ended queue for schema analyzer BFS traversal)
  • Comprehensive NoSQL query language reference added as a resource file for LLM context
  • Query Editor panel sizing adjusted (30/70 split instead of 20/80)
  • README updated with AI-Powered Query Assistance section
  • Extensive localization strings added
  • Telemetry instrumented throughout: query generation, schema sampling, model selection, feedback, cancellation
  • Unit tests for chatUtils, systemPrompt, sanitization, and copilotUtils

39 files changed | +7,313 additions | -35 deletions

languy and others added 30 commits September 3, 2025 15:51
* Initial plan

* Remove getConnectionInfo references from OperationParser and CosmosDbOperationsService

Co-authored-by: languy <21954022+languy@users.noreply.github.com>
…ide-by-side; refactor imports for consistency
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a GitHub Copilot–backed natural-language-to-query workflow for the Cosmos DB Query Editor and introduces a new @cosmosdb VS Code chat participant with query generation/explanation commands, schema sampling tool support, and related utilities.

Changes:

  • Introduces @cosmosdb chat participant with /editQuery, /generateQuery, /explainQuery, /question, plus supporting prompts/payload formatting and tooling.
  • Adds an “AI” entry point in the Query Editor UI (toolbar + generate input) and wires webview ↔ extension commands/events for query generation and explanation.
  • Adds schema sampling + sanitization + Copilot availability utilities, along with unit tests and a NoSQL query language reference asset.

Reviewed changes

Copilot reviewed 37 out of 38 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/webviews/cosmosdb/QueryEditor/state/QueryEditorState.tsx Adds AI-related UI state flags and actions.
src/webviews/cosmosdb/QueryEditor/state/QueryEditorContextProvider.tsx Adds new AI commands/events wiring (query generation + AI enablement).
src/webviews/cosmosdb/QueryEditor/state/QueryEditorContext.tsx Exposes internal reducer dispatch via context for UI components.
src/webviews/cosmosdb/QueryEditor/QueryPanel/usePromptHistory.ts New hook to support prompt history navigation in the generate input.
src/webviews/cosmosdb/QueryEditor/QueryPanel/QueryToolbarOverflow.tsx Adds conditional AI button/menu item and adjusts overflow IDs.
src/webviews/cosmosdb/QueryEditor/QueryPanel/QueryPanel.tsx Renders the new query-generation input area.
src/webviews/cosmosdb/QueryEditor/QueryPanel/QueryMonaco.tsx Syncs initial Monaco value back to state/extension on mount.
src/webviews/cosmosdb/QueryEditor/QueryPanel/GenerateQueryInput.tsx New UI for NL prompt entry, model selection, loading, confirmation, feedback.
src/webviews/cosmosdb/QueryEditor/QueryPanel/ExplainQueryButton.tsx New button to open Copilot chat for query explanation.
src/webviews/cosmosdb/QueryEditor/QueryPanel/AIButton.tsx New AI menu/button that routes to generate/explain actions.
src/webviews/cosmosdb/QueryEditor/QueryEditor.tsx Adjusts pane size defaults to accommodate new UI.
src/utils/sanitization.ts Adds shared sanitization helpers for markdown/code/URIs and SQL comments.
src/utils/sanitization.test.ts Unit tests for sanitization helpers.
src/utils/json/nosql/SchemaAnalyzer.ts Adds schema inference/aggregation utilities for sampled documents.
src/utils/json/nosql/NoSqlTypes.ts Adds NoSQL type inference and JSON type mapping helpers.
src/utils/copilotUtils.ts Adds Copilot availability detection + retry/listeners.
src/utils/copilotUtils.test.ts Unit tests for Copilot availability utilities.
src/panels/QueryEditorTab.ts Adds extension-side query generation, model selection, and AI state broadcast to webviews.
src/extensionVariables.ts Adds a global AI-enabled flag to the extension namespace.
src/extension.ts Registers chat participant + tool, initializes AI availability tracking.
src/cosmosdb/session/QuerySession.ts Records query execution history for AI context.
src/commands/registerCommands.ts Registers chat button commands for applying/opening suggested queries.
src/chat/userPayload.ts Defines and formats user payload blocks with delimiters for LLM requests.
src/chat/systemPrompt.ts Adds system prompts/defense rules and query generation/explanation instructions.
src/chat/systemPrompt.test.ts Tests prompt composition/defense rule inclusion.
src/chat/sampleDataTool.ts Adds LM tool to sample container schema and return inferred structure.
src/chat/queryOneShotExamples.ts Adds few-shot examples to guide query generation.
src/chat/index.ts Exports chat module surface area.
src/chat/cosmosDbChatParticipant.ts Implements the @cosmosdb chat participant and routing/operations.
src/chat/chatUtils.ts Adds LM request helpers, message ordering utilities, and active editor helpers.
src/chat/chatUtils.test.ts Unit tests for message ordering helper.
src/chat/README.md Documents chat participant usage and capabilities.
src/chat/OperationParser.ts Adds suggestion text for next operations based on context.
resources/azurecosmosdb-nosql-query-language.md Adds a NoSQL query language reference used for query generation guidance.
package.json Registers chat participant, tool activation events, commands, and dependency updates.
l10n/bundle.l10n.json Updates localization bundle with new user-facing strings.

Comment thread src/webviews/cosmosdb/QueryEditor/QueryPanel/GenerateQueryInput.tsx
Comment thread src/webviews/cosmosdb/QueryEditor/QueryPanel/GenerateQueryInput.tsx
Comment thread src/utils/json/nosql/NoSqlTypes.ts Outdated
Comment thread src/commands/registerCommands.ts Outdated
Comment thread src/chat/chatUtils.ts
sevoku
sevoku previously approved these changes Mar 27, 2026
bk201-
bk201- previously approved these changes Mar 27, 2026
…related messages. Show error on vscode, not in webview. Display LLM output as debug instead of info.
@languy languy dismissed stale reviews from bk201- and sevoku via bf457a8 March 27, 2026 16:48
@languy languy merged commit 4be671f into main Mar 30, 2026
5 checks passed
@languy languy deleted the dev/languye/cdb-chat-participant-with-copilot-query branch March 30, 2026 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants