mirror of
https://github.com/multica-ai/multica.git
synced 2026-07-16 22:59:04 +02:00
* feat(autopilot): add scheduled/triggered automation for AI agents Introduce the Autopilot feature — recurring automations that assign work to AI agents on a schedule or manual trigger. Supports two execution modes: create_issue (creates an issue for the agent to work on) and run_only (directly enqueues an agent task without issue pollution). Backend: migration (3 tables + 2 columns), sqlc queries, AutopilotService with concurrency policies (skip/queue/replace), HTTP CRUD + trigger endpoints, background cron scheduler (30s tick), event listeners for issue→run and task→run status sync. Frontend: types, API client methods, TanStack Query hooks with optimistic mutations, realtime cache invalidation, list page with create dialog, detail page with trigger management and run history, sidebar nav + routes for both web and desktop apps. * feat(autopilot): improve UX — trigger config, edit dialog, template gallery - Replace raw cron input with friendly frequency tabs (Hourly/Daily/Weekdays/Weekly/Custom), time picker, and timezone dropdown defaulting to user's local timezone - Fix Select components showing UUIDs instead of names (Base UI render function pattern) - Add Edit button on detail page opening a unified edit dialog - Remove project/concurrency/issue-title-template from create/edit (simplify for users) - Add trigger configuration inline during autopilot creation - Add template gallery on empty state (6 step-by-step workflow templates) - Rename "Description" to "Prompt" throughout UI - Inject autopilot run timestamp into issue description for agent date awareness - Treat issue status "in_review" as run completion (fixes skip on next trigger) - Make migration idempotent with IF NOT EXISTS clauses
113 lines
3.0 KiB
Go
113 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"github.com/multica-ai/multica/server/internal/events"
|
|
"github.com/multica-ai/multica/server/internal/logger"
|
|
"github.com/multica-ai/multica/server/internal/realtime"
|
|
"github.com/multica-ai/multica/server/internal/service"
|
|
db "github.com/multica-ai/multica/server/pkg/db/generated"
|
|
)
|
|
|
|
func main() {
|
|
logger.Init()
|
|
|
|
// Warn about missing configuration
|
|
if os.Getenv("JWT_SECRET") == "" {
|
|
slog.Warn("JWT_SECRET is not set — using insecure default. Set JWT_SECRET for production use.")
|
|
}
|
|
if os.Getenv("RESEND_API_KEY") == "" {
|
|
slog.Warn("RESEND_API_KEY is not set — email verification codes will be printed to the log instead of emailed.")
|
|
}
|
|
|
|
port := os.Getenv("PORT")
|
|
if port == "" {
|
|
port = "8080"
|
|
}
|
|
|
|
dbURL := os.Getenv("DATABASE_URL")
|
|
if dbURL == "" {
|
|
dbURL = "postgres://multica:multica@localhost:5432/multica?sslmode=disable"
|
|
}
|
|
|
|
// Connect to database
|
|
ctx := context.Background()
|
|
pool, err := pgxpool.New(ctx, dbURL)
|
|
if err != nil {
|
|
slog.Error("unable to connect to database", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
defer pool.Close()
|
|
|
|
if err := pool.Ping(ctx); err != nil {
|
|
slog.Error("unable to ping database", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
slog.Info("connected to database")
|
|
|
|
bus := events.New()
|
|
hub := realtime.NewHub()
|
|
go hub.Run()
|
|
registerListeners(bus, hub)
|
|
|
|
queries := db.New(pool)
|
|
// Order matters: subscriber listeners must register BEFORE notification listeners.
|
|
// The notification listener queries the subscriber table to determine recipients,
|
|
// so subscribers must be written first within the same synchronous event dispatch.
|
|
registerSubscriberListeners(bus, queries)
|
|
registerActivityListeners(bus, queries)
|
|
registerNotificationListeners(bus, queries)
|
|
|
|
r := NewRouter(pool, hub, bus)
|
|
|
|
srv := &http.Server{
|
|
Addr: ":" + port,
|
|
Handler: r,
|
|
}
|
|
|
|
// Start background workers.
|
|
sweepCtx, sweepCancel := context.WithCancel(context.Background())
|
|
autopilotCtx, autopilotCancel := context.WithCancel(context.Background())
|
|
taskSvc := service.NewTaskService(queries, hub, bus)
|
|
autopilotSvc := service.NewAutopilotService(queries, pool, bus, taskSvc)
|
|
registerAutopilotListeners(bus, autopilotSvc)
|
|
|
|
// Start background sweeper to mark stale runtimes as offline.
|
|
go runRuntimeSweeper(sweepCtx, queries, bus)
|
|
go runAutopilotScheduler(autopilotCtx, queries, autopilotSvc)
|
|
|
|
// Graceful shutdown
|
|
go func() {
|
|
slog.Info("server starting", "port", port)
|
|
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
slog.Error("server error", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
}()
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
<-quit
|
|
|
|
slog.Info("shutting down server")
|
|
sweepCancel()
|
|
autopilotCancel()
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
if err := srv.Shutdown(shutdownCtx); err != nil {
|
|
slog.Error("server forced to shutdown", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
slog.Info("server stopped")
|
|
}
|