Skip to content

Commit 0d89224

Browse files
trangeviJeffreyCA
andauthored
Update yaml objects to match maml changes. (#6003)
* Update yaml objects to match maml changes. Update other files to adapt to changes. Signed-off-by: trangevi <[email protected]> * Some more fixes for typing Signed-off-by: trangevi <[email protected]> * Missed one Signed-off-by: trangevi <[email protected]> * remove old file Signed-off-by: trangevi <[email protected]> * Update cli/azd/extensions/azure.foundry.ai.agents/internal/cmd/init.go Co-authored-by: JeffreyCA <[email protected]> * PR Comments Signed-off-by: trangevi <[email protected]> --------- Signed-off-by: trangevi <[email protected]> Co-authored-by: JeffreyCA <[email protected]>
1 parent 73b7edc commit 0d89224

File tree

10 files changed

+737
-461
lines changed

10 files changed

+737
-461
lines changed

cli/azd/extensions/azure.foundry.ai.agents/internal/cmd/init.go

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ func (a *InitAction) downloadAgentYaml(
701701
return nil, "", fmt.Errorf("marshaling agent manifest to YAML after parameter processing: %w", err)
702702
}
703703

704-
agentId := agentManifest.Agent.Name
704+
agentId := agentManifest.Name
705705

706706
// Use targetDir if provided or set to local file pointer, otherwise default to "src/{agentId}"
707707
if targetDir == "" {
@@ -719,12 +719,18 @@ func (a *InitAction) downloadAgentYaml(
719719
return nil, "", fmt.Errorf("saving file to %s: %w", filePath, err)
720720
}
721721

722-
if isGitHubUrl && agentManifest.Agent.Kind == agent_yaml.AgentKindHosted {
723-
// For hosted agents, download the entire parent directory
724-
fmt.Println("Downloading full directory for hosted agent")
725-
err := downloadParentDirectory(ctx, urlInfo, targetDir, ghCli, console)
726-
if err != nil {
727-
return nil, "", fmt.Errorf("downloading parent directory: %w", err)
722+
if isGitHubUrl {
723+
// Check if the template is a HostedContainerAgent or ContainerAgent
724+
_, isHostedContainer := agentManifest.Template.(agent_yaml.HostedContainerAgent)
725+
_, isContainerAgent := agentManifest.Template.(agent_yaml.ContainerAgent)
726+
727+
if isHostedContainer || isContainerAgent {
728+
// For container agents, download the entire parent directory
729+
fmt.Println("Downloading full directory for container agent")
730+
err := downloadParentDirectory(ctx, urlInfo, targetDir, ghCli, console)
731+
if err != nil {
732+
return nil, "", fmt.Errorf("downloading parent directory: %w", err)
733+
}
728734
}
729735
}
730736

@@ -735,15 +741,40 @@ func (a *InitAction) downloadAgentYaml(
735741

736742
func (a *InitAction) addToProject(ctx context.Context, targetDir string, agentManifest *agent_yaml.AgentManifest) error {
737743
var host string
738-
switch agentManifest.Agent.Kind {
744+
745+
// Convert the template to bytes
746+
templateBytes, err := json.Marshal(agentManifest.Template)
747+
if err != nil {
748+
return fmt.Errorf("failed to marshal agent template to JSON: %w", err)
749+
}
750+
751+
// Convert the bytes to a dictionary
752+
var templateDict map[string]interface{}
753+
if err := json.Unmarshal(templateBytes, &templateDict); err != nil {
754+
return fmt.Errorf("failed to unmarshal agent template from JSON: %w", err)
755+
}
756+
757+
// Convert the dictionary to bytes
758+
dictJsonBytes, err := json.Marshal(templateDict)
759+
if err != nil {
760+
return fmt.Errorf("failed to marshal templateDict to JSON: %w", err)
761+
}
762+
763+
// Convert the bytes to an Agent Definition
764+
var agentDef agent_yaml.AgentDefinition
765+
if err := json.Unmarshal(dictJsonBytes, &agentDef); err != nil {
766+
return fmt.Errorf("failed to unmarshal JSON to AgentDefinition: %w", err)
767+
}
768+
769+
switch agentDef.Kind {
739770
case "container":
740771
host = "containerapp"
741772
default:
742-
host = "foundry.agent"
773+
host = "foundry.containeragent"
743774
}
744775

745776
serviceConfig := &azdext.ServiceConfig{
746-
Name: agentManifest.Agent.Name,
777+
Name: strings.ReplaceAll(agentDef.Name, " ", ""),
747778
RelativePath: targetDir,
748779
Host: host,
749780
Language: "python",
@@ -755,7 +786,7 @@ func (a *InitAction) addToProject(ctx context.Context, targetDir string, agentMa
755786
return fmt.Errorf("adding agent service to project: %w", err)
756787
}
757788

758-
fmt.Printf("Added service '%s' to azure.yaml\n", agentManifest.Agent.Name)
789+
fmt.Printf("Added service '%s' to azure.yaml\n", agentDef.Name)
759790
return nil
760791
}
761792

@@ -1222,7 +1253,31 @@ func downloadDirectoryContents(
12221253
// }
12231254

12241255
func (a *InitAction) updateEnvironment(ctx context.Context, agentManifest *agent_yaml.AgentManifest) error {
1225-
fmt.Printf("Updating environment variables for agent kind: %s\n", agentManifest.Agent.Kind)
1256+
// Convert the template to bytes
1257+
templateBytes, err := json.Marshal(agentManifest.Template)
1258+
if err != nil {
1259+
return fmt.Errorf("failed to marshal agent template to JSON: %w", err)
1260+
}
1261+
1262+
// Convert the bytes to a dictionary
1263+
var templateDict map[string]interface{}
1264+
if err := json.Unmarshal(templateBytes, &templateDict); err != nil {
1265+
return fmt.Errorf("failed to unmarshal agent template from JSON: %w", err)
1266+
}
1267+
1268+
// Convert the dictionary to bytes
1269+
dictJsonBytes, err := json.Marshal(templateDict)
1270+
if err != nil {
1271+
return fmt.Errorf("failed to marshal templateDict to JSON: %w", err)
1272+
}
1273+
1274+
// Convert the bytes to an Agent Definition
1275+
var agentDef agent_yaml.AgentDefinition
1276+
if err := json.Unmarshal(dictJsonBytes, &agentDef); err != nil {
1277+
return fmt.Errorf("failed to unmarshal JSON to AgentDefinition: %w", err)
1278+
}
1279+
1280+
fmt.Printf("Updating environment variables for agent kind: %s\n", agentDef.Kind)
12261281

12271282
// Get current environment
12281283
envResponse, err := a.azdClient.Environment().GetCurrent(ctx, &azdext.EmptyRequest{})
@@ -1237,25 +1292,25 @@ func (a *InitAction) updateEnvironment(ctx context.Context, agentManifest *agent
12371292
envName := envResponse.Environment.Name
12381293

12391294
// Set environment variables based on agent kind
1240-
switch agentManifest.Agent.Kind {
1241-
case "hosted":
1295+
switch agentDef.Kind {
1296+
case agent_yaml.AgentKindPrompt:
1297+
agentDef := agentManifest.Template.(agent_yaml.PromptAgent)
1298+
if err := a.setEnvVar(ctx, envName, "AZURE_AI_FOUNDRY_MODEL_NAME", agentDef.Model.Id); err != nil {
1299+
return err
1300+
}
1301+
case agent_yaml.AgentKindHosted:
12421302
// Set environment variables for hosted agents
12431303
if err := a.setEnvVar(ctx, envName, "ENABLE_HOSTED_AGENTS", "true"); err != nil {
12441304
return err
12451305
}
1246-
case "container":
1306+
case agent_yaml.AgentKindYamlContainerApp:
12471307
// Set environment variables for foundry agents
12481308
if err := a.setEnvVar(ctx, envName, "ENABLE_CONTAINER_AGENTS", "true"); err != nil {
12491309
return err
12501310
}
12511311
}
12521312

1253-
// Model information should be set regardless of agent kind
1254-
if err := a.setEnvVar(ctx, envName, "AZURE_AI_FOUNDRY_MODEL_NAME", agentManifest.Agent.Model.Id); err != nil {
1255-
return err
1256-
}
1257-
1258-
fmt.Printf("Successfully updated environment variables for agent kind: %s\n", agentManifest.Agent.Kind)
1313+
fmt.Printf("Successfully updated environment variables for agent kind: %s\n", agentDef.Kind)
12591314
return nil
12601315
}
12611316

0 commit comments

Comments
 (0)