Skip to content

Commit

Permalink
Update: Split the viewApi
Browse files Browse the repository at this point in the history
  • Loading branch information
whats2000 committed Nov 15, 2024
1 parent 4ae8311 commit 42e4071
Show file tree
Hide file tree
Showing 16 changed files with 750 additions and 606 deletions.
45 changes: 45 additions & 0 deletions VSCodeExtension/code-brt/src/api/viewApi/historyManagerApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { HistoryManagerApi } from './types';
import { HistoryManager } from '../historyManager';

export const createHistoryManagerApi = (
historyManager: HistoryManager,
): HistoryManagerApi => {
return {
getCurrentHistory: () => {
return historyManager.getCurrentHistory();
},
addNewConversationHistory: async () => {
return await historyManager.addNewConversationHistory();
},
editLanguageModelConversationHistory: (entryID, newMessage) => {
historyManager.editConversationEntry(entryID, newMessage);
},
getHistoryIndexes: () => {
return historyManager.getHistoryIndexes();
},
switchHistory: (historyID) => {
return historyManager.switchHistory(historyID);
},
deleteHistory: async (historyID) => {
return await historyManager.deleteHistory(historyID);
},
updateHistoryTitleById: (historyID, title) => {
historyManager.updateHistoryTitleById(historyID, title);
},
addHistoryTag: (historyID, tag) => {
historyManager.addTagToHistory(historyID, tag);
},
removeHistoryTag: (historyID, tag) => {
historyManager.removeTagFromHistory(historyID, tag);
},
updateHistoryModelAdvanceSettings: (historyID, advanceSettings) => {
historyManager.updateHistoryModelAdvanceSettings(
historyID,
advanceSettings,
);
},
addConversationEntry: async (entry) => {
return await historyManager.addConversationEntry(entry);
},
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import {
type GetLanguageModelResponseParams,
LanguageModelServiceApi,
} from './types';
import type { LoadedModelServices } from '../../services/languageModel/types';
import { triggerEvent } from '../registerView';
import { HistoryManager } from '../historyManager';
import { SettingsManager } from '../settingsManager';

export const createLanguageModelServiceApi = (
models: LoadedModelServices,
historyManager: HistoryManager,
settingsManager: SettingsManager,
): LanguageModelServiceApi => {
return {
getLanguageModelResponse: async (
options: GetLanguageModelResponseParams,
) => {
return await models[options.modelServiceType].service.getResponse({
...options,
historyManager,
sendStreamResponse: options.useStream
? (msg) => {
triggerEvent('streamResponse', msg);
}
: undefined,
updateStatus: options.showStatus
? (status) => {
triggerEvent('updateStatus', status);
}
: undefined,
});
},
stopLanguageModelResponse: (modelServiceType) => {
models[modelServiceType].service.stopResponse();
},
getAvailableModels: (modelServiceType) => {
if (modelServiceType === 'custom') {
return settingsManager.get('customModels').map((model) => model.name);
}

return settingsManager.get(`${modelServiceType}AvailableModels`);
},
getCurrentModel: (modelServiceType) => {
return settingsManager.get(`lastSelectedModel`)[modelServiceType];
},
setAvailableModels: (modelServiceType, newAvailableModels) => {
settingsManager
.set(`${modelServiceType}AvailableModels`, newAvailableModels)
.then(() => {
models[modelServiceType].service.updateAvailableModels(
newAvailableModels,
);
});
},
setCustomModels: (newCustomModels) => {
settingsManager.set('customModels', newCustomModels).then(() => {
models.custom.service.updateAvailableModels(
newCustomModels.map((model) => model.name),
);
});
},
switchModel: (modelServiceType, modelName) => {
models[modelServiceType].service.switchModel(modelName);
},
getLatestAvailableModelNames: async (modelServiceType) => {
return await models[
modelServiceType
].service.getLatestAvailableModelNames();
},
};
};
83 changes: 83 additions & 0 deletions VSCodeExtension/code-brt/src/api/viewApi/miscApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import vscode from 'vscode';

import type { MiscApi } from './types';
import { ToolServiceProvider } from '../../services/tools';
import { triggerEvent } from '../registerView';
import { FileOperationsProvider } from '../../utils';
import { TerminalManager } from '../../integrations';

export const createMiscApi = (
ctx: vscode.ExtensionContext,
terminalManager: TerminalManager,
connectedViews: Partial<Record<string, vscode.WebviewView>>,
): MiscApi => {
return {
alertMessage: async (msg, type, selections) => {
const showFunction = {
info: vscode.window.showInformationMessage,
warning: vscode.window.showWarningMessage,
error: vscode.window.showErrorMessage,
}[type];

const selectionOptions = selections?.map((selection) => selection.text);
if (selectionOptions) {
await showFunction(msg, ...selectionOptions).then((selection) => {
if (!selection) {
return;
}
const commandArgs = selections?.find(
(s) => s.text === selection,
)?.commandArgs;
if (!commandArgs || commandArgs.length === 0) {
return;
}
vscode.commands.executeCommand(
commandArgs[0],
...commandArgs.slice(1),
);
});
}
},
uploadFile: async (base64Data, originalFileName) => {
return FileOperationsProvider.uploadFile(
ctx,
base64Data,
originalFileName,
);
},
deleteFile: async (absolutePath) => {
return FileOperationsProvider.deleteFile(absolutePath);
},
getWebviewUri: async (absolutePath: string) => {
return await FileOperationsProvider.getWebviewUri(
ctx,
connectedViews,
absolutePath,
);
},
openExternalLink: async (url) => {
await vscode.env.openExternal(vscode.Uri.parse(url));
},
openKeyboardShortcuts: async (commandId) => {
await vscode.commands.executeCommand(
'workbench.action.openGlobalKeybindings',
`@command:${commandId}`,
);
},
openExtensionMarketplace: async (extensionId) => {
await vscode.commands.executeCommand(
'workbench.extensions.search',
extensionId,
);
},
approveToolCall: async (toolCall) => {
return await ToolServiceProvider.executeToolCall(
toolCall,
terminalManager,
(status) => {
triggerEvent('updateStatus', status);
},
);
},
};
};
18 changes: 18 additions & 0 deletions VSCodeExtension/code-brt/src/api/viewApi/settingApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { SettingApi } from './types';
import { SettingsManager } from '../settingsManager';

export const createSettingApi = (
settingsManager: SettingsManager,
): SettingApi => {
return {
setSettingByKey: async (key, value) => {
await settingsManager.set(key, value);
},
getSettingByKey: (key) => {
return settingsManager.get(key);
},
getAllSettings: async () => {
return await settingsManager.getAllSettings();
},
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import {
AddConversationEntryParams,
ConversationEntry,
ConversationHistory,
ConversationHistoryIndexList,
ConversationModelAdvanceSettings,
} from '../../../types';

export type HistoryManagerApi = {
/**
* Get the conversation history.
* @returns The conversation history.
*/
getCurrentHistory: () => ConversationHistory;

/**
* Add a new conversation history.
* @returns The new conversation history.
*/
addNewConversationHistory: () => Promise<ConversationHistory>;

/**
* Edit the conversation history.
* @param entryID - The ID of the entry to edit.
* @param newMessage - The new message to set.
*/
editLanguageModelConversationHistory: (
entryID: string,
newMessage: string,
) => void;

/**
* Get the conversation history list.
*/
getHistoryIndexes: () => ConversationHistoryIndexList;

/**
* Switch the conversation history.
* @param historyID - The ID of the history to switch to.
* @returns The new conversation history.
*/
switchHistory: (historyID: string) => Promise<ConversationHistory>;

/**
* Delete a conversation history.
* @param historyID - The ID of the history to delete.
* @returns The new conversation history.
*/
deleteHistory: (historyID: string) => Promise<ConversationHistory>;

/**
* Edit the specified history title.
* @param historyID - The ID of the history to edit.
* @param title - The new title.
*/
updateHistoryTitleById: (historyID: string, title: string) => void;

/**
* Add a tag to a history.
* @param historyID - The ID of the history to add the tag to.
* @param tag - The tag to add.
*/
addHistoryTag: (historyID: string, tag: string) => void;

/**
* Remove a tag from a history.
* @param historyID - The ID of the history to remove the tag from.
* @param tag - The tag to remove.
*/
removeHistoryTag: (historyID: string, tag: string) => void;

/**
* Update the current history's model advance settings.
* @param historyID - The ID of the history to update the advance settings for.
* @param advanceSettings - The new advance settings.
*/
updateHistoryModelAdvanceSettings: (
historyID: string,
advanceSettings: ConversationModelAdvanceSettings,
) => void;

/**
* Add a conversation entry to the conversation history for a language model.
* @param parentID - The ID of the parent entry.
* @param sender - The sender of the message.
* @param message - The message to add.
* @param images - The images to add.
* @param modelServiceType - The type of the model service to add the entry to.
* @param modelName - The name of the model to add the entry to.
* @param toolCalls - The tool calls to add.
* @param toolResponses - The tool responses to add.
* @returns The ID of the new entry.
*/
addConversationEntry: (
entry: AddConversationEntryParams,
) => Promise<ConversationEntry>;
};
6 changes: 6 additions & 0 deletions VSCodeExtension/code-brt/src/api/viewApi/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type * from './historyManagerApi';
export type * from './languageModelServiceApi';
export type * from './miscApi';
export type * from './settingApi';
export type * from './viewApi';
export type * from './voiceServiceApi';
Loading

0 comments on commit 42e4071

Please sign in to comment.