Add AI-powered evidence description generation
Introduce a background worker that automatically generates compliance-focused descriptions for uploaded evidence files using configurable LLM providers. Descriptions are surfaced across all interfaces: GraphQL API, MCP API, CLI, and the console UI. Key changes: - Multi-provider LLM config with per-agent settings (pointer types for Temperature/MaxTokens to preserve zero values) - Evidence description worker with bounded concurrency - EvidenceDescriptionStatus typed enum with PostgreSQL enum type - New `prb evidence` CLI commands (list, view, delete) - Evidence description displayed in console table and preview - Migration only marks evidences without files as completed Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
@@ -30,18 +30,20 @@ import (
|
||||
|
||||
type (
|
||||
Evidence struct {
|
||||
ID gid.GID `db:"id"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
MeasureID gid.GID `db:"measure_id"`
|
||||
TaskID *gid.GID `db:"task_id"`
|
||||
State EvidenceState `db:"state"`
|
||||
ReferenceID string `db:"reference_id"`
|
||||
Type EvidenceType `db:"type"`
|
||||
URL string `db:"url"`
|
||||
EvidenceFileId *gid.GID `db:"evidence_file_id"`
|
||||
Description *string `db:"description"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
ID gid.GID `db:"id"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
MeasureID gid.GID `db:"measure_id"`
|
||||
TaskID *gid.GID `db:"task_id"`
|
||||
State EvidenceState `db:"state"`
|
||||
ReferenceID string `db:"reference_id"`
|
||||
Type EvidenceType `db:"type"`
|
||||
URL string `db:"url"`
|
||||
EvidenceFileId *gid.GID `db:"evidence_file_id"`
|
||||
Description *string `db:"description"`
|
||||
DescriptionStatus EvidenceDescriptionStatus `db:"description_status"`
|
||||
DescriptionProcessingStartedAt *time.Time `db:"description_processing_started_at"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
|
||||
Evidences []*Evidence
|
||||
@@ -89,6 +91,8 @@ INSERT INTO
|
||||
url,
|
||||
evidence_file_id,
|
||||
description,
|
||||
description_status,
|
||||
description_processing_started_at,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
@@ -103,6 +107,8 @@ VALUES (
|
||||
@url,
|
||||
@evidence_file_id,
|
||||
@description,
|
||||
@description_status,
|
||||
@description_processing_started_at,
|
||||
@created_at,
|
||||
@updated_at
|
||||
)
|
||||
@@ -114,18 +120,20 @@ WHERE evidences.state = 'REQUESTED';
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"evidence_id": e.ID,
|
||||
"measure_id": e.MeasureID,
|
||||
"task_id": e.TaskID,
|
||||
"reference_id": e.ReferenceID,
|
||||
"evidence_file_id": e.EvidenceFileId,
|
||||
"created_at": e.CreatedAt,
|
||||
"updated_at": e.UpdatedAt,
|
||||
"state": e.State,
|
||||
"type": e.Type,
|
||||
"url": e.URL,
|
||||
"description": e.Description,
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"evidence_id": e.ID,
|
||||
"measure_id": e.MeasureID,
|
||||
"task_id": e.TaskID,
|
||||
"reference_id": e.ReferenceID,
|
||||
"evidence_file_id": e.EvidenceFileId,
|
||||
"created_at": e.CreatedAt,
|
||||
"updated_at": e.UpdatedAt,
|
||||
"state": e.State,
|
||||
"type": e.Type,
|
||||
"url": e.URL,
|
||||
"description": e.Description,
|
||||
"description_status": e.DescriptionStatus,
|
||||
"description_processing_started_at": e.DescriptionProcessingStartedAt,
|
||||
}
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
return err
|
||||
@@ -150,6 +158,8 @@ INSERT INTO
|
||||
url,
|
||||
evidence_file_id,
|
||||
description,
|
||||
description_status,
|
||||
description_processing_started_at,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
@@ -165,25 +175,29 @@ VALUES (
|
||||
@url,
|
||||
@evidence_file_id,
|
||||
@description,
|
||||
@description_status,
|
||||
@description_processing_started_at,
|
||||
@created_at,
|
||||
@updated_at
|
||||
)
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"evidence_id": e.ID,
|
||||
"organization_id": e.OrganizationID,
|
||||
"measure_id": e.MeasureID,
|
||||
"task_id": e.TaskID,
|
||||
"reference_id": e.ReferenceID,
|
||||
"evidence_file_id": e.EvidenceFileId,
|
||||
"created_at": e.CreatedAt,
|
||||
"updated_at": e.UpdatedAt,
|
||||
"state": e.State,
|
||||
"type": e.Type,
|
||||
"url": e.URL,
|
||||
"description": e.Description,
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"evidence_id": e.ID,
|
||||
"organization_id": e.OrganizationID,
|
||||
"measure_id": e.MeasureID,
|
||||
"task_id": e.TaskID,
|
||||
"reference_id": e.ReferenceID,
|
||||
"evidence_file_id": e.EvidenceFileId,
|
||||
"created_at": e.CreatedAt,
|
||||
"updated_at": e.UpdatedAt,
|
||||
"state": e.State,
|
||||
"type": e.Type,
|
||||
"url": e.URL,
|
||||
"description": e.Description,
|
||||
"description_status": e.DescriptionStatus,
|
||||
"description_processing_started_at": e.DescriptionProcessingStartedAt,
|
||||
}
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
|
||||
@@ -218,6 +232,8 @@ SELECT
|
||||
url,
|
||||
evidence_file_id,
|
||||
description,
|
||||
description_status,
|
||||
description_processing_started_at,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
@@ -299,6 +315,8 @@ SELECT
|
||||
url,
|
||||
evidence_file_id,
|
||||
description,
|
||||
description_status,
|
||||
description_processing_started_at,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
@@ -381,6 +399,8 @@ SELECT
|
||||
url,
|
||||
evidence_file_id,
|
||||
description,
|
||||
description_status,
|
||||
description_processing_started_at,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
@@ -426,6 +446,8 @@ SET
|
||||
evidence_file_id = @evidence_file_id,
|
||||
url = @url,
|
||||
description = @description,
|
||||
description_status = @description_status,
|
||||
description_processing_started_at = @description_processing_started_at,
|
||||
updated_at = @updated_at
|
||||
WHERE
|
||||
%s
|
||||
@@ -435,13 +457,15 @@ WHERE
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"evidence_id": e.ID,
|
||||
"type": e.Type,
|
||||
"state": e.State,
|
||||
"evidence_file_id": e.EvidenceFileId,
|
||||
"url": e.URL,
|
||||
"description": e.Description,
|
||||
"updated_at": e.UpdatedAt,
|
||||
"evidence_id": e.ID,
|
||||
"type": e.Type,
|
||||
"state": e.State,
|
||||
"evidence_file_id": e.EvidenceFileId,
|
||||
"url": e.URL,
|
||||
"description": e.Description,
|
||||
"description_status": e.DescriptionStatus,
|
||||
"description_processing_started_at": e.DescriptionProcessingStartedAt,
|
||||
"updated_at": e.UpdatedAt,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
@@ -474,3 +498,71 @@ WHERE
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Evidence) LoadNextPendingDescriptionForUpdateSkipLocked(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
task_id,
|
||||
measure_id,
|
||||
reference_id,
|
||||
state,
|
||||
type,
|
||||
url,
|
||||
evidence_file_id,
|
||||
description,
|
||||
description_status,
|
||||
description_processing_started_at,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
evidences
|
||||
WHERE
|
||||
description_status = 'PENDING'
|
||||
AND evidence_file_id IS NOT NULL
|
||||
ORDER BY
|
||||
created_at ASC
|
||||
LIMIT 1
|
||||
FOR UPDATE SKIP LOCKED;
|
||||
`
|
||||
|
||||
rows, err := conn.Query(ctx, q)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query evidence: %w", err)
|
||||
}
|
||||
|
||||
evidence, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Evidence])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
return fmt.Errorf("cannot collect evidence: %w", err)
|
||||
}
|
||||
|
||||
*e = evidence
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ResetStaleDescriptionProcessing(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
staleAfter time.Duration,
|
||||
) error {
|
||||
q := `
|
||||
UPDATE evidences
|
||||
SET
|
||||
description_status = 'PENDING',
|
||||
description_processing_started_at = NULL
|
||||
WHERE
|
||||
description_status = 'PROCESSING'
|
||||
AND description_processing_started_at < $1;
|
||||
`
|
||||
|
||||
_, err := conn.Exec(ctx, q, time.Now().Add(-staleAfter))
|
||||
return err
|
||||
}
|
||||
|
||||
68
pkg/coredata/evidence_description_status.go
Normal file
68
pkg/coredata/evidence_description_status.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// Copyright (c) 2025-2026 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package coredata
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type (
|
||||
EvidenceDescriptionStatus string
|
||||
)
|
||||
|
||||
const (
|
||||
EvidenceDescriptionStatusPending EvidenceDescriptionStatus = "PENDING"
|
||||
EvidenceDescriptionStatusProcessing EvidenceDescriptionStatus = "PROCESSING"
|
||||
EvidenceDescriptionStatusCompleted EvidenceDescriptionStatus = "COMPLETED"
|
||||
)
|
||||
|
||||
func (s EvidenceDescriptionStatus) MarshalText() ([]byte, error) {
|
||||
return []byte(s.String()), nil
|
||||
}
|
||||
|
||||
func (s *EvidenceDescriptionStatus) UnmarshalText(data []byte) error {
|
||||
val := string(data)
|
||||
|
||||
switch val {
|
||||
case EvidenceDescriptionStatusPending.String():
|
||||
*s = EvidenceDescriptionStatusPending
|
||||
case EvidenceDescriptionStatusProcessing.String():
|
||||
*s = EvidenceDescriptionStatusProcessing
|
||||
case EvidenceDescriptionStatusCompleted.String():
|
||||
*s = EvidenceDescriptionStatusCompleted
|
||||
default:
|
||||
return fmt.Errorf("invalid EvidenceDescriptionStatus value: %q", val)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s EvidenceDescriptionStatus) String() string {
|
||||
return string(s)
|
||||
}
|
||||
|
||||
func (s *EvidenceDescriptionStatus) Scan(value any) error {
|
||||
val, ok := value.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid scan source for EvidenceDescriptionStatus, expected string got %T", value)
|
||||
}
|
||||
|
||||
return s.UnmarshalText([]byte(val))
|
||||
}
|
||||
|
||||
func (s EvidenceDescriptionStatus) Value() (driver.Value, error) {
|
||||
return s.String(), nil
|
||||
}
|
||||
10
pkg/coredata/migrations/20260326T131816Z.sql
Normal file
10
pkg/coredata/migrations/20260326T131816Z.sql
Normal file
@@ -0,0 +1,10 @@
|
||||
CREATE TYPE evidence_description_status AS ENUM ('PENDING', 'PROCESSING', 'COMPLETED');
|
||||
|
||||
ALTER TABLE evidences
|
||||
ADD COLUMN description_status evidence_description_status NOT NULL DEFAULT 'PENDING',
|
||||
ADD COLUMN description_processing_started_at TIMESTAMPTZ;
|
||||
|
||||
UPDATE evidences SET description_status = 'COMPLETED' WHERE evidence_file_id IS NULL;
|
||||
|
||||
ALTER TABLE evidences
|
||||
ALTER COLUMN description_status DROP DEFAULT;
|
||||
Reference in New Issue
Block a user