mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-03-26 14:01:14 -07:00
chore(tools): add toJSON to tools and invocations to reduce logging verbosity (#22899)
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import {
|
||||
BaseToolInvocation,
|
||||
DeclarativeTool,
|
||||
hasCycleInSchema,
|
||||
Kind,
|
||||
@@ -272,3 +273,55 @@ describe('Tools Read-Only property', () => {
|
||||
expect(searcher.isReadOnly).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('toJSON serialization', () => {
|
||||
it('DeclarativeTool.toJSON should return essential metadata', () => {
|
||||
const bus = createMockMessageBus();
|
||||
class MyTool extends DeclarativeTool<object, ToolResult> {
|
||||
build(_params: object): ToolInvocation<object, ToolResult> {
|
||||
throw new Error('Not implemented');
|
||||
}
|
||||
}
|
||||
const tool = new MyTool(
|
||||
'name',
|
||||
'display',
|
||||
'desc',
|
||||
Kind.Read,
|
||||
{ type: 'object' },
|
||||
bus,
|
||||
);
|
||||
const json = tool.toJSON();
|
||||
|
||||
expect(json).toEqual({
|
||||
name: 'name',
|
||||
displayName: 'display',
|
||||
description: 'desc',
|
||||
kind: Kind.Read,
|
||||
parameterSchema: { type: 'object' },
|
||||
});
|
||||
// Ensure messageBus is NOT included in serialization
|
||||
expect(Object.keys(json)).not.toContain('messageBus');
|
||||
expect(JSON.stringify(tool)).toContain('"name":"name"');
|
||||
expect(JSON.stringify(tool)).not.toContain('messageBus');
|
||||
});
|
||||
|
||||
it('BaseToolInvocation.toJSON should return only params', () => {
|
||||
const bus = createMockMessageBus();
|
||||
const params = { foo: 'bar' };
|
||||
class MyInvocation extends BaseToolInvocation<object, ToolResult> {
|
||||
getDescription() {
|
||||
return 'desc';
|
||||
}
|
||||
async execute() {
|
||||
return { llmContent: '', returnDisplay: '' };
|
||||
}
|
||||
}
|
||||
const invocation = new MyInvocation(params, bus, 'tool');
|
||||
const json = invocation.toJSON();
|
||||
|
||||
expect(json).toEqual({ params });
|
||||
// Ensure messageBus is NOT included in serialization
|
||||
expect(Object.keys(json)).not.toContain('messageBus');
|
||||
expect(JSON.stringify(invocation)).toBe('{"params":{"foo":"bar"}}');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -379,6 +379,12 @@ export abstract class BaseToolInvocation<
|
||||
updateOutput?: (output: ToolLiveOutput) => void,
|
||||
options?: ExecuteOptions,
|
||||
): Promise<TResult>;
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
params: this.params,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -498,6 +504,16 @@ export abstract class DeclarativeTool<
|
||||
return cloned;
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
name: this.name,
|
||||
displayName: this.displayName,
|
||||
description: this.description,
|
||||
kind: this.kind,
|
||||
parameterSchema: this.parameterSchema,
|
||||
};
|
||||
}
|
||||
|
||||
get isReadOnly(): boolean {
|
||||
return READ_ONLY_KINDS.includes(this.kind);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user