summaryrefslogtreecommitdiff
path: root/packages/server/src/tools/tools.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/server/src/tools/tools.ts')
-rw-r--r--packages/server/src/tools/tools.ts150
1 files changed, 150 insertions, 0 deletions
diff --git a/packages/server/src/tools/tools.ts b/packages/server/src/tools/tools.ts
new file mode 100644
index 00000000..4851f164
--- /dev/null
+++ b/packages/server/src/tools/tools.ts
@@ -0,0 +1,150 @@
+/**
+ * @license
+ * Copyright 2025 Google LLC
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+import { FunctionDeclaration, Schema } from '@google/genai';
+// Removed import for ../ui/types.js as confirmation is UI-specific
+
+/**
+ * Interface representing the base Tool functionality
+ */
+export interface Tool<
+ TParams = unknown,
+ TResult extends ToolResult = ToolResult,
+> {
+ /**
+ * The internal name of the tool (used for API calls)
+ */
+ name: string;
+
+ /**
+ * The user-friendly display name of the tool
+ */
+ displayName: string;
+
+ /**
+ * Description of what the tool does
+ */
+ description: string;
+
+ /**
+ * Function declaration schema from @google/genai
+ */
+ schema: FunctionDeclaration;
+
+ /**
+ * Validates the parameters for the tool
+ * @param params Parameters to validate
+ * @returns An error message string if invalid, null otherwise
+ */
+ validateToolParams(params: TParams): string | null;
+
+ /**
+ * Gets a pre-execution description of the tool operation
+ * @param params Parameters for the tool execution
+ * @returns A markdown string describing what the tool will do
+ * Optional for backward compatibility
+ */
+ getDescription(params: TParams): string;
+
+ // Removed shouldConfirmExecute as it's UI-specific
+
+ /**
+ * Executes the tool with the given parameters
+ * @param params Parameters for the tool execution
+ * @returns Result of the tool execution
+ */
+ execute(params: TParams): Promise<TResult>;
+}
+
+/**
+ * Base implementation for tools with common functionality
+ */
+export abstract class BaseTool<
+ TParams = unknown,
+ TResult extends ToolResult = ToolResult,
+> implements Tool<TParams, TResult>
+{
+ /**
+ * Creates a new instance of BaseTool
+ * @param name Internal name of the tool (used for API calls)
+ * @param displayName User-friendly display name of the tool
+ * @param description Description of what the tool does
+ * @param parameterSchema JSON Schema defining the parameters
+ */
+ constructor(
+ readonly name: string,
+ readonly displayName: string,
+ readonly description: string,
+ readonly parameterSchema: Record<string, unknown>,
+ ) {}
+
+ /**
+ * Function declaration schema computed from name, description, and parameterSchema
+ */
+ get schema(): FunctionDeclaration {
+ return {
+ name: this.name,
+ description: this.description,
+ parameters: this.parameterSchema as Schema,
+ };
+ }
+
+ /**
+ * Validates the parameters for the tool
+ * This is a placeholder implementation and should be overridden
+ * @param params Parameters to validate
+ * @returns An error message string if invalid, null otherwise
+ */
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
+ validateToolParams(params: TParams): string | null {
+ // Implementation would typically use a JSON Schema validator
+ // This is a placeholder that should be implemented by derived classes
+ return null;
+ }
+
+ /**
+ * Gets a pre-execution description of the tool operation
+ * Default implementation that should be overridden by derived classes
+ * @param params Parameters for the tool execution
+ * @returns A markdown string describing what the tool will do
+ */
+ getDescription(params: TParams): string {
+ return JSON.stringify(params);
+ }
+
+ // Removed shouldConfirmExecute as it's UI-specific
+
+ /**
+ * Abstract method to execute the tool with the given parameters
+ * Must be implemented by derived classes
+ * @param params Parameters for the tool execution
+ * @returns Result of the tool execution
+ */
+ abstract execute(params: TParams): Promise<TResult>;
+}
+
+export interface ToolResult {
+ /**
+ * Content meant to be included in LLM history.
+ * This should represent the factual outcome of the tool execution.
+ */
+ llmContent: string;
+
+ /**
+ * Markdown string for user display.
+ * This provides a user-friendly summary or visualization of the result.
+ * NOTE: This might also be considered UI-specific and could potentially be
+ * removed or modified in a further refactor if the server becomes purely API-driven.
+ * For now, we keep it as the core logic in ReadFileTool currently produces it.
+ */
+ returnDisplay: ToolResultDisplay;
+}
+
+export type ToolResultDisplay = string | FileDiff;
+
+export interface FileDiff {
+ fileDiff: string;
+}