![]() |
Sessions, menus and agents | ![]() |
This page describes the Scheme side of the AI integration: the plug-in which declares the AI sessions, the commands behind the Tools menu, and the registry of AI agents.
The plug-in lives in
src/plugins/ai
(copied to TeXmacs/plugins/ai by the build). Its file
progs/init-ai.scm
declares one plug-in per engine, whose name is the model name:
(plugin-configure albert
(:require (has-albert?))
(:request ,ai-request ,ai-result)
(:preferences #t)
(:session "Albert")
(:serializer ,ai-serialize)) |
The other engines (chatgpt, gemini, ollama, open-mistral-7b) use (:cmdline ,ai-cmdline ,ai-result) instead of :request. The availability tests are has-chatgpt? (the openai tool is in the path and OPENAI_API_KEY is set), has-gemini? and has-open-mistral-7b? (the key variable is set), has-ollama? (the ollama tool is in the path) and has-albert? (the preference albert api key is not empty). The same predicates decide which engines are offered in Tools\(\rightarrow\)AI engine.
The file also declares the preferences of the engines (see below), the plugin-preferences-widget shown for these plug-ins in the plug-in page of the preferences, and the helpers ollama-models, ollama-model-variants and ollama-default-model, which parse the output of ollama list. At load time it copies ALBERT_API_KEY into the preference albert api key if the variable is set.
The generic machinery for :cmdline and :request plug-ins is described in the plug-in machinery. For the AI sessions, the steps are:
The input field is serialized by the plug-in serializer ai-serialize (
tools/ai/ai-batch.scm
), as plain text or as a LaTeX snippet.
The link calls the Scheme function connection-cmdline or connection-request (
kernel/texmacs/tm-plugins.scm
) with the plug-in name, the chat name and the serialized input, which calls the function given in the configuration:
returns (cpp-ai-latex-command cmd name chat), a shell command. cmdline_link_rep::write runs it with /bin/sh -c in a child process, with standard error discarded.
returns (cpp-ai-latex-request cmd name chat), the request tree written as a Scheme string. request_link_rep::write parses it and starts an asynchronous http_post (the only form it accepts).
When the command or the request has finished, the collected output goes to texmacs_input_rep::cmdline_flush (
Data/Convert/Generic/input.cpp
), which calls connection-result, hence ai-result, that is, cpp-ai-latex-output, and writes the resulting tree as the output of the session.
The preference name-text-input (the Textual input toggle of the plug-in preferences, on by default) makes the input fields of the session text rather than mathematics (session-text-input? in
dynamic/session-edit.scm
).
The AI entries of
texmacs/menus/tools-menu.scm
are:
Sets or resets the preference "ai" to one of the available model names.
Shown when an engine is chosen and there is a selection; calls (ai-correct (get-preference "ai")).
Same condition; a submenu with one entry per supported language, which calls (ai-translate lan (get-preference "ai")).
Copy and Cut put (compress-html (selection-tree) 1) on the clipboard, Paste inserts (decompress-html clipboard 1). This lets the user work with the web interface of any chatbot while keeping the markup.
These commands are defined in
tools/ai/ai-batch.scm
, which is loaded at startup by
init-texmacs.scm
:
Takes the selection, cuts it to the primary clipboard, calls cpp-ai-correct with the language of the selection, and inserts the result. If the preference ai-correct show differences is on, it inserts instead the differences between the original and the corrected version, as computed by compare-versions (see versioning). If ai-correct explain is on and the model gave explanations, they are shown in the auxiliary buffer “Comments from AI about corrections”. Both preferences are set in the AI tab of the Convert page of the preferences.
The same for cpp-ai-translate, from the language of the selection into the chosen one.
Both are synchronous: the editor is blocked until the answer has arrived.
tools/ai/ai-translate.scm
implements (ai-translate* lan), which translates the selection, or the whole document if there is no selection, paragraph by paragraph and in the background, and ai-abort-translate, which stops it. Both are declared lazily in
init-texmacs.scm
; no menu entry calls them at present.
The work is organized with the process utilities of
utils/library/process.scm
: make-process splits the document into paragraphs and calls the processing function on each of them in turn, through tree pointers so that the user may keep editing. For each paragraph, translate-process-one compresses it, skips it if it contains no text, builds a request with cpp-ai-command and an agent asking for an HTML translation, and starts it with cpp-ai-async-eval-command. When the reply arrives, translate-processed-one extracts the answer, decompresses it and replaces the paragraph, but only if the paragraph has not been modified meanwhile, and then continues with the next paragraph. The model is the preference "ai".
An agent is a named set of instructions which is added to the requests of a given kind. Agents are stored in the user database of kind "ai-agents" (
database/ai-agents-db.scm
), with three entry types, corrector, interlocutor and translator, each with a single field instructions. They are edited like bibliographies, through Data\(\rightarrow\)Open AI agents, which opens tmfs://db/ai-agents/global (see the user interface of the database).
For each engine, the preferences engine ai-agents corrector, interlocutor and translator select an agent by name, or default. ai-agents-get-corrector, ai-agents-get-interlocutor and ai-agents-get-translator return the instructions of the selected agent; the defaults are a short built-in description for the corrector and nothing for the others. If the selected agent no longer exists, a warning is issued and the preference is reset to default.
ai.cpp
calls these functions only for Albert, and only the Albert preferences widget offers the choice.
The engine used by Tools\(\rightarrow\)Correct, Translate and ai-translate*.
The Ollama server and model.
The Albert key, the model variant (openweight-large, -medium, -small) and the number of earlier exchanges sent with each session request.
The selected agents.
Text input in the sessions of the plug-in name.
The presentation of corrections.
The timeout of HTTP requests in Qt 6 builds.
All sessions of an engine share one conversation. The links always pass the chat name "default" (
System/Link/cmdline_link.cpp:191
,
request_link.cpp:130
and
Data/Convert/Generic/input.cpp:478
), so the Albert history is shared by all Albert sessions and is never reset.
A failed correction removes the selection. ai-correct and ai-translate cut the selection before the request is made (
tools/ai/ai-batch.scm:62
and 84); if the request fails, the answer is empty and the selection is replaced by nothing. The original is still in the primary clipboard.
Asynchronous translation with Albert hangs. cpp-ai-async-eval-command refuses Albert requests (see engines, requests and transport) without calling the callback, so the translation process never moves to the next paragraph and stays registered as running.
Input is flattened in command line sessions. cmdline_link_rep::write replaces the newlines of the serialized input by spaces before building the command, so the LaTeX sent to the model is on a single line and line breaks, for instance in verbatim blocks, are lost. On Windows, the same function does not run anything at all, so the command line engines do not work there.
Keys and prompts on disk. The Albert key taken from ALBERT_API_KEY is stored in the preferences file in plain text, and the plug-in preferences widget displays it. Every ChatGPT prompt is written to $TEXMACS_HOME_PATH/system/tmp/chatgpt.txt.