Skip to content

Commit fc8f37b

Browse files
committed
feat(js/ai): expose restartTool/respondTool helper function in addition to tool.respond/restart
1 parent 0ce93b2 commit fc8f37b

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

js/ai/src/tool.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,71 @@ export type InterruptConfig<
408408
) => Record<string, any> | Promise<Record<string, any>>);
409409
};
410410

411+
/**
412+
* restartTool constructs a tool request corresponding to the provided interrupt tool request
413+
* that will then re-trigger the tool after e.g. a user confirms. In contrast to ToolAction.restart,
414+
* this is a standalone utility that does not require an active ToolAction instance.
415+
*
416+
* @param interrupt The interrupt tool request you want to restart.
417+
* @param resumedMetadata The metadata you want to provide to the tool to aide in reprocessing. Defaults to `true` if none is supplied.
418+
* @param options Additional options for restarting the tool.
419+
*
420+
* @beta
421+
*/
422+
export function restartTool(
423+
interrupt: ToolRequestPart,
424+
resumedMetadata?: any,
425+
options?: {
426+
/**
427+
* Replace the existing input arguments to the tool with different ones.
428+
**/
429+
replaceInput?: any;
430+
}
431+
): ToolRequestPart {
432+
let replaceInput = options?.replaceInput;
433+
return {
434+
toolRequest: stripUndefinedProps({
435+
name: interrupt.toolRequest.name,
436+
ref: interrupt.toolRequest.ref,
437+
input: replaceInput || interrupt.toolRequest.input,
438+
}),
439+
metadata: stripUndefinedProps({
440+
...interrupt.metadata,
441+
resumed: resumedMetadata || true,
442+
// annotate the original input if replacing it
443+
replacedInput: replaceInput ? interrupt.toolRequest.input : undefined,
444+
}),
445+
};
446+
}
447+
448+
/**
449+
* respondTool constructs a tool response part corresponding to the provided interrupt tool request
450+
* that bypasses normal tool execution and sends a manual output result. In contrast to ToolAction.respond,
451+
* this is a standalone utility that does not require an active ToolAction instance.
452+
*
453+
* @param interrupt The interrupt tool request you are responding to.
454+
* @param responseData The manual output result you want to send back to the model.
455+
* @param options Additional options for responding to the tool.
456+
*
457+
* @beta
458+
*/
459+
export function respondTool(
460+
interrupt: ToolRequestPart,
461+
responseData: any,
462+
options?: { metadata?: any }
463+
): ToolResponsePart {
464+
return {
465+
toolResponse: stripUndefinedProps({
466+
name: interrupt.toolRequest.name,
467+
ref: interrupt.toolRequest.ref,
468+
output: responseData,
469+
}),
470+
metadata: stripUndefinedProps({
471+
interruptResponse: options?.metadata || true,
472+
}),
473+
};
474+
}
475+
411476
export function isToolRequest(part: Part): part is ToolRequestPart {
412477
return !!part.toolRequest;
413478
}

js/genkit/src/common.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export {
4848
modelRef,
4949
rerankerRef,
5050
resource,
51+
respondTool,
52+
restartTool,
5153
retrieverRef,
5254
type DocumentData,
5355
type DynamicResourceAction,

0 commit comments

Comments
 (0)