mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-23 02:07:52 +00:00
Sync upstream Gemini-CLI v0.8.2 (#838)
This commit is contained in:
39
packages/core/src/output/json-formatter.ts
Normal file
39
packages/core/src/output/json-formatter.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import stripAnsi from 'strip-ansi';
|
||||
import type { SessionMetrics } from '../telemetry/uiTelemetry.js';
|
||||
import type { JsonError, JsonOutput } from './types.js';
|
||||
|
||||
export class JsonFormatter {
|
||||
format(response?: string, stats?: SessionMetrics, error?: JsonError): string {
|
||||
const output: JsonOutput = {};
|
||||
|
||||
if (response !== undefined) {
|
||||
output.response = stripAnsi(response);
|
||||
}
|
||||
|
||||
if (stats) {
|
||||
output.stats = stats;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
output.error = error;
|
||||
}
|
||||
|
||||
return JSON.stringify(output, null, 2);
|
||||
}
|
||||
|
||||
formatError(error: Error, code?: string | number): string {
|
||||
const jsonError: JsonError = {
|
||||
type: error.constructor.name,
|
||||
message: stripAnsi(error.message),
|
||||
...(code && { code }),
|
||||
};
|
||||
|
||||
return this.format(undefined, undefined, jsonError);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user