Use parameter properties where possible. (#242)

This commit is contained in:
Jacob Richman 2025-05-02 09:31:18 -07:00 committed by GitHub
parent a7679db6e9
commit 539ab947a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 18 additions and 53 deletions

View File

@ -65,18 +65,11 @@ export const ansiTheme: ColorsTheme = {
}; };
export class Theme { export class Theme {
/**
* The user-facing name of the theme.
*/
readonly name: string;
/** /**
* The default foreground color for text when no specific highlight rule applies. * The default foreground color for text when no specific highlight rule applies.
* This is an Ink-compatible color string (hex or name). * This is an Ink-compatible color string (hex or name).
*/ */
readonly defaultColor: string; readonly defaultColor: string;
readonly colors: ColorsTheme;
/** /**
* Stores the mapping from highlight.js class names (e.g., 'hljs-keyword') * Stores the mapping from highlight.js class names (e.g., 'hljs-keyword')
* to Ink-compatible color strings (hex or name). * to Ink-compatible color strings (hex or name).
@ -256,13 +249,11 @@ export class Theme {
* @param rawMappings The raw CSSProperties mappings from a react-syntax-highlighter theme object. * @param rawMappings The raw CSSProperties mappings from a react-syntax-highlighter theme object.
*/ */
constructor( constructor(
name: string, readonly name: string,
rawMappings: Record<string, CSSProperties>, rawMappings: Record<string, CSSProperties>,
colors: ColorsTheme, readonly colors: ColorsTheme,
) { ) {
this.name = name;
this._colorMap = Object.freeze(this._buildColorMap(rawMappings)); // Build and freeze the map this._colorMap = Object.freeze(this._buildColorMap(rawMappings)); // Build and freeze the map
this.colors = colors;
// Determine the default foreground color // Determine the default foreground color
const rawDefaultColor = rawMappings['hljs']?.color; const rawDefaultColor = rawMappings['hljs']?.color;

View File

@ -23,7 +23,6 @@ import { ReadManyFilesTool } from '../tools/read-many-files.js'; // Import ReadM
import { getResponseText } from '../utils/generateContentResponseUtilities.js'; import { getResponseText } from '../utils/generateContentResponseUtilities.js';
export class GeminiClient { export class GeminiClient {
private config: Config;
private client: GoogleGenAI; private client: GoogleGenAI;
private model: string; private model: string;
private generateContentConfig: GenerateContentConfig = { private generateContentConfig: GenerateContentConfig = {
@ -32,9 +31,8 @@ export class GeminiClient {
}; };
private readonly MAX_TURNS = 100; private readonly MAX_TURNS = 100;
constructor(config: Config) { constructor(private config: Config) {
this.client = new GoogleGenAI({ apiKey: config.getApiKey() }); this.client = new GoogleGenAI({ apiKey: config.getApiKey() });
this.config = config;
this.model = config.getModel(); this.model = config.getModel();
} }

View File

@ -84,7 +84,6 @@ export type ServerGeminiStreamEvent =
// A turn manages the agentic loop turn within the server context. // A turn manages the agentic loop turn within the server context.
export class Turn { export class Turn {
private readonly chat: Chat;
private readonly availableTools: Map<string, ServerTool>; // Use passed-in tools private readonly availableTools: Map<string, ServerTool>; // Use passed-in tools
private pendingToolCalls: Array<{ private pendingToolCalls: Array<{
callId: string; callId: string;
@ -95,8 +94,10 @@ export class Turn {
private confirmationDetails: ToolCallConfirmationDetails[]; private confirmationDetails: ToolCallConfirmationDetails[];
private debugResponses: GenerateContentResponse[]; private debugResponses: GenerateContentResponse[];
constructor(chat: Chat, availableTools: ServerTool[]) { constructor(
this.chat = chat; private readonly chat: Chat,
availableTools: ServerTool[],
) {
this.availableTools = new Map(availableTools.map((t) => [t.name, t])); this.availableTools = new Map(availableTools.map((t) => [t.name, t]));
this.pendingToolCalls = []; this.pendingToolCalls = [];
this.fnResponses = []; this.fnResponses = [];

View File

@ -59,13 +59,11 @@ export class EditTool extends BaseTool<EditToolParams, ToolResult> {
static readonly Name = 'replace'; // Keep static name static readonly Name = 'replace'; // Keep static name
private shouldAlwaysEdit = false; private shouldAlwaysEdit = false;
private readonly rootDirectory: string;
/** /**
* Creates a new instance of the EditLogic * Creates a new instance of the EditLogic
* @param rootDirectory Root directory to ground this tool in. * @param rootDirectory Root directory to ground this tool in.
*/ */
constructor(rootDirectory: string) { constructor(private readonly rootDirectory: string) {
// Note: The description here mentions other tools like ReadFileTool/WriteFileTool // Note: The description here mentions other tools like ReadFileTool/WriteFileTool
// by name. This might need updating if those tool names change. // by name. This might need updating if those tool names change.
super( super(

View File

@ -32,16 +32,11 @@ export interface GlobToolParams {
export class GlobTool extends BaseTool<GlobToolParams, ToolResult> { export class GlobTool extends BaseTool<GlobToolParams, ToolResult> {
static readonly Name = 'glob'; // Keep static name static readonly Name = 'glob'; // Keep static name
/**
* The root directory that this tool is grounded in.
*/
private rootDirectory: string;
/** /**
* Creates a new instance of the GlobLogic * Creates a new instance of the GlobLogic
* @param rootDirectory Root directory to ground this tool in. * @param rootDirectory Root directory to ground this tool in.
*/ */
constructor(rootDirectory: string) { constructor(private rootDirectory: string) {
super( super(
GlobTool.Name, GlobTool.Name,
'FindFiles', // Display name handled by CLI wrapper 'FindFiles', // Display name handled by CLI wrapper

View File

@ -54,13 +54,11 @@ interface GrepMatch {
export class GrepTool extends BaseTool<GrepToolParams, ToolResult> { export class GrepTool extends BaseTool<GrepToolParams, ToolResult> {
static readonly Name = 'search_file_content'; // Keep static name static readonly Name = 'search_file_content'; // Keep static name
private rootDirectory: string;
/** /**
* Creates a new instance of the GrepLogic * Creates a new instance of the GrepLogic
* @param rootDirectory Root directory to ground this tool in. All operations will be restricted to this directory. * @param rootDirectory Root directory to ground this tool in. All operations will be restricted to this directory.
*/ */
constructor(rootDirectory: string) { constructor(private rootDirectory: string) {
super( super(
GrepTool.Name, GrepTool.Name,
'SearchText', 'SearchText',

View File

@ -61,17 +61,11 @@ export interface FileEntry {
export class LSTool extends BaseTool<LSToolParams, ToolResult> { export class LSTool extends BaseTool<LSToolParams, ToolResult> {
static readonly Name = 'list_directory'; static readonly Name = 'list_directory';
/**
* The root directory that this tool is grounded in.
* All path operations will be restricted to this directory.
*/
private rootDirectory: string;
/** /**
* Creates a new instance of the LSLogic * Creates a new instance of the LSLogic
* @param rootDirectory Root directory to ground this tool in. All operations will be restricted to this directory. * @param rootDirectory Root directory to ground this tool in. All operations will be restricted to this directory.
*/ */
constructor(rootDirectory: string) { constructor(private rootDirectory: string) {
super( super(
LSTool.Name, LSTool.Name,
'ReadFolder', 'ReadFolder',

View File

@ -37,9 +37,8 @@ export class ReadFileTool extends BaseTool<ReadFileToolParams, ToolResult> {
static readonly Name: string = 'read_file'; static readonly Name: string = 'read_file';
private static readonly DEFAULT_MAX_LINES = 2000; private static readonly DEFAULT_MAX_LINES = 2000;
private static readonly MAX_LINE_LENGTH = 2000; private static readonly MAX_LINE_LENGTH = 2000;
private rootDirectory: string;
constructor(rootDirectory: string) { constructor(private rootDirectory: string) {
super( super(
ReadFileTool.Name, ReadFileTool.Name,
'ReadFile', 'ReadFile',

View File

@ -116,14 +116,13 @@ export class ReadManyFilesTool extends BaseTool<
ToolResult ToolResult
> { > {
static readonly Name: string = 'read_many_files'; static readonly Name: string = 'read_many_files';
readonly targetDir: string;
/** /**
* Creates an instance of ReadManyFilesTool. * Creates an instance of ReadManyFilesTool.
* @param targetDir The absolute root directory within which this tool is allowed to operate. * @param targetDir The absolute root directory within which this tool is allowed to operate.
* All paths provided in `params` will be resolved relative to this directory. * All paths provided in `params` will be resolved relative to this directory.
*/ */
constructor(targetDir: string) { constructor(readonly targetDir: string) {
const parameterSchema: Record<string, unknown> = { const parameterSchema: Record<string, unknown> = {
type: 'object', type: 'object',
properties: { properties: {

View File

@ -25,10 +25,9 @@ import { spawn } from 'child_process';
export class ShellTool extends BaseTool<ShellToolParams, ToolResult> { export class ShellTool extends BaseTool<ShellToolParams, ToolResult> {
static Name: string = 'execute_bash_command'; static Name: string = 'execute_bash_command';
private readonly config: Config;
private whitelist: Set<string> = new Set(); private whitelist: Set<string> = new Set();
constructor(config: Config) { constructor(private readonly config: Config) {
const toolDisplayName = 'Shell'; const toolDisplayName = 'Shell';
const descriptionUrl = new URL('shell.md', import.meta.url); const descriptionUrl = new URL('shell.md', import.meta.url);
const toolDescription = fs.readFileSync(descriptionUrl, 'utf-8'); const toolDescription = fs.readFileSync(descriptionUrl, 'utf-8');
@ -38,7 +37,6 @@ export class ShellTool extends BaseTool<ShellToolParams, ToolResult> {
toolDescription, toolDescription,
toolParameterSchema, toolParameterSchema,
); );
this.config = config;
} }
getDescription(params: ShellToolParams): string { getDescription(params: ShellToolParams): string {

View File

@ -46,8 +46,6 @@ interface QueuedCommand {
export class TerminalTool extends BaseTool<TerminalToolParams, ToolResult> { export class TerminalTool extends BaseTool<TerminalToolParams, ToolResult> {
static Name: string = 'execute_bash_command'; static Name: string = 'execute_bash_command';
private readonly rootDirectory: string;
private readonly outputLimit: number;
private bashProcess: ChildProcessWithoutNullStreams | null = null; private bashProcess: ChildProcessWithoutNullStreams | null = null;
private currentCwd: string; private currentCwd: string;
private isExecuting: boolean = false; private isExecuting: boolean = false;
@ -58,12 +56,11 @@ export class TerminalTool extends BaseTool<TerminalToolParams, ToolResult> {
private resolveShellReady: (() => void) | undefined; private resolveShellReady: (() => void) | undefined;
private rejectShellReady: ((reason?: unknown) => void) | undefined; private rejectShellReady: ((reason?: unknown) => void) | undefined;
private readonly backgroundTerminalAnalyzer: BackgroundTerminalAnalyzer; private readonly backgroundTerminalAnalyzer: BackgroundTerminalAnalyzer;
private readonly config: Config;
constructor( constructor(
rootDirectory: string, private readonly rootDirectory: string,
config: Config, private readonly config: Config,
outputLimit: number = MAX_OUTPUT_LENGTH, private readonly outputLimit: number = MAX_OUTPUT_LENGTH,
) { ) {
const toolDisplayName = 'Terminal'; const toolDisplayName = 'Terminal';
const toolDescription = `Executes one or more bash commands sequentially in a secure and persistent interactive shell session. Can run commands in the foreground (waiting for completion) or background (returning after launch, with subsequent status polling). const toolDescription = `Executes one or more bash commands sequentially in a secure and persistent interactive shell session. Can run commands in the foreground (waiting for completion) or background (returning after launch, with subsequent status polling).
@ -131,7 +128,6 @@ Use this tool for running build steps (\`npm install\`, \`make\`), linters (\`es
toolDescription, toolDescription,
toolParameterSchema, toolParameterSchema,
); );
this.config = config;
this.rootDirectory = path.resolve(rootDirectory); this.rootDirectory = path.resolve(rootDirectory);
this.currentCwd = this.rootDirectory; this.currentCwd = this.rootDirectory;
this.outputLimit = outputLimit; this.outputLimit = outputLimit;

View File

@ -41,9 +41,7 @@ export class WriteFileTool extends BaseTool<WriteFileToolParams, ToolResult> {
static readonly Name: string = 'write_file'; static readonly Name: string = 'write_file';
private shouldAlwaysWrite = false; private shouldAlwaysWrite = false;
private readonly rootDirectory: string; constructor(private readonly rootDirectory: string) {
constructor(rootDirectory: string) {
super( super(
WriteFileTool.Name, WriteFileTool.Name,
'WriteFile', 'WriteFile',