@@ -62,44 +62,40 @@ async function resizeAndConvertImage(
62
62
async function getStepFunAnalysis (
63
63
pieces : { cell : ImageData } [ ]
64
64
) : Promise < string [ ] [ ] > {
65
+ // 如果未识别棋子超过10个,直接报错
66
+ if ( pieces . length > 10 ) {
67
+ throw new Error ( '未识别棋子过多(超过10个),请检查图片质量或重新截图' ) ;
68
+ }
69
+
65
70
// 处理所有图片
66
71
const processedImages = await Promise . all (
67
72
pieces . map ( ( piece ) => resizeAndConvertImage ( piece . cell ) )
68
73
) ;
69
74
70
- // 分批处理,每批最多10张图片
71
- const results : string [ ] [ ] = [ ] ;
72
- for ( let i = 0 ; i < processedImages . length ; i += 10 ) {
73
- const batch = processedImages . slice ( i , i + 10 ) ;
74
- const batchFormData = new FormData ( ) ;
75
- batch . forEach ( ( blob ) => batchFormData . append ( 'image' , blob ) ) ;
76
-
77
- const response = await fetch (
78
- 'https://workers.nicesprite.com/api/stepfun/' ,
79
- {
80
- method : 'POST' ,
81
- body : batchFormData ,
82
- }
83
- ) ;
84
-
85
- if ( ! response . ok ) {
86
- throw new Error ( 'StepFun API 请求失败' ) ;
87
- }
75
+ // 构建请求数据
76
+ const formData = new FormData ( ) ;
77
+ processedImages . forEach ( ( blob ) => formData . append ( 'image' , blob ) ) ;
88
78
89
- const data = await response . json ( ) ;
90
- if ( ! data . choices ?. [ 0 ] ?. message ?. content ) {
91
- throw new Error ( 'StepFun API 返回格式错误' ) ;
92
- }
79
+ const response = await fetch ( 'https://workers.nicesprite.com/api/stepfun/' , {
80
+ method : 'POST' ,
81
+ body : formData ,
82
+ } ) ;
93
83
94
- const batchResults = data . choices [ 0 ] . message . content
95
- . split ( / , | , / )
96
- . map ( ( s : string ) => s . trim ( ) )
97
- . filter ( Boolean ) ;
84
+ if ( ! response . ok ) {
85
+ throw new Error ( 'StepFun API 请求失败' ) ;
86
+ }
98
87
99
- results . push ( batchResults ) ;
88
+ const data = await response . json ( ) ;
89
+ if ( ! data . choices ?. [ 0 ] ?. message ?. content ) {
90
+ throw new Error ( 'StepFun API 返回格式错误' ) ;
100
91
}
101
92
102
- return results ;
93
+ const results = data . choices [ 0 ] . message . content
94
+ . split ( / , | , / )
95
+ . map ( ( s : string ) => s . trim ( ) )
96
+ . filter ( Boolean ) ;
97
+
98
+ return [ results ] ;
103
99
}
104
100
105
101
export function useImageProcessing (
0 commit comments