The harness module: AI assistant
How to expose entities to the AI assistant, which tools are generated, and how permissions, the chat endpoint, and OpenAI configuration work.
GoatCMS dashboard: Codex and Claude workers
GoatCMS uses the signed-in Codex or Claude account of a connected Goat worker for its dashboard chat. Update the application and worker together. Choose the agent, model and reasoning effort on the dashboard; an offline worker leaves the turn queued, and an exhausted quota leaves it waiting for reset. There is no automatic fallback to the OpenAI API.
This application backend extends the foundation harness with the user's public GoatCMS MCP tools. MCP writes use current permissions, version checks, idempotency and prepared critical changes. start_task_planning hands an existing task to planning under the panel's rules and returns the queued plan run; it never starts execution. Generated harness tools retain the read-only/form behavior described below. UI actions require an explicit click. History, questions and cancellation survive page reloads; browser-session or MCP-connection revocation stops delegated access. No repository or external MCP server is required.
Other applications keep the foundation backend unless they provide AI_CHAT_BACKEND. A generated HarnessRegistry and its provider-neutral Invoke method let child code reuse the same permission-aware tools.
The harness module lets an AI agent work with application data from the administration panel.
The agent does not execute arbitrary code or arbitrary SQL queries. It only has a small, explicit set of tools generated from the application model:
- tools that read data,
- tools that open a form or a delete confirmation dialog.
The app/harness package itself has no knowledge of the application domain. The tool catalog, the entity field registry, and the permission rules are supplied by code generated from herd/_model.goat, in the same way as for the dashboard.
Read access and form approval in generated harness tools
For delegated MCP permissions and your first read, see
First MCP connection.
Harness tools come in two kinds:
- backend — executed on the server, read-only,
- frontend — never executed on the server.
A frontend tool does not save or delete anything. It turns the agent's request into a browser action: opening a pre-filled form, or a delete confirmation dialog showing the record's real data.
A change only happens when a person submits the form or confirms the deletion themselves.
In addition, a single conversation turn may contain at most one frontend action. The loop stops at the first such call, so nothing happens without the user seeing it.
The same rules are part of the model's system prompt:
- the model can never create, change, or delete data itself,
- backend tools are only for reading,
- the model must not invent tool names, field names, or record ids,
- it replies in the language the user is writing in.
Enabling the module for an entity
Module definition
The module is defined in the model as an entity module with two field sets:
entity:module:def --name harness --property:list:list=<<MODULEEOF
def --required
MODULEEOF --property:list:persist=<<MODULEEOF
def --required
MODULEEOFlist— fields the agent may read: returned by the query tool and shown in the delete confirmation dialog,persist— fields the agent may pre-fill in a create or edit form.
Adding the module to an entity
An entity is exposed to the agent with module:add --name=harness. Example for the user entity:
module:add --name=harness \
--property:list:list="username,email" \
--property:list:persist="role,username,email,phone,shipping_recipient,shipping_street,shipping_unit,shipping_postal_code,shipping_city,shipping_country,billing_recipient,billing_street,billing_unit,billing_postal_code,billing_city,billing_country"The persist set deliberately leaves out password and email_verified_at. The agent cannot suggest values for these fields even when the user's role is allowed to write them.
Choose these fields the same way you choose fields for the crud module: it is an explicit decision about what the assistant may see and what it may suggest.
Conversation entities
Conversation history is stored in two model entities:
ai_conversation— a conversation owned by one user,ai_message— a single message in a conversation.
Both entities must exist in herd/_model.goat. They have no crud module, so they are not exposed through the public CRUD API. Only the harness endpoint reads and writes them.
The integration is active only when the model both:
- contains at least one entity with the
harnessmodule, - contains the
ai_conversationandai_messageentities.
Otherwise the generated InitHarness function does nothing and the endpoint is not registered.
Code generation
After changing the model, regenerate the application:
goat reAmong other things, the generator creates:
goatapp/app/api/modelapi/harness_gen.go
goatapp/app/harness/store_gen.goand, in each entity's model package, the masks:
<Entity>HarnessListMask
<Entity>HarnessPersistMaskThe list mask always includes the id field, so the agent can refer to a specific record.
Do not edit these files by hand. The whole goatapp/ directory is generated.
Generated tools
Each entity with the harness module gets a set of three tools:
| Tool | Kind | Behavior |
|---|---|---|
query_<entity> | backend | Reads records for analysis. Never changes data. |
open_<entity>_form | frontend | Opens a create or edit form, optionally pre-filled. |
confirm_delete_<entity> | frontend | Opens a delete confirmation dialog with the record's current data. |
For example, the product entity gets query_product, open_product_form, and confirm_delete_product.
query_<entity> arguments
{
"fields": ["title", "sku"],
"filters": {"status": "active"},
"limit": 20
}fields— subset of fields to return; omit it to get every available field,filters— exact-match filters keyed by field name,limit— maximum number of rows; 20 by default, 50 at most.
Filters only compare for equality. There are no range operators, no full-text search, and no sorting.
open_<entity>_form arguments
{
"id": "optional-record-id",
"fields": {"title": "New product"}
}- without
id, a create form is opened, - with
id, the edit form of an existing record is opened, fieldscontains the values to pre-fill.
confirm_delete_<entity> arguments
{"id": "record-id"}The id field is required.
Permissions
The agent's effective access is always the conjunction of two masks:
- the mask of the signed-in user's role,
- the entity's
harnessmodule mask.
Neither one alone is enough. An agent acting for a user with the user role will not see fields that role cannot read, even if they are in the list set. It will not see fields outside list either, even if the role can read them.
In practice this means that:
- a field unknown to the entity is rejected with an error,
- a field the user cannot access is left out of the query result,
- filtering on an inaccessible field fails with an error,
- form pre-fill values are filtered by the role's write mask and the
persistset, - the record preview in the delete dialog contains only readable fields.
If the record does not exist, or none of its fields are readable, the preview is simply empty. This way the user cannot tell "no access" apart from "no such record".
Table and column names come from the generated registry and are validated. Values supplied by the model reach SQL only as query parameters.
The POST /api/ai/chat endpoint
The endpoint requires a signed-in session.
Request:
{
"conversationId": "optional-conversation-id",
"message": "Show the latest orders"
}Response:
{
"conversationId": "conversation-id",
"reply": "I've opened a form to add this product. Please review it and submit to confirm.",
"action": {
"path": "/admin/product",
"queryParams": {"aiAction": "create", "aiPrefill": "{\"title\":\"New product\"}"}
}
}The action field is present only when the model asked for a frontend tool. In that case reply is a fixed message produced by the server (currently in English), not text written by the model.
Error codes:
401— no signed-in session,400— empty message or invalid JSON,500— failure loading history or calling the model.
One turn, step by step
conversation history + new message
↓
the model picks a tool
↓
backend: run the read and return to the model
frontend: return the action to the browser and end the turn
↓
text replyA single turn may run at most 5 rounds of backend tools. When the limit is exceeded, the endpoint returns an error, so the model cannot loop forever.
Conversation history
Only user and assistant messages are stored. Intermediate tool calls exist only within one turn and are never replayed into later requests.
A conversation belongs to the user who started it. Sending the conversationId of someone else's conversation behaves as if that conversation did not exist.
Frontend
Dashboard widget
The chat is a dashboard widget of type ai_chat. In this application it is defined in herd/_model.goat:
section:add --name=assistant --type=cards --roles=admin,manager,user
widget:add --name=ai_assistant --type=ai_chat --label="AI Assistant"The ai_chat widget accepts no data source, aggregate, chart, or query. It only talks to /api/ai/chat.
Performing an action
When the response contains action, AiChatService navigates to the entity's generated view:
/admin/<entity-in-kebab-case>with these parameters:
aiAction—create,edit, ordelete,aiPrefill— JSON with values to pre-fill the create form,aiId— id of the record to edit or delete,aiPreview— JSON with the record preview for the delete confirmation dialog.
The generated entity manager reads these parameters, opens the matching form or dialog, and removes the parameters from the URL.
Pre-fill applies to the create form. For edit, the manager opens the existing record by aiId and shows its current values.
Configuration
The OpenAI client is configured through environment variables:
| Variable | Meaning | Default |
|---|---|---|
GOAT_AI_OPENAI_API_KEY | OpenAI API key for the assistant | none |
GOAT_AI_OPENAI_MODEL | Chat model used by the assistant | gpt-4o-mini |
With an empty key the endpoint is still registered, but every request fails and the widget shows a message saying the assistant is unavailable.
Do not confuse these variables with GOAT_OPENAI_API_KEY, which is used by the translation scripts (herd/translate.goat, translate-docs.goat).
The key is a secret. Keep it in your local env/dev.env or in the deployment configuration, and never commit it to the repository. herd/deploy.json declares GOAT_AI_OPENAI_API_KEY as a secret and GOAT_AI_OPENAI_MODEL as a regular variable.
Data privacy
Backend tool results are sent to the model as part of the conversation. Every field in the list set that the user can read may therefore reach OpenAI.
Adding a field to list is a decision to share it with an external AI provider. Do not put passwords, tokens, or data that must not be processed outside the application there.
Testing
The package logic is tested without contacting OpenAI. Run takes a Completer interface, which the tests replace with a fake client:
(cd goatapp && GOWORK=off go test ./app/harness/)The generated E2E test goatapp/web/e2e-server/harness.spec.ts always checks that anonymous requests (401) and empty messages (400) are rejected.
A full conversation calls the configured AI provider, so it only runs when you explicitly set this variable while running the Playwright tests:
GOAT_E2E_HARNESS_CHAT=1Summary
- The
harnessmodule exposes an entity to the AI assistant through three generated tools. - Reads are limited by the conjunction of the role mask and the
listset. - Form pre-fill is limited by the role's write mask and the
persistset. - The assistant never saves or deletes data itself; a person always does.
- It needs the
ai_conversationandai_messageentities and theGOAT_AI_OPENAI_API_KEYkey.
Application tools (child extensions)
Implement harness.ToolProvider with HarnessTools() []harness.Tool on an
application service and register it using AddHarnessExtension(service).
The optional HarnessExtensions() []any is read on each request. Existing
tool names take precedence; applications without extensions are unchanged.
Use services.SessionFromContext(ctx) to obtain the session and recheck
authorization on every call. For a custom form route, setFrontendActionCustom and Tool.Resolve, returning *harness.Action,
a message and an error. Empty paths are rejected. Resolvers prepare forms;
the person submits them to save changes.
The child boards integration lists boards, reads tasks and proposes create,
edit and move forms. Viewers can read; owners/editors can open forms; client
members cannot access these tools. Task content read by tools is sent to the
AI provider (OpenAI).