37 tok/s from Qwen3-Next-80B (48 GB) on a 32 GB laptop, 24 without drafts vs 2.8 for llama.cpp; reading 62 vs 16 tok/s. Faster than llama.cpp on all 13 models compared in Q4_K_M. LLMs bigger than your RAM, pure C, CPU or GPU.
C avx2ccpu-inferenceggufinference-enginelinuxllmlocal-llmmixture-of-expertsmoenvmequantizationqwen3simdspeculative-decodingvulkan
Release 2026-10-22 (3): Janas's classes for FreeBASIC - include/janas/janas_oop.bi: Janas.Model, Janas.Chat, their options, a reply's counters, a conversation kept by the program, a handler that receives a reply as it is written through its overridden methods, and an MCP server's tools; objects that close what they hold and cannot be copied; the replies through the classes the library's own in C, byte for byte (tests/fb_check on Qwen3-0.6B); the chat in FreeBASIC moved to examples/fb-chat, beside its version with classes
camauri6ff577762d97
diff --git a/ChangeLog.md b/ChangeLog.mdindex 1117f6f..12ddade 100644--- a/ChangeLog.md+++ b/ChangeLog.md@@ -2,6 +2,12 @@> Curated, user-facing summary of completed work, newest first.+## [2026-10-22] - Janas's classes for FreeBASIC++- **`include/janas/janas_oop.bi`: Janas-LLM and Janas-MCP with objects, for FreeBASIC** - `Janas.Model`, `Janas.Chat`, their options, a reply's counters, a conversation kept by the program, a handler that receives a reply as it is written through its overridden methods, and an MCP server's tools; objects that close what they hold at the end of their scope and cannot be copied, a Chat that knows when its Model closed; methods that return False and say why. `include/janas/janas.bi` takes `llm.bi` and `mcp.bi` in one include. `tests/test_fb`, in the suite where FreeBASIC is found, tries every class without a model; `tests/fb_check`, on a model, finds the classes' replies the library's own in C, byte for byte.+- **The chat in FreeBASIC moved from `tools/` to `examples/fb-chat/`**, beside its version with classes, `chat_oop.bas`; its counters' line no longer overflows past 99.9 tokens a second.+- `llm.bi`: its last note gives the parameter blocks' sizes as they are, 64, 80 and 192 bytes (it said 56, 72 and 176).+## [2026-10-22] - The module for Node.js in the Linux package- **The Linux package carries `janas.node`** and its types for TypeScript, `janas.d.ts`, in `node/`: a Node.js program opens a model with the package alone, without building Janas. The release workflow checks that the module, as packed, loads, where its runner has Node.js. The Windows package has no module yet.diff --git a/README.md b/README.mdindex c0a1ef2..7a9449d 100644--- a/README.md+++ b/README.md@@ -193,6 +193,7 @@ sudo pacman -S vulkan-headers glslang vulkan-tools # the same, Arch- [docs/chat.md](docs/chat.md) — `janas-chat`: its options and commands, the drafts, Janas's services, tools from MCP servers, a long prompt watched as it is read- [docs/library.md](docs/library.md) — using it from a program, in C, in FreeBASIC and BASIC MODERN, or in Node.js and TypeScript, and MCP servers from a program- [examples/node-chat](examples/node-chat/README.md) — a chat in TypeScript on the module for Node.js: the model inside the server's process, its replies streamed to the browser+- [examples/fb-chat](examples/fb-chat/README.md) — a chat in FreeBASIC, procedure by procedure and with Janas's classes- [docs/serving.md](docs/serving.md) — the model as tools for Claude Code, editors and other MCP clients (`janas-mcp`), and over HTTP, an API that follows OpenAI's with a chat page to try it from a browser (`janas-server`)- [docs/code-completion.md](docs/code-completion.md) — code filled in at an editor's cursor, in VSCodium and VS Code through `janas-server`: 1.5-2.2 s a proposal on a laptop, where a window slid a line was read again in 17 s- [docs/tools.md](docs/tools.md) — the converters, checkers and benchmarks built beside the programsdiff --git a/dist/README.md b/dist/README.mdindex 0af48b5..977664b 100644--- a/dist/README.md+++ b/dist/README.md@@ -52,7 +52,7 @@ Janas-LLM, the inference engine of Janas, runs large language models on ordinary| `jns_check` | what a `.jns` file says about itself: architecture, sizes, types, licence || `vkcheck` | whether this computer's GPU can run Janas's GPU backend, and why not: what the backend needs with the driver's answer, then three short tests on the GPU || `services/` | Janas's services, which bring data of the world to a model: flights, weather, maps, prices, earthquakes, Wikipedia, GitHub, git, this computer. `janas-chat` starts them by itself; any other MCP client can use them. See `services/README.md` |-| `lib/`, `include/` | `libjanas_llm` and `libjanas_mcp` with their C headers and FreeBASIC bindings, for programs of your own; the programs above do not need them |+| `lib/`, `include/` | `libjanas_llm` and `libjanas_mcp` with their C headers and FreeBASIC bindings (procedure by procedure, and as classes in `janas_oop.bi`), for programs of your own; the programs above do not need them || `node/` | `janas.node`, the library as a module for Node.js, and `janas.d.ts`, its types for TypeScript: a model opened inside a program of yours, its replies read as they are written (`docs/library.md`, "From Node.js"; tried with Node.js 20) |## Needsdiff --git a/docs/library.md b/docs/library.mdindex 3f35d7e..574ae0d 100644--- a/docs/library.md+++ b/docs/library.md@@ -53,16 +53,40 @@ doloop```-[`tools/chat.bas`](../tools/chat.bas) is a whole chat in a hundred lines, sampling and counters included:+[`examples/fb-chat/chat.bas`](../examples/fb-chat/chat.bas) is a whole chat in a hundred lines, sampling and counters included:```sh-fbc tools/chat.bas -i include -p bin/x86_64-linux -x janas-chat-fb+fbc examples/fb-chat/chat.bas -i include -p bin/x86_64-linux -x janas-chat-fbLD_LIBRARY_PATH=bin/x86_64-linux ./janas-chat-fb qwen3-next.jnsecho "Hello! Introduce yourself." | LD_LIBRARY_PATH=bin/x86_64-linux ./janas-chat-fb qwen3-next.jns```+[`include/janas/janas.bi`](../include/janas/janas.bi) takes the two libraries in one include: `llm.bi`, and `mcp.bi`, the MCP client's.+Two things to know. FreeBASIC does not tell names apart by their case, so the constant the C header calls `JANAS_LLM_ABI_VERSION` is `JANAS_LLM_ABI` there — it would otherwise collide with the function `janas_llm_abi_version()`. And its `line input` reads the terminal, not the standard input: to work in a pipe too, `chat.bas` asks `isatty` and reads a pipe through the `CONS` device.+### With objects, in FreeBASIC++[`include/janas/janas_oop.bi`](../include/janas/janas_oop.bi) builds classes on the two libraries, for FreeBASIC (BASIC MODERN keeps to `llm.bi`): `Janas.Model` and `Janas.Chat`, their options (`Janas.OpenOptions`, `Janas.ChatOptions`), a reply's counters (`Janas.ReplyStats`), a conversation kept by the program (`Janas.Conversation`), a handler that receives a reply as it is written (`Janas.ReplyHandler`, and `Janas.Printer` ready made), and an MCP server's tools (`Janas.McpServer`). The same conversation:++```basic+#include once "janas/janas_oop.bi"++dim as Janas.Model model = Janas.Model("qwen3-next.jns")+if model.IsOpen = false then print model.ErrorText : end 1+dim as Janas.Chat chat = Janas.Chat(model)+if chat.Send("Hello! Introduce yourself.") then+ dim as string piece+ while chat.NextPiece(piece)+ if chat.Reasoning = false then print piece;+ wend+end if+```++Or the reply whole, `print chat.Ask("Hello!")`, or through a handler: a type that extends `Janas.ReplyHandler` and overrides `OnPiece` (False stops the reply), `OnProgress` while a long prompt is read and `OnEnd` with the counters, which `chat.ReadReply(handler)` calls. The objects close what they hold when they go out of scope, and cannot be copied; a Chat lives on its Model, and closing the Model closes it too. Nothing raises an error: a method returns False, or an empty text, and the object says why in `ErrorCode` and `ErrorText`. The options are the library's own parameter blocks, so their fields have the C names, in any case: `options.Temperature = 0`, `options.max_reply = 400`. The bodies of the methods are in the same file: a program of one module includes it and is done; in a program of several, every module but one defines `JANAS_DECLARE_ONLY` before including it. [`examples/fb-chat/chat_oop.bas`](../examples/fb-chat/chat_oop.bas) is the chat of `chat.bas` with the classes.++What has been run, with FreeBASIC 1.10.1: `tests/test_fb`, among the tests `./build.sh test` runs when it finds the compiler (`FBC`, or `fbc` in the PATH): every class without a model, the fake MCP server's tools listed and called, a program of two modules, and a Model that does not compile when copied; and `tests/fb_check` on Qwen3-0.6B: the replies through the classes the same text, byte for byte, as the library's own in C, read piece by piece, whole, through a handler, after a reset and in a new chat.+## From Node.js`janas.node` is the library as a module for Node.js, made by `./build.sh` on Linux beside the programs (`bin/x86_64-linux/janas.node`), with its types for TypeScript beside it (`janas.d.ts`); the Linux package carries the two in `node/`. It is written in C against Node-API, version 8, whose functions it declares itself: building it needs nothing of Node's. It was tried with Node 20. Each model runs on a thread of its own, so JavaScript's thread never waits for it, and a reply is read as it is written:diff --git a/examples/fb-chat/README.md b/examples/fb-chat/README.mdnew file mode 100644index 0000000..ef0817a--- /dev/null+++ b/examples/fb-chat/README.md@@ -0,0 +1,20 @@+# A chat in FreeBASIC on Janas-LLM++The smallest chat on Janas-LLM, written twice:++- `chat.bas`, procedure by procedure, with `include/janas/llm.bi`: the C library's functions as they are, for FreeBASIC and for BASIC MODERN, the dialect Prabanta implements.+- `chat_oop.bas`, with the classes of `include/janas/janas_oop.bi`: a `Janas.Model`, its `Janas.Chat`, and a handler that extends `Janas.Printer` to print the reply as it is written and its counters at the end. Nothing is closed by hand: the objects close what they hold when the program ends.++Both read the user's lines from a terminal, or from a pipe.++## Building and running++Janas built first (`./build.sh` in the repository's root), which makes the shared libraries in `bin/x86_64-linux/`; then, from the root, with FreeBASIC 1.10:++```sh+fbc examples/fb-chat/chat_oop.bas -i include -p bin/x86_64-linux -x janas-chat-oop+LD_LIBRARY_PATH=bin/x86_64-linux ./janas-chat-oop models/jns/qwen3-4b-q4km.jns+echo "Ciao" | LD_LIBRARY_PATH=bin/x86_64-linux ./janas-chat-oop models/jns/qwen3-4b-q4km.jns+```++and the same for `chat.bas`. The classes are described in `docs/library.md`, "From FreeBASIC and BASIC MODERN".diff --git a/tools/chat.bas b/examples/fb-chat/chat.bassimilarity index 91%rename from tools/chat.basrename to examples/fb-chat/chat.basindex bf8de61..95e64df 100644--- a/tools/chat.bas+++ b/examples/fb-chat/chat.bas@@ -1,11 +1,12 @@'' SPDX-License-Identifier: GPL-3.0-or-later'' Copyright (C) 2026 Maurizio Cammalleri-'' chat.bas - the smallest chat on Janas-LLM, in FreeBASIC.+'' chat.bas - the smallest chat on Janas-LLM, in FreeBASIC, procedure by+'' procedure (llm.bi); chat_oop.bas beside it is the same with objects.'''' What a BASIC MODERN or FreeBASIC program needs to do: open a model, make a'' conversation, send a message, read the reply as it is written.''-'' fbc tools/chat.bas -i include -p bin/x86_64-linux -x janas-chat-fb+'' fbc examples/fb-chat/chat.bas -i include -p bin/x86_64-linux -x janas-chat-fb'' LD_LIBRARY_PATH=bin/x86_64-linux ./janas-chat-fb model.jns'' echo "Ciao" | LD_LIBRARY_PATH=bin/x86_64-linux ./janas-chat-fb model.jns''@@ -101,7 +102,7 @@ dodim as janas_llm_chat_stats stst.size = sizeof(st)if janas_llm_chat_stats(chat, @st) = JANAS_LLM_OK then- print using "[#### token in ###.## s, ##.# token/s, context #####/#####]"; _+ print using "[#### token in ###.## s, ###.# token/s, context #####/#####]"; _st.output_tokens; st.output_seconds; _st.output_tokens / st.output_seconds; _st.context_used; st.context_sizediff --git a/examples/fb-chat/chat_oop.bas b/examples/fb-chat/chat_oop.basnew file mode 100644index 0000000..016adef--- /dev/null+++ b/examples/fb-chat/chat_oop.bas@@ -0,0 +1,103 @@+'' SPDX-License-Identifier: GPL-3.0-or-later+'' Copyright (C) 2026 Maurizio Cammalleri+'' chat_oop.bas - the chat of chat.bas, with the classes of janas_oop.bi: a+'' Janas.Model, its Janas.Chat, and a handler that prints the reply as it is+'' written and its counters at the end. Nothing to close by hand: the objects+'' close what they hold when the program ends.+''+'' fbc examples/fb-chat/chat_oop.bas -i include -p bin/x86_64-linux -x janas-chat-oop+'' LD_LIBRARY_PATH=bin/x86_64-linux ./janas-chat-oop model.jns+'' echo "Ciao" | LD_LIBRARY_PATH=bin/x86_64-linux ./janas-chat-oop model.jns+''+#include once "janas/janas_oop.bi"++'' FreeBASIC reads the console, not the standard input: its LINE INPUT waits+'' for a terminal and never sees a pipe. So the terminal keeps LINE INPUT,+'' with the line editing it brings, and a pipe is read through the CONS+'' device (as in chat.bas).+declare function isatty cdecl alias "isatty" (byval fd as long) as long++type Console+ declare constructor()+ declare destructor()+ '' one line into msg; False at the end of the input+ declare function Ask(byref msg as string) as boolean+private:+ tty as boolean+ file as long+end type++constructor Console()+ tty = isatty(0) <> 0+ if tty then exit constructor+ file = freefile+ if open cons(for input as #file) <> 0 then file = 0+end constructor++destructor Console()+ if file then close #file+end destructor++function Console.Ask(byref msg as string) as boolean+ if tty then+ line input "> ", msg+ return true+ end if+ print "> ";+ if file = 0 orelse eof(file) then+ return false+ end if+ line input #file, msg+ print msg '' a pipe has no echo of its own+ return true+end function++'' the reply as it is written, then what it cost+type Terminal extends Janas.Printer+ declare sub OnEnd(byref st as const Janas.ReplyStats) override+end type++sub Terminal.OnEnd(byref st as const Janas.ReplyStats)+ print using "[#### token in ###.## s, ###.# token/s, context #####/#####]"; _+ st.output_tokens; st.output_seconds; st.TokensPerSecond(); _+ st.context_used; st.context_size+end sub++dim as string path = command(1)+if path = "" then+ print "usage: chat_oop <model.jns>"+ end 2+end if+if Janas.Model.AbiVersion() <> JANAS_LLM_ABI then+ print "the library speaks another ABI"+ end 1+end if++dim as Janas.Model model = Janas.Model(path)+if model.IsOpen = false then+ print model.ErrorText+ end 1+end if+print model.Description++dim as Janas.ChatOptions options+options.max_reply = 400 '' a model left to itself can fill the whole context+dim as Janas.Chat chat = Janas.Chat(model, options)+if chat.IsOpen = false then+ print chat.ErrorText+ end 1+end if++dim as Console cons_+dim as Terminal term+do+ dim as string msg+ if cons_.Ask(msg) = false then exit do+ if msg = "" orelse msg = "/quit" then exit do+ if chat.Send(msg) = false orelse chat.ReadReply(term) = false then+ print chat.ErrorText+ end if+loopdiff --git a/include/janas/janas.bi b/include/janas/janas.binew file mode 100644index 0000000..c24c6c5--- /dev/null+++ b/include/janas/janas.bi@@ -0,0 +1,15 @@+'' SPDX-License-Identifier: GPL-3.0-or-later+'' Copyright (C) 2026 Maurizio Cammalleri+'' janas.bi - Janas for FreeBASIC, and so for BASIC MODERN, the dialect+'' Prabanta implements: the two libraries in one include.+''+'' llm.bi declares Janas-LLM (a model and its conversation), mcp.bi Janas-MCP+'' (the tools of MCP servers), procedure by procedure as their C headers do.+'' janas_oop.bi builds classes on them, for FreeBASIC.+''+'' Build: fbc yours.bas -i <janas>/include -p <janas>/bin/x86_64-linux+'' Run: the libraries must be found (LD_LIBRARY_PATH, or install them)++#pragma once+#include once "janas/llm.bi"+#include once "janas/mcp.bi"diff --git a/include/janas/janas_oop.bi b/include/janas/janas_oop.binew file mode 100644index 0000000..efd31a7--- /dev/null+++ b/include/janas/janas_oop.bi@@ -0,0 +1,1631 @@+'' SPDX-License-Identifier: GPL-3.0-or-later+'' Copyright (C) 2026 Maurizio Cammalleri+'' janas_oop.bi - Janas for FreeBASIC, with objects: a model, its+'' conversation, the options of both, a reply's counters, a conversation kept+'' by the program, a handler that receives a reply as it is written, and an+'' MCP server's tools - classes in the namespace Janas, on the libraries that+'' janas.bi declares procedure by procedure.+''+'' #include once "janas/janas_oop.bi"+'' dim as Janas.Model model = Janas.Model("qwen3-4b.jns")+'' if model.IsOpen = false then print model.ErrorText : end 1+'' dim as Janas.Chat chat = Janas.Chat(model)+'' print chat.Ask("Hello! Introduce yourself.")+''+'' The objects close what they hold when they go out of scope, and cannot be+'' copied (a copy would close it twice). A Chat lives on its Model: closing+'' the Model closes the Chat too, which then answers that it is closed.+'' Nothing raises an error: a method that can fail returns False (or an empty+'' text), and the object says why in ErrorCode (JANAS_LLM_E..., as llm.bi+'' names them) and ErrorText, until its next call. The options are the+'' library's own parameter blocks, so their fields have the C names; FreeBASIC+'' does not mind their case: options.Temperature = 0.2.+''+'' The bodies of the methods are in this file too: a program of one module+'' includes it and is done. In a program of several modules, every module but+'' one defines JANAS_DECLARE_ONLY before including it, or the linker finds+'' the methods twice.+''+'' Build: fbc yours.bas -i <janas>/include -p <janas>/bin/x86_64-linux+'' Run: the libraries must be found (LD_LIBRARY_PATH, or install them)++#pragma once+#include once "janas/janas.bi"++namespace Janas++enum PowerMode explicit+ Auto = JANAS_LLM_MODE_AUTO '' max on mains power, eco on battery+ Eco = JANAS_LLM_MODE_ECO '' no GPU where it would only buy speed+ Max = JANAS_LLM_MODE_MAX '' whatever is fastest+end enum++enum AttentionMode explicit+ Auto = JANAS_LLM_ATTN_AUTO+ Exact = JANAS_LLM_ATTN_EXACT '' the query as it is, summed in floating point+ Fast = JANAS_LLM_ATTN_FAST '' sixteen-bit integers, an exact integer sum+end enum++'' reasoning before the reply, on the models that do it+enum ThinkMode explicit+ ModelDefault = -1+ Off = 0+ Think = 1+end enum++enum MessageRole explicit+ System = JANAS_LLM_ROLE_SYSTEM+ User = JANAS_LLM_ROLE_USER+ Assistant = JANAS_LLM_ROLE_ASSISTANT+ Tool = JANAS_LLM_ROLE_TOOL '' what a tool the model called answered+end enum++enum FinishReason explicit+ Running = 0+ Stop = JANAS_LLM_FINISH_STOP '' the model ended it+ Length = JANAS_LLM_FINISH_LENGTH '' max_reply tokens+ Context = JANAS_LLM_FINISH_CONTEXT '' the context is full+ Failure = JANAS_LLM_FINISH_ERROR+ Tools = JANAS_LLM_FINISH_TOOLS '' it ended after calling tools+end enum++enum ReplyStage explicit+ Idle = JANAS_LLM_STAGE_IDLE+ Reading = JANAS_LLM_STAGE_INPUT '' the prompt+ Writing = JANAS_LLM_STAGE_OUTPUT '' the reply+ Done = JANAS_LLM_STAGE_DONE+end enum++enum TokenPart explicit+ Answer = JANAS_LLM_PART_ANSWER+ Reasoning = JANAS_LLM_PART_REASONING+ InCall = JANAS_LLM_PART_CALL '' inside a tool call+ Mark = JANAS_LLM_PART_MARK '' reasoning or a call opened or closed+end enum++'' whether the model calls a tool+enum ToolMode explicit+ Auto = JANAS_LLM_TOOLS_AUTO+ None = JANAS_LLM_TOOLS_NONE+ Required = JANAS_LLM_TOOLS_REQUIRED+ Named = JANAS_LLM_TOOLS_FUNCTION '' the one named+end enum++'' the form of the answer+enum AnswerForm explicit+ Text = JANAS_LLM_FORMAT_TEXT+ Json = JANAS_LLM_FORMAT_JSON+ Schema = JANAS_LLM_FORMAT_SCHEMA '' valid against a JSON Schema+end enum++'' How a model opens: janas_llm_params, filled with the library's defaults,+'' and the files as strings. Set the fields by their C names+'' (options.n_ctx = 8192, options.Mode = Janas.PowerMode.Eco).+type OpenOptions extends janas_llm_params+ Mtp as string '' a multi-token prediction block's file; "": none+ FindMtp as boolean '' look for one beside the model (when Mtp is "")+ Draft as string '' a small model of the same family to guess ahead+ Gpu as boolean = true '' false: never give the GPU work+ declare constructor()+ '' memory for the expert cache, and left to other programs, in GiB+ declare property CacheGiB(byval gib as double)+ declare property ReserveGiB(byval gib as double)+end type++'' A conversation's parameters: janas_llm_chat_params with the library's+'' defaults (options.Temperature = 0, options.max_reply = 400,+'' options.Thinking = Janas.ThinkMode.Off).+type ChatOptions extends janas_llm_chat_params+ declare constructor()+end type++'' What a reply did: janas_llm_chat_stats, its fields by their C names+'' (output_tokens, cached_tokens...), and what follows from them.+type ReplyStats extends janas_llm_chat_stats+ declare constructor()+ declare const function TokensPerSecond() as double '' writing the reply+ declare const function PromptTokensPerSecond() as double '' reading the prompt+ declare const function Reason() as FinishReason+ declare const function FinishText() as string+end type++'' A conversation as a program keeps it, for Chat.Load: the messages in+'' order, only the first the system's, the last the user's or a tool's.+type Conversation+ declare sub Add(byval role as MessageRole, byref text as const string)+ declare sub Clear()+ declare const property Count() as long+ declare const function RoleOf(byval i as long) as MessageRole+ declare const function TextOf(byval i as long) as string+ roles(any) as long+ texts(any) as string+ n as long+end type++'' Receives a reply as it is written, from Chat.ReadReply: extend it and+'' override what you need. OnPiece returning False stops the reply there.+type ReplyHandler extends object+ declare virtual function OnPiece(byref text as const string, _+ byval reasoning as boolean) as boolean+ '' while a long prompt is read, after each block of it+ declare virtual sub OnProgress(byval readTokens as ulong, _+ byval totalTokens as ulong)+ declare virtual sub OnEnd(byref st as const ReplyStats)+ declare virtual destructor()+end type++'' A ReplyHandler that prints the answer as it comes, the reasoning left out+'' unless asked for, and nothing else.+type Printer extends ReplyHandler+ ShowReasoning as boolean+ declare function OnPiece(byref text as const string, _+ byval reasoning as boolean) as boolean override+end type++'' The handles a Model and its Chat share: each of them lets the other know+'' when it closes its own. For the classes of this file.+type Handles+ llm as janas_llm ptr+ chat as janas_llm_chat ptr+ refs as long+end type++'' A model file (.jns) opened: the weights stay on disk, the experts are read+'' from it and the most used kept in memory.+type Model+public:+ declare constructor()+ declare constructor(byref modelFile as const string)+ declare constructor(byref modelFile as const string, _+ byref settings as const OpenOptions)+ declare destructor()+ declare function Open(byref modelFile as const string) as boolean+ declare function Open(byref modelFile as const string, _+ byref settings as const OpenOptions) as boolean+ '' closes the model, and its chat+ declare sub Close()+ declare const property IsOpen() as boolean+ declare const property ErrorCode() as long+ declare const property ErrorText() as string+ declare const property Handle() as janas_llm ptr '' for the procedures of llm.bi+ declare const property Path() as string+ declare const property Name() as string '' from its metadata ("Qwen3 4B")+ declare const property Description() as string+ '' the system message Janas's programs give it+ declare const property DefaultSystem() as string+ declare const property Tuning() as string+ declare const property Reasons() as boolean+ declare property Mode(byval power as PowerMode)+ declare property Gpu(byval allowed as boolean)+ '' experts a token uses (Experts = 0: the model's own count again)+ declare const property Experts() as long+ declare property Experts(byval count_ as long)+ declare const property ExpertsMost() as long+ declare const property ExpertBits() as long+ declare const property Attention() as AttentionMode+ declare const property EmbedDim() as long '' 0: the model gives no embeddings+ declare function Embed(byref text as const string, vector() as single, _+ byval dims as long = 0) as boolean+ declare function Tokenize(byref text as const string, ids() as long, _+ byval special as boolean = false) as boolean+ declare function TokenText(byval id as long) as string+ declare function Memory(byref resident as ulongint, _+ byref cache as ulongint, byref kv as ulongint) _+ as boolean+ '' the expert cache filling from this machine's profile+ declare function Preload(byref loaded as longint, _+ byref total as longint) as boolean+ '' the NPU being made ready: True while it is+ declare function NpuPreparing(byref graphs as long, byref total as long, _+ byref seconds as double) as boolean+ declare const property Shared_() as Handles ptr '' for Chat+ declare static function AbiVersion() as long+ declare static function FindMtp(byref modelFile as const string) as string+ '' the memory a model would take, before opening it+ declare static function Estimate(byref modelFile as const string, _+ byref settings as const OpenOptions, _+ byref fixed as ulongint, _+ byref cacheAll as ulongint) as boolean+private:+ declare constructor(byref other as const Model) '' no copies+ declare operator let(byref other as const Model)+ declare function Fail(byval rc as long) as boolean+ declare function Done_() as boolean+ hs as Handles ptr+ lastCode as long+ lastText as string+ opened as string+end type++'' A model's conversation (one at a time per model). Send a message, then+'' read the reply: NextPiece piece by piece, Reply whole, or ReadReply with+'' a handler. Load gives it a whole conversation kept by the program.+type Chat+public:+ declare constructor(byref owner as Model)+ declare constructor(byref owner as Model, _+ byref settings as const ChatOptions)+ declare destructor()+ declare sub Close()+ declare const property IsOpen() as boolean+ declare const property ErrorCode() as long+ declare const property ErrorText() as string+ declare const property Handle() as janas_llm_chat ptr+ declare const property Options() as ChatOptions+ declare function SetOptions(byref settings as const ChatOptions) as boolean+ '' one parameter at a time, from the next reply on+ declare property Temperature(byval t as single)+ declare property MaxReply(byval limit as long)+ declare property Thinking(byval think as ThinkMode)+ '' the system message, before the first message (or after Reset)+ declare property System(byref text as const string)+ '' reads the system message and the tools before the first message: 1+ '' while more is left, 0 when read, negative on failure+ declare function Prepare(byref readTokens as long, _+ byref totalTokens as long) as long+ declare function Send(byref text as const string) as boolean+ declare function Load(byref conv as const Conversation) as boolean+ declare function Prompt(byref text as const string) as boolean+ declare function Infill(byref prefix as const string, _+ byref suffix as const string) as boolean+ declare function SendResults(results() as string) as boolean+ '' the reply: True with the next piece (empty while a long prompt is+ '' read), False when it is over (Finished) or failed (ErrorCode)+ declare function NextPiece(byref piece as string) as boolean+ declare const property Reasoning() as boolean '' the last piece was+ declare const property Finished() as boolean+ '' the rest of the reply, its answer: the reasoning left out+ declare function Reply() as string+ declare function ReadReply(byref handler as ReplyHandler) as boolean+ declare function Ask(byref text as const string) as string+ declare const function Stats() as ReplyStats+ declare function Reset() as boolean+ '' tools: OpenAI's JSON array; whether the model calls them; the calls+ '' of the reply and what they answered (SendResults, in their order)+ declare function Tools(byref json as const string) as boolean+ declare function ToolsAdd(byref json as const string) as boolean+ declare function ToolChoice(byval choice as ToolMode, _+ byref toolName as const string = "", _+ byval parallel as boolean = true) as boolean+ declare function Format(byval kind as AnswerForm, _+ byref schema as const string = "") as boolean+ declare const property Calls() as long+ declare function ToolCall(byval i as long, byref toolName as string, _+ byref args as string) as boolean+ declare function LogitBias(ids() as long, bias() as single) as boolean+ '' the tokens of the reply (options.logprobs = 1 for their+ '' log-probabilities, options.top_logprobs for the alternatives)+ declare const property Tokens() as long+ declare function Token(byval i as long, byref id as long, _+ byref part as TokenPart, byref logprob as single, _+ byref bytes as string) as boolean+ declare function TokenTop(byval i as long, ids() as long, _+ logprobs() as single, _+ byval most as long = 20) as boolean+ '' conversations kept computed besides the current one, and on disk+ declare function Keep(byval conversations as long, _+ byval maxBytes as ulongint = 0) as boolean+ declare function Kept(byref conversations as long, _+ byref usedBytes as ulongint) as boolean+ declare function KeepDisk(byval maxBytes as ulongint) as boolean+ declare function KeptDisk(byref files as long, _+ byref usedBytes as ulongint) as boolean+ '' a request on the side, the conversation put back as it was+ declare function Aside(byref systemText as const string, _+ byref text as const string, _+ byval maxTokens as long = 256) as string+ '' a conversation message by message, with the tool calls of the+ '' assistant's messages; Submit loads it as Load does+ declare function Begin() as boolean+ declare function Add(byval role as MessageRole, _+ byref text as const string) as boolean+ declare function AddCall(byref toolName as const string, _+ byref args as const string) as boolean+ declare function Submit() as boolean+ declare function Count() as long '' its tokens, nothing computed+private:+ declare constructor(byref other as const Chat) '' no copies+ declare operator let(byref other as const Chat)+ declare sub Make(byref owner as Model, byref settings as const ChatOptions)+ declare function Fail(byval rc as long) as boolean+ declare function Closed() as boolean+ declare function Done_() as boolean+ declare function Started(byval rc as long) as boolean+ hs as Handles ptr+ current as ChatOptions+ lastCode as long+ lastText as string+ lastReasoning as boolean+ over as boolean+end type++'' An MCP server's tools: a server started from a configuration file (by+'' default ~/.config/janas/mcp.json) or a command line, or reached at an+'' address. Its tools go to a Chat as Tools(server.Tools("prefix__")); a+'' call the model writes goes to CallTool without the prefix, and Result to+'' the Chat's SendResults. A tool runs with this program's rights: show the+'' user each call before it is made.+type McpServer+public:+ declare constructor()+ declare constructor(byref serverName as const string, _+ byref configFile as const string = "", _+ byval seconds as long = 0)+ declare destructor()+ declare function Open(byref serverName as const string, _+ byref configFile as const string = "", _+ byval seconds as long = 0) as boolean+ declare function OpenCommand(args() as string, _+ byref logName as const string = "", _+ byval seconds as long = 0) as boolean+ declare function OpenCommand(args() as string, env() as string, _+ byref logName as const string = "", _+ byval seconds as long = 0) as boolean+ declare function OpenUrl(byref url as const string, _+ byref serverName as const string = "", _+ byval seconds as long = 0) as boolean+ declare function OpenUrl(byref url as const string, headers() as string, _+ byref serverName as const string = "", _+ byval seconds as long = 0) as boolean+ declare sub Close()+ declare const property IsOpen() as boolean+ declare const property ErrorCode() as long+ declare const property ErrorText() as string+ declare const property Handle() as janas_mcp ptr '' for the procedures of mcp.bi+ declare property Info() as string+ declare property Instructions() as string+ declare const property ToolCount() as long+ declare property ToolNames() as string '' one per line+ declare function Tools(byref prefix as const string = "") as string+ '' 1 when a tool says it only reads, 0, -1 when it says nothing+ declare function ReadOnly(byref toolName as const string) as long+ declare function Refresh() as boolean+ '' True when the server answered: ResultFailed says whether the tool did+ declare function CallTool(byref toolName as const string, _+ byref args as const string = "", _+ byval seconds as long = 0) as boolean+ declare property Result() as string+ declare const property ResultFailed() as boolean+ declare property ResultUser() as string '' parts for the user alone+ declare property ResultJson() as string+ declare sub Cancel()+ declare static function AbiVersion() as long+ declare static function ConfigPath() as string+ declare static function ConfigServers(byref configFile as const string = "") _+ as string+private:+ declare constructor(byref other as const McpServer) '' no copies+ declare operator let(byref other as const McpServer)+ declare function Fail(byval rc as long) as boolean+ declare function Done_() as boolean+ declare function Text(byval what as long) as string+ srv as janas_mcp ptr+ lastCode as long+ lastText as string+end type++end namespace++#ifndef JANAS_DECLARE_ONLY++namespace Janas++'' what the library writes as text: into a buffer, its full length aside+type TextGetter as function cdecl(byval h as const any ptr, _+ byval buf as zstring ptr, _+ byval cap as long, _+ byval length as long ptr) as long++private function TakeText(byval getter as TextGetter, _+ byval h as const any ptr, byref rc as long) _+ as string+ dim as zstring * 1024 small+ dim as long n = 0+ rc = getter(h, @small, sizeof(small), @n)+ if rc = JANAS_LLM_OK andalso n < sizeof(small) then return small+ if (rc <> JANAS_LLM_OK andalso rc <> JANAS_LLM_ESMALL) orelse n <= 0 then+ return ""+ end if+ dim as zstring ptr big = callocate(n + 1)+ if big = 0 then+ rc = JANAS_LLM_ENOMEM+ return ""+ end if+ rc = getter(h, big, n + 1, @n)+ dim as string s+ if rc = JANAS_LLM_OK then s = *big+ deallocate(big)+ return s+end function++dim shared as zstring * 1 empty_++'' a string for the library: a pointer it may read len(s) bytes from+private function Z(byref s as const string) as zstring ptr+ if len(s) = 0 then return @empty_+ return cast(zstring ptr, strptr(s))+end function++'' the same, or NULL for an empty string (the library's "none")+private function ZN(byref s as const string) as zstring ptr+ if len(s) = 0 then return 0+ return cast(zstring ptr, strptr(s))+end function++'' the first element's address, or NULL for an empty array+private function First_(arr() as zstring ptr) as zstring ptr ptr+ if ubound(arr) < lbound(arr) then return 0+ return @arr(lbound(arr))+end function++private function Count_(arr() as string) as long+ if ubound(arr) < lbound(arr) then return 0+ return ubound(arr) - lbound(arr) + 1+end function++'' ---- OpenOptions, ChatOptions, ReplyStats++constructor OpenOptions()+ janas_llm_params_default(cast(janas_llm_params ptr, @this))+end constructor++property OpenOptions.CacheGiB(byval gib as double)+ cache_bytes = culngint(gib * 1073741824.0)+end property++property OpenOptions.ReserveGiB(byval gib as double)+ reserve_bytes = culngint(gib * 1073741824.0)+end property++constructor ChatOptions()+ janas_llm_chat_params_default(cast(janas_llm_chat_params ptr, @this))+end constructor++constructor ReplyStats()+ size = sizeof(janas_llm_chat_stats)+end constructor++function ReplyStats.TokensPerSecond() as double+ if output_seconds <= 0 then return 0+ return output_tokens / output_seconds+end function++function ReplyStats.PromptTokensPerSecond() as double+ if input_seconds <= 0 then return 0+ return input_tokens / input_seconds+end function++function ReplyStats.Reason() as FinishReason+ return cast(FinishReason, finish)+end function++function ReplyStats.FinishText() as string+ select case finish+ case JANAS_LLM_FINISH_STOP+ return "stop"+ case JANAS_LLM_FINISH_LENGTH+ return "length"+ case JANAS_LLM_FINISH_CONTEXT+ return "context"+ case JANAS_LLM_FINISH_ERROR+ return "error"+ case JANAS_LLM_FINISH_TOOLS+ return "tools"+ end select+ return ""+end function++'' ---- Conversation++sub Conversation.Add(byval role as MessageRole, byref text as const string)+ redim preserve roles(0 to n)+ redim preserve texts(0 to n)+ roles(n) = role+ texts(n) = text+ n += 1+end sub++sub Conversation.Clear()+ erase roles, texts+ n = 0+end sub++property Conversation.Count() as long+ return n+end property++function Conversation.RoleOf(byval i as long) as MessageRole+ if i < 0 orelse i >= n then return MessageRole.User+ return cast(MessageRole, roles(i))+end function++function Conversation.TextOf(byval i as long) as string+ if i < 0 orelse i >= n then return ""+ return texts(i)+end function++'' ---- ReplyHandler, Printer++function ReplyHandler.OnPiece(byref text as const string, _+ byval reasoning as boolean) as boolean+ return true+end function++sub ReplyHandler.OnProgress(byval readTokens as ulong, _+ byval totalTokens as ulong)+end sub++sub ReplyHandler.OnEnd(byref st as const ReplyStats)+end sub++destructor ReplyHandler()+end destructor++function Printer.OnPiece(byref text as const string, _+ byval reasoning as boolean) as boolean+ if reasoning = false orelse ShowReasoning then print text;+ return true+end function++'' ---- the handles a Model and its Chat share++private function HandlesNew() as Handles ptr+ dim as Handles ptr h = callocate(sizeof(Handles))+ if h then h->refs = 1+ return h+end function++private sub HandlesDrop(byref h as Handles ptr)+ if h = 0 then exit sub+ h->refs -= 1+ if h->refs = 0 then deallocate(h)+ h = 0+end sub++'' ---- Model++constructor Model()+end constructor++constructor Model(byref modelFile as const string)+ this.Open(modelFile)+end constructor++constructor Model(byref modelFile as const string, _+ byref settings as const OpenOptions)+ this.Open(modelFile, settings)+end constructor++destructor Model()+ this.Close()+end destructor++function Model.Fail(byval rc as long) as boolean+ lastCode = rc+ lastText = *janas_llm_last_error()+ return false+end function++function Model.Done_() as boolean+ lastCode = JANAS_LLM_OK+ lastText = ""+ return true+end function++function Model.Open(byref modelFile as const string) as boolean+ dim as OpenOptions settings+ return this.Open(modelFile, settings)+end function++function Model.Open(byref modelFile as const string, _+ byref settings as const OpenOptions) as boolean+ this.Close()+ dim as janas_llm_params p = *cast(const janas_llm_params ptr, @settings)+ dim as zstring * 4096 found+ if len(settings.Mtp) then+ p.mtp_path = strptr(settings.Mtp)+ elseif settings.FindMtp andalso _+ janas_llm_find_mtp(Z(modelFile), @found, sizeof(found)) = _+ JANAS_LLM_OK then+ p.mtp_path = @found+ end if+ if len(settings.Draft) then p.draft_path = strptr(settings.Draft)+ dim as janas_llm ptr llm+ dim as long rc = janas_llm_open(Z(modelFile), @p, @llm)+ if rc <> JANAS_LLM_OK then return Fail(rc)+ hs = HandlesNew()+ if hs = 0 then+ janas_llm_close(llm)+ lastCode = JANAS_LLM_ENOMEM+ lastText = "janas: out of memory"+ return false+ end if+ hs->llm = llm+ if settings.Gpu = false then janas_llm_set_gpu(llm, 0)+ opened = modelFile+ return Done_()+end function++sub Model.Close()+ if hs = 0 then exit sub+ '' the library destroys the chat with the model: the Chat learns it+ if hs->llm then janas_llm_close(hs->llm)+ hs->llm = 0+ hs->chat = 0+ HandlesDrop(hs)+ opened = ""+end sub++property Model.IsOpen() as boolean+ return hs <> 0 andalso hs->llm <> 0+end property++property Model.ErrorCode() as long+ return lastCode+end property++property Model.ErrorText() as string+ return lastText+end property++property Model.Handle() as janas_llm ptr+ if hs = 0 then return 0+ return hs->llm+end property++property Model.Path() as string+ return opened+end property++property Model.Shared_() as Handles ptr+ return hs+end property++property Model.Name() as string+ if this.IsOpen = false then return ""+ dim as long rc+ return TakeText(cast(TextGetter, @janas_llm_name), hs->llm, rc)+end property++property Model.Description() as string+ if this.IsOpen = false then return ""+ dim as long rc+ return TakeText(cast(TextGetter, @janas_llm_describe), hs->llm, rc)+end property++property Model.DefaultSystem() as string+ if this.IsOpen = false then return ""+ dim as long rc+ return TakeText(cast(TextGetter, @janas_llm_default_system), hs->llm, rc)+end property++property Model.Tuning() as string+ if this.IsOpen = false then return ""+ dim as long rc+ return TakeText(cast(TextGetter, @janas_llm_tuning), hs->llm, rc)+end property++property Model.Reasons() as boolean+ return this.IsOpen andalso janas_llm_reasons(hs->llm) <> 0+end property++property Model.Mode(byval power as PowerMode)+ if this.IsOpen then janas_llm_set_mode(hs->llm, power)+end property++property Model.Gpu(byval allowed as boolean)+ if this.IsOpen then janas_llm_set_gpu(hs->llm, iif(allowed, 1, 0))+end property++property Model.Experts() as long+ dim as long used = 0, most = 0+ if this.IsOpen then janas_llm_experts(hs->llm, @used, @most)+ return used+end property++property Model.Experts(byval count_ as long)+ if this.IsOpen then janas_llm_set_experts(hs->llm, count_)+end property++property Model.ExpertsMost() as long+ dim as long used = 0, most = 0+ if this.IsOpen then janas_llm_experts(hs->llm, @used, @most)+ return most+end property++property Model.ExpertBits() as long+ if this.IsOpen = false then return 0+ return janas_llm_expert_bits(hs->llm)+end property++property Model.Attention() as AttentionMode+ if this.IsOpen = false then return AttentionMode.Auto+ return cast(AttentionMode, janas_llm_attn_scores(hs->llm))+end property++property Model.EmbedDim() as long+ if this.IsOpen = false then return 0+ return janas_llm_embed_dim(hs->llm)+end property++function Model.Embed(byref text as const string, vector() as single, _+ byval dims as long = 0) as boolean+ erase vector+ if this.IsOpen = false then return Fail(JANAS_LLM_EINVAL)+ dim as long full = janas_llm_embed_dim(hs->llm)+ if full <= 0 then+ lastCode = JANAS_LLM_EMODEL+ lastText = "janas: the model gives no embeddings"+ return false+ end if+ redim vector(0 to full - 1)+ dim as long n = 0, tokens = 0+ dim as long rc = janas_llm_embed(hs->llm, Z(text), len(text), dims, _+ @vector(0), @n, @tokens)+ if rc <> JANAS_LLM_OK then+ erase vector+ return Fail(rc)+ end if+ if n > 0 andalso n < full then redim preserve vector(0 to n - 1)+ return Done_()+end function++function Model.Tokenize(byref text as const string, ids() as long, _+ byval special as boolean = false) as boolean+ erase ids+ if this.IsOpen = false then return Fail(JANAS_LLM_EINVAL)+ dim as long cap = len(text) + 16, n = 0+ redim ids(0 to cap - 1)+ dim as long rc = janas_llm_tokenize(hs->llm, Z(text), len(text), _+ iif(special, 1, 0), @ids(0), cap, @n)+ if rc = JANAS_LLM_ESMALL andalso n > cap then+ redim ids(0 to n - 1)+ rc = janas_llm_tokenize(hs->llm, Z(text), len(text), _+ iif(special, 1, 0), @ids(0), n, @n)+ end if+ if rc <> JANAS_LLM_OK then+ erase ids+ return Fail(rc)+ end if+ if n = 0 then erase ids else redim preserve ids(0 to n - 1)+ return Done_()+end function++function Model.TokenText(byval id as long) as string+ if this.IsOpen = false then return ""+ dim as zstring * 256 buf+ dim as long n = 0+ if janas_llm_token_text(hs->llm, id, @buf, sizeof(buf), @n) <> _+ JANAS_LLM_OK then return ""+ return left(buf, n)+end function++function Model.Memory(byref resident as ulongint, byref cache as ulongint, _+ byref kv as ulongint) as boolean+ resident = 0 : cache = 0 : kv = 0+ if this.IsOpen = false then return Fail(JANAS_LLM_EINVAL)+ dim as long rc = janas_llm_memory(hs->llm, @resident, @cache, @kv)+ if rc <> JANAS_LLM_OK then return Fail(rc)+ return Done_()+end function++function Model.Preload(byref loaded as longint, _+ byref total as longint) as boolean+ loaded = 0 : total = 0+ if this.IsOpen = false then return Fail(JANAS_LLM_EINVAL)+ dim as long rc = janas_llm_preload(hs->llm, @loaded, @total)+ if rc <> JANAS_LLM_OK then return Fail(rc)+ return Done_()+end function++function Model.NpuPreparing(byref graphs as long, byref total as long, _+ byref seconds as double) as boolean+ graphs = 0 : total = 0 : seconds = 0+ if this.IsOpen = false then return false+ return janas_llm_npu_preparing(hs->llm, @graphs, @total, @seconds) = 1+end function++function Model.AbiVersion() as long+ return janas_llm_abi_version()+end function++function Model.FindMtp(byref modelFile as const string) as string+ dim as zstring * 4096 found+ if janas_llm_find_mtp(Z(modelFile), @found, sizeof(found)) <> _+ JANAS_LLM_OK then return ""+ return found+end function++function Model.Estimate(byref modelFile as const string, _+ byref settings as const OpenOptions, _+ byref fixed as ulongint, _+ byref cacheAll as ulongint) as boolean+ fixed = 0 : cacheAll = 0+ dim as janas_llm_params p = *cast(const janas_llm_params ptr, @settings)+ if len(settings.Mtp) then p.mtp_path = strptr(settings.Mtp)+ return janas_llm_estimate(Z(modelFile), @p, @fixed, @cacheAll) = _+ JANAS_LLM_OK+end function++'' ---- Chat++constructor Chat(byref owner as Model)+ dim as ChatOptions settings+ Make(owner, settings)+end constructor++constructor Chat(byref owner as Model, byref settings as const ChatOptions)+ Make(owner, settings)+end constructor++sub Chat.Make(byref owner as Model, byref settings as const ChatOptions)+ current = settings+ over = true+ dim as Handles ptr h = owner.Shared_+ if h = 0 orelse h->llm = 0 then+ lastCode = JANAS_LLM_EINVAL+ lastText = "janas: the model is not open"+ exit sub+ end if+ dim as janas_llm_chat ptr c+ dim as long rc = janas_llm_chat_create(h->llm, _+ cast(const janas_llm_chat_params ptr, @current), @c)+ if rc <> JANAS_LLM_OK then+ Fail(rc)+ exit sub+ end if+ h->chat = c+ h->refs += 1+ hs = h+ Done_()+end sub++destructor Chat()+ this.Close()+end destructor++sub Chat.Close()+ if hs = 0 then exit sub+ '' a model closed first has destroyed it already+ if hs->chat then janas_llm_chat_destroy(hs->chat)+ hs->chat = 0+ HandlesDrop(hs)+end sub++function Chat.Fail(byval rc as long) as boolean+ lastCode = rc+ lastText = *janas_llm_last_error()+ return false+end function++function Chat.Done_() as boolean+ lastCode = JANAS_LLM_OK+ lastText = ""+ return true+end function++'' whether the chat is gone (never opened, closed, or its model closed)+function Chat.Closed() as boolean+ if hs <> 0 andalso hs->chat <> 0 then return false+ lastCode = JANAS_LLM_EINVAL+ lastText = "janas: the chat is closed"+ return true+end function++'' a reply asked for: it follows through NextPiece+function Chat.Started(byval rc as long) as boolean+ lastReasoning = false+ over = rc <> JANAS_LLM_OK+ if rc <> JANAS_LLM_OK then return Fail(rc)+ return Done_()+end function++property Chat.IsOpen() as boolean+ return hs <> 0 andalso hs->chat <> 0+end property++property Chat.ErrorCode() as long+ return lastCode+end property++property Chat.ErrorText() as string+ return lastText+end property++property Chat.Handle() as janas_llm_chat ptr+ if hs = 0 then return 0+ return hs->chat+end property++property Chat.Options() as ChatOptions+ return current+end property++function Chat.SetOptions(byref settings as const ChatOptions) as boolean+ if Closed() then return false+ current = settings+ dim as long rc = janas_llm_chat_set_params(hs->chat, _+ cast(const janas_llm_chat_params ptr, @current))+ if rc <> JANAS_LLM_OK then return Fail(rc)+ return Done_()+end function++property Chat.Temperature(byval t as single)+ dim as ChatOptions settings = current+ settings.temperature = t+ SetOptions(settings)+end property++property Chat.MaxReply(byval limit as long)+ dim as ChatOptions settings = current+ settings.max_reply = limit+ SetOptions(settings)+end property++property Chat.Thinking(byval think as ThinkMode)+ dim as ChatOptions settings = current+ settings.thinking = think+ SetOptions(settings)+end property++property Chat.System(byref text as const string)+ if Closed() then exit property+ dim as long rc = janas_llm_chat_system(hs->chat, Z(text), len(text))+ if rc <> JANAS_LLM_OK then Fail(rc) else Done_()+end property++function Chat.Prepare(byref readTokens as long, _+ byref totalTokens as long) as long+ readTokens = 0 : totalTokens = 0+ if Closed() then return lastCode+ dim as long rc = janas_llm_chat_prepare(hs->chat, @readTokens, _+ @totalTokens)+ if rc < 0 then Fail(rc) else Done_()+ return rc+end function++function Chat.Send(byref text as const string) as boolean+ if Closed() then return false+ return Started(janas_llm_chat_send(hs->chat, Z(text), len(text)))+end function++function Chat.Load(byref conv as const Conversation) as boolean+ if Closed() then return false+ dim as long n = conv.n+ if n <= 0 then+ lastCode = JANAS_LLM_EINVAL+ lastText = "janas: an empty conversation"+ return false+ end if+ redim as long roles(0 to n - 1), lens(0 to n - 1)+ redim as zstring ptr texts(0 to n - 1)+ for i as long = 0 to n - 1+ roles(i) = conv.roles(i)+ texts(i) = Z(conv.texts(i))+ lens(i) = len(conv.texts(i))+ next+ return Started(janas_llm_chat_load(hs->chat, n, @roles(0), @texts(0), _+ @lens(0)))+end function++function Chat.Prompt(byref text as const string) as boolean+ if Closed() then return false+ return Started(janas_llm_chat_prompt(hs->chat, Z(text), len(text)))+end function++function Chat.Infill(byref prefix as const string, _+ byref suffix as const string) as boolean+ if Closed() then return false+ return Started(janas_llm_chat_infill(hs->chat, Z(prefix), len(prefix), _+ Z(suffix), len(suffix)))+end function++function Chat.SendResults(results() as string) as boolean+ if Closed() then return false+ dim as long n = Count_(results())+ if n = 0 then return Started(janas_llm_chat_send_results(hs->chat, 0, _+ 0, 0))+ redim as zstring ptr texts(0 to n - 1)+ redim as long lens(0 to n - 1)+ dim as long first = lbound(results)+ for i as long = 0 to n - 1+ texts(i) = Z(results(first + i))+ lens(i) = len(results(first + i))+ next+ return Started(janas_llm_chat_send_results(hs->chat, n, @texts(0), _+ @lens(0)))+end function++function Chat.NextPiece(byref piece as string) as boolean+ piece = ""+ if over then return false+ if Closed() then+ over = true+ return false+ end if+ dim as zstring * 4097 buf+ dim as long n = 0+ dim as long rc = janas_llm_chat_next(hs->chat, @buf, sizeof(buf) - 1, @n)+ if rc = JANAS_LLM_DONE then+ over = true+ Done_()+ return false+ end if+ if rc <> JANAS_LLM_OK then+ over = true+ return Fail(rc)+ end if+ buf[n] = 0+ piece = buf+ lastReasoning = janas_llm_chat_thinking(hs->chat) <> 0+ return true+end function++property Chat.Reasoning() as boolean+ return lastReasoning+end property++property Chat.Finished() as boolean+ return over+end property++function Chat.Reply() as string+ dim as string piece, answer+ while NextPiece(piece)+ if lastReasoning = false then answer &= piece+ wend+ return answer+end function++function Chat.ReadReply(byref handler as ReplyHandler) as boolean+ dim as string piece+ while NextPiece(piece)+ if len(piece) then+ if handler.OnPiece(piece, lastReasoning) = false then return true+ else+ dim as ReplyStats st = Stats()+ if st.stage = JANAS_LLM_STAGE_INPUT then+ handler.OnProgress(st.input_done, st.input_tokens)+ end if+ end if+ wend+ if lastCode <> JANAS_LLM_OK then return false+ handler.OnEnd(Stats())+ return true+end function++function Chat.Ask(byref text as const string) as string+ if Send(text) = false then return ""+ return Reply()+end function++function Chat.Stats() as ReplyStats+ dim as ReplyStats st+ if hs <> 0 andalso hs->chat <> 0 then+ janas_llm_chat_stats(hs->chat, cast(janas_llm_chat_stats ptr, @st))+ end if+ return st+end function++function Chat.Reset() as boolean+ if Closed() then return false+ over = true+ dim as long rc = janas_llm_chat_reset(hs->chat)+ if rc <> JANAS_LLM_OK then return Fail(rc)+ return Done_()+end function++function Chat.Tools(byref json as const string) as boolean+ if Closed() then return false+ dim as long rc = janas_llm_chat_tools(hs->chat, _+ ZN(json), len(json))+ if rc <> JANAS_LLM_OK then return Fail(rc)+ return Done_()+end function++function Chat.ToolsAdd(byref json as const string) as boolean+ if Closed() then return false+ dim as long rc = janas_llm_chat_tools_add(hs->chat, _+ ZN(json), len(json))+ if rc <> JANAS_LLM_OK then return Fail(rc)+ return Done_()+end function++function Chat.ToolChoice(byval choice as ToolMode, _+ byref toolName as const string = "", _+ byval parallel as boolean = true) as boolean+ if Closed() then return false+ dim as long rc = janas_llm_chat_tool_choice(hs->chat, choice, _+ Z(toolName), len(toolName), iif(parallel, 1, 0))+ if rc <> JANAS_LLM_OK then return Fail(rc)+ return Done_()+end function++function Chat.Format(byval kind as AnswerForm, _+ byref schema as const string = "") as boolean+ if Closed() then return false+ dim as long rc = janas_llm_chat_format(hs->chat, kind, Z(schema), _+ len(schema))+ if rc <> JANAS_LLM_OK then return Fail(rc)+ return Done_()+end function++property Chat.Calls() as long+ if hs = 0 orelse hs->chat = 0 then return 0+ return janas_llm_chat_calls(hs->chat)+end property++function Chat.ToolCall(byval i as long, byref toolName as string, _+ byref args as string) as boolean+ toolName = "" : args = ""+ if Closed() then return false+ dim as zstring * 256 nm+ dim as long nlen = 0, alen = 0+ dim as long rc = janas_llm_chat_call(hs->chat, i, @nm, sizeof(nm), _+ @nlen, 0, 0, @alen)+ if rc <> JANAS_LLM_OK andalso rc <> JANAS_LLM_ESMALL then return Fail(rc)+ dim as zstring ptr a = callocate(alen + 1)+ dim as zstring ptr nb = callocate(nlen + 1)+ if a = 0 orelse nb = 0 then+ deallocate(a)+ deallocate(nb)+ lastCode = JANAS_LLM_ENOMEM+ lastText = "janas: out of memory"+ return false+ end if+ rc = janas_llm_chat_call(hs->chat, i, nb, nlen + 1, @nlen, a, alen + 1, _+ @alen)+ if rc = JANAS_LLM_OK then+ toolName = *nb+ args = *a+ end if+ deallocate(a)+ deallocate(nb)+ if rc <> JANAS_LLM_OK then return Fail(rc)+ return Done_()+end function++function Chat.LogitBias(ids() as long, bias() as single) as boolean+ if Closed() then return false+ dim as long n = ubound(ids) - lbound(ids) + 1+ if n <= 0 then+ dim as long rc = janas_llm_chat_logit_bias(hs->chat, 0, 0, 0)+ if rc <> JANAS_LLM_OK then return Fail(rc)+ return Done_()+ end if+ if ubound(bias) - lbound(bias) + 1 <> n then+ lastCode = JANAS_LLM_EINVAL+ lastText = "janas: as many biases as tokens"+ return false+ end if+ dim as long rc = janas_llm_chat_logit_bias(hs->chat, n, _+ @ids(lbound(ids)), @bias(lbound(bias)))+ if rc <> JANAS_LLM_OK then return Fail(rc)+ return Done_()+end function++property Chat.Tokens() as long+ if hs = 0 orelse hs->chat = 0 then return 0+ return janas_llm_chat_tokens(hs->chat)+end property++function Chat.Token(byval i as long, byref id as long, _+ byref part as TokenPart, byref logprob as single, _+ byref bytes as string) as boolean+ id = 0 : part = TokenPart.Answer : logprob = 0 : bytes = ""+ if Closed() then return false+ dim as zstring * 257 buf+ dim as long n = 0, p = 0+ dim as long rc = janas_llm_chat_token(hs->chat, i, @id, @p, @logprob, _+ @buf, sizeof(buf) - 1, @n)+ if rc <> JANAS_LLM_OK then return Fail(rc)+ part = cast(TokenPart, p)+ bytes = left(buf, n)+ return Done_()+end function++function Chat.TokenTop(byval i as long, ids() as long, _+ logprobs() as single, _+ byval most as long = 20) as boolean+ erase ids, logprobs+ if Closed() then return false+ if most < 1 then most = 1+ redim ids(0 to most - 1)+ redim logprobs(0 to most - 1)+ dim as long n = 0+ dim as long rc = janas_llm_chat_token_top(hs->chat, i, most, @ids(0), _+ @logprobs(0), @n)+ if rc <> JANAS_LLM_OK then+ erase ids, logprobs+ return Fail(rc)+ end if+ if n <= 0 then+ erase ids, logprobs+ else+ redim preserve ids(0 to n - 1)+ redim preserve logprobs(0 to n - 1)+ end if+ return Done_()+end function++function Chat.Keep(byval conversations as long, _+ byval maxBytes as ulongint = 0) as boolean+ if Closed() then return false+ dim as long rc = janas_llm_chat_keep(hs->chat, conversations, maxBytes)+ if rc <> JANAS_LLM_OK then return Fail(rc)+ return Done_()+end function++function Chat.Kept(byref conversations as long, _+ byref usedBytes as ulongint) as boolean+ conversations = 0 : usedBytes = 0+ if Closed() then return false+ dim as long rc = janas_llm_chat_kept(hs->chat, @conversations, @usedBytes)+ if rc <> JANAS_LLM_OK then return Fail(rc)+ return Done_()+end function++function Chat.KeepDisk(byval maxBytes as ulongint) as boolean+ if Closed() then return false+ dim as long rc = janas_llm_chat_keep_disk(hs->chat, maxBytes)+ if rc <> JANAS_LLM_OK then return Fail(rc)+ return Done_()+end function++function Chat.KeptDisk(byref files as long, _+ byref usedBytes as ulongint) as boolean+ files = 0 : usedBytes = 0+ if Closed() then return false+ dim as long rc = janas_llm_chat_kept_disk(hs->chat, @files, @usedBytes)+ if rc <> JANAS_LLM_OK then return Fail(rc)+ return Done_()+end function++function Chat.Aside(byref systemText as const string, _+ byref text as const string, _+ byval maxTokens as long = 256) as string+ if Closed() then return ""+ '' room for the whole answer at once: asking again would compute it again+ dim as long cap = maxTokens * 32 + 256, n = 0+ dim as zstring ptr buf = callocate(cap)+ if buf = 0 then+ lastCode = JANAS_LLM_ENOMEM+ lastText = "janas: out of memory"+ return ""+ end if+ dim as long rc = janas_llm_chat_aside(hs->chat, _+ ZN(systemText), _+ len(systemText), Z(text), len(text), maxTokens, _+ buf, cap, @n)+ if rc = JANAS_LLM_ESMALL andalso n >= cap then+ deallocate(buf)+ cap = n + 1+ buf = callocate(cap)+ if buf = 0 then+ lastCode = JANAS_LLM_ENOMEM+ lastText = "janas: out of memory"+ return ""+ end if+ rc = janas_llm_chat_aside(hs->chat, _+ ZN(systemText), len(systemText), _+ Z(text), len(text), maxTokens, buf, cap, @n)+ end if+ dim as string answer+ if rc = JANAS_LLM_OK then answer = *buf+ deallocate(buf)+ if rc <> JANAS_LLM_OK then+ Fail(rc)+ return ""+ end if+ Done_()+ return answer+end function++function Chat.Begin() as boolean+ if Closed() then return false+ dim as long rc = janas_llm_chat_begin(hs->chat)+ if rc <> JANAS_LLM_OK then return Fail(rc)+ return Done_()+end function++function Chat.Add(byval role as MessageRole, byref text as const string) _+ as boolean+ if Closed() then return false+ dim as long rc = janas_llm_chat_add(hs->chat, role, Z(text), len(text))+ if rc <> JANAS_LLM_OK then return Fail(rc)+ return Done_()+end function++function Chat.AddCall(byref toolName as const string, _+ byref args as const string) as boolean+ if Closed() then return false+ dim as long rc = janas_llm_chat_add_call(hs->chat, Z(toolName), _+ len(toolName), Z(args), len(args))+ if rc <> JANAS_LLM_OK then return Fail(rc)+ return Done_()+end function++function Chat.Submit() as boolean+ if Closed() then return false+ return Started(janas_llm_chat_run(hs->chat))+end function++function Chat.Count() as long+ if Closed() then return 0+ dim as long n = 0+ dim as long rc = janas_llm_chat_count(hs->chat, @n)+ if rc <> JANAS_LLM_OK then+ Fail(rc)+ return 0+ end if+ Done_()+ return n+end function++'' ---- McpServer++constructor McpServer()+end constructor++constructor McpServer(byref serverName as const string, _+ byref configFile as const string = "", _+ byval seconds as long = 0)+ this.Open(serverName, configFile, seconds)+end constructor++destructor McpServer()+ this.Close()+end destructor++function McpServer.Fail(byval rc as long) as boolean+ lastCode = rc+ lastText = *janas_mcp_last_error()+ return false+end function++function McpServer.Done_() as boolean+ lastCode = JANAS_MCP_OK+ lastText = ""+ return true+end function++function McpServer.Open(byref serverName as const string, _+ byref configFile as const string = "", _+ byval seconds as long = 0) as boolean+ this.Close()+ dim as long rc = janas_mcp_open(ZN(configFile), _+ Z(serverName), seconds, @srv)+ if rc <> JANAS_MCP_OK then+ srv = 0+ return Fail(rc)+ end if+ return Done_()+end function++function McpServer.OpenCommand(args() as string, _+ byref logName as const string = "", _+ byval seconds as long = 0) as boolean+ dim as string noEnv()+ return OpenCommand(args(), noEnv(), logName, seconds)+end function++function McpServer.OpenCommand(args() as string, env() as string, _+ byref logName as const string = "", _+ byval seconds as long = 0) as boolean+ this.Close()+ dim as long argc = Count_(args()), nenv = Count_(env())+ if argc = 0 then+ lastCode = JANAS_MCP_EINVAL+ lastText = "janas: no command"+ return false+ end if+ redim as zstring ptr argv(0 to argc - 1)+ for i as long = 0 to argc - 1+ argv(i) = Z(args(lbound(args) + i))+ next+ dim as zstring ptr envv()+ if nenv > 0 then redim envv(0 to nenv - 1)+ for i as long = 0 to nenv - 1+ envv(i) = Z(env(lbound(env) + i))+ next+ dim as long rc = janas_mcp_open_command(ZN(logName), argc, @argv(0), _+ nenv, First_(envv()), seconds, _+ @srv)+ if rc <> JANAS_MCP_OK then+ srv = 0+ return Fail(rc)+ end if+ return Done_()+end function++function McpServer.OpenUrl(byref url as const string, _+ byref serverName as const string = "", _+ byval seconds as long = 0) as boolean+ dim as string noHeaders()+ return OpenUrl(url, noHeaders(), serverName, seconds)+end function++function McpServer.OpenUrl(byref url as const string, headers() as string, _+ byref serverName as const string = "", _+ byval seconds as long = 0) as boolean+ this.Close()+ dim as long nh = Count_(headers())+ dim as zstring ptr hv()+ if nh > 0 then redim hv(0 to nh - 1)+ for i as long = 0 to nh - 1+ hv(i) = Z(headers(lbound(headers) + i))+ next+ dim as long rc = janas_mcp_open_url(ZN(serverName), Z(url), nh, _+ First_(hv()), seconds, @srv)+ if rc <> JANAS_MCP_OK then+ srv = 0+ return Fail(rc)+ end if+ return Done_()+end function++sub McpServer.Close()+ if srv then janas_mcp_close(srv)+ srv = 0+end sub++property McpServer.IsOpen() as boolean+ return srv <> 0+end property++property McpServer.ErrorCode() as long+ return lastCode+end property++property McpServer.ErrorText() as string+ return lastText+end property++property McpServer.Handle() as janas_mcp ptr+ return srv+end property++'' the server's texts, one getter each+function McpServer.Text(byval what as long) as string+ if srv = 0 then return ""+ dim as long rc+ dim as TextGetter getter+ select case what+ case 0+ getter = cast(TextGetter, @janas_mcp_info)+ case 1+ getter = cast(TextGetter, @janas_mcp_instructions)+ case 2+ getter = cast(TextGetter, @janas_mcp_tool_names)+ case 3+ getter = cast(TextGetter, @janas_mcp_result)+ case 4+ getter = cast(TextGetter, @janas_mcp_result_user)+ case else+ getter = cast(TextGetter, @janas_mcp_result_json)+ end select+ dim as string s = TakeText(getter, srv, rc)+ if rc <> JANAS_MCP_OK then Fail(rc) else Done_()+ return s+end function++property McpServer.Info() as string+ return Text(0)+end property++property McpServer.Instructions() as string+ return Text(1)+end property++property McpServer.ToolCount() as long+ if srv = 0 then return 0+ return janas_mcp_tool_count(srv)+end property++property McpServer.ToolNames() as string+ return Text(2)+end property++function McpServer.Tools(byref prefix as const string = "") as string+ if srv = 0 then return ""+ dim as zstring * 4096 small+ dim as long n = 0+ dim as long rc = janas_mcp_tools(srv, ZN(prefix), _+ len(prefix), @small, sizeof(small), @n)+ if rc = JANAS_MCP_OK andalso n < sizeof(small) then+ Done_()+ return small+ end if+ if rc <> JANAS_MCP_ESMALL then+ Fail(rc)+ return ""+ end if+ dim as zstring ptr big = callocate(n + 1)+ if big = 0 then+ lastCode = JANAS_MCP_ENOMEM+ lastText = "janas: out of memory"+ return ""+ end if+ rc = janas_mcp_tools(srv, ZN(prefix), len(prefix), _+ big, n + 1, @n)+ dim as string s+ if rc = JANAS_MCP_OK then s = *big+ deallocate(big)+ if rc <> JANAS_MCP_OK then Fail(rc) else Done_()+ return s+end function++function McpServer.ReadOnly(byref toolName as const string) as long+ if srv = 0 then return -1+ return janas_mcp_tool_read_only(srv, Z(toolName), len(toolName))+end function++function McpServer.Refresh() as boolean+ if srv = 0 then return false+ dim as long rc = janas_mcp_refresh(srv)+ if rc <> JANAS_MCP_OK then return Fail(rc)+ return Done_()+end function++function McpServer.CallTool(byref toolName as const string, _+ byref args as const string = "", _+ byval seconds as long = 0) as boolean+ if srv = 0 then+ lastCode = JANAS_MCP_EINVAL+ lastText = "janas: the server is not open"+ return false+ end if+ dim as long rc = janas_mcp_call(srv, Z(toolName), len(toolName), _+ ZN(args), len(args), _+ seconds)+ if rc <> JANAS_MCP_OK then return Fail(rc)+ return Done_()+end function++property McpServer.Result() as string+ return Text(3)+end property++property McpServer.ResultFailed() as boolean+ return srv <> 0 andalso janas_mcp_result_error(srv) = 1+end property++property McpServer.ResultUser() as string+ return Text(4)+end property++property McpServer.ResultJson() as string+ return Text(5)+end property++sub McpServer.Cancel()+ if srv then janas_mcp_cancel(srv)+end sub++function McpServer.AbiVersion() as long+ return janas_mcp_abi_version()+end function++function McpServer.ConfigPath() as string+ dim as zstring * 4096 buf+ dim as long n = 0+ if janas_mcp_config_path(@buf, sizeof(buf), @n) <> JANAS_MCP_OK then+ return ""+ end if+ return buf+end function++function McpServer.ConfigServers(byref configFile as const string = "") _+ as string+ dim as zstring * 8192 buf+ dim as long n = 0+ if janas_mcp_config_servers(ZN(configFile), _+ @buf, sizeof(buf), @n) <> JANAS_MCP_OK then+ return ""+ end if+ return buf+end function++end namespace++#endifdiff --git a/include/janas/llm.bi b/include/janas/llm.biindex 02c81d5..40bd9b2 100644--- a/include/janas/llm.bi+++ b/include/janas/llm.bi@@ -417,6 +417,7 @@ declare function janas_llm_chat_stats(byval c as const janas_llm_chat ptr, _end extern'' The parameter blocks carry their own size, and every call checks it, so a-'' header out of step with the library is refused instead of misread. As-'' built on 25 Sep 2026 the sizes are 56, 72 and 176 bytes; a program can see-'' for itself with sizeof() and with janas_llm_abi_version().+'' header out of step with the library is refused instead of misread. On+'' x86-64 the sizes are 64, 80 and 192 bytes (22 Oct 2026), the C header's+'' field for field; a program can see for itself with sizeof() and with+'' janas_llm_abi_version().diff --git a/tests/fb/check_oop.bas b/tests/fb/check_oop.basnew file mode 100644index 0000000..d7c5172--- /dev/null+++ b/tests/fb/check_oop.bas@@ -0,0 +1,143 @@+'' SPDX-License-Identifier: GPL-3.0-or-later+'' Copyright (C) 2026 Maurizio Cammalleri+'' check_oop.bas - janas_oop.bi on a model, run by tests/fb_check.c, which+'' compares the replies with the library's own in C:+''+'' check_oop model.jns "first message" "second message"+''+'' Greedy replies, read piece by piece, whole, through a handler, after a+'' reset and in a new chat; a conversation loaded, its start reused; one chat+'' at a time; the tokens of a text and back; a request on the side; the+'' model closed under a live chat, which then says it is closed and closes+'' nothing twice. Each reply written as "key hex", the rest as "FAIL ..."+'' lines; the exit status the count of failures.++#include once "janas/janas_oop.bi"++dim shared as long failures++sub check(byval ok as boolean, byref what as const string)+ if ok = false then+ failures += 1+ print "FAIL "; what+ end if+end sub++function hexOf(byref s as const string) as string+ dim as string h+ for i as long = 0 to len(s) - 1+ h &= lcase(hex(s[i], 2))+ next+ return h+end function++sub say(byref key as const string, byref value as const string)+ print key; " "; value+end sub++type Collect extends Janas.ReplyHandler+ answer as string+ pieces as long+ ended as boolean+ declare function OnPiece(byref text as const string, _+ byval reasoning as boolean) as boolean override+ declare sub OnEnd(byref st as const Janas.ReplyStats) override+end type++function Collect.OnPiece(byref text as const string, _+ byval reasoning as boolean) as boolean+ pieces += 1+ if reasoning = false then answer &= text+ return true+end function++sub Collect.OnEnd(byref st as const Janas.ReplyStats)+ ended = st.output_tokens > 0+end sub++dim as string modelFile = command(1), p1 = command(2), p2 = command(3)+dim as Janas.OpenOptions oo+oo.n_ctx = 4096+dim as Janas.Model model = Janas.Model(modelFile, oo)+if model.IsOpen = false then+ print "FAIL open: "; model.ErrorText+ end 1+end if+say("model", model.Name)+dim as Janas.ChatOptions greedy+greedy.Temperature = 0+greedy.Thinking = Janas.ThinkMode.Off+greedy.max_reply = 48++scope+ dim as Janas.Chat chat = Janas.Chat(model, greedy)+ check(chat.IsOpen, "the chat: " & chat.ErrorText)+ scope+ dim as Janas.Chat other = Janas.Chat(model)+ check(other.IsOpen = false andalso other.ErrorCode = JANAS_LLM_EBUSY, _+ "one chat at a time: " & other.ErrorText)+ end scope++ '' piece by piece+ check(chat.Send(p1), "send: " & chat.ErrorText)+ dim as string piece, t1+ while chat.NextPiece(piece)+ if chat.Reasoning = false then t1 &= piece+ wend+ check(chat.ErrorCode = 0 andalso chat.Finished, "the reply's end: " & _+ chat.ErrorText)+ say("t1", hexOf(t1))+ dim as Janas.ReplyStats st = chat.Stats()+ check(st.output_tokens > 0 andalso st.TokensPerSecond() > 0, _+ "the reply's counters")++ '' the whole conversation, its start already computed+ dim as Janas.Conversation conv+ conv.Add(Janas.MessageRole.User, p1)+ conv.Add(Janas.MessageRole.Assistant, t1)+ conv.Add(Janas.MessageRole.User, p2)+ check(chat.Load(conv), "load: " & chat.ErrorText)+ say("t2", hexOf(chat.Reply()))+ st = chat.Stats()+ say("cached", st.cached_tokens & " of " & st.prompt_tokens)+ check(st.cached_tokens > 0, "nothing of the prompt reused")++ '' whole, after a reset+ check(chat.Reset(), "reset: " & chat.ErrorText)+ say("t1again", hexOf(chat.Ask(p1)))++ '' through a handler+ check(chat.Reset(), "reset: " & chat.ErrorText)+ dim as Collect handler+ check(chat.Send(p1) andalso chat.ReadReply(handler), "the handler: " & _+ chat.ErrorText)+ say("t1handler", hexOf(handler.answer))+ check(handler.ended andalso handler.pieces > 0, "the handler's end")++ '' tokens, and a request on the side+ dim as long ids()+ check(model.Tokenize("Hello, world!", ids()), "tokenize: " & _+ model.ErrorText)+ dim as string back+ for i as long = lbound(ids) to ubound(ids)+ back &= model.TokenText(ids(i))+ next+ check(back = "Hello, world!", "the tokens' text: " & back)+ dim as string side = chat.Aside("", "Say yes.", 8)+ check(len(side) > 0, "a request on the side: " & chat.ErrorText)++ '' the model closed under a live chat+ model.Close()+ check(chat.IsOpen = false, "the chat closed with its model")+ check(chat.Send("x") = false andalso chat.ErrorText = _+ "janas: the chat is closed", "and it says so: " & chat.ErrorText)+end scope '' the chat's own close finds nothing to close++check(model.Open(modelFile, oo), "open again: " & model.ErrorText)+scope+ dim as Janas.Chat chat2 = Janas.Chat(model, greedy)+ say("t1chat2", hexOf(chat2.Ask(p1)))+end scope+model.Close()+say("done", str(failures))+end failuresdiff --git a/tests/fb/test_oop.bas b/tests/fb/test_oop.basnew file mode 100644index 0000000..df79c84--- /dev/null+++ b/tests/fb/test_oop.bas@@ -0,0 +1,198 @@+'' SPDX-License-Identifier: GPL-3.0-or-later+'' Copyright (C) 2026 Maurizio Cammalleri+'' test_oop.bas - janas_oop.bi without a model, run by tests/test_fb.c:+'' the parameter blocks as the C header lays them out, the options with the+'' library's defaults, a conversation kept by the program, a model and a+'' chat that do not open and say why, a handler's methods overridden, and+'' an MCP server - the fake one of the tests, given as the first argument -+'' started, its tools listed and called, and closed.+''+'' fbc tests/fb/test_oop.bas -i include -p bin/x86_64-linux -x test_oop+'' LD_LIBRARY_PATH=bin/x86_64-linux ./test_oop bin/x86_64-linux/mcp_fake_server++#include once "janas/janas_oop.bi"++dim shared as long checks, failures++sub check(byval ok as boolean, byref what as const string)+ checks += 1+ if ok = false then+ failures += 1+ print "FAIL "; what+ end if+end sub++'' the parameter blocks as llm.h lays them out on x86-64+#ifdef __FB_64BIT__+#assert sizeof(janas_llm_params) = 64+#assert sizeof(janas_llm_chat_params) = 80+#assert sizeof(janas_llm_chat_stats) = 192+#assert offsetof(janas_llm_params, reserve_bytes) = 48+#assert offsetof(janas_llm_chat_params, max_reasoning) = 72+#assert offsetof(janas_llm_chat_stats, reasoning_cut) = 188+#endif++check(Janas.Model.AbiVersion() = JANAS_LLM_ABI, "the library's ABI")+check(Janas.McpServer.AbiVersion() = JANAS_MCP_ABI, "the MCP library's ABI")++scope '' the options, with the library's defaults+ dim as Janas.ChatOptions co+ check(co.size = sizeof(janas_llm_chat_params), "ChatOptions.size")+ check(abs(co.Temperature - 0.7) < 1e-6, "the default temperature")+ check(co.top_k = 20 andalso co.speculate = 1 andalso co.recap = 1, _+ "the default top_k, speculate, recap")+ check(co.Thinking = Janas.ThinkMode.ModelDefault, "the default thinking")+ co.Temperature = 0+ co.max_reply = 48+ check(co.temperature = 0 andalso co.MAX_REPLY = 48, "fields by any case")+ dim as Janas.OpenOptions oo+ check(oo.size = sizeof(janas_llm_params), "OpenOptions.size")+ check(oo.n_ctx = 16384, "the default context")+ check(oo.Gpu = true andalso oo.FindMtp = false andalso oo.Mtp = "", _+ "the default files and GPU")+ oo.CacheGiB = 2+ check(oo.cache_bytes = 2147483648ull, "CacheGiB")+ dim as Janas.ReplyStats st+ check(st.size = sizeof(janas_llm_chat_stats), "ReplyStats.size")+ check(st.TokensPerSecond() = 0 andalso st.FinishText() = "" andalso _+ st.Reason() = Janas.FinishReason.Running, "an empty ReplyStats")+ st.finish = JANAS_LLM_FINISH_LENGTH+ st.output_tokens = 30+ st.output_seconds = 1.5+ check(st.FinishText() = "length" andalso _+ abs(st.TokensPerSecond() - 20) < 1e-9, "ReplyStats' methods")+end scope++'' read-only methods on const objects (a handler's OnEnd gets one)+sub constCalls(byref st as const Janas.ReplyStats, _+ byref conv as const Janas.Conversation, _+ byref m as const Janas.Model, byref c as const Janas.Chat, _+ byref srv as const Janas.McpServer)+ check(st.TokensPerSecond() = 0 andalso st.PromptTokensPerSecond() = 0 _+ andalso st.FinishText() = "", "a const ReplyStats")+ check(conv.Count = 1 andalso conv.TextOf(0) = "x" andalso _+ conv.RoleOf(0) = Janas.MessageRole.User, "a const Conversation")+ check(m.IsOpen = false andalso m.Name = "" andalso m.EmbedDim = 0 _+ andalso m.Experts = 0, "a const Model")+ check(c.IsOpen = false andalso c.Tokens = 0 andalso c.Calls = 0, _+ "a const Chat")+ check(srv.IsOpen = false andalso srv.ToolCount = 0, "a const McpServer")+end sub++scope+ dim as Janas.ReplyStats st+ dim as Janas.Conversation conv+ conv.Add(Janas.MessageRole.User, "x")+ dim as Janas.Model m+ dim as Janas.Chat c = Janas.Chat(m)+ dim as Janas.McpServer srv+ constCalls(st, conv, m, c, srv)+end scope++scope '' a conversation kept by the program+ dim as Janas.Conversation conv+ conv.Add(Janas.MessageRole.System, "Be brief.")+ conv.Add(Janas.MessageRole.User, "Ciao, città!")+ conv.Add(Janas.MessageRole.Assistant, "")+ check(conv.Count = 3, "three messages")+ check(conv.RoleOf(1) = Janas.MessageRole.User andalso _+ conv.TextOf(1) = "Ciao, città!" andalso conv.TextOf(2) = "", _+ "a message's role and text")+ check(conv.TextOf(7) = "", "a message that is not there")+ conv.Clear()+ check(conv.Count = 0, "cleared")+end scope++scope '' a model that does not open, and its chat+ dim as Janas.Model model = Janas.Model("/nonexistent/janas-test.jns")+ check(model.IsOpen = false, "a missing file does not open")+ check(model.ErrorCode = JANAS_LLM_EOPEN andalso len(model.ErrorText) > 0, _+ "and says why: " & model.ErrorText)+ check(model.Name = "" andalso model.Handle = 0, "nothing of it")+ dim as Janas.Chat chat = Janas.Chat(model)+ check(chat.IsOpen = false, "no chat without a model")+ check(chat.ErrorText = "janas: the model is not open", _+ "and it says so: " & chat.ErrorText)+ check(chat.Send("Hello") = false andalso chat.Ask("Hello") = "", _+ "a closed chat sends nothing")+ dim as string piece+ check(chat.NextPiece(piece) = false andalso chat.Finished, _+ "and reads nothing")+ check(Janas.Model.FindMtp("/nonexistent/janas-test.jns") = "", _+ "no MTP beside a missing file")+ dim as Janas.OpenOptions oo+ dim as ulongint fixed, cacheAll+ check(Janas.Model.Estimate("/nonexistent/janas-test.jns", oo, fixed, _+ cacheAll) = false, "no estimate of a missing file")+end scope++'' a handler: the methods overridden are the ones called+type Collect extends Janas.ReplyHandler+ pieces as long+ answer as string+ declare function OnPiece(byref text as const string, _+ byval reasoning as boolean) as boolean override+ declare sub OnEnd(byref st as const Janas.ReplyStats) override+ ended as boolean+end type++function Collect.OnPiece(byref text as const string, _+ byval reasoning as boolean) as boolean+ pieces += 1+ if reasoning = false then answer &= text+ return pieces < 3+end function++sub Collect.OnEnd(byref st as const Janas.ReplyStats)+ ended = true+end sub++scope+ dim as Collect c+ dim as Janas.ReplyHandler ptr h = @c+ check(h->OnPiece("a", false) andalso h->OnPiece("b", true) andalso _+ h->OnPiece("c", false) = false, "OnPiece overridden, and its stop")+ check(c.answer = "ac", "the reasoning left out")+ dim as Janas.ReplyStats st+ h->OnEnd(st)+ h->OnProgress(1, 2) '' the default: nothing+ check(c.ended, "OnEnd overridden")+end scope++'' an MCP server: the fake one, started from its command line+dim as string fake = command(1)+if len(fake) then+ dim as string argv(0 to 1) = {fake, "modern"}+ dim as Janas.McpServer server+ check(server.OpenCommand(argv()), "the fake server starts: " & _+ server.ErrorText)+ check(server.IsOpen andalso server.ToolCount >= 3, "it has tools")+ check(instr(server.ToolNames, "echo") > 0, "echo among them")+ dim as string tools = server.Tools("fake__")+ check(instr(tools, """fake__add""") > 0, "the tools as JSON, prefixed")+ check(server.CallTool("echo", "{""text"": ""ciao""}", 10), _+ "echo called: " & server.ErrorText)+ check(instr(server.Result, "ciao") > 0 andalso server.ResultFailed = false, _+ "echo answered: " & server.Result)+ check(server.CallTool("add", "{""a"": 2, ""b"": 3}", 10) andalso _+ instr(server.Result, "5") > 0, "add answered: " & server.Result)+ check(server.CallTool("fail", "", 10) andalso server.ResultFailed, _+ "a tool that fails says so")+ check(server.CallTool("no-such-tool", "", 10) = false andalso _+ server.ErrorCode <> 0, "an unknown tool")+ server.Close()+ check(server.IsOpen = false andalso server.CallTool("echo") = false, _+ "closed")+end if+dim as Janas.McpServer none = Janas.McpServer("no-such-server", _+ "/nonexistent/mcp.json")+check(none.IsOpen = false andalso none.ErrorCode = JANAS_MCP_EOPEN, _+ "no such server: " & none.ErrorText)+check(Janas.McpServer.ConfigServers("/nonexistent/mcp.json") = "", _+ "no servers in a missing file")++if failures then+ print "test_oop:"; failures; " of"; checks; " checks failed"+ end 1+end if+print "test_oop:"; checks; " checks, ok"diff --git a/tests/fb_check.c b/tests/fb_check.cnew file mode 100644index 0000000..745751a--- /dev/null+++ b/tests/fb_check.c@@ -0,0 +1,202 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * fb_check.c - Janas's classes for FreeBASIC against the library, on a+ * model:+ *+ * fb_check model.jns [seconds]+ *+ * First the library itself, here: a message and its reply, then the whole+ * conversation loaded with a second message, greedy, on the CPU alone+ * (JANAS_GPU=0, JANAS_NPU=0, for the same numbers in both processes). Then+ * tests/fb/check_oop.bas, built with the FreeBASIC compiler (FBC, or fbc in+ * the PATH), says the same through janas_oop.bi: the replies must be the+ * same text, byte for byte, read piece by piece, whole, through a handler,+ * after a reset and in a new chat; and it tries the rest (one chat at a+ * time, a conversation's start reused, tokens, a request on the side, the+ * model closed under a live chat). Not one of the tests run by build.sh: it+ * needs a model (Qwen3-0.6B takes seconds). Run from the sources' directory.+ */+#include <stdint.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "janas/llm.h"++#ifdef _WIN32+int main(void)+{+ printf("fb_check: tried on Linux only\n");+ return 0;+}+#else+#include "fb_run.h"++static const char p1[] = "Write one sentence about the sea.";+static const char p2[] = "And one about the mountains.";++/* the reply's answer, its reasoning left out */+static char *reply_text(janas_llm_chat *c, int32_t rc)+{+ size_t len = 0, cap = 4096;+ char *s = malloc(cap), buf[4096];+ int32_t n;+ while (s && rc == JANAS_LLM_OK &&+ (rc = janas_llm_chat_next(c, buf, sizeof(buf), &n)) ==+ JANAS_LLM_OK) {+ if (n <= 0 || janas_llm_chat_thinking(c))+ continue;+ if (len + (size_t)n + 1 > cap) {+ char *b = realloc(s, cap *= 2);+ if (!b) {+ free(s);+ return NULL;+ }+ s = b;+ }+ memcpy(s + len, buf, (size_t)n);+ len += (size_t)n;+ }+ if (!s || rc < 0) {+ fprintf(stderr, "fb_check: %s\n", janas_llm_last_error());+ free(s);+ return NULL;+ }+ s[len] = 0;+ return s;+}++static char *to_hex(const char *s)+{+ size_t n = strlen(s);+ char *h = malloc(2 * n + 1);+ for (size_t i = 0; h && i < n; i++)+ sprintf(h + 2 * i, "%02x", (unsigned char)s[i]);+ if (h)+ h[2 * n] = 0;+ return h;+}++/* the line "key value" of the program's output: its value is want */+static int same(const char *out, const char *key, const char *want)+{+ size_t kl = strlen(key);+ for (const char *line = out; *line;) {+ size_t n = strcspn(line, "\n");+ if (n > kl && !strncmp(line, key, kl) && line[kl] == ' ')+ return n - kl - 1 == strlen(want) &&+ !strncmp(line + kl + 1, want, n - kl - 1);+ line += n + (line[n] == '\n');+ }+ return 0;+}++int main(int argc, char **argv)+{+ if (argc < 2) {+ fprintf(stderr, "usage: fb_check model.jns [seconds]\n");+ return 2;+ }+ int timeout_s = argc > 2 ? atoi(argv[2]) : 600;+ char fbc[4096], bin[4096], tmp[4096], exe[4352];+ int variant = 0;+ if (!fb_find(fbc, sizeof(fbc)) || !fb_bin(bin, sizeof(bin), &variant) ||+ access("tests/fb/check_oop.bas", R_OK) != 0) {+ fprintf(stderr, "fb_check: no FreeBASIC compiler (FBC, or fbc in the "+ "PATH), or not run from the sources' directory\n");+ return 2;+ }+ if (variant) {+ fprintf(stderr, "fb_check: the shared libraries are the release "+ "build's: run bin/<cpu>-<os>/fb_check\n");+ return 2;+ }+ /* the same numbers in both processes: the CPU's */+ setenv("JANAS_GPU", "0", 1);+ setenv("JANAS_NPU", "0", 1);++ struct janas_llm_params p;+ janas_llm_params_default(&p);+ p.n_ctx = 4096;+ janas_llm *llm;+ if (janas_llm_open(argv[1], &p, &llm) != JANAS_LLM_OK) {+ fprintf(stderr, "fb_check: %s\n", janas_llm_last_error());+ return 2;+ }+ struct janas_llm_chat_params cp;+ janas_llm_chat_params_default(&cp);+ cp.temperature = 0;+ cp.thinking = 0;+ cp.max_reply = 48;+ janas_llm_chat *c;+ char *t1 = NULL, *t2 = NULL;+ if (janas_llm_chat_create(llm, &cp, &c) == JANAS_LLM_OK) {+ t1 = reply_text(c, janas_llm_chat_send(c, p1, -1));+ if (t1) {+ const int32_t roles[] = {JANAS_LLM_ROLE_USER,+ JANAS_LLM_ROLE_ASSISTANT,+ JANAS_LLM_ROLE_USER};+ const char *texts[] = {p1, t1, p2};+ t2 = reply_text(c, janas_llm_chat_load(c, 3, roles, texts, NULL));+ }+ janas_llm_chat_destroy(c);+ }+ janas_llm_close(llm);+ if (!t1 || !t2) {+ fprintf(stderr, "fb_check: the library's replies failed\n");+ return 1;+ }+ printf("library: \"%s\" / \"%s\"\n", t1, t2);++ if (!fb_tmpdir(tmp, sizeof(tmp))) {+ fprintf(stderr, "fb_check: no directory to build in: set TMPDIR\n");+ return 2;+ }+ static char out[1 << 20];+ snprintf(exe, sizeof(exe), "%s/check_oop", tmp);+ char *build[] = {fbc, "-w", "all", "tests/fb/check_oop.bas",+ "-i", "include", "-p", bin,+ "-x", exe, NULL};+ int failures = 0;+ int st = fb_exec(build, NULL, 300, out, sizeof(out));+ if (st != 0) {+ printf("FAIL check_oop.bas does not build:\n%s", out);+ fb_rmdir(tmp);+ return 1;+ }+ char *run[] = {exe, argv[1], (char *)p1, (char *)p2, NULL};+ st = fb_exec(run, bin, timeout_s, out, sizeof(out));+ fb_rmdir(tmp);+ char *h1 = to_hex(t1), *h2 = to_hex(t2);+ struct {+ const char *key, *want, *what;+ } cmp[] = {+ {"t1", h1, "the reply piece by piece"},+ {"t2", h2, "the conversation loaded, read whole"},+ {"t1again", h1, "the same message after a reset, Ask"},+ {"t1handler", h1, "the same message through a handler"},+ {"t1chat2", h1, "the same message in a new chat"},+ };+ printf("FreeBASIC: exit %d\n", st);+ failures += st != 0;+ for (size_t i = 0; i < sizeof(cmp) / sizeof(cmp[0]); i++) {+ int ok = h1 && h2 && same(out, cmp[i].key, cmp[i].want);+ printf("%-5s %s\n", ok ? "ok" : "FAIL", cmp[i].what);+ failures += !ok;+ }+ for (const char *line = out; *line;) {+ size_t n = strcspn(line, "\n");+ if (!strncmp(line, "FAIL", 4) || !strncmp(line, "model ", 6) ||+ !strncmp(line, "cached ", 7))+ printf("FreeBASIC: %.*s\n", (int)n, line);+ line += n + (line[n] == '\n');+ }+ printf("fb_check: %s\n", failures ? "FAIL" : "ok");+ free(h1);+ free(h2);+ free(t1);+ free(t2);+ return failures ? 1 : 0;+}+#endifdiff --git a/tests/fb_run.h b/tests/fb_run.hnew file mode 100644index 0000000..eba7bf2--- /dev/null+++ b/tests/fb_run.h@@ -0,0 +1,167 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * fb_run.h - FreeBASIC programs built and run, for the checks of+ * janas_oop.bi (test_fb.c, fb_check.c): the compiler from FBC or the PATH;+ * the directory of this program, where the shared libraries are; a+ * directory of its own for what is built ($TMPDIR, else /var/tmp); and a+ * command run with its output kept and a limit on its time. Linux only.+ */+#ifndef JANAS_TESTS_FB_RUN_H+#define JANAS_TESTS_FB_RUN_H++#include <dirent.h>+#include <poll.h>+#include <signal.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/wait.h>+#include <time.h>+#include <unistd.h>++/* the FreeBASIC compiler: $FBC, else fbc from the PATH; 0 when none */+static int fb_find(char *out, size_t cap)+{+ const char *env = getenv("FBC");+ if (env && *env) {+ if ((size_t)snprintf(out, cap, "%s", env) < cap &&+ access(out, X_OK) == 0)+ return 1;+ return 0;+ }+ const char *path = getenv("PATH");+ while (path && *path) {+ const char *end = strchr(path, ':');+ size_t n = end ? (size_t)(end - path) : strlen(path);+ if (n && (size_t)snprintf(out, cap, "%.*s/fbc", (int)n, path) < cap &&+ access(out, X_OK) == 0)+ return 1;+ path = end ? end + 1 : NULL;+ }+ return 0;+}++/* this program's directory (bin/<cpu>-<os>), and whether it is a variant's+ (test_fb-asan): the shared libraries are only the release build's */+static int fb_bin(char *out, size_t cap, int *variant)+{+ char self[4096];+ ssize_t n = readlink("/proc/self/exe", self, sizeof(self) - 1);+ if (n <= 0)+ return 0;+ self[n] = 0;+ char *base = strrchr(self, '/');+ if (!base)+ return 0;+ *base++ = 0;+ *variant = strchr(base, '-') != NULL;+ return (size_t)snprintf(out, cap, "%s", self) < cap;+}++/* a new directory for what is built */+static int fb_tmpdir(char *out, size_t cap)+{+ const char *t = getenv("TMPDIR");+ if ((size_t)snprintf(out, cap, "%s/janas-fb-XXXXXX",+ t && *t ? t : "/var/tmp") >= cap)+ return 0;+ return mkdtemp(out) != NULL;+}++/* the directory and the files in it (it holds nothing else) */+static void fb_rmdir(const char *dir)+{+ DIR *d = opendir(dir);+ struct dirent *e;+ char p[4352];+ while (d && (e = readdir(d)))+ if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..") &&+ (size_t)snprintf(p, sizeof(p), "%s/%s", dir, e->d_name) < sizeof(p))+ unlink(p);+ if (d)+ closedir(d);+ rmdir(dir);+}++/* a file with this text (test_fb.c writes small programs) */+__attribute__((unused)) static int fb_write(const char *path, const char *text)+{+ FILE *f = fopen(path, "w");+ if (!f)+ return 0;+ int ok = fputs(text, f) >= 0;+ return fclose(f) == 0 && ok;+}++/*+ * Runs argv (argv[0] a path) with LD_LIBRARY_PATH set to lib when it is not+ * NULL, its standard output and error into out (cap bytes, NUL-terminated,+ * the rest dropped). The exit status, or -1 when it could not run, died of a+ * signal, or was killed after timeout_s seconds.+ */+static int fb_exec(char *const argv[], const char *lib, int timeout_s,+ char *out, size_t cap)+{+ int fd[2];+ if (pipe(fd) != 0)+ return -1;+ fflush(stdout);+ fflush(stderr);+ pid_t pid = fork();+ if (pid < 0) {+ close(fd[0]);+ close(fd[1]);+ return -1;+ }+ if (pid == 0) {+ dup2(fd[1], 1);+ dup2(fd[1], 2);+ close(fd[0]);+ close(fd[1]);+ if (lib)+ setenv("LD_LIBRARY_PATH", lib, 1);+ execv(argv[0], argv);+ _exit(127);+ }+ close(fd[1]);+ size_t len = 0;+ int status = -1, done = 0;+ time_t end = time(NULL) + timeout_s;+ for (;;) {+ struct pollfd p = {.fd = fd[0], .events = POLLIN};+ int r = poll(&p, 1, 100);+ if (r > 0) {+ char buf[4096];+ ssize_t k = read(fd[0], buf, sizeof(buf));+ if (k > 0) {+ size_t take =+ (size_t)k < cap - 1 - len ? (size_t)k : cap - 1 - len;+ memcpy(out + len, buf, take);+ len += take;+ continue;+ }+ if (k == 0 && done)+ break;+ }+ if (!done) {+ int st;+ pid_t w = waitpid(pid, &st, WNOHANG);+ if (w == pid) {+ done = 1;+ status = WIFEXITED(st) ? WEXITSTATUS(st) : -1;+ } else if (time(NULL) > end) {+ kill(pid, SIGKILL);+ waitpid(pid, &st, 0);+ done = 1;+ status = -1;+ }+ } else if (r == 0)+ break;+ }+ close(fd[0]);+ out[len] = 0;+ return status;+}++#endifdiff --git a/tests/test_fb.c b/tests/test_fb.cnew file mode 100644index 0000000..715e974--- /dev/null+++ b/tests/test_fb.c@@ -0,0 +1,137 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * test_fb.c - Janas's classes for FreeBASIC (include/janas/janas_oop.bi),+ * without a model: tests/fb/test_oop.bas built with every warning on, which+ * must give none, and run (options, a conversation, a model and a chat that+ * do not open, a handler, the fake MCP server's tools); a program of two+ * modules, the second declaring only (JANAS_DECLARE_ONLY), which must link;+ * and a Model copied, which must not compile. Skipped without the FreeBASIC+ * compiler (FBC, or fbc in the PATH), out of the sources' directory, and in+ * the variants but release, which alone makes the shared libraries.+ */+#include <stdio.h>+#include <string.h>++#ifdef _WIN32+int main(void)+{+ printf("test_fb: skipped, tried on Linux only\n");+ return 0;+}+#else+#include <unistd.h>++#include "fb_run.h"++static int failures;++#define CHECK(cond, ...) \+ do { \+ if (!(cond)) { \+ failures++; \+ printf("FAIL %s:%d: ", __FILE__, __LINE__); \+ printf(__VA_ARGS__); \+ printf("\n"); \+ } \+ } while (0)++static const char two_main[] =+ "#include once \"janas/janas_oop.bi\"\n"+ "declare function other() as long\n"+ "dim as Janas.Model m = Janas.Model(\"/nonexistent/a.jns\")\n"+ "if m.IsOpen = false andalso other() = JANAS_LLM_EOPEN then print \"two "+ "modules ok\"\n";++static const char two_other[] =+ "#define JANAS_DECLARE_ONLY\n"+ "#include once \"janas/janas_oop.bi\"\n"+ "function other() as long\n"+ " dim as Janas.Model m = Janas.Model(\"/nonexistent/b.jns\")\n"+ " dim as Janas.Chat c = Janas.Chat(m)\n"+ " return m.ErrorCode\n"+ "end function\n";++static const char copied[] = "#include once \"janas/janas_oop.bi\"\n"+ "dim as Janas.Model a\n"+ "dim as Janas.Model b = a\n";++int main(void)+{+ char fbc[4096], bin[4096], tmp[4096], path[4352], path2[4352], exe[4352],+ lib_flag[4352], fake[4352];+ static char out[1 << 16];+ int variant = 0;+ if (!fb_find(fbc, sizeof(fbc))) {+ printf("test_fb: skipped, no FreeBASIC compiler (FBC, or fbc in the "+ "PATH)\n");+ return 0;+ }+ if (access("include/janas/janas_oop.bi", R_OK) != 0 ||+ access("tests/fb/test_oop.bas", R_OK) != 0) {+ printf("test_fb: skipped, the sources are not here\n");+ return 0;+ }+ if (!fb_bin(bin, sizeof(bin), &variant) || variant) {+ printf("test_fb: skipped, the shared libraries are the release "+ "build's\n");+ return 0;+ }+ if (!fb_tmpdir(tmp, sizeof(tmp))) {+ printf("test_fb: FAIL, no directory to build in: set TMPDIR\n");+ return 1;+ }+ snprintf(lib_flag, sizeof(lib_flag), "%s", bin);+ snprintf(fake, sizeof(fake), "%s/mcp_fake_server", bin);++ /* the test program, with every warning */+ snprintf(exe, sizeof(exe), "%s/test_oop", tmp);+ char *build[] = {fbc, "-w", "all", "tests/fb/test_oop.bas",+ "-i", "include", "-p", lib_flag,+ "-x", exe, NULL};+ int st = fb_exec(build, NULL, 300, out, sizeof(out));+ CHECK(st == 0, "test_oop.bas does not build:\n%s", out);+ CHECK(st != 0 || !strstr(out, "warning"), "warnings:\n%s", out);+ if (st == 0) {+ char *run[] = {exe, fake, NULL};+ st = fb_exec(run, bin, 120, out, sizeof(out));+ CHECK(st == 0 && strstr(out, "checks, ok"), "test_oop (exit %d):\n%s",+ st, out);+ if (st == 0)+ printf("%s", out);+ }++ /* two modules, one of them declaring only */+ snprintf(path, sizeof(path), "%s/two_main.bas", tmp);+ snprintf(path2, sizeof(path2), "%s/two_other.bas", tmp);+ snprintf(exe, sizeof(exe), "%s/two", tmp);+ CHECK(fb_write(path, two_main) && fb_write(path2, two_other),+ "cannot write in %s", tmp);+ char *two[] = {fbc, "-w", "all", path, path2, "-i",+ "include", "-p", lib_flag, "-x", exe, NULL};+ st = fb_exec(two, NULL, 300, out, sizeof(out));+ CHECK(st == 0, "two modules do not link:\n%s", out);+ if (st == 0) {+ char *run[] = {exe, NULL};+ st = fb_exec(run, bin, 60, out, sizeof(out));+ CHECK(st == 0 && strstr(out, "two modules ok"),+ "two modules (exit %d):\n%s", st, out);+ }++ /* a Model is not copied */+ snprintf(path, sizeof(path), "%s/copied.bas", tmp);+ snprintf(exe, sizeof(exe), "%s/copied", tmp);+ CHECK(fb_write(path, copied), "cannot write in %s", tmp);+ char *copy[] = {fbc, path, "-i", "include", "-p",+ lib_flag, "-x", exe, NULL};+ st = fb_exec(copy, NULL, 300, out, sizeof(out));+ CHECK(st != 0 && strstr(out, "public access"),+ "a Model was copied (it would be closed twice), or the build failed "+ "for another reason:\n%s",+ out);++ fb_rmdir(tmp);+ printf("test_fb: %s\n", failures ? "FAIL" : "ok");+ return failures ? 1 : 0;+}+#endif