TOpenCode (OpenCode Zen)
Source: source/classes/topencode.prg
TOpenCode talks to the OpenCode Zen chat API
(https://opencode.ai/zen/v1/chat/completions) with
Authorization: Bearer public. No API key. Free models
(hy3-free, mimo-v2.5-free,
deepseek-v4-flash-free, nemotron-3-ultra-free,
nemotron-3.5-lightning-free, laguna-s-2.1-free).
Uses hb_curl. Async methods need the Harbour MT VM
(build_new.bat <sample> hb32 mt).
Transport: hbcurl over a static libcurl
(Schannel) — no libcurl.dll to ship. Curl sources and
build scripts live in 3rdparty/ (outputs:
lib/libcurl_static*.{lib,a} for bcc32/bcc64/msvc32/
msvc64/gcc).
Architecture
Key DATA
| DATA | Default | Description |
|---|---|---|
cModel | "hy3-free" | Zen free model |
cUrl | Zen chat completions | Endpoint |
nTemperature | 0.7 | 0.0–2.0 |
aFallbackModels | other free models | Used on rate limit |
lRunning | .F. | Async in flight |
Methods
| Method | Description |
|---|---|
New( cModel ) | Own curl handle per instance (safe to run several in parallel). |
Send( cPrompt ) | Sync. Retries with cFallbackModel() on rate limit. |
SendWithSystem( cSys, cUser ) | Sync with a system prompt (agent role). |
SendAsync( cPrompt ) | Returns at once. Poll IsRunning(). |
SendWithSystemAsync( cSys, cUser ) | Async + system prompt. |
IsRunning() | .T. until the worker writes the result file. |
GetValue() | Assistant text from JSON; UTF-8 → ACP. |
Wait() / End() | Join the thread; cleanup curl. |
SetModel() / SetTemperature() | Change model / temperature. |
Async writes oc_<ms>_<seq>.json via a .part
file then rename, so three SendAsync() calls in the same tick
do not collide and IsRunning() never reads a half-written body.
Example
#include "FiveWin.ch"
function Main()
local o := TOpenCode():New( "mimo-v2.5-free" )
o:Send( "Say hello in one sentence." )
MsgInfo( o:GetValue() )
o:End()
return nil
Samples
| Sample | Description |
|---|---|
samples/ai/opencode1.prg | Three agents, each with its own TOpenCode, SendAsync + timer. Build hb32 mt. |
samples/ai/opencode2.prg | TWebView2 UI (WhatsApp layout): agents left, chat right, add/edit roles. Persists opencode2.json. Needs opencode2.html next to the exe. Build hb32 mt. |
cd samples\ai
build_new.bat opencode1 hb32 mt
build_new.bat opencode2 hb32 mt
:: run from this folder (libcurl.dll)
Notes
- No API key. OAuth /
auth.jsonon TOpenCode is unused (Zen rejectssk-keys). - Async requires
hbvmmt— passmttobuild_new.bat. - 32-bit samples need the x86
libcurl.dllbeside the exe (samples\ai). - Do not call the Zen API from inside
TWebView2:bOnBind; queue the work and run it from a timer (see opencode2).