GNU TeXmacsUser manualReferenceAboutEnglish
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.

1.The plug-in ai

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.

2.How a session request is processed

The generic machinery for :cmdline and :request plug-ins is described in the plug-in machinery. For the AI sessions, the steps are:

  1. 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.

  2. 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:

    ai-cmdline

    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.

    ai-request

    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).

  3. 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

).

3.The Tools menu

The AI entries of

texmacs/menus/tools-menu.scm

are:

AI engine

Sets or resets the preference "ai" to one of the available model names.

Correct

Shown when an engine is chosen and there is a selection; calls (ai-correct (get-preference "ai")).

Translate

Same condition; a submenu with one entry per supported language, which calls (ai-translate lan (get-preference "ai")).

External 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

:

ai-correct

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.

ai-translate

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.

4.Asynchronous translation

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".

5.AI agents

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.

6.Preferences

ai

The engine used by Tools\(\rightarrow\)Correct, Translate and ai-translate*.

ollama server, ollama port, ollama model

The Ollama server and model.

albert api key, albert model, albert chat history size

The Albert key, the model variant (openweight-large, -medium, -small) and the number of earlier exchanges sent with each session request.

albert ai-agents corrector, … interlocutor, … translator

The selected agents.

name-text-input

Text input in the sessions of the plug-in name.

ai-correct show differences, ai-correct explain

The presentation of corrections.

http request timeout

The timeout of HTTP requests in Qt 6 builds.

7.Pitfalls

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".