-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathcreate_template.go
More file actions
460 lines (434 loc) · 16.1 KB
/
create_template.go
File metadata and controls
460 lines (434 loc) · 16.1 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Copyright 2022-2026 Salesforce, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package project
import (
"context"
"fmt"
"strings"
"time"
"github.com/slackapi/slack-cli/internal/api"
"github.com/slackapi/slack-cli/internal/iostreams"
"github.com/slackapi/slack-cli/internal/pkg/create"
"github.com/slackapi/slack-cli/internal/shared"
"github.com/slackapi/slack-cli/internal/slackerror"
"github.com/slackapi/slack-cli/internal/slacktrace"
"github.com/slackapi/slack-cli/internal/style"
"github.com/spf13/cobra"
)
// getSelectionOptions returns the app template options for a given category.
func getSelectionOptions(categoryID string) []promptObject {
templatePromptObjects := map[string]([]promptObject){
"slack-cli#getting-started": {
{
Title: fmt.Sprintf("Bolt for JavaScript %s", style.Secondary("Node.js")),
Repository: "slack-samples/bolt-js-starter-template",
},
{
Title: fmt.Sprintf("Bolt for Python %s", style.Secondary("Python")),
Repository: "slack-samples/bolt-python-starter-template",
},
},
"slack-cli#ai-apps": {
{
Title: fmt.Sprintf("Starter Agent %s", style.Secondary("Start from scratch")),
Repository: "slack-cli#ai-apps/starter-agent",
},
{
Title: fmt.Sprintf("Support Agent %s", style.Secondary("Resolve IT support cases")),
Repository: "slack-cli#ai-apps/support-agent",
},
},
"slack-cli#automation-apps": {
{
Title: fmt.Sprintf("Bolt for JavaScript %s", style.Secondary("Node.js")),
Repository: "slack-samples/bolt-js-custom-function-template",
},
{
Title: fmt.Sprintf("Bolt for Python %s", style.Secondary("Python")),
Repository: "slack-samples/bolt-python-custom-function-template",
},
{
Title: fmt.Sprintf("Deno Slack SDK %s", style.Secondary("Deno")),
Repository: "slack-samples/deno-starter-template",
},
},
}
return templatePromptObjects[categoryID]
}
// getFrameworkOptions returns the framework choices for a given AI app template.
func getFrameworkOptions(template string) []promptObject {
frameworkPromptObjects := map[string][]promptObject{
"slack-cli#ai-apps/support-agent": {
{
Title: fmt.Sprintf("Bolt for JavaScript %s", style.Secondary("Node.js")),
Repository: "slack-cli#ai-apps/support-agent/bolt-js",
},
{
Title: fmt.Sprintf("Bolt for Python %s", style.Secondary("Python")),
Repository: "slack-cli#ai-apps/support-agent/bolt-python",
},
},
"slack-cli#ai-apps/starter-agent": {
{
Title: fmt.Sprintf("Bolt for JavaScript %s", style.Secondary("Node.js")),
Repository: "slack-cli#ai-apps/starter-agent/bolt-js",
},
{
Title: fmt.Sprintf("Bolt for Python %s", style.Secondary("Python")),
Repository: "slack-cli#ai-apps/starter-agent/bolt-python",
},
},
}
return frameworkPromptObjects[template]
}
// getAdapterOptions returns the AI agent framework choices for a given template and framework.
func getAdapterOptions(framework string) []promptObject {
adapterPromptObjects := map[string][]promptObject{
"slack-cli#ai-apps/support-agent/bolt-js": {
{
Title: fmt.Sprintf("Claude Agent SDK %s", style.Secondary("Bolt for JavaScript - Support Agent Template")),
Repository: "slack-samples/bolt-js-support-agent",
Subdir: "claude-agent-sdk",
},
{
Title: fmt.Sprintf("OpenAI Agents SDK %s", style.Secondary("Bolt for JavaScript - Support Agent Template")),
Repository: "slack-samples/bolt-js-support-agent",
Subdir: "openai-agents-sdk",
},
},
"slack-cli#ai-apps/support-agent/bolt-python": {
{
Title: fmt.Sprintf("Claude Agent SDK %s", style.Secondary("Bolt for Python - Support Agent Template")),
Repository: "slack-samples/bolt-python-support-agent",
Subdir: "claude-agent-sdk",
},
{
Title: fmt.Sprintf("OpenAI Agents SDK %s", style.Secondary("Bolt for Python - Support Agent Template")),
Repository: "slack-samples/bolt-python-support-agent",
Subdir: "openai-agents-sdk",
},
{
Title: fmt.Sprintf("Pydantic AI %s", style.Secondary("Bolt for Python - Support Agent Template")),
Repository: "slack-samples/bolt-python-support-agent",
Subdir: "pydantic-ai",
},
},
"slack-cli#ai-apps/starter-agent/bolt-js": {
{
Title: fmt.Sprintf("Claude Agent SDK %s", style.Secondary("Bolt for JavaScript - Starter Agent Template")),
Repository: "slack-samples/bolt-js-starter-agent",
Subdir: "claude-agent-sdk",
},
{
Title: fmt.Sprintf("OpenAI Agents SDK %s", style.Secondary("Bolt for JavaScript - Starter Agent Template")),
Repository: "slack-samples/bolt-js-starter-agent",
Subdir: "openai-agents-sdk",
},
},
"slack-cli#ai-apps/starter-agent/bolt-python": {
{
Title: fmt.Sprintf("Claude Agent SDK %s", style.Secondary("Bolt for Python - Starter Agent Template")),
Repository: "slack-samples/bolt-python-starter-agent",
Subdir: "claude-agent-sdk",
},
{
Title: fmt.Sprintf("OpenAI Agents SDK %s", style.Secondary("Bolt for Python - Starter Agent Template")),
Repository: "slack-samples/bolt-python-starter-agent",
Subdir: "openai-agents-sdk",
},
{
Title: fmt.Sprintf("Pydantic AI %s", style.Secondary("Bolt for Python - Starter Agent Template")),
Repository: "slack-samples/bolt-python-starter-agent",
Subdir: "pydantic-ai",
},
},
}
return adapterPromptObjects[framework]
}
// getSelectionOptionsForCategory returns the top-level category options for
// the create command template selection.
func getSelectionOptionsForCategory(clients *shared.ClientFactory) []promptObject {
return []promptObject{
{
Title: fmt.Sprintf("Starter app %s", style.Secondary("Getting started Slack app")),
Repository: "slack-cli#getting-started",
},
{
Title: fmt.Sprintf("AI Agent app %s", style.Secondary("Slack agents and assistants")),
Repository: "slack-cli#ai-apps",
},
{
Title: fmt.Sprintf("Automation app %s", style.Secondary("Custom steps and workflows")),
Repository: "slack-cli#automation-apps",
},
{
Title: "View more samples",
Repository: viewMoreSamples,
},
}
}
// promptTemplateSelection prompts the user to select a project template
func promptTemplateSelection(cmd *cobra.Command, clients *shared.ClientFactory, categoryShortcut string) (create.Template, error) {
ctx := cmd.Context()
var categoryID string
// Check if a category shortcut was provided
if categoryShortcut != "" {
switch categoryShortcut {
case "agent":
categoryID = "slack-cli#ai-apps"
default:
return create.Template{}, slackerror.New(slackerror.ErrInvalidArgs).
WithMessage("The %s category was not found", categoryShortcut)
}
} else {
// Prompt for the category
promptForCategory := "Select a category:"
optionsForCategory := getSelectionOptionsForCategory(clients)
titlesForCategory := make([]string, len(optionsForCategory))
for i, m := range optionsForCategory {
titlesForCategory[i] = m.Title
}
templateForCategory := getSelectionTemplate(clients)
// Print a trace with info about the category title options provided by CLI
clients.IO.PrintTrace(ctx, slacktrace.CreateCategoryOptions, strings.Join(titlesForCategory, ", "))
// Prompt to choose a category
selection, err := clients.IO.SelectPrompt(ctx, promptForCategory, titlesForCategory, iostreams.SelectPromptConfig{
Flag: clients.Config.Flags.Lookup("template"),
Required: true,
Template: templateForCategory,
})
if err != nil {
return create.Template{}, slackerror.ToSlackError(err)
} else if selection.Flag {
template, err := create.ResolveTemplateURL(selection.Option)
if err != nil {
return create.Template{}, err
}
confirm, err := confirmExternalTemplateSelection(cmd, clients, template)
if err != nil {
return create.Template{}, slackerror.ToSlackError(err)
} else if !confirm {
return create.Template{}, slackerror.New(slackerror.ErrUntrustedSource)
}
return template, nil
} else if selection.Prompt {
categoryID = optionsForCategory[selection.Index].Repository
}
if categoryID == viewMoreSamples {
sampler := api.NewHTTPClient(api.HTTPClientOptions{
TotalTimeOut: 60 * time.Second,
})
samples, err := create.GetSampleRepos(sampler)
if err != nil {
return create.Template{}, err
}
selectedSample, err := promptSampleSelection(ctx, clients, samples)
if err != nil {
return create.Template{}, err
}
return create.ResolveTemplateURL(selectedSample)
}
}
// Prompt for the example template
prompt := "Select a framework:"
if categoryID == "slack-cli#ai-apps" {
prompt = "Select a template:"
}
options := getSelectionOptions(categoryID)
titles := make([]string, len(options))
for i, m := range options {
titles[i] = m.Title
}
clients.IO.PrintTrace(ctx, slacktrace.CreateTemplateOptions, strings.Join(titles, ", "))
selection, err := clients.IO.SelectPrompt(ctx, prompt, titles, iostreams.SelectPromptConfig{
Description: func(value string, index int) string {
return options[index].Description
},
Required: true,
Template: getSelectionTemplate(clients),
})
if err != nil {
return create.Template{}, err
} else if selection.Flag {
return create.Template{}, slackerror.New(slackerror.ErrPrompt)
} else if selection.Prompt && !strings.HasPrefix(options[selection.Index].Repository, "slack-cli#") {
return create.ResolveTemplateURL(options[selection.Index].Repository)
}
template := options[selection.Index].Repository
// Prompt for the framework
frameworks := getFrameworkOptions(template)
frameworkChoices := make([]string, len(frameworks))
for i, opt := range frameworks {
frameworkChoices[i] = opt.Title
}
frameworkSelection, err := clients.IO.SelectPrompt(ctx, "Select a Bolt framework:", frameworkChoices, iostreams.SelectPromptConfig{
Description: func(value string, index int) string {
return frameworks[index].Description
},
Required: true,
Template: getSelectionTemplate(clients),
})
if err != nil {
return create.Template{}, err
} else if frameworkSelection.Flag {
return create.Template{}, slackerror.New(slackerror.ErrPrompt)
}
framework := frameworks[frameworkSelection.Index]
// Check if there are adapter options for this framework
adapters := getAdapterOptions(framework.Repository)
if len(adapters) > 0 {
adapterChoices := make([]string, len(adapters))
for i, opt := range adapters {
adapterChoices[i] = opt.Title
}
adapterSelection, err := clients.IO.SelectPrompt(ctx, "Select an agent framework:", adapterChoices, iostreams.SelectPromptConfig{
Description: func(value string, index int) string {
return adapters[index].Description
},
Required: true,
Template: getSelectionTemplate(clients),
})
if err != nil {
return create.Template{}, err
} else if adapterSelection.Flag {
return create.Template{}, slackerror.New(slackerror.ErrPrompt)
}
adapter := adapters[adapterSelection.Index]
resolved, err := create.ResolveTemplateURL(adapter.Repository)
if err != nil {
return create.Template{}, err
}
if adapter.Subdir != "" {
resolved.SetSubdir(adapter.Subdir)
}
return resolved, nil
}
// No adapter options - resolve the framework directly
resolved, err := create.ResolveTemplateURL(framework.Repository)
if err != nil {
return create.Template{}, err
}
if framework.Subdir != "" {
resolved.SetSubdir(framework.Subdir)
}
return resolved, nil
}
// confirmExternalTemplateSelection prompts the user to confirm that they want to create an app from
// an external template and saves their preference if they choose to ignore future warnings
func confirmExternalTemplateSelection(cmd *cobra.Command, clients *shared.ClientFactory, template create.Template) (bool, error) {
ctx := cmd.Context()
trustSources, err := clients.Config.SystemConfig.GetTrustUnknownSources(ctx)
if err != nil {
return false, err
}
if trustSources || template.IsTrusted() {
return true, nil
}
clients.IO.PrintWarning(ctx, "%s", style.Sectionf(style.TextSection{
Text: style.Bold("You are trying to use code published by an unknown author"),
Secondary: []string{
"We strongly advise reviewing the source code and dependencies of external",
"projects before use",
},
}))
selection, err := clients.IO.SelectPrompt(ctx, "Proceed?", []string{"Yes", "Yes, don't ask again", "No"}, iostreams.SelectPromptConfig{
Required: true,
Flag: clients.Config.Flags.Lookup("force"),
})
if err != nil {
return false, err
} else if selection.Option == "No" {
return false, nil
} else if selection.Option == "Yes, don't ask again" {
err = clients.Config.SystemConfig.SetTrustUnknownSources(ctx, true)
if err != nil {
return true, slackerror.Wrap(err, "failed to set trust_unknown_sources property to config")
}
}
return true, nil
}
// listTemplates prints available templates for the create command
func listTemplates(ctx context.Context, clients *shared.ClientFactory, categoryShortcut string) error {
type categoryInfo struct {
id string
name string
}
var categories []categoryInfo
if categoryShortcut == "agent" {
categories = []categoryInfo{
{id: "slack-cli#ai-apps/starter-agent", name: "Starter agent"},
{id: "slack-cli#ai-apps/support-agent", name: "Support agent"},
}
} else {
categories = []categoryInfo{
{id: "slack-cli#getting-started", name: "Getting started"},
{id: "slack-cli#ai-apps/starter-agent", name: "Starter agent"},
{id: "slack-cli#ai-apps/support-agent", name: "Support agent"},
{id: "slack-cli#automation-apps", name: "Automation apps"},
}
}
for _, category := range categories {
var secondary []string
if frameworks := getFrameworkOptions(category.id); len(frameworks) > 0 {
for _, fw := range frameworks {
if adapters := getAdapterOptions(fw.Repository); len(adapters) > 0 {
for _, adapter := range adapters {
repo := adapter.Repository
if adapter.Subdir != "" {
repo = fmt.Sprintf("%s --subdir %s", repo, adapter.Subdir)
}
secondary = append(secondary, repo)
}
} else {
secondary = append(secondary, fw.Repository)
}
}
} else {
for _, tmpl := range getSelectionOptions(category.id) {
secondary = append(secondary, tmpl.Repository)
}
}
clients.IO.PrintInfo(ctx, false, "%s", style.Sectionf(style.TextSection{
Emoji: "house_buildings",
Text: style.Bold(category.name),
Secondary: secondary,
}))
}
return nil
}
// getSelectionTemplate returns a custom formatted template used for selecting a
// project template during creation
func getSelectionTemplate(clients *shared.ClientFactory) string {
samplesURL := style.LinkText("https://docs.slack.dev/samples")
return fmt.Sprintf(`
{{- define "option"}}
{{- if eq .SelectedIndex .CurrentIndex }}{{color .Config.Icons.SelectFocus.Format }}{{ .Config.Icons.SelectFocus.Text }} {{else}}{{color "default+hb"}} {{end}}
{{- .CurrentOpt.Value}}{{color "reset"}}{{ if ne ($.GetDescription .CurrentOpt) "" }}{{"\n "}}{{color "250"}}{{ $.GetDescription .CurrentOpt }}{{"\n"}}{{end}}
{{- color "reset"}}
{{end}}
{{- if .ShowHelp }}{{- color .Config.Icons.Help.Format }}{{ .Config.Icons.Help.Text }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}}
{{- color .Config.Icons.Question.Format }}{{ .Config.Icons.Question.Text }} {{color "reset"}}
{{- color "default+hb"}}{{ .Message }}{{color "reset"}}
{{- if .ShowAnswer}}{{color "39+b"}} {{.Answer}}{{color "reset"}}
{{- else}}
{{- " "}}{{- color "39+b"}}[Use arrows to move]{{color "reset"}}
{{- "\n"}}
{{- range $ix, $option := .PageEntries}}
{{- template "option" $.IterateOption $ix $option}}
{{- end}}
{{- "Guided tutorials can be found at %s"}}{{color "reset"}}
{{end}}
`,
samplesURL)
}