mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-21 01:07:46 +00:00
Starting to modularize into separate cli / server packages. (#55)
* Starting to move a lot of code into packages/server * More of the massive refactor, builds and runs, some issues though. * Fixing outstanding issue with double messages. * Fixing a minor UI issue. * Fixing the build post-merge. * Running formatting. * Addressing comments.
This commit is contained in:
150
packages/server/src/tools/tools.ts
Normal file
150
packages/server/src/tools/tools.ts
Normal file
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user