-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmaterializer.go
More file actions
91 lines (82 loc) · 3.65 KB
/
Copy pathmaterializer.go
File metadata and controls
91 lines (82 loc) · 3.65 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package skill
import "github.com/agent-dance/agent-adaptor/internal/skillmaterializer"
// SkillCacheRootEnv names the AGENT_ADAPTOR_SKILL_CACHE_ROOT environment
// variable. It overrides the default materializer's cache root. Drivers use
// the same location to determine which materialized directories they may
// manage, so hosts should set it consistently for the whole process.
const SkillCacheRootEnv = "AGENT_ADAPTOR_SKILL_CACHE_ROOT"
// DefaultMaterializerOption configures the materializer returned by
// [NewDefaultSkillMaterializer].
type DefaultMaterializerOption func(*defaultMaterializerConfig)
type defaultMaterializerConfig struct {
cacheRoot string
maxArchiveBytes int64
maxFileBytes int64
maxArchiveEntries int
}
// WithSkillCacheRoot overrides the cache root the default materializer
// writes to. Empty input falls back to [SkillCacheRootEnv], then
// os.UserCacheDir(), then os.TempDir().
func WithSkillCacheRoot(path string) DefaultMaterializerOption {
return func(c *defaultMaterializerConfig) { c.cacheRoot = path }
}
// WithMaxArchiveSize caps the total compressed bytes the materializer
// reads from an [Archive] source. Streams that exceed the cap surface
// as an error before any extraction begins. Default 256 MiB; values
// <= 0 are treated as the default.
func WithMaxArchiveSize(bytes int64) DefaultMaterializerOption {
return func(c *defaultMaterializerConfig) { c.maxArchiveBytes = bytes }
}
// WithMaxFileSize caps the uncompressed bytes a single archive entry
// may occupy. Decompression bombs surface as an error mid-extraction
// and the partially extracted temporary directory is removed. Default
// 64 MiB; values <= 0 are treated as the default.
func WithMaxFileSize(bytes int64) DefaultMaterializerOption {
return func(c *defaultMaterializerConfig) { c.maxFileBytes = bytes }
}
// WithMaxArchiveEntries caps the number of entries (files + dirs) in a
// single archive. Archives with more entries surface as an error
// before any file is written. Default 10000; values <= 0 are treated
// as the default.
func WithMaxArchiveEntries(n int) DefaultMaterializerOption {
return func(c *defaultMaterializerConfig) { c.maxArchiveEntries = n }
}
// NewDefaultSkillMaterializer returns the SDK's built-in materializer,
// which handles the sources produced by [Dir], [FS], [Inline], and
// [Archive]. Agents install it automatically; construct one explicitly
// only to tune the options above or to compose a
// chain-of-responsibility around it:
//
// type storeMaterializer struct {
// fallback skill.Materializer
// store *MyStoreClient
// }
//
// func (m *storeMaterializer) Materialize(ctx context.Context, s skill.Skill) (string, error) {
// if src, ok := s.Source.(myCustomSource); ok {
// return m.store.Fetch(ctx, src)
// }
// return m.fallback.Materialize(ctx, s)
// }
func NewDefaultSkillMaterializer(opts ...DefaultMaterializerOption) Materializer {
var cfg defaultMaterializerConfig
for _, opt := range opts {
if opt != nil {
opt(&cfg)
}
}
materializerOpts := make([]skillmaterializer.Option, 0, 4)
if cfg.cacheRoot != "" {
materializerOpts = append(materializerOpts, skillmaterializer.WithSkillCacheRoot(cfg.cacheRoot))
}
if cfg.maxArchiveBytes > 0 {
materializerOpts = append(materializerOpts, skillmaterializer.WithMaxArchiveSize(cfg.maxArchiveBytes))
}
if cfg.maxFileBytes > 0 {
materializerOpts = append(materializerOpts, skillmaterializer.WithMaxFileSize(cfg.maxFileBytes))
}
if cfg.maxArchiveEntries > 0 {
materializerOpts = append(materializerOpts, skillmaterializer.WithMaxArchiveEntries(cfg.maxArchiveEntries))
}
return skillmaterializer.New(ErrSkillSourceMissing, materializerOpts...)
}