name.go 7.8 KB

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