package model import ( "fmt" "strconv" "strings" "time" ) type Size struct { MemoryMB int `toml:"memory_mb"` CPUs float64 `toml:"cpus"` } func (s Size) Add(o Size) Size { return Size{s.MemoryMB + o.MemoryMB, s.CPUs + o.CPUs} } func (s Size) Sub(o Size) Size { return Size{s.MemoryMB - o.MemoryMB, s.CPUs - o.CPUs} } func (s Size) Fits(in Size) bool { return s.MemoryMB <= in.MemoryMB && s.CPUs <= in.CPUs+1e-9 } func (s Size) String() string { mem := fmt.Sprintf("%d MB", s.MemoryMB) if s.MemoryMB >= 1024 && s.MemoryMB%1024 == 0 { mem = fmt.Sprintf("%d GB", s.MemoryMB/1024) } cpu := strconv.FormatFloat(s.CPUs, 'f', -1, 64) + " CPUs" if s.CPUs == 1 { cpu = "1 CPU" } return mem + " RAM, " + cpu } type OwnerKind string const ( OwnerUser OwnerKind = "user" OwnerChannel OwnerKind = "channel" ) type Owner struct { ID string `json:"id"` Kind OwnerKind `json:"kind"` Machine string `json:"machine"` Secret string `json:"secret"` Size Size `json:"size"` LastActive time.Time `json:"last_active"` Node string `json:"node,omitempty"` Drive string `json:"drive,omitempty"` StorageMinMB int64 `json:"storage_min_mb"` StorageMaxMB int64 `json:"storage_max_mb"` // 0 means the config default. IdleTimeout time.Duration `json:"idle_timeout,omitempty"` // The pinned message whose thread holds the container's start and stop updates. LifecycleChannel string `json:"lifecycle_channel,omitempty"` LifecycleTS string `json:"lifecycle_ts,omitempty"` } const Indefinite time.Duration = -1 type Session struct { Channel string `json:"channel"` ThreadTS string `json:"thread_ts"` OwnerID string `json:"owner_id"` ACPID string `json:"acp_id"` Title string `json:"title"` CreatedAt time.Time `json:"created_at"` // Empty means the channel's default. Listen ListenMode `json:"listen,omitempty"` } // Turn is a reply the bot is still working on, kept so it can carry on after a restart. type Turn struct { Channel string `json:"channel"` ThreadTS string `json:"thread_ts"` MessageTS string `json:"message_ts"` ReplyTS string `json:"reply_ts"` User string `json:"user"` OwnerID string `json:"owner_id"` DM bool `json:"dm,omitempty"` Heard bool `json:"heard,omitempty"` Text string `json:"text"` Files []TurnFile `json:"files,omitempty"` // The agent got the prompt, so the session already holds part of the turn. Prompted bool `json:"prompted,omitempty"` Resumes int `json:"resumes,omitempty"` StartedAt time.Time `json:"started_at"` } type TurnFile struct { Name string `json:"name"` Mode string `json:"mode,omitempty"` URL string `json:"url"` Size int `json:"size"` } // Whether the bot reads every reply in a channel thread, or only ones that // @mention it. DMs always read everything. type ListenMode string const ( ListenMentions ListenMode = "only-mentions" ListenAll ListenMode = "all-messages" ) func MachineName(id string, kind OwnerKind) string { if kind == OwnerChannel { return "c-" + strings.ToLower(id) } return "u-" + strings.ToLower(id) } func IsOurs(name string) bool { return strings.HasPrefix(name, "u-") || strings.HasPrefix(name, "c-") }