fix: split CacheMutator from DataProvider to avoid interfacebloat

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
dj-oyu 2026-03-20 15:58:51 +09:00
parent f293f54dca
commit 6f71bd4b4e
5 changed files with 21 additions and 10 deletions

View file

@ -324,6 +324,7 @@ func setupAndStartServices(
cfg.Channels.Telegram.AllowFrom,
cfg.WorkspacePath(),
)
handler.SetCacheMutator(dataProvider)
agentLoop.OnStateChange = miniappNotifier.Notify
if b := agentLoop.GetOrchBroadcaster(); b != nil {
handler.SetOrchBroadcaster(b)

View file

@ -297,7 +297,11 @@ func (h *Handler) apiCache(w http.ResponseWriter, r *http.Request) {
}
writeJSON(w, entries)
case http.MethodDelete:
n, err := h.provider.DeleteAllMediaCache()
if h.cacheMutator == nil {
http.Error(w, `{"error":"not supported"}`, http.StatusNotImplemented)
return
}
n, err := h.cacheMutator.DeleteAllMediaCache()
if err != nil {
http.Error(w, `{"error":"failed to delete cache"}`, http.StatusInternalServerError)
return
@ -314,12 +318,16 @@ func (h *Handler) apiCacheEntry(w http.ResponseWriter, r *http.Request) {
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
return
}
if h.cacheMutator == nil {
http.Error(w, `{"error":"not supported"}`, http.StatusNotImplemented)
return
}
hash := r.URL.Path[len("/miniapp/api/cache/"):]
if hash == "" {
http.Error(w, `{"error":"hash required"}`, http.StatusBadRequest)
return
}
if err := h.provider.DeleteMediaCache(hash); err != nil {
if err := h.cacheMutator.DeleteMediaCache(hash); err != nil {
http.Error(w, `{"error":"failed to delete entry"}`, http.StatusInternalServerError)
return
}

View file

@ -34,6 +34,7 @@ func mustMiniappStaticFS() fs.FS {
// Handler serves the Mini App HTML and API endpoints.
type Handler struct {
provider DataProvider
cacheMutator CacheMutator
sender CommandSender
botToken string
notifier *StateNotifier
@ -84,6 +85,11 @@ func (h *Handler) SetOrchBroadcaster(b *orch.Broadcaster) {
h.orchBroadcaster = b
}
// SetCacheMutator enables cache mutation operations (delete entry/clear all).
func (h *Handler) SetCacheMutator(m CacheMutator) {
h.cacheMutator = m
}
func (h *Handler) handleProtectedFunc(mux *http.ServeMux, pattern string, handler http.HandlerFunc) {
mux.HandleFunc(pattern, h.requireAuth(handler))
}

View file

@ -215,10 +215,6 @@ func (m *mockDataProvider) ListMediaCache(entryType string) []MediaCacheEntry {
return nil
}
func (m *mockDataProvider) DeleteMediaCache(hash string) error { return nil }
func (m *mockDataProvider) DeleteAllMediaCache() (int64, error) { return 0, nil }
type mockSender struct{}
func (m *mockSender) SendCommand(senderID, chatID, command string) {}
@ -575,10 +571,6 @@ func (m *mutatingDataProvider) ListMediaCache(entryType string) []MediaCacheEntr
return nil
}
func (m *mutatingDataProvider) DeleteMediaCache(hash string) error { return nil }
func (m *mutatingDataProvider) DeleteAllMediaCache() (int64, error) { return 0, nil }
// ── Dev proxy tests ──
func TestDevProxy_RegisterAndActivate(t *testing.T) {

View file

@ -137,6 +137,10 @@ type DataProvider interface {
GetContextInfo() ContextInfo
GetSystemPrompt() string
ListMediaCache(entryType string) []MediaCacheEntry
}
// CacheMutator extends DataProvider with cache mutation operations.
type CacheMutator interface {
DeleteMediaCache(hash string) error
DeleteAllMediaCache() (int64, error)
}