|
| 1 | +package versioncheck |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + skillDirEnv = "AIRBYTE_AGENT_SKILLS_DIR" |
| 14 | + defaultSkillsDir = ".claude/skills" |
| 15 | + skillName = "airbyte-agent" |
| 16 | + skillFileName = "SKILL.md" |
| 17 | +) |
| 18 | + |
| 19 | +// CheckSkill prints a one-line stderr warning when the installed |
| 20 | +// airbyte-agent skill is older than the version this binary was built |
| 21 | +// against. Silent in every other case: when disabled, when the output |
| 22 | +// stream is not a terminal, when either version is a non-release tag |
| 23 | +// (e.g. "dev"), when the skill file is missing, or when the installed |
| 24 | +// version is equal to or newer than expected. |
| 25 | +// |
| 26 | +// Unlike Run, CheckSkill does no network I/O — the expected version is |
| 27 | +// stamped into the binary at build time and the installed version lives |
| 28 | +// on local disk. |
| 29 | +func CheckSkill(expectedSkillVersion string, enabled bool, isTTY bool, stderr io.Writer) { |
| 30 | + if !enabled || !isTTY { |
| 31 | + return |
| 32 | + } |
| 33 | + expected, ok := parseVersion(expectedSkillVersion) |
| 34 | + if !ok { |
| 35 | + return |
| 36 | + } |
| 37 | + installedRaw, ok := readInstalledSkillVersion() |
| 38 | + if !ok { |
| 39 | + return |
| 40 | + } |
| 41 | + installed, ok := parseVersion(installedRaw) |
| 42 | + if !ok { |
| 43 | + return |
| 44 | + } |
| 45 | + if compareVersions(installed, expected) >= 0 { |
| 46 | + return |
| 47 | + } |
| 48 | + fmt.Fprint(stderr, formatSkillNudge(installedRaw, expectedSkillVersion)) |
| 49 | +} |
| 50 | + |
| 51 | +// skillFrontmatterVersionRE pulls `metadata.version` out of the YAML |
| 52 | +// frontmatter without pulling in a YAML dependency. It expects the |
| 53 | +// indented `version:` to appear under a `metadata:` block. Quotes |
| 54 | +// around the value are optional. |
| 55 | +var skillFrontmatterVersionRE = regexp.MustCompile(`(?m)^metadata:\s*\n(?:[ \t]+[^\n]*\n)*?[ \t]+version:[ \t]*["']?([^"'\n]+?)["']?[ \t]*$`) |
| 56 | + |
| 57 | +func readInstalledSkillVersion() (string, bool) { |
| 58 | + path := installedSkillPath() |
| 59 | + if path == "" { |
| 60 | + return "", false |
| 61 | + } |
| 62 | + data, err := os.ReadFile(path) |
| 63 | + if err != nil { |
| 64 | + return "", false |
| 65 | + } |
| 66 | + fm, ok := frontmatterBlock(data) |
| 67 | + if !ok { |
| 68 | + return "", false |
| 69 | + } |
| 70 | + m := skillFrontmatterVersionRE.FindSubmatch(fm) |
| 71 | + if m == nil { |
| 72 | + return "", false |
| 73 | + } |
| 74 | + return strings.TrimSpace(string(m[1])), true |
| 75 | +} |
| 76 | + |
| 77 | +// frontmatterBlock returns the bytes between the leading `---` fence and |
| 78 | +// the next `---` fence. Returns false when the file lacks frontmatter. |
| 79 | +func frontmatterBlock(data []byte) ([]byte, bool) { |
| 80 | + s := string(data) |
| 81 | + if !strings.HasPrefix(s, "---\n") && !strings.HasPrefix(s, "---\r\n") { |
| 82 | + return nil, false |
| 83 | + } |
| 84 | + rest := strings.TrimPrefix(strings.TrimPrefix(s, "---\r\n"), "---\n") |
| 85 | + end := strings.Index(rest, "\n---") |
| 86 | + if end < 0 { |
| 87 | + return nil, false |
| 88 | + } |
| 89 | + return []byte(rest[:end+1]), true |
| 90 | +} |
| 91 | + |
| 92 | +func installedSkillPath() string { |
| 93 | + if dir := strings.TrimSpace(os.Getenv(skillDirEnv)); dir != "" { |
| 94 | + return filepath.Join(dir, skillName, skillFileName) |
| 95 | + } |
| 96 | + home, err := os.UserHomeDir() |
| 97 | + if err != nil { |
| 98 | + return "" |
| 99 | + } |
| 100 | + return filepath.Join(home, defaultSkillsDir, skillName, skillFileName) |
| 101 | +} |
| 102 | + |
| 103 | +func formatSkillNudge(installed, expected string) string { |
| 104 | + var b strings.Builder |
| 105 | + fmt.Fprintf(&b, "Your airbyte-agent skill is %s — this CLI expects %s.\n", installed, expected) |
| 106 | + b.WriteString(" Reinstall with npx: npx skills add airbytehq/airbyte-agent-cli\n") |
| 107 | + b.WriteString(" Or reinstall via: curl -fsSL https://airbyte.ai/install.sh | sh\n") |
| 108 | + b.WriteString(" (silence: set \"version_check_enabled\": false in ~/.airbyte-agent/settings.json)\n") |
| 109 | + return b.String() |
| 110 | +} |
0 commit comments