11import { APP_DIR , ConfigIO , NoProjectError , findConfigRoot , setEnvVar } from '../../../../lib' ;
22import type { AgentEnvSpec , DirectoryPath , FilePath } from '../../../../schema' ;
3- import { getErrorMessage } from '../../../errors' ;
43import { type PythonSetupResult , setupPythonProject } from '../../../operations' ;
54import {
65 mapGenerateConfigToRenderConfig ,
@@ -12,6 +11,19 @@ import { executeImportAgent } from '../../../operations/agent/import';
1211import { buildAuthorizerConfigFromJwtConfig , createManagedOAuthCredential } from '../../../primitives/auth-utils' ;
1312import { computeDefaultCredentialEnvVarName } from '../../../primitives/credential-utils' ;
1413import { credentialPrimitive } from '../../../primitives/registry' ;
14+ import { withAddTelemetry } from '../../../telemetry/cli-command-run.js' ;
15+ import {
16+ AgentType as AgentTypeEnum ,
17+ AuthorizerType as AuthorizerTypeEnum ,
18+ Build ,
19+ Framework ,
20+ Language ,
21+ Memory as MemoryEnum ,
22+ ModelProvider ,
23+ NetworkMode ,
24+ Protocol ,
25+ standardize ,
26+ } from '../../../telemetry/schemas/common-shapes.js' ;
1527import { createRenderer } from '../../../templates' ;
1628import type { GenerateConfig } from '../generate/types' ;
1729import type { AddAgentConfig } from './types' ;
@@ -135,34 +147,25 @@ export function useAddAgent() {
135147 const addAgent = useCallback ( async ( config : AddAgentConfig ) : Promise < AddAgentOutcome > => {
136148 setIsLoading ( true ) ;
137149 try {
138- const configBaseDir = findConfigRoot ( ) ;
139- if ( ! configBaseDir ) {
140- return { ok : false , error : new NoProjectError ( ) . message } ;
141- }
142-
143- const configIO = new ConfigIO ( { baseDir : configBaseDir } ) ;
144-
145- if ( ! configIO . configExists ( 'project' ) ) {
146- return { ok : false , error : new NoProjectError ( ) . message } ;
147- }
148-
149- // Check for duplicate agent name
150- const project = await configIO . readProjectSpec ( ) ;
151- const existingAgent = project . runtimes . find ( agent => agent . name === config . name ) ;
152- if ( existingAgent ) {
153- return { ok : false , error : `Agent "${ config . name } " already exists in this project.` } ;
154- }
155-
156- // Branch based on agent type
157- if ( config . agentType === 'import' ) {
158- return await handleImportPath ( config , configBaseDir ) ;
159- } else if ( config . agentType === 'create' ) {
160- return await handleCreatePath ( config , configBaseDir ) ;
161- } else {
162- return await handleByoPath ( config , configIO , configBaseDir ) ;
150+ const result = await withAddTelemetry (
151+ 'add.agent' ,
152+ {
153+ language : standardize ( Language , config . language ) ,
154+ framework : standardize ( Framework , config . framework ) ,
155+ model_provider : standardize ( ModelProvider , config . modelProvider ) ,
156+ agent_type : standardize ( AgentTypeEnum , config . agentType ) ,
157+ build : standardize ( Build , config . buildType ) ,
158+ protocol : standardize ( Protocol , config . protocol ?? 'HTTP' ) ,
159+ network_mode : standardize ( NetworkMode , config . networkMode ?? 'PUBLIC' ) ,
160+ authorizer_type : standardize ( AuthorizerTypeEnum , config . authorizerType ?? 'NONE' ) ,
161+ memory : standardize ( MemoryEnum , config . memory ?? 'none' ) ,
162+ } ,
163+ ( ) => addAgentInner ( config )
164+ ) ;
165+ if ( ! result . success ) {
166+ return { ok : false , error : result . error } ;
163167 }
164- } catch ( err ) {
165- return { ok : false , error : getErrorMessage ( err ) } ;
168+ return result . outcome ;
166169 } finally {
167170 setIsLoading ( false ) ;
168171 }
@@ -175,6 +178,43 @@ export function useAddAgent() {
175178 return { addAgent, isLoading, reset } ;
176179}
177180
181+ type AddAgentInnerResult =
182+ | { success : true ; outcome : AddAgentCreateResult | AddAgentByoResult }
183+ | { success : false ; error : string } ;
184+
185+ async function addAgentInner ( config : AddAgentConfig ) : Promise < AddAgentInnerResult > {
186+ const configBaseDir = findConfigRoot ( ) ;
187+ if ( ! configBaseDir ) {
188+ return { success : false , error : new NoProjectError ( ) . message } ;
189+ }
190+
191+ const configIO = new ConfigIO ( { baseDir : configBaseDir } ) ;
192+
193+ if ( ! configIO . configExists ( 'project' ) ) {
194+ return { success : false , error : new NoProjectError ( ) . message } ;
195+ }
196+
197+ const project = await configIO . readProjectSpec ( ) ;
198+ const existingAgent = project . runtimes . find ( agent => agent . name === config . name ) ;
199+ if ( existingAgent ) {
200+ return { success : false , error : `Agent "${ config . name } " already exists in this project.` } ;
201+ }
202+
203+ let outcome : AddAgentCreateResult | AddAgentByoResult | AddAgentError ;
204+ if ( config . agentType === 'import' ) {
205+ outcome = await handleImportPath ( config , configBaseDir ) ;
206+ } else if ( config . agentType === 'create' ) {
207+ outcome = await handleCreatePath ( config , configBaseDir ) ;
208+ } else {
209+ outcome = await handleByoPath ( config , configIO , configBaseDir ) ;
210+ }
211+
212+ if ( ! outcome . ok ) {
213+ return { success : false , error : outcome . error } ;
214+ }
215+ return { success : true , outcome } ;
216+ }
217+
178218/**
179219 * Handle the "create" path: generate agent from template and write to project.
180220 */
0 commit comments