feat: publish sanitized custom.97 source
This commit is contained in:
1022 files changed
+69903
-19207
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -19,7 +20,7 @@ type detectionHandler struct{}
|
||||
|
||||
func (detectionHandler) Type() string { return "group_detection" }
|
||||
func (detectionHandler) Enabled() bool {
|
||||
return common.GetEnvOrDefaultBool("MODEL_DETECTION_ENABLED", true)
|
||||
return common.GetEnvOrDefaultBool("VISION_DETECTION_ENABLED", true)
|
||||
}
|
||||
func (detectionHandler) Interval() time.Duration { return 15 * time.Second }
|
||||
func (detectionHandler) NewPayload() any { return nil }
|
||||
@@ -28,9 +29,9 @@ func (detectionHandler) Run(ctx context.Context, task *model.SystemTask, runner
|
||||
_ = model.DeliverDetectionLogs()
|
||||
// Keep accounting facts, but expire bulky generated artwork after the public window.
|
||||
_ = model.DB.Model(&model.DetectionRun{}).Where("settled = ? AND created_at < ? AND html <> ?", true, time.Now().Add(-6*time.Hour).Unix(), "").Update("html", "").Error
|
||||
scheduleErr := scheduleBenchmarkDetections(ctx)
|
||||
if candyErr := scheduleAutomaticDetections(ctx, "candy"); scheduleErr == nil {
|
||||
scheduleErr = candyErr
|
||||
scheduleErr := scheduleVisionDetections(ctx)
|
||||
if err := scheduleCialloDetections(ctx); err != nil {
|
||||
scheduleErr = err
|
||||
}
|
||||
var err error
|
||||
{
|
||||
@@ -66,12 +67,8 @@ func (detectionHandler) Run(ctx context.Context, task *model.SystemTask, runner
|
||||
finishSystemTaskHandler(task, runner, status, nil, err)
|
||||
}
|
||||
|
||||
func scheduleBenchmarkDetections(ctx context.Context) error {
|
||||
return scheduleAutomaticDetections(ctx, "benchmark")
|
||||
}
|
||||
|
||||
func scheduleAutomaticDetections(ctx context.Context, kind string) error {
|
||||
channels, schedules, err := service.AutomaticDetectionChannelsDue(ctx, kind, time.Now())
|
||||
func scheduleVisionDetections(ctx context.Context) error {
|
||||
channels, schedules, err := service.AutomaticDetectionChannelsDue(ctx, "vision", time.Now())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -81,7 +78,7 @@ func scheduleAutomaticDetections(ctx context.Context, kind string) error {
|
||||
if len(groups) == 0 {
|
||||
continue
|
||||
}
|
||||
price, reserve, priceErr := service.DetectionPricing("", groups[0], kind)
|
||||
price, reserve, priceErr := service.DetectionPricing("", groups[0], "vision")
|
||||
if priceErr != nil {
|
||||
return priceErr
|
||||
}
|
||||
@@ -92,7 +89,7 @@ func scheduleAutomaticDetections(ctx context.Context, kind string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
run := &model.DetectionRun{ID: id, RequestKey: fmt.Sprintf("%s:%d:%s", kind, channel.Id, schedules[channel.Id].LastResultID), ChannelID: channel.Id, GroupName: groups[0], Kind: kind, Status: "pending", CreatedAt: now, Price: string(encoded), Reserved: reserve}
|
||||
run := &model.DetectionRun{ID: id, RequestKey: fmt.Sprintf("vision:%d:%s", channel.Id, schedules[channel.Id].LastResultID), ChannelID: channel.Id, GroupName: groups[0], Kind: "vision", Status: "pending", CreatedAt: now, Price: string(encoded), Reserved: reserve}
|
||||
if err = model.CreateDetection(run); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -100,7 +97,18 @@ func scheduleAutomaticDetections(ctx context.Context, kind string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var activeDetectionRuns sync.Map
|
||||
|
||||
func processDetection(ctx context.Context, run model.DetectionRun) {
|
||||
if _, running := activeDetectionRuns.LoadOrStore(run.ID, true); running {
|
||||
return
|
||||
}
|
||||
defer activeDetectionRuns.Delete(run.ID)
|
||||
// Immediate bot execution and the durable worker may observe the same task.
|
||||
if model.DB.First(&run, "id = ?", run.ID).Error != nil || run.Settled {
|
||||
return
|
||||
}
|
||||
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
@@ -110,6 +118,8 @@ func processDetection(ctx context.Context, run model.DetectionRun) {
|
||||
return
|
||||
}
|
||||
}
|
||||
taskCtx, taskCancel := context.WithDeadline(ctx, time.Unix(run.Deadline, 0))
|
||||
defer taskCancel()
|
||||
if time.Now().Unix() >= run.Deadline {
|
||||
var calls []model.DetectionCall
|
||||
if model.DB.Where("run_id = ?", run.ID).Find(&calls).Error != nil {
|
||||
@@ -129,25 +139,31 @@ func processDetection(ctx context.Context, run model.DetectionRun) {
|
||||
call.Quota = 0
|
||||
_ = model.FinishDetectionCall(&call)
|
||||
}
|
||||
finishDetection(run, "failed", "detection_timeout")
|
||||
if run.Kind == "vision" || run.Kind == "ciallo" {
|
||||
finishVisionDetection(run, "", "detection_timeout")
|
||||
} else {
|
||||
finishDetection(run, "failed", "detection_timeout")
|
||||
}
|
||||
return
|
||||
}
|
||||
if run.Status == "succeeded" || run.Status == "failed" {
|
||||
settleDetectionAndSchedule(run)
|
||||
return
|
||||
}
|
||||
if run.Kind != "fingerprint" && run.Kind != "benchmark" && run.Kind != "candy" {
|
||||
finishDetection(run, "failed", "detection_retired")
|
||||
if run.Kind == "fingerprint" {
|
||||
processPaidFingerprint(taskCtx, run)
|
||||
return
|
||||
}
|
||||
processPaidFingerprint(ctx, run)
|
||||
if run.Kind == "vision" || run.Kind == "ciallo" {
|
||||
processVisionDetection(taskCtx, run)
|
||||
return
|
||||
}
|
||||
finishDetection(run, "failed", "detection_retired")
|
||||
}
|
||||
|
||||
func finishDetection(run model.DetectionRun, status, reason string) {
|
||||
updates := map[string]any{"status": status, "error": reason}
|
||||
if status == "failed" && run.Kind == "benchmark" {
|
||||
updates["result"] = ""
|
||||
}
|
||||
|
||||
if model.DB.Model(&model.DetectionRun{}).Where("id = ?", run.ID).Updates(updates).Error != nil {
|
||||
return
|
||||
}
|
||||
@@ -155,13 +171,21 @@ func finishDetection(run model.DetectionRun, status, reason string) {
|
||||
}
|
||||
|
||||
func settleDetectionAndSchedule(run model.DetectionRun) {
|
||||
if model.SettleDetection(run.ID) != nil || run.UserID != 0 || (run.Kind != "benchmark" && run.Kind != "candy") {
|
||||
if model.SettleDetection(run.ID) != nil {
|
||||
return
|
||||
}
|
||||
if run.Kind == "vision" || run.Kind == "ciallo" || run.Kind == "fingerprint" {
|
||||
service.InvalidateGroupQualityCache()
|
||||
publishGroupStatusChange()
|
||||
}
|
||||
if run.UserID != 0 || strings.HasPrefix(run.RequestKey, "chain:") || (run.Kind != "vision" && run.Kind != "ciallo") {
|
||||
return
|
||||
}
|
||||
// Reconciliation also repairs the clock if a process exits after settlement.
|
||||
if err := model.DB.Select("id", "channel_id", "finished_at", "status", "quality").First(&run, "id = ?", run.ID).Error; err != nil {
|
||||
if err := model.DB.Select("id", "channel_id", "finished_at", "status", "quality", "kind", "error", "result").First(&run, "id = ?", run.ID).Error; err != nil {
|
||||
return
|
||||
}
|
||||
run = service.NormalizeVisionRun(run)
|
||||
outcome := "unknown"
|
||||
if run.Status == "succeeded" {
|
||||
outcome = run.Quality
|
||||
@@ -179,11 +203,9 @@ func processPaidFingerprint(ctx context.Context, run model.DetectionRun) {
|
||||
if run.Kind == "benchmark" {
|
||||
output, calls = benchmark.OutputTokens, benchmark.ProbeCount
|
||||
}
|
||||
if run.Kind == "candy" {
|
||||
output, calls = service.CandyOutputTokens, 1
|
||||
}
|
||||
|
||||
var price service.DetectionPrice
|
||||
if common.UnmarshalJsonStr(run.Price, &price) != nil || gjson.GetBytes(price.Request.Body, "max_output_tokens").Int() != int64(output) {
|
||||
if run.UserID > 0 && (common.UnmarshalJsonStr(run.Price, &price) != nil || gjson.GetBytes(price.Request.Body, "max_output_tokens").Int() != int64(output)) {
|
||||
finishDetection(run, "failed", "benchmark_changed")
|
||||
return
|
||||
}
|
||||
@@ -211,6 +233,7 @@ func processPaidFingerprint(ctx context.Context, run model.DetectionRun) {
|
||||
probeCtx, cancel := context.WithDeadline(ctx, time.Unix(run.Deadline-10, 0))
|
||||
detectionCalls.Store(call.ID, cancel)
|
||||
answer, count, probeErr := runModelDetectionProbe(probeCtx, channel, index, run.Kind)
|
||||
call.FirstTokenMS = answer.FirstTokenMS
|
||||
interrupted := probeCtx.Err() != nil
|
||||
cancel()
|
||||
available := probeErr == nil || answer.HasUsage
|
||||
@@ -224,16 +247,18 @@ func processPaidFingerprint(ctx context.Context, run model.DetectionRun) {
|
||||
if answer.HasUsage {
|
||||
usage, _ := common.Marshal(answer)
|
||||
call.Usage = string(usage)
|
||||
quota, quotaErr := service.DetectionUsageQuota(run.Price, answer.InputTokens, answer.OutputTokens, answer.CachedTokens, answer.CacheWriteTokens)
|
||||
if quotaErr != nil || quota > run.Reserved/calls {
|
||||
call.Error = "settlement_unavailable"
|
||||
} else {
|
||||
call.Quota = quota
|
||||
if run.UserID > 0 {
|
||||
quota, quotaErr := service.DetectionUsageQuota(run.Price, answer.InputTokens, answer.OutputTokens, answer.CachedTokens, answer.CacheWriteTokens)
|
||||
if quotaErr != nil || quota > run.Reserved/calls {
|
||||
call.Error = "settlement_unavailable"
|
||||
} else {
|
||||
call.Quota = quota
|
||||
}
|
||||
}
|
||||
} else {
|
||||
} else if run.UserID > 0 {
|
||||
call.Error = "usage_unavailable"
|
||||
}
|
||||
if probeErr == nil && ((run.Kind == "candy" && answer.Text != "") || (run.Kind == "benchmark" && benchmark.ValidSample(answer.Text)) || (run.Kind == "fingerprint" && fingerprint.ValidArray(answer.Text, count))) {
|
||||
if probeErr == nil && ((run.Kind == "benchmark" && benchmark.ValidSample(answer.Text)) || (run.Kind == "fingerprint" && fingerprint.ValidArray(answer.Text, count))) {
|
||||
sample := fingerprint.Sample{Text: answer.Text, Count: count}
|
||||
samples = append(samples, sample)
|
||||
encoded, _ := common.Marshal(sample)
|
||||
@@ -248,6 +273,10 @@ func processPaidFingerprint(ctx context.Context, run model.DetectionRun) {
|
||||
return
|
||||
}
|
||||
}
|
||||
if strings.HasPrefix(run.RequestKey, "chain:") && len(samples) != calls {
|
||||
finishDetection(run, "failed", "fingerprint_response_invalid")
|
||||
return
|
||||
}
|
||||
var result fingerprint.Result
|
||||
if run.Kind == "benchmark" {
|
||||
secondary := make([]benchmark.Sample, len(samples))
|
||||
@@ -257,12 +286,6 @@ func processPaidFingerprint(ctx context.Context, run model.DetectionRun) {
|
||||
scored, scoreErr := benchmark.Score(secondary)
|
||||
err = scoreErr
|
||||
result = fingerprint.Result{Status: scored.Status, Engine: scored.Engine, ValidSamples: scored.ValidSamples, Closest: scored.Closest, Weight: scored.Weight, Gap: scored.Gap, Matches: scored.Matches}
|
||||
} else if run.Kind == "candy" {
|
||||
if len(samples) != 1 {
|
||||
finishDetection(run, "failed", "answer_unavailable")
|
||||
return
|
||||
}
|
||||
result = fingerprint.Result{Status: service.CandyQuality(samples[0].Text), Engine: "candy-v1", ValidSamples: 1, Closest: samples[0].Text}
|
||||
} else {
|
||||
result, err = fingerprint.Score(samples)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user