-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathinit_templates.go
More file actions
190 lines (160 loc) · 5.52 KB
/
init_templates.go
File metadata and controls
190 lines (160 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package cmd
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"azureaiagent/internal/exterrors"
"github.com/azure/azure-dev/cli/azd/pkg/azdext"
)
const agentTemplatesURL = "https://aka.ms/foundry-agents"
// Template type constants
const (
// TemplateTypeAgent is a template that points to an agent.yaml manifest file.
TemplateTypeAgent = "agent"
// TemplateTypeAzd is a full azd template repository.
TemplateTypeAzd = "azd"
)
// AgentTemplate represents an agent template entry from the remote JSON catalog.
type AgentTemplate struct {
Title string `json:"title"`
Description string `json:"description"`
Language string `json:"language"`
Framework string `json:"framework"`
Source string `json:"source"`
Tags []string `json:"tags"`
}
// EffectiveType determines the template type by inspecting the source URL.
// If it ends with agent.yaml or agent.manifest.yaml, it's an agent manifest.
// Otherwise, it's treated as a full azd template repo.
func (t *AgentTemplate) EffectiveType() string {
lower := strings.ToLower(t.Source)
if strings.HasSuffix(lower, "/agent.yaml") ||
strings.HasSuffix(lower, "/agent.manifest.yaml") ||
lower == "agent.yaml" ||
lower == "agent.manifest.yaml" {
return TemplateTypeAgent
}
return TemplateTypeAzd
}
const (
initModeFromCode = "from_code"
initModeTemplate = "template"
)
// promptInitMode asks the user whether to use existing code or start from a template.
// Returns initModeFromCode or initModeTemplate.
func promptInitMode(ctx context.Context, azdClient *azdext.AzdClient) (string, error) {
choices := []*azdext.SelectChoice{
{Label: "Use the code in the current directory", Value: initModeFromCode},
{Label: "Start new from a template", Value: initModeTemplate},
}
resp, err := azdClient.Prompt().Select(ctx, &azdext.SelectRequest{
Options: &azdext.SelectOptions{
Message: "How do you want to initialize your agent?",
Choices: choices,
},
})
if err != nil {
if exterrors.IsCancellation(err) {
return "", exterrors.Cancelled("initialization mode selection was cancelled")
}
return "", fmt.Errorf("failed to prompt for initialization mode: %w", err)
}
return choices[*resp.Value].Value, nil
}
// fetchAgentTemplates retrieves the agent template catalog from the remote JSON URL.
func fetchAgentTemplates(ctx context.Context, httpClient *http.Client) ([]AgentTemplate, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, agentTemplatesURL, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
resp, err := httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to fetch agent templates: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to fetch agent templates: HTTP %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read agent templates response: %w", err)
}
var templates []AgentTemplate
if err := json.Unmarshal(body, &templates); err != nil {
return nil, fmt.Errorf("failed to parse agent templates: %w", err)
}
return templates, nil
}
// promptAgentTemplate guides the user through language selection and template selection.
// Returns the selected AgentTemplate. The caller should check EffectiveType() to determine
// whether to use the agent.yaml manifest flow or the full azd template flow.
func promptAgentTemplate(
ctx context.Context,
azdClient *azdext.AzdClient,
httpClient *http.Client,
) (*AgentTemplate, error) {
fmt.Println("Retrieving agent templates...")
templates, err := fetchAgentTemplates(ctx, httpClient)
if err != nil {
return nil, fmt.Errorf("failed to retrieve agent templates: %w", err)
}
if len(templates) == 0 {
return nil, fmt.Errorf("no agent templates available")
}
// Prompt for language
languageChoices := []*azdext.SelectChoice{
{Label: "Python", Value: "python"},
{Label: "C#", Value: "csharp"},
}
langResp, err := azdClient.Prompt().Select(ctx, &azdext.SelectRequest{
Options: &azdext.SelectOptions{
Message: "Select a language:",
Choices: languageChoices,
},
})
if err != nil {
if exterrors.IsCancellation(err) {
return nil, exterrors.Cancelled("language selection was cancelled")
}
return nil, fmt.Errorf("failed to prompt for language: %w", err)
}
selectedLanguage := languageChoices[*langResp.Value].Value
// Filter templates by selected language
var filtered []AgentTemplate
for _, t := range templates {
if t.Language == selectedLanguage {
filtered = append(filtered, t)
}
}
if len(filtered) == 0 {
return nil, fmt.Errorf("no agent templates available for %s", languageChoices[*langResp.Value].Label)
}
// Build template choices with framework in label
templateChoices := make([]*azdext.SelectChoice, len(filtered))
for i, t := range filtered {
label := fmt.Sprintf("%s (%s)", t.Title, t.Framework)
templateChoices[i] = &azdext.SelectChoice{
Label: label,
Value: fmt.Sprintf("%d", i),
}
}
templateResp, err := azdClient.Prompt().Select(ctx, &azdext.SelectRequest{
Options: &azdext.SelectOptions{
Message: "Select an agent template:",
Choices: templateChoices,
},
})
if err != nil {
if exterrors.IsCancellation(err) {
return nil, exterrors.Cancelled("template selection was cancelled")
}
return nil, fmt.Errorf("failed to prompt for template: %w", err)
}
selectedTemplate := filtered[*templateResp.Value]
return &selectedTemplate, nil
}