Adds a self-hosted (Ollama, qwen2.5-coder) model provider abstraction with a pluggable opt-in cloud adapter, schema grounding, and a shared cost/safety guard every AI-suggested query is assessed against -- compiling to and executing through the same unchanged Phase 2 IR/ compiler and Phase 4 tenant scoping as a hand-written query, no parallel execution path. Track A (built into the query bar): inline ghost-text autocomplete, "Explain this query", "Fix this query" with a diff view, and a rule-based "Optimize" suggestion. Track B: natural-language-to-query translation, always a separate review step from execution, with `sentryctl query --nl` requiring explicit confirmation to run. Every accepted/dismissed translate-fix-optimize interaction is logged into the same append-only audit_log table Phase 4 built. Two real product bugs were found and fixed via live browser verification (a Svelte effect re-running on every keystroke that silently cancelled the ghost-text debounce; a ghost-text widget positioned at document offset 0 instead of the cursor), and a real costguard logic bug (unbounded-aggregation vs. raw-row) was caught by its own test suite. New integration tests wire a real Ollama client through the real HTTP handler against a mock server matching Ollama's wire contract (hack/mock-ollama), keeping model-quality verification out of CI as a disclosed, periodic human-run check instead. See /docs/phase-7-ai-design.md and /docs/phase-7-runbook.md.
80 lines
2.3 KiB
Go
80 lines
2.3 KiB
Go
// Adapts *Store to api/ai/aiapi.InteractionLogger -- same shape as
|
|
// queryapi_adapter.go's QueryAPILogger, wired in by
|
|
// enterprise/cmd/enterprise-api alongside it (Phase 7 task 12).
|
|
package audit
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/sentry/sentry/api/ai/aiapi"
|
|
"github.com/sentry/sentry/api/authz"
|
|
)
|
|
|
|
// AIInteractionLogger implements aiapi.InteractionLogger by translating
|
|
// its InteractionEntry into this package's Entry, reading tenant/user
|
|
// identity from ctx -- same "read identity from ctx rather than the
|
|
// interface growing tenant-awareness" shape as QueryAPILogger.
|
|
type AIInteractionLogger struct {
|
|
store *Store
|
|
source Source
|
|
}
|
|
|
|
func NewAIInteractionLogger(store *Store, source Source) *AIInteractionLogger {
|
|
return &AIInteractionLogger{store: store, source: source}
|
|
}
|
|
|
|
// aiInteractionDetail is what Detail carries -- Operation/Confidence/
|
|
// Accepted/Edited don't have dedicated audit_log columns (same reasoning
|
|
// as role_change/grant_change already using Detail instead of
|
|
// query_text/row_count/duration_ms), only QueryText (FinalQuery) does.
|
|
type aiInteractionDetail struct {
|
|
Operation string `json:"operation"`
|
|
Input string `json:"input"`
|
|
Output string `json:"output"`
|
|
Confidence string `json:"confidence,omitempty"`
|
|
Accepted bool `json:"accepted"`
|
|
Edited bool `json:"edited"`
|
|
}
|
|
|
|
func (l *AIInteractionLogger) LogInteraction(ctx context.Context, entry aiapi.InteractionEntry) error {
|
|
identity, ok := authz.IdentityFromContext(ctx)
|
|
if !ok || identity.TenantID == "" {
|
|
return fmt.Errorf("audit: no tenant identity in context, refusing to write an unattributable audit entry")
|
|
}
|
|
|
|
var userID *string
|
|
if identity.UserID != "" {
|
|
userID = &identity.UserID
|
|
}
|
|
|
|
detail, err := json.Marshal(aiInteractionDetail{
|
|
Operation: entry.Operation,
|
|
Input: entry.Input,
|
|
Output: entry.Output,
|
|
Confidence: entry.Confidence,
|
|
Accepted: entry.Accepted,
|
|
Edited: entry.Edited,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("audit: marshaling ai interaction detail: %w", err)
|
|
}
|
|
|
|
var queryText *string
|
|
if entry.FinalQuery != "" {
|
|
queryText = &entry.FinalQuery
|
|
}
|
|
|
|
_, err = l.store.Append(ctx, Entry{
|
|
TenantID: identity.TenantID,
|
|
UserID: userID,
|
|
Source: l.source,
|
|
EventType: EventAIInteraction,
|
|
QueryText: queryText,
|
|
Status: StatusSuccess,
|
|
Detail: detail,
|
|
})
|
|
return err
|
|
}
|