Chat context provider sample (#1263)

This commit is contained in:
Alex Ross
2026-01-21 17:28:42 +01:00
committed by GitHub
parent 0c01b9ad1c
commit c71db4ab93
12 changed files with 3508 additions and 0 deletions

View File

@ -0,0 +1,8 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"dbaeumer.vscode-eslint",
"ms-vscode.extension-test-runner"
]
}

21
chat-context-sample/.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,21 @@
// A launch configuration that compiles the extension and then opens it inside a new window
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
{
"version": "0.2.0",
"configurations": [
{
"name": "Run Extension",
"type": "extensionHost",
"request": "launch",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}"
],
"outFiles": [
"${workspaceFolder}/out/**/*.js"
],
"preLaunchTask": "${defaultBuildTask}"
}
]
}

View File

@ -0,0 +1,11 @@
// Place your settings in this file to overwrite default and user settings.
{
"files.exclude": {
"out": false // set this to true to hide the "out" folder with the compiled JS files
},
"search.exclude": {
"out": true // set this to false to include "out" folder in search results
},
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
"typescript.tsc.autoDetect": "off"
}

20
chat-context-sample/.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,20 @@
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "watch",
"problemMatcher": "$tsc-watch",
"isBackground": true,
"presentation": {
"reveal": "never"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

View File

@ -0,0 +1,11 @@
.vscode/**
.vscode-test/**
src/**
.gitignore
.yarnrc
vsc-extension-quickstart.md
**/tsconfig.json
**/eslint.config.mjs
**/*.map
**/*.ts
**/.vscode-test.*

View File

@ -0,0 +1,9 @@
# Change Log
All notable changes to the "chat-context-sample" extension will be documented in this file.
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
## [Unreleased]
- Initial release

View File

@ -0,0 +1,18 @@
# Chat Context Provider Sample
This sample shows how to provide custom context to VS Code's chat feature using the Chat Context Provider API.
The sample uses the [`ChatContextProvider`](https://code.visualstudio.com/api/references/vscode-api#ChatContextProvider) API to implement a simple context provider that displays line count information for JSON files:
![JSON line count context](example.png)
The extension registers a chat context provider that:
- Activates when JSON files are opened
- Provides the line count of JSON files as contextual information
- Makes this context available in VS Code's chat interface
## VS Code API
### `ChatContextProvider` API proposal
You can find details about this API proposal [here](https://github.com/microsoft/vscode/issues/271104).

View File

@ -0,0 +1,28 @@
import typescriptEslint from "@typescript-eslint/eslint-plugin";
import tsParser from "@typescript-eslint/parser";
export default [{
files: ["**/*.ts"],
}, {
plugins: {
"@typescript-eslint": typescriptEslint,
},
languageOptions: {
parser: tsParser,
ecmaVersion: 2022,
sourceType: "module",
},
rules: {
"@typescript-eslint/naming-convention": ["warn", {
selector: "import",
format: ["camelCase", "PascalCase"],
}],
curly: "warn",
eqeqeq: "warn",
"no-throw-literal": "warn",
semi: "warn",
},
}];

3273
chat-context-sample/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,47 @@
{
"name": "chat-context-sample",
"displayName": "chat-context-sample",
"description": "Sample extension demonstrating the Chat Context Provider API",
"version": "0.0.1",
"engines": {
"vscode": "^1.109.0"
},
"categories": [
"Other"
],
"enabledApiProposals": [
"chatContextProvider"
],
"activationEvents": [
"onChatContextProvider:chat-context-sample.jsonLineCount",
"onLanguage:json"
],
"main": "./out/extension.js",
"contributes": {
"chatContext": [
{
"id": "chat-context-sample.jsonLineCount",
"icon": "$(json)",
"displayName": "JSON Line Count"
}
]
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"lint": "eslint src",
"postinstall": "npx @vscode/dts dev"
},
"devDependencies": {
"@types/vscode": "^1.108.0",
"@types/mocha": "^10.0.10",
"@types/node": "20.x",
"@typescript-eslint/eslint-plugin": "^8.31.1",
"@typescript-eslint/parser": "^8.31.1",
"eslint": "^9.25.1",
"typescript": "^5.8.3",
"@vscode/test-cli": "^0.0.11",
"@vscode/test-electron": "^2.5.2"
}
}

View File

@ -0,0 +1,45 @@
import * as vscode from 'vscode';
const PROVIDER_ID = 'chat-context-sample.jsonLineCount';
export function activate(context: vscode.ExtensionContext) {
console.log('Chat context sample extension is now active!');
// Register the chat context provider for JSON files
const provider: vscode.ChatContextProvider = {
provideChatContextForResource(options: { resource: vscode.Uri }, token: vscode.CancellationToken): vscode.ProviderResult<vscode.ChatContextItem | undefined> {
// Find the text document for this resource
const document = vscode.workspace.textDocuments.find(doc => doc.uri.toString() === options.resource.toString());
if (!document) {
return undefined;
}
const lineCount = document.lineCount;
const fileName = options.resource.path.split('/').pop() ?? 'unknown';
return {
icon: new vscode.ThemeIcon('json'),
label: `${fileName}: ${lineCount} lines`,
modelDescription: `The JSON file "${fileName}" has ${lineCount} lines.`,
tooltip: new vscode.MarkdownString(`**Line count:** ${lineCount}`),
value: `File: ${fileName}\nLine count: ${lineCount}`
};
},
resolveChatContext(context: vscode.ChatContextItem, token: vscode.CancellationToken): vscode.ProviderResult<vscode.ChatContextItem> {
// Context items already have values, so just return as-is
return context;
}
};
// Register with a document selector for JSON files
const disposable = vscode.chat.registerChatContextProvider(
{ language: 'json' },
PROVIDER_ID,
provider
);
context.subscriptions.push(disposable);
}
export function deactivate() { }

View File

@ -0,0 +1,17 @@
{
"compilerOptions": {
"module": "Node16",
"target": "ES2022",
"outDir": "out",
"lib": [
"ES2022"
],
"sourceMap": true,
"rootDir": "src",
"strict": true, /* enable all strict type-checking options */
/* Additional Checks */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
}
}