chore(mocktools): final step in unify mock tool definitions (#9166)

This commit is contained in:
Adam Weidman
2025-09-29 22:43:06 +02:00
committed by GitHub
parent 94f43c79d0
commit 0c3fcb7030
8 changed files with 171 additions and 228 deletions
+106 -3
View File
@@ -4,6 +4,10 @@
* SPDX-License-Identifier: Apache-2.0
*/
import type {
ModifiableDeclarativeTool,
ModifyContext,
} from '../tools/modifiable-tool.js';
import type {
ToolCallConfirmationDetails,
ToolInvocation,
@@ -27,7 +31,7 @@ interface MockToolOptions {
) => Promise<ToolCallConfirmationDetails | false>;
execute?: (
params: { [key: string]: unknown },
signal: AbortSignal,
signal?: AbortSignal,
updateOutput?: (output: string) => void,
) => Promise<ToolResult>;
params?: object;
@@ -48,7 +52,11 @@ class MockToolInvocation extends BaseToolInvocation<
signal: AbortSignal,
updateOutput?: (output: string) => void,
): Promise<ToolResult> {
return this.tool.execute(this.params, signal, updateOutput);
if (updateOutput) {
return this.tool.execute(this.params, signal, updateOutput);
} else {
return this.tool.execute(this.params);
}
}
override shouldConfirmExecute(
@@ -75,7 +83,7 @@ export class MockTool extends BaseDeclarativeTool<
) => Promise<ToolCallConfirmationDetails | false>;
execute: (
params: { [key: string]: unknown },
signal: AbortSignal,
signal?: AbortSignal,
updateOutput?: (output: string) => void,
) => Promise<ToolResult>;
@@ -113,3 +121,98 @@ export class MockTool extends BaseDeclarativeTool<
return new MockToolInvocation(this, params);
}
}
export const MOCK_TOOL_SHOULD_CONFIRM_EXECUTE = () =>
Promise.resolve({
type: 'exec' as const,
title: 'Confirm mockTool',
command: 'mockTool',
rootCommand: 'mockTool',
onConfirm: async () => {},
});
export class MockModifiableToolInvocation extends BaseToolInvocation<
Record<string, unknown>,
ToolResult
> {
constructor(
private readonly tool: MockModifiableTool,
params: Record<string, unknown>,
) {
super(params);
}
async execute(_abortSignal: AbortSignal): Promise<ToolResult> {
const result = this.tool.executeFn(this.params);
return (
result ?? {
llmContent: `Tool ${this.tool.name} executed successfully.`,
returnDisplay: `Tool ${this.tool.name} executed successfully.`,
}
);
}
override async shouldConfirmExecute(
_abortSignal: AbortSignal,
): Promise<ToolCallConfirmationDetails | false> {
if (this.tool.shouldConfirm) {
return {
type: 'edit',
title: 'Confirm Mock Tool',
fileName: 'test.txt',
filePath: 'test.txt',
fileDiff: 'diff',
originalContent: 'originalContent',
newContent: 'newContent',
onConfirm: async () => {},
};
}
return false;
}
getDescription(): string {
return `A mock modifiable tool invocation for ${this.tool.name}`;
}
}
/**
* Configurable mock modifiable tool for testing.
*/
export class MockModifiableTool
extends BaseDeclarativeTool<Record<string, unknown>, ToolResult>
implements ModifiableDeclarativeTool<Record<string, unknown>>
{
// Should be overrided in test file. Functionality will be updated in follow
// up PR which has MockModifiableTool expect MockTool
executeFn: (params: Record<string, unknown>) => ToolResult | undefined = () =>
undefined;
shouldConfirm = true;
constructor(name = 'mockModifiableTool') {
super(name, name, 'A mock modifiable tool for testing.', Kind.Other, {
type: 'object',
properties: { param: { type: 'string' } },
});
}
getModifyContext(
_abortSignal: AbortSignal,
): ModifyContext<Record<string, unknown>> {
return {
getFilePath: () => 'test.txt',
getCurrentContent: async () => 'old content',
getProposedContent: async () => 'new content',
createUpdatedParams: (
_oldContent: string,
modifiedProposedContent: string,
_originalParams: Record<string, unknown>,
) => ({ newContent: modifiedProposedContent }),
};
}
protected createInvocation(
params: Record<string, unknown>,
): ToolInvocation<Record<string, unknown>, ToolResult> {
return new MockModifiableToolInvocation(this, params);
}
}
-169
View File
@@ -1,169 +0,0 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { vi } from 'vitest';
import type {
ToolCallConfirmationDetails,
ToolInvocation,
ToolResult,
} from '../tools/tools.js';
import {
BaseDeclarativeTool,
BaseToolInvocation,
Kind,
} from '../tools/tools.js';
import type {
ModifiableDeclarativeTool,
ModifyContext,
} from '../tools/modifiable-tool.js';
class MockToolInvocation extends BaseToolInvocation<
{ [key: string]: unknown },
ToolResult
> {
constructor(
private readonly tool: MockTool,
params: { [key: string]: unknown },
) {
super(params);
}
async execute(_abortSignal: AbortSignal): Promise<ToolResult> {
const result = this.tool.executeFn(this.params);
return (
result ?? {
llmContent: `Tool ${this.tool.name} executed successfully.`,
returnDisplay: `Tool ${this.tool.name} executed successfully.`,
}
);
}
override async shouldConfirmExecute(
_abortSignal: AbortSignal,
): Promise<ToolCallConfirmationDetails | false> {
if (this.tool.shouldConfirm) {
return {
type: 'exec' as const,
title: `Confirm ${this.tool.displayName}`,
command: this.tool.name,
rootCommand: this.tool.name,
onConfirm: async () => {},
};
}
return false;
}
getDescription(): string {
return `A mock tool invocation for ${this.tool.name}`;
}
}
/**
* A highly configurable mock tool for testing purposes.
*/
export class MockTool extends BaseDeclarativeTool<
{ [key: string]: unknown },
ToolResult
> {
executeFn = vi.fn();
shouldConfirm = false;
constructor(
name = 'mock-tool',
displayName?: string,
description = 'A mock tool for testing.',
params = {
type: 'object',
properties: { param: { type: 'string' } },
},
) {
super(name, displayName ?? name, description, Kind.Other, params);
}
protected createInvocation(params: {
[key: string]: unknown;
}): ToolInvocation<{ [key: string]: unknown }, ToolResult> {
return new MockToolInvocation(this, params);
}
}
export class MockModifiableToolInvocation extends BaseToolInvocation<
Record<string, unknown>,
ToolResult
> {
constructor(
private readonly tool: MockModifiableTool,
params: Record<string, unknown>,
) {
super(params);
}
async execute(_abortSignal: AbortSignal): Promise<ToolResult> {
const result = this.tool.executeFn(this.params);
return (
result ?? {
llmContent: `Tool ${this.tool.name} executed successfully.`,
returnDisplay: `Tool ${this.tool.name} executed successfully.`,
}
);
}
override async shouldConfirmExecute(
_abortSignal: AbortSignal,
): Promise<ToolCallConfirmationDetails | false> {
if (this.tool.shouldConfirm) {
return {
type: 'edit',
title: 'Confirm Mock Tool',
fileName: 'test.txt',
filePath: 'test.txt',
fileDiff: 'diff',
originalContent: 'originalContent',
newContent: 'newContent',
onConfirm: async () => {},
};
}
return false;
}
getDescription(): string {
return `A mock modifiable tool invocation for ${this.tool.name}`;
}
}
/**
* Configurable mock modifiable tool for testing.
*/
export class MockModifiableTool
extends MockTool
implements ModifiableDeclarativeTool<Record<string, unknown>>
{
constructor(name = 'mockModifiableTool') {
super(name);
this.shouldConfirm = true;
}
getModifyContext(
_abortSignal: AbortSignal,
): ModifyContext<Record<string, unknown>> {
return {
getFilePath: () => 'test.txt',
getCurrentContent: async () => 'old content',
getProposedContent: async () => 'new content',
createUpdatedParams: (
_oldContent: string,
modifiedProposedContent: string,
_originalParams: Record<string, unknown>,
) => ({ newContent: modifiedProposedContent }),
};
}
protected override createInvocation(
params: Record<string, unknown>,
): ToolInvocation<Record<string, unknown>, ToolResult> {
return new MockModifiableToolInvocation(this, params);
}
}