@@ -39,6 +39,7 @@ const AUTH_ENV_API_KEY = "AGENTICFLOW_PUBLIC_API_KEY";
3939const DOCTOR_SCHEMA_VERSION = "agenticflow.doctor.v1" ;
4040const CATALOG_EXPORT_SCHEMA_VERSION = "agenticflow.catalog.export.v1" ;
4141const CATALOG_RANK_SCHEMA_VERSION = "agenticflow.catalog.rank.v1" ;
42+ const ERROR_SCHEMA_VERSION = "agenticflow.error.v1" ;
4243
4344// ═══════════════════════════════════════════════════════════════════
4445// Helpers
@@ -48,6 +49,64 @@ function printJson(data: unknown): void {
4849 console . log ( JSON . stringify ( data , null , 2 ) ) ;
4950}
5051
52+ function isJsonFlagEnabled ( ) : boolean {
53+ return process . argv . includes ( "--json" ) ;
54+ }
55+
56+ function printError ( code : string , message : string , hint ?: string , details ?: unknown ) : void {
57+ if ( isJsonFlagEnabled ( ) ) {
58+ const payload : Record < string , unknown > = {
59+ schema : ERROR_SCHEMA_VERSION ,
60+ code,
61+ message,
62+ } ;
63+ if ( hint ) payload [ "hint" ] = hint ;
64+ if ( details !== undefined ) payload [ "details" ] = details ;
65+ printJson ( payload ) ;
66+ return ;
67+ }
68+
69+ console . error ( `Error: ${ message } ` ) ;
70+ if ( hint ) console . error ( `Hint: ${ hint } ` ) ;
71+ }
72+
73+ function fail ( code : string , message : string , hint ?: string , details ?: unknown ) : never {
74+ printError ( code , message , hint , details ) ;
75+ process . exit ( 1 ) ;
76+ }
77+
78+ function parseOptionalInteger (
79+ rawValue : string | undefined ,
80+ optionName : string ,
81+ minimum : number ,
82+ ) : number | undefined {
83+ if ( rawValue == null ) return undefined ;
84+ const value = rawValue . trim ( ) ;
85+ if ( ! / ^ - ? \d + $ / . test ( value ) ) {
86+ const floor = minimum === 0 ? "0 or higher" : `${ minimum } or higher` ;
87+ fail (
88+ "invalid_option_value" ,
89+ `Invalid value for ${ optionName } : ${ rawValue } ` ,
90+ `Use an integer ${ floor } .` ,
91+ ) ;
92+ }
93+
94+ const parsed = Number . parseInt ( value , 10 ) ;
95+ if ( ! Number . isFinite ( parsed ) || parsed < minimum ) {
96+ const floor = minimum === 0 ? "0 or higher" : `${ minimum } or higher` ;
97+ fail (
98+ "invalid_option_value" ,
99+ `Invalid value for ${ optionName } : ${ rawValue } ` ,
100+ `Use an integer ${ floor } .` ,
101+ ) ;
102+ }
103+ return parsed ;
104+ }
105+
106+ function shouldUseColor ( parentOpts : { color ?: boolean } ) : boolean {
107+ return process . stdout . isTTY && process . stderr . isTTY && parentOpts . color !== false && ! ( "NO_COLOR" in process . env ) ;
108+ }
109+
51110/** Print an SDK result in CLI-friendly format. */
52111function printResult ( data : unknown ) : void {
53112 printJson ( data ) ;
@@ -108,8 +167,8 @@ async function run(fn: () => Promise<unknown>): Promise<void> {
108167 const result = await fn ( ) ;
109168 printResult ( result ) ;
110169 } catch ( err ) {
111- console . error ( `Error: ${ err instanceof Error ? err . message : err } ` ) ;
112- process . exit ( 1 ) ;
170+ const message = err instanceof Error ? err . message : String ( err ) ;
171+ fail ( "request_failed" , message ) ;
113172 }
114173}
115174
0 commit comments