Skip to content

About

A Win32 DLL for integrating AI providers (OpenAI, Anthropic, Gemini, etc.) with Clarion applications. Manages chat conversations via SQLite database and provides C-compatible exported functions.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ClarionChat DLL

A C++ DLL that interfaces with a Clarion application to provide AI functionality. The DLL connects to an SQLite database and makes API calls to various AI providers.

⚠️ IMPORTANT SECURITY NOTICE

This repository contains NO API keys or sensitive data. When using this code:

  1. Never commit API keys to version control
  2. Store API keys securely in your local database only
  3. *The SQLite database files (*.sqlite, .db) are excluded from this repository
  4. You must provide your own API keys for each AI provider

Setting Up API Keys

API keys should be stored in the SQLite database's aiProviders table. Create your own clarionai.sqlite database and add your API keys there. Never include the database file in version control.

Project Structure

  • ClarionChat.h - Main header file with exported functions
  • ClarionChat.cpp - Implementation of exported functions
  • ClarionChat.def - Module definition file
  • DatabaseManager.h - Database access layer header
  • DatabaseManager.cpp - Database access layer implementation
  • Provider.h - Base provider interface
  • ProviderFactory.h - Provider factory header
  • ProviderFactory.cpp - Provider factory implementation
  • AnthropicProvider.h - Anthropic-specific provider header
  • AnthropicProvider.cpp - Anthropic-specific provider implementation
  • Utils.h - Utility functions header
  • Utils.cpp - Utility functions implementation
  • sqlite3.h - SQLite header file
  • sqlite3.c - SQLite implementation
  • CMakeLists.txt - CMake build configuration
  • build.bat - Build script

Prerequisites

  • Visual Studio 2019 or later
  • CMake 3.10 or later
  • Windows SDK

Building the DLL

  1. Run the build.bat script to build the DLL:
build.bat
  1. The DLL will be created in the build\bin\Release directory.

Using in Clarion

Step 1: Create a .LIB File

Use Clarion's LibMaker to create a .LIB file from the ClarionChat.dll:

  1. Run LibMaker.exe
  2. Open the ClarionChat.dll
  3. Save as ClarionChat.LIB
  4. Verify that all exports are visible

Step 2: Add Module Declarations

Add the following module declarations to your Clarion application:

MODULE('ClarionChat.LIB')
  GetProviderModels(*CSTRING),CSTRING,RAW,PROC,PASCAL,NAME('GetProviderModels')
  SendChatMessage(*CSTRING),CSTRING,RAW,PROC,PASCAL,NAME('SendChatMessage')
  GetProviderModelsJson(*CSTRING),CSTRING,RAW,PROC,PASCAL,NAME('GetProviderModelsJson')
  SendChatMessageJson(*CSTRING),CSTRING,RAW,PROC,PASCAL,NAME('SendChatMessageJson')
  FreeString(LONG),PROC,PASCAL,NAME('FreeString')
END

Step 3: Use the Functions

Example usage of the GetProviderModels function:

providerGuid  STRING('ASOPELDURYCVLOPA')  ! Provider GUID
result        CSTRING                     ! Pointer to result string

result = GetProviderModels(providerGuid)
IF SUB(result, 1, 4) = 'fail'
  MESSAGE('Error: ' & result)
ELSE
  MESSAGE('Models updated successfully')
END

! Free the memory when done
! Convert CSTRING to LONG for FreeString
FreeString(ADDRESS(result))

IMPORTANT: Always call FreeString to free memory allocated by the DLL functions.

Exported Functions

FreeString

void __stdcall FreeString(char* str);

Frees memory allocated by DLL functions.

  • Parameters:
    • str - Pointer to string to free
  • Returns:
    • None

IMPORTANT: All strings returned by the DLL functions must be freed using this function to prevent memory leaks.

GetProviderModels

const char* __stdcall GetProviderModels(const char* providerGuid);

Gets the models for a specific provider.

  • Parameters:
    • providerGuid - The GUID of the provider
  • Returns:
    • A string containing "success" or "fail: [reason]"
  • Memory Management:
    • The returned string must be freed using FreeString

SendChatMessage

const char* __stdcall SendChatMessage(const char* chatHeaderGuid);

Sends a message to the AI provider and returns the response.

  • Parameters:
    • chatHeaderGuid - The GUID of the chat header
  • Returns:
    • The response from the AI provider or "fail: [reason]" if an error occurred
  • Memory Management:
    • The returned string must be freed using FreeString

GetProviderModelsJson

const char* __stdcall GetProviderModelsJson(const char* providerInfoJson);

Gets the models for a specific provider and returns them as a JSON string. This function does not access the SQLite database.

  • Parameters:
    • providerInfoJson - A JSON string containing the provider information
  • Returns:
    • A JSON string containing the models or "fail: [reason]" if an error occurred
  • Memory Management:
    • The returned string must be freed using FreeString

SendChatMessageJson

const char* __stdcall SendChatMessageJson(const char* chatInfoJson);

Sends a chat message to a specific provider using JSON input and output. This function does not access the SQLite database.

  • Parameters:
    • chatInfoJson - A JSON string containing the provider information, chat header, and chat detail
  • Returns:
    • A JSON string containing the assistant's response
  • Memory Management:
    • The returned string must be freed using FreeString

Memory Management

All strings returned by the DLL functions are dynamically allocated and must be freed by the caller using the FreeString function. Failure to do so will result in memory leaks.

Memory Management Pattern

! 1. Call the DLL function
result = SendChatMessage(chatHeaderGuid)

! 2. Use the result
IF SUB(result, 1, 4) = 'fail'
  MESSAGE('Error: ' & result)
ELSE
  MESSAGE('Response: ' & result)
END

! 3. Free the memory when done
! Convert CSTRING to LONG for FreeString
FreeString(ADDRESS(result))

C++ Example

// 1. Call the DLL function
const char* result = SendChatMessage(chatHeaderGuid);

// 2. Use the result
if (strncmp(result, "fail", 4) == 0) {
    std::cerr << "Error: " << result << std::endl;
} else {
    std::cout << "Response: " << result << std::endl;
}

// 3. Free the memory when done
FreeString((char*)result);

Clarion Example with JSON Functions

AIParm   CSTRING('{"Provider": {"name": "OpenAI","api_key": "your_api_key","api_endpoint": ""}}')
AIResult CSTRING

! Call the function
AIResult = GetProviderModelsJson(AIParm)

! Use the result
stModel.SetValue(AIResult)

! Free the memory - IMPORTANT: Use ADDRESS() to convert CSTRING to LONG
FreeString(ADDRESS(AIResult))

Database Structure

The DLL interacts with an SQLite database located at H:\Dev\C11Apps\ClarionAIDemo\clarionai.sqlite. The database contains two main tables:

aiProviders Table

CREATE TABLE aiProviders (
  GUID CHAR(16) PRIMARY KEY,
  NAME CHAR(20),
  DESCRIPTION CHAR(300),
  API_KEY CHAR(200),
  API_ENDPOINT CHAR(200)
);

aiModels Table

CREATE TABLE aiModels (
  GUID CHAR(16) PRIMARY KEY,
  PROVIDERGUID CHAR(16),
  NAME CHAR(20),
  DESCRIPTION CHAR(20),
  COST_PER_1M_TOKENS CHAR(10),
  IS_AVAILABLE CHAR(20)
);

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A Win32 DLL for integrating AI providers (OpenAI, Anthropic, Gemini, etc.) with Clarion applications. Manages chat conversations via SQLite database and provides C-compatible exported functions.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages