diff --git a/pkg/gateway/gateway.go b/pkg/gateway/gateway.go index dc600f6bb..414dfd465 100644 --- a/pkg/gateway/gateway.go +++ b/pkg/gateway/gateway.go @@ -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) diff --git a/pkg/miniapp/api.go b/pkg/miniapp/api.go index cdabe3748..21240ccb9 100644 --- a/pkg/miniapp/api.go +++ b/pkg/miniapp/api.go @@ -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 } diff --git a/pkg/miniapp/miniapp.go b/pkg/miniapp/miniapp.go index 3d2a4c46d..dc4f1dc10 100644 --- a/pkg/miniapp/miniapp.go +++ b/pkg/miniapp/miniapp.go @@ -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)) } diff --git a/pkg/miniapp/miniapp_test.go b/pkg/miniapp/miniapp_test.go index 73a0517c3..3be2e4958 100644 --- a/pkg/miniapp/miniapp_test.go +++ b/pkg/miniapp/miniapp_test.go @@ -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) { diff --git a/pkg/miniapp/types.go b/pkg/miniapp/types.go index ac7a7b1b9..26b052535 100644 --- a/pkg/miniapp/types.go +++ b/pkg/miniapp/types.go @@ -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) }