@pippit-dev/cli 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -3
- package/checksums.txt +6 -6
- package/cmd/download_result.go +1 -1
- package/cmd/generate_video/generate_video.go +48 -0
- package/cmd/generate_video/query_result.go +42 -0
- package/cmd/generate_video_test.go +542 -0
- package/cmd/get_thread.go +1 -0
- package/cmd/list_thread_file.go +1 -1
- package/cmd/root.go +27 -2
- package/cmd/short_drama/short_drama.go +52 -9
- package/cmd/short_drama_test.go +19 -19
- package/cmd/update/update.go +8 -8
- package/internal/common/client.go +29 -21
- package/internal/common/download_results.go +20 -14
- package/internal/common/error_log.go +62 -1
- package/internal/common/error_log_test.go +30 -0
- package/internal/common/get_thread.go +28 -14
- package/internal/common/get_thread_test.go +74 -0
- package/{cmd → internal/common}/json.go +3 -2
- package/internal/common/list_thread_file.go +6 -6
- package/internal/common/submit_run.go +29 -0
- package/internal/common/upload_file.go +23 -19
- package/internal/config/config.go +1 -1
- package/internal/generate_video/generate_video.go +199 -0
- package/internal/generate_video/query_result.go +344 -0
- package/internal/generate_video/query_result_test.go +31 -0
- package/internal/short_drama/submit_run.go +8 -28
- package/package.json +1 -1
- package/skills/short-drama/SKILL.md +8 -8
- package/skills/xyq-nest-skill/scripts/download_results.py +3 -1
- package/skills/xyq-nest-skill/scripts/upload_file.py +2 -2
- package/skills/xyq-nest-skill/scripts/xyq_common.py +4 -3
- package/cmd/upload_file.go +0 -50
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
package cmd
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"bytes"
|
|
5
|
+
"io"
|
|
6
|
+
"net/http"
|
|
7
|
+
"net/http/httptest"
|
|
8
|
+
"os"
|
|
9
|
+
"path/filepath"
|
|
10
|
+
"strings"
|
|
11
|
+
"testing"
|
|
12
|
+
|
|
13
|
+
"github.com/bytedance/sonic"
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
func TestGenerateVideo(t *testing.T) {
|
|
17
|
+
assetIDs := []string{"image_asset_1", "image_asset_2", "video_asset_1", "video_asset_2"}
|
|
18
|
+
uploadIndex := 0
|
|
19
|
+
|
|
20
|
+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
21
|
+
if r.Header.Get("Authorization") != "Bearer test-token" {
|
|
22
|
+
t.Fatalf("Authorization = %q, want test bearer token", r.Header.Get("Authorization"))
|
|
23
|
+
}
|
|
24
|
+
switch r.URL.Path {
|
|
25
|
+
case "/api/biz/v1/skill/upload_file":
|
|
26
|
+
if r.Method != http.MethodPost {
|
|
27
|
+
t.Fatalf("upload method = %s, want POST", r.Method)
|
|
28
|
+
}
|
|
29
|
+
if uploadIndex >= len(assetIDs) {
|
|
30
|
+
t.Fatalf("unexpected upload %d", uploadIndex)
|
|
31
|
+
}
|
|
32
|
+
if err := r.ParseMultipartForm(1 << 20); err != nil {
|
|
33
|
+
t.Fatalf("ParseMultipartForm(): %v", err)
|
|
34
|
+
}
|
|
35
|
+
files := r.MultipartForm.File["file"]
|
|
36
|
+
if len(files) != 1 {
|
|
37
|
+
t.Fatalf("file parts = %d, want 1", len(files))
|
|
38
|
+
}
|
|
39
|
+
uploadIndex++
|
|
40
|
+
_, _ = w.Write([]byte(`{"ret":"0","errmsg":"","data":{"pippit_asset_id":"` + assetIDs[uploadIndex-1] + `"}}`))
|
|
41
|
+
case "/api/biz/v1/skill/submit_run":
|
|
42
|
+
if uploadIndex != len(assetIDs) {
|
|
43
|
+
t.Fatalf("submit called after %d uploads, want %d", uploadIndex, len(assetIDs))
|
|
44
|
+
}
|
|
45
|
+
data, err := io.ReadAll(r.Body)
|
|
46
|
+
if err != nil {
|
|
47
|
+
t.Fatalf("read body: %v", err)
|
|
48
|
+
}
|
|
49
|
+
var body map[string]any
|
|
50
|
+
if err := sonic.Unmarshal(data, &body); err != nil {
|
|
51
|
+
t.Fatalf("decode body: %v", err)
|
|
52
|
+
}
|
|
53
|
+
if body["agent_name"] != "pippit_video_part_agent" {
|
|
54
|
+
t.Fatalf("agent_name = %v, want video part agent", body["agent_name"])
|
|
55
|
+
}
|
|
56
|
+
if body["message"] != "做个小猫视频" {
|
|
57
|
+
t.Fatalf("message = %v, want submitted prompt", body["message"])
|
|
58
|
+
}
|
|
59
|
+
param, ok := body["video_part_tool_param"].(map[string]any)
|
|
60
|
+
if !ok {
|
|
61
|
+
t.Fatalf("video_part_tool_param = %#v, want object", body["video_part_tool_param"])
|
|
62
|
+
}
|
|
63
|
+
if param["prompt"] != "做个小猫视频" {
|
|
64
|
+
t.Fatalf("prompt = %v, want submitted prompt", param["prompt"])
|
|
65
|
+
}
|
|
66
|
+
if param["duration_sec"] != float64(5) {
|
|
67
|
+
t.Fatalf("duration_sec = %v, want 5", param["duration_sec"])
|
|
68
|
+
}
|
|
69
|
+
if param["ratio"] != "9:16" || param["model"] != "seedance2.0_vision" || param["resolution"] != "720p" {
|
|
70
|
+
t.Fatalf("param = %#v, want ratio/model/resolution", param)
|
|
71
|
+
}
|
|
72
|
+
assertAssetRefs(t, param["images"], []string{"image_asset_1", "image_asset_2"})
|
|
73
|
+
assertAssetRefs(t, param["videos"], []string{"video_asset_1", "video_asset_2"})
|
|
74
|
+
_, _ = w.Write([]byte(`{"ret":"0","errmsg":"","data":{"run":{"thread_id":"thread_123","run_id":"run_456"},"web_thread_link":"https://xyq.example/thread_123"}}`))
|
|
75
|
+
default:
|
|
76
|
+
t.Fatalf("unexpected path %s", r.URL.Path)
|
|
77
|
+
}
|
|
78
|
+
}))
|
|
79
|
+
defer server.Close()
|
|
80
|
+
|
|
81
|
+
cwd := chdirTemp(t)
|
|
82
|
+
image1 := filepath.Join(cwd, "cat1.jpg")
|
|
83
|
+
image2 := filepath.Join(cwd, "cat2.jpg")
|
|
84
|
+
video1 := filepath.Join(cwd, "video1.mp4")
|
|
85
|
+
video2 := filepath.Join(cwd, "video2.mp4")
|
|
86
|
+
for _, path := range []string{image1, image2, video1, video2} {
|
|
87
|
+
if err := os.WriteFile(path, []byte("media-data"), 0o644); err != nil {
|
|
88
|
+
t.Fatalf("WriteFile(%s): %v", path, err)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
var stdout, stderr bytes.Buffer
|
|
93
|
+
root := newTestRootCommand(t, &stdout, &stderr, server.URL)
|
|
94
|
+
root.SetArgs([]string{
|
|
95
|
+
"generate-video",
|
|
96
|
+
"--prompt", "做个小猫视频",
|
|
97
|
+
"--image", image1,
|
|
98
|
+
"--image", image2,
|
|
99
|
+
"--video", video1,
|
|
100
|
+
"--video", video2,
|
|
101
|
+
"--duration", "5",
|
|
102
|
+
"--ratio", "9:16",
|
|
103
|
+
"--model", "seedance2.0_vision",
|
|
104
|
+
"--resolution", "720p",
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
if err := root.Execute(); err != nil {
|
|
108
|
+
t.Fatalf("Execute() error = %v, stderr = %s", err, stderr.String())
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
got := decodeJSON(t, stdout.Bytes())
|
|
112
|
+
if got["thread_id"] != "thread_123" || got["run_id"] != "run_456" {
|
|
113
|
+
t.Fatalf("output = %#v, want thread and run IDs", got)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
func TestGenerateVideoSkipsSemanticValidation(t *testing.T) {
|
|
118
|
+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
119
|
+
switch r.URL.Path {
|
|
120
|
+
case "/api/biz/v1/skill/submit_run":
|
|
121
|
+
_, _ = w.Write([]byte(`{"ret":"0","errmsg":"","data":{"run":{"thread_id":"thread_123","run_id":"run_456"}}}`))
|
|
122
|
+
default:
|
|
123
|
+
t.Fatalf("unexpected path %s", r.URL.Path)
|
|
124
|
+
}
|
|
125
|
+
}))
|
|
126
|
+
defer server.Close()
|
|
127
|
+
|
|
128
|
+
var stdout, stderr bytes.Buffer
|
|
129
|
+
root := newTestRootCommand(t, &stdout, &stderr, server.URL)
|
|
130
|
+
root.SetArgs([]string{
|
|
131
|
+
"generate-video",
|
|
132
|
+
"--prompt", "x",
|
|
133
|
+
"--duration", "1",
|
|
134
|
+
"--ratio", "1:1",
|
|
135
|
+
"--model", "bad_model",
|
|
136
|
+
"--resolution", "bad_resolution",
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
if err := root.Execute(); err != nil {
|
|
140
|
+
t.Fatalf("Execute() error = %v, stderr = %s", err, stderr.String())
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
func TestGenerateVideoRequiresPrompt(t *testing.T) {
|
|
145
|
+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
146
|
+
t.Fatal("server should not receive request without prompt")
|
|
147
|
+
}))
|
|
148
|
+
defer server.Close()
|
|
149
|
+
|
|
150
|
+
var stdout, stderr bytes.Buffer
|
|
151
|
+
root := newTestRootCommand(t, &stdout, &stderr, server.URL)
|
|
152
|
+
root.SetArgs([]string{
|
|
153
|
+
"generate-video",
|
|
154
|
+
"--duration", "1",
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
err := root.Execute()
|
|
158
|
+
if err == nil {
|
|
159
|
+
t.Fatal("Execute() error = nil, want prompt validation")
|
|
160
|
+
}
|
|
161
|
+
if !strings.Contains(err.Error(), "缺少必填参数 --prompt") {
|
|
162
|
+
t.Fatalf("error = %q, want prompt validation", err)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
func TestGenerateVideoRejectsTooManyImages(t *testing.T) {
|
|
167
|
+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
168
|
+
t.Fatal("server should not receive request when image count is invalid")
|
|
169
|
+
}))
|
|
170
|
+
defer server.Close()
|
|
171
|
+
|
|
172
|
+
var stdout, stderr bytes.Buffer
|
|
173
|
+
root := newTestRootCommand(t, &stdout, &stderr, server.URL)
|
|
174
|
+
args := []string{"generate-video", "--prompt", "x"}
|
|
175
|
+
for _, path := range mediaPaths("image", ".jpg", 10) {
|
|
176
|
+
args = append(args, "--image", path)
|
|
177
|
+
}
|
|
178
|
+
root.SetArgs(args)
|
|
179
|
+
|
|
180
|
+
err := root.Execute()
|
|
181
|
+
if err == nil {
|
|
182
|
+
t.Fatal("Execute() error = nil, want image count validation")
|
|
183
|
+
}
|
|
184
|
+
if !strings.Contains(err.Error(), "参考图片最多支持 9 个,当前传入 10 个") {
|
|
185
|
+
t.Fatalf("error = %q, want image count validation", err)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
func TestGenerateVideoRejectsTooManyVideos(t *testing.T) {
|
|
190
|
+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
191
|
+
t.Fatal("server should not receive request when video count is invalid")
|
|
192
|
+
}))
|
|
193
|
+
defer server.Close()
|
|
194
|
+
|
|
195
|
+
var stdout, stderr bytes.Buffer
|
|
196
|
+
root := newTestRootCommand(t, &stdout, &stderr, server.URL)
|
|
197
|
+
args := []string{"generate-video", "--prompt", "x"}
|
|
198
|
+
for _, path := range mediaPaths("video", ".mp4", 4) {
|
|
199
|
+
args = append(args, "--video", path)
|
|
200
|
+
}
|
|
201
|
+
root.SetArgs(args)
|
|
202
|
+
|
|
203
|
+
err := root.Execute()
|
|
204
|
+
if err == nil {
|
|
205
|
+
t.Fatal("Execute() error = nil, want video count validation")
|
|
206
|
+
}
|
|
207
|
+
if !strings.Contains(err.Error(), "参考视频最多支持 3 个,当前传入 4 个") {
|
|
208
|
+
t.Fatalf("error = %q, want video count validation", err)
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
func TestGenerateVideoSubmitRunErrorIncludesLogID(t *testing.T) {
|
|
213
|
+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
214
|
+
switch r.URL.Path {
|
|
215
|
+
case "/api/biz/v1/skill/submit_run":
|
|
216
|
+
_, _ = w.Write([]byte(`{"ret":"16008","errmsg":"提交Run任务失败","log_id":"log_123"}`))
|
|
217
|
+
default:
|
|
218
|
+
t.Fatalf("unexpected path %s", r.URL.Path)
|
|
219
|
+
}
|
|
220
|
+
}))
|
|
221
|
+
defer server.Close()
|
|
222
|
+
|
|
223
|
+
var stdout, stderr bytes.Buffer
|
|
224
|
+
root := newTestRootCommand(t, &stdout, &stderr, server.URL)
|
|
225
|
+
root.SetArgs([]string{
|
|
226
|
+
"generate-video",
|
|
227
|
+
"--prompt", "x",
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
err := root.Execute()
|
|
231
|
+
if err == nil {
|
|
232
|
+
t.Fatal("Execute() error = nil, want submit_run error")
|
|
233
|
+
}
|
|
234
|
+
if !strings.Contains(err.Error(), "log_id=log_123") {
|
|
235
|
+
t.Fatalf("error = %q, want log_id", err)
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
func TestGenerateVideoErrorLogIncludesLogID(t *testing.T) {
|
|
240
|
+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
241
|
+
switch r.URL.Path {
|
|
242
|
+
case "/api/biz/v1/skill/submit_run":
|
|
243
|
+
_, _ = w.Write([]byte(`{"ret":"16008","errmsg":"提交Run任务失败","log_id":"log_123"}`))
|
|
244
|
+
default:
|
|
245
|
+
t.Fatalf("unexpected path %s", r.URL.Path)
|
|
246
|
+
}
|
|
247
|
+
}))
|
|
248
|
+
defer server.Close()
|
|
249
|
+
|
|
250
|
+
clearDailyErrorLog(t)
|
|
251
|
+
var stdout, stderr bytes.Buffer
|
|
252
|
+
root := newTestRootCommand(t, &stdout, &stderr, server.URL)
|
|
253
|
+
root.SetArgs([]string{
|
|
254
|
+
"generate-video",
|
|
255
|
+
"--prompt", "x",
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
err := root.Execute()
|
|
259
|
+
if err == nil {
|
|
260
|
+
t.Fatal("Execute() error = nil, want submit_run error")
|
|
261
|
+
}
|
|
262
|
+
entries := readDailyErrorLog(t)
|
|
263
|
+
if len(entries) != 1 {
|
|
264
|
+
t.Fatalf("log entries = %d, want 1: %#v", len(entries), entries)
|
|
265
|
+
}
|
|
266
|
+
fields, ok := entries[0]["fields"].(map[string]any)
|
|
267
|
+
if !ok {
|
|
268
|
+
t.Fatalf("fields = %#v, want object", entries[0]["fields"])
|
|
269
|
+
}
|
|
270
|
+
if fields["log_id"] != "log_123" {
|
|
271
|
+
t.Fatalf("log_id = %v, want log_123", fields["log_id"])
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
func TestQueryResultDownloadsCompletedVideo(t *testing.T) {
|
|
276
|
+
var requestedDownload bool
|
|
277
|
+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
278
|
+
if r.Header.Get("Authorization") != "Bearer test-token" {
|
|
279
|
+
t.Fatalf("Authorization = %q, want test bearer token", r.Header.Get("Authorization"))
|
|
280
|
+
}
|
|
281
|
+
switch r.URL.Path {
|
|
282
|
+
case "/api/biz/v1/skill/get_thread":
|
|
283
|
+
if r.Method != http.MethodPost {
|
|
284
|
+
t.Fatalf("get_thread method = %s, want POST", r.Method)
|
|
285
|
+
}
|
|
286
|
+
data, err := io.ReadAll(r.Body)
|
|
287
|
+
if err != nil {
|
|
288
|
+
t.Fatalf("read body: %v", err)
|
|
289
|
+
}
|
|
290
|
+
var body map[string]any
|
|
291
|
+
if err := sonic.Unmarshal(data, &body); err != nil {
|
|
292
|
+
t.Fatalf("decode body: %v", err)
|
|
293
|
+
}
|
|
294
|
+
if body["thread_id"] != "thread_123" {
|
|
295
|
+
t.Fatalf("thread_id = %v, want thread_123", body["thread_id"])
|
|
296
|
+
}
|
|
297
|
+
if body["run_id"] != "run_456" {
|
|
298
|
+
t.Fatalf("run_id = %v, want run_456", body["run_id"])
|
|
299
|
+
}
|
|
300
|
+
if _, ok := body["version"]; ok {
|
|
301
|
+
t.Fatalf("version = %v, want omitted", body["version"])
|
|
302
|
+
}
|
|
303
|
+
_, _ = w.Write([]byte(`{"ret":"0","errmsg":"","data":{"thread":{"thread_id":"thread_123","run_list":[{"run_id":"run_456","state":3,"entry_list":[{"artifact":{"content":[{"sub_type":"biz/x_data_prompt_text","data":"做个小猫视频"},{"sub_type":"biz/x_data_video","data":"{\"video\":{\"download_url\":\"` + serverURL(r) + `/video.mp4\",\"title\":\"cat_video\",\"vid\":\"cat_vid\"}}"}]}}]}]}}}`))
|
|
304
|
+
case "/video.mp4":
|
|
305
|
+
requestedDownload = true
|
|
306
|
+
if r.Method != http.MethodGet {
|
|
307
|
+
t.Fatalf("download method = %s, want GET", r.Method)
|
|
308
|
+
}
|
|
309
|
+
_, _ = w.Write([]byte("video-data"))
|
|
310
|
+
default:
|
|
311
|
+
t.Fatalf("unexpected path %s", r.URL.Path)
|
|
312
|
+
}
|
|
313
|
+
}))
|
|
314
|
+
defer server.Close()
|
|
315
|
+
|
|
316
|
+
downloadDir := filepath.Join(t.TempDir(), "downloads")
|
|
317
|
+
var stdout, stderr bytes.Buffer
|
|
318
|
+
root := newTestRootCommand(t, &stdout, &stderr, server.URL)
|
|
319
|
+
root.SetArgs([]string{
|
|
320
|
+
"query-result",
|
|
321
|
+
"--thread-id", "thread_123",
|
|
322
|
+
"--run-id", "run_456",
|
|
323
|
+
"--download-dir", downloadDir,
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
if err := root.Execute(); err != nil {
|
|
327
|
+
t.Fatalf("Execute() error = %v, stderr = %s", err, stderr.String())
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
outputPath := filepath.Join(downloadDir, "cat_vid.mp4")
|
|
331
|
+
downloadURL := server.URL + "/video.mp4"
|
|
332
|
+
if !requestedDownload {
|
|
333
|
+
t.Fatal("download endpoint was not requested")
|
|
334
|
+
}
|
|
335
|
+
got := decodeJSON(t, stdout.Bytes())
|
|
336
|
+
if got["completed"] != true {
|
|
337
|
+
t.Fatalf("completed = %v, want true", got["completed"])
|
|
338
|
+
}
|
|
339
|
+
if got["thread_id"] != "thread_123" || got["run_id"] != "run_456" {
|
|
340
|
+
t.Fatalf("ids = (%v, %v), want thread/run ids", got["thread_id"], got["run_id"])
|
|
341
|
+
}
|
|
342
|
+
if _, ok := got["state"]; ok {
|
|
343
|
+
t.Fatalf("state should not be returned: %#v", got)
|
|
344
|
+
}
|
|
345
|
+
if got["error_message"] != "" {
|
|
346
|
+
t.Fatalf("error_message = %v, want empty", got["error_message"])
|
|
347
|
+
}
|
|
348
|
+
videos, ok := got["videos"].([]any)
|
|
349
|
+
if !ok || len(videos) != 1 {
|
|
350
|
+
t.Fatalf("videos = %#v, want one video", got["videos"])
|
|
351
|
+
}
|
|
352
|
+
video, ok := videos[0].(map[string]any)
|
|
353
|
+
if !ok {
|
|
354
|
+
t.Fatalf("video = %#v, want object", videos[0])
|
|
355
|
+
}
|
|
356
|
+
if video["download_url"] != downloadURL || video["output_path"] != outputPath {
|
|
357
|
+
t.Fatalf("video = %#v, want download_url/output_path", video)
|
|
358
|
+
}
|
|
359
|
+
for _, unwanted := range []string{"vid", "asset_id", "title"} {
|
|
360
|
+
if _, ok := video[unwanted]; ok {
|
|
361
|
+
t.Fatalf("video = %#v, should not contain %s", video, unwanted)
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
assertFileContent(t, outputPath, "video-data")
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
func TestQueryResultIgnoresVideoDataWithoutVideoSubType(t *testing.T) {
|
|
368
|
+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
369
|
+
switch r.URL.Path {
|
|
370
|
+
case "/api/biz/v1/skill/get_thread":
|
|
371
|
+
_, _ = w.Write([]byte(`{"ret":"0","errmsg":"","data":{"thread":{"thread_id":"thread_123","run_list":[{"run_id":"run_456","state":3,"entry_list":[{"artifact":{"content":[{"data":"{\"video\":{\"download_url\":\"` + serverURL(r) + `/video.mp4\",\"title\":\"cat_video\"}}"}]}}]}]}}}`))
|
|
372
|
+
default:
|
|
373
|
+
t.Fatalf("unexpected path %s", r.URL.Path)
|
|
374
|
+
}
|
|
375
|
+
}))
|
|
376
|
+
defer server.Close()
|
|
377
|
+
|
|
378
|
+
var stdout, stderr bytes.Buffer
|
|
379
|
+
root := newTestRootCommand(t, &stdout, &stderr, server.URL)
|
|
380
|
+
root.SetArgs([]string{
|
|
381
|
+
"query-result",
|
|
382
|
+
"--thread-id", "thread_123",
|
|
383
|
+
"--run-id", "run_456",
|
|
384
|
+
"--download-dir", t.TempDir(),
|
|
385
|
+
})
|
|
386
|
+
|
|
387
|
+
err := root.Execute()
|
|
388
|
+
if err == nil {
|
|
389
|
+
t.Fatal("Execute() error = nil, want no downloadable video error")
|
|
390
|
+
}
|
|
391
|
+
if !strings.Contains(err.Error(), "下载失败:未找到可下载的视频产物") {
|
|
392
|
+
t.Fatalf("error = %q, want no downloadable video error", err)
|
|
393
|
+
}
|
|
394
|
+
if stdout.Len() != 0 {
|
|
395
|
+
t.Fatalf("stdout = %q, want empty", stdout.String())
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
func TestQueryResultErrorLogIncludesLogID(t *testing.T) {
|
|
400
|
+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
401
|
+
switch r.URL.Path {
|
|
402
|
+
case "/api/biz/v1/skill/get_thread":
|
|
403
|
+
_, _ = w.Write([]byte(`{"ret":"5","errmsg":"创作失败","log_id":"log_456"}`))
|
|
404
|
+
default:
|
|
405
|
+
t.Fatalf("unexpected path %s", r.URL.Path)
|
|
406
|
+
}
|
|
407
|
+
}))
|
|
408
|
+
defer server.Close()
|
|
409
|
+
|
|
410
|
+
clearDailyErrorLog(t)
|
|
411
|
+
var stdout, stderr bytes.Buffer
|
|
412
|
+
root := newTestRootCommand(t, &stdout, &stderr, server.URL)
|
|
413
|
+
root.SetArgs([]string{
|
|
414
|
+
"query-result",
|
|
415
|
+
"--thread-id", "thread_123",
|
|
416
|
+
"--run-id", "run_456",
|
|
417
|
+
"--download-dir", t.TempDir(),
|
|
418
|
+
})
|
|
419
|
+
|
|
420
|
+
err := root.Execute()
|
|
421
|
+
if err == nil {
|
|
422
|
+
t.Fatal("Execute() error = nil, want get_thread error")
|
|
423
|
+
}
|
|
424
|
+
entries := readDailyErrorLog(t)
|
|
425
|
+
if len(entries) != 1 {
|
|
426
|
+
t.Fatalf("log entries = %d, want 1: %#v", len(entries), entries)
|
|
427
|
+
}
|
|
428
|
+
fields, ok := entries[0]["fields"].(map[string]any)
|
|
429
|
+
if !ok {
|
|
430
|
+
t.Fatalf("fields = %#v, want object", entries[0]["fields"])
|
|
431
|
+
}
|
|
432
|
+
if fields["log_id"] != "log_456" {
|
|
433
|
+
t.Fatalf("log_id = %v, want log_456", fields["log_id"])
|
|
434
|
+
}
|
|
435
|
+
if fields["thread_id"] != "thread_123" || fields["run_id"] != "run_456" {
|
|
436
|
+
t.Fatalf("fields = %#v, want thread/run ids", fields)
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
func TestQueryResultFailedReturnsErrorMessage(t *testing.T) {
|
|
441
|
+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
442
|
+
switch r.URL.Path {
|
|
443
|
+
case "/api/biz/v1/skill/get_thread":
|
|
444
|
+
_, _ = w.Write([]byte(`{"ret":"0","errmsg":"","data":{"thread":{"thread_id":"thread_123","run_list":[{"run_id":"run_456","state":4,"entry_list":[{"artifact":{"content":[{"sub_type":"biz/x_data_video","data":"{\"error_message\":\"生成失败\",\"error_code\":\"11001\"}"}]}}]}]}}}`))
|
|
445
|
+
default:
|
|
446
|
+
t.Fatalf("unexpected path %s", r.URL.Path)
|
|
447
|
+
}
|
|
448
|
+
}))
|
|
449
|
+
defer server.Close()
|
|
450
|
+
|
|
451
|
+
var stdout, stderr bytes.Buffer
|
|
452
|
+
root := newTestRootCommand(t, &stdout, &stderr, server.URL)
|
|
453
|
+
root.SetArgs([]string{
|
|
454
|
+
"query-result",
|
|
455
|
+
"--thread-id", "thread_123",
|
|
456
|
+
"--run-id", "run_456",
|
|
457
|
+
"--download-dir", t.TempDir(),
|
|
458
|
+
})
|
|
459
|
+
|
|
460
|
+
if err := root.Execute(); err != nil {
|
|
461
|
+
t.Fatalf("Execute() error = %v, stderr = %s", err, stderr.String())
|
|
462
|
+
}
|
|
463
|
+
got := decodeJSON(t, stdout.Bytes())
|
|
464
|
+
if got["completed"] != true {
|
|
465
|
+
t.Fatalf("completed = %v, want true", got["completed"])
|
|
466
|
+
}
|
|
467
|
+
if _, ok := got["state"]; ok {
|
|
468
|
+
t.Fatalf("state should not be returned: %#v", got)
|
|
469
|
+
}
|
|
470
|
+
if got["error_message"] != "生成失败 (error_code=11001)" {
|
|
471
|
+
t.Fatalf("error_message = %v, want failure message", got["error_message"])
|
|
472
|
+
}
|
|
473
|
+
videos, ok := got["videos"].([]any)
|
|
474
|
+
if !ok || len(videos) != 0 {
|
|
475
|
+
t.Fatalf("videos = %#v, want empty", got["videos"])
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
func TestQueryResultPendingDoesNotDownload(t *testing.T) {
|
|
480
|
+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
481
|
+
switch r.URL.Path {
|
|
482
|
+
case "/api/biz/v1/skill/get_thread":
|
|
483
|
+
_, _ = w.Write([]byte(`{"ret":"0","errmsg":"","data":{"thread":{"thread_id":"thread_123","run_list":[{"run_id":"run_456","state":1}]}}}`))
|
|
484
|
+
default:
|
|
485
|
+
t.Fatalf("unexpected path %s", r.URL.Path)
|
|
486
|
+
}
|
|
487
|
+
}))
|
|
488
|
+
defer server.Close()
|
|
489
|
+
|
|
490
|
+
var stdout, stderr bytes.Buffer
|
|
491
|
+
root := newTestRootCommand(t, &stdout, &stderr, server.URL)
|
|
492
|
+
root.SetArgs([]string{
|
|
493
|
+
"query-result",
|
|
494
|
+
"--thread-id", "thread_123",
|
|
495
|
+
"--run-id", "run_456",
|
|
496
|
+
"--download-dir", t.TempDir(),
|
|
497
|
+
})
|
|
498
|
+
|
|
499
|
+
if err := root.Execute(); err != nil {
|
|
500
|
+
t.Fatalf("Execute() error = %v, stderr = %s", err, stderr.String())
|
|
501
|
+
}
|
|
502
|
+
got := decodeJSON(t, stdout.Bytes())
|
|
503
|
+
if got["completed"] != false {
|
|
504
|
+
t.Fatalf("completed = %v, want false", got["completed"])
|
|
505
|
+
}
|
|
506
|
+
if _, ok := got["state"]; ok {
|
|
507
|
+
t.Fatalf("state should not be returned: %#v", got)
|
|
508
|
+
}
|
|
509
|
+
if got["error_message"] != "" {
|
|
510
|
+
t.Fatalf("error_message = %v, want empty", got["error_message"])
|
|
511
|
+
}
|
|
512
|
+
videos, ok := got["videos"].([]any)
|
|
513
|
+
if !ok || len(videos) != 0 {
|
|
514
|
+
t.Fatalf("videos = %#v, want empty", got["videos"])
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
func serverURL(r *http.Request) string {
|
|
519
|
+
return "http://" + r.Host
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
func assertAssetRefs(t *testing.T, got any, want []string) {
|
|
523
|
+
t.Helper()
|
|
524
|
+
items, ok := got.([]any)
|
|
525
|
+
if !ok || len(items) != len(want) {
|
|
526
|
+
t.Fatalf("asset refs = %#v, want %v", got, want)
|
|
527
|
+
}
|
|
528
|
+
for i, item := range items {
|
|
529
|
+
ref, ok := item.(map[string]any)
|
|
530
|
+
if !ok || ref["pippit_asset_id"] != want[i] {
|
|
531
|
+
t.Fatalf("asset refs[%d] = %#v, want %s", i, item, want[i])
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
func mediaPaths(prefix string, ext string, count int) []string {
|
|
537
|
+
paths := make([]string, 0, count)
|
|
538
|
+
for i := 0; i < count; i++ {
|
|
539
|
+
paths = append(paths, prefix+string(rune('a'+i))+ext)
|
|
540
|
+
}
|
|
541
|
+
return paths
|
|
542
|
+
}
|
package/cmd/get_thread.go
CHANGED
|
@@ -27,6 +27,7 @@ func newGetThreadCommand(stdout, stderr io.Writer, runner *common.Runner) *cobra
|
|
|
27
27
|
return fmt.Errorf("--thread-id is required")
|
|
28
28
|
}
|
|
29
29
|
opts.RunID = strings.TrimSpace(opts.RunID)
|
|
30
|
+
opts.Version = common.GetThreadVersionV2
|
|
30
31
|
|
|
31
32
|
result, err := common.GetThread(cmd.Context(), &opts, runner)
|
|
32
33
|
if err != nil {
|
package/cmd/list_thread_file.go
CHANGED
package/cmd/root.go
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
package cmd
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
|
+
"fmt"
|
|
4
5
|
"io"
|
|
5
6
|
"os"
|
|
7
|
+
"strings"
|
|
6
8
|
|
|
7
9
|
// authcmd "github.com/Pippit-dev/pippit-cli/cmd/auth"
|
|
10
|
+
"github.com/Pippit-dev/pippit-cli/cmd/generate_video"
|
|
8
11
|
"github.com/Pippit-dev/pippit-cli/cmd/short_drama"
|
|
9
12
|
updatecmd "github.com/Pippit-dev/pippit-cli/cmd/update"
|
|
10
13
|
"github.com/Pippit-dev/pippit-cli/internal/common"
|
|
@@ -29,7 +32,7 @@ func newRootCommand(stdout, stderr io.Writer, runner *common.Runner) *cobra.Comm
|
|
|
29
32
|
root := &cobra.Command{
|
|
30
33
|
Use: "pippit-tool-cli",
|
|
31
34
|
Short: "Pippit CLI",
|
|
32
|
-
Long: "Pippit CLI submits short-drama workflows, downloads generated assets, and updates the installed CLI package.",
|
|
35
|
+
Long: "Pippit CLI generates videos, submits short-drama workflows, downloads generated assets, and updates the installed CLI package.",
|
|
33
36
|
Version: version.Current(),
|
|
34
37
|
SilenceUsage: true,
|
|
35
38
|
SilenceErrors: true,
|
|
@@ -42,8 +45,30 @@ func newRootCommand(stdout, stderr io.Writer, runner *common.Runner) *cobra.Comm
|
|
|
42
45
|
root.AddCommand(newDownloadResultCommand(stdout, stderr, runner))
|
|
43
46
|
root.AddCommand(newGetThreadCommand(stdout, stderr, runner))
|
|
44
47
|
root.AddCommand(newListThreadFileCommand(stdout, stderr, runner))
|
|
45
|
-
root.AddCommand(
|
|
48
|
+
root.AddCommand(generate_video.NewCommand(stdout, stderr, runner))
|
|
49
|
+
root.AddCommand(generate_video.NewQueryResultCommand(stdout, stderr, runner))
|
|
46
50
|
root.AddCommand(short_drama.NewCommand(stdout, stderr, runner))
|
|
47
51
|
root.AddCommand(updatecmd.NewCommand(stdout, stderr))
|
|
52
|
+
localizeFlagErrors(root)
|
|
48
53
|
return root
|
|
49
54
|
}
|
|
55
|
+
|
|
56
|
+
func localizeFlagErrors(cmd *cobra.Command) {
|
|
57
|
+
cmd.SetFlagErrorFunc(func(_ *cobra.Command, err error) error {
|
|
58
|
+
return localizeFlagError(err)
|
|
59
|
+
})
|
|
60
|
+
for _, child := range cmd.Commands() {
|
|
61
|
+
localizeFlagErrors(child)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
func localizeFlagError(err error) error {
|
|
66
|
+
msg := err.Error()
|
|
67
|
+
if flag, ok := strings.CutPrefix(msg, "unknown flag: "); ok {
|
|
68
|
+
return fmt.Errorf("未知参数: %s", flag)
|
|
69
|
+
}
|
|
70
|
+
if flag, ok := strings.CutPrefix(msg, "flag needs an argument: "); ok {
|
|
71
|
+
return fmt.Errorf("参数 %s 缺少取值", flag)
|
|
72
|
+
}
|
|
73
|
+
return fmt.Errorf("参数解析失败: %s", msg)
|
|
74
|
+
}
|