Skip to content

Commit

Permalink
Move configuration to context.workspaceState from settings.json (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
potatoqualitee authored Jul 16, 2024
1 parent 9d60056 commit dcba2f3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 50 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,8 @@ Contributions to the Assistants Chat Extension are welcome! If you encounter any
* [VS Code Extension Samples - Chat Example](https://github.com/microsoft/vscode-extension-samples/tree/main/chat-sample)
* [Language Model API](https://code.visualstudio.com/api/extension-guides/language-model)
* [Prompt Builder](https://www.npmjs.com/package/@vscode/prompt-tsx)
* [Azure OpenAI Documentation](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/)
* [Azure OpenAI Documentation](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/)
## Changelog
1.1.0 - Moved configuration to context.workspaceState instead of settings.json
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "assistants-chat-extension",
"displayName": "Assistants Chat Extension",
"description": "A GitHub Copilot Chat Extension that integrates OpenAI Assistants",
"version": "1.0.5",
"version": "1.1.0",
"publisher": "chrissylemaire",
"categories": [
"AI",
Expand Down Expand Up @@ -61,7 +61,7 @@
"gpt-4"
],
"default": "gpt-3.5-turbo",
"description": "The model to use for the chat extension. Choose 'gpt-3.5-turbo' for faster performance or 'gpt-4' for slower but more complex and nuanced responses."
"description": "The model to use for the chat extension. Choose 'gpt-3.5-turbo' for faster performance or 'gpt-4' for slower but more complex and nuanced responses. I'm unsure if this does anything, actually."
},
"assistantsChatExtension.savedAssistantId": {
"type": "string",
Expand All @@ -79,7 +79,6 @@
"chatParticipants": [
{
"id": "assistant",
"sampleRequest": "Ask the assistant how to change the OpenAI assistant",
"fullName": "Assistant",
"name": "assistant",
"description": "Your OpenAI Assistant",
Expand All @@ -93,10 +92,6 @@
}
],
"commands": [
{
"command": "assistant.query",
"title": "AI Assistant: Ask Assistant"
},
{
"command": "assistantsChatExtension.setApiKey",
"title": "AI Assistant: Set API Key"
Expand Down
21 changes: 7 additions & 14 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,38 +108,32 @@ async function createWrapper(configuration: vscode.WorkspaceConfiguration): Prom
*/
async function updateChatParticipant(context: vscode.ExtensionContext, configuration: vscode.WorkspaceConfiguration) {
const model = configuration.get<string>('model', 'gpt-3.5-turbo');
let assistantId = configuration.get<string>('savedAssistantId', '');
let assistantId = context.workspaceState.get<string>('savedAssistantId', '');
const wrapper = await createWrapper(configuration);

if (assistantId) {
const assistants = await wrapper.getAssistants();
const savedAssistant = assistants.find((assistant: Assistant) => assistant.id === assistantId);

if (!savedAssistant) {
if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) {
await configuration.update('savedAssistantId', '', vscode.ConfigurationTarget.Workspace);

console.log("savedAssistantId cleared from settings.");
}
console.log("Saved assistant not found. Clearing savedAssistantId.");
context.workspaceState.update('savedAssistantId', undefined);
assistantId = '';
}
}

if (!assistantId) {
const newAssistantId = await promptForAssistant(wrapper, configuration);
const newAssistantId = await promptForAssistant(wrapper, context);
if (newAssistantId) {
assistantId = newAssistantId;
if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) {
await configuration.update('savedAssistantId', assistantId, vscode.ConfigurationTarget.Workspace);
console.log("savedAssistantId updated in workspace settings.");
}
context.workspaceState.update('savedAssistantId', assistantId);
} else {
console.warn('No assistant selected. The chat participant cannot be created.');
return;
}
}

const chatParticipant = await registerChatParticipant(context, wrapper, model, assistantId, configuration);
const chatParticipant = await registerChatParticipant(context, wrapper, model, assistantId);
console.log(`Chat participant updated with model: ${model} and assistantId: ${assistantId}`);
}

Expand Down Expand Up @@ -183,9 +177,8 @@ export async function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand('assistantsChatExtension.selectAssistant', async () => {
try {
const wrapper = await createWrapper(configuration);
const assistantId = await promptForAssistant(wrapper, configuration);
const assistantId = await promptForAssistant(wrapper, context);
if (assistantId) {
configuration = vscode.workspace.getConfiguration('assistantsChatExtension');
try {
await updateChatParticipant(context, configuration);
} catch (error) {
Expand Down
36 changes: 8 additions & 28 deletions src/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class Wrapper {
* @param stream - The chat response stream.
* @returns The ID of the selected assistant.
*/
export async function promptForAssistant(wrapper: Wrapper, configuration: vscode.WorkspaceConfiguration, stream?: vscode.ChatResponseStream): Promise<string | undefined> {
export async function promptForAssistant(wrapper: Wrapper, context: vscode.ExtensionContext, stream?: vscode.ChatResponseStream): Promise<string | undefined> {
const assistants = await wrapper.getAssistants();

if (assistants.length === 0) {
Expand Down Expand Up @@ -228,11 +228,7 @@ export async function promptForAssistant(wrapper: Wrapper, configuration: vscode
}
} else if (assistants.length === 1) {
const assistant = assistants[0];
if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) {
await configuration.update('savedAssistantId', assistant.id, vscode.ConfigurationTarget.Workspace);
} else {
console.warn("No workspace is open. The assistant ID won't be saved to workspace settings.");
}
context.workspaceState.update('savedAssistantId', assistant.id);
if (stream) {
stream.markdown(`Automatically selected assistant: ${assistant.name || assistant.id}${newlineSpacing}`);
}
Expand All @@ -251,11 +247,7 @@ export async function promptForAssistant(wrapper: Wrapper, configuration: vscode
if (selectedAssistantName) {
const selectedAssistant = assistants.find((assistant: Assistant) => assistant.name === selectedAssistantName);
if (selectedAssistant) {
if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) {
await configuration.update('savedAssistantId', selectedAssistant.id, vscode.ConfigurationTarget.Workspace);
} else {
console.log("No workspace is open. The assistant ID won't be saved to workspace settings.");
}
context.workspaceState.update('savedAssistantId', selectedAssistant.id);
return selectedAssistant.id;
}
} else {
Expand All @@ -281,7 +273,7 @@ export async function promptForAssistant(wrapper: Wrapper, configuration: vscode
* The function ensures that the "Using assistant" message is shown only on the first interaction
* or when starting a new session, and not after changing assistants.
*/
export async function registerChatParticipant(context: vscode.ExtensionContext, wrapper: Wrapper, model: string, assistantId: string, configuration: vscode.WorkspaceConfiguration): Promise<vscode.ChatParticipant> {
export async function registerChatParticipant(context: vscode.ExtensionContext, wrapper: Wrapper, model: string, assistantId: string): Promise<vscode.ChatParticipant> {
let userId = context.globalState.get<string>('assistantsChatExtension.userId');
if (!userId) {
userId = `user_${Date.now()}`;
Expand All @@ -293,14 +285,10 @@ export async function registerChatParticipant(context: vscode.ExtensionContext,
let isFirstInteraction = context.workspaceState.get<boolean>('assistantsChatExtension.isFirstInteraction', true);

if (request.command === 'change') {
const newAssistantId = await promptForAssistant(wrapper, configuration, stream);
const newAssistantId = await promptForAssistant(wrapper, context, stream);
if (newAssistantId) {
assistantId = newAssistantId;
if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) {
await configuration.update('savedAssistantId', assistantId, vscode.ConfigurationTarget.Workspace);
} else {
console.log("No workspace is open. The assistant ID won't be saved for future sessions.");
}
context.workspaceState.update('savedAssistantId', assistantId);
const assistants = await wrapper.getAssistants();
const selectedAssistant = assistants.find((assistant: Assistant) => assistant.id === assistantId);
if (selectedAssistant && selectedAssistant.name) {
Expand All @@ -309,11 +297,7 @@ export async function registerChatParticipant(context: vscode.ExtensionContext,
stream.markdown(`You have switched to the assistant with ID: **${assistantId}**.${newlineSpacing}`);
}
isFirstInteraction = false;
try {
await context.workspaceState.update('assistantsChatExtension.isFirstInteraction', false);
} catch (error) {
console.log("Unable to update workspace state. The interaction state won't be saved.");
}
context.workspaceState.update('assistantsChatExtension.isFirstInteraction', false);
} else {
stream.markdown(`No assistant selected. Please try again.${newlineSpacing}`);
}
Expand All @@ -326,11 +310,7 @@ export async function registerChatParticipant(context: vscode.ExtensionContext,
stream.markdown(`Using assistant: **${assistant.name || assistant.id}**. You can use the \`/change\` command to switch assistants.${newlineSpacing}`);
}
isFirstInteraction = false;
try {
await context.workspaceState.update('assistantsChatExtension.isFirstInteraction', false);
} catch (error) {
console.warn("Unable to update workspace state. The interaction state won't be saved.", error);
}
context.workspaceState.update('assistantsChatExtension.isFirstInteraction', false);
}

if (!assistantId) {
Expand Down

0 comments on commit dcba2f3

Please sign in to comment.