Files
Davide Silvestri eeca9ec5ee feat(agent): allow the tools an allowed tool requires (#6865)
Each Agent tool declares the tools that give the model an input it can't
get otherwise, like get_table_schema for create_rows. Saving allowed tools
without them is refused, Kuma refuses such a grant, and the tool permissions
modal allows them along with the tool and locks them while it is allowed.
Each section of the modal gets a Select all and Deselect all button.
2026-11-25 16:44:05 +01:00

131 lines
4.4 KiB
JavaScript

const GROUPS = {
database: { label: 'agent.toolGroupDatabase', icon: 'iconoir-db' },
}
/**
* Groups the tools the backend lists in `available_tools`, in their order, with
* their translated labels. A group without a label here, or a tool without a
* registered presenter, shows its name.
*
* @param {Array} availableTools Each tool's name, group, changes_data and
* whether the workspace can't use it.
* @param {Object} registry The registry containing shared tool presentations.
* @param {Function} t The translation function.
* @returns {Array} The groups, each with its type, label, icon and tools. A
* deactivated tool carries the modal that says why it is locked.
*/
export function groupAgentTools(availableTools, registry, t) {
const groups = new Map()
for (const tool of availableTools) {
if (!groups.has(tool.group)) {
const group = GROUPS[tool.group]
groups.set(tool.group, {
type: tool.group,
label: group ? t(group.label) : tool.group,
icon: group?.icon ?? 'iconoir-tools',
tools: [],
})
}
const presenter = toolPresenter(tool.name, registry)
groups.get(tool.group).tools.push({
name: tool.name,
changesData: tool.changes_data,
label: agentToolLabel(tool.name, registry, t),
description: presenter ? presenter.getDescription(t) : '',
deactivated: Boolean(tool.deactivated),
deactivatedClickModal: tool.deactivated
? (presenter?.getDeactivatedClickModal() ?? null)
: null,
})
}
return [...groups.values()]
}
/**
* @param {Array} availableTools Each tool's name and the tools it requires.
* @param {Iterable<string>} names The tools to start from.
* @returns {Set<string>} The tools these require, directly or through another.
*/
export function requiredAgentTools(availableTools, names) {
const requires = new Map(
availableTools.map((tool) => [tool.name, tool.requires ?? []])
)
const required = new Set()
const pending = [...names]
while (pending.length) {
for (const name of requires.get(pending.pop()) ?? []) {
if (!required.has(name)) {
required.add(name)
pending.push(name)
}
}
}
return required
}
/**
* @param {Array} availableTools Each tool's name and the tools it requires.
* @param {Set<string>} names The allowed tools.
* @returns {Map<string, Array<string>>} For each tool the allowed ones require,
* the names of the allowed tools that require it, in `availableTools` order.
*/
export function agentToolsRequiring(availableTools, names) {
const requiring = new Map()
for (const tool of availableTools) {
if (!names.has(tool.name)) continue
for (const required of requiredAgentTools(availableTools, [tool.name])) {
if (!requiring.has(required)) requiring.set(required, [])
requiring.get(required).push(tool.name)
}
}
return requiring
}
/**
* @param {Array} availableTools Each tool's name and whether it is deactivated.
* @returns {Array} The tools the Agent's workspace can use.
*/
export function usableAgentTools(availableTools) {
return availableTools.filter((tool) => !tool.deactivated)
}
function toolPresenter(name, registry) {
return registry.exists('toolPresentation', name)
? registry.get('toolPresentation', name)
: null
}
/** The tool's translated label, or its name without a registered presenter. */
export function agentToolLabel(name, registry, t) {
return toolPresenter(name, registry)?.getLabel(t) ?? name
}
/**
* The tools that ask first once the allowed tools change. Allowed tools keep
* their setting, and change tools that become allowed ask first.
*
* @param {Array} availableTools Each tool's name and changes_data.
* @param {Iterable<string>} prevAllowed The names allowed before the change.
* @param {Iterable<string>} prevAskFirst The names asking first before it.
* @param {Iterable<string>} nextAllowed The names allowed after it.
* @returns {Array<string>} The names that ask first, in `availableTools` order.
*/
export function nextAskFirst(
availableTools,
prevAllowed,
prevAskFirst,
nextAllowed
) {
const wasAllowed = new Set(prevAllowed)
const askedFirst = new Set(prevAskFirst)
const allowed = new Set(nextAllowed)
return availableTools
.filter(
(tool) =>
allowed.has(tool.name) &&
(askedFirst.has(tool.name) ||
(tool.changes_data && !wasAllowed.has(tool.name)))
)
.map((tool) => tool.name)
}