Create and implement your AI plugin

From RAD Studio
Jump to: navigation, search

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:

  1. Create a New Package by following the steps below:
    1. Open RAD Studio.
    2. Go to File > New > Package - Delphi.
    3. Enter a name for your package.
    4. Click OK.
  2. 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 from ToolsAPI.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.
    • MyPlugin.Setting.pas and MyPlugin.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 from ToolsAPI.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.
    • A unit to handle Rest requests and responses.
    • Any other optional UI or non-UI elements.
  3. Build and install your package.
  4. Restart RAD Studio.
  5. Find your plugin in the Smart CodeInsight Options page and configure it.
  6. 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.


See Also