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

flowchart LR A[TOpenCode] --> B[hb_curl] B --> C["opencode.ai/zen/v1/chat/completions"] A -->|SendAsync| D[hb_threadStart] D --> E[oc_*.json]

Key DATA

DATADefaultDescription
cModel"hy3-free"Zen free model
cUrlZen chat completionsEndpoint
nTemperature0.70.0–2.0
aFallbackModelsother free modelsUsed on rate limit
lRunning.F.Async in flight

Methods

MethodDescription
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

SampleDescription
samples/ai/opencode1.prgThree agents, each with its own TOpenCode, SendAsync + timer. Build hb32 mt.
samples/ai/opencode2.prgTWebView2 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

See Also