AI Integration Core
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
- Configuration: The user opens VS Code Settings and searches for "AI Notes".
- Select Provider: The user chooses between
vscode-lm-api(default) orsap-ai-core. - Specify Model: The user enters their target model in
ai-notes.aiModel(defaults togpt-4.1for LM API, orgpt-4ofor SAP AI Core). - Enter Credentials: If using SAP AI Core, the user provides a service key in
ai-notes.aiCoreServiceKey. - 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
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.');
}
}