| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "flag" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "os/signal" |
| 9 | "path/filepath" |
| 10 | "sort" |
| 11 | "strings" |
| 12 | "syscall" |
| 13 | |
| 14 | "reasonix/internal/boot" |
| 15 | "reasonix/internal/command" |
| 16 | "reasonix/internal/config" |
| 17 | "reasonix/internal/control" |
| 18 | "reasonix/internal/event" |
| 19 | "reasonix/internal/skill" |
| 20 | ) |
| 21 | |
| 22 | var setupSubagentCommand = func(ctx context.Context, modelName string, maxStepsOverride int, requireKey bool, sink event.Sink, workspaceRoot string) (*control.Controller, error) { |
| 23 | return setupProfile(ctx, modelName, maxStepsOverride, requireKey, sink, "", workspaceRoot) |
| 24 | } |
| 25 | |
| 26 | const subagentUsageText = `usage: |
| 27 | reasonix subagent list [--dir PATH] |
| 28 | reasonix subagent create <name> --description TEXT (--prompt TEXT | --prompt-file PATH) [--scope project|global] [--model REF] [--effort LEVEL] [--tools a,b] [--color NAME] [--dir PATH] |
| 29 | reasonix subagent edit <name> [--description TEXT] [--prompt TEXT | --prompt-file PATH] [--model REF] [--effort LEVEL] [--tools a,b] [--color NAME] [--dir PATH] |
| 30 | reasonix subagent delete <name> --yes [--dir PATH] |
| 31 | reasonix subagent try <name> [--model REF] [--max-steps N] [--dir PATH] <task> |
| 32 | reasonix subagent run <name> [--model REF] [--max-steps N] [--dir PATH] <task> |
| 33 | |
| 34 | Use --prompt-file - or pipe stdin to read a system prompt from stdin. |
| 35 | ` |
| 36 | |
| 37 | func subagentCommand(args []string) int { |
| 38 | if len(args) == 0 { |
| 39 | fmt.Fprint(os.Stderr, subagentUsageText) |
| 40 | return 2 |
| 41 | } |
| 42 | switch strings.ToLower(args[0]) { |
| 43 | case "list", "ls": |
| 44 | return subagentListCommand(args[1:]) |
| 45 | case "create", "new": |
| 46 | return subagentCreateCommand(args[1:]) |
| 47 | case "edit", "update": |
| 48 | return subagentEditCommand(args[1:]) |
| 49 | case "delete", "remove", "rm": |
| 50 | return subagentDeleteCommand(args[1:]) |
| 51 | case "try": |
| 52 | return subagentRunCommand(args[1:], true) |
| 53 | case "run": |
| 54 | return subagentRunCommand(args[1:], false) |
| 55 | case "help", "--help", "-h": |
| 56 | fmt.Print(subagentUsageText) |
| 57 | return 0 |
| 58 | default: |
| 59 | fmt.Fprintf(os.Stderr, "unknown subagent command %q\n\n%s", args[0], subagentUsageText) |
| 60 | return 2 |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func subagentListCommand(args []string) int { |
| 65 | fs := flag.NewFlagSet("subagent list", flag.ContinueOnError) |
| 66 | dir := fs.String("dir", "", "project root") |
| 67 | if code, ok := parseCommandFlags(fs, args); !ok { |
| 68 | return code |
| 69 | } |
| 70 | if len(fs.Args()) != 0 { |
| 71 | fmt.Fprint(os.Stderr, subagentUsageText) |
| 72 | return 2 |
| 73 | } |
| 74 | if rc := chdirTo(*dir); rc != 0 { |
| 75 | return rc |
| 76 | } |
| 77 | profiles := subagentProfiles(newCLISubagentStore().SlashList()) |
| 78 | cfg, _ := config.Load() |
| 79 | if len(profiles) == 0 { |
| 80 | fmt.Println("no subagent profiles found") |
| 81 | return 0 |
| 82 | } |
| 83 | for _, sk := range profiles { |
| 84 | attributes := []string{string(sk.Scope)} |
| 85 | if sk.Invocation == "manual" { |
| 86 | attributes = append(attributes, "manual") |
| 87 | } |
| 88 | if sk.ReadOnly { |
| 89 | attributes = append(attributes, "read-only") |
| 90 | } |
| 91 | model := sk.Model |
| 92 | effort := sk.Effort |
| 93 | if cfg != nil { |
| 94 | if override := subagentOverride(cfg.Agent.SubagentModels, sk.Name); override != "" { |
| 95 | model = override |
| 96 | } |
| 97 | if override := subagentOverride(cfg.Agent.SubagentEfforts, sk.Name); override != "" { |
| 98 | effort = override |
| 99 | } |
| 100 | } |
| 101 | if model != "" { |
| 102 | attributes = append(attributes, "model="+model) |
| 103 | } |
| 104 | if effort != "" { |
| 105 | attributes = append(attributes, "effort="+effort) |
| 106 | } |
| 107 | fmt.Printf("%-40s %-28s %s\n", sk.SlashName(), "["+strings.Join(attributes, ", ")+"]", sk.Description) |
| 108 | } |
| 109 | return 0 |
| 110 | } |
| 111 | |
| 112 | type optionalString struct { |
| 113 | value string |
| 114 | set bool |
| 115 | } |
| 116 | |
| 117 | func (v *optionalString) String() string { return v.value } |
| 118 | func (v *optionalString) Set(value string) error { |
| 119 | v.value = value |
| 120 | v.set = true |
| 121 | return nil |
| 122 | } |
| 123 | |
| 124 | type subagentProfileFlags struct { |
| 125 | description optionalString |
| 126 | prompt optionalString |
| 127 | promptFile optionalString |
| 128 | model optionalString |
| 129 | effort optionalString |
| 130 | tools optionalString |
| 131 | color optionalString |
| 132 | dir string |
| 133 | } |
| 134 | |
| 135 | func addSubagentProfileFlags(fs *flag.FlagSet, values *subagentProfileFlags) { |
| 136 | fs.Var(&values.description, "description", "one-line profile description") |
| 137 | fs.Var(&values.prompt, "prompt", "subagent system prompt") |
| 138 | fs.Var(&values.promptFile, "prompt-file", "read system prompt from a file (- for stdin)") |
| 139 | fs.Var(&values.model, "model", "per-profile model reference (empty clears on edit)") |
| 140 | fs.Var(&values.effort, "effort", "per-profile reasoning effort (empty clears on edit)") |
| 141 | fs.Var(&values.tools, "tools", "comma-separated allowed tools (empty means all tools)") |
| 142 | fs.Var(&values.color, "color", "profile color tag (empty clears on edit)") |
| 143 | fs.StringVar(&values.dir, "dir", "", "project root") |
| 144 | } |
| 145 | |
| 146 | func reportNamedSubagentHelp(args []string) bool { |
| 147 | if !commandHelpRequested(args, 1) { |
| 148 | return false |
| 149 | } |
| 150 | fmt.Fprint(os.Stdout, subagentUsageText) |
| 151 | return true |
| 152 | } |
| 153 | |
| 154 | func subagentCreateCommand(args []string) int { |
| 155 | if reportNamedSubagentHelp(args) { |
| 156 | return 0 |
| 157 | } |
| 158 | name, rest, ok := namedSubagentArgs(args) |
| 159 | if !ok { |
| 160 | fmt.Fprint(os.Stderr, subagentUsageText) |
| 161 | return 2 |
| 162 | } |
| 163 | fs := flag.NewFlagSet("subagent create", flag.ContinueOnError) |
| 164 | var values subagentProfileFlags |
| 165 | addSubagentProfileFlags(fs, &values) |
| 166 | scopeText := fs.String("scope", "", "project or global (default: project)") |
| 167 | if code, ok := parseCommandFlags(fs, rest); !ok { |
| 168 | return code |
| 169 | } |
| 170 | if len(fs.Args()) != 0 { |
| 171 | return 2 |
| 172 | } |
| 173 | if rc := chdirTo(values.dir); rc != 0 { |
| 174 | return rc |
| 175 | } |
| 176 | if !values.description.set || strings.TrimSpace(values.description.value) == "" { |
| 177 | fmt.Fprintln(os.Stderr, "subagent create: --description is required") |
| 178 | return 2 |
| 179 | } |
| 180 | prompt, changed, err := resolveSubagentPrompt(values.prompt, values.promptFile) |
| 181 | if err != nil { |
| 182 | fmt.Fprintln(os.Stderr, "subagent create:", err) |
| 183 | return 2 |
| 184 | } |
| 185 | if !changed { |
| 186 | prompt = readStdin() |
| 187 | changed = strings.TrimSpace(prompt) != "" |
| 188 | } |
| 189 | if !changed || strings.TrimSpace(prompt) == "" { |
| 190 | fmt.Fprintln(os.Stderr, "subagent create: --prompt or --prompt-file is required") |
| 191 | return 2 |
| 192 | } |
| 193 | store := newCLISubagentStore() |
| 194 | scope, err := profileCreateScope(*scopeText, store.HasProjectScope()) |
| 195 | if err != nil { |
| 196 | fmt.Fprintln(os.Stderr, "subagent create:", err) |
| 197 | return 2 |
| 198 | } |
| 199 | if err := refuseSubagentNameCollision(store.List(), name); err != nil { |
| 200 | fmt.Fprintln(os.Stderr, "subagent create:", err) |
| 201 | return 1 |
| 202 | } |
| 203 | content := renderCLIProfile(name, values.description.value, prompt, values.model.value, values.effort.value, parseToolList(values.tools.value), values.color.value, false) |
| 204 | path, err := store.CreateWithContent(name, scope, content) |
| 205 | if err != nil { |
| 206 | fmt.Fprintln(os.Stderr, "subagent create:", err) |
| 207 | return 1 |
| 208 | } |
| 209 | fmt.Printf("created subagent profile %q at %s\n", name, path) |
| 210 | return 0 |
| 211 | } |
| 212 | |
| 213 | func subagentEditCommand(args []string) int { |
| 214 | if reportNamedSubagentHelp(args) { |
| 215 | return 0 |
| 216 | } |
| 217 | name, rest, ok := namedSubagentArgs(args) |
| 218 | if !ok { |
| 219 | fmt.Fprint(os.Stderr, subagentUsageText) |
| 220 | return 2 |
| 221 | } |
| 222 | fs := flag.NewFlagSet("subagent edit", flag.ContinueOnError) |
| 223 | var values subagentProfileFlags |
| 224 | addSubagentProfileFlags(fs, &values) |
| 225 | if code, ok := parseCommandFlags(fs, rest); !ok { |
| 226 | return code |
| 227 | } |
| 228 | if len(fs.Args()) != 0 { |
| 229 | return 2 |
| 230 | } |
| 231 | if rc := chdirTo(values.dir); rc != 0 { |
| 232 | return rc |
| 233 | } |
| 234 | if !profileFlagsChanged(values) { |
| 235 | fmt.Fprintln(os.Stderr, "subagent edit: provide at least one field to update") |
| 236 | return 2 |
| 237 | } |
| 238 | store := newCLISubagentStore() |
| 239 | sk, ok := store.Read(name) |
| 240 | if !ok { |
| 241 | fmt.Fprintf(os.Stderr, "subagent edit: unknown profile %q\n", name) |
| 242 | return 1 |
| 243 | } |
| 244 | if sk.Scope == skill.ScopeBuiltin { |
| 245 | if err := editBuiltinSubagentProfile(sk, values); err != nil { |
| 246 | fmt.Fprintln(os.Stderr, "subagent edit:", err) |
| 247 | return 1 |
| 248 | } |
| 249 | fmt.Printf("updated built-in subagent profile %q overrides\n", sk.Name) |
| 250 | return 0 |
| 251 | } |
| 252 | if err := skill.ValidateEditableSubagentProfile(sk); err != nil { |
| 253 | fmt.Fprintln(os.Stderr, "subagent edit:", err) |
| 254 | return 1 |
| 255 | } |
| 256 | prompt, promptChanged, err := resolveSubagentPrompt(values.prompt, values.promptFile) |
| 257 | if err != nil { |
| 258 | fmt.Fprintln(os.Stderr, "subagent edit:", err) |
| 259 | return 2 |
| 260 | } |
| 261 | description, body := sk.Description, sk.Body |
| 262 | model, effort, color := sk.Model, sk.Effort, sk.Color |
| 263 | tools := append([]string(nil), sk.AllowedTools...) |
| 264 | if values.description.set { |
| 265 | description = values.description.value |
| 266 | } |
| 267 | if promptChanged { |
| 268 | body = prompt |
| 269 | } |
| 270 | if values.model.set { |
| 271 | model = values.model.value |
| 272 | } |
| 273 | if values.effort.set { |
| 274 | effort = values.effort.value |
| 275 | } |
| 276 | if values.tools.set { |
| 277 | tools = parseToolList(values.tools.value) |
| 278 | } |
| 279 | if values.color.set { |
| 280 | color = values.color.value |
| 281 | } |
| 282 | if strings.TrimSpace(description) == "" || strings.TrimSpace(body) == "" { |
| 283 | fmt.Fprintln(os.Stderr, "subagent edit: description and prompt cannot be empty") |
| 284 | return 2 |
| 285 | } |
| 286 | content := renderCLIProfile(sk.Name, description, body, model, effort, tools, color, sk.ReadOnly) |
| 287 | if err := store.UpdateContent(sk.Name, sk.Scope, content); err != nil { |
| 288 | fmt.Fprintln(os.Stderr, "subagent edit:", err) |
| 289 | return 1 |
| 290 | } |
| 291 | fmt.Printf("updated subagent profile %q\n", sk.Name) |
| 292 | return 0 |
| 293 | } |
| 294 | |
| 295 | func subagentDeleteCommand(args []string) int { |
| 296 | if reportNamedSubagentHelp(args) { |
| 297 | return 0 |
| 298 | } |
| 299 | name, rest, ok := namedSubagentArgs(args) |
| 300 | if !ok { |
| 301 | fmt.Fprint(os.Stderr, subagentUsageText) |
| 302 | return 2 |
| 303 | } |
| 304 | fs := flag.NewFlagSet("subagent delete", flag.ContinueOnError) |
| 305 | yes := fs.Bool("yes", false, "confirm deletion") |
| 306 | dir := fs.String("dir", "", "project root") |
| 307 | if code, ok := parseCommandFlags(fs, rest); !ok { |
| 308 | return code |
| 309 | } |
| 310 | if len(fs.Args()) != 0 { |
| 311 | return 2 |
| 312 | } |
| 313 | if !*yes { |
| 314 | fmt.Fprintln(os.Stderr, "subagent delete: pass --yes to confirm") |
| 315 | return 2 |
| 316 | } |
| 317 | if rc := chdirTo(*dir); rc != 0 { |
| 318 | return rc |
| 319 | } |
| 320 | store := newCLISubagentStore() |
| 321 | sk, ok := store.Read(name) |
| 322 | if !ok { |
| 323 | fmt.Fprintf(os.Stderr, "subagent delete: unknown profile %q\n", name) |
| 324 | return 1 |
| 325 | } |
| 326 | if err := skill.ValidateEditableSubagentProfile(sk); err != nil { |
| 327 | fmt.Fprintln(os.Stderr, "subagent delete:", err) |
| 328 | return 1 |
| 329 | } |
| 330 | if err := store.Delete(sk.Name, sk.Scope); err != nil { |
| 331 | fmt.Fprintln(os.Stderr, "subagent delete:", err) |
| 332 | return 1 |
| 333 | } |
| 334 | fmt.Printf("deleted subagent profile %q\n", sk.Name) |
| 335 | return 0 |
| 336 | } |
| 337 | |
| 338 | func subagentRunCommand(args []string, readOnly bool) int { |
| 339 | if reportNamedSubagentHelp(args) { |
| 340 | return 0 |
| 341 | } |
| 342 | name, rest, ok := namedSubagentArgs(args) |
| 343 | if !ok { |
| 344 | fmt.Fprint(os.Stderr, subagentUsageText) |
| 345 | return 2 |
| 346 | } |
| 347 | verb := "run" |
| 348 | if readOnly { |
| 349 | verb = "try" |
| 350 | } |
| 351 | fs := flag.NewFlagSet("subagent "+verb, flag.ContinueOnError) |
| 352 | model := fs.String("model", "", "default model reference") |
| 353 | maxSteps := fs.Int("max-steps", 0, "max tool-call rounds") |
| 354 | dir := fs.String("dir", "", "project root") |
| 355 | if code, ok := parseCommandFlags(fs, rest); !ok { |
| 356 | return code |
| 357 | } |
| 358 | if rc := chdirTo(*dir); rc != 0 { |
| 359 | return rc |
| 360 | } |
| 361 | workspaceRoot, err := workspaceRootForDir(*dir) |
| 362 | if err != nil { |
| 363 | fmt.Fprintf(os.Stderr, "subagent %s: %v\n", verb, err) |
| 364 | return 1 |
| 365 | } |
| 366 | task := strings.TrimSpace(strings.Join(fs.Args(), " ")) |
| 367 | if task == "" { |
| 368 | task = readStdin() |
| 369 | } |
| 370 | if task == "" { |
| 371 | fmt.Fprintf(os.Stderr, "subagent %s: task is required\n", verb) |
| 372 | return 2 |
| 373 | } |
| 374 | ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGHUP) |
| 375 | defer stop() |
| 376 | ctrl, err := setupSubagentCommand(ctx, *model, *maxSteps, true, event.Discard, workspaceRoot) |
| 377 | if err != nil { |
| 378 | fmt.Fprintf(os.Stderr, "subagent %s: %v\n", verb, err) |
| 379 | return 1 |
| 380 | } |
| 381 | defer ctrl.Close() |
| 382 | answer, err := ctrl.RunSubagentProfile(ctx, name, task, readOnly) |
| 383 | if err != nil { |
| 384 | fmt.Fprintf(os.Stderr, "subagent %s: %v\n", verb, err) |
| 385 | return 1 |
| 386 | } |
| 387 | fmt.Println(answer) |
| 388 | return 0 |
| 389 | } |
| 390 | |
| 391 | func namedSubagentArgs(args []string) (string, []string, bool) { |
| 392 | if len(args) == 0 || strings.HasPrefix(args[0], "-") { |
| 393 | return "", nil, false |
| 394 | } |
| 395 | name := strings.TrimSpace(args[0]) |
| 396 | return name, args[1:], name != "" |
| 397 | } |
| 398 | |
| 399 | func newCLISubagentStore() *skill.Store { |
| 400 | cwd, _ := os.Getwd() |
| 401 | var custom, excluded []string |
| 402 | var pluginPaths, pluginAgentPaths map[string][]string |
| 403 | maxDepth := 3 |
| 404 | if cfg, err := config.Load(); err == nil { |
| 405 | custom = cfg.SkillCustomPaths() |
| 406 | excluded = cfg.SkillExcludedPaths() |
| 407 | pluginPaths = cfg.PluginPackageSkillOwners() |
| 408 | pluginAgentPaths = cfg.PluginPackageAgentOwners() |
| 409 | maxDepth = cfg.SkillMaxDepth() |
| 410 | } |
| 411 | return skill.New(skill.Options{ |
| 412 | ProjectRoot: cwd, CustomPaths: custom, PluginPaths: pluginPaths, |
| 413 | PluginAgentPaths: pluginAgentPaths, ExcludedPaths: excluded, MaxDepth: maxDepth, |
| 414 | }) |
| 415 | } |
| 416 | |
| 417 | func subagentProfiles(skills []skill.Skill) []skill.Skill { |
| 418 | profiles := make([]skill.Skill, 0, len(skills)) |
| 419 | for _, sk := range skills { |
| 420 | if sk.RunAs == skill.RunSubagent { |
| 421 | profiles = append(profiles, sk) |
| 422 | } |
| 423 | } |
| 424 | sort.Slice(profiles, func(i, j int) bool { return profiles[i].Name < profiles[j].Name }) |
| 425 | return profiles |
| 426 | } |
| 427 | |
| 428 | func profileCreateScope(raw string, hasProject bool) (skill.Scope, error) { |
| 429 | switch strings.ToLower(strings.TrimSpace(raw)) { |
| 430 | case "": |
| 431 | if hasProject { |
| 432 | return skill.ScopeProject, nil |
| 433 | } |
| 434 | return skill.ScopeGlobal, nil |
| 435 | case "project": |
| 436 | if !hasProject { |
| 437 | return "", fmt.Errorf("project scope requires a workspace") |
| 438 | } |
| 439 | return skill.ScopeProject, nil |
| 440 | case "global": |
| 441 | return skill.ScopeGlobal, nil |
| 442 | default: |
| 443 | return "", fmt.Errorf("unsupported scope %q; use project or global", raw) |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | func refuseSubagentNameCollision(skills []skill.Skill, name string) error { |
| 448 | occupied := make([]string, 0, len(skills)) |
| 449 | for _, sk := range skills { |
| 450 | occupied = append(occupied, sk.Name, sk.SlashName()) |
| 451 | } |
| 452 | cwd, _ := os.Getwd() |
| 453 | commands, _ := command.LoadRoots(config.CommandRootsForRoot(cwd)...) |
| 454 | for _, custom := range commands { |
| 455 | occupied = append(occupied, custom.Name) |
| 456 | } |
| 457 | return skill.ValidateSubagentProfileName(name, occupied) |
| 458 | } |
| 459 | |
| 460 | func editBuiltinSubagentProfile(sk skill.Skill, values subagentProfileFlags) error { |
| 461 | if values.description.set || values.prompt.set || values.promptFile.set || values.tools.set || values.color.set { |
| 462 | return fmt.Errorf("built-in profile %q only supports --model and --effort overrides", sk.Name) |
| 463 | } |
| 464 | unlock := config.LockUserConfigEdits() |
| 465 | defer unlock() |
| 466 | path := config.UserConfigPath() |
| 467 | cfg := config.LoadForEdit(path) |
| 468 | if values.model.set { |
| 469 | deleteSubagentOverrideAliases(cfg.Agent.SubagentModels, sk.Name) |
| 470 | ref := strings.TrimSpace(values.model.value) |
| 471 | if ref != "" { |
| 472 | entry, ok := cfg.ResolveModel(ref) |
| 473 | if !ok { |
| 474 | return fmt.Errorf("unknown model %q", ref) |
| 475 | } |
| 476 | if cfg.Agent.SubagentModels == nil { |
| 477 | cfg.Agent.SubagentModels = map[string]string{} |
| 478 | } |
| 479 | cfg.Agent.SubagentModels[sk.Name] = entry.Name + "/" + entry.Model |
| 480 | } |
| 481 | } |
| 482 | if values.effort.set { |
| 483 | deleteSubagentOverrideAliases(cfg.Agent.SubagentEfforts, sk.Name) |
| 484 | level := strings.TrimSpace(values.effort.value) |
| 485 | if level != "" && level != "auto" { |
| 486 | explicit := subagentOverride(cfg.Agent.SubagentModels, sk.Name) |
| 487 | if explicit == "" { |
| 488 | explicit = strings.TrimSpace(cfg.Agent.SubagentModel) |
| 489 | } |
| 490 | model := explicit |
| 491 | if model == "" { |
| 492 | model, _, _ = cfg.ResolveNewSessionChatModel() |
| 493 | } |
| 494 | // Profile editing is an offline configuration operation. The model |
| 495 | // must resolve so effort capabilities can be validated, but it does |
| 496 | // not need a credential until the profile is actually run. |
| 497 | entry, ok := cfg.ResolveModel(model) |
| 498 | if !ok { |
| 499 | return fmt.Errorf("unknown subagent model %q", model) |
| 500 | } |
| 501 | effort, err := config.NormalizeEffort(entry, level) |
| 502 | if err != nil { |
| 503 | return err |
| 504 | } |
| 505 | if cfg.Agent.SubagentEfforts == nil { |
| 506 | cfg.Agent.SubagentEfforts = map[string]string{} |
| 507 | } |
| 508 | cfg.Agent.SubagentEfforts[sk.Name] = effort |
| 509 | } |
| 510 | } |
| 511 | return cfg.SaveTo(path) |
| 512 | } |
| 513 | |
| 514 | func subagentOverride(overrides map[string]string, name string) string { |
| 515 | for _, key := range boot.SubagentModelKeys(name) { |
| 516 | if value := strings.TrimSpace(overrides[key]); value != "" { |
| 517 | return value |
| 518 | } |
| 519 | } |
| 520 | return "" |
| 521 | } |
| 522 | |
| 523 | func deleteSubagentOverrideAliases(overrides map[string]string, name string) { |
| 524 | for _, key := range boot.SubagentModelKeys(name) { |
| 525 | delete(overrides, key) |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | func resolveSubagentPrompt(prompt, promptFile optionalString) (string, bool, error) { |
| 530 | if prompt.set && promptFile.set { |
| 531 | return "", false, fmt.Errorf("use only one of --prompt and --prompt-file") |
| 532 | } |
| 533 | if prompt.set { |
| 534 | return prompt.value, true, nil |
| 535 | } |
| 536 | if !promptFile.set { |
| 537 | return "", false, nil |
| 538 | } |
| 539 | if promptFile.value == "-" { |
| 540 | return readStdin(), true, nil |
| 541 | } |
| 542 | path, err := filepath.Abs(promptFile.value) |
| 543 | if err != nil { |
| 544 | return "", false, err |
| 545 | } |
| 546 | data, err := os.ReadFile(path) |
| 547 | if err != nil { |
| 548 | return "", false, err |
| 549 | } |
| 550 | return string(data), true, nil |
| 551 | } |
| 552 | |
| 553 | func profileFlagsChanged(values subagentProfileFlags) bool { |
| 554 | return values.description.set || values.prompt.set || values.promptFile.set || values.model.set || |
| 555 | values.effort.set || values.tools.set || values.color.set |
| 556 | } |
| 557 | |
| 558 | func parseToolList(raw string) []string { |
| 559 | seen := map[string]bool{} |
| 560 | var tools []string |
| 561 | for _, item := range strings.Split(raw, ",") { |
| 562 | name := strings.TrimSpace(item) |
| 563 | if name == "" || seen[name] { |
| 564 | continue |
| 565 | } |
| 566 | seen[name] = true |
| 567 | tools = append(tools, name) |
| 568 | } |
| 569 | return tools |
| 570 | } |
| 571 | |
| 572 | func renderCLIProfile(name, description, prompt, model, effort string, tools []string, color string, readOnly bool) string { |
| 573 | return skill.RenderSkillFile(skill.SkillFileOptions{ |
| 574 | Name: strings.TrimSpace(name), |
| 575 | Description: strings.TrimSpace(description), |
| 576 | Body: strings.TrimSpace(prompt), |
| 577 | RunAs: skill.RunSubagent, |
| 578 | Model: strings.TrimSpace(model), |
| 579 | Effort: strings.TrimSpace(effort), |
| 580 | AllowedTools: tools, |
| 581 | ReadOnly: readOnly, |
| 582 | Color: strings.TrimSpace(color), |
| 583 | Invocation: "manual", |
| 584 | }) |
| 585 | } |
| 586 |