diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9e476d8..84f3b7d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,14 @@
# Changelog
+## Unreleased - One tool registry (no behaviour change); built against QQQ 4.1.0-RC.32
+
+- The assistant builds against QQQ 4.1.0-RC.32 (was RC.25), the version applications pin. Nothing had to change for it; the chat's tool list is byte for byte the same under both versions.
+- Every tool is now registered once, in `tools.registry.ToolRegistry`: its name, its kind (`READ`, `PROPOSE`, `DISPLAY`), the checks it runs, its definition (description and input schema) and how one call of it runs. The chat's tool list (`ContextBuilder.tools`), a tool call of a turn and a confirmed write (`POST /actions/{id}/confirm`) all look a tool up there; none of them chooses a tool family any more.
+- "Run one tool call for this person under this policy, and log it" moved out of the turn engine into `engine.ToolCallService`, which the confirm route uses too: the refusal of a tool that is not offered, the call itself (with the person's own session), and the `assistant_tool_call` row.
+- Nothing a person, a model or an administrator sees changed: the same tools are offered in the same order with the same definitions, a call goes through the same checks in the same order, refusals read the same and the same log rows are written. `ChatToolSetTest` holds the tool list of the code before this change and compares it; `ToolCallChecksTest` passes unchanged on the code before and after.
+- A tool family is a class under `com.kingsrook.qbits.assistant.tools` that implements `ToolFamily` and has a public no-argument constructor. It is found when the runtime starts; no existing class has to name it. The runtime refuses to start if no family is found or two tools share a name.
+- `UiApiToolbox`, `AnalysisTools`, `BulkLoadTools`, `KnowledgeTool`, `PresentResultsTool` and the core kernel keep all of their behaviour; `UiApiToolFamily`, `BulkLoadToolFamily`, `KnowledgeToolFamily`, `PresentResultsToolFamily` and `engine.KernelToolFamily` register their tools. `ContextBuilder` is constructed with the registry (`withKnowledgeTool` and `withBulkLoadTools` are gone).
+
## Unreleased - The assistant works for people who do not administer it on a host that enforces table permissions in core actions; a person's own budget (UI API extension 1.8.0)
- Fixed: on a host with `QInstance.enforcePermissionsInCoreActions` on, a person with `assistant.hasAccess` and without `assistant.admin` was refused before any model call ("Not authorized to access the assistant admin data."). The assistant read its settings (policy, providers, models, prices, budgets, data access) and wrote that person's messages, tool calls and usage through tables whose permission rule is `assistant.admin`, with the person's session. `AssistantStore` now marks every one of its calls as the assistant's own service code (`withPermissionBypass`), the way qbit-collaboration and qbit-backups do.
diff --git a/CLAUDE.md b/CLAUDE.md
index 539ee17..c249673 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -63,6 +63,8 @@ Always use the wrapper: Maven 3.10 cannot read the parent POM. The committed def
- **Data access:**
- The vector index stores no record content: only source/table/key/field/chunk, content hash and embedding. Retrieval re-reads every hit through the UI API as the asking user, then applies the existing get_record exposure/masking path.
- Only `persistence/` calls QueryAction, InsertAction and the other data actions (ArchUnit).
+ - Every tool is registered once in `tools/registry/ToolRegistry` (name, kind, checks, definition, execute) by a `ToolFamily` found under `tools/` at startup; `engine/KernelToolFamily` (navigate, search_help) is the one the runtime names. A new tool family is new files only.
+ - A tool is looked up only in the registry. Outside `tools/`, only `engine/ToolCallService` runs a tool or writes an `assistant_tool_call` row (ArchUnit); the turn engine and the confirm route both go through it. A tool call runs as the signed-in person, never as the assistant.
- Data tools run only through `UiApiToolbox` → `UiApiGateway` (spec'd `/qqq/v1` operations with the user's `sessionUUID`); never core data actions for user data. Only `navigate` and `search_help` use `AiToolKernel.execute`.
- Data access (`DataAccessService`) and the policy only ever narrow what the UI API allows; a table whose AI exposure is NONE stays NONE.
- A write runs only after the owner confirms its pending action (unless the policy turns confirmation off), with the confirming request's session, once.
diff --git a/pom.xml b/pom.xml
index a94732b..89685f0 100644
--- a/pom.xml
+++ b/pom.xml
@@ -217,7 +217,7 @@
- 4.1.0-RC.25
+ 4.1.0-RC.32
diff --git a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/AssistantRuntime.java b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/AssistantRuntime.java
index ffa0ce3..fe0bcec 100644
--- a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/AssistantRuntime.java
+++ b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/AssistantRuntime.java
@@ -21,6 +21,7 @@ import java.lang.ref.WeakReference;
import java.net.InetAddress;
import java.time.Clock;
import java.time.Duration;
+import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@@ -34,12 +35,14 @@ import com.kingsrook.qbits.assistant.attachments.UploadRefSource;
import com.kingsrook.qbits.assistant.engine.BudgetService;
import com.kingsrook.qbits.assistant.engine.ClientErrors;
import com.kingsrook.qbits.assistant.engine.ContextBuilder;
+import com.kingsrook.qbits.assistant.engine.KernelToolFamily;
import com.kingsrook.qbits.assistant.engine.PolicyService;
import com.kingsrook.qbits.assistant.engine.ProfileService;
import com.kingsrook.qbits.assistant.engine.StarterPrompts;
import com.kingsrook.qbits.assistant.engine.ProviderService;
import com.kingsrook.qbits.assistant.engine.RateLimiter;
import com.kingsrook.qbits.assistant.engine.RunningTurn;
+import com.kingsrook.qbits.assistant.engine.ToolCallService;
import com.kingsrook.qbits.assistant.metadata.AssistantChannelWall;
import com.kingsrook.qbits.assistant.persistence.AssistantStore;
import com.kingsrook.qbits.assistant.rag.InMemoryVectorStore;
@@ -49,6 +52,7 @@ import com.kingsrook.qbits.assistant.scheduled.ScheduledTurn;
import com.kingsrook.qbits.assistant.tools.KnowledgeTool;
import com.kingsrook.qbits.assistant.tools.UiApiToolbox;
import com.kingsrook.qbits.assistant.tools.bulkload.BulkLoadTools;
+import com.kingsrook.qbits.assistant.tools.registry.ToolRegistry;
import com.kingsrook.qbits.assistant.uiapi.CoreUiApiGateway;
import com.kingsrook.qbits.assistant.uiapi.UiApiCall;
import com.kingsrook.qbits.assistant.uiapi.UiApiGateway;
@@ -93,6 +97,8 @@ public class AssistantRuntime
private final RateLimiter rateLimiter;
private final ClientErrors clientErrors;
private final AiToolKernel kernel;
+ private final ToolRegistry toolRegistry;
+ private final ToolCallService toolCalls;
private final ContextBuilder contextBuilder;
private final DataAccessService dataAccessService;
private final ProfileService profileService;
@@ -187,7 +193,13 @@ public class AssistantRuntime
this.knowledgeTool = new KnowledgeTool(this, ragService, vectors);
this.bulkLoadTools = new BulkLoadTools(qInstance, dataAccessService, config.getBulkLoadRunner());
this.attachmentService = new AttachmentService(config.getAttachments(), new UploadRefSource());
- this.contextBuilder = new ContextBuilder(config, kernel, toolbox).withKnowledgeTool(knowledgeTool).withBulkLoadTools(bulkLoadTools).withBudgets(budgetService);
+ ///////////////////////////////////////////////////////////////////////
+ // every tool, registered once: the families found under the tools //
+ // package, and the two record-free tools that stay on the kernel //
+ ///////////////////////////////////////////////////////////////////////
+ this.toolRegistry = ToolRegistry.discover(this, List.of(new KernelToolFamily()));
+ this.toolCalls = new ToolCallService(toolRegistry, store);
+ this.contextBuilder = new ContextBuilder(config, kernel, toolRegistry).withBudgets(budgetService);
if(Boolean.TRUE.equals(config.getRegisterShutdownHook()))
{
///////////////////////////////////////////////////////////////////////
@@ -316,6 +328,26 @@ public class AssistantRuntime
+ /*******************************************************************************
+ ** Every tool the assistant has; the only place a tool is looked up.
+ *******************************************************************************/
+ public ToolRegistry getToolRegistry()
+ {
+ return (toolRegistry);
+ }
+
+
+
+ /*******************************************************************************
+ ** Runs and logs one tool call for the signed-in person.
+ *******************************************************************************/
+ public ToolCallService getToolCalls()
+ {
+ return (toolCalls);
+ }
+
+
+
/*******************************************************************************
** Getter for uiApiGateway
*******************************************************************************/
diff --git a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/api/AssistantApi.java b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/api/AssistantApi.java
index 73b35d9..d671ec5 100644
--- a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/api/AssistantApi.java
+++ b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/api/AssistantApi.java
@@ -51,19 +51,16 @@ import com.kingsrook.qbits.assistant.model.AssistantConversation;
import com.kingsrook.qbits.assistant.model.AssistantFeedback;
import com.kingsrook.qbits.assistant.model.AssistantMessage;
import com.kingsrook.qbits.assistant.model.AssistantPendingAction;
-import com.kingsrook.qbits.assistant.model.AssistantToolCall;
import com.kingsrook.qbits.assistant.model.AssistantTurn;
import com.kingsrook.qbits.assistant.model.AssistantTurnEvent;
import com.kingsrook.qbits.assistant.persistence.AssistantStore;
import com.kingsrook.qbits.assistant.persistence.TranscriptRedactor;
import com.kingsrook.qbits.assistant.spi.ModelCapabilities;
-import com.kingsrook.qbits.assistant.tools.AnalysisTools;
import com.kingsrook.qbits.assistant.tools.UiApiToolbox;
-import com.kingsrook.qbits.assistant.tools.bulkload.BulkLoadTools;
+import com.kingsrook.qbits.assistant.tools.registry.ConfirmedToolCall;
import com.kingsrook.qqq.backend.core.actions.ai.AiRecordRef;
import com.kingsrook.qqq.backend.core.actions.ai.AiToolOutcome;
import com.kingsrook.qqq.backend.core.actions.ai.AiToolResult;
-import com.kingsrook.qqq.backend.core.actions.ai.output.AiRedactor;
import com.kingsrook.qqq.backend.core.actions.permissions.PermissionsHelper;
import com.kingsrook.qqq.backend.core.actions.permissions.TablePermissionSubType;
import com.kingsrook.qqq.backend.core.context.QContext;
@@ -1025,23 +1022,12 @@ public class AssistantApi
}
PolicyService.EffectivePolicy policy = runtime.getPolicyService().current(true);
UiApiToolbox.Context context = new UiApiToolbox.Context(session.getUuid(), session.getValue(QSession.VALUE_KEY_USER_TIMEZONE), runtime.getUiApiGateway(), policy.tools(), null, credentials, action.getTurnId() == null ? null : String.valueOf(action.getTurnId()));
- UiApiToolbox.Outcome outcome;
- if(BulkLoadTools.WRITE_TOOLS.contains(action.getToolName()))
- {
- outcome = runtime.getBulkLoadTools().executeConfirmed(context, action.getToolCallId(), action.getToolName(), action.getTableName(), values);
- }
- else if(AnalysisTools.WRITE_TOOLS.contains(action.getToolName()))
- {
- ///////////////////////////////////////////////////////////////////////
- // a saved analysis and its schedule: created as, and emailed to, the //
- // user who confirms (this request's session) //
- ///////////////////////////////////////////////////////////////////////
- outcome = runtime.getToolbox().executeConfirmedAnalysis(context, action.getToolCallId(), action.getToolName(), values, userId);
- }
- else
- {
- outcome = runtime.getToolbox().executeConfirmedWrite(context, action.getToolCallId(), action.getToolName(), action.getTableName(), action.getRecordId(), values);
- }
+ ///////////////////////////////////////////////////////////////////////
+ // the registry's tool runs its confirmed write, as the user who //
+ // confirms (this request's session) //
+ ///////////////////////////////////////////////////////////////////////
+ UiApiToolbox.Outcome outcome = runtime.getToolCalls().executeConfirmed(
+ new ConfirmedToolCall(context, action.getToolCallId(), action.getToolName(), action.getTableName(), action.getRecordId(), values, userId));
AiToolResult result = outcome.getResult();
boolean ok = result.getOutcome() == AiToolOutcome.OK;
@@ -1051,25 +1037,7 @@ public class AssistantApi
.withValue("httpStatus", outcome.getHttpStatus())
.withValue("resultSummary", truncate(result.getSummary(), 500)));
- store.insert(new AssistantToolCall()
- .withTurnId(action.getTurnId())
- .withClientChannel("assistant" + (action.getTurnId() == null ? "" : "/" + action.getTurnId()))
- .withConversationId(action.getConversationId())
- .withUserId(userId)
- .withToolCallId(action.getToolCallId())
- .withToolName(action.getToolName())
- .withInputJson(truncate(new AiRedactor(List.of()).redact(action.getParamsJson() == null ? "{}" : action.getParamsJson(), new HashSet<>()), 4000))
- .withOutcome(result.getOutcome() == null ? AiToolOutcome.ERROR.name() : result.getOutcome().name())
- .withPermissionCheck(result.getPermissionCheck())
- .withTableName(action.getTableName())
- .withRowCount(result.getRowCount())
- .withDurationMs(result.getDurationMs())
- .withResultSummary(truncate("Confirmed: " + result.getSummary(), 500))
- .withResultJson(result.getModelContent())
- .withUiApiOperation(outcome.getOperation())
- .withHttpStatus(outcome.getHttpStatus())
- .withAuditHref(outcome.getAuditHref())
- .withPendingActionId(id));
+ runtime.getToolCalls().logConfirmed(action, userId, outcome);
reportDecision(action, result, ok);
diff --git a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/engine/ContextBuilder.java b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/engine/ContextBuilder.java
index b6d21ed..157d4c5 100644
--- a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/engine/ContextBuilder.java
+++ b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/engine/ContextBuilder.java
@@ -25,7 +25,6 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
-import java.util.TreeMap;
import com.kingsrook.qbits.assistant.AssistantQBitConfig;
import com.kingsrook.qbits.assistant.AssistantSecurity;
import com.kingsrook.qbits.assistant.model.AssistantMessage;
@@ -35,11 +34,8 @@ import com.kingsrook.qbits.assistant.spi.ModelPart;
import com.kingsrook.qbits.assistant.spi.ModelRequest;
import com.kingsrook.qbits.assistant.spi.ModelToolDefinition;
import com.kingsrook.qbits.assistant.spi.SystemBlock;
-import com.kingsrook.qbits.assistant.tools.KnowledgeTool;
-import com.kingsrook.qbits.assistant.tools.PresentResultsTool;
-import com.kingsrook.qbits.assistant.tools.UiApiToolbox;
-import com.kingsrook.qbits.assistant.tools.bulkload.BulkLoadTools;
-import com.kingsrook.qqq.backend.core.actions.ai.AiToolDefinition;
+import com.kingsrook.qbits.assistant.tools.registry.ToolOffer;
+import com.kingsrook.qbits.assistant.tools.registry.ToolRegistry;
import com.kingsrook.qqq.backend.core.actions.ai.AiToolKernel;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QException;
@@ -76,10 +72,8 @@ public class ContextBuilder
private final AssistantQBitConfig config;
private final AiToolKernel kernel;
- private final UiApiToolbox toolbox;
+ private final ToolRegistry registry;
private volatile String appDigest;
- private KnowledgeTool knowledgeTool;
- private BulkLoadTools bulkLoadTools;
private BudgetService budgets;
@@ -87,11 +81,11 @@ public class ContextBuilder
/*******************************************************************************
**
*******************************************************************************/
- public ContextBuilder(AssistantQBitConfig config, AiToolKernel kernel, UiApiToolbox toolbox)
+ public ContextBuilder(AssistantQBitConfig config, AiToolKernel kernel, ToolRegistry registry)
{
this.config = config;
this.kernel = kernel;
- this.toolbox = toolbox;
+ this.registry = registry;
}
@@ -107,9 +101,10 @@ public class ContextBuilder
/*******************************************************************************
- ** The tools a policy allows, sorted by name (a stable cache prefix): the UI
- ** API data tools (write tools only when some table allows writes) plus the
- ** kernel's navigate and search_help.
+ ** The tools a policy allows, sorted by name (a stable cache prefix): every
+ ** tool of the registry that is offered now (the UI API data tools, with
+ ** write tools only when some table allows writes, the kernel's navigate and
+ ** search_help, and the rest).
*******************************************************************************/
public List tools(PolicyService.ToolPolicy policy)
{
@@ -119,51 +114,12 @@ public class ContextBuilder
/*******************************************************************************
- ** Attribute tool-discovery requests to the turn that needs them.
+ ** What the registry offers the person in context under the policy. Tool
+ ** discovery requests are attributed to the turn that needs them.
*******************************************************************************/
private List tools(PolicyService.ToolPolicy policy, String turnId)
{
- Map byName = new TreeMap<>();
- for(ModelToolDefinition definition : toolbox.definitions(policy, toolbox.anyWritableTable()))
- {
- byName.put(definition.getName(), definition.withStrict(isStrict(definition.getName(), true)));
- }
- for(AiToolDefinition definition : kernel.getToolDefinitions())
- {
- if(KERNEL_TOOLS.contains(definition.getName()) && (policy == null || policy.allows(definition.getName())))
- {
- byName.put(definition.getName(), new ModelToolDefinition()
- .withName(definition.getName())
- .withDescription(definition.getDescription())
- .withInputSchema(definition.getInputSchema())
- .withStrict(isStrict(definition.getName(), !Boolean.FALSE.equals(definition.getStrict()))));
- }
- }
- if(knowledgeTool != null && knowledgeTool.offered(policy, QContext.getQSession(), turnId))
- {
- byName.put(KnowledgeTool.NAME, knowledgeTool.definition());
- }
- byName.put(PresentResultsTool.NAME, PresentResultsTool.definition(isStrict(PresentResultsTool.NAME, true)));
- if(bulkLoadTools != null && bulkLoadTools.offered())
- {
- for(ModelToolDefinition definition : bulkLoadTools.definitions(policy, toolbox.anyWritableTable()))
- {
- byName.put(definition.getName(), definition);
- }
- }
- return (new ArrayList<>(byName.values()));
- }
-
-
-
- /*******************************************************************************
- ** Strict only when the host names the tool in strictToolNames: the provider
- ** compiles every strict schema of a request into one grammar, which it
- ** refuses when it grows too large.
- *******************************************************************************/
- boolean isStrict(String toolName, boolean strictCapable)
- {
- return (strictCapable && config.getStrictToolNames() != null && config.getStrictToolNames().contains(toolName));
+ return (registry.definitions(new ToolOffer(policy, QContext.getQSession(), turnId)));
}
@@ -520,24 +476,4 @@ public class ContextBuilder
- /*******************************************************************************
- ** Additive optional tool hook; UI API toolbox stays independently maintained.
- *******************************************************************************/
- public ContextBuilder withKnowledgeTool(KnowledgeTool knowledgeTool)
- {
- this.knowledgeTool = knowledgeTool;
- return (this);
- }
-
-
-
- /*******************************************************************************
- ** Fluent setter for the bulk-load tools.
- *******************************************************************************/
- public ContextBuilder withBulkLoadTools(BulkLoadTools bulkLoadTools)
- {
- this.bulkLoadTools = bulkLoadTools;
- return (this);
- }
-
}
diff --git a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/engine/KernelToolFamily.java b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/engine/KernelToolFamily.java
new file mode 100644
index 0000000..9d4e6e1
--- /dev/null
+++ b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/engine/KernelToolFamily.java
@@ -0,0 +1,160 @@
+/*
+ * Copyright 2026 Kingsrook, LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.kingsrook.qbits.assistant.engine;
+
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeSet;
+import com.kingsrook.qbits.assistant.AssistantRuntime;
+import com.kingsrook.qbits.assistant.spi.ModelToolDefinition;
+import com.kingsrook.qbits.assistant.tools.registry.AssistantTool;
+import com.kingsrook.qbits.assistant.tools.registry.ToolCallOutcome;
+import com.kingsrook.qbits.assistant.tools.registry.ToolCallRequest;
+import com.kingsrook.qbits.assistant.tools.registry.ToolCheck;
+import com.kingsrook.qbits.assistant.tools.registry.ToolFamily;
+import com.kingsrook.qbits.assistant.tools.registry.ToolKind;
+import com.kingsrook.qbits.assistant.tools.registry.ToolOffer;
+import com.kingsrook.qbits.assistant.tools.registry.ToolRegistry;
+import com.kingsrook.qqq.backend.core.actions.ai.AiToolCall;
+import com.kingsrook.qqq.backend.core.actions.ai.AiToolDefinition;
+
+
+/*******************************************************************************
+ ** The two tools that stay on the core AI tool kernel
+ ** ({@link ContextBuilder#KERNEL_TOOLS}: navigate and search_help, which read
+ ** no records) as registered tools. The kernel keeps all of their behaviour.
+ **
+ ** This family lives here, and the runtime names it, because nothing under
+ ** the tools package may touch the kernel (ArchitectureTest).
+ *******************************************************************************/
+public class KernelToolFamily implements ToolFamily
+{
+ private static final List CHECKS = List.of(ToolCheck.TOOL_SWITCH, ToolCheck.KERNEL);
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ @Override
+ public List tools(AssistantRuntime runtime)
+ {
+ List tools = new ArrayList<>();
+ for(String name : new TreeSet<>(ContextBuilder.KERNEL_TOOLS))
+ {
+ tools.add(new Tool(runtime, name));
+ }
+ return (tools);
+ }
+
+
+
+ /*******************************************************************************
+ ** One kernel tool.
+ *******************************************************************************/
+ private static final class Tool implements AssistantTool
+ {
+ private final AssistantRuntime runtime;
+ private final String name;
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ private Tool(AssistantRuntime runtime, String name)
+ {
+ this.runtime = runtime;
+ this.name = name;
+ }
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ @Override
+ public String name()
+ {
+ return (name);
+ }
+
+
+
+ /*******************************************************************************
+ ** navigate only builds an /app link for the panel to open (the page
+ ** enforces permission when it opens); search_help reads help text.
+ *******************************************************************************/
+ @Override
+ public ToolKind kind()
+ {
+ return ("navigate".equals(name) ? ToolKind.DISPLAY : ToolKind.READ);
+ }
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ @Override
+ public List checks()
+ {
+ return (CHECKS);
+ }
+
+
+
+ /*******************************************************************************
+ ** The kernel's own definition, when the policy allows the tool.
+ *******************************************************************************/
+ @Override
+ public ModelToolDefinition definition(ToolOffer offer)
+ {
+ Map offered = offer.once(KernelToolFamily.class, () ->
+ {
+ Map byName = new HashMap<>();
+ for(AiToolDefinition definition : runtime.getKernel().getToolDefinitions())
+ {
+ if(ContextBuilder.KERNEL_TOOLS.contains(definition.getName()) && offer.allows(definition.getName()))
+ {
+ byName.put(definition.getName(), new ModelToolDefinition()
+ .withName(definition.getName())
+ .withDescription(definition.getDescription())
+ .withInputSchema(definition.getInputSchema())
+ .withStrict(ToolRegistry.isStrict(runtime.getConfig(), definition.getName(), !Boolean.FALSE.equals(definition.getStrict()))));
+ }
+ }
+ return (byName);
+ });
+ return (offered.get(name));
+ }
+
+
+
+ /*******************************************************************************
+ ** No records are read: the kernel runs the call with the person's session.
+ *******************************************************************************/
+ @Override
+ public ToolCallOutcome execute(ToolCallRequest request)
+ {
+ return (ToolCallOutcome.of(runtime.getKernel().execute(request.session(), new AiToolCall().withId(request.toolCallId()).withName(request.toolName()).withInput(request.input()))));
+ }
+ }
+}
diff --git a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/engine/ToolCallService.java b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/engine/ToolCallService.java
new file mode 100644
index 0000000..db6f45c
--- /dev/null
+++ b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/engine/ToolCallService.java
@@ -0,0 +1,227 @@
+/*
+ * Copyright 2026 Kingsrook, LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.kingsrook.qbits.assistant.engine;
+
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import com.kingsrook.qbits.assistant.model.AssistantPendingAction;
+import com.kingsrook.qbits.assistant.model.AssistantToolCall;
+import com.kingsrook.qbits.assistant.persistence.AssistantStore;
+import com.kingsrook.qbits.assistant.tools.UiApiToolbox;
+import com.kingsrook.qbits.assistant.tools.registry.AssistantTool;
+import com.kingsrook.qbits.assistant.tools.registry.ConfirmedToolCall;
+import com.kingsrook.qbits.assistant.tools.registry.ToolCallOutcome;
+import com.kingsrook.qbits.assistant.tools.registry.ToolCallRequest;
+import com.kingsrook.qbits.assistant.tools.registry.ToolRegistry;
+import com.kingsrook.qqq.backend.core.actions.ai.AiToolOutcome;
+import com.kingsrook.qqq.backend.core.actions.ai.AiToolResult;
+import com.kingsrook.qqq.backend.core.actions.ai.output.AiRedactor;
+import com.kingsrook.qqq.backend.core.exceptions.QException;
+import com.kingsrook.qqq.backend.core.utils.JsonUtils;
+
+
+/*******************************************************************************
+ ** "Run one tool call for this person under this policy, and log it", for
+ ** every caller: the chat's turn engine and the confirm route today.
+ **
+ ** The steps, which a caller takes in this order:
+ ** 1. {@link #refusal}: a tool the person is not offered is refused before
+ ** anything runs.
+ ** 2. {@link #execute} (or {@link #executeConfirmed} for a write the person
+ ** confirmed): the registry's tool runs the call with the person's own
+ ** session, and applies its own checks (the policy's tool switch, the data
+ ** access, what the person may do in the UI API).
+ ** 3. {@link #log} (or {@link #logConfirmed}): one assistant_tool_call row,
+ ** for a refused call as for one that ran.
+ **
+ ** A call never runs as the assistant: the session is always the request's.
+ ** The log row is written through the store, as the same person.
+ *******************************************************************************/
+public class ToolCallService
+{
+ private final ToolRegistry registry;
+ private final AssistantStore store;
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ public ToolCallService(ToolRegistry registry, AssistantStore store)
+ {
+ this.registry = registry;
+ this.store = store;
+ }
+
+
+
+ /*******************************************************************************
+ ** One finished tool call, as the tool-call log keeps it.
+ **
+ ** @param turnId the turn the call was made in
+ ** @param conversationId the turn's conversation
+ ** @param userId the person the call ran as
+ ** @param toolCallId the model's id of the call
+ ** @param toolName the tool called (as the model named it)
+ ** @param clientChannel the channel label the call's requests carried
+ ** @param inputJson the input, already redacted
+ ** @param outcome the outcome's name
+ ** @param summary the one-line summary shown in the panel
+ ** @param result the tool's result
+ ** @param extras the UI API operation's detail; null when the call
+ ** had none (also when it was refused or timed out)
+ *******************************************************************************/
+ public record Finished(Integer turnId, Integer conversationId, String userId, String toolCallId, String toolName, String clientChannel, String inputJson, String outcome,
+ String summary, AiToolResult result, UiApiToolbox.Outcome extras)
+ {
+ }
+
+
+
+ /*******************************************************************************
+ ** The refusal of a call to a tool the person is not offered (a name the
+ ** model made up, or a tool the policy or the host switched off), or null
+ ** when the call may run.
+ *******************************************************************************/
+ public AiToolResult refusal(String toolCallId, String toolName, Set offeredTools)
+ {
+ if(offeredTools.contains(toolName))
+ {
+ return (null);
+ }
+ return (new AiToolResult().withToolCallId(toolCallId).withToolName(toolName).withOutcome(AiToolOutcome.INVALID_INPUT)
+ .withSummary("Unknown tool").withModelContent("Unknown tool \"" + toolName + "\". Use only the tools provided."));
+ }
+
+
+
+ /*******************************************************************************
+ ** Run one call with the request's session. A name the registry does not
+ ** have is refused like one that is not offered.
+ *******************************************************************************/
+ public ToolCallOutcome execute(ToolCallRequest request)
+ {
+ AssistantTool tool = registry.find(request.toolName());
+ if(tool == null)
+ {
+ return (ToolCallOutcome.of(refusal(request.toolCallId(), request.toolName(), Set.of())));
+ }
+ return (tool.execute(request));
+ }
+
+
+
+ /*******************************************************************************
+ ** Run a write the person confirmed, with the confirming request's session.
+ ** A name that proposes no write is refused.
+ *******************************************************************************/
+ public UiApiToolbox.Outcome executeConfirmed(ConfirmedToolCall call)
+ {
+ AssistantTool tool = registry.find(call.toolName());
+ if(tool == null)
+ {
+ return (UiApiToolbox.notAWriteTool(call.toolCallId(), call.toolName(), call.tableName()));
+ }
+ return (tool.executeConfirmed(call));
+ }
+
+
+
+ /*******************************************************************************
+ ** The tool-call log row of a call made in a turn.
+ *******************************************************************************/
+ public void log(Finished call) throws QException
+ {
+ AssistantTool tool = registry.find(call.toolName());
+ AiToolResult result = call.result();
+ UiApiToolbox.Outcome extras = call.extras();
+ store.insert(new AssistantToolCall()
+ .withTurnId(call.turnId())
+ .withConversationId(call.conversationId())
+ .withUserId(call.userId())
+ .withToolCallId(call.toolCallId())
+ .withToolName(call.toolName())
+ .withClientChannel(call.clientChannel())
+ .withInputJson(tool == null ? call.inputJson() : tool.forLog(call.inputJson()))
+ .withOutcome(call.outcome())
+ .withPermissionCheck(result.getPermissionCheck())
+ .withTableName(result.getTableName())
+ .withProcessName(result.getProcessName())
+ .withWidgetName(result.getWidgetName())
+ .withRowCount(result.getRowCount())
+ .withDurationMs(result.getDurationMs())
+ .withResultSummary(call.summary())
+ .withResultJson(tool == null ? result.getModelContent() : tool.forLog(result.getModelContent()))
+ .withRedactionsJson(result.getRedactions() == null || result.getRedactions().isEmpty() ? null : JsonUtils.toJson(result.getRedactions()))
+ .withUiApiOperation(extras == null ? (tool == null ? null : tool.operationWithoutDetail()) : extras.getOperation())
+ .withHttpStatus(extras == null ? null : extras.getHttpStatus())
+ .withAuditHref(extras == null ? null : extras.getAuditHref())
+ .withPendingActionId(extras == null ? null : extras.getPendingActionId()));
+ }
+
+
+
+ /*******************************************************************************
+ ** The tool-call log row of a confirmed write: the pending action's call,
+ ** run by the person who confirmed it.
+ *******************************************************************************/
+ public void logConfirmed(AssistantPendingAction action, String userId, UiApiToolbox.Outcome outcome) throws QException
+ {
+ AiToolResult result = outcome.getResult();
+ store.insert(new AssistantToolCall()
+ .withTurnId(action.getTurnId())
+ .withClientChannel("assistant" + (action.getTurnId() == null ? "" : "/" + action.getTurnId()))
+ .withConversationId(action.getConversationId())
+ .withUserId(userId)
+ .withToolCallId(action.getToolCallId())
+ .withToolName(action.getToolName())
+ .withInputJson(truncate(new AiRedactor(List.of()).redact(action.getParamsJson() == null ? "{}" : action.getParamsJson(), new HashSet<>()), 4000))
+ .withOutcome(result.getOutcome() == null ? AiToolOutcome.ERROR.name() : result.getOutcome().name())
+ .withPermissionCheck(result.getPermissionCheck())
+ .withTableName(action.getTableName())
+ .withRowCount(result.getRowCount())
+ .withDurationMs(result.getDurationMs())
+ .withResultSummary(truncate("Confirmed: " + result.getSummary(), 500))
+ .withResultJson(result.getModelContent())
+ .withUiApiOperation(outcome.getOperation())
+ .withHttpStatus(outcome.getHttpStatus())
+ .withAuditHref(outcome.getAuditHref())
+ .withPendingActionId(action.getId()));
+ }
+
+
+
+ /*******************************************************************************
+ ** Getter for registry
+ *******************************************************************************/
+ public ToolRegistry getRegistry()
+ {
+ return (registry);
+ }
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ private static String truncate(String value, int max)
+ {
+ return (value == null || value.length() <= max ? value : value.substring(0, max));
+ }
+}
diff --git a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/engine/TurnEngine.java b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/engine/TurnEngine.java
index ef45d72..fec4f46 100644
--- a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/engine/TurnEngine.java
+++ b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/engine/TurnEngine.java
@@ -31,6 +31,7 @@ import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CancellationException;
+import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
@@ -41,6 +42,7 @@ import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLongArray;
import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Supplier;
import com.kingsrook.qbits.assistant.AssistantQBitConfig;
import com.kingsrook.qbits.assistant.AssistantRuntime;
import com.kingsrook.qbits.assistant.attachments.AttachmentService;
@@ -48,7 +50,6 @@ import com.kingsrook.qbits.assistant.model.AssistantConversation;
import com.kingsrook.qbits.assistant.model.AssistantMessage;
import com.kingsrook.qbits.assistant.model.AssistantPendingAction;
import com.kingsrook.qbits.assistant.model.AssistantScheduledTask;
-import com.kingsrook.qbits.assistant.model.AssistantToolCall;
import com.kingsrook.qbits.assistant.model.AssistantTurn;
import com.kingsrook.qbits.assistant.persistence.AssistantStore;
import com.kingsrook.qbits.assistant.persistence.TranscriptRedactor;
@@ -65,14 +66,16 @@ import com.kingsrook.qbits.assistant.spi.ModelRequest;
import com.kingsrook.qbits.assistant.spi.ModelResponse;
import com.kingsrook.qbits.assistant.spi.ModelUsage;
import com.kingsrook.qbits.assistant.spi.StreamHandle;
-import com.kingsrook.qbits.assistant.tools.KnowledgeTool;
import com.kingsrook.qbits.assistant.tools.PresentResultsTool;
import com.kingsrook.qbits.assistant.tools.UiApiToolbox;
-import com.kingsrook.qbits.assistant.tools.bulkload.BulkLoadTools;
+import com.kingsrook.qbits.assistant.tools.registry.ToolCallOutcome;
+import com.kingsrook.qbits.assistant.tools.registry.ToolCallRequest;
+import com.kingsrook.qbits.assistant.tools.registry.ToolCallScope;
import com.kingsrook.qqq.backend.core.actions.ai.AiRecordRef;
import com.kingsrook.qqq.backend.core.actions.ai.AiToolCall;
import com.kingsrook.qqq.backend.core.actions.ai.AiToolOutcome;
import com.kingsrook.qqq.backend.core.actions.ai.AiToolResult;
+import com.kingsrook.qqq.backend.core.actions.ai.DataClassification;
import com.kingsrook.qqq.backend.core.actions.ai.output.AiRedactor;
import com.kingsrook.qqq.backend.core.context.CapturedContext;
import com.kingsrook.qqq.backend.core.context.QContext;
@@ -121,7 +124,7 @@ public class TurnEngine implements Runnable
private static final Set ERROR_OUTCOMES = Set.of(AiToolOutcome.DENIED, AiToolOutcome.NOT_FOUND, AiToolOutcome.INVALID_INPUT, AiToolOutcome.ERROR);
- private final PresentResultsTool presenter;
+ private final ToolCallScope toolScope = new TurnToolScope();
private final AssistantRuntime runtime;
private final AssistantQBitConfig config;
private final AssistantStore store;
@@ -189,7 +192,6 @@ public class TurnEngine implements Runnable
AssistantTurn turn, String userText, Map pageContext, PolicyService.EffectivePolicy policy, RunningTurn running, String softBudgetNotice)
{
this.runtime = runtime;
- this.presenter = new PresentResultsTool(runtime.getQInstance());
this.config = runtime.getConfig();
this.store = runtime.getStore();
this.session = session;
@@ -1077,10 +1079,10 @@ public class TurnEngine implements Runnable
phase("tool", use.getToolName());
emit(TurnEvent.TOOL_START, map("messageId", assistantMessage.getId(), "toolCallId", use.getToolUseId(), "name", use.getToolName(), "summary", ToolLabels.running(use.getToolName())));
- if(!allowedTools.contains(use.getToolName()))
+ AiToolResult refusal = runtime.getToolCalls().refusal(use.getToolUseId(), use.getToolName(), allowedTools);
+ if(refusal != null)
{
- results[i] = new AiToolResult().withToolCallId(use.getToolUseId()).withToolName(use.getToolName()).withOutcome(AiToolOutcome.INVALID_INPUT)
- .withSummary("Unknown tool").withModelContent("Unknown tool \"" + use.getToolName() + "\". Use only the tools provided.");
+ results[i] = refusal;
continue;
}
@@ -1104,45 +1106,13 @@ public class TurnEngine implements Runnable
{
if(!running.isCancelled())
{
- if(PresentResultsTool.NAME.equals(call.getName()))
- {
- results[index] = presenter.execute(toolContext(), call.getId(), call.getInput());
- }
- else if(ContextBuilder.KERNEL_TOOLS.contains(call.getName()))
- {
- ///////////////////////////////////////////////////
- // navigate / search_help: no records are read //
- ///////////////////////////////////////////////////
- results[index] = runtime.getKernel().execute(session, call);
- }
- else
- {
- ///////////////////////////////////////////////////////////
- // data tools: a /qqq/v1 call with the user's session //
- ///////////////////////////////////////////////////////////
- QContext.init(runtime.getQInstance(), session);
- try
- {
- if(KnowledgeTool.NAME.equals(call.getName()))
- {
- results[index] = runtime.getKnowledgeTool().execute(toolContext(), call.getId(), call.getInput(), classification.getClassification(), conversation.getId(), turn.getId());
- }
- else if(BulkLoadTools.NAMES.contains(call.getName()))
- {
- outcomes[index] = runtime.getBulkLoadTools().execute(toolContext(), call.getId(), call.getName(), call.getInput(), hipaaRoute());
- results[index] = outcomes[index].getResult();
- }
- else
- {
- outcomes[index] = runtime.getToolbox().execute(toolContext(), call.getId(), call.getName(), call.getInput());
- results[index] = outcomes[index].getResult();
- }
- }
- finally
- {
- QContext.clear();
- }
- }
+ ///////////////////////////////////////////////////////////////
+ // the registry's tool runs the call, as the user: with the //
+ // user's own session, policy and pending-action store //
+ ///////////////////////////////////////////////////////////////
+ ToolCallOutcome done = runtime.getToolCalls().execute(new ToolCallRequest(session, toolContext(), call.getId(), call.getName(), call.getInput(), toolScope));
+ outcomes[index] = done.extras();
+ results[index] = done.result();
}
}
finally
@@ -1338,28 +1308,8 @@ public class TurnEngine implements Runnable
try
{
String inputJson = redactInput(PresentResultsTool.NAME.equals(use.getToolName()) ? (presentation ? result.getRender() : Map.of()) : use.getInput());
- store.insert(new AssistantToolCall()
- .withTurnId(turn.getId())
- .withConversationId(conversation.getId())
- .withUserId(userId)
- .withToolCallId(use.getToolUseId())
- .withToolName(use.getToolName())
- .withClientChannel(scheduledTask == null ? "assistant/" + turn.getId() : "assistant/scheduled/" + scheduledTask.getId())
- .withInputJson(BulkLoadTools.NAMES.contains(use.getToolName()) ? BulkLoadTools.forLog(inputJson) : inputJson)
- .withOutcome(outcome)
- .withPermissionCheck(result.getPermissionCheck())
- .withTableName(result.getTableName())
- .withProcessName(result.getProcessName())
- .withWidgetName(result.getWidgetName())
- .withRowCount(result.getRowCount())
- .withDurationMs(result.getDurationMs())
- .withResultSummary(summary)
- .withResultJson(BulkLoadTools.NAMES.contains(use.getToolName()) ? BulkLoadTools.forLog(result.getModelContent()) : result.getModelContent())
- .withRedactionsJson(result.getRedactions() == null || result.getRedactions().isEmpty() ? null : JsonUtils.toJson(result.getRedactions()))
- .withUiApiOperation(extras == null ? ("search_knowledge".equals(use.getToolName()) ? "GET /table/{tableName}/{primaryKey}" : null) : extras.getOperation())
- .withHttpStatus(extras == null ? null : extras.getHttpStatus())
- .withAuditHref(extras == null ? null : extras.getAuditHref())
- .withPendingActionId(extras == null ? null : extras.getPendingActionId()));
+ runtime.getToolCalls().log(new ToolCallService.Finished(turn.getId(), conversation.getId(), userId, use.getToolUseId(), use.getToolName(),
+ scheduledTask == null ? "assistant/" + turn.getId() : "assistant/scheduled/" + scheduledTask.getId(), inputJson, outcome, summary, result, extras));
}
catch(QException e)
{
@@ -1370,17 +1320,68 @@ public class TurnEngine implements Runnable
/*******************************************************************************
- ** Where a data tool runs: the user's own session (by its UUID, for the UI
- ** API), the gateway, the policy's tool settings, and the pending-action
- ** store for writes that need confirmation.
+ ** What a tool call may know about this turn. Every value is the server's:
+ ** the classification reached so far, the turn's rows, and the turn's
+ ** provider's HIPAA eligibility (never anything the model says).
*******************************************************************************/
- private boolean hipaaRoute()
+ private final class TurnToolScope implements ToolCallScope
{
- ///////////////////////////////////////////////////////////////////////
- // the X-QQQ-AI-Route the bulk-load tools send: the turn's provider's //
- // HIPAA eligibility, never anything the model says //
- ///////////////////////////////////////////////////////////////////////
- return (active != null && active.row() != null && Boolean.TRUE.equals(active.row().getHipaaEligible()));
+ private final Map, Object> state = new ConcurrentHashMap<>();
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ @Override
+ public DataClassification classification()
+ {
+ return (classification.getClassification());
+ }
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ @Override
+ public Integer conversationId()
+ {
+ return (conversation.getId());
+ }
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ @Override
+ public Integer turnId()
+ {
+ return (turn.getId());
+ }
+
+
+
+ /*******************************************************************************
+ ** The X-QQQ-AI-Route the bulk-load tools send.
+ *******************************************************************************/
+ @Override
+ public boolean hipaaRoute()
+ {
+ return (active != null && active.row() != null && Boolean.TRUE.equals(active.row().getHipaaEligible()));
+ }
+
+
+
+ /*******************************************************************************
+ ** State a tool keeps for this turn (the presenter).
+ *******************************************************************************/
+ @Override
+ public T state(Class type, Supplier create)
+ {
+ return (type.cast(state.computeIfAbsent(type, key -> create.get())));
+ }
}
diff --git a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/KnowledgeToolFamily.java b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/KnowledgeToolFamily.java
new file mode 100644
index 0000000..8731e3a
--- /dev/null
+++ b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/KnowledgeToolFamily.java
@@ -0,0 +1,138 @@
+/*
+ * Copyright 2026 Kingsrook, LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.kingsrook.qbits.assistant.tools;
+
+
+import java.util.List;
+import com.kingsrook.qbits.assistant.AssistantRuntime;
+import com.kingsrook.qbits.assistant.spi.ModelToolDefinition;
+import com.kingsrook.qbits.assistant.tools.registry.AssistantTool;
+import com.kingsrook.qbits.assistant.tools.registry.ToolCallOutcome;
+import com.kingsrook.qbits.assistant.tools.registry.ToolCallRequest;
+import com.kingsrook.qbits.assistant.tools.registry.ToolCheck;
+import com.kingsrook.qbits.assistant.tools.registry.ToolFamily;
+import com.kingsrook.qbits.assistant.tools.registry.ToolKind;
+import com.kingsrook.qbits.assistant.tools.registry.ToolOffer;
+
+
+/*******************************************************************************
+ ** search_knowledge ({@link KnowledgeTool}) as a registered tool.
+ ** KnowledgeTool keeps all of its behaviour.
+ *******************************************************************************/
+public class KnowledgeToolFamily implements ToolFamily
+{
+ private static final List CHECKS = List.of(ToolCheck.TOOL_SWITCH, ToolCheck.DATA_ACCESS, ToolCheck.CLASSIFICATION, ToolCheck.PERSON_PERMISSIONS, ToolCheck.FIELD_EXPOSURE);
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ @Override
+ public List tools(AssistantRuntime runtime)
+ {
+ return (List.of(new Tool(runtime)));
+ }
+
+
+
+ /*******************************************************************************
+ ** The knowledge search.
+ *******************************************************************************/
+ private static final class Tool implements AssistantTool
+ {
+ private final AssistantRuntime runtime;
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ private Tool(AssistantRuntime runtime)
+ {
+ this.runtime = runtime;
+ }
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ @Override
+ public String name()
+ {
+ return (KnowledgeTool.NAME);
+ }
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ @Override
+ public ToolKind kind()
+ {
+ return (ToolKind.READ);
+ }
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ @Override
+ public List checks()
+ {
+ return (CHECKS);
+ }
+
+
+
+ /*******************************************************************************
+ ** Offered to a person who can read an enabled source's table on screen.
+ *******************************************************************************/
+ @Override
+ public ModelToolDefinition definition(ToolOffer offer)
+ {
+ KnowledgeTool tool = runtime.getKnowledgeTool();
+ return (tool.offered(offer.policy(), offer.session(), offer.turnId()) ? tool.definition() : null);
+ }
+
+
+
+ /*******************************************************************************
+ ** Every hit is read again through the UI API with the person's session.
+ *******************************************************************************/
+ @Override
+ public ToolCallOutcome execute(ToolCallRequest request)
+ {
+ return (request.inPersonsContext(runtime.getQInstance(), () -> ToolCallOutcome.of(runtime.getKnowledgeTool().execute(request.context(), request.toolCallId(), request.input(),
+ request.scope().classification(), request.scope().conversationId(), request.scope().turnId()))));
+ }
+
+
+
+ /*******************************************************************************
+ ** The operation each hit is read with.
+ *******************************************************************************/
+ @Override
+ public String operationWithoutDetail()
+ {
+ return ("GET /table/{tableName}/{primaryKey}");
+ }
+ }
+}
diff --git a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/PresentResultsToolFamily.java b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/PresentResultsToolFamily.java
new file mode 100644
index 0000000..ae55652
--- /dev/null
+++ b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/PresentResultsToolFamily.java
@@ -0,0 +1,131 @@
+/*
+ * Copyright 2026 Kingsrook, LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.kingsrook.qbits.assistant.tools;
+
+
+import java.util.List;
+import com.kingsrook.qbits.assistant.AssistantRuntime;
+import com.kingsrook.qbits.assistant.spi.ModelToolDefinition;
+import com.kingsrook.qbits.assistant.tools.registry.AssistantTool;
+import com.kingsrook.qbits.assistant.tools.registry.ToolCallOutcome;
+import com.kingsrook.qbits.assistant.tools.registry.ToolCallRequest;
+import com.kingsrook.qbits.assistant.tools.registry.ToolCheck;
+import com.kingsrook.qbits.assistant.tools.registry.ToolFamily;
+import com.kingsrook.qbits.assistant.tools.registry.ToolKind;
+import com.kingsrook.qbits.assistant.tools.registry.ToolOffer;
+import com.kingsrook.qbits.assistant.tools.registry.ToolRegistry;
+
+
+/*******************************************************************************
+ ** present_results ({@link PresentResultsTool}) as a registered tool.
+ ** PresentResultsTool keeps all of its behaviour.
+ *******************************************************************************/
+public class PresentResultsToolFamily implements ToolFamily
+{
+ ///////////////////////////////////////////////////////////////////////////
+ // the tables a presentation links to are checked against the metadata //
+ // the person's own session is given //
+ ///////////////////////////////////////////////////////////////////////////
+ private static final List CHECKS = List.of(ToolCheck.PERSON_PERMISSIONS);
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ @Override
+ public List tools(AssistantRuntime runtime)
+ {
+ return (List.of(new Tool(runtime)));
+ }
+
+
+
+ /*******************************************************************************
+ ** The presentation.
+ *******************************************************************************/
+ private static final class Tool implements AssistantTool
+ {
+ private final AssistantRuntime runtime;
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ private Tool(AssistantRuntime runtime)
+ {
+ this.runtime = runtime;
+ }
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ @Override
+ public String name()
+ {
+ return (PresentResultsTool.NAME);
+ }
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ @Override
+ public ToolKind kind()
+ {
+ return (ToolKind.DISPLAY);
+ }
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ @Override
+ public List checks()
+ {
+ return (CHECKS);
+ }
+
+
+
+ /*******************************************************************************
+ ** Always offered: the policy's allowed tools do not switch it off.
+ *******************************************************************************/
+ @Override
+ public ModelToolDefinition definition(ToolOffer offer)
+ {
+ return (PresentResultsTool.definition(ToolRegistry.isStrict(runtime.getConfig(), PresentResultsTool.NAME, true)));
+ }
+
+
+
+ /*******************************************************************************
+ ** One presenter per scope (in chat: per turn), shared by its calls.
+ *******************************************************************************/
+ @Override
+ public ToolCallOutcome execute(ToolCallRequest request)
+ {
+ PresentResultsTool presenter = request.scope().state(PresentResultsTool.class, () -> new PresentResultsTool(runtime.getQInstance()));
+ return (ToolCallOutcome.of(presenter.execute(request.context(), request.toolCallId(), request.input())));
+ }
+ }
+}
diff --git a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/UiApiToolFamily.java b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/UiApiToolFamily.java
new file mode 100644
index 0000000..9182a68
--- /dev/null
+++ b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/UiApiToolFamily.java
@@ -0,0 +1,200 @@
+/*
+ * Copyright 2026 Kingsrook, LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.kingsrook.qbits.assistant.tools;
+
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeSet;
+import com.kingsrook.qbits.assistant.AssistantRuntime;
+import com.kingsrook.qbits.assistant.spi.ModelToolDefinition;
+import com.kingsrook.qbits.assistant.tools.registry.AssistantTool;
+import com.kingsrook.qbits.assistant.tools.registry.ConfirmedToolCall;
+import com.kingsrook.qbits.assistant.tools.registry.ToolCallOutcome;
+import com.kingsrook.qbits.assistant.tools.registry.ToolCallRequest;
+import com.kingsrook.qbits.assistant.tools.registry.ToolCheck;
+import com.kingsrook.qbits.assistant.tools.registry.ToolFamily;
+import com.kingsrook.qbits.assistant.tools.registry.ToolKind;
+import com.kingsrook.qbits.assistant.tools.registry.ToolOffer;
+import com.kingsrook.qbits.assistant.tools.registry.ToolRegistry;
+import static com.kingsrook.qbits.assistant.tools.registry.ToolCheck.CONFIRMATION;
+import static com.kingsrook.qbits.assistant.tools.registry.ToolCheck.DATA_ACCESS;
+import static com.kingsrook.qbits.assistant.tools.registry.ToolCheck.FIELD_EXPOSURE;
+import static com.kingsrook.qbits.assistant.tools.registry.ToolCheck.PERSON_PERMISSIONS;
+import static com.kingsrook.qbits.assistant.tools.registry.ToolCheck.SPECD_OPERATION;
+import static com.kingsrook.qbits.assistant.tools.registry.ToolCheck.TOOL_SWITCH;
+
+
+/*******************************************************************************
+ ** The UI API data tools ({@link UiApiToolbox}, with the analysis tools it
+ ** carries) as registered tools: one per operation the toolbox knows. The
+ ** toolbox keeps all of their behaviour; this class only says which tools
+ ** there are and hands each call to it.
+ *******************************************************************************/
+public class UiApiToolFamily implements ToolFamily
+{
+ private static final List READ_THEN_EXPOSE = List.of(TOOL_SWITCH, SPECD_OPERATION, DATA_ACCESS, PERSON_PERMISSIONS, FIELD_EXPOSURE);
+ private static final List EXPOSE_THEN_READ = List.of(TOOL_SWITCH, SPECD_OPERATION, DATA_ACCESS, FIELD_EXPOSURE, PERSON_PERMISSIONS);
+ private static final List PROPOSED_WRITE = List.of(TOOL_SWITCH, SPECD_OPERATION, DATA_ACCESS, FIELD_EXPOSURE, CONFIRMATION, PERSON_PERMISSIONS);
+
+ ///////////////////////////////////////////////////////////////////////////
+ // the checks of each tool, in the order the toolbox first applies them //
+ ///////////////////////////////////////////////////////////////////////////
+ private static final Map> CHECKS = Map.ofEntries(
+ Map.entry(UiApiToolbox.LIST_TABLES, List.of(TOOL_SWITCH, SPECD_OPERATION, PERSON_PERMISSIONS, DATA_ACCESS)),
+ Map.entry(UiApiToolbox.DESCRIBE_TABLE, READ_THEN_EXPOSE),
+ Map.entry(UiApiToolbox.QUERY_RECORDS, EXPOSE_THEN_READ),
+ Map.entry(UiApiToolbox.COUNT_RECORDS, EXPOSE_THEN_READ),
+ Map.entry(UiApiToolbox.GET_RECORD, READ_THEN_EXPOSE),
+ Map.entry(UiApiToolbox.SEARCH_RECORDS, EXPOSE_THEN_READ),
+ Map.entry(UiApiToolbox.RENDER_WIDGET, List.of(TOOL_SWITCH, SPECD_OPERATION, PERSON_PERMISSIONS)),
+ Map.entry(UiApiToolbox.RECORD_HISTORY, READ_THEN_EXPOSE),
+ Map.entry(UiApiToolbox.CREATE_RECORD, PROPOSED_WRITE),
+ Map.entry(UiApiToolbox.UPDATE_RECORD, PROPOSED_WRITE),
+ Map.entry(UiApiToolbox.DELETE_RECORD, PROPOSED_WRITE),
+ Map.entry(AnalysisTools.ANALYZE_RECORDS, EXPOSE_THEN_READ),
+ Map.entry(AnalysisTools.SAVE_ANALYSIS, List.of(TOOL_SWITCH, SPECD_OPERATION, DATA_ACCESS, FIELD_EXPOSURE, PERSON_PERMISSIONS, CONFIRMATION)));
+
+
+
+ /*******************************************************************************
+ ** One tool per operation of the toolbox (the optional extensions' included;
+ ** whether a host has them is decided when the tool is offered).
+ *******************************************************************************/
+ @Override
+ public List tools(AssistantRuntime runtime)
+ {
+ TreeSet names = new TreeSet<>(UiApiToolbox.OPERATIONS.keySet());
+ names.addAll(UiApiToolbox.EXTENSION_OPERATIONS.keySet());
+
+ List tools = new ArrayList<>();
+ for(String name : names)
+ {
+ tools.add(new Tool(runtime, name));
+ }
+ return (tools);
+ }
+
+
+
+ /*******************************************************************************
+ ** One UI API data tool.
+ *******************************************************************************/
+ private static final class Tool implements AssistantTool
+ {
+ private final AssistantRuntime runtime;
+ private final String name;
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ private Tool(AssistantRuntime runtime, String name)
+ {
+ this.runtime = runtime;
+ this.name = name;
+ }
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ @Override
+ public String name()
+ {
+ return (name);
+ }
+
+
+
+ /*******************************************************************************
+ ** The record writes and save_analysis propose; everything else reads.
+ *******************************************************************************/
+ @Override
+ public ToolKind kind()
+ {
+ return (UiApiToolbox.WRITE_TOOLS.contains(name) || AnalysisTools.WRITE_TOOLS.contains(name) ? ToolKind.PROPOSE : ToolKind.READ);
+ }
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ @Override
+ public List checks()
+ {
+ return (CHECKS.get(name));
+ }
+
+
+
+ /*******************************************************************************
+ ** The toolbox's definitions for the policy are built once per offer (write
+ ** tools only when some table allows writes), as they were for the chat's
+ ** tool list.
+ *******************************************************************************/
+ @Override
+ public ModelToolDefinition definition(ToolOffer offer)
+ {
+ Map offered = offer.once(UiApiToolFamily.class, () ->
+ {
+ Map byName = new HashMap<>();
+ UiApiToolbox toolbox = runtime.getToolbox();
+ for(ModelToolDefinition definition : toolbox.definitions(offer.policy(), toolbox.anyWritableTable()))
+ {
+ byName.put(definition.getName(), definition.withStrict(ToolRegistry.isStrict(runtime.getConfig(), definition.getName(), true)));
+ }
+ return (byName);
+ });
+ return (offered.get(name));
+ }
+
+
+
+ /*******************************************************************************
+ ** A /qqq/v1 call with the person's session.
+ *******************************************************************************/
+ @Override
+ public ToolCallOutcome execute(ToolCallRequest request)
+ {
+ return (request.inPersonsContext(runtime.getQInstance(), () ->
+ ToolCallOutcome.of(runtime.getToolbox().execute(request.context(), request.toolCallId(), request.toolName(), request.input()))));
+ }
+
+
+
+ /*******************************************************************************
+ ** A saved analysis and its schedule are created as, and emailed to, the
+ ** person who confirms; every other name goes to the record write, which
+ ** refuses a tool that is not one.
+ *******************************************************************************/
+ @Override
+ public UiApiToolbox.Outcome executeConfirmed(ConfirmedToolCall call)
+ {
+ if(AnalysisTools.WRITE_TOOLS.contains(name))
+ {
+ return (runtime.getToolbox().executeConfirmedAnalysis(call.context(), call.toolCallId(), call.toolName(), call.values(), call.userId()));
+ }
+ return (runtime.getToolbox().executeConfirmedWrite(call.context(), call.toolCallId(), call.toolName(), call.tableName(), call.recordId(), call.values()));
+ }
+ }
+}
diff --git a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/UiApiToolbox.java b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/UiApiToolbox.java
index 8d2969f..cfb0abe 100644
--- a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/UiApiToolbox.java
+++ b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/UiApiToolbox.java
@@ -581,6 +581,22 @@ public class UiApiToolbox
+ /*******************************************************************************
+ ** The refusal of a confirmation whose tool proposes no write: what
+ ** executeConfirmedWrite answers for such a name (the tool registry gives it
+ ** for a tool, or a name, that has no confirmed write of its own).
+ *******************************************************************************/
+ public static Outcome notAWriteTool(String toolCallId, String toolName, String tableName)
+ {
+ String message = "Not a write tool: " + toolName;
+ AiToolResult result = new AiToolResult().withToolCallId(toolCallId).withToolName(toolName).withTableName(tableName)
+ .withOutcome(AiToolOutcome.INVALID_INPUT).withSummary(shortText(message)).withModelContent(message);
+ result.setDurationMs(0L);
+ return (new Outcome(result));
+ }
+
+
+
/*******************************************************************************
** Run a confirmed save_analysis (from a pending action) with the confirming
** user's session: the saved analysis and, when one was proposed, its
diff --git a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/bulkload/BulkLoadToolFamily.java b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/bulkload/BulkLoadToolFamily.java
new file mode 100644
index 0000000..8aba9fc
--- /dev/null
+++ b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/bulkload/BulkLoadToolFamily.java
@@ -0,0 +1,185 @@
+/*
+ * Copyright 2026 Kingsrook, LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.kingsrook.qbits.assistant.tools.bulkload;
+
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import com.kingsrook.qbits.assistant.AssistantRuntime;
+import com.kingsrook.qbits.assistant.spi.ModelToolDefinition;
+import com.kingsrook.qbits.assistant.tools.UiApiToolbox;
+import com.kingsrook.qbits.assistant.tools.registry.AssistantTool;
+import com.kingsrook.qbits.assistant.tools.registry.ConfirmedToolCall;
+import com.kingsrook.qbits.assistant.tools.registry.ToolCallOutcome;
+import com.kingsrook.qbits.assistant.tools.registry.ToolCallRequest;
+import com.kingsrook.qbits.assistant.tools.registry.ToolCheck;
+import com.kingsrook.qbits.assistant.tools.registry.ToolFamily;
+import com.kingsrook.qbits.assistant.tools.registry.ToolKind;
+import com.kingsrook.qbits.assistant.tools.registry.ToolOffer;
+
+
+/*******************************************************************************
+ ** The bulk-load tools ({@link BulkLoadTools}) as registered tools.
+ ** BulkLoadTools keeps all of their behaviour; this class only says which
+ ** tools there are and hands each call to it.
+ *******************************************************************************/
+public class BulkLoadToolFamily implements ToolFamily
+{
+ private static final List NAMES = List.of(BulkLoadTools.LIST, BulkLoadTools.DESCRIBE, BulkLoadTools.ATTACH, BulkLoadTools.SUGGEST, BulkLoadTools.DRY_RUN,
+ BulkLoadTools.SAVE, BulkLoadTools.RUN);
+
+ private static final List READ_CHECKS = List.of(ToolCheck.TOOL_SWITCH, ToolCheck.DATA_ACCESS, ToolCheck.PERSON_PERMISSIONS);
+ private static final List PROPOSE_CHECKS = List.of(ToolCheck.TOOL_SWITCH, ToolCheck.DATA_ACCESS, ToolCheck.CONFIRMATION, ToolCheck.PERSON_PERMISSIONS);
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ @Override
+ public List tools(AssistantRuntime runtime)
+ {
+ List tools = new ArrayList<>();
+ for(String name : NAMES)
+ {
+ tools.add(new Tool(runtime, name));
+ }
+ return (tools);
+ }
+
+
+
+ /*******************************************************************************
+ ** One bulk-load tool.
+ *******************************************************************************/
+ private static final class Tool implements AssistantTool
+ {
+ private final AssistantRuntime runtime;
+ private final String name;
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ private Tool(AssistantRuntime runtime, String name)
+ {
+ this.runtime = runtime;
+ this.name = name;
+ }
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ @Override
+ public String name()
+ {
+ return (name);
+ }
+
+
+
+ /*******************************************************************************
+ ** Saving a profile and running a load propose; the rest read.
+ *******************************************************************************/
+ @Override
+ public ToolKind kind()
+ {
+ return (BulkLoadTools.WRITE_TOOLS.contains(name) ? ToolKind.PROPOSE : ToolKind.READ);
+ }
+
+
+
+ /*******************************************************************************
+ **
+ *******************************************************************************/
+ @Override
+ public List checks()
+ {
+ return (BulkLoadTools.WRITE_TOOLS.contains(name) ? PROPOSE_CHECKS : READ_CHECKS);
+ }
+
+
+
+ /*******************************************************************************
+ ** Offered where the instance has saved bulk load profiles; the definitions
+ ** for the policy are built once per offer (the write tools only when some
+ ** table allows writes).
+ *******************************************************************************/
+ @Override
+ public ModelToolDefinition definition(ToolOffer offer)
+ {
+ Map offered = offer.once(BulkLoadToolFamily.class, () ->
+ {
+ Map byName = new HashMap<>();
+ BulkLoadTools tools = runtime.getBulkLoadTools();
+ if(tools.offered())
+ {
+ for(ModelToolDefinition definition : tools.definitions(offer.policy(), runtime.getToolbox().anyWritableTable()))
+ {
+ byName.put(definition.getName(), definition);
+ }
+ }
+ return (byName);
+ });
+ return (offered.get(name));
+ }
+
+
+
+ /*******************************************************************************
+ ** /qqq/v1 calls with the person's session; the X-QQQ-AI-Route they send is
+ ** the scope's (the turn's provider's HIPAA eligibility), never the model's.
+ *******************************************************************************/
+ @Override
+ public ToolCallOutcome execute(ToolCallRequest request)
+ {
+ return (request.inPersonsContext(runtime.getQInstance(), () ->
+ ToolCallOutcome.of(runtime.getBulkLoadTools().execute(request.context(), request.toolCallId(), request.toolName(), request.input(), request.scope().hipaaRoute()))));
+ }
+
+
+
+ /*******************************************************************************
+ ** A confirmed save or run; the tools that propose nothing refuse.
+ *******************************************************************************/
+ @Override
+ public UiApiToolbox.Outcome executeConfirmed(ConfirmedToolCall call)
+ {
+ if(!BulkLoadTools.WRITE_TOOLS.contains(name))
+ {
+ return (AssistantTool.super.executeConfirmed(call));
+ }
+ return (runtime.getBulkLoadTools().executeConfirmed(call.context(), call.toolCallId(), call.toolName(), call.tableName(), call.values()));
+ }
+
+
+
+ /*******************************************************************************
+ ** The log keeps the profile and the counts, not the file's values.
+ *******************************************************************************/
+ @Override
+ public String forLog(String json)
+ {
+ return (BulkLoadTools.forLog(json));
+ }
+ }
+}
diff --git a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/AssistantTool.java b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/AssistantTool.java
new file mode 100644
index 0000000..40ce707
--- /dev/null
+++ b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/AssistantTool.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2026 Kingsrook, LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.kingsrook.qbits.assistant.tools.registry;
+
+
+import java.util.List;
+import com.kingsrook.qbits.assistant.spi.ModelToolDefinition;
+import com.kingsrook.qbits.assistant.tools.UiApiToolbox;
+
+
+/*******************************************************************************
+ ** One tool of the assistant, registered once in the {@link ToolRegistry}:
+ ** its name, its kind, the checks it runs, its definition (description and
+ ** input schema) and how one call of it is run.
+ **
+ ** A tool call always runs as the signed-in person: with that person's own
+ ** session, current permissions and policy, never as the assistant.
+ *******************************************************************************/
+public interface AssistantTool
+{
+ /*******************************************************************************
+ ** The name the model calls the tool by; unique in the registry.
+ *******************************************************************************/
+ String name();
+
+
+
+ /*******************************************************************************
+ ** Whether the tool reads, proposes a write or displays.
+ *******************************************************************************/
+ ToolKind kind();
+
+
+
+ /*******************************************************************************
+ ** The checks one call of this tool goes through, in the order each first
+ ** applies.
+ *******************************************************************************/
+ List checks();
+
+
+
+ /*******************************************************************************
+ ** The definition (name, description, input schema, strict flag) this person
+ ** is offered now under this policy, or null when the tool is not offered:
+ ** the host lacks what it needs, the policy switched it off, or the person
+ ** may not use it.
+ *******************************************************************************/
+ ModelToolDefinition definition(ToolOffer offer);
+
+
+
+ /*******************************************************************************
+ ** Run one call as the person of the request. Refusals and bad input are
+ ** results, not exceptions.
+ *******************************************************************************/
+ ToolCallOutcome execute(ToolCallRequest request);
+
+
+
+ /*******************************************************************************
+ ** Run a write this tool proposed, now that the person confirmed it, with the
+ ** confirming request's session. A tool that proposes nothing refuses, as the
+ ** confirm route always has for such a name.
+ *******************************************************************************/
+ default UiApiToolbox.Outcome executeConfirmed(ConfirmedToolCall call)
+ {
+ return (UiApiToolbox.notAWriteTool(call.toolCallId(), call.toolName(), call.tableName()));
+ }
+
+
+
+ /*******************************************************************************
+ ** The input or result JSON as the tool-call log may keep it (a tool whose
+ ** JSON can carry more than the log should hold reduces it here).
+ *******************************************************************************/
+ default String forLog(String json)
+ {
+ return (json);
+ }
+
+
+
+ /*******************************************************************************
+ ** The UI API operation the tool-call log names for a call of this tool that
+ ** has no operation detail of its own; null for none.
+ *******************************************************************************/
+ default String operationWithoutDetail()
+ {
+ return (null);
+ }
+}
diff --git a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ConfirmedToolCall.java b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ConfirmedToolCall.java
new file mode 100644
index 0000000..058d351
--- /dev/null
+++ b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ConfirmedToolCall.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2026 Kingsrook, LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.kingsrook.qbits.assistant.tools.registry;
+
+
+import java.util.Map;
+import com.kingsrook.qbits.assistant.tools.UiApiToolbox;
+
+
+/*******************************************************************************
+ ** A proposed write the person has confirmed: the pending action's tool call,
+ ** to be run once with the confirming request's own session (the context).
+ **
+ ** @param context the confirming person's session, gateway and policy
+ ** @param toolCallId the proposing tool call's id
+ ** @param toolName the tool that proposed the write
+ ** @param tableName the pending action's table
+ ** @param recordId the pending action's record, if it has one
+ ** @param values the stored values of the proposal
+ ** @param userId the confirming person
+ *******************************************************************************/
+public record ConfirmedToolCall(UiApiToolbox.Context context, String toolCallId, String toolName, String tableName, String recordId, Map values, String userId)
+{
+}
diff --git a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolCallOutcome.java b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolCallOutcome.java
new file mode 100644
index 0000000..4809a67
--- /dev/null
+++ b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolCallOutcome.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2026 Kingsrook, LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.kingsrook.qbits.assistant.tools.registry;
+
+
+import com.kingsrook.qbits.assistant.tools.UiApiToolbox;
+import com.kingsrook.qqq.backend.core.actions.ai.AiToolResult;
+
+
+/*******************************************************************************
+ ** What one tool call gave back: the result for the model and the panel, and,
+ ** for a tool that works through a UI API operation, that operation's detail
+ ** for the tool-call log (null for a tool that has none).
+ *******************************************************************************/
+public record ToolCallOutcome(AiToolResult result, UiApiToolbox.Outcome extras)
+{
+ /*******************************************************************************
+ ** A result with no UI API operation detail.
+ *******************************************************************************/
+ public static ToolCallOutcome of(AiToolResult result)
+ {
+ return (new ToolCallOutcome(result, null));
+ }
+
+
+
+ /*******************************************************************************
+ ** The outcome of a UI API tool: its result and the operation's detail.
+ *******************************************************************************/
+ public static ToolCallOutcome of(UiApiToolbox.Outcome outcome)
+ {
+ return (new ToolCallOutcome(outcome.getResult(), outcome));
+ }
+}
diff --git a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolCallRequest.java b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolCallRequest.java
new file mode 100644
index 0000000..3bbf6d1
--- /dev/null
+++ b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolCallRequest.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2026 Kingsrook, LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.kingsrook.qbits.assistant.tools.registry;
+
+
+import java.util.Map;
+import java.util.function.Supplier;
+import com.kingsrook.qbits.assistant.tools.UiApiToolbox;
+import com.kingsrook.qqq.backend.core.context.QContext;
+import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
+import com.kingsrook.qqq.backend.core.model.session.QSession;
+
+
+/*******************************************************************************
+ ** One tool call, for one person.
+ **
+ ** @param session the signed-in person's own session
+ ** @param context that session for the UI API, with the gateway, the
+ ** policy's tool settings and where a proposed write is kept
+ ** @param toolCallId the model's id of the call
+ ** @param toolName the tool called
+ ** @param input the model's input
+ ** @param scope where the call runs
+ *******************************************************************************/
+public record ToolCallRequest(QSession session, UiApiToolbox.Context context, String toolCallId, String toolName, Map input, ToolCallScope scope)
+{
+ /*******************************************************************************
+ ** Run a data tool with the person's session as the thread's QQQ context,
+ ** and leave no context behind.
+ *******************************************************************************/
+ public T inPersonsContext(QInstance qInstance, Supplier call)
+ {
+ QContext.init(qInstance, session);
+ try
+ {
+ return (call.get());
+ }
+ finally
+ {
+ QContext.clear();
+ }
+ }
+}
diff --git a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolCallScope.java b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolCallScope.java
new file mode 100644
index 0000000..5070934
--- /dev/null
+++ b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolCallScope.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2026 Kingsrook, LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.kingsrook.qbits.assistant.tools.registry;
+
+
+import java.util.function.Supplier;
+import com.kingsrook.qqq.backend.core.actions.ai.DataClassification;
+
+
+/*******************************************************************************
+ ** What a tool call may know about where it runs (in chat: the turn). All of
+ ** it is decided by the server; nothing here comes from the model.
+ *******************************************************************************/
+public interface ToolCallScope
+{
+ /*******************************************************************************
+ ** The data classification reached so far.
+ *******************************************************************************/
+ DataClassification classification();
+
+
+
+ /*******************************************************************************
+ ** The conversation the call belongs to.
+ *******************************************************************************/
+ Integer conversationId();
+
+
+
+ /*******************************************************************************
+ ** The turn the call belongs to.
+ *******************************************************************************/
+ Integer turnId();
+
+
+
+ /*******************************************************************************
+ ** Whether the model that asked for the call is HIPAA eligible.
+ *******************************************************************************/
+ boolean hipaaRoute();
+
+
+
+ /*******************************************************************************
+ ** State a tool keeps for as long as the scope lives (in chat: one turn),
+ ** created on first use and then shared by the scope's calls.
+ *******************************************************************************/
+ T state(Class type, Supplier create);
+}
diff --git a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolCheck.java b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolCheck.java
new file mode 100644
index 0000000..69e276b
--- /dev/null
+++ b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolCheck.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2026 Kingsrook, LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.kingsrook.qbits.assistant.tools.registry;
+
+
+/*******************************************************************************
+ ** The checks a tool call can go through. A tool lists the ones it runs, in
+ ** the order each first applies ({@link AssistantTool#checks()}). Every one of
+ ** them only narrows what the signed-in person may already do; none can turn
+ ** a refusal of another into success.
+ *******************************************************************************/
+public enum ToolCheck
+{
+ /////////////////////////////////////////////////////////////////////////
+ // the policy's allowed tools (logged as "policy:tool"); also decides //
+ // whether the tool is offered at all //
+ /////////////////////////////////////////////////////////////////////////
+ TOOL_SWITCH,
+
+ /////////////////////////////////////////////////////////////////////////
+ // the operation behind the tool is a spec'd UI API operation that the //
+ // gateway may invoke (logged as "uiApi:notSpecd") //
+ /////////////////////////////////////////////////////////////////////////
+ SPECD_OPERATION,
+
+ /////////////////////////////////////////////////////////////////////////
+ // the assistant's data access for the table (logged as //
+ // "dataAccess:"): NONE refuses reads, READ refuses writes //
+ /////////////////////////////////////////////////////////////////////////
+ DATA_ACCESS,
+
+ /////////////////////////////////////////////////////////////////////////
+ // fields whose values the assistant may not see are refused in //
+ // filters and inputs, and masked or left out of results //
+ /////////////////////////////////////////////////////////////////////////
+ FIELD_EXPOSURE,
+
+ /////////////////////////////////////////////////////////////////////////
+ // the turn's data classification decides which model may be sent the //
+ // text //
+ /////////////////////////////////////////////////////////////////////////
+ CLASSIFICATION,
+
+ /////////////////////////////////////////////////////////////////////////
+ // the write waits as a pending action for the person's confirmation //
+ // in the panel (logged as "pending:") //
+ /////////////////////////////////////////////////////////////////////////
+ CONFIRMATION,
+
+ /////////////////////////////////////////////////////////////////////////
+ // the /qqq/v1 operation runs with the person's own session: core //
+ // applies their permissions, record locks and the channel wall, and //
+ // the tool result carries the operation's own status (logged as //
+ // "uiApi::") //
+ /////////////////////////////////////////////////////////////////////////
+ PERSON_PERMISSIONS,
+
+ /////////////////////////////////////////////////////////////////////////
+ // the core AI tool kernel's own checks, with the person's session //
+ // (navigate and search_help only: no records are read) //
+ /////////////////////////////////////////////////////////////////////////
+ KERNEL
+}
diff --git a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolFamily.java b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolFamily.java
new file mode 100644
index 0000000..285d7d2
--- /dev/null
+++ b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolFamily.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2026 Kingsrook, LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.kingsrook.qbits.assistant.tools.registry;
+
+
+import java.util.List;
+import com.kingsrook.qbits.assistant.AssistantRuntime;
+
+
+/*******************************************************************************
+ ** A group of tools that share their code (the UI API data tools, the
+ ** bulk-load tools, ...). A family is how tools get into the
+ ** {@link ToolRegistry}: a public top-level class under
+ ** {@link ToolRegistry#FAMILY_PACKAGE} that implements this interface and has
+ ** a public no-argument constructor is found when the runtime starts. Adding
+ ** a tool family is therefore adding files; no existing class names it.
+ **
+ ** tools() runs while the runtime is still being constructed: keep the
+ ** runtime and ask it for collaborators when a tool is offered or called,
+ ** not here.
+ *******************************************************************************/
+public interface ToolFamily
+{
+ /*******************************************************************************
+ ** Every tool of this family, whether or not a given host or person is
+ ** offered it (that is decided per request, by
+ ** {@link AssistantTool#definition(ToolOffer)}).
+ *******************************************************************************/
+ List tools(AssistantRuntime runtime);
+}
diff --git a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolKind.java b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolKind.java
new file mode 100644
index 0000000..6455e35
--- /dev/null
+++ b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolKind.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2026 Kingsrook, LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.kingsrook.qbits.assistant.tools.registry;
+
+
+/*******************************************************************************
+ ** What a tool does, as far as authorisation is concerned.
+ *******************************************************************************/
+public enum ToolKind
+{
+ /////////////////////////////////////////////////////////////////////////
+ // reads as the signed-in person; needs no confirmation //
+ /////////////////////////////////////////////////////////////////////////
+ READ,
+
+ /////////////////////////////////////////////////////////////////////////
+ // proposes a write: nothing is saved until the person confirms the //
+ // pending action (unless the policy turned confirmation off), and the //
+ // write then runs with the confirming request's session //
+ /////////////////////////////////////////////////////////////////////////
+ PROPOSE,
+
+ /////////////////////////////////////////////////////////////////////////
+ // shows something in the panel (a presentation, a page to open); it //
+ // changes nothing //
+ /////////////////////////////////////////////////////////////////////////
+ DISPLAY
+}
diff --git a/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolOffer.java b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolOffer.java
new file mode 100644
index 0000000..1a34c8c
--- /dev/null
+++ b/qbit-assistant/src/main/java/com/kingsrook/qbits/assistant/tools/registry/ToolOffer.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright 2026 Kingsrook, LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.kingsrook.qbits.assistant.tools.registry;
+
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.Supplier;
+import com.kingsrook.qbits.assistant.engine.PolicyService;
+import com.kingsrook.qqq.backend.core.model.session.QSession;
+
+
+/*******************************************************************************
+ ** One question "which tools is this person offered now": the policy in
+ ** force, the person's own session, and the turn the question belongs to (so
+ ** a UI API request made to answer it is attributed to that turn).
+ **
+ ** An offer lives for one such question. A family whose tools share one
+ ** answer (one list of definitions, one look at the data access) works it out
+ ** once per offer with {@link #once(Object, Supplier)}.
+ *******************************************************************************/
+public final class ToolOffer
+{
+ private final PolicyService.ToolPolicy policy;
+ private final QSession session;
+ private final String turnId;
+ private final Map