Skip to content

Commit 6708e33

Browse files
authored
registerTool() takes an optional AbortSignal (#147)
1 parent 529ac93 commit 6708e33

1 file changed

Lines changed: 51 additions & 22 deletions

File tree

index.bs

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ A <dfn>tool definition</dfn> is a [=struct=] with the following [=struct/items=]
145145
:: a [=boolean=], initially false.
146146
</dl>
147147

148+
<div algorithm>
149+
To <dfn for="model context">unregister a tool</dfn> for a [=model context=] given a [=string=]
150+
|tool name|, run these steps:
151+
152+
1. Let |tool map| be the associated [=model context=]'s [=model context/tool map=].
153+
154+
1. [=Assert=] |tool map|[|tool name|] [=map/exists=].
155+
156+
1. [=map/Remove=] |tool map|[|tool name|].
157+
158+
</div>
159+
160+
148161
<h2 id="api">API</h2>
149162

150163
<!--
@@ -177,8 +190,7 @@ The {{ModelContext}} interface provides methods for web applications to register
177190
<xmp class="idl">
178191
[Exposed=Window, SecureContext]
179192
interface ModelContext {
180-
undefined registerTool(ModelContextTool tool);
181-
undefined unregisterTool(DOMString name);
193+
undefined registerTool(ModelContextTool tool, optional ModelContextRegisterToolOptions options = {});
182194
};
183195
</xmp>
184196

@@ -187,20 +199,18 @@ is a [=model context=] [=struct=] created alongside the {{ModelContext}}.
187199

188200

189201
<dl class="domintro">
190-
<dt><code><var ignore>navigator</var>.{{Navigator/modelContext}}.{{ModelContext/registerTool(tool)}}</code></dt>
191-
<dd>
192-
<p>Registers a single tool without clearing the existing set of tools. The method throws an error, if a tool with the same name already exists, or if the {{ModelContextTool/inputSchema}} is invalid.
193-
</dd>
194-
195-
<dt><code><var ignore>navigator</var>.{{Navigator/modelContext}}.{{ModelContext/unregisterTool(name)}}</code></dt>
202+
<dt><code><var ignore>navigator</var>.{{Navigator/modelContext}}.{{ModelContext/registerTool(tool, options)}}</code></dt>
196203
<dd>
197-
<p>Removes the tool with the specified name from the registered set.
204+
<p>Registers a tool that [=agents=] can invoke. Throws an exception if a tool with the same name
205+
is already registered, if the given {{ModelContextTool/name}} or
206+
{{ModelContextTool/description}} are empty strings, or if the {{ModelContextTool/inputSchema}}
207+
is invalid.</p>
198208
</dd>
199209
</dl>
200210

201211

202212
<div algorithm>
203-
The <dfn method for=ModelContext>registerTool(<var>tool</var>)</dfn> method steps are:
213+
The <dfn method for=ModelContext>registerTool(<var>tool</var>, <var>options</var>)</dfn> method steps are:
204214

205215
1. Let |tool map| be [=this=]'s [=ModelContext/internal context=]'s [=model context/tool map=].
206216

@@ -235,6 +245,17 @@ The <dfn method for=ModelContext>registerTool(<var>tool</var>)</dfn> method step
235245
1. Let |read-only hint| be true if |tool|'s {{ModelContextTool/annotations}} [=map/exists=] and
236246
its {{ToolAnnotations/readOnlyHint}} is true. Otherwise, let it be false.
237247

248+
1. Let |signal| be |options|'s {{ModelContextRegisterToolOptions/signal}}.
249+
250+
1. If |signal| [=map/exists=], then:
251+
252+
1. If |signal| is [=AbortSignal/aborted=], then optionally [=report a warning to the console=]
253+
indicating that the tool was not registered because the {{AbortSignal}} was already
254+
[=AbortSignal/aborted=], and return.
255+
256+
1. [=AbortSignal/add|Add an abort algorithm=] to |signal| that [=model context/unregisters a
257+
tool=] from [=this=] [=ModelContext/internal context=] given |tool name|.
258+
238259
1. Let |tool definition| be a new [=tool definition=], with the following [=struct/items=]:
239260

240261
: [=tool definition/name=]
@@ -256,18 +277,6 @@ The <dfn method for=ModelContext>registerTool(<var>tool</var>)</dfn> method step
256277

257278
</div>
258279

259-
<div algorithm>
260-
The <dfn method for=ModelContext>unregisterTool(<var>name</var>)</dfn> method steps are:
261-
262-
1. Let |tool map| be [=this=]'s [=ModelContext/internal context=]'s [=model context/tool map=].
263-
264-
1. If |tool map|[|name|] does not [=map/exist=], then [=exception/throw=] an {{InvalidStateError}}
265-
{{DOMException}}.
266-
267-
1. [=map/Remove=] |tool map|[|name|].
268-
269-
</div>
270-
271280
<h4 id="model-context-tool">ModelContextTool Dictionary</h4>
272281

273282
The {{ModelContextTool}} dictionary describes a tool that can be invoked by [=agents=].
@@ -324,6 +333,26 @@ The {{ToolAnnotations}} dictionary provides optional metadata about a tool:
324333
:: If true, indicates that the tool does not modify any state and only reads data. This hint can help [=agents=] make decisions about when it is safe to call the tool.
325334
</dl>
326335

336+
<h4 id="model-context-register-tool-options">ModelContextRegisterToolOptions Dictionary</h4>
337+
338+
The {{ModelContextRegisterToolOptions}} dictionary carries information pertaining to a tool's
339+
registration, in contrast with the {{ModelContextTool}} dictionary which carries the tool
340+
definition itself.
341+
342+
<xmp class="idl">
343+
dictionary ModelContextRegisterToolOptions {
344+
AbortSignal signal;
345+
};
346+
</xmp>
347+
348+
<dl class="domintro">
349+
<dt><code><var ignore>tool</var>["{{ModelContextRegisterToolOptions/signal}}"]</code></dt>
350+
<dd>
351+
<p>An {{AbortSignal}} that unregisters the tool when aborted.
352+
</dd>
353+
</dl>
354+
355+
327356
<h4 id="model-context-client">ModelContextClient Interface</h4>
328357

329358
The {{ModelContextClient}} interface represents an [=agent=] executing a tool provided by the site through the {{ModelContext}} API.

0 commit comments

Comments
 (0)