name.go 8.9 KB

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