@@ -19,6 +19,8 @@ import type { ToolDefinition } from "./sessions/index.js";
1919import { asToolParamsRecord , jsonResult , ToolInputError } from "./tools/common.js" ;
2020import type { AnyAgentTool } from "./tools/common.js" ;
2121
22+ // Tool Search compacts large tool catalogs behind a small search/describe/call
23+ // surface, with an optional isolated code runner for multi-step catalog use.
2224export const TOOL_SEARCH_CODE_MODE_TOOL_NAME = "tool_search_code" ;
2325export const TOOL_SEARCH_RAW_TOOL_NAME = "tool_search" ;
2426export const TOOL_DESCRIBE_RAW_TOOL_NAME = "tool_describe" ;
@@ -58,6 +60,7 @@ export type ToolSearchCatalogToolExecutor = (params: {
5860 onUpdate ?: AgentToolUpdateCallback ;
5961} ) => Promise < AgentToolResult < unknown > > ;
6062
63+ /** Transcript projection for target tool calls made through Tool Search. */
6164export type ToolSearchTargetTranscriptProjection = {
6265 parentToolCallId ?: string ;
6366 toolCallId : string ;
@@ -68,6 +71,7 @@ export type ToolSearchTargetTranscriptProjection = {
6871 timestamp ?: number ;
6972} ;
7073
74+ /** Resolved Tool Search config after defaults, limits, and runtime support checks. */
7175export type ToolSearchConfig = {
7276 enabled : boolean ;
7377 mode : ToolSearchMode ;
@@ -76,6 +80,7 @@ export type ToolSearchConfig = {
7680 maxSearchLimit : number ;
7781} ;
7882
83+ /** Per-run/session context used by Tool Search control tools. */
7984export type ToolSearchToolContext = {
8085 config ?: OpenClawConfig ;
8186 runtimeConfig ?: OpenClawConfig ;
@@ -88,6 +93,7 @@ export type ToolSearchToolContext = {
8893 executeTool ?: ToolSearchCatalogToolExecutor ;
8994} ;
9095
96+ /** Catalog entry retained behind compacted Tool Search control tools. */
9197export type ToolSearchCatalogEntry = {
9298 id : string ;
9399 source : CatalogSource ;
@@ -217,6 +223,8 @@ function buildModelScriptSource(code) {
217223}
218224
219225function buildControllerSource() {
226+ // The controller returns promise-like bridge handles. The model code can await
227+ // them naturally, while the parent process serializes real tool calls.
220228 return (
221229 '"use strict";\n' +
222230 "(() => {\n" +
@@ -521,6 +529,8 @@ function catalogToolIdentity(tool: CatalogTool): number {
521529}
522530
523531function catalogEntriesFingerprint ( entries : readonly ToolSearchCatalogEntry [ ] ) : string {
532+ // Fingerprints include object identity for executable tools because function
533+ // bodies are not JSON-stable but catalog reuse must not bind stale executors.
524534 return entries
525535 . map ( ( entry ) =>
526536 [
@@ -827,10 +837,12 @@ export function projectToolSearchTargetTranscriptMessages(
827837 return projected ;
828838}
829839
840+ /** Create an explicit catalog holder for callers that cannot rely on session keys. */
830841export function createToolSearchCatalogRef ( ) : ToolSearchCatalogRef {
831842 return { } ;
832843}
833844
845+ /** Replace visible tools with Tool Search controls and register hidden catalog entries. */
834846export function applyToolSearchCatalog ( params : {
835847 tools : AnyAgentTool [ ] ;
836848 config ?: OpenClawConfig ;
@@ -857,6 +869,7 @@ export function applyToolSearchCatalog(params: {
857869 } ) ;
858870}
859871
872+ /** Move client-provided tools into an existing Tool Search catalog. */
860873export function addClientToolsToToolSearchCatalog ( params : {
861874 tools : ToolDefinition [ ] ;
862875 config ?: OpenClawConfig ;
@@ -872,6 +885,7 @@ export function addClientToolsToToolSearchCatalog(params: {
872885 } ) ;
873886}
874887
888+ /** Register catalog entries under run/session keys and optional direct refs. */
875889export function registerToolSearchCatalog ( params : {
876890 sessionId ?: string ;
877891 sessionKey ?: string ;
@@ -913,6 +927,7 @@ export function registerToolSearchCatalog(params: {
913927 return next ;
914928}
915929
930+ /** Clear Tool Search catalog state for a run/session/ref. */
916931export function clearToolSearchCatalog ( params : {
917932 sessionId ?: string ;
918933 sessionKey ?: string ;
@@ -1225,6 +1240,7 @@ export class ToolSearchRuntime {
12251240 }
12261241}
12271242
1243+ /** Compact a native tool list into visible control tools plus hidden catalog entries. */
12281244export function applyToolCatalogCompaction ( params : {
12291245 tools : AnyAgentTool [ ] ;
12301246 enabled : boolean ;
@@ -1348,6 +1364,7 @@ export function applyToolCatalogCompaction(params: {
13481364 } ;
13491365}
13501366
1367+ /** Append client-side tool definitions to an already registered catalog. */
13511368export function addClientToolsToToolCatalog ( params : {
13521369 tools : ToolDefinition [ ] ;
13531370 enabled : boolean ;
@@ -1562,6 +1579,8 @@ function runCodeModeChild(params: {
15621579 ) ;
15631580 } ;
15641581 if ( code === 0 && signal === null ) {
1582+ // A clean exit can race the final IPC result. Wait briefly before
1583+ // treating it as failure so flushed bridge/result messages can arrive.
15651584 exitRejectionTimer = setTimeout ( rejectOnExit , 250 ) ;
15661585 return ;
15671586 }
@@ -1645,6 +1664,7 @@ function readCode(args: unknown): string {
16451664 return code ;
16461665}
16471666
1667+ /** Create Tool Search control tools for the current run/session context. */
16481668export function createToolSearchTools ( ctx : ToolSearchToolContext ) : AnyAgentTool [ ] {
16491669 const config = resolveToolSearchConfig ( ctx . runtimeConfig ?? ctx . config ) ;
16501670 const runtime = new ToolSearchRuntime ( ctx , config ) ;
0 commit comments