Core Layer

AI Integration Core

Source Files: ai-core.ts, ai.ts Complexity: Low

The AI Integration Core is the foundation of the AI Notes extension. It abstracts LLM interactions behind a provider-agnostic interface and provides transport-level resilience to ensure smooth, uninterrupted user operations.

1. Overview

Visual Studio Code extensions that use AI often depend heavily on a specific API or require a direct OpenAI subscription. The AI Notes core abstracts this so the extension can operate seamlessly under two environments:

  • VS Code Language Model API (LM API): Native, zero-setup AI utilizing whatever models the user has licensed in their VS Code environment (e.g., Copilot Chat).
  • SAP AI Core: An enterprise-grade foundation models provider (Azure OpenAI models) configured using an enterprise service key.

2. User Experience (UX) Flow

  1. Configuration: The user opens VS Code Settings and searches for "AI Notes".
  2. Select Provider: The user chooses between vscode-lm-api (default) or sap-ai-core.
  3. Specify Model: The user enters their target model in ai-notes.aiModel (defaults to gpt-4.1 for LM API, or gpt-4o for SAP AI Core).
  4. Enter Credentials: If using SAP AI Core, the user provides a service key in ai-notes.aiCoreServiceKey.
  5. Seamless Execution: When a feature (like auto-classification, summarization, or chat) triggers, the extension queries the configured provider without the user having to manage separate keys or endpoints.

3. VS Code Visual Settings Mockup

Settings - AI Notes
βš™οΈ User Settings
// The LLM provider to use for AI Notes "ai-notes.llmProvider": "vscode-lm-api", // The OpenAI model to use for the LLM Provider "ai-notes.aiModel": "gpt-4o", // The service key for SAP AI Core (Only required if provider is sap-ai-core) "ai-notes.aiCoreServiceKey": "eyJhY2Nlc3NfdG9rZW4iOiJle..."

4. Data Structures & Types

export interface NoteMetadata {
    tags: string[];      // Relevant hashtags parsed for categorization
    name: string;        // Concisely proposed note slug (e.g. "meeting-notes")
    path: string;        // The relative folder destination inside the vault
}

5. Under the Hood & Code Walkthrough

5.1 Transport-level Retry Logic

All LLM requests pass through chatCompletionWithRetry. If a request fails due to temporary rate-limiting, authentication spikes, or internet dropped frames, it automatically retries up to three times synchronously before displaying an error alert to the user:

export async function chatCompletionWithRetry(prompt: string, retries = 3): Promise<string> {
    for (let attempt = 0; attempt < retries; attempt++) {
        try {
            return await aiCompletion(prompt);
        } catch (err: any) {
            console.warn(`Attempt ${attempt + 1} failed: ${err.message}`);
        }
    }
    throw new Error('All attempts to generate response failed.');
}

5.2 Native VS Code Language Model API Connection

Native execution routes through the vscode.lm namespace. It leverages built-in authentication systems directly within VS Code, letting users take advantage of zero-setup AI tools natively:

async function vsCodeLMAPIChatCompletion(prompt: string, aiModel: string): Promise<string> {
    if (vscode.lm && vscode.lm.selectChatModels) {
        const [model] = await vscode.lm.selectChatModels({
            vendor: 'copilot',
            family: aiModel,
        });
        if (!model) { throw new Error('No suitable AI model found.'); }

        const response = await model.sendRequest([
            vscode.LanguageModelChatMessage.User(prompt)
        ]);
        let completeResponse = '';
        for await (const fragment of response.text) {
            completeResponse += fragment;
        }
        return completeResponse;
    } else {
        throw new Error('VS Code LM API not available.');
    }
}