VS Code Treatment Assignment Service (TAS) Client

0.1.86 · active · verified Tue Apr 21

The `vscode-tas-client` package facilitates A/B experimentation within Visual Studio Code extensions by providing an interface to query and store experiment information from the Microsoft Treatment Assignment Service (TAS). As of version 0.1.86, it is specifically designed for integration with VS Code's extension host environment, leveraging `vscode.Memento` for caching experiment data and `IExperimentationTelemetry` for structured telemetry reporting, including GDPR-classified counterfactual logging. The package manages background refreshes of treatment variables every 30 minutes and offers both synchronous and asynchronous methods for service initialization and variable retrieval, allowing extensions to control data freshness and startup performance. Its core differentiation lies in its tight integration with the VS Code ecosystem, handling aspects like user population targeting, telemetry, and persistence seamlessly for extension developers. The release cadence is typically tied to internal Microsoft development cycles, often aligning with VS Code's own updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes the `vscode-tas-client` experimentation service within a VS Code extension's `activate` function and demonstrates how to retrieve treatment variable values, including forcing a refresh for the latest data. It also includes a mock telemetry implementation.

import * as vscode from 'vscode';
import {
  getExperimentationServiceAsync,
  TargetPopulation,
  IExperimentationService,
  IExperimentationTelemetry,
} from 'vscode-tas-client';

// A mock implementation for IExperimentationTelemetry to satisfy the interface.
// In a real extension, you would likely use VS Code's built-in telemetry reporter.
class MockTelemetry implements IExperimentationTelemetry {
  public commonProperties: Record<string, string> = {};

  setCommonProperties(properties: Record<string, string>): void {
    this.commonProperties = { ...this.commonProperties, ...properties };
  }

  postEvent(eventName: string, props?: Record<string, any>): void {
    console.log(`Telemetry Event: ${eventName}`, { ...this.commonProperties, ...props });
  }

  dispose(): void {
    // No-op for mock telemetry
  }
}

export async function activate(context: vscode.ExtensionContext) {
  console.log('Extension "my-experiment-extension" is active!');

  const extensionName = 'my-experiment-extension';
  const extensionVersion = '1.0.0';
  // Determine the target population based on user settings or environment
  const targetPopulation = process.env.VSCODE_INSIDERS === 'true' ? TargetPopulation.Insiders : TargetPopulation.Public;
  const telemetry = new MockTelemetry();
  const memento = context.globalState;

  let experimentationService: IExperimentationService;

  try {
    // Initialize the experimentation service asynchronously to ensure cached data is loaded.
    experimentationService = await getExperimentationServiceAsync(
      extensionName,
      extensionVersion,
      targetPopulation,
      telemetry,
      memento
    );
    console.log('Experimentation service initialized.');

    // Query a treatment variable. 'vscode' is the standard configId.
    const myFeatureToggleValue = experimentationService.getTreatmentVariable('vscode', 'myFeatureToggle');
    console.log(`Value for 'myFeatureToggle': ${myFeatureToggleValue}`);

    // If you need to force a refresh and get the very latest value from TAS,
    // use getTreatmentVariableAsync. This will trigger a network request.
    const latestMyFeatureToggleValue = await experimentationService.getTreatmentVariableAsync('vscode', 'myFeatureToggle');
    console.log(`Latest value for 'myFeatureToggle' after refresh: ${latestMyFeatureToggleValue}`);

  } catch (error) {
    console.error('Failed to initialize experimentation service:', error);
  }
}

// Standard VS Code extension deactivate function.
export function deactivate() {
  console.log('Extension "my-experiment-extension" is deactivating!');
}

view raw JSON →