name.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // Package model contains types and utilities for parsing, validating, and
  2. // working with model names and digests.
  3. package model
  4. import (
  5. "cmp"
  6. "encoding/hex"
  7. "errors"
  8. "fmt"
  9. "log/slog"
  10. "path/filepath"
  11. "strings"
  12. "unicode"
  13. )
  14. // Errors
  15. var (
  16. // ErrUnqualifiedName represents an error where a name is not fully
  17. // qualified. It is not used directly in this package, but is here
  18. // to avoid other packages inventing their own error type.
  19. // Additionally, it can be conveniently used via [Unqualified].
  20. ErrUnqualifiedName = errors.New("unqualified name")
  21. )
  22. // Unqualified is a helper function that returns an error with
  23. // ErrUnqualifiedName as the cause and the name as the message.
  24. func Unqualified(n Name) error {
  25. return fmt.Errorf("%w: %s", ErrUnqualifiedName, n)
  26. }
  27. // MissingPart is used to indicate any part of a name that was "promised" by
  28. // the presence of a separator, but is missing.
  29. //
  30. // The value was chosen because it is deemed unlikely to be set by a user,
  31. // not a valid part name valid when checked by [Name.IsValid], and easy to
  32. // spot in logs.
  33. const MissingPart = "!MISSING!"
  34. const (
  35. defaultHost = "registry.ollama.ai"
  36. defaultNamespace = "library"
  37. defaultTag = "latest"
  38. )
  39. // DefaultName returns a name with the default values for the host, namespace,
  40. // and tag parts. The model and digest parts are empty.
  41. //
  42. // - The default host is ("registry.ollama.ai")
  43. // - The default namespace is ("library")
  44. // - The default tag is ("latest")
  45. func DefaultName() Name {
  46. return Name{
  47. Host: defaultHost,
  48. Namespace: defaultNamespace,
  49. Tag: defaultTag,
  50. }
  51. }
  52. type partKind int
  53. const (
  54. kindHost partKind = iota
  55. kindNamespace
  56. kindModel
  57. kindTag
  58. kindDigest
  59. )
  60. func (k partKind) String() string {
  61. switch k {
  62. case kindHost:
  63. return "host"
  64. case kindNamespace:
  65. return "namespace"
  66. case kindModel:
  67. return "model"
  68. case kindTag:
  69. return "tag"
  70. case kindDigest:
  71. return "digest"
  72. default:
  73. return "unknown"
  74. }
  75. }
  76. // Name is a structured representation of a model name string, as defined by
  77. // [ParseNameNoDefaults].
  78. //
  79. // It is not guaranteed to be valid. Use [Name.IsValid] to check if the name
  80. // is valid.
  81. type Name struct {
  82. Host string
  83. Namespace string
  84. Model string
  85. Tag string
  86. RawDigest string
  87. }
  88. // ParseName parses and assembles a Name from a name string. The
  89. // format of a valid name string is:
  90. //
  91. // s:
  92. // { host } "/" { namespace } "/" { model } ":" { tag } "@" { digest }
  93. // { host } "/" { namespace } "/" { model } ":" { tag }
  94. // { host } "/" { namespace } "/" { model } "@" { digest }
  95. // { host } "/" { namespace } "/" { model }
  96. // { namespace } "/" { model } ":" { tag } "@" { digest }
  97. // { namespace } "/" { model } ":" { tag }
  98. // { namespace } "/" { model } "@" { digest }
  99. // { namespace } "/" { model }
  100. // { model } ":" { tag } "@" { digest }
  101. // { model } ":" { tag }
  102. // { model } "@" { digest }
  103. // { model }
  104. // "@" { digest }
  105. // host:
  106. // pattern: { alphanum | "_" } { alphanum | "-" | "_" | "." | ":" }*
  107. // length: [1, 350]
  108. // namespace:
  109. // pattern: { alphanum | "_" } { alphanum | "-" | "_" }*
  110. // length: [1, 80]
  111. // model:
  112. // pattern: { alphanum | "_" } { alphanum | "-" | "_" | "." }*
  113. // length: [1, 80]
  114. // tag:
  115. // pattern: { alphanum | "_" } { alphanum | "-" | "_" | "." }*
  116. // length: [1, 80]
  117. // digest:
  118. // pattern: { alphanum | "_" } { alphanum | "-" | ":" }*
  119. // length: [1, 80]
  120. //
  121. // Most users should use [ParseName] instead, unless need to support
  122. // different defaults than DefaultName.
  123. //
  124. // The name returned is not guaranteed to be valid. If it is not valid, the
  125. // field values are left in an undefined state. Use [Name.IsValid] to check
  126. // if the name is valid.
  127. func ParseName(s string) Name {
  128. return Merge(ParseNameBare(s), DefaultName())
  129. }
  130. // ParseNameBare parses s as a name string and returns a Name. No merge with
  131. // [DefaultName] is performed.
  132. func ParseNameBare(s string) Name {
  133. var n Name
  134. var promised bool
  135. s, n.RawDigest, promised = cutLast(s, "@")
  136. if promised && n.RawDigest == "" {
  137. n.RawDigest = MissingPart
  138. }
  139. // "/" is an illegal tag character, so we can use it to split the host
  140. if strings.LastIndex(s, ":") > strings.LastIndex(s, "/") {
  141. s, n.Tag, _ = cutPromised(s, ":")
  142. }
  143. s, n.Model, promised = cutPromised(s, "/")
  144. if !promised {
  145. n.Model = s
  146. return n
  147. }
  148. s, n.Namespace, promised = cutPromised(s, "/")
  149. if !promised {
  150. n.Namespace = s
  151. return n
  152. }
  153. scheme, host, ok := strings.Cut(s, "://")
  154. if !ok {
  155. host = scheme
  156. }
  157. n.Host = host
  158. return n
  159. }
  160. // ParseNameFromFilepath parses a 4-part filepath as a Name. The parts are
  161. // expected to be in the form:
  162. //
  163. // { host } "/" { namespace } "/" { model } "/" { tag }
  164. func ParseNameFromFilepath(s string) (n Name) {
  165. parts := strings.Split(s, string(filepath.Separator))
  166. if len(parts) != 4 {
  167. return Name{}
  168. }
  169. n.Host = parts[0]
  170. n.Namespace = parts[1]
  171. n.Model = parts[2]
  172. n.Tag = parts[3]
  173. if !n.IsFullyQualified() {
  174. return Name{}
  175. }
  176. return n
  177. }
  178. // Merge merges the host, namespace, and tag parts of the two names,
  179. // preferring the non-empty parts of a.
  180. func Merge(a, b Name) Name {
  181. a.Host = cmp.Or(a.Host, b.Host)
  182. a.Namespace = cmp.Or(a.Namespace, b.Namespace)
  183. a.Tag = cmp.Or(a.Tag, b.Tag)
  184. return a
  185. }
  186. // String returns the name string, in the format that [ParseNameNoDefaults]
  187. // accepts as valid, if [Name.IsValid] reports true; otherwise the empty
  188. // string is returned.
  189. func (n Name) String() string {
  190. var b strings.Builder
  191. if n.Host != "" {
  192. b.WriteString(n.Host)
  193. b.WriteByte('/')
  194. }
  195. if n.Namespace != "" {
  196. b.WriteString(n.Namespace)
  197. b.WriteByte('/')
  198. }
  199. b.WriteString(n.Model)
  200. if n.Tag != "" {
  201. b.WriteByte(':')
  202. b.WriteString(n.Tag)
  203. }
  204. if n.RawDigest != "" {
  205. b.WriteByte('@')
  206. b.WriteString(n.RawDigest)
  207. }
  208. return b.String()
  209. }
  210. // DisplayShort returns a short string version of the name.
  211. func (n Name) DisplayShortest() string {
  212. var sb strings.Builder
  213. if n.Host != defaultHost {
  214. sb.WriteString(n.Host)
  215. sb.WriteByte('/')
  216. sb.WriteString(n.Namespace)
  217. sb.WriteByte('/')
  218. } else if n.Namespace != defaultNamespace {
  219. sb.WriteString(n.Namespace)
  220. sb.WriteByte('/')
  221. }
  222. // always include model and tag
  223. sb.WriteString(n.Model)
  224. sb.WriteString(":")
  225. sb.WriteString(n.Tag)
  226. return sb.String()
  227. }
  228. // IsValid reports whether all parts of the name are present and valid. The
  229. // digest is a special case, and is checked for validity only if present.
  230. func (n Name) IsValid() bool {
  231. if n.RawDigest != "" && !isValidPart(kindDigest, n.RawDigest) {
  232. return false
  233. }
  234. return n.IsFullyQualified()
  235. }
  236. // IsFullyQualified returns true if all parts of the name are present and
  237. // valid without the digest.
  238. func (n Name) IsFullyQualified() bool {
  239. var parts = []string{
  240. n.Host,
  241. n.Namespace,
  242. n.Model,
  243. n.Tag,
  244. }
  245. for i, part := range parts {
  246. if !isValidPart(partKind(i), part) {
  247. return false
  248. }
  249. }
  250. return true
  251. }
  252. // Filepath returns a canonical filepath that represents the name with each part from
  253. // host to tag as a directory in the form:
  254. //
  255. // {host}/{namespace}/{model}/{tag}
  256. //
  257. // It uses the system's filepath separator and ensures the path is clean.
  258. //
  259. // It panics if the name is not fully qualified. Use [Name.IsFullyQualified]
  260. // to check if the name is fully qualified.
  261. func (n Name) Filepath() string {
  262. if !n.IsFullyQualified() {
  263. panic("illegal attempt to get filepath of invalid name")
  264. }
  265. return filepath.Join(
  266. n.Host,
  267. n.Namespace,
  268. n.Model,
  269. n.Tag,
  270. )
  271. }
  272. // LogValue returns a slog.Value that represents the name as a string.
  273. func (n Name) LogValue() slog.Value {
  274. return slog.StringValue(n.String())
  275. }
  276. func isValidLen(kind partKind, s string) bool {
  277. switch kind {
  278. case kindHost:
  279. return len(s) >= 1 && len(s) <= 350
  280. case kindTag:
  281. return len(s) >= 1 && len(s) <= 80
  282. default:
  283. return len(s) >= 1 && len(s) <= 80
  284. }
  285. }
  286. func isValidPart(kind partKind, s string) bool {
  287. if !isValidLen(kind, s) {
  288. return false
  289. }
  290. for i, c := range s {
  291. if i == 0 {
  292. if !isAlphanumericOrUnderscore(c) {
  293. return false
  294. }
  295. continue
  296. }
  297. switch c {
  298. case '_', '-':
  299. case '.':
  300. if kind == kindNamespace {
  301. return false
  302. }
  303. case ':':
  304. if kind != kindHost && kind != kindDigest {
  305. return false
  306. }
  307. default:
  308. if !isAlphanumericOrUnderscore(c) {
  309. return false
  310. }
  311. }
  312. }
  313. return true
  314. }
  315. func isAlphanumericOrUnderscore(c rune) bool {
  316. return unicode.IsLetter(c) || unicode.IsDigit(c) || c == '_'
  317. }
  318. func cutLast(s, sep string) (before, after string, ok bool) {
  319. i := strings.LastIndex(s, sep)
  320. if i >= 0 {
  321. return s[:i], s[i+len(sep):], true
  322. }
  323. return s, "", false
  324. }
  325. // cutPromised cuts the last part of s at the last occurrence of sep. If sep is
  326. // found, the part before and after sep are returned as-is unless empty, in
  327. // which case they are returned as MissingPart, which will cause
  328. // [Name.IsValid] to return false.
  329. func cutPromised(s, sep string) (before, after string, ok bool) {
  330. before, after, ok = cutLast(s, sep)
  331. if !ok {
  332. return before, after, false
  333. }
  334. return cmp.Or(before, MissingPart), cmp.Or(after, MissingPart), true
  335. }
  336. type DigestType byte
  337. const (
  338. DigestTypeInvalid DigestType = iota
  339. DigestTypeSHA256
  340. )
  341. func (t DigestType) String() string {
  342. switch t {
  343. case DigestTypeSHA256:
  344. return "sha256"
  345. default:
  346. return "invalid"
  347. }
  348. }
  349. type Digest struct {
  350. Type DigestType
  351. Sum [32]byte
  352. }
  353. func ParseDigest(s string) (Digest, error) {
  354. i := strings.IndexAny(s, "-:")
  355. if i < 0 {
  356. return Digest{}, fmt.Errorf("invalid digest %q", s)
  357. }
  358. typ, encSum := s[:i], s[i+1:]
  359. if typ != "sha256" {
  360. return Digest{}, fmt.Errorf("unsupported digest type %q", typ)
  361. }
  362. d := Digest{
  363. Type: DigestTypeSHA256,
  364. }
  365. n, err := hex.Decode(d.Sum[:], []byte(encSum))
  366. if err != nil {
  367. return Digest{}, err
  368. }
  369. if n != 32 {
  370. return Digest{}, fmt.Errorf("digest %q decoded to %d bytes; want 32", encSum, n)
  371. }
  372. return d, nil
  373. }
  374. func (d Digest) String() string {
  375. if d.Type == DigestTypeInvalid {
  376. return ""
  377. }
  378. return fmt.Sprintf("sha256-%x", d.Sum)
  379. }
  380. func (d Digest) IsValid() bool {
  381. return d.Type != DigestTypeInvalid
  382. }