(content string, skill SkillSpec, approveProjectShell func(SkillShellPermissionRequest) bool)
| 154 | |
| 155 | func (p *PromptProcessor) executeInlineShell(content string, skill SkillSpec, approveProjectShell func(SkillShellPermissionRequest) bool) (string, error) { |
| 156 | re := regexp.MustCompile("(?s)!`(?P<command>.+?)`") |
| 157 | matches := re.FindAllStringSubmatchIndex(content, -1) |
| 158 | if len(matches) == 0 { |
| 159 | return content, nil |
| 160 | } |
| 161 | var commands []string |
| 162 | for _, match := range matches { |
| 163 | command := strings.TrimSpace(content[match[2]:match[3]]) |
| 164 | decision := DecideInlineShellExecution(skill, command) |
| 165 | if !decision.Allowed { |
| 166 | return "", fmt.Errorf("%s", decision.Reason) |
| 167 | } |
| 168 | if decision.RequiresApproval { |
| 169 | if approveProjectShell == nil || !approveProjectShell(SkillShellPermissionRequest{SkillName: skill.CanonicalName, Command: command}) { |
| 170 | return "", fmt.Errorf("Skill '%s' shell command was denied: %s", skill.CanonicalName, command) |
| 171 | } |
| 172 | } |
| 173 | commands = append(commands, command) |
| 174 | } |
| 175 | var replacements []string |
| 176 | for _, command := range commands { |
| 177 | runCWD := skill.Directory |
| 178 | if runCWD == "" { |
| 179 | runCWD = p.Config.CWD |
| 180 | } |
| 181 | if err := RunShellSafetyChecks(command, runCWD); err != nil { |
| 182 | return "", err |
| 183 | } |
| 184 | output, err := p.runInlineShell(command, skill.Directory, optionalValue(skill.Frontmatter.Shell)) |
| 185 | if err != nil { |
| 186 | return "", err |
| 187 | } |
| 188 | replacements = append(replacements, output) |
| 189 | } |
| 190 | var rendered strings.Builder |
| 191 | cursor := 0 |
| 192 | for i, match := range matches { |
| 193 | rendered.WriteString(content[cursor:match[0]]) |
| 194 | rendered.WriteString(replacements[i]) |
| 195 | cursor = match[1] |
| 196 | } |
| 197 | rendered.WriteString(content[cursor:]) |
| 198 | return rendered.String(), nil |
| 199 | } |
| 200 | |
| 201 | func (p *PromptProcessor) runInlineShell(command, cwd, executable string) (string, error) { |
| 202 | runCWD := cwd |
| 203 | if runCWD == "" { |
no test coverage detected