Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions frontend/src/api/tools-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { openHands } from "./open-hands-axios";

/** One entry of ``GET /api/tools/catalog`` (SDK ``ToolCatalogEntry``). */
export interface ToolCatalogEntry {
name: string;
user_selectable: boolean;
usable: boolean;
description: string;
/** Whether a profile with ``tools: null`` gets this tool. */
in_default_set: boolean;
}

class ToolsService {
/** The tools an agent profile can pin, built-ins included. */
static async getToolCatalog(): Promise<ToolCatalogEntry[]> {
const { data } = await openHands.get<{ tools: ToolCatalogEntry[] }>(
"/api/tools/catalog",
);
return data.tools;
}
}

export default ToolsService;
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import {
import { formatCommand, tokenizeCommand } from "#/utils/shell-tokenize";
import {
DEFAULT_TOOL_TOGGLES,
buildPinnedTools,
buildProfileTools,
isStandardToolList,
readToolNames,
readToolToggles,
} from "./agent-profile-tools";
import {
Expand Down Expand Up @@ -58,6 +61,10 @@ export interface AgentProfileFormState {
llmProfileRef: string;
enableSubAgents: boolean;
enableSwitchLlmTool: boolean;
/** The standard tool set (shaped by the two toggles above), or a hand-picked list. */
toolsMode: ProfileScopeMode;
/** Canonical tool names of the hand-picked list (``toolsMode === "custom"``). */
toolsSelected: string[];
/** ``SchemaField`` value for ``tool_concurrency_limit`` ("" when unknown). */
toolConcurrency: string | boolean;
systemPromptMode: SystemPromptMode;
Expand Down Expand Up @@ -93,6 +100,8 @@ export function newAgentProfileFormState(
llmProfileRef: deps.defaultLlmProfileRef,
enableSubAgents: DEFAULT_TOOL_TOGGLES.enableSubAgents,
enableSwitchLlmTool: DEFAULT_TOOL_TOGGLES.enableSwitchLlmTool,
toolsMode: "standard",
toolsSelected: [],
toolConcurrency: deps.toolConcurrencyField
? normalizeFieldValue(deps.toolConcurrencyField, undefined)
: "",
Expand Down Expand Up @@ -157,6 +166,10 @@ export function agentProfileFormStateFromProfile(
}

const toggles = readToolToggles(profile.tools);
// A list the toggles could not have produced opens in the tools picker.
const toolsMode: ProfileScopeMode = isStandardToolList(profile.tools)
? "standard"
: "custom";
const persona = profile.persona ?? "";
const suffix = profile.system_message_suffix ?? "";
return {
Expand All @@ -166,6 +179,8 @@ export function agentProfileFormStateFromProfile(
llmProfileRef: profile.llm_profile_ref,
enableSubAgents: toggles.enableSubAgents,
enableSwitchLlmTool: toggles.enableSwitchLlmTool,
toolsMode,
toolsSelected: toolsMode === "custom" ? readToolNames(profile.tools) : [],
disabledSkills: profile.disabled_skills ?? [],
toolConcurrency: deps.toolConcurrencyField
? normalizeFieldValue(
Expand Down Expand Up @@ -197,19 +212,33 @@ export function isSystemPromptMissing(state: AgentProfileFormState): boolean {
);
}

/** A hand-picked tool list needs at least one tool to save. */
export function isToolSelectionMissing(state: AgentProfileFormState): boolean {
return (
state.agentKind === "openhands" &&
state.toolsMode === "custom" &&
state.toolsSelected.length === 0
);
}

export function isAgentProfileFormValid(state: AgentProfileFormState): boolean {
if (state.agentKind === "acp") {
return tokenizeCommand(state.acpCommandText).length > 0;
}
return state.llmProfileRef.trim().length > 0 && !isSystemPromptMissing(state);
return (
state.llmProfileRef.trim().length > 0 &&
!isSystemPromptMissing(state) &&
!isToolSelectionMissing(state)
);
}

/**
* Turn editor state into the variant-specific save body. Throws a
* user-facing ``Error`` when the parallel-tool-calls value fails the
* schema's coercion/min/max rules (``coerceFieldValue``). ``stored`` is the
* profile being edited, so the two tool toggles only add or remove their own
* entries and a preset's extra ``acp_args`` survive the save.
* profile being edited, so a pinned tool entry keeps its ``params``, the two
* tool toggles only add or remove their own entries and a preset's extra
* ``acp_args`` survive the save.
*/
export function buildAgentProfileSaveInput(
state: AgentProfileFormState,
Expand Down Expand Up @@ -243,16 +272,23 @@ export function buildAgentProfileSaveInput(
};
}

const storedTools =
stored?.agent_kind === "openhands" ? stored.tools : undefined;
const input: AgentProfileSaveInput = {
agent_kind: "openhands",
llm_profile_ref: state.llmProfileRef,
tools: buildProfileTools(
{
enableSubAgents: state.enableSubAgents,
enableSwitchLlmTool: state.enableSwitchLlmTool,
},
stored?.agent_kind === "openhands" ? stored.tools : undefined,
),
// "Standard set" rebuilds from the toggles unless the stored list already
// is the standard set (then its entries, params included, are kept).
tools:
state.toolsMode === "custom"
? buildPinnedTools(state.toolsSelected, storedTools)
: buildProfileTools(
{
enableSubAgents: state.enableSubAgents,
enableSwitchLlmTool: state.enableSwitchLlmTool,
},
isStandardToolList(storedTools) ? storedTools : undefined,
),
mcp_server_refs: mcpServerRefs,
secret_refs: secretRefs,
// One of the two carries the text; the other is cleared so a mode
Expand Down
Loading
Loading