name.go 9.0 KB

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