Use the record tables when migration 00059 is present, and hide statements from documents the caller cannot see. Co-authored-by: Cursor <cursoragent@cursor.com>
99 lines
2.7 KiB
Go
99 lines
2.7 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"chat-agent-system/internal/coordinator"
|
|
"chat-agent-system/internal/docgov"
|
|
"chat-agent-system/internal/projectinstance"
|
|
"chat-agent-system/internal/traceimpact"
|
|
)
|
|
|
|
type traceRecheck struct{ svc *traceimpact.Service }
|
|
|
|
func (t traceRecheck) StageNeedsAck(ctx context.Context, projectID int64, stageKey string) (bool, error) {
|
|
if t.svc == nil {
|
|
return false, nil
|
|
}
|
|
return t.svc.NeedsRecheckAck(ctx, projectID, stageKey)
|
|
}
|
|
|
|
func (t traceRecheck) Acknowledge(ctx context.Context, projectID int64, stageKey string, userID int64) error {
|
|
if t.svc == nil {
|
|
return nil
|
|
}
|
|
return t.svc.AcknowledgeRecheck(ctx, projectID, stageKey, userID)
|
|
}
|
|
|
|
type traceStage struct{ inst *projectinstance.Service }
|
|
|
|
func (t traceStage) ReopenCompletedStage(ctx context.Context, instanceID int64, stageKey string, docTaskKeys []string, operatorID int64) error {
|
|
if t.inst == nil {
|
|
return fmt.Errorf("trace: instance service unavailable")
|
|
}
|
|
return t.inst.ReopenStage(ctx, instanceID, stageKey, docTaskKeys, operatorID)
|
|
}
|
|
|
|
type traceUnlock struct{ gov *docgov.GovernanceEngine }
|
|
|
|
func (t traceUnlock) ReopenApprovedDocs(ctx context.Context, projectID int64, docTaskKeys []string, actorID int64, reason string) error {
|
|
if t.gov == nil {
|
|
return nil
|
|
}
|
|
for _, key := range docTaskKeys {
|
|
docID := fmt.Sprintf("doc_proj%d_%s", projectID, key)
|
|
_, err := t.gov.ReopenForRevision(ctx, docID, actorID, "creator", reason)
|
|
if err == nil || errors.Is(err, docgov.ErrDocumentNotFound) {
|
|
continue
|
|
}
|
|
if errors.Is(err, docgov.ErrInvalidStateTransition) {
|
|
continue
|
|
}
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type traceChat struct{ svc *traceimpact.Service }
|
|
|
|
func (t traceChat) AnalyzeImpact(ctx context.Context, projectID, _ int64, text string) (string, error) {
|
|
if t.svc == nil {
|
|
return "", fmt.Errorf("trace: unavailable")
|
|
}
|
|
rep, err := t.svc.Analyze(ctx, projectID, traceimpact.DocRef{DocID: "chat", Markdown: text, Visible: true}, nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return rep.Summary, nil
|
|
}
|
|
|
|
func projectIDFromControlledDoc(docID string) int64 {
|
|
rest := strings.TrimPrefix(docID, "doc_proj")
|
|
if rest == docID {
|
|
return 0
|
|
}
|
|
i := strings.Index(rest, "_")
|
|
if i <= 0 {
|
|
return 0
|
|
}
|
|
n, err := strconv.ParseInt(rest[:i], 10, 64)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return n
|
|
}
|
|
func (t traceChat) ProposeRevision(ctx context.Context, projectID, userID int64, text string, confirmed bool) (string, error) {
|
|
if t.svc == nil {
|
|
return "", fmt.Errorf("trace: unavailable")
|
|
}
|
|
id, err := t.svc.ProposeRevision(ctx, projectID, userID, text, confirmed)
|
|
if errors.Is(err, traceimpact.ErrNeedsConfirm) {
|
|
return "", coordinator.ErrImpactNeedsConfirm
|
|
}
|
|
return id, err
|
|
}
|