Files
geo/server/internal/tenant/app/desktop_task_events.go
T
root 55e1c2e74b feat(desktop): add monitor scheduler, Playwright CDP layer, and account health
Move monitor-task scheduling authority onto the client with a durable
file-backed queue that survives restart, drops stale cross-day tasks,
enforces per-platform serialism, and adapts global concurrency from
Electron process metrics. Publish tasks keep their existing FIFO.

Add a hidden Playwright CDP manager that attaches to Electron Chromium
on account session partitions, lets adapters opt into `executionMode:
"playwright"`, and leaves the existing hidden WebContentsView path in
place for current adapters.

Introduce an account-health subsystem with silent probes, projected
health/auth states, and IPC invalidation events so the renderer can
show accurate auth/probe status and verification timestamps.

Server-side, derive and forward title/business_date/scheduler_group_key/
question_text metadata on desktop task events so the local scheduler
can defer same-question fan-out before leasing.
2026-04-20 17:16:15 +08:00

127 lines
3.1 KiB
Go

package app
import (
"context"
"crypto/sha1"
"encoding/json"
"fmt"
"time"
"github.com/geo-platform/tenant-api/internal/shared/messaging/rabbitmq"
)
type DesktopTaskEvent struct {
Type string `json:"type"`
TaskID string `json:"task_id"`
JobID string `json:"job_id"`
WorkspaceID int64 `json:"workspace_id"`
TargetAccountID string `json:"target_account_id"`
TargetClientID string `json:"target_client_id"`
Platform string `json:"platform"`
Title *string `json:"title,omitempty"`
BusinessDate *string `json:"business_date,omitempty"`
SchedulerGroupKey *string `json:"scheduler_group_key,omitempty"`
QuestionText *string `json:"question_text,omitempty"`
Status string `json:"status"`
Kind string `json:"kind"`
UpdatedAt time.Time `json:"updated_at"`
}
func publishDesktopTaskEvent(ctx context.Context, client *rabbitmq.Client, event DesktopTaskEvent) error {
if client == nil {
return nil
}
payload, err := json.Marshal(event)
if err != nil {
return err
}
return client.PublishDesktopTaskEvent(ctx, payload)
}
func deriveDesktopTaskEventMetadataFromPayload(
kind string,
payload []byte,
) (title *string, businessDate *string, schedulerGroupKey *string, questionText *string) {
if len(payload) == 0 {
return nil, nil, nil, nil
}
var envelope map[string]any
if err := json.Unmarshal(payload, &envelope); err != nil {
return nil, nil, nil, nil
}
title = firstDesktopTaskEventString(envelope,
"title",
"question_title",
"questionTitle",
)
businessDate = firstDesktopTaskEventString(envelope,
"business_date",
"businessDate",
"metric_date",
"date",
)
questionText = firstDesktopTaskEventString(envelope,
"question_text",
"questionText",
"query",
"question",
"prompt",
"content",
)
if explicit := firstDesktopTaskEventString(envelope,
"scheduler_group_key",
"schedulerGroupKey",
); explicit != nil {
return title, businessDate, explicit, questionText
}
if questionHash := firstDesktopTaskEventString(envelope, "question_hash", "questionHash"); questionHash != nil {
return title, businessDate, questionHash, questionText
}
if questionID := firstDesktopTaskEventString(envelope, "question_id", "questionId"); questionID != nil {
value := fmt.Sprintf("qid:%s", *questionID)
return title, businessDate, &value, questionText
}
if kind == "monitor" && questionText != nil {
sum := sha1.Sum([]byte(*questionText))
value := fmt.Sprintf("qtxt:%x", sum)
return title, businessDate, &value, questionText
}
return title, businessDate, nil, questionText
}
func firstDesktopTaskEventString(payload map[string]any, keys ...string) *string {
for _, key := range keys {
value, exists := payload[key]
if !exists {
continue
}
switch typed := value.(type) {
case string:
if typed != "" {
return &typed
}
case float64:
text := fmt.Sprintf("%.0f", typed)
return &text
case int64:
text := fmt.Sprintf("%d", typed)
return &text
case int:
text := fmt.Sprintf("%d", typed)
return &text
}
}
return nil
}