Adding session id as part of json o/p (#14504)

This commit is contained in:
Jainam M
2025-12-04 22:36:20 +05:30
committed by GitHub
parent 84f521b1c6
commit 8b0a8f47c1
8 changed files with 126 additions and 27 deletions
+25 -3
View File
@@ -637,7 +637,11 @@ describe('runNonInteractive', () => {
);
expect(processStdoutSpy).toHaveBeenCalledWith(
JSON.stringify(
{ response: 'Hello World', stats: MOCK_SESSION_METRICS },
{
session_id: 'test-session-id',
response: 'Hello World',
stats: MOCK_SESSION_METRICS,
},
null,
2,
),
@@ -720,7 +724,15 @@ describe('runNonInteractive', () => {
// This should output JSON with empty response but include stats
expect(processStdoutSpy).toHaveBeenCalledWith(
JSON.stringify({ response: '', stats: MOCK_SESSION_METRICS }, null, 2),
JSON.stringify(
{
session_id: 'test-session-id',
response: '',
stats: MOCK_SESSION_METRICS,
},
null,
2,
),
);
});
@@ -755,7 +767,15 @@ describe('runNonInteractive', () => {
// This should output JSON with empty response but include stats
expect(processStdoutSpy).toHaveBeenCalledWith(
JSON.stringify({ response: '', stats: MOCK_SESSION_METRICS }, null, 2),
JSON.stringify(
{
session_id: 'test-session-id',
response: '',
stats: MOCK_SESSION_METRICS,
},
null,
2,
),
);
});
@@ -792,6 +812,7 @@ describe('runNonInteractive', () => {
expect(consoleErrorJsonSpy).toHaveBeenCalledWith(
JSON.stringify(
{
session_id: 'test-session-id',
error: {
type: 'Error',
message: 'Invalid input provided',
@@ -837,6 +858,7 @@ describe('runNonInteractive', () => {
expect(consoleErrorJsonSpy).toHaveBeenCalledWith(
JSON.stringify(
{
session_id: 'test-session-id',
error: {
type: 'FatalInputError',
message: 'Invalid command syntax provided',
+3 -1
View File
@@ -428,7 +428,9 @@ export async function runNonInteractive({
} else if (config.getOutputFormat() === OutputFormat.JSON) {
const formatter = new JsonFormatter();
const stats = uiTelemetryService.getMetrics();
textOutput.write(formatter.format(responseText, stats));
textOutput.write(
formatter.format(config.getSessionId(), responseText, stats),
);
} else {
textOutput.ensureTrailingNewline(); // Ensure a final newline
}
+23 -11
View File
@@ -29,18 +29,20 @@ vi.mock('@google/gemini-cli-core', async (importOriginal) => {
return `API Error: ${String(error)}`;
}),
JsonFormatter: vi.fn().mockImplementation(() => ({
formatError: vi.fn((error: Error, code?: string | number) =>
JSON.stringify(
{
error: {
type: error.constructor.name,
message: error.message,
...(code && { code }),
formatError: vi.fn(
(error: Error, code?: string | number, sessionId?: string) =>
JSON.stringify(
{
...(sessionId && { session_id: sessionId }),
error: {
type: error.constructor.name,
message: error.message,
...(code && { code }),
},
},
},
null,
2,
),
null,
2,
),
),
})),
StreamJsonFormatter: vi.fn().mockImplementation(() => ({
@@ -77,6 +79,8 @@ describe('errors', () => {
let processExitSpy: MockInstance;
let consoleErrorSpy: MockInstance;
const TEST_SESSION_ID = 'test-session-123';
beforeEach(() => {
// Reset mocks
vi.clearAllMocks();
@@ -93,6 +97,7 @@ describe('errors', () => {
mockConfig = {
getOutputFormat: vi.fn().mockReturnValue(OutputFormat.TEXT),
getContentGeneratorConfig: vi.fn().mockReturnValue({ authType: 'test' }),
getSessionId: vi.fn().mockReturnValue(TEST_SESSION_ID),
} as unknown as Config;
});
@@ -166,6 +171,7 @@ describe('errors', () => {
expect(consoleErrorSpy).toHaveBeenCalledWith(
JSON.stringify(
{
session_id: TEST_SESSION_ID,
error: {
type: 'Error',
message: 'Test error',
@@ -188,6 +194,7 @@ describe('errors', () => {
expect(consoleErrorSpy).toHaveBeenCalledWith(
JSON.stringify(
{
session_id: TEST_SESSION_ID,
error: {
type: 'Error',
message: 'Test error',
@@ -210,6 +217,7 @@ describe('errors', () => {
expect(consoleErrorSpy).toHaveBeenCalledWith(
JSON.stringify(
{
session_id: TEST_SESSION_ID,
error: {
type: 'FatalInputError',
message: 'Fatal error',
@@ -246,6 +254,7 @@ describe('errors', () => {
expect(consoleErrorSpy).toHaveBeenCalledWith(
JSON.stringify(
{
session_id: TEST_SESSION_ID,
error: {
type: 'Error',
message: 'Error with status',
@@ -398,6 +407,7 @@ describe('errors', () => {
expect(consoleErrorSpy).toHaveBeenCalledWith(
JSON.stringify(
{
session_id: TEST_SESSION_ID,
error: {
type: 'FatalToolExecutionError',
message: 'Error executing tool test-tool: Tool failed',
@@ -467,6 +477,7 @@ describe('errors', () => {
expect(consoleErrorSpy).toHaveBeenCalledWith(
JSON.stringify(
{
session_id: TEST_SESSION_ID,
error: {
type: 'FatalCancellationError',
message: 'Operation cancelled.',
@@ -529,6 +540,7 @@ describe('errors', () => {
expect(consoleErrorSpy).toHaveBeenCalledWith(
JSON.stringify(
{
session_id: TEST_SESSION_ID,
error: {
type: 'FatalTurnLimitedError',
message:
+4
View File
@@ -100,6 +100,7 @@ export function handleError(
const formattedError = formatter.formatError(
error instanceof Error ? error : new Error(getErrorMessage(error)),
errorCode,
config.getSessionId(),
);
console.error(formattedError);
@@ -152,6 +153,7 @@ export function handleToolError(
const formattedError = formatter.formatError(
toolExecutionError,
errorType ?? toolExecutionError.exitCode,
config.getSessionId(),
);
console.error(formattedError);
} else {
@@ -191,6 +193,7 @@ export function handleCancellationError(config: Config): never {
const formattedError = formatter.formatError(
cancellationError,
cancellationError.exitCode,
config.getSessionId(),
);
console.error(formattedError);
@@ -231,6 +234,7 @@ export function handleMaxTurnsExceededError(config: Config): never {
const formattedError = formatter.formatError(
maxTurnsError,
maxTurnsError.exitCode,
config.getSessionId(),
);
console.error(formattedError);