|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +// skillSlashCommandPattern matches a leading "/<command>" token up to the first |
| 11 | +// whitespace, so command arguments are never captured. |
| 12 | +var skillSlashCommandPattern = regexp.MustCompile(`^/([A-Za-z0-9][A-Za-z0-9._:/-]*)`) |
| 13 | + |
| 14 | +// filesystemRoots reject pasted absolute paths ("/Users/x", "/tmp/y") that would |
| 15 | +// otherwise look like commands. Only matched when the root is followed by another |
| 16 | +// path segment, so a bare "/dev" stays a command (see isFilesystemPath). |
| 17 | +var filesystemRoots = map[string]struct{}{ |
| 18 | + "users": {}, "home": {}, "tmp": {}, "usr": {}, "var": {}, "etc": {}, |
| 19 | + "opt": {}, "mnt": {}, "private": {}, "volumes": {}, "library": {}, |
| 20 | + "applications": {}, "system": {}, "bin": {}, "sbin": {}, "dev": {}, |
| 21 | + "proc": {}, "sys": {}, "root": {}, "srv": {}, "run": {}, "boot": {}, |
| 22 | + "lib": {}, "media": {}, "network": {}, "cores": {}, |
| 23 | +} |
| 24 | + |
| 25 | +// SkillEventFromPromptSlashCommand returns a skill event for a prompt beginning |
| 26 | +// with a "/<command>" slash command. A recorded prompt only contains a slash |
| 27 | +// command that was submitted as a turn, so runtime/UI-only commands (/mcp, |
| 28 | +// /model, ...) are naturally absent; pasted filesystem paths are rejected. |
| 29 | +// |
| 30 | +// Only the command token is stored, never the prompt body. Tool-call skills |
| 31 | +// (e.g. Claude Code's Skill tool) are captured separately by SkillEventExtractors |
| 32 | +// as "tool_invocation" events. |
| 33 | +func SkillEventFromPromptSlashCommand(agentName, prompt string, timestamp time.Time) (SkillEvent, bool) { |
| 34 | + trimmed := strings.TrimLeft(prompt, " \t\r\n") |
| 35 | + match := skillSlashCommandPattern.FindStringSubmatch(trimmed) |
| 36 | + if match == nil { |
| 37 | + return SkillEvent{}, false |
| 38 | + } |
| 39 | + |
| 40 | + token := strings.Trim(match[1], "/") |
| 41 | + if token == "" || isFilesystemPath(match[1]) { |
| 42 | + return SkillEvent{}, false |
| 43 | + } |
| 44 | + |
| 45 | + // Normalize Pi's "/skill:<name>" form to the bare skill name so the generic |
| 46 | + // event dedupes against Pi's native input_slash_command event, which records |
| 47 | + // the name without the "skill:" namespace. |
| 48 | + name := token |
| 49 | + if rest, ok := strings.CutPrefix(token, "skill:"); ok { |
| 50 | + if rest == "" { |
| 51 | + return SkillEvent{}, false |
| 52 | + } |
| 53 | + name = rest |
| 54 | + } |
| 55 | + |
| 56 | + command := "/" + token |
| 57 | + event := SkillEvent{ |
| 58 | + ID: promptSkillEventID(agentName, name, timestamp), |
| 59 | + EventType: SkillEventTypePromptInvocation, |
| 60 | + Skill: SkillEventSkill{ |
| 61 | + Name: name, |
| 62 | + }, |
| 63 | + Source: SkillEventSource{ |
| 64 | + Agent: agentName, |
| 65 | + Signal: SkillSignalPromptSlashCommand, |
| 66 | + Confidence: SkillConfidenceExplicit, |
| 67 | + }, |
| 68 | + Native: map[string]string{ |
| 69 | + "command": command, |
| 70 | + }, |
| 71 | + Collapse: SkillEventCollapse{ |
| 72 | + Target: SkillCollapseTargetUserMessage, |
| 73 | + Label: command, |
| 74 | + DefaultCollapsed: true, |
| 75 | + }, |
| 76 | + } |
| 77 | + if !timestamp.IsZero() { |
| 78 | + event.Timestamp = timestamp.UTC().Format(time.RFC3339Nano) |
| 79 | + } |
| 80 | + return event, true |
| 81 | +} |
| 82 | + |
| 83 | +// isFilesystemPath reports whether raw (the captured command token, e.g. |
| 84 | +// "Users/alice/x", "dev", "parent/child") is a pasted absolute filesystem path |
| 85 | +// rather than a slash command. It matches only when a well-known root segment is |
| 86 | +// FOLLOWED by a further path segment, so bare single-token commands that happen |
| 87 | +// to collide with a root name (e.g. "/dev", "/run", "/lib") are still treated as |
| 88 | +// commands — only "/dev/null", "/Users/alice/...", etc. are rejected. |
| 89 | +func isFilesystemPath(raw string) bool { |
| 90 | + first, rest, found := strings.Cut(raw, "/") |
| 91 | + if !found || rest == "" { |
| 92 | + return false |
| 93 | + } |
| 94 | + _, ok := filesystemRoots[strings.ToLower(first)] |
| 95 | + return ok |
| 96 | +} |
| 97 | + |
| 98 | +// AppendPromptSlashCommandSkillEvent adds a generic prompt-invocation skill |
| 99 | +// event for a "/<command>" prompt. If an agent adapter already surfaced an |
| 100 | +// equivalent prompt skill event (for example Pi's pre-expansion input event), |
| 101 | +// the adapter event wins and no generic duplicate is appended. |
| 102 | +func AppendPromptSlashCommandSkillEvent(events []SkillEvent, agentName, prompt string, timestamp time.Time) []SkillEvent { |
| 103 | + event, ok := SkillEventFromPromptSlashCommand(agentName, prompt, timestamp) |
| 104 | + if !ok { |
| 105 | + return events |
| 106 | + } |
| 107 | + if hasEquivalentPromptSkillEvent(events, event) { |
| 108 | + return events |
| 109 | + } |
| 110 | + return append(events, event) |
| 111 | +} |
| 112 | + |
| 113 | +func promptSkillEventID(agentName, skillName string, timestamp time.Time) string { |
| 114 | + if timestamp.IsZero() { |
| 115 | + return "" |
| 116 | + } |
| 117 | + return fmt.Sprintf("prompt-skill-%s-%s-%s", agentName, skillName, timestamp.UTC().Format(time.RFC3339Nano)) |
| 118 | +} |
| 119 | + |
| 120 | +func hasEquivalentPromptSkillEvent(events []SkillEvent, candidate SkillEvent) bool { |
| 121 | + candidateCommand := "" |
| 122 | + if candidate.Native != nil { |
| 123 | + candidateCommand = candidate.Native["command"] |
| 124 | + } |
| 125 | + for _, existing := range events { |
| 126 | + if existing.EventType != SkillEventTypePromptInvocation || existing.Skill.Name != candidate.Skill.Name { |
| 127 | + continue |
| 128 | + } |
| 129 | + if existing.ID != "" && candidate.ID != "" && existing.ID == candidate.ID { |
| 130 | + return true |
| 131 | + } |
| 132 | + if candidateCommand != "" && existing.Native != nil && existing.Native["command"] == candidateCommand { |
| 133 | + return true |
| 134 | + } |
| 135 | + if existing.Source.Signal == SkillSignalPiInputSlashCommand || existing.Source.Signal == SkillSignalPromptSlashCommand { |
| 136 | + return true |
| 137 | + } |
| 138 | + } |
| 139 | + return false |
| 140 | +} |
0 commit comments