fix(api): wire batch import create-run entry pipeline

This commit is contained in:
phamnazage-jpg
2026-05-22 16:12:52 +08:00
parent 7b8959d723
commit eac860e72f
3 changed files with 661 additions and 64 deletions

View File

@@ -2,13 +2,9 @@ package app
import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"sub2api-cn-relay-manager/internal/batch"
"sub2api-cn-relay-manager/internal/store/sqlite"
@@ -118,52 +114,17 @@ func buildCreateBatchImportRunAction(sqliteDSN string) func(context.Context, Cre
}
defer store.Close()
runID := fmt.Sprintf("run_%d", time.Now().UnixNano())
run := sqlite.ImportRun{
RunID: runID,
Mode: strings.TrimSpace(req.Mode),
AccessMode: strings.TrimSpace(req.AccessMode),
State: string(batch.RunStateRunning),
TotalItems: len(req.Entries),
}
if err := store.ImportRuns().Create(ctx, run); err != nil {
hostRow, client, err := resolveManagedHost(ctx, store, req.HostID, "", CreateHostAuth{})
if err != nil {
return BatchImportRunCreateResponse{}, err
}
for idx, entry := range req.Entries {
item := sqlite.ImportRunItem{
ItemID: fmt.Sprintf("%s-item-%d", runID, idx+1),
RunID: runID,
BaseURL: strings.TrimSpace(entry.BaseURL),
ProviderID: batch.NormalizeProviderID(entry.BaseURL),
APIKeyFingerprint: fingerprintBatchAPIKey(entry.APIKey),
RequestedModelsJSON: mustMarshalAppJSON(entry.RequestedModels, "[]"),
CurrentStage: string(batch.ItemStageProbe),
ConfirmationStatus: string(batch.ConfirmationPending),
AccessStatus: string(batch.AccessStatusUnknown),
MatchedAccountState: string(batch.MatchedAccountStateNone),
AccountResolution: string(batch.AccountResolutionCreated),
ProvisionReused: false,
CanonicalFamiliesJSON: "[]",
RawModelsJSON: "[]",
NormalizedModelsJSON: "[]",
RecommendedModelsJSON: "[]",
}
if err := store.ImportRunItems().Upsert(ctx, item); err != nil {
return BatchImportRunCreateResponse{}, err
}
runner := batchImportRuntimeRunner{
store: store,
hostRow: hostRow,
hostClient: client,
request: req,
}
return BatchImportRunCreateResponse{
RunID: runID,
State: string(batch.RunStateRunning),
ResultPage: "/batch-import/runs/" + runID,
TotalItems: len(req.Entries),
ActiveItems: 0,
DegradedItems: 0,
BrokenItems: 0,
WarningItems: 0,
}, nil
return runner.execute(ctx)
}
}
@@ -242,20 +203,3 @@ func defaultPositiveInt(value, fallback int) int {
}
return fallback
}
func fingerprintBatchAPIKey(apiKey string) string {
trimmed := strings.TrimSpace(apiKey)
if trimmed == "" {
return ""
}
sum := sha256.Sum256([]byte(trimmed))
return fmt.Sprintf("sha256:%x", sum[:4])
}
func mustMarshalAppJSON(value any, fallback string) string {
payload, err := json.Marshal(value)
if err != nil {
return fallback
}
return string(payload)
}