Skip to content

Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.

feat: add pipeline response types

wulienda99491a97b8
diff --git a/internal/pipeline/types.go b/internal/pipeline/types.go
new file mode 100644
index 00000000..91ae4b05
--- /dev/null
+++ b/internal/pipeline/types.go
@@ -0,0 +1,102 @@
+package pipeline
+
+type Stage string
+
+const (
+ StageSubtitle Stage = "subtitle"
+ StageTTS Stage = "tts"
+ StageRenderHorizontal Stage = "render-horizontal"
+ StageRenderVertical Stage = "render-vertical"
+ StageCover Stage = "cover"
+ StagePipeline Stage = "pipeline"
+)
+
+type CaptionSource string
+
+const (
+ CaptionSourceAny CaptionSource = "any"
+ CaptionSourceManual CaptionSource = "manual"
+ CaptionSourceAuto CaptionSource = "auto"
+ CaptionSourceWhisper CaptionSource = "whisper"
+)
+
+type LineMode string
+
+const (
+ LineModeTargetOnly LineMode = "target-only"
+ LineModeBilingualTargetTop LineMode = "bilingual-target-top"
+ LineModeBilingualTargetBottom LineMode = "bilingual-target-bottom"
+)
+
+type ErrorKind string
+
+const (
+ ErrorKindUsage ErrorKind = "usage"
+ ErrorKindRetryable ErrorKind = "retryable"
+ ErrorKindDependency ErrorKind = "dependency"
+ ErrorKindInternal ErrorKind = "internal"
+)
+
+type Error struct {
+ Kind ErrorKind `json:"kind"`
+ Code string `json:"code"`
+ Message string `json:"message"`
+ Retryable bool `json:"retryable"`
+}
+
+func (e *Error) Error() string {
+ if e == nil {
+ return ""
+ }
+ return e.Message
+}
+
+func ExitCodeForError(err *Error) int {
+ if err == nil {
+ return 0
+ }
+ switch err.Kind {
+ case ErrorKindUsage:
+ return 1
+ case ErrorKindRetryable:
+ return 2
+ case ErrorKindDependency:
+ return 3
+ default:
+ return 1
+ }
+}
+
+type Outputs struct {
+ OriginVideo string `json:"origin_video,omitempty"`
+ OriginAudio string `json:"origin_audio,omitempty"`
+ OriginSRT string `json:"origin_srt,omitempty"`
+ TargetSRT string `json:"target_srt,omitempty"`
+ BilingualSRT string `json:"bilingual_srt,omitempty"`
+ ShortOriginSRT string `json:"short_origin_srt,omitempty"`
+ ShortMixedSRT string `json:"short_origin_mixed_srt,omitempty"`
+ TTSAudio string `json:"tts_audio,omitempty"`
+ VideoWithTTS string `json:"video_with_tts,omitempty"`
+ HorizontalVideo string `json:"horizontal_video,omitempty"`
+ VerticalVideo string `json:"vertical_video,omitempty"`
+ TransferredVideo string `json:"transferred_vertical_video,omitempty"`
+ OriginCover string `json:"origin_cover,omitempty"`
+ GeneratedCover string `json:"generated_cover,omitempty"`
+ FinalCoverPrompt string `json:"cover_prompt,omitempty"`
+ OriginText string `json:"origin_text,omitempty"`
+ TargetText string `json:"target_text,omitempty"`
+}
+
+type Response struct {
+ OK bool `json:"ok"`
+ Stage Stage `json:"stage"`
+ Workdir string `json:"workdir,omitempty"`
+ TaskID string `json:"task_id,omitempty"`
+ CaptionSource string `json:"caption_source,omitempty"`
+ Inputs map[string]string `json:"inputs,omitempty"`
+ Outputs Outputs `json:"outputs,omitempty"`
+ Warnings []string `json:"warnings,omitempty"`
+ FailedIndexes []int `json:"failed_indexes,omitempty"`
+ Error *Error `json:"error,omitempty"`
+ DurationMS int64 `json:"duration_ms,omitempty"`
+}
diff --git a/internal/pipeline/types_test.go b/internal/pipeline/types_test.go
new file mode 100644
index 00000000..5c1e6f4c
--- /dev/null
+++ b/internal/pipeline/types_test.go
@@ -0,0 +1,58 @@
+package pipeline
+
+import (
+ "encoding/json"
+ "testing"
+)
+
+func TestResponseJSONShape(t *testing.T) {
+ resp := Response{
+ OK: true,
+ Stage: StageSubtitle,
+ Workdir: "tasks/demo",
+ TaskID: "demo",
+ Outputs: Outputs{
+ OriginSRT: "tasks/demo/origin_language_srt.srt",
+ TargetSRT: "tasks/demo/target_language_srt.srt",
+ BilingualSRT: "tasks/demo/bilingual_srt.srt",
+ },
+ Warnings: []string{"人工字幕未找到,使用自动字幕"},
+ DurationMS: 123,
+ }
+
+ data, err := json.Marshal(resp)
+ if err != nil {
+ t.Fatalf("Marshal() error = %v", err)
+ }
+
+ var got map[string]any
+ if err := json.Unmarshal(data, &got); err != nil {
+ t.Fatalf("Unmarshal() error = %v", err)
+ }
+ if got["ok"] != true {
+ t.Fatalf("ok = %v, want true", got["ok"])
+ }
+ if got["stage"] != "subtitle" {
+ t.Fatalf("stage = %v, want subtitle", got["stage"])
+ }
+ outputs := got["outputs"].(map[string]any)
+ if outputs["bilingual_srt"] != "tasks/demo/bilingual_srt.srt" {
+ t.Fatalf("bilingual_srt = %v", outputs["bilingual_srt"])
+ }
+}
+
+func TestExitCodeForErrorKind(t *testing.T) {
+ cases := []struct {
+ err *Error
+ want int
+ }{
+ {&Error{Kind: ErrorKindUsage}, 1},
+ {&Error{Kind: ErrorKindRetryable}, 2},
+ {&Error{Kind: ErrorKindDependency}, 3},
+ }
+ for _, tc := range cases {
+ if got := ExitCodeForError(tc.err); got != tc.want {
+ t.Fatalf("ExitCodeForError(%q) = %d, want %d", tc.err.Kind, got, tc.want)
+ }
+ }
+}