mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-06 21:11:50 +08:00
feat: search approval history by content and date
This commit is contained in:
1 parent
e33eedeb17
commit
2f2b78cc78
9 files changed
+255
-11
No files matched your search
+48
-2
@@ -4,6 +4,7 @@ import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -239,6 +240,7 @@ const approvalRowColumns = `ip.id, ip.rule_id, ip.conversation_id, ip.task_id, i
|
||||
const approvalRowJoins = `
|
||||
FROM intercept_pending ip
|
||||
LEFT JOIN conversations c ON c.id = ip.conversation_id
|
||||
LEFT JOIN tasks t ON t.id::text = ip.task_id
|
||||
LEFT JOIN intercept_rules ir ON ir.id = ip.rule_id`
|
||||
|
||||
const approvalRowSelect = `SELECT ` + approvalRowColumns + approvalRowJoins
|
||||
@@ -284,11 +286,14 @@ func (d *DB) ListAllIntercepts(limit int) ([]InterceptApprovalRow, error) {
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// InterceptApprovalFilter combines exact status and decision-source filters.
|
||||
// InterceptApprovalFilter combines status, decision-source and literal substring search.
|
||||
// Empty fields include all values.
|
||||
type InterceptApprovalFilter struct {
|
||||
Status string
|
||||
DecisionSource string
|
||||
Search string
|
||||
CreatedFrom string // inclusive UTC calendar date, YYYY-MM-DD
|
||||
CreatedTo string // inclusive UTC calendar date, YYYY-MM-DD
|
||||
}
|
||||
|
||||
// ListAllInterceptsPage returns one 1-based page and the total matching count.
|
||||
@@ -342,12 +347,53 @@ func (d *DB) listInterceptsPage(taskID string, page, size int, filter InterceptA
|
||||
add("ip.task_id", taskID)
|
||||
add("ip.status", filter.Status)
|
||||
add(approvalDecisionSource, filter.DecisionSource)
|
||||
if filter.CreatedFrom != "" {
|
||||
args = append(args, filter.CreatedFrom)
|
||||
conditions = append(conditions, "ip.created_at >= $"+fmt.Sprint(len(args))+"::date")
|
||||
}
|
||||
if filter.CreatedTo != "" {
|
||||
args = append(args, filter.CreatedTo)
|
||||
conditions = append(conditions, "ip.created_at < ($"+fmt.Sprint(len(args))+"::date + INTERVAL '1 day')")
|
||||
}
|
||||
search := strings.TrimSpace(filter.Search)
|
||||
if strings.HasPrefix(search, "#") {
|
||||
id, err := strconv.ParseInt(strings.TrimPrefix(search, "#"), 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
return nil, 0, fmt.Errorf("审批编号必须为 # 后接正整数")
|
||||
}
|
||||
add("ip.id", strconv.FormatInt(id, 10))
|
||||
} else if search != "" {
|
||||
// Treat SQL LIKE metacharacters literally, including URL percent escapes.
|
||||
pattern := strings.NewReplacer(`\`, `\\`, `%`, `\%`, `_`, `\_`).Replace(search)
|
||||
args = append(args, "%"+pattern+"%")
|
||||
like := "$" + fmt.Sprint(len(args))
|
||||
conditions = append(conditions, `(
|
||||
ip.id::text ILIKE `+like+`
|
||||
OR ip.conversation_id::text ILIKE `+like+`
|
||||
OR COALESCE(ip.task_id, '') ILIKE `+like+`
|
||||
OR COALESCE(ip.agent_name, '') ILIKE `+like+`
|
||||
OR COALESCE(ip.tool_name, '') ILIKE `+like+`
|
||||
OR COALESCE(ip.tool_input::text, '') ILIKE `+like+`
|
||||
OR COALESCE(ip.reason, '') ILIKE `+like+`
|
||||
OR COALESCE(ip.audit::text, '') ILIKE `+like+`
|
||||
OR COALESCE(c.title, '') ILIKE `+like+`
|
||||
OR COALESCE(c.agent_key, '') ILIKE `+like+`
|
||||
OR COALESCE(t.name, '') ILIKE `+like+`
|
||||
OR COALESCE(t.description, '') ILIKE `+like+`
|
||||
OR COALESCE(t.goal, '') ILIKE `+like+`
|
||||
OR COALESCE(ir.name, '') ILIKE `+like+`
|
||||
)`)
|
||||
}
|
||||
where := ""
|
||||
if len(conditions) > 0 {
|
||||
where = " WHERE " + strings.Join(conditions, " AND ")
|
||||
}
|
||||
var total int
|
||||
if err := d.QueryRow("SELECT COUNT(*) FROM intercept_pending ip"+where, args...).Scan(&total); err != nil {
|
||||
countFrom := "FROM intercept_pending ip"
|
||||
if search != "" && !strings.HasPrefix(search, "#") {
|
||||
countFrom = approvalRowJoins
|
||||
}
|
||||
if err := d.QueryRow("SELECT COUNT(*) "+countFrom+where, args...).Scan(&total); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"slices"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -138,4 +140,28 @@ func TestInterceptApprovalFilters(t *testing.T) {
|
||||
t.Fatal("deleted rule lost its source")
|
||||
}
|
||||
}
|
||||
|
||||
modelInput := json.RawMessage(`{"script_files":[{"path":"scripts/check.py","content":"https://example.test/a?x=100%"}]}`)
|
||||
searchID, err := d.CreateDecidedIntercept(0, 0, scope, "search-agent", "Bash", []byte(`{"command":"python3 scripts/check.py"}`), "denied", "[模型] searched", &InterceptAudit{ModelInput: modelInput, Output: "response contains example.test"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rows, total, err = d.ListTaskInterceptsPage(scope, 1, 20, InterceptApprovalFilter{Search: "example.test"})
|
||||
if err != nil || total != 1 || len(rows) != 1 || rows[0].ID != searchID {
|
||||
t.Fatalf("snapshot search total=%d rows=%d err=%v", total, len(rows), err)
|
||||
}
|
||||
rows, total, err = d.ListTaskInterceptsPage(scope, 1, 20, InterceptApprovalFilter{Search: "100%"})
|
||||
if err != nil || total != 1 || len(rows) != 1 {
|
||||
t.Fatalf("literal percent search total=%d rows=%d err=%v", total, len(rows), err)
|
||||
}
|
||||
rows, total, err = d.ListTaskInterceptsPage(scope, 1, 20, InterceptApprovalFilter{Search: "#" + strconv.FormatInt(searchID, 10)})
|
||||
if err != nil || total != 1 || len(rows) != 1 || rows[0].ID != searchID {
|
||||
t.Fatalf("id search total=%d rows=%d err=%v", total, len(rows), err)
|
||||
}
|
||||
rows, total, err = d.ListTaskInterceptsPage(scope, 1, 20, InterceptApprovalFilter{
|
||||
Search: "example.test", CreatedFrom: "2099-01-01", CreatedTo: "2099-01-01",
|
||||
})
|
||||
if err != nil || total != 1 || len(rows) != 1 || rows[0].ID != searchID {
|
||||
t.Fatalf("date search total=%d rows=%d err=%v", total, len(rows), err)
|
||||
}
|
||||
}
|
||||
@@ -1112,6 +1112,7 @@ CREATE TABLE IF NOT EXISTS intercept_pending (
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_intercept_pending_status ON intercept_pending(status, created_at DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_intercept_pending_task ON intercept_pending(task_id, created_at DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_intercept_pending_created ON intercept_pending(created_at DESC);
|
||||
-- 补旧库:reason 列(已发版,加列要带 IF NOT EXISTS)。
|
||||
ALTER TABLE intercept_pending ADD COLUMN IF NOT EXISTS reason TEXT NOT NULL DEFAULT '';
|
||||
-- Detail payloads are lazy-loaded; NULL preserves the meaning of legacy history.
|
||||
|
||||
Reference in new issue
Block a user