2024-04-26 20:08:32 +00:00
|
|
|
// Package model contains types and utilities for parsing, validating, and
|
|
|
|
// working with model names and digests.
|
2024-04-10 23:30:05 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cmp"
|
2024-04-30 23:38:03 +00:00
|
|
|
"encoding/hex"
|
2024-04-10 23:30:05 +00:00
|
|
|
"errors"
|
2024-04-12 20:57:57 +00:00
|
|
|
"fmt"
|
2024-04-10 23:30:05 +00:00
|
|
|
"log/slog"
|
2024-04-13 19:59:19 +00:00
|
|
|
"path/filepath"
|
2024-04-10 23:30:05 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Errors
|
|
|
|
var (
|
2024-04-26 20:08:32 +00:00
|
|
|
// ErrUnqualifiedName represents an error where a name is not fully
|
|
|
|
// qualified. It is not used directly in this package, but is here
|
|
|
|
// to avoid other packages inventing their own error type.
|
|
|
|
// Additionally, it can be conveniently used via [Unqualified].
|
|
|
|
ErrUnqualifiedName = errors.New("unqualified name")
|
2024-04-10 23:30:05 +00:00
|
|
|
)
|
|
|
|
|
2024-04-26 20:08:32 +00:00
|
|
|
// Unqualified is a helper function that returns an error with
|
|
|
|
// ErrUnqualifiedName as the cause and the name as the message.
|
|
|
|
func Unqualified(n Name) error {
|
|
|
|
return fmt.Errorf("%w: %s", ErrUnqualifiedName, n)
|
|
|
|
}
|
2024-04-12 20:57:57 +00:00
|
|
|
|
2024-04-26 20:08:32 +00:00
|
|
|
// MissingPart is used to indicate any part of a name that was "promised" by
|
|
|
|
// the presence of a separator, but is missing.
|
|
|
|
//
|
|
|
|
// The value was chosen because it is deemed unlikely to be set by a user,
|
|
|
|
// not a valid part name valid when checked by [Name.IsValid], and easy to
|
|
|
|
// spot in logs.
|
|
|
|
const MissingPart = "!MISSING!"
|
2024-04-10 23:30:05 +00:00
|
|
|
|
2024-04-17 21:54:14 +00:00
|
|
|
const (
|
|
|
|
defaultHost = "registry.ollama.ai"
|
|
|
|
defaultNamespace = "library"
|
|
|
|
defaultTag = "latest"
|
|
|
|
)
|
|
|
|
|
2024-04-26 20:08:32 +00:00
|
|
|
// DefaultName returns a name with the default values for the host, namespace,
|
|
|
|
// and tag parts. The model and digest parts are empty.
|
|
|
|
//
|
|
|
|
// - The default host is ("registry.ollama.ai")
|
|
|
|
// - The default namespace is ("library")
|
|
|
|
// - The default tag is ("latest")
|
|
|
|
func DefaultName() Name {
|
|
|
|
return Name{
|
2024-04-17 21:54:14 +00:00
|
|
|
Host: defaultHost,
|
|
|
|
Namespace: defaultNamespace,
|
|
|
|
Tag: defaultTag,
|
2024-04-26 20:08:32 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-10 23:30:05 +00:00
|
|
|
|
2024-04-26 20:08:32 +00:00
|
|
|
type partKind int
|
2024-04-10 23:30:05 +00:00
|
|
|
|
|
|
|
const (
|
2024-04-26 20:08:32 +00:00
|
|
|
kindHost partKind = iota
|
|
|
|
kindNamespace
|
|
|
|
kindModel
|
|
|
|
kindTag
|
|
|
|
kindDigest
|
2024-04-10 23:30:05 +00:00
|
|
|
)
|
|
|
|
|
2024-04-26 20:08:32 +00:00
|
|
|
func (k partKind) String() string {
|
|
|
|
switch k {
|
|
|
|
case kindHost:
|
|
|
|
return "host"
|
|
|
|
case kindNamespace:
|
|
|
|
return "namespace"
|
|
|
|
case kindModel:
|
|
|
|
return "model"
|
|
|
|
case kindTag:
|
|
|
|
return "tag"
|
|
|
|
case kindDigest:
|
|
|
|
return "digest"
|
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
2024-04-10 23:30:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 20:08:32 +00:00
|
|
|
// Name is a structured representation of a model name string, as defined by
|
|
|
|
// [ParseNameNoDefaults].
|
2024-04-10 23:30:05 +00:00
|
|
|
//
|
2024-04-26 20:08:32 +00:00
|
|
|
// It is not guaranteed to be valid. Use [Name.IsValid] to check if the name
|
|
|
|
// is valid.
|
2024-04-10 23:30:05 +00:00
|
|
|
type Name struct {
|
2024-04-26 20:08:32 +00:00
|
|
|
Host string
|
|
|
|
Namespace string
|
|
|
|
Model string
|
|
|
|
Tag string
|
|
|
|
RawDigest string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseName parses and assembles a Name from a name string. The
|
|
|
|
// format of a valid name string is:
|
|
|
|
//
|
|
|
|
// s:
|
|
|
|
// { host } "/" { namespace } "/" { model } ":" { tag } "@" { digest }
|
|
|
|
// { host } "/" { namespace } "/" { model } ":" { tag }
|
|
|
|
// { host } "/" { namespace } "/" { model } "@" { digest }
|
|
|
|
// { host } "/" { namespace } "/" { model }
|
|
|
|
// { namespace } "/" { model } ":" { tag } "@" { digest }
|
|
|
|
// { namespace } "/" { model } ":" { tag }
|
|
|
|
// { namespace } "/" { model } "@" { digest }
|
|
|
|
// { namespace } "/" { model }
|
|
|
|
// { model } ":" { tag } "@" { digest }
|
|
|
|
// { model } ":" { tag }
|
|
|
|
// { model } "@" { digest }
|
|
|
|
// { model }
|
|
|
|
// "@" { digest }
|
|
|
|
// host:
|
2024-04-28 04:24:52 +00:00
|
|
|
// pattern: { alphanum | "_" } { alphanum | "-" | "_" | "." | ":" }*
|
2024-04-26 20:08:32 +00:00
|
|
|
// length: [1, 350]
|
|
|
|
// namespace:
|
2024-04-28 04:24:52 +00:00
|
|
|
// pattern: { alphanum | "_" } { alphanum | "-" | "_" }*
|
2024-04-28 00:58:41 +00:00
|
|
|
// length: [1, 80]
|
2024-04-26 20:08:32 +00:00
|
|
|
// model:
|
2024-04-28 04:24:52 +00:00
|
|
|
// pattern: { alphanum | "_" } { alphanum | "-" | "_" | "." }*
|
2024-04-28 00:58:41 +00:00
|
|
|
// length: [1, 80]
|
2024-04-26 20:08:32 +00:00
|
|
|
// tag:
|
2024-04-28 04:24:52 +00:00
|
|
|
// pattern: { alphanum | "_" } { alphanum | "-" | "_" | "." }*
|
2024-04-26 20:08:32 +00:00
|
|
|
// length: [1, 80]
|
|
|
|
// digest:
|
2024-04-28 04:24:52 +00:00
|
|
|
// pattern: { alphanum | "_" } { alphanum | "-" | ":" }*
|
2024-04-28 00:58:41 +00:00
|
|
|
// length: [1, 80]
|
2024-04-26 20:08:32 +00:00
|
|
|
//
|
|
|
|
// Most users should use [ParseName] instead, unless need to support
|
|
|
|
// different defaults than DefaultName.
|
|
|
|
//
|
|
|
|
// The name returned is not guaranteed to be valid. If it is not valid, the
|
|
|
|
// field values are left in an undefined state. Use [Name.IsValid] to check
|
|
|
|
// if the name is valid.
|
2024-04-24 18:52:55 +00:00
|
|
|
func ParseName(s string) Name {
|
2024-04-26 21:58:07 +00:00
|
|
|
return Merge(ParseNameBare(s), DefaultName())
|
2024-04-24 18:52:55 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 21:58:07 +00:00
|
|
|
// ParseNameBare parses s as a name string and returns a Name. No merge with
|
|
|
|
// [DefaultName] is performed.
|
|
|
|
func ParseNameBare(s string) Name {
|
2024-04-26 20:08:32 +00:00
|
|
|
var n Name
|
|
|
|
var promised bool
|
2024-04-10 23:30:05 +00:00
|
|
|
|
2024-04-26 20:08:32 +00:00
|
|
|
s, n.RawDigest, promised = cutLast(s, "@")
|
|
|
|
if promised && n.RawDigest == "" {
|
|
|
|
n.RawDigest = MissingPart
|
2024-04-10 23:30:05 +00:00
|
|
|
}
|
|
|
|
|
2024-05-01 19:14:53 +00:00
|
|
|
// "/" is an illegal tag character, so we can use it to split the host
|
|
|
|
if strings.LastIndex(s, ":") > strings.LastIndex(s, "/") {
|
|
|
|
s, n.Tag, _ = cutPromised(s, ":")
|
|
|
|
}
|
|
|
|
|
2024-04-26 20:08:32 +00:00
|
|
|
s, n.Model, promised = cutPromised(s, "/")
|
|
|
|
if !promised {
|
|
|
|
n.Model = s
|
|
|
|
return n
|
2024-04-13 19:59:19 +00:00
|
|
|
}
|
2024-05-01 19:25:29 +00:00
|
|
|
|
2024-04-26 20:08:32 +00:00
|
|
|
s, n.Namespace, promised = cutPromised(s, "/")
|
|
|
|
if !promised {
|
|
|
|
n.Namespace = s
|
|
|
|
return n
|
2024-04-10 23:30:05 +00:00
|
|
|
}
|
2024-05-01 19:25:29 +00:00
|
|
|
|
|
|
|
scheme, host, ok := strings.Cut(s, "://")
|
2024-05-05 23:08:09 +00:00
|
|
|
if !ok {
|
2024-05-01 19:25:29 +00:00
|
|
|
host = scheme
|
|
|
|
}
|
|
|
|
n.Host = host
|
2024-04-10 23:30:05 +00:00
|
|
|
|
2024-04-26 20:08:32 +00:00
|
|
|
return n
|
2024-04-10 23:30:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 21:54:14 +00:00
|
|
|
// ParseNameFromFilepath parses a 4-part filepath as a Name. The parts are
|
|
|
|
// expected to be in the form:
|
|
|
|
//
|
|
|
|
// { host } "/" { namespace } "/" { model } "/" { tag }
|
|
|
|
func ParseNameFromFilepath(s string) (n Name) {
|
|
|
|
parts := strings.Split(s, string(filepath.Separator))
|
|
|
|
if len(parts) != 4 {
|
|
|
|
return Name{}
|
|
|
|
}
|
|
|
|
|
|
|
|
n.Host = parts[0]
|
|
|
|
n.Namespace = parts[1]
|
|
|
|
n.Model = parts[2]
|
|
|
|
n.Tag = parts[3]
|
|
|
|
if !n.IsFullyQualified() {
|
|
|
|
return Name{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2024-04-26 21:58:07 +00:00
|
|
|
// Merge merges the host, namespace, and tag parts of the two names,
|
2024-04-26 20:08:32 +00:00
|
|
|
// preferring the non-empty parts of a.
|
2024-04-26 21:58:07 +00:00
|
|
|
func Merge(a, b Name) Name {
|
2024-04-26 20:08:32 +00:00
|
|
|
a.Host = cmp.Or(a.Host, b.Host)
|
|
|
|
a.Namespace = cmp.Or(a.Namespace, b.Namespace)
|
|
|
|
a.Tag = cmp.Or(a.Tag, b.Tag)
|
|
|
|
return a
|
2024-04-10 23:30:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 20:08:32 +00:00
|
|
|
// String returns the name string, in the format that [ParseNameNoDefaults]
|
|
|
|
// accepts as valid, if [Name.IsValid] reports true; otherwise the empty
|
|
|
|
// string is returned.
|
|
|
|
func (n Name) String() string {
|
|
|
|
var b strings.Builder
|
|
|
|
if n.Host != "" {
|
|
|
|
b.WriteString(n.Host)
|
|
|
|
b.WriteByte('/')
|
2024-04-10 23:30:05 +00:00
|
|
|
}
|
2024-04-26 20:08:32 +00:00
|
|
|
if n.Namespace != "" {
|
|
|
|
b.WriteString(n.Namespace)
|
|
|
|
b.WriteByte('/')
|
2024-04-10 23:30:05 +00:00
|
|
|
}
|
2024-04-26 20:08:32 +00:00
|
|
|
b.WriteString(n.Model)
|
|
|
|
if n.Tag != "" {
|
|
|
|
b.WriteByte(':')
|
|
|
|
b.WriteString(n.Tag)
|
2024-04-10 23:30:05 +00:00
|
|
|
}
|
2024-04-26 20:08:32 +00:00
|
|
|
if n.RawDigest != "" {
|
|
|
|
b.WriteByte('@')
|
|
|
|
b.WriteString(n.RawDigest)
|
2024-04-10 23:30:05 +00:00
|
|
|
}
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2024-04-17 21:54:14 +00:00
|
|
|
// DisplayShort returns a short string version of the name.
|
|
|
|
func (n Name) DisplayShortest() string {
|
|
|
|
var sb strings.Builder
|
|
|
|
|
|
|
|
if n.Host != defaultHost {
|
|
|
|
sb.WriteString(n.Host)
|
|
|
|
sb.WriteByte('/')
|
|
|
|
sb.WriteString(n.Namespace)
|
|
|
|
sb.WriteByte('/')
|
|
|
|
} else if n.Namespace != defaultNamespace {
|
|
|
|
sb.WriteString(n.Namespace)
|
|
|
|
sb.WriteByte('/')
|
|
|
|
}
|
|
|
|
|
|
|
|
// always include model and tag
|
|
|
|
sb.WriteString(n.Model)
|
|
|
|
sb.WriteString(":")
|
|
|
|
sb.WriteString(n.Tag)
|
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
|
2024-04-26 20:08:32 +00:00
|
|
|
// IsValid reports whether all parts of the name are present and valid. The
|
|
|
|
// digest is a special case, and is checked for validity only if present.
|
|
|
|
func (n Name) IsValid() bool {
|
2024-04-27 04:14:28 +00:00
|
|
|
if n.RawDigest != "" && !isValidPart(kindDigest, n.RawDigest) {
|
2024-04-26 20:08:32 +00:00
|
|
|
return false
|
2024-04-10 23:30:05 +00:00
|
|
|
}
|
2024-04-26 20:08:32 +00:00
|
|
|
return n.IsFullyQualified()
|
2024-04-10 23:30:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 20:08:32 +00:00
|
|
|
// IsFullyQualified returns true if all parts of the name are present and
|
|
|
|
// valid without the digest.
|
|
|
|
func (n Name) IsFullyQualified() bool {
|
|
|
|
var parts = []string{
|
|
|
|
n.Host,
|
|
|
|
n.Namespace,
|
|
|
|
n.Model,
|
|
|
|
n.Tag,
|
|
|
|
}
|
|
|
|
for i, part := range parts {
|
|
|
|
if !isValidPart(partKind(i), part) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2024-04-10 23:30:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 20:08:32 +00:00
|
|
|
// Filepath returns a canonical filepath that represents the name with each part from
|
|
|
|
// host to tag as a directory in the form:
|
2024-04-10 23:30:05 +00:00
|
|
|
//
|
2024-04-26 20:08:32 +00:00
|
|
|
// {host}/{namespace}/{model}/{tag}
|
2024-04-10 23:30:05 +00:00
|
|
|
//
|
2024-04-26 20:08:32 +00:00
|
|
|
// It uses the system's filepath separator and ensures the path is clean.
|
2024-04-10 23:30:05 +00:00
|
|
|
//
|
2024-04-26 20:08:32 +00:00
|
|
|
// It panics if the name is not fully qualified. Use [Name.IsFullyQualified]
|
|
|
|
// to check if the name is fully qualified.
|
|
|
|
func (n Name) Filepath() string {
|
|
|
|
if !n.IsFullyQualified() {
|
|
|
|
panic("illegal attempt to get filepath of invalid name")
|
|
|
|
}
|
2024-05-08 15:47:09 +00:00
|
|
|
return filepath.Join(
|
|
|
|
strings.ToLower(filepath.Join(
|
|
|
|
n.Host,
|
|
|
|
n.Namespace,
|
|
|
|
n.Model,
|
|
|
|
)),
|
2024-04-30 18:09:19 +00:00
|
|
|
n.Tag,
|
2024-05-08 15:47:09 +00:00
|
|
|
)
|
2024-04-26 20:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LogValue returns a slog.Value that represents the name as a string.
|
|
|
|
func (n Name) LogValue() slog.Value {
|
|
|
|
return slog.StringValue(n.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func isValidLen(kind partKind, s string) bool {
|
|
|
|
switch kind {
|
|
|
|
case kindHost:
|
|
|
|
return len(s) >= 1 && len(s) <= 350
|
|
|
|
case kindTag:
|
|
|
|
return len(s) >= 1 && len(s) <= 80
|
|
|
|
default:
|
2024-04-28 00:58:41 +00:00
|
|
|
return len(s) >= 1 && len(s) <= 80
|
2024-04-10 23:30:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-26 20:08:32 +00:00
|
|
|
func isValidPart(kind partKind, s string) bool {
|
|
|
|
if !isValidLen(kind, s) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := range s {
|
|
|
|
if i == 0 {
|
2024-04-28 04:24:52 +00:00
|
|
|
if !isAlphanumericOrUnderscore(s[i]) {
|
2024-04-26 20:08:32 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
continue
|
2024-04-10 23:30:05 +00:00
|
|
|
}
|
2024-04-26 20:08:32 +00:00
|
|
|
switch s[i] {
|
|
|
|
case '_', '-':
|
|
|
|
case '.':
|
|
|
|
if kind == kindNamespace {
|
|
|
|
return false
|
2024-04-10 23:30:05 +00:00
|
|
|
}
|
2024-04-26 20:08:32 +00:00
|
|
|
case ':':
|
2024-04-27 04:14:28 +00:00
|
|
|
if kind != kindHost && kind != kindDigest {
|
2024-04-26 20:08:32 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
default:
|
2024-04-28 04:24:52 +00:00
|
|
|
if !isAlphanumericOrUnderscore(s[i]) {
|
2024-04-26 20:08:32 +00:00
|
|
|
return false
|
2024-04-10 23:30:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-26 20:08:32 +00:00
|
|
|
return true
|
2024-04-10 23:30:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-28 04:24:52 +00:00
|
|
|
func isAlphanumericOrUnderscore(c byte) bool {
|
|
|
|
return c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '_'
|
2024-04-10 23:30:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 20:08:32 +00:00
|
|
|
func cutLast(s, sep string) (before, after string, ok bool) {
|
|
|
|
i := strings.LastIndex(s, sep)
|
|
|
|
if i >= 0 {
|
|
|
|
return s[:i], s[i+len(sep):], true
|
|
|
|
}
|
|
|
|
return s, "", false
|
2024-04-10 23:30:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 20:08:32 +00:00
|
|
|
// cutPromised cuts the last part of s at the last occurrence of sep. If sep is
|
|
|
|
// found, the part before and after sep are returned as-is unless empty, in
|
|
|
|
// which case they are returned as MissingPart, which will cause
|
|
|
|
// [Name.IsValid] to return false.
|
|
|
|
func cutPromised(s, sep string) (before, after string, ok bool) {
|
|
|
|
before, after, ok = cutLast(s, sep)
|
|
|
|
if !ok {
|
|
|
|
return before, after, false
|
|
|
|
}
|
|
|
|
return cmp.Or(before, MissingPart), cmp.Or(after, MissingPart), true
|
2024-04-24 18:52:55 +00:00
|
|
|
}
|
2024-04-30 23:38:03 +00:00
|
|
|
|
|
|
|
type DigestType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
DigestTypeInvalid DigestType = iota
|
|
|
|
DigestTypeSHA256
|
|
|
|
)
|
|
|
|
|
|
|
|
func (t DigestType) String() string {
|
|
|
|
switch t {
|
|
|
|
case DigestTypeSHA256:
|
|
|
|
return "sha256"
|
|
|
|
default:
|
|
|
|
return "invalid"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Digest struct {
|
|
|
|
Type DigestType
|
|
|
|
Sum [32]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseDigest(s string) (Digest, error) {
|
|
|
|
i := strings.IndexAny(s, "-:")
|
|
|
|
if i < 0 {
|
|
|
|
return Digest{}, fmt.Errorf("invalid digest %q", s)
|
|
|
|
}
|
|
|
|
typ, encSum := s[:i], s[i+1:]
|
|
|
|
if typ != "sha256" {
|
|
|
|
return Digest{}, fmt.Errorf("unsupported digest type %q", typ)
|
|
|
|
}
|
|
|
|
d := Digest{
|
|
|
|
Type: DigestTypeSHA256,
|
|
|
|
}
|
|
|
|
n, err := hex.Decode(d.Sum[:], []byte(encSum))
|
|
|
|
if err != nil {
|
|
|
|
return Digest{}, err
|
|
|
|
}
|
|
|
|
if n != 32 {
|
|
|
|
return Digest{}, fmt.Errorf("digest %q decoded to %d bytes; want 32", encSum, n)
|
|
|
|
}
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d Digest) String() string {
|
|
|
|
if d.Type == DigestTypeInvalid {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("sha256-%x", d.Sum)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d Digest) IsValid() bool {
|
|
|
|
return d.Type != DigestTypeInvalid
|
|
|
|
}
|