Wednesday, December 11, 2024

Delphi and AI [2]: Clipboard Monitor

While preparing for my Delphi and AI workshop, I decided to keep a log of all my interactions with AI helpers in a file for later analysis. Initially, I searched for an existing utility to log clipboard changes to a file (and I found one), but then I thought—why not ask the AI helpers to help me create one? After all, it’s not a big problem: set up a timer, check if the clipboard content changes, and log the content to a file. What could be simpler?

I posed the same question to all five engines:

"I want to create a Delphi application that would monitor clipboard content (on Windows) and append clipboard content to a log file each time the clipboard has changed (and has a text inside)."

Let's see how they performed!

Logs and code are available here.

[OpenAI]

TL;DR Almost working code, well written and explained.

OpenAI wrote a detailed article, which firstly explained what to drop on a form and how to set up event handlers and later provided the full source code for the unit. While it did not create a DFM file it did add some interesting ideas about hiding the application and changing the location of the log (in the main block of code the log goes into the EXE folder).

The implementation works by calling Windows API function AddClipboardFormatListener and listening for the WM_CLIPBOARDUPDATE message. The code checks whether the clipboard contains text but does not compare new content to the previous one. If you copy same string twice, it will be logged twice. 

All in all, the implementation was almost perfect, I only had to add two units-System.IOUtils, and Vcl.Clipbrd. Original code is in file OpenAI\Unit1-OpenAI.pas.

I also learned something from the AI-generated code (success!). I had absolutely no idea that TFile knows how to append text files:

    // Append the log text to the file 

    TFile.AppendAllText(LogFilePath, LogText, TEncoding.UTF8);

[Ollama]

TL;DR Bad.

First attemp with Ollama has failed miserably. It suggested to implement form's OnClipboardChanged event (there is no such thing) and contained constructs such as 

ClipText += Clipboard.GetText();

I tried again (new attempt is in log ollama-2.log) and got a somehow working solution based around JVCL component TJvClipboardMonitor, which the AI incorrectly called TJvClipMonitor. Still, I had to implement a bunch of changes to get a somewhat working implementation.

The code also overwrites the log file on each change, which makes it more or less useless. When I complained about that, Ollama invented a new form in TStringList.SaveToFile:

SaveToFile('clipboard_log.txt', sfAppend);
// Use the sfAppend parameter to append instead of overwrite

Maybe we'll get it in Delphi some day, but currently this does not work. 

[Gemini]

TL;DR Impressively good, worked out of the box!

I did not expect much from Gemini. When we were testing it on the workshop (about a month ago), it was working really badly, producing mostly code that looked like Delphi but was using .NET-like functions that had no connections with Delphi. Todays attempt, however, was a complete success. I did not have to change the code at all! It even had a reference to Clipbrd and System.IOUtils units.

Gemini has generated really clean and well-documented code based on TTimer. There was no DFM so I had to place components on the form manually, but that was not a big problem. The code quality more than overcames that issue. The program is writing into the Documents folder, uses TStreamWriter in 'append' mode to log data and compares current clipboard to the previous version before logging. (OK, that last comes free with the TTimer-based imeplementation.) It also logs initial clipboard content on startup, which you may consider a feature or a bug.

[Claude]

TL;DR Almost working code, did provide a DFM.

Claude did create a nice almost-working application (I had to add the Vcl.Clipbrd unit) and has even generated a DFM file! It was really easy to put the application together. It uses a different Windows API than the OpenAI solution -- SetClipboardViewer -- and then monitors the WM_DRAWCLIPBOARD message. Logging is done with an old-school approach -- AssignFile/Append/Writeln/CloseFile. Nothing wrong with that.

Interestingly, the AI has created a Start and Stop buttons to manage clipboard monitor functionality. Again, this can be a feature or a limitation, depending on your need.

[CoPilot]

TL;DR Almost working code, simple and effective

CoPilot has also opted for a TTimer-based implementation, combined with AssignFile-style logging. Simple, clean and almost working. I had to add Winapi.Windows unit to the code manually.

Recap

All in all, I was positively surprised with the generated code. Well, with almost all generated code. Ollama/Codellama combo almost didn't reach the "minimum usable" level. Other engines, however, were impressively useful.

No comments:

Post a Comment