File size: 2,043 Bytes
857a91b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Package cache manages on-disk layout for models, artifacts and job scratch
// space under the runtime data directory.
package cache

import (
	"os"
	"path/filepath"
	"strings"
)

// Layout resolves filesystem paths under a base data directory.
//
//	<data>/
//	β”œβ”€β”€ models/huggingface/<ns>--<name>/{metadata.json,lock.json,snapshots/}
//	β”œβ”€β”€ mcp/
//	β”œβ”€β”€ agents/
//	β”œβ”€β”€ jobs/
//	└── logs/
type Layout struct {
	Base string
}

// New returns a Layout rooted at base.
func New(base string) *Layout { return &Layout{Base: base} }

// Models returns the models root directory.
func (l *Layout) Models() string { return filepath.Join(l.Base, "models") }

// HuggingFace returns the Hugging Face cache root.
func (l *Layout) HuggingFace() string { return filepath.Join(l.Models(), "huggingface") }

// MCP returns the MCP scratch root.
func (l *Layout) MCP() string { return filepath.Join(l.Base, "mcp") }

// Agents returns the agents root.
func (l *Layout) Agents() string { return filepath.Join(l.Base, "agents") }

// Jobs returns the jobs root.
func (l *Layout) Jobs() string { return filepath.Join(l.Base, "jobs") }

// Logs returns the logs root.
func (l *Layout) Logs() string { return filepath.Join(l.Base, "logs") }

// ModelDir returns the cache directory for a single Hugging Face model. The
// namespace and name are joined with "--" to keep a flat, filesystem-safe path.
func (l *Layout) ModelDir(namespace, name string) string {
	return filepath.Join(l.HuggingFace(), SafeModelKey(namespace, name))
}

// SafeModelKey builds the on-disk key for a model, e.g. "Qwen--Qwen2.5-7B-Instruct".
func SafeModelKey(namespace, name string) string {
	key := namespace + "--" + name
	key = strings.ReplaceAll(key, "/", "--")
	return key
}

// EnsureDirs creates all standard subdirectories under the base.
func (l *Layout) EnsureDirs() error {
	for _, d := range []string{l.HuggingFace(), l.MCP(), l.Agents(), l.Jobs(), l.Logs()} {
		if err := os.MkdirAll(d, 0o755); err != nil {
			return err
		}
	}
	return nil
}