forked from longtao.wu/droboticflow
feat: ✨ deliver workflow platform backend and editor improvements
This commit is contained in:
parent
44a6be5aeb
commit
46c762384c
9
.dockerignore
Normal file
9
.dockerignore
Normal file
@ -0,0 +1,9 @@
|
||||
node_modules
|
||||
dist
|
||||
build
|
||||
coverage
|
||||
.git
|
||||
.gitignore
|
||||
.env
|
||||
.env.local
|
||||
*.log
|
||||
24
.env.example
24
.env.example
@ -1,9 +1,15 @@
|
||||
# GEMINI_API_KEY: Required for Gemini AI API calls.
|
||||
# AI Studio automatically injects this at runtime from user secrets.
|
||||
# Users configure this via the Secrets panel in the AI Studio UI.
|
||||
GEMINI_API_KEY="MY_GEMINI_API_KEY"
|
||||
|
||||
# APP_URL: The URL where this applet is hosted.
|
||||
# AI Studio automatically injects this at runtime with the Cloud Run service URL.
|
||||
# Used for self-referential links, OAuth callbacks, and API endpoints.
|
||||
APP_URL="MY_APP_URL"
|
||||
PORT=3001
|
||||
WORKER_PORT=3002
|
||||
MONGO_URI=mongodb://127.0.0.1:27017
|
||||
MONGO_DB=droboticflow
|
||||
MONGO_CONNECT_RETRIES=30
|
||||
MONGO_CONNECT_RETRY_DELAY_MS=2000
|
||||
WORKER_POLL_INTERVAL_MS=2000
|
||||
K8S_ENABLED=false
|
||||
K8S_NAMESPACE=droboticflow
|
||||
K8S_IMAGE_PULL_SECRET_NAME=droboticflow-registry
|
||||
OPERATOR_RUNNER_IMAGE=ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-operator-runner:latest
|
||||
VITE_API_BASE_URL=
|
||||
AI_API_BASE_URL=https://coding.dashscope.aliyuncs.com/v1
|
||||
AI_MODEL=glm-5
|
||||
AI_API_KEY=
|
||||
|
||||
32
Dockerfile.api
Normal file
32
Dockerfile.api
Normal file
@ -0,0 +1,32 @@
|
||||
FROM node:22-alpine AS web-build
|
||||
WORKDIR /app
|
||||
|
||||
COPY package*.json ./
|
||||
RUN npm ci
|
||||
|
||||
COPY index.html metadata.json package.json tsconfig.json vite.config.ts ./
|
||||
COPY src ./src
|
||||
RUN npm run build:web
|
||||
|
||||
FROM golang:1.24-bookworm AS go-build
|
||||
WORKDIR /src
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
COPY cmd ./cmd
|
||||
COPY internal ./internal
|
||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /out/droboticflow-api ./cmd/api
|
||||
|
||||
FROM debian:bookworm-slim AS runtime
|
||||
WORKDIR /app
|
||||
ENV PORT=3001
|
||||
ENV DIST_DIR=/app/dist
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY --from=web-build /app/dist ./dist
|
||||
COPY --from=go-build /out/droboticflow-api /usr/local/bin/droboticflow-api
|
||||
|
||||
EXPOSE 3001
|
||||
CMD ["droboticflow-api"]
|
||||
7
Dockerfile.operator-runner
Normal file
7
Dockerfile.operator-runner
Normal file
@ -0,0 +1,7 @@
|
||||
FROM python:3.11-slim AS runtime
|
||||
WORKDIR /app
|
||||
ENV PYTHONUNBUFFERED=1
|
||||
|
||||
COPY backend/operator-runner/main.py ./main.py
|
||||
|
||||
CMD ["python", "/app/main.py"]
|
||||
20
Dockerfile.worker
Normal file
20
Dockerfile.worker
Normal file
@ -0,0 +1,20 @@
|
||||
FROM golang:1.24-bookworm AS build
|
||||
WORKDIR /src
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
COPY cmd ./cmd
|
||||
COPY internal ./internal
|
||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /out/droboticflow-worker ./cmd/worker
|
||||
|
||||
FROM debian:bookworm-slim AS runtime
|
||||
WORKDIR /app
|
||||
ENV WORKER_PORT=3002
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY --from=build /out/droboticflow-worker /usr/local/bin/droboticflow-worker
|
||||
|
||||
EXPOSE 3002
|
||||
CMD ["droboticflow-worker"]
|
||||
178
README.md
178
README.md
@ -1,20 +1,174 @@
|
||||
<div align="center">
|
||||
<img width="1200" height="475" alt="GHBanner" src="https://github.com/user-attachments/assets/0aa67016-6eaf-458a-adb2-6e31a0763ed6" />
|
||||
</div>
|
||||
# DRoboticFlow
|
||||
|
||||
# Run and deploy your AI Studio app
|
||||
DRoboticFlow is a workflow orchestration platform for embodied AI and vision pipelines. This repository now contains:
|
||||
|
||||
This contains everything you need to run your app locally.
|
||||
- a React/Vite frontend
|
||||
- a Go-based MongoDB-backed REST API
|
||||
- a Go-based worker service that executes workflow DAGs
|
||||
- a Python-based Kubernetes Job operator runner
|
||||
- deployment assets for MongoDB, Kubernetes, and Baidu Cloud Container Registry
|
||||
|
||||
View your app in AI Studio: https://ai.studio/apps/0006ee57-f2f7-45cb-b325-2830a8f19364
|
||||
## Architecture
|
||||
|
||||
## Run Locally
|
||||
- `cmd/api`: Go API entrypoint and SPA host
|
||||
- `cmd/worker`: Go worker entrypoint and worker control API
|
||||
- `internal/api`: REST handlers and request validation
|
||||
- `internal/worker`: run scheduler, DAG executor, audit pause/resume, worker control API
|
||||
- `internal/store`: Mongo-backed repositories plus in-memory test store
|
||||
- `internal/k8s`: Kubernetes Job launcher for operator nodes
|
||||
- `backend/operator-runner`: Python entrypoint used by Kubernetes Jobs
|
||||
- `src`: existing frontend, now wired to real APIs
|
||||
- `k8s`: Kubernetes manifest template
|
||||
- `scripts`: build, render, deploy, and smoke test scripts
|
||||
- `docs`: API, deployment, operations, and design documents
|
||||
|
||||
**Prerequisites:** Node.js
|
||||
## Local Development
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- Node.js 22+
|
||||
- Go 1.24+
|
||||
- Python 3.9+
|
||||
- MongoDB 7+ reachable at `MONGO_URI`
|
||||
|
||||
### Setup
|
||||
|
||||
1. Install dependencies:
|
||||
`npm install`
|
||||
2. Set the `GEMINI_API_KEY` in [.env.local](.env.local) to your Gemini API key
|
||||
3. Run the app:
|
||||
`npm run dev`
|
||||
|
||||
```bash
|
||||
npm install
|
||||
```
|
||||
|
||||
2. Copy the example environment file and adjust values:
|
||||
|
||||
```bash
|
||||
cp .env.example .env.local
|
||||
```
|
||||
|
||||
3. Start the Go API and frontend:
|
||||
|
||||
```bash
|
||||
npm run dev
|
||||
```
|
||||
|
||||
4. Start the full stack including Go worker:
|
||||
|
||||
```bash
|
||||
npm run dev:full
|
||||
```
|
||||
|
||||
### Useful Scripts
|
||||
|
||||
- `npm run lint`: type-check frontend and verify Go formatting
|
||||
- `npm test`: Go unit and integration tests
|
||||
- `npm run build`: build frontend assets and Go binaries
|
||||
- `npm run verify`: lint + test + build
|
||||
- `npm run smoke`: local smoke test against the running API and worker
|
||||
- `npm run reconcile:data -- <api-base-url>`: remove legacy demo data, reset settings, and bootstrap built-in operators through the real API
|
||||
|
||||
## Runtime Configuration
|
||||
|
||||
Key environment variables:
|
||||
|
||||
- `PORT`: API port, default `3001`
|
||||
- `WORKER_PORT`: worker port, default `3002`
|
||||
- `MONGO_URI`: MongoDB connection string
|
||||
- `MONGO_DB`: Mongo database name
|
||||
- `MONGO_CONNECT_RETRIES`: startup retry count for API and worker Mongo connections
|
||||
- `MONGO_CONNECT_RETRY_DELAY_MS`: delay between Mongo connection retries
|
||||
- `WORKER_POLL_INTERVAL_MS`: worker polling interval
|
||||
- `K8S_ENABLED`: enable Kubernetes Job execution in the worker
|
||||
- `K8S_NAMESPACE`: namespace used for Jobs
|
||||
- `K8S_IMAGE_PULL_SECRET_NAME`: image pull secret injected into operator-runner Jobs
|
||||
- `OPERATOR_RUNNER_IMAGE`: container image used for node execution Jobs
|
||||
- `AI_API_BASE_URL`: OpenAI-compatible DashScope endpoint used by server-side AI generation, default `https://coding.dashscope.aliyuncs.com/v1`
|
||||
- `AI_MODEL`: server-side model name, default `glm-5`
|
||||
- `AI_API_KEY`: server-side DashScope-compatible API key used for workflow/operator generation
|
||||
- `VITE_API_BASE_URL`: optional frontend API base URL override
|
||||
|
||||
The frontend never reads the AI key directly. Workflow generation and operator generation are proxied through the Go API so secrets stay on the server side.
|
||||
The settings page can update K8s access details, the worker defaults, the global `timezone`, `provider`, `baseURL`, `model`, and the server-side AI key. `GET /api/settings` only returns whether secrets exist via `k8s.hasKubeconfig` / `ai.hasAPIKey`; it does not echo plaintext kubeconfig or API keys back to the browser. Timestamps in the UI are rendered in the configured IANA timezone with compact minute-level formatting for cards, lists, and summaries, while logs keep finer granularity when needed. OSS-related bucket and path settings belong on OSS operators in workflows, not in the global system settings page. The OSS browser APIs also now require the current node `SOURCE_PATH` and only list content inside that node-scoped root.
|
||||
|
||||
Workflow execution concurrency now has two levels:
|
||||
|
||||
- system default: `settings.worker.maxConcurrentRuns`
|
||||
- workflow override: `workflow.maxConcurrentRuns`
|
||||
|
||||
The worker enforces both. A workflow value of `0` means "inherit the global default". Dispatch only starts queued runs while the global active run count is below the system limit and the active count for that workflow is below its effective limit.
|
||||
|
||||
Workflow-level execution safeguards are configured alongside the other top-bar workflow settings in the editor:
|
||||
|
||||
- `workflow.nodeTimeoutSeconds`
|
||||
- `0`: disable workflow-level per-node timeout
|
||||
- `1+`: fail an operator execution once a single node exceeds the configured seconds
|
||||
- `workflow.runTimeoutSeconds`
|
||||
- `0`: disable workflow-level timeout
|
||||
- `1+`: fail the current run once total execution time exceeds the configured seconds
|
||||
- `workflow.maxRetryAttempts`
|
||||
- `0`: disable automatic retry
|
||||
- `1+`: automatically enqueue a new retry run after a failure, up to the configured count
|
||||
- `workflow.retryBackoffSeconds`
|
||||
- `0`: retry immediately
|
||||
- `1+`: keep the retry run queued until its backoff window expires
|
||||
|
||||
Automatic retries create real follow-up runs with `trigger=retry`, `attempt=2+`, `parentRunId` pointing at the failed attempt, and `availableAt` marking the first dispatchable time after backoff. Monitoring and history stay auditable because the original run is never mutated into the retry attempt.
|
||||
|
||||
Workflow templates now carry the same workflow-level defaults as project workflows. When you save a workflow as a template, duplicate a template, edit a template, or instantiate a template into a project, the workflow `type`, `schedule`, and execution-safeguard fields move across with it instead of being dropped or reset.
|
||||
|
||||
Artifact previews are persisted with the run artifacts themselves. JSON artifacts keep their structured payload in `artifact.data`, while non-JSON artifacts can attach a compact `artifact.data.preview` block for text, CSV/TSV, JSONL, images, audio/video metadata, PDF/document hints, SQLite/rosbag summaries, and LeRobot dataset summaries. Artifacts also carry `portId` / `portName`, which lets the worker route multi-output operators by edge handle instead of mixing all outputs together.
|
||||
|
||||
The built-in operator catalog now includes real data-processing and robotics helpers on top of the original source/merge/upload/audit flow: `目录扫描`, `CSV 转 JSON`, `JSON 转 CSV`, `JSON 字段提取`, `JSON 过滤`, `JSON 映射重命名`, `文本模板渲染`, `条件分流`, `LeRobot 数据集概览`, `ROSBag 数据概览`, and `Shell 命令执行`.
|
||||
|
||||
## Deployment
|
||||
|
||||
The repository includes three container images:
|
||||
|
||||
- `droboticflow-api`
|
||||
- `droboticflow-worker`
|
||||
- `droboticflow-operator-runner`
|
||||
|
||||
The API image builds the Vite frontend and a Go binary into one container. The worker image contains a Go binary. The operator-runner image is Python-only and is launched by the worker as a Kubernetes Job.
|
||||
|
||||
Build and push images:
|
||||
|
||||
```bash
|
||||
./scripts/build-and-push-images.sh
|
||||
```
|
||||
|
||||
If the target cluster cannot pull the upstream `mongo:7` image from Docker Hub, mirror it into the same Baidu registry and set `MONGO_IMAGE` during deployment.
|
||||
|
||||
Render Kubernetes manifests:
|
||||
|
||||
```bash
|
||||
./scripts/render-manifests.sh
|
||||
```
|
||||
|
||||
Deploy to Kubernetes:
|
||||
|
||||
```bash
|
||||
./scripts/deploy-k8s.sh
|
||||
```
|
||||
|
||||
Reconcile persistent API data after deployment:
|
||||
|
||||
```bash
|
||||
./scripts/reconcile-system-data.sh http://<node-ip>:30080
|
||||
```
|
||||
|
||||
Run a smoke test locally:
|
||||
|
||||
```bash
|
||||
npm run smoke
|
||||
```
|
||||
|
||||
Run a smoke test against a deployed cluster:
|
||||
|
||||
```bash
|
||||
./scripts/smoke-test.sh http://<node-ip>:30080
|
||||
```
|
||||
|
||||
See the detailed guides:
|
||||
|
||||
- [`docs/api.md`](/Users/longtaowu/workspace/droboticflow/docs/api.md)
|
||||
- [`docs/deployment.md`](/Users/longtaowu/workspace/droboticflow/docs/deployment.md)
|
||||
- [`docs/operations.md`](/Users/longtaowu/workspace/droboticflow/docs/operations.md)
|
||||
|
||||
1470
backend/operator-runner/main.py
Normal file
1470
backend/operator-runner/main.py
Normal file
File diff suppressed because it is too large
Load Diff
86
cmd/api/main.go
Normal file
86
cmd/api/main.go
Normal file
@ -0,0 +1,86 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"droboticflow/internal/ai"
|
||||
"droboticflow/internal/api"
|
||||
"droboticflow/internal/config"
|
||||
"droboticflow/internal/retry"
|
||||
"droboticflow/internal/store"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func connectMongo(ctx context.Context, mongoURI string, attempts int, delay time.Duration) (*mongo.Client, error) {
|
||||
return retry.Do(ctx, attempts, delay, "mongodb connection", func(ctx context.Context, _ int) (*mongo.Client, error) {
|
||||
client, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoURI))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := client.Ping(ctx, nil); err != nil {
|
||||
_ = client.Disconnect(ctx)
|
||||
return nil, err
|
||||
}
|
||||
return client, nil
|
||||
})
|
||||
}
|
||||
|
||||
func spaHandler(apiHandler http.Handler, distDir string) http.Handler {
|
||||
fileServer := http.FileServer(http.Dir(distDir))
|
||||
indexPath := filepath.Join(distDir, "index.html")
|
||||
|
||||
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
if strings.HasPrefix(request.URL.Path, "/api/") || strings.HasPrefix(request.URL.Path, "/worker-api/") {
|
||||
apiHandler.ServeHTTP(writer, request)
|
||||
return
|
||||
}
|
||||
if request.URL.Path == "/" {
|
||||
fileServer.ServeHTTP(writer, request)
|
||||
return
|
||||
}
|
||||
targetPath := filepath.Join(distDir, filepath.Clean(request.URL.Path))
|
||||
if _, err := os.Stat(targetPath); err == nil {
|
||||
fileServer.ServeHTTP(writer, request)
|
||||
return
|
||||
}
|
||||
http.ServeFile(writer, request, indexPath)
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
cfg := config.Load()
|
||||
client, err := connectMongo(ctx, cfg.MongoURI, cfg.MongoConnectRetries, time.Duration(cfg.MongoConnectRetryDelayMS)*time.Millisecond)
|
||||
if err != nil {
|
||||
fmt.Printf("[api] failed to connect mongo: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer func() {
|
||||
_ = client.Disconnect(context.Background())
|
||||
}()
|
||||
|
||||
dataStore := store.NewMongoStore(client.Database(cfg.MongoDB))
|
||||
|
||||
aiService := ai.NewService(cfg.AIAPIBaseURL, cfg.AIAPIKey, cfg.AIModel)
|
||||
handler := spaHandler(api.NewHandler(dataStore, aiService), cfg.DistDir)
|
||||
server := &http.Server{
|
||||
Addr: fmt.Sprintf("0.0.0.0:%d", cfg.Port),
|
||||
Handler: handler,
|
||||
ReadHeaderTimeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
fmt.Printf("[api] listening on http://0.0.0.0:%d\n", cfg.Port)
|
||||
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
fmt.Printf("[api] failed to start: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
78
cmd/worker/main.go
Normal file
78
cmd/worker/main.go
Normal file
@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"droboticflow/internal/config"
|
||||
"droboticflow/internal/retry"
|
||||
"droboticflow/internal/store"
|
||||
"droboticflow/internal/worker"
|
||||
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func connectMongo(ctx context.Context, mongoURI string, attempts int, delay time.Duration) (*mongo.Client, error) {
|
||||
return retry.Do(ctx, attempts, delay, "mongodb connection", func(ctx context.Context, _ int) (*mongo.Client, error) {
|
||||
client, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoURI))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := client.Ping(ctx, nil); err != nil {
|
||||
_ = client.Disconnect(ctx)
|
||||
return nil, err
|
||||
}
|
||||
return client, nil
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
cfg := config.Load()
|
||||
client, err := connectMongo(ctx, cfg.MongoURI, cfg.MongoConnectRetries, time.Duration(cfg.MongoConnectRetryDelayMS)*time.Millisecond)
|
||||
if err != nil {
|
||||
fmt.Printf("[worker] failed to connect mongo: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer func() {
|
||||
_ = client.Disconnect(context.Background())
|
||||
}()
|
||||
|
||||
dataStore := store.NewMongoStore(client.Database(cfg.MongoDB))
|
||||
mode := "local"
|
||||
if cfg.K8sEnabled {
|
||||
mode = "k8s"
|
||||
}
|
||||
runtime, err := worker.NewRuntime(dataStore, mode, cfg.K8sNamespace, cfg.K8sImagePullSecretName, cfg.OperatorRunnerImage, cfg.LocalOperatorRunnerCmd)
|
||||
if err != nil {
|
||||
fmt.Printf("[worker] failed to initialize runtime: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
go func() {
|
||||
ticker := time.NewTicker(time.Duration(cfg.PollIntervalMS) * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
for range ticker.C {
|
||||
if err := runtime.ProcessAvailable(context.Background()); err != nil {
|
||||
fmt.Printf("[worker] processAvailable failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
server := &http.Server{
|
||||
Addr: fmt.Sprintf("0.0.0.0:%d", cfg.WorkerPort),
|
||||
Handler: worker.NewHTTPHandler(dataStore, runtime),
|
||||
ReadHeaderTimeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
fmt.Printf("[worker] listening on http://0.0.0.0:%d\n", cfg.WorkerPort)
|
||||
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
fmt.Printf("[worker] failed to start: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
261
docs/api.md
Normal file
261
docs/api.md
Normal file
@ -0,0 +1,261 @@
|
||||
# API Reference
|
||||
|
||||
## REST API
|
||||
|
||||
Base path: `/api`
|
||||
|
||||
### Health and Dashboard
|
||||
|
||||
- `GET /api/health`: service health check
|
||||
- `GET /api/ai/status`: returns whether server-side AI generation is enabled and which model is configured
|
||||
- `POST /api/ai/operators/generate`: generate a custom operator definition from natural language
|
||||
- `POST /api/ai/workflows/generate`: generate a workflow graph from natural language and the current operator catalog
|
||||
- `GET /api/dashboard`: aggregate counts for projects, operators, jobs, and audits
|
||||
|
||||
`POST /api/ai/operators/generate`
|
||||
|
||||
```json
|
||||
{
|
||||
"description": "生成一个算子,用于接收视频输入,抽取关键帧并输出图片列表。"
|
||||
}
|
||||
```
|
||||
|
||||
`POST /api/ai/workflows/generate`
|
||||
|
||||
```json
|
||||
{
|
||||
"description": "从对象存储读取视频,抽帧后调用目标检测算子,再把检测结果回传到对象存储。"
|
||||
}
|
||||
```
|
||||
|
||||
### Projects
|
||||
|
||||
- `GET /api/projects`
|
||||
- `POST /api/projects`
|
||||
- `GET /api/projects/:id`
|
||||
- `PUT /api/projects/:id`
|
||||
- `DELETE /api/projects/:id`
|
||||
- `GET /api/projects/:id/workflows`
|
||||
- `POST /api/projects/:id/workflows`
|
||||
|
||||
`POST /api/projects`
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "自动驾驶新项目",
|
||||
"description": "处理多摄像头感知数据"
|
||||
}
|
||||
```
|
||||
|
||||
### Workflows
|
||||
|
||||
- `GET /api/workflows/:id`
|
||||
- `PUT /api/workflows/:id`
|
||||
- `DELETE /api/workflows/:id`
|
||||
- `POST /api/workflows/:id/run`
|
||||
|
||||
`PUT /api/workflows/:id`
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "视觉处理流程",
|
||||
"description": "包含扫描、推理、上传",
|
||||
"type": "batch",
|
||||
"maxConcurrentRuns": 2,
|
||||
"nodeTimeoutSeconds": 120,
|
||||
"runTimeoutSeconds": 300,
|
||||
"maxRetryAttempts": 1,
|
||||
"retryBackoffSeconds": 30,
|
||||
"nodes": [],
|
||||
"edges": []
|
||||
}
|
||||
```
|
||||
|
||||
`POST /api/workflows/:id/run`
|
||||
|
||||
```json
|
||||
{
|
||||
"trigger": "manual"
|
||||
}
|
||||
```
|
||||
|
||||
### Templates
|
||||
|
||||
- `GET /api/templates`
|
||||
- `POST /api/templates`
|
||||
- `GET /api/templates/:id`
|
||||
- `PUT /api/templates/:id`
|
||||
- `DELETE /api/templates/:id`
|
||||
|
||||
Templates now persist the same workflow-level defaults used by project workflows, including `type`, `schedule`, `maxConcurrentRuns`, `nodeTimeoutSeconds`, `runTimeoutSeconds`, `maxRetryAttempts`, and `retryBackoffSeconds`.
|
||||
|
||||
### Operators
|
||||
|
||||
- `GET /api/operators`
|
||||
- `POST /api/operators`
|
||||
- `GET /api/operators/:id`
|
||||
- `PUT /api/operators/:id`
|
||||
- `DELETE /api/operators/:id`
|
||||
|
||||
The reconciled built-in catalog includes the original workflow primitives plus:
|
||||
|
||||
- `目录扫描`
|
||||
- `CSV 转 JSON`
|
||||
- `JSON 转 CSV`
|
||||
- `JSON 字段提取`
|
||||
- `JSON 过滤`
|
||||
- `JSON 映射重命名`
|
||||
- `文本模板渲染`
|
||||
- `条件分流`
|
||||
- `LeRobot 数据集概览`
|
||||
- `ROSBag 数据概览`
|
||||
- `Shell 命令执行`
|
||||
|
||||
`POST /api/operators`
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "自定义分割算子",
|
||||
"type": "custom",
|
||||
"category": "AI",
|
||||
"description": "执行图像分割",
|
||||
"inputs": [{ "id": "in-1", "name": "图像", "type": "image" }],
|
||||
"outputs": [{ "id": "out-1", "name": "结果", "type": "json" }],
|
||||
"image": "ccr.example.com/dataflow/custom-seg:latest"
|
||||
}
|
||||
```
|
||||
|
||||
### Runs and Monitoring
|
||||
|
||||
- `GET /api/runs`
|
||||
- `GET /api/runs/:id`
|
||||
- `GET /api/runs/:id/logs`
|
||||
- `GET /api/runs/:id/resources`
|
||||
- `GET /api/runs/:id/artifacts`
|
||||
|
||||
`GET /api/runs/:id/artifacts`
|
||||
|
||||
Each artifact item can now include:
|
||||
|
||||
- `portId` / `portName`: the originating output port so downstream edges can route multi-output operators correctly
|
||||
- `metadata.mimeType` / `metadata.sha256`: persisted file metadata
|
||||
- `data`: the real JSON payload for JSON artifacts
|
||||
- `data.preview`: a compact preview object for non-JSON artifacts such as text, CSV/TSV, JSONL, image thumbnails, audio/video metadata, PDF hints, SQLite/rosbag summaries, and LeRobot dataset summaries
|
||||
|
||||
### Audit
|
||||
|
||||
- `GET /api/audit-tasks`
|
||||
- `POST /api/audit-tasks/:id/approve`
|
||||
- `POST /api/audit-tasks/:id/reject`
|
||||
|
||||
### Users and Settings
|
||||
|
||||
- `GET /api/users`
|
||||
- `PUT /api/users/:id`
|
||||
- `GET /api/settings`
|
||||
- `PUT /api/settings`
|
||||
|
||||
`GET /api/settings`
|
||||
|
||||
```json
|
||||
{
|
||||
"id": "system",
|
||||
"k8s": {
|
||||
"enabled": true,
|
||||
"endpoint": "https://120.48.161.230:6443",
|
||||
"namespace": "droboticflow",
|
||||
"hasKubeconfig": true
|
||||
},
|
||||
"worker": {
|
||||
"pollIntervalMs": 2000,
|
||||
"maxConcurrentRuns": 2
|
||||
},
|
||||
"ai": {
|
||||
"provider": "dashscope-openai-compatible",
|
||||
"baseURL": "https://coding.dashscope.aliyuncs.com/v1",
|
||||
"model": "glm-5",
|
||||
"hasAPIKey": true
|
||||
},
|
||||
"timezone": "Asia/Shanghai"
|
||||
}
|
||||
```
|
||||
|
||||
`PUT /api/settings`
|
||||
|
||||
```json
|
||||
{
|
||||
"timezone": "Asia/Shanghai",
|
||||
"worker": {
|
||||
"maxConcurrentRuns": 2
|
||||
},
|
||||
"ai": {
|
||||
"provider": "dashscope-openai-compatible",
|
||||
"baseURL": "https://coding.dashscope.aliyuncs.com/v1",
|
||||
"model": "glm-5",
|
||||
"apiKey": "sk-..."
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Workflow create/update payloads also accept workflow-level execution settings:
|
||||
|
||||
- `0`: inherit the global worker default
|
||||
- `1+`: workflow-specific maximum active runs
|
||||
|
||||
Dispatch enforces both the global worker limit and the workflow-specific limit.
|
||||
|
||||
- `runTimeoutSeconds`
|
||||
- `0`: disable workflow-level timeout
|
||||
- `1+`: fail the current run if total execution time exceeds this many seconds
|
||||
- `nodeTimeoutSeconds`
|
||||
- `0`: disable workflow-level per-node timeout
|
||||
- `1+`: fail the current node if a single operator execution exceeds this many seconds
|
||||
- `maxRetryAttempts`
|
||||
- `0`: disable automatic retry
|
||||
- `1+`: automatically enqueue up to N retry runs after a failed attempt
|
||||
- `retryBackoffSeconds`
|
||||
- `0`: enqueue retry runs immediately
|
||||
- `1+`: delay retry dispatch by the configured number of seconds
|
||||
|
||||
Run responses may also include:
|
||||
|
||||
- `attempt`: current attempt number, starting from `1`
|
||||
- `parentRunId`: the failed run that spawned this retry run
|
||||
- `availableAt`: when this queued run becomes eligible for dispatch; retry backoff uses this field
|
||||
|
||||
### Storage
|
||||
|
||||
- `GET /api/storage/buckets?sourcePath=<node SOURCE_PATH>`
|
||||
- `GET /api/storage/objects?sourcePath=<node SOURCE_PATH>&bucket=<bucket>&prefix=<prefix>`
|
||||
|
||||
## Worker API
|
||||
|
||||
Base path: `/worker-api`
|
||||
|
||||
- `GET /worker-api/health`: worker health check
|
||||
- `GET /worker-api/stats`: current queue/running/audit counts
|
||||
- `POST /worker-api/process-once`: process one queued run immediately
|
||||
- `POST /worker-api/runs/:id/retry`: reset a run to `queued` and clear prior execution state
|
||||
|
||||
## Response Shape Notes
|
||||
|
||||
- list endpoints return `{ "items": [...] }`
|
||||
- delete endpoints return `204 No Content`
|
||||
- validation failures return `400`
|
||||
- missing resources return `404`
|
||||
- AI service unavailable returns `503`
|
||||
- upstream AI gateway failures return `502`
|
||||
- unhandled server errors return `500`
|
||||
|
||||
## Implementation Notes
|
||||
|
||||
- `/api/*` is served by the Go API binary in [`cmd/api`](/Users/longtaowu/workspace/droboticflow/cmd/api).
|
||||
- `/worker-api/*` is served by the Go worker binary in [`cmd/worker`](/Users/longtaowu/workspace/droboticflow/cmd/worker).
|
||||
- Workflow execution and storage models are defined in [`internal/domain`](/Users/longtaowu/workspace/droboticflow/internal/domain) and [`internal/store`](/Users/longtaowu/workspace/droboticflow/internal/store).
|
||||
- AI generation is implemented by [`internal/ai`](/Users/longtaowu/workspace/droboticflow/internal/ai) and is called only from the Go API, never directly from the browser.
|
||||
- `GET /api/settings` never returns the saved AI key in plaintext. It only exposes `ai.hasAPIKey` so the browser can show whether a server-side key is already configured.
|
||||
- `timezone` uses IANA timezone identifiers such as `Asia/Shanghai` or `America/Los_Angeles`. The frontend formats timestamps with second precision in that timezone.
|
||||
- Monitoring previews are rendered from the persisted artifact payload itself, so text/table/robotics previews keep working after the Kubernetes Job has exited and its local filesystem is gone.
|
||||
- OSS 相关配置不通过 `/api/settings` 管理。系统设置页只维护 K8s、worker、AI 和默认资源;OSS bucket/prefix 等参数由具体工作流节点维护。
|
||||
- `/api/storage/*` 也不再读取系统级存储根目录。浏览请求必须显式传入当前 OSS 节点超参里的 `SOURCE_PATH`,API 只在这个节点上下文内列 bucket 和对象。
|
||||
- Kubernetes Jobs launched for operator nodes use the Python entrypoint at [`backend/operator-runner/main.py`](/Users/longtaowu/workspace/droboticflow/backend/operator-runner/main.py).
|
||||
141
docs/deployment.md
Normal file
141
docs/deployment.md
Normal file
@ -0,0 +1,141 @@
|
||||
# Deployment Guide
|
||||
|
||||
## Images
|
||||
|
||||
This project deploys three images:
|
||||
|
||||
- `droboticflow-api`
|
||||
- `droboticflow-worker`
|
||||
- `droboticflow-operator-runner`
|
||||
|
||||
Default Baidu registry naming:
|
||||
|
||||
- `ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-api:<tag>`
|
||||
- `ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-worker:<tag>`
|
||||
- `ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-operator-runner:<tag>`
|
||||
|
||||
## 1. Log In to the Registry
|
||||
|
||||
Use the public endpoint unless you are deploying from the same VPC as the registry:
|
||||
|
||||
```bash
|
||||
docker login ccr-29eug8s3-pub.cnc.bj.baidubce.com --username <registry-user>
|
||||
```
|
||||
|
||||
## 2. Build and Push Images
|
||||
|
||||
```bash
|
||||
IMAGE_TAG=20260408 ./scripts/build-and-push-images.sh
|
||||
```
|
||||
|
||||
Image contents:
|
||||
|
||||
- `droboticflow-api`: Vite frontend assets + Go API binary
|
||||
- `droboticflow-worker`: Go worker binary
|
||||
- `droboticflow-operator-runner`: Python runtime + `backend/operator-runner/main.py`
|
||||
|
||||
The script prints the exact image URLs. Reuse those values for deployment:
|
||||
|
||||
```bash
|
||||
API_IMAGE=...
|
||||
WORKER_IMAGE=...
|
||||
OPERATOR_RUNNER_IMAGE=...
|
||||
```
|
||||
|
||||
If the target cluster cannot pull `mongo:7` directly from Docker Hub, mirror it to the same Baidu registry:
|
||||
|
||||
```bash
|
||||
docker buildx build \
|
||||
--platform linux/amd64 \
|
||||
-t ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-mongo:7-amd64 \
|
||||
--push \
|
||||
- <<'EOF'
|
||||
FROM mongo:7
|
||||
EOF
|
||||
```
|
||||
|
||||
## 3. Prepare Kubernetes Access
|
||||
|
||||
Point `kubectl` at the target cluster before deploying. The deployment scripts do not store kubeconfig in the repository.
|
||||
|
||||
Recommended environment variables:
|
||||
|
||||
```bash
|
||||
export K8S_NAMESPACE=droboticflow
|
||||
export API_IMAGE=ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-api:20260408
|
||||
export WORKER_IMAGE=ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-worker:20260408
|
||||
export OPERATOR_RUNNER_IMAGE=ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-operator-runner:20260408
|
||||
export MONGO_IMAGE=ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-mongo:7-amd64
|
||||
export REGISTRY_SERVER=ccr-29eug8s3-pub.cnc.bj.baidubce.com
|
||||
export REGISTRY_USERNAME=<registry-user>
|
||||
export REGISTRY_PASSWORD=<registry-password>
|
||||
export K8S_IMAGE_PULL_SECRET_NAME=droboticflow-registry
|
||||
export AI_API_BASE_URL=https://coding.dashscope.aliyuncs.com/v1
|
||||
export AI_MODEL=glm-5
|
||||
export AI_API_KEY=<dashscope-compatible-server-key>
|
||||
```
|
||||
|
||||
## 4. Deploy
|
||||
|
||||
```bash
|
||||
./scripts/deploy-k8s.sh
|
||||
```
|
||||
|
||||
The deploy script performs these actions:
|
||||
|
||||
1. creates the namespace if it does not exist
|
||||
2. creates or updates the image pull secret
|
||||
3. creates or updates MongoDB credentials, reusing the existing Secret values when present
|
||||
4. creates or updates the `droboticflow-ai-secret` Secret for server-side AI generation
|
||||
5. renders `k8s/droboticflow.template.yaml`
|
||||
6. applies the manifest to the cluster
|
||||
7. waits for MongoDB, API, and worker rollouts
|
||||
|
||||
## 5. Services Created
|
||||
|
||||
- `mongodb`: ClusterIP service for MongoDB
|
||||
- `droboticflow-api`: NodePort service that exposes the frontend and API
|
||||
- `droboticflow-worker`: ClusterIP service for worker health/debugging
|
||||
|
||||
Default API NodePort:
|
||||
|
||||
- `30080`
|
||||
|
||||
If the cluster node IP is reachable, the UI and API are exposed at:
|
||||
|
||||
```text
|
||||
http://<node-ip>:30080
|
||||
```
|
||||
|
||||
## 6. Manifest Variables
|
||||
|
||||
`scripts/render-manifests.sh` accepts:
|
||||
|
||||
- `K8S_NAMESPACE`
|
||||
- `MONGO_DB`
|
||||
- `MONGO_CONNECT_RETRIES`
|
||||
- `MONGO_CONNECT_RETRY_DELAY_MS`
|
||||
- `WORKER_POLL_INTERVAL_MS`
|
||||
- `API_NODE_PORT`
|
||||
- `API_REPLICAS`
|
||||
- `WORKER_REPLICAS`
|
||||
- `K8S_IMAGE_PULL_SECRET_NAME`
|
||||
- `AI_API_BASE_URL`
|
||||
- `AI_MODEL`
|
||||
- `API_IMAGE`
|
||||
- `WORKER_IMAGE`
|
||||
- `OPERATOR_RUNNER_IMAGE`
|
||||
- `MONGO_IMAGE`
|
||||
|
||||
## 7. Notes
|
||||
|
||||
- The provided manifest uses a single-replica MongoDB Deployment with `emptyDir` storage so it can run on clusters that do not expose a default `StorageClass`.
|
||||
- MongoDB should be mirrored to the Baidu registry as an `amd64` image when the cluster cannot pull from Docker Hub.
|
||||
- For production, replace the `emptyDir` volume with a PVC-backed deployment or StatefulSet.
|
||||
- API and worker entrypoints are Go binaries with Mongo startup retry, so they tolerate the database becoming ready after the Deployments start.
|
||||
- The worker runs with Kubernetes RBAC that allows creating and watching Jobs in the namespace.
|
||||
- The operator-runner Job template inherits the private registry pull secret name from `K8S_IMAGE_PULL_SECRET_NAME`.
|
||||
- The operator-runner is not a standalone Deployment. It is launched on demand as a Python Job container.
|
||||
- AI generation runs through the API Deployment. `AI_API_KEY` is mounted through the `droboticflow-ai-secret` Secret and must not be baked into frontend env vars.
|
||||
- If you use DashScope-compatible models, prefer a backend-allowed API key. Do not deploy tool-only Coding Plan keys into the cluster.
|
||||
- Production hardening still depends on the target cluster policy: TLS ingress, network policies, backup strategy, and secret rotation.
|
||||
125
docs/operations.md
Normal file
125
docs/operations.md
Normal file
@ -0,0 +1,125 @@
|
||||
# Operations Guide
|
||||
|
||||
## Service Responsibilities
|
||||
|
||||
### API
|
||||
|
||||
- validates incoming payloads
|
||||
- persists projects, workflows, operators, templates, settings, runs, and audit tasks
|
||||
- serves the built SPA in production
|
||||
- runs as a single Go binary
|
||||
|
||||
### Worker
|
||||
|
||||
- polls MongoDB for queued runs
|
||||
- auto-enqueues streaming workflows based on `schedule.interval`
|
||||
- executes workflow nodes in topological order
|
||||
- pauses runs on human-review nodes
|
||||
- resumes approved or rejected audit branches
|
||||
- creates Kubernetes Jobs when `K8S_ENABLED=true`
|
||||
- runs as a single Go binary
|
||||
|
||||
### Operator Runner
|
||||
|
||||
- receives `RUN_ID`, `WORKFLOW_ID`, `NODE_ID`, and `NODE_LABEL`
|
||||
- executes a generic Python operator contract
|
||||
- emits logs for the worker to associate with the run
|
||||
|
||||
## MongoDB Collections
|
||||
|
||||
- `projects`
|
||||
- `operators`
|
||||
- `workflows`
|
||||
- `templates`
|
||||
- `runs`
|
||||
- `auditTasks`
|
||||
- `users`
|
||||
- `settings`
|
||||
|
||||
## Run Lifecycle
|
||||
|
||||
1. frontend calls `POST /api/workflows/:id/run`
|
||||
2. API inserts a `queued` run document
|
||||
3. worker claims the run and marks it `running`
|
||||
4. worker executes start nodes and records logs, resource samples, and artifacts
|
||||
5. if a node requires review, the run moves to `waiting_for_audit`
|
||||
6. audit action updates the task to `approved` or `rejected`
|
||||
7. worker resumes the graph from the appropriate edge
|
||||
8. run ends in `completed` or `failed`
|
||||
|
||||
## Local Verification
|
||||
|
||||
Run the full verification loop:
|
||||
|
||||
```bash
|
||||
npm run verify
|
||||
```
|
||||
|
||||
Start the stack locally:
|
||||
|
||||
```bash
|
||||
npm run dev:full
|
||||
```
|
||||
|
||||
Smoke test the API and worker:
|
||||
|
||||
```bash
|
||||
./scripts/smoke-test.sh
|
||||
```
|
||||
|
||||
Reconcile persistent system data and remove legacy demo records:
|
||||
|
||||
```bash
|
||||
./scripts/reconcile-system-data.sh http://127.0.0.1:3001
|
||||
```
|
||||
|
||||
## Kubernetes Verification
|
||||
|
||||
Check workloads:
|
||||
|
||||
```bash
|
||||
kubectl -n droboticflow get pods
|
||||
kubectl -n droboticflow get jobs
|
||||
kubectl -n droboticflow get svc
|
||||
```
|
||||
|
||||
Inspect logs:
|
||||
|
||||
```bash
|
||||
kubectl -n droboticflow logs deploy/droboticflow-api
|
||||
kubectl -n droboticflow logs deploy/droboticflow-worker
|
||||
kubectl -n droboticflow logs job/<job-name>
|
||||
```
|
||||
|
||||
## Common Failure Modes
|
||||
|
||||
### API starts but returns Mongo errors
|
||||
|
||||
- confirm `mongodb` pod is healthy
|
||||
- confirm the app secret and database name match the Mongo init script
|
||||
- inspect `kubectl logs deploy/mongodb`
|
||||
|
||||
### Worker cannot launch Jobs
|
||||
|
||||
- confirm the worker pod is running with service account `droboticflow-worker`
|
||||
- confirm Role and RoleBinding exist in the namespace
|
||||
- inspect worker logs for Kubernetes API errors
|
||||
- confirm the operator-runner image pull secret name matches `K8S_IMAGE_PULL_SECRET_NAME`
|
||||
|
||||
### Image pull failure
|
||||
|
||||
- recreate `droboticflow-registry` secret with valid registry credentials
|
||||
- confirm the image tags pushed to the registry match the deployment values
|
||||
- confirm operator-runner Jobs inherit the same pull secret via the rendered Job manifest
|
||||
|
||||
### UI cannot load data
|
||||
|
||||
- confirm the API pod is healthy at `/api/health`
|
||||
- confirm the NodePort is reachable from the target network
|
||||
- inspect browser network traffic for failing endpoints
|
||||
|
||||
### Old demo data still appears after an upgrade
|
||||
|
||||
- run `./scripts/reconcile-system-data.sh http://<node-ip>:30080`
|
||||
- confirm `/api/users`, `/api/projects`, and `/api/templates` no longer return legacy seeded records
|
||||
- confirm `/api/operators` only returns the current built-in operator catalog with the active operator-runner image tag
|
||||
301
docs/plans/2026-04-08-droboticflow-design.md
Normal file
301
docs/plans/2026-04-08-droboticflow-design.md
Normal file
@ -0,0 +1,301 @@
|
||||
# DRoboticFlow Full-Stack Design
|
||||
|
||||
## Goal
|
||||
|
||||
Turn the current frontend prototype into a deployable workflow platform with:
|
||||
|
||||
- a real backend API for all existing pages
|
||||
- a real worker API and execution engine
|
||||
- MongoDB persistence
|
||||
- Kubernetes-based operator execution
|
||||
- Baidu Cloud Container Registry compatible image build and deployment flow
|
||||
|
||||
## Current State
|
||||
|
||||
The repository currently contains:
|
||||
|
||||
- a Vite/React frontend that started as a local-state prototype
|
||||
- a single `server.ts` demo file that mixed API placeholders, in-memory storage, and a non-production worker loop
|
||||
- no persistent database model
|
||||
- no real worker service
|
||||
- no deployment manifests, registry flow, or operations documentation
|
||||
|
||||
## Target Architecture
|
||||
|
||||
### Services
|
||||
|
||||
1. `api`
|
||||
- Go HTTP service for frontend-facing REST APIs
|
||||
- serves the built SPA in production
|
||||
- owns validation, CRUD operations, run creation, audit actions, settings, and dashboard aggregation
|
||||
|
||||
2. `worker`
|
||||
- Go HTTP service for worker health and control APIs
|
||||
- background polling loop for queued runs and scheduled streaming workflows
|
||||
- executes workflow DAGs node-by-node
|
||||
- integrates with Kubernetes Jobs when enabled
|
||||
|
||||
3. `operator-runner`
|
||||
- lightweight Python container entrypoint used by Kubernetes Jobs
|
||||
- executes a generic operator task contract for built-in operators
|
||||
- produces logs and artifacts back into Mongo-backed run records
|
||||
|
||||
4. `mongodb`
|
||||
- persistent document store for projects, operators, workflows, templates, runs, audit tasks, users, settings, and logs
|
||||
|
||||
### Runtime Modes
|
||||
|
||||
- `local`
|
||||
- used for tests and local development
|
||||
- worker executes the real Python operator runner or a real local Docker image without requiring a Kubernetes cluster
|
||||
|
||||
- `k8s`
|
||||
- worker creates Kubernetes Jobs for workflow nodes
|
||||
- jobs run container images pulled from Baidu Cloud Container Registry
|
||||
|
||||
## Data Model
|
||||
|
||||
### `projects`
|
||||
|
||||
- `id`
|
||||
- `name`
|
||||
- `description`
|
||||
- `status`: `active | archived`
|
||||
- `createdAt`
|
||||
- `updatedAt`
|
||||
|
||||
### `operators`
|
||||
|
||||
- `id`
|
||||
- `name`
|
||||
- `type`: `builtin | custom`
|
||||
- `category`
|
||||
- `description`
|
||||
- `inputs`
|
||||
- `outputs`
|
||||
- `image`
|
||||
- `skillDefinition`
|
||||
- `hyperparams`
|
||||
- `resources`: cpu and memory defaults
|
||||
- `createdAt`
|
||||
- `updatedAt`
|
||||
|
||||
### `workflows`
|
||||
|
||||
- `id`
|
||||
- `projectId`
|
||||
- `name`
|
||||
- `description`
|
||||
- `type`: `batch | streaming`
|
||||
- `nodes`
|
||||
- `edges`
|
||||
- `schedule`
|
||||
- `status`: `draft | ready | running | completed | failed`
|
||||
- `lastRunId`
|
||||
- `lastRunAt`
|
||||
- `createdAt`
|
||||
- `updatedAt`
|
||||
|
||||
### `templates`
|
||||
|
||||
- `id`
|
||||
- `name`
|
||||
- `description`
|
||||
- `nodes`
|
||||
- `edges`
|
||||
- `createdAt`
|
||||
- `updatedAt`
|
||||
|
||||
### `runs`
|
||||
|
||||
- `id`
|
||||
- `workflowId`
|
||||
- `projectId`
|
||||
- `workflowName`
|
||||
- `projectName`
|
||||
- `trigger`: `manual | scheduled | retry`
|
||||
- `status`: `queued | running | waiting_for_audit | completed | failed | cancelled`
|
||||
- `startedAt`
|
||||
- `endedAt`
|
||||
- `durationMs`
|
||||
- `currentNodeId`
|
||||
- `nodeExecutions`
|
||||
- `resourceSeries`
|
||||
- `artifacts`
|
||||
- `k8sRefs`
|
||||
- `error`
|
||||
- `createdAt`
|
||||
- `updatedAt`
|
||||
|
||||
### `auditTasks`
|
||||
|
||||
- `id`
|
||||
- `runId`
|
||||
- `workflowId`
|
||||
- `projectId`
|
||||
- `nodeId`
|
||||
- `workflowName`
|
||||
- `projectName`
|
||||
- `operatorName`
|
||||
- `status`: `pending | approved | rejected`
|
||||
- `data`
|
||||
- `createdAt`
|
||||
- `updatedAt`
|
||||
|
||||
### `users`
|
||||
|
||||
- `id`
|
||||
- `name`
|
||||
- `email`
|
||||
- `role`: `admin | auditor | operator`
|
||||
- `createdAt`
|
||||
- `updatedAt`
|
||||
|
||||
### `settings`
|
||||
|
||||
- singleton document keyed by `system`
|
||||
- `k8s.namespace`
|
||||
- `k8s.endpoint`
|
||||
- `k8s.enabled`
|
||||
- `worker.pollIntervalMs`
|
||||
- `worker.maxConcurrentRuns`
|
||||
- `storage.buckets`
|
||||
- `defaults.cpuRequest`
|
||||
- `defaults.cpuLimit`
|
||||
- `defaults.memoryRequest`
|
||||
- `defaults.memoryLimit`
|
||||
- `updatedAt`
|
||||
|
||||
## API Surface
|
||||
|
||||
### Frontend API
|
||||
|
||||
- `GET /api/health`
|
||||
- `GET /api/dashboard`
|
||||
- `GET /api/projects`
|
||||
- `POST /api/projects`
|
||||
- `GET /api/projects/:id`
|
||||
- `PUT /api/projects/:id`
|
||||
- `DELETE /api/projects/:id`
|
||||
- `GET /api/projects/:id/workflows`
|
||||
- `POST /api/projects/:id/workflows`
|
||||
- `GET /api/workflows/:id`
|
||||
- `PUT /api/workflows/:id`
|
||||
- `DELETE /api/workflows/:id`
|
||||
- `POST /api/workflows/:id/run`
|
||||
- `GET /api/templates`
|
||||
- `POST /api/templates`
|
||||
- `GET /api/templates/:id`
|
||||
- `PUT /api/templates/:id`
|
||||
- `DELETE /api/templates/:id`
|
||||
- `GET /api/operators`
|
||||
- `POST /api/operators`
|
||||
- `GET /api/operators/:id`
|
||||
- `PUT /api/operators/:id`
|
||||
- `DELETE /api/operators/:id`
|
||||
- `GET /api/runs`
|
||||
- `GET /api/runs/:id`
|
||||
- `GET /api/runs/:id/logs`
|
||||
- `GET /api/runs/:id/resources`
|
||||
- `GET /api/runs/:id/artifacts`
|
||||
- `GET /api/audit-tasks`
|
||||
- `POST /api/audit-tasks/:id/approve`
|
||||
- `POST /api/audit-tasks/:id/reject`
|
||||
- `GET /api/users`
|
||||
- `PUT /api/users/:id`
|
||||
- `GET /api/settings`
|
||||
- `PUT /api/settings`
|
||||
- `GET /api/storage/buckets`
|
||||
- `GET /api/storage/objects`
|
||||
|
||||
### Worker API
|
||||
|
||||
- `GET /worker-api/health`
|
||||
- `GET /worker-api/stats`
|
||||
- `POST /worker-api/process-once`
|
||||
- `POST /worker-api/runs/:id/retry`
|
||||
|
||||
## Workflow Execution Model
|
||||
|
||||
### Queueing
|
||||
|
||||
1. API validates a workflow.
|
||||
2. API creates a `run` with `status=queued`.
|
||||
3. Worker atomically claims the run.
|
||||
|
||||
### Execution
|
||||
|
||||
1. Worker builds a DAG from workflow `nodes` and `edges`.
|
||||
2. Start nodes with no incoming edges are scheduled first.
|
||||
3. Each node transitions through:
|
||||
- `queued`
|
||||
- `running`
|
||||
- `completed`
|
||||
- `failed`
|
||||
- `waiting_for_audit` when the operator requires manual review
|
||||
4. Downstream nodes run only after all dependencies complete.
|
||||
|
||||
### Audit Gate
|
||||
|
||||
If a node is a human review operator:
|
||||
|
||||
1. Worker creates an `auditTask`.
|
||||
2. Run status becomes `waiting_for_audit`.
|
||||
3. Approve/reject API updates the task.
|
||||
4. Worker resumes the run on the corresponding branch.
|
||||
|
||||
### Kubernetes Launch
|
||||
|
||||
When Kubernetes execution is enabled:
|
||||
|
||||
1. Worker resolves the node image.
|
||||
2. Worker creates a namespaced Job with run and node metadata as env vars.
|
||||
3. Worker watches Job completion and collects pod/job references.
|
||||
4. Worker stores status, logs, and basic resource samples in Mongo.
|
||||
|
||||
When disabled, the same state machine runs through the local execution path for deterministic tests and development.
|
||||
|
||||
## Frontend Integration Strategy
|
||||
|
||||
The existing UI is kept, but local prototype state is replaced with:
|
||||
|
||||
- shared API client utilities
|
||||
- fetch-based page loading and mutation handlers
|
||||
- run, monitoring, audit, settings, and storage views backed by real endpoints
|
||||
|
||||
The visual design stays intact; only data flow changes.
|
||||
|
||||
## Deployment Topology
|
||||
|
||||
### Containers
|
||||
|
||||
- `droboticflow-api`
|
||||
- `droboticflow-worker`
|
||||
- `droboticflow-operator-runner`
|
||||
|
||||
### Registry
|
||||
|
||||
Baidu Cloud Container Registry namespace:
|
||||
|
||||
- `ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow`
|
||||
|
||||
### Kubernetes Resources
|
||||
|
||||
- namespace
|
||||
- MongoDB secret, PVC, service, deployment/statefulset
|
||||
- API configmap, secret, deployment, service
|
||||
- Worker configmap, secret, deployment, service
|
||||
- image pull secret for Baidu registry
|
||||
|
||||
## Acceptance Criteria
|
||||
|
||||
The project is considered aligned when:
|
||||
|
||||
- frontend pages read and write real backend data
|
||||
- workflow runs are persisted in MongoDB
|
||||
- worker processes queued runs and updates monitoring data
|
||||
- audit tasks can pause and resume runs
|
||||
- settings are persisted and exposed via API
|
||||
- Docker images can be built and tagged for Baidu registry
|
||||
- Kubernetes manifests can deploy MongoDB, API, and worker
|
||||
- repo docs explain local development, deployment, and operations
|
||||
122
docs/plans/2026-04-08-droboticflow-implementation.md
Normal file
122
docs/plans/2026-04-08-droboticflow-implementation.md
Normal file
@ -0,0 +1,122 @@
|
||||
# DRoboticFlow Full-Stack Implementation Plan
|
||||
|
||||
**Goal:** Replace the prototype backend with a Go-based MongoDB-backed API and worker stack, wire the frontend to it, and add Kubernetes plus Baidu registry deployment assets.
|
||||
|
||||
**Architecture:** Keep the existing React frontend, add shared Go domain modules plus separate API and worker entrypoints, and use a Kubernetes launcher abstraction with a real local runner for development plus Kubernetes Jobs in cluster mode. Use a Python operator-runner container for Kubernetes Jobs. Update docs and deployment assets in the same change set so runtime assumptions stay aligned.
|
||||
|
||||
**Tech Stack:** React, Vite, Go, MongoDB, Go test, client-go, Python, Docker, Kubernetes YAML
|
||||
|
||||
---
|
||||
|
||||
### Task 1: Scaffold backend structure and shared models
|
||||
|
||||
**Files:**
|
||||
- Create: `cmd/api/main.go`
|
||||
- Create: `cmd/worker/main.go`
|
||||
- Create: `backend/operator-runner/main.py`
|
||||
- Create: `internal/domain/*.go`
|
||||
- Create: `internal/config/*.go`
|
||||
- Modify: `package.json`
|
||||
- Modify: `tsconfig.json`
|
||||
- Modify: `README.md`
|
||||
|
||||
**Steps:**
|
||||
1. Create Go entrypoints for the API and worker services.
|
||||
2. Add shared domain types, config loading, and identifier helpers.
|
||||
3. Add the Python operator-runner entrypoint used inside Kubernetes Jobs.
|
||||
4. Verify the binaries compile and the runtime contract is stable.
|
||||
|
||||
### Task 2: Add Mongo repositories and default settings
|
||||
|
||||
**Files:**
|
||||
- Create: `internal/store/*.go`
|
||||
- Test: `internal/store/*_test.go`
|
||||
|
||||
**Steps:**
|
||||
1. Add a store interface plus in-memory and Mongo implementations.
|
||||
2. Port CRUD behavior, dashboard aggregation, default settings, and queue-claim logic.
|
||||
3. Keep runtime bootstrap explicit so projects, operators, templates, users, runs, and audits are created through real APIs.
|
||||
4. Re-run store tests until green.
|
||||
|
||||
### Task 3: Implement API routes
|
||||
|
||||
**Files:**
|
||||
- Create: `internal/api/*.go`
|
||||
- Test: `internal/api/*_test.go`
|
||||
|
||||
**Steps:**
|
||||
1. Write failing tests for dashboard, CRUD, run creation, audit actions, and storage endpoints.
|
||||
2. Implement REST handlers with validation and consistent JSON responses.
|
||||
3. Register `/api/*` routes and SPA fallback handling.
|
||||
4. Re-run API tests until green.
|
||||
|
||||
### Task 4: Implement worker run lifecycle
|
||||
|
||||
**Files:**
|
||||
- Create: `internal/worker/*.go`
|
||||
- Create: `internal/retry/*.go`
|
||||
- Test: `internal/worker/*_test.go`
|
||||
- Test: `internal/retry/*_test.go`
|
||||
|
||||
**Steps:**
|
||||
1. Write failing tests for run claiming, DAG ordering, audit pause/resume, and streaming scheduling.
|
||||
2. Implement queue claiming, topological execution, node logging, artifact/resource updates, and run status transitions.
|
||||
3. Add worker control endpoints and polling loop.
|
||||
4. Re-run worker and retry tests until green.
|
||||
|
||||
### Task 5: Implement Kubernetes launcher and operator runner
|
||||
|
||||
**Files:**
|
||||
- Create: `internal/k8s/*.go`
|
||||
- Create: `backend/operator-runner/main.py`
|
||||
- Test: `internal/k8s/*_test.go`
|
||||
|
||||
**Steps:**
|
||||
1. Write failing tests for job spec generation and local execution fallback.
|
||||
2. Implement launcher abstraction for local and Kubernetes modes using `client-go`.
|
||||
3. Implement the Python operator-runner contract for generic built-in operators.
|
||||
4. Re-run launcher tests until green.
|
||||
|
||||
### Task 6: Refactor frontend to real API clients
|
||||
|
||||
**Files:**
|
||||
- Create: `src/services/api/client.ts`
|
||||
- Create: `src/services/api/*.ts`
|
||||
- Modify: `src/pages/*.tsx`
|
||||
- Modify: `src/context/UserContext.tsx`
|
||||
|
||||
**Steps:**
|
||||
1. Replace local prototype state with fetch-backed hooks or page effects.
|
||||
2. Wire project, workflow, operator, template, monitoring, audit, users, settings, and dashboard pages to the backend.
|
||||
3. Re-run the frontend type-check and build.
|
||||
4. Verify the existing UI still loads against the real API.
|
||||
|
||||
### Task 7: Add Docker and Kubernetes deployment assets
|
||||
|
||||
**Files:**
|
||||
- Create: `Dockerfile.api`
|
||||
- Create: `Dockerfile.worker`
|
||||
- Create: `Dockerfile.operator-runner`
|
||||
- Create: `k8s/*.yaml`
|
||||
- Create: `scripts/*.sh`
|
||||
|
||||
**Steps:**
|
||||
1. Add image build definitions, k8s manifests, and deploy scripts for MongoDB, API, worker, registry secret, and config.
|
||||
2. Ensure manifest env vars match the Go/Python runtime settings.
|
||||
3. Preserve cluster-specific fixes for Mongo startup retry, secret reuse, image tag propagation, `jobs/status` RBAC, and operator-runner `imagePullSecrets`.
|
||||
4. Re-run manifest rendering and local container build commands.
|
||||
|
||||
### Task 8: Update documentation and verification assets
|
||||
|
||||
**Files:**
|
||||
- Modify: `README.md`
|
||||
- Create: `docs/api.md`
|
||||
- Create: `docs/deployment.md`
|
||||
- Create: `docs/operations.md`
|
||||
- Modify: `.env.example`
|
||||
|
||||
**Steps:**
|
||||
1. Document local development, environment variables, worker behavior, deployment, and acceptance checks.
|
||||
2. Add Baidu registry and Kubernetes setup instructions without committing secrets.
|
||||
3. Run doc/code sync review and fix any drift.
|
||||
4. Run final verification loop: build, type check, tests, security grep, diff review.
|
||||
61
go.mod
Normal file
61
go.mod
Normal file
@ -0,0 +1,61 @@
|
||||
module droboticflow
|
||||
|
||||
go 1.24.0
|
||||
|
||||
require (
|
||||
github.com/go-chi/chi/v5 v5.2.3
|
||||
go.mongodb.org/mongo-driver v1.17.6
|
||||
k8s.io/api v0.34.1
|
||||
k8s.io/apimachinery v0.34.1
|
||||
k8s.io/client-go v0.34.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/emicklei/go-restful/v3 v3.12.2 // indirect
|
||||
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
|
||||
github.com/go-logr/logr v1.4.2 // indirect
|
||||
github.com/go-openapi/jsonpointer v0.21.0 // indirect
|
||||
github.com/go-openapi/jsonreference v0.20.2 // indirect
|
||||
github.com/go-openapi/swag v0.23.0 // indirect
|
||||
github.com/gogo/protobuf v1.3.2 // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/google/gnostic-models v0.7.0 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/josharian/intern v1.0.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/compress v1.16.7 // indirect
|
||||
github.com/mailru/easyjson v0.7.7 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
|
||||
github.com/montanaflynn/stats v0.7.1 // indirect
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/spf13/pflag v1.0.6 // indirect
|
||||
github.com/x448/float16 v0.8.4 // indirect
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
||||
github.com/xdg-go/scram v1.1.2 // indirect
|
||||
github.com/xdg-go/stringprep v1.0.4 // indirect
|
||||
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
|
||||
go.yaml.in/yaml/v2 v2.4.2 // indirect
|
||||
go.yaml.in/yaml/v3 v3.0.4 // indirect
|
||||
golang.org/x/crypto v0.36.0 // indirect
|
||||
golang.org/x/net v0.38.0 // indirect
|
||||
golang.org/x/oauth2 v0.27.0 // indirect
|
||||
golang.org/x/sync v0.12.0 // indirect
|
||||
golang.org/x/sys v0.31.0 // indirect
|
||||
golang.org/x/term v0.30.0 // indirect
|
||||
golang.org/x/text v0.23.0 // indirect
|
||||
golang.org/x/time v0.9.0 // indirect
|
||||
google.golang.org/protobuf v1.36.5 // indirect
|
||||
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
|
||||
gopkg.in/inf.v0 v0.9.1 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
k8s.io/klog/v2 v2.130.1 // indirect
|
||||
k8s.io/kube-openapi v0.0.0-20250710124328-f3f2b991d03b // indirect
|
||||
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397 // indirect
|
||||
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect
|
||||
sigs.k8s.io/randfill v1.0.0 // indirect
|
||||
sigs.k8s.io/structured-merge-diff/v6 v6.3.0 // indirect
|
||||
sigs.k8s.io/yaml v1.6.0 // indirect
|
||||
)
|
||||
193
go.sum
Normal file
193
go.sum
Normal file
@ -0,0 +1,193 @@
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/emicklei/go-restful/v3 v3.12.2 h1:DhwDP0vY3k8ZzE0RunuJy8GhNpPL6zqLkDf9B/a0/xU=
|
||||
github.com/emicklei/go-restful/v3 v3.12.2/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
|
||||
github.com/fxamacker/cbor/v2 v2.9.0 h1:NpKPmjDBgUfBms6tr6JZkTHtfFGcMKsw3eGcmD/sapM=
|
||||
github.com/fxamacker/cbor/v2 v2.9.0/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ=
|
||||
github.com/go-chi/chi/v5 v5.2.3 h1:WQIt9uxdsAbgIYgid+BpYc+liqQZGMHRaUwp0JUcvdE=
|
||||
github.com/go-chi/chi/v5 v5.2.3/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
|
||||
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
|
||||
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||
github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs=
|
||||
github.com/go-openapi/jsonpointer v0.21.0 h1:YgdVicSA9vH5RiHs9TZW5oyafXZFc6+2Vc1rr/O9oNQ=
|
||||
github.com/go-openapi/jsonpointer v0.21.0/go.mod h1:IUyH9l/+uyhIYQ/PXVA41Rexl+kOkAPDdXEYns6fzUY=
|
||||
github.com/go-openapi/jsonreference v0.20.2 h1:3sVjiK66+uXK/6oQ8xgcRKcFgQ5KXa2KvnJRumpMGbE=
|
||||
github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En5Ap4rVB5KVcIDZG2k=
|
||||
github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
|
||||
github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE=
|
||||
github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ=
|
||||
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
|
||||
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
|
||||
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
|
||||
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
|
||||
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/gnostic-models v0.7.0 h1:qwTtogB15McXDaNqTZdzPJRHvaVJlAl+HVQnLmJEJxo=
|
||||
github.com/google/gnostic-models v0.7.0/go.mod h1:whL5G0m6dmc5cPxKc5bdKdEN3UjI7OUGxBlw57miDrQ=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/pprof v0.0.0-20241029153458-d1b30febd7db h1:097atOisP2aRj7vFgYQBbFN4U4JNXUNYpxael3UzMyo=
|
||||
github.com/google/pprof v0.0.0-20241029153458-d1b30febd7db/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
|
||||
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
|
||||
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
|
||||
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee h1:W5t00kpgFdJifH4BDsTlE89Zl93FEloxaWZfGcifgq8=
|
||||
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
|
||||
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/onsi/ginkgo/v2 v2.21.0 h1:7rg/4f3rB88pb5obDgNZrNHrQ4e6WpjonchcpuBRnZM=
|
||||
github.com/onsi/ginkgo/v2 v2.21.0/go.mod h1:7Du3c42kxCUegi0IImZ1wUQzMBVecgIHjR1C+NkhLQo=
|
||||
github.com/onsi/gomega v1.35.1 h1:Cwbd75ZBPxFSuZ6T+rN/WCb/gOc6YgFBXLlZLhC7Ds4=
|
||||
github.com/onsi/gomega v1.35.1/go.mod h1:PvZbdDc8J6XJEpDK4HCuRBm8a6Fzp9/DmhC9C7yFlog=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
|
||||
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
||||
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
|
||||
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
|
||||
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
|
||||
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
|
||||
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
|
||||
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
|
||||
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
|
||||
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM=
|
||||
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
go.mongodb.org/mongo-driver v1.17.6 h1:87JUG1wZfWsr6rIz3ZmpH90rL5tea7O3IHuSwHUpsss=
|
||||
go.mongodb.org/mongo-driver v1.17.6/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ=
|
||||
go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
|
||||
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
|
||||
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
|
||||
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
|
||||
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
|
||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
|
||||
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
|
||||
golang.org/x/oauth2 v0.27.0 h1:da9Vo7/tDv5RH/7nZDz1eMGS/q1Vv1N/7FCrBhI9I3M=
|
||||
golang.org/x/oauth2 v0.27.0/go.mod h1:onh5ek6nERTohokkhCD/y2cV4Do3fxFHFuAejCkRWT8=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
|
||||
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
|
||||
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.30.0 h1:PQ39fJZ+mfadBm0y5WlL4vlM7Sx1Hgf13sMIY2+QS9Y=
|
||||
golang.org/x/term v0.30.0/go.mod h1:NYYFdzHoI5wRh/h5tDMdMqCqPJZEuNqVR5xJLd/n67g=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
|
||||
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
|
||||
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
|
||||
golang.org/x/time v0.9.0 h1:EsRrnYcQiGH+5FfbgvV4AP7qEZstoyrHB0DzarOQ4ZY=
|
||||
golang.org/x/time v0.9.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
||||
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ=
|
||||
golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
|
||||
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/evanphx/json-patch.v4 v4.12.0 h1:n6jtcsulIzXPJaxegRbvFNNrZDjbij7ny3gmSPG+6V4=
|
||||
gopkg.in/evanphx/json-patch.v4 v4.12.0/go.mod h1:p8EYWUEYMpynmqDbY58zCKCFZw8pRWMG4EsWvDvM72M=
|
||||
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
|
||||
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
k8s.io/api v0.34.1 h1:jC+153630BMdlFukegoEL8E/yT7aLyQkIVuwhmwDgJM=
|
||||
k8s.io/api v0.34.1/go.mod h1:SB80FxFtXn5/gwzCoN6QCtPD7Vbu5w2n1S0J5gFfTYk=
|
||||
k8s.io/apimachinery v0.34.1 h1:dTlxFls/eikpJxmAC7MVE8oOeP1zryV7iRyIjB0gky4=
|
||||
k8s.io/apimachinery v0.34.1/go.mod h1:/GwIlEcWuTX9zKIg2mbw0LRFIsXwrfoVxn+ef0X13lw=
|
||||
k8s.io/client-go v0.34.1 h1:ZUPJKgXsnKwVwmKKdPfw4tB58+7/Ik3CrjOEhsiZ7mY=
|
||||
k8s.io/client-go v0.34.1/go.mod h1:kA8v0FP+tk6sZA0yKLRG67LWjqufAoSHA2xVGKw9Of8=
|
||||
k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk=
|
||||
k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
|
||||
k8s.io/kube-openapi v0.0.0-20250710124328-f3f2b991d03b h1:MloQ9/bdJyIu9lb1PzujOPolHyvO06MXG5TUIj2mNAA=
|
||||
k8s.io/kube-openapi v0.0.0-20250710124328-f3f2b991d03b/go.mod h1:UZ2yyWbFTpuhSbFhv24aGNOdoRdJZgsIObGBUaYVsts=
|
||||
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397 h1:hwvWFiBzdWw1FhfY1FooPn3kzWuJ8tmbZBHi4zVsl1Y=
|
||||
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
|
||||
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 h1:gBQPwqORJ8d8/YNZWEjoZs7npUVDpVXUUOFfW6CgAqE=
|
||||
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8/go.mod h1:mdzfpAEoE6DHQEN0uh9ZbOCuHbLK5wOm7dK4ctXE9Tg=
|
||||
sigs.k8s.io/randfill v1.0.0 h1:JfjMILfT8A6RbawdsK2JXGBR5AQVfd+9TbzrlneTyrU=
|
||||
sigs.k8s.io/randfill v1.0.0/go.mod h1:XeLlZ/jmk4i1HRopwe7/aU3H5n1zNUcX6TM94b3QxOY=
|
||||
sigs.k8s.io/structured-merge-diff/v6 v6.3.0 h1:jTijUJbW353oVOd9oTlifJqOGEkUw2jB/fXCbTiQEco=
|
||||
sigs.k8s.io/structured-merge-diff/v6 v6.3.0/go.mod h1:M3W8sfWvn2HhQDIbGWj3S099YozAsymCo/wrT5ohRUE=
|
||||
sigs.k8s.io/yaml v1.6.0 h1:G8fkbMSAFqgEFgh4b1wmtzDnioxFCUgTZhlbj5P9QYs=
|
||||
sigs.k8s.io/yaml v1.6.0/go.mod h1:796bPqUfzR/0jLAl6XjHl3Ck7MiyVv8dbTdyT3/pMf4=
|
||||
@ -3,11 +3,10 @@
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>My Google AI Studio App</title>
|
||||
<title>DroboticFlow</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.tsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
712
internal/ai/client.go
Normal file
712
internal/ai/client.go
Normal file
@ -0,0 +1,712 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"droboticflow/internal/domain"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultProvider = "dashscope-openai-compatible"
|
||||
defaultRequestTimeout = 240 * time.Second
|
||||
defaultMaxAttempts = 2
|
||||
)
|
||||
|
||||
var ErrUnavailable = errors.New("AI service is not configured")
|
||||
|
||||
type Status struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Provider string `json:"provider"`
|
||||
BaseURL string `json:"baseURL,omitempty"`
|
||||
Model string `json:"model,omitempty"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
}
|
||||
|
||||
type GeneratedWorkflow struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Nodes []domain.WorkflowNode `json:"nodes"`
|
||||
Edges []domain.WorkflowEdge `json:"edges"`
|
||||
}
|
||||
|
||||
type GeneratedOperatorPort struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Transmission string `json:"transmission"`
|
||||
Description string `json:"description,omitempty"`
|
||||
}
|
||||
|
||||
type GeneratedOperator struct {
|
||||
Name string `json:"name"`
|
||||
Category string `json:"category"`
|
||||
Description string `json:"description"`
|
||||
Inputs []GeneratedOperatorPort `json:"inputs"`
|
||||
Outputs []GeneratedOperatorPort `json:"outputs"`
|
||||
Hyperparams []domain.HyperParam `json:"hyperparams"`
|
||||
SkillDefinition string `json:"skillDefinition"`
|
||||
}
|
||||
|
||||
type Service interface {
|
||||
Status() Status
|
||||
GenerateWorkflow(context.Context, string, []domain.OperatorRecord) (*GeneratedWorkflow, error)
|
||||
GenerateOperator(context.Context, string) (*GeneratedOperator, error)
|
||||
}
|
||||
|
||||
type disabledService struct {
|
||||
status Status
|
||||
}
|
||||
|
||||
func NewDisabledService(baseURL string, model string, reason string) Service {
|
||||
if strings.TrimSpace(reason) == "" {
|
||||
reason = "AI 服务未配置"
|
||||
}
|
||||
return &disabledService{
|
||||
status: Status{
|
||||
Enabled: false,
|
||||
Provider: defaultProvider,
|
||||
BaseURL: strings.TrimRight(strings.TrimSpace(baseURL), "/"),
|
||||
Model: strings.TrimSpace(model),
|
||||
Reason: reason,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (service *disabledService) Status() Status {
|
||||
return service.status
|
||||
}
|
||||
|
||||
func (service *disabledService) GenerateWorkflow(context.Context, string, []domain.OperatorRecord) (*GeneratedWorkflow, error) {
|
||||
return nil, ErrUnavailable
|
||||
}
|
||||
|
||||
func (service *disabledService) GenerateOperator(context.Context, string) (*GeneratedOperator, error) {
|
||||
return nil, ErrUnavailable
|
||||
}
|
||||
|
||||
type client struct {
|
||||
httpClient *http.Client
|
||||
baseURL string
|
||||
apiKey string
|
||||
model string
|
||||
requestTimeout time.Duration
|
||||
maxAttempts int
|
||||
}
|
||||
|
||||
func NewService(baseURL string, apiKey string, model string) Service {
|
||||
normalizedBaseURL := strings.TrimRight(strings.TrimSpace(baseURL), "/")
|
||||
normalizedKey := strings.TrimSpace(apiKey)
|
||||
normalizedModel := strings.TrimSpace(model)
|
||||
if normalizedModel == "" {
|
||||
normalizedModel = "glm-5"
|
||||
}
|
||||
if normalizedBaseURL == "" {
|
||||
normalizedBaseURL = "https://coding.dashscope.aliyuncs.com/v1"
|
||||
}
|
||||
if normalizedKey == "" {
|
||||
return NewDisabledService(normalizedBaseURL, normalizedModel, "服务端未配置 AI_API_KEY")
|
||||
}
|
||||
return &client{
|
||||
httpClient: &http.Client{},
|
||||
baseURL: normalizedBaseURL,
|
||||
apiKey: normalizedKey,
|
||||
model: normalizedModel,
|
||||
requestTimeout: defaultRequestTimeout,
|
||||
maxAttempts: defaultMaxAttempts,
|
||||
}
|
||||
}
|
||||
|
||||
func (client *client) Status() Status {
|
||||
return Status{
|
||||
Enabled: true,
|
||||
Provider: defaultProvider,
|
||||
BaseURL: client.baseURL,
|
||||
Model: client.model,
|
||||
}
|
||||
}
|
||||
|
||||
type chatMessage struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type chatCompletionRequest struct {
|
||||
Model string `json:"model"`
|
||||
Messages []chatMessage `json:"messages"`
|
||||
Temperature float64 `json:"temperature,omitempty"`
|
||||
}
|
||||
|
||||
type chatCompletionResponse struct {
|
||||
Choices []struct {
|
||||
Message struct {
|
||||
Content any `json:"content"`
|
||||
} `json:"message"`
|
||||
} `json:"choices"`
|
||||
Error *struct {
|
||||
Message string `json:"message"`
|
||||
} `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type workflowPlan struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Nodes []workflowPlanNode `json:"nodes"`
|
||||
Edges []workflowPlanEdge `json:"edges"`
|
||||
}
|
||||
|
||||
type workflowPlanNode struct {
|
||||
ID string `json:"id"`
|
||||
Label string `json:"label,omitempty"`
|
||||
OperatorID string `json:"operatorId"`
|
||||
Position domain.Position `json:"position"`
|
||||
HyperparamOverrides []domain.HyperParam `json:"hyperparamOverrides,omitempty"`
|
||||
Bucket string `json:"bucket,omitempty"`
|
||||
Prefix string `json:"prefix,omitempty"`
|
||||
Pattern string `json:"pattern,omitempty"`
|
||||
CPU string `json:"cpu,omitempty"`
|
||||
Memory string `json:"memory,omitempty"`
|
||||
}
|
||||
|
||||
type workflowPlanEdge struct {
|
||||
ID string `json:"id"`
|
||||
Source string `json:"source"`
|
||||
Target string `json:"target"`
|
||||
SourcePort string `json:"sourcePort,omitempty"`
|
||||
TargetPort string `json:"targetPort,omitempty"`
|
||||
}
|
||||
|
||||
func (client *client) GenerateWorkflow(ctx context.Context, description string, operators []domain.OperatorRecord) (*GeneratedWorkflow, error) {
|
||||
if strings.TrimSpace(description) == "" {
|
||||
return nil, fmt.Errorf("description is required")
|
||||
}
|
||||
if len(operators) == 0 {
|
||||
return nil, fmt.Errorf("no operators available")
|
||||
}
|
||||
|
||||
type operatorContext struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Category string `json:"category"`
|
||||
Description string `json:"description"`
|
||||
Inputs []domain.PortDefinition `json:"inputs"`
|
||||
Outputs []domain.PortDefinition `json:"outputs"`
|
||||
Image string `json:"image,omitempty"`
|
||||
SkillDefinition string `json:"skillDefinition,omitempty"`
|
||||
Hyperparams []domain.HyperParam `json:"hyperparams,omitempty"`
|
||||
}
|
||||
|
||||
available := make([]operatorContext, 0, len(operators))
|
||||
for _, operator := range operators {
|
||||
available = append(available, operatorContext{
|
||||
ID: operator.ID,
|
||||
Name: operator.Name,
|
||||
Category: operator.Category,
|
||||
Description: operator.Description,
|
||||
Inputs: operator.Inputs,
|
||||
Outputs: operator.Outputs,
|
||||
Image: operator.Image,
|
||||
SkillDefinition: operator.SkillDefinition,
|
||||
Hyperparams: operator.Hyperparams,
|
||||
})
|
||||
}
|
||||
|
||||
contextJSON, err := json.MarshalIndent(available, "", " ")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
systemPrompt := strings.TrimSpace(`
|
||||
You are an expert workflow architect for DroboticFlow.
|
||||
Return a single JSON object only. Do not wrap it in markdown.
|
||||
|
||||
Your task is to turn the user's requirement into a workflow PLAN using only the provided operators.
|
||||
|
||||
Rules:
|
||||
1. Use only operatorId values from the provided operator catalog.
|
||||
2. Nodes must be valid for DroboticFlow execution and connected in a runnable order.
|
||||
3. For each edge, sourcePort and targetPort must match the real port names from the chosen operators.
|
||||
4. Prefer batch workflows unless the user explicitly asks for streaming behavior.
|
||||
5. Keep names short and production-oriented.
|
||||
6. If a node needs operator-specific configuration, put it in hyperparamOverrides using real hyperparameter keys.
|
||||
7. Output schema:
|
||||
{
|
||||
"name": "workflow short name",
|
||||
"description": "short summary",
|
||||
"nodes": [
|
||||
{
|
||||
"id": "node-source",
|
||||
"label": "optional custom label",
|
||||
"operatorId": "operator-id",
|
||||
"position": {"x": 120, "y": 80},
|
||||
"hyperparamOverrides": [{"key":"ENV_KEY","value":"ENV_VALUE","description":"optional"}],
|
||||
"bucket": "optional storage bucket",
|
||||
"prefix": "optional storage prefix",
|
||||
"pattern": "optional filename pattern",
|
||||
"cpu": "optional cpu limit",
|
||||
"memory": "optional memory limit"
|
||||
}
|
||||
],
|
||||
"edges": [
|
||||
{
|
||||
"id": "edge-1",
|
||||
"source": "node-source",
|
||||
"target": "node-next",
|
||||
"sourcePort": "output port name",
|
||||
"targetPort": "input port name"
|
||||
}
|
||||
]
|
||||
}`)
|
||||
|
||||
userPrompt := fmt.Sprintf("User requirement:\n%s\n\nAvailable operators:\n%s", strings.TrimSpace(description), string(contextJSON))
|
||||
|
||||
var plan workflowPlan
|
||||
if err := client.generateJSON(ctx, systemPrompt, userPrompt, &plan); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(plan.Nodes) == 0 {
|
||||
return nil, fmt.Errorf("AI did not return any workflow nodes")
|
||||
}
|
||||
|
||||
operatorByID := make(map[string]domain.OperatorRecord, len(operators))
|
||||
nodes := make([]domain.WorkflowNode, 0, len(plan.Nodes))
|
||||
for _, operator := range operators {
|
||||
operatorByID[operator.ID] = operator
|
||||
}
|
||||
|
||||
nodeByID := make(map[string]domain.WorkflowNode, len(plan.Nodes))
|
||||
for _, item := range plan.Nodes {
|
||||
operator, ok := operatorByID[item.OperatorID]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("AI selected unknown operator %q", item.OperatorID)
|
||||
}
|
||||
if strings.TrimSpace(item.ID) == "" {
|
||||
return nil, fmt.Errorf("AI returned a workflow node without id")
|
||||
}
|
||||
node := domain.WorkflowNode{
|
||||
ID: strings.TrimSpace(item.ID),
|
||||
Type: "operator",
|
||||
Data: domain.WorkflowNodeData{
|
||||
Label: chooseValue(item.Label, operator.Name),
|
||||
Category: operator.Category,
|
||||
Description: operator.Description,
|
||||
Inputs: clonePorts(operator.Inputs),
|
||||
Outputs: clonePorts(operator.Outputs),
|
||||
Image: operator.Image,
|
||||
SkillDefinition: operator.SkillDefinition,
|
||||
Hyperparams: mergeHyperparams(operator.Hyperparams, item.HyperparamOverrides),
|
||||
Bucket: strings.TrimSpace(item.Bucket),
|
||||
Prefix: strings.TrimSpace(item.Prefix),
|
||||
Pattern: strings.TrimSpace(item.Pattern),
|
||||
CPU: strings.TrimSpace(item.CPU),
|
||||
Memory: strings.TrimSpace(item.Memory),
|
||||
},
|
||||
Position: item.Position,
|
||||
}
|
||||
nodes = append(nodes, node)
|
||||
nodeByID[node.ID] = node
|
||||
}
|
||||
|
||||
edges := make([]domain.WorkflowEdge, 0, len(plan.Edges))
|
||||
for _, item := range plan.Edges {
|
||||
if strings.TrimSpace(item.Source) == "" || strings.TrimSpace(item.Target) == "" {
|
||||
return nil, fmt.Errorf("AI returned an edge without source/target")
|
||||
}
|
||||
sourceNode, sourceOK := nodeByID[item.Source]
|
||||
targetNode, targetOK := nodeByID[item.Target]
|
||||
if !sourceOK || !targetOK {
|
||||
return nil, fmt.Errorf("AI returned an edge with unknown nodes: %s -> %s", item.Source, item.Target)
|
||||
}
|
||||
sourceHandle, err := resolvePortHandle(sourceNode.Data.Outputs, item.SourcePort)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("resolve source port for edge %s: %w", chooseValue(item.ID, item.Source+"-"+item.Target), err)
|
||||
}
|
||||
targetHandle, err := resolvePortHandle(targetNode.Data.Inputs, item.TargetPort)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("resolve target port for edge %s: %w", chooseValue(item.ID, item.Source+"-"+item.Target), err)
|
||||
}
|
||||
edges = append(edges, domain.WorkflowEdge{
|
||||
ID: chooseValue(strings.TrimSpace(item.ID), "edge-"+item.Source+"-"+item.Target),
|
||||
Source: item.Source,
|
||||
Target: item.Target,
|
||||
SourceHandle: sourceHandle,
|
||||
TargetHandle: targetHandle,
|
||||
})
|
||||
}
|
||||
|
||||
return &GeneratedWorkflow{
|
||||
Name: chooseValue(strings.TrimSpace(plan.Name), "AI 生成工作流"),
|
||||
Description: chooseValue(strings.TrimSpace(plan.Description), strings.TrimSpace(description)),
|
||||
Nodes: nodes,
|
||||
Edges: edges,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (client *client) GenerateOperator(ctx context.Context, description string) (*GeneratedOperator, error) {
|
||||
if strings.TrimSpace(description) == "" {
|
||||
return nil, fmt.Errorf("description is required")
|
||||
}
|
||||
|
||||
systemPrompt := strings.TrimSpace(`
|
||||
You are an expert DroboticFlow operator designer.
|
||||
Return one JSON object only. Do not wrap it in markdown.
|
||||
|
||||
Allowed categories: IO, CV, AI, MATH, HUMAN, FLOW
|
||||
Allowed port types: json, image, video, tensor, file
|
||||
Allowed transmission values: bus, oss, shm, stream
|
||||
|
||||
Return schema:
|
||||
{
|
||||
"name": "short operator name",
|
||||
"category": "AI",
|
||||
"description": "1-2 sentence description",
|
||||
"inputs": [{"name":"input","type":"json","transmission":"bus","description":"optional"}],
|
||||
"outputs": [{"name":"result","type":"json","transmission":"bus","description":"optional"}],
|
||||
"hyperparams": [{"key":"ENV_KEY","value":"default","description":"what it controls"}],
|
||||
"skillDefinition": "detailed markdown instructions for future workflow generation and operator usage"
|
||||
}`)
|
||||
|
||||
var generated GeneratedOperator
|
||||
if err := client.generateJSON(ctx, systemPrompt, strings.TrimSpace(description), &generated); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if strings.TrimSpace(generated.Name) == "" {
|
||||
return nil, fmt.Errorf("AI did not return operator name")
|
||||
}
|
||||
if !validCategory(generated.Category) {
|
||||
return nil, fmt.Errorf("AI returned unsupported operator category %q", generated.Category)
|
||||
}
|
||||
if err := validateGeneratedPorts(generated.Inputs); err != nil {
|
||||
return nil, fmt.Errorf("invalid generated operator inputs: %w", err)
|
||||
}
|
||||
if err := validateGeneratedPorts(generated.Outputs); err != nil {
|
||||
return nil, fmt.Errorf("invalid generated operator outputs: %w", err)
|
||||
}
|
||||
if strings.TrimSpace(generated.Description) == "" {
|
||||
generated.Description = strings.TrimSpace(description)
|
||||
}
|
||||
if strings.TrimSpace(generated.SkillDefinition) == "" {
|
||||
generated.SkillDefinition = generated.Description
|
||||
}
|
||||
return &generated, nil
|
||||
}
|
||||
|
||||
func (client *client) generateJSON(ctx context.Context, systemPrompt string, userPrompt string, target any) error {
|
||||
requestBody, err := json.Marshal(chatCompletionRequest{
|
||||
Model: client.model,
|
||||
Messages: []chatMessage{
|
||||
{Role: "system", Content: systemPrompt},
|
||||
{Role: "user", Content: userPrompt},
|
||||
},
|
||||
Temperature: 0.1,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
payload, err := client.chatCompletion(ctx, requestBody)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var completion chatCompletionResponse
|
||||
if err := json.Unmarshal(payload, &completion); err != nil {
|
||||
return fmt.Errorf("decode AI response: %w", err)
|
||||
}
|
||||
if completion.Error != nil && strings.TrimSpace(completion.Error.Message) != "" {
|
||||
return errors.New(completion.Error.Message)
|
||||
}
|
||||
if len(completion.Choices) == 0 {
|
||||
return fmt.Errorf("AI response does not contain choices")
|
||||
}
|
||||
|
||||
content := normalizeMessageContent(completion.Choices[0].Message.Content)
|
||||
if strings.TrimSpace(content) == "" {
|
||||
return fmt.Errorf("AI response content is empty")
|
||||
}
|
||||
parsedJSON, err := extractJSONObject(content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := json.Unmarshal([]byte(parsedJSON), target); err != nil {
|
||||
return fmt.Errorf("decode AI JSON payload: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type httpStatusError struct {
|
||||
StatusCode int
|
||||
Body string
|
||||
}
|
||||
|
||||
func (err *httpStatusError) Error() string {
|
||||
body := strings.TrimSpace(err.Body)
|
||||
if body == "" {
|
||||
return fmt.Sprintf("AI request failed with status %d", err.StatusCode)
|
||||
}
|
||||
return fmt.Sprintf("AI request failed with status %d: %s", err.StatusCode, body)
|
||||
}
|
||||
|
||||
func (client *client) chatCompletion(ctx context.Context, requestBody []byte) ([]byte, error) {
|
||||
attempts := client.maxAttempts
|
||||
if attempts < 1 {
|
||||
attempts = 1
|
||||
}
|
||||
|
||||
var lastErr error
|
||||
for attempt := 1; attempt <= attempts; attempt++ {
|
||||
payload, err := client.chatCompletionOnce(ctx, requestBody)
|
||||
if err == nil {
|
||||
return payload, nil
|
||||
}
|
||||
lastErr = err
|
||||
if !isRetriableAIError(err) || attempt == attempts || ctx.Err() != nil {
|
||||
break
|
||||
}
|
||||
if err := sleepWithContext(ctx, time.Duration(attempt)*2*time.Second); err != nil {
|
||||
lastErr = err
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil, normalizeAIError(lastErr)
|
||||
}
|
||||
|
||||
func (client *client) chatCompletionOnce(ctx context.Context, requestBody []byte) ([]byte, error) {
|
||||
requestCtx := ctx
|
||||
cancel := func() {}
|
||||
if _, hasDeadline := ctx.Deadline(); !hasDeadline && client.requestTimeout > 0 {
|
||||
requestCtx, cancel = context.WithTimeout(ctx, client.requestTimeout)
|
||||
}
|
||||
defer cancel()
|
||||
|
||||
request, err := http.NewRequestWithContext(requestCtx, http.MethodPost, client.baseURL+"/chat/completions", bytes.NewReader(requestBody))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
request.Header.Set("Authorization", "Bearer "+client.apiKey)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
response, err := client.httpClient.Do(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
payload, err := io.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if response.StatusCode >= 400 {
|
||||
return nil, &httpStatusError{
|
||||
StatusCode: response.StatusCode,
|
||||
Body: string(payload),
|
||||
}
|
||||
}
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
func isRetriableAIError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) || isTimeoutError(err) {
|
||||
return true
|
||||
}
|
||||
var statusErr *httpStatusError
|
||||
if errors.As(err, &statusErr) {
|
||||
return statusErr.StatusCode == http.StatusTooManyRequests ||
|
||||
statusErr.StatusCode == http.StatusRequestTimeout ||
|
||||
statusErr.StatusCode >= http.StatusInternalServerError
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isTimeoutError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
var netErr net.Error
|
||||
if errors.As(err, &netErr) && netErr.Timeout() {
|
||||
return true
|
||||
}
|
||||
return strings.Contains(strings.ToLower(err.Error()), "client.timeout")
|
||||
}
|
||||
|
||||
func normalizeAIError(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) || isTimeoutError(err) {
|
||||
return fmt.Errorf("AI 生成超时,请稍后重试,或缩短描述范围后再试")
|
||||
}
|
||||
var statusErr *httpStatusError
|
||||
if errors.As(err, &statusErr) {
|
||||
switch {
|
||||
case statusErr.StatusCode == http.StatusTooManyRequests:
|
||||
return fmt.Errorf("AI 服务当前限流,请稍后重试")
|
||||
case statusErr.StatusCode == http.StatusRequestTimeout:
|
||||
return fmt.Errorf("AI 服务响应超时,请稍后重试")
|
||||
case statusErr.StatusCode >= http.StatusInternalServerError:
|
||||
return fmt.Errorf("AI 服务暂时不可用,请稍后重试")
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func sleepWithContext(ctx context.Context, duration time.Duration) error {
|
||||
timer := time.NewTimer(duration)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-timer.C:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeMessageContent(content any) string {
|
||||
switch value := content.(type) {
|
||||
case string:
|
||||
return value
|
||||
case []any:
|
||||
parts := make([]string, 0, len(value))
|
||||
for _, item := range value {
|
||||
typed, ok := item.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if text, ok := typed["text"].(string); ok {
|
||||
parts = append(parts, text)
|
||||
}
|
||||
}
|
||||
return strings.Join(parts, "\n")
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func extractJSONObject(raw string) (string, error) {
|
||||
trimmed := strings.TrimSpace(raw)
|
||||
trimmed = strings.TrimPrefix(trimmed, "```json")
|
||||
trimmed = strings.TrimPrefix(trimmed, "```")
|
||||
trimmed = strings.TrimSuffix(trimmed, "```")
|
||||
trimmed = strings.TrimSpace(trimmed)
|
||||
start := strings.Index(trimmed, "{")
|
||||
end := strings.LastIndex(trimmed, "}")
|
||||
if start < 0 || end < start {
|
||||
return "", fmt.Errorf("AI response is not a JSON object")
|
||||
}
|
||||
return trimmed[start : end+1], nil
|
||||
}
|
||||
|
||||
func clonePorts(ports []domain.PortDefinition) []domain.PortDefinition {
|
||||
items := make([]domain.PortDefinition, len(ports))
|
||||
copy(items, ports)
|
||||
return items
|
||||
}
|
||||
|
||||
func mergeHyperparams(base []domain.HyperParam, overrides []domain.HyperParam) []domain.HyperParam {
|
||||
if len(base) == 0 && len(overrides) == 0 {
|
||||
return []domain.HyperParam{}
|
||||
}
|
||||
merged := make([]domain.HyperParam, len(base))
|
||||
copy(merged, base)
|
||||
indexByKey := make(map[string]int, len(merged))
|
||||
for index, item := range merged {
|
||||
indexByKey[strings.ToUpper(strings.TrimSpace(item.Key))] = index
|
||||
}
|
||||
for _, item := range overrides {
|
||||
key := strings.ToUpper(strings.TrimSpace(item.Key))
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
item.Key = strings.TrimSpace(item.Key)
|
||||
if index, ok := indexByKey[key]; ok {
|
||||
if strings.TrimSpace(item.Value) != "" {
|
||||
merged[index].Value = item.Value
|
||||
}
|
||||
if strings.TrimSpace(item.Description) != "" {
|
||||
merged[index].Description = item.Description
|
||||
}
|
||||
if strings.TrimSpace(item.Type) != "" {
|
||||
merged[index].Type = item.Type
|
||||
}
|
||||
continue
|
||||
}
|
||||
merged = append(merged, item)
|
||||
indexByKey[key] = len(merged) - 1
|
||||
}
|
||||
return merged
|
||||
}
|
||||
|
||||
func resolvePortHandle(ports []domain.PortDefinition, name string) (string, error) {
|
||||
normalizedName := strings.TrimSpace(strings.ToLower(name))
|
||||
if normalizedName == "" {
|
||||
if len(ports) == 1 {
|
||||
return ports[0].ID, nil
|
||||
}
|
||||
return "", fmt.Errorf("port name is required when multiple ports are available")
|
||||
}
|
||||
for _, port := range ports {
|
||||
if strings.ToLower(strings.TrimSpace(port.Name)) == normalizedName || strings.ToLower(strings.TrimSpace(port.ID)) == normalizedName {
|
||||
return port.ID, nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("port %q not found", name)
|
||||
}
|
||||
|
||||
func chooseValue(value string, fallback string) string {
|
||||
if strings.TrimSpace(value) == "" {
|
||||
return fallback
|
||||
}
|
||||
return strings.TrimSpace(value)
|
||||
}
|
||||
|
||||
func validCategory(value string) bool {
|
||||
switch strings.ToUpper(strings.TrimSpace(value)) {
|
||||
case "IO", "CV", "AI", "MATH", "HUMAN", "FLOW":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func validPortType(value string) bool {
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "json", "image", "video", "tensor", "file":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func validTransmission(value string) bool {
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "bus", "oss", "shm", "stream":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func validateGeneratedPorts(ports []GeneratedOperatorPort) error {
|
||||
for _, port := range ports {
|
||||
if strings.TrimSpace(port.Name) == "" {
|
||||
return fmt.Errorf("port name is required")
|
||||
}
|
||||
if !validPortType(port.Type) {
|
||||
return fmt.Errorf("unsupported port type %q", port.Type)
|
||||
}
|
||||
if !validTransmission(port.Transmission) {
|
||||
return fmt.Errorf("unsupported transmission %q", port.Transmission)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
67
internal/ai/client_test.go
Normal file
67
internal/ai/client_test.go
Normal file
@ -0,0 +1,67 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestGenerateOperatorRetriesTimeoutThenSucceeds(t *testing.T) {
|
||||
var attempts atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
count := attempts.Add(1)
|
||||
if count == 1 {
|
||||
time.Sleep(80 * time.Millisecond)
|
||||
return
|
||||
}
|
||||
writer.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(writer, `{
|
||||
"choices": [
|
||||
{
|
||||
"message": {
|
||||
"content": "{\"name\":\"Timeout Retry Operator\",\"category\":\"AI\",\"description\":\"retry ok\",\"inputs\":[{\"name\":\"input\",\"type\":\"json\",\"transmission\":\"bus\"}],\"outputs\":[{\"name\":\"result\",\"type\":\"json\",\"transmission\":\"bus\"}],\"hyperparams\":[],\"skillDefinition\":\"retry ok\"}"
|
||||
}
|
||||
}
|
||||
]
|
||||
}`)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
service := NewService(server.URL, "test-key", "glm-5").(*client)
|
||||
service.requestTimeout = 40 * time.Millisecond
|
||||
service.maxAttempts = 2
|
||||
|
||||
item, err := service.GenerateOperator(context.Background(), "generate operator")
|
||||
if err != nil {
|
||||
t.Fatalf("expected retry to succeed, got error: %v", err)
|
||||
}
|
||||
if item.Name != "Timeout Retry Operator" {
|
||||
t.Fatalf("unexpected operator name: %s", item.Name)
|
||||
}
|
||||
if attempts.Load() != 2 {
|
||||
t.Fatalf("expected 2 attempts, got %d", attempts.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateOperatorTimeoutReturnsFriendlyError(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
time.Sleep(80 * time.Millisecond)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
service := NewService(server.URL, "test-key", "glm-5").(*client)
|
||||
service.requestTimeout = 30 * time.Millisecond
|
||||
service.maxAttempts = 1
|
||||
|
||||
_, err := service.GenerateOperator(context.Background(), "generate operator")
|
||||
if err == nil {
|
||||
t.Fatalf("expected timeout error")
|
||||
}
|
||||
if err.Error() != "AI 生成超时,请稍后重试,或缩短描述范围后再试" {
|
||||
t.Fatalf("unexpected timeout message: %v", err)
|
||||
}
|
||||
}
|
||||
1274
internal/api/server.go
Normal file
1274
internal/api/server.go
Normal file
File diff suppressed because it is too large
Load Diff
579
internal/api/server_test.go
Normal file
579
internal/api/server_test.go
Normal file
@ -0,0 +1,579 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"droboticflow/internal/domain"
|
||||
"droboticflow/internal/store"
|
||||
)
|
||||
|
||||
func TestAPIHandlerSupportsDashboardCRUDAndRunQueueing(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
dataStore := store.NewMemoryStore()
|
||||
|
||||
storageRoot := t.TempDir()
|
||||
for _, bucket := range []string{"incoming", "processed"} {
|
||||
if err := os.Mkdir(filepath.Join(storageRoot, bucket), 0o755); err != nil {
|
||||
t.Fatalf("create bucket %s: %v", bucket, err)
|
||||
}
|
||||
}
|
||||
settings, err := dataStore.GetSettings(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("get settings: %v", err)
|
||||
}
|
||||
settings.K8s.Kubeconfig = "apiVersion: v1"
|
||||
if err := dataStore.UpsertSettings(ctx, *settings); err != nil {
|
||||
t.Fatalf("update settings: %v", err)
|
||||
}
|
||||
if _, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||||
Name: "Dashboard 项目",
|
||||
Description: "用于 dashboard 验证",
|
||||
}); err != nil {
|
||||
t.Fatalf("create dashboard project: %v", err)
|
||||
}
|
||||
|
||||
handler := NewHandler(dataStore, nil)
|
||||
|
||||
dashboardRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(dashboardRecorder, httptest.NewRequest(http.MethodGet, "/api/dashboard", nil))
|
||||
if dashboardRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("dashboard status: %d", dashboardRecorder.Code)
|
||||
}
|
||||
var dashboardBody map[string]any
|
||||
if err := json.Unmarshal(dashboardRecorder.Body.Bytes(), &dashboardBody); err != nil {
|
||||
t.Fatalf("decode dashboard: %v", err)
|
||||
}
|
||||
projects := dashboardBody["projects"].(map[string]any)
|
||||
if projects["active"].(float64) <= 0 {
|
||||
t.Fatalf("expected active projects")
|
||||
}
|
||||
|
||||
projectPayload := []byte(`{"name":"API 项目","description":"通过 API 创建"}`)
|
||||
projectRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(projectRecorder, httptest.NewRequest(http.MethodPost, "/api/projects", bytes.NewReader(projectPayload)))
|
||||
if projectRecorder.Code != http.StatusCreated {
|
||||
t.Fatalf("project create status: %d body=%s", projectRecorder.Code, projectRecorder.Body.String())
|
||||
}
|
||||
var createdProject map[string]any
|
||||
if err := json.Unmarshal(projectRecorder.Body.Bytes(), &createdProject); err != nil {
|
||||
t.Fatalf("decode created project: %v", err)
|
||||
}
|
||||
projectID := createdProject["id"].(string)
|
||||
if projectID == "" {
|
||||
t.Fatalf("expected project id")
|
||||
}
|
||||
|
||||
workflowPayload := []byte(`{"name":"API 工作流","description":"workflow","type":"batch","nodes":[{"id":"node-source","type":"operator","data":{"label":"真实数据源","category":"IO","description":"source","inputs":[],"outputs":[{"id":"out-1","name":"file","type":"file"}],"hyperparams":[{"key":"SOURCE_TEXT","value":"api-test"}]},"position":{"x":0,"y":0}}],"edges":[]}`)
|
||||
workflowRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(workflowRecorder, httptest.NewRequest(http.MethodPost, "/api/projects/"+projectID+"/workflows", bytes.NewReader(workflowPayload)))
|
||||
if workflowRecorder.Code != http.StatusCreated {
|
||||
t.Fatalf("workflow create status: %d body=%s", workflowRecorder.Code, workflowRecorder.Body.String())
|
||||
}
|
||||
var createdWorkflow map[string]any
|
||||
if err := json.Unmarshal(workflowRecorder.Body.Bytes(), &createdWorkflow); err != nil {
|
||||
t.Fatalf("decode workflow: %v", err)
|
||||
}
|
||||
|
||||
projectsRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(projectsRecorder, httptest.NewRequest(http.MethodGet, "/api/projects", nil))
|
||||
if projectsRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("projects list status: %d body=%s", projectsRecorder.Code, projectsRecorder.Body.String())
|
||||
}
|
||||
var projectsBody struct {
|
||||
Items []map[string]any `json:"items"`
|
||||
}
|
||||
if err := json.Unmarshal(projectsRecorder.Body.Bytes(), &projectsBody); err != nil {
|
||||
t.Fatalf("decode projects list: %v", err)
|
||||
}
|
||||
var countedProject map[string]any
|
||||
for _, item := range projectsBody.Items {
|
||||
if item["id"] == projectID {
|
||||
countedProject = item
|
||||
break
|
||||
}
|
||||
}
|
||||
if countedProject == nil {
|
||||
t.Fatalf("expected created project in projects list")
|
||||
}
|
||||
if countedProject["workflowCount"].(float64) != 1 {
|
||||
t.Fatalf("expected workflowCount=1 in projects list, got %v", countedProject["workflowCount"])
|
||||
}
|
||||
|
||||
projectGetRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(projectGetRecorder, httptest.NewRequest(http.MethodGet, "/api/projects/"+projectID, nil))
|
||||
if projectGetRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("project get status: %d body=%s", projectGetRecorder.Code, projectGetRecorder.Body.String())
|
||||
}
|
||||
var projectGetBody map[string]any
|
||||
if err := json.Unmarshal(projectGetRecorder.Body.Bytes(), &projectGetBody); err != nil {
|
||||
t.Fatalf("decode project get: %v", err)
|
||||
}
|
||||
if projectGetBody["workflowCount"].(float64) != 1 {
|
||||
t.Fatalf("expected workflowCount=1 in project get, got %v", projectGetBody["workflowCount"])
|
||||
}
|
||||
|
||||
runPayload := []byte(`{"trigger":"manual"}`)
|
||||
runRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(runRecorder, httptest.NewRequest(http.MethodPost, "/api/workflows/"+createdWorkflow["id"].(string)+"/run", bytes.NewReader(runPayload)))
|
||||
if runRecorder.Code != http.StatusAccepted {
|
||||
t.Fatalf("run create status: %d body=%s", runRecorder.Code, runRecorder.Body.String())
|
||||
}
|
||||
var queuedRun map[string]any
|
||||
if err := json.Unmarshal(runRecorder.Body.Bytes(), &queuedRun); err != nil {
|
||||
t.Fatalf("decode queued run: %v", err)
|
||||
}
|
||||
if queuedRun["status"].(string) != "queued" {
|
||||
t.Fatalf("expected queued status, got %v", queuedRun["status"])
|
||||
}
|
||||
|
||||
userPayload := []byte(`{"name":"API User","email":"api-user@example.com","role":"admin"}`)
|
||||
userRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(userRecorder, httptest.NewRequest(http.MethodPost, "/api/users", bytes.NewReader(userPayload)))
|
||||
if userRecorder.Code != http.StatusCreated {
|
||||
t.Fatalf("user create status: %d body=%s", userRecorder.Code, userRecorder.Body.String())
|
||||
}
|
||||
|
||||
runsRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(runsRecorder, httptest.NewRequest(http.MethodGet, "/api/runs", nil))
|
||||
if runsRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("runs list status: %d", runsRecorder.Code)
|
||||
}
|
||||
var runsBody struct {
|
||||
Items []map[string]any `json:"items"`
|
||||
}
|
||||
if err := json.Unmarshal(runsRecorder.Body.Bytes(), &runsBody); err != nil {
|
||||
t.Fatalf("decode runs: %v", err)
|
||||
}
|
||||
foundRun := false
|
||||
for _, item := range runsBody.Items {
|
||||
if item["id"] == queuedRun["id"] {
|
||||
foundRun = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !foundRun {
|
||||
t.Fatalf("expected queued run in listing")
|
||||
}
|
||||
|
||||
bucketsRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(bucketsRecorder, httptest.NewRequest(http.MethodGet, "/api/storage/buckets?sourcePath="+storageRoot, nil))
|
||||
if bucketsRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("buckets status: %d", bucketsRecorder.Code)
|
||||
}
|
||||
var bucketsBody struct {
|
||||
Items []string `json:"items"`
|
||||
}
|
||||
if err := json.Unmarshal(bucketsRecorder.Body.Bytes(), &bucketsBody); err != nil {
|
||||
t.Fatalf("decode buckets: %v", err)
|
||||
}
|
||||
if len(bucketsBody.Items) == 0 {
|
||||
t.Fatalf("expected storage buckets")
|
||||
}
|
||||
|
||||
objectsRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(
|
||||
objectsRecorder,
|
||||
httptest.NewRequest(http.MethodGet, "/api/storage/objects?sourcePath="+storageRoot+"&bucket=incoming&prefix=", nil),
|
||||
)
|
||||
if objectsRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("objects status: %d", objectsRecorder.Code)
|
||||
}
|
||||
|
||||
missingSourcePathRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(missingSourcePathRecorder, httptest.NewRequest(http.MethodGet, "/api/storage/buckets", nil))
|
||||
if missingSourcePathRecorder.Code != http.StatusBadRequest {
|
||||
t.Fatalf("missing sourcePath status: %d", missingSourcePathRecorder.Code)
|
||||
}
|
||||
|
||||
aiRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(aiRecorder, httptest.NewRequest(http.MethodGet, "/api/ai/status", nil))
|
||||
if aiRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("ai status: %d", aiRecorder.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSettingsAIConfigurationDrivesAIStatus(t *testing.T) {
|
||||
dataStore := store.NewMemoryStore()
|
||||
handler := NewHandler(dataStore, nil)
|
||||
|
||||
settingsPayload := []byte(`{
|
||||
"k8s": {
|
||||
"endpoint": "https://120.48.161.230:6443",
|
||||
"namespace": "droboticflow",
|
||||
"kubeconfig": "apiVersion: v1\nkind: Config"
|
||||
},
|
||||
"ai": {
|
||||
"provider": "dashscope-openai-compatible",
|
||||
"baseURL": "https://coding.dashscope.aliyuncs.com/v1",
|
||||
"model": "glm-5",
|
||||
"apiKey": "test-key"
|
||||
},
|
||||
"timezone": "Asia/Tokyo"
|
||||
}`)
|
||||
|
||||
updateRecorder := httptest.NewRecorder()
|
||||
updateRequest := httptest.NewRequest(http.MethodPut, "/api/settings", bytes.NewReader(settingsPayload))
|
||||
handler.ServeHTTP(updateRecorder, updateRequest)
|
||||
if updateRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("settings update status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
||||
}
|
||||
|
||||
statusRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(statusRecorder, httptest.NewRequest(http.MethodGet, "/api/ai/status", nil))
|
||||
if statusRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("ai status code: %d body=%s", statusRecorder.Code, statusRecorder.Body.String())
|
||||
}
|
||||
|
||||
var statusBody struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
BaseURL string `json:"baseURL"`
|
||||
Model string `json:"model"`
|
||||
Provider string `json:"provider"`
|
||||
}
|
||||
if err := json.Unmarshal(statusRecorder.Body.Bytes(), &statusBody); err != nil {
|
||||
t.Fatalf("decode ai status: %v", err)
|
||||
}
|
||||
if !statusBody.Enabled {
|
||||
t.Fatalf("expected ai status enabled")
|
||||
}
|
||||
if statusBody.Model != "glm-5" {
|
||||
t.Fatalf("unexpected model: %s", statusBody.Model)
|
||||
}
|
||||
if statusBody.BaseURL != "https://coding.dashscope.aliyuncs.com/v1" {
|
||||
t.Fatalf("unexpected baseURL: %s", statusBody.BaseURL)
|
||||
}
|
||||
|
||||
settingsRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(settingsRecorder, httptest.NewRequest(http.MethodGet, "/api/settings", nil))
|
||||
if settingsRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("settings get status: %d body=%s", settingsRecorder.Code, settingsRecorder.Body.String())
|
||||
}
|
||||
var settingsBody struct {
|
||||
K8s struct {
|
||||
Kubeconfig string `json:"kubeconfig"`
|
||||
KubeconfigPreview string `json:"kubeconfigPreview"`
|
||||
HasKubeconfig bool `json:"hasKubeconfig"`
|
||||
} `json:"k8s"`
|
||||
AI struct {
|
||||
APIKey string `json:"apiKey"`
|
||||
APIKeyPreview string `json:"apiKeyPreview"`
|
||||
HasAPIKey bool `json:"hasAPIKey"`
|
||||
} `json:"ai"`
|
||||
Timezone string `json:"timezone"`
|
||||
Storage any `json:"storage"`
|
||||
}
|
||||
if err := json.Unmarshal(settingsRecorder.Body.Bytes(), &settingsBody); err != nil {
|
||||
t.Fatalf("decode settings: %v", err)
|
||||
}
|
||||
if settingsBody.K8s.Kubeconfig != "" {
|
||||
t.Fatalf("expected settings kubeconfig to be sanitized, got %q", settingsBody.K8s.Kubeconfig)
|
||||
}
|
||||
if !settingsBody.K8s.HasKubeconfig {
|
||||
t.Fatalf("expected hasKubeconfig to be true")
|
||||
}
|
||||
if settingsBody.K8s.KubeconfigPreview == "" {
|
||||
t.Fatalf("expected kubeconfig preview to be returned")
|
||||
}
|
||||
if settingsBody.AI.APIKey != "" {
|
||||
t.Fatalf("expected settings apiKey to be sanitized")
|
||||
}
|
||||
if !settingsBody.AI.HasAPIKey {
|
||||
t.Fatalf("expected hasAPIKey to be true")
|
||||
}
|
||||
if settingsBody.AI.APIKeyPreview != "te****ey" {
|
||||
t.Fatalf("expected apiKey preview to be masked, got %q", settingsBody.AI.APIKeyPreview)
|
||||
}
|
||||
if settingsBody.Timezone != "Asia/Tokyo" {
|
||||
t.Fatalf("expected timezone to roundtrip, got %q", settingsBody.Timezone)
|
||||
}
|
||||
if settingsBody.Storage != nil {
|
||||
t.Fatalf("expected storage to be omitted from settings response")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTemplateExecutionConfigValidationAndPersistence(t *testing.T) {
|
||||
dataStore := store.NewMemoryStore()
|
||||
handler := NewHandler(dataStore, nil)
|
||||
|
||||
createPayload := []byte(`{
|
||||
"name":"执行保障模板",
|
||||
"description":"template execution config",
|
||||
"type":"streaming",
|
||||
"maxConcurrentRuns":2,
|
||||
"nodeTimeoutSeconds":12,
|
||||
"runTimeoutSeconds":34,
|
||||
"maxRetryAttempts":1,
|
||||
"retryBackoffSeconds":56,
|
||||
"schedule":{
|
||||
"interval":90,
|
||||
"sourcePath":"oss://incoming"
|
||||
},
|
||||
"nodes":[],
|
||||
"edges":[]
|
||||
}`)
|
||||
|
||||
createRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(
|
||||
createRecorder,
|
||||
httptest.NewRequest(http.MethodPost, "/api/templates", bytes.NewReader(createPayload)),
|
||||
)
|
||||
if createRecorder.Code != http.StatusCreated {
|
||||
t.Fatalf("template create status: %d body=%s", createRecorder.Code, createRecorder.Body.String())
|
||||
}
|
||||
|
||||
var created map[string]any
|
||||
if err := json.Unmarshal(createRecorder.Body.Bytes(), &created); err != nil {
|
||||
t.Fatalf("decode created template: %v", err)
|
||||
}
|
||||
if created["type"].(string) != "streaming" {
|
||||
t.Fatalf("expected streaming type, got %v", created["type"])
|
||||
}
|
||||
if created["retryBackoffSeconds"].(float64) != 56 {
|
||||
t.Fatalf("expected retryBackoffSeconds=56, got %v", created["retryBackoffSeconds"])
|
||||
}
|
||||
|
||||
templateID := created["id"].(string)
|
||||
getRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(getRecorder, httptest.NewRequest(http.MethodGet, "/api/templates/"+templateID, nil))
|
||||
if getRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("template get status: %d body=%s", getRecorder.Code, getRecorder.Body.String())
|
||||
}
|
||||
|
||||
var loaded map[string]any
|
||||
if err := json.Unmarshal(getRecorder.Body.Bytes(), &loaded); err != nil {
|
||||
t.Fatalf("decode loaded template: %v", err)
|
||||
}
|
||||
if loaded["nodeTimeoutSeconds"].(float64) != 12 {
|
||||
t.Fatalf("expected nodeTimeoutSeconds=12, got %v", loaded["nodeTimeoutSeconds"])
|
||||
}
|
||||
schedule := loaded["schedule"].(map[string]any)
|
||||
if schedule["interval"].(float64) != 90 {
|
||||
t.Fatalf("expected interval=90, got %v", schedule["interval"])
|
||||
}
|
||||
|
||||
updatePayload := []byte(`{
|
||||
"type":"batch",
|
||||
"maxConcurrentRuns":3,
|
||||
"nodeTimeoutSeconds":6,
|
||||
"runTimeoutSeconds":18,
|
||||
"maxRetryAttempts":2,
|
||||
"retryBackoffSeconds":9
|
||||
}`)
|
||||
updateRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(
|
||||
updateRecorder,
|
||||
httptest.NewRequest(http.MethodPut, "/api/templates/"+templateID, bytes.NewReader(updatePayload)),
|
||||
)
|
||||
if updateRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("template update status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
||||
}
|
||||
|
||||
var updated map[string]any
|
||||
if err := json.Unmarshal(updateRecorder.Body.Bytes(), &updated); err != nil {
|
||||
t.Fatalf("decode updated template: %v", err)
|
||||
}
|
||||
if updated["type"].(string) != "batch" {
|
||||
t.Fatalf("expected batch type after update, got %v", updated["type"])
|
||||
}
|
||||
if updated["maxConcurrentRuns"].(float64) != 3 {
|
||||
t.Fatalf("expected maxConcurrentRuns=3 after update, got %v", updated["maxConcurrentRuns"])
|
||||
}
|
||||
if updated["retryBackoffSeconds"].(float64) != 9 {
|
||||
t.Fatalf("expected retryBackoffSeconds=9 after update, got %v", updated["retryBackoffSeconds"])
|
||||
}
|
||||
|
||||
invalidPayload := []byte(`{"maxRetryAttempts":-1}`)
|
||||
invalidRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(
|
||||
invalidRecorder,
|
||||
httptest.NewRequest(http.MethodPut, "/api/templates/"+templateID, bytes.NewReader(invalidPayload)),
|
||||
)
|
||||
if invalidRecorder.Code != http.StatusBadRequest {
|
||||
t.Fatalf("invalid template update status: %d body=%s", invalidRecorder.Code, invalidRecorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkflowExecutionConfigValidationAndPersistence(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
dataStore := store.NewMemoryStore()
|
||||
handler := NewHandler(dataStore, nil)
|
||||
|
||||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||||
Name: "执行配置项目",
|
||||
Description: "workflow execution settings",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create project: %v", err)
|
||||
}
|
||||
|
||||
createPayload := []byte(`{
|
||||
"name":"执行配置工作流",
|
||||
"description":"workflow",
|
||||
"type":"batch",
|
||||
"maxConcurrentRuns":2,
|
||||
"nodeTimeoutSeconds":30,
|
||||
"runTimeoutSeconds":120,
|
||||
"maxRetryAttempts":3,
|
||||
"retryBackoffSeconds":15,
|
||||
"nodes":[{"id":"node-source","type":"operator","data":{"label":"真实数据源","category":"IO","description":"source","inputs":[],"outputs":[{"id":"out-1","name":"file","type":"file"}],"hyperparams":[{"key":"SOURCE_TEXT","value":"api-test"}]},"position":{"x":0,"y":0}}],
|
||||
"edges":[]
|
||||
}`)
|
||||
|
||||
createRecorder := httptest.NewRecorder()
|
||||
createRequest := httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(createPayload))
|
||||
handler.ServeHTTP(createRecorder, createRequest)
|
||||
if createRecorder.Code != http.StatusCreated {
|
||||
t.Fatalf("workflow create status: %d body=%s", createRecorder.Code, createRecorder.Body.String())
|
||||
}
|
||||
|
||||
var created domain.WorkflowRecord
|
||||
if err := json.Unmarshal(createRecorder.Body.Bytes(), &created); err != nil {
|
||||
t.Fatalf("decode created workflow: %v", err)
|
||||
}
|
||||
if created.RunTimeoutSeconds != 120 {
|
||||
t.Fatalf("expected runTimeoutSeconds=120, got %d", created.RunTimeoutSeconds)
|
||||
}
|
||||
if created.NodeTimeoutSeconds != 30 {
|
||||
t.Fatalf("expected nodeTimeoutSeconds=30, got %d", created.NodeTimeoutSeconds)
|
||||
}
|
||||
if created.MaxRetryAttempts != 3 {
|
||||
t.Fatalf("expected maxRetryAttempts=3, got %d", created.MaxRetryAttempts)
|
||||
}
|
||||
if created.RetryBackoffSeconds != 15 {
|
||||
t.Fatalf("expected retryBackoffSeconds=15, got %d", created.RetryBackoffSeconds)
|
||||
}
|
||||
|
||||
updatePayload := []byte(`{"nodeTimeoutSeconds":12,"runTimeoutSeconds":45,"maxRetryAttempts":1,"retryBackoffSeconds":9}`)
|
||||
updateRecorder := httptest.NewRecorder()
|
||||
updateRequest := httptest.NewRequest(http.MethodPut, "/api/workflows/"+created.ID, bytes.NewReader(updatePayload))
|
||||
handler.ServeHTTP(updateRecorder, updateRequest)
|
||||
if updateRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("workflow update status: %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
||||
}
|
||||
|
||||
var updated domain.WorkflowRecord
|
||||
if err := json.Unmarshal(updateRecorder.Body.Bytes(), &updated); err != nil {
|
||||
t.Fatalf("decode updated workflow: %v", err)
|
||||
}
|
||||
if updated.RunTimeoutSeconds != 45 {
|
||||
t.Fatalf("expected updated runTimeoutSeconds=45, got %d", updated.RunTimeoutSeconds)
|
||||
}
|
||||
if updated.NodeTimeoutSeconds != 12 {
|
||||
t.Fatalf("expected updated nodeTimeoutSeconds=12, got %d", updated.NodeTimeoutSeconds)
|
||||
}
|
||||
if updated.MaxRetryAttempts != 1 {
|
||||
t.Fatalf("expected updated maxRetryAttempts=1, got %d", updated.MaxRetryAttempts)
|
||||
}
|
||||
if updated.RetryBackoffSeconds != 9 {
|
||||
t.Fatalf("expected updated retryBackoffSeconds=9, got %d", updated.RetryBackoffSeconds)
|
||||
}
|
||||
|
||||
invalidCreatePayload := []byte(`{
|
||||
"name":"非法工作流",
|
||||
"description":"workflow",
|
||||
"type":"batch",
|
||||
"nodeTimeoutSeconds":-1,
|
||||
"runTimeoutSeconds":-1,
|
||||
"maxRetryAttempts":-2,
|
||||
"retryBackoffSeconds":-3,
|
||||
"nodes":[],
|
||||
"edges":[]
|
||||
}`)
|
||||
invalidCreateRecorder := httptest.NewRecorder()
|
||||
invalidCreateRequest := httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflows", bytes.NewReader(invalidCreatePayload))
|
||||
handler.ServeHTTP(invalidCreateRecorder, invalidCreateRequest)
|
||||
if invalidCreateRecorder.Code != http.StatusBadRequest {
|
||||
t.Fatalf("invalid create status: %d body=%s", invalidCreateRecorder.Code, invalidCreateRecorder.Body.String())
|
||||
}
|
||||
|
||||
invalidUpdatePayload := []byte(`{"nodeTimeoutSeconds":-1,"runTimeoutSeconds":-1,"maxRetryAttempts":-1,"retryBackoffSeconds":-1}`)
|
||||
invalidUpdateRecorder := httptest.NewRecorder()
|
||||
invalidUpdateRequest := httptest.NewRequest(http.MethodPut, "/api/workflows/"+created.ID, bytes.NewReader(invalidUpdatePayload))
|
||||
handler.ServeHTTP(invalidUpdateRecorder, invalidUpdateRequest)
|
||||
if invalidUpdateRecorder.Code != http.StatusBadRequest {
|
||||
t.Fatalf("invalid update status: %d body=%s", invalidUpdateRecorder.Code, invalidUpdateRecorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompletedWorkflowCannotBeRunAgainManually(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
dataStore := store.NewMemoryStore()
|
||||
|
||||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||||
Name: "已完成项目",
|
||||
Description: "禁止重复运行",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create project: %v", err)
|
||||
}
|
||||
|
||||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||||
ProjectID: project.ID,
|
||||
Name: "已完成工作流",
|
||||
Description: "completed",
|
||||
Type: domain.WorkflowTypeBatch,
|
||||
Nodes: []domain.WorkflowNode{
|
||||
{
|
||||
ID: "source",
|
||||
Type: "operator",
|
||||
Data: domain.WorkflowNodeData{
|
||||
Label: "数据源",
|
||||
Category: "IO",
|
||||
Description: "source",
|
||||
},
|
||||
Position: domain.Position{X: 0, Y: 0},
|
||||
},
|
||||
},
|
||||
Edges: []domain.WorkflowEdge{},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create workflow: %v", err)
|
||||
}
|
||||
workflow.Status = domain.WorkflowStatusCompleted
|
||||
if err := dataStore.UpsertWorkflow(ctx, *workflow); err != nil {
|
||||
t.Fatalf("mark workflow completed: %v", err)
|
||||
}
|
||||
|
||||
handler := NewHandler(dataStore, nil)
|
||||
runRecorder := httptest.NewRecorder()
|
||||
runRequest := httptest.NewRequest(
|
||||
http.MethodPost,
|
||||
"/api/workflows/"+workflow.ID+"/run",
|
||||
bytes.NewReader([]byte(`{"trigger":"manual"}`)),
|
||||
)
|
||||
handler.ServeHTTP(runRecorder, runRequest)
|
||||
if runRecorder.Code != http.StatusConflict {
|
||||
t.Fatalf("expected 409 for completed workflow rerun, got %d body=%s", runRecorder.Code, runRecorder.Body.String())
|
||||
}
|
||||
|
||||
var body map[string]string
|
||||
if err := json.Unmarshal(runRecorder.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("decode rerun response: %v", err)
|
||||
}
|
||||
if body["error"] == "" {
|
||||
t.Fatalf("expected rerun error message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSettingsRejectsInvalidTimezone(t *testing.T) {
|
||||
dataStore := store.NewMemoryStore()
|
||||
handler := NewHandler(dataStore, nil)
|
||||
|
||||
updateRecorder := httptest.NewRecorder()
|
||||
updateRequest := httptest.NewRequest(
|
||||
http.MethodPut,
|
||||
"/api/settings",
|
||||
bytes.NewReader([]byte(`{"timezone":"Mars/Base"}`)),
|
||||
)
|
||||
handler.ServeHTTP(updateRecorder, updateRequest)
|
||||
if updateRecorder.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected invalid timezone to fail, got %d body=%s", updateRecorder.Code, updateRecorder.Body.String())
|
||||
}
|
||||
}
|
||||
68
internal/config/config.go
Normal file
68
internal/config/config.go
Normal file
@ -0,0 +1,68 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type AppConfig struct {
|
||||
Port int
|
||||
WorkerPort int
|
||||
MongoURI string
|
||||
MongoDB string
|
||||
MongoConnectRetries int
|
||||
MongoConnectRetryDelayMS int
|
||||
PollIntervalMS int
|
||||
K8sEnabled bool
|
||||
K8sNamespace string
|
||||
K8sImagePullSecretName string
|
||||
OperatorRunnerImage string
|
||||
LocalOperatorRunnerCmd string
|
||||
AIAPIBaseURL string
|
||||
AIAPIKey string
|
||||
AIModel string
|
||||
DistDir string
|
||||
}
|
||||
|
||||
func parseInt(value string, fallback int) int {
|
||||
parsed, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
return fallback
|
||||
}
|
||||
return parsed
|
||||
}
|
||||
|
||||
func boolValue(value string) bool {
|
||||
return value == "true" || value == "1" || value == "yes"
|
||||
}
|
||||
|
||||
func Load() AppConfig {
|
||||
workingDirectory, _ := os.Getwd()
|
||||
return AppConfig{
|
||||
Port: parseInt(os.Getenv("PORT"), 3001),
|
||||
WorkerPort: parseInt(os.Getenv("WORKER_PORT"), 3002),
|
||||
MongoURI: getenv("MONGO_URI", "mongodb://127.0.0.1:27017"),
|
||||
MongoDB: getenv("MONGO_DB", "droboticflow"),
|
||||
MongoConnectRetries: parseInt(os.Getenv("MONGO_CONNECT_RETRIES"), 30),
|
||||
MongoConnectRetryDelayMS: parseInt(os.Getenv("MONGO_CONNECT_RETRY_DELAY_MS"), 2000),
|
||||
PollIntervalMS: parseInt(os.Getenv("WORKER_POLL_INTERVAL_MS"), 2000),
|
||||
K8sEnabled: boolValue(os.Getenv("K8S_ENABLED")),
|
||||
K8sNamespace: getenv("K8S_NAMESPACE", "droboticflow"),
|
||||
K8sImagePullSecretName: getenv("K8S_IMAGE_PULL_SECRET_NAME", "droboticflow-registry"),
|
||||
OperatorRunnerImage: getenv("OPERATOR_RUNNER_IMAGE", "ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-operator-runner:latest"),
|
||||
LocalOperatorRunnerCmd: getenv("LOCAL_OPERATOR_RUNNER_CMD", "python3 backend/operator-runner/main.py"),
|
||||
AIAPIBaseURL: getenv("AI_API_BASE_URL", "https://coding.dashscope.aliyuncs.com/v1"),
|
||||
AIAPIKey: os.Getenv("AI_API_KEY"),
|
||||
AIModel: getenv("AI_MODEL", "glm-5"),
|
||||
DistDir: getenv("DIST_DIR", filepath.Join(workingDirectory, "dist")),
|
||||
}
|
||||
}
|
||||
|
||||
func getenv(key string, fallback string) string {
|
||||
value := os.Getenv(key)
|
||||
if value == "" {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
12
internal/domain/id.go
Normal file
12
internal/domain/id.go
Normal file
@ -0,0 +1,12 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
func NewID(prefix string) string {
|
||||
random := make([]byte, 16)
|
||||
_, _ = rand.Read(random)
|
||||
return prefix + "-" + hex.EncodeToString(random)
|
||||
}
|
||||
445
internal/domain/types.go
Normal file
445
internal/domain/types.go
Normal file
@ -0,0 +1,445 @@
|
||||
package domain
|
||||
|
||||
type UserRole string
|
||||
|
||||
const (
|
||||
UserRoleAdmin UserRole = "admin"
|
||||
UserRoleAuditor UserRole = "auditor"
|
||||
UserRoleOperator UserRole = "operator"
|
||||
)
|
||||
|
||||
type PortType string
|
||||
|
||||
const (
|
||||
PortTypeJSON PortType = "json"
|
||||
PortTypeImage PortType = "image"
|
||||
PortTypeVideo PortType = "video"
|
||||
PortTypeTensor PortType = "tensor"
|
||||
PortTypeFile PortType = "file"
|
||||
)
|
||||
|
||||
type WorkflowStatus string
|
||||
|
||||
const (
|
||||
WorkflowStatusDraft WorkflowStatus = "draft"
|
||||
WorkflowStatusReady WorkflowStatus = "ready"
|
||||
WorkflowStatusRunning WorkflowStatus = "running"
|
||||
WorkflowStatusCompleted WorkflowStatus = "completed"
|
||||
WorkflowStatusFailed WorkflowStatus = "failed"
|
||||
)
|
||||
|
||||
type WorkflowType string
|
||||
|
||||
const (
|
||||
WorkflowTypeBatch WorkflowType = "batch"
|
||||
WorkflowTypeStreaming WorkflowType = "streaming"
|
||||
)
|
||||
|
||||
type RunTrigger string
|
||||
|
||||
const (
|
||||
RunTriggerManual RunTrigger = "manual"
|
||||
RunTriggerScheduled RunTrigger = "scheduled"
|
||||
RunTriggerRetry RunTrigger = "retry"
|
||||
)
|
||||
|
||||
type RunStatus string
|
||||
|
||||
const (
|
||||
RunStatusQueued RunStatus = "queued"
|
||||
RunStatusRunning RunStatus = "running"
|
||||
RunStatusWaitingForAudit RunStatus = "waiting_for_audit"
|
||||
RunStatusCompleted RunStatus = "completed"
|
||||
RunStatusFailed RunStatus = "failed"
|
||||
RunStatusCancelled RunStatus = "cancelled"
|
||||
)
|
||||
|
||||
type AuditStatus string
|
||||
|
||||
const (
|
||||
AuditStatusPending AuditStatus = "pending"
|
||||
AuditStatusApproved AuditStatus = "approved"
|
||||
AuditStatusRejected AuditStatus = "rejected"
|
||||
)
|
||||
|
||||
type NodeExecutionStatus string
|
||||
|
||||
const (
|
||||
NodeExecutionStatusQueued NodeExecutionStatus = "queued"
|
||||
NodeExecutionStatusRunning NodeExecutionStatus = "running"
|
||||
NodeExecutionStatusWaitingForAudit NodeExecutionStatus = "waiting_for_audit"
|
||||
NodeExecutionStatusCompleted NodeExecutionStatus = "completed"
|
||||
NodeExecutionStatusFailed NodeExecutionStatus = "failed"
|
||||
)
|
||||
|
||||
type Position struct {
|
||||
X float64 `json:"x" bson:"x"`
|
||||
Y float64 `json:"y" bson:"y"`
|
||||
}
|
||||
|
||||
type PortDefinition struct {
|
||||
ID string `json:"id" bson:"id"`
|
||||
Name string `json:"name" bson:"name"`
|
||||
Type PortType `json:"type" bson:"type"`
|
||||
Description string `json:"description,omitempty" bson:"description,omitempty"`
|
||||
Transmission string `json:"transmission,omitempty" bson:"transmission,omitempty"`
|
||||
}
|
||||
|
||||
type HyperParam struct {
|
||||
Key string `json:"key" bson:"key"`
|
||||
Value string `json:"value" bson:"value"`
|
||||
Description string `json:"description,omitempty" bson:"description,omitempty"`
|
||||
Type string `json:"type,omitempty" bson:"type,omitempty"`
|
||||
}
|
||||
|
||||
type WorkflowNodeData struct {
|
||||
Label string `json:"label" bson:"label"`
|
||||
Category string `json:"category" bson:"category"`
|
||||
Description string `json:"description" bson:"description"`
|
||||
Inputs []PortDefinition `json:"inputs" bson:"inputs"`
|
||||
Outputs []PortDefinition `json:"outputs" bson:"outputs"`
|
||||
Image string `json:"image,omitempty" bson:"image,omitempty"`
|
||||
SkillDefinition string `json:"skillDefinition,omitempty" bson:"skillDefinition,omitempty"`
|
||||
Hyperparams []HyperParam `json:"hyperparams,omitempty" bson:"hyperparams,omitempty"`
|
||||
Bucket string `json:"bucket,omitempty" bson:"bucket,omitempty"`
|
||||
Prefix string `json:"prefix,omitempty" bson:"prefix,omitempty"`
|
||||
Pattern string `json:"pattern,omitempty" bson:"pattern,omitempty"`
|
||||
CPU string `json:"cpu,omitempty" bson:"cpu,omitempty"`
|
||||
Memory string `json:"memory,omitempty" bson:"memory,omitempty"`
|
||||
}
|
||||
|
||||
type WorkflowNode struct {
|
||||
ID string `json:"id" bson:"id"`
|
||||
Type string `json:"type" bson:"type"`
|
||||
Data WorkflowNodeData `json:"data" bson:"data"`
|
||||
Position Position `json:"position" bson:"position"`
|
||||
}
|
||||
|
||||
type WorkflowEdge struct {
|
||||
ID string `json:"id" bson:"id"`
|
||||
Source string `json:"source" bson:"source"`
|
||||
SourceHandle string `json:"sourceHandle,omitempty" bson:"sourceHandle,omitempty"`
|
||||
Target string `json:"target" bson:"target"`
|
||||
TargetHandle string `json:"targetHandle,omitempty" bson:"targetHandle,omitempty"`
|
||||
Animated bool `json:"animated,omitempty" bson:"animated,omitempty"`
|
||||
}
|
||||
|
||||
type ProjectRecord struct {
|
||||
ID string `json:"id" bson:"id"`
|
||||
Name string `json:"name" bson:"name"`
|
||||
Description string `json:"description" bson:"description"`
|
||||
Status string `json:"status" bson:"status"`
|
||||
WorkflowCount int `json:"workflowCount" bson:"-"`
|
||||
CreatedAt string `json:"createdAt" bson:"createdAt"`
|
||||
UpdatedAt string `json:"updatedAt" bson:"updatedAt"`
|
||||
}
|
||||
|
||||
type OperatorResources struct {
|
||||
CPURequest string `json:"cpuRequest,omitempty" bson:"cpuRequest,omitempty"`
|
||||
CPULimit string `json:"cpuLimit,omitempty" bson:"cpuLimit,omitempty"`
|
||||
MemoryRequest string `json:"memoryRequest,omitempty" bson:"memoryRequest,omitempty"`
|
||||
MemoryLimit string `json:"memoryLimit,omitempty" bson:"memoryLimit,omitempty"`
|
||||
}
|
||||
|
||||
type OperatorRecord struct {
|
||||
ID string `json:"id" bson:"id"`
|
||||
Name string `json:"name" bson:"name"`
|
||||
Type string `json:"type" bson:"type"`
|
||||
Category string `json:"category" bson:"category"`
|
||||
Description string `json:"description" bson:"description"`
|
||||
Inputs []PortDefinition `json:"inputs" bson:"inputs"`
|
||||
Outputs []PortDefinition `json:"outputs" bson:"outputs"`
|
||||
ConfigSchema map[string]any `json:"configSchema,omitempty" bson:"configSchema,omitempty"`
|
||||
Image string `json:"image,omitempty" bson:"image,omitempty"`
|
||||
SkillDefinition string `json:"skillDefinition,omitempty" bson:"skillDefinition,omitempty"`
|
||||
Hyperparams []HyperParam `json:"hyperparams,omitempty" bson:"hyperparams,omitempty"`
|
||||
Resources *OperatorResources `json:"resources,omitempty" bson:"resources,omitempty"`
|
||||
CreatedAt string `json:"createdAt" bson:"createdAt"`
|
||||
UpdatedAt string `json:"updatedAt" bson:"updatedAt"`
|
||||
}
|
||||
|
||||
type WorkflowSchedule struct {
|
||||
Interval int `json:"interval" bson:"interval"`
|
||||
Cron string `json:"cron,omitempty" bson:"cron,omitempty"`
|
||||
SourcePath string `json:"sourcePath,omitempty" bson:"sourcePath,omitempty"`
|
||||
}
|
||||
|
||||
type WorkflowRecord struct {
|
||||
ID string `json:"id" bson:"id"`
|
||||
ProjectID string `json:"projectId" bson:"projectId"`
|
||||
Name string `json:"name" bson:"name"`
|
||||
Description string `json:"description" bson:"description"`
|
||||
Type WorkflowType `json:"type" bson:"type"`
|
||||
MaxConcurrentRuns int `json:"maxConcurrentRuns,omitempty" bson:"maxConcurrentRuns,omitempty"`
|
||||
NodeTimeoutSeconds int `json:"nodeTimeoutSeconds,omitempty" bson:"nodeTimeoutSeconds,omitempty"`
|
||||
RunTimeoutSeconds int `json:"runTimeoutSeconds,omitempty" bson:"runTimeoutSeconds,omitempty"`
|
||||
MaxRetryAttempts int `json:"maxRetryAttempts,omitempty" bson:"maxRetryAttempts,omitempty"`
|
||||
RetryBackoffSeconds int `json:"retryBackoffSeconds,omitempty" bson:"retryBackoffSeconds,omitempty"`
|
||||
Schedule *WorkflowSchedule `json:"schedule,omitempty" bson:"schedule,omitempty"`
|
||||
Nodes []WorkflowNode `json:"nodes" bson:"nodes"`
|
||||
Edges []WorkflowEdge `json:"edges" bson:"edges"`
|
||||
Status WorkflowStatus `json:"status" bson:"status"`
|
||||
LastRunID string `json:"lastRunId,omitempty" bson:"lastRunId,omitempty"`
|
||||
LastRunAt string `json:"lastRunAt,omitempty" bson:"lastRunAt,omitempty"`
|
||||
CreatedAt string `json:"createdAt" bson:"createdAt"`
|
||||
UpdatedAt string `json:"updatedAt" bson:"updatedAt"`
|
||||
}
|
||||
|
||||
type TemplateRecord struct {
|
||||
ID string `json:"id" bson:"id"`
|
||||
Name string `json:"name" bson:"name"`
|
||||
Description string `json:"description" bson:"description"`
|
||||
Type WorkflowType `json:"type" bson:"type"`
|
||||
MaxConcurrentRuns int `json:"maxConcurrentRuns,omitempty" bson:"maxConcurrentRuns,omitempty"`
|
||||
NodeTimeoutSeconds int `json:"nodeTimeoutSeconds,omitempty" bson:"nodeTimeoutSeconds,omitempty"`
|
||||
RunTimeoutSeconds int `json:"runTimeoutSeconds,omitempty" bson:"runTimeoutSeconds,omitempty"`
|
||||
MaxRetryAttempts int `json:"maxRetryAttempts,omitempty" bson:"maxRetryAttempts,omitempty"`
|
||||
RetryBackoffSeconds int `json:"retryBackoffSeconds,omitempty" bson:"retryBackoffSeconds,omitempty"`
|
||||
Schedule *WorkflowSchedule `json:"schedule,omitempty" bson:"schedule,omitempty"`
|
||||
Nodes []WorkflowNode `json:"nodes" bson:"nodes"`
|
||||
Edges []WorkflowEdge `json:"edges" bson:"edges"`
|
||||
CreatedAt string `json:"createdAt" bson:"createdAt"`
|
||||
UpdatedAt string `json:"updatedAt" bson:"updatedAt"`
|
||||
}
|
||||
|
||||
type LogEntryRecord struct {
|
||||
Timestamp string `json:"timestamp" bson:"timestamp"`
|
||||
Level string `json:"level" bson:"level"`
|
||||
Message string `json:"message" bson:"message"`
|
||||
Operator string `json:"operator,omitempty" bson:"operator,omitempty"`
|
||||
}
|
||||
|
||||
type ResourceSample struct {
|
||||
Timestamp string `json:"timestamp" bson:"timestamp"`
|
||||
CPU int `json:"cpu" bson:"cpu"`
|
||||
Memory int `json:"memory" bson:"memory"`
|
||||
GPU int `json:"gpu,omitempty" bson:"gpu,omitempty"`
|
||||
}
|
||||
|
||||
type ArtifactMetadata struct {
|
||||
Duration string `json:"duration,omitempty" bson:"duration,omitempty"`
|
||||
Resolution string `json:"resolution,omitempty" bson:"resolution,omitempty"`
|
||||
Codec string `json:"codec,omitempty" bson:"codec,omitempty"`
|
||||
Thumbnail string `json:"thumbnail,omitempty" bson:"thumbnail,omitempty"`
|
||||
SHA256 string `json:"sha256,omitempty" bson:"sha256,omitempty"`
|
||||
MimeType string `json:"mimeType,omitempty" bson:"mimeType,omitempty"`
|
||||
}
|
||||
|
||||
type ExecutionArtifact struct {
|
||||
ID string `json:"id" bson:"id"`
|
||||
RunID string `json:"runId" bson:"runId"`
|
||||
NodeID string `json:"nodeId" bson:"nodeId"`
|
||||
PortID string `json:"portId,omitempty" bson:"portId,omitempty"`
|
||||
PortName string `json:"portName,omitempty" bson:"portName,omitempty"`
|
||||
Name string `json:"name" bson:"name"`
|
||||
Type PortType `json:"type" bson:"type"`
|
||||
Size string `json:"size" bson:"size"`
|
||||
Path string `json:"path" bson:"path"`
|
||||
Metadata *ArtifactMetadata `json:"metadata,omitempty" bson:"metadata,omitempty"`
|
||||
Data map[string]any `json:"data,omitempty" bson:"data,omitempty"`
|
||||
}
|
||||
|
||||
type NodeExecutionRecord struct {
|
||||
NodeID string `json:"nodeId" bson:"nodeId"`
|
||||
NodeLabel string `json:"nodeLabel" bson:"nodeLabel"`
|
||||
Status NodeExecutionStatus `json:"status" bson:"status"`
|
||||
StartedAt string `json:"startedAt,omitempty" bson:"startedAt,omitempty"`
|
||||
EndedAt string `json:"endedAt,omitempty" bson:"endedAt,omitempty"`
|
||||
Logs []LogEntryRecord `json:"logs" bson:"logs"`
|
||||
Artifacts []ExecutionArtifact `json:"artifacts" bson:"artifacts"`
|
||||
ResourceSeries []ResourceSample `json:"resourceSeries" bson:"resourceSeries"`
|
||||
Error string `json:"error,omitempty" bson:"error,omitempty"`
|
||||
}
|
||||
|
||||
type K8sRef struct {
|
||||
NodeID string `json:"nodeId" bson:"nodeId"`
|
||||
JobName string `json:"jobName" bson:"jobName"`
|
||||
Namespace string `json:"namespace" bson:"namespace"`
|
||||
}
|
||||
|
||||
type RunRecord struct {
|
||||
ID string `json:"id" bson:"id"`
|
||||
WorkflowID string `json:"workflowId" bson:"workflowId"`
|
||||
WorkflowName string `json:"workflowName" bson:"workflowName"`
|
||||
ProjectID string `json:"projectId" bson:"projectId"`
|
||||
ProjectName string `json:"projectName" bson:"projectName"`
|
||||
Trigger RunTrigger `json:"trigger" bson:"trigger"`
|
||||
Attempt int `json:"attempt,omitempty" bson:"attempt,omitempty"`
|
||||
ParentRunID string `json:"parentRunId,omitempty" bson:"parentRunId,omitempty"`
|
||||
AvailableAt string `json:"availableAt,omitempty" bson:"availableAt,omitempty"`
|
||||
Status RunStatus `json:"status" bson:"status"`
|
||||
StartedAt string `json:"startedAt,omitempty" bson:"startedAt,omitempty"`
|
||||
EndedAt string `json:"endedAt,omitempty" bson:"endedAt,omitempty"`
|
||||
DurationMS int64 `json:"durationMs,omitempty" bson:"durationMs,omitempty"`
|
||||
CurrentNodeID string `json:"currentNodeId,omitempty" bson:"currentNodeId,omitempty"`
|
||||
NodeExecutions []NodeExecutionRecord `json:"nodeExecutions" bson:"nodeExecutions"`
|
||||
Logs []LogEntryRecord `json:"logs" bson:"logs"`
|
||||
ResourceSeries []ResourceSample `json:"resourceSeries" bson:"resourceSeries"`
|
||||
Artifacts []ExecutionArtifact `json:"artifacts" bson:"artifacts"`
|
||||
K8sRefs []K8sRef `json:"k8sRefs" bson:"k8sRefs"`
|
||||
Error string `json:"error,omitempty" bson:"error,omitempty"`
|
||||
CreatedAt string `json:"createdAt" bson:"createdAt"`
|
||||
UpdatedAt string `json:"updatedAt" bson:"updatedAt"`
|
||||
}
|
||||
|
||||
type AuditTaskPayload struct {
|
||||
Image string `json:"image" bson:"image"`
|
||||
Confidence float64 `json:"confidence" bson:"confidence"`
|
||||
DetectedObjects []string `json:"detectedObjects" bson:"detectedObjects"`
|
||||
Reason string `json:"reason" bson:"reason"`
|
||||
}
|
||||
|
||||
type AuditTaskRecord struct {
|
||||
ID string `json:"id" bson:"id"`
|
||||
RunID string `json:"runId" bson:"runId"`
|
||||
WorkflowID string `json:"workflowId" bson:"workflowId"`
|
||||
ProjectID string `json:"projectId" bson:"projectId"`
|
||||
NodeID string `json:"nodeId" bson:"nodeId"`
|
||||
WorkflowName string `json:"workflowName" bson:"workflowName"`
|
||||
ProjectName string `json:"projectName" bson:"projectName"`
|
||||
OperatorName string `json:"operatorName" bson:"operatorName"`
|
||||
CreatedAt string `json:"createdAt" bson:"createdAt"`
|
||||
UpdatedAt string `json:"updatedAt" bson:"updatedAt"`
|
||||
Status AuditStatus `json:"status" bson:"status"`
|
||||
Data AuditTaskPayload `json:"data" bson:"data"`
|
||||
}
|
||||
|
||||
type UserRecord struct {
|
||||
ID string `json:"id" bson:"id"`
|
||||
Name string `json:"name" bson:"name"`
|
||||
Email string `json:"email" bson:"email"`
|
||||
Role UserRole `json:"role" bson:"role"`
|
||||
CreatedAt string `json:"createdAt" bson:"createdAt"`
|
||||
UpdatedAt string `json:"updatedAt" bson:"updatedAt"`
|
||||
}
|
||||
|
||||
type K8sSettings struct {
|
||||
Enabled bool `json:"enabled" bson:"enabled"`
|
||||
Endpoint string `json:"endpoint,omitempty" bson:"endpoint,omitempty"`
|
||||
Namespace string `json:"namespace" bson:"namespace"`
|
||||
Kubeconfig string `json:"kubeconfig,omitempty" bson:"kubeconfig,omitempty"`
|
||||
KubeconfigPreview string `json:"kubeconfigPreview,omitempty" bson:"-"`
|
||||
HasKubeconfig bool `json:"hasKubeconfig,omitempty" bson:"-"`
|
||||
}
|
||||
|
||||
type WorkerSettings struct {
|
||||
PollIntervalMS int `json:"pollIntervalMs" bson:"pollIntervalMs"`
|
||||
MaxConcurrentRuns int `json:"maxConcurrentRuns" bson:"maxConcurrentRuns"`
|
||||
}
|
||||
|
||||
type StorageSettings struct {
|
||||
Endpoint string `json:"endpoint" bson:"endpoint"`
|
||||
Buckets []string `json:"buckets" bson:"buckets"`
|
||||
}
|
||||
|
||||
type AISettings struct {
|
||||
Provider string `json:"provider" bson:"provider"`
|
||||
BaseURL string `json:"baseURL" bson:"baseURL"`
|
||||
Model string `json:"model" bson:"model"`
|
||||
APIKey string `json:"apiKey,omitempty" bson:"apiKey,omitempty"`
|
||||
APIKeyPreview string `json:"apiKeyPreview,omitempty" bson:"-"`
|
||||
HasAPIKey bool `json:"hasAPIKey,omitempty" bson:"-"`
|
||||
}
|
||||
|
||||
type DefaultResources struct {
|
||||
CPURequest string `json:"cpuRequest" bson:"cpuRequest"`
|
||||
CPULimit string `json:"cpuLimit" bson:"cpuLimit"`
|
||||
MemoryRequest string `json:"memoryRequest" bson:"memoryRequest"`
|
||||
MemoryLimit string `json:"memoryLimit" bson:"memoryLimit"`
|
||||
}
|
||||
|
||||
type SettingsRecord struct {
|
||||
ID string `json:"id" bson:"id"`
|
||||
K8s K8sSettings `json:"k8s" bson:"k8s"`
|
||||
Worker WorkerSettings `json:"worker" bson:"worker"`
|
||||
Storage StorageSettings `json:"storage" bson:"storage"`
|
||||
AI AISettings `json:"ai" bson:"ai"`
|
||||
Defaults DefaultResources `json:"defaults" bson:"defaults"`
|
||||
Timezone string `json:"timezone" bson:"timezone"`
|
||||
UpdatedAt string `json:"updatedAt" bson:"updatedAt"`
|
||||
}
|
||||
|
||||
type SettingsView struct {
|
||||
ID string `json:"id"`
|
||||
K8s K8sSettings `json:"k8s"`
|
||||
Worker WorkerSettings `json:"worker"`
|
||||
AI AISettings `json:"ai"`
|
||||
Defaults DefaultResources `json:"defaults"`
|
||||
Timezone string `json:"timezone"`
|
||||
UpdatedAt string `json:"updatedAt"`
|
||||
}
|
||||
|
||||
type DashboardSummary struct {
|
||||
Projects struct {
|
||||
Active int `json:"active"`
|
||||
Archived int `json:"archived"`
|
||||
} `json:"projects"`
|
||||
Operators struct {
|
||||
Total int `json:"total"`
|
||||
Custom int `json:"custom"`
|
||||
} `json:"operators"`
|
||||
Jobs struct {
|
||||
Today int `json:"today"`
|
||||
SuccessRate float64 `json:"successRate"`
|
||||
Running int `json:"running"`
|
||||
Failed int `json:"failed"`
|
||||
AverageDurationMS int64 `json:"averageDurationMs"`
|
||||
} `json:"jobs"`
|
||||
Audits struct {
|
||||
Pending int `json:"pending"`
|
||||
} `json:"audits"`
|
||||
}
|
||||
|
||||
type CreateProjectInput struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
type CreateWorkflowInput struct {
|
||||
ProjectID string `json:"projectId"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Type WorkflowType `json:"type"`
|
||||
MaxConcurrentRuns int `json:"maxConcurrentRuns,omitempty"`
|
||||
NodeTimeoutSeconds int `json:"nodeTimeoutSeconds,omitempty"`
|
||||
RunTimeoutSeconds int `json:"runTimeoutSeconds,omitempty"`
|
||||
MaxRetryAttempts int `json:"maxRetryAttempts,omitempty"`
|
||||
RetryBackoffSeconds int `json:"retryBackoffSeconds,omitempty"`
|
||||
Nodes []WorkflowNode `json:"nodes"`
|
||||
Edges []WorkflowEdge `json:"edges"`
|
||||
Schedule *WorkflowSchedule `json:"schedule,omitempty"`
|
||||
}
|
||||
|
||||
type CreateTemplateInput struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Type WorkflowType `json:"type"`
|
||||
MaxConcurrentRuns int `json:"maxConcurrentRuns,omitempty"`
|
||||
NodeTimeoutSeconds int `json:"nodeTimeoutSeconds,omitempty"`
|
||||
RunTimeoutSeconds int `json:"runTimeoutSeconds,omitempty"`
|
||||
MaxRetryAttempts int `json:"maxRetryAttempts,omitempty"`
|
||||
RetryBackoffSeconds int `json:"retryBackoffSeconds,omitempty"`
|
||||
Schedule *WorkflowSchedule `json:"schedule,omitempty"`
|
||||
Nodes []WorkflowNode `json:"nodes"`
|
||||
Edges []WorkflowEdge `json:"edges"`
|
||||
}
|
||||
|
||||
type CreateOperatorInput struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Category string `json:"category"`
|
||||
Description string `json:"description"`
|
||||
Inputs []PortDefinition `json:"inputs"`
|
||||
Outputs []PortDefinition `json:"outputs"`
|
||||
ConfigSchema map[string]any `json:"configSchema,omitempty"`
|
||||
Image string `json:"image,omitempty"`
|
||||
SkillDefinition string `json:"skillDefinition,omitempty"`
|
||||
Hyperparams []HyperParam `json:"hyperparams,omitempty"`
|
||||
Resources *OperatorResources `json:"resources,omitempty"`
|
||||
}
|
||||
|
||||
type CreateUserInput struct {
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
Role UserRole `json:"role"`
|
||||
}
|
||||
83
internal/domain/workflow.go
Normal file
83
internal/domain/workflow.go
Normal file
@ -0,0 +1,83 @@
|
||||
package domain
|
||||
|
||||
import "strings"
|
||||
|
||||
func GetNodeByID(nodes []WorkflowNode, nodeID string) *WorkflowNode {
|
||||
for index := range nodes {
|
||||
if nodes[index].ID == nodeID {
|
||||
return &nodes[index]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetIncomingEdges(edges []WorkflowEdge, nodeID string) []WorkflowEdge {
|
||||
items := make([]WorkflowEdge, 0)
|
||||
for _, edge := range edges {
|
||||
if edge.Target == nodeID {
|
||||
items = append(items, edge)
|
||||
}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func GetOutgoingEdges(edges []WorkflowEdge, nodeID string) []WorkflowEdge {
|
||||
items := make([]WorkflowEdge, 0)
|
||||
for _, edge := range edges {
|
||||
if edge.Source == nodeID {
|
||||
items = append(items, edge)
|
||||
}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func GetStartNodeIDs(nodes []WorkflowNode, edges []WorkflowEdge) []string {
|
||||
items := make([]string, 0)
|
||||
for _, node := range nodes {
|
||||
hasIncoming := false
|
||||
for _, edge := range edges {
|
||||
if edge.Target == node.ID {
|
||||
hasIncoming = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasIncoming {
|
||||
items = append(items, node.ID)
|
||||
}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func auditDecisionMatchesHandle(handle string, decision AuditStatus) bool {
|
||||
if handle == "" {
|
||||
return decision == AuditStatusApproved
|
||||
}
|
||||
normalized := strings.ToLower(handle)
|
||||
switch decision {
|
||||
case AuditStatusApproved:
|
||||
return strings.Contains(normalized, "approved") || strings.Contains(normalized, "通过")
|
||||
case AuditStatusRejected:
|
||||
return strings.Contains(normalized, "rejected") || strings.Contains(normalized, "拒绝")
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func GetDecisionOutgoingEdges(edges []WorkflowEdge, nodeID string, decision AuditStatus) []WorkflowEdge {
|
||||
outgoing := GetOutgoingEdges(edges, nodeID)
|
||||
filtered := make([]WorkflowEdge, 0)
|
||||
for _, edge := range outgoing {
|
||||
if auditDecisionMatchesHandle(edge.SourceHandle, decision) {
|
||||
filtered = append(filtered, edge)
|
||||
}
|
||||
}
|
||||
if len(filtered) > 0 {
|
||||
return filtered
|
||||
}
|
||||
return outgoing
|
||||
}
|
||||
|
||||
func NodeRequiresAudit(node WorkflowNode) bool {
|
||||
label := strings.ToLower(node.Data.Label)
|
||||
return node.Data.Category == "HUMAN" || strings.Contains(label, "human review") || strings.Contains(node.Data.Label, "人工审核")
|
||||
}
|
||||
230
internal/k8s/launcher.go
Normal file
230
internal/k8s/launcher.go
Normal file
@ -0,0 +1,230 @@
|
||||
package k8s
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
batchv1 "k8s.io/api/batch/v1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
)
|
||||
|
||||
type LaunchInput struct {
|
||||
Image string
|
||||
RunID string
|
||||
WorkflowID string
|
||||
NodeID string
|
||||
NodeLabel string
|
||||
TimeoutSeconds int
|
||||
Env map[string]string
|
||||
}
|
||||
|
||||
type JobReference struct {
|
||||
JobName string
|
||||
Namespace string
|
||||
}
|
||||
|
||||
type LaunchResult struct {
|
||||
Reference *JobReference
|
||||
Output string
|
||||
}
|
||||
|
||||
type Launcher struct {
|
||||
enabled bool
|
||||
namespace string
|
||||
imagePullSecretName string
|
||||
clientset kubernetes.Interface
|
||||
}
|
||||
|
||||
var invalidNameCharacters = regexp.MustCompile(`[^a-z0-9-]+`)
|
||||
|
||||
func sanitizeName(value string) string {
|
||||
normalized := strings.ToLower(value)
|
||||
normalized = invalidNameCharacters.ReplaceAllString(normalized, "-")
|
||||
normalized = strings.Trim(normalized, "-")
|
||||
normalized = regexp.MustCompile(`-+`).ReplaceAllString(normalized, "-")
|
||||
if len(normalized) > 50 {
|
||||
return normalized[:50]
|
||||
}
|
||||
return normalized
|
||||
}
|
||||
|
||||
func CreateJobManifest(namespace string, imagePullSecretName string, input LaunchInput) *batchv1.Job {
|
||||
jobPrefix := sanitizeName(input.RunID + "-" + input.NodeID)
|
||||
if len(jobPrefix) > 52 {
|
||||
jobPrefix = jobPrefix[:52]
|
||||
}
|
||||
if jobPrefix == "" {
|
||||
jobPrefix = "droboticflow-node"
|
||||
}
|
||||
envVars := []corev1.EnvVar{
|
||||
{Name: "RUN_ID", Value: input.RunID},
|
||||
{Name: "WORKFLOW_ID", Value: input.WorkflowID},
|
||||
{Name: "NODE_ID", Value: input.NodeID},
|
||||
{Name: "NODE_LABEL", Value: input.NodeLabel},
|
||||
}
|
||||
for key, value := range input.Env {
|
||||
envVars = append(envVars, corev1.EnvVar{Name: key, Value: value})
|
||||
}
|
||||
|
||||
job := &batchv1.Job{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
GenerateName: jobPrefix + "-",
|
||||
Namespace: namespace,
|
||||
Labels: map[string]string{
|
||||
"app": "droboticflow",
|
||||
"component": "operator-runner",
|
||||
"runId": input.RunID,
|
||||
"workflowId": input.WorkflowID,
|
||||
"nodeId": input.NodeID,
|
||||
},
|
||||
},
|
||||
Spec: batchv1.JobSpec{
|
||||
BackoffLimit: func() *int32 { value := int32(0); return &value }(),
|
||||
Template: corev1.PodTemplateSpec{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Labels: map[string]string{
|
||||
"app": "droboticflow",
|
||||
"component": "operator-runner",
|
||||
"runId": input.RunID,
|
||||
},
|
||||
},
|
||||
Spec: corev1.PodSpec{
|
||||
RestartPolicy: corev1.RestartPolicyNever,
|
||||
Containers: []corev1.Container{
|
||||
{
|
||||
Name: "operator-runner",
|
||||
Image: input.Image,
|
||||
ImagePullPolicy: corev1.PullAlways,
|
||||
Env: envVars,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
if input.TimeoutSeconds > 0 {
|
||||
job.Spec.ActiveDeadlineSeconds = func() *int64 {
|
||||
value := int64(input.TimeoutSeconds)
|
||||
return &value
|
||||
}()
|
||||
}
|
||||
if imagePullSecretName != "" {
|
||||
job.Spec.Template.Spec.ImagePullSecrets = []corev1.LocalObjectReference{{Name: imagePullSecretName}}
|
||||
}
|
||||
return job
|
||||
}
|
||||
|
||||
func loadRestConfig() (*rest.Config, error) {
|
||||
if config, err := rest.InClusterConfig(); err == nil {
|
||||
return config, nil
|
||||
}
|
||||
kubeconfig := os.Getenv("KUBECONFIG")
|
||||
if kubeconfig == "" {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
kubeconfig = filepath.Join(home, ".kube", "config")
|
||||
}
|
||||
return clientcmd.BuildConfigFromFlags("", kubeconfig)
|
||||
}
|
||||
|
||||
func NewLauncher(enabled bool, namespace string, imagePullSecretName string) (*Launcher, error) {
|
||||
launcher := &Launcher{
|
||||
enabled: enabled,
|
||||
namespace: namespace,
|
||||
imagePullSecretName: imagePullSecretName,
|
||||
}
|
||||
if !enabled {
|
||||
return launcher, nil
|
||||
}
|
||||
config, err := loadRestConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
clientset, err := kubernetes.NewForConfig(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
launcher.clientset = clientset
|
||||
return launcher, nil
|
||||
}
|
||||
|
||||
func (launcher *Launcher) Enabled() bool {
|
||||
return launcher.enabled
|
||||
}
|
||||
|
||||
func (launcher *Launcher) readJobOutput(ctx context.Context, jobName string) (string, error) {
|
||||
var podName string
|
||||
for attempt := 0; attempt < 30; attempt++ {
|
||||
pods, err := launcher.clientset.CoreV1().Pods(launcher.namespace).List(ctx, metav1.ListOptions{
|
||||
LabelSelector: "job-name=" + jobName,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(pods.Items) > 0 {
|
||||
podName = pods.Items[0].Name
|
||||
break
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
if podName == "" {
|
||||
return "", fmt.Errorf("no pod found for job %s", jobName)
|
||||
}
|
||||
stream, err := launcher.clientset.CoreV1().Pods(launcher.namespace).GetLogs(podName, &corev1.PodLogOptions{}).Stream(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer stream.Close()
|
||||
output, err := io.ReadAll(stream)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(output), nil
|
||||
}
|
||||
|
||||
func (launcher *Launcher) Launch(ctx context.Context, input LaunchInput) (*LaunchResult, error) {
|
||||
if !launcher.enabled {
|
||||
return nil, nil
|
||||
}
|
||||
job := CreateJobManifest(launcher.namespace, launcher.imagePullSecretName, input)
|
||||
created, err := launcher.clientset.BatchV1().Jobs(launcher.namespace).Create(ctx, job, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
jobName := created.Name
|
||||
reference := &JobReference{JobName: jobName, Namespace: launcher.namespace}
|
||||
for attempt := 0; attempt < 300; attempt++ {
|
||||
status, err := launcher.clientset.BatchV1().Jobs(launcher.namespace).Get(ctx, jobName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if status.Status.Succeeded > 0 {
|
||||
output, err := launcher.readJobOutput(ctx, jobName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &LaunchResult{Reference: reference, Output: output}, nil
|
||||
}
|
||||
if status.Status.Failed > 0 {
|
||||
output, outputErr := launcher.readJobOutput(ctx, jobName)
|
||||
if outputErr != nil {
|
||||
return nil, fmt.Errorf("kubernetes job %s failed", jobName)
|
||||
}
|
||||
return nil, fmt.Errorf("kubernetes job %s failed: %s", jobName, strings.TrimSpace(output))
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
return nil, fmt.Errorf("timed out waiting for kubernetes job %s", jobName)
|
||||
}
|
||||
36
internal/k8s/launcher_test.go
Normal file
36
internal/k8s/launcher_test.go
Normal file
@ -0,0 +1,36 @@
|
||||
package k8s
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCreateJobManifest(t *testing.T) {
|
||||
manifest := CreateJobManifest("droboticflow", "droboticflow-registry", LaunchInput{
|
||||
Image: "ccr.example.com/dataflow/operator-runner:latest",
|
||||
RunID: "run-1",
|
||||
WorkflowID: "wf-1",
|
||||
NodeID: "node-1",
|
||||
NodeLabel: "模型推理",
|
||||
Env: map[string]string{
|
||||
"CPU_LIMIT": "500m",
|
||||
"MEMORY_LIMIT": "1Gi",
|
||||
},
|
||||
})
|
||||
|
||||
if manifest.GenerateName == "" || manifest.GenerateName[:5] != "run-1" {
|
||||
t.Fatalf("expected manifest generateName to contain run id, got %s", manifest.GenerateName)
|
||||
}
|
||||
if manifest.Spec.Template.Spec.Containers[0].Image != "ccr.example.com/dataflow/operator-runner:latest" {
|
||||
t.Fatalf("unexpected image: %s", manifest.Spec.Template.Spec.Containers[0].Image)
|
||||
}
|
||||
if len(manifest.Spec.Template.Spec.ImagePullSecrets) == 0 || manifest.Spec.Template.Spec.ImagePullSecrets[0].Name != "droboticflow-registry" {
|
||||
t.Fatalf("expected image pull secret")
|
||||
}
|
||||
foundNodeLabel := false
|
||||
for _, item := range manifest.Spec.Template.Spec.Containers[0].Env {
|
||||
if item.Name == "NODE_LABEL" && item.Value == "模型推理" {
|
||||
foundNodeLabel = true
|
||||
}
|
||||
}
|
||||
if !foundNodeLabel {
|
||||
t.Fatalf("expected NODE_LABEL env var")
|
||||
}
|
||||
}
|
||||
39
internal/retry/retry.go
Normal file
39
internal/retry/retry.go
Normal file
@ -0,0 +1,39 @@
|
||||
package retry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Do[T any](
|
||||
ctx context.Context,
|
||||
attempts int,
|
||||
delay time.Duration,
|
||||
label string,
|
||||
run func(context.Context, int) (T, error),
|
||||
) (T, error) {
|
||||
var zero T
|
||||
var lastErr error
|
||||
|
||||
for attempt := 1; attempt <= attempts; attempt++ {
|
||||
result, err := run(ctx, attempt)
|
||||
if err == nil {
|
||||
return result, nil
|
||||
}
|
||||
lastErr = err
|
||||
if attempt == attempts {
|
||||
break
|
||||
}
|
||||
|
||||
fmt.Printf("[startup] %s attempt %d/%d failed, retrying in %s\n", label, attempt, attempts, delay)
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return zero, ctx.Err()
|
||||
case <-time.After(delay):
|
||||
}
|
||||
}
|
||||
|
||||
return zero, lastErr
|
||||
}
|
||||
42
internal/retry/retry_test.go
Normal file
42
internal/retry/retry_test.go
Normal file
@ -0,0 +1,42 @@
|
||||
package retry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRetryDoRetriesUntilSuccess(t *testing.T) {
|
||||
attempts := 0
|
||||
result, err := Do(context.Background(), 3, 0, "test operation", func(_ context.Context, _ int) (string, error) {
|
||||
attempts++
|
||||
if attempts < 3 {
|
||||
return "", errors.New("not yet")
|
||||
}
|
||||
return "ok", nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("retry returned error: %v", err)
|
||||
}
|
||||
if result != "ok" {
|
||||
t.Fatalf("expected ok, got %s", result)
|
||||
}
|
||||
if attempts != 3 {
|
||||
t.Fatalf("expected 3 attempts, got %d", attempts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRetryDoReturnsLastError(t *testing.T) {
|
||||
attempts := 0
|
||||
_, err := Do(context.Background(), 2, time.Millisecond, "test failure", func(_ context.Context, _ int) (string, error) {
|
||||
attempts++
|
||||
return "", errors.New("boom")
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
if attempts != 2 {
|
||||
t.Fatalf("expected 2 attempts, got %d", attempts)
|
||||
}
|
||||
}
|
||||
5
internal/store/errors.go
Normal file
5
internal/store/errors.go
Normal file
@ -0,0 +1,5 @@
|
||||
package store
|
||||
|
||||
import "errors"
|
||||
|
||||
var ErrWorkflowAlreadyCompleted = errors.New("workflow has completed successfully and cannot be run again manually")
|
||||
265
internal/store/helpers.go
Normal file
265
internal/store/helpers.go
Normal file
@ -0,0 +1,265 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"droboticflow/internal/domain"
|
||||
)
|
||||
|
||||
func nowISO() string {
|
||||
return time.Now().UTC().Format(time.RFC3339Nano)
|
||||
}
|
||||
|
||||
func computeRunDuration(run *domain.RunRecord) {
|
||||
if run.StartedAt == "" || run.EndedAt == "" {
|
||||
return
|
||||
}
|
||||
startedAt, err := time.Parse(time.RFC3339Nano, run.StartedAt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
endedAt, err := time.Parse(time.RFC3339Nano, run.EndedAt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
run.DurationMS = endedAt.Sub(startedAt).Milliseconds()
|
||||
}
|
||||
|
||||
func parseISOTime(value string) (time.Time, bool) {
|
||||
if value == "" {
|
||||
return time.Time{}, false
|
||||
}
|
||||
parsed, err := time.Parse(time.RFC3339Nano, value)
|
||||
if err != nil {
|
||||
return time.Time{}, false
|
||||
}
|
||||
return parsed, true
|
||||
}
|
||||
|
||||
func runAvailableAt(run *domain.RunRecord) time.Time {
|
||||
if run == nil {
|
||||
return time.Time{}
|
||||
}
|
||||
if parsed, ok := parseISOTime(run.AvailableAt); ok {
|
||||
return parsed
|
||||
}
|
||||
if parsed, ok := parseISOTime(run.CreatedAt); ok {
|
||||
return parsed
|
||||
}
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
func runIsAvailable(run *domain.RunRecord, now time.Time) bool {
|
||||
availableAt := runAvailableAt(run)
|
||||
if availableAt.IsZero() {
|
||||
return true
|
||||
}
|
||||
return !availableAt.After(now)
|
||||
}
|
||||
|
||||
func clone[T any](value T) T {
|
||||
data, _ := json.Marshal(value)
|
||||
var out T
|
||||
_ = json.Unmarshal(data, &out)
|
||||
return out
|
||||
}
|
||||
|
||||
func defaultSettings() domain.SettingsRecord {
|
||||
return domain.SettingsRecord{
|
||||
ID: "system",
|
||||
K8s: domain.K8sSettings{
|
||||
Enabled: false,
|
||||
Namespace: "droboticflow",
|
||||
},
|
||||
Worker: domain.WorkerSettings{
|
||||
PollIntervalMS: 2000,
|
||||
MaxConcurrentRuns: 1,
|
||||
},
|
||||
Storage: domain.StorageSettings{
|
||||
Endpoint: "",
|
||||
Buckets: []string{},
|
||||
},
|
||||
AI: domain.AISettings{
|
||||
Provider: "dashscope-openai-compatible",
|
||||
BaseURL: "https://coding.dashscope.aliyuncs.com/v1",
|
||||
Model: "glm-5",
|
||||
},
|
||||
Timezone: "Asia/Shanghai",
|
||||
Defaults: domain.DefaultResources{
|
||||
CPURequest: "500m",
|
||||
CPULimit: "1000m",
|
||||
MemoryRequest: "1Gi",
|
||||
MemoryLimit: "2Gi",
|
||||
},
|
||||
UpdatedAt: nowISO(),
|
||||
}
|
||||
}
|
||||
|
||||
func ensureSettingsDefaults(item domain.SettingsRecord) domain.SettingsRecord {
|
||||
defaults := defaultSettings()
|
||||
if item.ID == "" {
|
||||
item.ID = defaults.ID
|
||||
}
|
||||
if item.K8s.Namespace == "" {
|
||||
item.K8s.Namespace = defaults.K8s.Namespace
|
||||
}
|
||||
if item.Worker.PollIntervalMS <= 0 {
|
||||
item.Worker.PollIntervalMS = defaults.Worker.PollIntervalMS
|
||||
}
|
||||
if item.Worker.MaxConcurrentRuns <= 0 {
|
||||
item.Worker.MaxConcurrentRuns = defaults.Worker.MaxConcurrentRuns
|
||||
}
|
||||
if item.AI.Provider == "" {
|
||||
item.AI.Provider = defaults.AI.Provider
|
||||
}
|
||||
if item.AI.BaseURL == "" {
|
||||
item.AI.BaseURL = defaults.AI.BaseURL
|
||||
}
|
||||
if item.AI.Model == "" {
|
||||
item.AI.Model = defaults.AI.Model
|
||||
}
|
||||
if item.Timezone == "" {
|
||||
item.Timezone = defaults.Timezone
|
||||
}
|
||||
if item.Defaults.CPURequest == "" {
|
||||
item.Defaults.CPURequest = defaults.Defaults.CPURequest
|
||||
}
|
||||
if item.Defaults.CPULimit == "" {
|
||||
item.Defaults.CPULimit = defaults.Defaults.CPULimit
|
||||
}
|
||||
if item.Defaults.MemoryRequest == "" {
|
||||
item.Defaults.MemoryRequest = defaults.Defaults.MemoryRequest
|
||||
}
|
||||
if item.Defaults.MemoryLimit == "" {
|
||||
item.Defaults.MemoryLimit = defaults.Defaults.MemoryLimit
|
||||
}
|
||||
if item.UpdatedAt == "" {
|
||||
item.UpdatedAt = nowISO()
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
func sortByCreatedAtDescending[T interface{ GetCreatedAt() string }](items []T) {
|
||||
sort.Slice(items, func(left int, right int) bool {
|
||||
return items[left].GetCreatedAt() > items[right].GetCreatedAt()
|
||||
})
|
||||
}
|
||||
|
||||
func validateWorkflowProject(project *domain.ProjectRecord, workflowID string) error {
|
||||
if project == nil {
|
||||
return fmt.Errorf("project for workflow %s not found", workflowID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func storageRoot(sourcePath string) string {
|
||||
trimmed := strings.TrimSpace(sourcePath)
|
||||
if trimmed == "" {
|
||||
return ""
|
||||
}
|
||||
if strings.HasPrefix(trimmed, "file://") {
|
||||
return strings.TrimPrefix(trimmed, "file://")
|
||||
}
|
||||
if strings.Contains(trimmed, "://") {
|
||||
return ""
|
||||
}
|
||||
return filepath.Clean(trimmed)
|
||||
}
|
||||
|
||||
func normalizeStorageBucket(bucket string) (string, bool) {
|
||||
trimmed := strings.Trim(strings.TrimSpace(bucket), "/\\")
|
||||
if trimmed == "" {
|
||||
return "", false
|
||||
}
|
||||
if strings.ContainsAny(trimmed, "/\\") || trimmed == "." || trimmed == ".." {
|
||||
return "", false
|
||||
}
|
||||
return trimmed, true
|
||||
}
|
||||
|
||||
func normalizeStoragePrefix(prefix string) (string, string, bool) {
|
||||
trimmed := strings.Trim(strings.TrimSpace(prefix), "/\\")
|
||||
if trimmed == "" {
|
||||
return "", "", true
|
||||
}
|
||||
parts := strings.Split(strings.ReplaceAll(trimmed, "\\", "/"), "/")
|
||||
cleanParts := make([]string, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
switch part {
|
||||
case "", ".":
|
||||
continue
|
||||
case "..":
|
||||
return "", "", false
|
||||
default:
|
||||
cleanParts = append(cleanParts, part)
|
||||
}
|
||||
}
|
||||
if len(cleanParts) == 0 {
|
||||
return "", "", true
|
||||
}
|
||||
return strings.Join(cleanParts, "/"), filepath.Join(cleanParts...), true
|
||||
}
|
||||
|
||||
func listBucketsFromSourcePath(sourcePath string) ([]string, error) {
|
||||
root := storageRoot(sourcePath)
|
||||
if root == "" {
|
||||
return []string{}, nil
|
||||
}
|
||||
entries, err := os.ReadDir(root)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return []string{}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
items := make([]string, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
items = append(items, entry.Name())
|
||||
}
|
||||
}
|
||||
sort.Strings(items)
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func listObjectsFromSourcePath(sourcePath string, bucket string, prefix string) ([]string, error) {
|
||||
root := storageRoot(sourcePath)
|
||||
bucketName, ok := normalizeStorageBucket(bucket)
|
||||
if root == "" || !ok {
|
||||
return []string{}, nil
|
||||
}
|
||||
normalizedPrefix, localPrefix, ok := normalizeStoragePrefix(prefix)
|
||||
if !ok {
|
||||
return []string{}, nil
|
||||
}
|
||||
basePath := filepath.Join(root, bucketName)
|
||||
if localPrefix != "" {
|
||||
basePath = filepath.Join(basePath, localPrefix)
|
||||
}
|
||||
entries, err := os.ReadDir(basePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return []string{}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if normalizedPrefix != "" && !strings.HasSuffix(normalizedPrefix, "/") {
|
||||
normalizedPrefix += "/"
|
||||
}
|
||||
items := make([]string, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
name := normalizedPrefix + entry.Name()
|
||||
if entry.IsDir() {
|
||||
name += "/"
|
||||
}
|
||||
items = append(items, name)
|
||||
}
|
||||
sort.Strings(items)
|
||||
return items, nil
|
||||
}
|
||||
766
internal/store/memory.go
Normal file
766
internal/store/memory.go
Normal file
@ -0,0 +1,766 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"droboticflow/internal/domain"
|
||||
)
|
||||
|
||||
type MemoryStore struct {
|
||||
mu sync.Mutex
|
||||
projects map[string]domain.ProjectRecord
|
||||
operators map[string]domain.OperatorRecord
|
||||
workflows map[string]domain.WorkflowRecord
|
||||
templates map[string]domain.TemplateRecord
|
||||
runs map[string]domain.RunRecord
|
||||
auditTasks map[string]domain.AuditTaskRecord
|
||||
users map[string]domain.UserRecord
|
||||
settings *domain.SettingsRecord
|
||||
}
|
||||
|
||||
func NewMemoryStore() *MemoryStore {
|
||||
return &MemoryStore{
|
||||
projects: map[string]domain.ProjectRecord{},
|
||||
operators: map[string]domain.OperatorRecord{},
|
||||
workflows: map[string]domain.WorkflowRecord{},
|
||||
templates: map[string]domain.TemplateRecord{},
|
||||
runs: map[string]domain.RunRecord{},
|
||||
auditTasks: map[string]domain.AuditTaskRecord{},
|
||||
users: map[string]domain.UserRecord{},
|
||||
}
|
||||
}
|
||||
|
||||
func sortProjects(items []domain.ProjectRecord) {
|
||||
sort.Slice(items, func(left int, right int) bool { return items[left].CreatedAt > items[right].CreatedAt })
|
||||
}
|
||||
|
||||
func sortOperators(items []domain.OperatorRecord) {
|
||||
sort.Slice(items, func(left int, right int) bool { return items[left].CreatedAt > items[right].CreatedAt })
|
||||
}
|
||||
|
||||
func sortWorkflows(items []domain.WorkflowRecord) {
|
||||
sort.Slice(items, func(left int, right int) bool { return items[left].CreatedAt > items[right].CreatedAt })
|
||||
}
|
||||
|
||||
func sortTemplates(items []domain.TemplateRecord) {
|
||||
sort.Slice(items, func(left int, right int) bool { return items[left].CreatedAt > items[right].CreatedAt })
|
||||
}
|
||||
|
||||
func sortRuns(items []domain.RunRecord) {
|
||||
sort.Slice(items, func(left int, right int) bool { return items[left].CreatedAt > items[right].CreatedAt })
|
||||
}
|
||||
|
||||
func sortAuditTasks(items []domain.AuditTaskRecord) {
|
||||
sort.Slice(items, func(left int, right int) bool { return items[left].CreatedAt > items[right].CreatedAt })
|
||||
}
|
||||
|
||||
func sortUsers(items []domain.UserRecord) {
|
||||
sort.Slice(items, func(left int, right int) bool { return items[left].CreatedAt < items[right].CreatedAt })
|
||||
}
|
||||
|
||||
func (store *MemoryStore) ListProjects(context.Context) ([]domain.ProjectRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
items := make([]domain.ProjectRecord, 0, len(store.projects))
|
||||
for _, project := range store.projects {
|
||||
items = append(items, clone(project))
|
||||
}
|
||||
sortProjects(items)
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) GetProject(_ context.Context, id string) (*domain.ProjectRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
project, ok := store.projects[id]
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
cloned := clone(project)
|
||||
return &cloned, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) CreateProject(_ context.Context, input domain.CreateProjectInput) (*domain.ProjectRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
now := nowISO()
|
||||
project := domain.ProjectRecord{
|
||||
ID: domain.NewID("project"),
|
||||
Name: input.Name,
|
||||
Description: input.Description,
|
||||
Status: "active",
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
store.projects[project.ID] = clone(project)
|
||||
cloned := clone(project)
|
||||
return &cloned, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) DeleteProject(_ context.Context, id string) (bool, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
if _, ok := store.projects[id]; !ok {
|
||||
return false, nil
|
||||
}
|
||||
delete(store.projects, id)
|
||||
|
||||
workflowIDs := make([]string, 0)
|
||||
for workflowID, workflow := range store.workflows {
|
||||
if workflow.ProjectID == id {
|
||||
workflowIDs = append(workflowIDs, workflowID)
|
||||
delete(store.workflows, workflowID)
|
||||
}
|
||||
}
|
||||
for runID, run := range store.runs {
|
||||
if run.ProjectID == id {
|
||||
delete(store.runs, runID)
|
||||
continue
|
||||
}
|
||||
for _, workflowID := range workflowIDs {
|
||||
if run.WorkflowID == workflowID {
|
||||
delete(store.runs, runID)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
for taskID, task := range store.auditTasks {
|
||||
if task.ProjectID == id {
|
||||
delete(store.auditTasks, taskID)
|
||||
continue
|
||||
}
|
||||
for _, workflowID := range workflowIDs {
|
||||
if task.WorkflowID == workflowID {
|
||||
delete(store.auditTasks, taskID)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) UpsertProject(_ context.Context, project domain.ProjectRecord) error {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
store.projects[project.ID] = clone(project)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) ListOperators(context.Context) ([]domain.OperatorRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
items := make([]domain.OperatorRecord, 0, len(store.operators))
|
||||
for _, item := range store.operators {
|
||||
items = append(items, clone(item))
|
||||
}
|
||||
sortOperators(items)
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) GetOperator(_ context.Context, id string) (*domain.OperatorRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
item, ok := store.operators[id]
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
cloned := clone(item)
|
||||
return &cloned, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) CreateOperator(_ context.Context, input domain.CreateOperatorInput) (*domain.OperatorRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
now := nowISO()
|
||||
item := domain.OperatorRecord{
|
||||
ID: domain.NewID("operator"),
|
||||
Name: input.Name,
|
||||
Type: input.Type,
|
||||
Category: input.Category,
|
||||
Description: input.Description,
|
||||
Inputs: clone(input.Inputs),
|
||||
Outputs: clone(input.Outputs),
|
||||
ConfigSchema: clone(input.ConfigSchema),
|
||||
Image: input.Image,
|
||||
SkillDefinition: input.SkillDefinition,
|
||||
Hyperparams: clone(input.Hyperparams),
|
||||
Resources: clone(input.Resources),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
store.operators[item.ID] = clone(item)
|
||||
cloned := clone(item)
|
||||
return &cloned, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) DeleteOperator(_ context.Context, id string) (bool, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
if _, ok := store.operators[id]; !ok {
|
||||
return false, nil
|
||||
}
|
||||
delete(store.operators, id)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) UpsertOperator(_ context.Context, item domain.OperatorRecord) error {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
store.operators[item.ID] = clone(item)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) ListWorkflowsByProject(_ context.Context, projectID string) ([]domain.WorkflowRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
items := make([]domain.WorkflowRecord, 0)
|
||||
for _, workflow := range store.workflows {
|
||||
if workflow.ProjectID == projectID {
|
||||
items = append(items, clone(workflow))
|
||||
}
|
||||
}
|
||||
sortWorkflows(items)
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) ListAllWorkflows(context.Context) ([]domain.WorkflowRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
items := make([]domain.WorkflowRecord, 0, len(store.workflows))
|
||||
for _, workflow := range store.workflows {
|
||||
items = append(items, clone(workflow))
|
||||
}
|
||||
sortWorkflows(items)
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) GetWorkflow(_ context.Context, id string) (*domain.WorkflowRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
workflow, ok := store.workflows[id]
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
cloned := clone(workflow)
|
||||
return &cloned, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) CreateWorkflow(_ context.Context, input domain.CreateWorkflowInput) (*domain.WorkflowRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
now := nowISO()
|
||||
item := domain.WorkflowRecord{
|
||||
ID: domain.NewID("workflow"),
|
||||
ProjectID: input.ProjectID,
|
||||
Name: input.Name,
|
||||
Description: input.Description,
|
||||
Type: input.Type,
|
||||
MaxConcurrentRuns: input.MaxConcurrentRuns,
|
||||
NodeTimeoutSeconds: input.NodeTimeoutSeconds,
|
||||
RunTimeoutSeconds: input.RunTimeoutSeconds,
|
||||
MaxRetryAttempts: input.MaxRetryAttempts,
|
||||
RetryBackoffSeconds: input.RetryBackoffSeconds,
|
||||
Schedule: clone(input.Schedule),
|
||||
Nodes: clone(input.Nodes),
|
||||
Edges: clone(input.Edges),
|
||||
Status: domain.WorkflowStatusDraft,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
store.workflows[item.ID] = clone(item)
|
||||
cloned := clone(item)
|
||||
return &cloned, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) DeleteWorkflow(_ context.Context, id string) (bool, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
if _, ok := store.workflows[id]; !ok {
|
||||
return false, nil
|
||||
}
|
||||
delete(store.workflows, id)
|
||||
for runID, run := range store.runs {
|
||||
if run.WorkflowID == id {
|
||||
delete(store.runs, runID)
|
||||
}
|
||||
}
|
||||
for taskID, task := range store.auditTasks {
|
||||
if task.WorkflowID == id {
|
||||
delete(store.auditTasks, taskID)
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) UpsertWorkflow(_ context.Context, workflow domain.WorkflowRecord) error {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
store.workflows[workflow.ID] = clone(workflow)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) ListTemplates(context.Context) ([]domain.TemplateRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
items := make([]domain.TemplateRecord, 0, len(store.templates))
|
||||
for _, item := range store.templates {
|
||||
items = append(items, clone(item))
|
||||
}
|
||||
sortTemplates(items)
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) GetTemplate(_ context.Context, id string) (*domain.TemplateRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
item, ok := store.templates[id]
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
cloned := clone(item)
|
||||
return &cloned, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) CreateTemplate(_ context.Context, input domain.CreateTemplateInput) (*domain.TemplateRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
now := nowISO()
|
||||
templateType := input.Type
|
||||
if templateType == "" {
|
||||
templateType = domain.WorkflowTypeBatch
|
||||
}
|
||||
item := domain.TemplateRecord{
|
||||
ID: domain.NewID("template"),
|
||||
Name: input.Name,
|
||||
Description: input.Description,
|
||||
Type: templateType,
|
||||
MaxConcurrentRuns: input.MaxConcurrentRuns,
|
||||
NodeTimeoutSeconds: input.NodeTimeoutSeconds,
|
||||
RunTimeoutSeconds: input.RunTimeoutSeconds,
|
||||
MaxRetryAttempts: input.MaxRetryAttempts,
|
||||
RetryBackoffSeconds: input.RetryBackoffSeconds,
|
||||
Schedule: clone(input.Schedule),
|
||||
Nodes: clone(input.Nodes),
|
||||
Edges: clone(input.Edges),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
store.templates[item.ID] = clone(item)
|
||||
cloned := clone(item)
|
||||
return &cloned, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) DeleteTemplate(_ context.Context, id string) (bool, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
if _, ok := store.templates[id]; !ok {
|
||||
return false, nil
|
||||
}
|
||||
delete(store.templates, id)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) UpsertTemplate(_ context.Context, item domain.TemplateRecord) error {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
store.templates[item.ID] = clone(item)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) ListRuns(_ context.Context, status string) ([]domain.RunRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
items := make([]domain.RunRecord, 0)
|
||||
for _, run := range store.runs {
|
||||
if status == "" || string(run.Status) == status {
|
||||
items = append(items, clone(run))
|
||||
}
|
||||
}
|
||||
sortRuns(items)
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) GetRun(_ context.Context, id string) (*domain.RunRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
run, ok := store.runs[id]
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
cloned := clone(run)
|
||||
return &cloned, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) CreateRun(_ context.Context, workflowID string, trigger domain.RunTrigger) (*domain.RunRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
workflow, ok := store.workflows[workflowID]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("workflow %s not found", workflowID)
|
||||
}
|
||||
if workflow.Status == domain.WorkflowStatusCompleted && trigger != domain.RunTriggerScheduled {
|
||||
return nil, ErrWorkflowAlreadyCompleted
|
||||
}
|
||||
project, ok := store.projects[workflow.ProjectID]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("project %s not found", workflow.ProjectID)
|
||||
}
|
||||
|
||||
now := nowISO()
|
||||
run := domain.RunRecord{
|
||||
ID: domain.NewID("run"),
|
||||
WorkflowID: workflow.ID,
|
||||
WorkflowName: workflow.Name,
|
||||
ProjectID: project.ID,
|
||||
ProjectName: project.Name,
|
||||
Trigger: trigger,
|
||||
Attempt: 1,
|
||||
AvailableAt: now,
|
||||
Status: domain.RunStatusQueued,
|
||||
NodeExecutions: []domain.NodeExecutionRecord{},
|
||||
Logs: []domain.LogEntryRecord{},
|
||||
ResourceSeries: []domain.ResourceSample{},
|
||||
Artifacts: []domain.ExecutionArtifact{},
|
||||
K8sRefs: []domain.K8sRef{},
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
store.runs[run.ID] = clone(run)
|
||||
|
||||
workflow.LastRunID = run.ID
|
||||
workflow.LastRunAt = now
|
||||
workflow.Status = domain.WorkflowStatusRunning
|
||||
workflow.UpdatedAt = now
|
||||
store.workflows[workflow.ID] = workflow
|
||||
|
||||
cloned := clone(run)
|
||||
return &cloned, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) ClaimNextQueuedRun(_ context.Context) (*domain.RunRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
var selected *domain.RunRecord
|
||||
now := time.Now().UTC()
|
||||
for _, run := range store.runs {
|
||||
if run.Status != domain.RunStatusQueued {
|
||||
continue
|
||||
}
|
||||
candidate := clone(run)
|
||||
if !runIsAvailable(&candidate, now) {
|
||||
continue
|
||||
}
|
||||
if selected == nil || candidate.CreatedAt < selected.CreatedAt {
|
||||
selected = &candidate
|
||||
}
|
||||
}
|
||||
if selected == nil {
|
||||
return nil, nil
|
||||
}
|
||||
selected.Status = domain.RunStatusRunning
|
||||
selected.StartedAt = nowISO()
|
||||
selected.UpdatedAt = nowISO()
|
||||
computeRunDuration(selected)
|
||||
store.runs[selected.ID] = clone(*selected)
|
||||
cloned := clone(*selected)
|
||||
return &cloned, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) ClaimQueuedRun(_ context.Context, runID string) (*domain.RunRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
run, ok := store.runs[runID]
|
||||
if !ok || run.Status != domain.RunStatusQueued {
|
||||
return nil, nil
|
||||
}
|
||||
if !runIsAvailable(&run, time.Now().UTC()) {
|
||||
return nil, nil
|
||||
}
|
||||
run.Status = domain.RunStatusRunning
|
||||
run.StartedAt = nowISO()
|
||||
run.UpdatedAt = nowISO()
|
||||
computeRunDuration(&run)
|
||||
store.runs[run.ID] = clone(run)
|
||||
cloned := clone(run)
|
||||
return &cloned, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) UpsertRun(_ context.Context, run domain.RunRecord) error {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
computeRunDuration(&run)
|
||||
store.runs[run.ID] = clone(run)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) ListAuditTasks(_ context.Context, status string) ([]domain.AuditTaskRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
items := make([]domain.AuditTaskRecord, 0)
|
||||
for _, task := range store.auditTasks {
|
||||
if status == "" || status == "all" || string(task.Status) == status {
|
||||
items = append(items, clone(task))
|
||||
}
|
||||
}
|
||||
sortAuditTasks(items)
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) GetAuditTask(_ context.Context, id string) (*domain.AuditTaskRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
task, ok := store.auditTasks[id]
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
cloned := clone(task)
|
||||
return &cloned, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) CreateAuditTask(_ context.Context, task domain.AuditTaskRecord) (*domain.AuditTaskRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
store.auditTasks[task.ID] = clone(task)
|
||||
cloned := clone(task)
|
||||
return &cloned, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) UpsertAuditTask(_ context.Context, task domain.AuditTaskRecord) error {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
store.auditTasks[task.ID] = clone(task)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) ListUsers(context.Context) ([]domain.UserRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
items := make([]domain.UserRecord, 0, len(store.users))
|
||||
for _, item := range store.users {
|
||||
items = append(items, clone(item))
|
||||
}
|
||||
sortUsers(items)
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) GetUser(_ context.Context, id string) (*domain.UserRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
item, ok := store.users[id]
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
cloned := clone(item)
|
||||
return &cloned, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) CreateUser(_ context.Context, input domain.CreateUserInput) (*domain.UserRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
now := nowISO()
|
||||
user := domain.UserRecord{
|
||||
ID: domain.NewID("user"),
|
||||
Name: input.Name,
|
||||
Email: input.Email,
|
||||
Role: input.Role,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
store.users[user.ID] = clone(user)
|
||||
cloned := clone(user)
|
||||
return &cloned, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) DeleteUser(_ context.Context, id string) (bool, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
if _, ok := store.users[id]; !ok {
|
||||
return false, nil
|
||||
}
|
||||
delete(store.users, id)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) UpsertUser(_ context.Context, item domain.UserRecord) error {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
store.users[item.ID] = clone(item)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) GetSettings(_ context.Context) (*domain.SettingsRecord, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
if store.settings == nil {
|
||||
defaultValue := defaultSettings()
|
||||
store.settings = &defaultValue
|
||||
}
|
||||
cloned := ensureSettingsDefaults(clone(*store.settings))
|
||||
return &cloned, nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) UpsertSettings(_ context.Context, item domain.SettingsRecord) error {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
cloned := ensureSettingsDefaults(clone(item))
|
||||
store.settings = &cloned
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *MemoryStore) GetDashboardSummary(ctx context.Context) (domain.DashboardSummary, error) {
|
||||
projects, err := store.ListProjects(ctx)
|
||||
if err != nil {
|
||||
return domain.DashboardSummary{}, err
|
||||
}
|
||||
operators, err := store.ListOperators(ctx)
|
||||
if err != nil {
|
||||
return domain.DashboardSummary{}, err
|
||||
}
|
||||
runs, err := store.ListRuns(ctx, "")
|
||||
if err != nil {
|
||||
return domain.DashboardSummary{}, err
|
||||
}
|
||||
auditTasks, err := store.ListAuditTasks(ctx, "all")
|
||||
if err != nil {
|
||||
return domain.DashboardSummary{}, err
|
||||
}
|
||||
|
||||
return buildDashboardSummary(projects, operators, runs, auditTasks), nil
|
||||
}
|
||||
|
||||
func buildDashboardSummary(
|
||||
projects []domain.ProjectRecord,
|
||||
operators []domain.OperatorRecord,
|
||||
runs []domain.RunRecord,
|
||||
auditTasks []domain.AuditTaskRecord,
|
||||
) domain.DashboardSummary {
|
||||
completedRuns := make([]domain.RunRecord, 0)
|
||||
failedRuns := make([]domain.RunRecord, 0)
|
||||
runningRuns := make([]domain.RunRecord, 0)
|
||||
for _, run := range runs {
|
||||
switch run.Status {
|
||||
case domain.RunStatusCompleted:
|
||||
completedRuns = append(completedRuns, run)
|
||||
case domain.RunStatusFailed:
|
||||
failedRuns = append(failedRuns, run)
|
||||
case domain.RunStatusRunning:
|
||||
runningRuns = append(runningRuns, run)
|
||||
}
|
||||
}
|
||||
|
||||
var durations []int64
|
||||
for _, run := range completedRuns {
|
||||
if run.DurationMS > 0 {
|
||||
durations = append(durations, run.DurationMS)
|
||||
}
|
||||
}
|
||||
var average int64
|
||||
if len(durations) > 0 {
|
||||
var total int64
|
||||
for _, duration := range durations {
|
||||
total += duration
|
||||
}
|
||||
average = total / int64(len(durations))
|
||||
}
|
||||
|
||||
var summary domain.DashboardSummary
|
||||
for _, project := range projects {
|
||||
if project.Status == "active" {
|
||||
summary.Projects.Active++
|
||||
}
|
||||
if project.Status == "archived" {
|
||||
summary.Projects.Archived++
|
||||
}
|
||||
}
|
||||
summary.Operators.Total = len(operators)
|
||||
for _, item := range operators {
|
||||
if item.Type == "custom" {
|
||||
summary.Operators.Custom++
|
||||
}
|
||||
}
|
||||
totalFinished := len(completedRuns) + len(failedRuns)
|
||||
summary.Jobs.Today = len(runs)
|
||||
if totalFinished == 0 {
|
||||
summary.Jobs.SuccessRate = 100
|
||||
} else {
|
||||
summary.Jobs.SuccessRate = float64(len(completedRuns)) * 100 / float64(totalFinished)
|
||||
}
|
||||
summary.Jobs.Running = len(runningRuns)
|
||||
summary.Jobs.Failed = len(failedRuns)
|
||||
summary.Jobs.AverageDurationMS = average
|
||||
for _, task := range auditTasks {
|
||||
if task.Status == domain.AuditStatusPending {
|
||||
summary.Audits.Pending++
|
||||
}
|
||||
}
|
||||
return summary
|
||||
}
|
||||
|
||||
func (store *MemoryStore) ListStorageBuckets(ctx context.Context, sourcePath string) ([]string, error) {
|
||||
return listBucketsFromSourcePath(sourcePath)
|
||||
}
|
||||
|
||||
func (store *MemoryStore) ListStorageObjects(ctx context.Context, sourcePath string, bucket string, prefix string) ([]string, error) {
|
||||
return listObjectsFromSourcePath(sourcePath, bucket, prefix)
|
||||
}
|
||||
|
||||
func MergeSettings(current domain.SettingsRecord, patch domain.SettingsRecord) domain.SettingsRecord {
|
||||
merged := current
|
||||
if patch.K8s.Namespace != "" {
|
||||
merged.K8s.Namespace = patch.K8s.Namespace
|
||||
}
|
||||
if patch.K8s.Endpoint != "" {
|
||||
merged.K8s.Endpoint = patch.K8s.Endpoint
|
||||
}
|
||||
if patch.K8s.Kubeconfig != "" {
|
||||
merged.K8s.Kubeconfig = patch.K8s.Kubeconfig
|
||||
}
|
||||
if patch.K8s.Enabled != current.K8s.Enabled {
|
||||
merged.K8s.Enabled = patch.K8s.Enabled
|
||||
}
|
||||
if patch.Worker.PollIntervalMS > 0 {
|
||||
merged.Worker.PollIntervalMS = patch.Worker.PollIntervalMS
|
||||
}
|
||||
if patch.Worker.MaxConcurrentRuns > 0 {
|
||||
merged.Worker.MaxConcurrentRuns = patch.Worker.MaxConcurrentRuns
|
||||
}
|
||||
if patch.Storage.Endpoint != "" {
|
||||
merged.Storage.Endpoint = patch.Storage.Endpoint
|
||||
}
|
||||
if len(patch.Storage.Buckets) > 0 {
|
||||
merged.Storage.Buckets = clone(patch.Storage.Buckets)
|
||||
}
|
||||
if patch.Defaults.CPURequest != "" {
|
||||
merged.Defaults.CPURequest = patch.Defaults.CPURequest
|
||||
}
|
||||
if patch.Defaults.CPULimit != "" {
|
||||
merged.Defaults.CPULimit = patch.Defaults.CPULimit
|
||||
}
|
||||
if patch.Defaults.MemoryRequest != "" {
|
||||
merged.Defaults.MemoryRequest = patch.Defaults.MemoryRequest
|
||||
}
|
||||
if patch.Defaults.MemoryLimit != "" {
|
||||
merged.Defaults.MemoryLimit = patch.Defaults.MemoryLimit
|
||||
}
|
||||
if patch.Timezone != "" {
|
||||
merged.Timezone = patch.Timezone
|
||||
}
|
||||
merged.UpdatedAt = nowISO()
|
||||
return merged
|
||||
}
|
||||
|
||||
func CopyRunWithUpdate(run domain.RunRecord) domain.RunRecord {
|
||||
copy := clone(run)
|
||||
computeRunDuration(©)
|
||||
copy.UpdatedAt = nowISO()
|
||||
return copy
|
||||
}
|
||||
|
||||
func NormalizeStatusFilter(status string) string {
|
||||
return strings.TrimSpace(status)
|
||||
}
|
||||
277
internal/store/memory_test.go
Normal file
277
internal/store/memory_test.go
Normal file
@ -0,0 +1,277 @@
|
||||
package store_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"droboticflow/internal/domain"
|
||||
"droboticflow/internal/store"
|
||||
)
|
||||
|
||||
func TestMemoryStoreCRUD(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
dataStore := store.NewMemoryStore()
|
||||
|
||||
storageRoot := t.TempDir()
|
||||
for _, bucket := range []string{"incoming", "processed"} {
|
||||
if err := os.Mkdir(filepath.Join(storageRoot, bucket), 0o755); err != nil {
|
||||
t.Fatalf("create bucket %s: %v", bucket, err)
|
||||
}
|
||||
}
|
||||
|
||||
settings, err := dataStore.GetSettings(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("get settings: %v", err)
|
||||
}
|
||||
if err := dataStore.UpsertSettings(ctx, *settings); err != nil {
|
||||
t.Fatalf("update settings: %v", err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(storageRoot, "incoming", "clips"), 0o755); err != nil {
|
||||
t.Fatalf("create nested prefix: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(storageRoot, "incoming", "clips", "sample.mp4"), []byte("data"), 0o644); err != nil {
|
||||
t.Fatalf("write sample object: %v", err)
|
||||
}
|
||||
|
||||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||||
Name: "新项目",
|
||||
Description: "用于仓储测试",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create project: %v", err)
|
||||
}
|
||||
if project.ID == "" {
|
||||
t.Fatalf("expected created project id")
|
||||
}
|
||||
|
||||
operator, err := dataStore.CreateOperator(ctx, domain.CreateOperatorInput{
|
||||
Name: "真实数据源",
|
||||
Type: "builtin",
|
||||
Category: "IO",
|
||||
Description: "Emit a manifest artifact from source text",
|
||||
Inputs: []domain.PortDefinition{},
|
||||
Outputs: []domain.PortDefinition{
|
||||
{ID: "out-1", Name: "manifest", Type: domain.PortTypeJSON},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create operator: %v", err)
|
||||
}
|
||||
if operator.ID == "" {
|
||||
t.Fatalf("expected created operator id")
|
||||
}
|
||||
|
||||
template, err := dataStore.CreateTemplate(ctx, domain.CreateTemplateInput{
|
||||
Name: "基础模板",
|
||||
Description: "用于模板列表测试",
|
||||
Nodes: []domain.WorkflowNode{
|
||||
{
|
||||
ID: "node-1",
|
||||
Type: "operator",
|
||||
Data: domain.WorkflowNodeData{
|
||||
Label: "真实数据源",
|
||||
Category: "IO",
|
||||
Description: "source",
|
||||
Outputs: []domain.PortDefinition{
|
||||
{ID: "out-1", Name: "manifest", Type: domain.PortTypeJSON},
|
||||
},
|
||||
},
|
||||
Position: domain.Position{X: 0, Y: 0},
|
||||
},
|
||||
},
|
||||
Edges: []domain.WorkflowEdge{},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create template: %v", err)
|
||||
}
|
||||
if template.ID == "" {
|
||||
t.Fatalf("expected created template id")
|
||||
}
|
||||
|
||||
user, err := dataStore.CreateUser(ctx, domain.CreateUserInput{
|
||||
Name: "测试用户",
|
||||
Email: "tester@example.com",
|
||||
Role: domain.UserRoleAdmin,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create user: %v", err)
|
||||
}
|
||||
if user.ID == "" {
|
||||
t.Fatalf("expected created user id")
|
||||
}
|
||||
|
||||
projects, err := dataStore.ListProjects(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("list projects: %v", err)
|
||||
}
|
||||
if len(projects) != 1 {
|
||||
t.Fatalf("expected 1 project, got %d", len(projects))
|
||||
}
|
||||
|
||||
operators, err := dataStore.ListOperators(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("list operators: %v", err)
|
||||
}
|
||||
if len(operators) != 1 {
|
||||
t.Fatalf("expected 1 operator, got %d", len(operators))
|
||||
}
|
||||
|
||||
templates, err := dataStore.ListTemplates(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("list templates: %v", err)
|
||||
}
|
||||
if len(templates) != 1 {
|
||||
t.Fatalf("expected 1 template, got %d", len(templates))
|
||||
}
|
||||
|
||||
users, err := dataStore.ListUsers(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("list users: %v", err)
|
||||
}
|
||||
if len(users) != 1 {
|
||||
t.Fatalf("expected 1 user, got %d", len(users))
|
||||
}
|
||||
|
||||
buckets, err := dataStore.ListStorageBuckets(ctx, storageRoot)
|
||||
if err != nil {
|
||||
t.Fatalf("list storage buckets: %v", err)
|
||||
}
|
||||
if len(buckets) != 2 {
|
||||
t.Fatalf("expected 2 buckets, got %d", len(buckets))
|
||||
}
|
||||
|
||||
objects, err := dataStore.ListStorageObjects(ctx, storageRoot, "incoming", "clips")
|
||||
if err != nil {
|
||||
t.Fatalf("list storage objects: %v", err)
|
||||
}
|
||||
if len(objects) != 1 || objects[0] != "clips/sample.mp4" {
|
||||
t.Fatalf("unexpected storage objects: %#v", objects)
|
||||
}
|
||||
|
||||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||||
ProjectID: project.ID,
|
||||
Name: "测试工作流",
|
||||
Description: "测试 DAG",
|
||||
Type: domain.WorkflowTypeBatch,
|
||||
Nodes: []domain.WorkflowNode{
|
||||
{
|
||||
ID: "n1",
|
||||
Type: "operator",
|
||||
Data: domain.WorkflowNodeData{
|
||||
Label: "真实数据源",
|
||||
Category: "IO",
|
||||
Description: "source",
|
||||
Inputs: []domain.PortDefinition{},
|
||||
Outputs: []domain.PortDefinition{
|
||||
{ID: "out-1", Name: "manifest", Type: domain.PortTypeJSON},
|
||||
},
|
||||
},
|
||||
Position: domain.Position{X: 0, Y: 0},
|
||||
},
|
||||
},
|
||||
Edges: []domain.WorkflowEdge{},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create workflow: %v", err)
|
||||
}
|
||||
if workflow.ProjectID != project.ID {
|
||||
t.Fatalf("expected workflow project %s, got %s", project.ID, workflow.ProjectID)
|
||||
}
|
||||
|
||||
projectWorkflows, err := dataStore.ListWorkflowsByProject(ctx, project.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("list project workflows: %v", err)
|
||||
}
|
||||
if len(projectWorkflows) != 1 {
|
||||
t.Fatalf("expected workflow in project listing")
|
||||
}
|
||||
|
||||
project.Description = "已更新描述"
|
||||
project.UpdatedAt = time.Now().UTC().Format(time.RFC3339Nano)
|
||||
if err := dataStore.UpsertProject(ctx, *project); err != nil {
|
||||
t.Fatalf("update project: %v", err)
|
||||
}
|
||||
|
||||
updatedProject, err := dataStore.GetProject(ctx, project.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("get updated project: %v", err)
|
||||
}
|
||||
if updatedProject == nil || updatedProject.Description != "已更新描述" {
|
||||
t.Fatalf("expected updated description")
|
||||
}
|
||||
|
||||
deleted, err := dataStore.DeleteProject(ctx, project.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("delete project: %v", err)
|
||||
}
|
||||
if !deleted {
|
||||
t.Fatalf("expected project deleted")
|
||||
}
|
||||
|
||||
deletedProject, err := dataStore.GetProject(ctx, project.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("get deleted project: %v", err)
|
||||
}
|
||||
if deletedProject != nil {
|
||||
t.Fatalf("expected deleted project to be nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompletedWorkflowCannotBeRunAgainManually(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
dataStore := store.NewMemoryStore()
|
||||
|
||||
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{
|
||||
Name: "只读项目",
|
||||
Description: "completed workflow guard",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create project: %v", err)
|
||||
}
|
||||
|
||||
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
||||
ProjectID: project.ID,
|
||||
Name: "已完成工作流",
|
||||
Description: "completed",
|
||||
Type: domain.WorkflowTypeBatch,
|
||||
Nodes: []domain.WorkflowNode{
|
||||
{
|
||||
ID: "node-1",
|
||||
Type: "operator",
|
||||
Data: domain.WorkflowNodeData{
|
||||
Label: "真实数据源",
|
||||
Category: "IO",
|
||||
Description: "source",
|
||||
},
|
||||
Position: domain.Position{X: 0, Y: 0},
|
||||
},
|
||||
},
|
||||
Edges: []domain.WorkflowEdge{},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("create workflow: %v", err)
|
||||
}
|
||||
|
||||
workflow.Status = domain.WorkflowStatusCompleted
|
||||
if err := dataStore.UpsertWorkflow(ctx, *workflow); err != nil {
|
||||
t.Fatalf("mark workflow completed: %v", err)
|
||||
}
|
||||
|
||||
if _, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual); err == nil {
|
||||
t.Fatalf("expected manual rerun to be rejected")
|
||||
} else if !errors.Is(err, store.ErrWorkflowAlreadyCompleted) {
|
||||
t.Fatalf("expected ErrWorkflowAlreadyCompleted, got %v", err)
|
||||
}
|
||||
|
||||
scheduledRun, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerScheduled)
|
||||
if err != nil {
|
||||
t.Fatalf("expected scheduled run to remain allowed, got %v", err)
|
||||
}
|
||||
if scheduledRun == nil || scheduledRun.Status != domain.RunStatusQueued {
|
||||
t.Fatalf("expected scheduled run queued, got %#v", scheduledRun)
|
||||
}
|
||||
}
|
||||
606
internal/store/mongo.go
Normal file
606
internal/store/mongo.go
Normal file
@ -0,0 +1,606 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"droboticflow/internal/domain"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type MongoStore struct {
|
||||
db *mongo.Database
|
||||
projects *mongo.Collection
|
||||
operators *mongo.Collection
|
||||
workflows *mongo.Collection
|
||||
templates *mongo.Collection
|
||||
runs *mongo.Collection
|
||||
auditTasks *mongo.Collection
|
||||
users *mongo.Collection
|
||||
settings *mongo.Collection
|
||||
}
|
||||
|
||||
func NewMongoStore(db *mongo.Database) *MongoStore {
|
||||
return &MongoStore{
|
||||
db: db,
|
||||
projects: db.Collection("projects"),
|
||||
operators: db.Collection("operators"),
|
||||
workflows: db.Collection("workflows"),
|
||||
templates: db.Collection("templates"),
|
||||
runs: db.Collection("runs"),
|
||||
auditTasks: db.Collection("auditTasks"),
|
||||
users: db.Collection("users"),
|
||||
settings: db.Collection("settings"),
|
||||
}
|
||||
}
|
||||
|
||||
func decodeMany[T any](ctx context.Context, cursor *mongo.Cursor) ([]T, error) {
|
||||
defer cursor.Close(ctx)
|
||||
items := make([]T, 0)
|
||||
for cursor.Next(ctx) {
|
||||
var item T
|
||||
if err := cursor.Decode(&item); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
return items, cursor.Err()
|
||||
}
|
||||
|
||||
func (store *MongoStore) ListProjects(ctx context.Context) ([]domain.ProjectRecord, error) {
|
||||
cursor, err := store.projects.Find(ctx, bson.M{}, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return decodeMany[domain.ProjectRecord](ctx, cursor)
|
||||
}
|
||||
|
||||
func (store *MongoStore) GetProject(ctx context.Context, id string) (*domain.ProjectRecord, error) {
|
||||
var item domain.ProjectRecord
|
||||
err := store.projects.FindOne(ctx, bson.M{"id": id}).Decode(&item)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) CreateProject(ctx context.Context, input domain.CreateProjectInput) (*domain.ProjectRecord, error) {
|
||||
now := nowISO()
|
||||
item := domain.ProjectRecord{
|
||||
ID: domain.NewID("project"),
|
||||
Name: input.Name,
|
||||
Description: input.Description,
|
||||
Status: "active",
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if err := store.UpsertProject(ctx, item); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) DeleteProject(ctx context.Context, id string) (bool, error) {
|
||||
workflowCursor, err := store.workflows.Find(ctx, bson.M{"projectId": id}, options.Find().SetProjection(bson.M{"id": 1}))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
workflowDocs, err := decodeMany[struct {
|
||||
ID string `bson:"id"`
|
||||
}](ctx, workflowCursor)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
workflowIDs := make([]string, 0, len(workflowDocs))
|
||||
for _, item := range workflowDocs {
|
||||
workflowIDs = append(workflowIDs, item.ID)
|
||||
}
|
||||
|
||||
if _, err := store.workflows.DeleteMany(ctx, bson.M{"projectId": id}); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err := store.runs.DeleteMany(ctx, bson.M{"projectId": id}); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err := store.auditTasks.DeleteMany(ctx, bson.M{"projectId": id}); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if len(workflowIDs) > 0 {
|
||||
if _, err := store.runs.DeleteMany(ctx, bson.M{"workflowId": bson.M{"$in": workflowIDs}}); err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
result, err := store.projects.DeleteOne(ctx, bson.M{"id": id})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return result.DeletedCount == 1, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) UpsertProject(ctx context.Context, item domain.ProjectRecord) error {
|
||||
_, err := store.projects.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
||||
return err
|
||||
}
|
||||
|
||||
func (store *MongoStore) ListOperators(ctx context.Context) ([]domain.OperatorRecord, error) {
|
||||
cursor, err := store.operators.Find(ctx, bson.M{}, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return decodeMany[domain.OperatorRecord](ctx, cursor)
|
||||
}
|
||||
|
||||
func (store *MongoStore) GetOperator(ctx context.Context, id string) (*domain.OperatorRecord, error) {
|
||||
var item domain.OperatorRecord
|
||||
err := store.operators.FindOne(ctx, bson.M{"id": id}).Decode(&item)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) CreateOperator(ctx context.Context, input domain.CreateOperatorInput) (*domain.OperatorRecord, error) {
|
||||
now := nowISO()
|
||||
item := domain.OperatorRecord{
|
||||
ID: domain.NewID("operator"),
|
||||
Name: input.Name,
|
||||
Type: input.Type,
|
||||
Category: input.Category,
|
||||
Description: input.Description,
|
||||
Inputs: input.Inputs,
|
||||
Outputs: input.Outputs,
|
||||
ConfigSchema: input.ConfigSchema,
|
||||
Image: input.Image,
|
||||
SkillDefinition: input.SkillDefinition,
|
||||
Hyperparams: input.Hyperparams,
|
||||
Resources: input.Resources,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if err := store.UpsertOperator(ctx, item); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) DeleteOperator(ctx context.Context, id string) (bool, error) {
|
||||
result, err := store.operators.DeleteOne(ctx, bson.M{"id": id})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return result.DeletedCount == 1, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) UpsertOperator(ctx context.Context, item domain.OperatorRecord) error {
|
||||
_, err := store.operators.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
||||
return err
|
||||
}
|
||||
|
||||
func (store *MongoStore) ListWorkflowsByProject(ctx context.Context, projectID string) ([]domain.WorkflowRecord, error) {
|
||||
cursor, err := store.workflows.Find(ctx, bson.M{"projectId": projectID}, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return decodeMany[domain.WorkflowRecord](ctx, cursor)
|
||||
}
|
||||
|
||||
func (store *MongoStore) ListAllWorkflows(ctx context.Context) ([]domain.WorkflowRecord, error) {
|
||||
cursor, err := store.workflows.Find(ctx, bson.M{}, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return decodeMany[domain.WorkflowRecord](ctx, cursor)
|
||||
}
|
||||
|
||||
func (store *MongoStore) GetWorkflow(ctx context.Context, id string) (*domain.WorkflowRecord, error) {
|
||||
var item domain.WorkflowRecord
|
||||
err := store.workflows.FindOne(ctx, bson.M{"id": id}).Decode(&item)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) CreateWorkflow(ctx context.Context, input domain.CreateWorkflowInput) (*domain.WorkflowRecord, error) {
|
||||
now := nowISO()
|
||||
item := domain.WorkflowRecord{
|
||||
ID: domain.NewID("workflow"),
|
||||
ProjectID: input.ProjectID,
|
||||
Name: input.Name,
|
||||
Description: input.Description,
|
||||
Type: input.Type,
|
||||
MaxConcurrentRuns: input.MaxConcurrentRuns,
|
||||
NodeTimeoutSeconds: input.NodeTimeoutSeconds,
|
||||
RunTimeoutSeconds: input.RunTimeoutSeconds,
|
||||
MaxRetryAttempts: input.MaxRetryAttempts,
|
||||
RetryBackoffSeconds: input.RetryBackoffSeconds,
|
||||
Schedule: input.Schedule,
|
||||
Nodes: input.Nodes,
|
||||
Edges: input.Edges,
|
||||
Status: domain.WorkflowStatusDraft,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if err := store.UpsertWorkflow(ctx, item); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) DeleteWorkflow(ctx context.Context, id string) (bool, error) {
|
||||
if _, err := store.runs.DeleteMany(ctx, bson.M{"workflowId": id}); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err := store.auditTasks.DeleteMany(ctx, bson.M{"workflowId": id}); err != nil {
|
||||
return false, err
|
||||
}
|
||||
result, err := store.workflows.DeleteOne(ctx, bson.M{"id": id})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return result.DeletedCount == 1, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) UpsertWorkflow(ctx context.Context, item domain.WorkflowRecord) error {
|
||||
_, err := store.workflows.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
||||
return err
|
||||
}
|
||||
|
||||
func (store *MongoStore) ListTemplates(ctx context.Context) ([]domain.TemplateRecord, error) {
|
||||
cursor, err := store.templates.Find(ctx, bson.M{}, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return decodeMany[domain.TemplateRecord](ctx, cursor)
|
||||
}
|
||||
|
||||
func (store *MongoStore) GetTemplate(ctx context.Context, id string) (*domain.TemplateRecord, error) {
|
||||
var item domain.TemplateRecord
|
||||
err := store.templates.FindOne(ctx, bson.M{"id": id}).Decode(&item)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) CreateTemplate(ctx context.Context, input domain.CreateTemplateInput) (*domain.TemplateRecord, error) {
|
||||
now := nowISO()
|
||||
templateType := input.Type
|
||||
if templateType == "" {
|
||||
templateType = domain.WorkflowTypeBatch
|
||||
}
|
||||
item := domain.TemplateRecord{
|
||||
ID: domain.NewID("template"),
|
||||
Name: input.Name,
|
||||
Description: input.Description,
|
||||
Type: templateType,
|
||||
MaxConcurrentRuns: input.MaxConcurrentRuns,
|
||||
NodeTimeoutSeconds: input.NodeTimeoutSeconds,
|
||||
RunTimeoutSeconds: input.RunTimeoutSeconds,
|
||||
MaxRetryAttempts: input.MaxRetryAttempts,
|
||||
RetryBackoffSeconds: input.RetryBackoffSeconds,
|
||||
Schedule: input.Schedule,
|
||||
Nodes: input.Nodes,
|
||||
Edges: input.Edges,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if err := store.UpsertTemplate(ctx, item); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) DeleteTemplate(ctx context.Context, id string) (bool, error) {
|
||||
result, err := store.templates.DeleteOne(ctx, bson.M{"id": id})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return result.DeletedCount == 1, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) UpsertTemplate(ctx context.Context, item domain.TemplateRecord) error {
|
||||
_, err := store.templates.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
||||
return err
|
||||
}
|
||||
|
||||
func (store *MongoStore) ListRuns(ctx context.Context, status string) ([]domain.RunRecord, error) {
|
||||
filter := bson.M{}
|
||||
if status != "" {
|
||||
filter["status"] = status
|
||||
}
|
||||
cursor, err := store.runs.Find(ctx, filter, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return decodeMany[domain.RunRecord](ctx, cursor)
|
||||
}
|
||||
|
||||
func (store *MongoStore) GetRun(ctx context.Context, id string) (*domain.RunRecord, error) {
|
||||
var item domain.RunRecord
|
||||
err := store.runs.FindOne(ctx, bson.M{"id": id}).Decode(&item)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) CreateRun(ctx context.Context, workflowID string, trigger domain.RunTrigger) (*domain.RunRecord, error) {
|
||||
workflow, err := store.GetWorkflow(ctx, workflowID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if workflow == nil {
|
||||
return nil, fmt.Errorf("workflow %s not found", workflowID)
|
||||
}
|
||||
if workflow.Status == domain.WorkflowStatusCompleted && trigger != domain.RunTriggerScheduled {
|
||||
return nil, ErrWorkflowAlreadyCompleted
|
||||
}
|
||||
project, err := store.GetProject(ctx, workflow.ProjectID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if project == nil {
|
||||
return nil, fmt.Errorf("project %s not found", workflow.ProjectID)
|
||||
}
|
||||
now := nowISO()
|
||||
run := domain.RunRecord{
|
||||
ID: domain.NewID("run"),
|
||||
WorkflowID: workflow.ID,
|
||||
WorkflowName: workflow.Name,
|
||||
ProjectID: project.ID,
|
||||
ProjectName: project.Name,
|
||||
Trigger: trigger,
|
||||
Attempt: 1,
|
||||
AvailableAt: now,
|
||||
Status: domain.RunStatusQueued,
|
||||
NodeExecutions: []domain.NodeExecutionRecord{},
|
||||
Logs: []domain.LogEntryRecord{},
|
||||
ResourceSeries: []domain.ResourceSample{},
|
||||
Artifacts: []domain.ExecutionArtifact{},
|
||||
K8sRefs: []domain.K8sRef{},
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if err := store.UpsertRun(ctx, run); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
workflow.LastRunID = run.ID
|
||||
workflow.LastRunAt = now
|
||||
workflow.Status = domain.WorkflowStatusRunning
|
||||
workflow.UpdatedAt = now
|
||||
if err := store.UpsertWorkflow(ctx, *workflow); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &run, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) ClaimNextQueuedRun(ctx context.Context) (*domain.RunRecord, error) {
|
||||
now := nowISO()
|
||||
result := store.runs.FindOneAndUpdate(
|
||||
ctx,
|
||||
bson.M{
|
||||
"status": string(domain.RunStatusQueued),
|
||||
"$or": []bson.M{
|
||||
{"availableAt": bson.M{"$exists": false}},
|
||||
{"availableAt": ""},
|
||||
{"availableAt": bson.M{"$lte": now}},
|
||||
},
|
||||
},
|
||||
bson.M{"$set": bson.M{
|
||||
"status": string(domain.RunStatusRunning),
|
||||
"startedAt": now,
|
||||
"updatedAt": now,
|
||||
}},
|
||||
options.FindOneAndUpdate().SetSort(bson.D{{Key: "createdAt", Value: 1}}).SetReturnDocument(options.After),
|
||||
)
|
||||
var item domain.RunRecord
|
||||
err := result.Decode(&item)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) ClaimQueuedRun(ctx context.Context, runID string) (*domain.RunRecord, error) {
|
||||
now := nowISO()
|
||||
result := store.runs.FindOneAndUpdate(
|
||||
ctx,
|
||||
bson.M{
|
||||
"id": runID,
|
||||
"status": string(domain.RunStatusQueued),
|
||||
"$or": []bson.M{
|
||||
{"availableAt": bson.M{"$exists": false}},
|
||||
{"availableAt": ""},
|
||||
{"availableAt": bson.M{"$lte": now}},
|
||||
},
|
||||
},
|
||||
bson.M{"$set": bson.M{
|
||||
"status": string(domain.RunStatusRunning),
|
||||
"startedAt": now,
|
||||
"updatedAt": now,
|
||||
}},
|
||||
options.FindOneAndUpdate().SetReturnDocument(options.After),
|
||||
)
|
||||
var item domain.RunRecord
|
||||
err := result.Decode(&item)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) UpsertRun(ctx context.Context, item domain.RunRecord) error {
|
||||
computeRunDuration(&item)
|
||||
_, err := store.runs.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
||||
return err
|
||||
}
|
||||
|
||||
func (store *MongoStore) ListAuditTasks(ctx context.Context, status string) ([]domain.AuditTaskRecord, error) {
|
||||
filter := bson.M{}
|
||||
if status != "" && status != "all" {
|
||||
filter["status"] = status
|
||||
}
|
||||
cursor, err := store.auditTasks.Find(ctx, filter, options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return decodeMany[domain.AuditTaskRecord](ctx, cursor)
|
||||
}
|
||||
|
||||
func (store *MongoStore) GetAuditTask(ctx context.Context, id string) (*domain.AuditTaskRecord, error) {
|
||||
var item domain.AuditTaskRecord
|
||||
err := store.auditTasks.FindOne(ctx, bson.M{"id": id}).Decode(&item)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) CreateAuditTask(ctx context.Context, item domain.AuditTaskRecord) (*domain.AuditTaskRecord, error) {
|
||||
if err := store.UpsertAuditTask(ctx, item); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) UpsertAuditTask(ctx context.Context, item domain.AuditTaskRecord) error {
|
||||
_, err := store.auditTasks.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
||||
return err
|
||||
}
|
||||
|
||||
func (store *MongoStore) ListUsers(ctx context.Context) ([]domain.UserRecord, error) {
|
||||
cursor, err := store.users.Find(ctx, bson.M{}, options.Find().SetSort(bson.D{{Key: "createdAt", Value: 1}}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return decodeMany[domain.UserRecord](ctx, cursor)
|
||||
}
|
||||
|
||||
func (store *MongoStore) GetUser(ctx context.Context, id string) (*domain.UserRecord, error) {
|
||||
var item domain.UserRecord
|
||||
err := store.users.FindOne(ctx, bson.M{"id": id}).Decode(&item)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) CreateUser(ctx context.Context, input domain.CreateUserInput) (*domain.UserRecord, error) {
|
||||
now := nowISO()
|
||||
item := domain.UserRecord{
|
||||
ID: domain.NewID("user"),
|
||||
Name: input.Name,
|
||||
Email: input.Email,
|
||||
Role: input.Role,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if err := store.UpsertUser(ctx, item); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &item, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) DeleteUser(ctx context.Context, id string) (bool, error) {
|
||||
result, err := store.users.DeleteOne(ctx, bson.M{"id": id})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return result.DeletedCount > 0, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) UpsertUser(ctx context.Context, item domain.UserRecord) error {
|
||||
_, err := store.users.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
||||
return err
|
||||
}
|
||||
|
||||
func (store *MongoStore) GetSettings(ctx context.Context) (*domain.SettingsRecord, error) {
|
||||
var item domain.SettingsRecord
|
||||
err := store.settings.FindOne(ctx, bson.M{"id": "system"}).Decode(&item)
|
||||
if err == mongo.ErrNoDocuments {
|
||||
fallback := defaultSettings()
|
||||
if err := store.UpsertSettings(ctx, fallback); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &fallback, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
normalized := ensureSettingsDefaults(item)
|
||||
return &normalized, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) UpsertSettings(ctx context.Context, item domain.SettingsRecord) error {
|
||||
item = ensureSettingsDefaults(item)
|
||||
_, err := store.settings.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
||||
return err
|
||||
}
|
||||
|
||||
func (store *MongoStore) GetDashboardSummary(ctx context.Context) (domain.DashboardSummary, error) {
|
||||
projects, err := store.ListProjects(ctx)
|
||||
if err != nil {
|
||||
return domain.DashboardSummary{}, err
|
||||
}
|
||||
operators, err := store.ListOperators(ctx)
|
||||
if err != nil {
|
||||
return domain.DashboardSummary{}, err
|
||||
}
|
||||
runs, err := store.ListRuns(ctx, "")
|
||||
if err != nil {
|
||||
return domain.DashboardSummary{}, err
|
||||
}
|
||||
auditTasks, err := store.ListAuditTasks(ctx, "all")
|
||||
if err != nil {
|
||||
return domain.DashboardSummary{}, err
|
||||
}
|
||||
summary := buildDashboardSummary(projects, operators, runs, auditTasks)
|
||||
summary.Jobs.SuccessRate = float64(int(summary.Jobs.SuccessRate*10)) / 10
|
||||
return summary, nil
|
||||
}
|
||||
|
||||
func (store *MongoStore) ListStorageBuckets(ctx context.Context, sourcePath string) ([]string, error) {
|
||||
return listBucketsFromSourcePath(sourcePath)
|
||||
}
|
||||
|
||||
func (store *MongoStore) ListStorageObjects(ctx context.Context, sourcePath string, bucket string, prefix string) ([]string, error) {
|
||||
items, err := listObjectsFromSourcePath(sourcePath, strings.TrimSpace(bucket), strings.TrimSpace(prefix))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sort.Strings(items)
|
||||
return items, nil
|
||||
}
|
||||
59
internal/store/store.go
Normal file
59
internal/store/store.go
Normal file
@ -0,0 +1,59 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"droboticflow/internal/domain"
|
||||
)
|
||||
|
||||
type Store interface {
|
||||
ListProjects(context.Context) ([]domain.ProjectRecord, error)
|
||||
GetProject(context.Context, string) (*domain.ProjectRecord, error)
|
||||
CreateProject(context.Context, domain.CreateProjectInput) (*domain.ProjectRecord, error)
|
||||
DeleteProject(context.Context, string) (bool, error)
|
||||
UpsertProject(context.Context, domain.ProjectRecord) error
|
||||
|
||||
ListOperators(context.Context) ([]domain.OperatorRecord, error)
|
||||
GetOperator(context.Context, string) (*domain.OperatorRecord, error)
|
||||
CreateOperator(context.Context, domain.CreateOperatorInput) (*domain.OperatorRecord, error)
|
||||
DeleteOperator(context.Context, string) (bool, error)
|
||||
UpsertOperator(context.Context, domain.OperatorRecord) error
|
||||
|
||||
ListWorkflowsByProject(context.Context, string) ([]domain.WorkflowRecord, error)
|
||||
ListAllWorkflows(context.Context) ([]domain.WorkflowRecord, error)
|
||||
GetWorkflow(context.Context, string) (*domain.WorkflowRecord, error)
|
||||
CreateWorkflow(context.Context, domain.CreateWorkflowInput) (*domain.WorkflowRecord, error)
|
||||
DeleteWorkflow(context.Context, string) (bool, error)
|
||||
UpsertWorkflow(context.Context, domain.WorkflowRecord) error
|
||||
|
||||
ListTemplates(context.Context) ([]domain.TemplateRecord, error)
|
||||
GetTemplate(context.Context, string) (*domain.TemplateRecord, error)
|
||||
CreateTemplate(context.Context, domain.CreateTemplateInput) (*domain.TemplateRecord, error)
|
||||
DeleteTemplate(context.Context, string) (bool, error)
|
||||
UpsertTemplate(context.Context, domain.TemplateRecord) error
|
||||
|
||||
ListRuns(context.Context, string) ([]domain.RunRecord, error)
|
||||
GetRun(context.Context, string) (*domain.RunRecord, error)
|
||||
CreateRun(context.Context, string, domain.RunTrigger) (*domain.RunRecord, error)
|
||||
ClaimNextQueuedRun(context.Context) (*domain.RunRecord, error)
|
||||
ClaimQueuedRun(context.Context, string) (*domain.RunRecord, error)
|
||||
UpsertRun(context.Context, domain.RunRecord) error
|
||||
|
||||
ListAuditTasks(context.Context, string) ([]domain.AuditTaskRecord, error)
|
||||
GetAuditTask(context.Context, string) (*domain.AuditTaskRecord, error)
|
||||
CreateAuditTask(context.Context, domain.AuditTaskRecord) (*domain.AuditTaskRecord, error)
|
||||
UpsertAuditTask(context.Context, domain.AuditTaskRecord) error
|
||||
|
||||
ListUsers(context.Context) ([]domain.UserRecord, error)
|
||||
GetUser(context.Context, string) (*domain.UserRecord, error)
|
||||
CreateUser(context.Context, domain.CreateUserInput) (*domain.UserRecord, error)
|
||||
DeleteUser(context.Context, string) (bool, error)
|
||||
UpsertUser(context.Context, domain.UserRecord) error
|
||||
|
||||
GetSettings(context.Context) (*domain.SettingsRecord, error)
|
||||
UpsertSettings(context.Context, domain.SettingsRecord) error
|
||||
|
||||
GetDashboardSummary(context.Context) (domain.DashboardSummary, error)
|
||||
ListStorageBuckets(context.Context, string) ([]string, error)
|
||||
ListStorageObjects(context.Context, string, string, string) ([]string, error)
|
||||
}
|
||||
329
internal/worker/executor.go
Normal file
329
internal/worker/executor.go
Normal file
@ -0,0 +1,329 @@
|
||||
package worker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"droboticflow/internal/domain"
|
||||
"droboticflow/internal/k8s"
|
||||
)
|
||||
|
||||
const resultPrefix = "DROBOTICFLOW_RESULT="
|
||||
|
||||
type executionResult struct {
|
||||
Logs []domain.LogEntryRecord `json:"logs"`
|
||||
Artifacts []domain.ExecutionArtifact `json:"artifacts"`
|
||||
ResourceSeries []domain.ResourceSample `json:"resourceSeries"`
|
||||
}
|
||||
|
||||
var invalidEnvCharacters = regexp.MustCompile(`[^A-Z0-9_]+`)
|
||||
|
||||
func sanitizeEnvKey(value string) string {
|
||||
normalized := strings.ToUpper(strings.TrimSpace(value))
|
||||
if normalized == "" {
|
||||
return ""
|
||||
}
|
||||
normalized = invalidEnvCharacters.ReplaceAllString(normalized, "_")
|
||||
normalized = strings.Trim(normalized, "_")
|
||||
if normalized == "" {
|
||||
return ""
|
||||
}
|
||||
if normalized[0] >= '0' && normalized[0] <= '9' {
|
||||
normalized = "HP_" + normalized
|
||||
}
|
||||
return normalized
|
||||
}
|
||||
|
||||
func cloneArtifacts(items []domain.ExecutionArtifact) []domain.ExecutionArtifact {
|
||||
cloned := make([]domain.ExecutionArtifact, 0, len(items))
|
||||
for _, item := range items {
|
||||
cloned = append(cloned, item)
|
||||
}
|
||||
return cloned
|
||||
}
|
||||
|
||||
func gatherInputArtifacts(workflow domain.WorkflowRecord, executionMap map[string]domain.NodeExecutionRecord, nodeID string) []domain.ExecutionArtifact {
|
||||
items := make([]domain.ExecutionArtifact, 0)
|
||||
seen := map[string]struct{}{}
|
||||
for _, edge := range domain.GetIncomingEdges(workflow.Edges, nodeID) {
|
||||
execution, ok := executionMap[edge.Source]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
requiresHandle := strings.TrimSpace(edge.SourceHandle) != ""
|
||||
hasPortAwareArtifacts := false
|
||||
for _, artifact := range execution.Artifacts {
|
||||
if strings.TrimSpace(artifact.PortID) != "" {
|
||||
hasPortAwareArtifacts = true
|
||||
break
|
||||
}
|
||||
}
|
||||
for _, artifact := range execution.Artifacts {
|
||||
if requiresHandle && hasPortAwareArtifacts && artifact.PortID != edge.SourceHandle {
|
||||
continue
|
||||
}
|
||||
key := artifact.ID
|
||||
if key == "" {
|
||||
key = artifact.NodeID + ":" + artifact.Name
|
||||
}
|
||||
if _, exists := seen[key]; exists {
|
||||
continue
|
||||
}
|
||||
seen[key] = struct{}{}
|
||||
items = append(items, artifact)
|
||||
}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func extractConfidence(artifact domain.ExecutionArtifact) (float64, bool) {
|
||||
if artifact.Data == nil {
|
||||
return 0, false
|
||||
}
|
||||
for _, key := range []string{"confidence", "score"} {
|
||||
value, ok := artifact.Data[key]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
switch typed := value.(type) {
|
||||
case float64:
|
||||
return typed, true
|
||||
case int:
|
||||
return float64(typed), true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func extractObjectHints(artifact domain.ExecutionArtifact) []string {
|
||||
if artifact.Data == nil {
|
||||
return nil
|
||||
}
|
||||
items := make([]string, 0)
|
||||
for _, key := range []string{"detectedObjects", "labels", "entries"} {
|
||||
value, ok := artifact.Data[key]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
switch typed := value.(type) {
|
||||
case []any:
|
||||
for _, candidate := range typed {
|
||||
text := strings.TrimSpace(fmt.Sprint(candidate))
|
||||
if text != "" {
|
||||
items = append(items, text)
|
||||
}
|
||||
}
|
||||
case []string:
|
||||
for _, candidate := range typed {
|
||||
if trimmed := strings.TrimSpace(candidate); trimmed != "" {
|
||||
items = append(items, trimmed)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func createAuditPayload(node domain.WorkflowNode, inputArtifacts []domain.ExecutionArtifact) domain.AuditTaskPayload {
|
||||
payload := domain.AuditTaskPayload{
|
||||
Reason: fmt.Sprintf("%s requires explicit approval before downstream execution", node.Data.Label),
|
||||
DetectedObjects: []string{},
|
||||
}
|
||||
for _, artifact := range inputArtifacts {
|
||||
if payload.Image == "" {
|
||||
switch {
|
||||
case artifact.Metadata != nil && artifact.Metadata.Thumbnail != "":
|
||||
payload.Image = artifact.Metadata.Thumbnail
|
||||
case artifact.Type == domain.PortTypeImage || artifact.Type == domain.PortTypeVideo:
|
||||
payload.Image = artifact.Path
|
||||
}
|
||||
}
|
||||
if payload.Confidence == 0 {
|
||||
if confidence, ok := extractConfidence(artifact); ok {
|
||||
payload.Confidence = confidence
|
||||
}
|
||||
}
|
||||
payload.DetectedObjects = append(payload.DetectedObjects, extractObjectHints(artifact)...)
|
||||
}
|
||||
if len(payload.DetectedObjects) == 0 {
|
||||
payload.DetectedObjects = []string{fmt.Sprintf("%d upstream artifact(s)", len(inputArtifacts))}
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
func prepareOutputDir(runID string, nodeID string) (string, error) {
|
||||
outputDir := filepath.Join(os.TempDir(), "droboticflow", runID, nodeID)
|
||||
if err := os.MkdirAll(outputDir, 0o755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return outputDir, nil
|
||||
}
|
||||
|
||||
func envMapToSlice(values map[string]string) []string {
|
||||
keys := make([]string, 0, len(values))
|
||||
for key := range values {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
slices.Sort(keys)
|
||||
items := make([]string, 0, len(keys))
|
||||
for _, key := range keys {
|
||||
items = append(items, key+"="+values[key])
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func buildNodeEnv(run *domain.RunRecord, node domain.WorkflowNode, inputArtifacts []domain.ExecutionArtifact, outputDir string) (map[string]string, error) {
|
||||
nodeDataJSON, err := json.Marshal(node.Data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
inputArtifactsJSON, err := json.Marshal(inputArtifacts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
env := map[string]string{
|
||||
"RUN_ID": run.ID,
|
||||
"WORKFLOW_ID": run.WorkflowID,
|
||||
"NODE_ID": node.ID,
|
||||
"NODE_LABEL": node.Data.Label,
|
||||
"NODE_TYPE": node.Type,
|
||||
"NODE_CATEGORY": node.Data.Category,
|
||||
"NODE_DESCRIPTION": node.Data.Description,
|
||||
"NODE_DATA_JSON": string(nodeDataJSON),
|
||||
"INPUT_ARTIFACTS_JSON": string(inputArtifactsJSON),
|
||||
"OUTPUT_DIR": outputDir,
|
||||
}
|
||||
if node.Data.Bucket != "" {
|
||||
env["STORAGE_BUCKET"] = node.Data.Bucket
|
||||
}
|
||||
if node.Data.Prefix != "" {
|
||||
env["STORAGE_PREFIX"] = node.Data.Prefix
|
||||
}
|
||||
if node.Data.Pattern != "" {
|
||||
env["STORAGE_PATTERN"] = node.Data.Pattern
|
||||
}
|
||||
if node.Data.CPU != "" {
|
||||
env["CPU_LIMIT"] = node.Data.CPU
|
||||
}
|
||||
if node.Data.Memory != "" {
|
||||
env["MEMORY_LIMIT"] = node.Data.Memory
|
||||
}
|
||||
if node.Data.Image != "" {
|
||||
env["NODE_IMAGE"] = node.Data.Image
|
||||
}
|
||||
for _, item := range node.Data.Hyperparams {
|
||||
key := sanitizeEnvKey(item.Key)
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
env[key] = item.Value
|
||||
}
|
||||
return env, nil
|
||||
}
|
||||
|
||||
func parseExecutionOutput(stdout string, nodeLabel string) (executionResult, error) {
|
||||
result := executionResult{
|
||||
Logs: []domain.LogEntryRecord{},
|
||||
Artifacts: []domain.ExecutionArtifact{},
|
||||
ResourceSeries: []domain.ResourceSample{},
|
||||
}
|
||||
lines := strings.Split(strings.ReplaceAll(stdout, "\r\n", "\n"), "\n")
|
||||
structuredPayload := ""
|
||||
rawLogs := make([]domain.LogEntryRecord, 0)
|
||||
for _, line := range lines {
|
||||
trimmed := strings.TrimSpace(line)
|
||||
if trimmed == "" {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(trimmed, resultPrefix) {
|
||||
structuredPayload = strings.TrimPrefix(trimmed, resultPrefix)
|
||||
continue
|
||||
}
|
||||
rawLogs = append(rawLogs, logEntry("INFO", trimmed, nodeLabel))
|
||||
}
|
||||
if structuredPayload == "" {
|
||||
result.Logs = rawLogs
|
||||
return result, fmt.Errorf("operator execution did not emit %s payload", resultPrefix)
|
||||
}
|
||||
var parsed executionResult
|
||||
if err := json.Unmarshal([]byte(structuredPayload), &parsed); err != nil {
|
||||
return result, fmt.Errorf("decode operator result: %w", err)
|
||||
}
|
||||
if len(parsed.Logs) > 0 {
|
||||
result.Logs = append(result.Logs, parsed.Logs...)
|
||||
} else {
|
||||
result.Logs = append(result.Logs, rawLogs...)
|
||||
}
|
||||
result.Artifacts = parsed.Artifacts
|
||||
result.ResourceSeries = parsed.ResourceSeries
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (runtime *Runtime) executeLocalProcess(ctx context.Context, command string, env map[string]string) (string, error) {
|
||||
cmd := exec.CommandContext(ctx, "sh", "-lc", command)
|
||||
cmd.Env = append(os.Environ(), envMapToSlice(env)...)
|
||||
output, err := cmd.CombinedOutput()
|
||||
return string(output), err
|
||||
}
|
||||
|
||||
func (runtime *Runtime) executeLocalDocker(ctx context.Context, image string, env map[string]string, outputDir string) (string, error) {
|
||||
args := []string{"run", "--rm", "-v", outputDir + ":" + outputDir}
|
||||
for key, value := range env {
|
||||
args = append(args, "-e", key+"="+value)
|
||||
}
|
||||
args = append(args, image)
|
||||
cmd := exec.CommandContext(ctx, "docker", args...)
|
||||
output, err := cmd.CombinedOutput()
|
||||
return string(output), err
|
||||
}
|
||||
|
||||
func (runtime *Runtime) executeNode(ctx context.Context, run *domain.RunRecord, node domain.WorkflowNode, inputArtifacts []domain.ExecutionArtifact) (executionResult, *k8s.JobReference, error) {
|
||||
outputDir, err := prepareOutputDir(run.ID, node.ID)
|
||||
if err != nil {
|
||||
return executionResult{}, nil, err
|
||||
}
|
||||
env, err := buildNodeEnv(run, node, inputArtifacts, outputDir)
|
||||
if err != nil {
|
||||
return executionResult{}, nil, err
|
||||
}
|
||||
if runtime.launcher.Enabled() {
|
||||
nodeTimeoutSeconds := 0
|
||||
if workflow, err := runtime.store.GetWorkflow(ctx, run.WorkflowID); err == nil && workflow != nil {
|
||||
nodeTimeoutSeconds = workflow.NodeTimeoutSeconds
|
||||
}
|
||||
launched, err := runtime.launcher.Launch(ctx, k8s.LaunchInput{
|
||||
Image: chooseImage(node, runtime.operatorRunnerImage),
|
||||
RunID: run.ID,
|
||||
WorkflowID: run.WorkflowID,
|
||||
NodeID: node.ID,
|
||||
NodeLabel: node.Data.Label,
|
||||
TimeoutSeconds: nodeTimeoutSeconds,
|
||||
Env: env,
|
||||
})
|
||||
if err != nil {
|
||||
return executionResult{}, nil, err
|
||||
}
|
||||
parsed, err := parseExecutionOutput(launched.Output, node.Data.Label)
|
||||
return parsed, launched.Reference, err
|
||||
}
|
||||
|
||||
image := chooseImage(node, runtime.operatorRunnerImage)
|
||||
var stdout string
|
||||
if node.Data.Image != "" && node.Data.Image != runtime.operatorRunnerImage {
|
||||
stdout, err = runtime.executeLocalDocker(ctx, image, env, outputDir)
|
||||
} else {
|
||||
stdout, err = runtime.executeLocalProcess(ctx, runtime.localRunnerCommand, env)
|
||||
}
|
||||
if err != nil {
|
||||
return executionResult{}, nil, fmt.Errorf("%w: %s", err, stdout)
|
||||
}
|
||||
parsed, err := parseExecutionOutput(stdout, node.Data.Label)
|
||||
return parsed, nil, err
|
||||
}
|
||||
97
internal/worker/http.go
Normal file
97
internal/worker/http.go
Normal file
@ -0,0 +1,97 @@
|
||||
package worker
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"droboticflow/internal/domain"
|
||||
"droboticflow/internal/store"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type HTTPServer struct {
|
||||
store store.Store
|
||||
runtime *Runtime
|
||||
}
|
||||
|
||||
func NewHTTPHandler(dataStore store.Store, runtime *Runtime) http.Handler {
|
||||
server := &HTTPServer{store: dataStore, runtime: runtime}
|
||||
router := chi.NewRouter()
|
||||
|
||||
router.Get("/worker-api/health", server.handleHealth)
|
||||
router.Get("/worker-api/stats", server.handleStats)
|
||||
router.Post("/worker-api/process-once", server.handleProcessOnce)
|
||||
router.Post("/worker-api/runs/{id}/retry", server.handleRetry)
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
func (server *HTTPServer) handleHealth(writer http.ResponseWriter, _ *http.Request) {
|
||||
renderWorkerJSON(writer, http.StatusOK, map[string]string{"status": "ok", "service": "droboticflow-worker"})
|
||||
}
|
||||
|
||||
func (server *HTTPServer) handleStats(writer http.ResponseWriter, request *http.Request) {
|
||||
runs, err := server.store.ListRuns(request.Context(), "")
|
||||
if err != nil {
|
||||
renderWorkerJSON(writer, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
response := map[string]int{"queued": 0, "running": 0, "waitingForAudit": 0}
|
||||
for _, run := range runs {
|
||||
switch run.Status {
|
||||
case domain.RunStatusQueued:
|
||||
response["queued"]++
|
||||
case domain.RunStatusRunning:
|
||||
response["running"]++
|
||||
case domain.RunStatusWaitingForAudit:
|
||||
response["waitingForAudit"]++
|
||||
}
|
||||
}
|
||||
renderWorkerJSON(writer, http.StatusOK, response)
|
||||
}
|
||||
|
||||
func (server *HTTPServer) handleProcessOnce(writer http.ResponseWriter, request *http.Request) {
|
||||
run, err := server.runtime.ProcessOnce(request.Context())
|
||||
if err != nil {
|
||||
renderWorkerJSON(writer, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
renderWorkerJSON(writer, http.StatusOK, map[string]any{"run": run})
|
||||
}
|
||||
|
||||
func (server *HTTPServer) handleRetry(writer http.ResponseWriter, request *http.Request) {
|
||||
run, err := server.store.GetRun(request.Context(), chi.URLParam(request, "id"))
|
||||
if err != nil {
|
||||
renderWorkerJSON(writer, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if run == nil {
|
||||
renderWorkerJSON(writer, http.StatusNotFound, map[string]string{"error": "Run not found"})
|
||||
return
|
||||
}
|
||||
run.Status = domain.RunStatusQueued
|
||||
run.StartedAt = ""
|
||||
run.EndedAt = ""
|
||||
run.DurationMS = 0
|
||||
run.CurrentNodeID = ""
|
||||
run.NodeExecutions = []domain.NodeExecutionRecord{}
|
||||
run.Logs = []domain.LogEntryRecord{}
|
||||
run.ResourceSeries = []domain.ResourceSample{}
|
||||
run.Artifacts = []domain.ExecutionArtifact{}
|
||||
run.K8sRefs = []domain.K8sRef{}
|
||||
run.Error = ""
|
||||
run.AvailableAt = nowISO()
|
||||
run.UpdatedAt = nowISO()
|
||||
if err := server.store.UpsertRun(request.Context(), *run); err != nil {
|
||||
renderWorkerJSON(writer, http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
renderWorkerJSON(writer, http.StatusAccepted, run)
|
||||
}
|
||||
|
||||
func renderWorkerJSON(writer http.ResponseWriter, status int, payload any) {
|
||||
writer.Header().Set("Content-Type", "application/json")
|
||||
writer.WriteHeader(status)
|
||||
_ = json.NewEncoder(writer).Encode(payload)
|
||||
}
|
||||
724
internal/worker/runtime.go
Normal file
724
internal/worker/runtime.go
Normal file
@ -0,0 +1,724 @@
|
||||
package worker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"droboticflow/internal/domain"
|
||||
"droboticflow/internal/k8s"
|
||||
"droboticflow/internal/store"
|
||||
)
|
||||
|
||||
type Runtime struct {
|
||||
store store.Store
|
||||
mode string
|
||||
namespace string
|
||||
imagePullSecretName string
|
||||
operatorRunnerImage string
|
||||
localRunnerCommand string
|
||||
launcher *k8s.Launcher
|
||||
}
|
||||
|
||||
var activeDispatchStatuses = map[domain.RunStatus]struct{}{
|
||||
domain.RunStatusRunning: {},
|
||||
domain.RunStatusWaitingForAudit: {},
|
||||
}
|
||||
|
||||
var queuedOrActiveStatuses = map[domain.RunStatus]struct{}{
|
||||
domain.RunStatusQueued: {},
|
||||
domain.RunStatusRunning: {},
|
||||
domain.RunStatusWaitingForAudit: {},
|
||||
}
|
||||
|
||||
func NewRuntime(dataStore store.Store, mode string, namespace string, imagePullSecretName string, operatorRunnerImage string, localRunnerCommand string) (*Runtime, error) {
|
||||
launcher, err := k8s.NewLauncher(mode == "k8s", namespace, imagePullSecretName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Runtime{
|
||||
store: dataStore,
|
||||
mode: mode,
|
||||
namespace: namespace,
|
||||
imagePullSecretName: imagePullSecretName,
|
||||
operatorRunnerImage: operatorRunnerImage,
|
||||
localRunnerCommand: localRunnerCommand,
|
||||
launcher: launcher,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func nowISO() string {
|
||||
return time.Now().UTC().Format(time.RFC3339Nano)
|
||||
}
|
||||
|
||||
func logEntry(level string, message string, operator string) domain.LogEntryRecord {
|
||||
return domain.LogEntryRecord{
|
||||
Timestamp: nowISO(),
|
||||
Level: level,
|
||||
Message: message,
|
||||
Operator: operator,
|
||||
}
|
||||
}
|
||||
|
||||
func findNodeExecution(run *domain.RunRecord, nodeID string) (int, *domain.NodeExecutionRecord) {
|
||||
for index := range run.NodeExecutions {
|
||||
if run.NodeExecutions[index].NodeID == nodeID {
|
||||
return index, &run.NodeExecutions[index]
|
||||
}
|
||||
}
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
func appendLog(run *domain.RunRecord, entry domain.LogEntryRecord) {
|
||||
run.Logs = append(run.Logs, entry)
|
||||
}
|
||||
|
||||
func applyExecution(run *domain.RunRecord, execution domain.NodeExecutionRecord) {
|
||||
index, _ := findNodeExecution(run, execution.NodeID)
|
||||
if index >= 0 {
|
||||
run.NodeExecutions[index] = execution
|
||||
} else {
|
||||
run.NodeExecutions = append(run.NodeExecutions, execution)
|
||||
}
|
||||
run.Logs = []domain.LogEntryRecord{}
|
||||
run.ResourceSeries = []domain.ResourceSample{}
|
||||
run.Artifacts = []domain.ExecutionArtifact{}
|
||||
for _, item := range run.NodeExecutions {
|
||||
for _, entry := range item.Logs {
|
||||
appendLog(run, entry)
|
||||
}
|
||||
if len(item.ResourceSeries) > 0 {
|
||||
run.ResourceSeries = append(run.ResourceSeries, item.ResourceSeries...)
|
||||
}
|
||||
if len(item.Artifacts) > 0 {
|
||||
run.Artifacts = append(run.Artifacts, item.Artifacts...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func globalConcurrencyLimit(settings *domain.SettingsRecord) int {
|
||||
if settings != nil && settings.Worker.MaxConcurrentRuns > 0 {
|
||||
return settings.Worker.MaxConcurrentRuns
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
func effectiveWorkflowConcurrencyLimit(workflow *domain.WorkflowRecord, settings *domain.SettingsRecord) int {
|
||||
if workflow != nil && workflow.MaxConcurrentRuns > 0 {
|
||||
return workflow.MaxConcurrentRuns
|
||||
}
|
||||
return globalConcurrencyLimit(settings)
|
||||
}
|
||||
|
||||
func selectQueuedRunsForDispatch(runs []domain.RunRecord, workflows []domain.WorkflowRecord, settings *domain.SettingsRecord) []string {
|
||||
globalLimit := globalConcurrencyLimit(settings)
|
||||
workflowByID := make(map[string]domain.WorkflowRecord, len(workflows))
|
||||
for _, workflow := range workflows {
|
||||
workflowByID[workflow.ID] = workflow
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
|
||||
activeByWorkflow := map[string]int{}
|
||||
activeTotal := 0
|
||||
queued := make([]domain.RunRecord, 0)
|
||||
for _, run := range runs {
|
||||
if _, ok := activeDispatchStatuses[run.Status]; ok {
|
||||
activeTotal++
|
||||
activeByWorkflow[run.WorkflowID]++
|
||||
continue
|
||||
}
|
||||
if run.Status == domain.RunStatusQueued {
|
||||
if !runDispatchReady(run, now) {
|
||||
continue
|
||||
}
|
||||
queued = append(queued, run)
|
||||
}
|
||||
}
|
||||
|
||||
available := globalLimit - activeTotal
|
||||
if available <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
sort.Slice(queued, func(left int, right int) bool {
|
||||
leftAvailableAt := runAvailableAt(queued[left])
|
||||
rightAvailableAt := runAvailableAt(queued[right])
|
||||
if leftAvailableAt.Equal(rightAvailableAt) {
|
||||
return queued[left].CreatedAt < queued[right].CreatedAt
|
||||
}
|
||||
return leftAvailableAt.Before(rightAvailableAt)
|
||||
})
|
||||
|
||||
selected := make([]string, 0, available)
|
||||
for _, run := range queued {
|
||||
if available == 0 {
|
||||
break
|
||||
}
|
||||
workflow := workflowByID[run.WorkflowID]
|
||||
workflowLimit := effectiveWorkflowConcurrencyLimit(&workflow, settings)
|
||||
if activeByWorkflow[run.WorkflowID] >= workflowLimit {
|
||||
continue
|
||||
}
|
||||
selected = append(selected, run.ID)
|
||||
activeByWorkflow[run.WorkflowID]++
|
||||
available--
|
||||
}
|
||||
return selected
|
||||
}
|
||||
|
||||
func (runtime *Runtime) enqueueStreamingWorkflows(ctx context.Context) error {
|
||||
workflows, err := runtime.store.ListAllWorkflows(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
runs, err := runtime.store.ListRuns(ctx, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
settings, err := runtime.store.GetSettings(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
for _, workflow := range workflows {
|
||||
if workflow.Type != domain.WorkflowTypeStreaming || workflow.Schedule == nil || workflow.Schedule.Interval <= 0 {
|
||||
continue
|
||||
}
|
||||
inflightCount := 0
|
||||
for _, run := range runs {
|
||||
if run.WorkflowID == workflow.ID {
|
||||
if _, ok := queuedOrActiveStatuses[run.Status]; ok {
|
||||
inflightCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
if inflightCount >= effectiveWorkflowConcurrencyLimit(&workflow, settings) {
|
||||
continue
|
||||
}
|
||||
lastRunAt := time.Time{}
|
||||
if workflow.LastRunAt != "" {
|
||||
parsed, err := time.Parse(time.RFC3339Nano, workflow.LastRunAt)
|
||||
if err == nil {
|
||||
lastRunAt = parsed
|
||||
}
|
||||
}
|
||||
if workflow.LastRunAt == "" || now.Sub(lastRunAt) >= time.Duration(workflow.Schedule.Interval)*time.Second {
|
||||
if _, err := runtime.store.CreateRun(ctx, workflow.ID, domain.RunTriggerScheduled); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (runtime *Runtime) processClaimedRun(claimed *domain.RunRecord) {
|
||||
if _, err := runtime.processRun(context.Background(), claimed); err != nil {
|
||||
fmt.Printf("[worker] process run %s failed: %v\n", claimed.ID, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (runtime *Runtime) ProcessAvailable(ctx context.Context) error {
|
||||
if err := runtime.enqueueStreamingWorkflows(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
settings, err := runtime.store.GetSettings(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
workflows, err := runtime.store.ListAllWorkflows(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
runs, err := runtime.store.ListRuns(ctx, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, runID := range selectQueuedRunsForDispatch(runs, workflows, settings) {
|
||||
claimed, err := runtime.store.ClaimQueuedRun(ctx, runID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if claimed == nil {
|
||||
continue
|
||||
}
|
||||
go runtime.processClaimedRun(claimed)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func chooseImage(node domain.WorkflowNode, fallback string) string {
|
||||
if node.Data.Image != "" {
|
||||
return node.Data.Image
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func chooseValue(value string, fallback string) string {
|
||||
if value == "" {
|
||||
return fallback
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func workflowRunTimeout(workflow *domain.WorkflowRecord) time.Duration {
|
||||
if workflow == nil || workflow.RunTimeoutSeconds <= 0 {
|
||||
return 0
|
||||
}
|
||||
return time.Duration(workflow.RunTimeoutSeconds) * time.Second
|
||||
}
|
||||
|
||||
func workflowNodeTimeout(workflow *domain.WorkflowRecord) time.Duration {
|
||||
if workflow == nil || workflow.NodeTimeoutSeconds <= 0 {
|
||||
return 0
|
||||
}
|
||||
return time.Duration(workflow.NodeTimeoutSeconds) * time.Second
|
||||
}
|
||||
|
||||
func normalizeRunError(workflow *domain.WorkflowRecord, err error, executionCtx context.Context, nodeTimedOut bool) string {
|
||||
if err == nil {
|
||||
return ""
|
||||
}
|
||||
if workflow != nil && workflow.RunTimeoutSeconds > 0 && (errors.Is(err, context.DeadlineExceeded) || errors.Is(executionCtx.Err(), context.DeadlineExceeded)) {
|
||||
return fmt.Sprintf("工作流执行超时(%d 秒)", workflow.RunTimeoutSeconds)
|
||||
}
|
||||
if workflow != nil && workflow.NodeTimeoutSeconds > 0 && nodeTimedOut {
|
||||
return fmt.Sprintf("节点执行超时(%d 秒)", workflow.NodeTimeoutSeconds)
|
||||
}
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
func shouldAutoRetry(workflow *domain.WorkflowRecord, run *domain.RunRecord) bool {
|
||||
if workflow == nil || run == nil || workflow.MaxRetryAttempts <= 0 {
|
||||
return false
|
||||
}
|
||||
attempt := run.Attempt
|
||||
if attempt <= 0 {
|
||||
attempt = 1
|
||||
}
|
||||
return attempt <= workflow.MaxRetryAttempts
|
||||
}
|
||||
|
||||
func nextRunAttempt(run *domain.RunRecord) int {
|
||||
if run == nil || run.Attempt <= 0 {
|
||||
return 1
|
||||
}
|
||||
return run.Attempt + 1
|
||||
}
|
||||
|
||||
func nextRunAvailableAt(workflow *domain.WorkflowRecord) string {
|
||||
if workflow == nil || workflow.RetryBackoffSeconds <= 0 {
|
||||
return nowISO()
|
||||
}
|
||||
return time.Now().UTC().Add(time.Duration(workflow.RetryBackoffSeconds) * time.Second).Format(time.RFC3339Nano)
|
||||
}
|
||||
|
||||
func (runtime *Runtime) enqueueRetryRun(ctx context.Context, workflow *domain.WorkflowRecord, failedRun *domain.RunRecord) (*domain.RunRecord, error) {
|
||||
if workflow == nil || failedRun == nil {
|
||||
return nil, nil
|
||||
}
|
||||
retryRun, err := runtime.store.CreateRun(ctx, workflow.ID, domain.RunTriggerRetry)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
retryRun.Attempt = nextRunAttempt(failedRun)
|
||||
retryRun.ParentRunID = failedRun.ID
|
||||
retryRun.AvailableAt = nextRunAvailableAt(workflow)
|
||||
retryRun.UpdatedAt = nowISO()
|
||||
if err := runtime.store.UpsertRun(ctx, *retryRun); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return retryRun, nil
|
||||
}
|
||||
|
||||
type nodeExecutionOutcome struct {
|
||||
nodeID string
|
||||
result executionResult
|
||||
reference *k8s.JobReference
|
||||
err error
|
||||
nodeTimedOut bool
|
||||
}
|
||||
|
||||
func runAvailableAt(run domain.RunRecord) time.Time {
|
||||
if run.AvailableAt != "" {
|
||||
if parsed, err := time.Parse(time.RFC3339Nano, run.AvailableAt); err == nil {
|
||||
return parsed
|
||||
}
|
||||
}
|
||||
if parsed, err := time.Parse(time.RFC3339Nano, run.CreatedAt); err == nil {
|
||||
return parsed
|
||||
}
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
func runDispatchReady(run domain.RunRecord, now time.Time) bool {
|
||||
availableAt := runAvailableAt(run)
|
||||
if availableAt.IsZero() {
|
||||
return true
|
||||
}
|
||||
return !availableAt.After(now)
|
||||
}
|
||||
|
||||
func syncCurrentNodeID(run *domain.RunRecord) {
|
||||
run.CurrentNodeID = ""
|
||||
for _, status := range []domain.NodeExecutionStatus{
|
||||
domain.NodeExecutionStatusWaitingForAudit,
|
||||
domain.NodeExecutionStatusRunning,
|
||||
} {
|
||||
for _, execution := range run.NodeExecutions {
|
||||
if execution.Status == status {
|
||||
run.CurrentNodeID = execution.NodeID
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func dependenciesComplete(workflow domain.WorkflowRecord, executionMap map[string]domain.NodeExecutionRecord, nodeID string) bool {
|
||||
for _, candidate := range domain.GetIncomingEdges(workflow.Edges, nodeID) {
|
||||
dependency, ok := executionMap[candidate.Source]
|
||||
if !ok || dependency.Status != domain.NodeExecutionStatusCompleted {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func queueNodeIfReady(workflow domain.WorkflowRecord, executionMap map[string]domain.NodeExecutionRecord, readyQueue *[]string, readySet map[string]struct{}, nodeID string) {
|
||||
if _, queued := readySet[nodeID]; queued {
|
||||
return
|
||||
}
|
||||
if execution, ok := executionMap[nodeID]; ok {
|
||||
if execution.Status == domain.NodeExecutionStatusCompleted ||
|
||||
execution.Status == domain.NodeExecutionStatusRunning ||
|
||||
execution.Status == domain.NodeExecutionStatusWaitingForAudit {
|
||||
return
|
||||
}
|
||||
}
|
||||
if !dependenciesComplete(workflow, executionMap, nodeID) {
|
||||
return
|
||||
}
|
||||
*readyQueue = append(*readyQueue, nodeID)
|
||||
readySet[nodeID] = struct{}{}
|
||||
}
|
||||
|
||||
func enqueueDependents(workflow domain.WorkflowRecord, executionMap map[string]domain.NodeExecutionRecord, readyQueue *[]string, readySet map[string]struct{}, nodeID string) {
|
||||
for _, edge := range domain.GetOutgoingEdges(workflow.Edges, nodeID) {
|
||||
queueNodeIfReady(workflow, executionMap, readyQueue, readySet, edge.Target)
|
||||
}
|
||||
}
|
||||
|
||||
func (runtime *Runtime) processRun(ctx context.Context, claimed *domain.RunRecord) (*domain.RunRecord, error) {
|
||||
workflow, err := runtime.store.GetWorkflow(ctx, claimed.WorkflowID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if workflow == nil {
|
||||
claimed.Status = domain.RunStatusFailed
|
||||
claimed.EndedAt = nowISO()
|
||||
claimed.Error = fmt.Sprintf("workflow %s not found", claimed.WorkflowID)
|
||||
claimed.UpdatedAt = nowISO()
|
||||
return claimed, runtime.store.UpsertRun(ctx, *claimed)
|
||||
}
|
||||
|
||||
currentRun, err := runtime.store.GetRun(ctx, claimed.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if currentRun == nil {
|
||||
currentRun = claimed
|
||||
}
|
||||
settings, err := runtime.store.GetSettings(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nodeConcurrencyLimit := effectiveWorkflowConcurrencyLimit(workflow, settings)
|
||||
if nodeConcurrencyLimit <= 0 {
|
||||
nodeConcurrencyLimit = 1
|
||||
}
|
||||
|
||||
executionMap := map[string]domain.NodeExecutionRecord{}
|
||||
for _, execution := range currentRun.NodeExecutions {
|
||||
executionMap[execution.NodeID] = execution
|
||||
}
|
||||
readyQueue := make([]string, 0)
|
||||
readySet := make(map[string]struct{})
|
||||
auditQueue := make([]string, 0)
|
||||
|
||||
if currentRun.CurrentNodeID != "" {
|
||||
tasks, err := runtime.store.ListAuditTasks(ctx, "all")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, task := range tasks {
|
||||
if task.RunID == currentRun.ID && task.NodeID == currentRun.CurrentNodeID && task.Status != domain.AuditStatusPending {
|
||||
node := domain.GetNodeByID(workflow.Nodes, currentRun.CurrentNodeID)
|
||||
if node != nil {
|
||||
execution := executionMap[node.ID]
|
||||
execution.NodeID = node.ID
|
||||
execution.NodeLabel = node.Data.Label
|
||||
execution.Status = domain.NodeExecutionStatusCompleted
|
||||
execution.EndedAt = nowISO()
|
||||
execution.Logs = append(execution.Logs, logEntry("INFO", fmt.Sprintf("Audit decision: %s", task.Status), node.Data.Label))
|
||||
executionMap[node.ID] = execution
|
||||
applyExecution(currentRun, execution)
|
||||
for _, target := range edgeTargets(domain.GetDecisionOutgoingEdges(workflow.Edges, node.ID, task.Status)) {
|
||||
queueNodeIfReady(*workflow, executionMap, &readyQueue, readySet, target)
|
||||
}
|
||||
currentRun.Status = domain.RunStatusRunning
|
||||
syncCurrentNodeID(currentRun)
|
||||
currentRun.UpdatedAt = nowISO()
|
||||
if err := runtime.store.UpsertRun(ctx, *currentRun); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(readyQueue) == 0 && len(executionMap) == 0 {
|
||||
for _, nodeID := range domain.GetStartNodeIDs(workflow.Nodes, workflow.Edges) {
|
||||
queueNodeIfReady(*workflow, executionMap, &readyQueue, readySet, nodeID)
|
||||
}
|
||||
}
|
||||
|
||||
executionCtx := ctx
|
||||
cancelExecution := func() {}
|
||||
if timeout := workflowRunTimeout(workflow); timeout > 0 {
|
||||
executionCtx, cancelExecution = context.WithTimeout(ctx, timeout)
|
||||
} else {
|
||||
executionCtx, cancelExecution = context.WithCancel(ctx)
|
||||
}
|
||||
defer cancelExecution()
|
||||
results := make(chan nodeExecutionOutcome, nodeConcurrencyLimit)
|
||||
runningNodes := make(map[string]struct{})
|
||||
for {
|
||||
launched := false
|
||||
for len(runningNodes) < nodeConcurrencyLimit && len(readyQueue) > 0 {
|
||||
nodeID := readyQueue[0]
|
||||
readyQueue = readyQueue[1:]
|
||||
delete(readySet, nodeID)
|
||||
|
||||
node := domain.GetNodeByID(workflow.Nodes, nodeID)
|
||||
if node == nil {
|
||||
continue
|
||||
}
|
||||
if existing, ok := executionMap[node.ID]; ok {
|
||||
if existing.Status == domain.NodeExecutionStatusCompleted ||
|
||||
existing.Status == domain.NodeExecutionStatusRunning ||
|
||||
existing.Status == domain.NodeExecutionStatusWaitingForAudit {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if !dependenciesComplete(*workflow, executionMap, node.ID) {
|
||||
continue
|
||||
}
|
||||
if domain.NodeRequiresAudit(*node) {
|
||||
auditQueue = append(auditQueue, node.ID)
|
||||
continue
|
||||
}
|
||||
|
||||
inputArtifacts := gatherInputArtifacts(*workflow, executionMap, node.ID)
|
||||
execution := domain.NodeExecutionRecord{
|
||||
NodeID: node.ID,
|
||||
NodeLabel: node.Data.Label,
|
||||
Status: domain.NodeExecutionStatusRunning,
|
||||
StartedAt: nowISO(),
|
||||
Logs: []domain.LogEntryRecord{logEntry("INFO", "Starting node execution", node.Data.Label)},
|
||||
Artifacts: []domain.ExecutionArtifact{},
|
||||
ResourceSeries: []domain.ResourceSample{},
|
||||
}
|
||||
executionMap[node.ID] = execution
|
||||
applyExecution(currentRun, execution)
|
||||
runningNodes[node.ID] = struct{}{}
|
||||
launched = true
|
||||
|
||||
runSnapshot := *currentRun
|
||||
nodeSnapshot := *node
|
||||
inputSnapshot := cloneArtifacts(inputArtifacts)
|
||||
nodeExecutionCtx := executionCtx
|
||||
cancelNodeExecution := func() {}
|
||||
if timeout := workflowNodeTimeout(workflow); timeout > 0 {
|
||||
nodeExecutionCtx, cancelNodeExecution = context.WithTimeout(executionCtx, timeout)
|
||||
}
|
||||
go func() {
|
||||
defer cancelNodeExecution()
|
||||
result, reference, err := runtime.executeNode(nodeExecutionCtx, &runSnapshot, nodeSnapshot, inputSnapshot)
|
||||
results <- nodeExecutionOutcome{
|
||||
nodeID: nodeSnapshot.ID,
|
||||
result: result,
|
||||
reference: reference,
|
||||
err: err,
|
||||
nodeTimedOut: errors.Is(nodeExecutionCtx.Err(), context.DeadlineExceeded),
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
if launched {
|
||||
currentRun.Status = domain.RunStatusRunning
|
||||
syncCurrentNodeID(currentRun)
|
||||
currentRun.UpdatedAt = nowISO()
|
||||
if err := runtime.store.UpsertRun(ctx, *currentRun); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if len(runningNodes) == 0 {
|
||||
if len(readyQueue) > 0 {
|
||||
continue
|
||||
}
|
||||
if len(auditQueue) > 0 {
|
||||
nodeID := auditQueue[0]
|
||||
auditQueue = auditQueue[1:]
|
||||
node := domain.GetNodeByID(workflow.Nodes, nodeID)
|
||||
if node == nil {
|
||||
continue
|
||||
}
|
||||
inputArtifacts := gatherInputArtifacts(*workflow, executionMap, node.ID)
|
||||
execution := executionMap[node.ID]
|
||||
if execution.NodeID == "" {
|
||||
execution = domain.NodeExecutionRecord{
|
||||
NodeID: node.ID,
|
||||
NodeLabel: node.Data.Label,
|
||||
Status: domain.NodeExecutionStatusRunning,
|
||||
StartedAt: nowISO(),
|
||||
Logs: []domain.LogEntryRecord{logEntry("INFO", "Starting node execution", node.Data.Label)},
|
||||
Artifacts: []domain.ExecutionArtifact{},
|
||||
ResourceSeries: []domain.ResourceSample{},
|
||||
}
|
||||
}
|
||||
task := domain.AuditTaskRecord{
|
||||
ID: domain.NewID("audit"),
|
||||
RunID: currentRun.ID,
|
||||
WorkflowID: workflow.ID,
|
||||
ProjectID: workflow.ProjectID,
|
||||
NodeID: node.ID,
|
||||
WorkflowName: workflow.Name,
|
||||
ProjectName: currentRun.ProjectName,
|
||||
OperatorName: node.Data.Label,
|
||||
CreatedAt: nowISO(),
|
||||
UpdatedAt: nowISO(),
|
||||
Status: domain.AuditStatusPending,
|
||||
Data: createAuditPayload(*node, inputArtifacts),
|
||||
}
|
||||
if _, err := runtime.store.CreateAuditTask(ctx, task); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
execution.Status = domain.NodeExecutionStatusWaitingForAudit
|
||||
execution.EndedAt = nowISO()
|
||||
execution.Artifacts = cloneArtifacts(inputArtifacts)
|
||||
execution.Logs = append(execution.Logs, logEntry("WARN", "Node is waiting for human review", node.Data.Label))
|
||||
executionMap[node.ID] = execution
|
||||
applyExecution(currentRun, execution)
|
||||
currentRun.Status = domain.RunStatusWaitingForAudit
|
||||
syncCurrentNodeID(currentRun)
|
||||
currentRun.UpdatedAt = nowISO()
|
||||
if err := runtime.store.UpsertRun(ctx, *currentRun); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
workflow.Status = domain.WorkflowStatusRunning
|
||||
workflow.UpdatedAt = nowISO()
|
||||
if err := runtime.store.UpsertWorkflow(ctx, *workflow); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return currentRun, nil
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
outcome := <-results
|
||||
delete(runningNodes, outcome.nodeID)
|
||||
execution := executionMap[outcome.nodeID]
|
||||
if outcome.err != nil {
|
||||
cancelExecution()
|
||||
execution.Status = domain.NodeExecutionStatusFailed
|
||||
execution.EndedAt = nowISO()
|
||||
execution.Error = normalizeRunError(workflow, outcome.err, executionCtx, outcome.nodeTimedOut)
|
||||
execution.Logs = append(execution.Logs, logEntry("ERROR", execution.Error, execution.NodeLabel))
|
||||
executionMap[outcome.nodeID] = execution
|
||||
applyExecution(currentRun, execution)
|
||||
currentRun.Status = domain.RunStatusFailed
|
||||
currentRun.CurrentNodeID = outcome.nodeID
|
||||
currentRun.EndedAt = nowISO()
|
||||
currentRun.Error = execution.Error
|
||||
currentRun.UpdatedAt = nowISO()
|
||||
if err := runtime.store.UpsertRun(ctx, *currentRun); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
workflow.Status = domain.WorkflowStatusFailed
|
||||
workflow.UpdatedAt = nowISO()
|
||||
if err := runtime.store.UpsertWorkflow(ctx, *workflow); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if shouldAutoRetry(workflow, currentRun) {
|
||||
retryRun, err := runtime.enqueueRetryRun(ctx, workflow, currentRun)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if retryRun != nil {
|
||||
appendLog(currentRun, logEntry("WARN", fmt.Sprintf("已自动创建重试运行,第 %d 次尝试已进入队列", retryRun.Attempt), execution.NodeLabel))
|
||||
currentRun.UpdatedAt = nowISO()
|
||||
if err := runtime.store.UpsertRun(ctx, *currentRun); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
return currentRun, nil
|
||||
}
|
||||
|
||||
execution.Status = domain.NodeExecutionStatusCompleted
|
||||
execution.EndedAt = nowISO()
|
||||
execution.Logs = append(execution.Logs, outcome.result.Logs...)
|
||||
execution.Artifacts = outcome.result.Artifacts
|
||||
execution.ResourceSeries = outcome.result.ResourceSeries
|
||||
executionMap[outcome.nodeID] = execution
|
||||
applyExecution(currentRun, execution)
|
||||
if outcome.reference != nil {
|
||||
currentRun.K8sRefs = append(currentRun.K8sRefs, domain.K8sRef{NodeID: outcome.nodeID, JobName: outcome.reference.JobName, Namespace: outcome.reference.Namespace})
|
||||
}
|
||||
enqueueDependents(*workflow, executionMap, &readyQueue, readySet, outcome.nodeID)
|
||||
currentRun.Status = domain.RunStatusRunning
|
||||
syncCurrentNodeID(currentRun)
|
||||
currentRun.UpdatedAt = nowISO()
|
||||
if err := runtime.store.UpsertRun(ctx, *currentRun); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
currentRun.Status = domain.RunStatusCompleted
|
||||
currentRun.EndedAt = nowISO()
|
||||
syncCurrentNodeID(currentRun)
|
||||
currentRun.UpdatedAt = nowISO()
|
||||
if err := runtime.store.UpsertRun(ctx, *currentRun); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
workflow.Status = domain.WorkflowStatusCompleted
|
||||
workflow.LastRunID = currentRun.ID
|
||||
workflow.LastRunAt = nowISO()
|
||||
workflow.UpdatedAt = nowISO()
|
||||
if err := runtime.store.UpsertWorkflow(ctx, *workflow); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return currentRun, nil
|
||||
}
|
||||
|
||||
func edgeTargets(edges []domain.WorkflowEdge) []string {
|
||||
items := make([]string, 0, len(edges))
|
||||
for _, edge := range edges {
|
||||
items = append(items, edge.Target)
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func (runtime *Runtime) ProcessOnce(ctx context.Context) (*domain.RunRecord, error) {
|
||||
if err := runtime.enqueueStreamingWorkflows(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
claimed, err := runtime.store.ClaimNextQueuedRun(ctx)
|
||||
if err != nil || claimed == nil {
|
||||
return claimed, err
|
||||
}
|
||||
return runtime.processRun(ctx, claimed)
|
||||
}
|
||||
1338
internal/worker/runtime_test.go
Normal file
1338
internal/worker/runtime_test.go
Normal file
File diff suppressed because it is too large
Load Diff
419
k8s/droboticflow.template.yaml
Normal file
419
k8s/droboticflow.template.yaml
Normal file
@ -0,0 +1,419 @@
|
||||
apiVersion: v1
|
||||
kind: Namespace
|
||||
metadata:
|
||||
name: __NAMESPACE__
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: ServiceAccount
|
||||
metadata:
|
||||
name: droboticflow-worker
|
||||
namespace: __NAMESPACE__
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: Role
|
||||
metadata:
|
||||
name: droboticflow-worker
|
||||
namespace: __NAMESPACE__
|
||||
rules:
|
||||
- apiGroups: ["batch"]
|
||||
resources: ["jobs"]
|
||||
verbs: ["create", "get", "list", "watch", "delete"]
|
||||
- apiGroups: ["batch"]
|
||||
resources: ["jobs/status"]
|
||||
verbs: ["get"]
|
||||
- apiGroups: [""]
|
||||
resources: ["pods", "pods/log"]
|
||||
verbs: ["get", "list", "watch"]
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: RoleBinding
|
||||
metadata:
|
||||
name: droboticflow-worker
|
||||
namespace: __NAMESPACE__
|
||||
subjects:
|
||||
- kind: ServiceAccount
|
||||
name: droboticflow-worker
|
||||
namespace: __NAMESPACE__
|
||||
roleRef:
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
kind: Role
|
||||
name: droboticflow-worker
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: droboticflow-config
|
||||
namespace: __NAMESPACE__
|
||||
data:
|
||||
PORT: "3001"
|
||||
WORKER_PORT: "3002"
|
||||
MONGO_DB: "__MONGO_DB__"
|
||||
MONGO_CONNECT_RETRIES: "__MONGO_CONNECT_RETRIES__"
|
||||
MONGO_CONNECT_RETRY_DELAY_MS: "__MONGO_CONNECT_RETRY_DELAY_MS__"
|
||||
WORKER_POLL_INTERVAL_MS: "__WORKER_POLL_INTERVAL_MS__"
|
||||
K8S_NAMESPACE: "__NAMESPACE__"
|
||||
K8S_IMAGE_PULL_SECRET_NAME: "__K8S_IMAGE_PULL_SECRET_NAME__"
|
||||
OPERATOR_RUNNER_IMAGE: "__OPERATOR_RUNNER_IMAGE__"
|
||||
AI_API_BASE_URL: "__AI_API_BASE_URL__"
|
||||
AI_MODEL: "__AI_MODEL__"
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: mongodb-init
|
||||
namespace: __NAMESPACE__
|
||||
data:
|
||||
init-app-user.sh: |
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
mongosh --quiet <<EOF
|
||||
use ${MONGO_DB}
|
||||
db.createUser({
|
||||
user: "${MONGO_APP_USERNAME}",
|
||||
pwd: "${MONGO_APP_PASSWORD}",
|
||||
roles: [{ role: "readWrite", db: "${MONGO_DB}" }]
|
||||
})
|
||||
EOF
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: mongodb
|
||||
namespace: __NAMESPACE__
|
||||
spec:
|
||||
selector:
|
||||
app: mongodb
|
||||
ports:
|
||||
- name: mongodb
|
||||
port: 27017
|
||||
targetPort: 27017
|
||||
---
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: mongodb
|
||||
namespace: __NAMESPACE__
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: mongodb
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: mongodb
|
||||
spec:
|
||||
imagePullSecrets:
|
||||
- name: droboticflow-registry
|
||||
containers:
|
||||
- name: mongodb
|
||||
image: __MONGO_IMAGE__
|
||||
imagePullPolicy: IfNotPresent
|
||||
ports:
|
||||
- containerPort: 27017
|
||||
name: mongodb
|
||||
env:
|
||||
- name: MONGO_INITDB_ROOT_USERNAME
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: droboticflow-mongo-secret
|
||||
key: MONGO_ROOT_USERNAME
|
||||
- name: MONGO_INITDB_ROOT_PASSWORD
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: droboticflow-mongo-secret
|
||||
key: MONGO_ROOT_PASSWORD
|
||||
- name: MONGO_APP_USERNAME
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: droboticflow-mongo-secret
|
||||
key: MONGO_APP_USERNAME
|
||||
- name: MONGO_APP_PASSWORD
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: droboticflow-mongo-secret
|
||||
key: MONGO_APP_PASSWORD
|
||||
- name: MONGO_DB
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: droboticflow-config
|
||||
key: MONGO_DB
|
||||
volumeMounts:
|
||||
- name: mongodb-data
|
||||
mountPath: /data/db
|
||||
- name: mongodb-init
|
||||
mountPath: /docker-entrypoint-initdb.d
|
||||
readinessProbe:
|
||||
tcpSocket:
|
||||
port: 27017
|
||||
initialDelaySeconds: 10
|
||||
periodSeconds: 10
|
||||
livenessProbe:
|
||||
tcpSocket:
|
||||
port: 27017
|
||||
initialDelaySeconds: 20
|
||||
periodSeconds: 20
|
||||
resources:
|
||||
requests:
|
||||
cpu: 500m
|
||||
memory: 512Mi
|
||||
limits:
|
||||
cpu: "1"
|
||||
memory: 1Gi
|
||||
volumes:
|
||||
- name: mongodb-data
|
||||
emptyDir: {}
|
||||
- name: mongodb-init
|
||||
configMap:
|
||||
name: mongodb-init
|
||||
defaultMode: 493
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: droboticflow-api
|
||||
namespace: __NAMESPACE__
|
||||
spec:
|
||||
type: NodePort
|
||||
selector:
|
||||
app: droboticflow-api
|
||||
ports:
|
||||
- name: http
|
||||
port: 80
|
||||
targetPort: 3001
|
||||
nodePort: __API_NODE_PORT__
|
||||
---
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: droboticflow-api
|
||||
namespace: __NAMESPACE__
|
||||
spec:
|
||||
replicas: __API_REPLICAS__
|
||||
minReadySeconds: 5
|
||||
strategy:
|
||||
type: RollingUpdate
|
||||
rollingUpdate:
|
||||
maxUnavailable: 0
|
||||
maxSurge: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: droboticflow-api
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: droboticflow-api
|
||||
spec:
|
||||
terminationGracePeriodSeconds: 30
|
||||
imagePullSecrets:
|
||||
- name: droboticflow-registry
|
||||
containers:
|
||||
- name: api
|
||||
image: __API_IMAGE__
|
||||
imagePullPolicy: Always
|
||||
ports:
|
||||
- containerPort: 3001
|
||||
name: http
|
||||
env:
|
||||
- name: PORT
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: droboticflow-config
|
||||
key: PORT
|
||||
- name: MONGO_DB
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: droboticflow-config
|
||||
key: MONGO_DB
|
||||
- name: MONGO_CONNECT_RETRIES
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: droboticflow-config
|
||||
key: MONGO_CONNECT_RETRIES
|
||||
- name: MONGO_CONNECT_RETRY_DELAY_MS
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: droboticflow-config
|
||||
key: MONGO_CONNECT_RETRY_DELAY_MS
|
||||
- name: MONGO_APP_USERNAME
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: droboticflow-mongo-secret
|
||||
key: MONGO_APP_USERNAME
|
||||
- name: MONGO_APP_PASSWORD
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: droboticflow-mongo-secret
|
||||
key: MONGO_APP_PASSWORD
|
||||
- name: MONGO_URI
|
||||
value: mongodb://$(MONGO_APP_USERNAME):$(MONGO_APP_PASSWORD)@mongodb:27017/$(MONGO_DB)?authSource=$(MONGO_DB)
|
||||
- name: DIST_DIR
|
||||
value: /app/dist
|
||||
- name: AI_API_BASE_URL
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: droboticflow-config
|
||||
key: AI_API_BASE_URL
|
||||
- name: AI_MODEL
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: droboticflow-config
|
||||
key: AI_MODEL
|
||||
- name: AI_API_KEY
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: droboticflow-ai-secret
|
||||
key: AI_API_KEY
|
||||
lifecycle:
|
||||
preStop:
|
||||
exec:
|
||||
command:
|
||||
- /bin/sh
|
||||
- -c
|
||||
- sleep 5
|
||||
readinessProbe:
|
||||
httpGet:
|
||||
path: /api/health
|
||||
port: http
|
||||
initialDelaySeconds: 10
|
||||
periodSeconds: 10
|
||||
livenessProbe:
|
||||
httpGet:
|
||||
path: /api/health
|
||||
port: http
|
||||
initialDelaySeconds: 20
|
||||
periodSeconds: 20
|
||||
resources:
|
||||
requests:
|
||||
cpu: 250m
|
||||
memory: 256Mi
|
||||
limits:
|
||||
cpu: "1"
|
||||
memory: 1Gi
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: droboticflow-worker
|
||||
namespace: __NAMESPACE__
|
||||
spec:
|
||||
selector:
|
||||
app: droboticflow-worker
|
||||
ports:
|
||||
- name: http
|
||||
port: 3002
|
||||
targetPort: 3002
|
||||
---
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: droboticflow-worker
|
||||
namespace: __NAMESPACE__
|
||||
spec:
|
||||
replicas: __WORKER_REPLICAS__
|
||||
minReadySeconds: 5
|
||||
strategy:
|
||||
type: RollingUpdate
|
||||
rollingUpdate:
|
||||
maxUnavailable: 0
|
||||
maxSurge: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: droboticflow-worker
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: droboticflow-worker
|
||||
spec:
|
||||
terminationGracePeriodSeconds: 30
|
||||
serviceAccountName: droboticflow-worker
|
||||
imagePullSecrets:
|
||||
- name: droboticflow-registry
|
||||
containers:
|
||||
- name: worker
|
||||
image: __WORKER_IMAGE__
|
||||
imagePullPolicy: Always
|
||||
ports:
|
||||
- containerPort: 3002
|
||||
name: http
|
||||
env:
|
||||
- name: WORKER_PORT
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: droboticflow-config
|
||||
key: WORKER_PORT
|
||||
- name: WORKER_POLL_INTERVAL_MS
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: droboticflow-config
|
||||
key: WORKER_POLL_INTERVAL_MS
|
||||
- name: MONGO_DB
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: droboticflow-config
|
||||
key: MONGO_DB
|
||||
- name: MONGO_CONNECT_RETRIES
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: droboticflow-config
|
||||
key: MONGO_CONNECT_RETRIES
|
||||
- name: MONGO_CONNECT_RETRY_DELAY_MS
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: droboticflow-config
|
||||
key: MONGO_CONNECT_RETRY_DELAY_MS
|
||||
- name: K8S_NAMESPACE
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: droboticflow-config
|
||||
key: K8S_NAMESPACE
|
||||
- name: OPERATOR_RUNNER_IMAGE
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: droboticflow-config
|
||||
key: OPERATOR_RUNNER_IMAGE
|
||||
- name: K8S_IMAGE_PULL_SECRET_NAME
|
||||
valueFrom:
|
||||
configMapKeyRef:
|
||||
name: droboticflow-config
|
||||
key: K8S_IMAGE_PULL_SECRET_NAME
|
||||
- name: MONGO_APP_USERNAME
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: droboticflow-mongo-secret
|
||||
key: MONGO_APP_USERNAME
|
||||
- name: MONGO_APP_PASSWORD
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: droboticflow-mongo-secret
|
||||
key: MONGO_APP_PASSWORD
|
||||
- name: MONGO_URI
|
||||
value: mongodb://$(MONGO_APP_USERNAME):$(MONGO_APP_PASSWORD)@mongodb:27017/$(MONGO_DB)?authSource=$(MONGO_DB)
|
||||
- name: K8S_ENABLED
|
||||
value: "true"
|
||||
lifecycle:
|
||||
preStop:
|
||||
exec:
|
||||
command:
|
||||
- /bin/sh
|
||||
- -c
|
||||
- sleep 5
|
||||
readinessProbe:
|
||||
httpGet:
|
||||
path: /worker-api/health
|
||||
port: http
|
||||
initialDelaySeconds: 10
|
||||
periodSeconds: 10
|
||||
livenessProbe:
|
||||
httpGet:
|
||||
path: /worker-api/health
|
||||
port: http
|
||||
initialDelaySeconds: 20
|
||||
periodSeconds: 20
|
||||
resources:
|
||||
requests:
|
||||
cpu: 250m
|
||||
memory: 256Mi
|
||||
limits:
|
||||
cpu: "1"
|
||||
memory: 1Gi
|
||||
@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "EmboFlow",
|
||||
"name": "DroboticFlow",
|
||||
"description": "面向具身智能场景的无代码数据处理平台,支持 K8s 算子编排与自动化流程管理。",
|
||||
"requestFramePermissions": []
|
||||
}
|
||||
|
||||
2207
package-lock.json
generated
2207
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
41
package.json
41
package.json
@ -1,24 +1,42 @@
|
||||
{
|
||||
"name": "react-example",
|
||||
"name": "droboticflow",
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "tsx server.ts",
|
||||
"build": "vite build",
|
||||
"dev": "concurrently -k -n api,web \"npm:dev:api\" \"npm:dev:web\"",
|
||||
"dev:full": "concurrently -k -n api,worker,web \"npm:dev:api\" \"npm:dev:worker\" \"npm:dev:web\"",
|
||||
"dev:api": "go run ./cmd/api",
|
||||
"dev:worker": "go run ./cmd/worker",
|
||||
"dev:web": "vite",
|
||||
"build": "npm run build:web && npm run build:go",
|
||||
"build:web": "vite build",
|
||||
"build:go": "mkdir -p build && go build -o build/droboticflow-api ./cmd/api && go build -o build/droboticflow-worker ./cmd/worker",
|
||||
"start": "./build/droboticflow-api",
|
||||
"start:api": "./build/droboticflow-api",
|
||||
"start:worker": "./build/droboticflow-worker",
|
||||
"start:operator-runner": "python3 backend/operator-runner/main.py",
|
||||
"preview": "vite preview",
|
||||
"clean": "rm -rf dist",
|
||||
"lint": "tsc --noEmit"
|
||||
"clean": "rm -rf dist build",
|
||||
"lint": "npm run lint:web && npm run lint:go",
|
||||
"lint:web": "tsc --noEmit",
|
||||
"lint:go": "bash -lc 'test -z \"$(gofmt -l cmd internal)\"'",
|
||||
"test": "go test ./...",
|
||||
"verify": "npm run lint && npm test && npm run build",
|
||||
"docker:build:api": "docker build -f Dockerfile.api -t droboticflow-api:local .",
|
||||
"docker:build:worker": "docker build -f Dockerfile.worker -t droboticflow-worker:local .",
|
||||
"docker:build:operator-runner": "docker build -f Dockerfile.operator-runner -t droboticflow-operator-runner:local .",
|
||||
"k8s:render": "./scripts/render-manifests.sh",
|
||||
"k8s:deploy": "./scripts/deploy-k8s.sh",
|
||||
"smoke": "./scripts/smoke-test.sh",
|
||||
"reconcile:data": "./scripts/reconcile-system-data.sh"
|
||||
},
|
||||
"dependencies": {
|
||||
"@google/genai": "^1.48.0",
|
||||
"@tailwindcss/vite": "^4.1.14",
|
||||
"@types/dagre": "^0.7.54",
|
||||
"@vitejs/plugin-react": "^5.0.4",
|
||||
"clsx": "^2.1.1",
|
||||
"dagre": "^0.8.5",
|
||||
"dotenv": "^17.2.3",
|
||||
"express": "^4.21.2",
|
||||
"lucide-react": "^0.546.0",
|
||||
"motion": "^12.23.24",
|
||||
"react": "^19.0.0",
|
||||
@ -27,14 +45,15 @@
|
||||
"reactflow": "^11.11.4",
|
||||
"recharts": "^3.8.1",
|
||||
"tailwind-merge": "^3.5.0",
|
||||
"vite": "^6.2.0"
|
||||
"vite": "^6.2.0",
|
||||
"zod": "^4.1.11"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/express": "^4.17.21",
|
||||
"@types/node": "^22.14.0",
|
||||
"autoprefixer": "^10.4.21",
|
||||
"concurrently": "^9.1.2",
|
||||
"playwright": "^1.59.1",
|
||||
"tailwindcss": "^4.1.14",
|
||||
"tsx": "^4.21.0",
|
||||
"typescript": "~5.8.2",
|
||||
"vite": "^6.2.0"
|
||||
}
|
||||
|
||||
41
scripts/build-and-push-images.sh
Executable file
41
scripts/build-and-push-images.sh
Executable file
@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
|
||||
REGISTRY_HOST="${REGISTRY_HOST:-ccr-29eug8s3-pub.cnc.bj.baidubce.com}"
|
||||
REGISTRY_NAMESPACE="${REGISTRY_NAMESPACE:-dataflow}"
|
||||
IMAGE_TAG="${IMAGE_TAG:-$(git -C "$ROOT_DIR" rev-parse --short HEAD 2>/dev/null || date +%Y%m%d%H%M%S)}"
|
||||
PLATFORM="${PLATFORM:-linux/amd64}"
|
||||
PUSH="${PUSH:-true}"
|
||||
|
||||
API_IMAGE="${API_IMAGE:-$REGISTRY_HOST/$REGISTRY_NAMESPACE/droboticflow-api:$IMAGE_TAG}"
|
||||
WORKER_IMAGE="${WORKER_IMAGE:-$REGISTRY_HOST/$REGISTRY_NAMESPACE/droboticflow-worker:$IMAGE_TAG}"
|
||||
OPERATOR_RUNNER_IMAGE="${OPERATOR_RUNNER_IMAGE:-$REGISTRY_HOST/$REGISTRY_NAMESPACE/droboticflow-operator-runner:$IMAGE_TAG}"
|
||||
|
||||
docker buildx inspect >/dev/null 2>&1 || docker buildx create --use >/dev/null
|
||||
|
||||
build_image() {
|
||||
local dockerfile="$1"
|
||||
local image="$2"
|
||||
local action_flag="--load"
|
||||
|
||||
if [[ "$PUSH" == "true" ]]; then
|
||||
action_flag="--push"
|
||||
fi
|
||||
|
||||
docker buildx build \
|
||||
--platform "$PLATFORM" \
|
||||
"$action_flag" \
|
||||
-f "$ROOT_DIR/$dockerfile" \
|
||||
-t "$image" \
|
||||
"$ROOT_DIR"
|
||||
}
|
||||
|
||||
build_image "Dockerfile.api" "$API_IMAGE"
|
||||
build_image "Dockerfile.worker" "$WORKER_IMAGE"
|
||||
build_image "Dockerfile.operator-runner" "$OPERATOR_RUNNER_IMAGE"
|
||||
|
||||
printf 'API_IMAGE=%s\n' "$API_IMAGE"
|
||||
printf 'WORKER_IMAGE=%s\n' "$WORKER_IMAGE"
|
||||
printf 'OPERATOR_RUNNER_IMAGE=%s\n' "$OPERATOR_RUNNER_IMAGE"
|
||||
111
scripts/deploy-k8s.sh
Executable file
111
scripts/deploy-k8s.sh
Executable file
@ -0,0 +1,111 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
K8S_NAMESPACE="${K8S_NAMESPACE:-droboticflow}"
|
||||
|
||||
REGISTRY_SERVER="${REGISTRY_SERVER:-ccr-29eug8s3-pub.cnc.bj.baidubce.com}"
|
||||
REGISTRY_USERNAME="${REGISTRY_USERNAME:-}"
|
||||
REGISTRY_PASSWORD="${REGISTRY_PASSWORD:-}"
|
||||
CREATE_REGISTRY_SECRET="${CREATE_REGISTRY_SECRET:-true}"
|
||||
|
||||
MONGO_DB="${MONGO_DB:-droboticflow}"
|
||||
MONGO_CONNECT_RETRIES="${MONGO_CONNECT_RETRIES:-30}"
|
||||
MONGO_CONNECT_RETRY_DELAY_MS="${MONGO_CONNECT_RETRY_DELAY_MS:-2000}"
|
||||
WORKER_POLL_INTERVAL_MS="${WORKER_POLL_INTERVAL_MS:-2000}"
|
||||
K8S_IMAGE_PULL_SECRET_NAME="${K8S_IMAGE_PULL_SECRET_NAME:-droboticflow-registry}"
|
||||
API_NODE_PORT="${API_NODE_PORT:-30080}"
|
||||
API_REPLICAS="${API_REPLICAS:-1}"
|
||||
WORKER_REPLICAS="${WORKER_REPLICAS:-1}"
|
||||
API_IMAGE="${API_IMAGE:-ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-api:latest}"
|
||||
WORKER_IMAGE="${WORKER_IMAGE:-ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-worker:latest}"
|
||||
OPERATOR_RUNNER_IMAGE="${OPERATOR_RUNNER_IMAGE:-ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-operator-runner:latest}"
|
||||
MONGO_IMAGE="${MONGO_IMAGE:-ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-mongo:7-amd64}"
|
||||
MONGO_ROOT_USERNAME="${MONGO_ROOT_USERNAME:-}"
|
||||
MONGO_ROOT_PASSWORD="${MONGO_ROOT_PASSWORD:-}"
|
||||
MONGO_APP_USERNAME="${MONGO_APP_USERNAME:-}"
|
||||
MONGO_APP_PASSWORD="${MONGO_APP_PASSWORD:-}"
|
||||
AI_API_BASE_URL="${AI_API_BASE_URL:-https://coding.dashscope.aliyuncs.com/v1}"
|
||||
AI_MODEL="${AI_MODEL:-glm-5}"
|
||||
AI_API_KEY="${AI_API_KEY:-}"
|
||||
|
||||
read_secret_value() {
|
||||
local secret_name="$1"
|
||||
local key="$2"
|
||||
kubectl -n "$K8S_NAMESPACE" get secret "$secret_name" \
|
||||
-o "jsonpath={.data.$key}" 2>/dev/null | base64 --decode 2>/dev/null || true
|
||||
}
|
||||
|
||||
kubectl get namespace "$K8S_NAMESPACE" >/dev/null 2>&1 || kubectl create namespace "$K8S_NAMESPACE"
|
||||
|
||||
if kubectl -n "$K8S_NAMESPACE" get statefulset mongodb >/dev/null 2>&1; then
|
||||
kubectl -n "$K8S_NAMESPACE" delete statefulset mongodb --ignore-not-found --wait=true
|
||||
kubectl -n "$K8S_NAMESPACE" delete pvc mongodb-data-mongodb-0 --ignore-not-found
|
||||
fi
|
||||
|
||||
if [[ "$CREATE_REGISTRY_SECRET" == "true" ]]; then
|
||||
: "${REGISTRY_USERNAME:?REGISTRY_USERNAME is required when CREATE_REGISTRY_SECRET=true}"
|
||||
: "${REGISTRY_PASSWORD:?REGISTRY_PASSWORD is required when CREATE_REGISTRY_SECRET=true}"
|
||||
|
||||
kubectl -n "$K8S_NAMESPACE" create secret docker-registry droboticflow-registry \
|
||||
--docker-server="$REGISTRY_SERVER" \
|
||||
--docker-username="$REGISTRY_USERNAME" \
|
||||
--docker-password="$REGISTRY_PASSWORD" \
|
||||
--dry-run=client \
|
||||
-o yaml | kubectl apply -f -
|
||||
fi
|
||||
|
||||
if kubectl -n "$K8S_NAMESPACE" get secret droboticflow-mongo-secret >/dev/null 2>&1; then
|
||||
MONGO_ROOT_USERNAME="${MONGO_ROOT_USERNAME:-$(read_secret_value droboticflow-mongo-secret MONGO_ROOT_USERNAME)}"
|
||||
MONGO_ROOT_PASSWORD="${MONGO_ROOT_PASSWORD:-$(read_secret_value droboticflow-mongo-secret MONGO_ROOT_PASSWORD)}"
|
||||
MONGO_APP_USERNAME="${MONGO_APP_USERNAME:-$(read_secret_value droboticflow-mongo-secret MONGO_APP_USERNAME)}"
|
||||
MONGO_APP_PASSWORD="${MONGO_APP_PASSWORD:-$(read_secret_value droboticflow-mongo-secret MONGO_APP_PASSWORD)}"
|
||||
fi
|
||||
|
||||
if kubectl -n "$K8S_NAMESPACE" get secret droboticflow-ai-secret >/dev/null 2>&1; then
|
||||
AI_API_KEY="${AI_API_KEY:-$(read_secret_value droboticflow-ai-secret AI_API_KEY)}"
|
||||
fi
|
||||
|
||||
MONGO_ROOT_USERNAME="${MONGO_ROOT_USERNAME:-root}"
|
||||
MONGO_ROOT_PASSWORD="${MONGO_ROOT_PASSWORD:-$(openssl rand -hex 16)}"
|
||||
MONGO_APP_USERNAME="${MONGO_APP_USERNAME:-droboticflow}"
|
||||
MONGO_APP_PASSWORD="${MONGO_APP_PASSWORD:-$(openssl rand -hex 16)}"
|
||||
|
||||
kubectl -n "$K8S_NAMESPACE" create secret generic droboticflow-mongo-secret \
|
||||
--from-literal=MONGO_ROOT_USERNAME="$MONGO_ROOT_USERNAME" \
|
||||
--from-literal=MONGO_ROOT_PASSWORD="$MONGO_ROOT_PASSWORD" \
|
||||
--from-literal=MONGO_APP_USERNAME="$MONGO_APP_USERNAME" \
|
||||
--from-literal=MONGO_APP_PASSWORD="$MONGO_APP_PASSWORD" \
|
||||
--dry-run=client \
|
||||
-o yaml | kubectl apply -f -
|
||||
|
||||
kubectl -n "$K8S_NAMESPACE" create secret generic droboticflow-ai-secret \
|
||||
--from-literal=AI_API_KEY="$AI_API_KEY" \
|
||||
--dry-run=client \
|
||||
-o yaml | kubectl apply -f -
|
||||
|
||||
env \
|
||||
K8S_NAMESPACE="$K8S_NAMESPACE" \
|
||||
MONGO_DB="$MONGO_DB" \
|
||||
MONGO_CONNECT_RETRIES="$MONGO_CONNECT_RETRIES" \
|
||||
MONGO_CONNECT_RETRY_DELAY_MS="$MONGO_CONNECT_RETRY_DELAY_MS" \
|
||||
WORKER_POLL_INTERVAL_MS="$WORKER_POLL_INTERVAL_MS" \
|
||||
K8S_IMAGE_PULL_SECRET_NAME="$K8S_IMAGE_PULL_SECRET_NAME" \
|
||||
API_NODE_PORT="$API_NODE_PORT" \
|
||||
API_REPLICAS="$API_REPLICAS" \
|
||||
WORKER_REPLICAS="$WORKER_REPLICAS" \
|
||||
AI_API_BASE_URL="$AI_API_BASE_URL" \
|
||||
AI_MODEL="$AI_MODEL" \
|
||||
AI_API_KEY="$AI_API_KEY" \
|
||||
API_IMAGE="$API_IMAGE" \
|
||||
WORKER_IMAGE="$WORKER_IMAGE" \
|
||||
OPERATOR_RUNNER_IMAGE="$OPERATOR_RUNNER_IMAGE" \
|
||||
MONGO_IMAGE="$MONGO_IMAGE" \
|
||||
"$ROOT_DIR/scripts/render-manifests.sh" | kubectl apply -f -
|
||||
|
||||
kubectl -n "$K8S_NAMESPACE" rollout status deployment/mongodb --timeout=300s
|
||||
kubectl -n "$K8S_NAMESPACE" rollout status deployment/droboticflow-api --timeout=300s
|
||||
kubectl -n "$K8S_NAMESPACE" rollout status deployment/droboticflow-worker --timeout=300s
|
||||
|
||||
kubectl -n "$K8S_NAMESPACE" get pods
|
||||
kubectl -n "$K8S_NAMESPACE" get svc droboticflow-api
|
||||
507
scripts/playwright-functional-check.mjs
Normal file
507
scripts/playwright-functional-check.mjs
Normal file
@ -0,0 +1,507 @@
|
||||
import { chromium } from "playwright";
|
||||
|
||||
const baseURL = (process.env.BASE_URL || process.argv[2] || "http://114.111.24.223:30080").replace(/\/+$/, "");
|
||||
const apiBaseURL = `${baseURL}`;
|
||||
const suffix = Date.now().toString(36);
|
||||
|
||||
const names = {
|
||||
admin: `PW Admin ${suffix}`,
|
||||
adminEmail: `pw-admin-${suffix}@playwright.local`,
|
||||
extraUser: `PW Operator ${suffix}`,
|
||||
extraUserEmail: `pw-operator-${suffix}@playwright.local`,
|
||||
project: `PW Project ${suffix}`,
|
||||
workflow: `PW Workflow ${suffix}`,
|
||||
template: `PW Template ${suffix}`,
|
||||
projectTemplate: `PW Project Template ${suffix}`,
|
||||
auditProject: `PW Audit ${suffix}`,
|
||||
};
|
||||
|
||||
function log(step) {
|
||||
process.stdout.write(`[playwright] ${step}\n`);
|
||||
}
|
||||
|
||||
function assert(condition, message) {
|
||||
if (!condition) {
|
||||
throw new Error(message);
|
||||
}
|
||||
}
|
||||
|
||||
async function api(path, init = {}) {
|
||||
let lastError;
|
||||
for (let attempt = 1; attempt <= 6; attempt += 1) {
|
||||
try {
|
||||
const response = await fetch(`${apiBaseURL}${path}`, {
|
||||
...init,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...(init.headers || {}),
|
||||
},
|
||||
});
|
||||
const text = await response.text();
|
||||
if (!response.ok) {
|
||||
throw new Error(`${init.method || "GET"} ${path} failed: ${response.status} ${text}`);
|
||||
}
|
||||
if (!text) {
|
||||
return null;
|
||||
}
|
||||
return JSON.parse(text);
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
if (attempt === 6) {
|
||||
break;
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, Math.min(attempt, 3) * 1000));
|
||||
}
|
||||
}
|
||||
throw lastError;
|
||||
}
|
||||
|
||||
async function poll(fn, predicate, label, timeoutMs = 120000, intervalMs = 2000) {
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
let lastValue;
|
||||
while (Date.now() < deadline) {
|
||||
lastValue = await fn();
|
||||
if (predicate(lastValue)) {
|
||||
return lastValue;
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
||||
}
|
||||
throw new Error(`Timed out waiting for ${label}. Last value: ${JSON.stringify(lastValue)}`);
|
||||
}
|
||||
|
||||
async function gotoWithRetry(page, url) {
|
||||
let lastError;
|
||||
for (let attempt = 1; attempt <= 6; attempt += 1) {
|
||||
try {
|
||||
await page.goto(url, { waitUntil: "networkidle" });
|
||||
return;
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
if (attempt === 6) {
|
||||
break;
|
||||
}
|
||||
await page.waitForTimeout(Math.min(attempt, 3) * 1000);
|
||||
}
|
||||
}
|
||||
throw lastError;
|
||||
}
|
||||
|
||||
async function clickWithRetry(page, locator) {
|
||||
let lastError;
|
||||
for (let attempt = 1; attempt <= 6; attempt += 1) {
|
||||
try {
|
||||
await locator.waitFor({ state: "visible" });
|
||||
await locator.scrollIntoViewIfNeeded();
|
||||
const box = await locator.boundingBox();
|
||||
if (!box) {
|
||||
throw new Error("Unable to resolve click target bounding box");
|
||||
}
|
||||
await page.mouse.click(box.x + box.width / 2, box.y + box.height / 2);
|
||||
return;
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
if (attempt === 6) {
|
||||
break;
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, Math.min(attempt, 3) * 500));
|
||||
}
|
||||
}
|
||||
throw lastError;
|
||||
}
|
||||
|
||||
async function cleanupTestData() {
|
||||
log("cleanup test data");
|
||||
const users = (await api("/api/users")).items;
|
||||
for (const user of users) {
|
||||
if (String(user.email || "").endsWith("@playwright.local")) {
|
||||
await api(`/api/users/${encodeURIComponent(user.id)}`, { method: "DELETE" });
|
||||
}
|
||||
}
|
||||
|
||||
const projects = (await api("/api/projects")).items;
|
||||
for (const project of projects) {
|
||||
if (String(project.name || "").startsWith("PW ")) {
|
||||
await api(`/api/projects/${encodeURIComponent(project.id)}`, { method: "DELETE" });
|
||||
}
|
||||
}
|
||||
|
||||
const templates = (await api("/api/templates")).items;
|
||||
for (const template of templates) {
|
||||
if (String(template.name || "").startsWith("PW ")) {
|
||||
await api(`/api/templates/${encodeURIComponent(template.id)}`, { method: "DELETE" });
|
||||
}
|
||||
}
|
||||
|
||||
const operators = (await api("/api/operators")).items;
|
||||
for (const operator of operators) {
|
||||
if (String(operator.name || "").startsWith("PW ")) {
|
||||
await api(`/api/operators/${encodeURIComponent(operator.id)}`, { method: "DELETE" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function createAuditWorkflow(projectName, workflowName) {
|
||||
const project = await api("/api/projects", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
name: projectName,
|
||||
description: "Created by Playwright audit setup",
|
||||
}),
|
||||
});
|
||||
|
||||
const workflow = await api(`/api/projects/${project.id}/workflows`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
name: workflowName,
|
||||
description: "Audit workflow created by Playwright",
|
||||
type: "batch",
|
||||
nodes: [
|
||||
{
|
||||
id: "review",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "人工审核 (Human Review)",
|
||||
category: "HUMAN",
|
||||
description: "review",
|
||||
inputs: [{ id: "in-1", name: "json", type: "json" }],
|
||||
outputs: [{ id: "approved", name: "审核通过", type: "json" }],
|
||||
},
|
||||
position: { x: 0, y: 0 },
|
||||
},
|
||||
{
|
||||
id: "upload",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "结果上传",
|
||||
category: "IO",
|
||||
description: "sink",
|
||||
inputs: [{ id: "in-1", name: "json", type: "json" }],
|
||||
outputs: [],
|
||||
},
|
||||
position: { x: 200, y: 0 },
|
||||
},
|
||||
],
|
||||
edges: [{ id: "review-upload", source: "review", sourceHandle: "approved", target: "upload", targetHandle: "in-1" }],
|
||||
}),
|
||||
});
|
||||
|
||||
const run = await api(`/api/workflows/${workflow.id}/run`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ trigger: "manual" }),
|
||||
});
|
||||
|
||||
await poll(
|
||||
async () => api(`/api/runs/${run.id}`),
|
||||
(item) => item.status === "waiting_for_audit",
|
||||
`audit run ${run.id} to enter waiting_for_audit`,
|
||||
120000,
|
||||
3000,
|
||||
);
|
||||
|
||||
return { project, workflow, run };
|
||||
}
|
||||
|
||||
async function fillLabeledInput(page, labelText, value) {
|
||||
const group = page.locator(`label:has-text("${labelText}")`).locator("..");
|
||||
const input = group.locator("input, textarea, select").first();
|
||||
await input.click();
|
||||
await input.fill(value);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
await cleanupTestData();
|
||||
|
||||
const browser = await chromium.launch({ headless: true });
|
||||
const context = await browser.newContext({ viewport: { width: 1440, height: 1100 } });
|
||||
const page = await context.newPage();
|
||||
|
||||
try {
|
||||
log("open bootstrap");
|
||||
await gotoWithRetry(page, baseURL);
|
||||
const bootstrapVisible = await page
|
||||
.getByText("新增真实平台用户")
|
||||
.isVisible({ timeout: 5000 })
|
||||
.catch(() => false);
|
||||
if (bootstrapVisible) {
|
||||
await page.getByPlaceholder("输入真实姓名或账号名").fill(names.admin);
|
||||
await page.getByPlaceholder("name@example.com").fill(names.adminEmail);
|
||||
await page.locator("select").first().selectOption("admin");
|
||||
await page.getByRole("button", { name: "创建并进入系统" }).click();
|
||||
}
|
||||
await page.getByRole("heading", { name: "DroboticFlow 仪表盘" }).waitFor();
|
||||
|
||||
log("settings save round-trip");
|
||||
await page.getByRole("link", { name: "系统配置" }).click();
|
||||
await page.getByRole("heading", { name: "系统配置" }).waitFor();
|
||||
const pollGroup = page.locator('label:has-text("轮询间隔 (ms)")').locator("..");
|
||||
const pollInput = pollGroup.locator("input");
|
||||
const originalPoll = Number(await pollInput.inputValue());
|
||||
await pollInput.fill(String(originalPoll + 1));
|
||||
await page.getByRole("button", { name: /保存配置/ }).click();
|
||||
await page.getByText("配置已保存到后端。").waitFor();
|
||||
await pollInput.fill(String(originalPoll));
|
||||
await page.getByRole("button", { name: /保存配置/ }).click();
|
||||
await page.getByText("配置已保存到后端。").waitFor();
|
||||
|
||||
log("operators create edit delete");
|
||||
await page.getByRole("link", { name: "算子管理" }).click();
|
||||
await page.getByRole("heading", { name: "算子管理" }).waitFor();
|
||||
await page.getByRole("button", { name: "注册算子" }).click();
|
||||
await page.getByPlaceholder("例如: 图像锐化").fill(`PW Operator ${suffix}`);
|
||||
await page.getByPlaceholder("描述该算子的功能与用途...").fill("Playwright created operator");
|
||||
await page.getByRole("button", { name: "确认注册" }).click();
|
||||
await page.getByPlaceholder("搜索算子...").fill(`PW Operator ${suffix}`);
|
||||
await page.getByText(`PW Operator ${suffix}`).waitFor();
|
||||
const operatorCard = page.locator("div").filter({ hasText: `PW Operator ${suffix}` }).first();
|
||||
await operatorCard.getByRole("button", { name: /配置详情|查看详情/ }).click();
|
||||
await page.getByPlaceholder("描述该算子的功能与用途...").fill("Playwright updated operator");
|
||||
await page.getByRole("button", { name: "保存修改" }).click();
|
||||
await page.getByPlaceholder("搜索算子...").fill(`PW Operator ${suffix}`);
|
||||
await page.locator("button:has(svg.lucide-trash-2)").first().click();
|
||||
await page.getByPlaceholder("搜索算子...").fill(`PW Operator ${suffix}`);
|
||||
await page.getByText("没有匹配的算子,请调整搜索条件。").waitFor();
|
||||
await page.getByPlaceholder("搜索算子...").fill("");
|
||||
|
||||
log("projects create and open");
|
||||
await page.getByRole("link", { name: "项目管理" }).click();
|
||||
await page.getByRole("heading", { name: "项目管理" }).waitFor();
|
||||
await page.getByRole("button", { name: "创建项目" }).click();
|
||||
await page.getByPlaceholder("请输入项目名称").fill(names.project);
|
||||
await page.getByPlaceholder("请输入项目描述,例如:处理机械臂抓取过程中的视觉数据").fill("Playwright project");
|
||||
await page.getByRole("button", { name: "立即创建" }).click();
|
||||
await page.getByPlaceholder("搜索项目...").fill(names.project);
|
||||
await page.getByText(names.project).waitFor();
|
||||
|
||||
const projectList = (await api("/api/projects")).items;
|
||||
const project = projectList.find((item) => item.name === names.project);
|
||||
assert(project, "created project not found via api");
|
||||
await gotoWithRetry(page, `${baseURL}/project/${project.id}`);
|
||||
|
||||
log("project detail create workflow");
|
||||
await page.getByRole("heading", { name: names.project }).waitFor();
|
||||
await page.getByRole("button", { name: "新建工作流" }).click();
|
||||
await page.getByPlaceholder("例如: 实时障碍物检测").fill(names.workflow);
|
||||
await page.getByRole("button", { name: "确认创建" }).click();
|
||||
await page.getByRole("button", { name: "运行工作流" }).waitFor();
|
||||
|
||||
const workflows = (await api(`/api/projects/${project.id}/workflows`)).items;
|
||||
const workflow = workflows.find((item) => item.name === names.workflow);
|
||||
assert(workflow, "created workflow not found via api");
|
||||
|
||||
log("seed workflow graph");
|
||||
await api(`/api/workflows/${workflow.id}`, {
|
||||
method: "PUT",
|
||||
body: JSON.stringify({
|
||||
nodes: [
|
||||
{
|
||||
id: "source-1",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "真实数据源",
|
||||
category: "IO",
|
||||
description: "Playwright source node",
|
||||
inputs: [],
|
||||
outputs: [{ id: "out-1", name: "manifest", type: "json" }],
|
||||
hyperparams: [{ key: "SOURCE_TEXT", value: `playwright-${suffix}`, description: "直接注入文本内容" }],
|
||||
},
|
||||
position: { x: 100, y: 100 },
|
||||
},
|
||||
{
|
||||
id: "review-1",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "人工审核 (Human Review)",
|
||||
category: "HUMAN",
|
||||
description: "Playwright review node",
|
||||
inputs: [{ id: "in-1", name: "json", type: "json" }],
|
||||
outputs: [
|
||||
{ id: "approved", name: "审核通过", type: "json" },
|
||||
{ id: "rejected", name: "审核拒绝", type: "json" },
|
||||
],
|
||||
},
|
||||
position: { x: 360, y: 100 },
|
||||
},
|
||||
{
|
||||
id: "upload-1",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "结果上传",
|
||||
category: "IO",
|
||||
description: "Playwright upload node",
|
||||
inputs: [{ id: "in-1", name: "json", type: "json" }],
|
||||
outputs: [],
|
||||
},
|
||||
position: { x: 620, y: 100 },
|
||||
},
|
||||
],
|
||||
edges: [
|
||||
{
|
||||
id: "source-review",
|
||||
source: "source-1",
|
||||
sourceHandle: "out-1",
|
||||
target: "review-1",
|
||||
targetHandle: "in-1",
|
||||
},
|
||||
{
|
||||
id: "review-upload",
|
||||
source: "review-1",
|
||||
sourceHandle: "approved",
|
||||
target: "upload-1",
|
||||
targetHandle: "in-1",
|
||||
},
|
||||
],
|
||||
}),
|
||||
});
|
||||
|
||||
await page.reload({ waitUntil: "networkidle" });
|
||||
await page.getByText("真实数据源").dblclick();
|
||||
const sourceGroup = page.locator('label:has-text("SOURCE_TEXT")').locator("..").locator("..");
|
||||
await sourceGroup.locator("input").fill(`playwright-ui-${suffix}`);
|
||||
await page.getByRole("button", { name: "保存" }).click();
|
||||
|
||||
log("run workflow from editor without redirect");
|
||||
await page.getByRole("button", { name: "运行工作流" }).click();
|
||||
await page.getByRole("button", { name: "查看执行监控" }).waitFor();
|
||||
assert(/\/workflow\//.test(page.url()), `expected to stay on workflow editor, got ${page.url()}`);
|
||||
await page.getByText(/当前节点:/).waitFor();
|
||||
await poll(
|
||||
async () => page.locator(".react-flow__edge.animated").count(),
|
||||
(count) => count > 0,
|
||||
"animated workflow edge on editor",
|
||||
30000,
|
||||
1000,
|
||||
);
|
||||
await poll(
|
||||
async () => {
|
||||
const runList = (await api("/api/runs")).items;
|
||||
return runList.find((item) => item.workflowName === names.workflow) || null;
|
||||
},
|
||||
(item) => Boolean(item?.id && item.status === "waiting_for_audit"),
|
||||
`editor run for ${names.workflow} waiting_for_audit`,
|
||||
120000,
|
||||
2000,
|
||||
);
|
||||
const editorRun = (await api("/api/runs")).items.find((item) => item.workflowName === names.workflow);
|
||||
assert(editorRun, "editor run not found via api");
|
||||
await page.getByRole("button", { name: "查看执行监控" }).click();
|
||||
await page.waitForURL(new RegExp(`/monitoring\\?runId=${editorRun.id}`));
|
||||
await page.getByRole("heading", { name: "执行监控" }).waitFor();
|
||||
await page.getByPlaceholder("搜索运行 ID、工作流或项目...").fill(names.workflow);
|
||||
await page.getByText(names.workflow).waitFor();
|
||||
const runRow = page.locator("tr").filter({ hasText: names.workflow }).first();
|
||||
await runRow.hover();
|
||||
await runRow.getByTitle("查看日志").click();
|
||||
await page.getByRole("heading", { name: "执行日志" }).waitFor();
|
||||
await page.getByRole("button", { name: "关闭" }).click();
|
||||
await runRow.hover();
|
||||
await runRow.getByTitle("查看资源").click();
|
||||
await page.getByRole("heading", { name: "资源监控" }).waitFor();
|
||||
await page.getByRole("button", { name: "关闭" }).click();
|
||||
await runRow.hover();
|
||||
await runRow.getByTitle("查看数据").click();
|
||||
await page.getByRole("heading", { name: "产出数据" }).waitFor();
|
||||
await page.getByRole("button", { name: "关闭" }).click();
|
||||
|
||||
const pendingAudit = await poll(
|
||||
async () => {
|
||||
const tasks = (await api("/api/audit-tasks?status=all")).items;
|
||||
return tasks.find((task) => task.runId === editorRun.id && task.status === "pending") || null;
|
||||
},
|
||||
(item) => Boolean(item?.id),
|
||||
`audit task for editor run ${editorRun.id}`,
|
||||
120000,
|
||||
2000,
|
||||
);
|
||||
await api(`/api/audit-tasks/${pendingAudit.id}/approve`, { method: "POST" });
|
||||
await poll(
|
||||
async () => api(`/api/runs/${editorRun.id}`),
|
||||
(item) => item.status === "completed",
|
||||
`editor run ${editorRun.id} completion after approval`,
|
||||
120000,
|
||||
2000,
|
||||
);
|
||||
|
||||
log("export template from workflow editor");
|
||||
await runRow.dblclick();
|
||||
await page.getByRole("button", { name: "导出模板" }).click();
|
||||
await page.locator('input').filter({ has: page.locator('.. >> text=模板名称') }).first().fill(names.template);
|
||||
await page.getByRole("button", { name: "创建模板" }).click();
|
||||
await page.waitForURL(/type=template/);
|
||||
|
||||
log("templates search and use");
|
||||
await page.getByRole("link", { name: "工作流模板" }).click();
|
||||
await page.getByRole("heading", { name: "工作流模板" }).waitFor();
|
||||
await page.getByPlaceholder("搜索模板...").fill(names.template);
|
||||
await page.getByText(names.template).waitFor();
|
||||
const templateCard = page.locator("div").filter({ hasText: names.template }).first();
|
||||
await templateCard
|
||||
.locator("button")
|
||||
.filter({ hasText: /^使用此模板$/ })
|
||||
.click();
|
||||
await page.getByText("选择目标项目").waitFor();
|
||||
await page.getByRole("button", { name: names.project }).click();
|
||||
await page.getByRole("button", { name: "确认并进入编辑器" }).click();
|
||||
await page.waitForURL(/\/workflow\//);
|
||||
|
||||
log("project detail save as template");
|
||||
await gotoWithRetry(page, `${baseURL}/project/${project.id}`);
|
||||
await page.getByRole("button", { name: "保存为模板" }).click();
|
||||
await page.getByRole("heading", { name: "保存为模板" }).waitFor();
|
||||
await page.locator("select").last().selectOption(workflow.id);
|
||||
await page.locator("input").last().fill(names.projectTemplate);
|
||||
await page.getByRole("button", { name: "保存模板" }).click();
|
||||
await page.waitForURL(/type=template/);
|
||||
|
||||
log("users create edit delete");
|
||||
await page.getByRole("link", { name: "用户管理" }).click();
|
||||
await page.getByRole("heading", { name: "用户管理" }).waitFor();
|
||||
await clickWithRetry(page, page.locator("button").filter({ hasText: /^创建用户$/ }).first());
|
||||
await page.getByRole("heading", { name: "创建用户" }).waitFor();
|
||||
await fillLabeledInput(page, "用户名", names.extraUser);
|
||||
await fillLabeledInput(page, "邮箱地址", names.extraUserEmail);
|
||||
await page.locator('label:has-text("角色权限")').locator("..").locator("select").selectOption("operator");
|
||||
await clickWithRetry(page, page.locator("button").filter({ hasText: /^创建用户$/ }).last());
|
||||
await page.getByText(names.extraUser).waitFor();
|
||||
const userRow = page.locator("tr").filter({ hasText: names.extraUser }).first();
|
||||
await userRow.getByRole("button", { name: "编辑" }).click();
|
||||
await page.locator('label:has-text("角色权限")').locator("..").locator("select").selectOption("auditor");
|
||||
await page.getByRole("button", { name: "保存修改" }).click();
|
||||
await userRow.getByRole("button", { name: "删除" }).click();
|
||||
await page.waitForTimeout(500);
|
||||
|
||||
log("audit approve and reject");
|
||||
const auditOne = await createAuditWorkflow(`${names.auditProject} A`, `PW Audit Workflow A ${suffix}`);
|
||||
const auditTwo = await createAuditWorkflow(`${names.auditProject} B`, `PW Audit Workflow B ${suffix}`);
|
||||
await gotoWithRetry(page, `${baseURL}/audit`);
|
||||
await page.getByRole("heading", { name: /任务审核/ }).waitFor();
|
||||
await page.getByText(`${names.auditProject} A`).click();
|
||||
await page.getByRole("button", { name: /审核通过/ }).click();
|
||||
await page.getByText(`${names.auditProject} B`).click();
|
||||
await page.getByRole("button", { name: /审核拒绝/ }).click();
|
||||
await page.getByRole("button", { name: "已处理" }).click();
|
||||
|
||||
await poll(
|
||||
async () => api(`/api/runs/${auditOne.run.id}`),
|
||||
(item) => item.status === "completed",
|
||||
`audit run ${auditOne.run.id} approved completion`,
|
||||
);
|
||||
await poll(
|
||||
async () => api(`/api/runs/${auditTwo.run.id}`),
|
||||
(item) => item.status === "completed",
|
||||
`audit run ${auditTwo.run.id} rejected completion`,
|
||||
);
|
||||
|
||||
log("functional check completed");
|
||||
} catch (error) {
|
||||
await page.screenshot({ path: `/tmp/droboticflow-playwright-failure-${suffix}.png`, fullPage: true }).catch(() => {});
|
||||
throw error;
|
||||
} finally {
|
||||
await context.close();
|
||||
await browser.close();
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error(error);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
605
scripts/playwright-preview-operators-check.mjs
Normal file
605
scripts/playwright-preview-operators-check.mjs
Normal file
@ -0,0 +1,605 @@
|
||||
import { chromium } from "playwright";
|
||||
|
||||
const baseURL = (process.env.BASE_URL || process.argv[2] || "http://114.111.24.223:30080").replace(/\/+$/, "");
|
||||
const suffix = Date.now().toString(36);
|
||||
const prefix = `PWX ${suffix}`;
|
||||
|
||||
const names = {
|
||||
user: `PWX Admin ${suffix}`,
|
||||
email: `pwx-admin-${suffix}@playwright.local`,
|
||||
project: `${prefix} Project`,
|
||||
previewWorkflow: `${prefix} Preview`,
|
||||
dataOpsWorkflow: `${prefix} DataOps`,
|
||||
csvWorkflow: `${prefix} CSV`,
|
||||
scanWorkflow: `${prefix} Scan`,
|
||||
rosbagWorkflow: `${prefix} ROSBag`,
|
||||
lerobotWorkflow: `${prefix} LeRobot`,
|
||||
};
|
||||
|
||||
const requiredOperators = [
|
||||
"目录扫描",
|
||||
"CSV 转 JSON",
|
||||
"JSON 转 CSV",
|
||||
"JSON 字段提取",
|
||||
"JSON 过滤",
|
||||
"JSON 映射重命名",
|
||||
"文本模板渲染",
|
||||
"条件分流",
|
||||
"LeRobot 数据集概览",
|
||||
"ROSBag 数据概览",
|
||||
"Shell 命令执行",
|
||||
];
|
||||
|
||||
function log(step) {
|
||||
process.stdout.write(`[preview-check] ${step}\n`);
|
||||
}
|
||||
|
||||
function assert(condition, message) {
|
||||
if (!condition) {
|
||||
throw new Error(message);
|
||||
}
|
||||
}
|
||||
|
||||
async function api(path, init = {}) {
|
||||
let lastError;
|
||||
for (let attempt = 1; attempt <= 6; attempt += 1) {
|
||||
try {
|
||||
const response = await fetch(`${baseURL}${path}`, {
|
||||
...init,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...(init.headers || {}),
|
||||
},
|
||||
});
|
||||
const text = await response.text();
|
||||
if (!response.ok) {
|
||||
throw new Error(`${init.method || "GET"} ${path} failed: ${response.status} ${text}`);
|
||||
}
|
||||
if (!text) {
|
||||
return null;
|
||||
}
|
||||
return JSON.parse(text);
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
if (attempt === 6) {
|
||||
break;
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, Math.min(attempt, 3) * 1000));
|
||||
}
|
||||
}
|
||||
throw lastError;
|
||||
}
|
||||
|
||||
async function poll(fn, predicate, label, timeoutMs = 180000, intervalMs = 2000) {
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
let lastValue;
|
||||
while (Date.now() < deadline) {
|
||||
lastValue = await fn();
|
||||
if (predicate(lastValue)) {
|
||||
return lastValue;
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
||||
}
|
||||
throw new Error(`Timed out waiting for ${label}. Last value: ${JSON.stringify(lastValue)}`);
|
||||
}
|
||||
|
||||
async function gotoWithRetry(page, url) {
|
||||
let lastError;
|
||||
for (let attempt = 1; attempt <= 6; attempt += 1) {
|
||||
try {
|
||||
await page.goto(url, { waitUntil: "networkidle" });
|
||||
return;
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
if (attempt === 6) {
|
||||
break;
|
||||
}
|
||||
await page.waitForTimeout(Math.min(attempt, 3) * 1000);
|
||||
}
|
||||
}
|
||||
throw lastError;
|
||||
}
|
||||
|
||||
async function cleanupTestData() {
|
||||
log("cleanup previous PWX test data");
|
||||
const projects = (await api("/api/projects")).items;
|
||||
for (const project of projects) {
|
||||
if (String(project.name || "").startsWith("PWX ")) {
|
||||
await api(`/api/projects/${encodeURIComponent(project.id)}`, { method: "DELETE" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function ensureUIReady(page) {
|
||||
await gotoWithRetry(page, baseURL);
|
||||
const bootstrapVisible = await page.getByText("新增真实平台用户").isVisible({ timeout: 5000 }).catch(() => false);
|
||||
if (bootstrapVisible) {
|
||||
await page.getByPlaceholder("输入真实姓名或账号名").fill(names.user);
|
||||
await page.getByPlaceholder("name@example.com").fill(names.email);
|
||||
await page.locator("select").first().selectOption("admin");
|
||||
await page.getByRole("button", { name: "创建并进入系统" }).click();
|
||||
}
|
||||
await page.getByRole("heading", { name: "DroboticFlow 仪表盘" }).waitFor();
|
||||
}
|
||||
|
||||
async function createProject() {
|
||||
return api("/api/projects", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
name: names.project,
|
||||
description: "Created by preview/operator Playwright check",
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
async function createWorkflow(projectId, name, nodes, edges) {
|
||||
return api(`/api/projects/${projectId}/workflows`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
name,
|
||||
description: `Created by ${name}`,
|
||||
type: "batch",
|
||||
nodes,
|
||||
edges,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
async function runWorkflow(workflowId) {
|
||||
const queued = await api(`/api/workflows/${workflowId}/run`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ trigger: "manual" }),
|
||||
});
|
||||
const finished = await poll(
|
||||
async () => api(`/api/runs/${queued.id}`),
|
||||
(run) => ["completed", "failed", "cancelled", "waiting_for_audit"].includes(run.status),
|
||||
`run ${queued.id} to finish`,
|
||||
180000,
|
||||
3000,
|
||||
);
|
||||
assert(finished.status === "completed", `Run ${queued.id} finished with ${finished.status}: ${finished.error || "unknown error"}`);
|
||||
return finished;
|
||||
}
|
||||
|
||||
async function createWorkflows(projectId) {
|
||||
const previewWorkflow = await createWorkflow(
|
||||
projectId,
|
||||
names.previewWorkflow,
|
||||
[
|
||||
{
|
||||
id: "generate",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "Shell 命令执行",
|
||||
category: "UTIL",
|
||||
description: "Generate preview artifacts",
|
||||
inputs: [],
|
||||
outputs: [{ id: "out-1", name: "result", type: "file" }],
|
||||
hyperparams: [
|
||||
{
|
||||
key: "COMMAND",
|
||||
value: "printf 'name,score\\nalpha,1\\nbeta,2\\n' > report.csv && printf 'hello\\nworld\\n' > notes.txt",
|
||||
},
|
||||
],
|
||||
},
|
||||
position: { x: 0, y: 0 },
|
||||
},
|
||||
],
|
||||
[],
|
||||
);
|
||||
|
||||
const dataOpsWorkflow = await createWorkflow(
|
||||
projectId,
|
||||
names.dataOpsWorkflow,
|
||||
[
|
||||
{
|
||||
id: "generate",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "Shell 命令执行",
|
||||
category: "UTIL",
|
||||
description: "Generate json payload",
|
||||
inputs: [],
|
||||
outputs: [{ id: "out-1", name: "payload", type: "json" }],
|
||||
hyperparams: [
|
||||
{
|
||||
key: "COMMAND",
|
||||
value: "printf '{\"device\":{\"id\":\"robot-1\",\"status\":\"ok\"},\"items\":[{\"kind\":\"keep\",\"value\":1},{\"kind\":\"drop\",\"value\":2}]}' > payload.json",
|
||||
},
|
||||
],
|
||||
},
|
||||
position: { x: 0, y: 0 },
|
||||
},
|
||||
{
|
||||
id: "extract",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "JSON 字段提取",
|
||||
category: "DATA",
|
||||
description: "Extract device id",
|
||||
inputs: [{ id: "in-1", name: "input", type: "json" }],
|
||||
outputs: [{ id: "out-1", name: "extracted", type: "json" }],
|
||||
hyperparams: [{ key: "FIELD_PATH", value: "device.id" }],
|
||||
},
|
||||
position: { x: 220, y: -160 },
|
||||
},
|
||||
{
|
||||
id: "filter",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "JSON 过滤",
|
||||
category: "DATA",
|
||||
description: "Filter keep rows",
|
||||
inputs: [{ id: "in-1", name: "input", type: "json" }],
|
||||
outputs: [{ id: "out-1", name: "filtered", type: "json" }],
|
||||
hyperparams: [
|
||||
{ key: "FIELD_PATH", value: "kind" },
|
||||
{ key: "OPERATOR", value: "eq" },
|
||||
{ key: "EXPECTED_JSON", value: "\"keep\"" },
|
||||
],
|
||||
},
|
||||
position: { x: 220, y: -20 },
|
||||
},
|
||||
{
|
||||
id: "map",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "JSON 映射重命名",
|
||||
category: "DATA",
|
||||
description: "Rename fields",
|
||||
inputs: [{ id: "in-1", name: "input", type: "json" }],
|
||||
outputs: [{ id: "out-1", name: "mapped", type: "json" }],
|
||||
hyperparams: [{ key: "FIELD_MAP_JSON", value: "{\"kind\":\"label\",\"value\":\"score\"}" }],
|
||||
},
|
||||
position: { x: 220, y: 120 },
|
||||
},
|
||||
{
|
||||
id: "csv",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "JSON 转 CSV",
|
||||
category: "DATA",
|
||||
description: "Export csv",
|
||||
inputs: [{ id: "in-1", name: "input", type: "json" }],
|
||||
outputs: [{ id: "out-1", name: "csv", type: "file" }],
|
||||
},
|
||||
position: { x: 460, y: 120 },
|
||||
},
|
||||
{
|
||||
id: "route",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "条件分流",
|
||||
category: "LOGIC",
|
||||
description: "Route rows",
|
||||
inputs: [{ id: "in-1", name: "input", type: "json" }],
|
||||
outputs: [
|
||||
{ id: "matched", name: "matched", type: "json" },
|
||||
{ id: "unmatched", name: "unmatched", type: "json" },
|
||||
],
|
||||
hyperparams: [
|
||||
{ key: "FIELD_PATH", value: "kind" },
|
||||
{ key: "OPERATOR", value: "eq" },
|
||||
{ key: "EXPECTED_JSON", value: "\"keep\"" },
|
||||
],
|
||||
},
|
||||
position: { x: 220, y: 260 },
|
||||
},
|
||||
{
|
||||
id: "keep-upload",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "结果上传",
|
||||
category: "IO",
|
||||
description: "matched sink",
|
||||
inputs: [{ id: "in-1", name: "input", type: "json" }],
|
||||
outputs: [],
|
||||
},
|
||||
position: { x: 500, y: 220 },
|
||||
},
|
||||
{
|
||||
id: "drop-upload",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "结果上传",
|
||||
category: "IO",
|
||||
description: "unmatched sink",
|
||||
inputs: [{ id: "in-1", name: "input", type: "json" }],
|
||||
outputs: [],
|
||||
},
|
||||
position: { x: 500, y: 320 },
|
||||
},
|
||||
],
|
||||
[
|
||||
{ id: "generate-extract", source: "generate", sourceHandle: "out-1", target: "extract", targetHandle: "in-1" },
|
||||
{ id: "generate-filter", source: "generate", sourceHandle: "out-1", target: "filter", targetHandle: "in-1" },
|
||||
{ id: "generate-map", source: "generate", sourceHandle: "out-1", target: "map", targetHandle: "in-1" },
|
||||
{ id: "generate-route", source: "generate", sourceHandle: "out-1", target: "route", targetHandle: "in-1" },
|
||||
{ id: "map-csv", source: "map", sourceHandle: "out-1", target: "csv", targetHandle: "in-1" },
|
||||
{ id: "route-keep", source: "route", sourceHandle: "matched", target: "keep-upload", targetHandle: "in-1" },
|
||||
{ id: "route-drop", source: "route", sourceHandle: "unmatched", target: "drop-upload", targetHandle: "in-1" },
|
||||
],
|
||||
);
|
||||
|
||||
const csvWorkflow = await createWorkflow(
|
||||
projectId,
|
||||
names.csvWorkflow,
|
||||
[
|
||||
{
|
||||
id: "generate",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "Shell 命令执行",
|
||||
category: "UTIL",
|
||||
description: "Generate csv",
|
||||
inputs: [],
|
||||
outputs: [{ id: "out-1", name: "table", type: "file" }],
|
||||
hyperparams: [
|
||||
{
|
||||
key: "COMMAND",
|
||||
value: "printf 'col_a,col_b\\n1,2\\n3,4\\n' > table.csv",
|
||||
},
|
||||
],
|
||||
},
|
||||
position: { x: 0, y: 0 },
|
||||
},
|
||||
{
|
||||
id: "convert",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "CSV 转 JSON",
|
||||
category: "DATA",
|
||||
description: "csv to json",
|
||||
inputs: [{ id: "in-1", name: "input", type: "json" }],
|
||||
outputs: [{ id: "out-1", name: "table", type: "json" }],
|
||||
},
|
||||
position: { x: 240, y: 0 },
|
||||
},
|
||||
],
|
||||
[{ id: "generate-convert", source: "generate", sourceHandle: "out-1", target: "convert", targetHandle: "in-1" }],
|
||||
);
|
||||
|
||||
const scanWorkflow = await createWorkflow(
|
||||
projectId,
|
||||
names.scanWorkflow,
|
||||
[
|
||||
{
|
||||
id: "generate",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "Shell 命令执行",
|
||||
category: "UTIL",
|
||||
description: "Generate directory tree",
|
||||
inputs: [],
|
||||
outputs: [{ id: "out-1", name: "dir", type: "json" }],
|
||||
hyperparams: [
|
||||
{
|
||||
key: "COMMAND",
|
||||
value: "mkdir -p scan-root && printf 'alpha' > scan-root/a.txt && printf '{\"beta\":1}' > scan-root/b.json && printf '{\"sourcePath\":\"'\"$OUTPUT_DIR\"'/scan-root\"}' > manifest.json",
|
||||
},
|
||||
],
|
||||
},
|
||||
position: { x: 0, y: 0 },
|
||||
},
|
||||
{
|
||||
id: "scan",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "目录扫描",
|
||||
category: "IO",
|
||||
description: "directory scan",
|
||||
inputs: [{ id: "in-1", name: "input", type: "json" }],
|
||||
outputs: [{ id: "out-1", name: "summary", type: "json" }],
|
||||
},
|
||||
position: { x: 240, y: 0 },
|
||||
},
|
||||
],
|
||||
[{ id: "generate-scan", source: "generate", sourceHandle: "out-1", target: "scan", targetHandle: "in-1" }],
|
||||
);
|
||||
|
||||
const rosbagWorkflow = await createWorkflow(
|
||||
projectId,
|
||||
names.rosbagWorkflow,
|
||||
[
|
||||
{
|
||||
id: "generate",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "Shell 命令执行",
|
||||
category: "UTIL",
|
||||
description: "Generate rosbag db3",
|
||||
inputs: [],
|
||||
outputs: [{ id: "out-1", name: "db3", type: "file" }],
|
||||
hyperparams: [
|
||||
{
|
||||
key: "COMMAND",
|
||||
value: "python3 - <<'PY'\nimport sqlite3\nconn = sqlite3.connect('bag_0.db3')\ncur = conn.cursor()\ncur.execute('create table topics (id integer primary key, name text, type text, serialization_format text)')\ncur.execute('create table messages (id integer primary key, topic_id integer, timestamp integer, data blob)')\ncur.execute(\"insert into topics (id, name, type, serialization_format) values (1, '/camera', 'sensor_msgs/msg/Image', 'cdr')\")\ncur.execute(\"insert into messages (topic_id, timestamp, data) values (1, 100, X'01')\")\ncur.execute(\"insert into messages (topic_id, timestamp, data) values (1, 200, X'02')\")\nconn.commit()\nconn.close()\nPY",
|
||||
},
|
||||
],
|
||||
},
|
||||
position: { x: 0, y: 0 },
|
||||
},
|
||||
{
|
||||
id: "summary",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "ROSBag 数据概览",
|
||||
category: "ROBOTICS",
|
||||
description: "rosbag summary",
|
||||
inputs: [{ id: "in-1", name: "input", type: "json" }],
|
||||
outputs: [{ id: "out-1", name: "summary", type: "json" }],
|
||||
},
|
||||
position: { x: 240, y: 0 },
|
||||
},
|
||||
],
|
||||
[{ id: "generate-summary", source: "generate", sourceHandle: "out-1", target: "summary", targetHandle: "in-1" }],
|
||||
);
|
||||
|
||||
const lerobotWorkflow = await createWorkflow(
|
||||
projectId,
|
||||
names.lerobotWorkflow,
|
||||
[
|
||||
{
|
||||
id: "generate",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "Shell 命令执行",
|
||||
category: "UTIL",
|
||||
description: "Generate lerobot dataset",
|
||||
inputs: [],
|
||||
outputs: [{ id: "out-1", name: "dataset", type: "file" }],
|
||||
hyperparams: [
|
||||
{
|
||||
key: "COMMAND",
|
||||
value: "mkdir -p dataset/meta dataset/data dataset/videos && printf '{\"robot_type\":\"aloha\",\"fps\":30}' > dataset/meta/info.json && printf '{\"episodes\":2}' > dataset/meta/stats.json && printf '{\"task\":\"pick\"}\\n{\"task\":\"place\"}\\n' > dataset/meta/tasks.jsonl && printf 'PAR1' > dataset/data/episode_000.parquet && printf 'video' > dataset/videos/episode_000.mp4",
|
||||
},
|
||||
],
|
||||
},
|
||||
position: { x: 0, y: 0 },
|
||||
},
|
||||
{
|
||||
id: "summary",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "LeRobot 数据集概览",
|
||||
category: "ROBOTICS",
|
||||
description: "lerobot summary",
|
||||
inputs: [{ id: "in-1", name: "input", type: "json" }],
|
||||
outputs: [{ id: "out-1", name: "summary", type: "json" }],
|
||||
},
|
||||
position: { x: 240, y: 0 },
|
||||
},
|
||||
],
|
||||
[{ id: "generate-summary", source: "generate", sourceHandle: "out-1", target: "summary", targetHandle: "in-1" }],
|
||||
);
|
||||
|
||||
return {
|
||||
previewWorkflow,
|
||||
dataOpsWorkflow,
|
||||
csvWorkflow,
|
||||
scanWorkflow,
|
||||
rosbagWorkflow,
|
||||
lerobotWorkflow,
|
||||
};
|
||||
}
|
||||
|
||||
function artifactByName(items, name) {
|
||||
const item = items.find((artifact) => artifact.name === name);
|
||||
assert(item, `Missing artifact ${name}`);
|
||||
return item;
|
||||
}
|
||||
|
||||
async function openArtifactModal(page, workflowName) {
|
||||
await page.getByRole("link", { name: "执行监控" }).click();
|
||||
await page.getByRole("heading", { name: "执行监控" }).waitFor();
|
||||
const search = page.getByPlaceholder("搜索运行 ID、工作流或项目...");
|
||||
await search.fill(workflowName);
|
||||
const row = page.locator("tr").filter({ hasText: workflowName }).first();
|
||||
await row.waitFor({ state: "visible" });
|
||||
await row.locator('button[title="查看数据"]').click();
|
||||
await page.getByText("产出数据").waitFor();
|
||||
}
|
||||
|
||||
async function closeArtifactModal(page) {
|
||||
await page.getByRole("button", { name: "关闭" }).click();
|
||||
await page.getByRole("button", { name: "关闭" }).waitFor({ state: "hidden" });
|
||||
}
|
||||
|
||||
async function main() {
|
||||
await cleanupTestData();
|
||||
|
||||
log("verify operator catalog");
|
||||
const operators = (await api("/api/operators")).items;
|
||||
for (const name of requiredOperators) {
|
||||
assert(operators.some((item) => item.name === name), `Missing built-in operator ${name}`);
|
||||
}
|
||||
|
||||
log("create workflows");
|
||||
const project = await createProject();
|
||||
const workflows = await createWorkflows(project.id);
|
||||
|
||||
log("run preview workflow");
|
||||
const previewRun = await runWorkflow(workflows.previewWorkflow.id);
|
||||
const previewArtifacts = (await api(`/api/runs/${previewRun.id}/artifacts`)).items;
|
||||
assert(artifactByName(previewArtifacts, "report.csv").data.preview.kind === "table", "report.csv preview should be table");
|
||||
assert(artifactByName(previewArtifacts, "notes.txt").data.preview.kind === "text", "notes.txt preview should be text");
|
||||
|
||||
log("run data ops workflow");
|
||||
const dataOpsRun = await runWorkflow(workflows.dataOpsWorkflow.id);
|
||||
const dataOpsArtifacts = (await api(`/api/runs/${dataOpsRun.id}/artifacts`)).items;
|
||||
assert(artifactByName(dataOpsArtifacts, "extracted.json").data.value === "robot-1", "JSON extract should return robot-1");
|
||||
assert(artifactByName(dataOpsArtifacts, "filtered.json").data.count === 1, "JSON filter should keep one row");
|
||||
const mapped = artifactByName(dataOpsArtifacts, "mapped.json").data.items;
|
||||
assert(Array.isArray(mapped) && mapped[0].label === "keep" && mapped[0].score === 1, "JSON map should rename fields");
|
||||
assert(artifactByName(dataOpsArtifacts, "rows.csv").data.preview.kind === "table", "JSON to CSV should produce previewable csv");
|
||||
assert(artifactByName(dataOpsArtifacts, "matched.json").portId === "matched", "matched artifact should carry portId");
|
||||
assert(artifactByName(dataOpsArtifacts, "unmatched.json").portId === "unmatched", "unmatched artifact should carry portId");
|
||||
|
||||
log("run csv conversion workflow");
|
||||
const csvRun = await runWorkflow(workflows.csvWorkflow.id);
|
||||
const csvArtifacts = (await api(`/api/runs/${csvRun.id}/artifacts`)).items;
|
||||
assert(artifactByName(csvArtifacts, "table.json").data.rowCount === 2, "CSV to JSON should convert two rows");
|
||||
|
||||
log("run directory scan workflow");
|
||||
const scanRun = await runWorkflow(workflows.scanWorkflow.id);
|
||||
const scanArtifacts = (await api(`/api/runs/${scanRun.id}/artifacts`)).items;
|
||||
assert(artifactByName(scanArtifacts, "directory-scan.json").data.summary.fileCount >= 2, "Directory scan should report files");
|
||||
|
||||
log("run rosbag workflow");
|
||||
const rosbagRun = await runWorkflow(workflows.rosbagWorkflow.id);
|
||||
const rosbagArtifacts = (await api(`/api/runs/${rosbagRun.id}/artifacts`)).items;
|
||||
const rosbagPreview = artifactByName(rosbagArtifacts, "rosbag-summary.json").data.preview;
|
||||
assert(rosbagPreview.format === "rosbag2", "ROSBag summary should detect rosbag2 sqlite");
|
||||
assert(Array.isArray(rosbagPreview.topics) && rosbagPreview.topics[0].name === "/camera", "ROSBag summary should include topics");
|
||||
|
||||
log("run lerobot workflow");
|
||||
const lerobotRun = await runWorkflow(workflows.lerobotWorkflow.id);
|
||||
const lerobotArtifacts = (await api(`/api/runs/${lerobotRun.id}/artifacts`)).items;
|
||||
const lerobotPreview = artifactByName(lerobotArtifacts, "lerobot-summary.json").data.preview;
|
||||
assert(lerobotPreview.format === "lerobot", "LeRobot summary should detect dataset");
|
||||
assert(lerobotPreview.info.robot_type === "aloha", "LeRobot summary should expose info.json");
|
||||
|
||||
log("open browser and verify UI previews");
|
||||
const browser = await chromium.launch({ headless: true });
|
||||
const context = await browser.newContext({ viewport: { width: 1440, height: 1100 } });
|
||||
const page = await context.newPage();
|
||||
try {
|
||||
await ensureUIReady(page);
|
||||
|
||||
await page.getByRole("link", { name: "算子管理" }).click();
|
||||
await page.getByRole("heading", { name: "算子管理" }).waitFor();
|
||||
await page.getByPlaceholder("搜索算子...").fill("ROSBag 数据概览");
|
||||
await page.getByText("ROSBag 数据概览").waitFor();
|
||||
await page.getByPlaceholder("搜索算子...").fill("LeRobot 数据集概览");
|
||||
await page.getByText("LeRobot 数据集概览").waitFor();
|
||||
|
||||
await openArtifactModal(page, names.previewWorkflow);
|
||||
await page.getByText("report.csv").click();
|
||||
await page.getByText("alpha").waitFor();
|
||||
await page.getByText("score").waitFor();
|
||||
await page.getByText("notes.txt").click();
|
||||
await page.getByText("hello").waitFor();
|
||||
await closeArtifactModal(page);
|
||||
|
||||
await openArtifactModal(page, names.rosbagWorkflow);
|
||||
await page.getByText("rosbag-summary.json").click();
|
||||
await page.locator("pre").filter({ hasText: '"/camera"' }).first().waitFor();
|
||||
await page.locator("pre").filter({ hasText: '"datasetType": "rosbag2"' }).first().waitFor();
|
||||
await closeArtifactModal(page);
|
||||
|
||||
await openArtifactModal(page, names.lerobotWorkflow);
|
||||
await page.getByText("lerobot-summary.json").click();
|
||||
await page.locator("pre").filter({ hasText: '"robot_type": "aloha"' }).first().waitFor();
|
||||
await page.locator("pre").filter({ hasText: '"format": "lerobot"' }).first().waitFor();
|
||||
await closeArtifactModal(page);
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
|
||||
log("preview and operator check completed");
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error(error);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
348
scripts/reconcile-system-data.sh
Executable file
348
scripts/reconcile-system-data.sh
Executable file
@ -0,0 +1,348 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
API_BASE_URL="${1:-${API_BASE_URL:-http://127.0.0.1:3001}}"
|
||||
K8S_ENDPOINT="${K8S_ENDPOINT:-https://120.48.161.230:6443}"
|
||||
K8S_NAMESPACE="${K8S_NAMESPACE:-droboticflow}"
|
||||
OPERATOR_RUNNER_IMAGE="${OPERATOR_RUNNER_IMAGE:-}"
|
||||
|
||||
if [[ -z "$OPERATOR_RUNNER_IMAGE" && -f /tmp/droboticflow-images-latest.env ]]; then
|
||||
# shellcheck disable=SC1091
|
||||
source /tmp/droboticflow-images-latest.env
|
||||
fi
|
||||
|
||||
: "${OPERATOR_RUNNER_IMAGE:?OPERATOR_RUNNER_IMAGE is required}"
|
||||
|
||||
python3 - "$API_BASE_URL" "$K8S_ENDPOINT" "$K8S_NAMESPACE" "$OPERATOR_RUNNER_IMAGE" <<'PY'
|
||||
import http.client
|
||||
import json
|
||||
import time
|
||||
import sys
|
||||
import urllib.error
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
api_base_url = sys.argv[1].rstrip("/")
|
||||
k8s_endpoint = sys.argv[2]
|
||||
k8s_namespace = sys.argv[3]
|
||||
operator_runner_image = sys.argv[4]
|
||||
|
||||
|
||||
def request(method, path, payload=None, expected=(200, 201, 202, 204)):
|
||||
body = None
|
||||
headers = {"Content-Type": "application/json"}
|
||||
if payload is not None:
|
||||
body = json.dumps(payload).encode("utf-8")
|
||||
last_error = None
|
||||
for attempt in range(1, 7):
|
||||
req = urllib.request.Request(f"{api_base_url}{path}", data=body, headers=headers, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=20) as response:
|
||||
status = response.getcode()
|
||||
raw = response.read().decode("utf-8")
|
||||
if status not in expected:
|
||||
raise SystemExit(f"{method} {path} returned unexpected status {status}: {raw}")
|
||||
if not raw:
|
||||
return None
|
||||
return json.loads(raw)
|
||||
except urllib.error.HTTPError as error:
|
||||
raw = error.read().decode("utf-8", errors="replace")
|
||||
raise SystemExit(f"{method} {path} failed with {error.code}: {raw}") from error
|
||||
except (urllib.error.URLError, http.client.RemoteDisconnected, TimeoutError) as error:
|
||||
last_error = error
|
||||
if attempt == 6:
|
||||
break
|
||||
time.sleep(min(attempt, 3))
|
||||
raise SystemExit(f"{method} {path} failed after retries: {last_error}")
|
||||
|
||||
|
||||
def delete_each(path_prefix, items, predicate):
|
||||
for item in items:
|
||||
if not predicate(item):
|
||||
continue
|
||||
print(f"delete {path_prefix}/{item['id']}")
|
||||
request("DELETE", f"{path_prefix}/{urllib.parse.quote(item['id'])}", expected=(204, 404))
|
||||
|
||||
|
||||
users = request("GET", "/api/users")["items"]
|
||||
projects = request("GET", "/api/projects")["items"]
|
||||
templates = request("GET", "/api/templates")["items"]
|
||||
operators = request("GET", "/api/operators")["items"]
|
||||
settings = request("GET", "/api/settings")
|
||||
|
||||
delete_each(
|
||||
"/api/users",
|
||||
users,
|
||||
lambda item: item.get("id") in {"1", "2", "3"} or item.get("email", "").endswith("@emboflow.ai"),
|
||||
)
|
||||
delete_each(
|
||||
"/api/projects",
|
||||
projects,
|
||||
lambda item: item.get("id") in {"1", "2", "3"} or item.get("description") == "Created by scripts/smoke-test.sh",
|
||||
)
|
||||
delete_each(
|
||||
"/api/templates",
|
||||
templates,
|
||||
lambda item: item.get("id", "").startswith("tpl-"),
|
||||
)
|
||||
delete_each(
|
||||
"/api/operators",
|
||||
operators,
|
||||
lambda item: item.get("id", "").startswith("op-") or str(item.get("image", "")).endswith(":latest"),
|
||||
)
|
||||
|
||||
request(
|
||||
"PUT",
|
||||
"/api/settings",
|
||||
{
|
||||
"k8s": {
|
||||
"enabled": True,
|
||||
"endpoint": k8s_endpoint,
|
||||
"namespace": k8s_namespace,
|
||||
},
|
||||
"storage": {
|
||||
"endpoint": "",
|
||||
"buckets": [],
|
||||
},
|
||||
"worker": {
|
||||
"pollIntervalMs": settings["worker"]["pollIntervalMs"],
|
||||
"maxConcurrentRuns": settings["worker"]["maxConcurrentRuns"],
|
||||
},
|
||||
"defaults": settings["defaults"],
|
||||
},
|
||||
)
|
||||
print("updated system settings")
|
||||
|
||||
builtin_operators = [
|
||||
{
|
||||
"name": "真实数据源",
|
||||
"type": "builtin",
|
||||
"category": "IO",
|
||||
"description": "从 SOURCE_TEXT、SOURCE_JSON、SOURCE_PATH 或上游输入生成真实源数据清单。",
|
||||
"inputs": [],
|
||||
"outputs": [{"id": "out-1", "name": "manifest", "type": "json", "transmission": "bus"}],
|
||||
"image": operator_runner_image,
|
||||
"skillDefinition": "工作流起点。读取真实输入源并生成结构化清单,供下游节点消费。",
|
||||
"hyperparams": [
|
||||
{"key": "SOURCE_TEXT", "value": "", "description": "直接注入文本内容"},
|
||||
{"key": "SOURCE_JSON", "value": "", "description": "直接注入 JSON 内容"},
|
||||
{"key": "SOURCE_PATH", "value": "", "description": "从本地路径读取真实数据"},
|
||||
],
|
||||
},
|
||||
{
|
||||
"name": "模型推理",
|
||||
"type": "builtin",
|
||||
"category": "AI",
|
||||
"description": "执行真实节点输入汇总、命令调用或模型镜像推理。",
|
||||
"inputs": [{"id": "in-1", "name": "input", "type": "json", "transmission": "bus"}],
|
||||
"outputs": [{"id": "out-1", "name": "result", "type": "json", "transmission": "bus"}],
|
||||
"image": operator_runner_image,
|
||||
"skillDefinition": "执行通用推理节点;若提供 COMMAND 或 SCRIPT,会真实执行命令并收集输出。",
|
||||
"hyperparams": [
|
||||
{"key": "COMMAND", "value": "", "description": "容器内执行的命令"},
|
||||
{"key": "SCRIPT", "value": "", "description": "可执行脚本内容"},
|
||||
],
|
||||
},
|
||||
{
|
||||
"name": "数据合并",
|
||||
"type": "builtin",
|
||||
"category": "MATH",
|
||||
"description": "将多个 JSON 输入合并成统一结果。",
|
||||
"inputs": [
|
||||
{"id": "in-1", "name": "left", "type": "json", "transmission": "bus"},
|
||||
{"id": "in-2", "name": "right", "type": "json", "transmission": "bus"},
|
||||
],
|
||||
"outputs": [{"id": "out-1", "name": "merged", "type": "json", "transmission": "bus"}],
|
||||
"image": operator_runner_image,
|
||||
"skillDefinition": "对多个 JSON 输入执行真实合并。",
|
||||
},
|
||||
{
|
||||
"name": "结果上传",
|
||||
"type": "builtin",
|
||||
"category": "IO",
|
||||
"description": "收集上游真实输出并生成上传结果清单。",
|
||||
"inputs": [{"id": "in-1", "name": "result", "type": "json", "transmission": "bus"}],
|
||||
"outputs": [],
|
||||
"image": operator_runner_image,
|
||||
"skillDefinition": "复制或汇总上游产物,生成上传结果清单。",
|
||||
},
|
||||
{
|
||||
"name": "人工审核",
|
||||
"type": "builtin",
|
||||
"category": "HUMAN",
|
||||
"description": "将真实输入送入人工审核并等待审核动作恢复流程。",
|
||||
"inputs": [{"id": "in-1", "name": "candidate", "type": "json", "transmission": "bus"}],
|
||||
"outputs": [
|
||||
{"id": "approved", "name": "approved", "type": "json", "transmission": "bus"},
|
||||
{"id": "rejected", "name": "rejected", "type": "json", "transmission": "bus"},
|
||||
],
|
||||
"image": operator_runner_image,
|
||||
"skillDefinition": "基于真实输入生成审核任务并等待人工处理。",
|
||||
},
|
||||
{
|
||||
"name": "目录扫描",
|
||||
"type": "builtin",
|
||||
"category": "IO",
|
||||
"description": "对真实目录做文件统计、扩展名分布和样例文件扫描。",
|
||||
"inputs": [{"id": "in-1", "name": "input", "type": "json", "transmission": "bus"}],
|
||||
"outputs": [{"id": "out-1", "name": "summary", "type": "json", "transmission": "bus"}],
|
||||
"image": operator_runner_image,
|
||||
"skillDefinition": "读取 SOURCE_PATH 或上游 source manifest,输出真实目录概览。",
|
||||
"hyperparams": [
|
||||
{"key": "SOURCE_PATH", "value": "", "description": "待扫描目录"},
|
||||
],
|
||||
},
|
||||
{
|
||||
"name": "CSV 转 JSON",
|
||||
"type": "builtin",
|
||||
"category": "DATA",
|
||||
"description": "将真实 CSV/TSV 文件转换成结构化 JSON 行列表。",
|
||||
"inputs": [{"id": "in-1", "name": "input", "type": "json", "transmission": "bus"}],
|
||||
"outputs": [{"id": "out-1", "name": "table", "type": "json", "transmission": "bus"}],
|
||||
"image": operator_runner_image,
|
||||
"skillDefinition": "读取 SOURCE_PATH 或上游 source manifest 指向的 csv/tsv 文件,输出完整 JSON 行数据。",
|
||||
"hyperparams": [
|
||||
{"key": "SOURCE_PATH", "value": "", "description": "csv/tsv 文件路径"},
|
||||
],
|
||||
},
|
||||
{
|
||||
"name": "JSON 转 CSV",
|
||||
"type": "builtin",
|
||||
"category": "DATA",
|
||||
"description": "将 JSON 行数据导出为真实 CSV 文件。",
|
||||
"inputs": [{"id": "in-1", "name": "input", "type": "json", "transmission": "bus"}],
|
||||
"outputs": [{"id": "out-1", "name": "csv", "type": "file", "transmission": "bus"}],
|
||||
"image": operator_runner_image,
|
||||
"skillDefinition": "把上游 JSON items/entries 或单条对象导出为真实 CSV 文件,并附带摘要。",
|
||||
},
|
||||
{
|
||||
"name": "JSON 字段提取",
|
||||
"type": "builtin",
|
||||
"category": "DATA",
|
||||
"description": "按字段路径从 JSON 输入中提取指定值。",
|
||||
"inputs": [{"id": "in-1", "name": "input", "type": "json", "transmission": "bus"}],
|
||||
"outputs": [{"id": "out-1", "name": "extracted", "type": "json", "transmission": "bus"}],
|
||||
"image": operator_runner_image,
|
||||
"skillDefinition": "根据 FIELD_PATH 从 JSON 输入中提取字段。",
|
||||
"hyperparams": [
|
||||
{"key": "FIELD_PATH", "value": "", "description": "点分字段路径,例如 metrics.score"},
|
||||
],
|
||||
},
|
||||
{
|
||||
"name": "JSON 过滤",
|
||||
"type": "builtin",
|
||||
"category": "DATA",
|
||||
"description": "按字段条件过滤 JSON 列表。",
|
||||
"inputs": [{"id": "in-1", "name": "input", "type": "json", "transmission": "bus"}],
|
||||
"outputs": [{"id": "out-1", "name": "filtered", "type": "json", "transmission": "bus"}],
|
||||
"image": operator_runner_image,
|
||||
"skillDefinition": "按 FIELD_PATH + OPERATOR + EXPECTED 条件真实过滤 JSON items。",
|
||||
"hyperparams": [
|
||||
{"key": "FIELD_PATH", "value": "", "description": "待判断字段路径"},
|
||||
{"key": "OPERATOR", "value": "eq", "description": "eq/ne/contains/gt/gte/lt/lte/exists/missing"},
|
||||
{"key": "EXPECTED", "value": "", "description": "字符串比较值"},
|
||||
{"key": "EXPECTED_JSON", "value": "", "description": "JSON 格式比较值,优先于 EXPECTED"},
|
||||
],
|
||||
},
|
||||
{
|
||||
"name": "JSON 映射重命名",
|
||||
"type": "builtin",
|
||||
"category": "DATA",
|
||||
"description": "按映射表批量重命名 JSON 字段。",
|
||||
"inputs": [{"id": "in-1", "name": "input", "type": "json", "transmission": "bus"}],
|
||||
"outputs": [{"id": "out-1", "name": "mapped", "type": "json", "transmission": "bus"}],
|
||||
"image": operator_runner_image,
|
||||
"skillDefinition": "根据 FIELD_MAP_JSON 真实执行字段名重映射。",
|
||||
"hyperparams": [
|
||||
{"key": "FIELD_MAP_JSON", "value": "{}", "description": "JSON 字段映射,例如 {\"old\":\"new\"}"},
|
||||
],
|
||||
},
|
||||
{
|
||||
"name": "文本模板渲染",
|
||||
"type": "builtin",
|
||||
"category": "UTIL",
|
||||
"description": "使用上游 JSON 上下文渲染真实文本模板文件。",
|
||||
"inputs": [{"id": "in-1", "name": "input", "type": "json", "transmission": "bus"}],
|
||||
"outputs": [{"id": "out-1", "name": "rendered", "type": "file", "transmission": "bus"}],
|
||||
"image": operator_runner_image,
|
||||
"skillDefinition": "支持 {{field}} 和 Python format 风格占位符,输出真实文本文件。",
|
||||
"hyperparams": [
|
||||
{"key": "TEMPLATE_TEXT", "value": "", "description": "模板文本"},
|
||||
{"key": "OUTPUT_NAME", "value": "rendered.txt", "description": "输出文件名"},
|
||||
],
|
||||
},
|
||||
{
|
||||
"name": "条件分流",
|
||||
"type": "builtin",
|
||||
"category": "LOGIC",
|
||||
"description": "按字段条件将输入拆分到 matched / unmatched 两个真实输出端口。",
|
||||
"inputs": [{"id": "in-1", "name": "input", "type": "json", "transmission": "bus"}],
|
||||
"outputs": [
|
||||
{"id": "matched", "name": "matched", "type": "json", "transmission": "bus"},
|
||||
{"id": "unmatched", "name": "unmatched", "type": "json", "transmission": "bus"},
|
||||
],
|
||||
"image": operator_runner_image,
|
||||
"skillDefinition": "按 FIELD_PATH + OPERATOR + EXPECTED 条件把真实数据分流到两个输出端口。",
|
||||
"hyperparams": [
|
||||
{"key": "FIELD_PATH", "value": "", "description": "待判断字段路径"},
|
||||
{"key": "OPERATOR", "value": "eq", "description": "eq/ne/contains/gt/gte/lt/lte/exists/missing"},
|
||||
{"key": "EXPECTED", "value": "", "description": "字符串比较值"},
|
||||
{"key": "EXPECTED_JSON", "value": "", "description": "JSON 格式比较值,优先于 EXPECTED"},
|
||||
],
|
||||
},
|
||||
{
|
||||
"name": "LeRobot 数据集概览",
|
||||
"type": "builtin",
|
||||
"category": "ROBOTICS",
|
||||
"description": "读取真实 LeRobot 数据集目录,输出 meta/tasks/parquet/videos 概览。",
|
||||
"inputs": [{"id": "in-1", "name": "input", "type": "json", "transmission": "bus"}],
|
||||
"outputs": [{"id": "out-1", "name": "summary", "type": "json", "transmission": "bus"}],
|
||||
"image": operator_runner_image,
|
||||
"skillDefinition": "面向 LeRobot 数据集目录的真实概览算子,适合机器人示教数据检查。",
|
||||
"hyperparams": [
|
||||
{"key": "SOURCE_PATH", "value": "", "description": "LeRobot 数据集根目录"},
|
||||
],
|
||||
},
|
||||
{
|
||||
"name": "ROSBag 数据概览",
|
||||
"type": "builtin",
|
||||
"category": "ROBOTICS",
|
||||
"description": "读取真实 rosbag1 / rosbag2 / mcap / db3 数据并输出概览。",
|
||||
"inputs": [{"id": "in-1", "name": "input", "type": "json", "transmission": "bus"}],
|
||||
"outputs": [{"id": "out-1", "name": "summary", "type": "json", "transmission": "bus"}],
|
||||
"image": operator_runner_image,
|
||||
"skillDefinition": "支持 rosbag db3 topic/message 概览,以及 bag/mcap 文件摘要。",
|
||||
"hyperparams": [
|
||||
{"key": "SOURCE_PATH", "value": "", "description": "bag/db3/mcap 文件或 rosbag2 目录"},
|
||||
],
|
||||
},
|
||||
{
|
||||
"name": "Shell 命令执行",
|
||||
"type": "builtin",
|
||||
"category": "UTIL",
|
||||
"description": "在 operator-runner 容器中真实执行 shell 命令并收集输出文件。",
|
||||
"inputs": [{"id": "in-1", "name": "input", "type": "json", "transmission": "bus"}],
|
||||
"outputs": [{"id": "out-1", "name": "result", "type": "file", "transmission": "bus"}],
|
||||
"image": operator_runner_image,
|
||||
"skillDefinition": "执行 COMMAND 或 SCRIPT,自动收集输出目录产物并生成预览元数据。",
|
||||
"hyperparams": [
|
||||
{"key": "COMMAND", "value": "", "description": "容器内执行的命令"},
|
||||
{"key": "SCRIPT", "value": "", "description": "可执行脚本内容"},
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
operators = request("GET", "/api/operators")["items"]
|
||||
operators_by_name = {item["name"]: item for item in operators}
|
||||
|
||||
for operator in builtin_operators:
|
||||
existing = operators_by_name.get(operator["name"])
|
||||
if existing:
|
||||
print(f"update operator {operator['name']}")
|
||||
request("PUT", f"/api/operators/{urllib.parse.quote(existing['id'])}", operator)
|
||||
else:
|
||||
print(f"create operator {operator['name']}")
|
||||
request("POST", "/api/operators", operator, expected=(201,))
|
||||
|
||||
print("reconciled system data")
|
||||
PY
|
||||
41
scripts/render-manifests.sh
Executable file
41
scripts/render-manifests.sh
Executable file
@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
TEMPLATE="$ROOT_DIR/k8s/droboticflow.template.yaml"
|
||||
|
||||
K8S_NAMESPACE="${K8S_NAMESPACE:-droboticflow}"
|
||||
MONGO_DB="${MONGO_DB:-droboticflow}"
|
||||
MONGO_CONNECT_RETRIES="${MONGO_CONNECT_RETRIES:-30}"
|
||||
MONGO_CONNECT_RETRY_DELAY_MS="${MONGO_CONNECT_RETRY_DELAY_MS:-2000}"
|
||||
WORKER_POLL_INTERVAL_MS="${WORKER_POLL_INTERVAL_MS:-2000}"
|
||||
K8S_IMAGE_PULL_SECRET_NAME="${K8S_IMAGE_PULL_SECRET_NAME:-droboticflow-registry}"
|
||||
API_NODE_PORT="${API_NODE_PORT:-30080}"
|
||||
API_REPLICAS="${API_REPLICAS:-1}"
|
||||
WORKER_REPLICAS="${WORKER_REPLICAS:-1}"
|
||||
AI_API_BASE_URL="${AI_API_BASE_URL:-https://coding.dashscope.aliyuncs.com/v1}"
|
||||
AI_MODEL="${AI_MODEL:-glm-5}"
|
||||
|
||||
API_IMAGE="${API_IMAGE:-ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-api:latest}"
|
||||
WORKER_IMAGE="${WORKER_IMAGE:-ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-worker:latest}"
|
||||
OPERATOR_RUNNER_IMAGE="${OPERATOR_RUNNER_IMAGE:-ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-operator-runner:latest}"
|
||||
MONGO_IMAGE="${MONGO_IMAGE:-ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-mongo:7}"
|
||||
|
||||
rendered="$(cat "$TEMPLATE")"
|
||||
rendered="${rendered//__NAMESPACE__/$K8S_NAMESPACE}"
|
||||
rendered="${rendered//__MONGO_DB__/$MONGO_DB}"
|
||||
rendered="${rendered//__MONGO_CONNECT_RETRIES__/$MONGO_CONNECT_RETRIES}"
|
||||
rendered="${rendered//__MONGO_CONNECT_RETRY_DELAY_MS__/$MONGO_CONNECT_RETRY_DELAY_MS}"
|
||||
rendered="${rendered//__WORKER_POLL_INTERVAL_MS__/$WORKER_POLL_INTERVAL_MS}"
|
||||
rendered="${rendered//__K8S_IMAGE_PULL_SECRET_NAME__/$K8S_IMAGE_PULL_SECRET_NAME}"
|
||||
rendered="${rendered//__API_NODE_PORT__/$API_NODE_PORT}"
|
||||
rendered="${rendered//__API_REPLICAS__/$API_REPLICAS}"
|
||||
rendered="${rendered//__WORKER_REPLICAS__/$WORKER_REPLICAS}"
|
||||
rendered="${rendered//__AI_API_BASE_URL__/$AI_API_BASE_URL}"
|
||||
rendered="${rendered//__AI_MODEL__/$AI_MODEL}"
|
||||
rendered="${rendered//__API_IMAGE__/$API_IMAGE}"
|
||||
rendered="${rendered//__WORKER_IMAGE__/$WORKER_IMAGE}"
|
||||
rendered="${rendered//__OPERATOR_RUNNER_IMAGE__/$OPERATOR_RUNNER_IMAGE}"
|
||||
rendered="${rendered//__MONGO_IMAGE__/$MONGO_IMAGE}"
|
||||
|
||||
printf '%s\n' "$rendered"
|
||||
138
scripts/smoke-test.sh
Executable file
138
scripts/smoke-test.sh
Executable file
@ -0,0 +1,138 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
if [[ $# -gt 0 ]]; then
|
||||
API_BASE_URL="$1"
|
||||
WORKER_BASE_URL="${2:-${WORKER_BASE_URL:-}}"
|
||||
else
|
||||
API_BASE_URL="${API_BASE_URL:-http://127.0.0.1:3001}"
|
||||
WORKER_BASE_URL="${WORKER_BASE_URL:-http://127.0.0.1:3002}"
|
||||
fi
|
||||
|
||||
json_get() {
|
||||
local expression="$1"
|
||||
python3 -c "import json,sys; print(${expression})"
|
||||
}
|
||||
|
||||
api_curl() {
|
||||
curl --retry 6 --retry-delay 2 --retry-connrefused --retry-all-errors -fsS "$@"
|
||||
}
|
||||
|
||||
api_health="$(api_curl "$API_BASE_URL/api/health")"
|
||||
printf 'api health: %s\n' "$api_health"
|
||||
printf '%s' "$api_health" | grep -q '"status":"ok"'
|
||||
|
||||
project_payload="$(python3 - <<'PY'
|
||||
import json
|
||||
print(json.dumps({
|
||||
"name": "Smoke Test Project",
|
||||
"description": "Created by scripts/smoke-test.sh",
|
||||
}))
|
||||
PY
|
||||
)"
|
||||
project_response="$(api_curl -X POST "$API_BASE_URL/api/projects" -H 'Content-Type: application/json' -d "$project_payload")"
|
||||
project_id="$(printf '%s' "$project_response" | json_get 'json.load(sys.stdin)["id"]')"
|
||||
printf 'created project: %s\n' "$project_response"
|
||||
|
||||
workflow_payload="$(python3 - <<'PY'
|
||||
import json
|
||||
print(json.dumps({
|
||||
"name": "Smoke Test Workflow",
|
||||
"description": "End-to-end workflow created by smoke test",
|
||||
"type": "batch",
|
||||
"nodes": [
|
||||
{
|
||||
"id": "source-1",
|
||||
"type": "operator",
|
||||
"data": {
|
||||
"label": "真实数据源",
|
||||
"category": "IO",
|
||||
"description": "Use SOURCE_TEXT to emit a real manifest artifact",
|
||||
"inputs": [],
|
||||
"outputs": [{"id": "out-1", "name": "manifest", "type": "json"}],
|
||||
"hyperparams": [{"key": "SOURCE_TEXT", "value": "smoke-test-payload"}],
|
||||
},
|
||||
"position": {"x": 0, "y": 0},
|
||||
},
|
||||
{
|
||||
"id": "upload-1",
|
||||
"type": "operator",
|
||||
"data": {
|
||||
"label": "结果上传",
|
||||
"category": "IO",
|
||||
"description": "Persist artifacts produced by upstream nodes",
|
||||
"inputs": [{"id": "in-1", "name": "manifest", "type": "json"}],
|
||||
"outputs": [],
|
||||
},
|
||||
"position": {"x": 240, "y": 0},
|
||||
},
|
||||
],
|
||||
"edges": [
|
||||
{
|
||||
"id": "edge-source-upload",
|
||||
"source": "source-1",
|
||||
"sourceHandle": "out-1",
|
||||
"target": "upload-1",
|
||||
"targetHandle": "in-1",
|
||||
}
|
||||
],
|
||||
}))
|
||||
PY
|
||||
)"
|
||||
workflow_response="$(api_curl -X POST "$API_BASE_URL/api/projects/$project_id/workflows" -H 'Content-Type: application/json' -d "$workflow_payload")"
|
||||
workflow_id="$(printf '%s' "$workflow_response" | json_get 'json.load(sys.stdin)["id"]')"
|
||||
printf 'created workflow: %s\n' "$workflow_response"
|
||||
|
||||
queued_run="$(api_curl -X POST "$API_BASE_URL/api/workflows/$workflow_id/run" -H 'Content-Type: application/json' -d '{"trigger":"manual"}')"
|
||||
run_id="$(printf '%s' "$queued_run" | json_get 'json.load(sys.stdin)["id"]')"
|
||||
printf 'queued run: %s\n' "$queued_run"
|
||||
|
||||
maybe_process_once() {
|
||||
if [[ -n "${WORKER_BASE_URL}" ]] && api_curl "$WORKER_BASE_URL/worker-api/health" >/dev/null 2>&1; then
|
||||
api_curl -X POST "$WORKER_BASE_URL/worker-api/process-once" >/dev/null
|
||||
fi
|
||||
}
|
||||
|
||||
if [[ -n "${WORKER_BASE_URL}" ]] && api_curl "$WORKER_BASE_URL/worker-api/health" >/dev/null 2>&1; then
|
||||
worker_health="$(api_curl "$WORKER_BASE_URL/worker-api/health")"
|
||||
printf 'worker health: %s\n' "$worker_health"
|
||||
fi
|
||||
|
||||
deadline=$((SECONDS + 180))
|
||||
final_run=""
|
||||
final_status=""
|
||||
|
||||
while (( SECONDS < deadline )); do
|
||||
maybe_process_once
|
||||
final_run="$(api_curl "$API_BASE_URL/api/runs/$run_id")"
|
||||
final_status="$(printf '%s' "$final_run" | json_get 'json.load(sys.stdin)["status"]')"
|
||||
printf 'run status: %s\n' "$final_status"
|
||||
|
||||
case "$final_status" in
|
||||
completed)
|
||||
break
|
||||
;;
|
||||
failed|cancelled|waiting_for_audit)
|
||||
printf 'unexpected terminal status: %s\n' "$final_run" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
sleep 2
|
||||
done
|
||||
|
||||
if [[ "$final_status" != "completed" ]]; then
|
||||
printf 'run did not complete before timeout: %s\n' "$final_run" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
logs_response="$(api_curl "$API_BASE_URL/api/runs/$run_id/logs")"
|
||||
artifacts_response="$(api_curl "$API_BASE_URL/api/runs/$run_id/artifacts")"
|
||||
printf 'logs: %s\n' "$logs_response"
|
||||
printf 'artifacts: %s\n' "$artifacts_response"
|
||||
|
||||
printf '%s' "$logs_response" | python3 -c 'import json, sys; payload = json.load(sys.stdin); items = payload.get("items", []);
|
||||
if not items: raise SystemExit("expected at least one log entry")'
|
||||
|
||||
printf '%s' "$artifacts_response" | python3 -c 'import json, sys; payload = json.load(sys.stdin); items = payload.get("items", []);
|
||||
if not items: raise SystemExit("expected at least one artifact")'
|
||||
105
server.ts
105
server.ts
@ -1,105 +0,0 @@
|
||||
import express from "express";
|
||||
import { createServer as createViteServer } from "vite";
|
||||
import path from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
/**
|
||||
* EmboFlow Backend Architecture Simulation
|
||||
*
|
||||
* API: Express HTTP Server (this file)
|
||||
* Worker: Background process polling "Mongo"
|
||||
* MongoDB: Simulated in-memory store
|
||||
*/
|
||||
|
||||
// Simulated MongoDB
|
||||
const db = {
|
||||
projects: [
|
||||
{ id: "1", name: "具身智能抓取任务", description: "处理机械臂抓取过程中的视觉数据", createdAt: "2024-03-20" },
|
||||
{ id: "2", name: "自动驾驶感知流程", description: "多传感器融合数据清洗与标注", createdAt: "2024-03-21" },
|
||||
],
|
||||
operators: [
|
||||
{ id: "op-1", name: "数据下载", type: "builtin", category: "IO", description: "从 OSS 下载原始数据" },
|
||||
{ id: "op-2", name: "图像预处理", type: "custom", category: "CV", description: "缩放、归一化图像数据" },
|
||||
{ id: "op-3", name: "模型推理", type: "builtin", category: "AI", description: "运行目标检测模型" },
|
||||
{ id: "op-4", name: "结果上传", type: "builtin", category: "IO", description: "将处理结果上传至 OSS" },
|
||||
],
|
||||
tasks: [] as any[],
|
||||
};
|
||||
|
||||
// Worker Simulation
|
||||
async function runWorker() {
|
||||
console.log("[Worker] Started polling tasks from Mongo...");
|
||||
|
||||
while (true) {
|
||||
const nextTask = db.tasks.find(t => t.status === "pending");
|
||||
if (nextTask) {
|
||||
nextTask.status = "running";
|
||||
console.log(`[Worker] Executing task: ${nextTask.id} (${nextTask.workflowName})`);
|
||||
|
||||
// Simulate long-running job (Docker/K8s)
|
||||
await new Promise(resolve => setTimeout(resolve, 5000));
|
||||
|
||||
nextTask.status = "completed";
|
||||
console.log(`[Worker] Task completed: ${nextTask.id}`);
|
||||
}
|
||||
await new Promise(resolve => setTimeout(resolve, 2000)); // Poll every 2s
|
||||
}
|
||||
}
|
||||
|
||||
async function startServer() {
|
||||
const app = express();
|
||||
const PORT = 3000;
|
||||
|
||||
app.use(express.json());
|
||||
|
||||
// API Routes
|
||||
app.get("/api/health", (req, res) => {
|
||||
res.json({ status: "ok", service: "EmboFlow API" });
|
||||
});
|
||||
|
||||
app.get("/api/projects", (req, res) => {
|
||||
res.json(db.projects);
|
||||
});
|
||||
|
||||
app.get("/api/operators", (req, res) => {
|
||||
res.json(db.operators);
|
||||
});
|
||||
|
||||
// Run Workflow (Push to Mongo for Worker)
|
||||
app.post("/api/workflow/:id/run", (req, res) => {
|
||||
const taskId = `run-${Date.now()}`;
|
||||
db.tasks.push({
|
||||
id: taskId,
|
||||
workflowId: req.params.id,
|
||||
workflowName: req.body.name || "Unknown Workflow",
|
||||
status: "pending",
|
||||
createdAt: new Date().toISOString()
|
||||
});
|
||||
res.json({ success: true, taskId });
|
||||
});
|
||||
|
||||
// Vite middleware for development
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
const vite = await createViteServer({
|
||||
server: { middlewareMode: true },
|
||||
appType: "spa",
|
||||
});
|
||||
app.use(vite.middlewares);
|
||||
} else {
|
||||
const distPath = path.join(process.cwd(), "dist");
|
||||
app.use(express.static(distPath));
|
||||
app.get("*", (req, res) => {
|
||||
res.sendFile(path.join(distPath, "index.html"));
|
||||
});
|
||||
}
|
||||
|
||||
app.listen(PORT, "0.0.0.0", () => {
|
||||
console.log(`[API] EmboFlow Server running on http://localhost:${PORT}`);
|
||||
runWorker(); // Start worker in background
|
||||
});
|
||||
}
|
||||
|
||||
startServer();
|
||||
69
src/App.tsx
69
src/App.tsx
@ -3,41 +3,54 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import React from "react";
|
||||
import React, { Suspense, lazy } from "react";
|
||||
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
|
||||
import { Layout } from "./components/Layout";
|
||||
import { TimeZoneProvider } from "./context/TimeZoneContext";
|
||||
import { UserProvider } from "./context/UserContext";
|
||||
import Dashboard from "./pages/Dashboard";
|
||||
import Projects from "./pages/Projects";
|
||||
import ProjectDetail from "./pages/ProjectDetail";
|
||||
import Operators from "./pages/Operators";
|
||||
import WorkflowEditor from "./pages/WorkflowEditor";
|
||||
import Monitoring from "./pages/Monitoring";
|
||||
import Settings from "./pages/Settings";
|
||||
import Templates from "./pages/Templates";
|
||||
import Users from "./pages/Users";
|
||||
import Audit from "./pages/Audit";
|
||||
|
||||
const Dashboard = lazy(() => import("./pages/Dashboard"));
|
||||
const Projects = lazy(() => import("./pages/Projects"));
|
||||
const ProjectDetail = lazy(() => import("./pages/ProjectDetail"));
|
||||
const Operators = lazy(() => import("./pages/Operators"));
|
||||
const WorkflowEditor = lazy(() => import("./pages/WorkflowEditor"));
|
||||
const Monitoring = lazy(() => import("./pages/Monitoring"));
|
||||
const Settings = lazy(() => import("./pages/Settings"));
|
||||
const Templates = lazy(() => import("./pages/Templates"));
|
||||
const Users = lazy(() => import("./pages/Users"));
|
||||
const Audit = lazy(() => import("./pages/Audit"));
|
||||
|
||||
function RouteFallback() {
|
||||
return (
|
||||
<div className="flex min-h-[320px] items-center justify-center text-sm text-slate-400">
|
||||
正在加载页面...
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function App() {
|
||||
return (
|
||||
<UserProvider>
|
||||
<Router>
|
||||
<Layout>
|
||||
<Routes>
|
||||
<Route path="/" element={<Dashboard />} />
|
||||
<Route path="/projects" element={<Projects />} />
|
||||
<Route path="/project/:id" element={<ProjectDetail />} />
|
||||
<Route path="/operators" element={<Operators />} />
|
||||
<Route path="/workflow/:id" element={<WorkflowEditor />} />
|
||||
<Route path="/monitoring" element={<Monitoring />} />
|
||||
<Route path="/audit" element={<Audit />} />
|
||||
<Route path="/settings" element={<Settings />} />
|
||||
<Route path="/templates" element={<Templates />} />
|
||||
<Route path="/users" element={<Users />} />
|
||||
</Routes>
|
||||
</Layout>
|
||||
</Router>
|
||||
<TimeZoneProvider>
|
||||
<Router>
|
||||
<Layout>
|
||||
<Suspense fallback={<RouteFallback />}>
|
||||
<Routes>
|
||||
<Route path="/" element={<Dashboard />} />
|
||||
<Route path="/projects" element={<Projects />} />
|
||||
<Route path="/project/:id" element={<ProjectDetail />} />
|
||||
<Route path="/operators" element={<Operators />} />
|
||||
<Route path="/workflow/:id" element={<WorkflowEditor />} />
|
||||
<Route path="/monitoring" element={<Monitoring />} />
|
||||
<Route path="/audit" element={<Audit />} />
|
||||
<Route path="/settings" element={<Settings />} />
|
||||
<Route path="/templates" element={<Templates />} />
|
||||
<Route path="/users" element={<Users />} />
|
||||
</Routes>
|
||||
</Suspense>
|
||||
</Layout>
|
||||
</Router>
|
||||
</TimeZoneProvider>
|
||||
</UserProvider>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
import React, { useState } from "react";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { X, Sparkles, Loader2, AlertCircle } from "lucide-react";
|
||||
import { generateWorkflowFromDescription, GeneratedWorkflow } from "../services/aiService";
|
||||
import { MOCK_OPERATORS } from "../constants";
|
||||
import { cn } from "../lib/utils";
|
||||
import type { Operator } from "../types";
|
||||
import { operatorsApi } from "../services/api/operators";
|
||||
import { useAIStatus } from "../hooks/useAIStatus";
|
||||
|
||||
interface AIGenerateModalProps {
|
||||
isOpen: boolean;
|
||||
@ -11,19 +13,32 @@ interface AIGenerateModalProps {
|
||||
}
|
||||
|
||||
export default function AIGenerateModal({ isOpen, onClose, onGenerate }: AIGenerateModalProps) {
|
||||
const aiStatus = useAIStatus();
|
||||
const [description, setDescription] = useState("");
|
||||
const [isGenerating, setIsGenerating] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [availableOperators, setAvailableOperators] = useState<Operator[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isOpen) {
|
||||
return;
|
||||
}
|
||||
operatorsApi.list().then(setAvailableOperators).catch((err) => {
|
||||
console.error("Failed to load operators for AI generation", err);
|
||||
setAvailableOperators([]);
|
||||
setError("无法加载真实算子列表,暂时不能生成工作流。");
|
||||
});
|
||||
}, [isOpen]);
|
||||
|
||||
const handleGenerate = async () => {
|
||||
if (!description.trim()) return;
|
||||
if (!description.trim() || availableOperators.length === 0) return;
|
||||
|
||||
setIsGenerating(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
const workflow = await generateWorkflowFromDescription(description, MOCK_OPERATORS as any);
|
||||
onGenerate(workflow, "AI 生成工作流", description);
|
||||
const workflow = await generateWorkflowFromDescription(description);
|
||||
onGenerate(workflow, workflow.name || "AI 生成工作流", workflow.description || description);
|
||||
onClose();
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : "生成失败,请稍后重试。");
|
||||
@ -68,6 +83,20 @@ export default function AIGenerateModal({ isOpen, onClose, onGenerate }: AIGener
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!aiStatus.enabled && (
|
||||
<div className="p-3 bg-yellow-500/10 border border-yellow-500/20 rounded-lg flex items-center gap-3 text-yellow-300 text-xs">
|
||||
<AlertCircle className="w-4 h-4 shrink-0" />
|
||||
{aiStatus.reason || "当前服务端未配置 AI 服务,AI 生成功能已禁用。"}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{aiStatus.enabled && (
|
||||
<div className="p-3 bg-blue-500/10 border border-blue-500/20 rounded-lg flex items-center gap-3 text-blue-200 text-xs">
|
||||
<Sparkles className="w-4 h-4 shrink-0" />
|
||||
当前由服务端模型 `{aiStatus.model || "glm-5"}` 生成,浏览器侧不会持有 API Key。
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="p-6 border-t border-[#2a2b2f] flex justify-end gap-3 bg-[#0a0a0a]/50">
|
||||
@ -80,10 +109,10 @@ export default function AIGenerateModal({ isOpen, onClose, onGenerate }: AIGener
|
||||
</button>
|
||||
<button
|
||||
onClick={handleGenerate}
|
||||
disabled={!description.trim() || isGenerating}
|
||||
disabled={!aiStatus.enabled || !description.trim() || isGenerating || availableOperators.length === 0}
|
||||
className={cn(
|
||||
"px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-md text-sm font-medium transition-all flex items-center gap-2",
|
||||
(!description.trim() || isGenerating) && "opacity-50 cursor-not-allowed"
|
||||
(!aiStatus.enabled || !description.trim() || isGenerating || availableOperators.length === 0) && "opacity-50 cursor-not-allowed"
|
||||
)}
|
||||
>
|
||||
{isGenerating ? (
|
||||
|
||||
@ -1,13 +1,52 @@
|
||||
import React, { useState } from "react";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { Loader2 } from "lucide-react";
|
||||
import { Sidebar } from "./Sidebar";
|
||||
import { Logo } from "./Logo";
|
||||
import { UserBootstrapPanel } from "./UserBootstrapPanel";
|
||||
import { useUser } from "../context/UserContext";
|
||||
import { apiRequest } from "../services/api/client";
|
||||
|
||||
interface LayoutProps {
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
type HealthState = {
|
||||
status: "loading" | "online" | "offline";
|
||||
service?: string;
|
||||
};
|
||||
|
||||
export function Layout({ children }: LayoutProps) {
|
||||
const [isCollapsed, setIsCollapsed] = useState(false);
|
||||
const [health, setHealth] = useState<HealthState>({ status: "loading" });
|
||||
const { currentUser, loading } = useUser();
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
|
||||
const loadHealth = async () => {
|
||||
try {
|
||||
const result = await apiRequest<{ status: string; service: string }>("/api/health");
|
||||
if (!cancelled) {
|
||||
setHealth({
|
||||
status: result.status === "ok" ? "online" : "offline",
|
||||
service: result.service,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
if (!cancelled) {
|
||||
console.error("Failed to load API health", error);
|
||||
setHealth({ status: "offline" });
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
loadHealth();
|
||||
const timer = window.setInterval(loadHealth, 15000);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
window.clearInterval(timer);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="flex h-screen bg-[#0a0a0a] text-gray-100 font-sans">
|
||||
@ -16,17 +55,61 @@ export function Layout({ children }: LayoutProps) {
|
||||
<header className="h-16 border-b border-[#2a2b2f] bg-[#151619] flex items-center justify-between px-8">
|
||||
<div className="flex items-center gap-4">
|
||||
<Logo size="sm" />
|
||||
<h2 className="text-sm font-medium text-gray-400 uppercase tracking-widest">EmboFlow Platform</h2>
|
||||
<h2 className="text-sm font-medium text-gray-400 uppercase tracking-widest">DroboticFlow Platform</h2>
|
||||
</div>
|
||||
<div className="flex items-center gap-4">
|
||||
<div className="flex items-center gap-2 px-3 py-1 bg-green-500/10 border border-green-500/20 rounded-full">
|
||||
<div className="w-2 h-2 bg-green-500 rounded-full animate-pulse" />
|
||||
<span className="text-[10px] font-mono text-green-500 uppercase tracking-wider">K8s Cluster: Online</span>
|
||||
<div
|
||||
className={[
|
||||
"flex items-center gap-2 px-3 py-1 rounded-full border",
|
||||
health.status === "online"
|
||||
? "bg-green-500/10 border-green-500/20"
|
||||
: health.status === "offline"
|
||||
? "bg-red-500/10 border-red-500/20"
|
||||
: "bg-gray-500/10 border-gray-500/20",
|
||||
].join(" ")}
|
||||
>
|
||||
<div
|
||||
className={[
|
||||
"w-2 h-2 rounded-full",
|
||||
health.status === "online"
|
||||
? "bg-green-500 animate-pulse"
|
||||
: health.status === "offline"
|
||||
? "bg-red-500"
|
||||
: "bg-gray-500 animate-pulse",
|
||||
].join(" ")}
|
||||
/>
|
||||
<span
|
||||
className={[
|
||||
"text-[10px] font-mono uppercase tracking-wider",
|
||||
health.status === "online"
|
||||
? "text-green-500"
|
||||
: health.status === "offline"
|
||||
? "text-red-400"
|
||||
: "text-gray-400",
|
||||
].join(" ")}
|
||||
>
|
||||
{health.status === "online"
|
||||
? `${health.service || "api"}: online`
|
||||
: health.status === "offline"
|
||||
? "api: offline"
|
||||
: "api: checking"}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
<div className="flex-1 overflow-auto p-8">
|
||||
{children}
|
||||
{loading ? (
|
||||
<div className="flex h-full items-center justify-center">
|
||||
<div className="flex items-center gap-3 rounded-2xl border border-[#2a2b2f] bg-[#151619] px-6 py-4 text-sm text-gray-400">
|
||||
<Loader2 className="w-4 h-4 animate-spin text-blue-500" />
|
||||
正在加载真实用户上下文...
|
||||
</div>
|
||||
</div>
|
||||
) : currentUser ? (
|
||||
children
|
||||
) : (
|
||||
<UserBootstrapPanel />
|
||||
)}
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
@ -54,8 +54,8 @@ export function Logo({ className, size = "md", showText = false }: LogoProps) {
|
||||
|
||||
{showText && (
|
||||
<div className="flex flex-col leading-none">
|
||||
<span className="text-white font-bold text-lg tracking-tight">EmboFlow</span>
|
||||
<span className="text-[10px] text-blue-400 font-mono uppercase tracking-widest mt-0.5">Digital Robot Flow</span>
|
||||
<span className="text-white font-bold text-lg tracking-tight">DroboticFlow</span>
|
||||
<span className="text-[10px] text-blue-400 font-mono uppercase tracking-widest mt-0.5">Digital Robotic Workflows</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@ -1,28 +1,27 @@
|
||||
import React, { useState } from "react";
|
||||
import React, { useEffect, useMemo, useState } from "react";
|
||||
import { NavLink } from "react-router-dom";
|
||||
import { Logo } from "./Logo";
|
||||
import { useUser } from "../context/UserContext";
|
||||
import {
|
||||
LayoutDashboard,
|
||||
FolderKanban,
|
||||
FileCode,
|
||||
Cpu,
|
||||
Activity,
|
||||
Settings,
|
||||
import {
|
||||
LayoutDashboard,
|
||||
FolderKanban,
|
||||
FileCode,
|
||||
Cpu,
|
||||
Activity,
|
||||
Settings,
|
||||
Users,
|
||||
Workflow,
|
||||
ChevronLeft,
|
||||
ChevronRight,
|
||||
User,
|
||||
X,
|
||||
Mail,
|
||||
Shield,
|
||||
Edit3,
|
||||
ClipboardCheck,
|
||||
LogOut
|
||||
LogOut,
|
||||
Loader2,
|
||||
} from "lucide-react";
|
||||
import { cn } from "../lib/utils";
|
||||
import { UserRole } from "../types";
|
||||
import type { UserRole } from "../types";
|
||||
|
||||
const navItems = [
|
||||
{ icon: LayoutDashboard, label: "仪表盘", path: "/", roles: ["admin", "auditor", "operator"] },
|
||||
@ -33,36 +32,83 @@ const navItems = [
|
||||
{ icon: Activity, label: "执行监控", path: "/monitoring", roles: ["admin", "auditor", "operator"] },
|
||||
{ icon: Users, label: "用户管理", path: "/users", roles: ["admin"] },
|
||||
{ icon: Settings, label: "系统配置", path: "/settings", roles: ["admin"] },
|
||||
];
|
||||
] as const;
|
||||
|
||||
interface SidebarProps {
|
||||
isCollapsed: boolean;
|
||||
onToggle: () => void;
|
||||
}
|
||||
|
||||
type ProfileDraft = {
|
||||
name: string;
|
||||
email: string;
|
||||
role: UserRole;
|
||||
};
|
||||
|
||||
export function Sidebar({ isCollapsed, onToggle }: SidebarProps) {
|
||||
const [isProfileModalOpen, setIsProfileModalOpen] = useState(false);
|
||||
const { role, setRole, name, setName, email, setEmail, logout } = useUser();
|
||||
const [profileDraft, setProfileDraft] = useState<ProfileDraft | null>(null);
|
||||
const [isSavingProfile, setIsSavingProfile] = useState(false);
|
||||
const {
|
||||
currentUser,
|
||||
users,
|
||||
role,
|
||||
name,
|
||||
email,
|
||||
selectUser,
|
||||
updateCurrentUser,
|
||||
logout,
|
||||
} = useUser();
|
||||
|
||||
const handleSaveProfile = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setIsProfileModalOpen(false);
|
||||
useEffect(() => {
|
||||
if (!isProfileModalOpen || !currentUser) {
|
||||
return;
|
||||
}
|
||||
setProfileDraft({
|
||||
name: currentUser.name,
|
||||
email: currentUser.email,
|
||||
role: currentUser.role,
|
||||
});
|
||||
}, [currentUser, isProfileModalOpen]);
|
||||
|
||||
const filteredNavItems = useMemo(() => {
|
||||
if (!role) {
|
||||
return [];
|
||||
}
|
||||
return navItems.filter((item) => item.roles.includes(role));
|
||||
}, [role]);
|
||||
|
||||
const handleSaveProfile = async (event: React.FormEvent) => {
|
||||
event.preventDefault();
|
||||
if (!profileDraft) {
|
||||
return;
|
||||
}
|
||||
setIsSavingProfile(true);
|
||||
try {
|
||||
await updateCurrentUser(profileDraft);
|
||||
setIsProfileModalOpen(false);
|
||||
} catch (error) {
|
||||
console.error("Failed to update current user", error);
|
||||
} finally {
|
||||
setIsSavingProfile(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleLogout = () => {
|
||||
logout();
|
||||
setIsProfileModalOpen(false);
|
||||
};
|
||||
|
||||
const filteredNavItems = navItems.filter(item =>
|
||||
item.roles.includes(role)
|
||||
);
|
||||
const avatarLabel = currentUser?.name?.slice(0, 2).toUpperCase() || "??";
|
||||
|
||||
return (
|
||||
<aside className={cn(
|
||||
"bg-[#151619] border-r border-[#2a2b2f] flex flex-col transition-all duration-300 relative",
|
||||
isCollapsed ? "w-20" : "w-64"
|
||||
)}>
|
||||
<button
|
||||
<aside
|
||||
className={cn(
|
||||
"bg-[#151619] border-r border-[#2a2b2f] flex flex-col transition-all duration-300 relative",
|
||||
isCollapsed ? "w-20" : "w-64",
|
||||
)}
|
||||
>
|
||||
<button
|
||||
onClick={onToggle}
|
||||
className="absolute -right-3 top-20 w-6 h-6 bg-[#2a2b2f] border border-[#3a3b3f] rounded-full flex items-center justify-center text-gray-400 hover:text-white z-50"
|
||||
>
|
||||
@ -72,55 +118,56 @@ export function Sidebar({ isCollapsed, onToggle }: SidebarProps) {
|
||||
<div className={cn("p-6 border-b border-[#2a2b2f]", isCollapsed && "px-4")}>
|
||||
<Logo showText={!isCollapsed} />
|
||||
</div>
|
||||
|
||||
|
||||
<nav className="flex-1 p-4 space-y-1 overflow-y-auto overflow-x-hidden">
|
||||
{filteredNavItems.map((item) => (
|
||||
<NavLink
|
||||
key={item.path}
|
||||
to={item.path}
|
||||
title={isCollapsed ? item.label : undefined}
|
||||
className={({ isActive }) => cn(
|
||||
"flex items-center gap-3 px-3 py-2 rounded-md text-sm font-medium transition-colors",
|
||||
isActive
|
||||
? "bg-blue-600/10 text-blue-400"
|
||||
: "text-gray-400 hover:text-white hover:bg-white/5",
|
||||
isCollapsed && "justify-center px-0"
|
||||
)}
|
||||
className={({ isActive }) =>
|
||||
cn(
|
||||
"flex items-center gap-3 px-3 py-2 rounded-md text-sm font-medium transition-colors",
|
||||
isActive ? "bg-blue-600/10 text-blue-400" : "text-gray-400 hover:text-white hover:bg-white/5",
|
||||
isCollapsed && "justify-center px-0",
|
||||
)
|
||||
}
|
||||
>
|
||||
<item.icon className="w-4 h-4 shrink-0" />
|
||||
{!isCollapsed && <span className="truncate">{item.label}</span>}
|
||||
</NavLink>
|
||||
))}
|
||||
</nav>
|
||||
|
||||
|
||||
<div className={cn("p-4 border-t border-[#2a2b2f]", isCollapsed && "px-2")}>
|
||||
<button
|
||||
<button
|
||||
onClick={() => setIsProfileModalOpen(true)}
|
||||
className={cn(
|
||||
"flex items-center gap-3 px-3 py-2 w-full rounded-md hover:bg-white/5 transition-colors group text-left",
|
||||
isCollapsed && "px-0 justify-center"
|
||||
"flex items-center gap-3 px-3 py-2 w-full rounded-md hover:bg-white/5 transition-colors group text-left",
|
||||
isCollapsed && "px-0 justify-center",
|
||||
)}
|
||||
>
|
||||
<div className="w-8 h-8 rounded-full bg-blue-600/20 border border-blue-500/30 flex items-center justify-center text-xs text-blue-400 font-bold shrink-0 group-hover:border-blue-500/50 transition-colors">
|
||||
{name.substring(0, 2).toUpperCase()}
|
||||
{avatarLabel}
|
||||
</div>
|
||||
{!isCollapsed && (
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="text-sm font-medium text-white truncate group-hover:text-blue-400 transition-colors">{name}</p>
|
||||
<p className="text-xs text-gray-500 truncate">{email}</p>
|
||||
<p className="text-sm font-medium text-white truncate group-hover:text-blue-400 transition-colors">
|
||||
{name || "未选择用户"}
|
||||
</p>
|
||||
<p className="text-xs text-gray-500 truncate">{email || "请选择或创建真实用户"}</p>
|
||||
</div>
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Profile Edit Modal */}
|
||||
{isProfileModalOpen && (
|
||||
<div className="fixed inset-0 z-[200] flex items-center justify-center p-4 bg-black/60 backdrop-blur-sm">
|
||||
<div className="bg-[#151619] border border-[#2a2b2f] rounded-2xl w-full max-w-md shadow-2xl overflow-hidden">
|
||||
<div className="px-6 py-4 border-b border-[#2a2b2f] flex items-center justify-between bg-[#1a1b1e]">
|
||||
<h2 className="text-xl font-bold text-white flex items-center gap-2">
|
||||
<User className="w-5 h-5 text-blue-500" />
|
||||
个人信息设置
|
||||
当前用户
|
||||
</h2>
|
||||
<button onClick={() => setIsProfileModalOpen(false)} className="text-gray-500 hover:text-white transition-colors">
|
||||
<X className="w-6 h-6" />
|
||||
@ -128,75 +175,104 @@ export function Sidebar({ isCollapsed, onToggle }: SidebarProps) {
|
||||
</div>
|
||||
<form onSubmit={handleSaveProfile}>
|
||||
<div className="p-6 space-y-4">
|
||||
<div className="flex justify-center pb-4">
|
||||
<div className="relative group">
|
||||
<div className="w-20 h-20 rounded-full bg-blue-600/20 border-2 border-blue-500/30 flex items-center justify-center text-2xl text-blue-400 font-bold">
|
||||
{name.substring(0, 2).toUpperCase()}
|
||||
</div>
|
||||
<div className="absolute inset-0 rounded-full bg-black/40 flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity cursor-pointer">
|
||||
<Edit3 className="w-5 h-5 text-white" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider flex items-center gap-1.5">
|
||||
<User className="w-3 h-3" /> 用户名
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">选择用户</label>
|
||||
<select
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500 text-white"
|
||||
value={name}
|
||||
onChange={e => setName(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider flex items-center gap-1.5">
|
||||
<Mail className="w-3 h-3" /> 邮箱地址
|
||||
</label>
|
||||
<input
|
||||
type="email"
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500 text-white"
|
||||
value={email}
|
||||
onChange={e => setEmail(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider flex items-center gap-1.5">
|
||||
<Shield className="w-3 h-3" /> 当前角色 (演示切换)
|
||||
</label>
|
||||
<select
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500 text-white"
|
||||
value={role}
|
||||
onChange={e => setRole(e.target.value as UserRole)}
|
||||
value={currentUser?.id || ""}
|
||||
onChange={(event) => selectUser(event.target.value)}
|
||||
>
|
||||
<option value="admin">管理员 (Admin)</option>
|
||||
<option value="auditor">审核员 (Auditor)</option>
|
||||
<option value="operator">操作员 (Operator)</option>
|
||||
<option value="" disabled>
|
||||
请选择用户
|
||||
</option>
|
||||
{users.map((item) => (
|
||||
<option key={item.id} value={item.id}>
|
||||
{item.name} · {item.role}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
{currentUser && profileDraft ? (
|
||||
<>
|
||||
<div className="flex justify-center pb-4">
|
||||
<div className="w-20 h-20 rounded-full bg-blue-600/20 border-2 border-blue-500/30 flex items-center justify-center text-2xl text-blue-400 font-bold">
|
||||
{avatarLabel}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider flex items-center gap-1.5">
|
||||
<User className="w-3 h-3" /> 用户名
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500 text-white"
|
||||
value={profileDraft.name}
|
||||
onChange={(event) => setProfileDraft((previous) => (previous ? { ...previous, name: event.target.value } : previous))}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider flex items-center gap-1.5">
|
||||
<Mail className="w-3 h-3" /> 邮箱地址
|
||||
</label>
|
||||
<input
|
||||
type="email"
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500 text-white"
|
||||
value={profileDraft.email}
|
||||
onChange={(event) => setProfileDraft((previous) => (previous ? { ...previous, email: event.target.value } : previous))}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider flex items-center gap-1.5">
|
||||
<Shield className="w-3 h-3" /> 角色
|
||||
</label>
|
||||
<select
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500 text-white"
|
||||
value={profileDraft.role}
|
||||
onChange={(event) =>
|
||||
setProfileDraft((previous) =>
|
||||
previous ? { ...previous, role: event.target.value as UserRole } : previous,
|
||||
)
|
||||
}
|
||||
>
|
||||
<option value="admin">admin</option>
|
||||
<option value="auditor">auditor</option>
|
||||
<option value="operator">operator</option>
|
||||
</select>
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<div className="rounded-lg border border-dashed border-[#2a2b2f] bg-[#0a0a0a] px-4 py-6 text-sm text-gray-500">
|
||||
当前没有选中的用户。请先在初始化页创建用户,或从已有用户列表中选择一个用户。
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="p-6 border-t border-[#2a2b2f] flex items-center justify-between bg-[#0a0a0a]/50">
|
||||
<button
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleLogout}
|
||||
className="flex items-center gap-2 px-4 py-2 text-sm font-medium text-red-500 hover:bg-red-500/10 rounded-md transition-colors"
|
||||
>
|
||||
<LogOut className="w-4 h-4" />
|
||||
退出登录
|
||||
退出当前用户
|
||||
</button>
|
||||
<div className="flex gap-3">
|
||||
<button
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setIsProfileModalOpen(false)}
|
||||
className="px-4 py-2 text-sm font-medium text-gray-400 hover:text-white transition-colors"
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
<button
|
||||
<button
|
||||
type="submit"
|
||||
className="px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-md text-sm font-medium transition-all"
|
||||
disabled={!profileDraft || isSavingProfile}
|
||||
className="px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-md text-sm font-medium transition-all disabled:opacity-50 disabled:cursor-not-allowed inline-flex items-center gap-2"
|
||||
>
|
||||
{isSavingProfile && <Loader2 className="w-4 h-4 animate-spin" />}
|
||||
保存
|
||||
</button>
|
||||
</div>
|
||||
|
||||
166
src/components/UserBootstrapPanel.tsx
Normal file
166
src/components/UserBootstrapPanel.tsx
Normal file
@ -0,0 +1,166 @@
|
||||
import React, { useMemo, useState } from "react";
|
||||
import { AlertCircle, Loader2, Mail, Shield, User, UserPlus } from "lucide-react";
|
||||
import { useUser } from "../context/UserContext";
|
||||
import type { UserRole } from "../types";
|
||||
|
||||
type UserFormState = {
|
||||
name: string;
|
||||
email: string;
|
||||
role: UserRole | "";
|
||||
};
|
||||
|
||||
const emptyForm: UserFormState = {
|
||||
name: "",
|
||||
email: "",
|
||||
role: "",
|
||||
};
|
||||
|
||||
export function UserBootstrapPanel() {
|
||||
const { users, createUser, selectUser, error } = useUser();
|
||||
const [form, setForm] = useState<UserFormState>(emptyForm);
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [submitError, setSubmitError] = useState<string | null>(null);
|
||||
|
||||
const canSubmit = useMemo(() => {
|
||||
return Boolean(form.name.trim() && form.email.trim() && form.role);
|
||||
}, [form.email, form.name, form.role]);
|
||||
|
||||
const handleCreate = async (event: React.FormEvent) => {
|
||||
event.preventDefault();
|
||||
if (!canSubmit || !form.role) {
|
||||
return;
|
||||
}
|
||||
setSubmitting(true);
|
||||
setSubmitError(null);
|
||||
try {
|
||||
await createUser({
|
||||
name: form.name.trim(),
|
||||
email: form.email.trim(),
|
||||
role: form.role,
|
||||
});
|
||||
setForm(emptyForm);
|
||||
} catch (err) {
|
||||
setSubmitError(err instanceof Error ? err.message : "创建用户失败");
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex h-full items-center justify-center p-8">
|
||||
<div className="w-full max-w-5xl rounded-3xl border border-[#2a2b2f] bg-[#151619] shadow-2xl overflow-hidden">
|
||||
<div className="grid gap-0 lg:grid-cols-[1.1fr_0.9fr]">
|
||||
<div className="border-b border-[#2a2b2f] bg-gradient-to-br from-blue-600/15 via-[#151619] to-[#151619] p-8 lg:border-b-0 lg:border-r">
|
||||
<div className="mb-8 space-y-3">
|
||||
<div className="inline-flex items-center gap-2 rounded-full border border-blue-500/20 bg-blue-500/10 px-3 py-1 text-[11px] font-medium uppercase tracking-widest text-blue-300">
|
||||
<UserPlus className="w-3.5 h-3.5" />
|
||||
用户初始化
|
||||
</div>
|
||||
<h1 className="text-3xl font-bold tracking-tight text-white">接入真实用户后再进入系统</h1>
|
||||
<p className="max-w-xl text-sm leading-6 text-gray-400">
|
||||
当前环境没有选中的平台用户。请选择一个已经存在的真实用户,或者先创建首个用户,再继续操作项目、模板和工作流。
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="space-y-4">
|
||||
<div className="text-xs font-bold uppercase tracking-[0.22em] text-gray-500">已有用户</div>
|
||||
{users.length > 0 ? (
|
||||
<div className="space-y-3">
|
||||
{users.map((item) => (
|
||||
<button
|
||||
key={item.id}
|
||||
onClick={() => selectUser(item.id)}
|
||||
className="flex w-full items-center justify-between rounded-2xl border border-[#2a2b2f] bg-[#0a0a0a] px-4 py-4 text-left transition-colors hover:border-blue-500/40 hover:bg-blue-500/5"
|
||||
>
|
||||
<div>
|
||||
<div className="text-sm font-semibold text-white">{item.name}</div>
|
||||
<div className="mt-1 text-xs text-gray-500">{item.email}</div>
|
||||
</div>
|
||||
<div className="rounded-full border border-[#2a2b2f] px-3 py-1 text-[10px] font-bold uppercase tracking-widest text-blue-300">
|
||||
{item.role}
|
||||
</div>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div className="rounded-2xl border border-dashed border-[#2a2b2f] bg-[#0a0a0a] px-4 py-6 text-sm text-gray-500">
|
||||
还没有任何用户记录,请先在右侧创建首个用户。
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="p-8">
|
||||
<form onSubmit={handleCreate} className="space-y-5">
|
||||
<div className="space-y-2">
|
||||
<div className="text-xs font-bold uppercase tracking-[0.22em] text-gray-500">创建用户</div>
|
||||
<h2 className="text-2xl font-bold text-white">新增真实平台用户</h2>
|
||||
</div>
|
||||
|
||||
{(error || submitError) && (
|
||||
<div className="flex items-start gap-3 rounded-2xl border border-red-500/20 bg-red-500/10 px-4 py-3 text-sm text-red-200">
|
||||
<AlertCircle className="mt-0.5 w-4 h-4 shrink-0" />
|
||||
<span>{submitError || error}</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<label className="flex items-center gap-2 text-xs font-medium uppercase tracking-wider text-gray-400">
|
||||
<User className="w-3.5 h-3.5" />
|
||||
用户名
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={form.name}
|
||||
onChange={(event) => setForm((previous) => ({ ...previous, name: event.target.value }))}
|
||||
className="w-full rounded-xl border border-[#2a2b2f] bg-[#0a0a0a] px-4 py-3 text-sm text-white focus:border-blue-500 focus:outline-none"
|
||||
placeholder="输入真实姓名或账号名"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<label className="flex items-center gap-2 text-xs font-medium uppercase tracking-wider text-gray-400">
|
||||
<Mail className="w-3.5 h-3.5" />
|
||||
邮箱地址
|
||||
</label>
|
||||
<input
|
||||
type="email"
|
||||
value={form.email}
|
||||
onChange={(event) => setForm((previous) => ({ ...previous, email: event.target.value }))}
|
||||
className="w-full rounded-xl border border-[#2a2b2f] bg-[#0a0a0a] px-4 py-3 text-sm text-white focus:border-blue-500 focus:outline-none"
|
||||
placeholder="name@example.com"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<label className="flex items-center gap-2 text-xs font-medium uppercase tracking-wider text-gray-400">
|
||||
<Shield className="w-3.5 h-3.5" />
|
||||
角色
|
||||
</label>
|
||||
<select
|
||||
value={form.role}
|
||||
onChange={(event) => setForm((previous) => ({ ...previous, role: event.target.value as UserRole | "" }))}
|
||||
className="w-full rounded-xl border border-[#2a2b2f] bg-[#0a0a0a] px-4 py-3 text-sm text-white focus:border-blue-500 focus:outline-none"
|
||||
>
|
||||
<option value="">请选择角色</option>
|
||||
<option value="admin">admin</option>
|
||||
<option value="auditor">auditor</option>
|
||||
<option value="operator">operator</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={!canSubmit || submitting}
|
||||
className="inline-flex w-full items-center justify-center gap-2 rounded-xl bg-blue-600 px-4 py-3 text-sm font-semibold text-white transition-colors hover:bg-blue-700 disabled:cursor-not-allowed disabled:opacity-50"
|
||||
>
|
||||
{submitting ? <Loader2 className="w-4 h-4 animate-spin" /> : <UserPlus className="w-4 h-4" />}
|
||||
{submitting ? "正在创建..." : "创建并进入系统"}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
169
src/constants.ts
169
src/constants.ts
@ -1,169 +0,0 @@
|
||||
import { Project, Operator } from "./types";
|
||||
|
||||
export const MOCK_PROJECTS: Project[] = [
|
||||
{ id: "1", name: "具身智能抓取任务", description: "处理机械臂抓取过程中的视觉数据", createdAt: "2024-03-20" },
|
||||
{ id: "2", name: "自动驾驶感知流程", description: "多传感器融合数据清洗与标注", createdAt: "2024-03-21" },
|
||||
{ id: "3", name: "视频自动化处理流水线", description: "自动扫描 OSS 目录并进行视频分割与标注", createdAt: "2024-04-07" },
|
||||
];
|
||||
|
||||
export const MOCK_OPERATORS: Operator[] = [
|
||||
{
|
||||
id: "op-1",
|
||||
name: "OSS 数据源",
|
||||
type: "builtin",
|
||||
category: "IO",
|
||||
description: "从指定的 OSS 路径获取原始数据并生成数据句柄",
|
||||
inputs: [],
|
||||
outputs: [{ id: "out-1", name: "数据句柄", type: "file", transmission: "oss" }],
|
||||
skillDefinition: "### OSS 数据源 (Data Source)\n作为工作流的起点,从指定的 OSS 路径获取原始视频或图像数据。它并不直接将数据下载到用户本地,而是生成一个数据句柄(Handle),供下游算子通过 Sidecar 自动挂载或流式读取。支持配置存储桶、路径前缀和文件过滤规则。",
|
||||
hyperparams: [
|
||||
{ key: "OSS_RETRY_COUNT", value: "3", description: "OSS 读取重试次数" },
|
||||
{ key: "OSS_TIMEOUT", value: "30", description: "请求超时时间 (s)" }
|
||||
]
|
||||
},
|
||||
{
|
||||
id: "op-12",
|
||||
name: "目录扫描器 (Dir Scanner)",
|
||||
type: "builtin",
|
||||
category: "IO",
|
||||
description: "定期扫描 OSS 目录,发现新增子目录并触发后续流程",
|
||||
inputs: [],
|
||||
outputs: [{ id: "out-1", name: "新增目录路径", type: "json", transmission: "bus" }],
|
||||
skillDefinition: "### 目录扫描器 (Directory Scanner)\n专门用于自动化流水线的触发。它会监听指定的 OSS 路径,当发现新的子目录创建时,会将该目录的完整路径作为 JSON 输出。常用于处理按批次上传的原始数据。",
|
||||
hyperparams: [
|
||||
{ key: "SCAN_INTERVAL", value: "60", description: "扫描间隔 (s)" },
|
||||
{ key: "MAX_DEPTH", value: "2", description: "扫描目录深度" }
|
||||
]
|
||||
},
|
||||
{
|
||||
id: "op-2",
|
||||
name: "图像预处理",
|
||||
type: "custom",
|
||||
category: "CV",
|
||||
description: "缩放、归一化图像数据",
|
||||
inputs: [{ id: "in-1", name: "图像", type: "image", transmission: "shm" }],
|
||||
outputs: [{ id: "out-1", name: "预处理图像", type: "image", transmission: "shm" }],
|
||||
skillDefinition: "### 图像预处理\n对输入的图像进行缩放、归一化和格式转换。输入必须是 Image 类型,输出也是 Image 类型。",
|
||||
hyperparams: [
|
||||
{ key: "RESIZE_WIDTH", value: "640" },
|
||||
{ key: "RESIZE_HEIGHT", value: "640" },
|
||||
{ key: "NORMALIZE_MEAN", value: "0.5,0.5,0.5" }
|
||||
]
|
||||
},
|
||||
{
|
||||
id: "op-10",
|
||||
name: "视频分割 (Video Splitter)",
|
||||
type: "builtin",
|
||||
category: "CV",
|
||||
description: "将长视频按场景或固定时长分割成多个短片段",
|
||||
inputs: [{ id: "in-1", name: "原始视频", type: "video", transmission: "oss" }],
|
||||
outputs: [{ id: "out-1", name: "视频片段列表", type: "json", transmission: "bus" }],
|
||||
skillDefinition: "### 视频分割 (Video Splitter)\n接受一个视频文件句柄,通过 FFmpeg 或 AI 场景检测算法将其分割。输出是一个包含所有片段 OSS 路径的 JSON 列表。支持配置分割时长、重叠时间或灵敏度。",
|
||||
hyperparams: [
|
||||
{ key: "SEGMENT_DURATION", value: "10", description: "分割时长 (s)" },
|
||||
{ key: "FFMPEG_THREADS", value: "4", description: "FFmpeg 并行线程数" }
|
||||
]
|
||||
},
|
||||
{
|
||||
id: "op-3",
|
||||
name: "模型推理",
|
||||
type: "builtin",
|
||||
category: "AI",
|
||||
description: "运行目标检测模型",
|
||||
inputs: [{ id: "in-1", name: "图像", type: "image", transmission: "shm" }],
|
||||
outputs: [{ id: "out-1", name: "检测结果", type: "json", transmission: "bus" }],
|
||||
skillDefinition: "### AI 模型推理\n运行 YOLO 或其他目标检测模型。接受预处理后的图像,输出包含检测框 and 类别的 JSON 数据。",
|
||||
hyperparams: [
|
||||
{ key: "CONFIDENCE_THRESHOLD", value: "0.5", description: "置信度阈值" },
|
||||
{ key: "IOU_THRESHOLD", value: "0.45", description: "IOU 阈值" },
|
||||
{ key: "GPU_MEMORY_FRACTION", value: "0.8" }
|
||||
]
|
||||
},
|
||||
{
|
||||
id: "op-11",
|
||||
name: "视频标注 (Video Annotator)",
|
||||
type: "builtin",
|
||||
category: "AI",
|
||||
description: "自动为视频片段生成标签、描述或检测框",
|
||||
inputs: [{ id: "in-1", name: "视频片段", type: "video", transmission: "oss" }],
|
||||
outputs: [{ id: "out-1", name: "标注数据", type: "json", transmission: "bus" }],
|
||||
skillDefinition: "### 视频标注 (Video Annotator)\n对视频内容进行自动化的元数据提取。包括但不限于:动作识别、物体追踪、场景描述。输出为标准的标注 JSON 格式,可直接用于模型训练。",
|
||||
hyperparams: [
|
||||
{ key: "ANNOTATION_MODE", value: "auto", description: "标注模式 (auto/manual)" },
|
||||
{ key: "BATCH_SIZE", value: "16" }
|
||||
]
|
||||
},
|
||||
{
|
||||
id: "op-4",
|
||||
name: "结果上传",
|
||||
type: "builtin",
|
||||
category: "IO",
|
||||
description: "将处理结果上传至 OSS",
|
||||
inputs: [{ id: "in-1", name: "结果数据", type: "json", transmission: "bus" }],
|
||||
outputs: [],
|
||||
skillDefinition: "### 结果上传\n将最终的处理结果(如检测 JSON)上传到指定的 OSS 存储桶。通常作为工作流的终点。"
|
||||
},
|
||||
{
|
||||
id: "op-5",
|
||||
name: "数据合并",
|
||||
type: "builtin",
|
||||
category: "MATH",
|
||||
description: "合并多个数据源",
|
||||
inputs: [
|
||||
{ id: "in-1", name: "数据 A", type: "json", transmission: "bus" },
|
||||
{ id: "in-2", name: "数据 B", type: "json", transmission: "bus" }
|
||||
],
|
||||
outputs: [{ id: "out-1", name: "合并数据", type: "json", transmission: "bus" }],
|
||||
skillDefinition: "### 数据合并 (Data Merge)\n将两个或多个 JSON 数据源合并为一个。支持深度合并或简单追加。"
|
||||
},
|
||||
{
|
||||
id: "op-6",
|
||||
name: "数据交集",
|
||||
type: "builtin",
|
||||
category: "MATH",
|
||||
description: "计算两个数据集的交集",
|
||||
inputs: [
|
||||
{ id: "in-1", name: "数据 A", type: "json", transmission: "bus" },
|
||||
{ id: "in-2", name: "数据 B", type: "json", transmission: "bus" }
|
||||
],
|
||||
outputs: [{ id: "out-1", name: "交集数据", type: "json", transmission: "bus" }],
|
||||
skillDefinition: "### 数据交集 (Data Intersection)\n计算两个 JSON 数据集的交集部分。常用于过滤共有特征或检测框。"
|
||||
},
|
||||
{
|
||||
id: "op-7",
|
||||
name: "数据导出",
|
||||
type: "builtin",
|
||||
category: "IO",
|
||||
description: "导出数据到本地或第三方系统",
|
||||
inputs: [{ id: "in-1", name: "数据", type: "json", transmission: "bus" }],
|
||||
outputs: [],
|
||||
skillDefinition: "### 数据导出 (Data Export)\n将处理后的数据导出为 CSV、Excel 或其他格式,支持直接下载或发送至 Webhook。"
|
||||
},
|
||||
{
|
||||
id: "op-8",
|
||||
name: "人工审核 (Human Review)",
|
||||
type: "builtin",
|
||||
category: "HUMAN",
|
||||
description: "将数据流转至人工审核队列,待审核员确认后继续执行",
|
||||
inputs: [{ id: "in-1", name: "待审数据", type: "json", transmission: "bus" }],
|
||||
outputs: [
|
||||
{ id: "out-1", name: "审核通过", type: "json", transmission: "bus" },
|
||||
{ id: "out-2", name: "审核拒绝", type: "json", transmission: "bus" }
|
||||
],
|
||||
skillDefinition: "### 人工审核 (Human Review)\n这是一个具身智能工作流中的关键节点。当自动化算法(如目标检测)的置信度较低时,可以将数据发送至此算子。工作流将在此暂停,并向审核员发送通知。审核员在管理后台查看数据并点击“通过”或“拒绝”后,工作流将根据结果选择不同的分支继续执行。"
|
||||
},
|
||||
{
|
||||
id: "op-9",
|
||||
name: "任务分配 (Task Distribution)",
|
||||
type: "builtin",
|
||||
category: "FLOW",
|
||||
description: "对输入数据进行 Shuffle 并按比例或数量分配给不同的审核员/队列",
|
||||
inputs: [{ id: "in-1", name: "待分配数据", type: "json", transmission: "bus" }],
|
||||
outputs: [
|
||||
{ id: "out-1", name: "队列 A", type: "json", transmission: "bus" },
|
||||
{ id: "out-2", name: "队列 B", type: "json", transmission: "bus" },
|
||||
{ id: "out-3", name: "队列 C", type: "json", transmission: "bus" }
|
||||
],
|
||||
skillDefinition: "### 任务分配 (Task Distribution)\n用于将大规模数据集分发给不同的处理队列或审核员。支持“洗牌”(Shuffle)功能以保证分配的随机性。分配模式包括:\n- **按比例分配**:例如 70% 分配给队列 A,30% 分配给队列 B。\n- **按数量分配**:例如前 100 条分配给队列 A,其余分配给队列 B。\n此算子通常连接在“人工审核”算子之前,用于实现多审核员并行的工作流。"
|
||||
},
|
||||
];
|
||||
61
src/context/TimeZoneContext.tsx
Normal file
61
src/context/TimeZoneContext.tsx
Normal file
@ -0,0 +1,61 @@
|
||||
import React, {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useState,
|
||||
} from "react";
|
||||
import { settingsApi } from "../services/api/settings";
|
||||
import { resolveTimeZone } from "../lib/datetime";
|
||||
|
||||
type TimeZoneContextValue = {
|
||||
timeZone: string;
|
||||
setTimeZone: (value: string) => void;
|
||||
refreshTimeZone: () => Promise<void>;
|
||||
};
|
||||
|
||||
const TimeZoneContext = createContext<TimeZoneContextValue | null>(null);
|
||||
|
||||
export function TimeZoneProvider({ children }: { children: React.ReactNode }) {
|
||||
const [timeZone, setTimeZoneState] = useState(() =>
|
||||
resolveTimeZone(undefined),
|
||||
);
|
||||
|
||||
const refreshTimeZone = useCallback(async () => {
|
||||
try {
|
||||
const settings = await settingsApi.get();
|
||||
setTimeZoneState(resolveTimeZone(settings.timezone));
|
||||
} catch (error) {
|
||||
console.error("Failed to refresh timezone settings", error);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
refreshTimeZone();
|
||||
}, [refreshTimeZone]);
|
||||
|
||||
const value = useMemo<TimeZoneContextValue>(
|
||||
() => ({
|
||||
timeZone,
|
||||
setTimeZone: (nextTimeZone: string) =>
|
||||
setTimeZoneState(resolveTimeZone(nextTimeZone)),
|
||||
refreshTimeZone,
|
||||
}),
|
||||
[refreshTimeZone, timeZone],
|
||||
);
|
||||
|
||||
return (
|
||||
<TimeZoneContext.Provider value={value}>
|
||||
{children}
|
||||
</TimeZoneContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useTimeZone() {
|
||||
const value = useContext(TimeZoneContext);
|
||||
if (!value) {
|
||||
throw new Error("useTimeZone must be used within TimeZoneProvider");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
@ -1,39 +1,152 @@
|
||||
import React, { createContext, useContext, useState, ReactNode } from 'react';
|
||||
import { UserRole } from '../types';
|
||||
import React, { createContext, useCallback, useContext, useEffect, useMemo, useState, ReactNode } from "react";
|
||||
import { usersApi } from "../services/api/users";
|
||||
import type { User, UserRole } from "../types";
|
||||
|
||||
const CURRENT_USER_STORAGE_KEY = "droboticflow.currentUserId";
|
||||
|
||||
interface UserContextType {
|
||||
role: UserRole;
|
||||
setRole: (role: UserRole) => void;
|
||||
currentUser: User | null;
|
||||
users: User[];
|
||||
role: UserRole | null;
|
||||
name: string;
|
||||
setName: (name: string) => void;
|
||||
email: string;
|
||||
setEmail: (email: string) => void;
|
||||
loading: boolean;
|
||||
error: string | null;
|
||||
refreshUsers: (options?: { silent?: boolean }) => Promise<User[]>;
|
||||
selectUser: (userId: string) => void;
|
||||
createUser: (payload: Pick<User, "name" | "email" | "role">) => Promise<User>;
|
||||
updateCurrentUser: (payload: Partial<User>) => Promise<User | null>;
|
||||
logout: () => void;
|
||||
}
|
||||
|
||||
const UserContext = createContext<UserContextType | undefined>(undefined);
|
||||
|
||||
function getStoredCurrentUserID() {
|
||||
return window.localStorage.getItem(CURRENT_USER_STORAGE_KEY);
|
||||
}
|
||||
|
||||
function persistCurrentUserID(userId: string | null) {
|
||||
if (userId) {
|
||||
window.localStorage.setItem(CURRENT_USER_STORAGE_KEY, userId);
|
||||
return;
|
||||
}
|
||||
window.localStorage.removeItem(CURRENT_USER_STORAGE_KEY);
|
||||
}
|
||||
|
||||
function resolveCurrentUser(items: User[], preferredID: string | null) {
|
||||
if (items.length === 0) {
|
||||
return null;
|
||||
}
|
||||
if (preferredID) {
|
||||
const matched = items.find((item) => item.id === preferredID);
|
||||
if (matched) {
|
||||
return matched;
|
||||
}
|
||||
}
|
||||
return items[0];
|
||||
}
|
||||
|
||||
export function UserProvider({ children }: { children: ReactNode }) {
|
||||
const [role, setRole] = useState<UserRole>('admin');
|
||||
const [name, setName] = useState('管理员');
|
||||
const [email, setEmail] = useState('admin@emboflow.ai');
|
||||
const [users, setUsers] = useState<User[]>([]);
|
||||
const [currentUser, setCurrentUser] = useState<User | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const logout = () => {
|
||||
console.log("Logging out...");
|
||||
window.location.reload();
|
||||
};
|
||||
const refreshUsers = useCallback(async (options: { silent?: boolean } = {}) => {
|
||||
const { silent = false } = options;
|
||||
if (!silent) {
|
||||
setLoading(true);
|
||||
}
|
||||
setError(null);
|
||||
try {
|
||||
const items = await usersApi.list();
|
||||
const resolvedCurrentUser = resolveCurrentUser(items, getStoredCurrentUserID());
|
||||
setUsers(items);
|
||||
setCurrentUser(resolvedCurrentUser);
|
||||
persistCurrentUserID(resolvedCurrentUser?.id || null);
|
||||
return items;
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : "加载用户失败";
|
||||
setError(message);
|
||||
setUsers([]);
|
||||
setCurrentUser(null);
|
||||
throw err;
|
||||
} finally {
|
||||
if (!silent) {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<UserContext.Provider value={{ role, setRole, name, setName, email, setEmail, logout }}>
|
||||
{children}
|
||||
</UserContext.Provider>
|
||||
useEffect(() => {
|
||||
refreshUsers().catch((err) => {
|
||||
console.error("Failed to load users", err);
|
||||
});
|
||||
}, [refreshUsers]);
|
||||
|
||||
const selectUser = useCallback(
|
||||
(userID: string) => {
|
||||
const selected = users.find((item) => item.id === userID) || null;
|
||||
setCurrentUser(selected);
|
||||
persistCurrentUserID(selected?.id || null);
|
||||
},
|
||||
[users],
|
||||
);
|
||||
|
||||
const createUser = useCallback(async (payload: Pick<User, "name" | "email" | "role">) => {
|
||||
const created = await usersApi.create(payload);
|
||||
setUsers((previous) => [created, ...previous]);
|
||||
setCurrentUser(created);
|
||||
setError(null);
|
||||
persistCurrentUserID(created.id);
|
||||
return created;
|
||||
}, []);
|
||||
|
||||
const updateCurrentUser = useCallback(
|
||||
async (payload: Partial<User>) => {
|
||||
if (!currentUser) {
|
||||
return null;
|
||||
}
|
||||
const updated = await usersApi.update(currentUser.id, payload);
|
||||
setUsers((previous) => previous.map((item) => (item.id === updated.id ? updated : item)));
|
||||
setCurrentUser(updated);
|
||||
setError(null);
|
||||
persistCurrentUserID(updated.id);
|
||||
return updated;
|
||||
},
|
||||
[currentUser],
|
||||
);
|
||||
|
||||
const logout = useCallback(() => {
|
||||
setCurrentUser(null);
|
||||
persistCurrentUserID(null);
|
||||
}, []);
|
||||
|
||||
const value = useMemo<UserContextType>(
|
||||
() => ({
|
||||
currentUser,
|
||||
users,
|
||||
role: currentUser?.role || null,
|
||||
name: currentUser?.name || "",
|
||||
email: currentUser?.email || "",
|
||||
loading,
|
||||
error,
|
||||
refreshUsers,
|
||||
selectUser,
|
||||
createUser,
|
||||
updateCurrentUser,
|
||||
logout,
|
||||
}),
|
||||
[createUser, currentUser, error, loading, logout, refreshUsers, selectUser, updateCurrentUser, users],
|
||||
);
|
||||
|
||||
return <UserContext.Provider value={value}>{children}</UserContext.Provider>;
|
||||
}
|
||||
|
||||
export function useUser() {
|
||||
const context = useContext(UserContext);
|
||||
if (context === undefined) {
|
||||
throw new Error('useUser must be used within a UserProvider');
|
||||
throw new Error("useUser must be used within a UserProvider");
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
42
src/hooks/useAIStatus.ts
Normal file
42
src/hooks/useAIStatus.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { aiApi, type AIStatus } from "../services/api/ai";
|
||||
|
||||
const defaultStatus: AIStatus = {
|
||||
enabled: false,
|
||||
provider: "dashscope-openai-compatible",
|
||||
reason: "正在检查 AI 服务状态...",
|
||||
};
|
||||
|
||||
export function useAIStatus() {
|
||||
const [status, setStatus] = useState<AIStatus>(defaultStatus);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
aiApi.status()
|
||||
.then((result) => {
|
||||
if (!cancelled) {
|
||||
setStatus(result);
|
||||
setLoading(false);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
if (!cancelled) {
|
||||
setStatus({
|
||||
enabled: false,
|
||||
provider: "dashscope-openai-compatible",
|
||||
reason: error instanceof Error ? error.message : "无法连接 AI 服务",
|
||||
});
|
||||
setLoading(false);
|
||||
}
|
||||
});
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, []);
|
||||
|
||||
return {
|
||||
...status,
|
||||
loading,
|
||||
};
|
||||
}
|
||||
49
src/lib/clipboard.js
Normal file
49
src/lib/clipboard.js
Normal file
@ -0,0 +1,49 @@
|
||||
function resolveRuntimeValue(value, fallback) {
|
||||
return value === undefined ? fallback : value;
|
||||
}
|
||||
|
||||
export async function copyText(text, runtime = {}) {
|
||||
const navigatorLike = resolveRuntimeValue(runtime.navigator, globalThis.navigator);
|
||||
const documentLike = resolveRuntimeValue(runtime.document, globalThis.document);
|
||||
const windowLike = resolveRuntimeValue(runtime.window, globalThis.window);
|
||||
|
||||
if (navigatorLike?.clipboard?.writeText) {
|
||||
await navigatorLike.clipboard.writeText(text);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (
|
||||
documentLike?.createElement &&
|
||||
documentLike?.body?.appendChild &&
|
||||
documentLike?.body?.removeChild &&
|
||||
typeof documentLike.execCommand === "function"
|
||||
) {
|
||||
const textarea = documentLike.createElement("textarea");
|
||||
textarea.value = text;
|
||||
textarea.setAttribute("readonly", "");
|
||||
textarea.style.position = "fixed";
|
||||
textarea.style.opacity = "0";
|
||||
textarea.style.pointerEvents = "none";
|
||||
textarea.style.top = "0";
|
||||
textarea.style.left = "0";
|
||||
|
||||
documentLike.body.appendChild(textarea);
|
||||
textarea.focus?.();
|
||||
textarea.select?.();
|
||||
textarea.setSelectionRange?.(0, text.length);
|
||||
|
||||
try {
|
||||
if (documentLike.execCommand("copy")) {
|
||||
return true;
|
||||
}
|
||||
} finally {
|
||||
documentLike.body.removeChild(textarea);
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof windowLike?.prompt === "function") {
|
||||
windowLike.prompt("当前浏览器不支持自动复制,请手动复制以下内容", text);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
142
src/lib/datetime.ts
Normal file
142
src/lib/datetime.ts
Normal file
@ -0,0 +1,142 @@
|
||||
const FALLBACK_TIME_ZONE = "Asia/Shanghai";
|
||||
const DEFAULT_LOCALE = "zh-CN";
|
||||
|
||||
export const COMMON_TIME_ZONES = [
|
||||
"Asia/Shanghai",
|
||||
"Asia/Tokyo",
|
||||
"Asia/Singapore",
|
||||
"UTC",
|
||||
"Europe/London",
|
||||
"Europe/Berlin",
|
||||
"America/Los_Angeles",
|
||||
"America/New_York",
|
||||
];
|
||||
|
||||
function isValidDate(value: string | undefined) {
|
||||
return parseDateValue(value) !== null;
|
||||
}
|
||||
|
||||
export function isValidTimeZone(value: string | undefined) {
|
||||
if (!value || !value.trim()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
new Intl.DateTimeFormat(DEFAULT_LOCALE, { timeZone: value.trim() });
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function browserTimeZone() {
|
||||
try {
|
||||
return Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveTimeZone(value: string | undefined) {
|
||||
if (isValidTimeZone(value)) {
|
||||
return value!.trim();
|
||||
}
|
||||
const browser = browserTimeZone();
|
||||
if (isValidTimeZone(browser)) {
|
||||
return browser.trim();
|
||||
}
|
||||
return FALLBACK_TIME_ZONE;
|
||||
}
|
||||
|
||||
function formatterParts(
|
||||
value: string,
|
||||
timeZone: string,
|
||||
options: Intl.DateTimeFormatOptions,
|
||||
) {
|
||||
const formatter = new Intl.DateTimeFormat(DEFAULT_LOCALE, {
|
||||
timeZone: resolveTimeZone(timeZone),
|
||||
hour12: false,
|
||||
...options,
|
||||
});
|
||||
const parsed = parseDateValue(value);
|
||||
if (!parsed) {
|
||||
return {} as Record<string, string>;
|
||||
}
|
||||
const parts = formatter.formatToParts(parsed);
|
||||
return Object.fromEntries(
|
||||
parts
|
||||
.filter((part) => part.type !== "literal")
|
||||
.map((part) => [part.type, part.value]),
|
||||
) as Record<string, string>;
|
||||
}
|
||||
|
||||
function normalizeDateValue(value: string | undefined) {
|
||||
if (!value) {
|
||||
return "";
|
||||
}
|
||||
return value
|
||||
.trim()
|
||||
.replace(/(\.\d{3})\d+(Z|[+-]\d{2}:\d{2})$/, "$1$2")
|
||||
.replace(/(\.\d{3})\d+$/, "$1");
|
||||
}
|
||||
|
||||
function parseDateValue(value: string | undefined) {
|
||||
const normalized = normalizeDateValue(value);
|
||||
if (!normalized) {
|
||||
return null;
|
||||
}
|
||||
const parsed = new Date(normalized);
|
||||
return Number.isNaN(parsed.getTime()) ? null : parsed;
|
||||
}
|
||||
|
||||
export function formatDateTime(
|
||||
value: string | undefined,
|
||||
timeZone: string | undefined,
|
||||
fallback = "--",
|
||||
) {
|
||||
if (!isValidDate(value)) {
|
||||
return value || fallback;
|
||||
}
|
||||
const parts = formatterParts(value!, resolveTimeZone(timeZone), {
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
});
|
||||
return `${parts.year}-${parts.month}-${parts.day} ${parts.hour}:${parts.minute}:${parts.second}`;
|
||||
}
|
||||
|
||||
export function formatCompactDateTime(
|
||||
value: string | undefined,
|
||||
timeZone: string | undefined,
|
||||
fallback = "--",
|
||||
) {
|
||||
if (!isValidDate(value)) {
|
||||
return value || fallback;
|
||||
}
|
||||
const parts = formatterParts(value!, resolveTimeZone(timeZone), {
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
});
|
||||
return `${parts.year}-${parts.month}-${parts.day} ${parts.hour}:${parts.minute}`;
|
||||
}
|
||||
|
||||
export function formatClockTime(
|
||||
value: string | undefined,
|
||||
timeZone: string | undefined,
|
||||
fallback = "--",
|
||||
) {
|
||||
if (!isValidDate(value)) {
|
||||
return value || fallback;
|
||||
}
|
||||
const parts = formatterParts(value!, resolveTimeZone(timeZone), {
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
});
|
||||
return `${parts.hour}:${parts.minute}:${parts.second}`;
|
||||
}
|
||||
50
src/lib/operator-library.js
Normal file
50
src/lib/operator-library.js
Normal file
@ -0,0 +1,50 @@
|
||||
const PREFERRED_CATEGORY_ORDER = [
|
||||
"IO",
|
||||
"DATA",
|
||||
"AI",
|
||||
"CV",
|
||||
"UTIL",
|
||||
"LOGIC",
|
||||
"MATH",
|
||||
"FLOW",
|
||||
"HUMAN",
|
||||
];
|
||||
|
||||
function normalizeCategory(category) {
|
||||
const normalized = String(category || "").trim().toUpperCase();
|
||||
return normalized || "OTHER";
|
||||
}
|
||||
|
||||
export function getOperatorLibrarySections(operators) {
|
||||
const sections = new Map();
|
||||
|
||||
operators.forEach((operator) => {
|
||||
const category = normalizeCategory(operator?.category);
|
||||
if (!sections.has(category)) {
|
||||
sections.set(category, []);
|
||||
}
|
||||
sections.get(category).push(operator);
|
||||
});
|
||||
|
||||
return [...sections.entries()]
|
||||
.sort(([left], [right]) => {
|
||||
const leftIndex = PREFERRED_CATEGORY_ORDER.indexOf(left);
|
||||
const rightIndex = PREFERRED_CATEGORY_ORDER.indexOf(right);
|
||||
|
||||
if (leftIndex !== -1 || rightIndex !== -1) {
|
||||
if (leftIndex === -1) {
|
||||
return 1;
|
||||
}
|
||||
if (rightIndex === -1) {
|
||||
return -1;
|
||||
}
|
||||
return leftIndex - rightIndex;
|
||||
}
|
||||
|
||||
return left.localeCompare(right, "zh-CN");
|
||||
})
|
||||
.map(([category, items]) => ({
|
||||
category,
|
||||
operators: items,
|
||||
}));
|
||||
}
|
||||
34
src/lib/settings.js
Normal file
34
src/lib/settings.js
Normal file
@ -0,0 +1,34 @@
|
||||
function trimOrOmit(value) {
|
||||
if (typeof value !== "string") {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const trimmed = value.trim();
|
||||
return trimmed.length > 0 ? trimmed : undefined;
|
||||
}
|
||||
|
||||
export function buildSettingsUpdatePayload(config) {
|
||||
const { kubeconfigPreview: _kubeconfigPreview, ...k8s } = config.k8s;
|
||||
const { apiKeyPreview: _apiKeyPreview, ...ai } = config.ai;
|
||||
const payload = {
|
||||
...config,
|
||||
k8s: { ...k8s },
|
||||
ai: { ...ai },
|
||||
};
|
||||
|
||||
const kubeconfig = trimOrOmit(payload.k8s.kubeconfig);
|
||||
if (kubeconfig) {
|
||||
payload.k8s.kubeconfig = kubeconfig;
|
||||
} else {
|
||||
delete payload.k8s.kubeconfig;
|
||||
}
|
||||
|
||||
const apiKey = trimOrOmit(payload.ai.apiKey);
|
||||
if (apiKey) {
|
||||
payload.ai.apiKey = apiKey;
|
||||
} else {
|
||||
delete payload.ai.apiKey;
|
||||
}
|
||||
|
||||
return payload;
|
||||
}
|
||||
38
src/lib/templates.js
Normal file
38
src/lib/templates.js
Normal file
@ -0,0 +1,38 @@
|
||||
export function buildTemplatePayload(source, overrides = {}) {
|
||||
return {
|
||||
name: overrides.name ?? source.name ?? "",
|
||||
description: overrides.description ?? source.description ?? "",
|
||||
type: source.type || "batch",
|
||||
maxConcurrentRuns: source.maxConcurrentRuns ?? 0,
|
||||
nodeTimeoutSeconds: source.nodeTimeoutSeconds ?? 0,
|
||||
runTimeoutSeconds: source.runTimeoutSeconds ?? 0,
|
||||
maxRetryAttempts: source.maxRetryAttempts ?? 0,
|
||||
retryBackoffSeconds: source.retryBackoffSeconds ?? 0,
|
||||
schedule: source.schedule ? { ...source.schedule } : undefined,
|
||||
nodes: source.nodes || [],
|
||||
edges: source.edges || [],
|
||||
};
|
||||
}
|
||||
|
||||
export function buildWorkflowFromTemplate(template, overrides = {}) {
|
||||
return {
|
||||
name: overrides.name ?? `${template.name || "工作流模板"} 实例`,
|
||||
description: overrides.description ?? template.description ?? "",
|
||||
type: overrides.type ?? template.type ?? "batch",
|
||||
maxConcurrentRuns:
|
||||
overrides.maxConcurrentRuns ?? template.maxConcurrentRuns ?? 0,
|
||||
nodeTimeoutSeconds:
|
||||
overrides.nodeTimeoutSeconds ?? template.nodeTimeoutSeconds ?? 0,
|
||||
runTimeoutSeconds:
|
||||
overrides.runTimeoutSeconds ?? template.runTimeoutSeconds ?? 0,
|
||||
maxRetryAttempts:
|
||||
overrides.maxRetryAttempts ?? template.maxRetryAttempts ?? 0,
|
||||
retryBackoffSeconds:
|
||||
overrides.retryBackoffSeconds ?? template.retryBackoffSeconds ?? 0,
|
||||
schedule:
|
||||
overrides.schedule ??
|
||||
(template.schedule ? { ...template.schedule } : undefined),
|
||||
nodes: overrides.nodes ?? template.nodes ?? [],
|
||||
edges: overrides.edges ?? template.edges ?? [],
|
||||
};
|
||||
}
|
||||
44
src/lib/workflow-canvas.js
Normal file
44
src/lib/workflow-canvas.js
Normal file
@ -0,0 +1,44 @@
|
||||
function normalizeHandle(value) {
|
||||
return value ?? "";
|
||||
}
|
||||
|
||||
export function canConnectToInput(edges, connection) {
|
||||
if (!connection?.target) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const targetHandle = normalizeHandle(connection.targetHandle);
|
||||
|
||||
return !edges.some(
|
||||
(edge) =>
|
||||
edge.target === connection.target &&
|
||||
normalizeHandle(edge.targetHandle) === targetHandle,
|
||||
);
|
||||
}
|
||||
|
||||
export function removeSelectedCanvasElements(nodes, edges) {
|
||||
const selectedNodeIds = new Set(
|
||||
nodes.filter((node) => node.selected).map((node) => node.id),
|
||||
);
|
||||
const selectedEdgeIds = new Set(
|
||||
edges.filter((edge) => edge.selected).map((edge) => edge.id),
|
||||
);
|
||||
|
||||
const nextNodes = nodes.filter((node) => !selectedNodeIds.has(node.id));
|
||||
const nextEdges = edges.filter((edge) => {
|
||||
if (selectedEdgeIds.has(edge.id)) {
|
||||
return false;
|
||||
}
|
||||
if (selectedNodeIds.has(edge.source) || selectedNodeIds.has(edge.target)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
|
||||
return {
|
||||
nodes: nextNodes,
|
||||
edges: nextEdges,
|
||||
removedNodeIds: [...selectedNodeIds],
|
||||
removedEdgeIds: [...selectedEdgeIds],
|
||||
};
|
||||
}
|
||||
32
src/lib/workflow-editor.js
Normal file
32
src/lib/workflow-editor.js
Normal file
@ -0,0 +1,32 @@
|
||||
export function getWorkflowRunActionState({
|
||||
isTemplate,
|
||||
isReadOnly,
|
||||
workflowStatus,
|
||||
isExecuting,
|
||||
activeRunInFlight,
|
||||
}) {
|
||||
if (isTemplate) {
|
||||
return { disabled: true, label: "模板不可运行", reason: "template" };
|
||||
}
|
||||
if (isExecuting) {
|
||||
return { disabled: true, label: "正在启动...", reason: "starting" };
|
||||
}
|
||||
if (activeRunInFlight || workflowStatus === "running") {
|
||||
return { disabled: true, label: "工作流运行中", reason: "running" };
|
||||
}
|
||||
if (workflowStatus === "completed") {
|
||||
return { disabled: true, label: "已成功运行", reason: "completed" };
|
||||
}
|
||||
if (isReadOnly) {
|
||||
return { disabled: true, label: "只读模式", reason: "readOnly" };
|
||||
}
|
||||
return { disabled: false, label: "运行工作流", reason: "ready" };
|
||||
}
|
||||
|
||||
export function isWorkflowSettingsLocked({ isReadOnly, workflowStatus }) {
|
||||
return (
|
||||
Boolean(isReadOnly) ||
|
||||
workflowStatus === "running" ||
|
||||
workflowStatus === "completed"
|
||||
);
|
||||
}
|
||||
144
src/lib/workflow-run.js
Normal file
144
src/lib/workflow-run.js
Normal file
@ -0,0 +1,144 @@
|
||||
const RUNNING_STATUSES = new Set(["queued", "running", "waiting_for_audit"]);
|
||||
|
||||
const NODE_COLORS = {
|
||||
idle: "#475569",
|
||||
queued: "#64748b",
|
||||
running: "#60a5fa",
|
||||
waiting_for_audit: "#f59e0b",
|
||||
completed: "#34d399",
|
||||
failed: "#f87171",
|
||||
};
|
||||
|
||||
const EDGE_STYLES = {
|
||||
running: { stroke: "#60a5fa", strokeWidth: 3, opacity: 1 },
|
||||
waiting_for_audit: { stroke: "#f59e0b", strokeWidth: 3, opacity: 1 },
|
||||
completed: { stroke: "#34d399", strokeWidth: 2.5, opacity: 0.95 },
|
||||
failed: { stroke: "#f87171", strokeWidth: 2.5, opacity: 1 },
|
||||
idle: { stroke: "#334155", strokeWidth: 1.5, opacity: 0.45 },
|
||||
};
|
||||
|
||||
function executionLookup(run) {
|
||||
return new Map(
|
||||
Array.isArray(run?.nodeExecutions)
|
||||
? run.nodeExecutions.map((item) => [item.nodeId, item])
|
||||
: [],
|
||||
);
|
||||
}
|
||||
|
||||
export function isRunInFlight(run) {
|
||||
return Boolean(run?.status && RUNNING_STATUSES.has(run.status));
|
||||
}
|
||||
|
||||
export function getTrackedRunId(runIdParam, workflow) {
|
||||
const explicitRunId =
|
||||
typeof runIdParam === "string" ? runIdParam.trim() : "";
|
||||
if (explicitRunId) {
|
||||
return explicitRunId;
|
||||
}
|
||||
|
||||
const workflowRunId =
|
||||
typeof workflow?.lastRunId === "string" ? workflow.lastRunId.trim() : "";
|
||||
if (workflow?.status === "running" && workflowRunId) {
|
||||
return workflowRunId;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
export function getNodeExecutionState(run, nodeId) {
|
||||
if (!run || !nodeId) {
|
||||
return "idle";
|
||||
}
|
||||
|
||||
const execution = executionLookup(run).get(nodeId);
|
||||
if (execution?.status) {
|
||||
return execution.status;
|
||||
}
|
||||
|
||||
if (run.currentNodeId === nodeId) {
|
||||
if (run.status === "waiting_for_audit") {
|
||||
return "waiting_for_audit";
|
||||
}
|
||||
if (run.status === "running") {
|
||||
return "running";
|
||||
}
|
||||
}
|
||||
|
||||
return "idle";
|
||||
}
|
||||
|
||||
function buildNodeStateLookup(nodes, run) {
|
||||
return new Map(nodes.map((node) => [node.id, getNodeExecutionState(run, node.id)]));
|
||||
}
|
||||
|
||||
function edgeExecutionState(edge, nodeStates) {
|
||||
const sourceState = nodeStates.get(edge.source) || "idle";
|
||||
const targetState = nodeStates.get(edge.target) || "idle";
|
||||
|
||||
if (targetState === "failed" && sourceState === "completed") {
|
||||
return "failed";
|
||||
}
|
||||
if (targetState === "running" && sourceState === "completed") {
|
||||
return "running";
|
||||
}
|
||||
if (targetState === "waiting_for_audit" && sourceState === "completed") {
|
||||
return "waiting_for_audit";
|
||||
}
|
||||
if (sourceState === "completed" && targetState === "completed") {
|
||||
return "completed";
|
||||
}
|
||||
return "idle";
|
||||
}
|
||||
|
||||
export function decorateWorkflowNodes(nodes, run) {
|
||||
return nodes.map((node) => {
|
||||
const executionState = getNodeExecutionState(run, node.id);
|
||||
return {
|
||||
...node,
|
||||
data: {
|
||||
...node.data,
|
||||
running: executionState === "running",
|
||||
executionState,
|
||||
minimapColor: NODE_COLORS[executionState] || NODE_COLORS.idle,
|
||||
},
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function decorateWorkflowEdges(edges, nodes, run) {
|
||||
const nodeStates = buildNodeStateLookup(nodes, run);
|
||||
|
||||
return edges.map((edge) => {
|
||||
const executionState = edgeExecutionState(edge, nodeStates);
|
||||
const accent = EDGE_STYLES[executionState] || EDGE_STYLES.idle;
|
||||
const animated =
|
||||
executionState === "running" || executionState === "waiting_for_audit";
|
||||
|
||||
return {
|
||||
...edge,
|
||||
animated: Boolean(edge.animated) || animated,
|
||||
style: {
|
||||
...edge.style,
|
||||
...accent,
|
||||
},
|
||||
data: {
|
||||
...edge.data,
|
||||
executionState,
|
||||
},
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function getCurrentNodeLabel(run, nodes) {
|
||||
if (!run?.currentNodeId) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const execution = executionLookup(run).get(run.currentNodeId);
|
||||
if (execution?.nodeLabel) {
|
||||
return execution.nodeLabel;
|
||||
}
|
||||
|
||||
const matchedNode = nodes.find((node) => node.id === run.currentNodeId);
|
||||
return matchedNode?.data?.label || "";
|
||||
}
|
||||
19
src/lib/workflow.ts
Normal file
19
src/lib/workflow.ts
Normal file
@ -0,0 +1,19 @@
|
||||
export function countWorkflowOperators(
|
||||
nodes?: Array<{ type?: string; data?: Record<string, unknown> } | null>,
|
||||
): number {
|
||||
if (!Array.isArray(nodes)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return nodes.filter((node) => {
|
||||
if (!node || typeof node !== "object") {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (node.type === "operator") {
|
||||
return true;
|
||||
}
|
||||
|
||||
return typeof node.type !== "string" && typeof node.data === "object";
|
||||
}).length;
|
||||
}
|
||||
@ -1,89 +1,58 @@
|
||||
import React, { useState } from "react";
|
||||
import {
|
||||
ClipboardCheck,
|
||||
Search,
|
||||
Filter,
|
||||
CheckCircle2,
|
||||
XCircle,
|
||||
Eye,
|
||||
Clock,
|
||||
User,
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import {
|
||||
ClipboardCheck,
|
||||
CheckCircle2,
|
||||
XCircle,
|
||||
Eye,
|
||||
Clock,
|
||||
User,
|
||||
ExternalLink,
|
||||
Info,
|
||||
AlertCircle
|
||||
AlertCircle,
|
||||
} from "lucide-react";
|
||||
import { cn } from "../lib/utils";
|
||||
import { motion, AnimatePresence } from "motion/react";
|
||||
|
||||
interface AuditTask {
|
||||
id: string;
|
||||
workflowName: string;
|
||||
projectName: string;
|
||||
operatorName: string;
|
||||
createdAt: string;
|
||||
status: "pending" | "approved" | "rejected";
|
||||
data: any;
|
||||
}
|
||||
|
||||
const MOCK_TASKS: AuditTask[] = [
|
||||
{
|
||||
id: "task-1",
|
||||
workflowName: "视觉处理流程",
|
||||
projectName: "具身智能抓取任务",
|
||||
operatorName: "人工审核 (Human Review)",
|
||||
createdAt: "2024-03-22 10:15",
|
||||
status: "pending",
|
||||
data: {
|
||||
image: "https://picsum.photos/seed/robot1/800/600",
|
||||
confidence: 0.65,
|
||||
detectedObjects: ["抓取目标 (Target)", "障碍物 (Obstacle)"],
|
||||
reason: "置信度低于阈值 (0.7),需要人工确认抓取点。"
|
||||
}
|
||||
},
|
||||
{
|
||||
id: "task-2",
|
||||
workflowName: "实时数据监听",
|
||||
projectName: "自动驾驶感知流程",
|
||||
operatorName: "人工审核 (Human Review)",
|
||||
createdAt: "2024-03-22 11:00",
|
||||
status: "pending",
|
||||
data: {
|
||||
image: "https://picsum.photos/seed/car1/800/600",
|
||||
confidence: 0.42,
|
||||
detectedObjects: ["行人 (Pedestrian)"],
|
||||
reason: "遮挡严重,无法确定行人意图。"
|
||||
}
|
||||
},
|
||||
{
|
||||
id: "task-3",
|
||||
workflowName: "视觉处理流程",
|
||||
projectName: "具身智能抓取任务",
|
||||
operatorName: "人工审核 (Human Review)",
|
||||
createdAt: "2024-03-22 09:30",
|
||||
status: "approved",
|
||||
data: {
|
||||
image: "https://picsum.photos/seed/robot2/800/600",
|
||||
confidence: 0.68,
|
||||
detectedObjects: ["抓取目标 (Target)"],
|
||||
reason: "边缘检测模糊。"
|
||||
}
|
||||
}
|
||||
];
|
||||
import type { AuditTask } from "../types";
|
||||
import { auditApi } from "../services/api/audit";
|
||||
import { useTimeZone } from "../context/TimeZoneContext";
|
||||
import { formatCompactDateTime } from "../lib/datetime";
|
||||
|
||||
export default function Audit() {
|
||||
const [tasks, setTasks] = useState(MOCK_TASKS);
|
||||
const navigate = useNavigate();
|
||||
const { timeZone } = useTimeZone();
|
||||
const [tasks, setTasks] = useState<AuditTask[]>([]);
|
||||
const [selectedTask, setSelectedTask] = useState<AuditTask | null>(null);
|
||||
const [filter, setFilter] = useState<"all" | "pending" | "completed">("pending");
|
||||
const [filter, setFilter] = useState<"all" | "pending" | "completed">(
|
||||
"pending",
|
||||
);
|
||||
|
||||
const filteredTasks = tasks.filter(task => {
|
||||
useEffect(() => {
|
||||
auditApi
|
||||
.list("all")
|
||||
.then(setTasks)
|
||||
.catch((error) => {
|
||||
console.error("Failed to load audit tasks", error);
|
||||
});
|
||||
}, []);
|
||||
|
||||
const filteredTasks = tasks.filter((task) => {
|
||||
if (filter === "all") return true;
|
||||
if (filter === "pending") return task.status === "pending";
|
||||
return task.status !== "pending";
|
||||
});
|
||||
|
||||
const handleAction = (taskId: string, action: "approved" | "rejected") => {
|
||||
setTasks(tasks.map(t => t.id === taskId ? { ...t, status: action } : t));
|
||||
setSelectedTask(null);
|
||||
const request =
|
||||
action === "approved"
|
||||
? auditApi.approve(taskId)
|
||||
: auditApi.reject(taskId);
|
||||
request
|
||||
.then((updated) => {
|
||||
setTasks(tasks.map((task) => (task.id === taskId ? updated : task)));
|
||||
setSelectedTask(null);
|
||||
})
|
||||
.catch((error) => console.error("Failed to resolve audit task", error));
|
||||
};
|
||||
|
||||
return (
|
||||
@ -94,34 +63,42 @@ export default function Audit() {
|
||||
<ClipboardCheck className="w-8 h-8 text-blue-500" />
|
||||
任务审核 (Task Audit)
|
||||
</h1>
|
||||
<p className="text-gray-500 mt-1">处理工作流中需要人工介入的审核请求。</p>
|
||||
<p className="text-gray-500 mt-1">
|
||||
处理工作流中需要人工介入的审核请求。
|
||||
</p>
|
||||
</div>
|
||||
|
||||
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="flex bg-[#151619] border border-[#2a2b2f] rounded-lg p-1">
|
||||
<button
|
||||
<button
|
||||
onClick={() => setFilter("pending")}
|
||||
className={cn(
|
||||
"px-4 py-1.5 text-xs font-medium rounded-md transition-all",
|
||||
filter === "pending" ? "bg-blue-600 text-white shadow-lg" : "text-gray-400 hover:text-white"
|
||||
filter === "pending"
|
||||
? "bg-blue-600 text-white shadow-lg"
|
||||
: "text-gray-400 hover:text-white",
|
||||
)}
|
||||
>
|
||||
待审核 ({tasks.filter(t => t.status === "pending").length})
|
||||
待审核 ({tasks.filter((t) => t.status === "pending").length})
|
||||
</button>
|
||||
<button
|
||||
<button
|
||||
onClick={() => setFilter("completed")}
|
||||
className={cn(
|
||||
"px-4 py-1.5 text-xs font-medium rounded-md transition-all",
|
||||
filter === "completed" ? "bg-blue-600 text-white shadow-lg" : "text-gray-400 hover:text-white"
|
||||
filter === "completed"
|
||||
? "bg-blue-600 text-white shadow-lg"
|
||||
: "text-gray-400 hover:text-white",
|
||||
)}
|
||||
>
|
||||
已处理
|
||||
</button>
|
||||
<button
|
||||
<button
|
||||
onClick={() => setFilter("all")}
|
||||
className={cn(
|
||||
"px-4 py-1.5 text-xs font-medium rounded-md transition-all",
|
||||
filter === "all" ? "bg-blue-600 text-white shadow-lg" : "text-gray-400 hover:text-white"
|
||||
filter === "all"
|
||||
? "bg-blue-600 text-white shadow-lg"
|
||||
: "text-gray-400 hover:text-white",
|
||||
)}
|
||||
>
|
||||
全部
|
||||
@ -146,7 +123,9 @@ export default function Audit() {
|
||||
onClick={() => setSelectedTask(task)}
|
||||
className={cn(
|
||||
"bg-[#151619] border rounded-xl p-4 cursor-pointer transition-all hover:shadow-xl group",
|
||||
selectedTask?.id === task.id ? "border-blue-500 bg-blue-500/5" : "border-[#2a2b2f] hover:border-gray-700"
|
||||
selectedTask?.id === task.id
|
||||
? "border-blue-500 bg-blue-500/5"
|
||||
: "border-[#2a2b2f] hover:border-gray-700",
|
||||
)}
|
||||
>
|
||||
<div className="flex items-center justify-between mb-3">
|
||||
@ -158,21 +137,32 @@ export default function Audit() {
|
||||
ID: {task.id}
|
||||
</span>
|
||||
</div>
|
||||
<span className={cn(
|
||||
"px-2 py-0.5 rounded-full text-[10px] font-medium uppercase tracking-wider border",
|
||||
task.status === "pending" ? "bg-orange-500/10 text-orange-500 border-orange-500/20" :
|
||||
task.status === "approved" ? "bg-green-500/10 text-green-500 border-green-500/20" :
|
||||
"bg-red-500/10 text-red-500 border-red-500/20"
|
||||
)}>
|
||||
{task.status === "pending" ? "待审核" : task.status === "approved" ? "已通过" : "已拒绝"}
|
||||
<span
|
||||
className={cn(
|
||||
"px-2 py-0.5 rounded-full text-[10px] font-medium uppercase tracking-wider border",
|
||||
task.status === "pending"
|
||||
? "bg-orange-500/10 text-orange-500 border-orange-500/20"
|
||||
: task.status === "approved"
|
||||
? "bg-green-500/10 text-green-500 border-green-500/20"
|
||||
: "bg-red-500/10 text-red-500 border-red-500/20",
|
||||
)}
|
||||
>
|
||||
{task.status === "pending"
|
||||
? "待审核"
|
||||
: task.status === "approved"
|
||||
? "已通过"
|
||||
: "已拒绝"}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<h3 className="text-white font-bold mb-1 group-hover:text-blue-400 transition-colors">{task.workflowName}</h3>
|
||||
|
||||
<h3 className="text-white font-bold mb-1 group-hover:text-blue-400 transition-colors">
|
||||
{task.workflowName}
|
||||
</h3>
|
||||
<p className="text-xs text-gray-500 mb-4 flex items-center gap-1.5">
|
||||
<Clock className="w-3 h-3" /> {task.createdAt}
|
||||
<Clock className="w-3 h-3" />{" "}
|
||||
{formatCompactDateTime(task.createdAt, timeZone)}
|
||||
</p>
|
||||
|
||||
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="w-6 h-6 rounded-full bg-gray-800 flex items-center justify-center">
|
||||
@ -193,7 +183,7 @@ export default function Audit() {
|
||||
<div className="w-[450px] bg-[#151619] border border-[#2a2b2f] rounded-2xl overflow-hidden flex flex-col shadow-2xl">
|
||||
{selectedTask ? (
|
||||
<AnimatePresence mode="wait">
|
||||
<motion.div
|
||||
<motion.div
|
||||
key={selectedTask.id}
|
||||
initial={{ opacity: 0, x: 20 }}
|
||||
animate={{ opacity: 1, x: 0 }}
|
||||
@ -204,24 +194,38 @@ export default function Audit() {
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<h2 className="text-lg font-bold text-white">审核详情</h2>
|
||||
<div className="flex items-center gap-2">
|
||||
<button className="p-2 hover:bg-white/5 rounded-md text-gray-500 hover:text-white transition-colors">
|
||||
<button
|
||||
onClick={() =>
|
||||
navigate(
|
||||
`/monitoring?runId=${encodeURIComponent(selectedTask.runId)}`,
|
||||
)
|
||||
}
|
||||
className="p-2 hover:bg-white/5 rounded-md text-gray-500 hover:text-white transition-colors"
|
||||
title="查看对应执行监控"
|
||||
>
|
||||
<ExternalLink className="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<div className="space-y-3">
|
||||
<div className="flex justify-between text-xs">
|
||||
<span className="text-gray-500">所属项目</span>
|
||||
<span className="text-gray-300 font-medium">{selectedTask.projectName}</span>
|
||||
<span className="text-gray-300 font-medium">
|
||||
{selectedTask.projectName}
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex justify-between text-xs">
|
||||
<span className="text-gray-500">工作流</span>
|
||||
<span className="text-gray-300 font-medium">{selectedTask.workflowName}</span>
|
||||
<span className="text-gray-300 font-medium">
|
||||
{selectedTask.workflowName}
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex justify-between text-xs">
|
||||
<span className="text-gray-500">触发算子</span>
|
||||
<span className="text-blue-400 font-medium">{selectedTask.operatorName}</span>
|
||||
<span className="text-blue-400 font-medium">
|
||||
{selectedTask.operatorName}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -233,16 +237,17 @@ export default function Audit() {
|
||||
<Eye className="w-3 h-3" /> 数据预览
|
||||
</label>
|
||||
<div className="aspect-video rounded-xl overflow-hidden border border-[#2a2b2f] bg-black relative group">
|
||||
<img
|
||||
src={selectedTask.data.image}
|
||||
alt="Audit Preview"
|
||||
<img
|
||||
src={selectedTask.data.image}
|
||||
alt="Audit Preview"
|
||||
className="w-full h-full object-cover"
|
||||
referrerPolicy="no-referrer"
|
||||
/>
|
||||
<div className="absolute inset-0 bg-gradient-to-t from-black/60 to-transparent flex flex-col justify-end p-4">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="px-2 py-0.5 bg-blue-600 text-white text-[10px] font-bold rounded">
|
||||
置信度: {(selectedTask.data.confidence * 100).toFixed(0)}%
|
||||
置信度:{" "}
|
||||
{(selectedTask.data.confidence * 100).toFixed(0)}%
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
@ -254,7 +259,9 @@ export default function Audit() {
|
||||
<div className="flex items-start gap-3">
|
||||
<AlertCircle className="w-5 h-5 text-blue-500 shrink-0 mt-0.5" />
|
||||
<div>
|
||||
<h4 className="text-sm font-bold text-blue-400 mb-1">审核原因</h4>
|
||||
<h4 className="text-sm font-bold text-blue-400 mb-1">
|
||||
审核原因
|
||||
</h4>
|
||||
<p className="text-xs text-gray-400 leading-relaxed">
|
||||
{selectedTask.data.reason}
|
||||
</p>
|
||||
@ -268,24 +275,29 @@ export default function Audit() {
|
||||
<Info className="w-3 h-3" /> 检测结果 (AI)
|
||||
</label>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{selectedTask.data.detectedObjects.map((obj: string, i: number) => (
|
||||
<span key={i} className="px-3 py-1 bg-[#1a1b1e] border border-[#2a2b2f] rounded-full text-xs text-gray-300">
|
||||
{obj}
|
||||
</span>
|
||||
))}
|
||||
{selectedTask.data.detectedObjects.map(
|
||||
(obj: string, i: number) => (
|
||||
<span
|
||||
key={i}
|
||||
className="px-3 py-1 bg-[#1a1b1e] border border-[#2a2b2f] rounded-full text-xs text-gray-300"
|
||||
>
|
||||
{obj}
|
||||
</span>
|
||||
),
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{selectedTask.status === "pending" ? (
|
||||
<div className="p-6 border-t border-[#2a2b2f] bg-[#1a1b1e] grid grid-cols-2 gap-4">
|
||||
<button
|
||||
<button
|
||||
onClick={() => handleAction(selectedTask.id, "rejected")}
|
||||
className="flex items-center justify-center gap-2 px-4 py-2.5 bg-red-500/10 hover:bg-red-500/20 text-red-500 border border-red-500/20 rounded-xl text-sm font-bold transition-all"
|
||||
>
|
||||
<XCircle className="w-4 h-4" /> 审核拒绝
|
||||
</button>
|
||||
<button
|
||||
<button
|
||||
onClick={() => handleAction(selectedTask.id, "approved")}
|
||||
className="flex items-center justify-center gap-2 px-4 py-2.5 bg-green-600 hover:bg-green-700 text-white rounded-xl text-sm font-bold shadow-lg shadow-green-600/20 transition-all"
|
||||
>
|
||||
@ -304,7 +316,9 @@ export default function Audit() {
|
||||
<div className="w-16 h-16 bg-[#1a1b1e] border border-[#2a2b2f] rounded-2xl flex items-center justify-center mb-6">
|
||||
<ClipboardCheck className="w-8 h-8 opacity-20" />
|
||||
</div>
|
||||
<h3 className="text-white font-bold mb-2">选择一个任务开始审核</h3>
|
||||
<h3 className="text-white font-bold mb-2">
|
||||
选择一个任务开始审核
|
||||
</h3>
|
||||
<p className="text-xs leading-relaxed">
|
||||
点击左侧列表中的待审核任务,查看详细数据并进行人工确认。
|
||||
</p>
|
||||
|
||||
@ -1,33 +1,47 @@
|
||||
import React from "react";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { dashboardApi } from "../services/api/dashboard";
|
||||
import { useUser } from "../context/UserContext";
|
||||
import type { DashboardSummary } from "../types";
|
||||
|
||||
export default function Dashboard() {
|
||||
const [summary, setSummary] = useState<DashboardSummary | null>(null);
|
||||
const { currentUser } = useUser();
|
||||
|
||||
useEffect(() => {
|
||||
dashboardApi.getSummary().then(setSummary).catch((error) => {
|
||||
console.error("Failed to load dashboard summary", error);
|
||||
});
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="space-y-8">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold tracking-tight text-white">EmboFlow 仪表盘</h1>
|
||||
<p className="text-gray-500 mt-1">欢迎回来,管理员。这是您的具身智能数据处理概览。</p>
|
||||
<h1 className="text-3xl font-bold tracking-tight text-white">DroboticFlow 仪表盘</h1>
|
||||
<p className="text-gray-500 mt-1">
|
||||
{currentUser ? `欢迎回来,${currentUser.name}。这里是当前平台的真实执行概览。` : "这里是当前平台的真实执行概览。"}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-3 gap-6">
|
||||
<div className="bg-gradient-to-br from-blue-600 to-blue-800 p-6 rounded-2xl shadow-xl shadow-blue-900/20">
|
||||
<h3 className="text-blue-100 text-sm font-medium uppercase tracking-wider mb-1">活跃项目</h3>
|
||||
<div className="text-4xl font-bold text-white">12</div>
|
||||
<div className="text-4xl font-bold text-white">{summary?.projects.active ?? "--"}</div>
|
||||
<div className="mt-4 text-blue-200 text-xs flex items-center gap-1">
|
||||
<span className="font-bold">+2</span> 较上周增加
|
||||
<span className="font-bold">{summary?.projects.archived ?? 0}</span> 已归档项目
|
||||
</div>
|
||||
</div>
|
||||
<div className="bg-[#151619] border border-[#2a2b2f] p-6 rounded-2xl">
|
||||
<h3 className="text-gray-500 text-sm font-medium uppercase tracking-wider mb-1">已注册算子</h3>
|
||||
<div className="text-4xl font-bold text-white">45</div>
|
||||
<div className="text-4xl font-bold text-white">{summary?.operators.total ?? "--"}</div>
|
||||
<div className="mt-4 text-gray-600 text-xs flex items-center gap-1">
|
||||
<span className="text-green-500 font-bold">100%</span> 正常运行
|
||||
<span className="text-green-500 font-bold">{summary?.operators.custom ?? 0}</span> 自定义算子
|
||||
</div>
|
||||
</div>
|
||||
<div className="bg-[#151619] border border-[#2a2b2f] p-6 rounded-2xl">
|
||||
<h3 className="text-gray-500 text-sm font-medium uppercase tracking-wider mb-1">今日 Job 执行数</h3>
|
||||
<div className="text-4xl font-bold text-white">1,284</div>
|
||||
<div className="text-4xl font-bold text-white">{summary?.jobs.today ?? "--"}</div>
|
||||
<div className="mt-4 text-gray-600 text-xs flex items-center gap-1">
|
||||
<span className="text-blue-500 font-bold">98.5%</span> 成功率
|
||||
<span className="text-blue-500 font-bold">{summary?.jobs.successRate ?? 0}%</span> 成功率
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,14 +1,29 @@
|
||||
import React, { useState } from "react";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { Cpu, Plus, Search, Code, Box, X, Info, Layers, Edit3, Trash2, Sparkles, Loader2, AlertCircle } from "lucide-react";
|
||||
import { useUser } from "../context/UserContext";
|
||||
import { MOCK_OPERATORS } from "../constants";
|
||||
import { cn } from "../lib/utils";
|
||||
import { Operator, PortDefinition } from "../types";
|
||||
import { generateOperatorFromDescription } from "../services/aiService";
|
||||
import { operatorsApi } from "../services/api/operators";
|
||||
import { useAIStatus } from "../hooks/useAIStatus";
|
||||
|
||||
function createPortID() {
|
||||
if (typeof globalThis.crypto?.randomUUID === "function") {
|
||||
return globalThis.crypto.randomUUID();
|
||||
}
|
||||
if (typeof globalThis.crypto?.getRandomValues === "function") {
|
||||
const bytes = new Uint8Array(16);
|
||||
globalThis.crypto.getRandomValues(bytes);
|
||||
return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
||||
}
|
||||
return `port-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
|
||||
}
|
||||
|
||||
export default function Operators() {
|
||||
const { role } = useUser();
|
||||
const [operators, setOperators] = useState<Operator[]>(MOCK_OPERATORS as any);
|
||||
const aiStatus = useAIStatus();
|
||||
const [operators, setOperators] = useState<Operator[]>([]);
|
||||
const [searchQuery, setSearchQuery] = useState("");
|
||||
const [isModalOpen, setIsModalOpen] = useState(false);
|
||||
const [isAIModalOpen, setIsAIModalOpen] = useState(false);
|
||||
const [aiDescription, setAiDescription] = useState("");
|
||||
@ -25,6 +40,12 @@ export default function Operators() {
|
||||
skillDefinition: ""
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
operatorsApi.list().then(setOperators).catch((error) => {
|
||||
console.error("Failed to load operators", error);
|
||||
});
|
||||
}, []);
|
||||
|
||||
const handleAddHyperParam = () => {
|
||||
const hp = {
|
||||
key: `PARAM_${(newOp.hyperparams?.length || 0) + 1}`,
|
||||
@ -35,9 +56,10 @@ export default function Operators() {
|
||||
};
|
||||
|
||||
const handleAddPort = (type: 'inputs' | 'outputs') => {
|
||||
const nextIndex = (newOp[type]?.length ?? 0) + 1;
|
||||
const port: PortDefinition = {
|
||||
id: Math.random().toString(36).substr(2, 9),
|
||||
name: `端口 ${newOp[type]?.length || 0 + 1}`,
|
||||
id: createPortID(),
|
||||
name: `端口 ${nextIndex}`,
|
||||
type: "json",
|
||||
transmission: "bus"
|
||||
};
|
||||
@ -47,21 +69,27 @@ export default function Operators() {
|
||||
const handleRegister = () => {
|
||||
if (newOp.name && newOp.description) {
|
||||
if (editingOpId) {
|
||||
setOperators(operators.map(op => op.id === editingOpId ? { ...op, ...newOp } as Operator : op));
|
||||
operatorsApi
|
||||
.update(editingOpId, newOp)
|
||||
.then((updated) =>
|
||||
setOperators(operators.map((op) => (op.id === editingOpId ? updated : op))),
|
||||
)
|
||||
.catch((error) => console.error("Failed to update operator", error));
|
||||
} else {
|
||||
const op: Operator = {
|
||||
id: `op-${Date.now()}`,
|
||||
name: newOp.name,
|
||||
type: "custom",
|
||||
category: newOp.category || "CV",
|
||||
description: newOp.description,
|
||||
inputs: newOp.inputs || [],
|
||||
outputs: newOp.outputs || [],
|
||||
hyperparams: newOp.hyperparams || [],
|
||||
image: newOp.image,
|
||||
skillDefinition: newOp.skillDefinition
|
||||
};
|
||||
setOperators([op, ...operators]);
|
||||
operatorsApi
|
||||
.create({
|
||||
name: newOp.name,
|
||||
type: "custom",
|
||||
category: newOp.category || "CV",
|
||||
description: newOp.description,
|
||||
inputs: newOp.inputs || [],
|
||||
outputs: newOp.outputs || [],
|
||||
hyperparams: newOp.hyperparams || [],
|
||||
image: newOp.image,
|
||||
skillDefinition: newOp.skillDefinition,
|
||||
})
|
||||
.then((created) => setOperators([created, ...operators]))
|
||||
.catch((error) => console.error("Failed to create operator", error));
|
||||
}
|
||||
setIsModalOpen(false);
|
||||
setEditingOpId(null);
|
||||
@ -88,8 +116,8 @@ export default function Operators() {
|
||||
name: generated.name,
|
||||
category: generated.category as any,
|
||||
description: generated.description,
|
||||
inputs: generated.inputs.map(i => ({ id: Math.random().toString(36).substr(2, 9), ...i })) as any,
|
||||
outputs: generated.outputs.map(o => ({ id: Math.random().toString(36).substr(2, 9), ...o })) as any,
|
||||
inputs: generated.inputs.map(i => ({ id: createPortID(), ...i })) as any,
|
||||
outputs: generated.outputs.map(o => ({ id: createPortID(), ...o })) as any,
|
||||
hyperparams: generated.hyperparams || [],
|
||||
skillDefinition: generated.skillDefinition
|
||||
});
|
||||
@ -104,10 +132,22 @@ export default function Operators() {
|
||||
};
|
||||
|
||||
const handleDelete = (id: string) => {
|
||||
setOperators(operators.filter(op => op.id !== id));
|
||||
operatorsApi
|
||||
.remove(id)
|
||||
.then(() => setOperators(operators.filter((op) => op.id !== id)))
|
||||
.catch((error) => console.error("Failed to delete operator", error));
|
||||
};
|
||||
|
||||
const canManage = role === "admin" || role === "operator";
|
||||
const normalizedSearch = searchQuery.trim().toLowerCase();
|
||||
const filteredOperators = operators.filter((op) => {
|
||||
if (!normalizedSearch) {
|
||||
return true;
|
||||
}
|
||||
return [op.name, op.description, op.category, op.type]
|
||||
.filter(Boolean)
|
||||
.some((value) => value.toLowerCase().includes(normalizedSearch));
|
||||
});
|
||||
|
||||
return (
|
||||
<div className="space-y-8">
|
||||
@ -124,7 +164,14 @@ export default function Operators() {
|
||||
setAiError(null);
|
||||
setIsAIModalOpen(true);
|
||||
}}
|
||||
className="flex items-center gap-2 bg-white/5 hover:bg-white/10 text-blue-400 px-4 py-2 rounded-md text-sm font-medium transition-colors border border-blue-500/20"
|
||||
disabled={!aiStatus.enabled}
|
||||
title={aiStatus.enabled ? `使用 ${aiStatus.model || "glm-5"} 辅助生成算子定义` : (aiStatus.reason || "当前服务端未配置 AI 服务")}
|
||||
className={cn(
|
||||
"flex items-center gap-2 px-4 py-2 rounded-md text-sm font-medium transition-colors border border-blue-500/20",
|
||||
aiStatus.enabled
|
||||
? "bg-white/5 hover:bg-white/10 text-blue-400"
|
||||
: "bg-white/5 text-gray-500 cursor-not-allowed opacity-60",
|
||||
)}
|
||||
>
|
||||
<Sparkles className="w-4 h-4" />
|
||||
AI 辅助注册
|
||||
@ -150,15 +197,18 @@ export default function Operators() {
|
||||
<input
|
||||
type="text"
|
||||
placeholder="搜索算子..."
|
||||
value={searchQuery}
|
||||
onChange={(event) => setSearchQuery(event.target.value)}
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md pl-10 pr-4 py-2 text-sm focus:outline-none focus:border-blue-500 transition-colors"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||
{operators.map((op) => (
|
||||
{filteredOperators.map((op) => (
|
||||
<div
|
||||
key={op.id}
|
||||
data-testid="operator-card"
|
||||
onDoubleClick={() => canManage && handleEdit(op)}
|
||||
className={cn(
|
||||
"bg-[#151619] border border-[#2a2b2f] rounded-xl p-6 hover:bg-[#1a1b1e] transition-all group relative",
|
||||
@ -251,6 +301,11 @@ export default function Operators() {
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
{filteredOperators.length === 0 && (
|
||||
<div className="col-span-full bg-[#151619] border border-[#2a2b2f] rounded-xl p-12 text-center text-gray-500">
|
||||
{operators.length === 0 ? "当前还没有算子,请先注册或同步内置算子。" : "没有匹配的算子,请调整搜索条件。"}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Registration/Edit Modal */}
|
||||
@ -663,6 +718,20 @@ export default function Operators() {
|
||||
{aiError}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!aiStatus.enabled && (
|
||||
<div className="p-3 bg-yellow-500/10 border border-yellow-500/20 rounded-lg flex items-center gap-3 text-yellow-300 text-xs">
|
||||
<AlertCircle className="w-4 h-4 shrink-0" />
|
||||
{aiStatus.reason || "当前服务端未配置 AI 服务,无法生成算子。"}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{aiStatus.enabled && (
|
||||
<div className="p-3 bg-blue-500/10 border border-blue-500/20 rounded-lg flex items-center gap-3 text-blue-200 text-xs">
|
||||
<Sparkles className="w-4 h-4 shrink-0" />
|
||||
当前由服务端模型 `{aiStatus.model || "glm-5"}` 生成算子定义,API Key 不会暴露到浏览器。
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="p-6 border-t border-[#2a2b2f] flex justify-end gap-3 bg-[#0a0a0a]/50">
|
||||
@ -675,10 +744,10 @@ export default function Operators() {
|
||||
</button>
|
||||
<button
|
||||
onClick={handleAIGenerate}
|
||||
disabled={!aiDescription.trim() || isGenerating}
|
||||
disabled={!aiStatus.enabled || !aiDescription.trim() || isGenerating}
|
||||
className={cn(
|
||||
"px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-md text-sm font-medium transition-all flex items-center gap-2",
|
||||
(!aiDescription.trim() || isGenerating) && "opacity-50 cursor-not-allowed"
|
||||
(!aiStatus.enabled || !aiDescription.trim() || isGenerating) && "opacity-50 cursor-not-allowed"
|
||||
)}
|
||||
>
|
||||
{isGenerating ? (
|
||||
|
||||
@ -1,12 +1,15 @@
|
||||
import React, { useState } from "react";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { useParams, useNavigate } from "react-router-dom";
|
||||
import { useTimeZone } from "../context/TimeZoneContext";
|
||||
import { useUser } from "../context/UserContext";
|
||||
import {
|
||||
Plus,
|
||||
ChevronLeft,
|
||||
Workflow,
|
||||
Play,
|
||||
Clock,
|
||||
import {
|
||||
AlertCircle,
|
||||
Plus,
|
||||
ChevronLeft,
|
||||
Workflow as WorkflowIcon,
|
||||
Play,
|
||||
Clock,
|
||||
Cpu,
|
||||
MoreVertical,
|
||||
ArrowRight,
|
||||
FileCode,
|
||||
@ -19,31 +22,93 @@ import {
|
||||
Sparkles,
|
||||
Trash2,
|
||||
Copy,
|
||||
Settings2
|
||||
Settings2,
|
||||
} from "lucide-react";
|
||||
import { MOCK_PROJECTS } from "../constants";
|
||||
import { cn } from "../lib/utils";
|
||||
import { WorkflowType } from "../types";
|
||||
import { formatCompactDateTime } from "../lib/datetime";
|
||||
import { buildTemplatePayload } from "../lib/templates.js";
|
||||
import { countWorkflowOperators } from "../lib/workflow";
|
||||
import { Project, Workflow as WorkflowRecord, WorkflowType } from "../types";
|
||||
import AIGenerateModal from "../components/AIGenerateModal";
|
||||
import { GeneratedWorkflow } from "../services/aiService";
|
||||
|
||||
const MOCK_WORKFLOWS = [
|
||||
{ id: "wf-1", name: "视觉处理流程", description: "主要负责图像缩放与目标检测", status: "completed", type: "batch" as WorkflowType, lastRun: "2024-03-22 10:00" },
|
||||
{ id: "wf-2", name: "实时数据监听", description: "定时从 OSS 目录获取新增内容运行", status: "running", type: "streaming" as WorkflowType, lastRun: "正在轮询..." },
|
||||
{ id: "wf-3", name: "模型评估流程", description: "计算 mAP 与推理延迟", status: "draft", type: "batch" as WorkflowType, lastRun: "未运行" },
|
||||
{ id: "wf-4", name: "数据增强预处理", description: "扩充训练集样本多样性", status: "failed", type: "batch" as WorkflowType, lastRun: "2024-03-21 15:30" },
|
||||
];
|
||||
import { projectsApi } from "../services/api/projects";
|
||||
import { templatesApi } from "../services/api/templates";
|
||||
import { workflowsApi } from "../services/api/workflows";
|
||||
import { useAIStatus } from "../hooks/useAIStatus";
|
||||
|
||||
export default function ProjectDetail() {
|
||||
const { id } = useParams();
|
||||
const navigate = useNavigate();
|
||||
const { timeZone } = useTimeZone();
|
||||
const { role } = useUser();
|
||||
const project = MOCK_PROJECTS.find(p => p.id === id);
|
||||
const aiStatus = useAIStatus();
|
||||
const [project, setProject] = useState<Project | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [loadError, setLoadError] = useState<string | null>(null);
|
||||
const [reloadToken, setReloadToken] = useState(0);
|
||||
const [isNewWorkflowModalOpen, setIsNewWorkflowModalOpen] = useState(false);
|
||||
const [isAIModalOpen, setIsAIModalOpen] = useState(false);
|
||||
const [isEditModalOpen, setIsEditModalOpen] = useState(false);
|
||||
const [editingWorkflow, setEditingWorkflow] = useState<any>(null);
|
||||
const [workflows, setWorkflows] = useState(MOCK_WORKFLOWS);
|
||||
const [isSaveTemplateModalOpen, setIsSaveTemplateModalOpen] = useState(false);
|
||||
const [editingWorkflow, setEditingWorkflow] = useState<WorkflowRecord | null>(
|
||||
null,
|
||||
);
|
||||
const [workflows, setWorkflows] = useState<WorkflowRecord[]>([]);
|
||||
const [newWfData, setNewWfData] = useState({
|
||||
name: "",
|
||||
type: "batch" as WorkflowType,
|
||||
});
|
||||
const [templateDraft, setTemplateDraft] = useState({
|
||||
workflowId: "",
|
||||
name: "",
|
||||
description: "",
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (!id) return;
|
||||
setLoading(true);
|
||||
setLoadError(null);
|
||||
Promise.all([projectsApi.get(id), projectsApi.listWorkflows(id)])
|
||||
.then(([loadedProject, loadedWorkflows]) => {
|
||||
setProject(loadedProject);
|
||||
setWorkflows(loadedWorkflows);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Failed to load project detail", error);
|
||||
setProject(null);
|
||||
setWorkflows([]);
|
||||
setLoadError(
|
||||
error instanceof Error ? error.message : "加载项目详情失败",
|
||||
);
|
||||
})
|
||||
.finally(() => {
|
||||
setLoading(false);
|
||||
});
|
||||
}, [id, reloadToken]);
|
||||
|
||||
if (loading) {
|
||||
return <div className="text-sm text-gray-500">正在加载项目详情...</div>;
|
||||
}
|
||||
|
||||
if (loadError) {
|
||||
return (
|
||||
<div className="space-y-4 rounded-2xl border border-red-500/20 bg-red-500/10 px-6 py-8">
|
||||
<div className="flex items-center gap-3 text-red-300">
|
||||
<AlertCircle className="w-5 h-5 shrink-0" />
|
||||
<div>
|
||||
<div className="font-semibold">加载项目详情失败</div>
|
||||
<div className="text-sm text-red-200/80">{loadError}</div>
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
onClick={() => setReloadToken((current) => current + 1)}
|
||||
className="inline-flex items-center gap-2 rounded-md bg-red-500/15 px-4 py-2 text-sm font-medium text-red-200 hover:bg-red-500/20"
|
||||
>
|
||||
重试加载
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!project) return <div>项目未找到</div>;
|
||||
|
||||
@ -53,103 +118,190 @@ export default function ProjectDetail() {
|
||||
return status !== "running" && status !== "completed";
|
||||
};
|
||||
|
||||
const handleAIGenerate = (workflow: GeneratedWorkflow, name: string, description: string) => {
|
||||
const wf = {
|
||||
id: `wf-${Date.now()}`,
|
||||
name: name,
|
||||
description: description,
|
||||
status: "draft",
|
||||
type: "batch" as WorkflowType,
|
||||
lastRun: "未运行",
|
||||
nodes: workflow.nodes,
|
||||
edges: workflow.edges
|
||||
};
|
||||
setWorkflows([wf, ...workflows]);
|
||||
setIsAIModalOpen(false);
|
||||
// Navigate to editor for the new workflow with generated nodes/edges
|
||||
navigate(`/workflow/${wf.id}?projectId=${id}`, {
|
||||
state: { nodes: workflow.nodes, edges: workflow.edges }
|
||||
});
|
||||
const handleAIGenerate = (
|
||||
workflow: GeneratedWorkflow,
|
||||
name: string,
|
||||
description: string,
|
||||
) => {
|
||||
if (!id) return;
|
||||
projectsApi
|
||||
.createWorkflow(id, {
|
||||
name,
|
||||
description,
|
||||
type: "batch",
|
||||
nodes: workflow.nodes,
|
||||
edges: workflow.edges,
|
||||
})
|
||||
.then((created) => {
|
||||
setWorkflows([created, ...workflows]);
|
||||
setIsAIModalOpen(false);
|
||||
navigate(`/workflow/${created.id}?projectId=${id}`);
|
||||
})
|
||||
.catch((error) => console.error("Failed to create AI workflow", error));
|
||||
};
|
||||
|
||||
const [newWfData, setNewWfData] = useState({ name: "", type: "batch" as WorkflowType });
|
||||
|
||||
const handleCreateWorkflow = () => {
|
||||
if (newWfData.name) {
|
||||
const wf = {
|
||||
id: `wf-${Date.now()}`,
|
||||
if (!id || !newWfData.name) return;
|
||||
projectsApi
|
||||
.createWorkflow(id, {
|
||||
name: newWfData.name,
|
||||
description: "新创建的工作流",
|
||||
status: "draft",
|
||||
type: newWfData.type,
|
||||
lastRun: "未运行",
|
||||
nodes: [],
|
||||
edges: []
|
||||
};
|
||||
setWorkflows([wf, ...workflows]);
|
||||
setIsNewWorkflowModalOpen(false);
|
||||
setNewWfData({ name: "", type: "batch" });
|
||||
navigate(`/workflow/${wf.id}?projectId=${id}`);
|
||||
}
|
||||
edges: [],
|
||||
})
|
||||
.then((created) => {
|
||||
setWorkflows([created, ...workflows]);
|
||||
setIsNewWorkflowModalOpen(false);
|
||||
setNewWfData({ name: "", type: "batch" });
|
||||
navigate(`/workflow/${created.id}?projectId=${id}`);
|
||||
})
|
||||
.catch((error) => console.error("Failed to create workflow", error));
|
||||
};
|
||||
|
||||
const handleDeleteWorkflow = (wfId: string) => {
|
||||
setWorkflows(workflows.filter(w => w.id !== wfId));
|
||||
workflowsApi
|
||||
.remove(wfId)
|
||||
.then(() =>
|
||||
setWorkflows(workflows.filter((workflow) => workflow.id !== wfId)),
|
||||
)
|
||||
.catch((error) => console.error("Failed to delete workflow", error));
|
||||
};
|
||||
|
||||
const handleCopyWorkflow = (wf: any) => {
|
||||
const newWf = {
|
||||
...wf,
|
||||
id: `wf-${Date.now()}`,
|
||||
name: `${wf.name} (副本)`,
|
||||
status: "draft",
|
||||
lastRun: "未运行",
|
||||
};
|
||||
setWorkflows([newWf, ...workflows]);
|
||||
const handleCopyWorkflow = (wf: WorkflowRecord) => {
|
||||
if (!id) return;
|
||||
projectsApi
|
||||
.createWorkflow(id, {
|
||||
name: `${wf.name} (副本)`,
|
||||
description: wf.description,
|
||||
type: wf.type,
|
||||
nodes: wf.nodes,
|
||||
edges: wf.edges,
|
||||
schedule: wf.schedule,
|
||||
})
|
||||
.then((created) => setWorkflows([created, ...workflows]))
|
||||
.catch((error) => console.error("Failed to copy workflow", error));
|
||||
};
|
||||
|
||||
const handleEditInfo = (wf: any) => {
|
||||
const handleEditInfo = (wf: WorkflowRecord) => {
|
||||
setEditingWorkflow({ ...wf });
|
||||
setIsEditModalOpen(true);
|
||||
};
|
||||
|
||||
const handleUpdateWorkflow = () => {
|
||||
if (editingWorkflow) {
|
||||
setWorkflows(workflows.map(w => w.id === editingWorkflow.id ? editingWorkflow : w));
|
||||
setIsEditModalOpen(false);
|
||||
setEditingWorkflow(null);
|
||||
workflowsApi
|
||||
.update(editingWorkflow.id, editingWorkflow)
|
||||
.then((updated) => {
|
||||
setWorkflows(
|
||||
workflows.map((workflow) =>
|
||||
workflow.id === updated.id ? updated : workflow,
|
||||
),
|
||||
);
|
||||
setIsEditModalOpen(false);
|
||||
setEditingWorkflow(null);
|
||||
})
|
||||
.catch((error) => console.error("Failed to update workflow", error));
|
||||
}
|
||||
};
|
||||
|
||||
const openSaveTemplateModal = () => {
|
||||
if (workflows.length === 0) {
|
||||
return;
|
||||
}
|
||||
const defaultWorkflow = workflows[0];
|
||||
setTemplateDraft({
|
||||
workflowId: defaultWorkflow.id,
|
||||
name: `${defaultWorkflow.name} 模板`,
|
||||
description:
|
||||
defaultWorkflow.description || `${project.name} 导出的工作流模板`,
|
||||
});
|
||||
setIsSaveTemplateModalOpen(true);
|
||||
};
|
||||
|
||||
const handleTemplateWorkflowChange = (workflowId: string) => {
|
||||
const matched = workflows.find((workflow) => workflow.id === workflowId);
|
||||
setTemplateDraft((previous) => ({
|
||||
workflowId,
|
||||
name: matched ? `${matched.name} 模板` : previous.name,
|
||||
description: matched?.description || previous.description,
|
||||
}));
|
||||
};
|
||||
|
||||
const handleSaveTemplate = () => {
|
||||
const sourceWorkflow = workflows.find(
|
||||
(workflow) => workflow.id === templateDraft.workflowId,
|
||||
);
|
||||
if (!sourceWorkflow || !templateDraft.name.trim()) {
|
||||
return;
|
||||
}
|
||||
templatesApi
|
||||
.create(
|
||||
buildTemplatePayload(sourceWorkflow, {
|
||||
name: templateDraft.name.trim(),
|
||||
description: templateDraft.description.trim(),
|
||||
}),
|
||||
)
|
||||
.then((created) => {
|
||||
setIsSaveTemplateModalOpen(false);
|
||||
navigate(`/workflow/${created.id}?type=template`);
|
||||
})
|
||||
.catch((error) =>
|
||||
console.error("Failed to save workflow as template", error),
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-8">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-4">
|
||||
<button
|
||||
<button
|
||||
onClick={() => navigate("/projects")}
|
||||
className="p-2 hover:bg-white/5 rounded-md text-gray-400 hover:text-white transition-colors"
|
||||
>
|
||||
<ChevronLeft className="w-5 h-5" />
|
||||
</button>
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold tracking-tight text-white">{project.name}</h1>
|
||||
<h1 className="text-3xl font-bold tracking-tight text-white">
|
||||
{project.name}
|
||||
</h1>
|
||||
<p className="text-gray-500 mt-1">{project.description}</p>
|
||||
</div>
|
||||
</div>
|
||||
{canManage && (
|
||||
<div className="flex gap-3">
|
||||
<button
|
||||
<button
|
||||
onClick={() => setIsAIModalOpen(true)}
|
||||
className="flex items-center gap-2 bg-white/5 hover:bg-white/10 text-blue-400 px-4 py-2 rounded-md text-sm font-medium transition-colors border border-blue-500/20"
|
||||
disabled={!aiStatus.enabled}
|
||||
title={
|
||||
aiStatus.enabled
|
||||
? `使用 ${aiStatus.model || "glm-5"} 生成工作流`
|
||||
: aiStatus.reason || "当前服务端未配置 AI 服务"
|
||||
}
|
||||
className={cn(
|
||||
"flex items-center gap-2 px-4 py-2 rounded-md text-sm font-medium transition-colors border border-blue-500/20",
|
||||
aiStatus.enabled
|
||||
? "bg-white/5 hover:bg-white/10 text-blue-400"
|
||||
: "bg-white/5 text-gray-500 cursor-not-allowed opacity-60",
|
||||
)}
|
||||
>
|
||||
<Sparkles className="w-4 h-4" />
|
||||
AI 智能生成
|
||||
</button>
|
||||
<button className="flex items-center gap-2 bg-white/5 hover:bg-white/10 text-white px-4 py-2 rounded-md text-sm font-medium transition-colors border border-[#2a2b2f]">
|
||||
<button
|
||||
onClick={openSaveTemplateModal}
|
||||
disabled={workflows.length === 0}
|
||||
className={cn(
|
||||
"flex items-center gap-2 px-4 py-2 rounded-md text-sm font-medium transition-colors border border-[#2a2b2f]",
|
||||
workflows.length > 0
|
||||
? "bg-white/5 hover:bg-white/10 text-white"
|
||||
: "bg-white/5 text-gray-500 cursor-not-allowed opacity-60",
|
||||
)}
|
||||
>
|
||||
<FileCode className="w-4 h-4" />
|
||||
保存为模板
|
||||
</button>
|
||||
<button
|
||||
<button
|
||||
onClick={() => setIsNewWorkflowModalOpen(true)}
|
||||
className="flex items-center gap-2 bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-md text-sm font-medium transition-colors"
|
||||
>
|
||||
@ -161,42 +313,83 @@ export default function ProjectDetail() {
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 gap-4">
|
||||
{workflows.map((wf) => (
|
||||
<div
|
||||
key={wf.id}
|
||||
onClick={() => navigate(`/workflow/${wf.id}?readOnly=${!isEditable(wf.status)}`)}
|
||||
className="group bg-[#151619] border border-[#2a2b2f] rounded-xl p-5 hover:border-blue-500/50 transition-all cursor-pointer flex items-center gap-6"
|
||||
>
|
||||
<div className={cn(
|
||||
"w-12 h-12 rounded-lg flex items-center justify-center shrink-0",
|
||||
wf.status === "completed" ? "bg-green-500/10 text-green-500" :
|
||||
wf.status === "running" ? "bg-blue-500/10 text-blue-500" :
|
||||
wf.status === "failed" ? "bg-red-500/10 text-red-500" :
|
||||
"bg-gray-500/10 text-gray-500"
|
||||
)}>
|
||||
{wf.status === "completed" ? <CheckCircle2 className="w-6 h-6" /> :
|
||||
wf.status === "running" ? (wf.type === 'streaming' ? <RefreshCw className="w-6 h-6 animate-spin" /> : <Play className="w-6 h-6 animate-pulse" />) :
|
||||
wf.status === "failed" ? <XCircle className="w-6 h-6" /> :
|
||||
<Workflow className="w-6 h-6" />}
|
||||
{workflows.map((wf) => {
|
||||
const operatorCount = countWorkflowOperators(wf.nodes);
|
||||
|
||||
return (
|
||||
<div
|
||||
key={wf.id}
|
||||
onClick={() =>
|
||||
navigate(
|
||||
(() => {
|
||||
const params = new URLSearchParams({
|
||||
projectId: project.id,
|
||||
readOnly: String(!isEditable(wf.status)),
|
||||
});
|
||||
if (wf.status === "running" && wf.lastRunId) {
|
||||
params.set("runId", wf.lastRunId);
|
||||
}
|
||||
return `/workflow/${wf.id}?${params.toString()}`;
|
||||
})(),
|
||||
)
|
||||
}
|
||||
className="group bg-[#151619] border border-[#2a2b2f] rounded-xl p-5 hover:border-blue-500/50 transition-all cursor-pointer flex items-center gap-6"
|
||||
>
|
||||
<div
|
||||
className={cn(
|
||||
"w-12 h-12 rounded-lg flex items-center justify-center shrink-0",
|
||||
wf.status === "completed"
|
||||
? "bg-green-500/10 text-green-500"
|
||||
: wf.status === "running"
|
||||
? "bg-blue-500/10 text-blue-500"
|
||||
: wf.status === "failed"
|
||||
? "bg-red-500/10 text-red-500"
|
||||
: "bg-gray-500/10 text-gray-500",
|
||||
)}
|
||||
>
|
||||
{wf.status === "completed" ? (
|
||||
<CheckCircle2 className="w-6 h-6" />
|
||||
) : wf.status === "running" ? (
|
||||
wf.type === "streaming" ? (
|
||||
<RefreshCw className="w-6 h-6 animate-spin" />
|
||||
) : (
|
||||
<Play className="w-6 h-6 animate-pulse" />
|
||||
)
|
||||
) : wf.status === "failed" ? (
|
||||
<XCircle className="w-6 h-6" />
|
||||
) : (
|
||||
<WorkflowIcon className="w-6 h-6" />
|
||||
)}
|
||||
</div>
|
||||
|
||||
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-3 mb-1">
|
||||
<h3 className="text-lg font-bold text-white truncate group-hover:text-blue-400 transition-colors">{wf.name}</h3>
|
||||
<h3 className="text-lg font-bold text-white truncate group-hover:text-blue-400 transition-colors">
|
||||
{wf.name}
|
||||
</h3>
|
||||
<div className="flex items-center gap-2">
|
||||
<span className={cn(
|
||||
"px-2 py-0.5 rounded-full text-[10px] font-medium uppercase tracking-wider border",
|
||||
wf.status === "running" ? "bg-blue-500/10 text-blue-500 border-blue-500/20" :
|
||||
wf.status === "completed" ? "bg-green-500/10 text-green-500 border-green-500/20" :
|
||||
wf.status === "failed" ? "bg-red-500/10 text-red-500 border-red-500/20" :
|
||||
"bg-gray-500/10 text-gray-500 border-gray-500/20"
|
||||
)}>
|
||||
<span
|
||||
className={cn(
|
||||
"px-2 py-0.5 rounded-full text-[10px] font-medium uppercase tracking-wider border",
|
||||
wf.status === "running"
|
||||
? "bg-blue-500/10 text-blue-500 border-blue-500/20"
|
||||
: wf.status === "completed"
|
||||
? "bg-green-500/10 text-green-500 border-green-500/20"
|
||||
: wf.status === "failed"
|
||||
? "bg-red-500/10 text-red-500 border-red-500/20"
|
||||
: "bg-gray-500/10 text-gray-500 border-gray-500/20",
|
||||
)}
|
||||
>
|
||||
{wf.status}
|
||||
</span>
|
||||
<span className={cn(
|
||||
"px-2 py-0.5 rounded-full text-[10px] font-medium uppercase tracking-wider border",
|
||||
wf.type === "streaming" ? "bg-purple-500/10 text-purple-500 border-purple-500/20" : "bg-gray-500/10 text-gray-400 border-gray-500/20"
|
||||
)}>
|
||||
<span
|
||||
className={cn(
|
||||
"px-2 py-0.5 rounded-full text-[10px] font-medium uppercase tracking-wider border",
|
||||
wf.type === "streaming"
|
||||
? "bg-purple-500/10 text-purple-500 border-purple-500/20"
|
||||
: "bg-gray-500/10 text-gray-400 border-gray-500/20",
|
||||
)}
|
||||
>
|
||||
{wf.type === "streaming" ? "持续执行" : "单次执行"}
|
||||
</span>
|
||||
</div>
|
||||
@ -206,20 +399,35 @@ export default function ProjectDetail() {
|
||||
|
||||
<div className="flex items-center gap-8 shrink-0">
|
||||
<div className="text-right">
|
||||
<div className="text-xs text-gray-600 uppercase font-mono tracking-widest mb-1">最后运行</div>
|
||||
<div className="text-sm text-gray-400 flex items-center gap-2 justify-end">
|
||||
<Clock className="w-3.5 h-3.5" />
|
||||
{wf.lastRun}
|
||||
<div className="text-xs text-gray-600 uppercase font-mono tracking-widest mb-1">
|
||||
算子数
|
||||
</div>
|
||||
<div className="inline-flex items-center gap-1.5 rounded-full border border-blue-500/20 bg-blue-500/10 px-2.5 py-1 text-sm font-medium text-blue-300">
|
||||
<Cpu className="w-3.5 h-3.5" />
|
||||
{operatorCount}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="text-right">
|
||||
<div className="text-xs text-gray-600 uppercase font-mono tracking-widest mb-1">
|
||||
最后运行
|
||||
</div>
|
||||
<div className="text-sm text-gray-400 flex items-center gap-2 justify-end">
|
||||
<Clock className="w-3.5 h-3.5" />
|
||||
{wf.lastRunAt
|
||||
? formatCompactDateTime(wf.lastRunAt, timeZone)
|
||||
: wf.lastRun || "未运行"}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<div className={cn(
|
||||
"flex items-center gap-2 px-4 py-2 rounded-md text-sm font-medium transition-all",
|
||||
isEditable(wf.status) && canManage
|
||||
? "bg-blue-600/10 text-blue-500"
|
||||
: "bg-gray-600/10 text-gray-500"
|
||||
)}>
|
||||
<div
|
||||
className={cn(
|
||||
"flex items-center gap-2 px-4 py-2 rounded-md text-sm font-medium transition-all",
|
||||
isEditable(wf.status) && canManage
|
||||
? "bg-blue-600/10 text-blue-500"
|
||||
: "bg-gray-600/10 text-gray-500",
|
||||
)}
|
||||
>
|
||||
{isEditable(wf.status) && canManage ? (
|
||||
<>
|
||||
<Edit3 className="w-4 h-4" />
|
||||
@ -234,23 +442,32 @@ export default function ProjectDetail() {
|
||||
</div>
|
||||
{canManage && (
|
||||
<>
|
||||
<button
|
||||
className="p-2 hover:bg-blue-500/10 rounded-md text-gray-500 hover:text-blue-400 transition-colors"
|
||||
onClick={(e) => { e.stopPropagation(); handleEditInfo(wf); }}
|
||||
<button
|
||||
className="p-2 hover:bg-blue-500/10 rounded-md text-gray-500 hover:text-blue-400 transition-colors"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
handleEditInfo(wf);
|
||||
}}
|
||||
title="编辑基本信息"
|
||||
>
|
||||
<Settings2 className="w-4 h-4" />
|
||||
</button>
|
||||
<button
|
||||
className="p-2 hover:bg-blue-500/10 rounded-md text-gray-500 hover:text-blue-400 transition-colors"
|
||||
onClick={(e) => { e.stopPropagation(); handleCopyWorkflow(wf); }}
|
||||
<button
|
||||
className="p-2 hover:bg-blue-500/10 rounded-md text-gray-500 hover:text-blue-400 transition-colors"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
handleCopyWorkflow(wf);
|
||||
}}
|
||||
title="复制工作流"
|
||||
>
|
||||
<Copy className="w-4 h-4" />
|
||||
</button>
|
||||
<button
|
||||
className="p-2 hover:bg-red-500/10 rounded-md text-gray-500 hover:text-red-500 transition-colors"
|
||||
onClick={(e) => { e.stopPropagation(); handleDeleteWorkflow(wf.id); }}
|
||||
<button
|
||||
className="p-2 hover:bg-red-500/10 rounded-md text-gray-500 hover:text-red-500 transition-colors"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
handleDeleteWorkflow(wf.id);
|
||||
}}
|
||||
title="删除工作流"
|
||||
>
|
||||
<Trash2 className="w-4 h-4" />
|
||||
@ -262,8 +479,14 @@ export default function ProjectDetail() {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
{workflows.length === 0 && (
|
||||
<div className="bg-[#151619] border border-[#2a2b2f] rounded-xl p-12 text-center text-gray-500">
|
||||
当前项目还没有工作流,请先创建一个真实工作流。
|
||||
</div>
|
||||
))}
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* New Workflow Modal */}
|
||||
@ -272,61 +495,86 @@ export default function ProjectDetail() {
|
||||
<div className="bg-[#151619] border border-[#2a2b2f] rounded-2xl w-full max-w-md shadow-2xl">
|
||||
<div className="px-6 py-4 border-b border-[#2a2b2f] flex items-center justify-between">
|
||||
<h2 className="text-xl font-bold text-white">创建新工作流</h2>
|
||||
<button onClick={() => setIsNewWorkflowModalOpen(false)} className="text-gray-500 hover:text-white">
|
||||
<button
|
||||
onClick={() => setIsNewWorkflowModalOpen(false)}
|
||||
className="text-gray-500 hover:text-white"
|
||||
>
|
||||
<X className="w-6 h-6" />
|
||||
</button>
|
||||
</div>
|
||||
<div className="p-6 space-y-6">
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">工作流名称</label>
|
||||
<input
|
||||
type="text"
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
工作流名称
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="例如: 实时障碍物检测"
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500"
|
||||
value={newWfData.name}
|
||||
onChange={e => setNewWfData({...newWfData, name: e.target.value})}
|
||||
onChange={(e) =>
|
||||
setNewWfData({ ...newWfData, name: e.target.value })
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
|
||||
|
||||
<div className="space-y-3">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">执行类型</label>
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
执行类型
|
||||
</label>
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<button
|
||||
onClick={() => setNewWfData({...newWfData, type: "batch"})}
|
||||
<button
|
||||
onClick={() =>
|
||||
setNewWfData({ ...newWfData, type: "batch" })
|
||||
}
|
||||
className={cn(
|
||||
"p-4 bg-[#0a0a0a] border rounded-xl text-left group transition-all",
|
||||
newWfData.type === "batch" ? "border-blue-600 ring-1 ring-blue-600" : "border-[#2a2b2f] hover:border-blue-500/50"
|
||||
newWfData.type === "batch"
|
||||
? "border-blue-600 ring-1 ring-blue-600"
|
||||
: "border-[#2a2b2f] hover:border-blue-500/50",
|
||||
)}
|
||||
>
|
||||
<div className="text-white font-bold mb-1">单次执行 (Batch)</div>
|
||||
<div className="text-[10px] text-gray-500">流程运行一次后自动结束,适用于离线数据处理。</div>
|
||||
<div className="text-white font-bold mb-1">
|
||||
单次执行 (Batch)
|
||||
</div>
|
||||
<div className="text-[10px] text-gray-500">
|
||||
流程运行一次后自动结束,适用于离线数据处理。
|
||||
</div>
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setNewWfData({...newWfData, type: "streaming"})}
|
||||
<button
|
||||
onClick={() =>
|
||||
setNewWfData({ ...newWfData, type: "streaming" })
|
||||
}
|
||||
className={cn(
|
||||
"p-4 bg-[#0a0a0a] border rounded-xl text-left group transition-all",
|
||||
newWfData.type === "streaming" ? "border-purple-600 ring-1 ring-purple-600" : "border-[#2a2b2f] hover:border-purple-500/50"
|
||||
newWfData.type === "streaming"
|
||||
? "border-purple-600 ring-1 ring-purple-600"
|
||||
: "border-[#2a2b2f] hover:border-purple-500/50",
|
||||
)}
|
||||
>
|
||||
<div className="text-white font-bold mb-1">持续执行 (Streaming)</div>
|
||||
<div className="text-[10px] text-gray-500">基于定时器轮询数据源,适用于实时感知与监控。</div>
|
||||
<div className="text-white font-bold mb-1">
|
||||
持续执行 (Streaming)
|
||||
</div>
|
||||
<div className="text-[10px] text-gray-500">
|
||||
基于定时器轮询数据源,适用于实时感知与监控。
|
||||
</div>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="p-6 border-t border-[#2a2b2f] flex justify-end gap-3">
|
||||
<button
|
||||
<button
|
||||
onClick={() => setIsNewWorkflowModalOpen(false)}
|
||||
className="px-4 py-2 text-sm font-medium text-gray-400 hover:text-white transition-colors"
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
<button
|
||||
<button
|
||||
onClick={handleCreateWorkflow}
|
||||
disabled={!newWfData.name}
|
||||
className={cn(
|
||||
"px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-md text-sm font-medium transition-colors",
|
||||
!newWfData.name && "opacity-50 cursor-not-allowed"
|
||||
!newWfData.name && "opacity-50 cursor-not-allowed",
|
||||
)}
|
||||
>
|
||||
确认创建
|
||||
@ -336,54 +584,163 @@ export default function ProjectDetail() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
<AIGenerateModal
|
||||
isOpen={isAIModalOpen}
|
||||
onClose={() => setIsAIModalOpen(false)}
|
||||
onGenerate={handleAIGenerate}
|
||||
<AIGenerateModal
|
||||
isOpen={isAIModalOpen}
|
||||
onClose={() => setIsAIModalOpen(false)}
|
||||
onGenerate={handleAIGenerate}
|
||||
/>
|
||||
|
||||
{isSaveTemplateModalOpen && (
|
||||
<div className="fixed inset-0 z-[100] flex items-center justify-center p-4 bg-black/60 backdrop-blur-sm">
|
||||
<div className="bg-[#151619] border border-[#2a2b2f] rounded-2xl w-full max-w-md shadow-2xl overflow-hidden">
|
||||
<div className="px-6 py-4 border-b border-[#2a2b2f] flex items-center justify-between bg-[#1a1b1e]">
|
||||
<h2 className="text-xl font-bold text-white flex items-center gap-2">
|
||||
<FileCode className="w-5 h-5 text-blue-500" />
|
||||
保存为模板
|
||||
</h2>
|
||||
<button
|
||||
onClick={() => setIsSaveTemplateModalOpen(false)}
|
||||
className="text-gray-500 hover:text-white transition-colors"
|
||||
>
|
||||
<X className="w-6 h-6" />
|
||||
</button>
|
||||
</div>
|
||||
<div className="p-6 space-y-4">
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
来源工作流
|
||||
</label>
|
||||
<select
|
||||
value={templateDraft.workflowId}
|
||||
onChange={(event) =>
|
||||
handleTemplateWorkflowChange(event.target.value)
|
||||
}
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500 text-white"
|
||||
>
|
||||
{workflows.map((workflow) => (
|
||||
<option key={workflow.id} value={workflow.id}>
|
||||
{workflow.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
模板名称
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={templateDraft.name}
|
||||
onChange={(event) =>
|
||||
setTemplateDraft((previous) => ({
|
||||
...previous,
|
||||
name: event.target.value,
|
||||
}))
|
||||
}
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500 text-white"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
模板描述
|
||||
</label>
|
||||
<textarea
|
||||
rows={4}
|
||||
value={templateDraft.description}
|
||||
onChange={(event) =>
|
||||
setTemplateDraft((previous) => ({
|
||||
...previous,
|
||||
description: event.target.value,
|
||||
}))
|
||||
}
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500 text-white resize-none"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="p-6 border-t border-[#2a2b2f] flex justify-end gap-3 bg-[#0a0a0a]/50">
|
||||
<button
|
||||
onClick={() => setIsSaveTemplateModalOpen(false)}
|
||||
className="px-4 py-2 text-sm font-medium text-gray-400 hover:text-white transition-colors"
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
<button
|
||||
onClick={handleSaveTemplate}
|
||||
disabled={
|
||||
!templateDraft.workflowId || !templateDraft.name.trim()
|
||||
}
|
||||
className={cn(
|
||||
"px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-md text-sm font-medium transition-colors",
|
||||
(!templateDraft.workflowId || !templateDraft.name.trim()) &&
|
||||
"opacity-50 cursor-not-allowed",
|
||||
)}
|
||||
>
|
||||
保存模板
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Edit Workflow Info Modal */}
|
||||
{isEditModalOpen && editingWorkflow && (
|
||||
<div className="fixed inset-0 z-[100] flex items-center justify-center p-4 bg-black/60 backdrop-blur-sm">
|
||||
<div className="bg-[#151619] border border-[#2a2b2f] rounded-2xl w-full max-w-md shadow-2xl">
|
||||
<div className="px-6 py-4 border-b border-[#2a2b2f] flex items-center justify-between">
|
||||
<h2 className="text-xl font-bold text-white">编辑工作流信息</h2>
|
||||
<button onClick={() => setIsEditModalOpen(false)} className="text-gray-500 hover:text-white">
|
||||
<button
|
||||
onClick={() => setIsEditModalOpen(false)}
|
||||
className="text-gray-500 hover:text-white"
|
||||
>
|
||||
<X className="w-6 h-6" />
|
||||
</button>
|
||||
</div>
|
||||
<div className="p-6 space-y-6">
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">工作流名称</label>
|
||||
<input
|
||||
type="text"
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
工作流名称
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500 text-white"
|
||||
value={editingWorkflow.name}
|
||||
onChange={e => setEditingWorkflow({...editingWorkflow, name: e.target.value})}
|
||||
onChange={(e) =>
|
||||
setEditingWorkflow({
|
||||
...editingWorkflow,
|
||||
name: e.target.value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">描述</label>
|
||||
<textarea
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
描述
|
||||
</label>
|
||||
<textarea
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm h-24 focus:outline-none focus:border-blue-500 text-white resize-none"
|
||||
value={editingWorkflow.description}
|
||||
onChange={e => setEditingWorkflow({...editingWorkflow, description: e.target.value})}
|
||||
onChange={(e) =>
|
||||
setEditingWorkflow({
|
||||
...editingWorkflow,
|
||||
description: e.target.value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="p-6 border-t border-[#2a2b2f] flex justify-end gap-3">
|
||||
<button
|
||||
<button
|
||||
onClick={() => setIsEditModalOpen(false)}
|
||||
className="px-4 py-2 text-sm font-medium text-gray-400 hover:text-white transition-colors"
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
<button
|
||||
<button
|
||||
onClick={handleUpdateWorkflow}
|
||||
disabled={!editingWorkflow.name}
|
||||
className={cn(
|
||||
"px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-md text-sm font-medium transition-colors",
|
||||
!editingWorkflow.name && "opacity-50 cursor-not-allowed"
|
||||
!editingWorkflow.name && "opacity-50 cursor-not-allowed",
|
||||
)}
|
||||
>
|
||||
保存修改
|
||||
|
||||
@ -1,22 +1,54 @@
|
||||
import React, { useState } from "react";
|
||||
import { Plus, Search, MoreVertical, Calendar, ArrowRight, FolderKanban, Edit3, Copy, Trash2, X, Check } from "lucide-react";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import {
|
||||
Plus,
|
||||
Search,
|
||||
MoreVertical,
|
||||
Calendar,
|
||||
ArrowRight,
|
||||
Workflow as WorkflowIcon,
|
||||
FolderKanban,
|
||||
Edit3,
|
||||
Copy,
|
||||
Trash2,
|
||||
X,
|
||||
Check,
|
||||
} from "lucide-react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useTimeZone } from "../context/TimeZoneContext";
|
||||
import { formatCompactDateTime } from "../lib/datetime";
|
||||
import { useUser } from "../context/UserContext";
|
||||
import { MOCK_PROJECTS } from "../constants";
|
||||
import { cn } from "../lib/utils";
|
||||
import { Project } from "../types";
|
||||
import { projectsApi } from "../services/api/projects";
|
||||
|
||||
export default function Projects() {
|
||||
const navigate = useNavigate();
|
||||
const { timeZone } = useTimeZone();
|
||||
const { role } = useUser();
|
||||
const [projects, setProjects] = useState<Project[]>(MOCK_PROJECTS);
|
||||
const [projects, setProjects] = useState<Project[]>([]);
|
||||
const [searchQuery, setSearchQuery] = useState("");
|
||||
const [statusFilter, setStatusFilter] = useState<
|
||||
"all" | "active" | "archived"
|
||||
>("all");
|
||||
const [isModalOpen, setIsModalOpen] = useState(false);
|
||||
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
|
||||
const [modalMode, setModalMode] = useState<"create" | "edit">("create");
|
||||
const [currentProject, setCurrentProject] = useState<Partial<Project>>({ name: "", description: "" });
|
||||
const [currentProject, setCurrentProject] = useState<Partial<Project>>({
|
||||
name: "",
|
||||
description: "",
|
||||
});
|
||||
const [projectToDelete, setProjectToDelete] = useState<string | null>(null);
|
||||
const [openMenuId, setOpenMenuId] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
projectsApi
|
||||
.list()
|
||||
.then(setProjects)
|
||||
.catch((error) => {
|
||||
console.error("Failed to load projects", error);
|
||||
});
|
||||
}, []);
|
||||
|
||||
const handleCreate = () => {
|
||||
setModalMode("create");
|
||||
setCurrentProject({ name: "", description: "" });
|
||||
@ -31,14 +63,16 @@ export default function Projects() {
|
||||
};
|
||||
|
||||
const handleCopy = (project: Project) => {
|
||||
const newProject: Project = {
|
||||
...project,
|
||||
id: `p-${Date.now()}`,
|
||||
name: `${project.name} (副本)`,
|
||||
createdAt: new Date().toISOString().split('T')[0]
|
||||
};
|
||||
setProjects([newProject, ...projects]);
|
||||
setOpenMenuId(null);
|
||||
projectsApi
|
||||
.create({
|
||||
name: `${project.name} (副本)`,
|
||||
description: project.description,
|
||||
})
|
||||
.then((created) => {
|
||||
setProjects([created, ...projects]);
|
||||
setOpenMenuId(null);
|
||||
})
|
||||
.catch((error) => console.error("Failed to copy project", error));
|
||||
};
|
||||
|
||||
const handleDelete = (id: string) => {
|
||||
@ -49,27 +83,53 @@ export default function Projects() {
|
||||
|
||||
const confirmDelete = () => {
|
||||
if (projectToDelete) {
|
||||
setProjects(projects.filter(p => p.id !== projectToDelete));
|
||||
setProjectToDelete(null);
|
||||
setIsDeleteModalOpen(false);
|
||||
projectsApi
|
||||
.remove(projectToDelete)
|
||||
.then(() => {
|
||||
setProjects(projects.filter((p) => p.id !== projectToDelete));
|
||||
setProjectToDelete(null);
|
||||
setIsDeleteModalOpen(false);
|
||||
})
|
||||
.catch((error) => console.error("Failed to delete project", error));
|
||||
}
|
||||
};
|
||||
|
||||
const canManage = role === "admin" || role === "operator";
|
||||
const normalizedSearch = searchQuery.trim().toLowerCase();
|
||||
const filteredProjects = projects.filter((project) => {
|
||||
const matchesSearch =
|
||||
normalizedSearch.length === 0 ||
|
||||
[project.name, project.description]
|
||||
.filter(Boolean)
|
||||
.some((value) => value.toLowerCase().includes(normalizedSearch));
|
||||
const projectStatus = project.status || "active";
|
||||
const matchesStatus =
|
||||
statusFilter === "all" || projectStatus === statusFilter;
|
||||
return matchesSearch && matchesStatus;
|
||||
});
|
||||
|
||||
const handleSave = () => {
|
||||
if (!currentProject.name) return;
|
||||
|
||||
if (modalMode === "create") {
|
||||
const newProject: Project = {
|
||||
id: `p-${Date.now()}`,
|
||||
name: currentProject.name,
|
||||
description: currentProject.description || "",
|
||||
createdAt: new Date().toISOString().split('T')[0]
|
||||
};
|
||||
setProjects([newProject, ...projects]);
|
||||
projectsApi
|
||||
.create({
|
||||
name: currentProject.name,
|
||||
description: currentProject.description || "",
|
||||
})
|
||||
.then((created) => setProjects([created, ...projects]))
|
||||
.catch((error) => console.error("Failed to create project", error));
|
||||
} else {
|
||||
setProjects(projects.map(p => p.id === currentProject.id ? (currentProject as Project) : p));
|
||||
projectsApi
|
||||
.update(currentProject.id as string, currentProject)
|
||||
.then((updated) =>
|
||||
setProjects(
|
||||
projects.map((project) =>
|
||||
project.id === updated.id ? updated : project,
|
||||
),
|
||||
),
|
||||
)
|
||||
.catch((error) => console.error("Failed to update project", error));
|
||||
}
|
||||
setIsModalOpen(false);
|
||||
};
|
||||
@ -78,11 +138,15 @@ export default function Projects() {
|
||||
<div className="space-y-8 relative">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold tracking-tight text-white">项目管理</h1>
|
||||
<p className="text-gray-500 mt-1">在 EmboFlow 中管理您的具身智能数据处理项目</p>
|
||||
<h1 className="text-3xl font-bold tracking-tight text-white">
|
||||
项目管理
|
||||
</h1>
|
||||
<p className="text-gray-500 mt-1">
|
||||
在 DroboticFlow 中管理您的具身智能数据处理项目
|
||||
</p>
|
||||
</div>
|
||||
{canManage && (
|
||||
<button
|
||||
<button
|
||||
onClick={handleCreate}
|
||||
className="flex items-center gap-2 bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-md text-sm font-medium transition-colors"
|
||||
>
|
||||
@ -95,23 +159,31 @@ export default function Projects() {
|
||||
<div className="flex items-center gap-4 bg-[#151619] p-4 rounded-lg border border-[#2a2b2f]">
|
||||
<div className="relative flex-1">
|
||||
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-500" />
|
||||
<input
|
||||
type="text"
|
||||
placeholder="搜索项目..."
|
||||
<input
|
||||
type="text"
|
||||
placeholder="搜索项目..."
|
||||
value={searchQuery}
|
||||
onChange={(event) => setSearchQuery(event.target.value)}
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md pl-10 pr-4 py-2 text-sm focus:outline-none focus:border-blue-500 transition-colors"
|
||||
/>
|
||||
</div>
|
||||
<select className="bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500">
|
||||
<option>所有状态</option>
|
||||
<option>活跃中</option>
|
||||
<option>已归档</option>
|
||||
<select
|
||||
value={statusFilter}
|
||||
onChange={(event) =>
|
||||
setStatusFilter(event.target.value as typeof statusFilter)
|
||||
}
|
||||
className="bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500"
|
||||
>
|
||||
<option value="all">所有状态</option>
|
||||
<option value="active">活跃中</option>
|
||||
<option value="archived">已归档</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||
{projects.map((project) => (
|
||||
<div
|
||||
key={project.id}
|
||||
{filteredProjects.map((project) => (
|
||||
<div
|
||||
key={project.id}
|
||||
onClick={() => navigate(`/project/${project.id}`)}
|
||||
className="group bg-[#151619] border border-[#2a2b2f] rounded-xl p-6 hover:border-blue-500/50 transition-all cursor-pointer relative"
|
||||
>
|
||||
@ -121,35 +193,46 @@ export default function Projects() {
|
||||
</div>
|
||||
{canManage && (
|
||||
<div className="relative">
|
||||
<button
|
||||
className="text-gray-500 hover:text-white p-1 rounded-md hover:bg-white/5 transition-colors"
|
||||
<button
|
||||
className="text-gray-500 hover:text-white p-1 rounded-md hover:bg-white/5 transition-colors"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
setOpenMenuId(openMenuId === project.id ? null : project.id);
|
||||
setOpenMenuId(
|
||||
openMenuId === project.id ? null : project.id,
|
||||
);
|
||||
}}
|
||||
>
|
||||
<MoreVertical className="w-5 h-5" />
|
||||
</button>
|
||||
|
||||
|
||||
{openMenuId === project.id && (
|
||||
<div className="absolute right-0 top-8 w-32 bg-[#1a1b1e] border border-[#2a2b2f] rounded-lg shadow-2xl z-50 py-1 overflow-hidden">
|
||||
<button
|
||||
onClick={(e) => { e.stopPropagation(); handleEdit(project); }}
|
||||
<button
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
handleEdit(project);
|
||||
}}
|
||||
className="w-full flex items-center gap-2 px-3 py-2 text-xs text-gray-400 hover:text-white hover:bg-white/5 transition-colors"
|
||||
>
|
||||
<Edit3 className="w-3.5 h-3.5" />
|
||||
编辑
|
||||
</button>
|
||||
<button
|
||||
onClick={(e) => { e.stopPropagation(); handleCopy(project); }}
|
||||
<button
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
handleCopy(project);
|
||||
}}
|
||||
className="w-full flex items-center gap-2 px-3 py-2 text-xs text-gray-400 hover:text-white hover:bg-white/5 transition-colors"
|
||||
>
|
||||
<Copy className="w-3.5 h-3.5" />
|
||||
复制副本
|
||||
</button>
|
||||
<div className="h-px bg-[#2a2b2f] my-1" />
|
||||
<button
|
||||
onClick={(e) => { e.stopPropagation(); handleDelete(project.id); }}
|
||||
<button
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
handleDelete(project.id);
|
||||
}}
|
||||
className="w-full flex items-center gap-2 px-3 py-2 text-xs text-red-500 hover:bg-red-500/10 transition-colors"
|
||||
>
|
||||
<Trash2 className="w-3.5 h-3.5" />
|
||||
@ -160,13 +243,23 @@ export default function Projects() {
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<h3 className="text-lg font-bold text-white mb-2">{project.name}</h3>
|
||||
<p className="text-gray-400 text-sm line-clamp-2 mb-6">{project.description}</p>
|
||||
|
||||
<h3 className="text-lg font-bold text-white mb-2">
|
||||
{project.name}
|
||||
</h3>
|
||||
<p className="text-gray-400 text-sm line-clamp-2 mb-6">
|
||||
{project.description}
|
||||
</p>
|
||||
<div className="mb-5 flex items-center gap-2">
|
||||
<span className="inline-flex items-center gap-1 rounded-full border border-blue-500/20 bg-blue-500/10 px-2.5 py-1 text-xs font-medium text-blue-300">
|
||||
<WorkflowIcon className="w-3.5 h-3.5" />
|
||||
{project.workflowCount ?? 0} 个工作流
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between pt-6 border-t border-[#2a2b2f]">
|
||||
<div className="flex items-center gap-2 text-xs text-gray-500">
|
||||
<Calendar className="w-3 h-3" />
|
||||
{project.createdAt}
|
||||
{formatCompactDateTime(project.createdAt, timeZone)}
|
||||
</div>
|
||||
<div className="flex items-center gap-1 text-xs font-medium text-blue-500 group-hover:translate-x-1 transition-transform">
|
||||
进入项目
|
||||
@ -175,6 +268,13 @@ export default function Projects() {
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
{filteredProjects.length === 0 && (
|
||||
<div className="col-span-full bg-[#151619] border border-[#2a2b2f] rounded-xl p-12 text-center text-gray-500">
|
||||
{projects.length === 0
|
||||
? "当前还没有项目,请先创建真实项目。"
|
||||
: "没有匹配的项目,请调整筛选条件。"}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Create/Edit Modal */}
|
||||
@ -183,49 +283,70 @@ export default function Projects() {
|
||||
<div className="bg-[#151619] border border-[#2a2b2f] rounded-2xl w-full max-w-md shadow-2xl overflow-hidden">
|
||||
<div className="px-6 py-4 border-b border-[#2a2b2f] flex items-center justify-between bg-[#1a1b1e]">
|
||||
<h2 className="text-xl font-bold text-white flex items-center gap-2">
|
||||
{modalMode === "create" ? <Plus className="w-5 h-5 text-blue-500" /> : <Edit3 className="w-5 h-5 text-blue-500" />}
|
||||
{modalMode === "create" ? (
|
||||
<Plus className="w-5 h-5 text-blue-500" />
|
||||
) : (
|
||||
<Edit3 className="w-5 h-5 text-blue-500" />
|
||||
)}
|
||||
{modalMode === "create" ? "创建新项目" : "编辑项目"}
|
||||
</h2>
|
||||
<button onClick={() => setIsModalOpen(false)} className="text-gray-500 hover:text-white transition-colors">
|
||||
<button
|
||||
onClick={() => setIsModalOpen(false)}
|
||||
className="text-gray-500 hover:text-white transition-colors"
|
||||
>
|
||||
<X className="w-6 h-6" />
|
||||
</button>
|
||||
</div>
|
||||
<div className="p-6 space-y-4">
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">项目名称</label>
|
||||
<input
|
||||
type="text"
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
项目名称
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={currentProject.name}
|
||||
onChange={(e) => setCurrentProject({ ...currentProject, name: e.target.value })}
|
||||
onChange={(e) =>
|
||||
setCurrentProject({
|
||||
...currentProject,
|
||||
name: e.target.value,
|
||||
})
|
||||
}
|
||||
placeholder="请输入项目名称"
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500 transition-colors text-white"
|
||||
autoFocus
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">项目描述</label>
|
||||
<textarea
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
项目描述
|
||||
</label>
|
||||
<textarea
|
||||
rows={4}
|
||||
value={currentProject.description}
|
||||
onChange={(e) => setCurrentProject({ ...currentProject, description: e.target.value })}
|
||||
onChange={(e) =>
|
||||
setCurrentProject({
|
||||
...currentProject,
|
||||
description: e.target.value,
|
||||
})
|
||||
}
|
||||
placeholder="请输入项目描述,例如:处理机械臂抓取过程中的视觉数据"
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500 transition-colors text-white resize-none"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="p-6 border-t border-[#2a2b2f] flex justify-end gap-3 bg-[#0a0a0a]/50">
|
||||
<button
|
||||
<button
|
||||
onClick={() => setIsModalOpen(false)}
|
||||
className="px-4 py-2 text-sm font-medium text-gray-400 hover:text-white transition-colors"
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
<button
|
||||
<button
|
||||
onClick={handleSave}
|
||||
disabled={!currentProject.name}
|
||||
className={cn(
|
||||
"px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-md text-sm font-medium transition-all flex items-center gap-2",
|
||||
!currentProject.name && "opacity-50 cursor-not-allowed"
|
||||
!currentProject.name && "opacity-50 cursor-not-allowed",
|
||||
)}
|
||||
>
|
||||
<Check className="w-4 h-4" />
|
||||
@ -251,13 +372,13 @@ export default function Projects() {
|
||||
</div>
|
||||
</div>
|
||||
<div className="p-6 border-t border-[#2a2b2f] flex gap-3 bg-[#0a0a0a]/50">
|
||||
<button
|
||||
<button
|
||||
onClick={() => setIsDeleteModalOpen(false)}
|
||||
className="flex-1 px-4 py-2 text-sm font-medium text-gray-400 hover:text-white hover:bg-white/5 rounded-md transition-colors"
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
<button
|
||||
<button
|
||||
onClick={confirmDelete}
|
||||
className="flex-1 px-4 py-2 bg-red-600 hover:bg-red-700 text-white rounded-md text-sm font-medium transition-all"
|
||||
>
|
||||
|
||||
@ -1,140 +1,595 @@
|
||||
import React from "react";
|
||||
import {
|
||||
Server,
|
||||
Cpu,
|
||||
HardDrive,
|
||||
ShieldCheck,
|
||||
Globe,
|
||||
Save
|
||||
import React, { useEffect, useState } from "react";
|
||||
import {
|
||||
AlertCircle,
|
||||
Bot,
|
||||
CheckCircle2,
|
||||
Clock3,
|
||||
Cpu,
|
||||
Loader2,
|
||||
Save,
|
||||
Server,
|
||||
} from "lucide-react";
|
||||
import { useTimeZone } from "../context/TimeZoneContext";
|
||||
import {
|
||||
COMMON_TIME_ZONES,
|
||||
formatCompactDateTime,
|
||||
formatDateTime,
|
||||
resolveTimeZone,
|
||||
} from "../lib/datetime";
|
||||
import { buildSettingsUpdatePayload } from "../lib/settings.js";
|
||||
import { settingsApi } from "../services/api/settings";
|
||||
import type { SystemConfig } from "../types";
|
||||
|
||||
type SaveState = "idle" | "saving" | "saved" | "error";
|
||||
|
||||
export default function Settings() {
|
||||
const [config, setConfig] = useState<SystemConfig | null>(null);
|
||||
const [saveState, setSaveState] = useState<SaveState>("idle");
|
||||
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
||||
const { setTimeZone } = useTimeZone();
|
||||
|
||||
useEffect(() => {
|
||||
settingsApi
|
||||
.get()
|
||||
.then((loaded) => {
|
||||
setConfig({ ...loaded, timezone: resolveTimeZone(loaded.timezone) });
|
||||
setErrorMessage(null);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Failed to load settings", error);
|
||||
setErrorMessage(
|
||||
error instanceof Error ? error.message : "加载系统配置失败",
|
||||
);
|
||||
});
|
||||
}, []);
|
||||
|
||||
const handleSave = async () => {
|
||||
if (!config) {
|
||||
return;
|
||||
}
|
||||
setSaveState("saving");
|
||||
setErrorMessage(null);
|
||||
try {
|
||||
const saved = await settingsApi.update(buildSettingsUpdatePayload(config));
|
||||
const normalized = {
|
||||
...saved,
|
||||
timezone: resolveTimeZone(saved.timezone),
|
||||
};
|
||||
setConfig(normalized);
|
||||
setTimeZone(normalized.timezone);
|
||||
setSaveState("saved");
|
||||
} catch (error) {
|
||||
console.error("Failed to save settings", error);
|
||||
setSaveState("error");
|
||||
setErrorMessage(
|
||||
error instanceof Error ? error.message : "保存系统配置失败",
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
if (!config) {
|
||||
return (
|
||||
<div className="max-w-4xl space-y-6">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold tracking-tight text-white">
|
||||
系统配置
|
||||
</h1>
|
||||
<p className="text-gray-500 mt-1">
|
||||
配置 K8s 集群、Worker 调度、显示时区、AI 生成服务及默认资源限制
|
||||
</p>
|
||||
</div>
|
||||
<div className="bg-[#151619] border border-[#2a2b2f] rounded-xl p-8 flex items-center gap-3 text-gray-400">
|
||||
<Loader2 className="w-5 h-5 animate-spin" />
|
||||
正在加载真实配置...
|
||||
</div>
|
||||
{errorMessage && (
|
||||
<div className="bg-red-500/10 border border-red-500/20 rounded-xl px-4 py-3 text-sm text-red-300 flex items-center gap-2">
|
||||
<AlertCircle className="w-4 h-4 shrink-0" />
|
||||
{errorMessage}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const savedAIKeyExists =
|
||||
Boolean(config.ai.hasAPIKey) &&
|
||||
!(config.ai.apiKey && config.ai.apiKey.trim().length > 0);
|
||||
const savedKubeconfigExists =
|
||||
Boolean(config.k8s.hasKubeconfig) &&
|
||||
!(config.k8s.kubeconfig && config.k8s.kubeconfig.trim().length > 0);
|
||||
const savedAIKeyPreview = config.ai.apiKeyPreview?.trim() || "";
|
||||
const savedKubeconfigPreview = config.k8s.kubeconfigPreview?.trim() || "";
|
||||
|
||||
return (
|
||||
<div className="max-w-4xl space-y-8">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold tracking-tight text-white">系统配置</h1>
|
||||
<p className="text-gray-500 mt-1">配置 K8s 集群、OSS 存储及算子默认运行参数</p>
|
||||
<h1 className="text-3xl font-bold tracking-tight text-white">
|
||||
系统配置
|
||||
</h1>
|
||||
<p className="text-gray-500 mt-1">
|
||||
配置 K8s 集群、Worker 调度、显示时区、AI 生成服务及默认资源限制
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{errorMessage && (
|
||||
<div className="bg-red-500/10 border border-red-500/20 rounded-xl px-4 py-3 text-sm text-red-300 flex items-center gap-2">
|
||||
<AlertCircle className="w-4 h-4 shrink-0" />
|
||||
{errorMessage}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{saveState === "saved" && (
|
||||
<div className="bg-green-500/10 border border-green-500/20 rounded-xl px-4 py-3 text-sm text-green-300 flex items-center gap-2">
|
||||
<CheckCircle2 className="w-4 h-4 shrink-0" />
|
||||
配置已保存到后端。
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="space-y-6">
|
||||
{/* K8s Configuration */}
|
||||
<section className="bg-[#151619] border border-[#2a2b2f] rounded-xl overflow-hidden">
|
||||
<div className="px-6 py-4 border-b border-[#2a2b2f] flex items-center justify-between">
|
||||
<div className="flex items-center gap-2">
|
||||
<Server className="w-4 h-4 text-blue-500" />
|
||||
<h3 className="font-bold text-white">K8s 集群配置</h3>
|
||||
</div>
|
||||
<span className="text-[10px] text-gray-500 bg-blue-500/10 px-2 py-0.5 rounded border border-blue-500/20">推荐方式: Kubeconfig</span>
|
||||
</div>
|
||||
<div className="p-6 space-y-6">
|
||||
{/* Primary Config: Kubeconfig */}
|
||||
<div className="space-y-1.5">
|
||||
<div className="flex items-center justify-between">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">Kubeconfig (YAML)</label>
|
||||
<div className="flex gap-3">
|
||||
<span className="text-[10px] text-blue-500 cursor-pointer hover:underline">从文件导入</span>
|
||||
<span className="text-[10px] text-gray-600">|</span>
|
||||
<span className="text-[10px] text-gray-500">支持多集群配置</span>
|
||||
</div>
|
||||
</div>
|
||||
<textarea
|
||||
rows={8}
|
||||
placeholder="apiVersion: v1
|
||||
kind: Config
|
||||
clusters:
|
||||
- cluster:
|
||||
server: https://k8s.flowai.io:6443
|
||||
..."
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-3 text-xs font-mono focus:outline-none focus:border-blue-500 transition-colors resize-none"
|
||||
<label className="inline-flex items-center gap-2 text-xs text-gray-400">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={config.k8s.enabled}
|
||||
onChange={(event) =>
|
||||
setConfig((current) =>
|
||||
current
|
||||
? {
|
||||
...current,
|
||||
k8s: {
|
||||
...current.k8s,
|
||||
enabled: event.target.checked,
|
||||
},
|
||||
}
|
||||
: current,
|
||||
)
|
||||
}
|
||||
className="w-4 h-4 rounded bg-[#0a0a0a] border-[#2a2b2f] text-blue-500 focus:ring-blue-500"
|
||||
/>
|
||||
<p className="text-[10px] text-gray-600">系统将优先从 Kubeconfig 中解析集群地址与凭证</p>
|
||||
启用 K8s 调度
|
||||
</label>
|
||||
</div>
|
||||
<div className="p-6 space-y-4">
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
集群 Endpoint
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={config.k8s.endpoint || ""}
|
||||
onChange={(event) =>
|
||||
setConfig((current) =>
|
||||
current
|
||||
? {
|
||||
...current,
|
||||
k8s: {
|
||||
...current.k8s,
|
||||
endpoint: event.target.value,
|
||||
},
|
||||
}
|
||||
: current,
|
||||
)
|
||||
}
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
默认命名空间
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={config.k8s.namespace}
|
||||
onChange={(event) =>
|
||||
setConfig((current) =>
|
||||
current
|
||||
? {
|
||||
...current,
|
||||
k8s: {
|
||||
...current.k8s,
|
||||
namespace: event.target.value,
|
||||
},
|
||||
}
|
||||
: current,
|
||||
)
|
||||
}
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Secondary/Override Config */}
|
||||
<div className="pt-4 border-t border-[#2a2b2f]/50">
|
||||
<div className="flex items-center gap-2 mb-4">
|
||||
<div className="text-[10px] font-bold text-gray-500 uppercase tracking-widest">解析结果 / 手动覆盖</div>
|
||||
<div className="flex-1 h-px bg-[#2a2b2f]/50"></div>
|
||||
</div>
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">集群 Endpoint</label>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="自动从 Kubeconfig 解析..."
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500 transition-colors text-gray-400"
|
||||
/>
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
{savedKubeconfigExists ? "当前 Kubeconfig 摘要" : "Kubeconfig"}
|
||||
</label>
|
||||
{savedKubeconfigPreview ? (
|
||||
<div className="rounded-md border border-[#2a2b2f] bg-[#0a0a0a] px-4 py-3">
|
||||
<p className="text-[11px] uppercase tracking-wider text-gray-500">
|
||||
仅回显前后缀
|
||||
</p>
|
||||
<p className="mt-2 break-all text-xs font-mono text-gray-200">
|
||||
{savedKubeconfigPreview}
|
||||
</p>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">默认命名空间 (Namespace)</label>
|
||||
<input
|
||||
type="text"
|
||||
defaultValue="flowai-jobs"
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500 transition-colors"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
<p className="text-[11px] font-medium text-gray-500 uppercase tracking-wider">
|
||||
{savedKubeconfigExists ? "替换 Kubeconfig" : "录入 Kubeconfig"}
|
||||
</p>
|
||||
<textarea
|
||||
rows={8}
|
||||
value={config.k8s.kubeconfig || ""}
|
||||
onChange={(event) =>
|
||||
setConfig((current) =>
|
||||
current
|
||||
? {
|
||||
...current,
|
||||
k8s: {
|
||||
...current.k8s,
|
||||
kubeconfig: event.target.value,
|
||||
hasKubeconfig:
|
||||
current.k8s.hasKubeconfig ||
|
||||
event.target.value.trim().length > 0,
|
||||
},
|
||||
}
|
||||
: current,
|
||||
)
|
||||
}
|
||||
placeholder={
|
||||
savedKubeconfigExists
|
||||
? "粘贴新的 kubeconfig;留空表示保持不变"
|
||||
: "粘贴完整 kubeconfig YAML,供系统记录集群访问信息。"
|
||||
}
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-3 text-xs font-mono focus:outline-none focus:border-blue-500 resize-none"
|
||||
/>
|
||||
<p className="text-[11px] text-gray-500">
|
||||
{savedKubeconfigExists
|
||||
? "服务端只回显 kubeconfig 的前后缀摘要。留空保存不会覆盖现有配置;重新粘贴新内容会替换。"
|
||||
: "OSS 相关配置不在系统设置里维护,而是在工作流中的 OSS 算子节点上单独设置。"}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{/* Default Resource Limits */}
|
||||
<section className="bg-[#151619] border border-[#2a2b2f] rounded-xl overflow-hidden">
|
||||
<div className="px-6 py-4 border-b border-[#2a2b2f] flex items-center gap-2">
|
||||
<Cpu className="w-4 h-4 text-purple-500" />
|
||||
<h3 className="font-bold text-white">默认资源限制</h3>
|
||||
<h3 className="font-bold text-white">Worker 与资源默认值</h3>
|
||||
</div>
|
||||
<div className="p-6 space-y-4">
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div className="p-6 space-y-6">
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">默认 CPU (Request/Limit)</label>
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
轮询间隔 (ms)
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
min={1}
|
||||
value={config.worker.pollIntervalMs}
|
||||
onChange={(event) =>
|
||||
setConfig((current) =>
|
||||
current
|
||||
? {
|
||||
...current,
|
||||
worker: {
|
||||
...current.worker,
|
||||
pollIntervalMs:
|
||||
Number(event.target.value) ||
|
||||
current.worker.pollIntervalMs,
|
||||
},
|
||||
}
|
||||
: current,
|
||||
)
|
||||
}
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-purple-500"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
默认最大并发数
|
||||
</label>
|
||||
<input
|
||||
type="number"
|
||||
min={1}
|
||||
value={config.worker.maxConcurrentRuns}
|
||||
onChange={(event) =>
|
||||
setConfig((current) =>
|
||||
current
|
||||
? {
|
||||
...current,
|
||||
worker: {
|
||||
...current.worker,
|
||||
maxConcurrentRuns:
|
||||
Number(event.target.value) ||
|
||||
current.worker.maxConcurrentRuns,
|
||||
},
|
||||
}
|
||||
: current,
|
||||
)
|
||||
}
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-purple-500"
|
||||
/>
|
||||
<p className="text-[11px] text-gray-500">
|
||||
控制默认排队调度数量,以及单次工作流执行里可同时运行的节点数。工作流级配置可覆盖它。
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
默认 CPU (Request / Limit)
|
||||
</label>
|
||||
<div className="flex items-center gap-2">
|
||||
<input type="text" defaultValue="500m" className="flex-1 bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm" />
|
||||
<input
|
||||
type="text"
|
||||
value={config.defaults.cpuRequest}
|
||||
onChange={(event) =>
|
||||
setConfig((current) =>
|
||||
current
|
||||
? {
|
||||
...current,
|
||||
defaults: {
|
||||
...current.defaults,
|
||||
cpuRequest: event.target.value,
|
||||
},
|
||||
}
|
||||
: current,
|
||||
)
|
||||
}
|
||||
className="flex-1 bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm"
|
||||
/>
|
||||
<span className="text-gray-600">/</span>
|
||||
<input type="text" defaultValue="1000m" className="flex-1 bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm" />
|
||||
<input
|
||||
type="text"
|
||||
value={config.defaults.cpuLimit}
|
||||
onChange={(event) =>
|
||||
setConfig((current) =>
|
||||
current
|
||||
? {
|
||||
...current,
|
||||
defaults: {
|
||||
...current.defaults,
|
||||
cpuLimit: event.target.value,
|
||||
},
|
||||
}
|
||||
: current,
|
||||
)
|
||||
}
|
||||
className="flex-1 bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">默认内存 (Request/Limit)</label>
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
默认内存 (Request / Limit)
|
||||
</label>
|
||||
<div className="flex items-center gap-2">
|
||||
<input type="text" defaultValue="1Gi" className="flex-1 bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm" />
|
||||
<input
|
||||
type="text"
|
||||
value={config.defaults.memoryRequest}
|
||||
onChange={(event) =>
|
||||
setConfig((current) =>
|
||||
current
|
||||
? {
|
||||
...current,
|
||||
defaults: {
|
||||
...current.defaults,
|
||||
memoryRequest: event.target.value,
|
||||
},
|
||||
}
|
||||
: current,
|
||||
)
|
||||
}
|
||||
className="flex-1 bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm"
|
||||
/>
|
||||
<span className="text-gray-600">/</span>
|
||||
<input type="text" defaultValue="2Gi" className="flex-1 bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm" />
|
||||
<input
|
||||
type="text"
|
||||
value={config.defaults.memoryLimit}
|
||||
onChange={(event) =>
|
||||
setConfig((current) =>
|
||||
current
|
||||
? {
|
||||
...current,
|
||||
defaults: {
|
||||
...current.defaults,
|
||||
memoryLimit: event.target.value,
|
||||
},
|
||||
}
|
||||
: current,
|
||||
)
|
||||
}
|
||||
className="flex-1 bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{/* OSS Configuration */}
|
||||
<section className="bg-[#151619] border border-[#2a2b2f] rounded-xl overflow-hidden">
|
||||
<div className="px-6 py-4 border-b border-[#2a2b2f] flex items-center gap-2">
|
||||
<HardDrive className="w-4 h-4 text-orange-500" />
|
||||
<h3 className="font-bold text-white">OSS 存储配置</h3>
|
||||
<Clock3 className="w-4 h-4 text-amber-400" />
|
||||
<h3 className="font-bold text-white">显示与时区</h3>
|
||||
</div>
|
||||
<div className="p-6 space-y-4">
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">Endpoint</label>
|
||||
<input type="text" defaultValue="oss-cn-hangzhou.aliyuncs.com" className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm" />
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
IANA 时区
|
||||
</label>
|
||||
<input
|
||||
list="system-timezone-options"
|
||||
type="text"
|
||||
value={config.timezone}
|
||||
onChange={(event) =>
|
||||
setConfig((current) =>
|
||||
current
|
||||
? { ...current, timezone: event.target.value }
|
||||
: current,
|
||||
)
|
||||
}
|
||||
placeholder="例如 Asia/Shanghai"
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-amber-400"
|
||||
/>
|
||||
<datalist id="system-timezone-options">
|
||||
{COMMON_TIME_ZONES.map((timeZone) => (
|
||||
<option key={timeZone} value={timeZone} />
|
||||
))}
|
||||
</datalist>
|
||||
<p className="text-[11px] text-gray-500">
|
||||
页面里的创建时间、更新时间和运行时间都会按这里的时区展示,并统一收敛到分钟级;日志与资源图表会保留更细的时间粒度。
|
||||
</p>
|
||||
</div>
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section className="bg-[#151619] border border-[#2a2b2f] rounded-xl overflow-hidden">
|
||||
<div className="px-6 py-4 border-b border-[#2a2b2f] flex items-center gap-2">
|
||||
<Bot className="w-4 h-4 text-cyan-400" />
|
||||
<h3 className="font-bold text-white">AI 生成配置</h3>
|
||||
</div>
|
||||
<div className="p-6 space-y-4">
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">Access Key ID</label>
|
||||
<input type="password" value="••••••••••••••••" className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm" readOnly />
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
Provider
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={config.ai.provider}
|
||||
onChange={(event) =>
|
||||
setConfig((current) =>
|
||||
current
|
||||
? {
|
||||
...current,
|
||||
ai: { ...current.ai, provider: event.target.value },
|
||||
}
|
||||
: current,
|
||||
)
|
||||
}
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-cyan-500"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">Access Key Secret</label>
|
||||
<input type="password" value="••••••••••••••••" className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm" readOnly />
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
模型
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={config.ai.model}
|
||||
onChange={(event) =>
|
||||
setConfig((current) =>
|
||||
current
|
||||
? {
|
||||
...current,
|
||||
ai: { ...current.ai, model: event.target.value },
|
||||
}
|
||||
: current,
|
||||
)
|
||||
}
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-cyan-500"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
OpenAI 兼容 Base URL
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={config.ai.baseURL}
|
||||
onChange={(event) =>
|
||||
setConfig((current) =>
|
||||
current
|
||||
? {
|
||||
...current,
|
||||
ai: { ...current.ai, baseURL: event.target.value },
|
||||
}
|
||||
: current,
|
||||
)
|
||||
}
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-cyan-500"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
{savedAIKeyExists ? "当前 API Key 摘要" : "API Key"}
|
||||
</label>
|
||||
{savedAIKeyPreview ? (
|
||||
<div className="rounded-md border border-[#2a2b2f] bg-[#0a0a0a] px-4 py-3">
|
||||
<p className="text-[11px] uppercase tracking-wider text-gray-500">
|
||||
仅回显前后缀
|
||||
</p>
|
||||
<p className="mt-2 break-all font-mono text-sm text-gray-200">
|
||||
{savedAIKeyPreview}
|
||||
</p>
|
||||
</div>
|
||||
) : null}
|
||||
<p className="text-[11px] font-medium text-gray-500 uppercase tracking-wider">
|
||||
{savedAIKeyExists ? "替换 API Key" : "录入 API Key"}
|
||||
</p>
|
||||
<input
|
||||
type="password"
|
||||
value={config.ai.apiKey || ""}
|
||||
placeholder={
|
||||
savedAIKeyExists
|
||||
? "输入新 Key;留空表示保持不变"
|
||||
: "输入服务端 AI Key"
|
||||
}
|
||||
onChange={(event) =>
|
||||
setConfig((current) =>
|
||||
current
|
||||
? {
|
||||
...current,
|
||||
ai: {
|
||||
...current.ai,
|
||||
apiKey: event.target.value,
|
||||
hasAPIKey:
|
||||
current.ai.hasAPIKey ||
|
||||
event.target.value.trim().length > 0,
|
||||
},
|
||||
}
|
||||
: current,
|
||||
)
|
||||
}
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-cyan-500"
|
||||
/>
|
||||
<p className="text-[11px] text-gray-500">
|
||||
{savedAIKeyExists
|
||||
? "服务端只回显 AI Key 的前后缀摘要。留空保存不会覆盖现有 Key;输入新值会替换。"
|
||||
: "这里保存的是服务端 AI 生成配置。保存后,AI 生成算子与 AI 生成工作流会立即使用这组配置。"}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-end pt-4">
|
||||
<button className="flex items-center gap-2 bg-blue-600 hover:bg-blue-700 text-white px-6 py-2 rounded-md text-sm font-medium transition-colors">
|
||||
<Save className="w-4 h-4" />
|
||||
保存配置
|
||||
<div className="flex items-center justify-between pt-4">
|
||||
<p className="text-xs text-gray-500">
|
||||
最近更新时间:{" "}
|
||||
{formatCompactDateTime(
|
||||
config.updatedAt,
|
||||
config.timezone,
|
||||
"尚未保存",
|
||||
)}
|
||||
</p>
|
||||
<button
|
||||
onClick={handleSave}
|
||||
disabled={saveState === "saving"}
|
||||
className="flex items-center gap-2 bg-blue-600 hover:bg-blue-700 text-white px-6 py-2 rounded-md text-sm font-medium transition-colors disabled:opacity-60 disabled:cursor-not-allowed"
|
||||
>
|
||||
{saveState === "saving" ? (
|
||||
<Loader2 className="w-4 h-4 animate-spin" />
|
||||
) : (
|
||||
<Save className="w-4 h-4" />
|
||||
)}
|
||||
{saveState === "saving" ? "正在保存..." : "保存配置"}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -1,162 +1,164 @@
|
||||
import React, { useState } from "react";
|
||||
import { Plus, Search, FileCode, Copy, Trash2, ArrowRight, X, Edit3, Sparkles, FolderKanban, Check } from "lucide-react";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import {
|
||||
Plus,
|
||||
Search,
|
||||
FileCode,
|
||||
Cpu,
|
||||
Copy,
|
||||
Trash2,
|
||||
ArrowRight,
|
||||
X,
|
||||
Edit3,
|
||||
Sparkles,
|
||||
FolderKanban,
|
||||
Check,
|
||||
} from "lucide-react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useTimeZone } from "../context/TimeZoneContext";
|
||||
import { useUser } from "../context/UserContext";
|
||||
import { formatCompactDateTime } from "../lib/datetime";
|
||||
import {
|
||||
buildTemplatePayload,
|
||||
buildWorkflowFromTemplate,
|
||||
} from "../lib/templates.js";
|
||||
import { countWorkflowOperators } from "../lib/workflow";
|
||||
import { cn } from "../lib/utils";
|
||||
import AIGenerateModal from "../components/AIGenerateModal";
|
||||
import { GeneratedWorkflow } from "../services/aiService";
|
||||
import { MOCK_PROJECTS } from "../constants";
|
||||
|
||||
const INITIAL_TEMPLATES = [
|
||||
{
|
||||
id: "tpl-1",
|
||||
name: "通用图像处理模板",
|
||||
description: "包含缩放、灰度化与归一化算子",
|
||||
createdAt: "2024-03-20",
|
||||
nodes: [
|
||||
{
|
||||
id: "1",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "图像缩放",
|
||||
category: "CV",
|
||||
inputs: [{ id: "in-0", name: "input", type: "video" }],
|
||||
outputs: [{ id: "out-0", name: "output", type: "video" }],
|
||||
hyperparams: [
|
||||
{ key: "SCALE_FACTOR", value: "0.5", description: "缩放比例" },
|
||||
{ key: "INTERPOLATION", value: "linear", description: "插值算法" }
|
||||
]
|
||||
},
|
||||
position: { x: 100, y: 100 }
|
||||
},
|
||||
{
|
||||
id: "2",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "灰度化",
|
||||
category: "CV",
|
||||
inputs: [{ id: "in-0", name: "input", type: "video" }],
|
||||
outputs: [{ id: "out-0", name: "output", type: "video" }],
|
||||
hyperparams: [
|
||||
{ key: "COLOR_SPACE", value: "GRAY", description: "目标颜色空间" }
|
||||
]
|
||||
},
|
||||
position: { x: 400, y: 100 }
|
||||
},
|
||||
{
|
||||
id: "3",
|
||||
type: "operator",
|
||||
data: {
|
||||
label: "归一化",
|
||||
category: "CV",
|
||||
inputs: [{ id: "in-0", name: "input", type: "video" }],
|
||||
outputs: [{ id: "out-0", name: "output", type: "video" }],
|
||||
hyperparams: [
|
||||
{ key: "NORM_TYPE", value: "MINMAX", description: "归一化类型" }
|
||||
]
|
||||
},
|
||||
position: { x: 700, y: 100 }
|
||||
}
|
||||
],
|
||||
edges: [
|
||||
{ id: "e1-2", source: "1", sourceHandle: "out-0", target: "2", targetHandle: "in-0", animated: true },
|
||||
{ id: "e2-3", source: "2", sourceHandle: "out-0", target: "3", targetHandle: "in-0", animated: true }
|
||||
]
|
||||
},
|
||||
{
|
||||
id: "tpl-2",
|
||||
name: "YOLO 检测流水线",
|
||||
description: "预置 YOLOv8 推理与结果解析",
|
||||
createdAt: "2024-03-21",
|
||||
nodes: [
|
||||
{ id: "1", type: "operator", data: { label: "视频流输入", category: "IO", inputs: [], outputs: [{ id: "out-0", name: "stream", type: "video" }] }, position: { x: 100, y: 100 } },
|
||||
{ id: "2", type: "operator", data: { label: "YOLOv8 推理", category: "AI", inputs: [{ id: "in-0", name: "frame", type: "video" }], outputs: [{ id: "out-0", name: "detections", type: "json" }] }, position: { x: 400, y: 100 } },
|
||||
{ id: "3", type: "operator", data: { label: "结果可视化", category: "CV", inputs: [{ id: "in-0", name: "frame", type: "video" }, { id: "in-1", name: "data", type: "json" }], outputs: [{ id: "out-0", name: "output", type: "video" }] }, position: { x: 700, y: 100 } }
|
||||
],
|
||||
edges: [
|
||||
{ id: "e1-2", source: "1", sourceHandle: "out-0", target: "2", targetHandle: "in-0", animated: true },
|
||||
{ id: "e1-3", source: "1", sourceHandle: "out-0", target: "3", targetHandle: "in-0", animated: true },
|
||||
{ id: "e2-3", source: "2", sourceHandle: "out-0", target: "3", targetHandle: "in-1", animated: true }
|
||||
]
|
||||
},
|
||||
];
|
||||
import { projectsApi } from "../services/api/projects";
|
||||
import { templatesApi } from "../services/api/templates";
|
||||
import type { Project, WorkflowTemplate } from "../types";
|
||||
import { useAIStatus } from "../hooks/useAIStatus";
|
||||
|
||||
export default function Templates() {
|
||||
const navigate = useNavigate();
|
||||
const { timeZone } = useTimeZone();
|
||||
const { role } = useUser();
|
||||
const [templates, setTemplates] = useState(INITIAL_TEMPLATES);
|
||||
const aiStatus = useAIStatus();
|
||||
const [templates, setTemplates] = useState<WorkflowTemplate[]>([]);
|
||||
const [projects, setProjects] = useState<Project[]>([]);
|
||||
const [isModalOpen, setIsModalOpen] = useState(false);
|
||||
const [isAIModalOpen, setIsAIModalOpen] = useState(false);
|
||||
const [isProjectSelectOpen, setIsProjectSelectOpen] = useState(false);
|
||||
const [selectedTemplate, setSelectedTemplate] = useState<any>(null);
|
||||
const [selectedProjectId, setSelectedProjectId] = useState<string | null>(null);
|
||||
const [selectedProjectId, setSelectedProjectId] = useState<string | null>(
|
||||
null,
|
||||
);
|
||||
const [searchQuery, setSearchQuery] = useState("");
|
||||
const [newTpl, setNewTpl] = useState({ name: "", description: "" });
|
||||
|
||||
useEffect(() => {
|
||||
Promise.all([templatesApi.list(), projectsApi.list()])
|
||||
.then(([loadedTemplates, loadedProjects]) => {
|
||||
setTemplates(loadedTemplates);
|
||||
setProjects(loadedProjects);
|
||||
})
|
||||
.catch((error) => console.error("Failed to load templates", error));
|
||||
}, []);
|
||||
|
||||
const handleCreate = () => {
|
||||
if (newTpl.name) {
|
||||
const tpl = {
|
||||
id: `tpl-${Date.now()}`,
|
||||
name: newTpl.name,
|
||||
description: newTpl.description,
|
||||
createdAt: new Date().toISOString().split('T')[0],
|
||||
nodes: [],
|
||||
edges: []
|
||||
};
|
||||
setTemplates([tpl, ...templates]);
|
||||
setIsModalOpen(false);
|
||||
setNewTpl({ name: "", description: "" });
|
||||
// Navigate to editor for the new template
|
||||
navigate(`/workflow/${tpl.id}?type=template`);
|
||||
templatesApi
|
||||
.create(
|
||||
buildTemplatePayload({
|
||||
name: newTpl.name,
|
||||
description: newTpl.description,
|
||||
type: "batch",
|
||||
nodes: [],
|
||||
edges: [],
|
||||
}),
|
||||
)
|
||||
.then((created) => {
|
||||
setTemplates([created, ...templates]);
|
||||
setIsModalOpen(false);
|
||||
setNewTpl({ name: "", description: "" });
|
||||
navigate(`/workflow/${created.id}?type=template`);
|
||||
})
|
||||
.catch((error) => console.error("Failed to create template", error));
|
||||
}
|
||||
};
|
||||
|
||||
const handleAIGenerate = (workflow: GeneratedWorkflow, name: string, description: string) => {
|
||||
const tpl = {
|
||||
id: `tpl-${Date.now()}`,
|
||||
name: name,
|
||||
description: description,
|
||||
createdAt: new Date().toISOString().split('T')[0],
|
||||
nodes: workflow.nodes,
|
||||
edges: workflow.edges
|
||||
};
|
||||
setTemplates([tpl, ...templates]);
|
||||
setIsAIModalOpen(false);
|
||||
// Navigate to editor for the new template with generated nodes/edges
|
||||
navigate(`/workflow/${tpl.id}?type=template`, {
|
||||
state: { nodes: workflow.nodes, edges: workflow.edges }
|
||||
});
|
||||
const handleAIGenerate = (
|
||||
workflow: GeneratedWorkflow,
|
||||
name: string,
|
||||
description: string,
|
||||
) => {
|
||||
templatesApi
|
||||
.create(
|
||||
buildTemplatePayload(
|
||||
{
|
||||
...workflow,
|
||||
nodes: workflow.nodes,
|
||||
edges: workflow.edges,
|
||||
},
|
||||
{ name, description },
|
||||
),
|
||||
)
|
||||
.then((created) => {
|
||||
setTemplates([created, ...templates]);
|
||||
setIsAIModalOpen(false);
|
||||
navigate(`/workflow/${created.id}?type=template`);
|
||||
})
|
||||
.catch((error) => console.error("Failed to create AI template", error));
|
||||
};
|
||||
|
||||
const handleCopy = (tpl: any) => {
|
||||
const copy = {
|
||||
...tpl,
|
||||
id: `tpl-${Date.now()}`,
|
||||
name: `${tpl.name} (副本)`,
|
||||
createdAt: new Date().toISOString().split('T')[0]
|
||||
};
|
||||
setTemplates([copy, ...templates]);
|
||||
templatesApi
|
||||
.create(
|
||||
buildTemplatePayload(tpl, {
|
||||
name: `${tpl.name} (副本)`,
|
||||
description: tpl.description,
|
||||
}),
|
||||
)
|
||||
.then((created) => setTemplates([created, ...templates]))
|
||||
.catch((error) => console.error("Failed to copy template", error));
|
||||
};
|
||||
|
||||
const handleDelete = (id: string) => {
|
||||
setTemplates(templates.filter(t => t.id !== id));
|
||||
templatesApi
|
||||
.remove(id)
|
||||
.then(() =>
|
||||
setTemplates(templates.filter((template) => template.id !== id)),
|
||||
)
|
||||
.catch((error) => console.error("Failed to delete template", error));
|
||||
};
|
||||
|
||||
const canManage = role === "admin" || role === "operator";
|
||||
const normalizedSearch = searchQuery.trim().toLowerCase();
|
||||
const filteredTemplates = templates.filter((template) => {
|
||||
if (!normalizedSearch) {
|
||||
return true;
|
||||
}
|
||||
return [template.name, template.description]
|
||||
.filter(Boolean)
|
||||
.some((value) => value.toLowerCase().includes(normalizedSearch));
|
||||
});
|
||||
|
||||
const handleUseTemplate = (tpl: any) => {
|
||||
setSelectedTemplate(tpl);
|
||||
setSelectedProjectId(null);
|
||||
setIsProjectSelectOpen(true);
|
||||
};
|
||||
|
||||
const openTemplateEditor = (templateId: string) => {
|
||||
navigate(`/workflow/${templateId}?type=template`);
|
||||
};
|
||||
|
||||
const confirmUseTemplate = () => {
|
||||
if (selectedTemplate && selectedProjectId) {
|
||||
navigate(`/workflow/wf-${Date.now()}?projectId=${selectedProjectId}&type=batch`, {
|
||||
state: {
|
||||
nodes: selectedTemplate.nodes || [],
|
||||
edges: selectedTemplate.edges || []
|
||||
}
|
||||
});
|
||||
setIsProjectSelectOpen(false);
|
||||
projectsApi
|
||||
.createWorkflow(
|
||||
selectedProjectId,
|
||||
buildWorkflowFromTemplate(selectedTemplate),
|
||||
)
|
||||
.then((created) => {
|
||||
setIsProjectSelectOpen(false);
|
||||
setSelectedProjectId(null);
|
||||
navigate(`/workflow/${created.id}?projectId=${selectedProjectId}`);
|
||||
})
|
||||
.catch((error) =>
|
||||
console.error("Failed to instantiate template workflow", error),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
@ -164,19 +166,34 @@ export default function Templates() {
|
||||
<div className="space-y-8">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold tracking-tight text-white">工作流模板</h1>
|
||||
<p className="text-gray-500 mt-1">复用经过验证的数据处理流程,加速开发效率</p>
|
||||
<h1 className="text-3xl font-bold tracking-tight text-white">
|
||||
工作流模板
|
||||
</h1>
|
||||
<p className="text-gray-500 mt-1">
|
||||
复用经过验证的数据处理流程,加速开发效率
|
||||
</p>
|
||||
</div>
|
||||
{canManage && (
|
||||
<div className="flex gap-3">
|
||||
<button
|
||||
<button
|
||||
onClick={() => setIsAIModalOpen(true)}
|
||||
className="flex items-center gap-2 bg-white/5 hover:bg-white/10 text-blue-400 px-4 py-2 rounded-md text-sm font-medium transition-colors border border-blue-500/20"
|
||||
disabled={!aiStatus.enabled}
|
||||
title={
|
||||
aiStatus.enabled
|
||||
? `使用 ${aiStatus.model || "glm-5"} 生成模板`
|
||||
: aiStatus.reason || "当前服务端未配置 AI 服务"
|
||||
}
|
||||
className={cn(
|
||||
"flex items-center gap-2 px-4 py-2 rounded-md text-sm font-medium transition-colors border border-blue-500/20",
|
||||
aiStatus.enabled
|
||||
? "bg-white/5 hover:bg-white/10 text-blue-400"
|
||||
: "bg-white/5 text-gray-500 cursor-not-allowed opacity-60",
|
||||
)}
|
||||
>
|
||||
<Sparkles className="w-4 h-4" />
|
||||
AI 智能生成
|
||||
</button>
|
||||
<button
|
||||
<button
|
||||
onClick={() => setIsModalOpen(true)}
|
||||
className="flex items-center gap-2 bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-md text-sm font-medium transition-colors"
|
||||
>
|
||||
@ -190,70 +207,109 @@ export default function Templates() {
|
||||
<div className="flex items-center gap-4 bg-[#151619] p-4 rounded-lg border border-[#2a2b2f]">
|
||||
<div className="relative flex-1">
|
||||
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-500" />
|
||||
<input
|
||||
type="text"
|
||||
placeholder="搜索模板..."
|
||||
<input
|
||||
type="text"
|
||||
placeholder="搜索模板..."
|
||||
value={searchQuery}
|
||||
onChange={(event) => setSearchQuery(event.target.value)}
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md pl-10 pr-4 py-2 text-sm focus:outline-none focus:border-blue-500 transition-colors"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||
{templates.map((tpl) => (
|
||||
<div key={tpl.id} className="group bg-[#151619] border border-[#2a2b2f] rounded-xl p-6 hover:border-blue-500/50 transition-all flex flex-col">
|
||||
<div className="flex items-start justify-between mb-4">
|
||||
<div className="w-12 h-12 bg-blue-600/10 rounded-lg flex items-center justify-center">
|
||||
<FileCode className="w-6 h-6 text-blue-500" />
|
||||
</div>
|
||||
{canManage && (
|
||||
<div className="flex items-center gap-1">
|
||||
<button
|
||||
onClick={(e) => { e.stopPropagation(); handleCopy(tpl); }}
|
||||
className="p-2 hover:bg-white/5 rounded-md text-gray-500 hover:text-white transition-colors"
|
||||
title="复制模板"
|
||||
>
|
||||
<Copy className="w-4 h-4" />
|
||||
</button>
|
||||
<button
|
||||
onClick={(e) => { e.stopPropagation(); handleDelete(tpl.id); }}
|
||||
className="p-2 hover:bg-red-500/10 rounded-md text-gray-500 hover:text-red-500 transition-colors"
|
||||
title="删除模板"
|
||||
>
|
||||
<Trash2 className="w-4 h-4" />
|
||||
</button>
|
||||
{filteredTemplates.map((tpl) => {
|
||||
const operatorCount = countWorkflowOperators(tpl.nodes);
|
||||
|
||||
return (
|
||||
<div
|
||||
key={tpl.id}
|
||||
role="button"
|
||||
tabIndex={0}
|
||||
onDoubleClick={() => openTemplateEditor(tpl.id)}
|
||||
onKeyDown={(event) => {
|
||||
if (event.key === "Enter") {
|
||||
openTemplateEditor(tpl.id);
|
||||
}
|
||||
}}
|
||||
className="group cursor-pointer bg-[#151619] border border-[#2a2b2f] rounded-xl p-6 hover:border-blue-500/50 transition-all flex flex-col focus:outline-none focus:ring-2 focus:ring-blue-500/40"
|
||||
title="双击进入模板编辑"
|
||||
>
|
||||
<div className="flex items-start justify-between mb-4">
|
||||
<div className="w-12 h-12 bg-blue-600/10 rounded-lg flex items-center justify-center">
|
||||
<FileCode className="w-6 h-6 text-blue-500" />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<h3 className="text-lg font-bold text-white mb-1">{tpl.name}</h3>
|
||||
<p className="text-sm text-gray-500 mb-6 flex-1 line-clamp-2">{tpl.description}</p>
|
||||
|
||||
<div className="flex items-center justify-between pt-4 border-t border-[#2a2b2f]">
|
||||
<span className="text-[10px] text-gray-600 font-mono uppercase tracking-widest">创建于 {tpl.createdAt}</span>
|
||||
<div className="flex items-center gap-3">
|
||||
<button
|
||||
onClick={() => navigate(`/workflow/${tpl.id}?type=template`)}
|
||||
className="text-xs text-gray-400 hover:text-white font-medium flex items-center gap-1 transition-all"
|
||||
>
|
||||
<Edit3 className="w-3 h-3" />
|
||||
{canManage ? "编辑" : "查看"}
|
||||
</button>
|
||||
{canManage && (
|
||||
<button
|
||||
onClick={() => handleUseTemplate(tpl)}
|
||||
className="text-xs text-blue-500 font-medium flex items-center gap-1 group-hover:gap-2 transition-all"
|
||||
>
|
||||
使用此模板
|
||||
<ArrowRight className="w-3 h-3" />
|
||||
</button>
|
||||
<div className="flex items-center gap-1">
|
||||
<button
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
handleCopy(tpl);
|
||||
}}
|
||||
className="p-2 hover:bg-white/5 rounded-md text-gray-500 hover:text-white transition-colors"
|
||||
title="复制模板"
|
||||
>
|
||||
<Copy className="w-4 h-4" />
|
||||
</button>
|
||||
<button
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
handleDelete(tpl.id);
|
||||
}}
|
||||
className="p-2 hover:bg-red-500/10 rounded-md text-gray-500 hover:text-red-500 transition-colors"
|
||||
title="删除模板"
|
||||
>
|
||||
<Trash2 className="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<h3 className="text-lg font-bold text-white mb-1">{tpl.name}</h3>
|
||||
<p className="text-sm text-gray-500 mb-6 flex-1 line-clamp-2">
|
||||
{tpl.description}
|
||||
</p>
|
||||
<div className="mb-5 flex items-center gap-2">
|
||||
<span className="inline-flex items-center gap-1 rounded-full border border-blue-500/20 bg-blue-500/10 px-2.5 py-1 text-xs font-medium text-blue-300">
|
||||
<Cpu className="w-3.5 h-3.5" />
|
||||
{operatorCount} 个算子
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between pt-4 border-t border-[#2a2b2f]">
|
||||
<span className="block text-[11px] text-gray-500">
|
||||
创建于 {formatCompactDateTime(tpl.createdAt, timeZone)}
|
||||
</span>
|
||||
<div className="flex items-center gap-3">
|
||||
<button
|
||||
onClick={() => openTemplateEditor(tpl.id)}
|
||||
className="text-xs text-gray-400 hover:text-white font-medium flex items-center gap-1 transition-all"
|
||||
>
|
||||
<Edit3 className="w-3 h-3" />
|
||||
{canManage ? "编辑" : "查看"}
|
||||
</button>
|
||||
{canManage && (
|
||||
<button
|
||||
onClick={() => handleUseTemplate(tpl)}
|
||||
disabled={projects.length === 0}
|
||||
className="text-xs text-blue-500 font-medium flex items-center gap-1 group-hover:gap-2 transition-all"
|
||||
>
|
||||
{projects.length === 0 ? "请先创建项目" : "使用此模板"}
|
||||
<ArrowRight className="w-3 h-3" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
{templates.length === 0 && (
|
||||
);
|
||||
})}
|
||||
{filteredTemplates.length === 0 && (
|
||||
<div className="col-span-full bg-[#151619] border border-[#2a2b2f] rounded-xl p-12 text-center">
|
||||
<p className="text-gray-500">暂无模板,您可以点击右上角创建或从项目中保存。</p>
|
||||
<p className="text-gray-500">
|
||||
{templates.length === 0
|
||||
? "暂无模板,您可以点击右上角创建或从项目中保存。"
|
||||
: "没有匹配的模板,请调整搜索条件。"}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
@ -264,37 +320,48 @@ export default function Templates() {
|
||||
<div className="bg-[#151619] border border-[#2a2b2f] rounded-2xl w-full max-w-md shadow-2xl">
|
||||
<div className="px-6 py-4 border-b border-[#2a2b2f] flex items-center justify-between">
|
||||
<h2 className="text-xl font-bold text-white">创建新模板</h2>
|
||||
<button onClick={() => setIsModalOpen(false)} className="text-gray-500 hover:text-white">
|
||||
<button
|
||||
onClick={() => setIsModalOpen(false)}
|
||||
className="text-gray-500 hover:text-white"
|
||||
>
|
||||
<X className="w-6 h-6" />
|
||||
</button>
|
||||
</div>
|
||||
<div className="p-6 space-y-4">
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">模板名称</label>
|
||||
<input
|
||||
type="text"
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
模板名称
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="例如: 基础视觉处理"
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500"
|
||||
onChange={e => setNewTpl({...newTpl, name: e.target.value})}
|
||||
onChange={(e) =>
|
||||
setNewTpl({ ...newTpl, name: e.target.value })
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">描述</label>
|
||||
<textarea
|
||||
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider">
|
||||
描述
|
||||
</label>
|
||||
<textarea
|
||||
placeholder="简要描述模板的用途..."
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm h-24 focus:outline-none focus:border-blue-500"
|
||||
onChange={e => setNewTpl({...newTpl, description: e.target.value})}
|
||||
onChange={(e) =>
|
||||
setNewTpl({ ...newTpl, description: e.target.value })
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="p-6 border-t border-[#2a2b2f] flex justify-end gap-3">
|
||||
<button
|
||||
<button
|
||||
onClick={() => setIsModalOpen(false)}
|
||||
className="px-4 py-2 text-sm font-medium text-gray-400 hover:text-white transition-colors"
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
<button
|
||||
<button
|
||||
onClick={handleCreate}
|
||||
className="px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-md text-sm font-medium transition-colors"
|
||||
>
|
||||
@ -305,10 +372,10 @@ export default function Templates() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
<AIGenerateModal
|
||||
isOpen={isAIModalOpen}
|
||||
onClose={() => setIsAIModalOpen(false)}
|
||||
onGenerate={handleAIGenerate}
|
||||
<AIGenerateModal
|
||||
isOpen={isAIModalOpen}
|
||||
onClose={() => setIsAIModalOpen(false)}
|
||||
onGenerate={handleAIGenerate}
|
||||
/>
|
||||
|
||||
{/* Project Selection Modal */}
|
||||
@ -320,46 +387,59 @@ export default function Templates() {
|
||||
<FolderKanban className="w-5 h-5 text-blue-500" />
|
||||
选择目标项目
|
||||
</h2>
|
||||
<button onClick={() => setIsProjectSelectOpen(false)} className="text-gray-500 hover:text-white transition-colors">
|
||||
<button
|
||||
onClick={() => setIsProjectSelectOpen(false)}
|
||||
className="text-gray-500 hover:text-white transition-colors"
|
||||
>
|
||||
<X className="w-6 h-6" />
|
||||
</button>
|
||||
</div>
|
||||
<div className="p-6 space-y-4">
|
||||
<p className="text-sm text-gray-400">请选择要将此模板应用到的项目:</p>
|
||||
<p className="text-sm text-gray-400">
|
||||
请选择要将此模板应用到的项目:
|
||||
</p>
|
||||
<div className="space-y-2 max-h-60 overflow-y-auto pr-2 custom-scrollbar">
|
||||
{MOCK_PROJECTS.map((project) => (
|
||||
<button
|
||||
key={project.id}
|
||||
onClick={() => setSelectedProjectId(project.id)}
|
||||
className={cn(
|
||||
"w-full flex items-center justify-between p-4 rounded-xl border transition-all text-left",
|
||||
selectedProjectId === project.id
|
||||
? "bg-blue-600/10 border-blue-500 text-white"
|
||||
: "bg-[#0a0a0a] border-[#2a2b2f] text-gray-400 hover:border-gray-500"
|
||||
)}
|
||||
>
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="w-2 h-2 rounded-full bg-blue-500" />
|
||||
<span className="font-medium">{project.name}</span>
|
||||
</div>
|
||||
{selectedProjectId === project.id && <Check className="w-4 h-4 text-blue-500" />}
|
||||
</button>
|
||||
))}
|
||||
{projects.length === 0 ? (
|
||||
<div className="rounded-xl border border-dashed border-[#2a2b2f] bg-[#0a0a0a] px-4 py-6 text-sm text-gray-500">
|
||||
当前没有可用项目,请先到项目管理页创建一个真实项目。
|
||||
</div>
|
||||
) : (
|
||||
projects.map((project) => (
|
||||
<button
|
||||
key={project.id}
|
||||
onClick={() => setSelectedProjectId(project.id)}
|
||||
className={cn(
|
||||
"w-full flex items-center justify-between p-4 rounded-xl border transition-all text-left",
|
||||
selectedProjectId === project.id
|
||||
? "bg-blue-600/10 border-blue-500 text-white"
|
||||
: "bg-[#0a0a0a] border-[#2a2b2f] text-gray-400 hover:border-gray-500",
|
||||
)}
|
||||
>
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="w-2 h-2 rounded-full bg-blue-500" />
|
||||
<span className="font-medium">{project.name}</span>
|
||||
</div>
|
||||
{selectedProjectId === project.id && (
|
||||
<Check className="w-4 h-4 text-blue-500" />
|
||||
)}
|
||||
</button>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="p-6 border-t border-[#2a2b2f] flex justify-end gap-3 bg-[#0a0a0a]/50">
|
||||
<button
|
||||
<button
|
||||
onClick={() => setIsProjectSelectOpen(false)}
|
||||
className="px-4 py-2 text-sm font-medium text-gray-400 hover:text-white transition-colors"
|
||||
>
|
||||
取消
|
||||
</button>
|
||||
<button
|
||||
<button
|
||||
onClick={confirmUseTemplate}
|
||||
disabled={!selectedProjectId}
|
||||
className={cn(
|
||||
"px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-md text-sm font-medium transition-all",
|
||||
!selectedProjectId && "opacity-50 cursor-not-allowed"
|
||||
!selectedProjectId && "opacity-50 cursor-not-allowed",
|
||||
)}
|
||||
>
|
||||
确认并进入编辑器
|
||||
|
||||
@ -1,43 +1,86 @@
|
||||
import React, { useState } from "react";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { cn } from "../lib/utils";
|
||||
import { X, User, Shield, Mail } from "lucide-react";
|
||||
|
||||
interface UserData {
|
||||
id: string;
|
||||
name: string;
|
||||
role: string;
|
||||
email: string;
|
||||
}
|
||||
|
||||
const INITIAL_USERS: UserData[] = [
|
||||
{ id: "1", name: "管理员", role: "admin", email: "admin@emboflow.ai" },
|
||||
{ id: "2", name: "张三", role: "auditor", email: "zhangsan@emboflow.ai" },
|
||||
{ id: "3", name: "李四", role: "operator", email: "lisi@emboflow.ai" },
|
||||
];
|
||||
import { X, User, Shield, Mail, Plus, Trash2 } from "lucide-react";
|
||||
import { useUser } from "../context/UserContext";
|
||||
import type { User as UserData } from "../types";
|
||||
import { usersApi } from "../services/api/users";
|
||||
|
||||
export default function Users() {
|
||||
const [users, setUsers] = useState<UserData[]>(INITIAL_USERS);
|
||||
const { refreshUsers } = useUser();
|
||||
const [users, setUsers] = useState<UserData[]>([]);
|
||||
const [editingUser, setEditingUser] = useState<UserData | null>(null);
|
||||
const [isModalOpen, setIsModalOpen] = useState(false);
|
||||
const [modalMode, setModalMode] = useState<"create" | "edit">("edit");
|
||||
|
||||
useEffect(() => {
|
||||
refreshUsers({ silent: true }).then(setUsers).catch((error) => {
|
||||
console.error("Failed to load users", error);
|
||||
});
|
||||
}, [refreshUsers]);
|
||||
|
||||
const handleEdit = (user: UserData) => {
|
||||
setModalMode("edit");
|
||||
setEditingUser({ ...user });
|
||||
setIsModalOpen(true);
|
||||
};
|
||||
|
||||
const handleCreate = () => {
|
||||
setModalMode("create");
|
||||
setEditingUser({
|
||||
id: "",
|
||||
name: "",
|
||||
email: "",
|
||||
role: "operator",
|
||||
});
|
||||
setIsModalOpen(true);
|
||||
};
|
||||
|
||||
const handleSave = () => {
|
||||
if (editingUser) {
|
||||
setUsers(users.map(u => u.id === editingUser.id ? editingUser : u));
|
||||
setIsModalOpen(false);
|
||||
setEditingUser(null);
|
||||
const request =
|
||||
modalMode === "create"
|
||||
? usersApi.create({
|
||||
name: editingUser.name,
|
||||
email: editingUser.email,
|
||||
role: editingUser.role,
|
||||
})
|
||||
: usersApi.update(editingUser.id, editingUser);
|
||||
|
||||
request
|
||||
.then(async () => {
|
||||
const items = await refreshUsers({ silent: true });
|
||||
setUsers(items);
|
||||
setIsModalOpen(false);
|
||||
setEditingUser(null);
|
||||
})
|
||||
.catch((error) => console.error("Failed to save user", error));
|
||||
}
|
||||
};
|
||||
|
||||
const handleDelete = (id: string) => {
|
||||
usersApi
|
||||
.remove(id)
|
||||
.then(async () => {
|
||||
const items = await refreshUsers({ silent: true });
|
||||
setUsers(items);
|
||||
})
|
||||
.catch((error) => console.error("Failed to delete user", error));
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-8">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold tracking-tight text-white">用户管理</h1>
|
||||
<p className="text-gray-500 mt-1">管理平台成员及其访问权限</p>
|
||||
<div className="flex items-center justify-between gap-4">
|
||||
<div>
|
||||
<h1 className="text-3xl font-bold tracking-tight text-white">用户管理</h1>
|
||||
<p className="text-gray-500 mt-1">管理平台成员及其访问权限</p>
|
||||
</div>
|
||||
<button
|
||||
onClick={handleCreate}
|
||||
className="inline-flex items-center gap-2 rounded-md bg-blue-600 px-4 py-2 text-sm font-medium text-white transition-colors hover:bg-blue-700"
|
||||
>
|
||||
<Plus className="w-4 h-4" />
|
||||
创建用户
|
||||
</button>
|
||||
</div>
|
||||
<div className="bg-[#151619] border border-[#2a2b2f] rounded-xl overflow-hidden shadow-xl">
|
||||
<table className="w-full text-left text-sm">
|
||||
@ -65,12 +108,21 @@ export default function Users() {
|
||||
</span>
|
||||
</td>
|
||||
<td className="px-6 py-4 text-right">
|
||||
<button
|
||||
onClick={() => handleEdit(u)}
|
||||
className="text-xs text-blue-500 hover:text-blue-400 font-medium transition-colors"
|
||||
>
|
||||
编辑
|
||||
</button>
|
||||
<div className="inline-flex items-center gap-3">
|
||||
<button
|
||||
onClick={() => handleEdit(u)}
|
||||
className="text-xs text-blue-500 hover:text-blue-400 font-medium transition-colors"
|
||||
>
|
||||
编辑
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleDelete(u.id)}
|
||||
className="inline-flex items-center gap-1 text-xs text-red-500 hover:text-red-400 font-medium transition-colors"
|
||||
>
|
||||
<Trash2 className="w-3 h-3" />
|
||||
删除
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
@ -85,7 +137,7 @@ export default function Users() {
|
||||
<div className="px-6 py-4 border-b border-[#2a2b2f] flex items-center justify-between bg-[#1a1b1e]">
|
||||
<h2 className="text-xl font-bold text-white flex items-center gap-2">
|
||||
<User className="w-5 h-5 text-blue-500" />
|
||||
编辑用户信息
|
||||
{modalMode === "create" ? "创建用户" : "编辑用户信息"}
|
||||
</h2>
|
||||
<button onClick={() => setIsModalOpen(false)} className="text-gray-500 hover:text-white transition-colors">
|
||||
<X className="w-6 h-6" />
|
||||
@ -121,7 +173,7 @@ export default function Users() {
|
||||
<select
|
||||
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500 text-white"
|
||||
value={editingUser.role}
|
||||
onChange={e => setEditingUser({...editingUser, role: e.target.value})}
|
||||
onChange={e => setEditingUser({...editingUser, role: e.target.value as UserData["role"]})}
|
||||
>
|
||||
<option value="admin">Admin</option>
|
||||
<option value="auditor">Auditor</option>
|
||||
@ -140,7 +192,7 @@ export default function Users() {
|
||||
onClick={handleSave}
|
||||
className="px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-md text-sm font-medium transition-all"
|
||||
>
|
||||
保存修改
|
||||
{modalMode === "create" ? "创建用户" : "保存修改"}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,220 +1,20 @@
|
||||
import { GoogleGenAI, Type } from "@google/genai";
|
||||
import { Operator } from "../types";
|
||||
import {
|
||||
aiApi,
|
||||
type AIStatus,
|
||||
type GeneratedOperator,
|
||||
type GeneratedWorkflow,
|
||||
} from "./api/ai";
|
||||
|
||||
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY || "" });
|
||||
export type { AIStatus, GeneratedOperator, GeneratedWorkflow };
|
||||
|
||||
export interface GeneratedWorkflow {
|
||||
nodes: any[];
|
||||
edges: any[];
|
||||
export async function getAIStatus() {
|
||||
return aiApi.status();
|
||||
}
|
||||
|
||||
export async function generateWorkflowFromDescription(
|
||||
description: string,
|
||||
availableOperators: Operator[]
|
||||
): Promise<GeneratedWorkflow> {
|
||||
const model = "gemini-3-flash-preview";
|
||||
|
||||
// Prepare operator context for the prompt
|
||||
const operatorContext = availableOperators.map(op => ({
|
||||
id: op.id,
|
||||
name: op.name,
|
||||
category: op.category,
|
||||
description: op.description,
|
||||
inputs: op.inputs.map(i => ({ name: i.name, type: i.type })),
|
||||
outputs: op.outputs.map(o => ({ name: o.name, type: o.type })),
|
||||
hyperparams: op.hyperparams || [],
|
||||
skill: op.skillDefinition || "No detailed description available."
|
||||
}));
|
||||
|
||||
const systemInstruction = `
|
||||
You are an expert workflow architect for EmboFlow, a platform for Embodied AI and Computer Vision.
|
||||
Your task is to generate a workflow (nodes and edges) based on a user's natural language description.
|
||||
|
||||
Available Operators:
|
||||
${JSON.stringify(operatorContext, null, 2)}
|
||||
|
||||
Rules:
|
||||
1. Use only the available operators listed above.
|
||||
2. Connect them logically based on their input and output types.
|
||||
3. Output MUST be a valid JSON object with "nodes" and "edges" arrays.
|
||||
4. Nodes should have: id, type: "operator", position: {x, y}, data: { label, category, description, inputs, outputs }.
|
||||
5. Edges should have: id, source, target, sourceHandle, targetHandle.
|
||||
6. Position nodes logically (e.g., source at top, processing in middle, sink at bottom).
|
||||
7. Ensure port types match when creating edges.
|
||||
`;
|
||||
|
||||
const response = await ai.models.generateContent({
|
||||
model,
|
||||
contents: `User Request: ${description}`,
|
||||
config: {
|
||||
systemInstruction,
|
||||
responseMimeType: "application/json",
|
||||
responseSchema: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
nodes: {
|
||||
type: Type.ARRAY,
|
||||
items: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
id: { type: Type.STRING },
|
||||
type: { type: Type.STRING },
|
||||
position: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
x: { type: Type.NUMBER },
|
||||
y: { type: Type.NUMBER }
|
||||
},
|
||||
required: ["x", "y"]
|
||||
},
|
||||
data: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
label: { type: Type.STRING },
|
||||
category: { type: Type.STRING },
|
||||
description: { type: Type.STRING },
|
||||
inputs: { type: Type.ARRAY, items: { type: Type.OBJECT, properties: { id: { type: Type.STRING }, name: { type: Type.STRING }, type: { type: Type.STRING } } } },
|
||||
outputs: { type: Type.ARRAY, items: { type: Type.OBJECT, properties: { id: { type: Type.STRING }, name: { type: Type.STRING }, type: { type: Type.STRING } } } },
|
||||
hyperparams: {
|
||||
type: Type.ARRAY,
|
||||
items: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
key: { type: Type.STRING },
|
||||
value: { type: Type.STRING },
|
||||
description: { type: Type.STRING }
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
required: ["label", "category"]
|
||||
}
|
||||
},
|
||||
required: ["id", "type", "position", "data"]
|
||||
}
|
||||
},
|
||||
edges: {
|
||||
type: Type.ARRAY,
|
||||
items: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
id: { type: Type.STRING },
|
||||
source: { type: Type.STRING },
|
||||
target: { type: Type.STRING },
|
||||
sourceHandle: { type: Type.STRING },
|
||||
targetHandle: { type: Type.STRING }
|
||||
},
|
||||
required: ["id", "source", "target"]
|
||||
}
|
||||
}
|
||||
},
|
||||
required: ["nodes", "edges"]
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
const result = JSON.parse(response.text || "{}");
|
||||
return result as GeneratedWorkflow;
|
||||
} catch (error) {
|
||||
console.error("Failed to parse AI response:", error);
|
||||
throw new Error("AI generated an invalid workflow structure.");
|
||||
}
|
||||
export async function generateWorkflowFromDescription(description: string): Promise<GeneratedWorkflow> {
|
||||
return aiApi.generateWorkflow(description);
|
||||
}
|
||||
|
||||
export interface GeneratedOperator {
|
||||
name: string;
|
||||
category: string;
|
||||
description: string;
|
||||
inputs: { name: string; type: string; transmission: string }[];
|
||||
outputs: { name: string; type: string; transmission: string }[];
|
||||
hyperparams: { key: string; value: string; description: string }[];
|
||||
skillDefinition: string;
|
||||
}
|
||||
|
||||
export async function generateOperatorFromDescription(
|
||||
description: string
|
||||
): Promise<GeneratedOperator> {
|
||||
const model = "gemini-3-flash-preview";
|
||||
|
||||
const systemInstruction = `
|
||||
You are an AI assistant helping developers register new operators for EmboFlow, an Embodied AI platform.
|
||||
Based on the user's description, generate the operator's metadata.
|
||||
|
||||
Categories: CV (Computer Vision), AI (Artificial Intelligence), IO (Input/Output), MATH (Mathematics).
|
||||
Port Types: json, image, video, tensor, file.
|
||||
Transmission Strategies: bus (for json), oss (for video/file), shm (for image/tensor), stream (for high-frequency tensor).
|
||||
|
||||
Output MUST be a valid JSON object with:
|
||||
- name: A concise name.
|
||||
- category: One of the categories above.
|
||||
- description: A clear 1-2 sentence description.
|
||||
- inputs: Array of { name, type, transmission }.
|
||||
- outputs: Array of { name, type, transmission }.
|
||||
- hyperparams: Array of { key, value, description } for K8s environment variables.
|
||||
- skillDefinition: A detailed Markdown description for other AIs to understand this operator.
|
||||
`;
|
||||
|
||||
const response = await ai.models.generateContent({
|
||||
model,
|
||||
contents: `User Request: ${description}`,
|
||||
config: {
|
||||
systemInstruction,
|
||||
responseMimeType: "application/json",
|
||||
responseSchema: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
name: { type: Type.STRING },
|
||||
category: { type: Type.STRING },
|
||||
description: { type: Type.STRING },
|
||||
inputs: {
|
||||
type: Type.ARRAY,
|
||||
items: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
name: { type: Type.STRING },
|
||||
type: { type: Type.STRING },
|
||||
transmission: { type: Type.STRING }
|
||||
},
|
||||
required: ["name", "type", "transmission"]
|
||||
}
|
||||
},
|
||||
outputs: {
|
||||
type: Type.ARRAY,
|
||||
items: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
name: { type: Type.STRING },
|
||||
type: { type: Type.STRING },
|
||||
transmission: { type: Type.STRING }
|
||||
},
|
||||
required: ["name", "type", "transmission"]
|
||||
}
|
||||
},
|
||||
hyperparams: {
|
||||
type: Type.ARRAY,
|
||||
items: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
key: { type: Type.STRING },
|
||||
value: { type: Type.STRING },
|
||||
description: { type: Type.STRING }
|
||||
},
|
||||
required: ["key", "value"]
|
||||
}
|
||||
},
|
||||
skillDefinition: { type: Type.STRING }
|
||||
},
|
||||
required: ["name", "category", "description", "inputs", "outputs", "hyperparams", "skillDefinition"]
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
const result = JSON.parse(response.text || "{}");
|
||||
return result as GeneratedOperator;
|
||||
} catch (error) {
|
||||
console.error("Failed to parse AI response:", error);
|
||||
throw new Error("AI generated an invalid operator structure.");
|
||||
}
|
||||
export async function generateOperatorFromDescription(description: string): Promise<GeneratedOperator> {
|
||||
return aiApi.generateOperator(description);
|
||||
}
|
||||
|
||||
55
src/services/api/ai.ts
Normal file
55
src/services/api/ai.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import { apiRequest } from "./client";
|
||||
|
||||
export interface AIStatus {
|
||||
enabled: boolean;
|
||||
provider: string;
|
||||
baseURL?: string;
|
||||
model?: string;
|
||||
reason?: string;
|
||||
}
|
||||
|
||||
export interface GeneratedWorkflow {
|
||||
name: string;
|
||||
description: string;
|
||||
nodes: any[];
|
||||
edges: any[];
|
||||
}
|
||||
|
||||
export interface GeneratedOperator {
|
||||
name: string;
|
||||
category: string;
|
||||
description: string;
|
||||
inputs: { name: string; type: string; transmission: string; description?: string }[];
|
||||
outputs: { name: string; type: string; transmission: string; description?: string }[];
|
||||
hyperparams: { key: string; value: string; description?: string; type?: string }[];
|
||||
skillDefinition: string;
|
||||
}
|
||||
|
||||
let statusCache: AIStatus | null = null;
|
||||
let statusPromise: Promise<AIStatus> | null = null;
|
||||
|
||||
async function loadStatus(force = false) {
|
||||
if (!force && statusCache) {
|
||||
return statusCache;
|
||||
}
|
||||
if (!force && statusPromise) {
|
||||
return statusPromise;
|
||||
}
|
||||
statusPromise = apiRequest<AIStatus>("/api/ai/status").then((status) => {
|
||||
statusCache = status;
|
||||
statusPromise = null;
|
||||
return status;
|
||||
}).catch((error) => {
|
||||
statusPromise = null;
|
||||
throw error;
|
||||
});
|
||||
return statusPromise;
|
||||
}
|
||||
|
||||
export const aiApi = {
|
||||
status: (force = false) => loadStatus(force),
|
||||
generateWorkflow: (description: string) =>
|
||||
apiRequest<GeneratedWorkflow>("/api/ai/workflows/generate", { method: "POST" }, { description }),
|
||||
generateOperator: (description: string) =>
|
||||
apiRequest<GeneratedOperator>("/api/ai/operators/generate", { method: "POST" }, { description }),
|
||||
};
|
||||
9
src/services/api/audit.ts
Normal file
9
src/services/api/audit.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import type { AuditTask } from "../../types";
|
||||
import { apiRequest } from "./client";
|
||||
|
||||
export const auditApi = {
|
||||
list: async (status: "all" | "pending" | "approved" | "rejected" = "all") =>
|
||||
(await apiRequest<{ items: AuditTask[] }>(`/api/audit-tasks?status=${status}`)).items,
|
||||
approve: (id: string) => apiRequest<AuditTask>(`/api/audit-tasks/${id}/approve`, { method: "POST" }),
|
||||
reject: (id: string) => apiRequest<AuditTask>(`/api/audit-tasks/${id}/reject`, { method: "POST" }),
|
||||
};
|
||||
54
src/services/api/client.ts
Normal file
54
src/services/api/client.ts
Normal file
@ -0,0 +1,54 @@
|
||||
const trimTrailingSlash = (value: string) => value.replace(/\/+$/, "");
|
||||
|
||||
const API_BASE_URL = trimTrailingSlash(import.meta.env.VITE_API_BASE_URL || "");
|
||||
|
||||
const buildUrl = (path: string) => `${API_BASE_URL}${path}`;
|
||||
|
||||
type JsonBody = Record<string, unknown> | unknown[] | undefined;
|
||||
|
||||
const RETRIABLE_STATUS = new Set([502, 503, 504]);
|
||||
|
||||
function isRetriableMethod(method?: string) {
|
||||
return !method || method.toUpperCase() === "GET";
|
||||
}
|
||||
|
||||
export async function apiRequest<T>(path: string, init?: RequestInit, body?: JsonBody): Promise<T> {
|
||||
const retriable = isRetriableMethod(init?.method);
|
||||
let lastError: Error | null = null;
|
||||
|
||||
for (let attempt = 1; attempt <= (retriable ? 4 : 1); attempt += 1) {
|
||||
try {
|
||||
const response = await fetch(buildUrl(path), {
|
||||
...init,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...(init?.headers ?? {}),
|
||||
},
|
||||
body: body === undefined ? init?.body : JSON.stringify(body),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text();
|
||||
if (retriable && RETRIABLE_STATUS.has(response.status) && attempt < 4) {
|
||||
await new Promise((resolve) => window.setTimeout(resolve, attempt * 500));
|
||||
continue;
|
||||
}
|
||||
throw new Error(text || `Request failed: ${response.status}`);
|
||||
}
|
||||
|
||||
if (response.status === 204) {
|
||||
return undefined as T;
|
||||
}
|
||||
|
||||
return (await response.json()) as T;
|
||||
} catch (error) {
|
||||
lastError = error instanceof Error ? error : new Error(String(error));
|
||||
if (!retriable || attempt === 4) {
|
||||
break;
|
||||
}
|
||||
await new Promise((resolve) => window.setTimeout(resolve, attempt * 500));
|
||||
}
|
||||
}
|
||||
|
||||
throw lastError || new Error("Request failed");
|
||||
}
|
||||
6
src/services/api/dashboard.ts
Normal file
6
src/services/api/dashboard.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import type { DashboardSummary } from "../../types";
|
||||
import { apiRequest } from "./client";
|
||||
|
||||
export const dashboardApi = {
|
||||
getSummary: () => apiRequest<DashboardSummary>("/api/dashboard"),
|
||||
};
|
||||
@ -1,103 +1,45 @@
|
||||
import { PortType } from "../../types";
|
||||
import type {
|
||||
ExecutionData,
|
||||
ExecutionRun,
|
||||
LogEntry,
|
||||
ResourceUsage,
|
||||
} from "../../types";
|
||||
import { apiRequest } from "./client";
|
||||
|
||||
export interface LogEntry {
|
||||
timestamp: string;
|
||||
level: "INFO" | "WARN" | "ERROR" | "DEBUG";
|
||||
message: string;
|
||||
operator?: string;
|
||||
}
|
||||
|
||||
export interface ResourceUsage {
|
||||
timestamp: string;
|
||||
cpu: number; // percentage
|
||||
memory: number; // MiB
|
||||
gpu?: number; // percentage
|
||||
}
|
||||
|
||||
export interface ExecutionData {
|
||||
id: string;
|
||||
name: string;
|
||||
type: PortType;
|
||||
size: string;
|
||||
path: string;
|
||||
metadata?: {
|
||||
duration?: string;
|
||||
resolution?: string;
|
||||
codec?: string;
|
||||
thumbnail?: string;
|
||||
};
|
||||
}
|
||||
|
||||
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
|
||||
export { type ExecutionData, type LogEntry, type ResourceUsage };
|
||||
|
||||
export const monitoringApi = {
|
||||
getLogs: async (runId: string): Promise<LogEntry[]> => {
|
||||
await sleep(500);
|
||||
return [
|
||||
{ timestamp: "2024-03-22 10:00:01", level: "INFO", message: `Initializing execution for ${runId}...`, operator: "System" },
|
||||
{ timestamp: "2024-03-22 10:00:05", level: "INFO", message: "Pulling docker image: registry.emboflow.ai/operators/cv:latest", operator: "K8s-Worker" },
|
||||
{ timestamp: "2024-03-22 10:01:12", level: "INFO", message: "Loading model weights into GPU memory...", operator: "YOLOv8-Inference" },
|
||||
{ timestamp: "2024-03-22 10:02:45", level: "DEBUG", message: "Batch processing started (size=32)", operator: "YOLOv8-Inference" },
|
||||
{ timestamp: "2024-03-22 10:03:10", level: "INFO", message: "Detected 15 objects in frame 0012", operator: "YOLOv8-Inference" },
|
||||
{ timestamp: "2024-03-22 10:04:55", level: "WARN", message: "High latency detected in OSS upload", operator: "Result-Uploader" },
|
||||
{ timestamp: "2024-03-22 10:05:20", level: "INFO", message: "Execution completed successfully", operator: "System" },
|
||||
];
|
||||
},
|
||||
|
||||
getResourceUsage: async (runId: string): Promise<ResourceUsage[]> => {
|
||||
await sleep(400);
|
||||
return Array.from({ length: 10 }).map((_, i) => ({
|
||||
timestamp: `10:0${i}:00`,
|
||||
cpu: 45 + Math.random() * 30,
|
||||
memory: 1024 + Math.random() * 512,
|
||||
gpu: 60 + Math.random() * 20,
|
||||
}));
|
||||
},
|
||||
|
||||
getExecutionData: async (runId: string): Promise<ExecutionData[]> => {
|
||||
await sleep(600);
|
||||
return [
|
||||
{
|
||||
id: "d1",
|
||||
name: "raw_camera_feed_01.mp4",
|
||||
type: "video",
|
||||
size: "4.2 GB",
|
||||
path: "oss://emboflow-bucket/runs/run-4/raw/cam_01.mp4",
|
||||
metadata: { duration: "15:00", resolution: "4K", codec: "H.264", thumbnail: "https://picsum.photos/seed/cam1/320/180" }
|
||||
},
|
||||
{
|
||||
id: "d2",
|
||||
name: "raw_camera_feed_02.mp4",
|
||||
type: "video",
|
||||
size: "3.8 GB",
|
||||
path: "oss://emboflow-bucket/runs/run-4/raw/cam_02.mp4",
|
||||
metadata: { duration: "15:00", resolution: "4K", codec: "H.264", thumbnail: "https://picsum.photos/seed/cam2/320/180" }
|
||||
},
|
||||
{
|
||||
id: "d3",
|
||||
name: "detection_overlay_final.mp4",
|
||||
type: "video",
|
||||
size: "850 MB",
|
||||
path: "oss://emboflow-bucket/runs/run-4/processed/overlay.mp4",
|
||||
metadata: { duration: "15:00", resolution: "1080p", codec: "H.265", thumbnail: "https://picsum.photos/seed/overlay/320/180" }
|
||||
},
|
||||
{ id: "d4", name: "inference_results.json", type: "json", size: "12 MB", path: "oss://emboflow-bucket/runs/run-4/results.json" },
|
||||
];
|
||||
},
|
||||
|
||||
listOssBuckets: async (): Promise<string[]> => {
|
||||
await sleep(300);
|
||||
return ["emboflow-raw-data", "emboflow-models", "emboflow-results", "user-eustance-bucket"];
|
||||
},
|
||||
|
||||
listOssObjects: async (bucket: string, prefix: string): Promise<string[]> => {
|
||||
await sleep(400);
|
||||
return [
|
||||
`${prefix}video_01.mp4`,
|
||||
`${prefix}video_02.mp4`,
|
||||
`${prefix}metadata.json`,
|
||||
`${prefix}labels/`,
|
||||
`${prefix}processed/`
|
||||
];
|
||||
}
|
||||
listRuns: async () =>
|
||||
(await apiRequest<{ items: ExecutionRun[] }>("/api/runs")).items,
|
||||
getRun: (runId: string) => apiRequest<ExecutionRun>(`/api/runs/${runId}`),
|
||||
getLogs: async (runId: string): Promise<LogEntry[]> =>
|
||||
(await apiRequest<{ items: LogEntry[] }>(`/api/runs/${runId}/logs`)).items,
|
||||
getResourceUsage: async (runId: string): Promise<ResourceUsage[]> =>
|
||||
(
|
||||
await apiRequest<{ items: ResourceUsage[] }>(
|
||||
`/api/runs/${runId}/resources`,
|
||||
)
|
||||
).items,
|
||||
getExecutionData: async (runId: string): Promise<ExecutionData[]> =>
|
||||
(
|
||||
await apiRequest<{ items: ExecutionData[] }>(
|
||||
`/api/runs/${runId}/artifacts`,
|
||||
)
|
||||
).items,
|
||||
listOssBuckets: async (sourcePath: string): Promise<string[]> =>
|
||||
(
|
||||
await apiRequest<{ items: string[] }>(
|
||||
`/api/storage/buckets?sourcePath=${encodeURIComponent(sourcePath)}`,
|
||||
)
|
||||
).items,
|
||||
listOssObjects: async (
|
||||
sourcePath: string,
|
||||
bucket: string,
|
||||
prefix: string,
|
||||
): Promise<string[]> =>
|
||||
(
|
||||
await apiRequest<{ items: string[] }>(
|
||||
`/api/storage/objects?sourcePath=${encodeURIComponent(sourcePath)}&bucket=${encodeURIComponent(bucket)}&prefix=${encodeURIComponent(prefix)}`,
|
||||
)
|
||||
).items,
|
||||
};
|
||||
|
||||
11
src/services/api/operators.ts
Normal file
11
src/services/api/operators.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import type { Operator } from "../../types";
|
||||
import { apiRequest } from "./client";
|
||||
|
||||
export const operatorsApi = {
|
||||
list: async () => (await apiRequest<{ items: Operator[] }>("/api/operators")).items,
|
||||
create: (payload: Partial<Operator>) =>
|
||||
apiRequest<Operator>("/api/operators", { method: "POST" }, payload),
|
||||
update: (id: string, payload: Partial<Operator>) =>
|
||||
apiRequest<Operator>(`/api/operators/${id}`, { method: "PUT" }, payload),
|
||||
remove: (id: string) => apiRequest<void>(`/api/operators/${id}`, { method: "DELETE" }),
|
||||
};
|
||||
16
src/services/api/projects.ts
Normal file
16
src/services/api/projects.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import type { Project, Workflow } from "../../types";
|
||||
import { apiRequest } from "./client";
|
||||
|
||||
export const projectsApi = {
|
||||
list: async () => (await apiRequest<{ items: Project[] }>("/api/projects")).items,
|
||||
get: (id: string) => apiRequest<Project>(`/api/projects/${id}`),
|
||||
create: (payload: { name: string; description: string }) =>
|
||||
apiRequest<Project>("/api/projects", { method: "POST" }, payload),
|
||||
update: (id: string, payload: Partial<Project>) =>
|
||||
apiRequest<Project>(`/api/projects/${id}`, { method: "PUT" }, payload),
|
||||
remove: (id: string) => apiRequest<void>(`/api/projects/${id}`, { method: "DELETE" }),
|
||||
listWorkflows: async (projectId: string) =>
|
||||
(await apiRequest<{ items: Workflow[] }>(`/api/projects/${projectId}/workflows`)).items,
|
||||
createWorkflow: (projectId: string, payload: Partial<Workflow>) =>
|
||||
apiRequest<Workflow>(`/api/projects/${projectId}/workflows`, { method: "POST" }, payload),
|
||||
};
|
||||
8
src/services/api/settings.ts
Normal file
8
src/services/api/settings.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import type { SystemConfig } from "../../types";
|
||||
import { apiRequest } from "./client";
|
||||
|
||||
export const settingsApi = {
|
||||
get: () => apiRequest<SystemConfig>("/api/settings"),
|
||||
update: (payload: Partial<SystemConfig>) =>
|
||||
apiRequest<SystemConfig>("/api/settings", { method: "PUT" }, payload),
|
||||
};
|
||||
12
src/services/api/templates.ts
Normal file
12
src/services/api/templates.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import type { WorkflowTemplate } from "../../types";
|
||||
import { apiRequest } from "./client";
|
||||
|
||||
export const templatesApi = {
|
||||
list: async () => (await apiRequest<{ items: WorkflowTemplate[] }>("/api/templates")).items,
|
||||
get: (id: string) => apiRequest<WorkflowTemplate>(`/api/templates/${id}`),
|
||||
create: (payload: Partial<WorkflowTemplate>) =>
|
||||
apiRequest<WorkflowTemplate>("/api/templates", { method: "POST" }, payload),
|
||||
update: (id: string, payload: Partial<WorkflowTemplate>) =>
|
||||
apiRequest<WorkflowTemplate>(`/api/templates/${id}`, { method: "PUT" }, payload),
|
||||
remove: (id: string) => apiRequest<void>(`/api/templates/${id}`, { method: "DELETE" }),
|
||||
};
|
||||
11
src/services/api/users.ts
Normal file
11
src/services/api/users.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import type { User } from "../../types";
|
||||
import { apiRequest } from "./client";
|
||||
|
||||
export const usersApi = {
|
||||
list: async () => (await apiRequest<{ items: User[] }>("/api/users")).items,
|
||||
create: (payload: Pick<User, "name" | "email" | "role">) =>
|
||||
apiRequest<User>("/api/users", { method: "POST" }, payload),
|
||||
update: (id: string, payload: Partial<User>) =>
|
||||
apiRequest<User>(`/api/users/${id}`, { method: "PUT" }, payload),
|
||||
remove: (id: string) => apiRequest<void>(`/api/users/${id}`, { method: "DELETE" }),
|
||||
};
|
||||
11
src/services/api/workflows.ts
Normal file
11
src/services/api/workflows.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import type { ExecutionRun, Workflow } from "../../types";
|
||||
import { apiRequest } from "./client";
|
||||
|
||||
export const workflowsApi = {
|
||||
get: (id: string) => apiRequest<Workflow>(`/api/workflows/${id}`),
|
||||
update: (id: string, payload: Partial<Workflow>) =>
|
||||
apiRequest<Workflow>(`/api/workflows/${id}`, { method: "PUT" }, payload),
|
||||
remove: (id: string) => apiRequest<void>(`/api/workflows/${id}`, { method: "DELETE" }),
|
||||
run: (id: string, trigger: ExecutionRun["trigger"] = "manual") =>
|
||||
apiRequest<ExecutionRun>(`/api/workflows/${id}/run`, { method: "POST" }, { trigger }),
|
||||
};
|
||||
@ -1,27 +0,0 @@
|
||||
import { GoogleGenAI } from "@google/genai";
|
||||
|
||||
async function generateLogo() {
|
||||
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
|
||||
const response = await ai.models.generateContent({
|
||||
model: 'gemini-2.5-flash-image',
|
||||
contents: {
|
||||
parts: [
|
||||
{
|
||||
text: 'A modern, minimalist app icon for a platform called EmboFlow. The icon should feature a stylized robot head integrated with the letter "D" and flow lines representing data connectivity. Professional blue and dark gray color palette, high-tech aesthetic, vector style, clean lines, flat design, suitable for a software logo.',
|
||||
},
|
||||
],
|
||||
},
|
||||
config: {
|
||||
imageConfig: {
|
||||
aspectRatio: "1:1",
|
||||
imageSize: "1K"
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
for (const part of response.candidates[0].content.parts) {
|
||||
if (part.inlineData) {
|
||||
return `data:image/png;base64,${part.inlineData.data}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
181
src/types.ts
181
src/types.ts
@ -5,13 +5,18 @@ export interface User {
|
||||
name: string;
|
||||
email: string;
|
||||
role: UserRole;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
export interface Project {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string;
|
||||
status?: "active" | "archived";
|
||||
workflowCount?: number;
|
||||
createdAt: string;
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
export type PortType = "json" | "image" | "video" | "tensor" | "file";
|
||||
@ -60,26 +65,192 @@ export interface Workflow {
|
||||
name: string;
|
||||
description: string;
|
||||
type: WorkflowType;
|
||||
maxConcurrentRuns?: number;
|
||||
nodeTimeoutSeconds?: number;
|
||||
runTimeoutSeconds?: number;
|
||||
maxRetryAttempts?: number;
|
||||
retryBackoffSeconds?: number;
|
||||
schedule?: WorkflowSchedule;
|
||||
nodes: any[];
|
||||
edges: any[];
|
||||
status: WorkflowStatus;
|
||||
lastRun?: string;
|
||||
lastRunId?: string;
|
||||
lastRunAt?: string;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
export interface WorkflowTemplate {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string;
|
||||
type: WorkflowType;
|
||||
maxConcurrentRuns?: number;
|
||||
nodeTimeoutSeconds?: number;
|
||||
runTimeoutSeconds?: number;
|
||||
maxRetryAttempts?: number;
|
||||
retryBackoffSeconds?: number;
|
||||
schedule?: WorkflowSchedule;
|
||||
nodes: any[];
|
||||
edges: any[];
|
||||
createdAt: string;
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
export interface SystemConfig {
|
||||
k8sCluster: string;
|
||||
defaultCpu: string;
|
||||
defaultMemory: string;
|
||||
ossEndpoint: string;
|
||||
ossBucket: string;
|
||||
id?: string;
|
||||
k8s: {
|
||||
enabled: boolean;
|
||||
endpoint?: string;
|
||||
namespace: string;
|
||||
kubeconfig?: string;
|
||||
kubeconfigPreview?: string;
|
||||
hasKubeconfig?: boolean;
|
||||
};
|
||||
worker: {
|
||||
pollIntervalMs: number;
|
||||
maxConcurrentRuns: number;
|
||||
};
|
||||
ai: {
|
||||
provider: string;
|
||||
baseURL: string;
|
||||
model: string;
|
||||
apiKey?: string;
|
||||
apiKeyPreview?: string;
|
||||
hasAPIKey?: boolean;
|
||||
};
|
||||
defaults: {
|
||||
cpuRequest: string;
|
||||
cpuLimit: string;
|
||||
memoryRequest: string;
|
||||
memoryLimit: string;
|
||||
};
|
||||
timezone: string;
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
export interface DashboardSummary {
|
||||
projects: {
|
||||
active: number;
|
||||
archived: number;
|
||||
};
|
||||
operators: {
|
||||
total: number;
|
||||
custom: number;
|
||||
};
|
||||
jobs: {
|
||||
today: number;
|
||||
successRate: number;
|
||||
running: number;
|
||||
failed: number;
|
||||
averageDurationMs: number;
|
||||
};
|
||||
audits: {
|
||||
pending: number;
|
||||
};
|
||||
}
|
||||
|
||||
export interface LogEntry {
|
||||
timestamp: string;
|
||||
level: "INFO" | "WARN" | "ERROR" | "DEBUG";
|
||||
message: string;
|
||||
operator?: string;
|
||||
}
|
||||
|
||||
export interface ResourceUsage {
|
||||
timestamp: string;
|
||||
cpu: number;
|
||||
memory: number;
|
||||
gpu?: number;
|
||||
}
|
||||
|
||||
export interface ExecutionData {
|
||||
id: string;
|
||||
runId: string;
|
||||
nodeId: string;
|
||||
portId?: string;
|
||||
portName?: string;
|
||||
name: string;
|
||||
type: PortType;
|
||||
size: string;
|
||||
path: string;
|
||||
metadata?: {
|
||||
duration?: string;
|
||||
resolution?: string;
|
||||
codec?: string;
|
||||
thumbnail?: string;
|
||||
mimeType?: string;
|
||||
sha256?: string;
|
||||
};
|
||||
data?: Record<string, any>;
|
||||
}
|
||||
|
||||
export type NodeExecutionStatus =
|
||||
| "queued"
|
||||
| "running"
|
||||
| "waiting_for_audit"
|
||||
| "completed"
|
||||
| "failed";
|
||||
|
||||
export interface NodeExecution {
|
||||
nodeId: string;
|
||||
nodeLabel: string;
|
||||
status: NodeExecutionStatus;
|
||||
startedAt?: string;
|
||||
endedAt?: string;
|
||||
logs: LogEntry[];
|
||||
artifacts: ExecutionData[];
|
||||
resourceSeries: ResourceUsage[];
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface ExecutionRun {
|
||||
id: string;
|
||||
workflowId: string;
|
||||
workflowName: string;
|
||||
projectId: string;
|
||||
projectName: string;
|
||||
trigger: "manual" | "scheduled" | "retry";
|
||||
attempt?: number;
|
||||
parentRunId?: string;
|
||||
availableAt?: string;
|
||||
status:
|
||||
| "queued"
|
||||
| "running"
|
||||
| "waiting_for_audit"
|
||||
| "completed"
|
||||
| "failed"
|
||||
| "cancelled";
|
||||
startedAt?: string;
|
||||
endedAt?: string;
|
||||
durationMs?: number;
|
||||
currentNodeId?: string;
|
||||
nodeExecutions: NodeExecution[];
|
||||
logs: LogEntry[];
|
||||
resourceSeries: ResourceUsage[];
|
||||
artifacts: ExecutionData[];
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface AuditTask {
|
||||
id: string;
|
||||
runId: string;
|
||||
workflowId: string;
|
||||
projectId: string;
|
||||
nodeId: string;
|
||||
workflowName: string;
|
||||
projectName: string;
|
||||
operatorName: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
status: "pending" | "approved" | "rejected";
|
||||
data: {
|
||||
image: string;
|
||||
confidence: number;
|
||||
detectedObjects: string[];
|
||||
reason: string;
|
||||
};
|
||||
}
|
||||
|
||||
79
tests/frontend/clipboard.test.mjs
Normal file
79
tests/frontend/clipboard.test.mjs
Normal file
@ -0,0 +1,79 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import { copyText } from "../../src/lib/clipboard.js";
|
||||
|
||||
test("uses clipboard API when available", async () => {
|
||||
let copied = "";
|
||||
|
||||
const result = await copyText("payload-path", {
|
||||
navigator: {
|
||||
clipboard: {
|
||||
async writeText(text) {
|
||||
copied = text;
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
assert.equal(result, true);
|
||||
assert.equal(copied, "payload-path");
|
||||
});
|
||||
|
||||
test("falls back to execCommand when clipboard API is unavailable", async () => {
|
||||
let execCommandCalls = 0;
|
||||
let appendedNode = null;
|
||||
let removedNode = null;
|
||||
|
||||
const textarea = {
|
||||
value: "",
|
||||
style: {},
|
||||
setAttribute(name, value) {
|
||||
this[name] = value;
|
||||
},
|
||||
focusCalled: false,
|
||||
selectCalled: false,
|
||||
selectionRange: null,
|
||||
focus() {
|
||||
this.focusCalled = true;
|
||||
},
|
||||
select() {
|
||||
this.selectCalled = true;
|
||||
},
|
||||
setSelectionRange(start, end) {
|
||||
this.selectionRange = [start, end];
|
||||
},
|
||||
};
|
||||
|
||||
const result = await copyText("artifact-path", {
|
||||
navigator: {},
|
||||
document: {
|
||||
createElement(tagName) {
|
||||
assert.equal(tagName, "textarea");
|
||||
return textarea;
|
||||
},
|
||||
body: {
|
||||
appendChild(node) {
|
||||
appendedNode = node;
|
||||
},
|
||||
removeChild(node) {
|
||||
removedNode = node;
|
||||
},
|
||||
},
|
||||
execCommand(command) {
|
||||
execCommandCalls += 1;
|
||||
assert.equal(command, "copy");
|
||||
return true;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
assert.equal(result, true);
|
||||
assert.equal(execCommandCalls, 1);
|
||||
assert.equal(textarea.value, "artifact-path");
|
||||
assert.equal(textarea.focusCalled, true);
|
||||
assert.equal(textarea.selectCalled, true);
|
||||
assert.deepEqual(textarea.selectionRange, [0, "artifact-path".length]);
|
||||
assert.equal(appendedNode, textarea);
|
||||
assert.equal(removedNode, textarea);
|
||||
});
|
||||
37
tests/frontend/operator-library.test.mjs
Normal file
37
tests/frontend/operator-library.test.mjs
Normal file
@ -0,0 +1,37 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import { getOperatorLibrarySections } from "../../src/lib/operator-library.js";
|
||||
|
||||
test("operator library keeps every category returned by the api", () => {
|
||||
const sections = getOperatorLibrarySections([
|
||||
{ id: "1", name: "Scan", category: "IO" },
|
||||
{ id: "2", name: "Filter", category: "DATA" },
|
||||
{ id: "3", name: "Shell", category: "UTIL" },
|
||||
{ id: "4", name: "Route", category: "LOGIC" },
|
||||
{ id: "5", name: "Review", category: "HUMAN" },
|
||||
]);
|
||||
|
||||
assert.deepEqual(
|
||||
sections.map((section) => section.category),
|
||||
["IO", "DATA", "UTIL", "LOGIC", "HUMAN"],
|
||||
);
|
||||
assert.equal(
|
||||
sections.reduce((count, section) => count + section.operators.length, 0),
|
||||
5,
|
||||
);
|
||||
});
|
||||
|
||||
test("operator library uses preferred ordering for known categories", () => {
|
||||
const sections = getOperatorLibrarySections([
|
||||
{ id: "1", name: "Logic", category: "LOGIC" },
|
||||
{ id: "2", name: "AI", category: "AI" },
|
||||
{ id: "3", name: "IO", category: "IO" },
|
||||
{ id: "4", name: "Math", category: "MATH" },
|
||||
]);
|
||||
|
||||
assert.deepEqual(
|
||||
sections.map((section) => section.category),
|
||||
["IO", "AI", "LOGIC", "MATH"],
|
||||
);
|
||||
});
|
||||
76
tests/frontend/settings-secrets.test.mjs
Normal file
76
tests/frontend/settings-secrets.test.mjs
Normal file
@ -0,0 +1,76 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import { buildSettingsUpdatePayload } from "../../src/lib/settings.js";
|
||||
|
||||
test("does not resend stored secrets when secret inputs are blank", () => {
|
||||
const payload = buildSettingsUpdatePayload({
|
||||
id: "settings-1",
|
||||
k8s: {
|
||||
enabled: true,
|
||||
endpoint: "https://cluster.example.com",
|
||||
namespace: "droboticflow",
|
||||
kubeconfig: "",
|
||||
hasKubeconfig: true,
|
||||
kubeconfigPreview: "apiV****ig==",
|
||||
},
|
||||
worker: {
|
||||
pollIntervalMs: 2000,
|
||||
maxConcurrentRuns: 4,
|
||||
},
|
||||
ai: {
|
||||
provider: "dashscope-openai-compatible",
|
||||
baseURL: "https://coding.dashscope.aliyuncs.com/v1",
|
||||
model: "glm-5",
|
||||
apiKey: "",
|
||||
hasAPIKey: true,
|
||||
apiKeyPreview: "sk-t****1234",
|
||||
},
|
||||
defaults: {
|
||||
cpuRequest: "500m",
|
||||
cpuLimit: "1000m",
|
||||
memoryRequest: "1Gi",
|
||||
memoryLimit: "2Gi",
|
||||
},
|
||||
timezone: "Asia/Shanghai",
|
||||
});
|
||||
|
||||
assert.equal("kubeconfig" in payload.k8s, false);
|
||||
assert.equal("kubeconfigPreview" in payload.k8s, false);
|
||||
assert.equal("apiKey" in payload.ai, false);
|
||||
assert.equal("apiKeyPreview" in payload.ai, false);
|
||||
});
|
||||
|
||||
test("sends new secrets when the user provides replacement values", () => {
|
||||
const payload = buildSettingsUpdatePayload({
|
||||
id: "settings-1",
|
||||
k8s: {
|
||||
enabled: true,
|
||||
endpoint: "https://cluster.example.com",
|
||||
namespace: "droboticflow",
|
||||
kubeconfig: " apiVersion: v1\nkind: Config ",
|
||||
hasKubeconfig: true,
|
||||
},
|
||||
worker: {
|
||||
pollIntervalMs: 2000,
|
||||
maxConcurrentRuns: 4,
|
||||
},
|
||||
ai: {
|
||||
provider: "dashscope-openai-compatible",
|
||||
baseURL: "https://coding.dashscope.aliyuncs.com/v1",
|
||||
model: "glm-5",
|
||||
apiKey: " new-key ",
|
||||
hasAPIKey: true,
|
||||
},
|
||||
defaults: {
|
||||
cpuRequest: "500m",
|
||||
cpuLimit: "1000m",
|
||||
memoryRequest: "1Gi",
|
||||
memoryLimit: "2Gi",
|
||||
},
|
||||
timezone: "Asia/Shanghai",
|
||||
});
|
||||
|
||||
assert.equal(payload.k8s.kubeconfig, "apiVersion: v1\nkind: Config");
|
||||
assert.equal(payload.ai.apiKey, "new-key");
|
||||
});
|
||||
85
tests/frontend/template-settings.test.mjs
Normal file
85
tests/frontend/template-settings.test.mjs
Normal file
@ -0,0 +1,85 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import {
|
||||
buildTemplatePayload,
|
||||
buildWorkflowFromTemplate,
|
||||
} from "../../src/lib/templates.js";
|
||||
|
||||
test("buildWorkflowFromTemplate preserves workflow-level execution config", () => {
|
||||
const payload = buildWorkflowFromTemplate({
|
||||
name: "视频审核模板",
|
||||
description: "template description",
|
||||
type: "streaming",
|
||||
maxConcurrentRuns: 2,
|
||||
nodeTimeoutSeconds: 12,
|
||||
runTimeoutSeconds: 34,
|
||||
maxRetryAttempts: 1,
|
||||
retryBackoffSeconds: 56,
|
||||
schedule: {
|
||||
interval: 90,
|
||||
sourcePath: "oss://incoming/videos",
|
||||
},
|
||||
nodes: [{ id: "node-1" }],
|
||||
edges: [{ id: "edge-1" }],
|
||||
});
|
||||
|
||||
assert.deepEqual(payload, {
|
||||
name: "视频审核模板 实例",
|
||||
description: "template description",
|
||||
type: "streaming",
|
||||
maxConcurrentRuns: 2,
|
||||
nodeTimeoutSeconds: 12,
|
||||
runTimeoutSeconds: 34,
|
||||
maxRetryAttempts: 1,
|
||||
retryBackoffSeconds: 56,
|
||||
schedule: {
|
||||
interval: 90,
|
||||
sourcePath: "oss://incoming/videos",
|
||||
},
|
||||
nodes: [{ id: "node-1" }],
|
||||
edges: [{ id: "edge-1" }],
|
||||
});
|
||||
});
|
||||
|
||||
test("buildTemplatePayload carries workflow-level config into templates", () => {
|
||||
const payload = buildTemplatePayload(
|
||||
{
|
||||
name: "实时监听工作流",
|
||||
description: "workflow description",
|
||||
type: "streaming",
|
||||
maxConcurrentRuns: 3,
|
||||
nodeTimeoutSeconds: 22,
|
||||
runTimeoutSeconds: 44,
|
||||
maxRetryAttempts: 2,
|
||||
retryBackoffSeconds: 11,
|
||||
schedule: {
|
||||
interval: 30,
|
||||
sourcePath: "oss://stream",
|
||||
},
|
||||
nodes: [{ id: "node-a" }],
|
||||
edges: [],
|
||||
},
|
||||
{
|
||||
name: "实时监听模板",
|
||||
description: "template description",
|
||||
},
|
||||
);
|
||||
|
||||
assert.deepEqual(payload, {
|
||||
name: "实时监听模板",
|
||||
description: "template description",
|
||||
type: "streaming",
|
||||
maxConcurrentRuns: 3,
|
||||
nodeTimeoutSeconds: 22,
|
||||
runTimeoutSeconds: 44,
|
||||
maxRetryAttempts: 2,
|
||||
retryBackoffSeconds: 11,
|
||||
schedule: {
|
||||
interval: 30,
|
||||
sourcePath: "oss://stream",
|
||||
},
|
||||
nodes: [{ id: "node-a" }],
|
||||
edges: [],
|
||||
});
|
||||
});
|
||||
75
tests/frontend/workflow-canvas.test.mjs
Normal file
75
tests/frontend/workflow-canvas.test.mjs
Normal file
@ -0,0 +1,75 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import {
|
||||
canConnectToInput,
|
||||
removeSelectedCanvasElements,
|
||||
} from "../../src/lib/workflow-canvas.js";
|
||||
|
||||
test("input handle only accepts a single incoming edge", () => {
|
||||
const edges = [
|
||||
{
|
||||
id: "edge-a",
|
||||
source: "source-a",
|
||||
sourceHandle: "out-1",
|
||||
target: "node-b",
|
||||
targetHandle: "in-1",
|
||||
},
|
||||
];
|
||||
|
||||
assert.equal(
|
||||
canConnectToInput(edges, {
|
||||
source: "source-c",
|
||||
sourceHandle: "out-1",
|
||||
target: "node-b",
|
||||
targetHandle: "in-1",
|
||||
}),
|
||||
false,
|
||||
);
|
||||
|
||||
assert.equal(
|
||||
canConnectToInput(edges, {
|
||||
source: "source-c",
|
||||
sourceHandle: "out-1",
|
||||
target: "node-b",
|
||||
targetHandle: "in-2",
|
||||
}),
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
test("removing selected nodes also removes incident edges", () => {
|
||||
const result = removeSelectedCanvasElements(
|
||||
[
|
||||
{ id: "node-a", selected: true },
|
||||
{ id: "node-b", selected: false },
|
||||
],
|
||||
[
|
||||
{ id: "edge-a", source: "node-a", target: "node-b", selected: false },
|
||||
{ id: "edge-b", source: "node-b", target: "node-a", selected: false },
|
||||
],
|
||||
);
|
||||
|
||||
assert.deepEqual(result.removedNodeIds, ["node-a"]);
|
||||
assert.deepEqual(result.nodes, [{ id: "node-b", selected: false }]);
|
||||
assert.deepEqual(result.edges, []);
|
||||
});
|
||||
|
||||
test("removing selected edges leaves nodes intact", () => {
|
||||
const result = removeSelectedCanvasElements(
|
||||
[
|
||||
{ id: "node-a", selected: false },
|
||||
{ id: "node-b", selected: false },
|
||||
],
|
||||
[
|
||||
{ id: "edge-a", source: "node-a", target: "node-b", selected: true },
|
||||
{ id: "edge-b", source: "node-b", target: "node-a", selected: false },
|
||||
],
|
||||
);
|
||||
|
||||
assert.deepEqual(result.removedEdgeIds, ["edge-a"]);
|
||||
assert.equal(result.nodes.length, 2);
|
||||
assert.deepEqual(result.edges, [
|
||||
{ id: "edge-b", source: "node-b", target: "node-a", selected: false },
|
||||
]);
|
||||
});
|
||||
72
tests/frontend/workflow-editor.test.mjs
Normal file
72
tests/frontend/workflow-editor.test.mjs
Normal file
@ -0,0 +1,72 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import {
|
||||
getWorkflowRunActionState,
|
||||
isWorkflowSettingsLocked,
|
||||
} from "../../src/lib/workflow-editor.js";
|
||||
|
||||
test("completed workflows cannot be run manually again", () => {
|
||||
const action = getWorkflowRunActionState({
|
||||
isTemplate: false,
|
||||
isReadOnly: true,
|
||||
workflowStatus: "completed",
|
||||
isExecuting: false,
|
||||
activeRunInFlight: false,
|
||||
});
|
||||
|
||||
assert.equal(action.disabled, true);
|
||||
assert.equal(action.reason, "completed");
|
||||
assert.equal(action.label, "已成功运行");
|
||||
});
|
||||
|
||||
test("active runs take precedence over readonly button labels", () => {
|
||||
const action = getWorkflowRunActionState({
|
||||
isTemplate: false,
|
||||
isReadOnly: true,
|
||||
workflowStatus: "running",
|
||||
isExecuting: false,
|
||||
activeRunInFlight: true,
|
||||
});
|
||||
|
||||
assert.equal(action.disabled, true);
|
||||
assert.equal(action.reason, "running");
|
||||
assert.equal(action.label, "工作流运行中");
|
||||
});
|
||||
|
||||
test("workflow status running also disables the run action", () => {
|
||||
const action = getWorkflowRunActionState({
|
||||
isTemplate: false,
|
||||
isReadOnly: false,
|
||||
workflowStatus: "running",
|
||||
isExecuting: false,
|
||||
activeRunInFlight: false,
|
||||
});
|
||||
|
||||
assert.equal(action.disabled, true);
|
||||
assert.equal(action.reason, "running");
|
||||
});
|
||||
|
||||
test("workflow settings lock for readonly and completed workflows", () => {
|
||||
assert.equal(
|
||||
isWorkflowSettingsLocked({
|
||||
isReadOnly: false,
|
||||
workflowStatus: "completed",
|
||||
}),
|
||||
true,
|
||||
);
|
||||
assert.equal(
|
||||
isWorkflowSettingsLocked({
|
||||
isReadOnly: true,
|
||||
workflowStatus: "draft",
|
||||
}),
|
||||
true,
|
||||
);
|
||||
assert.equal(
|
||||
isWorkflowSettingsLocked({
|
||||
isReadOnly: false,
|
||||
workflowStatus: "draft",
|
||||
}),
|
||||
false,
|
||||
);
|
||||
});
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user