LlmExportHistory Function  
 
BOOL WINAPI LlmExportHistory(
  HCLIENT hClient,  
  LPCTSTR lpszFileName,  
  LONG nMessageCount,  
  LPLONG lpnExported  
);

The LlmExportHistory function exports the conversation history to a JSON-formatted file.

Parameters

hClient
A handle to the client session.
lpszFileName
A pointer to a null-terminated string which specifies the name of the file to be created. This parameter cannot be NULL or a zero-length string.
nMessageCount
The number of the most recent messages and responses which should be exported. If this value is zero, all messages in the current conversation are exported.
lpnExported
A pointer to an integer which will receive the number of messages which were exported when the function returns. If this information is not required, this parameter may be NULL.

Return Value

If the function succeeds, the return value is non-zero. If the function fails, the return value is zero. To get extended error information, call LlmGetLastError.

Remarks

The LlmExportHistory function exports the current conversation history associated with the client session to a JSON-formatted file. The exported history includes both client messages and model responses stored in the current session history.

This function only exports conversation history currently stored in memory for the client session. It cannot be used if the conversation history has been disabled by setting the history size to zero.

If the lpszFileName parameter specifies a directory or device name, the function will fail. It is permitted to use environment variables in the file name by surrounding the variable name with the percent symbol. If the file name contains any leading or trailing space characters, they will be removed. If the file already exists, the current process must have permission to overwrite the file. If the file has the read-only attribute set, the function will fail and the last error code will be set to ST_ERROR_ACCESS_DENIED.

If the nMessageCount parameter is non-zero, the function exports only the most recent messages in the conversation history. Older messages are skipped. If the specified count exceeds the number of available messages, all available messages are exported.

The exported file uses UTF-8 encoded JSON text and is intended to be human-readable. Applications should not depend on the exact formatting or internal structure of the file because future versions of the library may include additional metadata or fields. To help prevent data loss, the function writes the exported history to a temporary file and only replaces the destination file after the export operation has completed successfully. If the export fails, any existing file specified by the lpszFileName parameter is left unchanged.

The exported file is intended to provide a simple mechanism for persisting conversation state between application sessions. A previously exported history file can later be restored using the LlmImportHistory function.

Important: The conversation history may contain sensitive or private information. Applications should take reasonable precautions to protect exported history files, such as limiting access permissions or encrypting the exported file. Applications which allow users to export conversation history should consider informing users that the exported file may contain private information.

Example

LONG nExported = 0;

// Export the complete conversation history to a JSON file
if (LlmExportHistory(hClient,
                     _T("%USERPROFILE%\\Documents\\conversation.json"),
                     0,
                     &nExported))
{
    _tprintf(_T("Exported %ld messages\n"), nExported);
}
else
{
    DWORD dwError = LlmGetLastError();
    _tprintf(_T("Unable to export conversation history, error 0x%08lX\n"), dwError);
}

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: csllmv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also

LlmGetMessageCount, LlmImportHistory