11import { DynamicAgentTemplate } from '@codebuff/common/types/dynamic-agent-template'
22import * as fs from 'fs'
3- import { green , red , yellow , cyan } from 'picocolors'
4- import { websiteUrl } from '../config'
5- import { logger } from '../utils/logger'
6- import { loadLocalAgents } from '../agents/load-agents'
3+ import { cyan , green , red , yellow } from 'picocolors'
74import { getAgentsDirectory } from '../agents/agent-utils'
5+ import { loadLocalAgents } from '../agents/load-agents'
6+ import { websiteUrl } from '../config'
87import { getUserCredentials } from '../credentials'
98
109interface PublishResponse {
@@ -13,6 +12,12 @@ interface PublishResponse {
1312 version ?: string
1413 message ?: string
1514 error ?: string
15+ details ?: string
16+ validationErrors ?: Array < {
17+ code : string
18+ message : string
19+ path : ( string | number ) [ ]
20+ } >
1621}
1722
1823/**
@@ -106,34 +111,25 @@ interface PublishResponse {
106111 `❌ Error publishing ${ template . displayName } : ${ error instanceof Error ? error . message : String ( error ) } `
107112 )
108113 )
109- logger . error (
110- {
111- error : error instanceof Error ? error . message : String ( error ) ,
112- agentId : template . id ,
113- } ,
114- 'Error publishing agent template'
115- )
114+ // Avoid logger.error here as it can cause sonic boom errors that mask the real error
115+ // The error is already displayed to the user via console.log above
116116 }
117117 } catch ( error ) {
118118 console . log (
119119 red (
120- `Error during publish: ${ error instanceof Error ? error . message : String ( error ) } `
120+ `Error during publish: ${ error instanceof Error ? error . message + '\n' + error . stack : String ( error ) } `
121121 )
122122 )
123- logger . error (
124- {
125- error : error instanceof Error ? error . message : String ( error ) ,
126- } ,
127- 'Error during publish command'
128- )
123+ // Avoid logger.error here as it can cause sonic boom errors that mask the real error
124+ // The error is already displayed to the user via console.log above
129125 }
130126}
131127
132128/**
133129 * Publish an agent template to the backend
134130 */
135131async function publishAgentTemplate (
136- template : DynamicAgentTemplate ,
132+ data : DynamicAgentTemplate ,
137133 authToken : string
138134) : Promise < PublishResponse > {
139135 try {
@@ -144,17 +140,47 @@ async function publishAgentTemplate(
144140 Cookie : `next-auth.session-token=${ authToken } ` ,
145141 } ,
146142 body : JSON . stringify ( {
147- template ,
143+ data ,
148144 } ) ,
149145 } )
150146
151- const result = await response . json ( )
147+ let result : any
148+ try {
149+ result = await response . json ( )
150+ } catch ( jsonError ) {
151+ return {
152+ success : false ,
153+ error : `Failed to parse server response: ${ response . status } ${ response . statusText } ` ,
154+ }
155+ }
152156
153157 if ( ! response . ok ) {
158+ // Extract detailed error information from the response
159+ let errorMessage =
160+ result . error || `HTTP ${ response . status } : ${ response . statusText } `
161+
162+ // If there are validation details, include them
163+ if ( result . details ) {
164+ errorMessage += `\n\nDetails: ${ result . details } `
165+ }
166+
167+ // If there are specific validation errors, format them nicely
168+ if ( result . validationErrors && Array . isArray ( result . validationErrors ) ) {
169+ const formattedErrors = result . validationErrors
170+ . map ( ( err : any ) => {
171+ const path =
172+ err . path && err . path . length > 0 ? `${ err . path . join ( '.' ) } : ` : ''
173+ return ` • ${ path } ${ err . message } `
174+ } )
175+ . join ( '\n' )
176+ errorMessage += `\n\nValidation errors:\n${ formattedErrors } `
177+ }
178+
154179 return {
155180 success : false ,
156- error :
157- result . error || `HTTP ${ response . status } : ${ response . statusText } ` ,
181+ error : errorMessage ,
182+ details : result . details ,
183+ validationErrors : result . validationErrors ,
158184 }
159185 }
160186
@@ -165,9 +191,17 @@ async function publishAgentTemplate(
165191 message : result . message ,
166192 }
167193 } catch ( error ) {
194+ // Handle network errors, timeouts, etc.
195+ if ( error instanceof TypeError && error . message . includes ( 'fetch' ) ) {
196+ return {
197+ success : false ,
198+ error : `Network error: Unable to connect to ${ websiteUrl } . Please check your internet connection and try again.` ,
199+ }
200+ }
201+
168202 return {
169203 success : false ,
170- error : error instanceof Error ? error . message : String ( error ) ,
204+ error : `Unexpected error: ${ error instanceof Error ? error . message : String ( error ) } ` ,
171205 }
172206 }
173207}
0 commit comments