Create and implement your AI plugin
Go Up to Smart CodeInsight
Create an AI plugin
RAD Studio gives you the opportunity to implement your own AI Plugin on top of the provided AIEngine and ToolsAPI.AI.
Using the new interfaces provided in unit ToolsAPI.AI you can consume the available AI plugins or create your own Plugin. To achieve this, you need to create a standard Delphi package (bpl) by following these steps:
- Create a New Package by following the steps below:
- Open RAD Studio.
- Go to File > New > Package - Delphi.
- Enter a name for your package.
- Click OK.
- Add Required Units, the project structure can be like the following:
-
MyPlugin.Main.pas
Attention: Please consider the following key points when setting up this unit:- The unit must include a class that implements the
IOTAAIPlugin
interface fromToolsAPI.AI
- The following functions and procedures must be implemented properly:
-
function GetName: string;
-
function GetFeatures: TAIFeatures;
-
function GetEnabled: Boolean;
-
function GetSettingFrame(AOwner: TComponent): IOTAAIPluginSetting;
-
function AddNotifier(const ANotifier: IOTAAIServicesNotifier): Integer;
-
procedure RemoveNotifier(const AIndex: Integer);
-
- The plugin must register itself on the AIEngine using a “Register” procedure.
- The unit must include a class that implements the
-
MyPlugin.Setting.pas
andMyPlugin.Setting.dfm
which is a Tframe.Attention: Please consider the following key points when setting up the frame (MyPlugin.Setting
):- The TFrame class must carefully implement the
IOTAAIPluginSetting
interface fromToolsAPI.AI
. - The SaveSettings and LoadSettings procedures are mandatory.
Tip: They must be implemented to store and reload the settings smoothly and efficiently, you can do it in your desired approach like keeping them in the Windows registry, in the ini file, or even in an SQLite database. - Use the
ParameterValidations
function to validate your parameters and avoid the malfunctioning of your plugin and reciprocally the IDE.
- The TFrame class must carefully implement the
- A unit to handle Rest requests and responses.
- Any other optional UI or non-UI elements.
-
- Build and install your package.
- Restart RAD Studio.
- Find your plugin in the Smart CodeInsight Options page and configure it.
- Use it for AI commands in the Chat Window or Editor.
Implement an AI backend
Create a class implementing IOTAAIPlugin. This is the public-facing (and IDE-facing) interface your AI plugin needs to implement and abstract over AI functionalities. Implement GetFeatures to return the set of TAIFeatures that your plugin supports.
Register your new plugin via RegisterPlugin to return a plugin index. At shutdown, remove it via UnregisterPlugin, passing the same index.
Here is an example of the Anthropic Claude plugin :
class procedure TClaudePlugin.ProductStartup(Sender: TObject);
begin
if Supports(BorlandIDEServices, IOTAAIEngineService, AIEngineService) then
begin
ClaudePlugin := TClaudePlugin.Create;
PluginIndex := AIEngineService.RegisterPlugin(ClaudePlugin);
end;
end;
class procedure TClaudePlugin.ProductFinish(Sender: TObject);
begin
if Assigned(AIEngineService) and (PluginIndex <> -1) then
AIEngineService.UnregisterPlugin(PluginIndex);
end;
These methods are called during the initialization and finalization clauses.
The GetSettingFrame method contains the settings and returns an IOTAAIPluginSetting. This property is notified when loading into the UI from the plugin (LoadSettings) or moving from the UI to the plugin (SaveSettings). You can also return a boolean from GetModified to indicate if any changes were made, which affects whether you are prompted to close the Options dialog without saving.